@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
@@ -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,145 +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 === "TimeoutError") {
96
- console.error(
97
- `[wecom-http] timeout method=${method} target=${target} durationMs=${Date.now() - startedAt} proxy=${proxyUrl || "none"}`,
98
- );
99
- } else if (err instanceof Error && err.name === "TypeError" && err.message === "fetch failed") {
100
- const cause = (err as any).cause;
101
- console.error(
102
- `[wecom-http] fetch failed method=${method} target=${target} durationMs=${Date.now() - startedAt} proxy=${proxyUrl || "none"}${cause ? ` cause=${String(cause)}` : ""}`,
103
- );
104
- } else if (err instanceof Error && err.name === "AbortError") {
105
- console.error(
106
- `[wecom-http] aborted method=${method} target=${target} durationMs=${Date.now() - startedAt} proxy=${proxyUrl || "none"}`,
107
- );
108
- }
109
- throw err;
110
- }
111
- }
112
-
113
- /**
114
- * **readResponseBodyAsBuffer (读取响应 Body)**
115
- *
116
- * 将 Response Body 读取为 Buffer,支持最大字节限制以防止内存溢出。
117
- * 适用于下载媒体文件等场景。
118
- */
119
- export async function readResponseBodyAsBuffer(res: Response, maxBytes?: number): Promise<Buffer> {
120
- if (!res.body) return Buffer.alloc(0);
121
-
122
- const limit = maxBytes && Number.isFinite(maxBytes) && maxBytes > 0 ? maxBytes : undefined;
123
- const chunks: Uint8Array[] = [];
124
- let total = 0;
125
-
126
- const reader = res.body.getReader();
127
- while (true) {
128
- const { done, value } = await reader.read();
129
- if (done) break;
130
- if (!value) continue;
131
-
132
- total += value.byteLength;
133
- if (limit && total > limit) {
134
- try {
135
- await reader.cancel("body too large");
136
- } catch {
137
- // ignore
138
- }
139
- throw new Error(`response body too large (>${limit} bytes)`);
140
- }
141
- chunks.push(value);
142
- }
143
-
144
- return Buffer.concat(chunks.map((c) => Buffer.from(c)));
145
- }
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();