@workclaw/openclaw-workclaw 1.0.18 → 1.0.20
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 +30 -11
- package/dist/api.d.ts +3 -0
- package/dist/api.js +3 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +264 -0
- package/dist/setup-entry.d.ts +2 -0
- package/dist/setup-entry.js +12 -0
- package/dist/src/accounts.d.ts +66 -0
- package/dist/src/accounts.js +279 -0
- package/dist/src/api/accounts-api.d.ts +2 -0
- package/dist/src/api/accounts-api.js +130 -0
- package/dist/src/api/prompts-api.d.ts +2 -0
- package/dist/src/api/prompts-api.js +103 -0
- package/dist/src/api/session-api.d.ts +2 -0
- package/dist/src/api/session-api.js +207 -0
- package/dist/src/api/skills-api.d.ts +2 -0
- package/dist/src/api/skills-api.js +64 -0
- package/dist/src/api/workspace.d.ts +3 -0
- package/dist/src/api/workspace.js +30 -0
- package/dist/src/channel.d.ts +11 -0
- package/{src/channel.ts → dist/src/channel.js} +199 -225
- package/dist/src/config-schema.d.ts +93 -0
- package/dist/src/config-schema.js +56 -0
- package/dist/src/connection/workclaw-client.d.ts +147 -0
- package/dist/src/connection/workclaw-client.js +351 -0
- package/dist/src/gateway/agent-handlers.d.ts +12 -0
- package/{src/gateway/agent-handlers.ts → dist/src/gateway/agent-handlers.js} +372 -462
- package/dist/src/gateway/config-writer.d.ts +71 -0
- package/dist/src/gateway/config-writer.js +302 -0
- package/dist/src/gateway/cron-tasks-handler.d.ts +19 -0
- package/dist/src/gateway/cron-tasks-handler.js +188 -0
- package/dist/src/gateway/message-context.d.ts +80 -0
- package/{src/gateway/message-context.ts → dist/src/gateway/message-context.js} +509 -594
- package/dist/src/gateway/message-dispatcher.d.ts +18 -0
- package/dist/src/gateway/message-dispatcher.js +572 -0
- package/dist/src/gateway/reconnect.d.ts +27 -0
- package/{src/gateway/reconnect.ts → dist/src/gateway/reconnect.js} +210 -253
- package/dist/src/gateway/skills-handler.d.ts +29 -0
- package/dist/src/gateway/skills-handler.js +615 -0
- package/dist/src/gateway/skills-list-handler.d.ts +27 -0
- package/dist/src/gateway/skills-list-handler.js +233 -0
- package/dist/src/gateway/tools-list-handler.d.ts +30 -0
- package/dist/src/gateway/tools-list-handler.js +101 -0
- package/dist/src/gateway/workclaw-gateway.d.ts +14 -0
- package/dist/src/gateway/workclaw-gateway.js +237 -0
- package/dist/src/media/upload.d.ts +13 -0
- package/dist/src/media/upload.js +125 -0
- package/dist/src/outbound/index.d.ts +36 -0
- package/dist/src/outbound/index.js +123 -0
- package/dist/src/outbound/workclaw-sender.d.ts +36 -0
- package/{src/outbound/workclaw-sender.ts → dist/src/outbound/workclaw-sender.js} +95 -158
- package/dist/src/runtime.d.ts +116 -0
- package/dist/src/runtime.js +374 -0
- package/dist/src/secret-contract-api.d.ts +4 -0
- package/dist/src/secret-contract-api.js +4 -0
- package/{src/send.ts → dist/src/send.d.ts} +1 -1
- package/dist/src/send.js +1 -0
- package/dist/src/setup-api.d.ts +3 -0
- package/dist/src/setup-api.js +3 -0
- package/dist/src/setup-core.d.ts +3 -0
- package/dist/src/setup-core.js +13 -0
- package/dist/src/setup-surface.d.ts +7 -0
- package/dist/src/setup-surface.js +363 -0
- package/dist/src/tools/openclaw-workclaw-cron/api/index.d.ts +93 -0
- package/dist/src/tools/openclaw-workclaw-cron/api/index.js +209 -0
- package/dist/src/tools/openclaw-workclaw-cron/index.d.ts +10 -0
- package/{src/tools/openclaw-workclaw-cron/index.ts → dist/src/tools/openclaw-workclaw-cron/index.js} +33 -39
- package/dist/src/tools/openclaw-workclaw-cron/src/add/params.d.ts +16 -0
- package/dist/src/tools/openclaw-workclaw-cron/src/add/params.js +152 -0
- package/dist/src/tools/openclaw-workclaw-cron/src/add/sync.d.ts +18 -0
- package/dist/src/tools/openclaw-workclaw-cron/src/add/sync.js +162 -0
- package/dist/src/tools/openclaw-workclaw-cron/src/disable/params.d.ts +14 -0
- package/dist/src/tools/openclaw-workclaw-cron/src/disable/params.js +80 -0
- package/dist/src/tools/openclaw-workclaw-cron/src/disable/sync.d.ts +14 -0
- package/dist/src/tools/openclaw-workclaw-cron/src/disable/sync.js +107 -0
- package/dist/src/tools/openclaw-workclaw-cron/src/enable/params.d.ts +14 -0
- package/dist/src/tools/openclaw-workclaw-cron/src/enable/params.js +80 -0
- package/dist/src/tools/openclaw-workclaw-cron/src/enable/sync.d.ts +14 -0
- package/dist/src/tools/openclaw-workclaw-cron/src/enable/sync.js +107 -0
- package/dist/src/tools/openclaw-workclaw-cron/src/notify/sync.d.ts +17 -0
- package/dist/src/tools/openclaw-workclaw-cron/src/notify/sync.js +127 -0
- package/dist/src/tools/openclaw-workclaw-cron/src/remove/params.d.ts +14 -0
- package/dist/src/tools/openclaw-workclaw-cron/src/remove/params.js +87 -0
- package/dist/src/tools/openclaw-workclaw-cron/src/remove/sync.d.ts +14 -0
- package/dist/src/tools/openclaw-workclaw-cron/src/remove/sync.js +107 -0
- package/dist/src/tools/openclaw-workclaw-cron/src/update/params.d.ts +17 -0
- package/dist/src/tools/openclaw-workclaw-cron/src/update/params.js +164 -0
- package/dist/src/tools/openclaw-workclaw-cron/src/update/sync.d.ts +18 -0
- package/dist/src/tools/openclaw-workclaw-cron/src/update/sync.js +135 -0
- package/dist/src/tools/openclaw-workclaw-cron/types/index.d.ts +52 -0
- package/dist/src/tools/openclaw-workclaw-cron/types/index.js +4 -0
- package/dist/src/tools/openclaw-workclaw-cron/utils/index.d.ts +40 -0
- package/dist/src/tools/openclaw-workclaw-cron/utils/index.js +122 -0
- package/dist/src/tools/openclaw-workclaw-system/index.d.ts +10 -0
- package/{src/tools/openclaw-workclaw-system/index.ts → dist/src/tools/openclaw-workclaw-system/index.js} +15 -17
- package/dist/src/tools/openclaw-workclaw-system/src/get/index.d.ts +8 -0
- package/dist/src/tools/openclaw-workclaw-system/src/get/index.js +64 -0
- package/dist/src/tools/openclaw-workclaw-system/src/token/index.d.ts +8 -0
- package/dist/src/tools/openclaw-workclaw-system/src/token/index.js +78 -0
- package/{src/types.ts → dist/src/types.d.ts} +39 -58
- package/dist/src/types.js +1 -0
- package/dist/src/utils/content.d.ts +15 -0
- package/{src/utils/content.ts → dist/src/utils/content.js} +38 -35
- package/package.json +13 -9
- package/index.ts +0 -302
- package/setup-entry.ts +0 -6
- package/src/accounts.ts +0 -312
- package/src/api/accounts-api.ts +0 -156
- package/src/api/prompts-api.ts +0 -116
- package/src/api/session-api.ts +0 -238
- package/src/api/skills-api.ts +0 -72
- package/src/api/workspace.ts +0 -41
- package/src/config-schema.ts +0 -110
- package/src/connection/workclaw-client.ts +0 -643
- package/src/gateway/config-writer.ts +0 -296
- package/src/gateway/message-dispatcher.ts +0 -641
- package/src/gateway/skills-handler.ts +0 -741
- package/src/gateway/skills-list-handler.ts +0 -332
- package/src/gateway/tools-list-handler.ts +0 -160
- package/src/gateway/workclaw-gateway.ts +0 -367
- package/src/media/upload.ts +0 -157
- package/src/outbound/index.ts +0 -185
- package/src/runtime.ts +0 -497
- package/src/tools/openclaw-workclaw-cron/api/index.ts +0 -326
- package/src/tools/openclaw-workclaw-cron/src/add/params.ts +0 -177
- package/src/tools/openclaw-workclaw-cron/src/add/sync.ts +0 -188
- package/src/tools/openclaw-workclaw-cron/src/disable/params.ts +0 -100
- package/src/tools/openclaw-workclaw-cron/src/disable/sync.ts +0 -127
- package/src/tools/openclaw-workclaw-cron/src/enable/params.ts +0 -100
- package/src/tools/openclaw-workclaw-cron/src/enable/sync.ts +0 -127
- package/src/tools/openclaw-workclaw-cron/src/notify/sync.ts +0 -148
- package/src/tools/openclaw-workclaw-cron/src/remove/params.ts +0 -109
- package/src/tools/openclaw-workclaw-cron/src/remove/sync.ts +0 -127
- package/src/tools/openclaw-workclaw-cron/src/update/params.ts +0 -195
- package/src/tools/openclaw-workclaw-cron/src/update/sync.ts +0 -161
- package/src/tools/openclaw-workclaw-cron/types/index.ts +0 -55
- package/src/tools/openclaw-workclaw-cron/utils/index.ts +0 -141
- package/src/tools/openclaw-workclaw-system/src/get/index.ts +0 -77
- package/src/tools/openclaw-workclaw-system/src/token/index.ts +0 -93
- package/tests/accounts.test.ts +0 -285
- package/tests/message-context.test.ts +0 -313
- package/tests/reconnect.test.ts +0 -257
- package/tests/workclaw-client.test.ts +0 -112
- package/tsconfig.json +0 -14
- package/vitest.config.ts +0 -8
|
@@ -1,594 +1,509 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
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
|
-
return {
|
|
156
|
-
type:
|
|
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
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
return
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
return
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
*
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
});
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
// 收集下载成功的文件信息(MediaUrls 由 SDK 用于显示 [media attached])
|
|
511
|
-
for (const { att, localPath, mediaType } of downloadResults) {
|
|
512
|
-
if (localPath) {
|
|
513
|
-
// 将绝对路径转为根相对路径(去掉 ~/.openclaw/ 前缀,转为 /.openclaw/... 形式)
|
|
514
|
-
// OpenClaw 的 skill 系统会 block 绝对路径(C:\...)和 ~ 路径
|
|
515
|
-
// 兼容 Windows 路径格式(C:\Users\用户名\.openclaw\... 或 C:\Users\用户名.openclaw\...)
|
|
516
|
-
const homeDir = os.homedir().replace(/\\/g, '/').trim(); // 统一用正斜杠,去除尾随空格
|
|
517
|
-
const normalizedLocalPath = localPath.replace(/\\/g, '/');
|
|
518
|
-
const relativePath = normalizedLocalPath.startsWith(homeDir + '/.openclaw/')
|
|
519
|
-
? '/' + normalizedLocalPath.slice(homeDir.length + 1) // 用 / 开头表示根相对路径
|
|
520
|
-
: normalizedLocalPath;
|
|
521
|
-
mediaUrls.push(relativePath);
|
|
522
|
-
mediaTypes.push(mediaType);
|
|
523
|
-
log?.info?.(`[Attachments] Downloaded: ${relativePath}`);
|
|
524
|
-
} else {
|
|
525
|
-
log?.warn?.(`[Attachments] Failed to download: ${att.url}`);
|
|
526
|
-
}
|
|
527
|
-
}
|
|
528
|
-
|
|
529
|
-
log?.info?.(`[Attachments] Downloaded ${mediaUrls.length}/${message.attachments.length} files`);
|
|
530
|
-
}
|
|
531
|
-
const fromLabel = message.to;
|
|
532
|
-
const body = pluginRuntime.channel.reply.formatInboundEnvelope({
|
|
533
|
-
channel: OPENCLAW_WORKCLAW_CHANNEL,
|
|
534
|
-
from: fromLabel,
|
|
535
|
-
timestamp: message.timestamp,
|
|
536
|
-
body: bodyText,
|
|
537
|
-
chatType: message.chatType,
|
|
538
|
-
sender: { name: message.to, id: message.to },
|
|
539
|
-
previousTimestamp,
|
|
540
|
-
envelope: envelopeOptions,
|
|
541
|
-
});
|
|
542
|
-
|
|
543
|
-
// 构建完整的 ctx
|
|
544
|
-
const finalized = pluginRuntime.channel.reply.finalizeInboundContext({
|
|
545
|
-
Body: body,
|
|
546
|
-
BodyForAgent: bodyText,
|
|
547
|
-
RawBody: JSON.stringify(message),
|
|
548
|
-
CommandBody: message.text,
|
|
549
|
-
From: message.from,
|
|
550
|
-
To: message.to,
|
|
551
|
-
SessionKey: sessionKey,
|
|
552
|
-
AccountId: accountId,
|
|
553
|
-
ChatType: message.chatType,
|
|
554
|
-
ConversationLabel: fromLabel,
|
|
555
|
-
Provider: OPENCLAW_WORKCLAW_CHANNEL,
|
|
556
|
-
Surface: "openclaw-workclaw_dm",
|
|
557
|
-
MessageSid: message.messageId,
|
|
558
|
-
Timestamp: message.timestamp,
|
|
559
|
-
OriginatingChannel: OPENCLAW_WORKCLAW_CHANNEL,
|
|
560
|
-
OriginatingTo: message.to,
|
|
561
|
-
// 附件媒体信息(供 skill 读取本地文件)
|
|
562
|
-
MediaPath: mediaUrls[0] || undefined,
|
|
563
|
-
MediaUrl: mediaUrls[0] || undefined,
|
|
564
|
-
MediaUrls: mediaUrls.length > 0 ? mediaUrls : undefined,
|
|
565
|
-
MediaTypes: mediaTypes.length > 0 ? mediaTypes : undefined,
|
|
566
|
-
});
|
|
567
|
-
|
|
568
|
-
// 记录 session(需要在 dispatchReplyWithBufferedBlockDispatcher 之前调用)
|
|
569
|
-
// 这样 OpenClaw 才能在工具执行时正确关联 session
|
|
570
|
-
if (sessionKey && recordSession) {
|
|
571
|
-
try {
|
|
572
|
-
await pluginRuntime.channel.session.recordInboundSession({
|
|
573
|
-
storePath,
|
|
574
|
-
sessionKey: finalized.SessionKey || sessionKey,
|
|
575
|
-
ctx: finalized,
|
|
576
|
-
updateLastRoute: {
|
|
577
|
-
sessionKey,
|
|
578
|
-
channel: OPENCLAW_WORKCLAW_CHANNEL,
|
|
579
|
-
to: message.to,
|
|
580
|
-
accountId,
|
|
581
|
-
},
|
|
582
|
-
onRecordError: (err: unknown) => {
|
|
583
|
-
log?.error?.(`[Session] recordInboundSession failed: ${String(err)}`);
|
|
584
|
-
},
|
|
585
|
-
});
|
|
586
|
-
log?.info?.(`[Session] Recorded session for key=${sessionKey}`);
|
|
587
|
-
} catch (err) {
|
|
588
|
-
log?.warn?.(`[Session] recordInboundSession error: ${String(err)}`);
|
|
589
|
-
}
|
|
590
|
-
}
|
|
591
|
-
|
|
592
|
-
return finalized;
|
|
593
|
-
}
|
|
594
|
-
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import http from 'node:http';
|
|
3
|
+
import https from 'node:https';
|
|
4
|
+
import os from 'node:os';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
/** Channel/plugin name constant */
|
|
7
|
+
export const OPENCLAW_WORKCLAW_CHANNEL = 'openclaw-workclaw';
|
|
8
|
+
export function parseInboundMessage(data, allowRawJsonPayload) {
|
|
9
|
+
let input = {};
|
|
10
|
+
try {
|
|
11
|
+
input = data ? JSON.parse(data) : {};
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
// Not JSON, treat as plain text
|
|
15
|
+
}
|
|
16
|
+
const text = typeof input.text === 'string'
|
|
17
|
+
? input.text
|
|
18
|
+
: typeof input.message === 'string'
|
|
19
|
+
? input.message
|
|
20
|
+
: typeof input.body === 'string'
|
|
21
|
+
? input.body
|
|
22
|
+
: data;
|
|
23
|
+
const to = String(input.to ?? input.chatId ?? input.target ?? '');
|
|
24
|
+
const from = String(input.from ?? input.senderId ?? input.userId ?? '');
|
|
25
|
+
const chatType = String(input.chatType || '').toLowerCase() === 'group' ? 'group' : 'dm';
|
|
26
|
+
const messageId = String(input.messageId ?? input.id ?? '');
|
|
27
|
+
const rawPayload = allowRawJsonPayload ? JSON.stringify(input ?? {}) : data;
|
|
28
|
+
return {
|
|
29
|
+
text,
|
|
30
|
+
to,
|
|
31
|
+
from,
|
|
32
|
+
chatType,
|
|
33
|
+
messageId,
|
|
34
|
+
rawPayload,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
export function parseWorkClawMessage(data, log) {
|
|
38
|
+
// {"specVersion":"1.0","type":"EVENT","metadata":{"time":"1773749163831","event":"INIT_AGENT","contentType":"application/json"},"data":"\"12557533957824965\""}
|
|
39
|
+
let input = {};
|
|
40
|
+
try {
|
|
41
|
+
input = data ? JSON.parse(data) : {};
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
log?.info?.(`Failed to parse message data=${data}`);
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
// Handle plain text ping
|
|
48
|
+
if (typeof input === 'string' && input.toLowerCase() === 'ping') {
|
|
49
|
+
return {
|
|
50
|
+
type: 'ping',
|
|
51
|
+
pongData: input,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
const envelope = input && typeof input === 'object' ? input : { data: input };
|
|
55
|
+
const topic = envelope?.metadata?.topic;
|
|
56
|
+
const type = String(envelope?.type || '').trim().toUpperCase();
|
|
57
|
+
const topicLower = String(topic || '').trim().toLowerCase();
|
|
58
|
+
if (type === 'INIT_AGENT') {
|
|
59
|
+
let parsedData = envelope?.data;
|
|
60
|
+
if (typeof parsedData === 'string') {
|
|
61
|
+
try {
|
|
62
|
+
parsedData = JSON.parse(parsedData);
|
|
63
|
+
}
|
|
64
|
+
catch (e) {
|
|
65
|
+
// 解析失败,保持原样
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return {
|
|
69
|
+
type: 'init_agent',
|
|
70
|
+
eventData: parsedData,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
// Handle SYSTEM messages
|
|
74
|
+
if (type === 'SYSTEM') {
|
|
75
|
+
if (topicLower === 'ping') {
|
|
76
|
+
return {
|
|
77
|
+
type: 'ping',
|
|
78
|
+
pongData: envelope?.data,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
if (topicLower === 'disconnect') {
|
|
82
|
+
return {
|
|
83
|
+
type: 'disconnect',
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
// Unknown system message type, ignore
|
|
87
|
+
return {
|
|
88
|
+
type: 'ignored',
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
// Handle EVENT messages (AGENT_CREATED, AGENT_UPDATED, AGENT_DELETED, TOOLS_LIST, SKILLS_LIST, INIT_AGENT)
|
|
92
|
+
if (type === 'EVENT') {
|
|
93
|
+
const eventType = String(envelope?.metadata?.event || '').trim().toUpperCase();
|
|
94
|
+
// 解析 data 字段(可能是 JSON 字符串)
|
|
95
|
+
let parsedData = envelope?.data;
|
|
96
|
+
if (typeof parsedData === 'string') {
|
|
97
|
+
try {
|
|
98
|
+
parsedData = JSON.parse(parsedData);
|
|
99
|
+
}
|
|
100
|
+
catch (e) {
|
|
101
|
+
// 解析失败,保持原样
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
if (eventType === 'AGENT_CREATED') {
|
|
105
|
+
return {
|
|
106
|
+
type: 'agent_created',
|
|
107
|
+
eventData: parsedData,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
if (eventType === 'AGENT_UPDATED') {
|
|
111
|
+
return {
|
|
112
|
+
type: 'agent_updated',
|
|
113
|
+
eventData: parsedData,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
if (eventType === 'AGENT_DELETED') {
|
|
117
|
+
return {
|
|
118
|
+
type: 'agent_deleted',
|
|
119
|
+
eventData: parsedData,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
if (eventType === 'TOOLS_LIST') {
|
|
123
|
+
return {
|
|
124
|
+
type: 'tools_list',
|
|
125
|
+
eventData: parsedData,
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
if (eventType === 'SKILLS_LIST') {
|
|
129
|
+
return {
|
|
130
|
+
type: 'skills_list',
|
|
131
|
+
eventData: parsedData,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
if (eventType === 'INIT_AGENT') {
|
|
135
|
+
return {
|
|
136
|
+
type: 'init_agent',
|
|
137
|
+
eventData: parsedData,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
if (eventType === 'SKILL_DO_REMIND') {
|
|
141
|
+
const eventTopic = String(parsedData?.dotype || '').toLowerCase();
|
|
142
|
+
return {
|
|
143
|
+
type: 'skills_event',
|
|
144
|
+
eventData: {
|
|
145
|
+
topic: `skills/${eventTopic}`,
|
|
146
|
+
requestId: '',
|
|
147
|
+
callbackUrl: '',
|
|
148
|
+
authToken: '',
|
|
149
|
+
data: parsedData,
|
|
150
|
+
},
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
if (eventType === 'CRON_REMIND') {
|
|
154
|
+
const eventTopic = String(parsedData?.dotype || '').toLowerCase();
|
|
155
|
+
return {
|
|
156
|
+
type: 'cron_task_event',
|
|
157
|
+
cronTaskEvent: {
|
|
158
|
+
dotype: `${eventTopic}`,
|
|
159
|
+
data: parsedData,
|
|
160
|
+
},
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
// Unknown event type, ignore
|
|
164
|
+
return {
|
|
165
|
+
type: 'ignored',
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
// Only process CALLBACK type with AGENT_MESSAGE topic
|
|
169
|
+
if (type !== 'CALLBACK' || String(topic || '').trim().toUpperCase() !== 'AGENT_MESSAGE') {
|
|
170
|
+
return {
|
|
171
|
+
type: 'ignored',
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
// 解析 data 字段(可能是 JSON 字符串)
|
|
175
|
+
let payload = envelope?.data || {};
|
|
176
|
+
if (typeof payload === 'string') {
|
|
177
|
+
try {
|
|
178
|
+
payload = JSON.parse(payload);
|
|
179
|
+
}
|
|
180
|
+
catch (e) {
|
|
181
|
+
// 解析失败,保持原样
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
const text = typeof payload?.message?.content === 'string'
|
|
185
|
+
? payload.message.content
|
|
186
|
+
: typeof payload?.message === 'string'
|
|
187
|
+
? payload.message
|
|
188
|
+
: '';
|
|
189
|
+
// 判断文件是否为图片
|
|
190
|
+
function isImageFile(url) {
|
|
191
|
+
const imageExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.webp', '.bmp', '.svg', '.ico'];
|
|
192
|
+
const lowerUrl = url.toLowerCase();
|
|
193
|
+
return imageExtensions.some(ext => lowerUrl.endsWith(ext));
|
|
194
|
+
}
|
|
195
|
+
// 解析 fileUrl 列表到 attachments(供 SDK 和 AI 使用)
|
|
196
|
+
const fileUrlList = Array.isArray(payload?.fileUrl) ? payload.fileUrl : [];
|
|
197
|
+
const attachments = fileUrlList.map(url => ({
|
|
198
|
+
url,
|
|
199
|
+
fileId: payload?.fileId ? String(payload.fileId) : '',
|
|
200
|
+
type: isImageFile(url) ? 'image' : 'file',
|
|
201
|
+
}));
|
|
202
|
+
return {
|
|
203
|
+
type: 'agent_message',
|
|
204
|
+
message: {
|
|
205
|
+
text,
|
|
206
|
+
userId: String(payload?.userId ?? ''),
|
|
207
|
+
openConversationId: String(payload?.conversationId ?? ''),
|
|
208
|
+
messageId: String(payload?.msgId ?? ''),
|
|
209
|
+
agentId: payload?.agentId,
|
|
210
|
+
attachments: attachments.length > 0 ? attachments : undefined,
|
|
211
|
+
},
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* 从 URL 扩展名判断媒体类型
|
|
216
|
+
*/
|
|
217
|
+
function detectMediaType(url) {
|
|
218
|
+
const lower = url.toLowerCase();
|
|
219
|
+
if (lower.endsWith('.jpg') || lower.endsWith('.jpeg') || lower.endsWith('.png')
|
|
220
|
+
|| lower.endsWith('.gif') || lower.endsWith('.webp') || lower.endsWith('.bmp')
|
|
221
|
+
|| lower.endsWith('.svg') || lower.endsWith('.ico')) {
|
|
222
|
+
return 'image';
|
|
223
|
+
}
|
|
224
|
+
if (lower.endsWith('.mp3') || lower.endsWith('.wav') || lower.endsWith('.amr')
|
|
225
|
+
|| lower.endsWith('.m4a') || lower.endsWith('.ogg')) {
|
|
226
|
+
return 'audio';
|
|
227
|
+
}
|
|
228
|
+
if (lower.endsWith('.mp4') || lower.endsWith('.avi') || lower.endsWith('.mov')
|
|
229
|
+
|| lower.endsWith('.mkv') || lower.endsWith('.webm')) {
|
|
230
|
+
return 'video';
|
|
231
|
+
}
|
|
232
|
+
if (lower.endsWith('.pdf'))
|
|
233
|
+
return 'application/pdf';
|
|
234
|
+
if (lower.endsWith('.docx'))
|
|
235
|
+
return 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
|
|
236
|
+
if (lower.endsWith('.xlsx'))
|
|
237
|
+
return 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
|
|
238
|
+
if (lower.endsWith('.pptx'))
|
|
239
|
+
return 'application/vnd.openxmlformats-officedocument.presentationml.presentation';
|
|
240
|
+
if (lower.endsWith('.doc'))
|
|
241
|
+
return 'application/msword';
|
|
242
|
+
if (lower.endsWith('.xls'))
|
|
243
|
+
return 'application/vnd.ms-excel';
|
|
244
|
+
if (lower.endsWith('.ppt'))
|
|
245
|
+
return 'application/vnd.ms-powerpoint';
|
|
246
|
+
if (lower.endsWith('.txt'))
|
|
247
|
+
return 'text/plain';
|
|
248
|
+
if (lower.endsWith('.zip'))
|
|
249
|
+
return 'application/zip';
|
|
250
|
+
if (lower.endsWith('.rar'))
|
|
251
|
+
return 'application/x-rar-compressed';
|
|
252
|
+
return 'application/octet-stream';
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* 从 URL 扩展名获取人类可读的类型标签
|
|
256
|
+
*/
|
|
257
|
+
function getMediaTypeLabel(url) {
|
|
258
|
+
const lower = url.toLowerCase();
|
|
259
|
+
if (lower.endsWith('.pdf'))
|
|
260
|
+
return 'PDF';
|
|
261
|
+
if (lower.endsWith('.docx'))
|
|
262
|
+
return 'Word';
|
|
263
|
+
if (lower.endsWith('.doc'))
|
|
264
|
+
return 'Word';
|
|
265
|
+
if (lower.endsWith('.xlsx'))
|
|
266
|
+
return 'Excel';
|
|
267
|
+
if (lower.endsWith('.xls'))
|
|
268
|
+
return 'Excel';
|
|
269
|
+
if (lower.endsWith('.pptx'))
|
|
270
|
+
return 'PowerPoint';
|
|
271
|
+
if (lower.endsWith('.ppt'))
|
|
272
|
+
return 'PowerPoint';
|
|
273
|
+
if (lower.endsWith('.txt'))
|
|
274
|
+
return '文本';
|
|
275
|
+
if (lower.endsWith('.jpg') || lower.endsWith('.jpeg'))
|
|
276
|
+
return 'JPG图片';
|
|
277
|
+
if (lower.endsWith('.png'))
|
|
278
|
+
return 'PNG图片';
|
|
279
|
+
if (lower.endsWith('.gif'))
|
|
280
|
+
return 'GIF图片';
|
|
281
|
+
if (lower.endsWith('.webp'))
|
|
282
|
+
return 'WebP图片';
|
|
283
|
+
if (lower.endsWith('.mp3'))
|
|
284
|
+
return '音频';
|
|
285
|
+
if (lower.endsWith('.wav'))
|
|
286
|
+
return '音频';
|
|
287
|
+
if (lower.endsWith('.mp4'))
|
|
288
|
+
return '视频';
|
|
289
|
+
if (lower.endsWith('.zip') || lower.endsWith('.rar'))
|
|
290
|
+
return '压缩包';
|
|
291
|
+
return '文件';
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* 下载文件到本地下载目录(按 accountId/peerId 隔离),返回本地路径
|
|
295
|
+
*/
|
|
296
|
+
async function downloadFileToLocal(url, accountId, peerId, log) {
|
|
297
|
+
const downloadsDir = path.join(os.homedir(), '.openclaw', 'media', 'workclaw', 'downloads', accountId, peerId);
|
|
298
|
+
try {
|
|
299
|
+
await fs.mkdir(downloadsDir, { recursive: true });
|
|
300
|
+
}
|
|
301
|
+
catch { }
|
|
302
|
+
const urlObj = new URL(url);
|
|
303
|
+
const fileName = path.basename(urlObj.pathname) || 'file';
|
|
304
|
+
const ext = path.extname(fileName);
|
|
305
|
+
const stem = path.basename(fileName, ext);
|
|
306
|
+
const localFileName = `${stem}_${Date.now()}${ext}`;
|
|
307
|
+
const localFilePath = path.join(downloadsDir, localFileName);
|
|
308
|
+
return new Promise((resolve) => {
|
|
309
|
+
const client = url.startsWith('https:') ? https : http;
|
|
310
|
+
const req = client.get(url, (res) => {
|
|
311
|
+
if (res.statusCode !== 200) {
|
|
312
|
+
log?.warn?.(`downloadFile: HTTP ${res.statusCode} for ${url}`);
|
|
313
|
+
resolve(null);
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
const chunks = [];
|
|
317
|
+
res.on('data', (chunk) => chunks.push(chunk));
|
|
318
|
+
res.on('end', async () => {
|
|
319
|
+
try {
|
|
320
|
+
const buffer = Buffer.concat(chunks);
|
|
321
|
+
await fs.writeFile(localFilePath, buffer);
|
|
322
|
+
log?.info?.(`downloadFile: saved ${buffer.length} bytes -> ${localFilePath}`);
|
|
323
|
+
resolve(localFilePath);
|
|
324
|
+
}
|
|
325
|
+
catch (err) {
|
|
326
|
+
log?.error?.(`downloadFile: write failed: ${String(err)}`);
|
|
327
|
+
resolve(null);
|
|
328
|
+
}
|
|
329
|
+
});
|
|
330
|
+
res.on('error', (err) => {
|
|
331
|
+
log?.error?.(`downloadFile: read error: ${String(err)}`);
|
|
332
|
+
resolve(null);
|
|
333
|
+
});
|
|
334
|
+
});
|
|
335
|
+
req.on('error', (err) => {
|
|
336
|
+
log?.error?.(`downloadFile: request error: ${String(err)}`);
|
|
337
|
+
resolve(null);
|
|
338
|
+
});
|
|
339
|
+
req.setTimeout(30000, () => {
|
|
340
|
+
req.destroy();
|
|
341
|
+
log?.warn?.(`downloadFile: timeout for ${url}`);
|
|
342
|
+
resolve(null);
|
|
343
|
+
});
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* Build inbound context for WorkClaw channel.
|
|
348
|
+
*
|
|
349
|
+
* When pluginRuntime is provided, performs full session handling:
|
|
350
|
+
* - resolveStorePath, readSessionUpdatedAt, formatInboundEnvelope
|
|
351
|
+
* - finalizeInboundContext, recordInboundSession
|
|
352
|
+
*
|
|
353
|
+
* @param message - The inbound message
|
|
354
|
+
* @param accountId - Account ID
|
|
355
|
+
* @param config - Account config (WorkclawConfig)
|
|
356
|
+
* @param agentId - Target agent ID (optional)
|
|
357
|
+
* @param pluginRuntime - OpenClaw plugin runtime (optional, enables full session handling)
|
|
358
|
+
* @param recordSession - Whether to record session (default true)
|
|
359
|
+
* @param log - Optional logger
|
|
360
|
+
*/
|
|
361
|
+
export async function buildInboundContext(message, accountId, config, agentId, pluginRuntime, recordSession = true, log) {
|
|
362
|
+
// 尝试从 rawPayload 中提取已有的 sessionKey(新建话题时 openclaw 会生成新的 session)
|
|
363
|
+
let existingSessionKey;
|
|
364
|
+
try {
|
|
365
|
+
const rawPayload = JSON.parse(message.rawPayload);
|
|
366
|
+
existingSessionKey = rawPayload.sessionKey;
|
|
367
|
+
}
|
|
368
|
+
catch {
|
|
369
|
+
// ignore
|
|
370
|
+
}
|
|
371
|
+
const peerId = message.from;
|
|
372
|
+
const computedSessionKey = existingSessionKey
|
|
373
|
+
|| (agentId && agentId !== 'main'
|
|
374
|
+
? `agent:${agentId}:openclaw-workclaw:${accountId}:direct:${peerId}`
|
|
375
|
+
: undefined);
|
|
376
|
+
const sessionKey = computedSessionKey || (agentId && agentId !== 'main'
|
|
377
|
+
? `agent:${agentId}:openclaw-workclaw:${accountId}:direct:${peerId}`
|
|
378
|
+
: undefined);
|
|
379
|
+
// Basic context (used as base for both paths)
|
|
380
|
+
// parseWorkClawMessage 已将 fileUrl 列表追加到 message.text 中
|
|
381
|
+
const result = {
|
|
382
|
+
Body: message.text,
|
|
383
|
+
BodyForAgent: message.text,
|
|
384
|
+
RawBody: JSON.stringify(message),
|
|
385
|
+
From: message.from,
|
|
386
|
+
To: message.to,
|
|
387
|
+
ChatType: message.chatType,
|
|
388
|
+
Provider: OPENCLAW_WORKCLAW_CHANNEL,
|
|
389
|
+
Surface: OPENCLAW_WORKCLAW_CHANNEL,
|
|
390
|
+
Timestamp: message.timestamp,
|
|
391
|
+
AccountId: accountId,
|
|
392
|
+
AgentId: agentId,
|
|
393
|
+
MessageSid: message.messageId,
|
|
394
|
+
OriginatingChannel: OPENCLAW_WORKCLAW_CHANNEL,
|
|
395
|
+
OriginatingTo: message.to,
|
|
396
|
+
};
|
|
397
|
+
if (sessionKey) {
|
|
398
|
+
result.SessionKey = sessionKey;
|
|
399
|
+
}
|
|
400
|
+
// If no pluginRuntime provided, return basic context without session recording
|
|
401
|
+
if (!pluginRuntime) {
|
|
402
|
+
return result;
|
|
403
|
+
}
|
|
404
|
+
// Full session handling path
|
|
405
|
+
const storePath = pluginRuntime.channel.session.resolveStorePath(config.session?.store, { agentId });
|
|
406
|
+
log?.info?.(`[Session] storePath=${storePath}, agentId=${agentId}, sessionKey=${sessionKey}`);
|
|
407
|
+
// 获取 envelope 格式选项
|
|
408
|
+
const envelopeOptions = pluginRuntime.channel.reply.resolveEnvelopeFormatOptions(config);
|
|
409
|
+
// 获取上次的 session 更新时间
|
|
410
|
+
const previousTimestamp = pluginRuntime.channel.session.readSessionUpdatedAt({
|
|
411
|
+
storePath,
|
|
412
|
+
sessionKey: sessionKey,
|
|
413
|
+
});
|
|
414
|
+
log?.info?.(`[Session] previousTimestamp=${previousTimestamp}`);
|
|
415
|
+
// 处理附件:下载 fileUrl 到本地,并收集媒体信息
|
|
416
|
+
let mediaUrls = [];
|
|
417
|
+
let mediaTypes = [];
|
|
418
|
+
let bodyText = message.text;
|
|
419
|
+
if (message.attachments && message.attachments.length > 0) {
|
|
420
|
+
log?.info?.(`[Attachments] Downloading ${message.attachments.length} files...`);
|
|
421
|
+
// 并行下载所有附件
|
|
422
|
+
const downloadResults = await Promise.all(message.attachments.map(async (att) => {
|
|
423
|
+
const localPath = await downloadFileToLocal(att.url, accountId, message.from, log);
|
|
424
|
+
const mediaType = detectMediaType(att.url);
|
|
425
|
+
const typeLabel = getMediaTypeLabel(att.url);
|
|
426
|
+
return { att, localPath, mediaType, typeLabel };
|
|
427
|
+
}));
|
|
428
|
+
// 收集下载成功的文件信息(MediaUrls 由 SDK 用于显示 [media attached])
|
|
429
|
+
for (const { att, localPath, mediaType } of downloadResults) {
|
|
430
|
+
if (localPath) {
|
|
431
|
+
// 将绝对路径转为根相对路径(去掉 ~/.openclaw/ 前缀,转为 /.openclaw/... 形式)
|
|
432
|
+
// OpenClaw 的 skill 系统会 block 绝对路径(C:\...)和 ~ 路径
|
|
433
|
+
// 兼容 Windows 路径格式(C:\Users\用户名\.openclaw\... 或 C:\Users\用户名.openclaw\...)
|
|
434
|
+
const homeDir = os.homedir().replace(/\\/g, '/').trim(); // 统一用正斜杠,去除尾随空格
|
|
435
|
+
const normalizedLocalPath = localPath.replace(/\\/g, '/');
|
|
436
|
+
const relativePath = normalizedLocalPath.startsWith(`${homeDir}/.openclaw/`)
|
|
437
|
+
? `/${normalizedLocalPath.slice(homeDir.length + 1)}` // 用 / 开头表示根相对路径
|
|
438
|
+
: normalizedLocalPath;
|
|
439
|
+
mediaUrls.push(relativePath);
|
|
440
|
+
mediaTypes.push(mediaType);
|
|
441
|
+
log?.info?.(`[Attachments] Downloaded: ${relativePath}`);
|
|
442
|
+
}
|
|
443
|
+
else {
|
|
444
|
+
log?.warn?.(`[Attachments] Failed to download: ${att.url}`);
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
log?.info?.(`[Attachments] Downloaded ${mediaUrls.length}/${message.attachments.length} files`);
|
|
448
|
+
}
|
|
449
|
+
const fromLabel = message.to;
|
|
450
|
+
const body = pluginRuntime.channel.reply.formatInboundEnvelope({
|
|
451
|
+
channel: OPENCLAW_WORKCLAW_CHANNEL,
|
|
452
|
+
from: fromLabel,
|
|
453
|
+
timestamp: message.timestamp,
|
|
454
|
+
body: bodyText,
|
|
455
|
+
chatType: message.chatType,
|
|
456
|
+
sender: { name: message.to, id: message.to },
|
|
457
|
+
previousTimestamp,
|
|
458
|
+
envelope: envelopeOptions,
|
|
459
|
+
});
|
|
460
|
+
// 构建完整的 ctx
|
|
461
|
+
const finalized = pluginRuntime.channel.reply.finalizeInboundContext({
|
|
462
|
+
Body: body,
|
|
463
|
+
BodyForAgent: bodyText,
|
|
464
|
+
RawBody: JSON.stringify(message),
|
|
465
|
+
CommandBody: message.text,
|
|
466
|
+
From: message.from,
|
|
467
|
+
To: message.to,
|
|
468
|
+
SessionKey: sessionKey,
|
|
469
|
+
AccountId: accountId,
|
|
470
|
+
ChatType: message.chatType,
|
|
471
|
+
ConversationLabel: fromLabel,
|
|
472
|
+
Provider: OPENCLAW_WORKCLAW_CHANNEL,
|
|
473
|
+
Surface: 'openclaw-workclaw_dm',
|
|
474
|
+
MessageSid: message.messageId,
|
|
475
|
+
Timestamp: message.timestamp,
|
|
476
|
+
OriginatingChannel: OPENCLAW_WORKCLAW_CHANNEL,
|
|
477
|
+
OriginatingTo: message.to,
|
|
478
|
+
// 附件媒体信息(供 skill 读取本地文件)
|
|
479
|
+
MediaPath: mediaUrls[0] || undefined,
|
|
480
|
+
MediaUrl: mediaUrls[0] || undefined,
|
|
481
|
+
MediaUrls: mediaUrls.length > 0 ? mediaUrls : undefined,
|
|
482
|
+
MediaTypes: mediaTypes.length > 0 ? mediaTypes : undefined,
|
|
483
|
+
});
|
|
484
|
+
// 记录 session(需要在 dispatchReplyWithBufferedBlockDispatcher 之前调用)
|
|
485
|
+
// 这样 OpenClaw 才能在工具执行时正确关联 session
|
|
486
|
+
if (sessionKey && recordSession) {
|
|
487
|
+
try {
|
|
488
|
+
await pluginRuntime.channel.session.recordInboundSession({
|
|
489
|
+
storePath,
|
|
490
|
+
sessionKey: finalized.SessionKey || sessionKey,
|
|
491
|
+
ctx: finalized,
|
|
492
|
+
updateLastRoute: {
|
|
493
|
+
sessionKey,
|
|
494
|
+
channel: OPENCLAW_WORKCLAW_CHANNEL,
|
|
495
|
+
to: message.to,
|
|
496
|
+
accountId,
|
|
497
|
+
},
|
|
498
|
+
onRecordError: (err) => {
|
|
499
|
+
log?.error?.(`[Session] recordInboundSession failed: ${String(err)}`);
|
|
500
|
+
},
|
|
501
|
+
});
|
|
502
|
+
log?.info?.(`[Session] Recorded session for key=${sessionKey}`);
|
|
503
|
+
}
|
|
504
|
+
catch (err) {
|
|
505
|
+
log?.warn?.(`[Session] recordInboundSession error: ${String(err)}`);
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
return finalized;
|
|
509
|
+
}
|