@yanhaidao/wecom 2.4.120 → 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 (323) hide show
  1. package/README.md +4 -5
  2. package/dist/index.js +68 -0
  3. package/dist/src/accounts.js +20 -0
  4. package/dist/src/agent/handler.js +895 -0
  5. package/dist/src/agent/index.js +5 -0
  6. package/dist/src/app/account-runtime.js +216 -0
  7. package/dist/src/app/bootstrap.js +19 -0
  8. package/dist/src/app/index.js +118 -0
  9. package/dist/src/capability/agent/delivery-service.js +63 -0
  10. package/dist/src/capability/agent/fallback-policy.js +6 -0
  11. package/dist/src/capability/agent/ingress-service.js +33 -0
  12. package/dist/src/capability/agent/upstream-delivery-service.js +71 -0
  13. package/dist/src/capability/bot/dispatch-config.js +45 -0
  14. package/dist/src/capability/bot/fallback-delivery.js +147 -0
  15. package/dist/src/capability/bot/local-path-delivery.js +178 -0
  16. package/dist/src/capability/bot/sandbox-media.js +138 -0
  17. package/dist/src/capability/bot/service.js +49 -0
  18. package/dist/src/capability/bot/stream-delivery.js +321 -0
  19. package/dist/src/capability/bot/stream-finalizer.js +81 -0
  20. package/dist/src/capability/bot/stream-orchestrator.js +318 -0
  21. package/dist/src/capability/bot/types.js +1 -0
  22. package/{src/capability/calendar/client.ts → dist/src/capability/calendar/client.js} +118 -241
  23. package/{src/capability/calendar/schema.ts → dist/src/capability/calendar/schema.js} +0 -38
  24. package/dist/src/capability/calendar/tool.js +365 -0
  25. package/dist/src/capability/calendar/types.js +12 -0
  26. package/{src/capability/doc/client.ts → dist/src/capability/doc/client.js} +370 -605
  27. package/{src/capability/doc/schema.ts → dist/src/capability/doc/schema.js} +345 -394
  28. package/dist/src/capability/doc/tool.js +1556 -0
  29. package/dist/src/capability/doc/types.js +113 -0
  30. package/dist/src/capability/mcp/index.js +3 -0
  31. package/dist/src/capability/mcp/schema.js +102 -0
  32. package/dist/src/capability/mcp/tool.js +146 -0
  33. package/dist/src/capability/mcp/transport.js +293 -0
  34. package/dist/src/channel.js +224 -0
  35. package/dist/src/config/accounts.js +236 -0
  36. package/dist/src/config/derived-paths.js +31 -0
  37. package/dist/src/config/index.js +7 -0
  38. package/dist/src/config/media.js +110 -0
  39. package/dist/src/config/network.js +32 -0
  40. package/dist/src/config/routing.js +20 -0
  41. package/dist/src/config/runtime-config.js +25 -0
  42. package/dist/src/config/schema.js +4 -0
  43. package/{src/config-schema.ts → dist/src/config-schema.js} +1 -1
  44. package/dist/src/context-store.js +219 -0
  45. package/{src/crypto/aes.ts → dist/src/crypto/aes.js} +11 -28
  46. package/dist/src/crypto/index.js +9 -0
  47. package/{src/crypto/signature.ts → dist/src/crypto/signature.js} +3 -18
  48. package/{src/crypto/xml.ts → dist/src/crypto/xml.js} +3 -11
  49. package/dist/src/crypto.js +145 -0
  50. package/dist/src/domain/models.js +1 -0
  51. package/dist/src/domain/policies.js +32 -0
  52. package/{src/dynamic-agent.ts → dist/src/dynamic-agent.js} +36 -73
  53. package/dist/src/gateway-monitor.js +139 -0
  54. package/dist/src/http.js +114 -0
  55. package/{src/media.ts → dist/src/media.js} +21 -40
  56. package/dist/src/monitor/limits.js +7 -0
  57. package/dist/src/monitor/state.js +28 -0
  58. package/dist/src/monitor.js +84 -0
  59. package/dist/src/observability/audit-log.js +30 -0
  60. package/dist/src/observability/legacy-operational-event-store.js +22 -0
  61. package/dist/src/observability/raw-envelope-log.js +24 -0
  62. package/dist/src/observability/status-registry.js +9 -0
  63. package/dist/src/observability/transport-session-view.js +14 -0
  64. package/dist/src/onboarding.js +546 -0
  65. package/dist/src/outbound.js +557 -0
  66. package/dist/src/runtime/dispatcher.js +57 -0
  67. package/{src/runtime/index.ts → dist/src/runtime/index.js} +0 -1
  68. package/dist/src/runtime/outbound-intent.js +1 -0
  69. package/dist/src/runtime/reply-orchestrator.js +38 -0
  70. package/dist/src/runtime/routing-bridge.js +26 -0
  71. package/dist/src/runtime/session-manager.js +112 -0
  72. package/dist/src/runtime/source-registry.js +174 -0
  73. package/dist/src/runtime.js +1 -0
  74. package/dist/src/shared/command-auth.js +57 -0
  75. package/{src/shared/index.ts → dist/src/shared/index.js} +0 -1
  76. package/dist/src/shared/media-asset.js +65 -0
  77. package/dist/src/shared/media-service.js +59 -0
  78. package/dist/src/shared/media-types.js +1 -0
  79. package/{src/shared/xml-parser.ts → dist/src/shared/xml-parser.js} +72 -63
  80. package/dist/src/store/active-reply-store.js +41 -0
  81. package/dist/src/store/interfaces.js +1 -0
  82. package/dist/src/store/memory-store.js +33 -0
  83. package/dist/src/store/stream-batch-store.js +319 -0
  84. package/{src/target.ts → dist/src/target.js} +15 -48
  85. package/dist/src/transport/agent-api/client.js +168 -0
  86. package/dist/src/transport/agent-api/core.js +337 -0
  87. package/dist/src/transport/agent-api/delivery.js +28 -0
  88. package/dist/src/transport/agent-api/media-upload.js +4 -0
  89. package/dist/src/transport/agent-api/reply.js +24 -0
  90. package/dist/src/transport/agent-api/upstream-delivery.js +30 -0
  91. package/dist/src/transport/agent-api/upstream-media-upload.js +46 -0
  92. package/dist/src/transport/agent-api/upstream-reply.js +26 -0
  93. package/dist/src/transport/agent-callback/http-handler.js +30 -0
  94. package/dist/src/transport/agent-callback/inbound.js +4 -0
  95. package/dist/src/transport/agent-callback/reply.js +8 -0
  96. package/dist/src/transport/agent-callback/request-handler.js +189 -0
  97. package/dist/src/transport/agent-callback/session.js +15 -0
  98. package/dist/src/transport/bot-webhook/active-reply.js +27 -0
  99. package/dist/src/transport/bot-webhook/http-handler.js +31 -0
  100. package/dist/src/transport/bot-webhook/inbound-normalizer.js +496 -0
  101. package/dist/src/transport/bot-webhook/inbound.js +4 -0
  102. package/dist/src/transport/bot-webhook/message-shape.js +98 -0
  103. package/dist/src/transport/bot-webhook/protocol.js +124 -0
  104. package/dist/src/transport/bot-webhook/reply.js +9 -0
  105. package/dist/src/transport/bot-webhook/request-handler.js +285 -0
  106. package/dist/src/transport/bot-webhook/session.js +15 -0
  107. package/dist/src/transport/bot-ws/inbound.js +147 -0
  108. package/dist/src/transport/bot-ws/media.js +236 -0
  109. package/dist/src/transport/bot-ws/reply.js +310 -0
  110. package/dist/src/transport/bot-ws/sdk-adapter.js +257 -0
  111. package/dist/src/transport/bot-ws/session.js +15 -0
  112. package/dist/src/transport/http/common.js +78 -0
  113. package/dist/src/transport/http/registry.js +71 -0
  114. package/dist/src/transport/http/request-handler.js +51 -0
  115. package/{src/transport/index.ts → dist/src/transport/index.js} +2 -10
  116. package/dist/src/types/account.js +1 -0
  117. package/dist/src/types/config.js +1 -0
  118. package/dist/src/types/constants.js +28 -0
  119. package/dist/src/types/events.js +1 -0
  120. package/dist/src/types/index.js +1 -0
  121. package/dist/src/types/legacy-stream.js +1 -0
  122. package/dist/src/types/message.js +5 -0
  123. package/dist/src/types/runtime-context.js +1 -0
  124. package/dist/src/types/runtime.js +1 -0
  125. package/dist/src/types.js +1 -0
  126. package/dist/src/upstream/index.js +111 -0
  127. package/dist/src/wecom_msg_adapter/markdown_adapter.js +280 -0
  128. package/openclaw.plugin.json +15 -0
  129. package/package.json +18 -1
  130. package/.github/workflows/release.yml +0 -143
  131. package/GOVERNANCE.md +0 -26
  132. package/MENU_EVENT_CONF.md +0 -500
  133. package/MENU_EVENT_PLAN.md +0 -440
  134. package/SKILLS_CAL.md +0 -895
  135. package/SKILLS_DOC.md +0 -2288
  136. package/UPSTREAM_CONFIG.md +0 -170
  137. package/UPSTREAM_PLAN.md +0 -175
  138. package/assets/01.bot-add.png +0 -0
  139. package/assets/01.bot-setp2.png +0 -0
  140. package/assets/01.image.jpg +0 -0
  141. package/assets/02.agent.add.png +0 -0
  142. package/assets/02.agent.api-set.png +0 -0
  143. package/assets/02.image.jpg +0 -0
  144. package/assets/03.agent.page.png +0 -0
  145. package/assets/03.bot.page.png +0 -0
  146. package/assets/link-me.jpg +0 -0
  147. package/assets/register.png +0 -0
  148. package/changelog/v2.2.28.md +0 -70
  149. package/changelog/v2.3.10.md +0 -17
  150. package/changelog/v2.3.11.md +0 -19
  151. package/changelog/v2.3.12.md +0 -25
  152. package/changelog/v2.3.13.md +0 -19
  153. package/changelog/v2.3.14.md +0 -48
  154. package/changelog/v2.3.15.md +0 -15
  155. package/changelog/v2.3.16.md +0 -11
  156. package/changelog/v2.3.18.md +0 -22
  157. package/changelog/v2.3.19.md +0 -73
  158. package/changelog/v2.3.2.md +0 -28
  159. package/changelog/v2.3.26.md +0 -21
  160. package/changelog/v2.3.27.md +0 -33
  161. package/changelog/v2.3.4.md +0 -20
  162. package/changelog/v2.3.9.md +0 -22
  163. package/changelog/v2.4.12.md +0 -37
  164. package/compat-single-account.md +0 -148
  165. package/index.test.ts +0 -38
  166. package/scripts/test-proxy.ts +0 -70
  167. package/scripts/wecom/README.md +0 -123
  168. package/scripts/wecom/menu-click-help.js +0 -59
  169. package/scripts/wecom/menu-click-help.py +0 -55
  170. package/src/accounts.ts +0 -34
  171. package/src/agent/api-client.upload.test.ts +0 -109
  172. package/src/agent/event-router.test.ts +0 -421
  173. package/src/agent/event-router.ts +0 -272
  174. package/src/agent/handler.event-filter.test.ts +0 -135
  175. package/src/agent/handler.ts +0 -1250
  176. package/src/agent/index.ts +0 -12
  177. package/src/agent/script-runner.ts +0 -186
  178. package/src/agent/test-fixtures/invalid-json-script.mjs +0 -1
  179. package/src/agent/test-fixtures/reply-event-script.mjs +0 -29
  180. package/src/agent/test-fixtures/reply-event-script.py +0 -17
  181. package/src/app/account-runtime.ts +0 -276
  182. package/src/app/bootstrap.ts +0 -29
  183. package/src/app/index.ts +0 -192
  184. package/src/capability/agent/delivery-service.ts +0 -87
  185. package/src/capability/agent/fallback-policy.ts +0 -13
  186. package/src/capability/agent/ingress-service.ts +0 -38
  187. package/src/capability/agent/upstream-delivery-service.ts +0 -96
  188. package/src/capability/bot/dispatch-config.ts +0 -47
  189. package/src/capability/bot/fallback-delivery.ts +0 -178
  190. package/src/capability/bot/local-path-delivery.ts +0 -215
  191. package/src/capability/bot/sandbox-media.test.ts +0 -221
  192. package/src/capability/bot/sandbox-media.ts +0 -176
  193. package/src/capability/bot/service.ts +0 -56
  194. package/src/capability/bot/stream-delivery.ts +0 -379
  195. package/src/capability/bot/stream-finalizer.ts +0 -120
  196. package/src/capability/bot/stream-orchestrator.ts +0 -371
  197. package/src/capability/bot/types.ts +0 -8
  198. package/src/capability/calendar/SKILLS_CHECKLIST.md +0 -251
  199. package/src/capability/calendar/tool.ts +0 -417
  200. package/src/capability/calendar/types.ts +0 -309
  201. package/src/capability/doc/tool.ts +0 -1629
  202. package/src/capability/doc/types.ts +0 -792
  203. package/src/capability/mcp/index.ts +0 -10
  204. package/src/capability/mcp/schema.ts +0 -107
  205. package/src/capability/mcp/tool.ts +0 -174
  206. package/src/capability/mcp/transport.ts +0 -394
  207. package/src/channel.config.test.ts +0 -180
  208. package/src/channel.lifecycle.test.ts +0 -255
  209. package/src/channel.meta.test.ts +0 -26
  210. package/src/channel.ts +0 -256
  211. package/src/config/accounts.resolve.test.ts +0 -75
  212. package/src/config/accounts.ts +0 -312
  213. package/src/config/derived-paths.test.ts +0 -111
  214. package/src/config/derived-paths.ts +0 -41
  215. package/src/config/index.ts +0 -22
  216. package/src/config/media.test.ts +0 -113
  217. package/src/config/media.ts +0 -139
  218. package/src/config/network.ts +0 -20
  219. package/src/config/routing.test.ts +0 -88
  220. package/src/config/routing.ts +0 -26
  221. package/src/config/runtime-config.ts +0 -46
  222. package/src/config/schema.ts +0 -144
  223. package/src/context-store.ts +0 -297
  224. package/src/crypto/index.ts +0 -24
  225. package/src/crypto.test.ts +0 -32
  226. package/src/crypto.ts +0 -176
  227. package/src/domain/models.ts +0 -7
  228. package/src/domain/policies.ts +0 -36
  229. package/src/dynamic-agent.account-scope.test.ts +0 -17
  230. package/src/gateway-monitor.ts +0 -181
  231. package/src/http.ts +0 -137
  232. package/src/media.test.ts +0 -82
  233. package/src/monitor/limits.ts +0 -7
  234. package/src/monitor/state.queue.test.ts +0 -185
  235. package/src/monitor/state.ts +0 -34
  236. package/src/monitor.active.test.ts +0 -245
  237. package/src/monitor.inbound-filter.test.ts +0 -63
  238. package/src/monitor.integration.test.ts +0 -208
  239. package/src/monitor.ts +0 -121
  240. package/src/monitor.webhook.test.ts +0 -774
  241. package/src/observability/audit-log.ts +0 -48
  242. package/src/observability/legacy-operational-event-store.ts +0 -36
  243. package/src/observability/raw-envelope-log.ts +0 -28
  244. package/src/observability/status-registry.ts +0 -13
  245. package/src/observability/transport-session-view.ts +0 -14
  246. package/src/onboarding.test.ts +0 -336
  247. package/src/onboarding.ts +0 -704
  248. package/src/outbound.test.ts +0 -1271
  249. package/src/outbound.ts +0 -746
  250. package/src/runtime/dispatcher.ts +0 -71
  251. package/src/runtime/outbound-intent.ts +0 -4
  252. package/src/runtime/reply-orchestrator.test.ts +0 -71
  253. package/src/runtime/reply-orchestrator.ts +0 -67
  254. package/src/runtime/routing-bridge.test.ts +0 -115
  255. package/src/runtime/routing-bridge.ts +0 -44
  256. package/src/runtime/session-manager.test.ts +0 -174
  257. package/src/runtime/session-manager.ts +0 -139
  258. package/src/runtime/source-registry.ts +0 -249
  259. package/src/runtime.ts +0 -14
  260. package/src/shared/command-auth.ts +0 -87
  261. package/src/shared/media-asset.ts +0 -78
  262. package/src/shared/media-service.test.ts +0 -111
  263. package/src/shared/media-service.ts +0 -84
  264. package/src/shared/media-types.ts +0 -5
  265. package/src/shared/xml-parser.test.ts +0 -50
  266. package/src/store/active-reply-store.ts +0 -42
  267. package/src/store/interfaces.ts +0 -11
  268. package/src/store/memory-store.ts +0 -43
  269. package/src/store/stream-batch-store.ts +0 -350
  270. package/src/transport/agent-api/client.ts +0 -277
  271. package/src/transport/agent-api/core.ts +0 -463
  272. package/src/transport/agent-api/delivery.ts +0 -41
  273. package/src/transport/agent-api/media-upload.ts +0 -11
  274. package/src/transport/agent-api/reply.ts +0 -39
  275. package/src/transport/agent-api/upstream-delivery.ts +0 -45
  276. package/src/transport/agent-api/upstream-media-upload.ts +0 -70
  277. package/src/transport/agent-api/upstream-reply.ts +0 -43
  278. package/src/transport/agent-callback/http-handler.ts +0 -47
  279. package/src/transport/agent-callback/inbound.ts +0 -5
  280. package/src/transport/agent-callback/reply.ts +0 -13
  281. package/src/transport/agent-callback/request-handler.ts +0 -244
  282. package/src/transport/agent-callback/session.ts +0 -23
  283. package/src/transport/bot-webhook/active-reply.ts +0 -39
  284. package/src/transport/bot-webhook/http-handler.ts +0 -48
  285. package/src/transport/bot-webhook/inbound-normalizer.ts +0 -371
  286. package/src/transport/bot-webhook/inbound.ts +0 -5
  287. package/src/transport/bot-webhook/message-shape.ts +0 -89
  288. package/src/transport/bot-webhook/protocol.ts +0 -148
  289. package/src/transport/bot-webhook/reply.ts +0 -15
  290. package/src/transport/bot-webhook/request-handler.ts +0 -394
  291. package/src/transport/bot-webhook/session.ts +0 -23
  292. package/src/transport/bot-ws/inbound.test.ts +0 -96
  293. package/src/transport/bot-ws/inbound.ts +0 -116
  294. package/src/transport/bot-ws/media.test.ts +0 -44
  295. package/src/transport/bot-ws/media.ts +0 -321
  296. package/src/transport/bot-ws/reply.test.ts +0 -450
  297. package/src/transport/bot-ws/reply.ts +0 -365
  298. package/src/transport/bot-ws/sdk-adapter.test.ts +0 -187
  299. package/src/transport/bot-ws/sdk-adapter.ts +0 -314
  300. package/src/transport/bot-ws/session.ts +0 -28
  301. package/src/transport/http/common.ts +0 -109
  302. package/src/transport/http/registry.ts +0 -92
  303. package/src/transport/http/request-handler.ts +0 -84
  304. package/src/types/account.ts +0 -72
  305. package/src/types/config.ts +0 -166
  306. package/src/types/constants.ts +0 -31
  307. package/src/types/events.ts +0 -21
  308. package/src/types/global.d.ts +0 -9
  309. package/src/types/index.ts +0 -17
  310. package/src/types/legacy-stream.ts +0 -50
  311. package/src/types/message.ts +0 -187
  312. package/src/types/runtime-context.ts +0 -28
  313. package/src/types/runtime.ts +0 -165
  314. package/src/types.ts +0 -41
  315. package/src/upstream/index.ts +0 -150
  316. package/src/upstream.test.ts +0 -84
  317. package/src/wecom_msg_adapter/markdown_adapter.ts +0 -331
  318. package/tsconfig.json +0 -22
  319. package/vitest.config.ts +0 -26
  320. /package/{src/capability/agent/index.ts → dist/src/capability/agent/index.js} +0 -0
  321. /package/{src/capability/bot/index.ts → dist/src/capability/bot/index.js} +0 -0
  322. /package/{src/capability/calendar/index.ts → dist/src/capability/calendar/index.js} +0 -0
  323. /package/{src/capability/index.ts → dist/src/capability/index.js} +0 -0
@@ -1,36 +0,0 @@
1
- import type { ResolvedBotAccount, UnifiedInboundEvent } from "../types/index.js";
2
-
3
- export function assertBotPrimaryTransport(account: ResolvedBotAccount): void {
4
- if (account.primaryTransport === "ws" && !account.wsConfigured) {
5
- throw new Error(`WeCom bot account "${account.accountId}" is missing bot.ws credentials.`);
6
- }
7
- if (account.primaryTransport === "webhook" && !account.webhookConfigured) {
8
- throw new Error(`WeCom bot account "${account.accountId}" is missing bot.webhook credentials.`);
9
- }
10
- }
11
-
12
- export function buildDedupKey(event: UnifiedInboundEvent): string {
13
- return `${event.accountId}:${event.transport}:${event.messageId}`;
14
- }
15
-
16
- export function resolveConversationKey(event: UnifiedInboundEvent): string {
17
- const conversation = event.conversation;
18
- return [event.accountId, conversation.peerKind, conversation.peerId, conversation.senderId].join(":");
19
- }
20
-
21
- export function normalizeWecomAllowFromEntry(raw: string): string {
22
- return raw
23
- .trim()
24
- .toLowerCase()
25
- .replace(/^wecom:/, "")
26
- .replace(/^user:/, "")
27
- .replace(/^userid:/, "");
28
- }
29
-
30
- export function isWecomSenderAllowed(senderUserId: string, allowFrom: string[]): boolean {
31
- const list = allowFrom.map((entry) => normalizeWecomAllowFromEntry(entry)).filter(Boolean);
32
- if (list.includes("*")) return true;
33
- const normalizedSender = normalizeWecomAllowFromEntry(senderUserId);
34
- if (!normalizedSender) return false;
35
- return list.includes(normalizedSender);
36
- }
@@ -1,17 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
-
3
- import { generateAgentId } from "./dynamic-agent.js";
4
-
5
- describe("generateAgentId account scoping", () => {
6
- it("generates different ids for same peer across accounts", () => {
7
- const a = generateAgentId("dm", "zhangsan", "acct-a");
8
- const b = generateAgentId("dm", "zhangsan", "acct-b");
9
- expect(a).toBe("wecom-acct-a-dm-zhangsan");
10
- expect(b).toBe("wecom-acct-b-dm-zhangsan");
11
- expect(a).not.toBe(b);
12
- });
13
-
14
- it("falls back to default account scope when accountId is omitted", () => {
15
- expect(generateAgentId("group", "wr123456")).toBe("wecom-default-group-wr123456");
16
- });
17
- });
@@ -1,181 +0,0 @@
1
- import type {
2
- ChannelGatewayContext,
3
- OpenClawConfig,
4
- } from "openclaw/plugin-sdk";
5
-
6
- import {
7
- listWecomAccountIds,
8
- resolveDerivedPathSummary,
9
- resolveWecomAccount,
10
- resolveWecomAccountConflict,
11
- } from "./config/index.js";
12
- import { createAccountRuntime } from "./app/bootstrap.js";
13
- import { registerAccountRuntime, unregisterAccountRuntime } from "./app/index.js";
14
- import type { ResolvedWecomAccount, WecomConfig } from "./types/index.js";
15
- import { WecomBotCapabilityService } from "./capability/bot/index.js";
16
- import { WecomAgentIngressService } from "./capability/agent/index.js";
17
- import type { WecomRuntimeEnv } from "./types/runtime-context.js";
18
-
19
- type AccountRouteRegistryItem = {
20
- botPaths: string[];
21
- agentPaths: string[];
22
- };
23
-
24
- const accountRouteRegistry = new Map<string, AccountRouteRegistryItem>();
25
-
26
- function logRegisteredRouteSummary(
27
- ctx: ChannelGatewayContext<ResolvedWecomAccount>,
28
- preferredOrder: string[],
29
- ): void {
30
- const seen = new Set<string>();
31
- const orderedAccountIds = [
32
- ...preferredOrder.filter((accountId) => accountRouteRegistry.has(accountId)),
33
- ...Array.from(accountRouteRegistry.keys())
34
- .filter((accountId) => !seen.has(accountId))
35
- .sort((a, b) => a.localeCompare(b)),
36
- ].filter((accountId) => {
37
- if (seen.has(accountId)) return false;
38
- seen.add(accountId);
39
- return true;
40
- });
41
-
42
- const entries = orderedAccountIds
43
- .map((accountId) => {
44
- const routes = accountRouteRegistry.get(accountId);
45
- if (!routes) return undefined;
46
- const botText = routes.botPaths.length > 0 ? routes.botPaths.join(", ") : "未启用";
47
- const agentText = routes.agentPaths.length > 0 ? routes.agentPaths.join(", ") : "未启用";
48
- return `accountId=${accountId}(Bot: ${botText};Agent: ${agentText})`;
49
- })
50
- .filter((entry): entry is string => Boolean(entry));
51
- const summary = entries.length > 0 ? entries.join("; ") : "无";
52
- ctx.log?.info(`[${ctx.account.accountId}] 已注册账号路由汇总:${summary}`);
53
- }
54
-
55
- function resolveExpectedRouteSummaryAccountIds(cfg: OpenClawConfig): string[] {
56
- return listWecomAccountIds(cfg)
57
- .filter((accountId) => {
58
- const conflict = resolveWecomAccountConflict({ cfg, accountId });
59
- if (conflict) return false;
60
- const account = resolveWecomAccount({ cfg, accountId });
61
- if (!account.enabled || !account.configured) return false;
62
- return Boolean(account.bot?.configured || account.agent?.configured);
63
- })
64
- .sort((a, b) => a.localeCompare(b));
65
- }
66
-
67
- function waitForAbortSignal(abortSignal: AbortSignal): Promise<void> {
68
- if (abortSignal.aborted) {
69
- return Promise.resolve();
70
- }
71
- return new Promise<void>((resolve) => {
72
- const onAbort = () => {
73
- abortSignal.removeEventListener("abort", onAbort);
74
- resolve();
75
- };
76
- abortSignal.addEventListener("abort", onAbort, { once: true });
77
- });
78
- }
79
-
80
- /**
81
- * Keeps WeCom webhook targets registered for the account lifecycle.
82
- * The promise only settles after gateway abort/reload signals shutdown.
83
- */
84
- export async function monitorWecomProvider(
85
- ctx: ChannelGatewayContext<ResolvedWecomAccount>,
86
- ): Promise<void> {
87
- const account = ctx.account;
88
- const cfg = ctx.cfg as OpenClawConfig;
89
- const expectedRouteSummaryAccountIds = resolveExpectedRouteSummaryAccountIds(cfg);
90
- const conflict = resolveWecomAccountConflict({
91
- cfg,
92
- accountId: account.accountId,
93
- });
94
- if (conflict) {
95
- ctx.setStatus({
96
- accountId: account.accountId,
97
- running: false,
98
- configured: false,
99
- lastError: conflict.message,
100
- });
101
- throw new Error(conflict.message);
102
- }
103
- const bot = account.bot;
104
- const agent = account.agent;
105
- const botConfigured = Boolean(bot?.configured);
106
- const agentConfigured = Boolean(agent?.configured);
107
-
108
- if (!botConfigured && !agentConfigured) {
109
- ctx.log?.warn(`[${account.accountId}] wecom not configured; channel is idle`);
110
- ctx.setStatus({ accountId: account.accountId, running: false, configured: false });
111
- await waitForAbortSignal(ctx.abortSignal);
112
- return;
113
- }
114
-
115
- const accountRuntime = createAccountRuntime(ctx);
116
- registerAccountRuntime(accountRuntime);
117
- const botPaths: string[] = [];
118
- const agentPaths: string[] = [];
119
- const runtimeEnv: WecomRuntimeEnv = {
120
- log: (message) => ctx.log?.info(message),
121
- error: (message) => ctx.log?.error(message),
122
- };
123
- const botService = new WecomBotCapabilityService(
124
- accountRuntime,
125
- cfg,
126
- runtimeEnv,
127
- );
128
- const agentIngress = new WecomAgentIngressService(accountRuntime, cfg, runtimeEnv);
129
- try {
130
- ctx.log?.info(
131
- `[${account.accountId}] wecom runtime start bot=${bot?.primaryTransport ?? "disabled"} agent=${agentConfigured ? "callback/api" : "disabled"}`,
132
- );
133
- const botRegistration = botService.start();
134
- if (botRegistration) {
135
- botPaths.push(...botRegistration.descriptors);
136
- ctx.log?.info(
137
- `[${account.accountId}] wecom bot ${botRegistration.transport} started: ${botRegistration.descriptors.join(", ")}`,
138
- );
139
- }
140
-
141
- const agentRegistration = agentIngress.start();
142
- if (agentRegistration) {
143
- agentPaths.push(...agentRegistration.descriptors);
144
- ctx.log?.info(
145
- `[${account.accountId}] wecom agent ${agentRegistration.transport} started: ${agentRegistration.descriptors.join(", ")}`,
146
- );
147
- }
148
-
149
- accountRouteRegistry.set(account.accountId, { botPaths, agentPaths });
150
- const shouldLogSummary =
151
- expectedRouteSummaryAccountIds.length <= 1 ||
152
- expectedRouteSummaryAccountIds.every((accountId) => accountRouteRegistry.has(accountId));
153
- if (shouldLogSummary) {
154
- logRegisteredRouteSummary(ctx, expectedRouteSummaryAccountIds);
155
- }
156
-
157
- ctx.setStatus({
158
- running: true,
159
- configured: true,
160
- webhookPath: botPaths[0] ?? agentPaths[0] ?? null,
161
- lastStartAt: Date.now(),
162
- ...accountRuntime.buildRuntimeStatus(),
163
- });
164
- ctx.log?.info(
165
- `[${account.accountId}] runtime status health=${accountRuntime.buildRuntimeStatus().health} transports=${(accountRuntime.buildRuntimeStatus().transportSessions ?? []).join(" | ") || "none"}`,
166
- );
167
-
168
- await waitForAbortSignal(ctx.abortSignal);
169
- } finally {
170
- botService.stop();
171
- agentIngress.stop();
172
- accountRouteRegistry.delete(account.accountId);
173
- unregisterAccountRuntime(account.accountId);
174
- ctx.setStatus({
175
- running: false,
176
- lastStopAt: Date.now(),
177
- ...accountRuntime.buildRuntimeStatus(),
178
- });
179
- ctx.log?.info(`[${account.accountId}] wecom runtime stopped`);
180
- }
181
- }
package/src/http.ts DELETED
@@ -1,137 +0,0 @@
1
- import type { Dispatcher } from "undici";
2
- import { ProxyAgent, fetch as undiciFetch } from "undici";
3
-
4
- type ProxyDispatcher = Dispatcher;
5
-
6
- const proxyDispatchers = new Map<string, ProxyDispatcher>();
7
-
8
- /**
9
- * **getProxyDispatcher (获取代理 Dispatcher)**
10
- *
11
- * 缓存并复用 ProxyAgent,避免重复创建连接池。
12
- */
13
- function getProxyDispatcher(proxyUrl: string): ProxyDispatcher {
14
- const existing = proxyDispatchers.get(proxyUrl);
15
- if (existing) return existing;
16
- const created = new ProxyAgent(proxyUrl);
17
- proxyDispatchers.set(proxyUrl, created);
18
- return created;
19
- }
20
-
21
- function summarizeHttpTarget(input: string | URL): string {
22
- try {
23
- const url = typeof input === "string" ? new URL(input) : input;
24
- return `${url.origin}${url.pathname}`;
25
- } catch {
26
- return String(input);
27
- }
28
- }
29
-
30
- function mergeAbortSignal(params: {
31
- signal?: AbortSignal;
32
- timeoutMs?: number;
33
- }): AbortSignal | undefined {
34
- const signals: AbortSignal[] = [];
35
- if (params.signal) signals.push(params.signal);
36
- if (params.timeoutMs && Number.isFinite(params.timeoutMs) && params.timeoutMs > 0) {
37
- signals.push(AbortSignal.timeout(params.timeoutMs));
38
- }
39
- if (!signals.length) return undefined;
40
- if (signals.length === 1) return signals[0];
41
- return AbortSignal.any(signals);
42
- }
43
-
44
- /**
45
- * **WecomHttpOptions (HTTP 选项)**
46
- *
47
- * @property proxyUrl 代理服务器地址
48
- * @property timeoutMs 请求超时时间 (毫秒)
49
- * @property signal AbortSignal 信号
50
- */
51
- export type WecomHttpOptions = {
52
- proxyUrl?: string;
53
- timeoutMs?: number;
54
- signal?: AbortSignal;
55
- };
56
-
57
- /**
58
- * **wecomFetch (统一 HTTP 请求)**
59
- *
60
- * 基于 `undici` 的 fetch 封装,自动处理 ProxyAgent 和 Timeout。
61
- * 所有对企业微信 API 的调用都应经过此函数。
62
- */
63
- export async function wecomFetch(input: string | URL, init?: RequestInit, opts?: WecomHttpOptions): Promise<Response> {
64
- const proxyUrl = opts?.proxyUrl?.trim() ?? "";
65
- const dispatcher = proxyUrl ? getProxyDispatcher(proxyUrl) : undefined;
66
- const startedAt = Date.now();
67
- const method = (init?.method ?? "GET").toUpperCase();
68
- const target = summarizeHttpTarget(input);
69
-
70
- const initSignal = init?.signal ?? undefined;
71
- const signal = mergeAbortSignal({ signal: opts?.signal ?? initSignal, timeoutMs: opts?.timeoutMs });
72
-
73
- const headers = new Headers(init?.headers ?? {});
74
- if (!headers.has("User-Agent")) {
75
- headers.set("User-Agent", "OpenClaw/2.0 (WeCom-Agent)");
76
- }
77
-
78
- const nextInit: RequestInit & { dispatcher?: Dispatcher } = {
79
- ...(init ?? {}),
80
- ...(signal ? { signal } : {}),
81
- ...(dispatcher ? { dispatcher } : {}),
82
- headers,
83
- };
84
-
85
- try {
86
- console.log(
87
- `[wecom-http] request method=${method} target=${target} proxy=${proxyUrl || "none"} timeoutMs=${String(opts?.timeoutMs ?? "none")}`,
88
- );
89
- const response = await undiciFetch(input, nextInit as Parameters<typeof undiciFetch>[1]) as unknown as Response;
90
- console.log(
91
- `[wecom-http] response method=${method} target=${target} status=${response.status} durationMs=${Date.now() - startedAt}`,
92
- );
93
- return response;
94
- } catch (err: unknown) {
95
- if (err instanceof Error && err.name === "TypeError" && err.message === "fetch failed") {
96
- const cause = (err as any).cause;
97
- console.error(
98
- `[wecom-http] fetch failed method=${method} target=${target} durationMs=${Date.now() - startedAt} proxy=${proxyUrl || "none"}${cause ? ` cause=${String(cause)}` : ""}`,
99
- );
100
- }
101
- throw err;
102
- }
103
- }
104
-
105
- /**
106
- * **readResponseBodyAsBuffer (读取响应 Body)**
107
- *
108
- * 将 Response Body 读取为 Buffer,支持最大字节限制以防止内存溢出。
109
- * 适用于下载媒体文件等场景。
110
- */
111
- export async function readResponseBodyAsBuffer(res: Response, maxBytes?: number): Promise<Buffer> {
112
- if (!res.body) return Buffer.alloc(0);
113
-
114
- const limit = maxBytes && Number.isFinite(maxBytes) && maxBytes > 0 ? maxBytes : undefined;
115
- const chunks: Uint8Array[] = [];
116
- let total = 0;
117
-
118
- const reader = res.body.getReader();
119
- while (true) {
120
- const { done, value } = await reader.read();
121
- if (done) break;
122
- if (!value) continue;
123
-
124
- total += value.byteLength;
125
- if (limit && total > limit) {
126
- try {
127
- await reader.cancel("body too large");
128
- } catch {
129
- // ignore
130
- }
131
- throw new Error(`response body too large (>${limit} bytes)`);
132
- }
133
- chunks.push(value);
134
- }
135
-
136
- return Buffer.concat(chunks.map((c) => Buffer.from(c)));
137
- }
package/src/media.test.ts DELETED
@@ -1,82 +0,0 @@
1
- import { describe, it, expect, vi } from "vitest";
2
- import { decryptWecomMedia, decryptWecomMediaWithMeta } from "./media.js";
3
- import { WECOM_PKCS7_BLOCK_SIZE } from "./crypto.js";
4
- import crypto from "node:crypto";
5
-
6
- const { undiciFetch } = vi.hoisted(() => {
7
- const undiciFetch = vi.fn();
8
- return { undiciFetch };
9
- });
10
-
11
- vi.mock("undici", () => ({
12
- fetch: undiciFetch,
13
- ProxyAgent: class ProxyAgent { },
14
- }));
15
-
16
- function pkcs7Pad(buf: Buffer, blockSize: number): Buffer {
17
- const mod = buf.length % blockSize;
18
- const pad = mod === 0 ? blockSize : blockSize - mod;
19
- const padByte = Buffer.from([pad]);
20
- return Buffer.concat([buf, Buffer.alloc(pad, padByte[0]!)]);
21
- }
22
-
23
- describe("decryptWecomMedia", () => {
24
- it("should download and decrypt media successfully", async () => {
25
- // 1. Setup Key and Data
26
- const aesKeyBase64 = "jWmYm7qr5nMoCAstdRmNjt3p7vsH8HkK+qiJqQ0aaaa="; // 32 bytes when decoded + padding
27
- const aesKey = Buffer.from(aesKeyBase64 + "=", "base64");
28
- const iv = aesKey.subarray(0, 16);
29
-
30
- const originalData = Buffer.from("Hello WeCom Image Data", "utf8");
31
-
32
- // 2. Encrypt manually (AES-256-CBC + PKCS7)
33
- const padded = pkcs7Pad(originalData, WECOM_PKCS7_BLOCK_SIZE);
34
- const cipher = crypto.createCipheriv("aes-256-cbc", aesKey, iv);
35
- cipher.setAutoPadding(false);
36
- const encrypted = Buffer.concat([cipher.update(padded), cipher.final()]);
37
-
38
- // 3. Mock HTTP fetch
39
- undiciFetch.mockResolvedValue(new Response(encrypted));
40
-
41
- // 4. Test
42
- const decrypted = await decryptWecomMedia("http://mock.url/image", aesKeyBase64);
43
-
44
- // 5. Assert
45
- expect(decrypted.toString("utf8")).toBe("Hello WeCom Image Data");
46
- expect(undiciFetch).toHaveBeenCalledWith(
47
- "http://mock.url/image",
48
- expect.objectContaining({ signal: expect.anything() }),
49
- );
50
- });
51
-
52
- it("should fail if key is invalid", async () => {
53
- await expect(decryptWecomMedia("http://url", "invalid-key")).rejects.toThrow();
54
- });
55
-
56
- it("should return source metadata when using decryptWecomMediaWithMeta", async () => {
57
- const aesKeyBase64 = "jWmYm7qr5nMoCAstdRmNjt3p7vsH8HkK+qiJqQ0aaaa=";
58
- const aesKey = Buffer.from(aesKeyBase64 + "=", "base64");
59
- const iv = aesKey.subarray(0, 16);
60
- const originalData = Buffer.from("meta test", "utf8");
61
- const padded = pkcs7Pad(originalData, WECOM_PKCS7_BLOCK_SIZE);
62
- const cipher = crypto.createCipheriv("aes-256-cbc", aesKey, iv);
63
- cipher.setAutoPadding(false);
64
- const encrypted = Buffer.concat([cipher.update(padded), cipher.final()]);
65
-
66
- undiciFetch.mockResolvedValue(
67
- new Response(encrypted, {
68
- status: 200,
69
- headers: {
70
- "content-type": "application/octet-stream; charset=binary",
71
- "content-disposition": "attachment; filename*=UTF-8''report%20v1.docx",
72
- },
73
- }),
74
- );
75
-
76
- const decrypted = await decryptWecomMediaWithMeta("http://mock.url/media?id=1", aesKeyBase64);
77
- expect(decrypted.buffer.toString("utf8")).toBe("meta test");
78
- expect(decrypted.sourceContentType).toBe("application/octet-stream");
79
- expect(decrypted.sourceFilename).toBe("report v1.docx");
80
- expect(decrypted.sourceUrl).toBe("http://mock.url/media?id=1");
81
- });
82
- });
@@ -1,7 +0,0 @@
1
- export const LIMITS = {
2
- STREAM_TTL_MS: 10 * 60 * 1000,
3
- ACTIVE_REPLY_TTL_MS: 60 * 60 * 1000,
4
- DEFAULT_DEBOUNCE_MS: 500,
5
- STREAM_MAX_BYTES: 20_480,
6
- REQUEST_TIMEOUT_MS: 15_000,
7
- };
@@ -1,185 +0,0 @@
1
- import { describe, expect, test, vi } from "vitest";
2
-
3
- import type { WecomBotInboundMessage as WecomInboundMessage } from "../types/index.js";
4
- import type { WecomWebhookTarget } from "./types.js";
5
- import { StreamStore } from "./state.js";
6
-
7
- describe("wecom StreamStore queue", () => {
8
- test("does not merge into active batch; flushes queued batch after active finishes", async () => {
9
- vi.useFakeTimers();
10
- try {
11
- const store = new StreamStore();
12
- const flushed: string[] = [];
13
- store.setFlushHandler((pending) => flushed.push(pending.streamId));
14
-
15
- const target = {
16
- account: {} as any,
17
- config: {} as any,
18
- runtime: {},
19
- core: {} as any,
20
- path: "/wecom",
21
- } satisfies WecomWebhookTarget;
22
-
23
- const conversationKey = "wecom:default:U:C";
24
-
25
- const msg1 = { msgid: "M1" } satisfies WecomInboundMessage;
26
- const msg2 = { msgid: "M2" } satisfies WecomInboundMessage;
27
-
28
- const r1 = store.addPendingMessage({
29
- conversationKey,
30
- target,
31
- msg: msg1,
32
- msgContent: "1",
33
- nonce: "n",
34
- timestamp: "t",
35
- debounceMs: 10,
36
- });
37
- const r2 = store.addPendingMessage({
38
- conversationKey,
39
- target,
40
- msg: msg2,
41
- msgContent: "2",
42
- nonce: "n",
43
- timestamp: "t",
44
- debounceMs: 10,
45
- });
46
-
47
- expect(r1.status).toBe("active_new");
48
- // 初始批次不接收合并:第二条进入 queued
49
- expect(r2.status).toBe("queued_new");
50
- expect(r2.streamId).not.toBe(r1.streamId);
51
-
52
- // Follow-ups within queued should merge into queued (status queued_merged).
53
- const r3 = store.addPendingMessage({
54
- conversationKey,
55
- target,
56
- msg: { msgid: "M3" } as any,
57
- msgContent: "3",
58
- nonce: "n",
59
- timestamp: "t",
60
- debounceMs: 10,
61
- });
62
- expect(r3.status).toBe("queued_merged");
63
- expect(r3.streamId).toBe(r2.streamId);
64
-
65
- // Active batch flushes at debounce time.
66
- await vi.advanceTimersByTimeAsync(11);
67
- expect(flushed).toEqual([r1.streamId]);
68
-
69
- // Queued batch timer also fires, but cannot flush until active finishes.
70
- await vi.advanceTimersByTimeAsync(11);
71
- expect(flushed).toEqual([r1.streamId]);
72
-
73
- // Once the active stream finishes, queued batch is promoted and flushes immediately.
74
- store.onStreamFinished(r1.streamId);
75
- expect(flushed).toEqual([r1.streamId, r2.streamId]);
76
- } finally {
77
- vi.useRealTimers();
78
- }
79
- });
80
-
81
- test("merges into active batch when it has not started yet (even after promotion)", async () => {
82
- vi.useFakeTimers();
83
- try {
84
- const store = new StreamStore();
85
- const flushed: string[] = [];
86
- store.setFlushHandler((pending) => flushed.push(pending.streamId));
87
-
88
- const target = {
89
- account: {} as any,
90
- config: {} as any,
91
- runtime: {},
92
- core: {} as any,
93
- path: "/wecom",
94
- } satisfies WecomWebhookTarget;
95
-
96
- const conversationKey = "wecom:default:U:C2";
97
-
98
- // 1 becomes active and flushes; mark as started to simulate "processing started".
99
- const r1 = store.addPendingMessage({
100
- conversationKey,
101
- target,
102
- msg: { msgid: "M1" } as any,
103
- msgContent: "1",
104
- nonce: "n",
105
- timestamp: "t",
106
- debounceMs: 10,
107
- });
108
- store.markStarted(r1.streamId);
109
- await vi.advanceTimersByTimeAsync(11);
110
- expect(flushed).toEqual([r1.streamId]);
111
-
112
- // 2 enters queued with a longer debounce; it should NOT become readyToFlush yet.
113
- const r2 = store.addPendingMessage({
114
- conversationKey,
115
- target,
116
- msg: { msgid: "M2" } as any,
117
- msgContent: "2",
118
- nonce: "n",
119
- timestamp: "t",
120
- debounceMs: 100,
121
- });
122
- expect(flushed).toEqual([r1.streamId]);
123
-
124
- // Finish 1, promote 2 to active (but do NOT flush immediately since it's not readyToFlush).
125
- store.onStreamFinished(r1.streamId);
126
- expect(flushed).toEqual([r1.streamId]);
127
-
128
- // Now 2 is active, but (in real monitor) it may still be in debounce before markStarted.
129
- // We simulate that by NOT calling markStarted. Follow-up should merge into active (same streamId).
130
- const r3 = store.addPendingMessage({
131
- conversationKey,
132
- target,
133
- msg: { msgid: "M3" } as any,
134
- msgContent: "3",
135
- nonce: "n",
136
- timestamp: "t",
137
- debounceMs: 10,
138
- });
139
- expect(r3.streamId).toBe(r2.streamId);
140
- expect(r3.status).toBe("active_merged");
141
- } finally {
142
- vi.useRealTimers();
143
- }
144
- });
145
-
146
- test("clears conversation state when idle so next message becomes active", async () => {
147
- const store = new StreamStore();
148
- store.setFlushHandler(() => { });
149
-
150
- const target = {
151
- account: {} as any,
152
- config: {} as any,
153
- runtime: {},
154
- core: {} as any,
155
- path: "/wecom",
156
- } satisfies WecomWebhookTarget;
157
-
158
- const conversationKey = "wecom:default:U:idle";
159
-
160
- const r1 = store.addPendingMessage({
161
- conversationKey,
162
- target,
163
- msg: { msgid: "M1" } as any,
164
- msgContent: "1",
165
- nonce: "n",
166
- timestamp: "t",
167
- debounceMs: 10,
168
- });
169
- store.markStarted(r1.streamId);
170
- store.markFinished(r1.streamId);
171
- store.onStreamFinished(r1.streamId);
172
-
173
- const r2 = store.addPendingMessage({
174
- conversationKey,
175
- target,
176
- msg: { msgid: "M2" } as any,
177
- msgContent: "2",
178
- nonce: "n",
179
- timestamp: "t",
180
- debounceMs: 10,
181
- });
182
- expect(r2.status).toBe("active_new");
183
- expect(r2.streamId).not.toBe(r1.streamId);
184
- });
185
- });
@@ -1,34 +0,0 @@
1
- import { LegacyOperationalEventStore, type MonitorOperationalEvent } from "../observability/legacy-operational-event-store.js";
2
- import { ActiveReplyStore } from "../store/active-reply-store.js";
3
- import { StreamStore } from "../store/stream-batch-store.js";
4
- import { LIMITS } from "./limits.js";
5
-
6
- export { LIMITS, StreamStore, ActiveReplyStore };
7
- export type { MonitorOperationalEvent };
8
-
9
- class MonitorState {
10
- public readonly streamStore = new StreamStore();
11
- public readonly activeReplyStore = new ActiveReplyStore("multi");
12
- public readonly operationalEvents = new LegacyOperationalEventStore();
13
-
14
- private pruneInterval?: NodeJS.Timeout;
15
-
16
- public startPruning(intervalMs: number = 60_000): void {
17
- if (this.pruneInterval) return;
18
- this.pruneInterval = setInterval(() => {
19
- const now = Date.now();
20
- this.streamStore.prune(now);
21
- this.activeReplyStore.prune(now);
22
- this.operationalEvents.prune(now);
23
- }, intervalMs);
24
- }
25
-
26
- public stopPruning(): void {
27
- if (this.pruneInterval) {
28
- clearInterval(this.pruneInterval);
29
- this.pruneInterval = undefined;
30
- }
31
- }
32
- }
33
-
34
- export const monitorState = new MonitorState();