skyloom 1.12.0 → 1.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/ci.yml +36 -36
- package/README.md +142 -46
- package/config/default.yaml +43 -47
- package/config/models.yaml +155 -155
- package/config/providers.yaml +39 -39
- package/config/skills/api_integrator/SKILL.md +15 -15
- package/config/skills/arch_designer/SKILL.md +13 -13
- package/config/skills/ci_cd_manager/SKILL.md +14 -14
- package/config/skills/code_analysis/SKILL.md +13 -13
- package/config/skills/code_generator/SKILL.md +12 -12
- package/config/skills/code_reviewer/SKILL.md +13 -13
- package/config/skills/content_writer/SKILL.md +14 -14
- package/config/skills/data_transformer/SKILL.md +15 -15
- package/config/skills/document_analysis/SKILL.md +13 -13
- package/config/skills/emotional_companion/SKILL.md +15 -15
- package/config/skills/performance_checker/SKILL.md +14 -14
- package/config/skills/security_auditor/SKILL.md +14 -14
- package/config/skills/self_evolve/SKILL.md +13 -13
- package/config/skills/sys_operator/SKILL.md +15 -15
- package/config/skills/task_planner/SKILL.md +14 -14
- package/config/skills/web_research/SKILL.md +14 -14
- package/config/skills/workflow_designer/SKILL.md +13 -13
- package/dist/agents/dew.js +52 -52
- package/dist/agents/fair.js +84 -84
- package/dist/agents/fog.js +30 -30
- package/dist/agents/frost.js +32 -32
- package/dist/agents/rain.js +32 -32
- package/dist/agents/snow.js +68 -68
- package/dist/cli/main.js +103 -51
- package/dist/cli/main.js.map +1 -1
- package/dist/cli/tui.d.ts.map +1 -1
- package/dist/cli/tui.js +8 -1
- package/dist/cli/tui.js.map +1 -1
- package/dist/core/agent/task.d.ts +58 -0
- package/dist/core/agent/task.d.ts.map +1 -0
- package/dist/core/agent/task.js +83 -0
- package/dist/core/agent/task.js.map +1 -0
- package/dist/core/agent.d.ts +2 -45
- package/dist/core/agent.d.ts.map +1 -1
- package/dist/core/agent.js +61 -145
- package/dist/core/agent.js.map +1 -1
- package/dist/core/agent_helpers.d.ts +10 -0
- package/dist/core/agent_helpers.d.ts.map +1 -1
- package/dist/core/agent_helpers.js +39 -0
- package/dist/core/agent_helpers.js.map +1 -1
- package/dist/core/catalog.d.ts +71 -0
- package/dist/core/catalog.d.ts.map +1 -0
- package/dist/core/catalog.js +176 -0
- package/dist/core/catalog.js.map +1 -0
- package/dist/core/config.d.ts +8 -0
- package/dist/core/config.d.ts.map +1 -1
- package/dist/core/config.js +12 -4
- package/dist/core/config.js.map +1 -1
- package/dist/core/factory.js +16 -16
- package/dist/core/llm.d.ts +7 -0
- package/dist/core/llm.d.ts.map +1 -1
- package/dist/core/llm.js +139 -7
- package/dist/core/llm.js.map +1 -1
- package/dist/core/longdoc.js +5 -5
- package/dist/core/memory.d.ts.map +1 -1
- package/dist/core/memory.js +69 -62
- package/dist/core/memory.js.map +1 -1
- package/dist/core/theme.d.ts +46 -0
- package/dist/core/theme.d.ts.map +1 -0
- package/dist/core/theme.js +42 -0
- package/dist/core/theme.js.map +1 -0
- package/dist/web/server.js +542 -519
- package/dist/web/server.js.map +1 -1
- package/docs/AESTHETIC_DESIGN.md +144 -0
- package/docs/OPTIMIZATION_PLAN.md +178 -0
- package/package.json +60 -60
- package/scripts/install.js +48 -48
- package/scripts/link.js +10 -10
- package/setup.bat +79 -79
- package/skill-test-ty2fOA/test.md +10 -10
- package/src/agents/dew.ts +70 -70
- package/src/agents/fair.ts +102 -102
- package/src/agents/fog.ts +48 -48
- package/src/agents/frost.ts +50 -50
- package/src/agents/rain.ts +50 -50
- package/src/agents/snow.ts +239 -239
- package/src/cli/main.ts +425 -372
- package/src/cli/mode.ts +58 -58
- package/src/cli/tui.ts +272 -269
- package/src/core/agent/task.ts +100 -0
- package/src/core/agent.ts +1446 -1549
- package/src/core/agent_helpers.ts +496 -461
- package/src/core/arbitrate.ts +162 -162
- package/src/core/catalog.ts +178 -0
- package/src/core/checkpoint.ts +94 -94
- package/src/core/config.ts +20 -4
- package/src/core/estimate.ts +104 -104
- package/src/core/evolve.ts +191 -191
- package/src/core/factory.ts +627 -627
- package/src/core/filter.ts +103 -103
- package/src/core/graph.ts +156 -156
- package/src/core/icons.ts +53 -53
- package/src/core/index.ts +37 -37
- package/src/core/learn.ts +146 -146
- package/src/core/llm.ts +108 -5
- package/src/core/longdoc.ts +155 -155
- package/src/core/mcp_server.ts +176 -176
- package/src/core/memory.ts +1178 -1171
- package/src/core/profile.ts +255 -255
- package/src/core/router.ts +124 -124
- package/src/core/sandbox.ts +142 -142
- package/src/core/security.ts +243 -243
- package/src/core/skill.ts +342 -342
- package/src/core/theme.ts +65 -0
- package/src/core/tool_router.ts +193 -193
- package/src/core/vector.ts +152 -152
- package/src/core/workspace.ts +150 -150
- package/src/plugins/loader.ts +66 -66
- package/src/skills/loader.ts +46 -46
- package/src/sql.js.d.ts +29 -29
- package/src/tools/builtin.ts +380 -380
- package/src/tools/computer.ts +269 -269
- package/src/tools/delegate.ts +49 -49
- package/src/web/server.ts +660 -634
- package/src/web/tts.ts +93 -93
- package/tests/agent_helpers.test.ts +48 -0
- package/tests/bus.test.ts +121 -121
- package/tests/catalog.test.ts +86 -0
- package/tests/config.test.ts +41 -0
- package/tests/icons.test.ts +45 -45
- package/tests/memory.test.ts +147 -0
- package/tests/router.test.ts +86 -86
- package/tests/schemas.test.ts +51 -51
- package/tests/semantic.test.ts +83 -83
- package/tests/setup.ts +10 -10
- package/tests/skill.test.ts +172 -172
- package/tests/task.test.ts +60 -0
- package/tests/tool.test.ts +108 -108
- package/tests/tool_router.test.ts +71 -71
- package/vitest.config.ts +17 -17
package/src/core/evolve.ts
CHANGED
|
@@ -1,191 +1,191 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 自我进化模块 — Prompt self-optimization via failure analysis.
|
|
3
|
-
*
|
|
4
|
-
* When an agent repeatedly fails at similar tasks, this module analyzes
|
|
5
|
-
* the failure patterns and suggests targeted improvements to the agent's
|
|
6
|
-
* System Prompt. The agent can then apply these suggestions to improve
|
|
7
|
-
* future performance.
|
|
8
|
-
*
|
|
9
|
-
* Architecture:
|
|
10
|
-
* Failure log → Pattern analysis → Prompt diff → Agent.applyDiff()
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
import * as fs from "fs";
|
|
14
|
-
import * as path from "path";
|
|
15
|
-
import { USER_CONFIG_DIR } from "./config";
|
|
16
|
-
import { getLogger } from "./logger";
|
|
17
|
-
|
|
18
|
-
const log = getLogger("evolve");
|
|
19
|
-
|
|
20
|
-
/* ═══════════════════════════════════════
|
|
21
|
-
Prompt diff — a suggested change
|
|
22
|
-
═══════════════════════════════════════ */
|
|
23
|
-
export interface PromptDiff {
|
|
24
|
-
id: string;
|
|
25
|
-
ts: string;
|
|
26
|
-
agent: string;
|
|
27
|
-
reason: string; // Why this change is needed
|
|
28
|
-
before: string; // Old prompt fragment
|
|
29
|
-
after: string; // New prompt fragment
|
|
30
|
-
applied: boolean;
|
|
31
|
-
improvement?: string; // Measured improvement after applying
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/* ═══════════════════════════════════════
|
|
35
|
-
Failure analysis
|
|
36
|
-
═══════════════════════════════════════ */
|
|
37
|
-
export interface FailureAnalysis {
|
|
38
|
-
agent: string;
|
|
39
|
-
period: string;
|
|
40
|
-
totalCalls: number;
|
|
41
|
-
failureCount: number;
|
|
42
|
-
topFailures: Array<{ pattern: string; count: number }>;
|
|
43
|
-
suggestedDiffs: PromptDiff[];
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const evolveDir = path.join(USER_CONFIG_DIR, "evolve");
|
|
47
|
-
function ensureDir() { if (!fs.existsSync(evolveDir)) fs.mkdirSync(evolveDir, { recursive: true }); }
|
|
48
|
-
|
|
49
|
-
/** Analyze recent failures from the learning module and suggest prompt improvements. */
|
|
50
|
-
export function analyzeFailures(
|
|
51
|
-
agent: string,
|
|
52
|
-
experiences: Array<{ pattern: string; solution: string; frequency: number; lastSeen: string }>,
|
|
53
|
-
systemPrompt: string
|
|
54
|
-
): FailureAnalysis {
|
|
55
|
-
const recent = experiences.filter(e => {
|
|
56
|
-
try { return new Date(e.lastSeen).getTime() > Date.now() - 7 * 86400000; }
|
|
57
|
-
catch { return false; }
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
const topFailures = recent
|
|
61
|
-
.sort((a, b) => b.frequency - a.frequency)
|
|
62
|
-
.slice(0, 5)
|
|
63
|
-
.map(e => ({ pattern: e.pattern, count: e.frequency }));
|
|
64
|
-
|
|
65
|
-
const suggestedDiffs: PromptDiff[] = [];
|
|
66
|
-
|
|
67
|
-
// Rule-based suggestions from failure patterns
|
|
68
|
-
for (const f of topFailures) {
|
|
69
|
-
const lower = f.pattern.toLowerCase();
|
|
70
|
-
|
|
71
|
-
// Search storm → add search budget rule
|
|
72
|
-
if ((lower.includes("search") || lower.includes("web_search")) && f.count >= 3) {
|
|
73
|
-
const rule = `- 搜索不超过 5 轮。5 轮后直接基于已有信息综合回答。`;
|
|
74
|
-
if (!systemPrompt.includes("搜索不超过")) {
|
|
75
|
-
suggestedDiffs.push({
|
|
76
|
-
id: Math.random().toString(36).slice(2, 8),
|
|
77
|
-
ts: new Date().toISOString(), agent,
|
|
78
|
-
reason: `搜索风暴 (${f.count} 次重复搜索)`,
|
|
79
|
-
before: "", after: rule, applied: false,
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// Empty response → add deliverable checklist
|
|
85
|
-
if ((lower.includes("empty") || lower.includes("placeholder") || lower.includes("完成了")) && f.count >= 2) {
|
|
86
|
-
const rule = `- 完成任务后,必须输出实际产物(代码/文件路径/数据),禁止只说"完成了"而无产出。`;
|
|
87
|
-
if (!systemPrompt.includes("必须输出实际产物")) {
|
|
88
|
-
suggestedDiffs.push({
|
|
89
|
-
id: Math.random().toString(36).slice(2, 8),
|
|
90
|
-
ts: new Date().toISOString(), agent,
|
|
91
|
-
reason: `空响应/占位 (${f.count} 次)`,
|
|
92
|
-
before: "", after: rule, applied: false,
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
// Tool not found → add tool discovery to prompt
|
|
98
|
-
if (lower.includes("does not exist") || lower.includes("tool") && lower.includes("not found")) {
|
|
99
|
-
const rule = `- 使用不熟悉的工具前先调 list_skills 查看可用工具列表。`;
|
|
100
|
-
if (!systemPrompt.includes("list_skills")) {
|
|
101
|
-
suggestedDiffs.push({
|
|
102
|
-
id: Math.random().toString(36).slice(2, 8),
|
|
103
|
-
ts: new Date().toISOString(), agent,
|
|
104
|
-
reason: `工具不存在 (${f.count} 次)`,
|
|
105
|
-
before: "", after: rule, applied: false,
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
// File not found → add path verification rule
|
|
111
|
-
if (lower.includes("file not found") || lower.includes("directory not found")) {
|
|
112
|
-
const rule = `- 文件操作前先用 list_directory 或 read_file 确认路径存在。`;
|
|
113
|
-
if (!systemPrompt.includes("确认路径存在")) {
|
|
114
|
-
suggestedDiffs.push({
|
|
115
|
-
id: Math.random().toString(36).slice(2, 8),
|
|
116
|
-
ts: new Date().toISOString(), agent,
|
|
117
|
-
reason: `文件路径错误 (${f.count} 次)`,
|
|
118
|
-
before: "", after: rule, applied: false,
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
// General rule: if failure rate > 20%, suggest self-review
|
|
125
|
-
const recentExperiences = recent.filter(e => {
|
|
126
|
-
try { return new Date(e.lastSeen).getTime() > Date.now() - 3 * 86400000; }
|
|
127
|
-
catch { return false; }
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
// Deduplicate suggestions
|
|
131
|
-
const seen = new Set<string>();
|
|
132
|
-
const uniqueDiffs = suggestedDiffs.filter(d => {
|
|
133
|
-
const key = d.after.slice(0, 30);
|
|
134
|
-
if (seen.has(key)) return false;
|
|
135
|
-
seen.add(key); return true;
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
// Max 3 suggestions per analysis
|
|
139
|
-
return {
|
|
140
|
-
agent, period: "last 7 days",
|
|
141
|
-
totalCalls: 0, failureCount: 0,
|
|
142
|
-
topFailures,
|
|
143
|
-
suggestedDiffs: uniqueDiffs.slice(0, 3),
|
|
144
|
-
};
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/* ═══════════════════════════════════════
|
|
148
|
-
Apply prompt diff to agent
|
|
149
|
-
═══════════════════════════════════════ */
|
|
150
|
-
export function applyPromptDiff(agent: any, diff: PromptDiff): boolean {
|
|
151
|
-
try {
|
|
152
|
-
const currentPrompt = agent.systemPrompt;
|
|
153
|
-
if (!diff.after || currentPrompt.includes(diff.after.slice(0, 20))) return false;
|
|
154
|
-
|
|
155
|
-
// Append the new rule after "## 行为守则" or "## Behavior" section
|
|
156
|
-
const marker = currentPrompt.includes("行为守则") ? "## 行为守则" : "## Behavior";
|
|
157
|
-
const idx = currentPrompt.indexOf(marker);
|
|
158
|
-
if (idx < 0) { agent.systemPrompt += "\n" + diff.after; }
|
|
159
|
-
else {
|
|
160
|
-
const insertPoint = currentPrompt.indexOf("\n", currentPrompt.indexOf("\n-", idx) + 1);
|
|
161
|
-
agent.systemPrompt = currentPrompt.slice(0, insertPoint) + "\n" + diff.after + "\n" + currentPrompt.slice(insertPoint);
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
diff.applied = true;
|
|
165
|
-
diff.improvement = "pending evaluation";
|
|
166
|
-
|
|
167
|
-
// Persist the diff
|
|
168
|
-
ensureDir();
|
|
169
|
-
const file = path.join(evolveDir, `${diff.agent}_diffs.jsonl`);
|
|
170
|
-
fs.appendFileSync(file, JSON.stringify(diff) + "\n");
|
|
171
|
-
|
|
172
|
-
agent.rebuildSystemPrompt();
|
|
173
|
-
return true;
|
|
174
|
-
} catch (e) {
|
|
175
|
-
log.warn("apply_prompt_diff_failed", { agent: diff.agent, error: String(e) });
|
|
176
|
-
return false;
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
/** Get all applied diffs for an agent. */
|
|
181
|
-
export function getAppliedDiffs(agent: string): PromptDiff[] {
|
|
182
|
-
const diffs: PromptDiff[] = [];
|
|
183
|
-
try {
|
|
184
|
-
const file = path.join(evolveDir, `${agent}_diffs.jsonl`);
|
|
185
|
-
if (fs.existsSync(file)) {
|
|
186
|
-
const lines = fs.readFileSync(file, "utf-8").split("\n").filter(Boolean);
|
|
187
|
-
for (const line of lines) { try { diffs.push(JSON.parse(line)); } catch { } }
|
|
188
|
-
}
|
|
189
|
-
} catch { /* ignore */ }
|
|
190
|
-
return diffs;
|
|
191
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* 自我进化模块 — Prompt self-optimization via failure analysis.
|
|
3
|
+
*
|
|
4
|
+
* When an agent repeatedly fails at similar tasks, this module analyzes
|
|
5
|
+
* the failure patterns and suggests targeted improvements to the agent's
|
|
6
|
+
* System Prompt. The agent can then apply these suggestions to improve
|
|
7
|
+
* future performance.
|
|
8
|
+
*
|
|
9
|
+
* Architecture:
|
|
10
|
+
* Failure log → Pattern analysis → Prompt diff → Agent.applyDiff()
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import * as fs from "fs";
|
|
14
|
+
import * as path from "path";
|
|
15
|
+
import { USER_CONFIG_DIR } from "./config";
|
|
16
|
+
import { getLogger } from "./logger";
|
|
17
|
+
|
|
18
|
+
const log = getLogger("evolve");
|
|
19
|
+
|
|
20
|
+
/* ═══════════════════════════════════════
|
|
21
|
+
Prompt diff — a suggested change
|
|
22
|
+
═══════════════════════════════════════ */
|
|
23
|
+
export interface PromptDiff {
|
|
24
|
+
id: string;
|
|
25
|
+
ts: string;
|
|
26
|
+
agent: string;
|
|
27
|
+
reason: string; // Why this change is needed
|
|
28
|
+
before: string; // Old prompt fragment
|
|
29
|
+
after: string; // New prompt fragment
|
|
30
|
+
applied: boolean;
|
|
31
|
+
improvement?: string; // Measured improvement after applying
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/* ═══════════════════════════════════════
|
|
35
|
+
Failure analysis
|
|
36
|
+
═══════════════════════════════════════ */
|
|
37
|
+
export interface FailureAnalysis {
|
|
38
|
+
agent: string;
|
|
39
|
+
period: string;
|
|
40
|
+
totalCalls: number;
|
|
41
|
+
failureCount: number;
|
|
42
|
+
topFailures: Array<{ pattern: string; count: number }>;
|
|
43
|
+
suggestedDiffs: PromptDiff[];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const evolveDir = path.join(USER_CONFIG_DIR, "evolve");
|
|
47
|
+
function ensureDir() { if (!fs.existsSync(evolveDir)) fs.mkdirSync(evolveDir, { recursive: true }); }
|
|
48
|
+
|
|
49
|
+
/** Analyze recent failures from the learning module and suggest prompt improvements. */
|
|
50
|
+
export function analyzeFailures(
|
|
51
|
+
agent: string,
|
|
52
|
+
experiences: Array<{ pattern: string; solution: string; frequency: number; lastSeen: string }>,
|
|
53
|
+
systemPrompt: string
|
|
54
|
+
): FailureAnalysis {
|
|
55
|
+
const recent = experiences.filter(e => {
|
|
56
|
+
try { return new Date(e.lastSeen).getTime() > Date.now() - 7 * 86400000; }
|
|
57
|
+
catch { return false; }
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
const topFailures = recent
|
|
61
|
+
.sort((a, b) => b.frequency - a.frequency)
|
|
62
|
+
.slice(0, 5)
|
|
63
|
+
.map(e => ({ pattern: e.pattern, count: e.frequency }));
|
|
64
|
+
|
|
65
|
+
const suggestedDiffs: PromptDiff[] = [];
|
|
66
|
+
|
|
67
|
+
// Rule-based suggestions from failure patterns
|
|
68
|
+
for (const f of topFailures) {
|
|
69
|
+
const lower = f.pattern.toLowerCase();
|
|
70
|
+
|
|
71
|
+
// Search storm → add search budget rule
|
|
72
|
+
if ((lower.includes("search") || lower.includes("web_search")) && f.count >= 3) {
|
|
73
|
+
const rule = `- 搜索不超过 5 轮。5 轮后直接基于已有信息综合回答。`;
|
|
74
|
+
if (!systemPrompt.includes("搜索不超过")) {
|
|
75
|
+
suggestedDiffs.push({
|
|
76
|
+
id: Math.random().toString(36).slice(2, 8),
|
|
77
|
+
ts: new Date().toISOString(), agent,
|
|
78
|
+
reason: `搜索风暴 (${f.count} 次重复搜索)`,
|
|
79
|
+
before: "", after: rule, applied: false,
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Empty response → add deliverable checklist
|
|
85
|
+
if ((lower.includes("empty") || lower.includes("placeholder") || lower.includes("完成了")) && f.count >= 2) {
|
|
86
|
+
const rule = `- 完成任务后,必须输出实际产物(代码/文件路径/数据),禁止只说"完成了"而无产出。`;
|
|
87
|
+
if (!systemPrompt.includes("必须输出实际产物")) {
|
|
88
|
+
suggestedDiffs.push({
|
|
89
|
+
id: Math.random().toString(36).slice(2, 8),
|
|
90
|
+
ts: new Date().toISOString(), agent,
|
|
91
|
+
reason: `空响应/占位 (${f.count} 次)`,
|
|
92
|
+
before: "", after: rule, applied: false,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Tool not found → add tool discovery to prompt
|
|
98
|
+
if (lower.includes("does not exist") || lower.includes("tool") && lower.includes("not found")) {
|
|
99
|
+
const rule = `- 使用不熟悉的工具前先调 list_skills 查看可用工具列表。`;
|
|
100
|
+
if (!systemPrompt.includes("list_skills")) {
|
|
101
|
+
suggestedDiffs.push({
|
|
102
|
+
id: Math.random().toString(36).slice(2, 8),
|
|
103
|
+
ts: new Date().toISOString(), agent,
|
|
104
|
+
reason: `工具不存在 (${f.count} 次)`,
|
|
105
|
+
before: "", after: rule, applied: false,
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// File not found → add path verification rule
|
|
111
|
+
if (lower.includes("file not found") || lower.includes("directory not found")) {
|
|
112
|
+
const rule = `- 文件操作前先用 list_directory 或 read_file 确认路径存在。`;
|
|
113
|
+
if (!systemPrompt.includes("确认路径存在")) {
|
|
114
|
+
suggestedDiffs.push({
|
|
115
|
+
id: Math.random().toString(36).slice(2, 8),
|
|
116
|
+
ts: new Date().toISOString(), agent,
|
|
117
|
+
reason: `文件路径错误 (${f.count} 次)`,
|
|
118
|
+
before: "", after: rule, applied: false,
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// General rule: if failure rate > 20%, suggest self-review
|
|
125
|
+
const recentExperiences = recent.filter(e => {
|
|
126
|
+
try { return new Date(e.lastSeen).getTime() > Date.now() - 3 * 86400000; }
|
|
127
|
+
catch { return false; }
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
// Deduplicate suggestions
|
|
131
|
+
const seen = new Set<string>();
|
|
132
|
+
const uniqueDiffs = suggestedDiffs.filter(d => {
|
|
133
|
+
const key = d.after.slice(0, 30);
|
|
134
|
+
if (seen.has(key)) return false;
|
|
135
|
+
seen.add(key); return true;
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
// Max 3 suggestions per analysis
|
|
139
|
+
return {
|
|
140
|
+
agent, period: "last 7 days",
|
|
141
|
+
totalCalls: 0, failureCount: 0,
|
|
142
|
+
topFailures,
|
|
143
|
+
suggestedDiffs: uniqueDiffs.slice(0, 3),
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/* ═══════════════════════════════════════
|
|
148
|
+
Apply prompt diff to agent
|
|
149
|
+
═══════════════════════════════════════ */
|
|
150
|
+
export function applyPromptDiff(agent: any, diff: PromptDiff): boolean {
|
|
151
|
+
try {
|
|
152
|
+
const currentPrompt = agent.systemPrompt;
|
|
153
|
+
if (!diff.after || currentPrompt.includes(diff.after.slice(0, 20))) return false;
|
|
154
|
+
|
|
155
|
+
// Append the new rule after "## 行为守则" or "## Behavior" section
|
|
156
|
+
const marker = currentPrompt.includes("行为守则") ? "## 行为守则" : "## Behavior";
|
|
157
|
+
const idx = currentPrompt.indexOf(marker);
|
|
158
|
+
if (idx < 0) { agent.systemPrompt += "\n" + diff.after; }
|
|
159
|
+
else {
|
|
160
|
+
const insertPoint = currentPrompt.indexOf("\n", currentPrompt.indexOf("\n-", idx) + 1);
|
|
161
|
+
agent.systemPrompt = currentPrompt.slice(0, insertPoint) + "\n" + diff.after + "\n" + currentPrompt.slice(insertPoint);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
diff.applied = true;
|
|
165
|
+
diff.improvement = "pending evaluation";
|
|
166
|
+
|
|
167
|
+
// Persist the diff
|
|
168
|
+
ensureDir();
|
|
169
|
+
const file = path.join(evolveDir, `${diff.agent}_diffs.jsonl`);
|
|
170
|
+
fs.appendFileSync(file, JSON.stringify(diff) + "\n");
|
|
171
|
+
|
|
172
|
+
agent.rebuildSystemPrompt();
|
|
173
|
+
return true;
|
|
174
|
+
} catch (e) {
|
|
175
|
+
log.warn("apply_prompt_diff_failed", { agent: diff.agent, error: String(e) });
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** Get all applied diffs for an agent. */
|
|
181
|
+
export function getAppliedDiffs(agent: string): PromptDiff[] {
|
|
182
|
+
const diffs: PromptDiff[] = [];
|
|
183
|
+
try {
|
|
184
|
+
const file = path.join(evolveDir, `${agent}_diffs.jsonl`);
|
|
185
|
+
if (fs.existsSync(file)) {
|
|
186
|
+
const lines = fs.readFileSync(file, "utf-8").split("\n").filter(Boolean);
|
|
187
|
+
for (const line of lines) { try { diffs.push(JSON.parse(line)); } catch { } }
|
|
188
|
+
}
|
|
189
|
+
} catch { /* ignore */ }
|
|
190
|
+
return diffs;
|
|
191
|
+
}
|