@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.
Files changed (48) hide show
  1. package/README.md +325 -0
  2. package/index.ts +298 -0
  3. package/openclaw.plugin.json +10 -0
  4. package/package.json +43 -0
  5. package/skills/openclaw-workclaw-cron/SKILL.md +458 -0
  6. package/src/accounts.ts +287 -0
  7. package/src/api/accounts-api.ts +157 -0
  8. package/src/api/prompts-api.ts +123 -0
  9. package/src/api/session-api.ts +247 -0
  10. package/src/api/skills-api.ts +74 -0
  11. package/src/api/workspace.ts +43 -0
  12. package/src/channel.ts +227 -0
  13. package/src/config-schema.ts +110 -0
  14. package/src/connection/workclaw-client.ts +656 -0
  15. package/src/gateway/agent-handlers.ts +557 -0
  16. package/src/gateway/config-writer.ts +311 -0
  17. package/src/gateway/message-context.ts +422 -0
  18. package/src/gateway/message-dispatcher.ts +601 -0
  19. package/src/gateway/reconnect.ts +149 -0
  20. package/src/gateway/skills-handler.ts +759 -0
  21. package/src/gateway/skills-list-handler.ts +332 -0
  22. package/src/gateway/tools-list-handler.ts +162 -0
  23. package/src/gateway/workclaw-gateway.ts +521 -0
  24. package/src/media/upload.ts +168 -0
  25. package/src/outbound/index.ts +183 -0
  26. package/src/outbound/workclaw-sender.ts +157 -0
  27. package/src/runtime.ts +400 -0
  28. package/src/send.ts +1 -0
  29. package/src/tools/openclaw-workclaw-cron/api/index.ts +326 -0
  30. package/src/tools/openclaw-workclaw-cron/index.ts +39 -0
  31. package/src/tools/openclaw-workclaw-cron/src/add/params.ts +176 -0
  32. package/src/tools/openclaw-workclaw-cron/src/add/sync.ts +188 -0
  33. package/src/tools/openclaw-workclaw-cron/src/disable/params.ts +100 -0
  34. package/src/tools/openclaw-workclaw-cron/src/disable/sync.ts +127 -0
  35. package/src/tools/openclaw-workclaw-cron/src/enable/params.ts +100 -0
  36. package/src/tools/openclaw-workclaw-cron/src/enable/sync.ts +127 -0
  37. package/src/tools/openclaw-workclaw-cron/src/notify/sync.ts +148 -0
  38. package/src/tools/openclaw-workclaw-cron/src/remove/params.ts +109 -0
  39. package/src/tools/openclaw-workclaw-cron/src/remove/sync.ts +127 -0
  40. package/src/tools/openclaw-workclaw-cron/src/update/params.ts +197 -0
  41. package/src/tools/openclaw-workclaw-cron/src/update/sync.ts +161 -0
  42. package/src/tools/openclaw-workclaw-cron/types/index.ts +55 -0
  43. package/src/tools/openclaw-workclaw-cron/utils/index.ts +141 -0
  44. package/src/types.ts +60 -0
  45. package/src/utils/content.ts +40 -0
  46. package/templates/IDENTITY.md +14 -0
  47. package/templates/SOUL.md +0 -0
  48. package/tsconfig.json +11 -0
@@ -0,0 +1,656 @@
1
+ import { Agent, fetch as undiciFetch } from 'undici'
2
+ import { getOpenclawWorkclawLogger } from '../runtime.js'
3
+
4
+ interface WorkClawTokenCache {
5
+ token: string
6
+ expiresAt: number
7
+ pending?: Promise<string>
8
+ }
9
+
10
+ const tokenCache = new Map<string, WorkClawTokenCache>()
11
+
12
+ export function clearOpenclawWorkclawTokenCache(cacheKey: string): void {
13
+ tokenCache.delete(cacheKey)
14
+ }
15
+
16
+ export function normalizeBaseUrl(value: string | undefined): string {
17
+ const raw = (value || 'https://open.workbrain.cn/open-apis').trim()
18
+ return raw.endsWith('/') ? raw.slice(0, -1) : raw
19
+ }
20
+
21
+ function createWsEndpoint(baseUrl: string): string {
22
+ const wsBase = baseUrl.replace(/^https?:\/\//i, match =>
23
+ match.toLowerCase() === 'https://' ? 'wss://' : 'ws://')
24
+ return `${wsBase}/v1/ws/connect`
25
+ }
26
+
27
+ export async function doFetchJson(
28
+ url: string,
29
+ init: RequestInit,
30
+ allowInsecureTls?: boolean,
31
+ requestTimeout?: number,
32
+ ): Promise<any> {
33
+ const dispatcher = allowInsecureTls
34
+ ? new Agent({ connect: { rejectUnauthorized: false } })
35
+ : undefined
36
+ const fetcher = allowInsecureTls ? undiciFetch : globalThis.fetch
37
+ const controller = new AbortController()
38
+ const timeoutMs = typeof requestTimeout === 'number' && requestTimeout > 0 ? requestTimeout : 30000
39
+ const timeoutId = setTimeout(() => controller.abort(), timeoutMs)
40
+
41
+ let response: any
42
+ let text = ''
43
+ try {
44
+ response = await fetcher(url, {
45
+ ...init,
46
+ signal: controller.signal,
47
+ ...(dispatcher ? { dispatcher } : {}),
48
+ } as any)
49
+ text = await response.text().catch(() => '')
50
+ }
51
+ finally {
52
+ clearTimeout(timeoutId)
53
+ }
54
+
55
+ let data: any = {}
56
+ if (text) {
57
+ try {
58
+ data = JSON.parse(text)
59
+ }
60
+ catch {
61
+ data = {}
62
+ }
63
+ }
64
+
65
+ if (!response.ok) {
66
+ const message = data?.message || text || `HTTP ${response.status}`
67
+ throw new Error(message)
68
+ }
69
+ return data
70
+ }
71
+
72
+ export interface OpenclawWorkclawConnectionConfig {
73
+ baseUrl?: string
74
+ websocketUrl?: string
75
+ appKey?: string
76
+ appSecret?: string
77
+ localIp?: string
78
+ allowInsecureTls?: boolean
79
+ requestTimeout?: number
80
+ }
81
+
82
+ export async function getOpenclawWorkclawAccessToken(
83
+ cacheKey: string,
84
+ config: OpenclawWorkclawConnectionConfig,
85
+ ): Promise<string> {
86
+ const startedAt = Date.now()
87
+ const baseUrl = normalizeBaseUrl(config.baseUrl)
88
+ const requestUrl = `${baseUrl}/authen/v1/access_token/internal`
89
+ const timeoutMs
90
+ = typeof config.requestTimeout === 'number' && config.requestTimeout > 0
91
+ ? config.requestTimeout
92
+ : 30000
93
+
94
+ getOpenclawWorkclawLogger().info(
95
+ `getOpenclawWorkclawAccessToken start cacheKey=${cacheKey} requestUrl=${requestUrl} baseUrl=${baseUrl} allowInsecureTls=${Boolean(config.allowInsecureTls)} timeoutMs=${timeoutMs}`,
96
+ )
97
+
98
+ try {
99
+ const cached = tokenCache.get(cacheKey)
100
+
101
+ if (cached && cached.token) {
102
+ const now = Date.now()
103
+ const refreshAt = cached.expiresAt - 5 * 60 * 1000
104
+ const remainingMs = cached.expiresAt - now
105
+ getOpenclawWorkclawLogger().info(
106
+ `Token cache found cacheKey=${cacheKey} expiresAt=${new Date(cached.expiresAt).toISOString()} remainingMs=${remainingMs}`,
107
+ )
108
+
109
+ if (now < refreshAt) {
110
+ getOpenclawWorkclawLogger().info(
111
+ `Using cached token cacheKey=${cacheKey} elapsedMs=${now - startedAt}`,
112
+ )
113
+ return cached.token
114
+ }
115
+
116
+ getOpenclawWorkclawLogger().info(
117
+ `Cached token near expiry/expired, refreshing cacheKey=${cacheKey} elapsedMs=${now - startedAt}`,
118
+ )
119
+ }
120
+
121
+ if (cached?.pending) {
122
+ getOpenclawWorkclawLogger().info(
123
+ `Using pending token request cacheKey=${cacheKey} elapsedMs=${Date.now() - startedAt}`,
124
+ )
125
+ return cached.pending
126
+ }
127
+
128
+ if (!config.appKey || !config.appSecret) {
129
+ getOpenclawWorkclawLogger().error(
130
+ `Missing appKey/appSecret cacheKey=${cacheKey} appKey=${String(config.appKey || '') ? 'present' : 'missing'} appSecret=${String(config.appSecret || '') ? 'present' : 'missing'}`,
131
+ )
132
+ throw new Error('Missing appKey/appSecret')
133
+ }
134
+
135
+ const pending = (async (): Promise<string> => {
136
+ getOpenclawWorkclawLogger().info(
137
+ `Requesting access token url=${requestUrl} appKeyPrefix=${String(config.appKey).slice(0, 8)}... appSecretLen=${String(config.appSecret).length}`,
138
+ )
139
+
140
+ const data = await doFetchJson(
141
+ requestUrl,
142
+ {
143
+ method: 'POST',
144
+ headers: { 'Content-Type': 'application/json' },
145
+ body: JSON.stringify({
146
+ app_key: config.appKey,
147
+ app_secret: config.appSecret,
148
+ }),
149
+ },
150
+ config.allowInsecureTls,
151
+ config.requestTimeout,
152
+ )
153
+
154
+ getOpenclawWorkclawLogger().info(
155
+ `Access token response code=${String(data?.code ?? '')} errCode=${String(data?.errCode ?? '')} errMsg=${String(data?.errMsg ?? data?.message ?? '')}`,
156
+ )
157
+
158
+ if (data?.code !== 200 || !data?.data?.accessToken) {
159
+ throw new Error(
160
+ String(data?.message || data?.errMsg || 'Failed to acquire access token'),
161
+ )
162
+ }
163
+
164
+ const token = String(data.data.accessToken)
165
+ const expiresIn = Number(data.data.expiresIn || 0)
166
+ const expiresAt = Date.now() + (expiresIn > 0 ? expiresIn * 1000 : 0)
167
+
168
+ getOpenclawWorkclawLogger().info(
169
+ `Access token acquired tokenLen=${token.length} expiresInSec=${expiresIn} expiresAt=${new Date(expiresAt).toISOString()}`,
170
+ )
171
+
172
+ tokenCache.set(cacheKey, { token, expiresAt })
173
+ return token
174
+ })()
175
+
176
+ tokenCache.set(cacheKey, {
177
+ token: cached?.token || '',
178
+ expiresAt: cached?.expiresAt || 0,
179
+ pending,
180
+ })
181
+
182
+ const token = await pending
183
+ getOpenclawWorkclawLogger().info(
184
+ `getOpenclawWorkclawAccessToken success cacheKey=${cacheKey} elapsedMs=${Date.now() - startedAt}`,
185
+ )
186
+ return token
187
+ }
188
+ catch (error) {
189
+ getOpenclawWorkclawLogger().error(
190
+ `getOpenclawWorkclawAccessToken failed cacheKey=${cacheKey} requestUrl=${requestUrl} baseUrl=${baseUrl} elapsedMs=${Date.now() - startedAt} error=${String(error)}`,
191
+ )
192
+ throw error
193
+ }
194
+ }
195
+
196
+ export async function openOpenclawWorkclawConnection(
197
+ cacheKey: string,
198
+ config: OpenclawWorkclawConnectionConfig,
199
+ ): Promise<{ endpoint: string, ticket: string }> {
200
+ const startedAt = Date.now()
201
+ const baseUrl = normalizeBaseUrl(config.baseUrl)
202
+ const requestUrl = `${baseUrl}/open-apis/v1/connections/open`
203
+ const timeoutMs
204
+ = typeof config.requestTimeout === 'number' && config.requestTimeout > 0
205
+ ? config.requestTimeout
206
+ : 30000
207
+
208
+ const appKeyRaw = String(config.appKey ?? '')
209
+ const appSecretRaw = String(config.appSecret ?? '')
210
+
211
+ getOpenclawWorkclawLogger().info(
212
+ `openOpenclawWorkclawConnection start cacheKey=${cacheKey} baseUrl=${baseUrl} requestUrl=${requestUrl} allowInsecureTls=${Boolean(config.allowInsecureTls)} timeoutMs=${timeoutMs} localIp=${String(config.localIp ?? '')} websocketUrl=${String(config.websocketUrl ?? '')} appKeyPrefix=${appKeyRaw ? `${appKeyRaw.slice(0, 8)}...` : 'missing'} appSecretLen=${appSecretRaw.length}`,
213
+ )
214
+
215
+ const tokenStartedAt = Date.now()
216
+ const token = await getOpenclawWorkclawAccessToken(cacheKey, config)
217
+ getOpenclawWorkclawLogger().info(
218
+ `openOpenclawWorkclawConnection got access token cacheKey=${cacheKey} tokenLen=${token.length} elapsedMs=${Date.now() - tokenStartedAt}`,
219
+ )
220
+
221
+ const payload = {
222
+ appKey: config.appKey,
223
+ appSecret: config.appSecret,
224
+ subscriptions: [
225
+ { type: 'CALLBACK', topic: 'AGENT_MESSAGE' },
226
+ { type: 'EVENT', topic: '*' },
227
+ { type: 'SYSTEM', topic: '*' },
228
+ ],
229
+ ...(config.localIp ? { localIp: config.localIp } : {}),
230
+ }
231
+
232
+ getOpenclawWorkclawLogger().info(
233
+ `Opening connection url=${requestUrl} cacheKey=${cacheKey} subscriptions=${payload.subscriptions.length}`,
234
+ )
235
+
236
+ const data = await doFetchJson(
237
+ requestUrl,
238
+ {
239
+ method: 'POST',
240
+ headers: {
241
+ 'Content-Type': 'application/json',
242
+ 'Authorization': `Bearer ${token}`,
243
+ },
244
+ body: JSON.stringify(payload),
245
+ },
246
+ config.allowInsecureTls,
247
+ config.requestTimeout,
248
+ )
249
+
250
+ getOpenclawWorkclawLogger().info(
251
+ `Open connection response code=${String(data?.code ?? '')} errCode=${String(data?.errCode ?? '')} errMsg=${String(data?.errMsg ?? data?.message ?? '')} hasTicket=${Boolean(data?.data?.ticket)} hasEndpoint=${Boolean(data?.data?.endpoint)}`,
252
+ )
253
+
254
+ if (data?.code !== 200 || !data?.data?.ticket) {
255
+ throw new Error(String(data?.message || data?.errMsg || 'Failed to open connection'))
256
+ }
257
+
258
+ const ticket = String(data.data.ticket)
259
+
260
+ let endpointSource: 'config' | 'api' | 'derived' = 'derived'
261
+
262
+ // 优先使用配置中的 websocketUrl,其次使用 API 返回的 endpoint,最后使用默认构建的 endpoint
263
+ const endpoint = config.websocketUrl
264
+ ? ((endpointSource = 'config'), String(config.websocketUrl))
265
+ : data?.data?.endpoint
266
+ ? ((endpointSource = 'api'), String(data.data.endpoint))
267
+ : ((endpointSource = 'derived'), createWsEndpoint(baseUrl))
268
+
269
+ getOpenclawWorkclawLogger().info(
270
+ `openOpenclawWorkclawConnection success cacheKey=${cacheKey} endpoint=${endpoint} endpointSource=${endpointSource} ticketLen=${ticket.length} elapsedMs=${Date.now() - startedAt}`,
271
+ )
272
+
273
+ return { endpoint, ticket }
274
+ }
275
+
276
+ export interface AgentInstance {
277
+ id: string
278
+ createTime: string
279
+ updateTime: string
280
+ futureId: string
281
+ phone: string
282
+ nickName: string
283
+ imageId: string
284
+ level: number
285
+ identity: string
286
+ type: string
287
+ name: string
288
+ gender: string
289
+ age: number | null
290
+ tcId: string | null
291
+ city: string | null
292
+ loginType: string | null
293
+ status: string
294
+ publicScope: string
295
+ ip: string | null
296
+ phoneName: string | null
297
+ tip: string | null
298
+ email: string | null
299
+ featureId: string | null
300
+ job: string | null
301
+ newUser: boolean
302
+ imageKnBase: string | null
303
+ recommend: string | null
304
+ comment: string | null
305
+ treeKnbase: string | null
306
+ preferredLanguage: string
307
+ timezone: string
308
+ introduction: string
309
+ backgroundId: string
310
+ voiceId: string
311
+ characterSettings: string | null
312
+ personFeatures: string | null
313
+ learningFeatures: string | null
314
+ workFeatures: string | null
315
+ socializeFeatures: string | null
316
+ useKnowledge: boolean
317
+ roomOnlyKnowledge: boolean
318
+ roomWebSearch: boolean
319
+ abilityId: string | null
320
+ baiduCensoringStrategy: string | null
321
+ knowledgeCheckStrategy: string | null
322
+ interestTags: string | null
323
+ temporaryId: string | null
324
+ treeBatchNo: string | null
325
+ allowJoinSpace: boolean
326
+ modelId: string
327
+ orgId: string | null
328
+ qrCodeUrl: string | null
329
+ locationPoint: {
330
+ x: number
331
+ y: number
332
+ } | null
333
+ hot: string
334
+ score: number
335
+ professionalUser: string | null
336
+ autoCreateFace: boolean
337
+ liveId: string | null
338
+ liveStartTime: string | null
339
+ isIpAgent: number
340
+ isAdopted: number
341
+ }
342
+
343
+ /**
344
+ * 获取智能体列表
345
+ * POST /open-apis/instance/list
346
+ */
347
+ export async function getAgentInstances(
348
+ cacheKey: string,
349
+ config: OpenclawWorkclawConnectionConfig,
350
+ ): Promise<AgentInstance[]> {
351
+ const baseUrl = normalizeBaseUrl(config.baseUrl)
352
+ const token = await getOpenclawWorkclawAccessToken(cacheKey, config)
353
+
354
+ const payload = {
355
+ appKey: config.appKey,
356
+ }
357
+
358
+ getOpenclawWorkclawLogger().info(`Getting agent instances from ${baseUrl}/open-apis/instance/list`)
359
+ getOpenclawWorkclawLogger().info(`Payload:`, JSON.stringify(payload, null, 2))
360
+
361
+ const data = await doFetchJson(
362
+ `${baseUrl}/open-apis/instance/list`,
363
+ {
364
+ method: 'POST',
365
+ headers: {
366
+ 'Content-Type': 'application/json',
367
+ 'Authorization': `Bearer ${token}`,
368
+ },
369
+ body: JSON.stringify(payload),
370
+ },
371
+ config.allowInsecureTls,
372
+ config.requestTimeout,
373
+ )
374
+ getOpenclawWorkclawLogger().info(`Agent instances response:`, JSON.stringify(data, null, 2))
375
+
376
+ if (data?.code !== 200 || !Array.isArray(data?.data)) {
377
+ throw new Error(data?.message || 'Failed to get agent instances')
378
+ }
379
+
380
+ return data.data as AgentInstance[]
381
+ }
382
+
383
+ export interface WorkClawMessageParams {
384
+ cacheKey: string
385
+ config: OpenclawWorkclawConnectionConfig
386
+ agentId: string | number
387
+ receiveId: string | number
388
+ msgType: string
389
+ content: string
390
+ openConversationId?: string
391
+ replayMsgId?: string
392
+ endpoint?: string
393
+ last?: boolean
394
+ }
395
+
396
+ /**
397
+ * 发送主动消息(非回复)
398
+ * POST /im/v1/messages
399
+ */
400
+ export async function sendOpenclawWorkclawMessage(params: WorkClawMessageParams): Promise<string> {
401
+ const baseUrl = normalizeBaseUrl(params.config.baseUrl)
402
+ const token = await getOpenclawWorkclawAccessToken(params.cacheKey, params.config)
403
+
404
+ // 主动消息参数
405
+ const payload: any = {
406
+ agentId: params.agentId,
407
+ receiveId: params.receiveId,
408
+ msgType: params.msgType,
409
+ content: params.content,
410
+ }
411
+
412
+ // 添加 last 字段(仅当为 true 时)
413
+ if (params.last === true) {
414
+ payload.is_last = true
415
+ }
416
+
417
+ // openConversationId 非必填,API 字段名为 conversationId
418
+ if (params.openConversationId) {
419
+ payload.conversationId = params.openConversationId
420
+ }
421
+
422
+ const endpoint = params.endpoint || '/open-apis/im/v1/messages'
423
+ getOpenclawWorkclawLogger().info(`Sending proactive message to ${baseUrl}${endpoint}`)
424
+ getOpenclawWorkclawLogger().info(`Payload:`, JSON.stringify(payload, null, 2))
425
+
426
+ const data = await doFetchJson(
427
+ `${baseUrl}${endpoint}`,
428
+ {
429
+ method: 'POST',
430
+ headers: {
431
+ 'Content-Type': 'application/json',
432
+ 'Authorization': `Bearer ${token}`,
433
+ },
434
+ body: JSON.stringify(payload),
435
+ },
436
+ params.config.allowInsecureTls,
437
+ params.config.requestTimeout,
438
+ )
439
+ getOpenclawWorkclawLogger().info(`Send message response:`, JSON.stringify(data, null, 2))
440
+
441
+ if (data?.code !== 200 || !data?.data?.msgId) {
442
+ throw new Error(data?.message || 'Failed to send message')
443
+ }
444
+ return String(data.data.msgId)
445
+ }
446
+
447
+ /**
448
+ * 发送回复消息
449
+ * POST /im/v1/messages/{messageId}/reply
450
+ */
451
+ export async function sendOpenclawWorkclawReplyMessage(params: WorkClawMessageParams & { messageId: string }): Promise<string> {
452
+ const baseUrl = normalizeBaseUrl(params.config.baseUrl)
453
+ const token = await getOpenclawWorkclawAccessToken(params.cacheKey, params.config)
454
+
455
+ // 回复消息参数
456
+ // 注意:虽然 API 文档说不需要 openConversationId,但为了保险起见还是会传递
457
+ const payload: any = {
458
+ agentId: params.agentId,
459
+ receiveId: params.receiveId,
460
+ msgType: params.msgType,
461
+ content: params.content,
462
+ }
463
+
464
+ getOpenclawWorkclawLogger().info(`Reply payload is last : ${params.last} `)
465
+
466
+ // 添加 last 字段(仅当为 true 时)
467
+ if (params.last === true) {
468
+ payload.is_last = true
469
+ }
470
+
471
+ // 如果提供了 openConversationId,也添加到 payload 中(以防 API 需要)
472
+ if (params.openConversationId) {
473
+ payload.conversationId = params.openConversationId
474
+ getOpenclawWorkclawLogger().info(`Reply payload includes conversationId: ${params.openConversationId}`)
475
+ }
476
+ else {
477
+ getOpenclawWorkclawLogger().info(`Reply payload does NOT include conversationId (openConversationId is empty)`)
478
+ }
479
+
480
+ const endpoint = params.endpoint || `/open-apis/im/v1/messages/${params.messageId}/reply`
481
+ getOpenclawWorkclawLogger().info(`Sending reply message to ${baseUrl}${endpoint}`)
482
+ getOpenclawWorkclawLogger().info(`Reply Payload:`, JSON.stringify(payload, null, 2))
483
+
484
+ const data = await doFetchJson(
485
+ `${baseUrl}${endpoint}`,
486
+ {
487
+ method: 'POST',
488
+ headers: {
489
+ 'Content-Type': 'application/json',
490
+ 'Authorization': `Bearer ${token}`,
491
+ },
492
+ body: JSON.stringify(payload),
493
+ },
494
+ params.config.allowInsecureTls,
495
+ params.config.requestTimeout,
496
+ )
497
+ getOpenclawWorkclawLogger().info(`Send reply response:`, JSON.stringify(data, null, 2))
498
+
499
+ if (data?.code !== 200 || !data?.data?.msgId) {
500
+ throw new Error(data?.message || 'Failed to send reply')
501
+ }
502
+ return String(data.data.msgId)
503
+ }
504
+
505
+ export function resolveOpenclawWorkclawMessage(
506
+ text: string,
507
+ mediaUrl?: string,
508
+ ): { msgType: string, content: string } {
509
+ if (!mediaUrl || text.trim()) {
510
+ return { msgType: 'text', content: text }
511
+ }
512
+ const url = mediaUrl.trim()
513
+ const lower = url.toLowerCase()
514
+ const isAudio
515
+ = lower.endsWith('.mp3')
516
+ || lower.endsWith('.wav')
517
+ || lower.endsWith('.aac')
518
+ || lower.endsWith('.m4a')
519
+ || lower.endsWith('.ogg')
520
+ const payload = JSON.stringify({ url })
521
+ return { msgType: isAudio ? 'audio' : 'image', content: payload }
522
+ }
523
+
524
+ export interface CronJobPayload {
525
+ messageId: number | string
526
+ planId?: number | string
527
+ clawJobId: string
528
+ name: string
529
+ kind: string // 'at' | 'every' | 'cron'
530
+ expr: string
531
+ message?: string
532
+ }
533
+
534
+ /**
535
+ * 同步 openclaw 创建的定时任务至后端
536
+ * POST /cron/job/add
537
+ */
538
+ export async function syncOpenclawWorkclawCronJobToBackend(
539
+ cacheKey: string,
540
+ config: OpenclawWorkclawConnectionConfig,
541
+ payload: CronJobPayload,
542
+ ): Promise<any> {
543
+ const baseUrl = normalizeBaseUrl(config.baseUrl)
544
+ const token = await getOpenclawWorkclawAccessToken(cacheKey, config)
545
+
546
+ // 尝试添加 /open-apis 前缀,以匹配其他 API 的模式 (如 getAgentInstances)
547
+ const requestUrl = `${baseUrl}/open-apis/cron/job/add`
548
+ getOpenclawWorkclawLogger().info(`Syncing cron job to ${requestUrl}`)
549
+ getOpenclawWorkclawLogger().info(`Payload:`, JSON.stringify(payload, null, 2))
550
+
551
+ const data = await doFetchJson(
552
+ requestUrl,
553
+ {
554
+ method: 'POST',
555
+ headers: {
556
+ 'Content-Type': 'application/json',
557
+ 'Authorization': `Bearer ${token}`,
558
+ },
559
+ body: JSON.stringify(payload),
560
+ },
561
+ config.allowInsecureTls,
562
+ config.requestTimeout,
563
+ )
564
+ getOpenclawWorkclawLogger().info(`Sync cron job response:`, JSON.stringify(data, null, 2))
565
+
566
+ if (data?.code !== 200 && data?.code !== 0) {
567
+ throw new Error(data?.message || data?.msg || 'Failed to sync cron job')
568
+ }
569
+ return data
570
+ }
571
+
572
+ /**
573
+ * 获取AppKey所对应的模型配置
574
+ * POST /open-apis/instance/apiKey/{appKey}
575
+ */
576
+ export async function getOpenclawWorkclawModelConfigByAppKey(
577
+ cacheKey: string,
578
+ config: OpenclawWorkclawConnectionConfig,
579
+ ): Promise<{
580
+ baseUrl: string
581
+ apiKey: string
582
+ }> {
583
+ const baseUrl = normalizeBaseUrl(config.baseUrl)
584
+ const token = await getOpenclawWorkclawAccessToken(cacheKey, config)
585
+ const appKey = config.appKey
586
+
587
+ if (!appKey) {
588
+ throw new Error('Missing appKey for getModelKeyByAppKey')
589
+ }
590
+
591
+ const requestUrl = `${baseUrl}/open-apis/instance/apiKey/${appKey}`
592
+ getOpenclawWorkclawLogger().info(`Getting model key from ${requestUrl}`)
593
+
594
+ const data = await doFetchJson(
595
+ requestUrl,
596
+ {
597
+ method: 'GET',
598
+ headers: {
599
+ 'Content-Type': 'x-www-form-urlencoded',
600
+ 'Authorization': `Bearer ${token}`,
601
+ },
602
+ },
603
+ config.allowInsecureTls,
604
+ config.requestTimeout,
605
+ )
606
+
607
+ getOpenclawWorkclawLogger().info(`Model key response:`, JSON.stringify(data, null, 2))
608
+
609
+ if (data?.code !== 200 || !data?.data) {
610
+ throw new Error(data?.message || data?.msg || 'Failed to get model key')
611
+ }
612
+
613
+ return data.data
614
+ }
615
+
616
+ export interface CronJobMessagePayload {
617
+ clawJobId: string
618
+ message: string
619
+ }
620
+
621
+ /**
622
+ * 此时定时任务触发消息至后端
623
+ * POST /open-apis/cron/job/message
624
+ */
625
+ export async function sendOpenclawWorkclawCronJobMessageToBackend(
626
+ cacheKey: string,
627
+ config: OpenclawWorkclawConnectionConfig,
628
+ payload: CronJobMessagePayload,
629
+ ): Promise<any> {
630
+ const baseUrl = normalizeBaseUrl(config.baseUrl)
631
+ const token = await getOpenclawWorkclawAccessToken(cacheKey, config)
632
+
633
+ const requestUrl = `${baseUrl}/open-apis/cron/job/message`
634
+ getOpenclawWorkclawLogger().info(`Sending cron job message to ${requestUrl}`)
635
+ getOpenclawWorkclawLogger().info(`Payload:`, JSON.stringify(payload, null, 2))
636
+
637
+ const data = await doFetchJson(
638
+ requestUrl,
639
+ {
640
+ method: 'POST',
641
+ headers: {
642
+ 'Content-Type': 'application/json',
643
+ 'Authorization': `Bearer ${token}`,
644
+ },
645
+ body: JSON.stringify(payload),
646
+ },
647
+ config.allowInsecureTls,
648
+ config.requestTimeout,
649
+ )
650
+ getOpenclawWorkclawLogger().info(`Send cron job message response:`, JSON.stringify(data, null, 2))
651
+
652
+ if (data?.code !== 200 && data?.code !== 0) {
653
+ throw new Error(data?.message || data?.msg || 'Failed to send cron job message')
654
+ }
655
+ return data
656
+ }