-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadvanced-usage.js
More file actions
119 lines (96 loc) · 4.63 KB
/
Copy pathadvanced-usage.js
File metadata and controls
119 lines (96 loc) · 4.63 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
#!/usr/bin/env node
/**
* Skill Evolution Plugin - 进阶用法示例
*
* 这个示例展示了多次执行后的模式识别和建议生成
*/
const { SkillEvolutionPlugin } = require('../src/index');
const chalkImport = require('chalk');
const chalk = chalkImport.default || chalkImport;
// 创建插件实例
const plugin = new SkillEvolutionPlugin({
threshold: 3 // 3 次相似修正后建议
});
console.log(chalk.green('═══════════════════════════════════════════════════════════'));
console.log(chalk.green(' Skill Evolution Plugin - 进阶用法示例'));
console.log(chalk.green('═══════════════════════════════════════════════════════════'));
console.log('');
const skillName = 'agent-browser';
// 模拟 5 次 Skill 执行
for (let i = 1; i <= 5; i++) {
console.log(chalk.blue(`\n--- 第 ${i} 次执行 ---`));
const sessionId = plugin.startMonitoring(skillName, '帮我登录 example.com');
// 模拟工具调用
plugin.recordToolCall(sessionId, 'agent-browser.open',
{ url: 'https://example.com/login' },
{ success: true }
);
plugin.recordToolCall(sessionId, 'agent-browser.snapshot',
{},
{ success: true, elements: ['@e1', '@e2', '@e4'] }
);
plugin.recordToolCall(sessionId, 'agent-browser.fill',
{ ref: '@e1', text: 'test' },
{ success: true }
);
// 第 3、4、5 次遇到元素找不到的问题
if (i >= 3) {
console.log(chalk.yellow(' ⚠️ 模拟:元素 @e3 未找到'));
// 记录失败
plugin.recordToolCall(sessionId, 'agent-browser.click',
{ ref: '@e3' },
{ success: false, error: 'Element not found' }
);
// AI 自动修正:重新 snapshot
plugin.recordToolCall(sessionId, 'agent-browser.snapshot',
{},
{ success: true, elements: ['@e1', '@e2', '@e5'] },
{ retryCount: 1, retryReason: 'Element not found' }
);
// 使用新选择器成功
plugin.recordToolCall(sessionId, 'agent-browser.click',
{ ref: '@e5' },
{ success: true },
{ correctedFrom: { ref: '@e3' }, correctionReason: 'Re-snapshot revealed new element' }
);
} else {
plugin.recordToolCall(sessionId, 'agent-browser.click',
{ ref: '@e3' },
{ success: true }
);
}
// 结束会话
plugin.stopMonitoring(sessionId, 'success');
console.log(chalk.gray(` ✓ 会话 ${sessionId} 已保存`));
}
// 生成统计报告
console.log(chalk.green('\n═══════════════════════════════════════════════════════════'));
console.log(chalk.green(' 生成进化报告'));
console.log(chalk.green('═══════════════════════════════════════════════════════════'));
const report = plugin.generateReport(skillName);
console.log(report);
// 分析模式
console.log(chalk.green('═══════════════════════════════════════════════════════════'));
console.log(chalk.green(' 模式分析(JSON 格式)'));
console.log(chalk.green('═══════════════════════════════════════════════════════════'));
const analysis = plugin.analyze(skillName);
console.log(JSON.stringify(analysis, null, 2));
// 列出补丁
console.log(chalk.green('\n═══════════════════════════════════════════════════════════'));
console.log(chalk.green(' 已应用的补丁'));
console.log(chalk.green('═══════════════════════════════════════════════════════════'));
const patches = plugin.listPatches(skillName);
if (patches.length === 0) {
console.log(' 暂无补丁');
} else {
patches.forEach(p => {
console.log(` - ${p.file}`);
if (p.description) {
console.log(` ${chalk.gray(p.description)}`);
}
});
}
console.log('');
console.log(chalk.green('示例完成!'));
console.log('');
console.log(chalk.gray('日志已保存到:~/.openclaw/skills/agent-browser/.evolution/execution-log.jsonl'));