@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,463 +0,0 @@
1
- import crypto from "node:crypto";
2
-
3
- import { resolveWecomEgressProxyUrlFromNetwork } from "../../config/index.js";
4
- import { readResponseBodyAsBuffer, wecomFetch } from "../../http.js";
5
- import { API_ENDPOINTS, LIMITS } from "../../types/constants.js";
6
- import type { ResolvedAgentAccount } from "../../types/index.js";
7
-
8
- type TokenCache = {
9
- token: string;
10
- expiresAt: number;
11
- refreshPromise: Promise<string> | null;
12
- };
13
-
14
- const tokenCaches = new Map<string, TokenCache>();
15
-
16
- function truncateForLog(raw: string, maxChars = 180): string {
17
- const compact = raw.replace(/\s+/g, " ").trim();
18
- if (compact.length <= maxChars) return compact;
19
- return `${compact.slice(0, maxChars)}...(truncated)`;
20
- }
21
-
22
- export function normalizeUploadFilename(filename: string): string {
23
- const trimmed = filename.trim();
24
- if (!trimmed) return "file.bin";
25
- const ext = trimmed.includes(".") ? `.${trimmed.split(".").pop()!.toLowerCase()}` : "";
26
- const base = ext ? trimmed.slice(0, -ext.length) : trimmed;
27
- const sanitizedBase = base
28
- .replace(/[^\x20-\x7e]/g, "_")
29
- .replace(/["\\/;=]/g, "_")
30
- .replace(/\s+/g, "_")
31
- .replace(/_+/g, "_")
32
- .replace(/^_+|_+$/g, "");
33
- const safeBase = sanitizedBase || "file";
34
- const safeExt = ext.replace(/[^a-z0-9.]/g, "");
35
- return `${safeBase}${safeExt || ".bin"}`;
36
- }
37
-
38
- export function guessUploadContentType(filename: string): string {
39
- const ext = filename.split(".").pop()?.toLowerCase() || "";
40
- const contentTypeMap: Record<string, string> = {
41
- jpg: "image/jpg",
42
- jpeg: "image/jpeg",
43
- png: "image/png",
44
- gif: "image/gif",
45
- webp: "image/webp",
46
- bmp: "image/bmp",
47
- amr: "voice/amr",
48
- mp3: "audio/mpeg",
49
- wav: "audio/wav",
50
- m4a: "audio/mp4",
51
- ogg: "audio/ogg",
52
- mp4: "video/mp4",
53
- mov: "video/quicktime",
54
- txt: "text/plain",
55
- md: "text/markdown",
56
- csv: "text/csv",
57
- tsv: "text/tab-separated-values",
58
- json: "application/json",
59
- xml: "application/xml",
60
- yaml: "application/yaml",
61
- yml: "application/yaml",
62
- pdf: "application/pdf",
63
- doc: "application/msword",
64
- docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
65
- xls: "application/vnd.ms-excel",
66
- xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
67
- ppt: "application/vnd.ms-powerpoint",
68
- pptx: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
69
- rtf: "application/rtf",
70
- odt: "application/vnd.oasis.opendocument.text",
71
- zip: "application/zip",
72
- rar: "application/vnd.rar",
73
- "7z": "application/x-7z-compressed",
74
- gz: "application/gzip",
75
- tgz: "application/gzip",
76
- tar: "application/x-tar",
77
- };
78
- return contentTypeMap[ext] || "application/octet-stream";
79
- }
80
-
81
- function requireAgentId(agent: ResolvedAgentAccount): number {
82
- if (typeof agent.agentId === "number" && Number.isFinite(agent.agentId)) return agent.agentId;
83
- throw new Error(`wecom agent account=${agent.accountId} missing agentId; sending via cgi-bin/message/send requires agentId`);
84
- }
85
-
86
- /**
87
- * 获取主企业的 access_token
88
- * 使用 corpid + corpsecret
89
- */
90
- export async function getAccessToken(agent: ResolvedAgentAccount): Promise<string> {
91
- const cacheKey = `${agent.corpId}:${String(agent.agentId ?? "na")}`;
92
- let cache = tokenCaches.get(cacheKey);
93
-
94
- if (!cache) {
95
- cache = { token: "", expiresAt: 0, refreshPromise: null };
96
- tokenCaches.set(cacheKey, cache);
97
- }
98
-
99
- const now = Date.now();
100
- if (cache.token && cache.expiresAt > now + LIMITS.TOKEN_REFRESH_BUFFER_MS) {
101
- return cache.token;
102
- }
103
-
104
- if (cache.refreshPromise) {
105
- return cache.refreshPromise;
106
- }
107
-
108
- cache.refreshPromise = (async () => {
109
- try {
110
- const url = `${API_ENDPOINTS.GET_TOKEN}?corpid=${encodeURIComponent(agent.corpId)}&corpsecret=${encodeURIComponent(agent.corpSecret)}`;
111
-
112
- const res = await wecomFetch(url, undefined, {
113
- proxyUrl: resolveWecomEgressProxyUrlFromNetwork(agent.network),
114
- timeoutMs: LIMITS.REQUEST_TIMEOUT_MS,
115
- });
116
- const json = (await res.json()) as { access_token?: string; expires_in?: number; errcode?: number; errmsg?: string };
117
-
118
- if (!json?.access_token) {
119
- throw new Error(`gettoken failed: ${json?.errcode} ${json?.errmsg}`);
120
- }
121
-
122
- cache!.token = json.access_token;
123
- cache!.expiresAt = Date.now() + (json.expires_in ?? 7200) * 1000;
124
- return cache!.token;
125
- } finally {
126
- cache!.refreshPromise = null;
127
- }
128
- })();
129
-
130
- return cache.refreshPromise;
131
- }
132
-
133
- /**
134
- * 获取下游企业的 access_token
135
- *
136
- * 根据企业微信文档:https://developer.work.weixin.qq.com/document/path/95816
137
- *
138
- * 请求方式:POST(HTTPS)
139
- * 请求地址:https://qyapi.weixin.qq.com/cgi-bin/corpgroup/corp/gettoken?access_token=ACCESS_TOKEN
140
- *
141
- * 请求体:
142
- * {
143
- * "corpid": "下游企业corpid",
144
- * "business_type": 1, // 1 表示上下游企业
145
- * "agentid": 下游企业应用ID
146
- * }
147
- *
148
- * 注意:需要使用上游企业的 access_token 作为调用凭证
149
- */
150
- export async function getUpstreamAccessToken(params: {
151
- primaryAgent: ResolvedAgentAccount;
152
- upstreamCorpId: string;
153
- upstreamAgentId: number;
154
- }): Promise<string> {
155
- const { primaryAgent, upstreamCorpId, upstreamAgentId } = params;
156
-
157
- // 缓存 key 增加 primaryCorpId 维度,避免多主企业之间碰撞
158
- const cacheKey = `upstream:${primaryAgent.corpId}:${upstreamCorpId}:${upstreamAgentId}`;
159
- let cache = tokenCaches.get(cacheKey);
160
-
161
- if (!cache) {
162
- cache = { token: "", expiresAt: 0, refreshPromise: null };
163
- tokenCaches.set(cacheKey, cache);
164
- }
165
-
166
- const now = Date.now();
167
- if (cache.token && cache.expiresAt > now + LIMITS.TOKEN_REFRESH_BUFFER_MS) {
168
- return cache.token;
169
- }
170
-
171
- if (cache.refreshPromise) {
172
- return cache.refreshPromise;
173
- }
174
-
175
- cache.refreshPromise = (async () => {
176
- try {
177
- // 1. 先获取上游企业的 access_token
178
- const primaryToken = await getAccessToken(primaryAgent);
179
-
180
- // 2. 调用 corpgroup/corp/gettoken 获取下游企业的 access_token
181
- const url = `https://qyapi.weixin.qq.com/cgi-bin/corpgroup/corp/gettoken?access_token=${encodeURIComponent(primaryToken)}`;
182
-
183
- const requestBody = {
184
- corpid: upstreamCorpId,
185
- business_type: 1, // 1 表示上下游企业
186
- agentid: upstreamAgentId,
187
- };
188
-
189
- const res = await wecomFetch(
190
- url,
191
- {
192
- method: "POST",
193
- headers: { "Content-Type": "application/json" },
194
- body: JSON.stringify(requestBody),
195
- },
196
- {
197
- proxyUrl: resolveWecomEgressProxyUrlFromNetwork(primaryAgent.network),
198
- timeoutMs: LIMITS.REQUEST_TIMEOUT_MS,
199
- },
200
- );
201
-
202
- const json = (await res.json()) as {
203
- access_token?: string;
204
- expires_in?: number;
205
- errcode?: number;
206
- errmsg?: string
207
- };
208
-
209
- if (!json?.access_token) {
210
- throw new Error(`get upstream token failed: ${json?.errcode} ${json?.errmsg}`);
211
- }
212
-
213
- cache!.token = json.access_token;
214
- cache!.expiresAt = Date.now() + (json.expires_in ?? 7200) * 1000;
215
- return cache!.token;
216
- } finally {
217
- cache!.refreshPromise = null;
218
- }
219
- })();
220
-
221
- return cache.refreshPromise;
222
- }
223
-
224
- export async function sendText(params: {
225
- agent: ResolvedAgentAccount;
226
- toUser?: string;
227
- toParty?: string;
228
- toTag?: string;
229
- chatId?: string;
230
- text: string;
231
- }): Promise<void> {
232
- const { agent, toUser, toParty, toTag, chatId, text } = params;
233
- console.log(
234
- `[wecom-agent-api] sendText request account=${agent.accountId} agentId=${String(agent.agentId ?? "N/A")} corpId=${agent.corpId} ` +
235
- `toUser=${toUser ?? ""} toParty=${toParty ?? ""} toTag=${toTag ?? ""} chatId=${chatId ?? ""} ` +
236
- `textLen=${text.length} textPreview=${JSON.stringify(truncateForLog(text))}`,
237
- );
238
- const token = await getAccessToken(agent);
239
-
240
- const useChat = Boolean(chatId);
241
- const url = useChat
242
- ? `${API_ENDPOINTS.SEND_APPCHAT}?access_token=${encodeURIComponent(token)}`
243
- : `${API_ENDPOINTS.SEND_MESSAGE}?access_token=${encodeURIComponent(token)}`;
244
-
245
- const body = useChat
246
- ? { chatid: chatId, msgtype: "text", text: { content: text } }
247
- : {
248
- touser: toUser,
249
- toparty: toParty,
250
- totag: toTag,
251
- msgtype: "text",
252
- agentid: requireAgentId(agent),
253
- text: { content: text },
254
- };
255
-
256
- const res = await wecomFetch(
257
- url,
258
- {
259
- method: "POST",
260
- headers: { "Content-Type": "application/json" },
261
- body: JSON.stringify(body),
262
- },
263
- { proxyUrl: resolveWecomEgressProxyUrlFromNetwork(agent.network), timeoutMs: LIMITS.REQUEST_TIMEOUT_MS },
264
- );
265
- const json = (await res.json()) as {
266
- errcode?: number;
267
- errmsg?: string;
268
- invaliduser?: string;
269
- invalidparty?: string;
270
- invalidtag?: string;
271
- };
272
-
273
- console.log(
274
- `[wecom-agent-api] sendText response account=${agent.accountId} agentId=${String(agent.agentId ?? "N/A")} corpId=${agent.corpId} ` +
275
- `toUser=${toUser ?? ""} toParty=${toParty ?? ""} toTag=${toTag ?? ""} chatId=${chatId ?? ""} ` +
276
- `errcode=${String(json?.errcode ?? "N/A")} errmsg=${json?.errmsg ?? ""} ` +
277
- `invaliduser=${json?.invaliduser ?? ""} invalidparty=${json?.invalidparty ?? ""} invalidtag=${json?.invalidtag ?? ""}`,
278
- );
279
-
280
- if (json?.errcode !== 0) {
281
- throw new Error(`send failed: ${json?.errcode} ${json?.errmsg}`);
282
- }
283
-
284
- if (json?.invaliduser || json?.invalidparty || json?.invalidtag) {
285
- const details = [
286
- json.invaliduser ? `invaliduser=${json.invaliduser}` : "",
287
- json.invalidparty ? `invalidparty=${json.invalidparty}` : "",
288
- json.invalidtag ? `invalidtag=${json.invalidtag}` : "",
289
- ]
290
- .filter(Boolean)
291
- .join(", ");
292
- throw new Error(`send partial failure: ${details}`);
293
- }
294
- }
295
-
296
- export async function uploadMedia(params: {
297
- agent: ResolvedAgentAccount;
298
- type: "image" | "voice" | "video" | "file";
299
- buffer: Buffer;
300
- filename: string;
301
- }): Promise<string> {
302
- const { agent, type, buffer, filename } = params;
303
- const safeFilename = normalizeUploadFilename(filename);
304
- const token = await getAccessToken(agent);
305
- const proxyUrl = resolveWecomEgressProxyUrlFromNetwork(agent.network);
306
- const url = `${API_ENDPOINTS.UPLOAD_MEDIA}?access_token=${encodeURIComponent(token)}&type=${encodeURIComponent(type)}&debug=1`;
307
-
308
- console.log(`[wecom-upload] Uploading media: type=${type}, filename=${safeFilename}, size=${buffer.length} bytes, corpId=${agent.corpId}`);
309
-
310
- const uploadOnce = async (fileContentType: string) => {
311
- const boundary = `----WebKitFormBoundary${crypto.randomBytes(16).toString("hex")}`;
312
- const header = Buffer.from(
313
- `--${boundary}\r\n` +
314
- `Content-Disposition: form-data; name="media"; filename="${safeFilename}"; filelength=${buffer.length}\r\n` +
315
- `Content-Type: ${fileContentType}\r\n\r\n`,
316
- );
317
- const footer = Buffer.from(`\r\n--${boundary}--\r\n`);
318
- const body = Buffer.concat([header, buffer, footer]);
319
-
320
- console.log(`[wecom-upload] Multipart body size=${body.length}, boundary=${boundary}, fileContentType=${fileContentType}`);
321
-
322
- const res = await wecomFetch(
323
- url,
324
- {
325
- method: "POST",
326
- headers: {
327
- "Content-Type": `multipart/form-data; boundary=${boundary}`,
328
- "Content-Length": String(body.length),
329
- },
330
- body,
331
- },
332
- { proxyUrl, timeoutMs: LIMITS.REQUEST_TIMEOUT_MS },
333
- );
334
- const json = (await res.json()) as { media_id?: string; errcode?: number; errmsg?: string };
335
- console.log(`[wecom-upload] Response:`, JSON.stringify(json));
336
- return json;
337
- };
338
-
339
- const preferredContentType = guessUploadContentType(safeFilename);
340
- let json = await uploadOnce(preferredContentType);
341
-
342
- if (!json?.media_id && preferredContentType !== "application/octet-stream") {
343
- console.warn(
344
- `[wecom-upload] Upload failed with ${preferredContentType}, retrying as application/octet-stream: ${json?.errcode} ${json?.errmsg}`,
345
- );
346
- json = await uploadOnce("application/octet-stream");
347
- }
348
-
349
- if (!json?.media_id) {
350
- throw new Error(`upload failed: ${json?.errcode} ${json?.errmsg}`);
351
- }
352
- return json.media_id;
353
- }
354
-
355
- export async function sendMedia(params: {
356
- agent: ResolvedAgentAccount;
357
- toUser?: string;
358
- toParty?: string;
359
- toTag?: string;
360
- chatId?: string;
361
- mediaId: string;
362
- mediaType: "image" | "voice" | "video" | "file";
363
- title?: string;
364
- description?: string;
365
- }): Promise<void> {
366
- const { agent, toUser, toParty, toTag, chatId, mediaId, mediaType, title, description } = params;
367
- const token = await getAccessToken(agent);
368
-
369
- const useChat = Boolean(chatId);
370
- const url = useChat
371
- ? `${API_ENDPOINTS.SEND_APPCHAT}?access_token=${encodeURIComponent(token)}`
372
- : `${API_ENDPOINTS.SEND_MESSAGE}?access_token=${encodeURIComponent(token)}`;
373
-
374
- const mediaPayload = mediaType === "video" ? { media_id: mediaId, title: title ?? "Video", description: description ?? "" } : { media_id: mediaId };
375
- const body = useChat
376
- ? { chatid: chatId, msgtype: mediaType, [mediaType]: mediaPayload }
377
- : {
378
- touser: toUser,
379
- toparty: toParty,
380
- totag: toTag,
381
- msgtype: mediaType,
382
- agentid: requireAgentId(agent),
383
- [mediaType]: mediaPayload,
384
- };
385
-
386
- const res = await wecomFetch(
387
- url,
388
- {
389
- method: "POST",
390
- headers: { "Content-Type": "application/json" },
391
- body: JSON.stringify(body),
392
- },
393
- { proxyUrl: resolveWecomEgressProxyUrlFromNetwork(agent.network), timeoutMs: LIMITS.REQUEST_TIMEOUT_MS },
394
- );
395
- const json = (await res.json()) as {
396
- errcode?: number;
397
- errmsg?: string;
398
- invaliduser?: string;
399
- invalidparty?: string;
400
- invalidtag?: string;
401
- };
402
-
403
- if (json?.errcode !== 0) {
404
- throw new Error(`send ${mediaType} failed: ${json?.errcode} ${json?.errmsg}`);
405
- }
406
-
407
- if (json?.invaliduser || json?.invalidparty || json?.invalidtag) {
408
- const details = [
409
- json.invaliduser ? `invaliduser=${json.invaliduser}` : "",
410
- json.invalidparty ? `invalidparty=${json.invalidparty}` : "",
411
- json.invalidtag ? `invalidtag=${json.invalidtag}` : "",
412
- ]
413
- .filter(Boolean)
414
- .join(", ");
415
- throw new Error(`send ${mediaType} partial failure: ${details}`);
416
- }
417
- }
418
-
419
- export async function downloadMedia(params: {
420
- agent: ResolvedAgentAccount;
421
- mediaId: string;
422
- maxBytes?: number;
423
- }): Promise<{ buffer: Buffer; contentType: string; filename?: string }> {
424
- const { agent, mediaId } = params;
425
- const token = await getAccessToken(agent);
426
- const url = `${API_ENDPOINTS.DOWNLOAD_MEDIA}?access_token=${encodeURIComponent(token)}&media_id=${encodeURIComponent(mediaId)}`;
427
-
428
- const res = await wecomFetch(url, undefined, {
429
- proxyUrl: resolveWecomEgressProxyUrlFromNetwork(agent.network),
430
- timeoutMs: LIMITS.REQUEST_TIMEOUT_MS,
431
- });
432
-
433
- if (!res.ok) {
434
- throw new Error(`download failed: ${res.status}`);
435
- }
436
-
437
- const contentType = res.headers.get("content-type") || "application/octet-stream";
438
- const disposition = res.headers.get("content-disposition") || "";
439
- const filename = (() => {
440
- const mStar = disposition.match(/filename\*\s*=\s*([^;]+)/i);
441
- if (mStar) {
442
- const raw = mStar[1]!.trim().replace(/^"(.*)"$/, "$1");
443
- const parts = raw.split("''");
444
- const encoded = parts.length === 2 ? parts[1]! : raw;
445
- try {
446
- return decodeURIComponent(encoded);
447
- } catch {
448
- return encoded;
449
- }
450
- }
451
- const m = disposition.match(/filename\s*=\s*([^;]+)/i);
452
- if (!m) return undefined;
453
- return m[1]!.trim().replace(/^"(.*)"$/, "$1") || undefined;
454
- })();
455
-
456
- if (contentType.includes("application/json")) {
457
- const json = (await res.json()) as { errcode?: number; errmsg?: string };
458
- throw new Error(`download failed: ${json?.errcode} ${json?.errmsg}`);
459
- }
460
-
461
- const buffer = await readResponseBodyAsBuffer(res, params.maxBytes);
462
- return { buffer, contentType, filename };
463
- }
@@ -1,41 +0,0 @@
1
- import type { ResolvedAgentAccount } from "../../types/index.js";
2
- import type { WecomTarget } from "../../target.js";
3
- import { sendAgentApiMediaReply, sendAgentApiTextReply } from "./reply.js";
4
- import { uploadAgentApiMedia } from "./media-upload.js";
5
-
6
- export async function deliverAgentApiText(params: {
7
- agent: ResolvedAgentAccount;
8
- target: WecomTarget;
9
- text: string;
10
- }): Promise<void> {
11
- await sendAgentApiTextReply(params);
12
- }
13
-
14
- export async function deliverAgentApiMedia(params: {
15
- agent: ResolvedAgentAccount;
16
- target: WecomTarget;
17
- buffer: Buffer;
18
- filename: string;
19
- contentType: string;
20
- text?: string;
21
- }): Promise<void> {
22
- let mediaType: "image" | "voice" | "video" | "file" = "file";
23
- if (params.contentType.startsWith("image/")) mediaType = "image";
24
- else if (params.contentType.startsWith("audio/")) mediaType = "voice";
25
- else if (params.contentType.startsWith("video/")) mediaType = "video";
26
-
27
- const mediaId = await uploadAgentApiMedia({
28
- agent: params.agent,
29
- type: mediaType,
30
- buffer: params.buffer,
31
- filename: params.filename,
32
- });
33
- await sendAgentApiMediaReply({
34
- agent: params.agent,
35
- target: params.target,
36
- mediaId,
37
- mediaType,
38
- title: mediaType === "video" ? params.text?.trim().slice(0, 64) : undefined,
39
- description: mediaType === "video" ? params.text?.trim().slice(0, 512) : undefined,
40
- });
41
- }
@@ -1,11 +0,0 @@
1
- import type { ResolvedAgentAccount } from "../../types/index.js";
2
- import { uploadMedia as uploadLegacyMedia } from "./core.js";
3
-
4
- export async function uploadAgentApiMedia(params: {
5
- agent: ResolvedAgentAccount;
6
- type: "image" | "voice" | "video" | "file";
7
- buffer: Buffer;
8
- filename: string;
9
- }): Promise<string> {
10
- return uploadLegacyMedia(params);
11
- }
@@ -1,39 +0,0 @@
1
- import type { ResolvedAgentAccount } from "../../types/index.js";
2
- import type { WecomTarget } from "../../target.js";
3
- import { sendAgentApiMedia, sendAgentApiText } from "./client.js";
4
-
5
- export async function sendAgentApiTextReply(params: {
6
- agent: ResolvedAgentAccount;
7
- target: WecomTarget;
8
- text: string;
9
- }): Promise<void> {
10
- await sendAgentApiText({
11
- agent: params.agent,
12
- toUser: params.target.touser,
13
- toParty: params.target.toparty,
14
- toTag: params.target.totag,
15
- chatId: params.target.chatid,
16
- text: params.text,
17
- });
18
- }
19
-
20
- export async function sendAgentApiMediaReply(params: {
21
- agent: ResolvedAgentAccount;
22
- target: WecomTarget;
23
- mediaId: string;
24
- mediaType: "image" | "voice" | "video" | "file";
25
- title?: string;
26
- description?: string;
27
- }): Promise<void> {
28
- await sendAgentApiMedia({
29
- agent: params.agent,
30
- toUser: params.target.touser,
31
- toParty: params.target.toparty,
32
- toTag: params.target.totag,
33
- chatId: params.target.chatid,
34
- mediaId: params.mediaId,
35
- mediaType: params.mediaType,
36
- title: params.title,
37
- description: params.description,
38
- });
39
- }
@@ -1,45 +0,0 @@
1
- import type { ResolvedAgentAccount } from "../../types/index.js";
2
- import type { WecomTarget } from "../../target.js";
3
- import { sendUpstreamAgentApiMediaReply, sendUpstreamAgentApiTextReply } from "./upstream-reply.js";
4
-
5
- export async function deliverUpstreamAgentApiText(params: {
6
- upstreamAgent: ResolvedAgentAccount;
7
- primaryAgent: ResolvedAgentAccount;
8
- target: WecomTarget;
9
- text: string;
10
- }): Promise<void> {
11
- await sendUpstreamAgentApiTextReply(params);
12
- }
13
-
14
- export async function deliverUpstreamAgentApiMedia(params: {
15
- upstreamAgent: ResolvedAgentAccount;
16
- primaryAgent: ResolvedAgentAccount;
17
- target: WecomTarget;
18
- buffer: Buffer;
19
- filename: string;
20
- contentType: string;
21
- text?: string;
22
- }): Promise<void> {
23
- let mediaType: "image" | "voice" | "video" | "file" = "file";
24
- if (params.contentType.startsWith("image/")) mediaType = "image";
25
- else if (params.contentType.startsWith("audio/")) mediaType = "voice";
26
- else if (params.contentType.startsWith("video/")) mediaType = "video";
27
-
28
- const { uploadUpstreamAgentApiMedia } = await import("./upstream-media-upload.js");
29
- const mediaId = await uploadUpstreamAgentApiMedia({
30
- upstreamAgent: params.upstreamAgent,
31
- primaryAgent: params.primaryAgent,
32
- type: mediaType,
33
- buffer: params.buffer,
34
- filename: params.filename,
35
- });
36
- await sendUpstreamAgentApiMediaReply({
37
- upstreamAgent: params.upstreamAgent,
38
- primaryAgent: params.primaryAgent,
39
- target: params.target,
40
- mediaId,
41
- mediaType,
42
- title: mediaType === "video" ? params.text?.trim().slice(0, 64) : undefined,
43
- description: mediaType === "video" ? params.text?.trim().slice(0, 512) : undefined,
44
- });
45
- }
@@ -1,70 +0,0 @@
1
- import crypto from "node:crypto";
2
-
3
- import { resolveWecomEgressProxyUrlFromNetwork } from "../../config/index.js";
4
- import { LIMITS } from "../../types/constants.js";
5
- import { wecomFetch } from "../../http.js";
6
- import type { ResolvedAgentAccount } from "../../types/index.js";
7
- import { guessUploadContentType, normalizeUploadFilename } from "./core.js";
8
- import { getUpstreamAgentApiAccessToken } from "./client.js";
9
-
10
- export async function uploadUpstreamAgentApiMedia(params: {
11
- upstreamAgent: ResolvedAgentAccount;
12
- primaryAgent: ResolvedAgentAccount;
13
- type: "image" | "voice" | "video" | "file";
14
- buffer: Buffer;
15
- filename: string;
16
- }): Promise<string> {
17
- const { upstreamAgent, primaryAgent, type, buffer, filename } = params;
18
- const safeFilename = normalizeUploadFilename(filename);
19
-
20
- // 使用下游企业的 access_token
21
- const token = await getUpstreamAgentApiAccessToken({
22
- primaryAgent,
23
- upstreamCorpId: upstreamAgent.corpId,
24
- upstreamAgentId: upstreamAgent.agentId!,
25
- });
26
-
27
- const proxyUrl = resolveWecomEgressProxyUrlFromNetwork(upstreamAgent.network);
28
- const url = `https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=${encodeURIComponent(token)}&type=${encodeURIComponent(type)}&debug=1`;
29
-
30
- const uploadOnce = async (fileContentType: string) => {
31
- const boundary = `----WebKitFormBoundary${crypto.randomBytes(16).toString("hex")}`;
32
- const header = Buffer.from(
33
- `--${boundary}\r\n` +
34
- `Content-Disposition: form-data; name="media"; filename="${safeFilename}"; filelength=${buffer.length}\r\n` +
35
- `Content-Type: ${fileContentType}\r\n\r\n`,
36
- );
37
- const footer = Buffer.from(`\r\n--${boundary}--\r\n`);
38
- const body = Buffer.concat([header, buffer, footer]);
39
-
40
- const res = await wecomFetch(
41
- url,
42
- {
43
- method: "POST",
44
- headers: {
45
- "Content-Type": `multipart/form-data; boundary=${boundary}`,
46
- "Content-Length": String(body.length),
47
- },
48
- body,
49
- },
50
- { proxyUrl, timeoutMs: LIMITS.REQUEST_TIMEOUT_MS },
51
- );
52
- const json = (await res.json()) as { media_id?: string; errcode?: number; errmsg?: string };
53
- return json;
54
- };
55
-
56
- const preferredContentType = guessUploadContentType(safeFilename);
57
- let json = await uploadOnce(preferredContentType);
58
-
59
- if (!json?.media_id && preferredContentType !== "application/octet-stream") {
60
- console.warn(
61
- `[wecom-upstream-upload] Upload failed with ${preferredContentType}, retrying as application/octet-stream: ${json?.errcode} ${json?.errmsg}`,
62
- );
63
- json = await uploadOnce("application/octet-stream");
64
- }
65
-
66
- if (!json?.media_id) {
67
- throw new Error(`upload failed: ${json?.errcode} ${json?.errmsg}`);
68
- }
69
- return json.media_id;
70
- }