-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
442 lines (384 loc) · 16.5 KB
/
Copy path__init__.py
File metadata and controls
442 lines (384 loc) · 16.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
"""
自进化插件 — Hermes 标准工具版
8阶段 Loop | 3层评测 | 5维 AND 门控
提供 7 个标准 Hermes 工具:
self_evo_status - 查看进化状态
self_evo_scan - 扫描候选技能
self_evo_approve - 批准进化请求
self_evo_reject - 拒绝进化请求
self_evo_execute - 执行已批准的进化
self_evo_evolve - 直接进化指定技能
self_evo_rollback - 回滚技能到备份
"""
import json
import logging
import sys
from pathlib import Path
from typing import Any, Dict, Optional
logger = logging.getLogger("hermes_plugins.self_evolution")
# ── 确保 self_evolution 包可导入 ──
_SELF_EVO_DIR = str(Path(__file__).parent)
_PARENT_DIR = str(Path(__file__).parent.parent)
if _PARENT_DIR not in sys.path:
sys.path.insert(0, _PARENT_DIR)
if _SELF_EVO_DIR not in sys.path:
sys.path.insert(0, _SELF_EVO_DIR)
# ═══════════════════════════════════════════════════════════════════
# 工具处理器
# ═══════════════════════════════════════════════════════════════════
def _tool_status(args: Dict[str, Any], **_kw) -> str:
"""查看所有 skill 的进化状态。"""
try:
from self_evolution.triggers.auto_trigger import AutoEvolutionManager
aem = AutoEvolutionManager()
states = aem.list_all()
result = []
for s in states:
result.append({
"skill": s.skill_name,
"status": s.status,
"usage_count": s.usage_count,
"evolution_count": s.evolution_count,
"evolved_score": round(s.evolved_score, 3) if s.evolved_score else 0,
})
pending = [s for s in states if s.status == "pending"]
approved = [s for s in states if s.status == "approved"]
return json.dumps({
"total": len(states),
"pending": len(pending),
"approved": len(approved),
"skills": result[:50], # 限制返回数量
}, ensure_ascii=False, indent=2)
except Exception as e:
return json.dumps({"error": str(e)}, ensure_ascii=False)
def _tool_scan(args: Dict[str, Any], **_kw) -> str:
"""扫描所有 skill,筛选达到进化阈值的候选。"""
try:
from self_evolution.triggers.auto_trigger import AutoEvolutionManager
aem = AutoEvolutionManager()
candidates = aem.scan_candidates()
if not candidates:
return json.dumps({
"status": "ok",
"candidates": 0,
"message": "没有需要进化的 skill",
}, ensure_ascii=False)
result = []
for c in candidates:
result.append({
"skill": c.skill_name,
"usage_count": c.usage_count,
"current_score": round(c.evolved_score, 3) if c.evolved_score else 0,
"reason": getattr(c, "reason", ""),
})
return json.dumps({
"status": "ok",
"candidates": len(candidates),
"skills": result,
"message": f"发现 {len(candidates)} 个候选 skill,使用 self_evo_approve 批准",
}, ensure_ascii=False, indent=2)
except Exception as e:
return json.dumps({"error": str(e)}, ensure_ascii=False)
def _tool_approve(args: Dict[str, Any], **_kw) -> str:
"""批准指定 skill 的进化请求。"""
name = args.get("name", "")
if not name:
return json.dumps({"error": "name is required"})
try:
from self_evolution.triggers.auto_trigger import AutoEvolutionManager
aem = AutoEvolutionManager()
if aem.approve(name):
return json.dumps({
"status": "ok",
"message": f"已批准 {name} 的进化请求,使用 self_evo_execute 执行",
}, ensure_ascii=False)
else:
return json.dumps({
"status": "error",
"message": f"{name} 不在待审批状态",
}, ensure_ascii=False)
except Exception as e:
return json.dumps({"error": str(e)}, ensure_ascii=False)
def _tool_reject(args: Dict[str, Any], **_kw) -> str:
"""拒绝指定 skill 的进化请求。"""
name = args.get("name", "")
if not name:
return json.dumps({"error": "name is required"})
try:
from self_evolution.triggers.auto_trigger import AutoEvolutionManager
aem = AutoEvolutionManager()
if aem.reject(name):
return json.dumps({
"status": "ok",
"message": f"已拒绝 {name},进入冷却期",
}, ensure_ascii=False)
else:
return json.dumps({
"status": "error",
"message": f"{name} 不在待审批状态",
}, ensure_ascii=False)
except Exception as e:
return json.dumps({"error": str(e)}, ensure_ascii=False)
def _tool_execute(args: Dict[str, Any], **_kw) -> str:
"""执行所有已批准的进化任务。"""
try:
from self_evolution.triggers.auto_trigger import AutoEvolutionManager
from self_evolution.core.evolution_manager import EvolutionManager
from self_evolution.core.evolution_provider import EvolutionPhase
aem = AutoEvolutionManager()
approved = aem.get_approved()
if not approved:
return json.dumps({
"status": "ok",
"message": "没有已批准的进化任务",
"executed": 0,
}, ensure_ascii=False)
# 初始化进化管理器
manager = EvolutionManager()
target_dir = str(Path.home() / ".hermes")
iterations = args.get("iterations", 5)
fast = args.get("fast", True)
auto_deploy = args.get("auto_deploy", True)
manager.initialize_all(
target_dir,
model=args.get("model", "deepseek-v4-pro"),
iterations=iterations,
use_llm_eval=not fast,
)
results = []
for state in approved:
aem.mark_executing(state.skill_name)
try:
result = manager.evolve_skill(
state.skill_name,
phase=EvolutionPhase.SKILL,
iterations=iterations,
auto_deploy=auto_deploy,
)
if result.error:
results.append({
"skill": state.skill_name,
"status": "failed",
"error": result.error,
})
aem._states[state.skill_name].status = "pending"
else:
results.append({
"skill": state.skill_name,
"status": "success",
"baseline": round(result.baseline_score, 3),
"evolved": round(result.evolved_score, 3),
"improvement": round(result.improvement, 3),
"deployed": result.deployed,
"constraint_passed": result.constraint_passed,
})
aem.mark_done(state.skill_name, result.evolved_score)
except Exception as e:
results.append({
"skill": state.skill_name,
"status": "error",
"error": str(e),
})
aem._save()
success_count = sum(1 for r in results if r["status"] == "success")
return json.dumps({
"status": "ok",
"executed": len(results),
"success": success_count,
"results": results,
}, ensure_ascii=False, indent=2)
except Exception as e:
return json.dumps({"error": str(e)}, ensure_ascii=False)
def _tool_evolve(args: Dict[str, Any], **_kw) -> str:
"""直接进化指定 skill(跳过审批流程)。"""
name = args.get("name", "")
if not name:
return json.dumps({"error": "name is required"})
try:
from self_evolution.core.evolution_manager import EvolutionManager
from self_evolution.core.evolution_provider import EvolutionPhase
manager = EvolutionManager()
target_dir = str(Path.home() / ".hermes")
iterations = args.get("iterations", 5)
fast = args.get("fast", True)
auto_deploy = args.get("auto_deploy", True)
manager.initialize_all(
target_dir,
model=args.get("model", "deepseek-v4-pro"),
iterations=iterations,
use_llm_eval=not fast,
)
result = manager.evolve_skill(
name,
phase=EvolutionPhase.SKILL,
iterations=iterations,
auto_deploy=auto_deploy,
)
if result.error:
return json.dumps({
"status": "error",
"skill": name,
"error": result.error,
}, ensure_ascii=False)
constraint_checks = {}
if result.constraint_details and hasattr(result.constraint_details, 'checks'):
constraint_checks = {
k: v for k, v in result.constraint_details.checks.items()
}
return json.dumps({
"status": "ok",
"skill": name,
"baseline": round(result.baseline_score, 3),
"evolved": round(result.evolved_score, 3),
"improvement": round(result.improvement, 3),
"iterations_used": result.iterations_used,
"holdout_score": round(result.holdout_score, 3) if result.holdout_score else None,
"constraint_passed": result.constraint_passed,
"constraint_checks": constraint_checks,
"deployed": result.deployed,
}, ensure_ascii=False, indent=2)
except Exception as e:
return json.dumps({"error": str(e)}, ensure_ascii=False)
def _tool_rollback(args: Dict[str, Any], **_kw) -> str:
"""回滚指定 skill 到最近备份。"""
name = args.get("name", "")
if not name:
return json.dumps({"error": "name is required"})
try:
from self_evolution.core.evolution_manager import EvolutionManager
from self_evolution.core.evolution_provider import EvolutionPhase
manager = EvolutionManager()
target_dir = str(Path.home() / ".hermes")
manager.initialize_all(target_dir, model="deepseek-v4-pro", iterations=1)
provider = manager.get_provider(EvolutionPhase.SKILL)
if not provider:
return json.dumps({"error": "no SKILL provider available"})
provider.initialize(target_dir)
skill_path = Path(target_dir) / "skills" / name / "SKILL.md"
if provider.handle_rollback(str(skill_path)):
return json.dumps({
"status": "ok",
"message": f"已回滚 {name} 到最近备份",
}, ensure_ascii=False)
else:
return json.dumps({
"status": "error",
"message": f"{name} 回滚失败(可能没有备份)",
}, ensure_ascii=False)
except Exception as e:
return json.dumps({"error": str(e)}, ensure_ascii=False)
# ═══════════════════════════════════════════════════════════════════
# 工具定义
# ═══════════════════════════════════════════════════════════════════
TOOLS = [
{
"name": "self_evo_status",
"description": "查看所有技能的自进化状态(监控/pending/已批准/执行中/完成)。",
"parameters": {"type": "object", "properties": {}},
"handler": _tool_status,
},
{
"name": "self_evo_scan",
"description": "扫描所有技能,筛选达到进化阈值的候选。用于发现需要进化的技能。",
"parameters": {"type": "object", "properties": {}},
"handler": _tool_scan,
},
{
"name": "self_evo_approve",
"description": "批准指定技能的进化请求。扫描后处于 pending 状态的技能可被批准。",
"parameters": {
"type": "object",
"properties": {
"name": {"type": "string", "description": "技能名称"},
},
"required": ["name"],
},
"handler": _tool_approve,
},
{
"name": "self_evo_reject",
"description": "拒绝指定技能的进化请求,该技能将进入冷却期。",
"parameters": {
"type": "object",
"properties": {
"name": {"type": "string", "description": "技能名称"},
},
"required": ["name"],
},
"handler": _tool_reject,
},
{
"name": "self_evo_execute",
"description": "执行所有已批准的进化任务。8阶段优化 Loop,自动部署通过验证的版本。",
"parameters": {
"type": "object",
"properties": {
"iterations": {"type": "integer", "description": "迭代次数(默认 5)", "default": 5},
"fast": {"type": "boolean", "description": "快速模式(默认 true)", "default": True},
"auto_deploy": {"type": "boolean", "description": "自动部署(默认 true)", "default": True},
"model": {"type": "string", "description": "评估模型(默认 deepseek-v4-pro)"},
},
},
"handler": _tool_execute,
},
{
"name": "self_evo_evolve",
"description": "直接进化指定技能(跳过审批)。适合手动触发单个技能的进化。",
"parameters": {
"type": "object",
"properties": {
"name": {"type": "string", "description": "技能名称"},
"iterations": {"type": "integer", "description": "迭代次数(默认 5)", "default": 5},
"fast": {"type": "boolean", "description": "快速模式(默认 true)", "default": True},
"auto_deploy": {"type": "boolean", "description": "自动部署(默认 true)", "default": True},
"model": {"type": "string", "description": "评估模型(默认 deepseek-v4-pro)"},
},
"required": ["name"],
},
"handler": _tool_evolve,
},
{
"name": "self_evo_rollback",
"description": "回滚指定技能到最近备份版本。",
"parameters": {
"type": "object",
"properties": {
"name": {"type": "string", "description": "技能名称"},
},
"required": ["name"],
},
"handler": _tool_rollback,
},
]
# ═══════════════════════════════════════════════════════════════════
# Hermes 插件注册
# ═══════════════════════════════════════════════════════════════════
def register(ctx) -> None:
"""Hermes 插件入口:注册所有工具。"""
registered = 0
for tool_def in TOOLS:
name = tool_def["name"]
handler = tool_def["handler"]
schema = tool_def.get("parameters", {})
try:
ctx.register_tool(
name=name,
handler=handler,
schema=schema,
toolset="self_evolution",
)
registered += 1
except Exception as exc:
logger.warning("SelfEvolution: failed to register tool %s: %s", name, exc)
logger.info(
"SelfEvolution v1.0 registered: %d tools",
registered,
)
def create_manager(optimizer_name: str = "diversify") -> "EvolutionManager":
"""创建并初始化 EvolutionManager,注册默认提供者。"""
from self_evolution.core.evolution_manager import EvolutionManager
from self_evolution.default_provider import DefaultEvolutionProvider
manager = EvolutionManager()
manager.add_provider(DefaultEvolutionProvider(optimizer_name=optimizer_name))
return manager
__all__ = ["register", "TOOLS", "create_manager"]