@workclaw/openclaw-workclaw 1.0.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/README.md +325 -0
- package/index.ts +298 -0
- package/openclaw.plugin.json +10 -0
- package/package.json +43 -0
- package/skills/openclaw-workclaw-cron/SKILL.md +458 -0
- package/src/accounts.ts +287 -0
- package/src/api/accounts-api.ts +157 -0
- package/src/api/prompts-api.ts +123 -0
- package/src/api/session-api.ts +247 -0
- package/src/api/skills-api.ts +74 -0
- package/src/api/workspace.ts +43 -0
- package/src/channel.ts +227 -0
- package/src/config-schema.ts +110 -0
- package/src/connection/workclaw-client.ts +656 -0
- package/src/gateway/agent-handlers.ts +557 -0
- package/src/gateway/config-writer.ts +311 -0
- package/src/gateway/message-context.ts +422 -0
- package/src/gateway/message-dispatcher.ts +601 -0
- package/src/gateway/reconnect.ts +149 -0
- package/src/gateway/skills-handler.ts +759 -0
- package/src/gateway/skills-list-handler.ts +332 -0
- package/src/gateway/tools-list-handler.ts +162 -0
- package/src/gateway/workclaw-gateway.ts +521 -0
- package/src/media/upload.ts +168 -0
- package/src/outbound/index.ts +183 -0
- package/src/outbound/workclaw-sender.ts +157 -0
- package/src/runtime.ts +400 -0
- package/src/send.ts +1 -0
- package/src/tools/openclaw-workclaw-cron/api/index.ts +326 -0
- package/src/tools/openclaw-workclaw-cron/index.ts +39 -0
- package/src/tools/openclaw-workclaw-cron/src/add/params.ts +176 -0
- package/src/tools/openclaw-workclaw-cron/src/add/sync.ts +188 -0
- package/src/tools/openclaw-workclaw-cron/src/disable/params.ts +100 -0
- package/src/tools/openclaw-workclaw-cron/src/disable/sync.ts +127 -0
- package/src/tools/openclaw-workclaw-cron/src/enable/params.ts +100 -0
- package/src/tools/openclaw-workclaw-cron/src/enable/sync.ts +127 -0
- package/src/tools/openclaw-workclaw-cron/src/notify/sync.ts +148 -0
- package/src/tools/openclaw-workclaw-cron/src/remove/params.ts +109 -0
- package/src/tools/openclaw-workclaw-cron/src/remove/sync.ts +127 -0
- package/src/tools/openclaw-workclaw-cron/src/update/params.ts +197 -0
- package/src/tools/openclaw-workclaw-cron/src/update/sync.ts +161 -0
- package/src/tools/openclaw-workclaw-cron/types/index.ts +55 -0
- package/src/tools/openclaw-workclaw-cron/utils/index.ts +141 -0
- package/src/types.ts +60 -0
- package/src/utils/content.ts +40 -0
- package/templates/IDENTITY.md +14 -0
- package/templates/SOUL.md +0 -0
- package/tsconfig.json +11 -0
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Skills list handler - 处理 SKILLS_LIST 事件
|
|
3
|
+
* 从 OpenClaw 获取 skills 列表并通过回调返回
|
|
4
|
+
*/
|
|
5
|
+
import { readFileSync } from 'node:fs'
|
|
6
|
+
|
|
7
|
+
export interface SkillsListEvent {
|
|
8
|
+
agentId: string | number
|
|
9
|
+
userId: string | number
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface SkillItem {
|
|
13
|
+
typeStr: string
|
|
14
|
+
name: string
|
|
15
|
+
description: string
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface SkillsListCallbackPayload {
|
|
19
|
+
userId: number
|
|
20
|
+
agentId: number
|
|
21
|
+
dataList: SkillItem[]
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* 处理 SKILLS_LIST 事件
|
|
26
|
+
* @param event - 事件数据
|
|
27
|
+
* @param baseUrl - 回调基础 URL
|
|
28
|
+
* @param token - 鉴权 token
|
|
29
|
+
* @param log 日志对象(可选)
|
|
30
|
+
* @param {Function} [log.info] - 信息日志函数
|
|
31
|
+
* @param {Function} [log.error] - 错误日志函数
|
|
32
|
+
*/
|
|
33
|
+
export async function handleSkillsListEvent(
|
|
34
|
+
event: SkillsListEvent,
|
|
35
|
+
baseUrl: string,
|
|
36
|
+
token: string,
|
|
37
|
+
log?: {
|
|
38
|
+
info?: (msg: string) => void
|
|
39
|
+
error?: (msg: string) => void
|
|
40
|
+
},
|
|
41
|
+
): Promise<void> {
|
|
42
|
+
const agentId = Number(event.agentId)
|
|
43
|
+
const userId = Number(event.userId)
|
|
44
|
+
|
|
45
|
+
log?.info?.(`SkillsList: Handling SKILLS_LIST event for agentId: ${agentId}, userId: ${userId}`)
|
|
46
|
+
|
|
47
|
+
try {
|
|
48
|
+
// 从 OpenClaw 获取 skills 列表
|
|
49
|
+
const skills = await fetchSkillsFromOpenClaw(agentId, userId, log)
|
|
50
|
+
|
|
51
|
+
// 构建回调 payload
|
|
52
|
+
const callbackPayload: SkillsListCallbackPayload = {
|
|
53
|
+
userId,
|
|
54
|
+
agentId,
|
|
55
|
+
dataList: skills,
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// 发送回调
|
|
59
|
+
const callbackUrl = `${baseUrl.replace(/\/$/, '')}/open-apis/v1/claw/push/skills`
|
|
60
|
+
await sendSkillsListCallback(callbackUrl, callbackPayload, token, log)
|
|
61
|
+
|
|
62
|
+
log?.info?.(`SkillsList: Successfully processed SKILLS_LIST event, sent ${skills.length} skills`)
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
log?.error?.(`SkillsList: Failed to handle SKILLS_LIST event: ${String(error)}`)
|
|
66
|
+
throw error
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* 从 OpenClaw 获取 skills 列表
|
|
72
|
+
* 包括全局技能和智能体工作区的专有技能
|
|
73
|
+
*/
|
|
74
|
+
async function fetchSkillsFromOpenClaw(
|
|
75
|
+
agentId: number,
|
|
76
|
+
_userId: number,
|
|
77
|
+
log?: {
|
|
78
|
+
info?: (msg: string) => void
|
|
79
|
+
error?: (msg: string) => void
|
|
80
|
+
},
|
|
81
|
+
): Promise<SkillItem[]> {
|
|
82
|
+
log?.info?.(`SkillsList: Fetching skills from OpenClaw for agentId: ${agentId}`)
|
|
83
|
+
|
|
84
|
+
try {
|
|
85
|
+
// 使用 OpenClaw CLI 获取 skills 列表(全局技能)
|
|
86
|
+
const { execSync } = await import('node:child_process')
|
|
87
|
+
|
|
88
|
+
const result = execSync('openclaw skills list', {
|
|
89
|
+
encoding: 'utf-8',
|
|
90
|
+
timeout: 30000, // 30秒超时
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
// 解析输出
|
|
94
|
+
const globalSkills = parseSkillsListOutput(result)
|
|
95
|
+
|
|
96
|
+
// 检查智能体工作区中的专有技能
|
|
97
|
+
const agentWorkspaceSkills = await fetchAgentWorkspaceSkills(agentId, log)
|
|
98
|
+
|
|
99
|
+
// 合并技能列表,去重
|
|
100
|
+
const allSkills = mergeSkills(globalSkills, agentWorkspaceSkills)
|
|
101
|
+
|
|
102
|
+
log?.info?.(`SkillsList: Found ${allSkills.length} skills (${globalSkills.length} global, ${agentWorkspaceSkills.length} agent-specific)`)
|
|
103
|
+
return allSkills
|
|
104
|
+
}
|
|
105
|
+
catch (error) {
|
|
106
|
+
log?.error?.(`SkillsList: Error fetching skills: ${String(error)}`)
|
|
107
|
+
// 如果获取失败,返回空列表
|
|
108
|
+
return []
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* 获取智能体工作区中的专有技能
|
|
114
|
+
*/
|
|
115
|
+
async function fetchAgentWorkspaceSkills(
|
|
116
|
+
agentId: number,
|
|
117
|
+
log?: {
|
|
118
|
+
info?: (msg: string) => void
|
|
119
|
+
error?: (msg: string) => void
|
|
120
|
+
},
|
|
121
|
+
): Promise<SkillItem[]> {
|
|
122
|
+
const skills: SkillItem[] = []
|
|
123
|
+
|
|
124
|
+
try {
|
|
125
|
+
// 智能体工作区路径
|
|
126
|
+
const { homedir } = await import('node:os')
|
|
127
|
+
const { join } = await import('node:path')
|
|
128
|
+
const agentWorkspace = join(homedir(), '.openclaw', 'agents', `openclaw-workclaw-${agentId}`)
|
|
129
|
+
const skillsDir = join(agentWorkspace, 'skills')
|
|
130
|
+
|
|
131
|
+
// 检查技能目录是否存在
|
|
132
|
+
const { existsSync, readdirSync, statSync } = await import('node:fs')
|
|
133
|
+
if (!existsSync(skillsDir)) {
|
|
134
|
+
log?.info?.(`SkillsList: No skills directory found in agent workspace: ${skillsDir}`)
|
|
135
|
+
return skills
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// 读取技能目录中的子目录(每个子目录是一个技能)
|
|
139
|
+
const items = readdirSync(skillsDir)
|
|
140
|
+
for (const item of items) {
|
|
141
|
+
const itemPath = join(skillsDir, item)
|
|
142
|
+
const stat = statSync(itemPath)
|
|
143
|
+
|
|
144
|
+
// 如果是目录,检查里面的技能文件
|
|
145
|
+
if (stat.isDirectory()) {
|
|
146
|
+
const skillFiles = readdirSync(itemPath)
|
|
147
|
+
for (const skillFile of skillFiles) {
|
|
148
|
+
if (skillFile.endsWith('.md') || skillFile.endsWith('.prose')) {
|
|
149
|
+
const skillFilePath = join(itemPath, skillFile)
|
|
150
|
+
const skill = parseSkillFile(skillFilePath, item, log)
|
|
151
|
+
if (skill) {
|
|
152
|
+
skills.push(skill)
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
log?.info?.(`SkillsList: Found ${skills.length} agent-specific skills in ${skillsDir}`)
|
|
160
|
+
}
|
|
161
|
+
catch (error) {
|
|
162
|
+
log?.error?.(`SkillsList: Error reading agent workspace skills: ${String(error)}`)
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return skills
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* 解析技能文件
|
|
170
|
+
*/
|
|
171
|
+
function parseSkillFile(
|
|
172
|
+
filePath: string,
|
|
173
|
+
skillName: string,
|
|
174
|
+
log?: {
|
|
175
|
+
info?: (msg: string) => void
|
|
176
|
+
error?: (msg: string) => void
|
|
177
|
+
},
|
|
178
|
+
): SkillItem | null {
|
|
179
|
+
try {
|
|
180
|
+
const content = readFileSync(filePath, 'utf-8')
|
|
181
|
+
|
|
182
|
+
// 提取描述(从文件内容中)
|
|
183
|
+
let description = 'Agent-specific skill'
|
|
184
|
+
const lines = content.split('\n')
|
|
185
|
+
for (const line of lines) {
|
|
186
|
+
if (line.startsWith('# ')) {
|
|
187
|
+
description = line.substring(2).trim()
|
|
188
|
+
break
|
|
189
|
+
}
|
|
190
|
+
else if (line.startsWith('summary:') || line.startsWith('Summary:')) {
|
|
191
|
+
description = line.substring(line.indexOf(':') + 1).trim()
|
|
192
|
+
break
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return {
|
|
197
|
+
typeStr: 'skill',
|
|
198
|
+
name: skillName,
|
|
199
|
+
description,
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
catch (error) {
|
|
203
|
+
log?.error?.(`SkillsList: Error parsing skill file ${filePath}: ${String(error)}`)
|
|
204
|
+
return null
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* 合并技能列表,去重
|
|
210
|
+
*/
|
|
211
|
+
function mergeSkills(globalSkills: SkillItem[], agentSkills: SkillItem[]): SkillItem[] {
|
|
212
|
+
const skillMap = new Map<string, SkillItem>()
|
|
213
|
+
|
|
214
|
+
// 先添加全局技能
|
|
215
|
+
for (const skill of globalSkills) {
|
|
216
|
+
skillMap.set(skill.name, skill)
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// 再添加智能体专有技能(会覆盖同名的全局技能)
|
|
220
|
+
for (const skill of agentSkills) {
|
|
221
|
+
skillMap.set(skill.name, skill)
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
return Array.from(skillMap.values())
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* 解析 openclaw skills list 的输出
|
|
229
|
+
*/
|
|
230
|
+
function parseSkillsListOutput(output: string): SkillItem[] {
|
|
231
|
+
const skills: SkillItem[] = []
|
|
232
|
+
const lines = output.split('\n')
|
|
233
|
+
|
|
234
|
+
// 查找表格内容(从表头后开始)
|
|
235
|
+
let inTable = false
|
|
236
|
+
let headerLine = -1
|
|
237
|
+
|
|
238
|
+
for (let i = 0; i < lines.length; i++) {
|
|
239
|
+
const line = lines[i]
|
|
240
|
+
|
|
241
|
+
// 找到表头行(包含 Status | Skill | Description | Source)
|
|
242
|
+
if (line.includes('Status') && line.includes('Skill') && line.includes('Description')) {
|
|
243
|
+
headerLine = i
|
|
244
|
+
inTable = true
|
|
245
|
+
continue
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// 跳过分隔线
|
|
249
|
+
if (inTable && (line.startsWith('├') || line.startsWith('┌') || line.startsWith('└'))) {
|
|
250
|
+
continue
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// 解析表格行
|
|
254
|
+
if (inTable && line.startsWith('│') && i > headerLine + 1) {
|
|
255
|
+
const parsed = parseSkillsTableRow(line)
|
|
256
|
+
if (parsed) {
|
|
257
|
+
skills.push(parsed)
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// 表格结束
|
|
262
|
+
if (inTable && line.startsWith('└')) {
|
|
263
|
+
break
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
return skills
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* 解析单行表格数据
|
|
272
|
+
*/
|
|
273
|
+
function parseSkillsTableRow(line: string): SkillItem | null {
|
|
274
|
+
// 移除开头的 │ 和结尾的 │
|
|
275
|
+
const content = line.replace(/^│\s*/, '').replace(/\s*│$/, '')
|
|
276
|
+
|
|
277
|
+
// 按 │ 分割列
|
|
278
|
+
const columns = content.split('│').map(col => col.trim())
|
|
279
|
+
|
|
280
|
+
if (columns.length < 4) {
|
|
281
|
+
return null
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
const status = columns[0]
|
|
285
|
+
const skillName = columns[1]
|
|
286
|
+
const description = columns[2]
|
|
287
|
+
|
|
288
|
+
// 只返回 ready 状态的 skills
|
|
289
|
+
if (!status.includes('✓') && !status.includes('ready')) {
|
|
290
|
+
return null
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// 提取 skill 名称(移除 emoji)
|
|
294
|
+
const cleanName = skillName.replace(/[\u{1F300}-\u{1F9FF}]/gu, '').trim()
|
|
295
|
+
|
|
296
|
+
return {
|
|
297
|
+
typeStr: 'skill',
|
|
298
|
+
name: cleanName,
|
|
299
|
+
description,
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* 发送 skills 列表回调
|
|
305
|
+
*/
|
|
306
|
+
async function sendSkillsListCallback(
|
|
307
|
+
callbackUrl: string,
|
|
308
|
+
payload: SkillsListCallbackPayload,
|
|
309
|
+
token: string,
|
|
310
|
+
log?: {
|
|
311
|
+
info?: (msg: string) => void
|
|
312
|
+
error?: (msg: string) => void
|
|
313
|
+
},
|
|
314
|
+
): Promise<void> {
|
|
315
|
+
log?.info?.(`SkillsList: Sending callback to ${callbackUrl}`)
|
|
316
|
+
|
|
317
|
+
const response = await fetch(callbackUrl, {
|
|
318
|
+
method: 'POST',
|
|
319
|
+
headers: {
|
|
320
|
+
'Content-Type': 'application/json',
|
|
321
|
+
'Authorization': `Bearer ${token}`,
|
|
322
|
+
},
|
|
323
|
+
body: JSON.stringify(payload),
|
|
324
|
+
})
|
|
325
|
+
|
|
326
|
+
if (!response.ok) {
|
|
327
|
+
const errorText = await response.text().catch(() => 'Unknown error')
|
|
328
|
+
throw new Error(`Callback failed: ${response.status} ${errorText}`)
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
log?.info?.(`SkillsList: Callback sent successfully`)
|
|
332
|
+
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tools List 事件处理器
|
|
3
|
+
* 处理云端下发的 TOOLS_LIST EVENT 消息
|
|
4
|
+
* 从 OpenClaw 获取 tools 列表并通过 HTTP 回调返回
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { fetch } from 'undici'
|
|
8
|
+
|
|
9
|
+
export interface ToolsListEvent {
|
|
10
|
+
agentId: string | number
|
|
11
|
+
userId: string | number
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface ToolItem {
|
|
15
|
+
typeStr: string
|
|
16
|
+
name: string
|
|
17
|
+
description: string
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface ToolsListCallbackPayload {
|
|
21
|
+
userId: number
|
|
22
|
+
agentId: number
|
|
23
|
+
dataList: ToolItem[]
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* 处理 TOOLS_LIST 事件
|
|
28
|
+
* @param event - 事件数据
|
|
29
|
+
* @param baseUrl - 回调基础 URL
|
|
30
|
+
* @param token - 鉴权 token
|
|
31
|
+
* @param log - 日志对象(可选)
|
|
32
|
+
*/
|
|
33
|
+
export async function handleToolsListEvent(
|
|
34
|
+
event: ToolsListEvent,
|
|
35
|
+
baseUrl: string,
|
|
36
|
+
token: string,
|
|
37
|
+
log?: {
|
|
38
|
+
info?: (msg: string) => void
|
|
39
|
+
error?: (msg: string) => void
|
|
40
|
+
},
|
|
41
|
+
): Promise<void> {
|
|
42
|
+
const agentId = Number(event.agentId)
|
|
43
|
+
const userId = Number(event.userId)
|
|
44
|
+
|
|
45
|
+
log?.info?.(`ToolsList: Handling TOOLS_LIST event for agentId: ${agentId}, userId: ${userId}`)
|
|
46
|
+
|
|
47
|
+
try {
|
|
48
|
+
// 从 OpenClaw 获取 tools 列表
|
|
49
|
+
const tools = await fetchToolsFromOpenClaw(agentId, userId, log)
|
|
50
|
+
|
|
51
|
+
// 构建回调 payload
|
|
52
|
+
const callbackPayload: ToolsListCallbackPayload = {
|
|
53
|
+
userId,
|
|
54
|
+
agentId,
|
|
55
|
+
dataList: tools,
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// 发送回调
|
|
59
|
+
const callbackUrl = `${baseUrl.replace(/\/$/, '')}/open-apis/v1/claw/push/tools`
|
|
60
|
+
await sendToolsListCallback(callbackUrl, callbackPayload, token, log)
|
|
61
|
+
|
|
62
|
+
log?.info?.(`ToolsList: Successfully processed TOOLS_LIST event, sent ${tools.length} tools`)
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
log?.error?.(`ToolsList: Failed to handle TOOLS_LIST event: ${String(error)}`)
|
|
66
|
+
throw error
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* 从 OpenClaw 获取 tools 列表
|
|
72
|
+
* 根据 agent 的 tools.allow 配置决定返回哪些工具
|
|
73
|
+
*/
|
|
74
|
+
async function fetchToolsFromOpenClaw(
|
|
75
|
+
agentId: number,
|
|
76
|
+
_userId: number,
|
|
77
|
+
log?: {
|
|
78
|
+
info?: (msg: string) => void
|
|
79
|
+
error?: (msg: string) => void
|
|
80
|
+
},
|
|
81
|
+
): Promise<ToolItem[]> {
|
|
82
|
+
log?.info?.(`ToolsList: Fetching tools from OpenClaw for agentId: ${agentId}`)
|
|
83
|
+
|
|
84
|
+
// OpenClaw 所有内置工具列表
|
|
85
|
+
const allTools: ToolItem[] = [
|
|
86
|
+
{ typeStr: 'function', name: 'web_search', description: 'Search the web for information using Brave Search API.' },
|
|
87
|
+
{ typeStr: 'function', name: 'web_fetch', description: 'Fetch and read content from a URL.' },
|
|
88
|
+
{ typeStr: 'function', name: 'sessions_list', description: 'List all active agent sessions.' },
|
|
89
|
+
{ typeStr: 'function', name: 'sessions_history', description: 'Get the history of a specific session.' },
|
|
90
|
+
{ typeStr: 'function', name: 'memory_search', description: 'Search memory for past conversations and information.' },
|
|
91
|
+
{ typeStr: 'function', name: 'memory_get', description: 'Get specific memory by ID.' },
|
|
92
|
+
{ typeStr: 'function', name: 'gateway', description: 'Gateway control operations.' },
|
|
93
|
+
{ typeStr: 'function', name: 'exec', description: 'Execute shell commands.' },
|
|
94
|
+
]
|
|
95
|
+
|
|
96
|
+
try {
|
|
97
|
+
// 读取 OpenClaw 配置文件
|
|
98
|
+
const { readFile } = await import('node:fs/promises')
|
|
99
|
+
const { homedir } = await import('node:os')
|
|
100
|
+
const path = await import('node:path')
|
|
101
|
+
|
|
102
|
+
const configPath = path.join(homedir(), '.openclaw', 'openclaw.json')
|
|
103
|
+
const configContent = await readFile(configPath, 'utf-8')
|
|
104
|
+
const config = JSON.parse(configContent)
|
|
105
|
+
|
|
106
|
+
// 查找对应的 agent 配置
|
|
107
|
+
const agentConfig = config.agents?.list?.find((agent: any) =>
|
|
108
|
+
String(agent.id) === String(agentId)
|
|
109
|
+
|| agent.id === `openclaw-workclaw-${agentId}`,
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
// 获取 tools.allow 配置
|
|
113
|
+
const allowTools = agentConfig?.tools?.allow
|
|
114
|
+
|
|
115
|
+
// 如果没有 allow 配置,或者 allow 是 ["*"],返回所有工具
|
|
116
|
+
if (!allowTools || allowTools.length === 0 || allowTools.includes('*')) {
|
|
117
|
+
log?.info?.(`ToolsList: Agent has no allow restriction or allow=[*], returning all ${allTools.length} tools`)
|
|
118
|
+
return allTools
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// 如果有具体的 allow 列表,只返回列表中的工具
|
|
122
|
+
const filteredTools = allTools.filter(tool => allowTools.includes(tool.name))
|
|
123
|
+
log?.info?.(`ToolsList: Agent allow=${JSON.stringify(allowTools)}, returning ${filteredTools.length} tools`)
|
|
124
|
+
return filteredTools
|
|
125
|
+
}
|
|
126
|
+
catch (error) {
|
|
127
|
+
log?.error?.(`ToolsList: Error reading agent config: ${String(error)}, returning all tools`)
|
|
128
|
+
// 如果读取配置失败,返回所有工具
|
|
129
|
+
return allTools
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* 发送 tools 列表回调
|
|
135
|
+
*/
|
|
136
|
+
async function sendToolsListCallback(
|
|
137
|
+
callbackUrl: string,
|
|
138
|
+
payload: ToolsListCallbackPayload,
|
|
139
|
+
token: string,
|
|
140
|
+
log?: {
|
|
141
|
+
info?: (msg: string) => void
|
|
142
|
+
error?: (msg: string) => void
|
|
143
|
+
},
|
|
144
|
+
): Promise<void> {
|
|
145
|
+
log?.info?.(`ToolsList: Sending callback to ${callbackUrl}`)
|
|
146
|
+
|
|
147
|
+
const response = await fetch(callbackUrl, {
|
|
148
|
+
method: 'POST',
|
|
149
|
+
headers: {
|
|
150
|
+
'Content-Type': 'application/json',
|
|
151
|
+
'Authorization': `Bearer ${token}`,
|
|
152
|
+
},
|
|
153
|
+
body: JSON.stringify(payload),
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
if (!response.ok) {
|
|
157
|
+
const errorText = await response.text()
|
|
158
|
+
throw new Error(`Callback failed: ${response.status} ${response.statusText} - ${errorText}`)
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
log?.info?.(`ToolsList: Callback sent successfully: ${response.status}`)
|
|
162
|
+
}
|