@botharness/im-provider 4.32.0-botharness.3

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 (412) 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 +602 -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 +17 -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 +318 -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 +237 -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 +257 -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 +388 -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 +552 -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 +224 -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 +25 -0
  201. package/src/channels/discord/discord-api.mjs +347 -0
  202. package/src/channels/discord/discord-bridge.mjs +16 -0
  203. package/src/channels/discord/discord-controller.mjs +16 -0
  204. package/src/channels/discord/discord-runtime.mjs +974 -0
  205. package/src/channels/discord/harness-client.mjs +11 -0
  206. package/src/channels/discord/state-store.mjs +3 -0
  207. package/src/channels/email/config-store.mjs +194 -0
  208. package/src/channels/email/email-api.mjs +20 -0
  209. package/src/channels/email/email-bridge.mjs +17 -0
  210. package/src/channels/email/email-controller.mjs +824 -0
  211. package/src/channels/email/email-runtime.mjs +699 -0
  212. package/src/channels/email/harness-client.mjs +7 -0
  213. package/src/channels/email/mail-format.mjs +91 -0
  214. package/src/channels/email/state-store.mjs +176 -0
  215. package/src/channels/email/transport.mjs +64 -0
  216. package/src/channels/email/transports/agent-mail.mjs +604 -0
  217. package/src/channels/email/transports/agently-cli.mjs +288 -0
  218. package/src/channels/email/transports/imap-smtp.mjs +165 -0
  219. package/src/channels/feishu/bridge.mjs +6286 -0
  220. package/src/channels/feishu/config.mjs +71 -0
  221. package/src/channels/feishu/external-consumer.mjs +77 -0
  222. package/src/channels/feishu/external-files.mjs +111 -0
  223. package/src/channels/feishu/feishu-app.mjs +54 -0
  224. package/src/channels/feishu/feishu-cards.mjs +1285 -0
  225. package/src/channels/feishu/feishu-channel.mjs +651 -0
  226. package/src/channels/feishu/feishu-runtime.mjs +950 -0
  227. package/src/channels/feishu/group-message-permission-manager.mjs +72 -0
  228. package/src/channels/feishu/group-response-mode.mjs +15 -0
  229. package/src/channels/feishu/harness-client.mjs +19 -0
  230. package/src/channels/feishu/history-reader.mjs +77 -0
  231. package/src/channels/feishu/live-cot.mjs +260 -0
  232. package/src/channels/feishu/message-utils.mjs +613 -0
  233. package/src/channels/feishu/multi-bot-controller.mjs +1588 -0
  234. package/src/channels/feishu/nearby-history.mjs +72 -0
  235. package/src/channels/feishu/plugin-config-store.mjs +216 -0
  236. package/src/channels/feishu/plugin-controller.mjs +254 -0
  237. package/src/channels/feishu/registration-manager.mjs +349 -0
  238. package/src/channels/feishu/repair-manager.mjs +138 -0
  239. package/src/channels/feishu/reply-context.mjs +22 -0
  240. package/src/channels/feishu/slash-command-panel.mjs +155 -0
  241. package/src/channels/feishu/slash-command-registry.mjs +495 -0
  242. package/src/channels/feishu/state-store.mjs +214 -0
  243. package/src/channels/feishu/step-push-mode.mjs +25 -0
  244. package/src/channels/feishu/voice-config.mjs +34 -0
  245. package/src/channels/feishu/voice.mjs +134 -0
  246. package/src/channels/imessage/config-store.mjs +24 -0
  247. package/src/channels/imessage/controller.mjs +37 -0
  248. package/src/channels/imessage/harness-client.mjs +7 -0
  249. package/src/channels/imessage/imessage-api.mjs +194 -0
  250. package/src/channels/imessage/imessage-bridge.mjs +16 -0
  251. package/src/channels/imessage/runtime.mjs +102 -0
  252. package/src/channels/imessage/state-store.mjs +3 -0
  253. package/src/channels/matrix/matrix-api.mjs +696 -0
  254. package/src/channels/matrix/matrix-bridge.mjs +20 -0
  255. package/src/channels/matrix/matrix-config-store.mjs +368 -0
  256. package/src/channels/matrix/matrix-controller.mjs +404 -0
  257. package/src/channels/matrix/matrix-crypto-store.mjs +279 -0
  258. package/src/channels/matrix/matrix-crypto.mjs +1014 -0
  259. package/src/channels/matrix/matrix-harness-client.mjs +11 -0
  260. package/src/channels/matrix/matrix-normalize.mjs +357 -0
  261. package/src/channels/matrix/matrix-rich-text.mjs +313 -0
  262. package/src/channels/matrix/matrix-room-history.mjs +316 -0
  263. package/src/channels/matrix/matrix-runtime.mjs +1014 -0
  264. package/src/channels/office/config-store.mjs +107 -0
  265. package/src/channels/office/office-controller.mjs +178 -0
  266. package/src/channels/office/office-job-executor.mjs +391 -0
  267. package/src/channels/office/office-runtime.mjs +175 -0
  268. package/src/channels/office/office-transport.mjs +171 -0
  269. package/src/channels/office/protocol.mjs +43 -0
  270. package/src/channels/qq/config-store.mjs +173 -0
  271. package/src/channels/qq/harness-client.mjs +11 -0
  272. package/src/channels/qq/markdown-reply.mjs +446 -0
  273. package/src/channels/qq/qq-bridge.mjs +1507 -0
  274. package/src/channels/qq/qq-controller.mjs +514 -0
  275. package/src/channels/qq/qq-menu.mjs +208 -0
  276. package/src/channels/qq/qq-runtime.mjs +303 -0
  277. package/src/channels/qq/qr-auth.mjs +24 -0
  278. package/src/channels/qq/state-error.mjs +17 -0
  279. package/src/channels/qq/state-store.mjs +143 -0
  280. package/src/channels/shared/access-policy.mjs +210 -0
  281. package/src/channels/shared/agent-preset.mjs +76 -0
  282. package/src/channels/shared/batch-input.mjs +229 -0
  283. package/src/channels/shared/bot-alias.mjs +29 -0
  284. package/src/channels/shared/bot-workspace-store.mjs +2383 -0
  285. package/src/channels/shared/command-catalog.mjs +107 -0
  286. package/src/channels/shared/command-permission.mjs +32 -0
  287. package/src/channels/shared/compact-command.mjs +109 -0
  288. package/src/channels/shared/config-read-error.mjs +24 -0
  289. package/src/channels/shared/connection-error.mjs +191 -0
  290. package/src/channels/shared/connection-test.mjs +58 -0
  291. package/src/channels/shared/context-enhancement.mjs +372 -0
  292. package/src/channels/shared/control-command.mjs +111 -0
  293. package/src/channels/shared/conversation-state-store.mjs +165 -0
  294. package/src/channels/shared/default-workspace.mjs +31 -0
  295. package/src/channels/shared/deferred-delivery-coordinator.mjs +241 -0
  296. package/src/channels/shared/deferred-delivery.mjs +161 -0
  297. package/src/channels/shared/deferred-state.mjs +53 -0
  298. package/src/channels/shared/diagnostic-details.mjs +95 -0
  299. package/src/channels/shared/editable-message-stream.mjs +100 -0
  300. package/src/channels/shared/exclusive-inbound-consumers.mjs +71 -0
  301. package/src/channels/shared/file-download.mjs +64 -0
  302. package/src/channels/shared/harness-approval.mjs +508 -0
  303. package/src/channels/shared/harness-client.mjs +2143 -0
  304. package/src/channels/shared/harness-mux.mjs +123 -0
  305. package/src/channels/shared/harness-question.mjs +95 -0
  306. package/src/channels/shared/harness-session-binding.mjs +119 -0
  307. package/src/channels/shared/history-command.mjs +193 -0
  308. package/src/channels/shared/i18n-en/diagnostics.mjs +55 -0
  309. package/src/channels/shared/i18n-en/dingtalk.mjs +81 -0
  310. package/src/channels/shared/i18n-en/discord.mjs +10 -0
  311. package/src/channels/shared/i18n-en/email.mjs +25 -0
  312. package/src/channels/shared/i18n-en/feishu.mjs +404 -0
  313. package/src/channels/shared/i18n-en/image-input.mjs +18 -0
  314. package/src/channels/shared/i18n-en/matrix.mjs +85 -0
  315. package/src/channels/shared/i18n-en/office.mjs +23 -0
  316. package/src/channels/shared/i18n-en/qq.mjs +96 -0
  317. package/src/channels/shared/i18n-en/shared-a.mjs +241 -0
  318. package/src/channels/shared/i18n-en/shared-b.mjs +263 -0
  319. package/src/channels/shared/i18n-en/shared-c.mjs +203 -0
  320. package/src/channels/shared/i18n-en/slack.mjs +27 -0
  321. package/src/channels/shared/i18n-en/telegram.mjs +27 -0
  322. package/src/channels/shared/i18n-en/wecom-app.mjs +33 -0
  323. package/src/channels/shared/i18n-en/wecom.mjs +62 -0
  324. package/src/channels/shared/i18n-en/weixin.mjs +88 -0
  325. package/src/channels/shared/i18n-en/whatsapp.mjs +17 -0
  326. package/src/channels/shared/i18n-en.mjs +45 -0
  327. package/src/channels/shared/i18n.mjs +78 -0
  328. package/src/channels/shared/im-source-guidance.mjs +65 -0
  329. package/src/channels/shared/image-input-policy.mjs +35 -0
  330. package/src/channels/shared/image-input-settings-store.mjs +60 -0
  331. package/src/channels/shared/image-input.mjs +124 -0
  332. package/src/channels/shared/image-prompt.mjs +360 -0
  333. package/src/channels/shared/inbound-access.mjs +43 -0
  334. package/src/channels/shared/inbound-file.mjs +323 -0
  335. package/src/channels/shared/inbound-ttl-store.mjs +110 -0
  336. package/src/channels/shared/inbound-ttl.mjs +22 -0
  337. package/src/channels/shared/injected-context.mjs +362 -0
  338. package/src/channels/shared/interface-language-store.mjs +127 -0
  339. package/src/channels/shared/interface-language.mjs +51 -0
  340. package/src/channels/shared/message-failure.mjs +257 -0
  341. package/src/channels/shared/model-command.mjs +741 -0
  342. package/src/channels/shared/model-setting.mjs +145 -0
  343. package/src/channels/shared/preset-command.mjs +308 -0
  344. package/src/channels/shared/semantic/artifact-delivery.mjs +177 -0
  345. package/src/channels/shared/semantic/artifact.mjs +750 -0
  346. package/src/channels/shared/semantic/delivery.mjs +193 -0
  347. package/src/channels/shared/semantic/reply-reference.mjs +148 -0
  348. package/src/channels/shared/session-binding-lock.mjs +40 -0
  349. package/src/channels/shared/session-channel-labels.mjs +28 -0
  350. package/src/channels/shared/session-reply-recovery.mjs +104 -0
  351. package/src/channels/shared/session-sync-registry.mjs +22 -0
  352. package/src/channels/shared/session-title.mjs +74 -0
  353. package/src/channels/shared/status-reaction.mjs +107 -0
  354. package/src/channels/shared/text-harness-bridge.mjs +1623 -0
  355. package/src/channels/shared/token-bot-controller.mjs +417 -0
  356. package/src/channels/shared/token-config-store.mjs +208 -0
  357. package/src/channels/shared/workspace-command.mjs +532 -0
  358. package/src/channels/shared/workspace-session.mjs +196 -0
  359. package/src/channels/slack/config-store.mjs +177 -0
  360. package/src/channels/slack/external-consumer.mjs +80 -0
  361. package/src/channels/slack/external-files.mjs +90 -0
  362. package/src/channels/slack/harness-client.mjs +11 -0
  363. package/src/channels/slack/history-reader.mjs +203 -0
  364. package/src/channels/slack/manifest.mjs +37 -0
  365. package/src/channels/slack/slack-api.mjs +636 -0
  366. package/src/channels/slack/slack-bridge.mjs +20 -0
  367. package/src/channels/slack/slack-controller.mjs +468 -0
  368. package/src/channels/slack/slack-runtime.mjs +879 -0
  369. package/src/channels/slack/state-store.mjs +3 -0
  370. package/src/channels/telegram/config-store.mjs +89 -0
  371. package/src/channels/telegram/harness-client.mjs +11 -0
  372. package/src/channels/telegram/state-store.mjs +3 -0
  373. package/src/channels/telegram/telegram-api.mjs +486 -0
  374. package/src/channels/telegram/telegram-bridge.mjs +81 -0
  375. package/src/channels/telegram/telegram-controller.mjs +52 -0
  376. package/src/channels/telegram/telegram-http.mjs +21 -0
  377. package/src/channels/telegram/telegram-rich-message.mjs +149 -0
  378. package/src/channels/telegram/telegram-runtime.mjs +1471 -0
  379. package/src/channels/wecom/config-store.mjs +172 -0
  380. package/src/channels/wecom/harness-client.mjs +11 -0
  381. package/src/channels/wecom/qr-auth.mjs +95 -0
  382. package/src/channels/wecom/send-error.mjs +31 -0
  383. package/src/channels/wecom/state-store.mjs +107 -0
  384. package/src/channels/wecom/wecom-bridge.mjs +1833 -0
  385. package/src/channels/wecom/wecom-cards.mjs +101 -0
  386. package/src/channels/wecom/wecom-controller.mjs +530 -0
  387. package/src/channels/wecom/wecom-runtime.mjs +269 -0
  388. package/src/channels/wecom-app/callback-server.mjs +471 -0
  389. package/src/channels/wecom-app/config-store.mjs +242 -0
  390. package/src/channels/wecom-app/harness-client.mjs +11 -0
  391. package/src/channels/wecom-app/state-store.mjs +107 -0
  392. package/src/channels/wecom-app/wecom-app-api.mjs +432 -0
  393. package/src/channels/wecom-app/wecom-app-bridge.mjs +1072 -0
  394. package/src/channels/wecom-app/wecom-app-controller.mjs +453 -0
  395. package/src/channels/wecom-app/wecom-app-runtime.mjs +222 -0
  396. package/src/channels/weixin/config-store.mjs +192 -0
  397. package/src/channels/weixin/connection-error.en.mjs +137 -0
  398. package/src/channels/weixin/connection-error.mjs +153 -0
  399. package/src/channels/weixin/diagnostic-details.mjs +1 -0
  400. package/src/channels/weixin/harness-client.mjs +19 -0
  401. package/src/channels/weixin/state-store.mjs +291 -0
  402. package/src/channels/weixin/weixin-api.mjs +949 -0
  403. package/src/channels/weixin/weixin-bridge.mjs +1537 -0
  404. package/src/channels/weixin/weixin-controller.mjs +669 -0
  405. package/src/channels/weixin/weixin-runtime.mjs +416 -0
  406. package/src/channels/whatsapp/config-store.mjs +210 -0
  407. package/src/channels/whatsapp/harness-client.mjs +11 -0
  408. package/src/channels/whatsapp/state-store.mjs +3 -0
  409. package/src/channels/whatsapp/whatsapp-bridge.mjs +18 -0
  410. package/src/channels/whatsapp/whatsapp-controller.mjs +470 -0
  411. package/src/channels/whatsapp/whatsapp-runtime.mjs +917 -0
  412. package/src/channels/whatsapp/whatsapp-web-session.mjs +214 -0
@@ -0,0 +1,284 @@
1
+ import { createHash, randomUUID } from 'node:crypto';
2
+ import { mkdir, readFile, rename, unlink, writeFile } from 'node:fs/promises';
3
+ import { dirname } from 'node:path';
4
+
5
+ import { t } from '../shared/i18n.mjs';
6
+
7
+ const EMPTY_DOCUMENT = Object.freeze({ version: 1, bots: Object.freeze([]) });
8
+ const STORED_BOT_KEYS = new Set(['clientId', 'secretRef', 'approvedSenders']);
9
+
10
+ function cleanString(value) {
11
+ return typeof value === 'string' && value.trim() ? value.trim() : null;
12
+ }
13
+
14
+ function digest(value) {
15
+ return createHash('sha256').update(value).digest('hex');
16
+ }
17
+
18
+ function safeBotId(value) {
19
+ const id = cleanString(value);
20
+ return id && /^dt_[a-f0-9]{24}$/.test(id) ? id : null;
21
+ }
22
+
23
+ function safeSecretRef(value) {
24
+ const ref = cleanString(value);
25
+ return ref && /^DSH_DINGTALK_BOT_SECRET_[A-F0-9]{24}$/.test(ref) ? ref : null;
26
+ }
27
+
28
+ function safeSenderKey(value) {
29
+ const key = cleanString(value);
30
+ return key && /^dt_sender_[a-f0-9]{32}$/.test(key) ? key : null;
31
+ }
32
+
33
+ function normalizeApprovedSender(value) {
34
+ const record = typeof value === 'string' ? { staffId: value } : value;
35
+ if (!record || typeof record !== 'object' || Array.isArray(record)) return null;
36
+ const senderKey = safeSenderKey(record.senderKey);
37
+ const staffId = cleanString(record.staffId);
38
+ if (!senderKey || !staffId) return null;
39
+ return Object.freeze({
40
+ senderKey,
41
+ staffId,
42
+ displayName: cleanString(record.displayName),
43
+ approvedAt: cleanString(record.approvedAt),
44
+ });
45
+ }
46
+
47
+ function normalizeApprovedSenders(value) {
48
+ if (!Array.isArray(value)) return null;
49
+ const senders = value.map(normalizeApprovedSender);
50
+ if (senders.some((sender) => sender === null)) return null;
51
+ const ids = new Set();
52
+ const keys = new Set();
53
+ for (const sender of senders) {
54
+ if (ids.has(sender.staffId) || keys.has(sender.senderKey)) return null;
55
+ ids.add(sender.staffId);
56
+ keys.add(sender.senderKey);
57
+ }
58
+ return Object.freeze(senders);
59
+ }
60
+
61
+ /**
62
+ * Derives stable non-secret identifiers for a DingTalk bot credential.
63
+ * @param {string} clientId DingTalk application client ID.
64
+ * @returns {{botId: string, secretRef: string}} Derived identifiers.
65
+ */
66
+ export function deriveDingtalkBotIdentity(clientId) {
67
+ const value = cleanString(clientId);
68
+ if (!value) throw new TypeError('clientId is required');
69
+ const valueDigest = digest(value).slice(0, 24);
70
+ return Object.freeze({
71
+ botId: `dt_${valueDigest}`,
72
+ secretRef: `DSH_DINGTALK_BOT_SECRET_${valueDigest.toUpperCase()}`,
73
+ });
74
+ }
75
+
76
+ /**
77
+ * Creates a random browser-safe key for an approved DingTalk sender.
78
+ * @returns {string} Opaque sender key.
79
+ */
80
+ export function deriveDingtalkSenderKey() {
81
+ return `dt_sender_${randomUUID().replaceAll('-', '')}`;
82
+ }
83
+
84
+ /**
85
+ * Redacts a DingTalk sender ID for display.
86
+ * @param {string} staffId DingTalk staff ID.
87
+ * @returns {string} Partially redacted identifier.
88
+ */
89
+ export function maskDingtalkSenderId(staffId) {
90
+ const value = cleanString(staffId);
91
+ if (!value) return t('钉钉用户');
92
+ return t('身份已隐藏');
93
+ }
94
+
95
+ /**
96
+ * Redacts a DingTalk client ID for display.
97
+ * @param {string} clientId DingTalk application client ID.
98
+ * @returns {string} Partially redacted client ID.
99
+ */
100
+ export function maskDingtalkClientId(clientId) {
101
+ const value = cleanString(clientId);
102
+ if (!value) return t('钉钉机器人');
103
+ if (value.length <= 8) return `${value.slice(0, 2)}••••`;
104
+ return `${value.slice(0, 4)}••••${value.slice(-4)}`;
105
+ }
106
+
107
+ function normalizeBot(value, { stored = false } = {}) {
108
+ if (!value || typeof value !== 'object' || Array.isArray(value)) return null;
109
+ if ('clientSecret' in value || 'client_secret' in value || 'deviceCode' in value) return null;
110
+ if (stored && Object.keys(value).some((key) => !STORED_BOT_KEYS.has(key))) return null;
111
+ const clientId = cleanString(value.clientId);
112
+ const secretRef = safeSecretRef(value.secretRef);
113
+ const approvedSenders = normalizeApprovedSenders(value.approvedSenders ?? []);
114
+ if (!clientId || !secretRef || !approvedSenders) return null;
115
+ const identity = deriveDingtalkBotIdentity(clientId);
116
+ if (identity.secretRef !== secretRef) return null;
117
+ const suppliedBotId = value.botId === undefined ? identity.botId : safeBotId(value.botId);
118
+ if (suppliedBotId !== identity.botId) return null;
119
+ return Object.freeze({
120
+ botId: identity.botId,
121
+ clientId,
122
+ secretRef,
123
+ approvedSenders,
124
+ });
125
+ }
126
+
127
+ function normalizeDocument(value) {
128
+ if (!value || value.version !== 1 || !Array.isArray(value.bots)) return null;
129
+ const bots = value.bots.map((bot) => normalizeBot(bot, { stored: true }));
130
+ if (bots.some((bot) => bot === null)) return null;
131
+ const botIds = new Set();
132
+ const clientIds = new Set();
133
+ const secretRefs = new Set();
134
+ for (const bot of bots) {
135
+ if (botIds.has(bot.botId) || clientIds.has(bot.clientId) || secretRefs.has(bot.secretRef)) {
136
+ return null;
137
+ }
138
+ botIds.add(bot.botId);
139
+ clientIds.add(bot.clientId);
140
+ secretRefs.add(bot.secretRef);
141
+ }
142
+ return Object.freeze({ version: 1, bots: Object.freeze(bots) });
143
+ }
144
+
145
+ function storedDocument(document) {
146
+ return {
147
+ version: 1,
148
+ bots: document.bots.map((bot) => ({
149
+ clientId: bot.clientId,
150
+ secretRef: bot.secretRef,
151
+ approvedSenders: bot.approvedSenders.map((sender) => ({
152
+ senderKey: sender.senderKey,
153
+ staffId: sender.staffId,
154
+ displayName: sender.displayName,
155
+ approvedAt: sender.approvedAt,
156
+ })),
157
+ })),
158
+ };
159
+ }
160
+
161
+ /** Atomic non-secret DingTalk bot configuration store. */
162
+ export class DingtalkConfigStore {
163
+ #path;
164
+ #value = EMPTY_DOCUMENT;
165
+ #writeQueue = Promise.resolve();
166
+
167
+ /** @param {string} path Absolute or process-relative configuration file path. */
168
+ constructor(path) {
169
+ if (!cleanString(path)) throw new TypeError('config path is required');
170
+ this.#path = path;
171
+ }
172
+
173
+ /** @returns {Promise<DingtalkConfigStore>} Loaded store. */
174
+ async load() {
175
+ try {
176
+ const normalized = normalizeDocument(JSON.parse(await readFile(this.#path, 'utf8')));
177
+ if (!normalized) throw new Error('dsh-dingtalk config contains invalid bot data');
178
+ this.#value = normalized;
179
+ } catch (error) {
180
+ if (error?.code !== 'ENOENT') throw error;
181
+ this.#value = EMPTY_DOCUMENT;
182
+ }
183
+ return this;
184
+ }
185
+
186
+ /** @returns {Array<object>} Cloned bot configurations with derived bot IDs. */
187
+ list() {
188
+ return structuredClone(this.#value.bots);
189
+ }
190
+
191
+ /** @param {string} botId Derived bot ID. @returns {object|null} Bot configuration. */
192
+ get(botId) {
193
+ const found = this.#value.bots.find((bot) => bot.botId === botId);
194
+ return found ? structuredClone(found) : null;
195
+ }
196
+
197
+ /** @param {string} clientId DingTalk client ID. @returns {object|null} Bot configuration. */
198
+ getByClientId(clientId) {
199
+ const found = this.#value.bots.find((bot) => bot.clientId === clientId);
200
+ return found ? structuredClone(found) : null;
201
+ }
202
+
203
+ /** @param {object} value Bot configuration without a client secret. @returns {Promise<object>} Saved config. */
204
+ async save(value) {
205
+ const normalized = normalizeBot(value);
206
+ if (!normalized) throw new Error('Refusing to persist invalid dsh-dingtalk bot data');
207
+ return this.#mutate((bots) => {
208
+ const collision = bots.find(
209
+ (bot) => (bot.clientId === normalized.clientId || bot.secretRef === normalized.secretRef)
210
+ && bot.botId !== normalized.botId,
211
+ );
212
+ if (collision) throw new Error('Duplicate DingTalk bot identity');
213
+ const index = bots.findIndex((bot) => bot.botId === normalized.botId);
214
+ if (index === -1) bots.push(normalized);
215
+ else bots[index] = normalized;
216
+ return structuredClone(normalized);
217
+ });
218
+ }
219
+
220
+ /** @param {string} botId Derived bot ID. @returns {Promise<object|null>} Removed config. */
221
+ async remove(botId) {
222
+ if (!safeBotId(botId)) throw new TypeError('Invalid DingTalk bot id');
223
+ return this.#mutate((bots) => {
224
+ const index = bots.findIndex((bot) => bot.botId === botId);
225
+ if (index === -1) return null;
226
+ const [removed] = bots.splice(index, 1);
227
+ return structuredClone(removed);
228
+ });
229
+ }
230
+
231
+ /** Removes the configuration file and resets the in-memory store. */
232
+ async clear() {
233
+ const operation = this.#writeQueue.then(async () => {
234
+ try {
235
+ await unlink(this.#path);
236
+ } catch (error) {
237
+ if (error?.code !== 'ENOENT') throw error;
238
+ }
239
+ this.#value = EMPTY_DOCUMENT;
240
+ });
241
+ this.#writeQueue = operation.then(() => undefined, () => undefined);
242
+ await operation;
243
+ }
244
+
245
+ async #mutate(mutator) {
246
+ let result;
247
+ const operation = this.#writeQueue.then(async () => {
248
+ const bots = [...this.#value.bots];
249
+ result = mutator(bots);
250
+ const document = Object.freeze({ version: 1, bots: Object.freeze(bots) });
251
+ await this.#write(document);
252
+ this.#value = document;
253
+ });
254
+ this.#writeQueue = operation.then(() => undefined, () => undefined);
255
+ await operation;
256
+ return result;
257
+ }
258
+
259
+ async #write(document) {
260
+ await mkdir(dirname(this.#path), { recursive: true, mode: 0o700 });
261
+ const temporary = `${this.#path}.${process.pid}.${randomUUID()}.tmp`;
262
+ try {
263
+ await writeFile(temporary, `${JSON.stringify(storedDocument(document), null, 2)}\n`, {
264
+ encoding: 'utf8',
265
+ flag: 'wx',
266
+ mode: 0o600,
267
+ });
268
+ await rename(temporary, this.#path);
269
+ } catch (error) {
270
+ try {
271
+ await unlink(temporary);
272
+ } catch (cleanupError) {
273
+ if (cleanupError?.code !== 'ENOENT') throw new AggregateError([error, cleanupError]);
274
+ }
275
+ throw error;
276
+ }
277
+ }
278
+ }
279
+
280
+ export { deriveDingtalkBotIdentity as deriveDingTalkBotIdentity };
281
+ export { deriveDingtalkSenderKey as deriveDingTalkSenderKey };
282
+ export { maskDingtalkClientId as maskDingTalkClientId };
283
+ export { maskDingtalkSenderId as maskDingTalkSenderId };
284
+ export { DingtalkConfigStore as DingTalkConfigStore };
@@ -0,0 +1,292 @@
1
+ import { connectionErrorChain, connectionStageError } from '../shared/connection-error.mjs';
2
+ import { randomUUID } from 'node:crypto';
3
+ import { createRequire } from 'node:module';
4
+
5
+ import { t } from '../shared/i18n.mjs';
6
+
7
+ const require = createRequire(import.meta.url);
8
+ const STAGE_CODES = new Set([
9
+ 'dingtalk-harness-connect-failed',
10
+ 'dingtalk-runtime-prepare-failed',
11
+ 'dingtalk-stream-client-load-failed',
12
+ 'dingtalk-stream-listener-failed',
13
+ 'dingtalk-stream-connect-failed',
14
+ ]);
15
+ const PROXY_VARIABLES = Object.freeze([
16
+ 'HTTPS_PROXY',
17
+ 'https_proxy',
18
+ 'HTTP_PROXY',
19
+ 'http_proxy',
20
+ 'ALL_PROXY',
21
+ 'all_proxy',
22
+ ]);
23
+ const VERSION_PATTERN = /^\d+\.\d+\.\d+(?:[-+][A-Za-z0-9.-]+)?$/;
24
+ const PUBLIC_REFERENCE_PATTERN = /^DT-CONN-[A-F0-9]{8}$/;
25
+ let installedDependencyVersions;
26
+
27
+ function nonEmptyString(value, maxLength = 500) {
28
+ if (typeof value !== 'string') return null;
29
+ const text = value.trim();
30
+ return text ? text.slice(0, maxLength) : null;
31
+ }
32
+
33
+ function safeVersion(value) {
34
+ const version = nonEmptyString(value, 80);
35
+ return version && VERSION_PATTERN.test(version) ? version : null;
36
+ }
37
+
38
+ function resolvedPackage(name, from = require) {
39
+ try {
40
+ const packagePath = from.resolve(`${name}/package.json`);
41
+ return {
42
+ version: safeVersion(from(packagePath)?.version),
43
+ require: createRequire(packagePath),
44
+ };
45
+ } catch {
46
+ return null;
47
+ }
48
+ }
49
+
50
+ /** Returns only non-sensitive versions involved in the external DingTalk Stream dependency chain. */
51
+ export function installedDingtalkConnectionDependencies() {
52
+ if (installedDependencyVersions) return installedDependencyVersions;
53
+ const dingtalkStream = resolvedPackage('dingtalk-stream');
54
+ const axios = dingtalkStream ? resolvedPackage('axios', dingtalkStream.require) : null;
55
+ const httpsProxyAgent = axios ? resolvedPackage('https-proxy-agent', axios.require) : null;
56
+ const agentBase = httpsProxyAgent ? resolvedPackage('agent-base', httpsProxyAgent.require) : null;
57
+ installedDependencyVersions = Object.freeze({
58
+ dingtalkStream: dingtalkStream?.version ?? null,
59
+ axios: axios?.version ?? null,
60
+ httpsProxyAgent: httpsProxyAgent?.version ?? null,
61
+ agentBase: agentBase?.version ?? null,
62
+ });
63
+ return installedDependencyVersions;
64
+ }
65
+
66
+ function errorChain(error) { return connectionErrorChain(error).chain; }
67
+
68
+ function statusFrom(error) {
69
+ if (Number.isInteger(error?.status)) return error.status;
70
+ if (Number.isInteger(error?.statusCode)) return error.statusCode;
71
+ if (Number.isInteger(error?.response?.status)) return error.response.status;
72
+ return null;
73
+ }
74
+
75
+ function providerCodeFrom(error) {
76
+ const value = nonEmptyString(
77
+ error?.providerCode
78
+ ?? error?.response?.data?.code
79
+ ?? error?.response?.data?.errorCode
80
+ ?? error?.response?.data?.errcode,
81
+ 100,
82
+ );
83
+ return value && /^[A-Za-z0-9_.:-]+$/.test(value) ? value : null;
84
+ }
85
+
86
+ function redactMessage(value, sensitiveValues) {
87
+ let message = nonEmptyString(value);
88
+ if (!message) return null;
89
+ for (const sensitive of sensitiveValues) {
90
+ const text = nonEmptyString(sensitive, 2_048);
91
+ if (text && text.length >= 4) message = message.replaceAll(text, '••••');
92
+ }
93
+ return message
94
+ .replace(/(https?:\/\/)[^/\s@]+@/giu, '$1••••@')
95
+ .replace(/([?&](?:appsecret|client_secret|clientsecret|access_token|token|password)=)[^&\s]*/giu, '$1••••')
96
+ .replace(/((?:app|client|access)[_-]?secret|authorization|password|token)\s*[=:]\s*[^\s,;,。]+/giu, '$1=••••')
97
+ .replace(/\b[A-Za-z0-9_.-]*secret[A-Za-z0-9_.-]*\b/giu, '••••')
98
+ .slice(0, 500);
99
+ }
100
+
101
+ function safeDependencyVersions(value) {
102
+ const source = value && typeof value === 'object' ? value : {};
103
+ return {
104
+ dingtalkStream: safeVersion(source.dingtalkStream),
105
+ axios: safeVersion(source.axios),
106
+ httpsProxyAgent: safeVersion(source.httpsProxyAgent),
107
+ agentBase: safeVersion(source.agentBase),
108
+ };
109
+ }
110
+
111
+ function diagnosticErrors(chain, sensitiveValues) {
112
+ return chain.map((error) => {
113
+ const name = nonEmptyString(error?.name, 80);
114
+ const code = nonEmptyString(error?.code, 100);
115
+ const status = statusFrom(error);
116
+ const providerCode = providerCodeFrom(error);
117
+ const message = redactMessage(error?.message, sensitiveValues);
118
+ return {
119
+ ...(name ? { name } : {}),
120
+ ...(code ? { code } : {}),
121
+ ...(status !== null ? { status } : {}),
122
+ ...(providerCode ? { providerCode } : {}),
123
+ ...(message ? { message } : {}),
124
+ };
125
+ });
126
+ }
127
+
128
+ function publicReference(value) {
129
+ return PUBLIC_REFERENCE_PATTERN.test(value ?? '')
130
+ ? value
131
+ : `DT-CONN-${randomUUID().replaceAll('-', '').slice(0, 8).toUpperCase()}`;
132
+ }
133
+
134
+ function fixedPublicError(code, message, hint, referenceId) {
135
+ return Object.freeze({ code, message: t(message), hint: t(hint), referenceId });
136
+ }
137
+
138
+ /** Adds a stable startup stage without discarding the original exception as `cause`. */
139
+ export function dingtalkRuntimeStartError(code, cause) {
140
+ if (cause?.name === 'AbortError' || STAGE_CODES.has(cause?.code)) return cause;
141
+ const error = new Error(
142
+ nonEmptyString(cause?.message) ?? 'DingTalk runtime startup failed',
143
+ { cause },
144
+ );
145
+ error.name = 'DingtalkRuntimeStartError';
146
+ error.code = STAGE_CODES.has(code) ? code : 'dingtalk-runtime-prepare-failed';
147
+ return connectionStageError(error, ({ 'dingtalk-harness-connect-failed': 'harness.check',
148
+ 'dingtalk-runtime-prepare-failed': 'runtime.prepare', 'dingtalk-stream-client-load-failed': 'sdk.load',
149
+ 'dingtalk-stream-listener-failed': 'runtime.prepare', 'dingtalk-stream-connect-failed': 'connection.start' })[error.code]);
150
+ }
151
+
152
+ /** Creates browser-safe guidance plus a redacted Host-log diagnostic for one connection failure. */
153
+ export function describeDingtalkConnectionFailure(error, {
154
+ fallbackMessage = '钉钉连接未就绪,请稍后重试。',
155
+ clientId,
156
+ clientSecret,
157
+ environment = process.env,
158
+ dependencies = installedDingtalkConnectionDependencies(),
159
+ nodeVersion = process.versions.node,
160
+ referenceId: suppliedReferenceId,
161
+ } = {}) {
162
+ const referenceId = publicReference(suppliedReferenceId);
163
+ const chain = errorChain(error);
164
+ const codes = new Set(chain
165
+ .map((entry) => nonEmptyString(entry?.code, 100)?.toUpperCase())
166
+ .filter(Boolean));
167
+ const messages = chain
168
+ .map((entry) => nonEmptyString(entry?.message)?.toLowerCase())
169
+ .filter(Boolean);
170
+ const statuses = chain.map(statusFrom).filter((status) => status !== null);
171
+ const stage = chain.map((entry) => entry?.code).find((code) => STAGE_CODES.has(code)) ?? null;
172
+ const proxyVariables = PROXY_VARIABLES.filter((name) => nonEmptyString(environment?.[name], 4_096));
173
+ const proxyConfigured = proxyVariables.length > 0;
174
+ const versions = safeDependencyVersions(dependencies);
175
+ const messageContains = (pattern) => messages.some((message) => pattern.test(message));
176
+ const codeContains = (pattern) => [...codes].some((code) => pattern.test(code));
177
+ const proxyFailureSignal = statuses.includes(407)
178
+ || codeContains(/(?:PROXY|ERR_INVALID_PROTOCOL)/u)
179
+ || messageContains(/proxy|tunneling socket/u);
180
+
181
+ let publicError;
182
+ if (stage === 'dingtalk-stream-connect-failed'
183
+ && proxyConfigured
184
+ && versions.agentBase === '6.0.0') {
185
+ publicError = fixedPublicError(
186
+ 'stream-proxy-dependency-incompatible',
187
+ '钉钉 Stream 连接失败:检测到代理依赖 agent-base 6.0.0。',
188
+ '请将 DSH profile 中的 agent-base@6 固定为 6.0.2 后重新安装依赖,或升级 pnpm 后重新解析 lockfile。',
189
+ referenceId,
190
+ );
191
+ } else if (stage === 'dingtalk-harness-connect-failed') {
192
+ publicError = fixedPublicError(
193
+ 'harness-unavailable',
194
+ '插件无法连接本机 Harness。',
195
+ '请确认 dsh web 正常运行,并查看 dsh web 日志中相同参考号对应的诊断信息。',
196
+ referenceId,
197
+ );
198
+ } else if (stage === 'dingtalk-stream-client-load-failed') {
199
+ publicError = fixedPublicError(
200
+ 'stream-sdk-load-failed',
201
+ '钉钉 Stream SDK 加载失败。',
202
+ '请重新安装当前 DSH profile 的插件依赖,并查看 dsh web 日志中相同参考号对应的诊断信息。',
203
+ referenceId,
204
+ );
205
+ } else if (statuses.some((status) => status === 401 || status === 403)) {
206
+ publicError = fixedPublicError(
207
+ 'stream-credentials-rejected',
208
+ '钉钉拒绝了当前应用凭据。',
209
+ '请核对 Client ID、Client Secret 和机器人权限后重试。',
210
+ referenceId,
211
+ );
212
+ } else if (codeContains(/(?:TIMEOUT|ETIMEDOUT)/u) || messageContains(/timed? out|timeout/u)) {
213
+ publicError = fixedPublicError(
214
+ 'stream-handshake-timeout',
215
+ '连接钉钉 Stream 超时。',
216
+ '请检查网络、代理和防火墙后重试;详细原因可在 dsh web 日志中按参考号查找。',
217
+ referenceId,
218
+ );
219
+ } else if (codeContains(/^(?:ENOTFOUND|EAI_AGAIN)$/u)) {
220
+ publicError = fixedPublicError(
221
+ 'stream-dns-failed',
222
+ '无法解析钉钉服务地址。',
223
+ '请检查 DNS、网络和代理设置;详细原因可在 dsh web 日志中按参考号查找。',
224
+ referenceId,
225
+ );
226
+ } else if (codeContains(/(?:CERT|TLS|SSL|UNABLE_TO_VERIFY)/u)) {
227
+ publicError = fixedPublicError(
228
+ 'stream-tls-failed',
229
+ '钉钉 Stream 的 TLS 连接校验失败。',
230
+ '请检查系统证书、代理证书或 HTTPS 中间代理;详细原因可在 dsh web 日志中按参考号查找。',
231
+ referenceId,
232
+ );
233
+ } else if (stage === 'dingtalk-stream-connect-failed'
234
+ && proxyFailureSignal) {
235
+ publicError = fixedPublicError(
236
+ 'stream-proxy-failed',
237
+ '钉钉 Stream 无法通过当前代理建立连接。',
238
+ '请检查 HTTP_PROXY、HTTPS_PROXY、NO_PROXY 和代理连通性;详细原因可在 dsh web 日志中按参考号查找。',
239
+ referenceId,
240
+ );
241
+ } else if (stage === 'dingtalk-stream-connect-failed') {
242
+ publicError = fixedPublicError(
243
+ 'stream-connect-failed',
244
+ '钉钉 Stream 消息连接建立失败。',
245
+ '请检查网络和机器人配置;详细原因可在 dsh web 日志中按参考号查找。',
246
+ referenceId,
247
+ );
248
+ } else if (stage === 'dingtalk-stream-listener-failed') {
249
+ publicError = fixedPublicError(
250
+ 'stream-listener-failed',
251
+ '钉钉 Stream 消息监听初始化失败。',
252
+ '请确认 dsh-im 与 dingtalk-stream 版本兼容,并按参考号查看 dsh web 日志。',
253
+ referenceId,
254
+ );
255
+ } else if (stage === 'dingtalk-runtime-prepare-failed') {
256
+ publicError = fixedPublicError(
257
+ 'runtime-prepare-failed',
258
+ '钉钉机器人运行环境初始化失败。',
259
+ '请检查 DSH 数据目录、工作区和插件依赖,并按参考号查看 dsh web 日志。',
260
+ referenceId,
261
+ );
262
+ } else {
263
+ publicError = fixedPublicError(
264
+ 'connection-failed',
265
+ fallbackMessage,
266
+ '请在 dsh web 日志中查找相同参考号,以获取已脱敏的具体错误。',
267
+ referenceId,
268
+ );
269
+ }
270
+
271
+ return Object.freeze({
272
+ publicError,
273
+ diagnostic: Object.freeze({
274
+ referenceId,
275
+ category: publicError.code,
276
+ stage,
277
+ runtime: { node: safeVersion(nodeVersion) },
278
+ proxy: { configured: proxyConfigured, variables: proxyVariables },
279
+ dependencies: versions,
280
+ errors: diagnosticErrors(chain, [clientId, clientSecret]),
281
+ }),
282
+ });
283
+ }
284
+
285
+ /** Carries only a pre-built public projection across the Host RPC boundary. */
286
+ export function dingtalkPublicConnectionError(publicError, cause) {
287
+ const error = new Error(publicError.message, { cause });
288
+ error.name = 'DingtalkPublicConnectionError';
289
+ error.code = publicError.code;
290
+ error.publicError = structuredClone(publicError);
291
+ return error;
292
+ }