@botharness/im-provider 4.32.0-botharness.15

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 (423) hide show
  1. package/LICENSE +21 -0
  2. package/PROVENANCE.json +25 -0
  3. package/README.en.md +445 -0
  4. package/README.md +448 -0
  5. package/THIRD_PARTY_NOTICES.md +93 -0
  6. package/assets/dingtalk-menu-template.json +6 -0
  7. package/assets/logo-dsh-im-chinese-readme-3x2.png +0 -0
  8. package/assets/logo-dsh-im-connecting-readme-3x2.png +0 -0
  9. package/assets/logo-dsh-im-connecting-square-v4.png +0 -0
  10. package/assets/logo-icon.png +0 -0
  11. package/assets/logo-plugin-message-link-gradient.webp +0 -0
  12. package/assets/logo-plugin-phone.png +0 -0
  13. package/assets/logo.png +0 -0
  14. package/assets/logo_cn.png +0 -0
  15. package/assets/screenshot-menu-card.png +0 -0
  16. package/cordis.patch.yml +3 -0
  17. package/lib/client.js +20330 -0
  18. package/lib/index.js +604 -0
  19. package/locale/en.json +5 -0
  20. package/locale/zh.json +5 -0
  21. package/package.json +181 -0
  22. package/plugin-src/client/access-policy-settings.js +362 -0
  23. package/plugin-src/client/agent-preset.js +119 -0
  24. package/plugin-src/client/bot-alias.js +169 -0
  25. package/plugin-src/client/build.mjs +38 -0
  26. package/plugin-src/client/channel-card-meta.js +132 -0
  27. package/plugin-src/client/channel-logos.js +190 -0
  28. package/plugin-src/client/channels/dingtalk/api.js +267 -0
  29. package/plugin-src/client/channels/dingtalk/index.js +954 -0
  30. package/plugin-src/client/channels/dingtalk/styles.js +136 -0
  31. package/plugin-src/client/channels/discord/api.js +11 -0
  32. package/plugin-src/client/channels/discord/index.js +25 -0
  33. package/plugin-src/client/channels/discord/styles.js +19 -0
  34. package/plugin-src/client/channels/email/api.js +52 -0
  35. package/plugin-src/client/channels/email/index.js +728 -0
  36. package/plugin-src/client/channels/email/styles.js +51 -0
  37. package/plugin-src/client/channels/feishu/api.js +405 -0
  38. package/plugin-src/client/channels/feishu/group-settings.js +581 -0
  39. package/plugin-src/client/channels/feishu/index.js +1583 -0
  40. package/plugin-src/client/channels/feishu/slash-panel-settings.js +322 -0
  41. package/plugin-src/client/channels/feishu/styles.js +573 -0
  42. package/plugin-src/client/channels/feishu/voice-editor.js +115 -0
  43. package/plugin-src/client/channels/feishu/voice-settings.js +71 -0
  44. package/plugin-src/client/channels/imessage/api.js +12 -0
  45. package/plugin-src/client/channels/imessage/index.js +46 -0
  46. package/plugin-src/client/channels/imessage/styles.js +19 -0
  47. package/plugin-src/client/channels/matrix/api.js +11 -0
  48. package/plugin-src/client/channels/matrix/index.js +151 -0
  49. package/plugin-src/client/channels/matrix/styles.js +36 -0
  50. package/plugin-src/client/channels/office/api.js +46 -0
  51. package/plugin-src/client/channels/office/index.js +153 -0
  52. package/plugin-src/client/channels/office/styles.js +50 -0
  53. package/plugin-src/client/channels/qq/api.js +171 -0
  54. package/plugin-src/client/channels/qq/index.js +593 -0
  55. package/plugin-src/client/channels/qq/styles.js +19 -0
  56. package/plugin-src/client/channels/shared/collapsible-account.js +103 -0
  57. package/plugin-src/client/channels/shared/token-api.js +124 -0
  58. package/plugin-src/client/channels/shared/token-channel.js +495 -0
  59. package/plugin-src/client/channels/slack/api.js +11 -0
  60. package/plugin-src/client/channels/slack/index.js +131 -0
  61. package/plugin-src/client/channels/slack/styles.js +34 -0
  62. package/plugin-src/client/channels/telegram/api.js +11 -0
  63. package/plugin-src/client/channels/telegram/index.js +28 -0
  64. package/plugin-src/client/channels/telegram/styles.js +31 -0
  65. package/plugin-src/client/channels/telegram/thinking-traces.js +25 -0
  66. package/plugin-src/client/channels/wecom/api.js +160 -0
  67. package/plugin-src/client/channels/wecom/index.js +625 -0
  68. package/plugin-src/client/channels/wecom/styles.js +19 -0
  69. package/plugin-src/client/channels/wecom-app/api.js +132 -0
  70. package/plugin-src/client/channels/wecom-app/index.js +634 -0
  71. package/plugin-src/client/channels/wecom-app/styles.js +25 -0
  72. package/plugin-src/client/channels/weixin/api.js +211 -0
  73. package/plugin-src/client/channels/weixin/connection-error.js +1 -0
  74. package/plugin-src/client/channels/weixin/index.js +801 -0
  75. package/plugin-src/client/channels/weixin/styles.js +115 -0
  76. package/plugin-src/client/channels/whatsapp/api.js +152 -0
  77. package/plugin-src/client/channels/whatsapp/index.js +609 -0
  78. package/plugin-src/client/channels/whatsapp/styles.js +19 -0
  79. package/plugin-src/client/connection-error.js +87 -0
  80. package/plugin-src/client/context-enhancement.js +415 -0
  81. package/plugin-src/client/credential-binding.js +118 -0
  82. package/plugin-src/client/delivery-settings.js +905 -0
  83. package/plugin-src/client/global-settings.js +433 -0
  84. package/plugin-src/client/i18n.js +1314 -0
  85. package/plugin-src/client/index.js +624 -0
  86. package/plugin-src/client/interface-language.js +89 -0
  87. package/plugin-src/client/last-message-error.js +18 -0
  88. package/plugin-src/client/lifecycle.js +86 -0
  89. package/plugin-src/client/loopback-recovery.js +75 -0
  90. package/plugin-src/client/model-setting.js +172 -0
  91. package/plugin-src/client/panel-error-boundary.js +21 -0
  92. package/plugin-src/client/plugin-page.js +51 -0
  93. package/plugin-src/client/session-channel-logos.js +243 -0
  94. package/plugin-src/client/styles.js +770 -0
  95. package/plugin-src/client/update-panel.js +14 -0
  96. package/plugin-src/client/workspace-directory-picker.js +280 -0
  97. package/plugin-src/client/workspace-editor.js +69 -0
  98. package/plugin-src/client/workspace-snapshot-fence.js +28 -0
  99. package/plugin-src/host/build.mjs +52 -0
  100. package/plugin-src/host/channels/dingtalk/connection-supervisor.mjs +130 -0
  101. package/plugin-src/host/channels/dingtalk/index.mjs +35 -0
  102. package/plugin-src/host/channels/dingtalk/production.mjs +193 -0
  103. package/plugin-src/host/channels/dingtalk/rpc.mjs +359 -0
  104. package/plugin-src/host/channels/discord/index.mjs +33 -0
  105. package/plugin-src/host/channels/discord/production.mjs +18 -0
  106. package/plugin-src/host/channels/discord/rpc.mjs +21 -0
  107. package/plugin-src/host/channels/email/availability.mjs +60 -0
  108. package/plugin-src/host/channels/email/index.mjs +36 -0
  109. package/plugin-src/host/channels/email/production.mjs +58 -0
  110. package/plugin-src/host/channels/email/rpc.mjs +163 -0
  111. package/plugin-src/host/channels/feishu/connection-supervisor.mjs +167 -0
  112. package/plugin-src/host/channels/feishu/controller.mjs +172 -0
  113. package/plugin-src/host/channels/feishu/credential-store.mjs +84 -0
  114. package/plugin-src/host/channels/feishu/index.mjs +69 -0
  115. package/plugin-src/host/channels/feishu/production.mjs +319 -0
  116. package/plugin-src/host/channels/feishu/rpc.mjs +863 -0
  117. package/plugin-src/host/channels/imessage/index.mjs +21 -0
  118. package/plugin-src/host/channels/imessage/production.mjs +13 -0
  119. package/plugin-src/host/channels/imessage/rpc.mjs +78 -0
  120. package/plugin-src/host/channels/matrix/index.mjs +31 -0
  121. package/plugin-src/host/channels/matrix/production.mjs +241 -0
  122. package/plugin-src/host/channels/matrix/rpc.mjs +228 -0
  123. package/plugin-src/host/channels/office/index.mjs +14 -0
  124. package/plugin-src/host/channels/office/production.mjs +45 -0
  125. package/plugin-src/host/channels/office/rpc.mjs +51 -0
  126. package/plugin-src/host/channels/qq/connection-supervisor.mjs +124 -0
  127. package/plugin-src/host/channels/qq/index.mjs +28 -0
  128. package/plugin-src/host/channels/qq/production.mjs +181 -0
  129. package/plugin-src/host/channels/qq/rpc.mjs +250 -0
  130. package/plugin-src/host/channels/shared/access-policy-production.mjs +137 -0
  131. package/plugin-src/host/channels/shared/access-policy-rpc.mjs +17 -0
  132. package/plugin-src/host/channels/shared/agent-preset-rpc.mjs +25 -0
  133. package/plugin-src/host/channels/shared/bot-alias-rpc.mjs +15 -0
  134. package/plugin-src/host/channels/shared/connection-supervisor.mjs +133 -0
  135. package/plugin-src/host/channels/shared/context-enhancement-rpc.mjs +17 -0
  136. package/plugin-src/host/channels/shared/model-setting-rpc.mjs +18 -0
  137. package/plugin-src/host/channels/shared/production.mjs +239 -0
  138. package/plugin-src/host/channels/shared/rpc.mjs +226 -0
  139. package/plugin-src/host/channels/shared/startup-error.mjs +55 -0
  140. package/plugin-src/host/channels/shared/startup.mjs +76 -0
  141. package/plugin-src/host/channels/shared/thinking-traces-rpc.mjs +11 -0
  142. package/plugin-src/host/channels/shared/workspace-rpc.mjs +31 -0
  143. package/plugin-src/host/channels/slack/index.mjs +31 -0
  144. package/plugin-src/host/channels/slack/production.mjs +176 -0
  145. package/plugin-src/host/channels/slack/rpc.mjs +221 -0
  146. package/plugin-src/host/channels/telegram/index.mjs +33 -0
  147. package/plugin-src/host/channels/telegram/production.mjs +19 -0
  148. package/plugin-src/host/channels/telegram/rpc.mjs +22 -0
  149. package/plugin-src/host/channels/wecom/connection-supervisor.mjs +124 -0
  150. package/plugin-src/host/channels/wecom/index.mjs +34 -0
  151. package/plugin-src/host/channels/wecom/production.mjs +187 -0
  152. package/plugin-src/host/channels/wecom/rpc.mjs +255 -0
  153. package/plugin-src/host/channels/wecom-app/index.mjs +33 -0
  154. package/plugin-src/host/channels/wecom-app/production.mjs +221 -0
  155. package/plugin-src/host/channels/wecom-app/rpc.mjs +235 -0
  156. package/plugin-src/host/channels/weixin/connection-supervisor.mjs +139 -0
  157. package/plugin-src/host/channels/weixin/index.mjs +44 -0
  158. package/plugin-src/host/channels/weixin/production.mjs +258 -0
  159. package/plugin-src/host/channels/weixin/rpc.mjs +285 -0
  160. package/plugin-src/host/channels/whatsapp/index.mjs +33 -0
  161. package/plugin-src/host/channels/whatsapp/production.mjs +198 -0
  162. package/plugin-src/host/channels/whatsapp/rpc.mjs +242 -0
  163. package/plugin-src/host/delivery-adapter.mjs +411 -0
  164. package/plugin-src/host/delivery-http.mjs +132 -0
  165. package/plugin-src/host/delivery-rpc.mjs +173 -0
  166. package/plugin-src/host/delivery-service.mjs +618 -0
  167. package/plugin-src/host/delivery-suggestions.mjs +198 -0
  168. package/plugin-src/host/harness-command-executor.mjs +48 -0
  169. package/plugin-src/host/harness-connection.mjs +20 -0
  170. package/plugin-src/host/harness-session-coordinator.mjs +183 -0
  171. package/plugin-src/host/host-language-rpc.mjs +71 -0
  172. package/plugin-src/host/host-language.mjs +157 -0
  173. package/plugin-src/host/image-input-rpc.mjs +24 -0
  174. package/plugin-src/host/inbound-ttl-rpc.mjs +86 -0
  175. package/plugin-src/host/inbound-ttl-runtime.mjs +85 -0
  176. package/plugin-src/host/inbound-ttl-service.mjs +179 -0
  177. package/plugin-src/host/index.mjs +229 -0
  178. package/plugin-src/host/injected-context.mjs +104 -0
  179. package/plugin-src/host/lark-sdk-handshake-patch.mjs +181 -0
  180. package/plugin-src/host/modern-harness-api.mjs +738 -0
  181. package/plugin-src/host/rpc-authority.mjs +12 -0
  182. package/plugin-src/host/session-sync-coordinator.mjs +233 -0
  183. package/plugin-src/host/session-title-prefix.mjs +122 -0
  184. package/plugin-src/host/update-policy.mjs +31 -0
  185. package/plugin-src/host/update-rpc.mjs +16 -0
  186. package/plugin-src/host/update-runtime.mjs +339 -0
  187. package/plugin-src/host/update-service.mjs +392 -0
  188. package/plugin-src/management-rpc.mjs +87 -0
  189. package/src/channels/dingtalk/config-store.mjs +284 -0
  190. package/src/channels/dingtalk/connection-error.mjs +292 -0
  191. package/src/channels/dingtalk/device-auth.mjs +240 -0
  192. package/src/channels/dingtalk/dingtalk-api.mjs +1140 -0
  193. package/src/channels/dingtalk/dingtalk-bridge.mjs +1889 -0
  194. package/src/channels/dingtalk/dingtalk-card-stream.mjs +242 -0
  195. package/src/channels/dingtalk/dingtalk-controller.mjs +852 -0
  196. package/src/channels/dingtalk/dingtalk-menu.mjs +95 -0
  197. package/src/channels/dingtalk/dingtalk-runtime.mjs +482 -0
  198. package/src/channels/dingtalk/harness-client.mjs +19 -0
  199. package/src/channels/dingtalk/state-store.mjs +325 -0
  200. package/src/channels/discord/config-store.mjs +28 -0
  201. package/src/channels/discord/discord-api.mjs +423 -0
  202. package/src/channels/discord/discord-bridge.mjs +16 -0
  203. package/src/channels/discord/discord-controller.mjs +29 -0
  204. package/src/channels/discord/discord-runtime.mjs +1091 -0
  205. package/src/channels/discord/external-consumer.mjs +199 -0
  206. package/src/channels/discord/external-files.mjs +97 -0
  207. package/src/channels/discord/harness-client.mjs +11 -0
  208. package/src/channels/discord/history-reader.mjs +142 -0
  209. package/src/channels/discord/nearby-history.mjs +112 -0
  210. package/src/channels/discord/state-store.mjs +3 -0
  211. package/src/channels/email/config-store.mjs +194 -0
  212. package/src/channels/email/email-api.mjs +20 -0
  213. package/src/channels/email/email-bridge.mjs +17 -0
  214. package/src/channels/email/email-controller.mjs +824 -0
  215. package/src/channels/email/email-runtime.mjs +699 -0
  216. package/src/channels/email/harness-client.mjs +7 -0
  217. package/src/channels/email/mail-format.mjs +91 -0
  218. package/src/channels/email/state-store.mjs +176 -0
  219. package/src/channels/email/transport.mjs +64 -0
  220. package/src/channels/email/transports/agent-mail.mjs +604 -0
  221. package/src/channels/email/transports/agently-cli.mjs +288 -0
  222. package/src/channels/email/transports/imap-smtp.mjs +165 -0
  223. package/src/channels/feishu/bridge.mjs +6286 -0
  224. package/src/channels/feishu/config.mjs +71 -0
  225. package/src/channels/feishu/external-cards.mjs +87 -0
  226. package/src/channels/feishu/external-consumer.mjs +89 -0
  227. package/src/channels/feishu/external-files.mjs +115 -0
  228. package/src/channels/feishu/external-images.mjs +50 -0
  229. package/src/channels/feishu/external-names.mjs +99 -0
  230. package/src/channels/feishu/feishu-app.mjs +54 -0
  231. package/src/channels/feishu/feishu-cards.mjs +1285 -0
  232. package/src/channels/feishu/feishu-channel.mjs +651 -0
  233. package/src/channels/feishu/feishu-runtime.mjs +982 -0
  234. package/src/channels/feishu/group-message-permission-manager.mjs +72 -0
  235. package/src/channels/feishu/group-response-mode.mjs +15 -0
  236. package/src/channels/feishu/harness-client.mjs +19 -0
  237. package/src/channels/feishu/history-reader.mjs +77 -0
  238. package/src/channels/feishu/live-cot.mjs +260 -0
  239. package/src/channels/feishu/message-utils.mjs +613 -0
  240. package/src/channels/feishu/multi-bot-controller.mjs +1619 -0
  241. package/src/channels/feishu/nearby-history.mjs +72 -0
  242. package/src/channels/feishu/plugin-config-store.mjs +216 -0
  243. package/src/channels/feishu/plugin-controller.mjs +254 -0
  244. package/src/channels/feishu/registration-manager.mjs +349 -0
  245. package/src/channels/feishu/repair-manager.mjs +138 -0
  246. package/src/channels/feishu/reply-context.mjs +22 -0
  247. package/src/channels/feishu/slash-command-panel.mjs +155 -0
  248. package/src/channels/feishu/slash-command-registry.mjs +495 -0
  249. package/src/channels/feishu/state-store.mjs +214 -0
  250. package/src/channels/feishu/step-push-mode.mjs +25 -0
  251. package/src/channels/feishu/voice-config.mjs +34 -0
  252. package/src/channels/feishu/voice.mjs +134 -0
  253. package/src/channels/imessage/config-store.mjs +24 -0
  254. package/src/channels/imessage/controller.mjs +37 -0
  255. package/src/channels/imessage/harness-client.mjs +7 -0
  256. package/src/channels/imessage/imessage-api.mjs +194 -0
  257. package/src/channels/imessage/imessage-bridge.mjs +16 -0
  258. package/src/channels/imessage/runtime.mjs +102 -0
  259. package/src/channels/imessage/state-store.mjs +3 -0
  260. package/src/channels/matrix/matrix-api.mjs +696 -0
  261. package/src/channels/matrix/matrix-bridge.mjs +20 -0
  262. package/src/channels/matrix/matrix-config-store.mjs +368 -0
  263. package/src/channels/matrix/matrix-controller.mjs +404 -0
  264. package/src/channels/matrix/matrix-crypto-store.mjs +279 -0
  265. package/src/channels/matrix/matrix-crypto.mjs +1014 -0
  266. package/src/channels/matrix/matrix-harness-client.mjs +11 -0
  267. package/src/channels/matrix/matrix-normalize.mjs +357 -0
  268. package/src/channels/matrix/matrix-rich-text.mjs +313 -0
  269. package/src/channels/matrix/matrix-room-history.mjs +316 -0
  270. package/src/channels/matrix/matrix-runtime.mjs +1014 -0
  271. package/src/channels/office/config-store.mjs +107 -0
  272. package/src/channels/office/office-controller.mjs +178 -0
  273. package/src/channels/office/office-job-executor.mjs +391 -0
  274. package/src/channels/office/office-runtime.mjs +175 -0
  275. package/src/channels/office/office-transport.mjs +171 -0
  276. package/src/channels/office/protocol.mjs +43 -0
  277. package/src/channels/qq/config-store.mjs +173 -0
  278. package/src/channels/qq/harness-client.mjs +11 -0
  279. package/src/channels/qq/markdown-reply.mjs +446 -0
  280. package/src/channels/qq/qq-bridge.mjs +1507 -0
  281. package/src/channels/qq/qq-controller.mjs +514 -0
  282. package/src/channels/qq/qq-menu.mjs +208 -0
  283. package/src/channels/qq/qq-runtime.mjs +303 -0
  284. package/src/channels/qq/qr-auth.mjs +24 -0
  285. package/src/channels/qq/state-error.mjs +17 -0
  286. package/src/channels/qq/state-store.mjs +143 -0
  287. package/src/channels/shared/access-policy.mjs +210 -0
  288. package/src/channels/shared/agent-preset.mjs +76 -0
  289. package/src/channels/shared/batch-input.mjs +229 -0
  290. package/src/channels/shared/bot-alias.mjs +29 -0
  291. package/src/channels/shared/bot-workspace-store.mjs +2383 -0
  292. package/src/channels/shared/command-catalog.mjs +107 -0
  293. package/src/channels/shared/command-permission.mjs +32 -0
  294. package/src/channels/shared/compact-command.mjs +109 -0
  295. package/src/channels/shared/config-read-error.mjs +24 -0
  296. package/src/channels/shared/connection-error.mjs +191 -0
  297. package/src/channels/shared/connection-test.mjs +58 -0
  298. package/src/channels/shared/context-enhancement.mjs +372 -0
  299. package/src/channels/shared/control-command.mjs +111 -0
  300. package/src/channels/shared/conversation-state-store.mjs +165 -0
  301. package/src/channels/shared/default-workspace.mjs +31 -0
  302. package/src/channels/shared/deferred-delivery-coordinator.mjs +241 -0
  303. package/src/channels/shared/deferred-delivery.mjs +161 -0
  304. package/src/channels/shared/deferred-state.mjs +53 -0
  305. package/src/channels/shared/diagnostic-details.mjs +95 -0
  306. package/src/channels/shared/editable-message-stream.mjs +100 -0
  307. package/src/channels/shared/exclusive-inbound-consumers.mjs +91 -0
  308. package/src/channels/shared/file-download.mjs +64 -0
  309. package/src/channels/shared/harness-approval.mjs +508 -0
  310. package/src/channels/shared/harness-client.mjs +2143 -0
  311. package/src/channels/shared/harness-mux.mjs +123 -0
  312. package/src/channels/shared/harness-question.mjs +95 -0
  313. package/src/channels/shared/harness-session-binding.mjs +119 -0
  314. package/src/channels/shared/history-command.mjs +193 -0
  315. package/src/channels/shared/i18n-en/diagnostics.mjs +55 -0
  316. package/src/channels/shared/i18n-en/dingtalk.mjs +81 -0
  317. package/src/channels/shared/i18n-en/discord.mjs +10 -0
  318. package/src/channels/shared/i18n-en/email.mjs +25 -0
  319. package/src/channels/shared/i18n-en/feishu.mjs +404 -0
  320. package/src/channels/shared/i18n-en/image-input.mjs +18 -0
  321. package/src/channels/shared/i18n-en/matrix.mjs +85 -0
  322. package/src/channels/shared/i18n-en/office.mjs +23 -0
  323. package/src/channels/shared/i18n-en/qq.mjs +96 -0
  324. package/src/channels/shared/i18n-en/shared-a.mjs +241 -0
  325. package/src/channels/shared/i18n-en/shared-b.mjs +263 -0
  326. package/src/channels/shared/i18n-en/shared-c.mjs +203 -0
  327. package/src/channels/shared/i18n-en/slack.mjs +27 -0
  328. package/src/channels/shared/i18n-en/telegram.mjs +27 -0
  329. package/src/channels/shared/i18n-en/wecom-app.mjs +33 -0
  330. package/src/channels/shared/i18n-en/wecom.mjs +62 -0
  331. package/src/channels/shared/i18n-en/weixin.mjs +88 -0
  332. package/src/channels/shared/i18n-en/whatsapp.mjs +17 -0
  333. package/src/channels/shared/i18n-en.mjs +45 -0
  334. package/src/channels/shared/i18n.mjs +78 -0
  335. package/src/channels/shared/im-source-guidance.mjs +65 -0
  336. package/src/channels/shared/image-input-policy.mjs +35 -0
  337. package/src/channels/shared/image-input-settings-store.mjs +60 -0
  338. package/src/channels/shared/image-input.mjs +124 -0
  339. package/src/channels/shared/image-prompt.mjs +360 -0
  340. package/src/channels/shared/inbound-access.mjs +43 -0
  341. package/src/channels/shared/inbound-file.mjs +323 -0
  342. package/src/channels/shared/inbound-ttl-store.mjs +110 -0
  343. package/src/channels/shared/inbound-ttl.mjs +22 -0
  344. package/src/channels/shared/injected-context.mjs +362 -0
  345. package/src/channels/shared/interface-language-store.mjs +127 -0
  346. package/src/channels/shared/interface-language.mjs +51 -0
  347. package/src/channels/shared/message-failure.mjs +257 -0
  348. package/src/channels/shared/model-command.mjs +741 -0
  349. package/src/channels/shared/model-setting.mjs +145 -0
  350. package/src/channels/shared/preset-command.mjs +308 -0
  351. package/src/channels/shared/semantic/artifact-delivery.mjs +177 -0
  352. package/src/channels/shared/semantic/artifact.mjs +750 -0
  353. package/src/channels/shared/semantic/delivery.mjs +193 -0
  354. package/src/channels/shared/semantic/reply-reference.mjs +148 -0
  355. package/src/channels/shared/session-binding-lock.mjs +40 -0
  356. package/src/channels/shared/session-channel-labels.mjs +28 -0
  357. package/src/channels/shared/session-reply-recovery.mjs +104 -0
  358. package/src/channels/shared/session-sync-registry.mjs +22 -0
  359. package/src/channels/shared/session-title.mjs +74 -0
  360. package/src/channels/shared/status-reaction.mjs +107 -0
  361. package/src/channels/shared/text-harness-bridge.mjs +1623 -0
  362. package/src/channels/shared/token-bot-controller.mjs +529 -0
  363. package/src/channels/shared/token-config-store.mjs +208 -0
  364. package/src/channels/shared/video-media.mjs +9 -0
  365. package/src/channels/shared/workspace-command.mjs +532 -0
  366. package/src/channels/shared/workspace-session.mjs +196 -0
  367. package/src/channels/slack/config-store.mjs +177 -0
  368. package/src/channels/slack/external-consumer.mjs +93 -0
  369. package/src/channels/slack/external-files.mjs +90 -0
  370. package/src/channels/slack/harness-client.mjs +11 -0
  371. package/src/channels/slack/history-reader.mjs +203 -0
  372. package/src/channels/slack/manifest.mjs +37 -0
  373. package/src/channels/slack/slack-api.mjs +644 -0
  374. package/src/channels/slack/slack-bridge.mjs +20 -0
  375. package/src/channels/slack/slack-controller.mjs +468 -0
  376. package/src/channels/slack/slack-runtime.mjs +895 -0
  377. package/src/channels/slack/state-store.mjs +3 -0
  378. package/src/channels/telegram/config-store.mjs +89 -0
  379. package/src/channels/telegram/harness-client.mjs +11 -0
  380. package/src/channels/telegram/state-store.mjs +3 -0
  381. package/src/channels/telegram/telegram-api.mjs +486 -0
  382. package/src/channels/telegram/telegram-bridge.mjs +81 -0
  383. package/src/channels/telegram/telegram-controller.mjs +52 -0
  384. package/src/channels/telegram/telegram-http.mjs +21 -0
  385. package/src/channels/telegram/telegram-rich-message.mjs +149 -0
  386. package/src/channels/telegram/telegram-runtime.mjs +1471 -0
  387. package/src/channels/wecom/config-store.mjs +172 -0
  388. package/src/channels/wecom/harness-client.mjs +11 -0
  389. package/src/channels/wecom/qr-auth.mjs +95 -0
  390. package/src/channels/wecom/send-error.mjs +31 -0
  391. package/src/channels/wecom/state-store.mjs +107 -0
  392. package/src/channels/wecom/wecom-bridge.mjs +1833 -0
  393. package/src/channels/wecom/wecom-cards.mjs +101 -0
  394. package/src/channels/wecom/wecom-controller.mjs +530 -0
  395. package/src/channels/wecom/wecom-runtime.mjs +269 -0
  396. package/src/channels/wecom-app/callback-server.mjs +471 -0
  397. package/src/channels/wecom-app/config-store.mjs +242 -0
  398. package/src/channels/wecom-app/harness-client.mjs +11 -0
  399. package/src/channels/wecom-app/state-store.mjs +107 -0
  400. package/src/channels/wecom-app/wecom-app-api.mjs +432 -0
  401. package/src/channels/wecom-app/wecom-app-bridge.mjs +1072 -0
  402. package/src/channels/wecom-app/wecom-app-controller.mjs +453 -0
  403. package/src/channels/wecom-app/wecom-app-runtime.mjs +222 -0
  404. package/src/channels/weixin/config-store.mjs +194 -0
  405. package/src/channels/weixin/connection-error.en.mjs +137 -0
  406. package/src/channels/weixin/connection-error.mjs +153 -0
  407. package/src/channels/weixin/diagnostic-details.mjs +1 -0
  408. package/src/channels/weixin/external-consumer.mjs +160 -0
  409. package/src/channels/weixin/external-files.mjs +84 -0
  410. package/src/channels/weixin/external-typing.mjs +120 -0
  411. package/src/channels/weixin/harness-client.mjs +19 -0
  412. package/src/channels/weixin/state-store.mjs +316 -0
  413. package/src/channels/weixin/weixin-api.mjs +1009 -0
  414. package/src/channels/weixin/weixin-bridge.mjs +1537 -0
  415. package/src/channels/weixin/weixin-controller.mjs +779 -0
  416. package/src/channels/weixin/weixin-runtime.mjs +507 -0
  417. package/src/channels/whatsapp/config-store.mjs +210 -0
  418. package/src/channels/whatsapp/harness-client.mjs +11 -0
  419. package/src/channels/whatsapp/state-store.mjs +3 -0
  420. package/src/channels/whatsapp/whatsapp-bridge.mjs +18 -0
  421. package/src/channels/whatsapp/whatsapp-controller.mjs +470 -0
  422. package/src/channels/whatsapp/whatsapp-runtime.mjs +917 -0
  423. package/src/channels/whatsapp/whatsapp-web-session.mjs +214 -0
@@ -0,0 +1,71 @@
1
+ import { execFileSync } from 'node:child_process';
2
+ import { resolve } from 'node:path';
3
+
4
+ function required(name, value) {
5
+ if (typeof value !== 'string' || value.trim() === '') {
6
+ throw new Error(`Missing required configuration: ${name}`);
7
+ }
8
+ return value.trim();
9
+ }
10
+
11
+ function readSecret(appId) {
12
+ if (process.env.FEISHU_APP_SECRET?.trim()) return process.env.FEISHU_APP_SECRET.trim();
13
+
14
+ const service = process.env.FEISHU_SECRET_SERVICE?.trim();
15
+ if (!service) throw new Error('Missing FEISHU_APP_SECRET or FEISHU_SECRET_SERVICE');
16
+
17
+ try {
18
+ return execFileSync('/usr/bin/security', [
19
+ 'find-generic-password',
20
+ '-a',
21
+ appId,
22
+ '-s',
23
+ service,
24
+ '-w',
25
+ ], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] }).trim();
26
+ } catch {
27
+ throw new Error(`Unable to read Feishu app secret from macOS Keychain service ${service}`);
28
+ }
29
+ }
30
+
31
+ function bool(name, fallback) {
32
+ const raw = process.env[name];
33
+ if (raw === undefined) return fallback;
34
+ return !['0', 'false', 'no', 'off'].includes(raw.trim().toLowerCase());
35
+ }
36
+
37
+ function csvSet(value) {
38
+ return new Set((value ?? '')
39
+ .split(',')
40
+ .map((item) => item.trim())
41
+ .filter(Boolean));
42
+ }
43
+
44
+ function requiredCsvSet(name, value) {
45
+ const items = csvSet(value);
46
+ if (items.size === 0) throw new Error(`Missing required configuration: ${name}`);
47
+ return items;
48
+ }
49
+
50
+ export function loadConfig() {
51
+ const appId = required('FEISHU_APP_ID', process.env.FEISHU_APP_ID);
52
+ const appSecret = required('FEISHU_APP_SECRET', readSecret(appId));
53
+ const workspace = required('HARNESS_WORKSPACE', process.env.HARNESS_WORKSPACE);
54
+
55
+ return Object.freeze({
56
+ appId,
57
+ appSecret,
58
+ harnessBaseUrl: new URL(process.env.HARNESS_BASE_URL ?? 'http://127.0.0.1:3080'),
59
+ harnessWorkspace: resolve(workspace),
60
+ harnessAgentPreset: process.env.HARNESS_AGENT_PRESET?.trim() || 'standard',
61
+ harnessAutostart: bool('HARNESS_AUTOSTART', true),
62
+ dshBin: process.env.DSH_BIN?.trim() || 'dsh',
63
+ healthPort: Number.parseInt(process.env.BRIDGE_HEALTH_PORT ?? '3091', 10),
64
+ statePath: resolve(process.env.BRIDGE_STATE_PATH ?? './data/state.json'),
65
+ replyTimeoutMs: Number.parseInt(process.env.HARNESS_REPLY_TIMEOUT_MS ?? '600000', 10),
66
+ allowedSenderOpenIds: requiredCsvSet(
67
+ 'FEISHU_ALLOWED_OPEN_IDS',
68
+ process.env.FEISHU_ALLOWED_OPEN_IDS,
69
+ ),
70
+ });
71
+ }
@@ -0,0 +1,87 @@
1
+ import { qualifyExternalReply } from './reply-context.mjs';
2
+
3
+ const refuse = (code) => { throw Object.assign(new Error(code), { code }); };
4
+ const uuid = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
5
+ const identifier = (value, prefix) => typeof value === 'string' && value.length <= 512 && new RegExp(`^${prefix}_[A-Za-z0-9]+$`).test(value);
6
+ const text = (value, max) => typeof value === 'string' && value.length > 0 && value.length <= max;
7
+
8
+ export function externalApprovalCard(value) {
9
+ if (!value || !uuid.test(value.requestId ?? '') || !text(value.title, 120) || !text(value.detail, 16000)
10
+ || !['pending', 'web-required', 'allowed-once', 'rejected', 'expired', 'executed', 'execution-failed', 'execution-unknown', 'test'].includes(value.status)) refuse('bad-request');
11
+ const elements = [{ tag: 'div', text: { tag: 'plain_text', content: value.detail } }];
12
+ if (value.status === 'pending') elements.push({ tag: 'action', actions: [
13
+ { tag: 'button', type: 'primary', text: { tag: 'plain_text', content: 'Allow once / 允许一次' }, value: { namespace: 'botharness/approval-v1', requestId: value.requestId, action: 'allowed-once' } },
14
+ { tag: 'button', type: 'danger', text: { tag: 'plain_text', content: 'Reject / 拒绝' }, value: { namespace: 'botharness/approval-v1', requestId: value.requestId, action: 'rejected' } },
15
+ ] });
16
+ return JSON.stringify({ config: { wide_screen_mode: true }, header: { title: { tag: 'plain_text', content: value.title }, template: 'blue' }, elements });
17
+ }
18
+
19
+ export function normalizeExternalCardAction(event, identity) {
20
+ const appId = event?.header?.app_id ?? event?.app_id;
21
+ if (appId !== undefined && appId !== identity.appId) refuse('account-changed');
22
+ const value = event?.action?.value;
23
+ if (value?.namespace !== 'botharness/approval-v1') return null;
24
+ const actorId = event?.operator?.open_id;
25
+ const messageId = event?.context?.open_message_id;
26
+ const conversationId = event?.context?.open_chat_id;
27
+ if (!identifier(actorId, 'ou') || !identifier(messageId, 'om') || !identifier(conversationId, 'oc')
28
+ || event.action.tag !== 'button' || !uuid.test(value.requestId ?? '')
29
+ || !['allowed-once', 'rejected'].includes(value.action)
30
+ || !/^[a-f0-9]{64}$/.test(identity.fingerprint ?? '')) refuse('invalid-inbound');
31
+ return Object.freeze({ version: 1, channel: 'feishu', botId: identity.botId, fingerprint: identity.fingerprint,
32
+ actorId, messageId, conversationId, requestId: value.requestId, action: value.action });
33
+ }
34
+
35
+ export async function replyExternalApprovalCard(client, route, card, { signal, assertCurrent, beforeSend } = {}) {
36
+ const content = externalApprovalCard(card);
37
+ assertCurrent?.();
38
+ const current = await qualifyExternalReply(client, route, signal);
39
+ if (current.actorId !== route.actorId) refuse('stale-route');
40
+ const chat = await client.im.v1.chat.get({ path: { chat_id: route.conversationId } }, { signal });
41
+ if (chat?.code !== 0 || chat?.data?.chat_mode !== 'p2p') refuse('stale-route');
42
+ assertCurrent?.();
43
+ signal?.throwIfAborted();
44
+ if (typeof beforeSend !== 'function' || beforeSend() !== true) refuse('stale-route');
45
+ const result = await client.im.v1.message.create({ params: { receive_id_type: 'chat_id' },
46
+ data: { receive_id: route.conversationId, msg_type: 'interactive', content, uuid: card.requestId } }, { signal });
47
+ if (typeof result?.code !== 'number') refuse('send-result-unknown');
48
+ if (result.code !== 0) refuse('card-provider-rejected');
49
+ if (!identifier(result.data?.message_id, 'om') || result.data?.chat_id !== route.conversationId) refuse('send-result-unknown');
50
+ return { sent: true, receipt: { version: 1, messageId: result.data.message_id, conversationId: result.data.chat_id } };
51
+ }
52
+
53
+ export async function updateExternalApprovalCard(client, identity, receipt, card, { signal, assertCurrent, beforeSend } = {}) {
54
+ const content = externalApprovalCard(card);
55
+ if (!identifier(receipt?.messageId, 'om') || !identifier(receipt?.conversationId, 'oc')) refuse('bad-request');
56
+ assertCurrent?.();
57
+ const result = await client.im.v1.message.get({ path: { message_id: receipt.messageId } }, { signal });
58
+ const source = result?.data?.items?.find(item => item.message_id === receipt.messageId);
59
+ const ownSender = source?.sender?.sender_type === 'app' &&
60
+ ((source.sender.id_type === 'app_id' && source.sender.id === identity.appId) ||
61
+ (source.sender.id_type === 'open_id' && source.sender.id === identity.botOpenId));
62
+ if (result?.code !== 0 || !ownSender || source.deleted || source.chat_id !== receipt.conversationId || source.msg_type !== 'interactive') refuse('stale-route');
63
+ assertCurrent?.();
64
+ signal?.throwIfAborted();
65
+ if (typeof beforeSend !== 'function' || beforeSend() !== true) refuse('stale-route');
66
+ const patched = await client.im.v1.message.patch({ path: { message_id: receipt.messageId }, data: { content } }, { signal });
67
+ if (typeof patched?.code !== 'number') refuse('send-result-unknown');
68
+ if (patched.code !== 0) refuse('card-provider-rejected');
69
+ return { updated: true };
70
+ }
71
+
72
+ export async function consumeExternalCardAction(accept, event, signal, timeoutMs = 2000) {
73
+ const deadline = new AbortController();
74
+ const lifetime = signal ? AbortSignal.any([signal, deadline.signal]) : deadline.signal;
75
+ let timer;
76
+ try {
77
+ return await Promise.race([
78
+ Promise.resolve().then(() => accept(event, { signal: lifetime })),
79
+ new Promise((_, reject) => {
80
+ timer = setTimeout(() => { deadline.abort(); reject(new Error('action-deadline')); }, timeoutMs);
81
+ timer.unref?.();
82
+ }),
83
+ ]);
84
+ } catch {
85
+ return { toast: { type: 'error', content: 'Request not accepted / 请求未被接受' } };
86
+ } finally { clearTimeout(timer); }
87
+ }
@@ -0,0 +1,89 @@
1
+ import { nativeImageContent } from './external-images.mjs';
2
+ function refusal(code) {
3
+ const error = new Error(code);
4
+ error.code = code;
5
+ return error;
6
+ }
7
+
8
+ function identifier(value) {
9
+ return typeof value === 'string' && value.length > 0 && value.length <= 512 ? value : undefined;
10
+ }
11
+
12
+ export function normalizeExternalText(event, { botId, appId, botOpenId, fingerprint }) {
13
+ const message = event?.message;
14
+ const actorId = identifier(event?.sender?.sender_id?.open_id);
15
+ const messageId = identifier(message?.message_id);
16
+ const conversationId = identifier(message?.chat_id);
17
+ const eventId = identifier(event?.event_id ?? event?.header?.event_id);
18
+ const eventApp = event?.app_id ?? event?.header?.app_id;
19
+ if (eventApp !== undefined && eventApp !== appId) throw refusal('account-changed');
20
+ if (event?.sender?.sender_type === 'app' || event?.sender?.sender_type === 'bot') return null;
21
+ if (event?.sender?.sender_type !== 'user'
22
+ || !['p2p', 'group'].includes(message?.chat_type) || message?.message_type !== 'text') return null;
23
+ if (message.chat_type === 'group' && !identifier(botOpenId)) throw refusal('account-unverified');
24
+ if (message.mentions !== undefined && (!Array.isArray(message.mentions) || message.mentions.length > 100))
25
+ throw refusal('invalid-inbound');
26
+ const mentions = Object.freeze((message.mentions ?? []).map(mention => {
27
+ const id = identifier(mention?.id?.open_id ?? mention?.open_id);
28
+ const key = identifier(mention?.key);
29
+ if (!id || !key) throw refusal('invalid-inbound');
30
+ const name = identifier(mention?.name);
31
+ return Object.freeze({ id, key, ...(name ? { name } : {}) });
32
+ }));
33
+ if (!actorId || !messageId || !conversationId || !eventId
34
+ || !/^[a-f0-9]{64}$/.test(fingerprint ?? '')) throw refusal('invalid-inbound');
35
+ let text;
36
+ try { text = JSON.parse(message.content)?.text; } catch { throw refusal('invalid-inbound'); }
37
+ if (typeof text !== 'string' || !text.trim() || text.length > 16000) throw refusal('invalid-inbound');
38
+ const milliseconds = Number(message.create_time);
39
+ const timestamp = new Date(milliseconds);
40
+ if (!Number.isFinite(milliseconds) || milliseconds <= 0 || !Number.isFinite(timestamp.getTime()))
41
+ throw refusal('invalid-inbound');
42
+ const route = Object.freeze({
43
+ messageId, conversationId, actorId,
44
+ ...(identifier(message.thread_id) ? { threadId: message.thread_id } : {}),
45
+ ...(identifier(message.root_id) ? { rootId: message.root_id } : {}),
46
+ ...(identifier(message.parent_id) ? { parentId: message.parent_id } : {}),
47
+ });
48
+ return Object.freeze({
49
+ version: 1, channel: 'feishu', botId, fingerprint, eventId, messageId,
50
+ actor: Object.freeze({ kind: 'user', id: actorId,
51
+ ...(identifier(event.sender.sender_name) ? { name: event.sender.sender_name } : {}) }),
52
+ conversation: Object.freeze({ kind: message.chat_type === 'group' ? 'group' : 'dm', id: conversationId }),
53
+ mentions, mentionedAccount: mentions.some(mention => mention.id === botOpenId),
54
+ at: timestamp.toISOString(), text, reply: route,
55
+ replay: Object.freeze({ kind: 'provider-redelivery', resumeCursor: false, gapPossible: true }),
56
+ });
57
+ }
58
+
59
+ export function normalizeExternalMedia(event, identity) {
60
+ const message = event?.message;
61
+ if (!['image', 'post'].includes(message?.message_type)) return normalizeExternalText(event, identity);
62
+ if (event?.sender?.sender_type !== 'user') return null;
63
+ const media = nativeImageContent(message, message.chat_id);
64
+ if (!media) return null;
65
+ const base = normalizeExternalText({ ...event, message: { ...message, message_type: 'text', content: JSON.stringify({ text: media.text }) } }, identity);
66
+ if (!base) return null;
67
+ return Object.freeze({ ...base, attachments: Object.freeze(media.attachments.map(Object.freeze)), contentParts: Object.freeze(media.contentParts.map(Object.freeze)) });
68
+ }
69
+
70
+ export function normalizeOwnTextEcho(event, { botId, appId, botOpenId, fingerprint }) {
71
+ if (!['app', 'bot'].includes(event?.sender?.sender_type)) return null;
72
+ if (event?.sender?.sender_id?.open_id !== botOpenId && event?.sender?.sender_id?.app_id !== appId) return null;
73
+ const message = event?.message;
74
+ if (message?.chat_type !== 'group' || message?.message_type !== 'text') return null;
75
+ const eventApp = event?.app_id ?? event?.header?.app_id;
76
+ if (eventApp !== undefined && eventApp !== appId) throw refusal('account-changed');
77
+ let text;
78
+ try { text = JSON.parse(message.content)?.text; } catch { throw refusal('invalid-inbound'); }
79
+ const eventId = identifier(event?.event_id ?? event?.header?.event_id);
80
+ const messageId = identifier(message.message_id);
81
+ const conversationId = identifier(message.chat_id);
82
+ const at = new Date(Number(message.create_time));
83
+ if (!eventId || !messageId || !conversationId || typeof text !== 'string' || !text.trim()
84
+ || text.length > 16000 || !Number.isFinite(at.getTime()) || !/^[a-f0-9]{64}$/.test(fingerprint ?? ''))
85
+ throw refusal('invalid-inbound');
86
+ return Object.freeze({ version: 1, botId, fingerprint, eventId, messageId, conversationId, text, at: at.toISOString() });
87
+ }
88
+
89
+ export { ExclusiveInboundConsumers } from '../shared/exclusive-inbound-consumers.mjs';
@@ -0,0 +1,115 @@
1
+ import { nativeImageContent } from './external-images.mjs';
2
+ import { createHash } from 'node:crypto';
3
+ import { VerifiedFeishuChannel } from './feishu-channel.mjs';
4
+
5
+ const MAX_BYTES = 25 * 1024 * 1024;
6
+ const failure = (code) => Object.assign(new Error(code), { code });
7
+
8
+ export async function checkedReplySource(client, route, signal) {
9
+ signal?.throwIfAborted();
10
+ if (!route || !route.messageId || !route.conversationId || !route.actorId) throw failure('bad-request');
11
+ const current = await client.im.v1.message.get({ path: { message_id: route.messageId } });
12
+ const source = current?.data?.items?.find(item => item.message_id === route.messageId);
13
+ if (current?.code || !source || source.deleted === true || source.chat_id !== route.conversationId
14
+ || source.sender?.sender_type !== 'user' || source.sender?.id_type !== 'open_id'
15
+ || source.sender?.id !== route.actorId || (source.thread_id || undefined) !== route.threadId
16
+ || (source.root_id || undefined) !== route.rootId || (source.parent_id || undefined) !== route.parentId)
17
+ throw failure('stale-route');
18
+ signal?.throwIfAborted();
19
+ return source;
20
+ }
21
+
22
+ async function parentFile(client, route, signal) {
23
+ if (!route.parentId || route.parentId === route.messageId) return undefined;
24
+ signal?.throwIfAborted();
25
+ const result = await client.im.v1.message.get({ path: { message_id: route.parentId } });
26
+ const file = result?.data?.items?.find(item => item.message_id === route.parentId);
27
+ if (result?.code || !file || file.deleted === true || file.chat_id !== route.conversationId)
28
+ throw failure('stale-route');
29
+ if (file.msg_type !== 'file') return undefined;
30
+ if (file.thread_id && route.threadId && file.thread_id !== route.threadId) throw failure('stale-route');
31
+ let content;
32
+ try { content = JSON.parse(file.body?.content); } catch { throw failure('invalid-inbound'); }
33
+ if (typeof content?.file_key !== 'string' || !content.file_key || content.file_key.length > 512
34
+ || typeof content.file_name !== 'string' || !content.file_name || content.file_name.length > 512)
35
+ throw failure('invalid-inbound');
36
+ signal?.throwIfAborted();
37
+ return Object.freeze({
38
+ id: createHash('sha256').update(JSON.stringify([route.conversationId, file.message_id, content.file_key])).digest('hex'),
39
+ messageId: file.message_id, resourceKey: content.file_key, name: content.file_name,
40
+ });
41
+ }
42
+
43
+ export async function externalAttachments(client, evidence, signal) {
44
+ if (evidence.attachments?.length || !evidence.mentionedAccount || !evidence.reply.parentId) return evidence;
45
+ const file = await parentFile(client, evidence.reply, signal);
46
+ return file ? Object.freeze({ ...evidence, attachments: Object.freeze([file]) }) : evidence;
47
+ }
48
+
49
+ export async function readExternalFile(client, route, attachment, { signal, assertCurrent }) {
50
+ const source = await checkedReplySource(client, route, signal);
51
+ const ownImages = nativeImageContent(source, route.conversationId);
52
+ const current = attachment.mediaType === 'image/unknown'
53
+ ? ownImages?.attachments.find(item => item.id === attachment.id)
54
+ : await parentFile(client, route, signal);
55
+ if (!current || JSON.stringify(current) !== JSON.stringify(attachment)) throw failure('stale-route');
56
+ assertCurrent();
57
+ const resource = await client.im.v1.messageResource.get({
58
+ path: { message_id: current.messageId, file_key: current.resourceKey }, params: { type: current.mediaType === 'image/unknown' ? 'image' : 'file' },
59
+ });
60
+ signal?.throwIfAborted();
61
+ assertCurrent();
62
+ const stream = resource?.getReadableStream?.();
63
+ if (!stream || typeof stream[Symbol.asyncIterator] !== 'function') throw failure('resource-unavailable');
64
+ const declared = Number(resource?.headers?.['content-length']);
65
+ if (Number.isFinite(declared) && declared > MAX_BYTES) {
66
+ stream.destroy?.();
67
+ throw failure('artifact-too-large');
68
+ }
69
+ const cancel = () => stream.destroy?.();
70
+ signal?.addEventListener('abort', cancel, { once: true });
71
+ return (async function* () {
72
+ let size = 0;
73
+ try {
74
+ signal?.throwIfAborted();
75
+ for await (const chunk of stream) {
76
+ signal?.throwIfAborted();
77
+ assertCurrent();
78
+ size += chunk.byteLength;
79
+ if (size > MAX_BYTES) throw failure('artifact-too-large');
80
+ yield new Uint8Array(chunk);
81
+ }
82
+ signal?.throwIfAborted();
83
+ assertCurrent();
84
+ } finally {
85
+ signal?.removeEventListener('abort', cancel);
86
+ stream.destroy?.();
87
+ }
88
+ })();
89
+ }
90
+
91
+ export async function replyExternalFile(client, route, file, { signal, assertCurrent }) {
92
+ if (!file || typeof file.id !== 'string' || !file.id || typeof file.name !== 'string'
93
+ || !file.name || !(file.bytes instanceof Uint8Array) || file.bytes.byteLength < 1
94
+ || file.bytes.byteLength > MAX_BYTES) throw failure('bad-request');
95
+ await checkedReplySource(client, route, signal);
96
+ assertCurrent();
97
+ const channel = new VerifiedFeishuChannel({ client });
98
+ try {
99
+ await channel.sendFile(route.conversationId, {
100
+ artifactId: file.id, deliveryKey: file.id, fileName: file.name, bytes: Buffer.from(file.bytes),
101
+ }, { replyTo: route.messageId, replyInThread: Boolean(route.threadId), signal,
102
+ retryUncertain: false,
103
+ beforeSend: async () => {
104
+ await checkedReplySource(client, route, signal);
105
+ assertCurrent();
106
+ },
107
+ });
108
+ return { sent: true };
109
+ } catch (error) {
110
+ if (error?.code === 'artifact-provider-failed') throw failure('file-upload-failed');
111
+ if (['artifact-permission-required', 'artifact-too-large', 'artifact-empty', 'artifact-provider-rejected', 'artifact-rate-limited'].includes(error?.code))
112
+ throw failure('file-provider-rejected');
113
+ throw error;
114
+ }
115
+ }
@@ -0,0 +1,50 @@
1
+ import { createHash } from 'node:crypto';
2
+
3
+ const failure = () => Object.assign(new Error('invalid-inbound'), { code: 'invalid-inbound' });
4
+ const key = value => typeof value === 'string' && value.length > 0 && value.length <= 512;
5
+
6
+ // Native image keys are private resource selectors, not browser URLs.
7
+ export function nativeImageContent(message, conversationId) {
8
+ const type = message.message_type ?? message.msg_type;
9
+ if (!['image', 'post'].includes(type)) return undefined;
10
+ let value;
11
+ try { value = JSON.parse(message.content ?? message.body?.content); } catch { throw failure(); }
12
+ const messageId = message.message_id;
13
+ if (!key(messageId) || !key(conversationId)) throw failure();
14
+ const attachments = [];
15
+ const contentParts = [];
16
+ const text = value => {
17
+ if (typeof value !== 'string') throw failure();
18
+ if (value) contentParts.push({ kind: 'text', text: value });
19
+ };
20
+ const image = resourceKey => {
21
+ if (!key(resourceKey)) throw failure();
22
+ const id = createHash('sha256').update(JSON.stringify([conversationId, messageId, resourceKey])).digest('hex');
23
+ if (attachments.some(item => item.id === id)) { contentParts.push({ kind: 'attachment', id }); return; }
24
+ if (attachments.length >= 32) throw failure();
25
+ attachments.push({ id, messageId, resourceKey, name: 'image', mediaType: 'image/unknown' });
26
+ contentParts.push({ kind: 'attachment', id });
27
+ };
28
+ if (type === 'image') image(value?.image_key);
29
+ else {
30
+ // The get-message API returns one resolved post; receive events may carry locales.
31
+ const post = Array.isArray(value?.content) ? value : Object.values(value ?? {}).find(item => Array.isArray(item?.content));
32
+ if (!post || post.content.length > 128) throw failure();
33
+ if (post.title !== undefined && post.title !== '') { text(post.title); text('\n'); }
34
+ for (let rowIndex = 0; rowIndex < post.content.length; rowIndex++) {
35
+ const row = post.content[rowIndex];
36
+ if (!Array.isArray(row) || row.length > 128) throw failure();
37
+ for (const item of row) {
38
+ if (item?.tag === 'img') image(item.image_key);
39
+ else if (['text', 'a'].includes(item?.tag)) text(item.text ?? '');
40
+ else if (item?.tag === 'at') { const label = item.user_name || item.user_id; if (!key(label)) throw failure(); text('@' + label); }
41
+ else throw failure(); // A narrow image slice must not silently drop other native content.
42
+ }
43
+ if (rowIndex < post.content.length - 1) text('\n');
44
+ }
45
+ if (!attachments.length) return undefined;
46
+ }
47
+ const plainText = contentParts.filter(item => item.kind === 'text').map(item => item.text).join('');
48
+ if (plainText.length > 16000 || contentParts.length > 256) throw failure();
49
+ return { text: plainText.trim() ? plainText : '[Image]', attachments, contentParts };
50
+ }
@@ -0,0 +1,99 @@
1
+ const NAME_TTL_MS = 60 * 60 * 1000;
2
+ const MISS_TTL_MS = 60 * 1000;
3
+ const CACHE_LIMIT = 1000;
4
+
5
+ function displayName(value) {
6
+ return typeof value === 'string' && value.trim() && value.length <= 512 ? value.trim() : undefined;
7
+ }
8
+
9
+ /** Display names are presentation only; a bounded per-account cache keeps them off the delivery path. */
10
+ export function createExternalNameCache({ now = Date.now } = {}) {
11
+ const entries = new Map();
12
+ return {
13
+ get(key) {
14
+ const entry = entries.get(key);
15
+ if (!entry) return undefined;
16
+ if (entry.expiresAt <= now()) { entries.delete(key); return undefined; }
17
+ return entry;
18
+ },
19
+ set(key, name) {
20
+ entries.delete(key);
21
+ entries.set(key, { name, expiresAt: now() + (name ? NAME_TTL_MS : MISS_TTL_MS) });
22
+ while (entries.size > CACHE_LIMIT) entries.delete(entries.keys().next().value);
23
+ },
24
+ };
25
+ }
26
+
27
+ async function bounded(signal, timeoutMs, lookup) {
28
+ signal?.throwIfAborted();
29
+ const controller = new AbortController();
30
+ const cancel = () => controller.abort(signal.reason);
31
+ signal?.addEventListener('abort', cancel, { once: true });
32
+ let rejectAbort;
33
+ const aborted = new Promise((_, reject) => { rejectAbort = reject; });
34
+ const onAbort = () => rejectAbort(controller.signal.reason);
35
+ controller.signal.addEventListener('abort', onAbort, { once: true });
36
+ const timer = setTimeout(() => controller.abort(new Error('metadata-timeout')), timeoutMs);
37
+ try {
38
+ const result = await Promise.race([lookup(controller.signal), aborted]);
39
+ signal?.throwIfAborted();
40
+ return result;
41
+ } catch {
42
+ signal?.throwIfAborted();
43
+ return undefined;
44
+ } finally {
45
+ clearTimeout(timer);
46
+ signal?.removeEventListener('abort', cancel);
47
+ controller.signal.removeEventListener('abort', onAbort);
48
+ }
49
+ }
50
+
51
+ function withActorName(evidence, name) {
52
+ return name ? Object.freeze({ ...evidence, actor: Object.freeze({ ...evidence.actor, name }) }) : evidence;
53
+ }
54
+
55
+ export async function externalSenderName(client, evidence, { signal, timeoutMs = 3000, cache } = {}) {
56
+ signal?.throwIfAborted();
57
+ if (displayName(evidence.actor.name)) return evidence;
58
+ const cached = cache?.get(`sender:${evidence.actor.id}`);
59
+ if (cached) return withActorName(evidence, cached.name);
60
+ const name = await bounded(signal, timeoutMs, async lookupSignal => {
61
+ const response = await client.im.v1.message.get(
62
+ { path: { message_id: evidence.messageId }, params: { with_sender_name: true } }, { signal: lookupSignal });
63
+ const route = evidence.reply;
64
+ const source = response?.data?.items?.find(item => item.message_id === evidence.messageId);
65
+ if (response?.code || !source || source.deleted === true || source.msg_type !== 'text'
66
+ || source.chat_id !== route.conversationId
67
+ || source.sender?.sender_type !== 'user' || source.sender?.id_type !== 'open_id'
68
+ || source.sender?.id !== evidence.actor.id
69
+ || (source.thread_id || undefined) !== route.threadId
70
+ || (source.root_id || undefined) !== route.rootId
71
+ || (source.parent_id || undefined) !== route.parentId) return undefined;
72
+ if (JSON.parse(source.body?.content)?.text !== evidence.text) return undefined;
73
+ return displayName(source.sender.sender_name);
74
+ });
75
+ if (name) cache?.set(`sender:${evidence.actor.id}`, name);
76
+ return withActorName(evidence, name);
77
+ }
78
+
79
+ export async function externalConversationName(client, evidence, { signal, timeoutMs = 3000, cache } = {}) {
80
+ signal?.throwIfAborted();
81
+ if (evidence.conversation?.kind !== 'group' || displayName(evidence.conversation.name)) return evidence;
82
+ const key = `chat:${evidence.conversation.id}`;
83
+ const cached = cache?.get(key);
84
+ const name = cached ? cached.name : await bounded(signal, timeoutMs, async lookupSignal => {
85
+ const response = await client.im.v1.chat.get({ path: { chat_id: evidence.conversation.id } }, { signal: lookupSignal });
86
+ return response?.code ? undefined : displayName(response?.data?.name);
87
+ });
88
+ if (!cached) cache?.set(key, name);
89
+ return name ? Object.freeze({ ...evidence, conversation: Object.freeze({ ...evidence.conversation, name }) }) : evidence;
90
+ }
91
+
92
+ export async function externalNames(client, evidence, options = {}) {
93
+ const [sender, conversation] = await Promise.all([
94
+ externalSenderName(client, evidence, options),
95
+ externalConversationName(client, evidence, options),
96
+ ]);
97
+ return conversation === evidence ? sender
98
+ : sender === evidence ? conversation : Object.freeze({ ...sender, conversation: conversation.conversation });
99
+ }
@@ -0,0 +1,54 @@
1
+ function endpointFor(domain, path) {
2
+ const origin = domain === 'lark' ? 'https://open.larksuite.com' : 'https://open.feishu.cn';
3
+ return new URL(path, origin);
4
+ }
5
+
6
+ function jsonResponse(body, operation) {
7
+ if (!body || typeof body !== 'object' || Array.isArray(body)) {
8
+ throw new Error(`${operation} returned a non-JSON response`);
9
+ }
10
+ if (body.code !== 0) {
11
+ throw new Error(`${operation} failed: ${body.msg || `code ${body.code}`}`);
12
+ }
13
+ return body;
14
+ }
15
+
16
+ /** Validate freshly provisioned credentials and read the bot identity. */
17
+ export async function verifyFeishuApp({
18
+ appId,
19
+ appSecret,
20
+ domain = 'feishu',
21
+ httpInstance,
22
+ timeoutMs = 15000,
23
+ }) {
24
+ if (!appId || !appSecret) throw new Error('Feishu credentials are incomplete');
25
+ if (!httpInstance || typeof httpInstance.request !== 'function') {
26
+ throw new TypeError('Feishu verification requires an HTTP instance');
27
+ }
28
+ const tokenBody = jsonResponse(await httpInstance.request({
29
+ method: 'POST',
30
+ url: endpointFor(domain, '/open-apis/auth/v3/tenant_access_token/internal').href,
31
+ headers: { 'content-type': 'application/json; charset=utf-8' },
32
+ data: { app_id: appId, app_secret: appSecret },
33
+ signal: AbortSignal.timeout(timeoutMs),
34
+ timeout: timeoutMs,
35
+ }), 'Feishu authentication');
36
+ if (!tokenBody.tenant_access_token) {
37
+ throw new Error('Feishu authentication returned no tenant access token');
38
+ }
39
+
40
+ const botBody = jsonResponse(await httpInstance.request({
41
+ method: 'GET',
42
+ url: endpointFor(domain, '/open-apis/bot/v3/info/').href,
43
+ headers: { authorization: `Bearer ${tokenBody.tenant_access_token}` },
44
+ signal: AbortSignal.timeout(timeoutMs),
45
+ timeout: timeoutMs,
46
+ }), 'Feishu bot verification');
47
+ const bot = botBody.bot ?? {};
48
+ return Object.freeze({
49
+ appId,
50
+ name: bot.app_name ?? bot.bot_name ?? null,
51
+ openId: bot.open_id ?? null,
52
+ activated: bot.activate_status ?? null,
53
+ });
54
+ }