@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,107 @@
1
+ import { t } from './i18n.mjs';
2
+
3
+ const TEXT_CHANNELS = Object.freeze(['telegram', 'slack', 'discord', 'whatsapp', 'matrix']);
4
+
5
+ function defineCatalogCommand(name, description, help, { aliases = [], ...options } = {}) {
6
+ return Object.freeze({
7
+ name,
8
+ description,
9
+ channels: TEXT_CHANNELS,
10
+ menuVisible: true,
11
+ ...options,
12
+ aliases: Object.freeze(aliases.map((alias) => Object.freeze({
13
+ menuVisible: true,
14
+ ...(typeof alias === 'string' ? { name: alias } : alias),
15
+ }))),
16
+ help: Object.freeze(help),
17
+ });
18
+ }
19
+
20
+ // Presentation metadata for TextHarnessBridge commands, in help order.
21
+ // Keep each command's aliases, short description and full usage together here.
22
+ // Handlers still own parsing and authorization; privateOnly is descriptive.
23
+ // enabled: false removes a definition from both consumers; menuVisible: false
24
+ // hides a command (or alias) only from the native menu, leaving help available.
25
+ export const SHARED_COMMAND_CATALOG = Object.freeze([
26
+ defineCatalogCommand('new', '开启一个全新会话', [
27
+ '/new 开启一个全新会话',
28
+ ]),
29
+ defineCatalogCommand('compact', '压缩当前会话的较早上下文', [
30
+ '/compact 压缩当前会话的较早上下文',
31
+ ]),
32
+ defineCatalogCommand('history', '查看最近历史消息(仅私聊)', [
33
+ '/history [数量] 查看最近历史消息(默认 3 条,最多 5 条)',
34
+ ], { privateOnly: true }),
35
+ defineCatalogCommand('workspace', '切换工作区', [
36
+ '/workspace 工作区序号或绝对路径 切换工作区',
37
+ ], { aliases: ['ws'] }),
38
+ defineCatalogCommand('conv', '设置当前对话专属工作区', [
39
+ '/conv 或 /conversation 查看当前对话工作区',
40
+ '/conv 工作区绝对路径或序号 设置当前对话专属工作区',
41
+ '/conv clear 清除专属工作区,回到 bot 默认工作区',
42
+ ], { aliases: ['conversation', 'thread'] }),
43
+ defineCatalogCommand('workspacelist', '列出工作区绝对路径', [
44
+ '/workspacelist 列出工作区绝对路径',
45
+ '/ws、/wsl、/workspaces 工作区命令别名',
46
+ ], { aliases: ['workspaces', 'wsl'] }),
47
+ defineCatalogCommand('sessionlist', '列出会话 ID 和标题', [
48
+ '/sessionlist 或 /sessions [工作区序号或绝对路径] 列出会话 ID 和标题',
49
+ '/sessionlist --limit N 仅列出当前工作区前 N 个会话',
50
+ ], { aliases: ['sessions'] }),
51
+ defineCatalogCommand('session', '将当前聊天绑定到指定会话', [
52
+ '/session Session ID 或当前工作区序号 将当前聊天绑定到指定会话',
53
+ ]),
54
+ defineCatalogCommand('models', '按序号列出所有可用模型', [
55
+ '/models 按序号列出所有可用模型',
56
+ ]),
57
+ defineCatalogCommand('reasoninglist', '按序号列出当前模型可用推理等级', [
58
+ '/reasoninglist 或 /reasonings 按序号列出当前模型可用推理等级',
59
+ ], { aliases: ['reasonings'] }),
60
+ defineCatalogCommand('reasoning', '查看或切换当前推理等级', [
61
+ '/reasoning [序号、等级ID或 --default] 查看或切换当前推理等级',
62
+ ]),
63
+ defineCatalogCommand('model', '查看或切换当前会话模型', [
64
+ '/model [序号或完整模型ID] [推理等级ID] 查看或切换当前会话模型',
65
+ '示例:先发 /models,再发 /model 2 [推理等级ID]',
66
+ ]),
67
+ defineCatalogCommand('presetlist', '列出可用 Agent Preset', [
68
+ '/presetlist 或 /presets 按序号列出可用 Agent Preset',
69
+ ], { aliases: ['presets'] }),
70
+ defineCatalogCommand('preset', '查看或设置新会话 Agent Preset', [
71
+ '/preset [序号或完整ID] 查看或设置当前机器人 Agent Preset',
72
+ '纯数字 ID:/preset id:<ID>',
73
+ '/preset --default 跟随 Host 默认',
74
+ ]),
75
+ defineCatalogCommand('stop', '停止当前任务', [
76
+ '/stop 停止当前任务',
77
+ ]),
78
+ defineCatalogCommand('steer', '纠偏当前任务', [
79
+ '/steer 补充指令 纠偏当前任务',
80
+ ]),
81
+ defineCatalogCommand('batch', '开始批量输入(仅私聊)', [
82
+ '/batch 开始批量输入(仅私聊,最多 10 条文字)',
83
+ ], { privateOnly: true }),
84
+ defineCatalogCommand('send', '提交当前批次', [
85
+ '/send 提交当前批次',
86
+ ], { privateOnly: true }),
87
+ defineCatalogCommand('cancel', '取消当前批次', [
88
+ '/cancel 取消当前批次',
89
+ ], { privateOnly: true }),
90
+ defineCatalogCommand('status', '检查连接状态', [
91
+ '/status 检查连接状态',
92
+ ]),
93
+ defineCatalogCommand('version', '查看插件版本', [
94
+ '/version 查看插件版本',
95
+ ]),
96
+ defineCatalogCommand('help', '显示帮助', [
97
+ '/help 显示本帮助',
98
+ ]),
99
+ ]);
100
+
101
+ export function commandsForChannel(channel, catalog = SHARED_COMMAND_CATALOG) {
102
+ return catalog.filter((item) => item.enabled !== false && item.channels.includes(channel));
103
+ }
104
+
105
+ export function commandHelpLines(channel, catalog = SHARED_COMMAND_CATALOG) {
106
+ return commandsForChannel(channel, catalog).flatMap((item) => item.help.map((line) => t(line)));
107
+ }
@@ -0,0 +1,32 @@
1
+ import { isBatchInputCommand } from './batch-input.mjs';
2
+ import { isCompactCommand } from './compact-command.mjs';
3
+ import { isControlCommand } from './control-command.mjs';
4
+ import { isHistoryCommand } from './history-command.mjs';
5
+ import { isModelCommand } from './model-command.mjs';
6
+ import { isPresetCommand } from './preset-command.mjs';
7
+ import { isWorkspaceCommand } from './workspace-command.mjs';
8
+
9
+ const SIMPLE_TEXT_COMMANDS = new Set(['/help', '/status', '/new']);
10
+
11
+ /**
12
+ * Match only commands that the shared bridges already execute locally.
13
+ * Unknown slash-prefixed text remains an ordinary prompt.
14
+ */
15
+ export function isSharedLocalCommand(text, {
16
+ hasImages = false,
17
+ hasFiles = false,
18
+ } = {}) {
19
+ if (typeof text !== 'string') return false;
20
+ const command = text.trim();
21
+ if (!command) return false;
22
+ if (isBatchInputCommand(command) || isHistoryCommand(command)) return true;
23
+ if (!hasFiles && (
24
+ isControlCommand(command)
25
+ || isModelCommand(command)
26
+ || isPresetCommand(command)
27
+ )) return true;
28
+ if (hasImages || hasFiles) return false;
29
+ return SIMPLE_TEXT_COMMANDS.has(command.toLowerCase())
30
+ || isWorkspaceCommand(command)
31
+ || isCompactCommand(command);
32
+ }
@@ -0,0 +1,109 @@
1
+ import { t } from './i18n.mjs';
2
+ import { WORKSPACE_SESSION_STALE } from './workspace-session.mjs';
3
+
4
+ const COMPACT_COMMAND = /^\/compact(?=$|\s)([\s\S]*)$/i;
5
+ const COMPACT_USAGE = '用法:/compact(不带参数)';
6
+
7
+ const COMPACT_RESULT_TEXT = new Map([
8
+ ['No compactable history yet.', '暂无可压缩的历史记录。'],
9
+ [
10
+ 'Compaction is unavailable because this process has an active compaction, or the agent is not idle.',
11
+ '当前会话正在生成回复或执行压缩,请稍后重试。',
12
+ ],
13
+ ['Compaction cancelled.', '上下文压缩已取消。'],
14
+ [
15
+ 'The history selected for compaction changed before it could be replaced. The conversation is unchanged; the attempt is recorded in the session log.',
16
+ '压缩期间会话历史发生变化,本次未修改会话,请重试。',
17
+ ],
18
+ [
19
+ 'Compaction could not produce a useful summary. The conversation is unchanged; the attempt is recorded in the session log.',
20
+ '未能生成有效的压缩摘要,本次未修改会话。',
21
+ ],
22
+ [
23
+ 'Compaction did not finish cleanly; some session history may have changed. Inspect the current session state before retrying.',
24
+ '上下文压缩未正常完成,部分会话历史可能已变化,请检查会话后再重试。',
25
+ ],
26
+ [
27
+ 'Compaction finished, but the session could not be saved.',
28
+ '上下文已压缩,但会话保存失败。',
29
+ ],
30
+ ]);
31
+
32
+ function commandResult(message) {
33
+ return { handled: true, message, messages: [message] };
34
+ }
35
+
36
+ function compactResultText(result) {
37
+ if (!result || typeof result !== 'object'
38
+ || !['success', 'error'].includes(result.kind)
39
+ || (result.text !== undefined && typeof result.text !== 'string')) {
40
+ throw new TypeError('Harness returned an invalid /compact result');
41
+ }
42
+ const text = result.text?.trim() ?? '';
43
+ const compacted = /^Compacted (\d+) history items \(~(\d+) tokens\)\.$/u.exec(text);
44
+ if (compacted) {
45
+ return t('已压缩 {count} 条历史记录(约 {tokens} 个 token)。', { count: compacted[1], tokens: compacted[2] });
46
+ }
47
+ if (COMPACT_RESULT_TEXT.has(text)) return t(COMPACT_RESULT_TEXT.get(text));
48
+ if (text) return text;
49
+ return result.kind === 'success' ? t('上下文压缩完成。') : t('上下文压缩失败。');
50
+ }
51
+
52
+ function compactErrorMessage(error) {
53
+ const code = error?.code ?? error?.failure?.code;
54
+ if (code === 'session-not-found') {
55
+ return t('当前聊天绑定的会话已不存在,请发送新消息开启会话。');
56
+ }
57
+ if (code === 'agent-busy') return t('当前会话正在生成回复,请稍后重试。');
58
+ if (code === 'cancelled' || error?.name === 'AbortError') return t('上下文压缩已取消。');
59
+ if (code === WORKSPACE_SESSION_STALE || code === 'workspace-bot-not-found') {
60
+ return t('工作区或机器人状态已发生变化,请重试。');
61
+ }
62
+ if (code === 'commands-unavailable') {
63
+ return t('当前 Harness 暂不支持从机器人执行上下文压缩。');
64
+ }
65
+ return t('上下文压缩失败,请稍后重试。');
66
+ }
67
+
68
+ /**
69
+ * Execute the explicit Harness compaction command for an existing IM conversation Session.
70
+ * Unknown input returns null so the caller may continue ordinary message routing.
71
+ */
72
+ export async function runCompactCommand(text, harness, state, conversationKey, options = {}) {
73
+ if (!isCompactCommand(text)) return null;
74
+ const match = COMPACT_COMMAND.exec(text.trim());
75
+ if (match[1].trim()) return commandResult(t(COMPACT_USAGE));
76
+ if (typeof state?.sessionFor !== 'function') {
77
+ return commandResult(t('当前机器人没有可用的会话状态。'));
78
+ }
79
+ const sessionId = state.sessionFor(conversationKey);
80
+ if (typeof sessionId !== 'string' || !sessionId) {
81
+ return commandResult(t('当前聊天还没有可压缩的会话,请先发送一条消息。'));
82
+ }
83
+ try {
84
+ let execution;
85
+ if (typeof harness?.workspaceSession === 'function') {
86
+ const session = harness.workspaceSession(sessionId, conversationKey);
87
+ if (typeof session?.executeCommand !== 'function') {
88
+ return commandResult(t('当前机器人暂不支持上下文压缩。'));
89
+ }
90
+ execution = await session.executeCommand('/compact', options);
91
+ } else {
92
+ // Legacy Harnesses have no scoped Session handle to carry the route fence.
93
+ if (typeof harness?.executeCommand !== 'function') {
94
+ return commandResult(t('当前机器人暂不支持上下文压缩。'));
95
+ }
96
+ execution = await harness.executeCommand(sessionId, '/compact', options);
97
+ }
98
+ if (execution === undefined) {
99
+ return commandResult(t('当前 Harness 未注册 /compact 命令,请确认上下文压缩组件已启用。'));
100
+ }
101
+ return commandResult(compactResultText(execution?.result));
102
+ } catch (error) {
103
+ return commandResult(compactErrorMessage(error));
104
+ }
105
+ }
106
+
107
+ export function isCompactCommand(text) {
108
+ return typeof text === 'string' && COMPACT_COMMAND.test(text.trim());
109
+ }
@@ -0,0 +1,24 @@
1
+ // Metadata belongs to the validation/read boundary, never to parser excerpts or
2
+ // arbitrary error properties. Keep the original exception type/message so other
3
+ // channels retain their existing startup classification.
4
+ const details = new WeakMap();
5
+
6
+ export function configValidationError(message, field, issue) {
7
+ const error = new Error(message);
8
+ details.set(error, { reason: 'invalid-config', field, issue });
9
+ return error;
10
+ }
11
+
12
+ export function withConfigResource(error, resource) {
13
+ if (error && typeof error === 'object') {
14
+ details.set(error, {
15
+ ...details.get(error), resource,
16
+ ...(error instanceof SyntaxError ? { reason: 'invalid-json' } : {}),
17
+ });
18
+ }
19
+ return error;
20
+ }
21
+
22
+ export function configReadErrorDetails(error) {
23
+ return details.get(error);
24
+ }
@@ -0,0 +1,191 @@
1
+ import { randomUUID } from 'node:crypto';
2
+ import packageInfo from '../../../package.json' with { type: 'json' };
3
+ import { normalizeDiagnosticDetails } from './diagnostic-details.mjs';
4
+ import { t } from './i18n.mjs';
5
+
6
+ const reported = new WeakMap();
7
+ const lastReport = new WeakMap();
8
+ const stages = new WeakMap();
9
+ const OWNED_REASONS = { 'connect-timeout': 'timeout', 'telegram-timeout': 'timeout', 'cli-timeout': 'timeout', 'telegram-response-invalid': 'invalid-response', 'office-protocol-mismatch': 'invalid-response' };
10
+ const STANDARD_NAMES = new Set(['Error', 'TypeError', 'SyntaxError', 'AggregateError', 'DOMException', 'AxiosError', 'TimeoutError', 'AbortError']);
11
+ const KNOWN_MESSAGES = {
12
+ 'discord-401': 'Discord Bot Token 无效,请重新填写。',
13
+ 'discord-intents': 'Discord Gateway Intents 配置不正确,请检查 Developer Portal 的 Bot 设置。',
14
+ 'webhook-configured': '该 Telegram 机器人已配置 Webhook,请先在原服务中移除 Webhook 后重试。',
15
+ 'invalid-device-token': 'AI Office Device Token 无效。',
16
+ 'office-hook-unavailable': 'AI Office Hook 尚未上线或地址不正确。',
17
+ 'messages-database-permission-required': '请在 macOS 系统设置中授予 DSH 完全磁盘访问权限,以读取 Messages 数据库。',
18
+ 'messages-automation-permission-required': '请在 macOS 系统设置中允许 DSH 自动控制 Messages。',
19
+ 'messages-permission-required': '先在 macOS 系统设置中授予 Messages 权限。',
20
+ };
21
+
22
+ /** Inspect a bounded error graph, including SDK aggregate failures. Never copy exception text. */
23
+ export function connectionErrorChain(error) {
24
+ const queue = [error], seen = new Set(), chain = [];
25
+ while (queue.length && chain.length < 16) {
26
+ const entry = queue.shift();
27
+ if (!entry || typeof entry !== 'object' || seen.has(entry)) continue;
28
+ seen.add(entry);
29
+ chain.push(entry);
30
+ if (entry.cause && typeof entry.cause === 'object') queue.push(entry.cause);
31
+ if (Array.isArray(entry.errors)) queue.push(...entry.errors.slice(0, 16));
32
+ }
33
+ return { chain, truncated: queue.some(e => e && typeof e === 'object' && !seen.has(e)) || chain.some(e => Array.isArray(e.errors) && e.errors.length > 16) };
34
+ }
35
+
36
+ export function extractConnectionEvidence(error) {
37
+ const { chain, truncated } = connectionErrorChain(error);
38
+ const errors = chain.map(e => ({
39
+ type: STANDARD_NAMES.has(e.name) ? e.name : 'Error',
40
+ ...normalizeDiagnosticDetails({
41
+ reason: OWNED_REASONS[e.code] ?? e.code ?? (e.name === 'TimeoutError' ? 'timeout' : e instanceof SyntaxError ? 'invalid-json' : undefined),
42
+ httpStatus: e.status ?? e.statusCode ?? e.response?.status,
43
+ providerCode: e.providerCode,
44
+ }),
45
+ hasCause: Boolean(e.cause && typeof e.cause === 'object'),
46
+ hasErrors: Array.isArray(e.errors),
47
+ }));
48
+ const allReasons = [...new Set(errors.map(e => e.reason).filter(r => r && !['unknown', 'multiple-causes'].includes(r)))];
49
+ const precise = allReasons.filter(r => !['timeout', 'http-error', 'provider-rejected', 'invalid-response'].includes(r));
50
+ const reasons = precise.length ? precise : allReasons;
51
+ const first = field => chain.map(e => normalizeDiagnosticDetails({ [field]: e[field] })[field]).find(v => v !== undefined);
52
+ return {
53
+ chain, errors,
54
+ details: normalizeDiagnosticDetails({
55
+ reason: reasons.length > 1 ? 'multiple-causes' : reasons[0] ?? 'unknown',
56
+ ...(reasons.length > 1 ? { reasons } : {}),
57
+ httpStatus: errors.find(e => e.httpStatus !== undefined)?.httpStatus,
58
+ providerCode: errors.find(e => e.providerCode !== undefined)?.providerCode,
59
+ durationMs: first('durationMs'), timeoutMs: first('timeoutMs'), host: first('host'), truncated,
60
+ }),
61
+ };
62
+ }
63
+
64
+ export async function atConnectionStage(stage, operation, resource) {
65
+ try { return await operation(); }
66
+ catch (error) { throw connectionStageError(error, stage, resource); }
67
+ }
68
+
69
+ export function connectionStageError(cause, stage, resource) {
70
+ if (!cause || typeof cause !== 'object') cause = new Error('Operation failed');
71
+ if (!stages.has(cause)) stages.set(cause, normalizeDiagnosticDetails({ stage, resource }));
72
+ return cause;
73
+ }
74
+
75
+ /** Reuse the Controller's report when the same exception reaches RPC. */
76
+ export function reportedConnectionError(error) {
77
+ return connectionErrorChain(error).chain.map(e => reported.get(e) ?? lastReport.get(e)).find(Boolean);
78
+ }
79
+
80
+ export function diagnosticRpcResult(diagnostics, cause, result, context) {
81
+ if (cause?.name === 'AbortError') return { ok: false, error: { code: 'cancelled', message: t('操作已取消。'), details: {} } };
82
+ if (result?.ok !== false || ['cancelled', 'bad-request'].includes(result.error?.code)) return result;
83
+ const error = reportedConnectionError(cause) ?? diagnostics.report(cause, { ...context, publicError: result.error });
84
+ return { ...result, error: context?.untrustedPublicError ? error.publicError : { ...error.publicError, ...result.error, details: error.publicError.details } };
85
+ }
86
+
87
+ export function connectionHint(details) {
88
+ const r = details.reason;
89
+ if (r === 'multiple-causes') return t('检测到多个底层原因,请展开诊断详情逐项检查。');
90
+ if (['ENOTFOUND', 'EAI_AGAIN'].includes(r)) return t('服务域名解析失败,请检查运行 DSH 的机器的 DNS 和网络设置。');
91
+ if (r === 'ECONNREFUSED') return t('目标拒绝连接,请检查服务地址、端口和访问限制。');
92
+ if (['ECONNRESET', 'EPIPE', 'UND_ERR_SOCKET'].includes(r)) return t('连接已中断,请检查网络和目标服务状态。');
93
+ if (['ENETUNREACH', 'EHOSTUNREACH', 'ERR_NETWORK_ACCESS_DENIED'].includes(r)) return t('目标网络不可达,请检查运行 DSH 的机器的网络出口和访问限制。');
94
+ if (/CERT|TLS|SSL|ISSUER|SIGNATURE/.test(r ?? '')) return t('TLS 或证书校验失败,请检查系统时间、证书链和网络设置。');
95
+ if (/TIMEOUT/.test(r ?? '') || r === 'ETIMEDOUT' || r === 'timeout') return t('请求超时,请检查目标服务和网络;诊断详情中可查看已知的耗时。');
96
+ if (r === 'ERR_PROXY_CONNECTION_FAILED' || details.httpStatus === 407) return t('代理连接或认证失败,请检查 DSH 进程使用的代理配置。');
97
+ if (['EAUTH', 'AUTHENTICATIONFAILED'].includes(r)) return t('服务认证失败,请检查账号和授权信息。');
98
+ if (['EACCES', 'EPERM', 'EROFS'].includes(r)) return t('操作权限不足,请检查对应资源的访问权限。');
99
+ if (r === 'ENOSPC') return t('磁盘空间不足,请释放空间后重试。');
100
+ if (r === 'ENOENT') return t('所需文件或本地工具不存在,请检查安装和配置。');
101
+ if (r === 'invalid-json' || r === 'invalid-response') return t('数据格式无效,请按失败阶段检查服务响应或本地配置。');
102
+ if (details.httpStatus === 429) return t('服务正在限流,请稍后重试。');
103
+ if ([401, 403].includes(details.httpStatus)) return t('服务拒绝了请求,请检查凭据、权限或访问限制;HTTP 状态本身不能确认登录失效。');
104
+ if (details.httpStatus >= 500) return t('目标服务暂时异常,请稍后重试。');
105
+ if (details.httpStatus) return t('目标服务返回了 HTTP 错误,请查看诊断详情。');
106
+ return t('暂未识别具体原因,请复制诊断信息,并查看相同参考号的 DSH 日志。');
107
+ }
108
+
109
+ export function operationStage(operation) {
110
+ return ({ 'connector.test': 'connection.test', 'connector.configure': 'credential.verify', 'connector.reconnect': 'connection.start', 'connector.remove': 'account.remove', startup: 'startup.load', 'connection.restore': 'connection.start', 'connection.monitor': 'connection.poll',
111
+ 'connection.status': 'status.read', 'connection.close': 'connection.stop', 'provision.begin': 'qr.begin',
112
+ 'provision.poll': 'qr.poll', 'provision.verify': 'qr.verify', 'bot.auth.start': 'qr.begin', 'bot.auth.poll': 'qr.poll',
113
+ 'bot.reconnect': 'connection.start', 'bot.delete': 'account.remove', 'permissions.status': 'permission.check',
114
+ 'bot.bind-credentials': 'credential.verify', 'bot.bind-mailbox': 'credential.verify', 'bot.bind-native': 'permission.check',
115
+ 'connection.test': 'connection.test', 'message.send': 'message.send', 'message.receive': 'message.receive',
116
+ })[operation] ?? 'operation';
117
+ }
118
+
119
+ /** One reporter per channel owner. Existing safe business guidance can be supplied as publicError. */
120
+ export function createConnectionDiagnostics({ channel, logger = console, now = Date.now, prefix = 'IM-CONN', describe } = {}) {
121
+ const recent = new Map();
122
+ const write = (level, record) => { try { logger[level]?.(`[dsh-${channel}] ${JSON.stringify(record)}`); } catch { /* Logging cannot break an operation. */ } };
123
+ function report(cause, context = {}) {
124
+ if (reported.has(cause)) return cause;
125
+ if (context.reuse && reportedConnectionError(cause)) return reportedConnectionError(cause);
126
+ const evidence = extractConnectionEvidence(cause);
127
+ const prior = evidence.chain.map(e => reported.get(e)).find(Boolean);
128
+ if (prior) return prior;
129
+ const described = describe?.(cause, context, evidence) ?? {};
130
+ const known = evidence.chain.find(e => Object.hasOwn(KNOWN_MESSAGES, e.code ?? ''));
131
+ let safe = described.publicError ?? context.publicError ?? {};
132
+ if (known && !safe.code?.startsWith('stream-')) safe = { ...safe, code: known.code, message: t(KNOWN_MESSAGES[known.code]) };
133
+ // These legacy handlers used exception messages as their public error. Keep only owned guidance.
134
+ if (['email', 'imessage', 'office'].includes(channel) && !known && context.untrustedPublicError) {
135
+ safe = { code: `${channel}-operation-failed`, message: t('操作失败,请查看原因和诊断详情。') };
136
+ }
137
+ const owned = evidence.chain.map(e => stages.get(e)).find(Boolean) ?? {};
138
+ const details = normalizeDiagnosticDetails({
139
+ ...evidence.details, ...context, ...safe.details, ...described.details, ...owned,
140
+ channel, stage: owned.stage ?? described.details?.stage ?? safe.details?.stage ?? context.stage ?? operationStage(context.operation),
141
+ pluginVersion: packageInfo.version, nodeVersion: process.versions.node, platform: process.platform,
142
+ });
143
+ const code = safe.code ?? context.code ?? `${channel}-operation-failed`;
144
+ const hint = described.hint ?? safe.hint ?? safe.details?.hint ?? connectionHint(details);
145
+ const message = t(safe.message) ?? t('操作失败,请查看原因和诊断详情。');
146
+ const key = JSON.stringify([context.botId, context.attemptId, details.operation, details.stage, code, details.reason, details.reasons, details.httpStatus, details.providerCode, details.resource, details.file, details.field, details.issue]);
147
+ const previous = recent.get(key), time = now();
148
+ const wrap = publicError => {
149
+ const error = new Error(publicError.message, { cause });
150
+ error.code = publicError.code;
151
+ error.publicError = structuredClone(publicError);
152
+ reported.set(error, error);
153
+ if (cause && typeof cause === 'object') lastReport.set(cause, error);
154
+ return error;
155
+ };
156
+ if (context.automatic && previous && time - previous.time < 60_000) {
157
+ previous.retries += 1;
158
+ return wrap(previous.publicError);
159
+ }
160
+ if (previous?.retries) write('warn', { event: 'connection-retries', referenceId: previous.publicError.details.referenceId, retries: previous.retries });
161
+ details.referenceId = normalizeDiagnosticDetails({ referenceId: safe.referenceId }).referenceId ?? `${prefix}-${randomUUID().replaceAll('-', '').slice(0, 8).toUpperCase()}`;
162
+ details.occurredAt = new Date(time).toISOString();
163
+ details.hint = hint;
164
+ const publicError = { code, message, details };
165
+ if (context.automatic) {
166
+ if (recent.size >= 128) recent.delete(recent.keys().next().value);
167
+ recent.set(key, { time, publicError, retries: 0, botId: context.botId });
168
+ }
169
+ write(context.warning ? 'warn' : 'error', {
170
+ event: context.warning ? 'connection-warning' : 'connection-failure', code, ...details,
171
+ errors: evidence.errors,
172
+ ...(normalizeDiagnosticDetails({ referenceId: context.parentReferenceId }).referenceId ? { parentReferenceId: context.parentReferenceId } : {}),
173
+ });
174
+ return wrap(publicError);
175
+ }
176
+ function clear(botId) {
177
+ for (const [key, entry] of recent) {
178
+ if (botId !== undefined && entry.botId !== botId) continue;
179
+ if (entry.retries) write('info', { event: 'connection-retries', referenceId: entry.publicError.details.referenceId, retries: entry.retries });
180
+ recent.delete(key);
181
+ }
182
+ }
183
+ function outcome(error, rollback) {
184
+ if (!reported.has(error)) return;
185
+ if (!normalizeDiagnosticDetails({ rollback }).rollback) return;
186
+ error.publicError.details.rollback = rollback;
187
+ error.publicError.details.hint = rollback === 'succeeded' ? t('操作未完成,已恢复之前的本机状态。请处理上述问题后重试。') : t('原账号状态尚未确认,请重新读取状态,并反馈诊断信息。');
188
+ write('info', { event: 'connection-outcome', referenceId: error.publicError.details.referenceId, rollback });
189
+ }
190
+ return { report, clear, outcome };
191
+ }
@@ -0,0 +1,58 @@
1
+ import { t } from './i18n.mjs';
2
+
3
+ const targets = new WeakMap();
4
+
5
+ export const CONNECTION_TEST_STATE_IDENTITY = Symbol('dsh-im.connection-test-state-identity');
6
+
7
+ function stateIdentity(state) {
8
+ return state?.[CONNECTION_TEST_STATE_IDENTITY] ?? state;
9
+ }
10
+
11
+ function cleanText(value) {
12
+ return typeof value === 'string' && value.trim() ? value.trim() : null;
13
+ }
14
+
15
+ export function rememberConnectionTestTarget(state, target) {
16
+ if (!state || !target || typeof target !== 'object') return false;
17
+ try {
18
+ targets.set(stateIdentity(state), structuredClone(target));
19
+ return true;
20
+ } catch {
21
+ return false;
22
+ }
23
+ }
24
+
25
+ export function connectionTestTarget(state) {
26
+ const target = state ? targets.get(stateIdentity(state)) : null;
27
+ return target ? structuredClone(target) : null;
28
+ }
29
+
30
+ export function connectionTestMessage(botName, channelLabel = t('机器人')) {
31
+ const name = cleanText(botName) ?? channelLabel;
32
+ return `${t('✅ DeepSeek Harness 连接测试成功')}\n${t('这条消息由「IM机器人」设置页中的“{name}”机器人卡片发出。', { name })}`;
33
+ }
34
+
35
+ export function connectionTestTargetUnavailable(channelLabel = t('机器人')) {
36
+ const error = new Error(t('{channelLabel}尚未收到可用于测试的私聊消息。', { channelLabel }));
37
+ error.code = 'test-target-unavailable';
38
+ return error;
39
+ }
40
+
41
+ export async function sendRememberedConnectionTest({ state, send, text, channelLabel }) {
42
+ const target = connectionTestTarget(state);
43
+ if (!target) throw connectionTestTargetUnavailable(channelLabel);
44
+ await send(target, text);
45
+ return { sent: true };
46
+ }
47
+
48
+ export function publicConnectionTestResult(error, { diagnostics, botId } = {}) {
49
+ if (!error) return Object.freeze({ sent: true });
50
+ return Object.freeze({
51
+ sent: false,
52
+ code: error?.code === 'test-target-unavailable'
53
+ ? 'test-target-unavailable'
54
+ : 'test-message-failed',
55
+ ...(diagnostics && error?.code !== 'test-target-unavailable'
56
+ ? { error: diagnostics.report(error, { operation: 'connection.test', stage: 'connection.test', botId }).publicError } : {}),
57
+ });
58
+ }