feihong-code 0.5.1 → 0.6.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/CHANGELOG.md +465 -405
- package/README.md +59 -0
- package/dist/agent/code-writer.js +6 -3
- package/dist/agent/code-writer.js.map +1 -1
- package/dist/agent/experience.js +105 -70
- package/dist/agent/experience.js.map +1 -1
- package/dist/agent/orchestrator.js +103 -21
- package/dist/agent/orchestrator.js.map +1 -1
- package/dist/agent/planner.js +30 -7
- package/dist/agent/planner.js.map +1 -1
- package/dist/agent/prompts.js +9 -0
- package/dist/agent/prompts.js.map +1 -1
- package/dist/agent/self-heal.js +43 -13
- package/dist/agent/self-heal.js.map +1 -1
- package/dist/cli/run.js +13 -14
- package/dist/cli/run.js.map +1 -1
- package/dist/cli/version.js +1 -1
- package/dist/enterprise/audit.js +70 -11
- package/dist/enterprise/audit.js.map +1 -1
- package/dist/models/model-router.js +83 -23
- package/dist/models/model-router.js.map +1 -1
- package/dist/models/model.dto.js +25 -3
- package/dist/models/model.dto.js.map +1 -1
- package/dist/models/providers/ollama.provider.js +12 -0
- package/dist/models/providers/ollama.provider.js.map +1 -1
- package/dist/models/providers/openai-compatible.provider.js +14 -1
- package/dist/models/providers/openai-compatible.provider.js.map +1 -1
- package/dist/self-evolve/hook.js +2 -2
- package/dist/self-evolve/hook.js.map +1 -1
- package/dist/self-evolve/hook.ts +80 -0
- package/dist/self-evolve/manager.d.ts +8 -0
- package/dist/self-evolve/manager.js +401 -0
- package/dist/shared/errors.js +6 -2
- package/dist/shared/errors.js.map +1 -1
- package/dist/shared/secure-store.js +117 -0
- package/dist/shared/secure-store.js.map +1 -0
- package/dist/tools/mcp/mcp-client.js +35 -13
- package/dist/tools/mcp/mcp-client.js.map +1 -1
- package/dist/tools/shell/exec.js +47 -3
- package/dist/tools/shell/exec.js.map +1 -1
- package/dist/tools/shell/run-shell.tool.js +33 -6
- package/dist/tools/shell/run-shell.tool.js.map +1 -1
- package/dist/tools/tool.registry.js +32 -0
- package/dist/tools/tool.registry.js.map +1 -1
- package/dist/web/auth.js +143 -7
- package/dist/web/auth.js.map +1 -1
- package/dist/web/public/css/style.css +1546 -0
- package/dist/web/public/index.html +806 -3253
- package/dist/web/public/js/api.js +309 -0
- package/dist/web/public/js/app.js +1472 -0
- package/dist/web/public/js/ui.js +832 -0
- package/dist/web/public/js/utils.js +170 -0
- package/dist/web/server.js +224 -77
- package/dist/web/server.js.map +1 -1
- package/dist/web/task-queue.js +128 -10
- package/dist/web/task-queue.js.map +1 -1
- package/docs/App/344/275/277/347/224/250/350/257/264/346/230/216/344/271/246.md +299 -0
- package/docs/App/346/212/200/346/234/257/350/257/264/346/230/216/344/271/246.md +554 -0
- package/docs/SELF-EVOLVE-GUIDE.md +313 -0
- package/docs/error-codes.md +198 -0
- package/docs/screenshots/cli-demo.png +0 -0
- package/docs/screenshots/feature-comparison.png +0 -0
- package/docs/screenshots/web-console.png +0 -0
- package/docs//351/241/265/351/235/242/345/212/237/350/203/275/345/244/215/347/233/230/344/270/216/345/206/222/347/203/237/346/265/213/350/257/225/346/212/245/345/221/212.html +117 -0
- package/package.json +9 -15
- package/tool-schema.json +198 -127
- package/dist/web/public/index.html.tmp +0 -3117
- package/dist/web/public/index_new.html +0 -3196
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
declare class SelfEvolveManager {
|
|
2
|
+
init(): void;
|
|
3
|
+
recordFailure(errorType: string, error: any, attemptedSolutions?: string[]): any;
|
|
4
|
+
searchSolution(errorType: string, errorMessage: string): any[];
|
|
5
|
+
generateDailyReport(): any;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export { SelfEvolveManager };
|
|
@@ -0,0 +1,401 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Self-Evolve Manager - 自我迭代升级管理器
|
|
4
|
+
* 负责记录失败、创建技能、定期复盘
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const fs = require('fs');
|
|
8
|
+
const path = require('path');
|
|
9
|
+
const { v4: uuidv4 } = require('uuid');
|
|
10
|
+
|
|
11
|
+
// 确保uuid已安装或实现简单的UUID
|
|
12
|
+
function generateId() {
|
|
13
|
+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
|
|
14
|
+
const r = Math.random() * 16 | 0;
|
|
15
|
+
const v = c === 'x' ? r : (r & 0x3 | 0x8);
|
|
16
|
+
return v.toString(16);
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
class SelfEvolveManager {
|
|
21
|
+
constructor() {
|
|
22
|
+
this.baseDir = path.join(process.env.HOME || process.env.USERPROFILE || '.', '.feihong-code', 'self-evolve');
|
|
23
|
+
this.failuresFile = path.join(this.baseDir, 'failures.json');
|
|
24
|
+
this.skillsIndexFile = path.join(this.baseDir, 'skills-index.json');
|
|
25
|
+
this.historyFile = path.join(this.baseDir, 'history.json');
|
|
26
|
+
|
|
27
|
+
this.ensureDirs();
|
|
28
|
+
this.loadData();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
ensureDirs() {
|
|
32
|
+
if (!fs.existsSync(this.baseDir)) {
|
|
33
|
+
fs.mkdirSync(this.baseDir, { recursive: true });
|
|
34
|
+
}
|
|
35
|
+
// 确保用户技能目录也存在
|
|
36
|
+
const userSkillsDir = path.join(process.env.HOME || process.env.USERPROFILE || '.', '.feihong-code', 'skills');
|
|
37
|
+
if (!fs.existsSync(userSkillsDir)) {
|
|
38
|
+
fs.mkdirSync(userSkillsDir, { recursive: true });
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
loadData() {
|
|
43
|
+
this.failures = this.loadJSON(this.failuresFile, []);
|
|
44
|
+
this.skillsIndex = this.loadJSON(this.skillsIndexFile, []);
|
|
45
|
+
this.history = this.loadJSON(this.historyFile, []);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
loadJSON(file, defaultValue) {
|
|
49
|
+
try {
|
|
50
|
+
if (fs.existsSync(file)) {
|
|
51
|
+
const content = fs.readFileSync(file, 'utf8');
|
|
52
|
+
return JSON.parse(content);
|
|
53
|
+
}
|
|
54
|
+
} catch (e) {
|
|
55
|
+
console.error(`加载 ${file} 失败:`, e.message);
|
|
56
|
+
}
|
|
57
|
+
return defaultValue;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
saveData() {
|
|
61
|
+
this.saveJSON(this.failuresFile, this.failures);
|
|
62
|
+
this.saveJSON(this.skillsIndexFile, this.skillsIndex);
|
|
63
|
+
this.saveJSON(this.historyFile, this.history);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
saveJSON(file, data) {
|
|
67
|
+
try {
|
|
68
|
+
fs.writeFileSync(file, JSON.stringify(data, null, 2), 'utf8');
|
|
69
|
+
} catch (e) {
|
|
70
|
+
console.error(`保存 ${file} 失败:`, e.message);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* 记录任务失败
|
|
76
|
+
*/
|
|
77
|
+
recordFailure(task, error, attemptedSolutions = [], rootCause = null) {
|
|
78
|
+
const failure = {
|
|
79
|
+
id: generateId(),
|
|
80
|
+
timestamp: new Date().toISOString(),
|
|
81
|
+
task: task,
|
|
82
|
+
error_type: this.categorizeError(error),
|
|
83
|
+
error_message: typeof error === 'string' ? error : (error?.message || String(error)),
|
|
84
|
+
attempted_solutions: attemptedSolutions,
|
|
85
|
+
root_cause: rootCause || '待分析',
|
|
86
|
+
solution: null,
|
|
87
|
+
created_skill: false,
|
|
88
|
+
skill_name: null,
|
|
89
|
+
status: 'pending'
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
this.failures.push(failure);
|
|
93
|
+
this.history.push({
|
|
94
|
+
type: 'failure',
|
|
95
|
+
timestamp: failure.timestamp,
|
|
96
|
+
failure_id: failure.id
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
this.saveData();
|
|
100
|
+
console.log(`[自我迭代] 记录失败任务 #${failure.id.slice(0, 8)}`);
|
|
101
|
+
return failure;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* 错误分类
|
|
106
|
+
*/
|
|
107
|
+
categorizeError(error) {
|
|
108
|
+
const msg = typeof error === 'string' ? error.toLowerCase() : '';
|
|
109
|
+
|
|
110
|
+
if (msg.includes('error ts') || msg.includes('syntax error') || msg.includes('编译')) {
|
|
111
|
+
return 'compile-error';
|
|
112
|
+
}
|
|
113
|
+
if (msg.includes('typeerror') || msg.includes('referenceerror')) {
|
|
114
|
+
return 'runtime-error';
|
|
115
|
+
}
|
|
116
|
+
if (msg.includes('path traversal') || msg.includes('outside workspace')) {
|
|
117
|
+
return 'path-error';
|
|
118
|
+
}
|
|
119
|
+
if (msg.includes('timeout') || msg.includes('etimedout')) {
|
|
120
|
+
return 'timeout';
|
|
121
|
+
}
|
|
122
|
+
if (msg.includes('permission denied') || msg.includes('eacces')) {
|
|
123
|
+
return 'permission-error';
|
|
124
|
+
}
|
|
125
|
+
if (msg.includes('rate limit') || msg.includes('429') || msg.includes('5xx')) {
|
|
126
|
+
return 'api-error';
|
|
127
|
+
}
|
|
128
|
+
return 'unknown';
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* 搜索已知解决方案
|
|
133
|
+
*/
|
|
134
|
+
searchSolution(errorType, errorMessage) {
|
|
135
|
+
// 先按类型精确匹配
|
|
136
|
+
let matches = this.skillsIndex.filter(s => s.error_pattern === errorType);
|
|
137
|
+
|
|
138
|
+
// 再按关键词模糊匹配
|
|
139
|
+
if (matches.length === 0) {
|
|
140
|
+
const keywords = errorMessage.toLowerCase().split(/\s+/).filter(k => k.length > 3);
|
|
141
|
+
for (const keyword of keywords) {
|
|
142
|
+
matches = this.skillsIndex.filter(s =>
|
|
143
|
+
s.triggers && s.triggers.some(t => t.includes(keyword))
|
|
144
|
+
);
|
|
145
|
+
if (matches.length > 0) break;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return matches;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* 标记问题已解决
|
|
154
|
+
*/
|
|
155
|
+
markResolved(failureId, solution, skillName = null) {
|
|
156
|
+
const failure = this.failures.find(f => f.id === failureId);
|
|
157
|
+
if (!failure) return null;
|
|
158
|
+
|
|
159
|
+
failure.solution = solution;
|
|
160
|
+
failure.status = 'resolved';
|
|
161
|
+
if (skillName) {
|
|
162
|
+
failure.created_skill = true;
|
|
163
|
+
failure.skill_name = skillName;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
this.history.push({
|
|
167
|
+
type: 'resolved',
|
|
168
|
+
timestamp: new Date().toISOString(),
|
|
169
|
+
failure_id: failureId,
|
|
170
|
+
solution: solution
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
this.saveData();
|
|
174
|
+
console.log(`[自我迭代] 问题 #${failureId.slice(0, 8)} 已解决`);
|
|
175
|
+
return failure;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* 创建新技能
|
|
180
|
+
*/
|
|
181
|
+
createSkill(name, description, triggers, errorPattern, solution) {
|
|
182
|
+
const skill = {
|
|
183
|
+
name,
|
|
184
|
+
description,
|
|
185
|
+
triggers: triggers || [name],
|
|
186
|
+
error_pattern: errorPattern,
|
|
187
|
+
solution,
|
|
188
|
+
created_at: new Date().toISOString(),
|
|
189
|
+
usage_count: 0,
|
|
190
|
+
version: '1.0.0'
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
// 添加到索引
|
|
194
|
+
this.skillsIndex.push(skill);
|
|
195
|
+
|
|
196
|
+
// 创建SKILL.md文件
|
|
197
|
+
this.saveSkillToFile(skill);
|
|
198
|
+
|
|
199
|
+
// 更新相关失败记录
|
|
200
|
+
this.failures.forEach(f => {
|
|
201
|
+
if (f.error_type === errorPattern ||
|
|
202
|
+
(f.error_message && f.error_message.toLowerCase().includes(name.toLowerCase()))) {
|
|
203
|
+
f.solution = `应用技能: ${name}`;
|
|
204
|
+
f.status = 'resolved';
|
|
205
|
+
f.created_skill = true;
|
|
206
|
+
f.skill_name = name;
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
this.history.push({
|
|
211
|
+
type: 'skill_created',
|
|
212
|
+
timestamp: new Date().toISOString(),
|
|
213
|
+
skill_name: name
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
this.saveData();
|
|
217
|
+
console.log(`[自我迭代] 新技能已创建: ${name}`);
|
|
218
|
+
return skill;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* 保存技能到文件
|
|
223
|
+
*/
|
|
224
|
+
saveSkillToFile(skill) {
|
|
225
|
+
const skillDir = path.join(
|
|
226
|
+
process.env.HOME || process.env.USERPROFILE || '.',
|
|
227
|
+
'.feihong-code',
|
|
228
|
+
'skills',
|
|
229
|
+
skill.name
|
|
230
|
+
);
|
|
231
|
+
|
|
232
|
+
if (!fs.existsSync(skillDir)) {
|
|
233
|
+
fs.mkdirSync(skillDir, { recursive: true });
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const triggersText = skill.triggers ? skill.triggers.join(', ') : skill.name;
|
|
237
|
+
const content = `---
|
|
238
|
+
name: ${skill.name}
|
|
239
|
+
description: ${skill.description}
|
|
240
|
+
triggers: [${triggersText}]
|
|
241
|
+
---
|
|
242
|
+
|
|
243
|
+
# ${skill.name} 技能
|
|
244
|
+
|
|
245
|
+
**自动创建的解决技能**
|
|
246
|
+
|
|
247
|
+
## 触发条件
|
|
248
|
+
${skill.triggers ? skill.triggers.join('\n- ') : skill.name}
|
|
249
|
+
|
|
250
|
+
## 错误模式
|
|
251
|
+
${skill.error_pattern}
|
|
252
|
+
|
|
253
|
+
## 解决方案
|
|
254
|
+
${skill.solution}
|
|
255
|
+
|
|
256
|
+
## 创建时间
|
|
257
|
+
${skill.created_at}
|
|
258
|
+
`;
|
|
259
|
+
|
|
260
|
+
fs.writeFileSync(path.join(skillDir, 'SKILL.md'), content, 'utf8');
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* 生成每日复盘报告
|
|
265
|
+
*/
|
|
266
|
+
generateDailyReport() {
|
|
267
|
+
const today = new Date().toISOString().split('T')[0];
|
|
268
|
+
const todayFailures = this.failures.filter(f => f.timestamp.startsWith(today));
|
|
269
|
+
const todayResolutions = this.history.filter(h =>
|
|
270
|
+
h.timestamp.startsWith(today) && h.type === 'resolved'
|
|
271
|
+
);
|
|
272
|
+
|
|
273
|
+
const report = {
|
|
274
|
+
date: today,
|
|
275
|
+
total_failures: todayFailures.length,
|
|
276
|
+
resolved: todayResolutions.length,
|
|
277
|
+
pending: todayFailures.filter(f => f.status !== 'resolved').length,
|
|
278
|
+
new_skills: this.history.filter(h =>
|
|
279
|
+
h.timestamp.startsWith(today) && h.type === 'skill_created'
|
|
280
|
+
).length,
|
|
281
|
+
failures: todayFailures.map(f => ({
|
|
282
|
+
id: f.id.slice(0, 8),
|
|
283
|
+
type: f.error_type,
|
|
284
|
+
message: f.error_message.slice(0, 100),
|
|
285
|
+
status: f.status
|
|
286
|
+
}))
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
// 保存报告
|
|
290
|
+
const reportFile = path.join(this.baseDir, `report-${today}.json`);
|
|
291
|
+
fs.writeFileSync(reportFile, JSON.stringify(report, null, 2));
|
|
292
|
+
|
|
293
|
+
console.log(`[自我迭代] 每日报告已生成: ${today}`);
|
|
294
|
+
console.log(` - 失败任务: ${report.total_failures}`);
|
|
295
|
+
console.log(` - 已解决: ${report.resolved}`);
|
|
296
|
+
console.log(` - 待处理: ${report.pending}`);
|
|
297
|
+
console.log(` - 新技能: ${report.new_skills}`);
|
|
298
|
+
|
|
299
|
+
return report;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* 统计分析
|
|
304
|
+
*/
|
|
305
|
+
getStatistics() {
|
|
306
|
+
const totalFailures = this.failures.length;
|
|
307
|
+
const resolved = this.failures.filter(f => f.status === 'resolved').length;
|
|
308
|
+
const byType = {};
|
|
309
|
+
|
|
310
|
+
this.failures.forEach(f => {
|
|
311
|
+
byType[f.error_type] = (byType[f.error_type] || 0) + 1;
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
return {
|
|
315
|
+
total_failures: totalFailures,
|
|
316
|
+
resolved: resolved,
|
|
317
|
+
pending: totalFailures - resolved,
|
|
318
|
+
resolution_rate: totalFailures > 0 ? ((resolved / totalFailures) * 100).toFixed(1) + '%' : '0%',
|
|
319
|
+
by_type: byType,
|
|
320
|
+
total_skills: this.skillsIndex.length,
|
|
321
|
+
history_entries: this.history.length
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* 列出所有失败记录
|
|
327
|
+
*/
|
|
328
|
+
listFailures(filters = {}) {
|
|
329
|
+
let result = this.failures;
|
|
330
|
+
|
|
331
|
+
if (filters.type) {
|
|
332
|
+
result = result.filter(f => f.error_type === filters.type);
|
|
333
|
+
}
|
|
334
|
+
if (filters.status) {
|
|
335
|
+
result = result.filter(f => f.status === filters.status);
|
|
336
|
+
}
|
|
337
|
+
if (filters.days) {
|
|
338
|
+
const cutoff = new Date(Date.now() - filters.days * 24 * 60 * 60 * 1000);
|
|
339
|
+
result = result.filter(f => new Date(f.timestamp) >= cutoff);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
return result;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* 列出所有技能
|
|
347
|
+
*/
|
|
348
|
+
listSkills() {
|
|
349
|
+
return this.skillsIndex;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* 初始化系统
|
|
354
|
+
*/
|
|
355
|
+
init() {
|
|
356
|
+
this.ensureDirs();
|
|
357
|
+
this.loadData();
|
|
358
|
+
|
|
359
|
+
// 创建初始数据文件(如果不存在)
|
|
360
|
+
if (!fs.existsSync(this.failuresFile)) {
|
|
361
|
+
this.saveData();
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
console.log('[自我迭代] 系统已初始化');
|
|
365
|
+
console.log(` - 基础目录: ${this.baseDir}`);
|
|
366
|
+
console.log(` - 失败记录: ${this.failures.length} 条`);
|
|
367
|
+
console.log(` - 技能库: ${this.skillsIndex.length} 个`);
|
|
368
|
+
console.log(` - 历史记录: ${this.history.length} 条`);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
module.exports = { SelfEvolveManager };
|
|
373
|
+
|
|
374
|
+
// CLI 入口
|
|
375
|
+
if (require.main === module) {
|
|
376
|
+
const manager = new SelfEvolveManager();
|
|
377
|
+
const args = process.argv.slice(2);
|
|
378
|
+
|
|
379
|
+
if (args[0] === 'init') {
|
|
380
|
+
manager.init();
|
|
381
|
+
} else if (args[0] === 'status') {
|
|
382
|
+
console.log(JSON.stringify(manager.getStatistics(), null, 2));
|
|
383
|
+
} else if (args[0] === 'failures' && args[1] === 'list') {
|
|
384
|
+
const filters = {};
|
|
385
|
+
if (args[2] === '--type' && args[3]) filters.type = args[3];
|
|
386
|
+
if (args[2] === '--days' && args[3]) filters.days = parseInt(args[3]);
|
|
387
|
+
console.log(JSON.stringify(manager.listFailures(filters), null, 2));
|
|
388
|
+
} else if (args[0] === 'skills' && args[1] === 'list') {
|
|
389
|
+
console.log(JSON.stringify(manager.listSkills(), null, 2));
|
|
390
|
+
} else if (args[0] === 'review' && args[1] === '--daily') {
|
|
391
|
+
manager.generateDailyReport();
|
|
392
|
+
} else {
|
|
393
|
+
console.log('自我迭代管理系统');
|
|
394
|
+
console.log('用法:');
|
|
395
|
+
console.log(' self-evolve init - 初始化系统');
|
|
396
|
+
console.log(' self-evolve status - 查看统计');
|
|
397
|
+
console.log(' self-evolve failures list - 列出失败');
|
|
398
|
+
console.log(' self-evolve skills list - 列出技能');
|
|
399
|
+
console.log(' self-evolve review --daily - 每日复盘');
|
|
400
|
+
}
|
|
401
|
+
}
|
package/dist/shared/errors.js
CHANGED
|
@@ -32,9 +32,13 @@ exports.ConfigError = ConfigError;
|
|
|
32
32
|
/** 模型调用失败 */
|
|
33
33
|
class ModelError extends AppError {
|
|
34
34
|
provider;
|
|
35
|
-
|
|
36
|
-
|
|
35
|
+
statusCode;
|
|
36
|
+
constructor(message, provider,
|
|
37
|
+
/** 上游 HTTP 状态码(如 429/500/401/400),供路由层按状态码决定轮换/重试策略 */
|
|
38
|
+
statusCode) {
|
|
39
|
+
super(`模型调用失败[${provider}]: ${message}`, 'MODEL_ERROR', statusCode ?? 502);
|
|
37
40
|
this.provider = provider;
|
|
41
|
+
this.statusCode = statusCode;
|
|
38
42
|
}
|
|
39
43
|
}
|
|
40
44
|
exports.ModelError = ModelError;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/shared/errors.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/shared/errors.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAwDH,gCAEC;AAxDD,iBAAiB;AACjB,MAAa,QAAS,SAAQ,KAAK;IAGf;IACA;IACA;IAJlB,YACE,OAAe,EACC,IAAY,EACZ,MAAc,EACd,gBAAgB,IAAI;QAEpC,KAAK,CAAC,OAAO,CAAC,CAAC;QAJC,SAAI,GAAJ,IAAI,CAAQ;QACZ,WAAM,GAAN,MAAM,CAAQ;QACd,kBAAa,GAAb,aAAa,CAAO;QAGpC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC;IACpC,CAAC;CACF;AAVD,4BAUC;AAED,4BAA4B;AAC5B,MAAa,WAAY,SAAQ,QAAQ;IACvC,YAAY,GAAW;QACrB,KAAK,CAAC,aAAa,GAAG,EAAE,EAAE,cAAc,EAAE,GAAG,CAAC,CAAC;IACjD,CAAC;CACF;AAJD,kCAIC;AAED,aAAa;AACb,MAAa,UAAW,SAAQ,QAAQ;IAGpB;IAEA;IAJlB,YACE,OAAe,EACC,QAAgB;IAChC,uDAAuD;IACvC,UAAmB;QAEnC,KAAK,CAAC,UAAU,QAAQ,MAAM,OAAO,EAAE,EAAE,aAAa,EAAE,UAAU,IAAI,GAAG,CAAC,CAAC;QAJ3D,aAAQ,GAAR,QAAQ,CAAQ;QAEhB,eAAU,GAAV,UAAU,CAAS;IAGrC,CAAC;CACF;AATD,gCASC;AAED,aAAa;AACb,MAAa,SAAU,SAAQ,QAAQ;IACrC,YAAY,IAAY,EAAE,OAAe;QACvC,KAAK,CAAC,MAAM,IAAI,UAAU,OAAO,EAAE,EAAE,YAAY,EAAE,GAAG,CAAC,CAAC;IAC1D,CAAC;CACF;AAJD,8BAIC;AAED,aAAa;AACb,MAAa,qBAAsB,SAAQ,QAAQ;IACjD,YAAY,MAAc;QACxB,KAAK,CAAC,UAAU,MAAM,EAAE,EAAE,mBAAmB,EAAE,GAAG,CAAC,CAAC;IACtD,CAAC;CACF;AAJD,sDAIC;AAED,sBAAsB;AACtB,MAAa,aAAc,SAAQ,QAAQ;IACzC,YAAY,OAAe;QACzB,KAAK,CAAC,SAAS,OAAO,EAAE,EAAE,gBAAgB,EAAE,GAAG,CAAC,CAAC;IACnD,CAAC;CACF;AAJD,sCAIC;AAED,yBAAyB;AACzB,SAAgB,UAAU,CAAC,CAAU;IACnC,OAAO,CAAC,YAAY,QAAQ,CAAC;AAC/B,CAAC"}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getMasterKey = getMasterKey;
|
|
4
|
+
exports.encryptText = encryptText;
|
|
5
|
+
exports.decryptText = decryptText;
|
|
6
|
+
exports.isEncrypted = isEncrypted;
|
|
7
|
+
exports.getRsaKeys = getRsaKeys;
|
|
8
|
+
exports.rsaEncrypt = rsaEncrypt;
|
|
9
|
+
exports.rsaDecrypt = rsaDecrypt;
|
|
10
|
+
/**
|
|
11
|
+
* 飞虹 Code - 安全存储与通信加密工具
|
|
12
|
+
* 晋江市飞虹智科技企业管理有限公司 · 飞扬企源研发中心 · 负责人:吴赐虹
|
|
13
|
+
*
|
|
14
|
+
* 三重加密体系支撑模块:
|
|
15
|
+
* - AES-256-GCM:敏感数据落盘加密(模型 API Key、会话令牌)【存储层】
|
|
16
|
+
* - RSA-2048:敏感参数端到端加密传输(App 公钥加密 → 服务器私钥解密)【通信层】
|
|
17
|
+
* - 密钥管理:主密钥优先取环境变量 FH_SECRET,否则首次生成持久化 $FH_HOME/.secret
|
|
18
|
+
* (RSA 私钥持久化 $FH_HOME/rsa_private.pem,公钥经 API 提供给客户端)
|
|
19
|
+
*/
|
|
20
|
+
const crypto_1 = require("crypto");
|
|
21
|
+
const fs_1 = require("fs");
|
|
22
|
+
const path_1 = require("path");
|
|
23
|
+
/** 主密钥:优先环境变量 FH_SECRET(>=16 字符),否则首次生成并持久化 $FH_HOME/.secret */
|
|
24
|
+
function getMasterKey(homeDir) {
|
|
25
|
+
const env = process.env.FH_SECRET;
|
|
26
|
+
if (env && env.length >= 16)
|
|
27
|
+
return (0, crypto_1.createHash)('sha256').update(env).digest();
|
|
28
|
+
const file = (0, path_1.join)(homeDir, '.secret');
|
|
29
|
+
if ((0, fs_1.existsSync)(file)) {
|
|
30
|
+
const hex = (0, fs_1.readFileSync)(file, 'utf8').trim();
|
|
31
|
+
if (hex.length === 64)
|
|
32
|
+
return Buffer.from(hex, 'hex');
|
|
33
|
+
}
|
|
34
|
+
try {
|
|
35
|
+
(0, fs_1.mkdirSync)(homeDir, { recursive: true });
|
|
36
|
+
const key = (0, crypto_1.randomBytes)(32);
|
|
37
|
+
(0, fs_1.writeFileSync)(file, key.toString('hex'), 'utf8');
|
|
38
|
+
return key;
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
// 极端情况(目录不可写):退回进程内随机密钥(重启失效,仅兜底)
|
|
42
|
+
return (0, crypto_1.randomBytes)(32);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/** AES-256-GCM 加密,输出格式 v1:iv:cipher:tag(全部 base64) */
|
|
46
|
+
function encryptText(plain, key) {
|
|
47
|
+
const iv = (0, crypto_1.randomBytes)(12);
|
|
48
|
+
const cipher = (0, crypto_1.createCipheriv)('aes-256-gcm', key, iv);
|
|
49
|
+
const enc = Buffer.concat([cipher.update(String(plain), 'utf8'), cipher.final()]);
|
|
50
|
+
const tag = cipher.getAuthTag();
|
|
51
|
+
return `v1:${iv.toString('base64')}:${enc.toString('base64')}:${tag.toString('base64')}`;
|
|
52
|
+
}
|
|
53
|
+
/** AES-256-GCM 解密;格式错误/密钥错误返回空串(调用方按空处理) */
|
|
54
|
+
function decryptText(payload, key) {
|
|
55
|
+
try {
|
|
56
|
+
const parts = String(payload).split(':');
|
|
57
|
+
if (parts.length !== 4 || parts[0] !== 'v1')
|
|
58
|
+
return '';
|
|
59
|
+
const iv = Buffer.from(parts[1], 'base64');
|
|
60
|
+
const enc = Buffer.from(parts[2], 'base64');
|
|
61
|
+
const tag = Buffer.from(parts[3], 'base64');
|
|
62
|
+
const decipher = (0, crypto_1.createDecipheriv)('aes-256-gcm', key, iv);
|
|
63
|
+
decipher.setAuthTag(tag);
|
|
64
|
+
return Buffer.concat([decipher.update(enc), decipher.final()]).toString('utf8');
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
return '';
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/** 是否已是加密格式 */
|
|
71
|
+
function isEncrypted(s) {
|
|
72
|
+
return typeof s === 'string' && s.startsWith('v1:');
|
|
73
|
+
}
|
|
74
|
+
/** RSA-2048 密钥对:首次生成并持久化,后续复用 */
|
|
75
|
+
function getRsaKeys(homeDir) {
|
|
76
|
+
const pubFile = (0, path_1.join)(homeDir, 'rsa_public.pem');
|
|
77
|
+
const prvFile = (0, path_1.join)(homeDir, 'rsa_private.pem');
|
|
78
|
+
if ((0, fs_1.existsSync)(pubFile) && (0, fs_1.existsSync)(prvFile)) {
|
|
79
|
+
return {
|
|
80
|
+
publicKey: (0, fs_1.readFileSync)(pubFile, 'utf8'),
|
|
81
|
+
privateKey: (0, fs_1.readFileSync)(prvFile, 'utf8'),
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
try {
|
|
85
|
+
(0, fs_1.mkdirSync)(homeDir, { recursive: true });
|
|
86
|
+
const { publicKey, privateKey } = (0, crypto_1.generateKeyPairSync)('rsa', {
|
|
87
|
+
modulusLength: 2048,
|
|
88
|
+
publicKeyEncoding: { type: 'spki', format: 'pem' },
|
|
89
|
+
privateKeyEncoding: { type: 'pkcs8', format: 'pem' },
|
|
90
|
+
});
|
|
91
|
+
(0, fs_1.writeFileSync)(pubFile, publicKey, 'utf8');
|
|
92
|
+
(0, fs_1.writeFileSync)(prvFile, privateKey, 'utf8');
|
|
93
|
+
return { publicKey, privateKey };
|
|
94
|
+
}
|
|
95
|
+
catch {
|
|
96
|
+
const { publicKey, privateKey } = (0, crypto_1.generateKeyPairSync)('rsa', {
|
|
97
|
+
modulusLength: 2048,
|
|
98
|
+
publicKeyEncoding: { type: 'spki', format: 'pem' },
|
|
99
|
+
privateKeyEncoding: { type: 'pkcs8', format: 'pem' },
|
|
100
|
+
});
|
|
101
|
+
return { publicKey, privateKey };
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
/** RSA 公钥加密(OAEP-SHA256),返回 base64 */
|
|
105
|
+
function rsaEncrypt(plain, publicKeyPem) {
|
|
106
|
+
return (0, crypto_1.publicEncrypt)({ key: publicKeyPem, padding: crypto_1.constants.RSA_PKCS1_OAEP_PADDING, oaepHash: 'sha256' }, Buffer.from(String(plain), 'utf8')).toString('base64');
|
|
107
|
+
}
|
|
108
|
+
/** RSA 私钥解密(OAEP-SHA256);失败返回空串 */
|
|
109
|
+
function rsaDecrypt(cipherB64, privateKeyPem) {
|
|
110
|
+
try {
|
|
111
|
+
return (0, crypto_1.privateDecrypt)({ key: privateKeyPem, padding: crypto_1.constants.RSA_PKCS1_OAEP_PADDING, oaepHash: 'sha256' }, Buffer.from(String(cipherB64), 'base64')).toString('utf8');
|
|
112
|
+
}
|
|
113
|
+
catch {
|
|
114
|
+
return '';
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
//# sourceMappingURL=secure-store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"secure-store.js","sourceRoot":"","sources":["../../src/shared/secure-store.ts"],"names":[],"mappings":";;AAwBA,oCAiBC;AAGD,kCAMC;AAGD,kCAaC;AAGD,kCAEC;AAGD,gCA2BC;AAGD,gCAKC;AAGD,gCASC;AAzHD;;;;;;;;;GASG;AACH,mCASgB;AAChB,2BAAwE;AACxE,+BAA4B;AAE5B,gEAAgE;AAChE,SAAgB,YAAY,CAAC,OAAe;IAC1C,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,SAAS,CAAC;IAClC,IAAI,GAAG,IAAI,GAAG,CAAC,MAAM,IAAI,EAAE;QAAE,OAAO,IAAA,mBAAU,EAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC;IAC9E,MAAM,IAAI,GAAG,IAAA,WAAI,EAAC,OAAO,EAAE,SAAS,CAAC,CAAC;IACtC,IAAI,IAAA,eAAU,EAAC,IAAI,CAAC,EAAE,CAAC;QACrB,MAAM,GAAG,GAAG,IAAA,iBAAY,EAAC,IAAI,EAAE,MAAM,CAAC,CAAC,IAAI,EAAE,CAAC;QAC9C,IAAI,GAAG,CAAC,MAAM,KAAK,EAAE;YAAE,OAAO,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;IACxD,CAAC;IACD,IAAI,CAAC;QACH,IAAA,cAAS,EAAC,OAAO,EAAE,EAAE,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mCAAmC;AACnC,SAAgB,UAAU,CAAC,SAAiB,EAAE,aAAqB;IACjE,IAAI,CAAC;QACH,OAAO,IAAA,uBAAc,EACnB,EAAE,GAAG,EAAE,aAAa,EAAE,OAAO,EAAE,kBAAS,CAAC,sBAAsB,EAAE,QAAQ,EAAE,QAAQ,EAAE,EACrF,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,EAAE,QAAQ,CAAC,CACzC,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;IACrB,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,EAAE,CAAC;IACZ,CAAC;AACH,CAAC"}
|
|
@@ -79,21 +79,43 @@ class McpClient {
|
|
|
79
79
|
const tools = resp.result.tools ?? [];
|
|
80
80
|
return tools;
|
|
81
81
|
}
|
|
82
|
-
/**
|
|
82
|
+
/** 调用服务器工具(参数自动序列化,文本结果拼接返回),含瞬态错误指数退避重试 */
|
|
83
83
|
async callTool(toolName, args) {
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
|
|
84
|
+
const maxRetries = 3;
|
|
85
|
+
let lastErr;
|
|
86
|
+
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
87
|
+
try {
|
|
88
|
+
const resp = await this.request('tools/call', { name: toolName, arguments: args }, this.cfg.callTimeoutMs ?? 120000);
|
|
89
|
+
if (!resp.result) {
|
|
90
|
+
return { ok: false, output: '', error: `tools/call 失败: ${resp.error?.message ?? '无响应'}` };
|
|
91
|
+
}
|
|
92
|
+
const result = resp.result;
|
|
93
|
+
const text = (result.content ?? [])
|
|
94
|
+
.map((c) => c.text ?? '')
|
|
95
|
+
.join('\n');
|
|
96
|
+
return {
|
|
97
|
+
ok: !result.isError,
|
|
98
|
+
output: text,
|
|
99
|
+
error: result.isError ? text.slice(0, 500) : undefined,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
catch (e) {
|
|
103
|
+
lastErr = e instanceof Error ? e : new Error(String(e));
|
|
104
|
+
const msg = lastErr.message;
|
|
105
|
+
// 进程退出/连接断开是终结性错误,直接抛出;仅对瞬态超时/网络错误重试
|
|
106
|
+
if (/MCP 进程退出|MCP 进程启动失败|MCP 请求未运行/i.test(msg)) {
|
|
107
|
+
throw lastErr;
|
|
108
|
+
}
|
|
109
|
+
const transient = /MCP 请求超时|ECONNRESET|ECONNREFUSED|ETIMEDOUT|network/i.test(msg);
|
|
110
|
+
if (!transient || attempt === maxRetries)
|
|
111
|
+
break;
|
|
112
|
+
logger_1.logger.warn(`MCP callTool 瞬态错误,指数退避重试 ${attempt + 1}/${maxRetries}`, {
|
|
113
|
+
tool: toolName, error: msg,
|
|
114
|
+
});
|
|
115
|
+
await new Promise((r) => setTimeout(r, 500 * Math.pow(2, attempt)));
|
|
116
|
+
}
|
|
87
117
|
}
|
|
88
|
-
|
|
89
|
-
const text = (result.content ?? [])
|
|
90
|
-
.map((c) => c.text ?? '')
|
|
91
|
-
.join('\n');
|
|
92
|
-
return {
|
|
93
|
-
ok: !result.isError,
|
|
94
|
-
output: text,
|
|
95
|
-
error: result.isError ? text.slice(0, 500) : undefined,
|
|
96
|
-
};
|
|
118
|
+
return { ok: false, output: '', error: lastErr?.message ?? 'tools/call 失败' };
|
|
97
119
|
}
|
|
98
120
|
/** 关闭连接(结束子进程) */
|
|
99
121
|
async close() {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mcp-client.js","sourceRoot":"","sources":["../../../src/tools/mcp/mcp-client.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"mcp-client.js","sourceRoot":"","sources":["../../../src/tools/mcp/mcp-client.ts"],"names":[],"mappings":";;;AA+NA,gCASC;AAxOD;;;;;;;;;;;GAWG;AACH,iDAAyD;AACzD,uCAA2D;AAC3D,gDAA6C;AAsC7C,0CAA0C;AAC1C,SAAS,YAAY,CAAC,KAAmB,EAAE,GAAY;IACrD,KAAK,CAAC,KAAK,EAAE,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,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