@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,9 +1,9 @@
1
1
  /**
2
2
  * WeCom Target Resolver (企业微信目标解析器)
3
- *
3
+ *
4
4
  * 解析 OpenClaw 的 `to` 字段(原始目标字符串),将其转换为企业微信支持的具体接收对象。
5
5
  * 支持显式前缀 (party:, tag: 等) 和基于规则的启发式推断。
6
- *
6
+ *
7
7
  * **关于“目标发送”与“消息记录”的对应关系 (Target vs Inbound):**
8
8
  * - **发送 (Outbound)**: 支持一对多广播 (Party/Tag)。
9
9
  * 例如发送给 `party:1`,消息会触达该部门下所有成员。
@@ -12,24 +12,7 @@
12
12
  * 因此,Outbound Target (如 Party) 与 Inbound Source (User) 不需要也不可能 1:1 强匹配。
13
13
  * 广播是“发后即忘” (Fire-and-Forget) 的通知模式,而回复是具体的会话模式。
14
14
  */
15
-
16
- export interface WecomTarget {
17
- touser?: string;
18
- toparty?: string;
19
- totag?: string;
20
- chatid?: string;
21
- }
22
-
23
- export interface ScopedWecomTarget {
24
- accountId?: string;
25
- target: WecomTarget;
26
- rawTarget: string;
27
- }
28
-
29
- function parseUpstreamScopedTarget(raw: string): {
30
- accountId?: string;
31
- userId: string;
32
- } | undefined {
15
+ function parseUpstreamScopedTarget(raw) {
33
16
  const legacyScoped = raw.match(/^wecom-agent-upstream:([^:]+):([^:]+):(.+)$/i);
34
17
  if (legacyScoped) {
35
18
  return {
@@ -37,40 +20,35 @@ function parseUpstreamScopedTarget(raw: string): {
37
20
  userId: legacyScoped[3]?.trim() || "",
38
21
  };
39
22
  }
40
-
41
23
  const queryIndex = raw.indexOf("?upstream_corp=");
42
24
  if (queryIndex < 0 || !raw.startsWith("wecom-agent:")) {
43
25
  return undefined;
44
26
  }
45
-
46
27
  const pathPart = raw.slice(0, queryIndex);
47
28
  const match = pathPart.match(/^wecom-agent:([^:]+):user:(.+)$/i);
48
29
  if (!match) {
49
30
  return undefined;
50
31
  }
51
-
52
32
  return {
53
33
  accountId: match[1]?.trim(),
54
34
  userId: match[2]?.trim() || "",
55
35
  };
56
36
  }
57
-
58
- export function buildWecomContextTarget(contextToken: string): string {
37
+ export function buildWecomContextTarget(contextToken) {
59
38
  return `wecom:context:${contextToken}`;
60
39
  }
61
-
62
- export function resolveWecomContextTarget(raw: string | undefined): { contextToken: string } | undefined {
40
+ export function resolveWecomContextTarget(raw) {
63
41
  const trimmed = raw?.trim();
64
- if (!trimmed) return undefined;
42
+ if (!trimmed)
43
+ return undefined;
65
44
  const match = trimmed.match(/^(?:wecom|wechatwork|wework|qywx):context:(.+)$/i);
66
45
  const contextToken = match?.[1]?.trim();
67
46
  return contextToken ? { contextToken } : undefined;
68
47
  }
69
-
70
48
  /**
71
49
  * Parses a raw target string into a WeComTarget object.
72
50
  * 解析原始目标字符串为 WeComTarget 对象。
73
- *
51
+ *
74
52
  * 逻辑:
75
53
  * 1. 先检查显式类型前缀 (user:, group:, party:, tag:) —— 优先匹配,不受命名空间前缀影响
76
54
  * 2. 移除标准命名空间前缀 (wecom:, qywx: 等)
@@ -79,14 +57,13 @@ export function resolveWecomContextTarget(raw: string | undefined): { contextTok
79
57
  * - 以 "wr" 或 "wc" 开头 -> Chat ID (群聊)
80
58
  * - 纯数字 -> 默认 User ID (用户),避免误判部门导致 81013 错误
81
59
  * - 其他 -> User ID (用户)
82
- *
60
+ *
83
61
  * @param raw - The raw target string (e.g. "party:1", "zhangsan", "wecom:user:xxx")
84
62
  */
85
- export function resolveWecomTarget(raw: string | undefined, options?: { preferUserForDigits?: boolean }): WecomTarget | undefined {
86
- if (!raw?.trim()) return undefined;
87
-
63
+ export function resolveWecomTarget(raw, options) {
64
+ if (!raw?.trim())
65
+ return undefined;
88
66
  const trimmed = raw.trim();
89
-
90
67
  // 1. 先检查原始字符串中的类型前缀(处理 user:xxx 无前缀格式)
91
68
  // 这样即使没有 wecom: 前缀,也能正确识别类型
92
69
  if (/^user:/i.test(trimmed)) {
@@ -101,10 +78,8 @@ export function resolveWecomTarget(raw: string | undefined, options?: { preferUs
101
78
  if (/^tag:/i.test(trimmed)) {
102
79
  return { totag: trimmed.replace(/^tag:/i, "").trim() };
103
80
  }
104
-
105
81
  // 2. Remove standard namespace prefixes (移除标准命名空间前缀)
106
82
  let clean = trimmed.replace(/^(wecom-agent|wecom|wechatwork|wework|qywx):/i, "");
107
-
108
83
  // 3. 再次检查类型前缀(处理 wecom:user:xxx 格式)
109
84
  if (/^user:/i.test(clean)) {
110
85
  return { touser: clean.replace(/^user:/i, "").trim() };
@@ -118,15 +93,12 @@ export function resolveWecomTarget(raw: string | undefined, options?: { preferUs
118
93
  if (/^tag:/i.test(clean)) {
119
94
  return { totag: clean.replace(/^tag:/i, "").trim() };
120
95
  }
121
-
122
96
  // 4. Heuristics (启发式规则)
123
-
124
97
  // Chat ID typically starts with 'wr' or 'wc'
125
98
  // 群聊 ID 通常以 'wr' (外部群) 或 'wc' 开头
126
99
  if (/^(wr|wc)/i.test(clean)) {
127
100
  return { chatid: clean };
128
101
  }
129
-
130
102
  // Pure digits: Default to User (纯数字默认为用户)
131
103
  // 原因:1) Bot WS 主动推送只接受 touser/chatid,不接受 toparty/totag
132
104
  // 2) 用户 ID 在企业微信中常为纯数字
@@ -138,16 +110,13 @@ export function resolveWecomTarget(raw: string | undefined, options?: { preferUs
138
110
  }
139
111
  return { touser: clean };
140
112
  }
141
-
142
113
  // Default to User (默认为用户)
143
114
  return { touser: clean };
144
115
  }
145
-
146
- export function resolveScopedWecomTarget(raw: string | undefined, defaultAccountId?: string): ScopedWecomTarget | undefined {
147
- if (!raw?.trim()) return undefined;
148
-
116
+ export function resolveScopedWecomTarget(raw, defaultAccountId) {
117
+ if (!raw?.trim())
118
+ return undefined;
149
119
  const trimmed = raw.trim();
150
-
151
120
  const upstreamScoped = parseUpstreamScopedTarget(trimmed);
152
121
  if (upstreamScoped) {
153
122
  const accountId = upstreamScoped.accountId || defaultAccountId;
@@ -157,7 +126,6 @@ export function resolveScopedWecomTarget(raw: string | undefined, defaultAccount
157
126
  rawTarget: upstreamScoped.userId,
158
127
  };
159
128
  }
160
-
161
129
  const agentScoped = trimmed.match(/^wecom-agent:([^:]+):(.+)$/i);
162
130
  if (agentScoped) {
163
131
  const accountId = agentScoped[1]?.trim() || defaultAccountId;
@@ -167,7 +135,6 @@ export function resolveScopedWecomTarget(raw: string | undefined, defaultAccount
167
135
  const target = resolveWecomTarget(rawTarget, { preferUserForDigits: true });
168
136
  return target ? { accountId, target, rawTarget } : undefined;
169
137
  }
170
-
171
138
  const target = resolveWecomTarget(trimmed);
172
139
  return target
173
140
  ? {
@@ -0,0 +1,168 @@
1
+ import { LIMITS } from "../../types/constants.js";
2
+ import { downloadMedia as downloadLegacyMedia, getAccessToken as getLegacyAccessToken, getUpstreamAccessToken as getLegacyUpstreamAccessToken, sendMedia as sendLegacyMedia, sendText as sendLegacyText, } from "./core.js";
3
+ export async function getAgentApiAccessToken(agent) {
4
+ return getLegacyAccessToken(agent);
5
+ }
6
+ export async function getUpstreamAgentApiAccessToken(params) {
7
+ return getLegacyUpstreamAccessToken(params);
8
+ }
9
+ export async function sendAgentApiText(params) {
10
+ await sendLegacyText(params);
11
+ }
12
+ export async function sendAgentApiMedia(params) {
13
+ await sendLegacyMedia(params);
14
+ }
15
+ export async function downloadAgentApiMedia(params) {
16
+ return downloadLegacyMedia(params);
17
+ }
18
+ export async function downloadUpstreamAgentApiMedia(params) {
19
+ const { upstreamAgent, primaryAgent, mediaId, maxBytes } = params;
20
+ const token = await getUpstreamAgentApiAccessToken({
21
+ primaryAgent,
22
+ upstreamCorpId: upstreamAgent.corpId,
23
+ upstreamAgentId: upstreamAgent.agentId,
24
+ });
25
+ const url = `https://qyapi.weixin.qq.com/cgi-bin/media/get?access_token=${encodeURIComponent(token)}&media_id=${encodeURIComponent(mediaId)}`;
26
+ const { wecomFetch, readResponseBodyAsBuffer } = await import("../../http.js");
27
+ const { resolveWecomEgressProxyUrlFromNetwork } = await import("../../config/index.js");
28
+ const res = await wecomFetch(url, undefined, {
29
+ proxyUrl: resolveWecomEgressProxyUrlFromNetwork(upstreamAgent.network),
30
+ timeoutMs: LIMITS.REQUEST_TIMEOUT_MS,
31
+ });
32
+ if (!res.ok) {
33
+ throw new Error(`download failed: ${res.status}`);
34
+ }
35
+ const contentType = res.headers.get("content-type") || "application/octet-stream";
36
+ const disposition = res.headers.get("content-disposition") || "";
37
+ const filename = (() => {
38
+ const mStar = disposition.match(/filename\*\s*=\s*([^;]+)/i);
39
+ if (mStar) {
40
+ const raw = mStar[1].trim().replace(/^"(.*)"$/, "$1");
41
+ const parts = raw.split("''");
42
+ const encoded = parts.length === 2 ? parts[1] : raw;
43
+ try {
44
+ return decodeURIComponent(encoded);
45
+ }
46
+ catch {
47
+ return encoded;
48
+ }
49
+ }
50
+ const m = disposition.match(/filename\s*=\s*([^;]+)/i);
51
+ if (!m)
52
+ return undefined;
53
+ return m[1].trim().replace(/^"(.*)"$/, "$1") || undefined;
54
+ })();
55
+ if (contentType.includes("application/json")) {
56
+ const json = (await res.json());
57
+ throw new Error(`download failed: ${json?.errcode} ${json?.errmsg}`);
58
+ }
59
+ const buffer = await readResponseBodyAsBuffer(res, maxBytes);
60
+ return { buffer, contentType, filename };
61
+ }
62
+ /**
63
+ * 发送文本消息给上下游用户
64
+ * 使用下游企业的 access_token 和 agentId
65
+ */
66
+ export async function sendUpstreamAgentApiText(params) {
67
+ const { upstreamAgent, primaryAgent, toUser, toParty, toTag, chatId, text } = params;
68
+ // 获取下游企业的 access_token
69
+ const token = await getUpstreamAgentApiAccessToken({
70
+ primaryAgent,
71
+ upstreamCorpId: upstreamAgent.corpId,
72
+ upstreamAgentId: upstreamAgent.agentId,
73
+ });
74
+ const useChat = Boolean(chatId);
75
+ const url = useChat
76
+ ? `https://qyapi.weixin.qq.com/cgi-bin/appchat/send?access_token=${encodeURIComponent(token)}`
77
+ : `https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=${encodeURIComponent(token)}`;
78
+ const body = useChat
79
+ ? { chatid: chatId, msgtype: "text", text: { content: text } }
80
+ : {
81
+ touser: toUser,
82
+ toparty: toParty,
83
+ totag: toTag,
84
+ msgtype: "text",
85
+ agentid: upstreamAgent.agentId,
86
+ text: { content: text },
87
+ };
88
+ const { wecomFetch } = await import("../../http.js");
89
+ const { resolveWecomEgressProxyUrlFromNetwork } = await import("../../config/index.js");
90
+ const res = await wecomFetch(url, {
91
+ method: "POST",
92
+ headers: { "Content-Type": "application/json" },
93
+ body: JSON.stringify(body),
94
+ }, {
95
+ proxyUrl: resolveWecomEgressProxyUrlFromNetwork(upstreamAgent.network),
96
+ timeoutMs: LIMITS.REQUEST_TIMEOUT_MS,
97
+ });
98
+ const json = (await res.json());
99
+ if (json?.errcode !== 0) {
100
+ throw new Error(`send failed: ${json?.errcode} ${json?.errmsg}`);
101
+ }
102
+ if (json?.invaliduser || json?.invalidparty || json?.invalidtag) {
103
+ const details = [
104
+ json.invaliduser ? `invaliduser=${json.invaliduser}` : "",
105
+ json.invalidparty ? `invalidparty=${json.invalidparty}` : "",
106
+ json.invalidtag ? `invalidtag=${json.invalidtag}` : "",
107
+ ]
108
+ .filter(Boolean)
109
+ .join(", ");
110
+ throw new Error(`send partial failure: ${details}`);
111
+ }
112
+ }
113
+ /**
114
+ * 发送媒体消息给上下游用户
115
+ * 使用下游企业的 access_token 和 agentId
116
+ */
117
+ export async function sendUpstreamAgentApiMedia(params) {
118
+ const { upstreamAgent, primaryAgent, toUser, toParty, toTag, chatId, mediaId, mediaType, title, description } = params;
119
+ // 获取下游企业的 access_token
120
+ const token = await getUpstreamAgentApiAccessToken({
121
+ primaryAgent,
122
+ upstreamCorpId: upstreamAgent.corpId,
123
+ upstreamAgentId: upstreamAgent.agentId,
124
+ });
125
+ console.log(`[wecom-upstream-api] sendMedia corpId=${upstreamAgent.corpId} agentId=${upstreamAgent.agentId} ` +
126
+ `toUser=${toUser ?? ""} mediaType=${mediaType}`);
127
+ const useChat = Boolean(chatId);
128
+ const url = useChat
129
+ ? `https://qyapi.weixin.qq.com/cgi-bin/appchat/send?access_token=${encodeURIComponent(token)}`
130
+ : `https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=${encodeURIComponent(token)}`;
131
+ const mediaPayload = mediaType === "video"
132
+ ? { media_id: mediaId, title: title ?? "Video", description: description ?? "" }
133
+ : { media_id: mediaId };
134
+ const body = useChat
135
+ ? { chatid: chatId, msgtype: mediaType, [mediaType]: mediaPayload }
136
+ : {
137
+ touser: toUser,
138
+ toparty: toParty,
139
+ totag: toTag,
140
+ msgtype: mediaType,
141
+ agentid: upstreamAgent.agentId,
142
+ [mediaType]: mediaPayload,
143
+ };
144
+ const { wecomFetch } = await import("../../http.js");
145
+ const { resolveWecomEgressProxyUrlFromNetwork } = await import("../../config/index.js");
146
+ const res = await wecomFetch(url, {
147
+ method: "POST",
148
+ headers: { "Content-Type": "application/json" },
149
+ body: JSON.stringify(body),
150
+ }, {
151
+ proxyUrl: resolveWecomEgressProxyUrlFromNetwork(upstreamAgent.network),
152
+ timeoutMs: LIMITS.REQUEST_TIMEOUT_MS,
153
+ });
154
+ const json = (await res.json());
155
+ if (json?.errcode !== 0) {
156
+ throw new Error(`send ${mediaType} failed: ${json?.errcode} ${json?.errmsg}`);
157
+ }
158
+ if (json?.invaliduser || json?.invalidparty || json?.invalidtag) {
159
+ const details = [
160
+ json.invaliduser ? `invaliduser=${json.invaliduser}` : "",
161
+ json.invalidparty ? `invalidparty=${json.invalidparty}` : "",
162
+ json.invalidtag ? `invalidtag=${json.invalidtag}` : "",
163
+ ]
164
+ .filter(Boolean)
165
+ .join(", ");
166
+ throw new Error(`send ${mediaType} partial failure: ${details}`);
167
+ }
168
+ }
@@ -0,0 +1,337 @@
1
+ import crypto from "node:crypto";
2
+ import { resolveWecomEgressProxyUrlFromNetwork } from "../../config/index.js";
3
+ import { readResponseBodyAsBuffer, wecomFetch } from "../../http.js";
4
+ import { API_ENDPOINTS, LIMITS } from "../../types/constants.js";
5
+ const tokenCaches = new Map();
6
+ function truncateForLog(raw, maxChars = 180) {
7
+ const compact = raw.replace(/\s+/g, " ").trim();
8
+ if (compact.length <= maxChars)
9
+ return compact;
10
+ return `${compact.slice(0, maxChars)}...(truncated)`;
11
+ }
12
+ export function normalizeUploadFilename(filename) {
13
+ const trimmed = filename.trim();
14
+ if (!trimmed)
15
+ return "file.bin";
16
+ const ext = trimmed.includes(".") ? `.${trimmed.split(".").pop().toLowerCase()}` : "";
17
+ const base = ext ? trimmed.slice(0, -ext.length) : trimmed;
18
+ const sanitizedBase = base
19
+ .replace(/[^\x20-\x7e]/g, "_")
20
+ .replace(/["\\/;=]/g, "_")
21
+ .replace(/\s+/g, "_")
22
+ .replace(/_+/g, "_")
23
+ .replace(/^_+|_+$/g, "");
24
+ const safeBase = sanitizedBase || "file";
25
+ const safeExt = ext.replace(/[^a-z0-9.]/g, "");
26
+ return `${safeBase}${safeExt || ".bin"}`;
27
+ }
28
+ export function guessUploadContentType(filename) {
29
+ const ext = filename.split(".").pop()?.toLowerCase() || "";
30
+ const contentTypeMap = {
31
+ jpg: "image/jpg",
32
+ jpeg: "image/jpeg",
33
+ png: "image/png",
34
+ gif: "image/gif",
35
+ webp: "image/webp",
36
+ bmp: "image/bmp",
37
+ amr: "voice/amr",
38
+ mp3: "audio/mpeg",
39
+ wav: "audio/wav",
40
+ m4a: "audio/mp4",
41
+ ogg: "audio/ogg",
42
+ mp4: "video/mp4",
43
+ mov: "video/quicktime",
44
+ txt: "text/plain",
45
+ md: "text/markdown",
46
+ csv: "text/csv",
47
+ tsv: "text/tab-separated-values",
48
+ json: "application/json",
49
+ xml: "application/xml",
50
+ yaml: "application/yaml",
51
+ yml: "application/yaml",
52
+ pdf: "application/pdf",
53
+ doc: "application/msword",
54
+ docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
55
+ xls: "application/vnd.ms-excel",
56
+ xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
57
+ ppt: "application/vnd.ms-powerpoint",
58
+ pptx: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
59
+ rtf: "application/rtf",
60
+ odt: "application/vnd.oasis.opendocument.text",
61
+ zip: "application/zip",
62
+ rar: "application/vnd.rar",
63
+ "7z": "application/x-7z-compressed",
64
+ gz: "application/gzip",
65
+ tgz: "application/gzip",
66
+ tar: "application/x-tar",
67
+ };
68
+ return contentTypeMap[ext] || "application/octet-stream";
69
+ }
70
+ function requireAgentId(agent) {
71
+ if (typeof agent.agentId === "number" && Number.isFinite(agent.agentId))
72
+ return agent.agentId;
73
+ throw new Error(`wecom agent account=${agent.accountId} missing agentId; sending via cgi-bin/message/send requires agentId`);
74
+ }
75
+ /**
76
+ * 获取主企业的 access_token
77
+ * 使用 corpid + corpsecret
78
+ */
79
+ export async function getAccessToken(agent) {
80
+ const cacheKey = `${agent.corpId}:${String(agent.agentId ?? "na")}`;
81
+ let cache = tokenCaches.get(cacheKey);
82
+ if (!cache) {
83
+ cache = { token: "", expiresAt: 0, refreshPromise: null };
84
+ tokenCaches.set(cacheKey, cache);
85
+ }
86
+ const now = Date.now();
87
+ if (cache.token && cache.expiresAt > now + LIMITS.TOKEN_REFRESH_BUFFER_MS) {
88
+ return cache.token;
89
+ }
90
+ if (cache.refreshPromise) {
91
+ return cache.refreshPromise;
92
+ }
93
+ cache.refreshPromise = (async () => {
94
+ try {
95
+ const url = `${API_ENDPOINTS.GET_TOKEN}?corpid=${encodeURIComponent(agent.corpId)}&corpsecret=${encodeURIComponent(agent.corpSecret)}`;
96
+ const res = await wecomFetch(url, undefined, {
97
+ proxyUrl: resolveWecomEgressProxyUrlFromNetwork(agent.network),
98
+ timeoutMs: LIMITS.REQUEST_TIMEOUT_MS,
99
+ });
100
+ const json = (await res.json());
101
+ if (!json?.access_token) {
102
+ throw new Error(`gettoken failed: ${json?.errcode} ${json?.errmsg}`);
103
+ }
104
+ cache.token = json.access_token;
105
+ cache.expiresAt = Date.now() + (json.expires_in ?? 7200) * 1000;
106
+ return cache.token;
107
+ }
108
+ finally {
109
+ cache.refreshPromise = null;
110
+ }
111
+ })();
112
+ return cache.refreshPromise;
113
+ }
114
+ /**
115
+ * 获取下游企业的 access_token
116
+ *
117
+ * 根据企业微信文档:https://developer.work.weixin.qq.com/document/path/95816
118
+ *
119
+ * 请求方式:POST(HTTPS)
120
+ * 请求地址:https://qyapi.weixin.qq.com/cgi-bin/corpgroup/corp/gettoken?access_token=ACCESS_TOKEN
121
+ *
122
+ * 请求体:
123
+ * {
124
+ * "corpid": "下游企业corpid",
125
+ * "business_type": 1, // 1 表示上下游企业
126
+ * "agentid": 下游企业应用ID
127
+ * }
128
+ *
129
+ * 注意:需要使用上游企业的 access_token 作为调用凭证
130
+ */
131
+ export async function getUpstreamAccessToken(params) {
132
+ const { primaryAgent, upstreamCorpId, upstreamAgentId } = params;
133
+ // 缓存 key 增加 primaryCorpId 维度,避免多主企业之间碰撞
134
+ const cacheKey = `upstream:${primaryAgent.corpId}:${upstreamCorpId}:${upstreamAgentId}`;
135
+ let cache = tokenCaches.get(cacheKey);
136
+ if (!cache) {
137
+ cache = { token: "", expiresAt: 0, refreshPromise: null };
138
+ tokenCaches.set(cacheKey, cache);
139
+ }
140
+ const now = Date.now();
141
+ if (cache.token && cache.expiresAt > now + LIMITS.TOKEN_REFRESH_BUFFER_MS) {
142
+ return cache.token;
143
+ }
144
+ if (cache.refreshPromise) {
145
+ return cache.refreshPromise;
146
+ }
147
+ cache.refreshPromise = (async () => {
148
+ try {
149
+ // 1. 先获取上游企业的 access_token
150
+ const primaryToken = await getAccessToken(primaryAgent);
151
+ // 2. 调用 corpgroup/corp/gettoken 获取下游企业的 access_token
152
+ const url = `https://qyapi.weixin.qq.com/cgi-bin/corpgroup/corp/gettoken?access_token=${encodeURIComponent(primaryToken)}`;
153
+ const requestBody = {
154
+ corpid: upstreamCorpId,
155
+ business_type: 1, // 1 表示上下游企业
156
+ agentid: upstreamAgentId,
157
+ };
158
+ const res = await wecomFetch(url, {
159
+ method: "POST",
160
+ headers: { "Content-Type": "application/json" },
161
+ body: JSON.stringify(requestBody),
162
+ }, {
163
+ proxyUrl: resolveWecomEgressProxyUrlFromNetwork(primaryAgent.network),
164
+ timeoutMs: LIMITS.REQUEST_TIMEOUT_MS,
165
+ });
166
+ const json = (await res.json());
167
+ if (!json?.access_token) {
168
+ throw new Error(`get upstream token failed: ${json?.errcode} ${json?.errmsg}`);
169
+ }
170
+ cache.token = json.access_token;
171
+ cache.expiresAt = Date.now() + (json.expires_in ?? 7200) * 1000;
172
+ return cache.token;
173
+ }
174
+ finally {
175
+ cache.refreshPromise = null;
176
+ }
177
+ })();
178
+ return cache.refreshPromise;
179
+ }
180
+ export async function sendText(params) {
181
+ const { agent, toUser, toParty, toTag, chatId, text } = params;
182
+ console.log(`[wecom-agent-api] sendText request account=${agent.accountId} agentId=${String(agent.agentId ?? "N/A")} corpId=${agent.corpId} ` +
183
+ `toUser=${toUser ?? ""} toParty=${toParty ?? ""} toTag=${toTag ?? ""} chatId=${chatId ?? ""} ` +
184
+ `textLen=${text.length} textPreview=${JSON.stringify(truncateForLog(text))}`);
185
+ const token = await getAccessToken(agent);
186
+ const useChat = Boolean(chatId);
187
+ const url = useChat
188
+ ? `${API_ENDPOINTS.SEND_APPCHAT}?access_token=${encodeURIComponent(token)}`
189
+ : `${API_ENDPOINTS.SEND_MESSAGE}?access_token=${encodeURIComponent(token)}`;
190
+ const body = useChat
191
+ ? { chatid: chatId, msgtype: "text", text: { content: text } }
192
+ : {
193
+ touser: toUser,
194
+ toparty: toParty,
195
+ totag: toTag,
196
+ msgtype: "text",
197
+ agentid: requireAgentId(agent),
198
+ text: { content: text },
199
+ };
200
+ const res = await wecomFetch(url, {
201
+ method: "POST",
202
+ headers: { "Content-Type": "application/json" },
203
+ body: JSON.stringify(body),
204
+ }, { proxyUrl: resolveWecomEgressProxyUrlFromNetwork(agent.network), timeoutMs: LIMITS.REQUEST_TIMEOUT_MS });
205
+ const json = (await res.json());
206
+ console.log(`[wecom-agent-api] sendText response account=${agent.accountId} agentId=${String(agent.agentId ?? "N/A")} corpId=${agent.corpId} ` +
207
+ `toUser=${toUser ?? ""} toParty=${toParty ?? ""} toTag=${toTag ?? ""} chatId=${chatId ?? ""} ` +
208
+ `errcode=${String(json?.errcode ?? "N/A")} errmsg=${json?.errmsg ?? ""} ` +
209
+ `invaliduser=${json?.invaliduser ?? ""} invalidparty=${json?.invalidparty ?? ""} invalidtag=${json?.invalidtag ?? ""}`);
210
+ if (json?.errcode !== 0) {
211
+ throw new Error(`send failed: ${json?.errcode} ${json?.errmsg}`);
212
+ }
213
+ if (json?.invaliduser || json?.invalidparty || json?.invalidtag) {
214
+ const details = [
215
+ json.invaliduser ? `invaliduser=${json.invaliduser}` : "",
216
+ json.invalidparty ? `invalidparty=${json.invalidparty}` : "",
217
+ json.invalidtag ? `invalidtag=${json.invalidtag}` : "",
218
+ ]
219
+ .filter(Boolean)
220
+ .join(", ");
221
+ throw new Error(`send partial failure: ${details}`);
222
+ }
223
+ }
224
+ export async function uploadMedia(params) {
225
+ const { agent, type, buffer, filename } = params;
226
+ const safeFilename = normalizeUploadFilename(filename);
227
+ const token = await getAccessToken(agent);
228
+ const proxyUrl = resolveWecomEgressProxyUrlFromNetwork(agent.network);
229
+ const url = `${API_ENDPOINTS.UPLOAD_MEDIA}?access_token=${encodeURIComponent(token)}&type=${encodeURIComponent(type)}&debug=1`;
230
+ console.log(`[wecom-upload] Uploading media: type=${type}, filename=${safeFilename}, size=${buffer.length} bytes, corpId=${agent.corpId}`);
231
+ const uploadOnce = async (fileContentType) => {
232
+ const boundary = `----WebKitFormBoundary${crypto.randomBytes(16).toString("hex")}`;
233
+ const header = Buffer.from(`--${boundary}\r\n` +
234
+ `Content-Disposition: form-data; name="media"; filename="${safeFilename}"; filelength=${buffer.length}\r\n` +
235
+ `Content-Type: ${fileContentType}\r\n\r\n`);
236
+ const footer = Buffer.from(`\r\n--${boundary}--\r\n`);
237
+ const body = Buffer.concat([header, buffer, footer]);
238
+ console.log(`[wecom-upload] Multipart body size=${body.length}, boundary=${boundary}, fileContentType=${fileContentType}`);
239
+ const res = await wecomFetch(url, {
240
+ method: "POST",
241
+ headers: {
242
+ "Content-Type": `multipart/form-data; boundary=${boundary}`,
243
+ "Content-Length": String(body.length),
244
+ },
245
+ body,
246
+ }, { proxyUrl, timeoutMs: LIMITS.REQUEST_TIMEOUT_MS });
247
+ const json = (await res.json());
248
+ console.log(`[wecom-upload] Response:`, JSON.stringify(json));
249
+ return json;
250
+ };
251
+ const preferredContentType = guessUploadContentType(safeFilename);
252
+ let json = await uploadOnce(preferredContentType);
253
+ if (!json?.media_id && preferredContentType !== "application/octet-stream") {
254
+ console.warn(`[wecom-upload] Upload failed with ${preferredContentType}, retrying as application/octet-stream: ${json?.errcode} ${json?.errmsg}`);
255
+ json = await uploadOnce("application/octet-stream");
256
+ }
257
+ if (!json?.media_id) {
258
+ throw new Error(`upload failed: ${json?.errcode} ${json?.errmsg}`);
259
+ }
260
+ return json.media_id;
261
+ }
262
+ export async function sendMedia(params) {
263
+ const { agent, toUser, toParty, toTag, chatId, mediaId, mediaType, title, description } = params;
264
+ const token = await getAccessToken(agent);
265
+ const useChat = Boolean(chatId);
266
+ const url = useChat
267
+ ? `${API_ENDPOINTS.SEND_APPCHAT}?access_token=${encodeURIComponent(token)}`
268
+ : `${API_ENDPOINTS.SEND_MESSAGE}?access_token=${encodeURIComponent(token)}`;
269
+ const mediaPayload = mediaType === "video" ? { media_id: mediaId, title: title ?? "Video", description: description ?? "" } : { media_id: mediaId };
270
+ const body = useChat
271
+ ? { chatid: chatId, msgtype: mediaType, [mediaType]: mediaPayload }
272
+ : {
273
+ touser: toUser,
274
+ toparty: toParty,
275
+ totag: toTag,
276
+ msgtype: mediaType,
277
+ agentid: requireAgentId(agent),
278
+ [mediaType]: mediaPayload,
279
+ };
280
+ const res = await wecomFetch(url, {
281
+ method: "POST",
282
+ headers: { "Content-Type": "application/json" },
283
+ body: JSON.stringify(body),
284
+ }, { proxyUrl: resolveWecomEgressProxyUrlFromNetwork(agent.network), timeoutMs: LIMITS.REQUEST_TIMEOUT_MS });
285
+ const json = (await res.json());
286
+ if (json?.errcode !== 0) {
287
+ throw new Error(`send ${mediaType} failed: ${json?.errcode} ${json?.errmsg}`);
288
+ }
289
+ if (json?.invaliduser || json?.invalidparty || json?.invalidtag) {
290
+ const details = [
291
+ json.invaliduser ? `invaliduser=${json.invaliduser}` : "",
292
+ json.invalidparty ? `invalidparty=${json.invalidparty}` : "",
293
+ json.invalidtag ? `invalidtag=${json.invalidtag}` : "",
294
+ ]
295
+ .filter(Boolean)
296
+ .join(", ");
297
+ throw new Error(`send ${mediaType} partial failure: ${details}`);
298
+ }
299
+ }
300
+ export async function downloadMedia(params) {
301
+ const { agent, mediaId } = params;
302
+ const token = await getAccessToken(agent);
303
+ const url = `${API_ENDPOINTS.DOWNLOAD_MEDIA}?access_token=${encodeURIComponent(token)}&media_id=${encodeURIComponent(mediaId)}`;
304
+ const res = await wecomFetch(url, undefined, {
305
+ proxyUrl: resolveWecomEgressProxyUrlFromNetwork(agent.network),
306
+ timeoutMs: LIMITS.REQUEST_TIMEOUT_MS,
307
+ });
308
+ if (!res.ok) {
309
+ throw new Error(`download failed: ${res.status}`);
310
+ }
311
+ const contentType = res.headers.get("content-type") || "application/octet-stream";
312
+ const disposition = res.headers.get("content-disposition") || "";
313
+ const filename = (() => {
314
+ const mStar = disposition.match(/filename\*\s*=\s*([^;]+)/i);
315
+ if (mStar) {
316
+ const raw = mStar[1].trim().replace(/^"(.*)"$/, "$1");
317
+ const parts = raw.split("''");
318
+ const encoded = parts.length === 2 ? parts[1] : raw;
319
+ try {
320
+ return decodeURIComponent(encoded);
321
+ }
322
+ catch {
323
+ return encoded;
324
+ }
325
+ }
326
+ const m = disposition.match(/filename\s*=\s*([^;]+)/i);
327
+ if (!m)
328
+ return undefined;
329
+ return m[1].trim().replace(/^"(.*)"$/, "$1") || undefined;
330
+ })();
331
+ if (contentType.includes("application/json")) {
332
+ const json = (await res.json());
333
+ throw new Error(`download failed: ${json?.errcode} ${json?.errmsg}`);
334
+ }
335
+ const buffer = await readResponseBodyAsBuffer(res, params.maxBytes);
336
+ return { buffer, contentType, filename };
337
+ }