-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenclaw-integration.js
More file actions
194 lines (160 loc) · 5.89 KB
/
Copy pathopenclaw-integration.js
File metadata and controls
194 lines (160 loc) · 5.89 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
#!/usr/bin/env node
/**
* Skill Evolution Plugin - OpenClaw 集成示例
*
* 这个示例展示了如何将 Skill Evolution Plugin 集成到 OpenClaw 中
*/
const { SkillEvolutionPlugin } = require('../src/index');
// 创建全局插件实例
const evolutionPlugin = new SkillEvolutionPlugin({
enabled: true,
logPath: '~/.openclaw/skills',
threshold: 3
});
/**
* 模拟 OpenClaw 的 Skill 执行器
*
* 这是伪代码,展示集成思路
*/
class MockSkillExecutor {
constructor() {
this.currentSkillName = null;
this.currentUserRequest = null;
this.currentSessionId = null;
}
/**
* 执行 Skill
*/
async executeSkill(skillName, userRequest, steps) {
console.log(`\n📝 执行 Skill: ${skillName}`);
console.log(` 用户请求:${userRequest}`);
console.log('');
this.currentSkillName = skillName;
this.currentUserRequest = userRequest;
// 1. 开始监控
this.currentSessionId = evolutionPlugin.startMonitoring(skillName, userRequest);
try {
// 2. 执行每个步骤
for (const step of steps) {
await this.executeTool(step.tool, step.params);
}
// 3. 结束监控
evolutionPlugin.stopMonitoring(this.currentSessionId, 'success');
console.log('\n✅ Skill 执行完成');
} catch (error) {
// 执行失败
evolutionPlugin.stopMonitoring(this.currentSessionId, 'failed');
console.log(`\n❌ Skill 执行失败:${error.message}`);
throw error;
} finally {
// 清理
this.currentSessionId = null;
this.currentSkillName = null;
this.currentUserRequest = null;
}
}
/**
* 执行工具调用
*/
async executeTool(toolName, params) {
console.log(` 🔧 执行:${toolName}(${JSON.stringify(params)})`);
// 模拟执行
const result = await this.mockExecute(toolName, params);
// 记录工具调用
evolutionPlugin.recordToolCall(this.currentSessionId, toolName, params, result);
if (!result.success) {
throw new Error(result.error || 'Tool execution failed');
}
console.log(` ✓ 成功`);
return result;
}
/**
* 模拟工具执行(实际应该调用 OpenClaw 的工具)
*/
async mockExecute(toolName, params) {
// 模拟延迟
await new Promise(resolve => setTimeout(resolve, 100));
// 模拟成功
if (toolName === 'agent-browser.open') {
return { success: true };
}
if (toolName === 'agent-browser.snapshot') {
return {
success: true,
elements: ['@e1', '@e2', '@e5']
};
}
if (toolName === 'agent-browser.fill') {
return { success: true };
}
if (toolName === 'agent-browser.click') {
// 模拟第 3、4、5 次执行时元素找不到的问题
const executionCount = this.getExecutionCount();
if (executionCount >= 3 && params.ref === '@e3') {
return {
success: false,
error: 'Element not found'
};
}
return { success: true };
}
return { success: true };
}
/**
* 获取执行次数(用于模拟)
*/
getExecutionCount() {
const fs = require('fs');
const path = require('path');
const homeDir = process.env.HOME || process.env.USERPROFILE;
const logFile = path.join(homeDir, '.openclaw/skills/agent-browser/.evolution/execution-log.jsonl');
if (!fs.existsSync(logFile)) {
return 1;
}
const content = fs.readFileSync(logFile, 'utf-8');
const lines = content.trim().split('\n').filter(line => line.trim());
return lines.length + 1;
}
}
/**
* 主函数 - 演示集成
*/
async function main() {
const chalkImport = require('chalk');
const chalk = chalkImport.default || chalkImport;
console.log(chalk.green('═══════════════════════════════════════════════════════════'));
console.log(chalk.green(' Skill Evolution Plugin - OpenClaw 集成示例'));
console.log(chalk.green('═══════════════════════════════════════════════════════════'));
const executor = new MockSkillExecutor();
// 模拟 5 次 Skill 执行
const steps = [
{ tool: 'agent-browser.open', params: { url: 'https://example.com/login' } },
{ tool: 'agent-browser.snapshot', params: {} },
{ tool: 'agent-browser.fill', params: { ref: '@e1', text: 'test' } },
{ tool: 'agent-browser.click', params: { ref: '@e3' } }
];
for (let i = 1; i <= 5; i++) {
console.log(chalk.blue(`\n${'═'.repeat(60)}`));
console.log(chalk.blue(`第 ${i} 次执行`));
console.log(chalk.blue('═'.repeat(60)));
try {
await executor.executeSkill('agent-browser', '帮我登录 example.com', steps);
} catch (error) {
// 忽略错误,继续下一次
}
}
// 生成报告
console.log(chalk.green('\n═══════════════════════════════════════════════════════════'));
console.log(chalk.green(' 生成进化报告'));
console.log(chalk.green('═══════════════════════════════════════════════════════════'));
const report = evolutionPlugin.generateReport('agent-browser');
console.log(report);
console.log(chalk.green('集成示例完成!'));
console.log('');
console.log(chalk.gray('这个示例展示了如何将 Skill Evolution Plugin 集成到 OpenClaw 的 Skill 执行器中。'));
console.log(chalk.gray('实际集成时,需要修改 OpenClaw 的 SkillExecutor.js 文件。'));
console.log('');
console.log(chalk.gray('详见:integration/README.md'));
}
// 运行示例
main().catch(console.error);