Repository navigation
Expand file tree
/
Copy pathbasic-usage.js
More file actions
107 lines (88 loc) · 3.98 KB
/
Copy pathbasic-usage.js
File metadata and controls
107 lines (88 loc) · 3.98 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
#!/usr/bin/env node
/**
* Skill Evolution Plugin - 基础用法示例
*
* 这个示例展示了如何使用 Skill Evolution Plugin 的核心 API
*/
const { SkillEvolutionPlugin } = require('../src/index');
// 1. 创建插件实例
const plugin = new SkillEvolutionPlugin({
enabled: true,
logPath: '~/.openclaw/skills',
threshold: 3
});
console.log('═══════════════════════════════════════════════════════════');
console.log(' Skill Evolution Plugin - 基础用法示例');
console.log('═══════════════════════════════════════════════════════════');
console.log('');
// 2. 模拟 Skill 执行
const skillName = 'agent-browser';
const userRequest = '帮我登录 example.com';
console.log(`📝 模拟用户请求:${userRequest}`);
console.log(`🔧 使用 Skill: ${skillName}`);
console.log('');
// 3. 开始监控
const sessionId = plugin.startMonitoring(skillName, userRequest);
console.log(`✅ 开始监控会话:${sessionId}`);
console.log('');
// 4. 记录工具调用
console.log('📊 记录工具调用...');
// 步骤 1: 打开网页
plugin.recordToolCall(sessionId, 'agent-browser.open',
{ url: 'https://example.com/login' },
{ success: true }
);
console.log(' ✓ agent-browser.open');
// 步骤 2: 获取页面元素
plugin.recordToolCall(sessionId, 'agent-browser.snapshot',
{},
{ success: true, elements: ['@e1', '@e2', '@e4'] }
);
console.log(' ✓ agent-browser.snapshot');
// 步骤 3: 填写表单
plugin.recordToolCall(sessionId, 'agent-browser.fill',
{ ref: '@e1', text: 'test' },
{ success: true }
);
console.log(' ✓ agent-browser.fill');
// 步骤 4: 点击按钮(遇到问题)
console.log(' ⚠️ 模拟:元素 @e3 未找到');
plugin.recordToolCall(sessionId, 'agent-browser.click',
{ ref: '@e3' },
{ success: false, error: 'Element not found' }
);
console.log(' ✗ agent-browser.click (@e3) - 失败');
// 步骤 5: AI 自动修正 - 重新 snapshot
plugin.recordToolCall(sessionId, 'agent-browser.snapshot',
{},
{ success: true, elements: ['@e1', '@e2', '@e5'] },
{ retryCount: 1, retryReason: 'Element not found' }
);
console.log(' ✓ agent-browser.snapshot (重试)');
// 步骤 6: 使用新选择器成功
plugin.recordToolCall(sessionId, 'agent-browser.click',
{ ref: '@e5' },
{ success: true },
{ correctedFrom: { ref: '@e3' }, correctionReason: 'Re-snapshot revealed new element' }
);
console.log(' ✓ agent-browser.click (@e5) - 成功(修正后)');
console.log('');
// 5. 结束监控
const report = plugin.stopMonitoring(sessionId, 'success');
console.log(`✅ 会话结束,已保存日志`);
console.log('');
// 6. 显示执行报告
console.log('═══════════════════════════════════════════════════════════');
console.log(' 执行报告');
console.log('═══════════════════════════════════════════════════════════');
console.log(JSON.stringify(report, null, 2));
console.log('');
// 7. 生成人类可读的报告
console.log('═══════════════════════════════════════════════════════════');
console.log(' 进化报告');
console.log('═══════════════════════════════════════════════════════════');
const evolutionReport = plugin.generateReport(skillName);
console.log(evolutionReport);
console.log('示例完成!');
console.log('');
console.log('日志已保存到:~/.openclaw/skills/agent-browser/.evolution/execution-log.jsonl');