@yanhaidao/wecom 2.4.160 → 2.5.110

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 (313) hide show
  1. package/dist/index.js +68 -0
  2. package/dist/src/accounts.js +20 -0
  3. package/dist/src/agent/handler.js +895 -0
  4. package/dist/src/agent/index.js +5 -0
  5. package/dist/src/app/account-runtime.js +216 -0
  6. package/dist/src/app/bootstrap.js +19 -0
  7. package/dist/src/app/index.js +118 -0
  8. package/dist/src/capability/agent/delivery-service.js +63 -0
  9. package/dist/src/capability/agent/fallback-policy.js +6 -0
  10. package/dist/src/capability/agent/ingress-service.js +33 -0
  11. package/dist/src/capability/agent/upstream-delivery-service.js +71 -0
  12. package/dist/src/capability/bot/dispatch-config.js +45 -0
  13. package/dist/src/capability/bot/fallback-delivery.js +147 -0
  14. package/dist/src/capability/bot/local-path-delivery.js +178 -0
  15. package/dist/src/capability/bot/sandbox-media.js +138 -0
  16. package/dist/src/capability/bot/service.js +49 -0
  17. package/dist/src/capability/bot/stream-delivery.js +321 -0
  18. package/dist/src/capability/bot/stream-finalizer.js +81 -0
  19. package/dist/src/capability/bot/stream-orchestrator.js +318 -0
  20. package/dist/src/capability/bot/types.js +1 -0
  21. package/{src/capability/calendar/client.ts → dist/src/capability/calendar/client.js} +118 -241
  22. package/{src/capability/calendar/schema.ts → dist/src/capability/calendar/schema.js} +0 -38
  23. package/dist/src/capability/calendar/tool.js +365 -0
  24. package/dist/src/capability/calendar/types.js +12 -0
  25. package/{src/capability/doc/client.ts → dist/src/capability/doc/client.js} +370 -605
  26. package/{src/capability/doc/schema.ts → dist/src/capability/doc/schema.js} +345 -394
  27. package/dist/src/capability/doc/tool.js +1556 -0
  28. package/dist/src/capability/doc/types.js +113 -0
  29. package/dist/src/capability/mcp/index.js +3 -0
  30. package/dist/src/capability/mcp/schema.js +102 -0
  31. package/dist/src/capability/mcp/tool.js +146 -0
  32. package/dist/src/capability/mcp/transport.js +293 -0
  33. package/dist/src/channel.js +224 -0
  34. package/dist/src/config/accounts.js +236 -0
  35. package/dist/src/config/derived-paths.js +31 -0
  36. package/dist/src/config/index.js +7 -0
  37. package/dist/src/config/media.js +110 -0
  38. package/dist/src/config/network.js +32 -0
  39. package/dist/src/config/routing.js +20 -0
  40. package/dist/src/config/runtime-config.js +25 -0
  41. package/dist/src/config/schema.js +4 -0
  42. package/{src/config-schema.ts → dist/src/config-schema.js} +1 -1
  43. package/dist/src/context-store.js +219 -0
  44. package/{src/crypto/aes.ts → dist/src/crypto/aes.js} +11 -28
  45. package/dist/src/crypto/index.js +9 -0
  46. package/{src/crypto/signature.ts → dist/src/crypto/signature.js} +3 -18
  47. package/{src/crypto/xml.ts → dist/src/crypto/xml.js} +3 -11
  48. package/dist/src/crypto.js +145 -0
  49. package/dist/src/domain/models.js +1 -0
  50. package/dist/src/domain/policies.js +32 -0
  51. package/{src/dynamic-agent.ts → dist/src/dynamic-agent.js} +36 -73
  52. package/dist/src/gateway-monitor.js +139 -0
  53. package/dist/src/http.js +114 -0
  54. package/{src/media.ts → dist/src/media.js} +21 -40
  55. package/dist/src/monitor/limits.js +7 -0
  56. package/dist/src/monitor/state.js +28 -0
  57. package/dist/src/monitor.js +84 -0
  58. package/dist/src/observability/audit-log.js +30 -0
  59. package/dist/src/observability/legacy-operational-event-store.js +22 -0
  60. package/dist/src/observability/raw-envelope-log.js +24 -0
  61. package/dist/src/observability/status-registry.js +9 -0
  62. package/dist/src/observability/transport-session-view.js +14 -0
  63. package/dist/src/onboarding.js +546 -0
  64. package/dist/src/outbound.js +557 -0
  65. package/dist/src/runtime/dispatcher.js +57 -0
  66. package/{src/runtime/index.ts → dist/src/runtime/index.js} +0 -1
  67. package/dist/src/runtime/outbound-intent.js +1 -0
  68. package/dist/src/runtime/reply-orchestrator.js +38 -0
  69. package/dist/src/runtime/routing-bridge.js +26 -0
  70. package/dist/src/runtime/session-manager.js +112 -0
  71. package/dist/src/runtime/source-registry.js +174 -0
  72. package/dist/src/runtime.js +1 -0
  73. package/dist/src/shared/command-auth.js +57 -0
  74. package/{src/shared/index.ts → dist/src/shared/index.js} +0 -1
  75. package/dist/src/shared/media-asset.js +65 -0
  76. package/dist/src/shared/media-service.js +59 -0
  77. package/dist/src/shared/media-types.js +1 -0
  78. package/{src/shared/xml-parser.ts → dist/src/shared/xml-parser.js} +72 -63
  79. package/dist/src/store/active-reply-store.js +41 -0
  80. package/dist/src/store/interfaces.js +1 -0
  81. package/dist/src/store/memory-store.js +33 -0
  82. package/dist/src/store/stream-batch-store.js +319 -0
  83. package/{src/target.ts → dist/src/target.js} +15 -48
  84. package/dist/src/transport/agent-api/client.js +168 -0
  85. package/dist/src/transport/agent-api/core.js +337 -0
  86. package/dist/src/transport/agent-api/delivery.js +28 -0
  87. package/dist/src/transport/agent-api/media-upload.js +4 -0
  88. package/dist/src/transport/agent-api/reply.js +24 -0
  89. package/dist/src/transport/agent-api/upstream-delivery.js +30 -0
  90. package/dist/src/transport/agent-api/upstream-media-upload.js +46 -0
  91. package/dist/src/transport/agent-api/upstream-reply.js +26 -0
  92. package/dist/src/transport/agent-callback/http-handler.js +30 -0
  93. package/dist/src/transport/agent-callback/inbound.js +4 -0
  94. package/dist/src/transport/agent-callback/reply.js +8 -0
  95. package/dist/src/transport/agent-callback/request-handler.js +189 -0
  96. package/dist/src/transport/agent-callback/session.js +15 -0
  97. package/dist/src/transport/bot-webhook/active-reply.js +27 -0
  98. package/dist/src/transport/bot-webhook/http-handler.js +31 -0
  99. package/dist/src/transport/bot-webhook/inbound-normalizer.js +496 -0
  100. package/dist/src/transport/bot-webhook/inbound.js +4 -0
  101. package/dist/src/transport/bot-webhook/message-shape.js +98 -0
  102. package/dist/src/transport/bot-webhook/protocol.js +124 -0
  103. package/dist/src/transport/bot-webhook/reply.js +9 -0
  104. package/dist/src/transport/bot-webhook/request-handler.js +285 -0
  105. package/dist/src/transport/bot-webhook/session.js +15 -0
  106. package/dist/src/transport/bot-ws/inbound.js +147 -0
  107. package/dist/src/transport/bot-ws/media.js +236 -0
  108. package/dist/src/transport/bot-ws/reply.js +310 -0
  109. package/dist/src/transport/bot-ws/sdk-adapter.js +257 -0
  110. package/dist/src/transport/bot-ws/session.js +15 -0
  111. package/dist/src/transport/http/common.js +78 -0
  112. package/dist/src/transport/http/registry.js +71 -0
  113. package/dist/src/transport/http/request-handler.js +51 -0
  114. package/{src/transport/index.ts → dist/src/transport/index.js} +2 -10
  115. package/dist/src/types/account.js +1 -0
  116. package/dist/src/types/config.js +1 -0
  117. package/dist/src/types/constants.js +28 -0
  118. package/dist/src/types/events.js +1 -0
  119. package/dist/src/types/index.js +1 -0
  120. package/dist/src/types/legacy-stream.js +1 -0
  121. package/dist/src/types/message.js +5 -0
  122. package/dist/src/types/runtime-context.js +1 -0
  123. package/dist/src/types/runtime.js +1 -0
  124. package/dist/src/types.js +1 -0
  125. package/dist/src/upstream/index.js +111 -0
  126. package/dist/src/wecom_msg_adapter/markdown_adapter.js +280 -0
  127. package/openclaw.plugin.json +15 -0
  128. package/package.json +18 -1
  129. package/.github/workflows/release.yml +0 -143
  130. package/GOVERNANCE.md +0 -26
  131. package/SKILLS_CAL.md +0 -895
  132. package/SKILLS_DOC.md +0 -2288
  133. package/UPSTREAM_CONFIG.md +0 -170
  134. package/UPSTREAM_PLAN.md +0 -175
  135. package/assets/01.bot-add.png +0 -0
  136. package/assets/01.bot-setp2.png +0 -0
  137. package/assets/01.image.jpg +0 -0
  138. package/assets/02.agent.add.png +0 -0
  139. package/assets/02.agent.api-set.png +0 -0
  140. package/assets/02.image.jpg +0 -0
  141. package/assets/03.agent.page.png +0 -0
  142. package/assets/03.bot.page.png +0 -0
  143. package/assets/link-me.jpg +0 -0
  144. package/assets/register.png +0 -0
  145. package/changelog/v2.2.28.md +0 -70
  146. package/changelog/v2.3.10.md +0 -17
  147. package/changelog/v2.3.11.md +0 -19
  148. package/changelog/v2.3.12.md +0 -25
  149. package/changelog/v2.3.13.md +0 -19
  150. package/changelog/v2.3.14.md +0 -48
  151. package/changelog/v2.3.15.md +0 -15
  152. package/changelog/v2.3.16.md +0 -11
  153. package/changelog/v2.3.18.md +0 -22
  154. package/changelog/v2.3.19.md +0 -73
  155. package/changelog/v2.3.2.md +0 -28
  156. package/changelog/v2.3.26.md +0 -21
  157. package/changelog/v2.3.27.md +0 -33
  158. package/changelog/v2.3.4.md +0 -20
  159. package/changelog/v2.3.9.md +0 -22
  160. package/changelog/v2.4.12.md +0 -37
  161. package/changelog/v2.4.16.md +0 -19
  162. package/compat-single-account.md +0 -148
  163. package/index.test.ts +0 -38
  164. package/scripts/test-proxy.ts +0 -70
  165. package/src/accounts.ts +0 -34
  166. package/src/agent/api-client.upload.test.ts +0 -109
  167. package/src/agent/handler.event-filter.test.ts +0 -100
  168. package/src/agent/handler.ts +0 -1105
  169. package/src/agent/index.ts +0 -12
  170. package/src/app/account-runtime.ts +0 -276
  171. package/src/app/bootstrap.ts +0 -29
  172. package/src/app/index.ts +0 -192
  173. package/src/capability/agent/delivery-service.ts +0 -87
  174. package/src/capability/agent/fallback-policy.ts +0 -13
  175. package/src/capability/agent/ingress-service.ts +0 -38
  176. package/src/capability/agent/upstream-delivery-service.ts +0 -96
  177. package/src/capability/bot/dispatch-config.ts +0 -47
  178. package/src/capability/bot/fallback-delivery.ts +0 -178
  179. package/src/capability/bot/local-path-delivery.ts +0 -215
  180. package/src/capability/bot/sandbox-media.test.ts +0 -221
  181. package/src/capability/bot/sandbox-media.ts +0 -176
  182. package/src/capability/bot/service.ts +0 -56
  183. package/src/capability/bot/stream-delivery.ts +0 -379
  184. package/src/capability/bot/stream-finalizer.ts +0 -120
  185. package/src/capability/bot/stream-orchestrator.ts +0 -371
  186. package/src/capability/bot/types.ts +0 -8
  187. package/src/capability/calendar/SKILLS_CHECKLIST.md +0 -251
  188. package/src/capability/calendar/tool.ts +0 -417
  189. package/src/capability/calendar/types.ts +0 -309
  190. package/src/capability/doc/tool.ts +0 -1629
  191. package/src/capability/doc/types.ts +0 -792
  192. package/src/capability/mcp/index.ts +0 -10
  193. package/src/capability/mcp/schema.ts +0 -107
  194. package/src/capability/mcp/tool.ts +0 -174
  195. package/src/capability/mcp/transport.ts +0 -394
  196. package/src/channel.config.test.ts +0 -147
  197. package/src/channel.lifecycle.test.ts +0 -255
  198. package/src/channel.meta.test.ts +0 -26
  199. package/src/channel.ts +0 -256
  200. package/src/config/accounts.resolve.test.ts +0 -75
  201. package/src/config/accounts.ts +0 -296
  202. package/src/config/derived-paths.test.ts +0 -111
  203. package/src/config/derived-paths.ts +0 -41
  204. package/src/config/index.ts +0 -26
  205. package/src/config/media.test.ts +0 -113
  206. package/src/config/media.ts +0 -139
  207. package/src/config/network.ts +0 -53
  208. package/src/config/routing.test.ts +0 -88
  209. package/src/config/routing.ts +0 -26
  210. package/src/config/runtime-config.ts +0 -46
  211. package/src/config/schema.ts +0 -90
  212. package/src/context-store.ts +0 -297
  213. package/src/crypto/index.ts +0 -24
  214. package/src/crypto.test.ts +0 -32
  215. package/src/crypto.ts +0 -176
  216. package/src/domain/models.ts +0 -7
  217. package/src/domain/policies.ts +0 -36
  218. package/src/dynamic-agent.account-scope.test.ts +0 -17
  219. package/src/gateway-monitor.ts +0 -181
  220. package/src/http.ts +0 -145
  221. package/src/media.test.ts +0 -82
  222. package/src/monitor/limits.ts +0 -7
  223. package/src/monitor/state.queue.test.ts +0 -185
  224. package/src/monitor/state.ts +0 -34
  225. package/src/monitor.active.test.ts +0 -245
  226. package/src/monitor.inbound-filter.test.ts +0 -63
  227. package/src/monitor.integration.test.ts +0 -208
  228. package/src/monitor.ts +0 -121
  229. package/src/monitor.webhook.test.ts +0 -774
  230. package/src/observability/audit-log.ts +0 -48
  231. package/src/observability/legacy-operational-event-store.ts +0 -36
  232. package/src/observability/raw-envelope-log.ts +0 -28
  233. package/src/observability/status-registry.ts +0 -13
  234. package/src/observability/transport-session-view.ts +0 -14
  235. package/src/onboarding.test.ts +0 -336
  236. package/src/onboarding.ts +0 -704
  237. package/src/outbound.test.ts +0 -1271
  238. package/src/outbound.ts +0 -746
  239. package/src/runtime/dispatcher.ts +0 -71
  240. package/src/runtime/outbound-intent.ts +0 -4
  241. package/src/runtime/reply-orchestrator.test.ts +0 -71
  242. package/src/runtime/reply-orchestrator.ts +0 -67
  243. package/src/runtime/routing-bridge.test.ts +0 -115
  244. package/src/runtime/routing-bridge.ts +0 -44
  245. package/src/runtime/session-manager.test.ts +0 -174
  246. package/src/runtime/session-manager.ts +0 -139
  247. package/src/runtime/source-registry.ts +0 -249
  248. package/src/runtime.ts +0 -14
  249. package/src/shared/command-auth.ts +0 -87
  250. package/src/shared/media-asset.ts +0 -78
  251. package/src/shared/media-service.test.ts +0 -111
  252. package/src/shared/media-service.ts +0 -84
  253. package/src/shared/media-types.ts +0 -5
  254. package/src/shared/xml-parser.test.ts +0 -50
  255. package/src/store/active-reply-store.ts +0 -42
  256. package/src/store/interfaces.ts +0 -11
  257. package/src/store/memory-store.ts +0 -43
  258. package/src/store/stream-batch-store.ts +0 -350
  259. package/src/transport/agent-api/client.ts +0 -277
  260. package/src/transport/agent-api/core.ts +0 -463
  261. package/src/transport/agent-api/delivery.ts +0 -41
  262. package/src/transport/agent-api/media-upload.ts +0 -11
  263. package/src/transport/agent-api/reply.ts +0 -39
  264. package/src/transport/agent-api/upstream-delivery.ts +0 -45
  265. package/src/transport/agent-api/upstream-media-upload.ts +0 -70
  266. package/src/transport/agent-api/upstream-reply.ts +0 -43
  267. package/src/transport/agent-callback/http-handler.ts +0 -47
  268. package/src/transport/agent-callback/inbound.ts +0 -5
  269. package/src/transport/agent-callback/reply.ts +0 -13
  270. package/src/transport/agent-callback/request-handler.ts +0 -244
  271. package/src/transport/agent-callback/session.ts +0 -23
  272. package/src/transport/bot-webhook/active-reply.ts +0 -39
  273. package/src/transport/bot-webhook/http-handler.ts +0 -48
  274. package/src/transport/bot-webhook/inbound-normalizer.test.ts +0 -433
  275. package/src/transport/bot-webhook/inbound-normalizer.ts +0 -558
  276. package/src/transport/bot-webhook/inbound.ts +0 -5
  277. package/src/transport/bot-webhook/message-shape.ts +0 -92
  278. package/src/transport/bot-webhook/protocol.ts +0 -148
  279. package/src/transport/bot-webhook/reply.ts +0 -15
  280. package/src/transport/bot-webhook/request-handler.ts +0 -394
  281. package/src/transport/bot-webhook/session.ts +0 -23
  282. package/src/transport/bot-ws/inbound.test.ts +0 -290
  283. package/src/transport/bot-ws/inbound.ts +0 -163
  284. package/src/transport/bot-ws/media.test.ts +0 -44
  285. package/src/transport/bot-ws/media.ts +0 -321
  286. package/src/transport/bot-ws/reply.test.ts +0 -450
  287. package/src/transport/bot-ws/reply.ts +0 -365
  288. package/src/transport/bot-ws/sdk-adapter.test.ts +0 -187
  289. package/src/transport/bot-ws/sdk-adapter.ts +0 -314
  290. package/src/transport/bot-ws/session.ts +0 -28
  291. package/src/transport/http/common.ts +0 -109
  292. package/src/transport/http/registry.ts +0 -92
  293. package/src/transport/http/request-handler.ts +0 -84
  294. package/src/types/account.ts +0 -70
  295. package/src/types/config.ts +0 -114
  296. package/src/types/constants.ts +0 -31
  297. package/src/types/events.ts +0 -21
  298. package/src/types/global.d.ts +0 -9
  299. package/src/types/index.ts +0 -17
  300. package/src/types/legacy-stream.ts +0 -50
  301. package/src/types/message.ts +0 -189
  302. package/src/types/runtime-context.ts +0 -28
  303. package/src/types/runtime.ts +0 -165
  304. package/src/types.ts +0 -41
  305. package/src/upstream/index.ts +0 -150
  306. package/src/upstream.test.ts +0 -84
  307. package/src/wecom_msg_adapter/markdown_adapter.ts +0 -331
  308. package/tsconfig.json +0 -22
  309. package/vitest.config.ts +0 -26
  310. /package/{src/capability/agent/index.ts → dist/src/capability/agent/index.js} +0 -0
  311. /package/{src/capability/bot/index.ts → dist/src/capability/bot/index.js} +0 -0
  312. /package/{src/capability/calendar/index.ts → dist/src/capability/calendar/index.js} +0 -0
  313. /package/{src/capability/index.ts → dist/src/capability/index.js} +0 -0
@@ -0,0 +1,257 @@
1
+ import crypto from "node:crypto";
2
+ import AiBot, { generateReqId, } from "@wecom/aibot-node-sdk";
3
+ import { registerBotWsPushHandle, unregisterBotWsPushHandle } from "../../app/index.js";
4
+ import { clearWecomMcpAccountCache } from "../../capability/mcp/index.js";
5
+ import { mapBotWsFrameToInboundEvent } from "./inbound.js";
6
+ import { uploadAndSendBotWsMedia } from "./media.js";
7
+ import { createBotWsReplyHandle } from "./reply.js";
8
+ import { createBotWsSessionSnapshot } from "./session.js";
9
+ export class BotWsSdkAdapter {
10
+ runtime;
11
+ log;
12
+ client;
13
+ ownerId;
14
+ constructor(runtime, log) {
15
+ this.runtime = runtime;
16
+ this.log = log;
17
+ this.ownerId = `${this.runtime.account.accountId}:ws:${crypto.randomUUID().slice(0, 8)}`;
18
+ }
19
+ start() {
20
+ const bot = this.runtime.account.bot;
21
+ if (!bot?.wsConfigured || !bot.ws) {
22
+ throw new Error(`WeCom bot account "${this.runtime.account.accountId}" missing WS config.`);
23
+ }
24
+ this.log.info?.(`[wecom-ws] start account=${this.runtime.account.accountId} botId=${bot.ws.botId} wsUrl=default heartbeat=default reconnectInterval=default`);
25
+ const client = new AiBot.WSClient({
26
+ botId: bot.ws.botId,
27
+ secret: bot.ws.secret,
28
+ logger: {
29
+ debug: (message, ...args) => this.log.info?.(`[wecom-ws] ${message} ${args.join(" ")}`.trim()),
30
+ info: (message, ...args) => this.log.info?.(`[wecom-ws] ${message} ${args.join(" ")}`.trim()),
31
+ warn: (message, ...args) => this.log.warn?.(`[wecom-ws] ${message} ${args.join(" ")}`.trim()),
32
+ error: (message, ...args) => this.log.error?.(`[wecom-ws] ${message} ${args.join(" ")}`.trim()),
33
+ },
34
+ });
35
+ this.client = client;
36
+ registerBotWsPushHandle(this.runtime.account.accountId, {
37
+ isConnected: () => client.isConnected,
38
+ replyCommand: async ({ cmd, body, headers }) => {
39
+ const replyHeaders = {
40
+ ...(headers ?? {}),
41
+ req_id: headers?.req_id ?? generateReqId("wecom_ws"),
42
+ };
43
+ const result = await client.reply({ headers: replyHeaders }, body ?? {}, cmd);
44
+ this.runtime.touchTransportSession("bot-ws", {
45
+ ownerId: this.ownerId,
46
+ running: true,
47
+ connected: client.isConnected,
48
+ authenticated: client.isConnected,
49
+ lastOutboundAt: Date.now(),
50
+ lastError: undefined,
51
+ });
52
+ return result;
53
+ },
54
+ sendMarkdown: async (chatId, content) => {
55
+ await client.sendMessage(chatId, {
56
+ msgtype: "markdown",
57
+ markdown: { content },
58
+ });
59
+ this.runtime.touchTransportSession("bot-ws", {
60
+ ownerId: this.ownerId,
61
+ running: true,
62
+ connected: client.isConnected,
63
+ authenticated: client.isConnected,
64
+ lastOutboundAt: Date.now(),
65
+ lastError: undefined,
66
+ });
67
+ },
68
+ sendMedia: async ({ chatId, mediaUrl, text, mediaLocalRoots, maxBytes }) => {
69
+ const result = await uploadAndSendBotWsMedia({
70
+ wsClient: client,
71
+ chatId,
72
+ mediaUrl,
73
+ mediaLocalRoots,
74
+ maxBytes,
75
+ });
76
+ if (result.ok && text?.trim()) {
77
+ await client.sendMessage(chatId, {
78
+ msgtype: "markdown",
79
+ markdown: { content: text.trim() },
80
+ });
81
+ }
82
+ this.runtime.touchTransportSession("bot-ws", {
83
+ ownerId: this.ownerId,
84
+ running: true,
85
+ connected: client.isConnected,
86
+ authenticated: client.isConnected,
87
+ lastOutboundAt: Date.now(),
88
+ lastError: result.ok ? undefined : result.error,
89
+ });
90
+ return result;
91
+ },
92
+ });
93
+ client.on("connected", () => {
94
+ this.log.info?.(`[wecom-ws] connected account=${this.runtime.account.accountId}`);
95
+ this.runtime.updateTransportSession(createBotWsSessionSnapshot({
96
+ accountId: this.runtime.account.accountId,
97
+ ownerId: this.ownerId,
98
+ connected: true,
99
+ authenticated: false,
100
+ }));
101
+ });
102
+ client.on("authenticated", () => {
103
+ this.log.info?.(`[wecom-ws] authenticated account=${this.runtime.account.accountId}`);
104
+ this.runtime.updateTransportSession(createBotWsSessionSnapshot({
105
+ accountId: this.runtime.account.accountId,
106
+ ownerId: this.ownerId,
107
+ connected: true,
108
+ authenticated: true,
109
+ }));
110
+ });
111
+ client.on("disconnected", (reason) => {
112
+ clearWecomMcpAccountCache(this.runtime.account.accountId);
113
+ const normalizedReason = String(reason ?? "").toLowerCase();
114
+ const kicked = normalizedReason.includes("kick") ||
115
+ normalizedReason.includes("owner") ||
116
+ normalizedReason.includes("replaced");
117
+ this.log.warn?.(`[wecom-ws] disconnected account=${this.runtime.account.accountId} kicked=${String(kicked)} reason=${reason ?? "unknown"}`);
118
+ if (kicked) {
119
+ this.runtime.recordOperationalIssue({
120
+ transport: "bot-ws",
121
+ category: "ws-kicked",
122
+ summary: `ws owner lost: ${reason ?? "unknown"}`,
123
+ error: reason ?? "unknown",
124
+ });
125
+ }
126
+ this.runtime.updateTransportSession(createBotWsSessionSnapshot({
127
+ accountId: this.runtime.account.accountId,
128
+ ownerId: this.ownerId,
129
+ running: false,
130
+ connected: false,
131
+ authenticated: false,
132
+ lastDisconnectedAt: Date.now(),
133
+ lastError: reason,
134
+ }));
135
+ });
136
+ client.on("reconnecting", (attempt) => {
137
+ this.log.warn?.(`[wecom-ws] reconnecting account=${this.runtime.account.accountId} attempt=${attempt}`);
138
+ });
139
+ client.on("error", (error) => {
140
+ this.log.error?.(`[wecom-ws] error account=${this.runtime.account.accountId} message=${error.message}`);
141
+ this.runtime.updateTransportSession(createBotWsSessionSnapshot({
142
+ accountId: this.runtime.account.accountId,
143
+ ownerId: this.ownerId,
144
+ running: false,
145
+ connected: client.isConnected,
146
+ authenticated: client.isConnected,
147
+ lastError: error.message,
148
+ }));
149
+ });
150
+ const handleFrame = async (frame) => {
151
+ const botAccount = this.runtime.account.bot;
152
+ if (!botAccount) {
153
+ return;
154
+ }
155
+ this.log.info?.(`[wecom-ws] frame account=${this.runtime.account.accountId} cmd=${frame.cmd} reqId=${frame.headers.req_id ?? "n/a"}`);
156
+ this.runtime.touchTransportSession("bot-ws", {
157
+ ownerId: this.ownerId,
158
+ running: true,
159
+ connected: client.isConnected,
160
+ authenticated: client.isConnected,
161
+ lastInboundAt: Date.now(),
162
+ });
163
+ const event = mapBotWsFrameToInboundEvent({
164
+ account: botAccount,
165
+ frame,
166
+ });
167
+ const replyHandle = createBotWsReplyHandle({
168
+ client,
169
+ frame,
170
+ accountId: this.runtime.account.accountId,
171
+ inboundKind: event.inboundKind,
172
+ placeholderContent: botAccount.config.streamPlaceholderContent,
173
+ autoSendPlaceholder: event.inboundKind === "text" ||
174
+ event.inboundKind === "image" ||
175
+ event.inboundKind === "file" ||
176
+ event.inboundKind === "voice" ||
177
+ event.inboundKind === "mixed",
178
+ onDeliver: () => {
179
+ this.runtime.touchTransportSession("bot-ws", {
180
+ ownerId: this.ownerId,
181
+ running: true,
182
+ connected: client.isConnected,
183
+ authenticated: client.isConnected,
184
+ lastOutboundAt: Date.now(),
185
+ });
186
+ },
187
+ onFail: (error) => {
188
+ this.runtime.touchTransportSession("bot-ws", {
189
+ ownerId: this.ownerId,
190
+ running: client.isConnected,
191
+ connected: client.isConnected,
192
+ authenticated: client.isConnected,
193
+ lastError: error instanceof Error ? error.message : String(error),
194
+ });
195
+ },
196
+ });
197
+ const staticWelcomeText = event.inboundKind === "welcome" ? botAccount.config.welcomeText?.trim() : undefined;
198
+ if (staticWelcomeText) {
199
+ this.log.info?.(`[wecom-ws] static welcome reply account=${this.runtime.account.accountId} messageId=${event.messageId} peer=${event.conversation.peerKind}:${event.conversation.peerId} len=${staticWelcomeText.length}`);
200
+ await replyHandle.deliver({
201
+ text: staticWelcomeText,
202
+ }, { kind: "final" });
203
+ this.log.info?.(`[wecom-ws] static welcome delivered account=${this.runtime.account.accountId} messageId=${event.messageId}`);
204
+ return;
205
+ }
206
+ await this.runtime.handleEvent(event, replyHandle);
207
+ };
208
+ const runHandleFrame = (frame) => {
209
+ void handleFrame(frame).catch((error) => {
210
+ const message = error instanceof Error ? error.message : String(error);
211
+ this.log.error?.(`[wecom-ws] frame handler failed account=${this.runtime.account.accountId} reqId=${frame.headers?.req_id ?? "n/a"} message=${message}`);
212
+ this.runtime.recordOperationalIssue({
213
+ transport: "bot-ws",
214
+ category: "runtime-error",
215
+ messageId: frame.body?.msgid,
216
+ raw: {
217
+ transport: "bot-ws",
218
+ command: frame.cmd,
219
+ headers: frame.headers,
220
+ body: frame.body,
221
+ envelopeType: "ws",
222
+ },
223
+ summary: `bot-ws frame handler crashed reqId=${frame.headers?.req_id ?? "n/a"}`,
224
+ error: message,
225
+ });
226
+ this.runtime.touchTransportSession("bot-ws", {
227
+ ownerId: this.ownerId,
228
+ running: client.isConnected,
229
+ connected: client.isConnected,
230
+ authenticated: client.isConnected,
231
+ lastError: message,
232
+ });
233
+ });
234
+ };
235
+ client.on("message", (frame) => {
236
+ runHandleFrame(frame);
237
+ });
238
+ client.on("event", (frame) => {
239
+ runHandleFrame(frame);
240
+ });
241
+ client.connect();
242
+ }
243
+ stop() {
244
+ this.log.info?.(`[wecom-ws] stop account=${this.runtime.account.accountId}`);
245
+ clearWecomMcpAccountCache(this.runtime.account.accountId);
246
+ unregisterBotWsPushHandle(this.runtime.account.accountId);
247
+ this.runtime.updateTransportSession(createBotWsSessionSnapshot({
248
+ accountId: this.runtime.account.accountId,
249
+ ownerId: this.ownerId,
250
+ running: false,
251
+ connected: false,
252
+ authenticated: false,
253
+ lastDisconnectedAt: Date.now(),
254
+ }));
255
+ this.client?.disconnect();
256
+ }
257
+ }
@@ -0,0 +1,15 @@
1
+ export function createBotWsSessionSnapshot(params) {
2
+ return {
3
+ accountId: params.accountId,
4
+ transport: "bot-ws",
5
+ running: params.running ?? true,
6
+ ownerId: params.ownerId,
7
+ connected: params.connected,
8
+ authenticated: params.authenticated,
9
+ lastConnectedAt: params.lastConnectedAt ?? (params.connected ? Date.now() : undefined),
10
+ lastDisconnectedAt: params.lastDisconnectedAt,
11
+ lastInboundAt: params.lastInboundAt,
12
+ lastOutboundAt: params.lastOutboundAt,
13
+ lastError: params.lastError,
14
+ };
15
+ }
@@ -0,0 +1,78 @@
1
+ import { WEBHOOK_PATHS } from "../../types/constants.js";
2
+ export function normalizeWecomWebhookPath(raw) {
3
+ const trimmed = raw.trim();
4
+ if (!trimmed)
5
+ return "/";
6
+ const withSlash = trimmed.startsWith("/") ? trimmed : `/${trimmed}`;
7
+ if (withSlash.length > 1 && withSlash.endsWith("/"))
8
+ return withSlash.slice(0, -1);
9
+ return withSlash;
10
+ }
11
+ export function resolveQueryParams(req) {
12
+ const url = new URL(req.url ?? "/", "http://localhost");
13
+ return url.searchParams;
14
+ }
15
+ export function resolvePath(req) {
16
+ const url = new URL(req.url ?? "/", "http://localhost");
17
+ return normalizeWecomWebhookPath(url.pathname || "/");
18
+ }
19
+ export function resolveSignatureParam(params) {
20
+ return params.get("msg_signature") ?? params.get("msgsignature") ?? params.get("signature") ?? "";
21
+ }
22
+ export function isAgentCallbackPathCandidate(path) {
23
+ return (path === WEBHOOK_PATHS.AGENT ||
24
+ path === WEBHOOK_PATHS.AGENT_PLUGIN ||
25
+ path.startsWith(`${WEBHOOK_PATHS.AGENT}/`) ||
26
+ path.startsWith(`${WEBHOOK_PATHS.AGENT_PLUGIN}/`));
27
+ }
28
+ export function isNonMatrixWecomBasePath(path) {
29
+ return (path === WEBHOOK_PATHS.BOT ||
30
+ path === WEBHOOK_PATHS.BOT_ALT ||
31
+ path === WEBHOOK_PATHS.AGENT ||
32
+ path === WEBHOOK_PATHS.BOT_PLUGIN ||
33
+ path === WEBHOOK_PATHS.AGENT_PLUGIN);
34
+ }
35
+ function maskAccountId(accountId) {
36
+ const normalized = accountId.trim();
37
+ if (!normalized)
38
+ return "***";
39
+ if (normalized.length <= 4)
40
+ return `${normalized[0] ?? "*"}***`;
41
+ return `${normalized.slice(0, 2)}***${normalized.slice(-2)}`;
42
+ }
43
+ export function logRouteFailure(params) {
44
+ const payload = {
45
+ reqId: params.reqId,
46
+ path: params.path,
47
+ method: params.method,
48
+ reason: params.reason,
49
+ candidateAccountIds: params.candidateAccountIds.map(maskAccountId),
50
+ };
51
+ console.error(`[wecom] route-error ${JSON.stringify(payload)}`);
52
+ }
53
+ export function writeRouteFailure(res, reason, message) {
54
+ res.statusCode = 401;
55
+ res.setHeader("Content-Type", "application/json; charset=utf-8");
56
+ res.end(JSON.stringify({ error: reason, message }));
57
+ }
58
+ export async function readTextBody(req, maxBytes) {
59
+ const chunks = [];
60
+ let total = 0;
61
+ return await new Promise((resolve) => {
62
+ req.on("data", (chunk) => {
63
+ total += chunk.length;
64
+ if (total > maxBytes) {
65
+ resolve({ ok: false, error: "payload too large" });
66
+ req.destroy();
67
+ return;
68
+ }
69
+ chunks.push(chunk);
70
+ });
71
+ req.on("end", () => {
72
+ resolve({ ok: true, value: Buffer.concat(chunks).toString("utf8") });
73
+ });
74
+ req.on("error", (err) => {
75
+ resolve({ ok: false, error: String(err) });
76
+ });
77
+ });
78
+ }
@@ -0,0 +1,71 @@
1
+ import { DEFAULT_ACCOUNT_ID } from "openclaw/plugin-sdk/routing";
2
+ import { monitorState } from "../../monitor/state.js";
3
+ import { WEBHOOK_PATHS } from "../../types/constants.js";
4
+ import { normalizeWecomWebhookPath } from "./common.js";
5
+ const webhookTargets = new Map();
6
+ const agentTargets = new Map();
7
+ function ensurePruneTimer() {
8
+ monitorState.startPruning();
9
+ }
10
+ function checkPruneTimer() {
11
+ if (webhookTargets.size === 0 && agentTargets.size === 0) {
12
+ monitorState.stopPruning();
13
+ }
14
+ }
15
+ export function registerWecomWebhookTarget(target) {
16
+ const key = normalizeWecomWebhookPath(target.path);
17
+ const normalizedTarget = { ...target, path: key };
18
+ const existing = webhookTargets.get(key) ?? [];
19
+ webhookTargets.set(key, [...existing, normalizedTarget]);
20
+ ensurePruneTimer();
21
+ return () => {
22
+ const updated = (webhookTargets.get(key) ?? []).filter((entry) => entry !== normalizedTarget);
23
+ if (updated.length > 0)
24
+ webhookTargets.set(key, updated);
25
+ else
26
+ webhookTargets.delete(key);
27
+ checkPruneTimer();
28
+ };
29
+ }
30
+ export function registerAgentWebhookTarget(target) {
31
+ const key = normalizeWecomWebhookPath(target.path);
32
+ const normalizedTarget = { ...target, path: key };
33
+ const existing = agentTargets.get(key) ?? [];
34
+ agentTargets.set(key, [...existing, normalizedTarget]);
35
+ ensurePruneTimer();
36
+ return () => {
37
+ const updated = (agentTargets.get(key) ?? []).filter((entry) => entry !== normalizedTarget);
38
+ if (updated.length > 0)
39
+ agentTargets.set(key, updated);
40
+ else
41
+ agentTargets.delete(key);
42
+ checkPruneTimer();
43
+ };
44
+ }
45
+ export function getWecomWebhookTargets(path) {
46
+ return webhookTargets.get(normalizeWecomWebhookPath(path)) ?? [];
47
+ }
48
+ export function getAgentWebhookTargets(path) {
49
+ return agentTargets.get(normalizeWecomWebhookPath(path)) ?? [];
50
+ }
51
+ export function hasMatrixExplicitRoutesRegistered() {
52
+ for (const [key, targets] of webhookTargets.entries()) {
53
+ if (key.startsWith(`${WEBHOOK_PATHS.BOT_ALT}/`) && targets.some((target) => target.account.accountId !== DEFAULT_ACCOUNT_ID)) {
54
+ return true;
55
+ }
56
+ if (key.startsWith(`${WEBHOOK_PATHS.BOT_PLUGIN}/`) &&
57
+ targets.some((target) => target.account.accountId !== DEFAULT_ACCOUNT_ID)) {
58
+ return true;
59
+ }
60
+ }
61
+ for (const [key, targets] of agentTargets.entries()) {
62
+ if (key.startsWith(`${WEBHOOK_PATHS.AGENT}/`) && targets.some((target) => target.agent.accountId !== DEFAULT_ACCOUNT_ID)) {
63
+ return true;
64
+ }
65
+ if (key.startsWith(`${WEBHOOK_PATHS.AGENT_PLUGIN}/`) &&
66
+ targets.some((target) => target.agent.accountId !== DEFAULT_ACCOUNT_ID)) {
67
+ return true;
68
+ }
69
+ }
70
+ return false;
71
+ }
@@ -0,0 +1,51 @@
1
+ import crypto from "node:crypto";
2
+ import { handleAgentCallbackRequest } from "../agent-callback/request-handler.js";
3
+ import { getAgentWebhookTargets, getWecomWebhookTargets, hasMatrixExplicitRoutesRegistered } from "./registry.js";
4
+ import { isAgentCallbackPathCandidate, isNonMatrixWecomBasePath, logRouteFailure, resolvePath, resolveQueryParams, writeRouteFailure, } from "./common.js";
5
+ export async function handleWecomHttpRequest(params) {
6
+ const { req, res, handleBotWebhookRequest } = params;
7
+ const path = resolvePath(req);
8
+ const reqId = crypto.randomUUID().slice(0, 8);
9
+ const remote = req.socket?.remoteAddress ?? "unknown";
10
+ const ua = String(req.headers["user-agent"] ?? "");
11
+ const cl = String(req.headers["content-length"] ?? "");
12
+ const q = resolveQueryParams(req);
13
+ const hasTimestamp = Boolean(q.get("timestamp"));
14
+ const hasNonce = Boolean(q.get("nonce"));
15
+ const hasEchostr = Boolean(q.get("echostr"));
16
+ const hasMsgSig = Boolean(q.get("msg_signature"));
17
+ const hasSignature = Boolean(q.get("signature"));
18
+ console.log(`[wecom] inbound(http): reqId=${reqId} path=${path} method=${req.method ?? "UNKNOWN"} remote=${remote} ua=${ua ? `"${ua}"` : "N/A"} contentLength=${cl || "N/A"} query={timestamp:${hasTimestamp},nonce:${hasNonce},echostr:${hasEchostr},msg_signature:${hasMsgSig},signature:${hasSignature}}`);
19
+ if (hasMatrixExplicitRoutesRegistered() && isNonMatrixWecomBasePath(path)) {
20
+ logRouteFailure({
21
+ reqId,
22
+ path,
23
+ method: req.method ?? "UNKNOWN",
24
+ reason: "wecom_matrix_path_required",
25
+ candidateAccountIds: [],
26
+ });
27
+ writeRouteFailure(res, "wecom_matrix_path_required", "Matrix mode requires explicit account path. Use /plugins/wecom/bot/{accountId} or /plugins/wecom/agent/{accountId}.");
28
+ return true;
29
+ }
30
+ const agentTargets = getAgentWebhookTargets(path);
31
+ if (agentTargets.length > 0 || isAgentCallbackPathCandidate(path)) {
32
+ return handleAgentCallbackRequest({
33
+ req,
34
+ res,
35
+ path,
36
+ reqId,
37
+ targets: agentTargets,
38
+ });
39
+ }
40
+ const botTargets = getWecomWebhookTargets(path);
41
+ if (botTargets.length === 0) {
42
+ return false;
43
+ }
44
+ return handleBotWebhookRequest({
45
+ req,
46
+ res,
47
+ path,
48
+ reqId,
49
+ targets: botTargets,
50
+ });
51
+ }
@@ -1,14 +1,6 @@
1
1
  export { BotWsSdkAdapter } from "./bot-ws/sdk-adapter.js";
2
2
  export { startBotWebhookTransport } from "./bot-webhook/http-handler.js";
3
3
  export { startAgentCallbackTransport } from "./agent-callback/http-handler.js";
4
- export {
5
- deliverAgentApiMedia,
6
- deliverAgentApiText,
7
- } from "./agent-api/delivery.js";
8
- export {
9
- downloadAgentApiMedia,
10
- getAgentApiAccessToken,
11
- sendAgentApiMedia,
12
- sendAgentApiText,
13
- } from "./agent-api/client.js";
4
+ export { deliverAgentApiMedia, deliverAgentApiText, } from "./agent-api/delivery.js";
5
+ export { downloadAgentApiMedia, getAgentApiAccessToken, sendAgentApiMedia, sendAgentApiText, } from "./agent-api/client.js";
14
6
  export { uploadAgentApiMedia } from "./agent-api/media-upload.js";
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,28 @@
1
+ export const WEBHOOK_PATHS = {
2
+ BOT: "/wecom/bot",
3
+ BOT_ALT: "/wecom",
4
+ AGENT: "/wecom/agent",
5
+ BOT_PLUGIN: "/plugins/wecom/bot",
6
+ AGENT_PLUGIN: "/plugins/wecom/agent",
7
+ };
8
+ export const API_ENDPOINTS = {
9
+ GET_TOKEN: "https://qyapi.weixin.qq.com/cgi-bin/gettoken",
10
+ SEND_MESSAGE: "https://qyapi.weixin.qq.com/cgi-bin/message/send",
11
+ SEND_APPCHAT: "https://qyapi.weixin.qq.com/cgi-bin/appchat/send",
12
+ UPLOAD_MEDIA: "https://qyapi.weixin.qq.com/cgi-bin/media/upload",
13
+ DOWNLOAD_MEDIA: "https://qyapi.weixin.qq.com/cgi-bin/media/get",
14
+ };
15
+ export const LIMITS = {
16
+ TEXT_MAX_BYTES: 20_480,
17
+ TOKEN_REFRESH_BUFFER_MS: 60_000,
18
+ REQUEST_TIMEOUT_MS: 15_000,
19
+ MAX_REQUEST_BODY_SIZE: 1024 * 1024,
20
+ BOT_WEBHOOK_PASSIVE_WINDOW_MS: 5_000,
21
+ BOT_WEBHOOK_RESPONSE_URL_TTL_MS: 60 * 60 * 1000,
22
+ BOT_STREAM_WINDOW_MS: 6 * 60 * 1000,
23
+ BOT_WS_HEARTBEAT_MS: 30_000,
24
+ };
25
+ export const CRYPTO = {
26
+ PKCS7_BLOCK_SIZE: 32,
27
+ AES_KEY_LENGTH: 32,
28
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export * from "./constants.js";
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,5 @@
1
+ /**
2
+ * WeCom 消息类型定义
3
+ * Bot 和 Agent 模式共用
4
+ */
5
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,111 @@
1
+ /**
2
+ * 上下游企业支持模块
3
+ *
4
+ * 根据企业微信文档:https://developer.work.weixin.qq.com/document/path/97213
5
+ *
6
+ * 关键逻辑:
7
+ * 1. 上下游企业消息中的 ToUserName 是下游企业的 CorpID
8
+ * 2. 需要使用下游企业的 access_token 来发送消息
9
+ * 3. 获取下游企业 access_token 的接口:
10
+ * POST https://qyapi.weixin.qq.com/cgi-bin/corpgroup/corp/gettoken?access_token=ACCESS_TOKEN
11
+ * {
12
+ * "corpid": "下游企业corpid",
13
+ * "business_type": 1, // 1 表示上下游企业
14
+ * "agentid": 下游企业应用ID
15
+ * }
16
+ * 4. 需要使用上游企业的 access_token 作为调用凭证
17
+ */
18
+ /**
19
+ * 从消息中检测是否是上下游用户
20
+ * 通过比较消息中的 ToUserName(CorpID)与配置的 CorpID
21
+ */
22
+ export function detectUpstreamUser(params) {
23
+ const { messageToUserName, primaryCorpId } = params;
24
+ if (!messageToUserName?.trim() || !primaryCorpId?.trim()) {
25
+ return false;
26
+ }
27
+ const normalizedMessageCorpId = messageToUserName.trim().toLowerCase();
28
+ const normalizedPrimaryCorpId = primaryCorpId.trim().toLowerCase();
29
+ // 如果消息中的 CorpID 与主 CorpID 不同,则是上下游用户
30
+ return normalizedMessageCorpId !== normalizedPrimaryCorpId;
31
+ }
32
+ /**
33
+ * 为上下游用户创建临时的 Agent 配置
34
+ * 使用下游企业的 CorpID 和 AgentID,但保持主企业的 corpSecret
35
+ *
36
+ * 注意:这个配置用于发送消息,但获取 access_token 时需要使用专门的
37
+ * corpgroup/corp/gettoken 接口
38
+ */
39
+ export function createUpstreamAgentConfig(params) {
40
+ const { baseAgent, upstreamCorpId, upstreamAgentId } = params;
41
+ return {
42
+ ...baseAgent,
43
+ corpId: upstreamCorpId,
44
+ agentId: upstreamAgentId,
45
+ // corpSecret 保持主企业的,用于获取下游企业的 access_token
46
+ // token 和 encodingAESKey 保持主企业的,用于回调验证
47
+ };
48
+ }
49
+ /**
50
+ * 从配置中解析上下游企业映射
51
+ * 支持在 agent 配置中添加 upstreamCorps 字段
52
+ */
53
+ export function resolveUpstreamCorpConfig(params) {
54
+ const { upstreamCorpId, upstreamCorps } = params;
55
+ if (!upstreamCorps) {
56
+ return undefined;
57
+ }
58
+ // Normalize to array format (support both Record<string, ...> and array)
59
+ const entries = Array.isArray(upstreamCorps)
60
+ ? upstreamCorps.map((item, i) => [String(i), item])
61
+ : Object.entries(upstreamCorps);
62
+ // Find matching upstream config
63
+ const normalizedTargetCorpId = upstreamCorpId.trim().toLowerCase();
64
+ for (const [key, config] of entries) {
65
+ const normalizedConfigCorpId = config.corpId.trim().toLowerCase();
66
+ if (normalizedConfigCorpId === normalizedTargetCorpId) {
67
+ return config;
68
+ }
69
+ }
70
+ return undefined;
71
+ }
72
+ /**
73
+ * 构建上下游用户的回复目标
74
+ * 格式: wecom-agent-upstream:{accountId}:{corpId}:{userId}
75
+ */
76
+ export function buildUpstreamAgentSessionTarget(userId, accountId, upstreamCorpId) {
77
+ return `wecom-agent-upstream:${accountId}:${upstreamCorpId}:${userId}`;
78
+ }
79
+ /**
80
+ * 解析上下游用户的回复目标
81
+ */
82
+ export function parseUpstreamAgentSessionTarget(target) {
83
+ const prefix = "wecom-agent-upstream:";
84
+ if (target.startsWith(prefix)) {
85
+ const parts = target.slice(prefix.length).split(":");
86
+ if (parts.length !== 3) {
87
+ return undefined;
88
+ }
89
+ return {
90
+ accountId: parts[0],
91
+ upstreamCorpId: parts[1],
92
+ userId: parts[2],
93
+ };
94
+ }
95
+ // 兼容当前工作区里尚未持久化的新格式,避免旧会话目标失效。
96
+ const queryIndex = target.indexOf("?upstream_corp=");
97
+ if (queryIndex < 0 || !target.startsWith("wecom-agent:")) {
98
+ return undefined;
99
+ }
100
+ const pathPart = target.slice(0, queryIndex);
101
+ const upstreamCorpId = target.slice(queryIndex + "?upstream_corp=".length).trim();
102
+ const match = pathPart.match(/^wecom-agent:([^:]+):user:(.+)$/i);
103
+ if (!match || !upstreamCorpId) {
104
+ return undefined;
105
+ }
106
+ return {
107
+ accountId: match[1].trim(),
108
+ upstreamCorpId,
109
+ userId: match[2].trim(),
110
+ };
111
+ }