@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,365 +0,0 @@
1
- import {
2
- generateReqId,
3
- type WsFrame,
4
- type BaseMessage,
5
- type EventMessage,
6
- type WSClient,
7
- } from "@wecom/aibot-node-sdk";
8
- import { formatErrorMessage } from "openclaw/plugin-sdk/infra-runtime";
9
- import { resolveWecomMediaMaxBytes, resolveWecomMergedMediaLocalRoots } from "../../config/index.js";
10
- import { getWecomRuntime } from "../../runtime.js";
11
- import type { ReplyHandle, ReplyPayload } from "../../types/index.js";
12
- import { toWeComMarkdownV2 } from "../../wecom_msg_adapter/markdown_adapter.js";
13
- import { uploadAndSendBotWsMedia } from "./media.js";
14
-
15
- const PLACEHOLDER_KEEPALIVE_MS = 3000;
16
- const MAX_KEEPALIVE_MS = 120 * 1000; // Force stop keepalive after 120s if ignored
17
-
18
- function isInvalidReqIdError(error: unknown): boolean {
19
- if (!error || typeof error !== "object") {
20
- return false;
21
- }
22
- const errcode = "errcode" in error ? Number(error.errcode) : undefined;
23
- const errmsg = "errmsg" in error ? String(error.errmsg ?? "") : "";
24
- return errcode === 846605 || errmsg.includes("invalid req_id");
25
- }
26
-
27
- function isExpiredStreamUpdateError(error: unknown): boolean {
28
- if (!error || typeof error !== "object") {
29
- return false;
30
- }
31
- const errcode = "errcode" in error ? Number(error.errcode) : undefined;
32
- const errmsg = "errmsg" in error ? String(error.errmsg ?? "").toLowerCase() : "";
33
- return errcode === 846608 || errmsg.includes("stream message update expired");
34
- }
35
-
36
- /** SDK rejects with a plain Error whose message contains "ack timeout" when
37
- * the WeCom server does not acknowledge a reply within 5 s. Once timed out
38
- * the reqId slot is released; further replies on the same reqId will fail. */
39
- function isAckTimeoutError(error: unknown): boolean {
40
- return error instanceof Error && error.message.includes("ack timeout");
41
- }
42
-
43
- function isTerminalReplyError(error: unknown): boolean {
44
- return (
45
- isInvalidReqIdError(error) || isExpiredStreamUpdateError(error) || isAckTimeoutError(error)
46
- );
47
- }
48
-
49
- function formatMediaFailure(mediaUrl: string, error?: string, rejectReason?: string): string {
50
- const reason = rejectReason || error || "unknown";
51
- return `媒体发送失败:${mediaUrl} (${reason})`;
52
- }
53
-
54
- // Global registry to track active keepalives by peerId
55
- interface ActiveKeepalive {
56
- reqId: string;
57
- stop: () => void;
58
- }
59
- const activeKeepalivesByPeer = new Map<string, Set<ActiveKeepalive>>();
60
-
61
- export function createBotWsReplyHandle(params: {
62
- client: WSClient;
63
- frame: WsFrame<BaseMessage | EventMessage>;
64
- accountId: string;
65
- inboundKind: string;
66
- placeholderContent?: string;
67
- autoSendPlaceholder?: boolean;
68
- onDeliver?: () => void;
69
- onFail?: (error: unknown) => void;
70
- }): ReplyHandle {
71
- let streamId: string | undefined;
72
- let accumulatedText = "";
73
- let deferredMediaUrls: string[] = [];
74
- const resolveStreamId = () => {
75
- streamId ||= generateReqId("stream");
76
- return streamId;
77
- };
78
-
79
- const placeholderText = params.placeholderContent?.trim() || "⏳ 正在思考中...\n\n";
80
- let streamSettled = false;
81
- let placeholderInFlight = false;
82
- let placeholderKeepalive: ReturnType<typeof setInterval> | undefined;
83
- let placeholderTimeout: ReturnType<typeof setTimeout> | undefined;
84
-
85
- // Extract peerId for clustering handles
86
- const body = params.frame.body as any;
87
- const peerId = String(
88
- (body?.chattype === "group" ? body?.chatid || body?.from?.userid : body?.from?.userid) ||
89
- "unknown",
90
- );
91
- const reqId = params.frame.headers.req_id || "unknown";
92
-
93
- const isEvent =
94
- params.inboundKind === "welcome" ||
95
- params.inboundKind === "event" ||
96
- params.inboundKind === "template-card-event";
97
-
98
- const stopPlaceholderKeepalive = () => {
99
- if (placeholderKeepalive) {
100
- clearInterval(placeholderKeepalive);
101
- placeholderKeepalive = undefined;
102
- }
103
- if (placeholderTimeout) {
104
- clearTimeout(placeholderTimeout);
105
- placeholderTimeout = undefined;
106
- }
107
-
108
- // Remove from registry
109
- const keepalives = activeKeepalivesByPeer.get(peerId);
110
- if (keepalives) {
111
- for (const ka of keepalives) {
112
- if (ka.reqId === reqId) {
113
- keepalives.delete(ka);
114
- }
115
- }
116
- if (keepalives.size === 0) {
117
- activeKeepalivesByPeer.delete(peerId);
118
- }
119
- }
120
- };
121
-
122
- const settleStream = () => {
123
- if (streamSettled) return;
124
- streamSettled = true;
125
- stopPlaceholderKeepalive();
126
- };
127
-
128
- const sendPlaceholder = () => {
129
- if (streamSettled || placeholderInFlight || isEvent) return;
130
- placeholderInFlight = true;
131
- params.client
132
- .replyStream(params.frame, resolveStreamId(), placeholderText, false)
133
- .catch((error) => {
134
- if (!isTerminalReplyError(error)) {
135
- return;
136
- }
137
- settleStream();
138
- params.onFail?.(error);
139
- })
140
- .finally(() => {
141
- placeholderInFlight = false;
142
- });
143
- };
144
-
145
- const notifyPeerActive = () => {
146
- // A genuine reply or reasoning is happening on THIS handle.
147
- // It means the core SDK has chosen this handle to deliver the response.
148
- // We can safely terminate all other orphaned keepalives for this peer to prevent infinite loops.
149
- const keepalives = activeKeepalivesByPeer.get(peerId);
150
- if (keepalives) {
151
- for (const ka of keepalives) {
152
- if (ka.reqId !== reqId) {
153
- ka.stop();
154
- }
155
- }
156
- }
157
- };
158
-
159
- const mergeDeferredMediaUrls = (urls: string[]): string[] => {
160
- if (urls.length === 0) {
161
- return deferredMediaUrls;
162
- }
163
- const merged = [...deferredMediaUrls];
164
- for (const url of urls) {
165
- if (!merged.includes(url)) {
166
- merged.push(url);
167
- }
168
- }
169
- deferredMediaUrls = merged;
170
- return deferredMediaUrls;
171
- };
172
-
173
- if (params.autoSendPlaceholder !== false && !isEvent) {
174
- sendPlaceholder();
175
- placeholderKeepalive = setInterval(() => {
176
- sendPlaceholder();
177
- }, PLACEHOLDER_KEEPALIVE_MS);
178
-
179
- // Safety net: force stop keepalive after MAX_KEEPALIVE_MS
180
- // in case the message is completely ignored by the core and never triggers deliver/fail
181
- placeholderTimeout = setTimeout(() => {
182
- stopPlaceholderKeepalive();
183
- }, MAX_KEEPALIVE_MS);
184
-
185
- // Register keepalive
186
- let keepalives = activeKeepalivesByPeer.get(peerId);
187
- if (!keepalives) {
188
- keepalives = new Set();
189
- activeKeepalivesByPeer.set(peerId, keepalives);
190
- }
191
- keepalives.add({ reqId, stop: stopPlaceholderKeepalive });
192
- }
193
-
194
- return {
195
- context: {
196
- transport: "bot-ws",
197
- accountId: params.accountId,
198
- reqId: params.frame.headers.req_id,
199
- raw: {
200
- transport: "bot-ws",
201
- command: params.frame.cmd,
202
- headers: params.frame.headers,
203
- body: params.frame.body,
204
- envelopeType: "ws",
205
- },
206
- },
207
- deliver: async (payload: ReplyPayload, info) => {
208
- // Mark this chat as active on this handle
209
- notifyPeerActive();
210
-
211
- if (payload.isReasoning) {
212
- // We reset the safety timeout if reasoning is actively streaming
213
- if (placeholderTimeout && !isEvent) {
214
- clearTimeout(placeholderTimeout);
215
- placeholderTimeout = setTimeout(() => {
216
- stopPlaceholderKeepalive();
217
- }, MAX_KEEPALIVE_MS);
218
- }
219
- return;
220
- }
221
-
222
- const text = payload.text?.trim() || "";
223
- const incomingMediaUrls = payload.mediaUrls || (payload.mediaUrl ? [payload.mediaUrl] : []);
224
- const hasIncomingMedia = incomingMediaUrls.length > 0;
225
- if (info.kind !== "final" && hasIncomingMedia) {
226
- mergeDeferredMediaUrls(incomingMediaUrls);
227
- }
228
- const mediaUrls =
229
- info.kind === "final" ? mergeDeferredMediaUrls(incomingMediaUrls) : incomingMediaUrls;
230
- if (!text && mediaUrls.length === 0) {
231
- return;
232
- }
233
-
234
- if (info.kind === "block") {
235
- if (!text) {
236
- return;
237
- }
238
- accumulatedText = accumulatedText ? `${accumulatedText}\n${text}` : text;
239
- }
240
-
241
- const outboundText =
242
- info.kind === "final"
243
- ? accumulatedText
244
- ? text
245
- ? `${accumulatedText}\n${text}`
246
- : accumulatedText
247
- : text
248
- : accumulatedText || text;
249
-
250
- let finalText = outboundText;
251
- if (info.kind === "final" && mediaUrls.length > 0) {
252
- const cfg = getWecomRuntime().config.loadConfig();
253
- const mediaLocalRoots = resolveWecomMergedMediaLocalRoots({ cfg });
254
- const mediaMaxBytes = resolveWecomMediaMaxBytes(cfg, params.accountId);
255
- const mediaFailures: string[] = [];
256
- const mediaNotes: string[] = [];
257
- let mediaSent = 0;
258
- for (const mediaUrl of mediaUrls) {
259
- const result = await uploadAndSendBotWsMedia({
260
- wsClient: params.client,
261
- chatId: peerId,
262
- mediaUrl,
263
- mediaLocalRoots,
264
- maxBytes: mediaMaxBytes,
265
- });
266
- if (result.ok) {
267
- mediaSent += 1;
268
- if (result.downgradeNote) {
269
- mediaNotes.push(result.downgradeNote);
270
- }
271
- continue;
272
- }
273
- mediaFailures.push(formatMediaFailure(mediaUrl, result.error, result.rejectReason));
274
- }
275
-
276
- if (!finalText && mediaSent > 0) {
277
- finalText = "文件已发送。";
278
- }
279
- if (mediaFailures.length > 0) {
280
- finalText = finalText
281
- ? `${finalText}\n\n${mediaFailures.join("\n")}`
282
- : mediaFailures.join("\n");
283
- }
284
- if (mediaNotes.length > 0) {
285
- finalText = finalText
286
- ? `${finalText}\n\n${mediaNotes.join("\n")}`
287
- : mediaNotes.join("\n");
288
- }
289
- deferredMediaUrls = [];
290
- }
291
- if (!finalText) {
292
- return;
293
- }
294
-
295
- // Event frames do not support streaming chunks
296
- if (isEvent && info.kind !== "final") {
297
- return;
298
- }
299
-
300
- settleStream();
301
- try {
302
- if (params.inboundKind === "welcome") {
303
- await params.client.replyWelcome(params.frame, {
304
- msgtype: "text",
305
- text: { content: finalText },
306
- });
307
- } else if (isEvent) {
308
- // Send push message for other events
309
- await params.client.sendMessage(peerId, {
310
- msgtype: "markdown",
311
- markdown: { content: toWeComMarkdownV2(finalText) },
312
- });
313
- } else {
314
- await params.client.replyStream(
315
- params.frame,
316
- resolveStreamId(),
317
- toWeComMarkdownV2(finalText),
318
- info.kind === "final",
319
- );
320
- }
321
- } catch (error) {
322
- if (isTerminalReplyError(error)) {
323
- params.onFail?.(error);
324
- return;
325
- }
326
- throw error;
327
- }
328
- params.onDeliver?.();
329
- },
330
- fail: async (error: unknown) => {
331
- notifyPeerActive();
332
- settleStream();
333
- if (isTerminalReplyError(error)) {
334
- params.onFail?.(error);
335
- return;
336
- }
337
- const message = formatErrorMessage(error);
338
- const text = `WeCom WS reply failed: ${message}`;
339
-
340
- try {
341
- if (params.inboundKind === "welcome") {
342
- await params.client.replyWelcome(params.frame, {
343
- msgtype: "text",
344
- text: { content: text },
345
- });
346
- } else if (isEvent) {
347
- await params.client.sendMessage(peerId, {
348
- msgtype: "markdown",
349
- markdown: { content: text },
350
- });
351
- } else {
352
- await params.client.replyStream(params.frame, resolveStreamId(), text, true);
353
- }
354
- } catch (sendError) {
355
- params.onFail?.(sendError);
356
- return;
357
- }
358
- params.onFail?.(error);
359
- },
360
- markExternalActivity: () => {
361
- notifyPeerActive();
362
- stopPlaceholderKeepalive();
363
- },
364
- };
365
- }
@@ -1,187 +0,0 @@
1
- import { afterEach, describe, expect, it, vi } from "vitest";
2
-
3
- const sdkMockState = vi.hoisted(() => {
4
- class MockWSClient {
5
- readonly handlers = new Map<string, Array<(payload: any) => void>>();
6
- readonly isConnected = true;
7
- readonly replyStream = vi.fn().mockResolvedValue(undefined);
8
- readonly replyWelcome = vi.fn().mockResolvedValue(undefined);
9
-
10
- constructor(_options: unknown) {
11
- sdkMockState.client = this;
12
- }
13
-
14
- on(event: string, handler: (payload: any) => void): void {
15
- const current = this.handlers.get(event) ?? [];
16
- current.push(handler);
17
- this.handlers.set(event, current);
18
- }
19
-
20
- emit(event: string, payload: any): void {
21
- for (const handler of this.handlers.get(event) ?? []) {
22
- handler(payload);
23
- }
24
- }
25
-
26
- connect(): void {}
27
-
28
- disconnect(): void {}
29
- }
30
-
31
- return {
32
- client: null as InstanceType<typeof MockWSClient> | null,
33
- MockWSClient,
34
- };
35
- });
36
-
37
- vi.mock("@wecom/aibot-node-sdk", () => ({
38
- default: {
39
- WSClient: sdkMockState.MockWSClient,
40
- },
41
- WSClient: sdkMockState.MockWSClient,
42
- generateReqId: (prefix: string) => `${prefix}-1`,
43
- }));
44
-
45
- import { BotWsSdkAdapter } from "./sdk-adapter.js";
46
-
47
- const waitForAsyncCallbacks = async () => {
48
- await Promise.resolve();
49
- await new Promise((resolve) => setTimeout(resolve, 0));
50
- };
51
-
52
- describe("BotWsSdkAdapter", () => {
53
- const unhandledRejections: unknown[] = [];
54
- const onUnhandledRejection = (reason: unknown) => {
55
- unhandledRejections.push(reason);
56
- };
57
-
58
- afterEach(() => {
59
- process.off("unhandledRejection", onUnhandledRejection);
60
- unhandledRejections.length = 0;
61
- sdkMockState.client = null;
62
- });
63
-
64
- it("contains frame handler rejections instead of leaking unhandled rejections", async () => {
65
- process.on("unhandledRejection", onUnhandledRejection);
66
-
67
- const runtime = {
68
- account: {
69
- accountId: "acc-1",
70
- bot: {
71
- wsConfigured: true,
72
- ws: {
73
- botId: "bot-1",
74
- secret: "secret-1",
75
- },
76
- config: {},
77
- },
78
- },
79
- handleEvent: vi.fn().mockRejectedValue(new Error("frame exploded")),
80
- updateTransportSession: vi.fn(),
81
- touchTransportSession: vi.fn(),
82
- recordOperationalIssue: vi.fn(),
83
- };
84
- const log = {
85
- info: vi.fn(),
86
- warn: vi.fn(),
87
- error: vi.fn(),
88
- };
89
-
90
- new BotWsSdkAdapter(runtime as any, log as any).start();
91
-
92
- sdkMockState.client?.emit("message", {
93
- cmd: "aibot_msg_callback",
94
- headers: { req_id: "req-1" },
95
- body: {
96
- msgid: "msg-1",
97
- msgtype: "text",
98
- from: { userid: "user-1" },
99
- text: { content: "hello" },
100
- },
101
- });
102
-
103
- await waitForAsyncCallbacks();
104
-
105
- expect(runtime.handleEvent).toHaveBeenCalledTimes(1);
106
- expect(runtime.recordOperationalIssue).toHaveBeenCalledWith(
107
- expect.objectContaining({
108
- transport: "bot-ws",
109
- category: "runtime-error",
110
- messageId: "msg-1",
111
- error: "frame exploded",
112
- }),
113
- );
114
- expect(runtime.touchTransportSession).toHaveBeenCalledWith(
115
- "bot-ws",
116
- expect.objectContaining({
117
- lastError: "frame exploded",
118
- }),
119
- );
120
- expect(log.error).toHaveBeenCalledWith(
121
- expect.stringContaining(
122
- "frame handler failed account=acc-1 reqId=req-1 message=frame exploded",
123
- ),
124
- );
125
- expect(unhandledRejections).toHaveLength(0);
126
- });
127
-
128
- it("short-circuits enter_chat welcome events to a static ws welcome reply", async () => {
129
- process.on("unhandledRejection", onUnhandledRejection);
130
-
131
- const runtime = {
132
- account: {
133
- accountId: "acc-1",
134
- bot: {
135
- wsConfigured: true,
136
- ws: {
137
- botId: "bot-1",
138
- secret: "secret-1",
139
- },
140
- config: {
141
- welcomeText: "欢迎来到 WeCom",
142
- },
143
- },
144
- },
145
- handleEvent: vi.fn().mockResolvedValue(undefined),
146
- updateTransportSession: vi.fn(),
147
- touchTransportSession: vi.fn(),
148
- recordOperationalIssue: vi.fn(),
149
- };
150
- const log = {
151
- info: vi.fn(),
152
- warn: vi.fn(),
153
- error: vi.fn(),
154
- };
155
-
156
- new BotWsSdkAdapter(runtime as any, log as any).start();
157
-
158
- sdkMockState.client?.emit("event", {
159
- cmd: "aibot_event_callback",
160
- headers: { req_id: "req-welcome" },
161
- body: {
162
- msgid: "msg-welcome",
163
- msgtype: "event",
164
- chattype: "single",
165
- from: { userid: "user-1" },
166
- event: { eventtype: "enter_chat" },
167
- },
168
- });
169
-
170
- await waitForAsyncCallbacks();
171
-
172
- expect(runtime.handleEvent).not.toHaveBeenCalled();
173
- expect(sdkMockState.client?.replyWelcome).toHaveBeenCalledWith(
174
- expect.objectContaining({
175
- headers: { req_id: "req-welcome" },
176
- }),
177
- {
178
- msgtype: "text",
179
- text: { content: "欢迎来到 WeCom" },
180
- },
181
- );
182
- expect(log.info).toHaveBeenCalledWith(
183
- expect.stringContaining("static welcome delivered account=acc-1 messageId=msg-welcome"),
184
- );
185
- expect(unhandledRejections).toHaveLength(0);
186
- });
187
- });