@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,351 +0,0 @@
1
- import { Agent, fetch as undiciFetch } from "undici";
2
- import { getWorkclawLogger } from "../runtime.js";
3
- const tokenCache = new Map();
4
- export function clearWorkclawTokenCache(cacheKey) {
5
- tokenCache.delete(cacheKey);
6
- }
7
- export function normalizeBaseUrl(value) {
8
- const raw = (value || "https://workbrain.cn/backend-api").trim();
9
- return raw.endsWith("/") ? raw.slice(0, -1) : raw;
10
- }
11
- function createWsEndpoint(baseUrl) {
12
- const wsBase = baseUrl.replace(/^https?:\/\//i, (match) => match.toLowerCase() === "https://" ? "wss://" : "ws://");
13
- return `${wsBase}/v1/ws/connect`;
14
- }
15
- export async function doFetchJson(url, init, allowInsecureTls, requestTimeout) {
16
- const dispatcher = allowInsecureTls
17
- ? new Agent({ connect: { rejectUnauthorized: false } })
18
- : undefined;
19
- const fetcher = allowInsecureTls ? undiciFetch : globalThis.fetch;
20
- const controller = new AbortController();
21
- const timeoutMs = typeof requestTimeout === "number" && requestTimeout > 0 ? requestTimeout : 30000;
22
- const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
23
- let response;
24
- let text = "";
25
- try {
26
- response = await fetcher(url, {
27
- ...init,
28
- signal: controller.signal,
29
- ...(dispatcher ? { dispatcher } : {}),
30
- });
31
- text = await response.text().catch(() => "");
32
- }
33
- finally {
34
- clearTimeout(timeoutId);
35
- }
36
- let data = {};
37
- if (text) {
38
- try {
39
- data = JSON.parse(text);
40
- }
41
- catch {
42
- data = {};
43
- }
44
- }
45
- if (!response.ok) {
46
- const message = data?.message || text || `HTTP ${response.status}`;
47
- throw new Error(message);
48
- }
49
- return data;
50
- }
51
- async function fetchAccessTokenFromServer(requestUrl, config) {
52
- getWorkclawLogger().info(`Requesting access token url=${requestUrl} appKeyPrefix=${String(config.appKey)}... appSecretLen=${String(config.appSecret)}`);
53
- const data = await doFetchJson(requestUrl, {
54
- method: "POST",
55
- headers: { "Content-Type": "application/json" },
56
- body: JSON.stringify({
57
- app_key: config.appKey,
58
- app_secret: config.appSecret,
59
- }),
60
- }, config.allowInsecureTls, config.requestTimeout);
61
- getWorkclawLogger().info(`Access token response code=${String(data?.code ?? "")} errCode=${String(data?.errCode ?? "")} errMsg=${String(data?.errMsg ?? data?.message ?? "")}`);
62
- if (data?.code !== 200 || !data?.data?.accessToken) {
63
- throw new Error(String(data?.message || data?.errMsg || "Failed to acquire access token"));
64
- }
65
- const token = String(data.data.accessToken);
66
- const expiresIn = Number(data.data.expiresIn || 0);
67
- const expiresAt = Date.now() + (expiresIn > 0 ? expiresIn * 1000 : 0);
68
- getWorkclawLogger().info(`Access token acquired tokenLen=${token.length} expiresInSec=${expiresIn} expiresAt=${new Date(expiresAt).toISOString()}`);
69
- return { token, expiresAt };
70
- }
71
- export async function getWorkclawAccessToken(cacheKey, config) {
72
- const startedAt = Date.now();
73
- const baseUrl = normalizeBaseUrl(config.baseUrl);
74
- const requestUrl = `${baseUrl}/authen/v1/access_token/internal`;
75
- const timeoutMs = typeof config.requestTimeout === "number" && config.requestTimeout > 0
76
- ? config.requestTimeout
77
- : 30000;
78
- getWorkclawLogger().info(`getWorkclawAccessToken start cacheKey=${cacheKey} requestUrl=${requestUrl} baseUrl=${baseUrl} allowInsecureTls=${Boolean(config.allowInsecureTls)} timeoutMs=${timeoutMs}`);
79
- try {
80
- const cached = tokenCache.get(cacheKey);
81
- if (cached && cached.token) {
82
- const now = Date.now();
83
- const refreshAt = cached.expiresAt - 5 * 60 * 1000;
84
- const remainingMs = cached.expiresAt - now;
85
- getWorkclawLogger().info(`Token cache found cacheKey=${cacheKey} expiresAt=${new Date(cached.expiresAt).toISOString()} remainingMs=${remainingMs}`);
86
- if (now < refreshAt) {
87
- getWorkclawLogger().info(`Using cached token cacheKey=${cacheKey} elapsedMs=${now - startedAt}`);
88
- return cached.token;
89
- }
90
- getWorkclawLogger().info(`Cached token near expiry/expired, refreshing cacheKey=${cacheKey} elapsedMs=${now - startedAt}`);
91
- }
92
- if (!config.appKey || !config.appSecret) {
93
- getWorkclawLogger().error(`Missing appKey/appSecret cacheKey=${cacheKey} appKey=${String(config.appKey || "") ? "present" : "missing"} appSecret=${String(config.appSecret || "") ? "present" : "missing"}`);
94
- throw new Error("Missing appKey/appSecret");
95
- }
96
- const result = await fetchAccessTokenFromServer(requestUrl, config);
97
- tokenCache.set(cacheKey, { token: result.token, expiresAt: result.expiresAt });
98
- getWorkclawLogger().info(`getWorkclawAccessToken success cacheKey=${cacheKey} elapsedMs=${Date.now() - startedAt}`);
99
- return result.token;
100
- }
101
- catch (error) {
102
- getWorkclawLogger().error(`getWorkclawAccessToken failed cacheKey=${cacheKey} requestUrl=${requestUrl} baseUrl=${baseUrl} elapsedMs=${Date.now() - startedAt} error=${String(error)}`);
103
- throw error;
104
- }
105
- }
106
- export async function openWorkclawConnection(cacheKey, config) {
107
- const startedAt = Date.now();
108
- const baseUrl = normalizeBaseUrl(config.baseUrl);
109
- const requestUrl = `${baseUrl}/open-apis/v1/connections/open`;
110
- const timeoutMs = typeof config.requestTimeout === "number" && config.requestTimeout > 0
111
- ? config.requestTimeout
112
- : 30000;
113
- const appKeyRaw = String(config.appKey ?? "");
114
- const appSecretRaw = String(config.appSecret ?? "");
115
- getWorkclawLogger().info(`openWorkclawConnection start cacheKey=${cacheKey} baseUrl=${baseUrl} requestUrl=${requestUrl} allowInsecureTls=${Boolean(config.allowInsecureTls)} timeoutMs=${timeoutMs} localIp=${String(config.localIp ?? "")} websocketUrl=${String(config.websocketUrl ?? "")} appKeyPrefix=${appKeyRaw ? `${appKeyRaw.slice(0, 8)}...` : "missing"} appSecretLen=${appSecretRaw.length}`);
116
- const tokenStartedAt = Date.now();
117
- const token = await getWorkclawAccessToken(cacheKey, config);
118
- getWorkclawLogger().info(`openWorkclawConnection got access token cacheKey=${cacheKey} tokenLen=${token.length} elapsedMs=${Date.now() - tokenStartedAt}`);
119
- const payload = {
120
- appKey: config.appKey,
121
- appSecret: config.appSecret,
122
- subscriptions: [
123
- { type: "CALLBACK", topic: "AGENT_MESSAGE" },
124
- { type: "EVENT", topic: "*" },
125
- { type: "SYSTEM", topic: "*" },
126
- ],
127
- ...(config.localIp ? { localIp: config.localIp } : {}),
128
- };
129
- getWorkclawLogger().info(`Opening connection url=${requestUrl} cacheKey=${cacheKey} subscriptions=${payload.subscriptions.length}`);
130
- const data = await doFetchJson(requestUrl, {
131
- method: "POST",
132
- headers: {
133
- "Content-Type": "application/json",
134
- "Authorization": `Bearer ${token}`,
135
- },
136
- body: JSON.stringify(payload),
137
- }, config.allowInsecureTls, config.requestTimeout);
138
- getWorkclawLogger().info(`Open connection response code=${String(data?.code ?? "")} errCode=${String(data?.errCode ?? "")} errMsg=${String(data?.errMsg ?? data?.message ?? "")} hasTicket=${Boolean(data?.data?.ticket)} hasEndpoint=${Boolean(data?.data?.endpoint)}`);
139
- if (data?.code !== 200 || !data?.data?.ticket) {
140
- throw new Error(String(data?.message || data?.errMsg || "Failed to open connection"));
141
- }
142
- const ticket = String(data.data.ticket);
143
- let endpointSource = "derived";
144
- // 优先使用配置中的 websocketUrl,其次使用 API 返回的 endpoint,最后使用默认构建的 endpoint
145
- const endpoint = config.websocketUrl
146
- ? ((endpointSource = "config"), String(config.websocketUrl))
147
- : data?.data?.endpoint
148
- ? ((endpointSource = "api"), String(data.data.endpoint))
149
- : ((endpointSource = "derived"), createWsEndpoint(baseUrl));
150
- getWorkclawLogger().info(`openWorkclawConnection success cacheKey=${cacheKey} endpoint=${endpoint} endpointSource=${endpointSource} ticketLen=${ticket.length} elapsedMs=${Date.now() - startedAt}`);
151
- return { endpoint, ticket };
152
- }
153
- /**
154
- * 获取智能体列表
155
- * POST /open-apis/instance/list
156
- */
157
- export async function getAgentInstances(cacheKey, config) {
158
- const baseUrl = normalizeBaseUrl(config.baseUrl);
159
- const token = await getWorkclawAccessToken(cacheKey, config);
160
- const payload = {
161
- appKey: config.appKey,
162
- };
163
- getWorkclawLogger().info(`Getting agent instances from ${baseUrl}/open-apis/instance/list`);
164
- getWorkclawLogger().info(`Payload:`, JSON.stringify(payload, null, 2));
165
- const data = await doFetchJson(`${baseUrl}/open-apis/instance/list`, {
166
- method: "POST",
167
- headers: {
168
- "Content-Type": "application/json",
169
- "Authorization": `Bearer ${token}`,
170
- },
171
- body: JSON.stringify(payload),
172
- }, config.allowInsecureTls, config.requestTimeout);
173
- getWorkclawLogger().info(`Agent instances response:`, JSON.stringify(data, null, 2));
174
- if (data?.code !== 200 || !Array.isArray(data?.data)) {
175
- throw new Error(data?.message || "Failed to get agent instances");
176
- }
177
- return data.data;
178
- }
179
- /**
180
- * 发送主动消息(非回复)
181
- * POST /im/v1/messages
182
- */
183
- export async function sendWorkclawMessage(params) {
184
- const baseUrl = normalizeBaseUrl(params.config.baseUrl);
185
- const token = await getWorkclawAccessToken(params.cacheKey, params.config);
186
- // 主动消息参数
187
- const payload = {
188
- agentId: params.agentId,
189
- receiveId: params.receiveId,
190
- msgType: params.msgType,
191
- content: params.content,
192
- };
193
- // 添加 last 字段(仅当为 true 时)
194
- if (params.last === true) {
195
- payload.is_last = true;
196
- }
197
- // openConversationId 非必填,API 字段名为 conversationId
198
- if (params.openConversationId) {
199
- payload.conversationId = params.openConversationId;
200
- }
201
- const endpoint = params.endpoint || "/open-apis/im/v1/messages";
202
- getWorkclawLogger().info(`Sending proactive message to ${baseUrl}${endpoint}`);
203
- getWorkclawLogger().info(`Payload:`, JSON.stringify(payload, null, 2));
204
- const data = await doFetchJson(`${baseUrl}${endpoint}`, {
205
- method: "POST",
206
- headers: {
207
- "Content-Type": "application/json",
208
- "Authorization": `Bearer ${token}`,
209
- },
210
- body: JSON.stringify(payload),
211
- }, params.config.allowInsecureTls, params.config.requestTimeout);
212
- getWorkclawLogger().info(`Send message response:`, JSON.stringify(data, null, 2));
213
- if (data?.code !== 200 || !data?.data?.msgId) {
214
- throw new Error(data?.message || "Failed to send message");
215
- }
216
- return String(data.data.msgId);
217
- }
218
- /**
219
- * 发送回复消息
220
- * POST /im/v1/messages/{messageId}/reply
221
- */
222
- export async function sendWorkclawReplyMessage(params) {
223
- const baseUrl = normalizeBaseUrl(params.config.baseUrl);
224
- const token = await getWorkclawAccessToken(params.cacheKey, params.config);
225
- // 回复消息参数
226
- // 注意:虽然 API 文档说不需要 openConversationId,但为了保险起见还是会传递
227
- const payload = {
228
- agentId: params.agentId,
229
- receiveId: params.receiveId,
230
- msgType: params.msgType,
231
- content: params.content,
232
- };
233
- getWorkclawLogger().info(`Reply payload is last : ${params.last} `);
234
- // 添加 last 字段(仅当为 true 时)
235
- if (params.last === true) {
236
- payload.is_last = true;
237
- }
238
- // 如果提供了 openConversationId,也添加到 payload 中(以防 API 需要)
239
- if (params.openConversationId) {
240
- payload.conversationId = params.openConversationId;
241
- getWorkclawLogger().info(`Reply payload includes conversationId: ${params.openConversationId}`);
242
- }
243
- else {
244
- getWorkclawLogger().info(`Reply payload does NOT include conversationId (openConversationId is empty)`);
245
- }
246
- const endpoint = params.endpoint || `/open-apis/im/v1/messages/${params.messageId}/reply`;
247
- getWorkclawLogger().info(`Sending reply message to ${baseUrl}${endpoint}`);
248
- getWorkclawLogger().info(`Reply Payload:`, JSON.stringify(payload, null, 2));
249
- const data = await doFetchJson(`${baseUrl}${endpoint}`, {
250
- method: "POST",
251
- headers: {
252
- "Content-Type": "application/json",
253
- "Authorization": `Bearer ${token}`,
254
- },
255
- body: JSON.stringify(payload),
256
- }, params.config.allowInsecureTls, params.config.requestTimeout);
257
- getWorkclawLogger().info(`Send reply response:`, JSON.stringify(data, null, 2));
258
- if (data?.code !== 200 || !data?.data?.msgId) {
259
- throw new Error(data?.message || "Failed to send reply");
260
- }
261
- return String(data.data.msgId);
262
- }
263
- export function resolveWorkclawMessage(text, mediaUrl) {
264
- if (!mediaUrl || text.trim()) {
265
- return { msgType: "text", content: text };
266
- }
267
- const url = mediaUrl.trim();
268
- const lower = url.toLowerCase();
269
- const isAudio = lower.endsWith(".mp3") ||
270
- lower.endsWith(".wav") ||
271
- lower.endsWith(".aac") ||
272
- lower.endsWith(".m4a") ||
273
- lower.endsWith(".ogg");
274
- const payload = JSON.stringify({ url });
275
- return { msgType: isAudio ? "audio" : "image", content: payload };
276
- }
277
- /**
278
- * 同步 openclaw 创建的定时任务至后端
279
- * POST /cron/job/add
280
- */
281
- export async function syncWorkclawCronJobToBackend(cacheKey, config, payload) {
282
- const baseUrl = normalizeBaseUrl(config.baseUrl);
283
- const token = await getWorkclawAccessToken(cacheKey, config);
284
- // 尝试添加 /open-apis 前缀,以匹配其他 API 的模式 (如 getAgentInstances)
285
- const requestUrl = `${baseUrl}/open-apis/cron/job/add`;
286
- getWorkclawLogger().info(`Syncing cron job to ${requestUrl}`);
287
- getWorkclawLogger().info(`Payload:`, JSON.stringify(payload, null, 2));
288
- const data = await doFetchJson(requestUrl, {
289
- method: "POST",
290
- headers: {
291
- "Content-Type": "application/json",
292
- "Authorization": `Bearer ${token}`,
293
- },
294
- body: JSON.stringify(payload),
295
- }, config.allowInsecureTls, config.requestTimeout);
296
- getWorkclawLogger().info(`Sync cron job response:`, JSON.stringify(data, null, 2));
297
- if (data?.code !== 200 && data?.code !== 0) {
298
- throw new Error(data?.message || data?.msg || "Failed to sync cron job");
299
- }
300
- return data;
301
- }
302
- /**
303
- * 获取AppKey所对应的模型配置
304
- * POST /open-apis/instance/apiKey/{appKey}
305
- */
306
- export async function getWorkclawModelConfigByAppKey(cacheKey, config) {
307
- const baseUrl = normalizeBaseUrl(config.baseUrl);
308
- const token = await getWorkclawAccessToken(cacheKey, config);
309
- const appKey = config.appKey;
310
- if (!appKey) {
311
- throw new Error("Missing appKey for getModelKeyByAppKey");
312
- }
313
- const requestUrl = `${baseUrl}/open-apis/instance/apiKey/${appKey}`;
314
- getWorkclawLogger().info(`Getting model key from ${requestUrl}`);
315
- const data = await doFetchJson(requestUrl, {
316
- method: "GET",
317
- headers: {
318
- "Content-Type": "x-www-form-urlencoded",
319
- "Authorization": `Bearer ${token}`,
320
- }
321
- }, config.allowInsecureTls, config.requestTimeout);
322
- getWorkclawLogger().info(`Model key response:`, JSON.stringify(data, null, 2));
323
- if (data?.code !== 200 || !data?.data) {
324
- throw new Error(data?.message || data?.msg || "Failed to get model key");
325
- }
326
- return data.data;
327
- }
328
- /**
329
- * 此时定时任务触发消息至后端
330
- * POST /open-apis/cron/job/message
331
- */
332
- export async function sendWorkclawCronJobMessageToBackend(cacheKey, config, payload) {
333
- const baseUrl = normalizeBaseUrl(config.baseUrl);
334
- const token = await getWorkclawAccessToken(cacheKey, config);
335
- const requestUrl = `${baseUrl}/open-apis/cron/job/message`;
336
- getWorkclawLogger().info(`Sending cron job message to ${requestUrl}`);
337
- getWorkclawLogger().info(`Payload:`, JSON.stringify(payload, null, 2));
338
- const data = await doFetchJson(requestUrl, {
339
- method: "POST",
340
- headers: {
341
- "Content-Type": "application/json",
342
- "Authorization": `Bearer ${token}`,
343
- },
344
- body: JSON.stringify(payload),
345
- }, config.allowInsecureTls, config.requestTimeout);
346
- getWorkclawLogger().info(`Send cron job message response:`, JSON.stringify(data, null, 2));
347
- if (data?.code !== 200 && data?.code !== 0) {
348
- throw new Error(data?.message || data?.msg || "Failed to send cron job message");
349
- }
350
- return data;
351
- }