@workclaw/openclaw-workclaw 1.0.23 → 1.0.26

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 (210) hide show
  1. package/dist/api.d.ts +4 -3
  2. package/dist/api.d.ts.map +1 -0
  3. package/dist/api.js +379 -3
  4. package/dist/channel-DSvR6Irx.js +50017 -0
  5. package/dist/cron-tasks-handler-C-mP3VmV.js +152 -0
  6. package/dist/index.d.ts +5 -4
  7. package/dist/index.d.ts.map +1 -0
  8. package/dist/index.js +2056 -232
  9. package/dist/jiti-4i2h0FGP.js +4814 -0
  10. package/dist/setup-entry.d.ts +2 -1
  11. package/dist/setup-entry.d.ts.map +1 -0
  12. package/dist/setup-entry.js +181 -11
  13. package/dist/src/accounts.d.ts +3 -2
  14. package/dist/src/accounts.d.ts.map +1 -0
  15. package/dist/src/api/accounts-api.d.ts +2 -1
  16. package/dist/src/api/accounts-api.d.ts.map +1 -0
  17. package/dist/src/api/prompts-api.d.ts +2 -1
  18. package/dist/src/api/prompts-api.d.ts.map +1 -0
  19. package/dist/src/api/session-api.d.ts +2 -1
  20. package/dist/src/api/session-api.d.ts.map +1 -0
  21. package/dist/src/api/skills-api.d.ts +2 -1
  22. package/dist/src/api/skills-api.d.ts.map +1 -0
  23. package/dist/src/api/workspace.d.ts +2 -1
  24. package/dist/src/api/workspace.d.ts.map +1 -0
  25. package/dist/src/channel.d.ts +3 -2
  26. package/dist/src/channel.d.ts.map +1 -0
  27. package/dist/src/config-schema.d.ts +1 -6
  28. package/dist/src/config-schema.d.ts.map +1 -0
  29. package/dist/src/connection/workclaw-client.d.ts +6 -4
  30. package/dist/src/connection/workclaw-client.d.ts.map +1 -0
  31. package/dist/src/gateway/agent-handlers.d.ts +3 -2
  32. package/dist/src/gateway/agent-handlers.d.ts.map +1 -0
  33. package/dist/src/gateway/config-writer.d.ts +3 -2
  34. package/dist/src/gateway/config-writer.d.ts.map +1 -0
  35. package/dist/src/gateway/cron-tasks-handler.d.ts +1 -0
  36. package/dist/src/gateway/cron-tasks-handler.d.ts.map +1 -0
  37. package/dist/src/gateway/message-context.d.ts +2 -1
  38. package/dist/src/gateway/message-context.d.ts.map +1 -0
  39. package/dist/src/gateway/message-dispatcher.d.ts +3 -4
  40. package/dist/src/gateway/message-dispatcher.d.ts.map +1 -0
  41. package/dist/src/gateway/reconnect.d.ts +3 -5
  42. package/dist/src/gateway/reconnect.d.ts.map +1 -0
  43. package/dist/src/gateway/skills-handler.d.ts +1 -0
  44. package/dist/src/gateway/skills-handler.d.ts.map +1 -0
  45. package/dist/src/gateway/skills-list-handler.d.ts +1 -0
  46. package/dist/src/gateway/skills-list-handler.d.ts.map +1 -0
  47. package/dist/src/gateway/tools-list-handler.d.ts +1 -0
  48. package/dist/src/gateway/tools-list-handler.d.ts.map +1 -0
  49. package/dist/src/gateway/workclaw-gateway.d.ts +4 -7
  50. package/dist/src/gateway/workclaw-gateway.d.ts.map +1 -0
  51. package/dist/src/media/upload.d.ts +1 -0
  52. package/dist/src/media/upload.d.ts.map +1 -0
  53. package/dist/src/outbound/index.d.ts +9 -6
  54. package/dist/src/outbound/index.d.ts.map +1 -0
  55. package/dist/src/outbound/workclaw-sender.d.ts +6 -4
  56. package/dist/src/outbound/workclaw-sender.d.ts.map +1 -0
  57. package/dist/src/runtime.d.ts +11 -9
  58. package/dist/src/runtime.d.ts.map +1 -0
  59. package/dist/src/secret-contract-api.d.ts +1 -0
  60. package/dist/src/secret-contract-api.d.ts.map +1 -0
  61. package/dist/src/send.d.ts +2 -1
  62. package/dist/src/send.d.ts.map +1 -0
  63. package/dist/src/setup-api.d.ts +4 -3
  64. package/dist/src/setup-api.d.ts.map +1 -0
  65. package/dist/src/setup-core.d.ts +2 -1
  66. package/dist/src/setup-core.d.ts.map +1 -0
  67. package/dist/src/setup-surface.d.ts +3 -2
  68. package/dist/src/setup-surface.d.ts.map +1 -0
  69. package/dist/src/tools/openclaw-workclaw-cron/api/index.d.ts +2 -4
  70. package/dist/src/tools/openclaw-workclaw-cron/api/index.d.ts.map +1 -0
  71. package/dist/src/tools/openclaw-workclaw-cron/index.d.ts +1 -0
  72. package/dist/src/tools/openclaw-workclaw-cron/index.d.ts.map +1 -0
  73. package/dist/src/tools/openclaw-workclaw-cron/src/add/params.d.ts +2 -4
  74. package/dist/src/tools/openclaw-workclaw-cron/src/add/params.d.ts.map +1 -0
  75. package/dist/src/tools/openclaw-workclaw-cron/src/add/sync.d.ts +2 -4
  76. package/dist/src/tools/openclaw-workclaw-cron/src/add/sync.d.ts.map +1 -0
  77. package/dist/src/tools/openclaw-workclaw-cron/src/disable/params.d.ts +2 -4
  78. package/dist/src/tools/openclaw-workclaw-cron/src/disable/params.d.ts.map +1 -0
  79. package/dist/src/tools/openclaw-workclaw-cron/src/disable/sync.d.ts +2 -4
  80. package/dist/src/tools/openclaw-workclaw-cron/src/disable/sync.d.ts.map +1 -0
  81. package/dist/src/tools/openclaw-workclaw-cron/src/enable/params.d.ts +2 -4
  82. package/dist/src/tools/openclaw-workclaw-cron/src/enable/params.d.ts.map +1 -0
  83. package/dist/src/tools/openclaw-workclaw-cron/src/enable/sync.d.ts +2 -4
  84. package/dist/src/tools/openclaw-workclaw-cron/src/enable/sync.d.ts.map +1 -0
  85. package/dist/src/tools/openclaw-workclaw-cron/src/notify/sync.d.ts +2 -6
  86. package/dist/src/tools/openclaw-workclaw-cron/src/notify/sync.d.ts.map +1 -0
  87. package/dist/src/tools/openclaw-workclaw-cron/src/remove/params.d.ts +2 -4
  88. package/dist/src/tools/openclaw-workclaw-cron/src/remove/params.d.ts.map +1 -0
  89. package/dist/src/tools/openclaw-workclaw-cron/src/remove/sync.d.ts +2 -4
  90. package/dist/src/tools/openclaw-workclaw-cron/src/remove/sync.d.ts.map +1 -0
  91. package/dist/src/tools/openclaw-workclaw-cron/src/update/params.d.ts +2 -4
  92. package/dist/src/tools/openclaw-workclaw-cron/src/update/params.d.ts.map +1 -0
  93. package/dist/src/tools/openclaw-workclaw-cron/src/update/sync.d.ts +2 -4
  94. package/dist/src/tools/openclaw-workclaw-cron/src/update/sync.d.ts.map +1 -0
  95. package/dist/src/tools/openclaw-workclaw-cron/types/index.d.ts +1 -0
  96. package/dist/src/tools/openclaw-workclaw-cron/types/index.d.ts.map +1 -0
  97. package/dist/src/tools/openclaw-workclaw-cron/utils/index.d.ts +1 -0
  98. package/dist/src/tools/openclaw-workclaw-cron/utils/index.d.ts.map +1 -0
  99. package/dist/src/tools/openclaw-workclaw-system/index.d.ts +1 -0
  100. package/dist/src/tools/openclaw-workclaw-system/index.d.ts.map +1 -0
  101. package/dist/src/tools/openclaw-workclaw-system/src/get/index.d.ts +2 -4
  102. package/dist/src/tools/openclaw-workclaw-system/src/get/index.d.ts.map +1 -0
  103. package/dist/src/tools/openclaw-workclaw-system/src/token/index.d.ts +2 -4
  104. package/dist/src/tools/openclaw-workclaw-system/src/token/index.d.ts.map +1 -0
  105. package/dist/src/types.d.ts +7 -7
  106. package/dist/src/types.d.ts.map +1 -0
  107. package/dist/src/utils/content.d.ts +1 -0
  108. package/dist/src/utils/content.d.ts.map +1 -0
  109. package/openclaw.plugin.json +6 -0
  110. package/package.json +69 -45
  111. package/api.ts +0 -3
  112. package/dist/src/accounts.js +0 -279
  113. package/dist/src/api/accounts-api.js +0 -130
  114. package/dist/src/api/prompts-api.js +0 -103
  115. package/dist/src/api/session-api.js +0 -207
  116. package/dist/src/api/skills-api.js +0 -64
  117. package/dist/src/api/workspace.js +0 -30
  118. package/dist/src/channel.js +0 -199
  119. package/dist/src/config-schema.js +0 -56
  120. package/dist/src/connection/workclaw-client.js +0 -351
  121. package/dist/src/gateway/agent-handlers.js +0 -433
  122. package/dist/src/gateway/config-writer.js +0 -302
  123. package/dist/src/gateway/cron-tasks-handler.js +0 -188
  124. package/dist/src/gateway/message-context.js +0 -509
  125. package/dist/src/gateway/message-dispatcher.js +0 -572
  126. package/dist/src/gateway/reconnect.js +0 -210
  127. package/dist/src/gateway/skills-handler.js +0 -615
  128. package/dist/src/gateway/skills-list-handler.js +0 -233
  129. package/dist/src/gateway/tools-list-handler.js +0 -101
  130. package/dist/src/gateway/workclaw-gateway.js +0 -237
  131. package/dist/src/media/upload.js +0 -125
  132. package/dist/src/outbound/index.js +0 -123
  133. package/dist/src/outbound/workclaw-sender.js +0 -95
  134. package/dist/src/runtime.js +0 -374
  135. package/dist/src/secret-contract-api.js +0 -4
  136. package/dist/src/send.js +0 -1
  137. package/dist/src/setup-api.js +0 -3
  138. package/dist/src/setup-core.js +0 -13
  139. package/dist/src/setup-surface.js +0 -363
  140. package/dist/src/tools/openclaw-workclaw-cron/api/index.js +0 -209
  141. package/dist/src/tools/openclaw-workclaw-cron/index.js +0 -33
  142. package/dist/src/tools/openclaw-workclaw-cron/src/add/params.js +0 -152
  143. package/dist/src/tools/openclaw-workclaw-cron/src/add/sync.js +0 -162
  144. package/dist/src/tools/openclaw-workclaw-cron/src/disable/params.js +0 -80
  145. package/dist/src/tools/openclaw-workclaw-cron/src/disable/sync.js +0 -107
  146. package/dist/src/tools/openclaw-workclaw-cron/src/enable/params.js +0 -80
  147. package/dist/src/tools/openclaw-workclaw-cron/src/enable/sync.js +0 -107
  148. package/dist/src/tools/openclaw-workclaw-cron/src/notify/sync.js +0 -127
  149. package/dist/src/tools/openclaw-workclaw-cron/src/remove/params.js +0 -87
  150. package/dist/src/tools/openclaw-workclaw-cron/src/remove/sync.js +0 -107
  151. package/dist/src/tools/openclaw-workclaw-cron/src/update/params.js +0 -164
  152. package/dist/src/tools/openclaw-workclaw-cron/src/update/sync.js +0 -135
  153. package/dist/src/tools/openclaw-workclaw-cron/types/index.js +0 -4
  154. package/dist/src/tools/openclaw-workclaw-cron/utils/index.js +0 -122
  155. package/dist/src/tools/openclaw-workclaw-system/index.js +0 -15
  156. package/dist/src/tools/openclaw-workclaw-system/src/get/index.js +0 -64
  157. package/dist/src/tools/openclaw-workclaw-system/src/token/index.js +0 -78
  158. package/dist/src/types.js +0 -1
  159. package/dist/src/utils/content.js +0 -38
  160. package/index.ts +0 -325
  161. package/setup-entry.ts +0 -13
  162. package/src/accounts.ts +0 -360
  163. package/src/api/accounts-api.ts +0 -156
  164. package/src/api/prompts-api.ts +0 -122
  165. package/src/api/session-api.ts +0 -246
  166. package/src/api/skills-api.ts +0 -74
  167. package/src/api/workspace.ts +0 -45
  168. package/src/channel.ts +0 -226
  169. package/src/config-schema.ts +0 -60
  170. package/src/connection/workclaw-client.ts +0 -618
  171. package/src/gateway/agent-handlers.ts +0 -551
  172. package/src/gateway/config-writer.ts +0 -378
  173. package/src/gateway/cron-tasks-handler.ts +0 -230
  174. package/src/gateway/message-context.ts +0 -645
  175. package/src/gateway/message-dispatcher.ts +0 -688
  176. package/src/gateway/reconnect.ts +0 -260
  177. package/src/gateway/skills-handler.ts +0 -805
  178. package/src/gateway/skills-list-handler.ts +0 -332
  179. package/src/gateway/tools-list-handler.ts +0 -161
  180. package/src/gateway/workclaw-gateway.ts +0 -298
  181. package/src/media/upload.ts +0 -168
  182. package/src/outbound/index.ts +0 -191
  183. package/src/outbound/workclaw-sender.ts +0 -161
  184. package/src/runtime.ts +0 -520
  185. package/src/secret-contract-api.ts +0 -4
  186. package/src/send.ts +0 -1
  187. package/src/setup-api.ts +0 -3
  188. package/src/setup-core.ts +0 -25
  189. package/src/setup-surface.ts +0 -498
  190. package/src/tools/openclaw-workclaw-cron/api/index.ts +0 -326
  191. package/src/tools/openclaw-workclaw-cron/index.ts +0 -39
  192. package/src/tools/openclaw-workclaw-cron/src/add/params.ts +0 -177
  193. package/src/tools/openclaw-workclaw-cron/src/add/sync.ts +0 -188
  194. package/src/tools/openclaw-workclaw-cron/src/disable/params.ts +0 -100
  195. package/src/tools/openclaw-workclaw-cron/src/disable/sync.ts +0 -127
  196. package/src/tools/openclaw-workclaw-cron/src/enable/params.ts +0 -100
  197. package/src/tools/openclaw-workclaw-cron/src/enable/sync.ts +0 -127
  198. package/src/tools/openclaw-workclaw-cron/src/notify/sync.ts +0 -148
  199. package/src/tools/openclaw-workclaw-cron/src/remove/params.ts +0 -109
  200. package/src/tools/openclaw-workclaw-cron/src/remove/sync.ts +0 -127
  201. package/src/tools/openclaw-workclaw-cron/src/update/params.ts +0 -195
  202. package/src/tools/openclaw-workclaw-cron/src/update/sync.ts +0 -161
  203. package/src/tools/openclaw-workclaw-cron/types/index.ts +0 -55
  204. package/src/tools/openclaw-workclaw-cron/utils/index.ts +0 -141
  205. package/src/tools/openclaw-workclaw-system/index.ts +0 -17
  206. package/src/tools/openclaw-workclaw-system/src/get/index.ts +0 -77
  207. package/src/tools/openclaw-workclaw-system/src/token/index.ts +0 -93
  208. package/src/types.ts +0 -50
  209. package/src/utils/content.ts +0 -40
  210. package/tsconfig.json +0 -34
@@ -1,123 +0,0 @@
1
- import { resolveWorkclawAccountWithCache } from "../accounts.js";
2
- import { resolveAccountByUserIdAndAgentId } from "../accounts.js";
3
- import { isLocalMediaSource, resolveLocalPath, uploadLocalMedia } from "../media/upload.js";
4
- import { sendWorkclawOutboundMessage, } from "./workclaw-sender.js";
5
- import { getWorkclawLogger, setLastOutboundAt } from "../runtime.js";
6
- import { loadOpenConversationId } from "../gateway/config-writer.js";
7
- import { stat } from "node:fs/promises";
8
- /**
9
- * 根据 target (userId) 和可选的 agentId 查找匹配的账户
10
- * 如果同时提供 userId 和 agentId,走 O(1) 查找
11
- * 否则遍历所有账户,查找 userId 等于 target 的账户
12
- */
13
- function findAccountIdByTarget(cfg, target, agentId) {
14
- // O(1) 查找:userId + agentId 唯一确定一个账户
15
- if (agentId !== undefined && agentId !== null) {
16
- return resolveAccountByUserIdAndAgentId(cfg, target, String(agentId)) ?? undefined;
17
- }
18
- // 遍历:只有 userId 时,无法唯一确定账户(一个 userId 对应多个 agentId)
19
- // 需要通过 bindings 或其他方式 resolve,这里暂不支持
20
- return undefined;
21
- }
22
- async function resolveMediaUrl(mediaUrl, config) {
23
- if (!mediaUrl)
24
- return undefined;
25
- if (!isLocalMediaSource(mediaUrl))
26
- return mediaUrl;
27
- const uploadUrl = config.uploadUrl;
28
- if (!uploadUrl) {
29
- throw new Error('uploadUrl not configured for local mediaUrl');
30
- }
31
- const filePath = resolveLocalPath(mediaUrl);
32
- if (typeof config.mediaMaxMb === "number" && config.mediaMaxMb > 0) {
33
- const stats = await stat(filePath);
34
- const maxBytes = config.mediaMaxMb * 1024 * 1024;
35
- if (stats.size > maxBytes) {
36
- throw new Error(`mediaUrl exceeds limit (${config.mediaMaxMb} MB)`);
37
- }
38
- }
39
- return uploadLocalMedia({
40
- uploadUrl,
41
- filePath,
42
- uploadFieldName: config.uploadFieldName,
43
- uploadHeaders: config.uploadHeaders,
44
- uploadFormFields: config.uploadFormFields,
45
- uploadResponseUrlPath: config.uploadResponseUrlPath,
46
- requestTimeout: config.requestTimeout ?? 30000,
47
- allowInsecureTls: config.allowInsecureTls,
48
- });
49
- }
50
- export async function sendMessageWorkclaw(params) {
51
- const { cfg, to, text, mediaUrl, replyToMessageId, accountId, openConversationId, agentId, messageType, msgType, last } = params;
52
- // 如果没有指定 messageType,根据是否有 replyToMessageId 来判断
53
- // - 有 replyToMessageId: 回复消息
54
- // - 没有 replyToMessageId: 主动推送
55
- const resolvedMessageType = messageType ?? (replyToMessageId ? 'reply' : 'push');
56
- // 如果没有指定 accountId,尝试根据 target (to) 和 agentId 查找匹配的账户
57
- let resolvedAccountId = accountId;
58
- if (!resolvedAccountId) {
59
- const foundAccountId = findAccountIdByTarget(cfg, to, agentId);
60
- if (foundAccountId) {
61
- resolvedAccountId = foundAccountId;
62
- }
63
- }
64
- const account = resolveWorkclawAccountWithCache({ cfg, accountId: resolvedAccountId });
65
- if (!account.configured) {
66
- throw new Error(`Workclaw account "${account.accountId}" not configured`);
67
- }
68
- const resolvedMediaUrl = await resolveMediaUrl(mediaUrl, account.config);
69
- const cfgChannel = cfg.channels?.['openclaw-workclaw'] ?? {};
70
- const cfgAccounts = cfgChannel.accounts ?? {};
71
- const cfgAccount = cfgAccounts[resolvedAccountId] ?? {};
72
- const cfgOpenConversationId = cfgAccount?.openConversationId ?? cfgChannel?.openConversationId;
73
- // 最后尝试从 state 文件恢复(进程重启后 cfg 没有持久化的 openConversationId)
74
- const stateOpenConversationId = loadOpenConversationId(resolvedAccountId, to);
75
- const effectiveOpenConversationId = openConversationId ?? cfgOpenConversationId ?? stateOpenConversationId;
76
- getWorkclawLogger().info(`sendMessageWorkclaw - resolvedAccountId: ${resolvedAccountId}, openConversationId param: ${openConversationId}, cfgOpenConversationId: ${cfgOpenConversationId}, effectiveOpenConversationId: ${effectiveOpenConversationId}, messageType: ${resolvedMessageType}, replyToMessageId: ${replyToMessageId}`);
77
- // 使用 appKey 作为缓存键,让所有账户共享同一个 token
78
- const tokenCacheKey = account.config.appKey || account.accountId;
79
- const result = await sendWorkclawOutboundMessage({
80
- cacheKey: tokenCacheKey,
81
- to,
82
- text,
83
- msgType,
84
- mediaUrl: resolvedMediaUrl,
85
- replyToMessageId,
86
- openConversationId: effectiveOpenConversationId,
87
- agentId,
88
- config: account.config,
89
- messageType: resolvedMessageType,
90
- last,
91
- });
92
- // 更新上次出站时间
93
- if (resolvedAccountId) {
94
- setLastOutboundAt(resolvedAccountId, Date.now());
95
- }
96
- return {
97
- messageId: result.messageId,
98
- chatId: to,
99
- };
100
- }
101
- export async function getMessageWorkclaw(_params) {
102
- return null;
103
- }
104
- /**
105
- * 发送主动推送消息(定时任务使用)
106
- * 使用 pushEndpoint 配置的端口
107
- */
108
- export async function sendPushMessageWorkclaw(params) {
109
- return sendMessageWorkclaw({
110
- ...params,
111
- messageType: 'push',
112
- });
113
- }
114
- /**
115
- * 发送回复消息(用户触发)
116
- * 使用 replyEndpoint 配置的端口
117
- */
118
- export async function sendReplyMessageWorkclaw(params) {
119
- return sendMessageWorkclaw({
120
- ...params,
121
- messageType: 'reply',
122
- });
123
- }
@@ -1,95 +0,0 @@
1
- import { sendWorkclawMessage, sendWorkclawReplyMessage as sendWorkclawReplyToMessage, resolveWorkclawMessage, } from "../connection/workclaw-client.js";
2
- import { getWorkclawLogger } from "../runtime.js";
3
- export async function sendWorkclawOutboundMessage(params) {
4
- const { cacheKey, to, mediaUrl, replyToMessageId, openConversationId, agentId, config, messageType = "reply", last, } = params;
5
- let msgType = params.msgType;
6
- let content = params.text;
7
- // 优先级: 传入参数 > 配置
8
- const resolvedAgentId = agentId ?? config.agentId;
9
- if (resolvedAgentId === undefined || resolvedAgentId === null || String(resolvedAgentId).trim() === "") {
10
- throw new Error("agentId is required for sending messages. " +
11
- "Please create an account via cloud platform command or configure accounts in openclaw.json");
12
- }
13
- if (!msgType) {
14
- ({ msgType, content } = resolveWorkclawMessage(content, mediaUrl));
15
- }
16
- // msgType 转换为 "1"(文本)或其他类型码
17
- const messageTypeCode = msgType === "text" ? "1" : msgType;
18
- // 构建 WorkclawConnectionConfig(短名格式,用于 connection 模块)
19
- const workclawConfig = {
20
- baseUrl: config.baseUrl ?? "",
21
- appKey: config.appKey ?? "",
22
- appSecret: config.appSecret ?? "",
23
- localIp: config.localIp,
24
- allowInsecureTls: config.allowInsecureTls,
25
- requestTimeout: config.requestTimeout,
26
- };
27
- // 解析 userId 作为 receiveId(优先使用配置中保存的原始 userId)
28
- // 配置中的 userId 是原始值(可能为负数),to 是绝对值(OpenClaw 要求)
29
- const receiveId = config.userId ?? to;
30
- // 使用固定的 endpoint
31
- const replyEndpoint = "/open-apis/im/v1/messages/{messageId}/reply";
32
- const pushEndpoint = "/open-apis/im/v1/messages";
33
- if (messageType === "reply") {
34
- // 回复消息需要 replyToMessageId
35
- if (!replyToMessageId) {
36
- throw new Error("replyToMessageId is required for reply message");
37
- }
38
- // 优先使用传入的 openConversationId,其次使用配置中持久化的
39
- const resolvedOpenConversationId = openConversationId ?? config.openConversationId;
40
- getWorkclawLogger().info(`Sending reply message to messageId: ${replyToMessageId}, openConversationId: ${resolvedOpenConversationId}`);
41
- const msgId = await sendWorkclawReplyToMessage({
42
- cacheKey,
43
- config: workclawConfig,
44
- agentId: resolvedAgentId,
45
- receiveId,
46
- msgType: messageTypeCode,
47
- content,
48
- messageId: replyToMessageId,
49
- openConversationId: resolvedOpenConversationId,
50
- endpoint: replyEndpoint.replace("{messageId}", String(replyToMessageId)),
51
- last,
52
- });
53
- return { messageId: msgId };
54
- }
55
- else {
56
- // 主动推送消息
57
- getWorkclawLogger().info(` Sending proactive message`);
58
- // 优先使用传入的 openConversationId,其次使用配置中持久化的
59
- const resolvedOpenConversationId = openConversationId ?? config.openConversationId;
60
- getWorkclawLogger().info(` Resolved openConversationId: ${resolvedOpenConversationId}`);
61
- getWorkclawLogger().info(` Config openConversationId: ${config.openConversationId}`);
62
- const msgId = await sendWorkclawMessage({
63
- cacheKey,
64
- config: workclawConfig,
65
- agentId: resolvedAgentId,
66
- receiveId,
67
- msgType: messageTypeCode,
68
- content,
69
- openConversationId: resolvedOpenConversationId,
70
- endpoint: pushEndpoint,
71
- last,
72
- });
73
- return { messageId: msgId };
74
- }
75
- }
76
- /**
77
- * 发送主动推送消息(定时任务使用)
78
- * POST /im/v1/messages
79
- */
80
- export async function sendWorkClawPushMessage(params) {
81
- return sendWorkclawOutboundMessage({
82
- ...params,
83
- messageType: "push",
84
- });
85
- }
86
- /**
87
- * 发送回复消息(用户触发)
88
- * POST /im/v1/messages/{messageId}/reply
89
- */
90
- export async function sendWorkclawReplyMessage(params) {
91
- return sendWorkclawOutboundMessage({
92
- ...params,
93
- messageType: "reply",
94
- });
95
- }
@@ -1,374 +0,0 @@
1
- let runtime = null;
2
- // WebSocket 连接存储: key 为 appKey
3
- const wsByKey = new Map();
4
- // 额外需要的映射
5
- const accountIdsByAppKey = new Map();
6
- const appKeyByAccountId = new Map();
7
- const reconnectSchedulers = new Map();
8
- const dispatchersByAppKey = new Map();
9
- // 正在连接中的 appKey → Promise<ws>(成功后 resolve(ws),失败 resolve(null))
10
- const connectingPromises = new Map();
11
- const lastMessageAtByKey = new Map(); // 上次消息时间,key 为 accountId 或 appKey
12
- export function setWorkclawRuntime(next) {
13
- runtime = next;
14
- }
15
- export function getWorkclawRuntime() {
16
- if (!runtime) {
17
- throw new Error("Workclaw runtime not initialized");
18
- }
19
- return runtime;
20
- }
21
- let _logger = null;
22
- const _noop = () => { };
23
- function _fallbackLogger() {
24
- return {
25
- info: console.log.bind(console),
26
- warn: console.warn.bind(console),
27
- error: console.error.bind(console),
28
- debug: _noop,
29
- };
30
- }
31
- /** Store the gateway's log into runtime. Called from startAccount. */
32
- export function setWorkclawLoggerFromContext(log) {
33
- _logger = log;
34
- }
35
- /** Get the shared logger. Returns ctx.log from gateway if set, otherwise a console fallback. */
36
- export function getWorkclawLogger() {
37
- if (!_logger) {
38
- return _fallbackLogger();
39
- }
40
- // If the stored logger's info is a noop, fall back to console
41
- const l = _logger;
42
- if (l.info === _noop) {
43
- return _fallbackLogger();
44
- }
45
- return _logger;
46
- }
47
- /**
48
- * Wrap a logger with a prefix automatically prepended to every message.
49
- * All messages logged via the returned logger will have the prefix attached.
50
- */
51
- export function createLogger(prefix, log) {
52
- const fallback = _fallbackLogger();
53
- return {
54
- info: log?.info ? (msg, ...args) => log.info(`${prefix} ${msg}`, ...args) : fallback.info,
55
- warn: log?.warn ? (msg, ...args) => log.warn(`${prefix} ${msg}`, ...args) : fallback.warn,
56
- error: log?.error ? (msg, ...args) => log.error(`${prefix} ${msg}`, ...args) : fallback.error,
57
- debug: log?.debug ? (msg, ...args) => log.debug(`${prefix} ${msg}`, ...args) : fallback.debug,
58
- };
59
- }
60
- export function setWorkclawWsConnection(key, ws) {
61
- wsByKey.set(key, ws);
62
- }
63
- export function clearWorkclawWsConnection(key) {
64
- wsByKey.delete(key);
65
- }
66
- export function getWorkclawWsConnection(key) {
67
- return wsByKey.get(key);
68
- }
69
- export function setLastInboundAt(key, timestamp) {
70
- lastMessageAtByKey.set(key, timestamp);
71
- }
72
- export function getLastInboundAt(key) {
73
- return lastMessageAtByKey.get(key);
74
- }
75
- const lastOutboundAtByKey = new Map();
76
- // Cached connection config: accountId -> connection config with stable appKey/appSecret
77
- // This prevents re-resolving from cfg which may have been mutated
78
- const connectionConfigCache = new Map();
79
- export function setWorkclawConnectionConfig(accountId, config) {
80
- connectionConfigCache.set(accountId, {
81
- appKey: config.appKey,
82
- appSecret: config.appSecret,
83
- baseUrl: config.baseUrl ?? "",
84
- websocketUrl: config.websocketUrl,
85
- localIp: config.localIp,
86
- allowInsecureTls: config.allowInsecureTls,
87
- requestTimeout: config.requestTimeout,
88
- });
89
- }
90
- export function getWorkclawConnectionConfig(accountId) {
91
- return connectionConfigCache.get(accountId);
92
- }
93
- export function clearWorkclawConnectionConfig(accountId) {
94
- connectionConfigCache.delete(accountId);
95
- }
96
- export function setLastOutboundAt(key, timestamp) {
97
- lastOutboundAtByKey.set(key, timestamp);
98
- }
99
- export function getLastOutboundAt(key) {
100
- return lastOutboundAtByKey.get(key);
101
- }
102
- // 注册账号上下文
103
- export function registerAccountContext(appKey, accountId, ctx) {
104
- if (!accountIdsByAppKey.has(appKey)) {
105
- accountIdsByAppKey.set(appKey, new Set());
106
- }
107
- accountIdsByAppKey.get(appKey).add(accountId);
108
- appKeyByAccountId.set(accountId, appKey);
109
- if (!dispatchersByAppKey.has(appKey)) {
110
- dispatchersByAppKey.set(appKey, new Map());
111
- }
112
- dispatchersByAppKey.get(appKey).set(accountId, { ctx });
113
- }
114
- export function unregisterAccountContext(appKey, accountId) {
115
- accountIdsByAppKey.get(appKey)?.delete(accountId);
116
- appKeyByAccountId.delete(accountId);
117
- dispatchersByAppKey.get(appKey)?.delete(accountId);
118
- }
119
- export function getAccountIdsByAppKey(appKey) {
120
- return Array.from(accountIdsByAppKey.get(appKey) ?? []);
121
- }
122
- export function getAppKeyByAccountId(accountId) {
123
- return appKeyByAccountId.get(accountId);
124
- }
125
- export function getDispatcherByAppKeyAndAccountId(appKey, accountId) {
126
- return dispatchersByAppKey.get(appKey)?.get(accountId);
127
- }
128
- export function getAllDispatchersByAppKey(appKey) {
129
- return dispatchersByAppKey.get(appKey);
130
- }
131
- export function setReconnectScheduler(appKey, scheduler) {
132
- reconnectSchedulers.set(appKey, scheduler);
133
- }
134
- export function getReconnectScheduler(appKey) {
135
- return reconnectSchedulers.get(appKey);
136
- }
137
- export function clearReconnectScheduler(appKey) {
138
- reconnectSchedulers.delete(appKey);
139
- }
140
- // 尝试开始连接
141
- // 关键:所有并发调用者必须共享同一个 Promise,而不是各自创建新的导致重复建连
142
- export function tryStartConnecting(appKey) {
143
- // 优先查 connectingPromises,确保所有并发调用者共享同一个 Promise
144
- const existingPromise = connectingPromises.get(appKey);
145
- if (existingPromise !== undefined) {
146
- // 已有并发账号在连接,等待同一个 Promise
147
- return {
148
- isConnector: false,
149
- existingWs: wsByKey.get(appKey),
150
- connectingPromise: existingPromise,
151
- };
152
- }
153
- // Map 里没有 entry,自己是新 connector
154
- let resolvePromise;
155
- const promise = new Promise((resolve) => { resolvePromise = resolve; });
156
- promise._resolve = resolvePromise;
157
- connectingPromises.set(appKey, promise);
158
- return { isConnector: true, existingWs: undefined, connectingPromise: promise };
159
- }
160
- // 标记连接完成(成功或失败都需要调用)
161
- // ws 不传或传 undefined 表示失败
162
- export function finishConnecting(appKey, ws) {
163
- const promise = connectingPromises.get(appKey);
164
- if (promise) {
165
- promise._resolve?.(ws ?? null);
166
- connectingPromises.delete(appKey);
167
- }
168
- // ws 已建立成功时存入 wsByKey,供后续 tryStartConnecting 返回 existingWs
169
- if (ws) {
170
- wsByKey.set(appKey, ws);
171
- }
172
- }
173
- // runId -> ToolContext 映射表
174
- const runIdToToolContext = new Map();
175
- // toolCallId -> runId 映射表(用于 tool_result_persist 通过 toolCallId 找到 runId,再找到 ctx)
176
- const toolCallIdToRunId = new Map();
177
- // 获取工具上下文(通过 runId)
178
- export function getToolContext(runId) {
179
- return runIdToToolContext.get(runId);
180
- }
181
- // 设置工具上下文(仅通过 runId)
182
- export function setToolContext(runId, ctx) {
183
- runIdToToolContext.set(runId, ctx);
184
- }
185
- // 建立 toolCallId -> runId 的映射(用于 after_tool_call 中)
186
- export function setToolCallIdToRunIdMapping(toolCallId, runId) {
187
- toolCallIdToRunId.set(toolCallId, runId);
188
- }
189
- // 通过 toolCallId 获取 runId
190
- export function getRunIdByToolCallId(toolCallId) {
191
- return toolCallIdToRunId.get(toolCallId);
192
- }
193
- // 删除工具上下文
194
- export function deleteToolContext(runId) {
195
- runIdToToolContext.delete(runId);
196
- }
197
- // 清空所有工具上下文(用于清理)
198
- export function clearAllToolContexts() {
199
- runIdToToolContext.clear();
200
- toolCallIdToRunId.clear();
201
- }
202
- // 调用sub-agent进行协作
203
- export async function spawnSubAgent(params) {
204
- if (!runtime) {
205
- throw new Error("Workclaw runtime not initialized");
206
- }
207
- try {
208
- // 通过runtime调用sessions_spawn
209
- const sessionsSpawn = runtime?.sessions_spawn;
210
- if (!sessionsSpawn || typeof sessionsSpawn !== "function") {
211
- // 如果runtime没有提供sessions_spawn,返回模拟数据
212
- getWorkclawLogger().warn(`Runtime does not provide sessions_spawn, returning mock response for agent ${params.agentId}`);
213
- return {
214
- ok: true,
215
- summary: `[模拟] Agent ${params.agentId} 完成任务: ${params.task.substring(0, 50)}...`
216
- };
217
- }
218
- const result = await sessionsSpawn({
219
- agentId: params.agentId,
220
- task: params.task,
221
- label: params.label,
222
- model: params.model
223
- });
224
- return {
225
- ok: true,
226
- sessionKey: result.sessionKey,
227
- summary: result.summary
228
- };
229
- }
230
- catch (error) {
231
- getWorkclawLogger().error(`Failed to spawn sub-agent: ${error.message}`);
232
- return {
233
- ok: false,
234
- error: error.message
235
- };
236
- }
237
- }
238
- export const __openclaw_workclaw_runtime_emitted = true;
239
- /**
240
- * 获取工具执行开始的提示(用于 onToolStart)
241
- */
242
- export function getToolStartHint(toolName) {
243
- if (!toolName)
244
- return "🔄 正在执行工具...";
245
- const normalizedToolName = toolName.toLowerCase().trim();
246
- const toolMap = {
247
- // 📁 文件操作
248
- read: { emoji: "📖", title: "读取文件", hint: "正在读取文件", aliases: ["file_read", "read_file"] },
249
- write: { emoji: "✍️", title: "写入文件", hint: "正在写入文件", aliases: ["file_write", "write_file"] },
250
- edit: { emoji: "✏️", title: "编辑文件", hint: "正在编辑文件", aliases: ["file_edit", "modify"] },
251
- // 💻 系统命令
252
- exec: { emoji: "⚡", title: "执行命令", hint: "正在执行命令", aliases: ["bash", "shell", "cmd", "command"] },
253
- process: { emoji: "🔄", title: "进程管理", hint: "正在处理进程", aliases: ["process_manager"] },
254
- // 🌐 网络/浏览器
255
- web_fetch: { emoji: "🕸️", title: "获取网页", hint: "正在获取网页内容", aliases: ["fetch", "http_get", "http_request", "curl"] },
256
- web_search: { emoji: "🔍", title: "搜索网页", hint: "正在搜索网页", aliases: ["search", "google_search", "brave_search"] },
257
- browser: { emoji: "🌐", title: "浏览器控制", hint: "正在控制浏览器", aliases: ["browser_control", "playwright"] },
258
- // 💬 消息/通信
259
- message: { emoji: "💌", title: "发送消息", hint: "正在发送消息", aliases: ["send_message", "mcp_deliver", "deliver", "notify"] },
260
- tts: { emoji: "🔊", title: "文字转语音", hint: "正在转换语音", aliases: ["text_to_speech", "speak"] },
261
- // 🎭 会话管理
262
- sessions_spawn: { emoji: "🧬", title: "创建子代理", hint: "正在创建子代理", aliases: ["spawn", "create_agent", "new_session"] },
263
- sessions_send: { emoji: "📤", title: "跨会话发消息", hint: "正在发送跨会话消息", aliases: ["send_to_session", "forward"] },
264
- sessions_list: { emoji: "📋", title: "列出会话", hint: "正在列出会话", aliases: ["list_sessions"] },
265
- sessions_history: { emoji: "📜", title: "获取历史", hint: "正在获取历史消息", aliases: ["get_history", "history"] },
266
- sessions_yield: { emoji: "⏸️", title: "暂停让出", hint: "正在暂停等待", aliases: ["yield", "pause"] },
267
- subagents: { emoji: "🐛", title: "子代理管理", hint: "正在管理子代理", aliases: ["subagent_manager"] },
268
- agents_list: { emoji: "🤖", title: "列出代理", hint: "正在列出可用代理", aliases: ["list_agents"] },
269
- // 🧠 记忆/知识
270
- memory_search: { emoji: "🔎", title: "搜索记忆", hint: "正在搜索记忆", aliases: ["search_memory", "recall"] },
271
- memory_get: { emoji: "📝", title: "读取记忆", hint: "正在读取记忆片段", aliases: ["get_memory", "read_memory"] },
272
- // ⏰ 定时任务
273
- "openclaw-workclaw-cron-add-params": { emoji: "⏱️", title: "创建定时任务", hint: "正在创建定时任务", aliases: ["cron_add", "add_timer"] },
274
- "openclaw-workclaw-cron-update-params": { emoji: "🔧", title: "修改定时任务", hint: "正在修改定时任务", aliases: ["cron_update", "update_timer"] },
275
- "openclaw-workclaw-cron-remove-params": { emoji: "🗑️", title: "删除定时任务", hint: "正在删除定时任务", aliases: ["cron_remove", "remove_timer"] },
276
- "openclaw-workclaw-cron-notify-sync": { emoji: "🔔", title: "定时触发通知", hint: "定时任务已触发", aliases: ["cron_trigger", "notify"] },
277
- "openclaw-workclaw-cron-add-sync": { emoji: "✅", title: "同步定时任务", hint: "正在同步到后端", aliases: [] },
278
- "openclaw-workclaw-cron-update-sync": { emoji: "✅", title: "同步定时更新", hint: "正在同步更新", aliases: [] },
279
- "openclaw-workclaw-cron-remove-sync": { emoji: "✅", title: "同步定时删除", hint: "正在同步删除", aliases: [] },
280
- "openclaw-workclaw-cron-disable-params": { emoji: "⏸️", title: "禁用定时任务", hint: "正在禁用定时任务", aliases: ["cron_disable"] },
281
- "openclaw-workclaw-cron-enable-params": { emoji: "▶️", title: "启用定时任务", hint: "正在启用定时任务", aliases: ["cron_enable"] },
282
- // 📊 其他
283
- session_status: { emoji: "📊", title: "会话状态", hint: "正在获取会话状态", aliases: ["status", "get_status"] },
284
- canvas: { emoji: "🎨", title: "画布控制", hint: "正在控制画布", aliases: ["canvas_control", "draw"] },
285
- };
286
- // 精确匹配
287
- if (toolMap[normalizedToolName]) {
288
- const tool = toolMap[normalizedToolName];
289
- return `${tool.emoji} ${tool.hint}...`;
290
- }
291
- // 别名匹配
292
- for (const [key, tool] of Object.entries(toolMap)) {
293
- if (tool.aliases.includes(normalizedToolName)) {
294
- return `${tool.emoji} ${tool.hint}...`;
295
- }
296
- }
297
- // 默认
298
- return `🔄 正在执行 ${toolName}...`;
299
- }
300
- /**
301
- * 获取工具执行完成的提示(用于 after_tool_call)
302
- */
303
- export function getToolResultHint(toolName) {
304
- if (!toolName)
305
- return "✅ 工具执行完成";
306
- const normalizedToolName = toolName.toLowerCase().trim();
307
- const toolMap = {
308
- // 📁 文件操作
309
- read: { emoji: "📖", title: "读取文件", hint: "已读取文件", aliases: ["file_read", "read_file"] },
310
- write: { emoji: "✍️", title: "写入文件", hint: "已写入文件", aliases: ["file_write", "write_file"] },
311
- edit: { emoji: "✏️", title: "编辑文件", hint: "已编辑文件", aliases: ["file_edit", "modify"] },
312
- // 💻 系统命令
313
- exec: { emoji: "⚡", title: "执行命令", hint: "已执行命令", aliases: ["bash", "shell", "cmd", "command"] },
314
- process: { emoji: "🔄", title: "进程管理", hint: "已处理进程", aliases: ["process_manager"] },
315
- // 🌐 网络/浏览器
316
- web_fetch: { emoji: "🕸️", title: "获取网页", hint: "已获取网页", aliases: ["fetch", "http_get", "http_request", "curl"] },
317
- web_search: { emoji: "🔍", title: "搜索网页", hint: "已搜索网页", aliases: ["search", "google_search", "brave_search"] },
318
- browser: { emoji: "🌐", title: "浏览器控制", hint: "已控制浏览器", aliases: ["browser_control", "playwright"] },
319
- // 💬 消息/通信
320
- message: { emoji: "💌", title: "发送消息", hint: "已发送消息", aliases: ["send_message", "mcp_deliver", "deliver", "notify"] },
321
- tts: { emoji: "🔊", title: "文字转语音", hint: "已转换语音", aliases: ["text_to_speech", "speak"] },
322
- // 🎭 会话管理
323
- sessions_spawn: { emoji: "🧬", title: "创建子代理", hint: "已创建子代理", aliases: ["spawn", "create_agent", "new_session"] },
324
- sessions_send: { emoji: "📤", title: "跨会话发消息", hint: "已发送跨会话消息", aliases: ["send_to_session", "forward"] },
325
- sessions_list: { emoji: "📋", title: "列出会话", hint: "已列出会话", aliases: ["list_sessions"] },
326
- sessions_history: { emoji: "📜", title: "获取历史", hint: "已获取历史消息", aliases: ["get_history", "history"] },
327
- sessions_yield: { emoji: "⏸️", title: "暂停让出", hint: "已暂停等待", aliases: ["yield", "pause"] },
328
- subagents: { emoji: "🐛", title: "子代理管理", hint: "已管理子代理", aliases: ["subagent_manager"] },
329
- agents_list: { emoji: "🤖", title: "列出代理", hint: "已列出代理", aliases: ["list_agents"] },
330
- // 🧠 记忆/知识
331
- memory_search: { emoji: "🔎", title: "搜索记忆", hint: "已搜索记忆", aliases: ["search_memory", "recall"] },
332
- memory_get: { emoji: "📝", title: "读取记忆", hint: "已读取记忆", aliases: ["get_memory", "read_memory"] },
333
- // ⏰ 定时任务
334
- "openclaw-workclaw-cron-add-params": { emoji: "⏱️", title: "创建定时任务", hint: "已创建定时任务", aliases: ["cron_add", "add_timer"] },
335
- "openclaw-workclaw-cron-update-params": { emoji: "🔧", title: "修改定时任务", hint: "已修改定时任务", aliases: ["cron_update", "update_timer"] },
336
- "openclaw-workclaw-cron-remove-params": { emoji: "🗑️", title: "删除定时任务", hint: "已删除定时任务", aliases: ["cron_remove", "remove_timer"] },
337
- "openclaw-workclaw-cron-notify-sync": { emoji: "🔔", title: "定时触发通知", hint: "定时任务已触发", aliases: ["cron_trigger", "notify"] },
338
- "openclaw-workclaw-cron-add-sync": { emoji: "✅", title: "同步定时任务", hint: "已同步定时任务", aliases: [] },
339
- "openclaw-workclaw-cron-update-sync": { emoji: "✅", title: "同步定时更新", hint: "已同步更新", aliases: [] },
340
- "openclaw-workclaw-cron-remove-sync": { emoji: "✅", title: "同步定时删除", hint: "已同步删除", aliases: [] },
341
- "openclaw-workclaw-cron-disable-params": { emoji: "⏸️", title: "禁用定时任务", hint: "已禁用定时任务", aliases: ["cron_disable"] },
342
- "openclaw-workclaw-cron-enable-params": { emoji: "▶️", title: "启用定时任务", hint: "已启用定时任务", aliases: ["cron_enable"] },
343
- // 📊 其他
344
- session_status: { emoji: "📊", title: "会话状态", hint: "已获取会话状态", aliases: ["status", "get_status"] },
345
- canvas: { emoji: "🎨", title: "画布控制", hint: "已控制画布", aliases: ["canvas_control", "draw"] },
346
- };
347
- // 精确匹配
348
- if (toolMap[normalizedToolName]) {
349
- const tool = toolMap[normalizedToolName];
350
- return `${tool.emoji} ${tool.hint}`;
351
- }
352
- // 别名匹配
353
- for (const [key, tool] of Object.entries(toolMap)) {
354
- if (tool.aliases.includes(normalizedToolName)) {
355
- return `${tool.emoji} ${tool.hint}`;
356
- }
357
- }
358
- // 默认
359
- return `✅ 已执行 ${toolName}`;
360
- }
361
- /**
362
- * 获取 Workclaw 渠道密钥
363
- * 这是 setup 向导必需的接口
364
- */
365
- export async function getWorkclawSecrets(params) {
366
- const secrets = {};
367
- if (params.appKey) {
368
- secrets.appKey = params.appKey;
369
- }
370
- if (params.appSecret) {
371
- secrets.appSecret = params.appSecret;
372
- }
373
- return secrets;
374
- }
@@ -1,4 +0,0 @@
1
- export const channelSecrets = {
2
- specifier: "./runtime.js",
3
- exportName: "getWorkclawSecrets",
4
- };
package/dist/src/send.js DELETED
@@ -1 +0,0 @@
1
- export { sendMessageWorkclaw, getMessageWorkclaw } from "./outbound/index.js";
@@ -1,3 +0,0 @@
1
- export { workclawPlugin } from "./channel.js";
2
- export * from "./setup-core.js";
3
- export * from "./setup-surface.js";
@@ -1,13 +0,0 @@
1
- import { DEFAULT_ACCOUNT_ID, } from "openclaw/plugin-sdk/setup";
2
- import { resolveDefaultWorkclawAccountId } from "./accounts.js";
3
- import { patchWorkclawConfig } from "./setup-surface.js";
4
- export function setWorkclawNamedAccountEnabled(cfg, accountId, enabled) {
5
- return patchWorkclawConfig(cfg, accountId, { enabled });
6
- }
7
- export const workclawSetupAdapter = {
8
- resolveAccountId: ({ cfg, accountId }) => accountId?.trim() || resolveDefaultWorkclawAccountId(cfg),
9
- applyAccountConfig: ({ cfg, accountId }) => {
10
- const isDefault = !accountId || accountId === DEFAULT_ACCOUNT_ID;
11
- return patchWorkclawConfig(cfg, isDefault ? DEFAULT_ACCOUNT_ID : accountId, { enabled: true });
12
- },
13
- };