@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.
Files changed (145) hide show
  1. package/README.md +30 -11
  2. package/dist/api.d.ts +3 -0
  3. package/dist/api.js +3 -0
  4. package/dist/index.d.ts +11 -0
  5. package/dist/index.js +264 -0
  6. package/dist/setup-entry.d.ts +2 -0
  7. package/dist/setup-entry.js +12 -0
  8. package/dist/src/accounts.d.ts +66 -0
  9. package/dist/src/accounts.js +279 -0
  10. package/dist/src/api/accounts-api.d.ts +2 -0
  11. package/dist/src/api/accounts-api.js +130 -0
  12. package/dist/src/api/prompts-api.d.ts +2 -0
  13. package/dist/src/api/prompts-api.js +103 -0
  14. package/dist/src/api/session-api.d.ts +2 -0
  15. package/dist/src/api/session-api.js +207 -0
  16. package/dist/src/api/skills-api.d.ts +2 -0
  17. package/dist/src/api/skills-api.js +64 -0
  18. package/dist/src/api/workspace.d.ts +3 -0
  19. package/dist/src/api/workspace.js +30 -0
  20. package/dist/src/channel.d.ts +11 -0
  21. package/{src/channel.ts → dist/src/channel.js} +199 -225
  22. package/dist/src/config-schema.d.ts +93 -0
  23. package/dist/src/config-schema.js +56 -0
  24. package/dist/src/connection/workclaw-client.d.ts +147 -0
  25. package/dist/src/connection/workclaw-client.js +351 -0
  26. package/dist/src/gateway/agent-handlers.d.ts +12 -0
  27. package/{src/gateway/agent-handlers.ts → dist/src/gateway/agent-handlers.js} +372 -462
  28. package/dist/src/gateway/config-writer.d.ts +71 -0
  29. package/dist/src/gateway/config-writer.js +302 -0
  30. package/dist/src/gateway/cron-tasks-handler.d.ts +19 -0
  31. package/dist/src/gateway/cron-tasks-handler.js +188 -0
  32. package/dist/src/gateway/message-context.d.ts +80 -0
  33. package/{src/gateway/message-context.ts → dist/src/gateway/message-context.js} +509 -594
  34. package/dist/src/gateway/message-dispatcher.d.ts +18 -0
  35. package/dist/src/gateway/message-dispatcher.js +572 -0
  36. package/dist/src/gateway/reconnect.d.ts +27 -0
  37. package/{src/gateway/reconnect.ts → dist/src/gateway/reconnect.js} +210 -253
  38. package/dist/src/gateway/skills-handler.d.ts +29 -0
  39. package/dist/src/gateway/skills-handler.js +615 -0
  40. package/dist/src/gateway/skills-list-handler.d.ts +27 -0
  41. package/dist/src/gateway/skills-list-handler.js +233 -0
  42. package/dist/src/gateway/tools-list-handler.d.ts +30 -0
  43. package/dist/src/gateway/tools-list-handler.js +101 -0
  44. package/dist/src/gateway/workclaw-gateway.d.ts +14 -0
  45. package/dist/src/gateway/workclaw-gateway.js +237 -0
  46. package/dist/src/media/upload.d.ts +13 -0
  47. package/dist/src/media/upload.js +125 -0
  48. package/dist/src/outbound/index.d.ts +36 -0
  49. package/dist/src/outbound/index.js +123 -0
  50. package/dist/src/outbound/workclaw-sender.d.ts +36 -0
  51. package/{src/outbound/workclaw-sender.ts → dist/src/outbound/workclaw-sender.js} +95 -158
  52. package/dist/src/runtime.d.ts +116 -0
  53. package/dist/src/runtime.js +374 -0
  54. package/dist/src/secret-contract-api.d.ts +4 -0
  55. package/dist/src/secret-contract-api.js +4 -0
  56. package/{src/send.ts → dist/src/send.d.ts} +1 -1
  57. package/dist/src/send.js +1 -0
  58. package/dist/src/setup-api.d.ts +3 -0
  59. package/dist/src/setup-api.js +3 -0
  60. package/dist/src/setup-core.d.ts +3 -0
  61. package/dist/src/setup-core.js +13 -0
  62. package/dist/src/setup-surface.d.ts +7 -0
  63. package/dist/src/setup-surface.js +363 -0
  64. package/dist/src/tools/openclaw-workclaw-cron/api/index.d.ts +93 -0
  65. package/dist/src/tools/openclaw-workclaw-cron/api/index.js +209 -0
  66. package/dist/src/tools/openclaw-workclaw-cron/index.d.ts +10 -0
  67. package/{src/tools/openclaw-workclaw-cron/index.ts → dist/src/tools/openclaw-workclaw-cron/index.js} +33 -39
  68. package/dist/src/tools/openclaw-workclaw-cron/src/add/params.d.ts +16 -0
  69. package/dist/src/tools/openclaw-workclaw-cron/src/add/params.js +152 -0
  70. package/dist/src/tools/openclaw-workclaw-cron/src/add/sync.d.ts +18 -0
  71. package/dist/src/tools/openclaw-workclaw-cron/src/add/sync.js +162 -0
  72. package/dist/src/tools/openclaw-workclaw-cron/src/disable/params.d.ts +14 -0
  73. package/dist/src/tools/openclaw-workclaw-cron/src/disable/params.js +80 -0
  74. package/dist/src/tools/openclaw-workclaw-cron/src/disable/sync.d.ts +14 -0
  75. package/dist/src/tools/openclaw-workclaw-cron/src/disable/sync.js +107 -0
  76. package/dist/src/tools/openclaw-workclaw-cron/src/enable/params.d.ts +14 -0
  77. package/dist/src/tools/openclaw-workclaw-cron/src/enable/params.js +80 -0
  78. package/dist/src/tools/openclaw-workclaw-cron/src/enable/sync.d.ts +14 -0
  79. package/dist/src/tools/openclaw-workclaw-cron/src/enable/sync.js +107 -0
  80. package/dist/src/tools/openclaw-workclaw-cron/src/notify/sync.d.ts +17 -0
  81. package/dist/src/tools/openclaw-workclaw-cron/src/notify/sync.js +127 -0
  82. package/dist/src/tools/openclaw-workclaw-cron/src/remove/params.d.ts +14 -0
  83. package/dist/src/tools/openclaw-workclaw-cron/src/remove/params.js +87 -0
  84. package/dist/src/tools/openclaw-workclaw-cron/src/remove/sync.d.ts +14 -0
  85. package/dist/src/tools/openclaw-workclaw-cron/src/remove/sync.js +107 -0
  86. package/dist/src/tools/openclaw-workclaw-cron/src/update/params.d.ts +17 -0
  87. package/dist/src/tools/openclaw-workclaw-cron/src/update/params.js +164 -0
  88. package/dist/src/tools/openclaw-workclaw-cron/src/update/sync.d.ts +18 -0
  89. package/dist/src/tools/openclaw-workclaw-cron/src/update/sync.js +135 -0
  90. package/dist/src/tools/openclaw-workclaw-cron/types/index.d.ts +52 -0
  91. package/dist/src/tools/openclaw-workclaw-cron/types/index.js +4 -0
  92. package/dist/src/tools/openclaw-workclaw-cron/utils/index.d.ts +40 -0
  93. package/dist/src/tools/openclaw-workclaw-cron/utils/index.js +122 -0
  94. package/dist/src/tools/openclaw-workclaw-system/index.d.ts +10 -0
  95. package/{src/tools/openclaw-workclaw-system/index.ts → dist/src/tools/openclaw-workclaw-system/index.js} +15 -17
  96. package/dist/src/tools/openclaw-workclaw-system/src/get/index.d.ts +8 -0
  97. package/dist/src/tools/openclaw-workclaw-system/src/get/index.js +64 -0
  98. package/dist/src/tools/openclaw-workclaw-system/src/token/index.d.ts +8 -0
  99. package/dist/src/tools/openclaw-workclaw-system/src/token/index.js +78 -0
  100. package/{src/types.ts → dist/src/types.d.ts} +39 -58
  101. package/dist/src/types.js +1 -0
  102. package/dist/src/utils/content.d.ts +15 -0
  103. package/{src/utils/content.ts → dist/src/utils/content.js} +38 -35
  104. package/package.json +13 -9
  105. package/index.ts +0 -302
  106. package/setup-entry.ts +0 -6
  107. package/src/accounts.ts +0 -312
  108. package/src/api/accounts-api.ts +0 -156
  109. package/src/api/prompts-api.ts +0 -116
  110. package/src/api/session-api.ts +0 -238
  111. package/src/api/skills-api.ts +0 -72
  112. package/src/api/workspace.ts +0 -41
  113. package/src/config-schema.ts +0 -110
  114. package/src/connection/workclaw-client.ts +0 -643
  115. package/src/gateway/config-writer.ts +0 -296
  116. package/src/gateway/message-dispatcher.ts +0 -641
  117. package/src/gateway/skills-handler.ts +0 -741
  118. package/src/gateway/skills-list-handler.ts +0 -332
  119. package/src/gateway/tools-list-handler.ts +0 -160
  120. package/src/gateway/workclaw-gateway.ts +0 -367
  121. package/src/media/upload.ts +0 -157
  122. package/src/outbound/index.ts +0 -185
  123. package/src/runtime.ts +0 -497
  124. package/src/tools/openclaw-workclaw-cron/api/index.ts +0 -326
  125. package/src/tools/openclaw-workclaw-cron/src/add/params.ts +0 -177
  126. package/src/tools/openclaw-workclaw-cron/src/add/sync.ts +0 -188
  127. package/src/tools/openclaw-workclaw-cron/src/disable/params.ts +0 -100
  128. package/src/tools/openclaw-workclaw-cron/src/disable/sync.ts +0 -127
  129. package/src/tools/openclaw-workclaw-cron/src/enable/params.ts +0 -100
  130. package/src/tools/openclaw-workclaw-cron/src/enable/sync.ts +0 -127
  131. package/src/tools/openclaw-workclaw-cron/src/notify/sync.ts +0 -148
  132. package/src/tools/openclaw-workclaw-cron/src/remove/params.ts +0 -109
  133. package/src/tools/openclaw-workclaw-cron/src/remove/sync.ts +0 -127
  134. package/src/tools/openclaw-workclaw-cron/src/update/params.ts +0 -195
  135. package/src/tools/openclaw-workclaw-cron/src/update/sync.ts +0 -161
  136. package/src/tools/openclaw-workclaw-cron/types/index.ts +0 -55
  137. package/src/tools/openclaw-workclaw-cron/utils/index.ts +0 -141
  138. package/src/tools/openclaw-workclaw-system/src/get/index.ts +0 -77
  139. package/src/tools/openclaw-workclaw-system/src/token/index.ts +0 -93
  140. package/tests/accounts.test.ts +0 -285
  141. package/tests/message-context.test.ts +0 -313
  142. package/tests/reconnect.test.ts +0 -257
  143. package/tests/workclaw-client.test.ts +0 -112
  144. package/tsconfig.json +0 -14
  145. package/vitest.config.ts +0 -8
@@ -1,641 +0,0 @@
1
- /**
2
- * Message Dispatcher - handles all WorkClaw WebSocket message types
3
- */
4
-
5
- import type { ResolvedWorkclawAccount, WorkClawBaseConfig, WorkClawAccountConfig } from "../types.js";
6
- import { parseWorkClawMessage, type ParseWorkClawResult } from "./message-context.js";
7
- import { buildInboundContext, type InboundMessage } from "./message-context.js";
8
- import { handleAgentCreated, handleAgentUpdated, handleAgentDeleted } from "./agent-handlers.js";
9
- import { saveOpenConversationId, saveWorkClawAgentId, saveWorkClawApiKey, saveWorkClawUserId } from "./config-writer.js";
10
- import { getWorkclawWsConnection, setLastInboundAt } from "../runtime.js";
11
- import { getWorkclawRuntime } from "../runtime.js";
12
- import { sendMessageWorkclaw } from "../outbound/index.js";
13
- import { resolveAccountByUserIdAndAgentId, resolveWorkclawAccountWithCache } from "../accounts.js";
14
- import { getWorkclawAccessToken, getWorkclawModelConfigByAppKey } from "../connection/workclaw-client.js";
15
- import { setToolContext } from "../runtime.js";
16
- import { handleSkillsEvent } from "./skills-handler.js";
17
- import { handleToolsListEvent as doHandleToolsListEvent } from "./tools-list-handler.js";
18
- import { handleSkillsListEvent as doHandleSkillsListEvent } from "./skills-list-handler.js";
19
- import { request } from "undici";
20
-
21
- export interface MessageDispatcherContext {
22
- accountId: string;
23
- account: ResolvedWorkclawAccount;
24
- cfg: any;
25
- baseConfig: WorkClawBaseConfig;
26
- accountConfig: WorkClawAccountConfig;
27
- log?: {
28
- info?: (msg: string) => void;
29
- warn?: (msg: string) => void;
30
- error?: (msg: string) => void;
31
- debug?: (msg: string) => void;
32
- };
33
- scheduleReconnect: () => void;
34
- }
35
-
36
- /** Log an error safely */
37
- function logError(log: MessageDispatcherContext["log"], context: string, err: unknown): void {
38
- const errAny = err as { stack?: string };
39
- const stack = typeof errAny?.stack === "string" ? `\nStack: ${errAny.stack}` : "";
40
- log?.error?.(`Dispatcher ${context}: ${String(err)}${stack}`);
41
- }
42
-
43
- // ---------------------------------------------------------------------------
44
- // Individual message handlers
45
- // ---------------------------------------------------------------------------
46
-
47
- async function handlePing(ctx: MessageDispatcherContext, result: ParseWorkClawResult): Promise<void> {
48
- const { accountId, log } = ctx;
49
- const ws = getWorkclawWsConnection(accountId);
50
- if (!ws) return;
51
-
52
- try {
53
- const pongResponse = {
54
- code: 200,
55
- message: "pong",
56
- metadata: { contentType: "application/json" },
57
- data: JSON.stringify(result.pongData),
58
- };
59
- ws.send(JSON.stringify(pongResponse));
60
- log?.debug?.(`pong`);
61
- } catch (err) {
62
- logError(log, "Failed to send pong", err);
63
- }
64
- }
65
-
66
- async function handleDisconnect(ctx: MessageDispatcherContext): Promise<void> {
67
- const { accountId, log, scheduleReconnect } = ctx;
68
- const ws = getWorkclawWsConnection(accountId);
69
- if (ws) {
70
- try {
71
- ws.close();
72
- log?.info?.(`Closed WebSocket due to disconnect command`);
73
- } catch (err) {
74
- logError(log, "Failed to close WebSocket", err);
75
- }
76
- } else {
77
- log?.info?.(`WebSocket already cleared, triggering reconnect...`);
78
- scheduleReconnect();
79
- }
80
- }
81
-
82
- async function handleAgentEvent(
83
- ctx: MessageDispatcherContext,
84
- result: ParseWorkClawResult,
85
- ): Promise<void> {
86
- const { cfg, log, baseConfig, accountId } = ctx;
87
- const eventData = result.eventData;
88
- if (!eventData) {
89
- log?.warn?.(`Received ${result.type} event but no data`);
90
- return;
91
- }
92
-
93
- const agentId = String(eventData.id || "");
94
- const userId = String(eventData.futureId || "");
95
-
96
- if (!agentId) {
97
- log?.warn?.(`Received ${result.type} event but missing agent id`);
98
- return;
99
- }
100
-
101
- log?.info?.(`Processing ${result.type} event for agent ${agentId}, userId: ${userId}`);
102
-
103
- const accountData = {
104
- agentId,
105
- userId,
106
- nickName: eventData.nickName,
107
- phone: eventData.phone,
108
- status: eventData.status,
109
- introduction: eventData.introduction,
110
- name: eventData.name,
111
- tip: eventData.tip,
112
- characterSettings: eventData.characterSettings,
113
- personFeatures: eventData.personFeatures,
114
- workFeatures: eventData.workFeatures,
115
- learningFeatures: eventData.learningFeatures,
116
- socializeFeatures: eventData.socializeFeatures,
117
- job: eventData.job,
118
- city: eventData.city,
119
- };
120
-
121
- switch (result.type) {
122
- case "agent_created":
123
- await handleAgentCreated(accountData, cfg, log);
124
- handleAgentCreatedMessage(baseConfig, agentId, accountId, log)
125
- break;
126
- case "agent_updated":
127
- await handleAgentUpdated(accountData, cfg, log);
128
- break;
129
- case "agent_deleted":
130
- await handleAgentDeleted(accountData, cfg, log);
131
- break;
132
- }
133
- }
134
-
135
- async function handleSkillsProcessingEvent(ctx: MessageDispatcherContext, result: ParseWorkClawResult): Promise<void> {
136
- const { accountId, baseConfig, log } = ctx;
137
- if (!result.eventData) return;
138
- try {
139
- const connConfig = {
140
- baseUrl: baseConfig.baseUrl || "",
141
- websocketUrl: baseConfig.websocketUrl,
142
- appKey: baseConfig.appKey,
143
- appSecret: baseConfig.appSecret,
144
- localIp: baseConfig.localIp,
145
- allowInsecureTls: baseConfig.allowInsecureTls,
146
- requestTimeout: baseConfig.requestTimeout,
147
- };
148
- const tokenCacheKey = baseConfig.appKey || accountId;
149
- const token = await getWorkclawAccessToken(tokenCacheKey, connConfig);
150
- await handleSkillsEvent(result.eventData, token, connConfig.baseUrl, connConfig.appKey || accountId, log);
151
- } catch (err) {
152
- logError(log, "handleSkillsEvent failed", err);
153
- }
154
- }
155
-
156
- async function handleToolsListEvent(ctx: MessageDispatcherContext, result: ParseWorkClawResult): Promise<void> {
157
- const { accountId, baseConfig, log } = ctx;
158
- if (!result.eventData) return;
159
- try {
160
- const connConfig = {
161
- baseUrl: baseConfig.baseUrl || "",
162
- websocketUrl: baseConfig.websocketUrl,
163
- appKey: baseConfig.appKey,
164
- appSecret: baseConfig.appSecret,
165
- localIp: baseConfig.localIp,
166
- allowInsecureTls: baseConfig.allowInsecureTls,
167
- requestTimeout: baseConfig.requestTimeout,
168
- };
169
- const tokenCacheKey = baseConfig.appKey || accountId;
170
- const token = await getWorkclawAccessToken(tokenCacheKey, connConfig);
171
- await doHandleToolsListEvent(result.eventData, connConfig.baseUrl, token, log);
172
- } catch (err) {
173
- logError(log, "handleToolsListEvent failed", err);
174
- }
175
- }
176
-
177
- async function handleSkillsListEvent(ctx: MessageDispatcherContext, result: ParseWorkClawResult): Promise<void> {
178
- const { accountId, baseConfig, log } = ctx;
179
- if (!result.eventData) return;
180
- try {
181
- const connConfig = {
182
- baseUrl: baseConfig.baseUrl || "",
183
- websocketUrl: baseConfig.websocketUrl,
184
- appKey: baseConfig.appKey,
185
- appSecret: baseConfig.appSecret,
186
- localIp: baseConfig.localIp,
187
- allowInsecureTls: baseConfig.allowInsecureTls,
188
- requestTimeout: baseConfig.requestTimeout,
189
- };
190
- const tokenCacheKey = baseConfig.appKey || accountId;
191
- const token = await getWorkclawAccessToken(tokenCacheKey, connConfig);
192
- await doHandleSkillsListEvent(result.eventData, connConfig.baseUrl, token, log);
193
- } catch (err) {
194
- logError(log, "handleSkillsListEvent failed", err);
195
- }
196
- }
197
-
198
- async function handleInitAgentEvent(
199
- ctx: MessageDispatcherContext,
200
- result: ParseWorkClawResult,
201
- ): Promise<void> {
202
- const { accountId, cfg, baseConfig, log } = ctx;
203
- const initStartedAt = Date.now();
204
- const eventData = result.eventData;
205
-
206
- if (!eventData) {
207
- log?.warn?.(`init_agent received but no data`);
208
- return;
209
- }
210
-
211
- log?.info?.(
212
- `init_agent accountId=${accountId} eventDataType=${typeof eventData} preview=${JSON.stringify(eventData).slice(0, 200)}`,
213
- );
214
-
215
- const connConfig = {
216
- baseUrl: baseConfig.baseUrl || "",
217
- websocketUrl: baseConfig.websocketUrl,
218
- appKey: baseConfig.appKey,
219
- appSecret: baseConfig.appSecret,
220
- localIp: baseConfig.localIp,
221
- allowInsecureTls: baseConfig.allowInsecureTls,
222
- requestTimeout: baseConfig.requestTimeout,
223
- };
224
-
225
- // Fetch and save apiKey
226
- try {
227
- const tokenCacheKey = baseConfig.appKey || accountId;
228
- const { baseUrl: modelBaseUrl, apiKey } = await getWorkclawModelConfigByAppKey(tokenCacheKey, connConfig);
229
- const maskedKey = apiKey ? `${String(apiKey).slice(0, 6)}...(${String(apiKey).length})` : "missing";
230
-
231
- log?.info?.(`init_agent model fetched baseUrl=${modelBaseUrl} apiKey=${maskedKey}`);
232
-
233
-
234
- await saveWorkClawApiKey(apiKey, cfg, log);
235
- log?.info?.(`init_agent model config saved elapsedMs=${Date.now() - initStartedAt}`);
236
- } catch (err) {
237
- logError(log, "init_agent model fetch/save failed", err);
238
- }
239
-
240
- // Save default agentId
241
- const agentId = String(eventData?.id || eventData?.agentId || "");
242
- if (agentId) {
243
- try {
244
- await saveWorkClawAgentId("default", agentId, cfg, log);
245
- log?.info?.(`init_agent saved default agentId=${agentId}`);
246
- } catch (err) {
247
- logError(log, "init_agent agentId save failed", err);
248
- }
249
- }
250
-
251
- // Save default userId
252
- const userId = String(eventData?.futureId || eventData?.userId || "");
253
- if (userId) {
254
- try {
255
- await saveWorkClawUserId("default", userId, cfg, log);
256
- log?.info?.(`init_agent saved default userId=${userId}`);
257
- } catch (err) {
258
- logError(log, "init_agent userId save failed", err);
259
- }
260
- }
261
-
262
- log?.info?.(`init_agent handled elapsedMs=${Date.now() - initStartedAt}`);
263
- }
264
-
265
- async function handleAgentMessage(
266
- ctx: MessageDispatcherContext,
267
- data: string,
268
- result: ParseWorkClawResult,
269
- ): Promise<void> {
270
- const { accountId, account, cfg, accountConfig, log } = ctx;
271
- const { message } = result;
272
- if (!message) return;
273
-
274
- // 这里的msgAgentId是云平台的agentId
275
- const { text, userId, openConversationId, messageId: parsedMessageId, agentId: msgAgentId } = message;
276
-
277
- // Resolve account from message
278
- let messageAccountId = accountId;
279
- let messageAccount = account;
280
-
281
- if (userId && msgAgentId) {
282
- const resolvedAccountId = resolveAccountByUserIdAndAgentId(cfg, String(userId), String(msgAgentId));
283
- if (resolvedAccountId) {
284
- const resolved = resolveWorkclawAccountWithCache({ cfg, accountId: resolvedAccountId });
285
- messageAccountId = resolvedAccountId;
286
- messageAccount = resolved;
287
- log?.info?.(`Resolved account ${resolvedAccountId} from message (userId=${userId}, agentId=${msgAgentId})`);
288
- } else if (String(msgAgentId) !== String(accountConfig.agentId)) {
289
- log?.warn?.(`Account not found for userId=${userId}, agentId=${msgAgentId}, ignoring`);
290
- return;
291
- }
292
- }
293
-
294
- const resolvedAccountConfig = messageAccount.config as unknown as WorkClawAccountConfig;
295
- const msgAgentIdResolved = msgAgentId ?? accountConfig.agentId;
296
-
297
- // Save agentId if not already set
298
- if (msgAgentId && !resolvedAccountConfig.agentId) {
299
- try {
300
- await saveWorkClawAgentId(messageAccountId, msgAgentId, cfg, log);
301
- log?.info?.(`Saved agentId ${msgAgentId} to account ${messageAccountId}`);
302
- } catch (err) {
303
- logError(log, "saveWorkClawAgentId failed", err);
304
- }
305
- }
306
-
307
- // Save openConversationId if new
308
- if (openConversationId && openConversationId !== resolvedAccountConfig.openConversationId) {
309
- try {
310
- await saveOpenConversationId(messageAccountId, openConversationId, cfg, log);
311
- log?.info?.(`Saved openConversationId ${openConversationId} to account ${messageAccountId}`);
312
- } catch (err) {
313
- logError(log, "saveOpenConversationId failed", err);
314
- }
315
- }
316
-
317
- // Save userId if not already set (directly to openclawWorkclaw level)
318
- const workclawConfig = cfg?.channels?.['openclaw-workclaw'] || {};
319
- if (userId && !workclawConfig.userId) {
320
- try {
321
- await saveWorkClawUserId(messageAccountId, userId, cfg, log);
322
- log?.info?.(`Saved userId ${userId} to workclaw config`);
323
- } catch (err) {
324
- logError(log, "saveWorkClawUserId failed", err);
325
- }
326
- }
327
-
328
- const resolvedOpenConversationId = openConversationId || resolvedAccountConfig.openConversationId || "";
329
- const to = userId;
330
- const bodyText = text || data;
331
-
332
- if (!bodyText) {
333
- log?.warn?.(`Empty body, ignoring`);
334
- return;
335
- }
336
-
337
- // Route via bindings
338
- const bindings = cfg?.bindings ?? [];
339
- let targetAgentId = "default";
340
- const matchedBinding = bindings.find((binding: any) => {
341
- const match = binding?.match;
342
- if (!match || match.channel !== "openclaw-workclaw") return false;
343
- return match.accountId === messageAccountId || match.accountId === "*";
344
- });
345
- if (matchedBinding) {
346
- targetAgentId = matchedBinding.agentId;
347
- log?.info?.(`Routing to agent ${targetAgentId} via binding (account: ${messageAccountId})`);
348
- }
349
-
350
- if (!targetAgentId) {
351
- log?.warn?.(`Missing target, ignoring`);
352
- return;
353
- }
354
-
355
- // 构建用于解析 sessionKey 的消息对象
356
- // to = agentId (智能体/机器人), from = userId (用户)
357
- const attachments = message.attachments
358
- const inboundMessage: InboundMessage = {
359
- text: bodyText,
360
- to: to,
361
- from: userId,
362
- chatType: "dm",
363
- messageId: parsedMessageId,
364
- rawPayload: data,
365
- timestamp: Date.now(),
366
- attachments: attachments,
367
- };
368
- log?.info?.(`handleAgentMessage: text=${bodyText?.substring(0, 50)}, attachments=${attachments?.length || 0}`)
369
- const pluginRuntime = getWorkclawRuntime();
370
-
371
- // 使用扩展后的 buildInboundContext,它会处理完整的 session 流程
372
- const finalized = await buildInboundContext(
373
- inboundMessage,
374
- messageAccountId,
375
- messageAccount.config,
376
- targetAgentId,
377
- pluginRuntime,
378
- true,
379
- log,
380
- );
381
-
382
- // 维护当前工具名称状态
383
- let currentToolName: string | null = null;
384
- // 追踪 final 是否已投递(框架在流式输出时会 drop final,需要手动补发 last: true)
385
- let finalDelivered = false;
386
-
387
- // 发送工具消息的辅助函数
388
- const sendToolMessage = async (text: string, isStart: boolean = false) => {
389
- try {
390
- await sendMessageWorkclaw({
391
- cfg,
392
- to: to,
393
- text,
394
- accountId: messageAccountId,
395
- openConversationId: resolvedOpenConversationId,
396
- agentId: msgAgentIdResolved,
397
- replyToMessageId: parsedMessageId,
398
- last: false,
399
- });
400
- } catch (err) {
401
- logError(log, isStart ? "Tool start message send failed" : "Tool result message send failed", err);
402
- }
403
- };
404
-
405
- await pluginRuntime.channel.reply.dispatchReplyWithBufferedBlockDispatcher({
406
- ctx: finalized,
407
- cfg,
408
- replyOptions: {
409
- onAgentRunStart(runId: string) {
410
- // 保存 runId 和 ctx 的关联,供 hook 使用
411
- setToolContext(runId, {
412
- target: targetAgentId,
413
- replyToMessageId: parsedMessageId,
414
- openConversationId: resolvedOpenConversationId,
415
- accountId: messageAccountId,
416
- agentId: String(msgAgentIdResolved),
417
- sessionKey: (finalized.SessionKey as string) || "",
418
- });
419
- log?.info?.(`[AGENT] onAgentRunStart: runId=${runId} target=${targetAgentId}`);
420
- },
421
- /*onToolStart(payload) {
422
- log?.info?.(`[AGENT] onToolStart: ${JSON.stringify(payload)}`);
423
- if (payload.phase === "start") {
424
- currentToolName = payload.name ?? null;
425
- const hint = getToolStartHint(currentToolName);
426
- log?.info?.(`[TOOL] Starting tool: ${currentToolName}, sending hint: ${hint}`);
427
- sendToolMessage(hint, true).catch((err) => {
428
- logError(log, "Tool start hint send failed", err);
429
- });
430
- }
431
- }*/
432
- },
433
- dispatcherOptions: {
434
- deliver: async (payload: any, info?: any) => {
435
- // 记录每次 deliver 调用,帮助调试 info.kind 问题
436
- log?.info?.(`[DELIVER] deliver called: info.kind=${info?.kind}, payload.text=${String(payload?.text ?? "").substring(0, 100)}`);
437
- const resolvedReplyToId = payload?.replyToId || parsedMessageId;
438
- let textOut = String(payload.text ?? payload.body ?? "");
439
- const target = to || String(payload.to ?? "");
440
-
441
- if (!target) {
442
- log?.warn?.(`Skipping reply: no target`);
443
- return;
444
- }
445
-
446
- if (!textOut.trim()) {
447
- log?.warn?.(`Empty reply text, skipping`);
448
- return;
449
- }
450
-
451
- if (info?.kind === "tool") return;
452
-
453
- // 处理工具类型的 deliver
454
- /*if (info?.kind === "tool") {
455
- // log?.info?.(`[DELIVER] tool reply: ${JSON.stringify(payload)}`);
456
- const toolText = payload.text ?? "";
457
- if (toolText.trim()) {
458
- // 使用 formattedToolOutput 格式发送
459
- try {
460
- await sendMessageWorkclaw({
461
- cfg,
462
- to: targetAgentId,
463
- text: JSON.stringify({
464
- name: currentToolName || "Unknown",
465
- content: toolText,
466
- state: "result",
467
- }),
468
- msgType: "26",
469
- accountId: messageAccountId,
470
- openConversationId: resolvedOpenConversationId,
471
- agentId: msgAgentIdResolved,
472
- replyToMessageId: parsedMessageId,
473
- });
474
- } catch (err) {
475
- logError(log, "Tool result send failed", err);
476
- }
477
- }
478
- return;
479
- }*/
480
-
481
- // final 时清空工具状态
482
- if (info?.kind === "final") {
483
- currentToolName = null;
484
- finalDelivered = true;
485
- log?.info?.(`[DELIVER] final received, cleared currentToolName`);
486
- }
487
-
488
- // 明确设置 last:final 时为 true,否则为 false(显式转换,避免 undefined)
489
- const isFinal = info?.kind === "final";
490
- const isLast = isFinal ? true : false;
491
- log?.info?.(`[DELIVER] is last: ${isLast} (info.kind=${info?.kind})`);
492
-
493
- try {
494
- const result = await sendMessageWorkclaw({
495
- cfg, to: target, text: textOut,
496
- accountId: messageAccountId, openConversationId: resolvedOpenConversationId,
497
- agentId: msgAgentIdResolved, replyToMessageId: resolvedReplyToId,
498
- last: isLast,
499
- });
500
- log?.info?.(`Reply sent messageId=${result.messageId}`);
501
- } catch (err) {
502
- logError(log, "Reply send failed", err);
503
- throw err;
504
- }
505
- },
506
- },
507
- });
508
-
509
- // 框架在流式输出时会 drop final payload,检测到时应手动补发 last: true
510
- if (!finalDelivered) {
511
- log?.warn?.(`[DELIVER] final was dropped by framework, sending last: true`);
512
- try {
513
- // 发送一个空格作为占位内容,确保 last: true 能被服务端接受
514
- await sendMessageWorkclaw({
515
- cfg, to: to || String(userId || ""),
516
- text: "",
517
- accountId: messageAccountId, openConversationId: resolvedOpenConversationId,
518
- agentId: msgAgentIdResolved, replyToMessageId: parsedMessageId,
519
- last: true,
520
- });
521
- } catch (err) {
522
- // 服务端可能拒绝空内容的 last 信号,忽略即可
523
- const errStr = String(err);
524
- if (errStr.includes("content can't be empty")) {
525
- log?.info?.(`[DELIVER] last signal skipped (empty content not supported)`);
526
- } else {
527
- logError(log, "Final/last signal send failed", err);
528
- }
529
- }
530
- }
531
- }
532
-
533
- // ---------------------------------------------------------------------------
534
- // Main dispatcher
535
- // ---------------------------------------------------------------------------
536
-
537
- export async function dispatchWorkclawMessage(
538
- data: string,
539
- ctx: MessageDispatcherContext,
540
- ): Promise<void> {
541
- const { log } = ctx;
542
-
543
- const rawHasAuthToken =
544
- data.includes("authToken") ||
545
- data.toLowerCase().includes("authorization") ||
546
- data.toLowerCase().includes("bearer ");
547
- const rawPreview = data.length > 300 ? `${data.slice(0, 300)}...` : data;
548
- /*log?.info?.(
549
- `Raw message bytes=${data.length} hasAuthToken=${rawHasAuthToken} preview=${rawPreview}`,
550
- );*/
551
-
552
- const result = parseWorkClawMessage(data, log);
553
- if (!result) {
554
- log?.warn?.(`Failed to parse message`);
555
- return;
556
- }
557
-
558
- log?.info?.(`Message type=${result.type}`);
559
-
560
- // 更新上次消息时间(始终用 accountId,因为每个账号的入站时间需要独立追踪)
561
- const { accountId } = ctx;
562
- if (accountId) {
563
- setLastInboundAt(accountId, Date.now());
564
- }
565
-
566
- switch (result.type) {
567
- case "ping":
568
- await handlePing(ctx, result);
569
- return;
570
-
571
- case "disconnect":
572
- await handleDisconnect(ctx);
573
- return;
574
-
575
- case "agent_created":
576
- case "agent_updated":
577
- case "agent_deleted":
578
- await handleAgentEvent(ctx, result);
579
- return;
580
-
581
- case "skills_event":
582
- await handleSkillsProcessingEvent(ctx, result);
583
- return;
584
-
585
- case "tools_list":
586
- await handleToolsListEvent(ctx, result);
587
- return;
588
-
589
- case "skills_list":
590
- await handleSkillsListEvent(ctx, result);
591
- return;
592
-
593
- case "init_agent":
594
- await handleInitAgentEvent(ctx, result);
595
- return;
596
-
597
- case "ignored":
598
- log?.info?.(`Ignoring message type=${result.type}`);
599
- return;
600
-
601
- case "agent_message":
602
- await handleAgentMessage(ctx, data, result);
603
- return;
604
-
605
- default:
606
- log?.info?.(`Unknown message type: ${result.type}, ignoring`);
607
- }
608
- }
609
-
610
-
611
- // 创建成功回调
612
- async function handleAgentCreatedMessage(baseConfig: WorkClawBaseConfig, agentId: string, accountId: string, log?: any) {
613
- // 通知
614
- try {
615
- const callbackUrl = `${baseConfig.baseUrl}/open-apis/instance/agent-created/status`;
616
- const tokenCacheKey = baseConfig.appKey || accountId;
617
- const callbackData = {
618
- "agentId": agentId,
619
- "success": true
620
- };
621
-
622
- log?.info?.(`[AgentCreated] Calling callback API: ${callbackUrl}`);
623
- const token = await getWorkclawAccessToken(tokenCacheKey, baseConfig);
624
-
625
- const response = await request(callbackUrl, {
626
- method: "POST",
627
- headers: {
628
- "Content-Type": "application/json",
629
- "Authorization": `Bearer ${token}`,
630
- },
631
- body: JSON.stringify(callbackData),
632
- });
633
-
634
- const responseBody = await response.body.text();
635
- log?.info?.(`[AgentCreated] Callback API response: status=${response.statusCode}, body=${responseBody}`);
636
-
637
- } catch (callbackErr) {
638
- // 接口调用失败不影响主流程,只记录错误
639
- log?.error?.(`[AgentCreated] Callback API failed: ${String(callbackErr)}`);
640
- }
641
- }