@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,208 @@
1
+ import { randomBytes } from 'node:crypto';
2
+ import { ApiError } from '@tencent-connect/qqbot-nodejs';
3
+ import { workspacePathSnapshot } from '../shared/workspace-command.mjs';
4
+ import { t } from '../shared/i18n.mjs';
5
+
6
+ export const QQ_MENU_TTL_MS = 15 * 60_000;
7
+ const PAGE_SIZE = 6;
8
+ const MAX_MENUS = 256;
9
+ const SECTIONS = new Set(['main', 'sessions', 'workspaces', 'presets', 'models', 'steer', 'custom', 'status', 'help']);
10
+ const clean = (value, limit = 160) => [...String(value ?? '').replace(/[\p{Cc}\p{Cf}]/gu, ' ')].slice(0, limit).join('');
11
+ const command = (label, text) => ({ label, action: { kind: 'command', text } });
12
+ const section = (label, name) => ({ label, action: { kind: 'section', name } });
13
+ const back = () => section(t('返回主菜单'), 'main');
14
+
15
+ export function isQqMenuCommand(text) {
16
+ return typeof text === 'string' && /^\/(?:m|menu)(?=$|\s)/iu.test(text.trim());
17
+ }
18
+
19
+ export function parseQqMenuCommand(text) {
20
+ const match = /^\/(?:m|menu)(?:\s+(\S+))?(?:\s+(\S+))?(?:\s+(\S+))?$/iu.exec(text.trim());
21
+ if (!match) return { error: true };
22
+ const name = match[1]?.toLowerCase() ?? 'main';
23
+ if (SECTIONS.has(name) && !match[2]) return { name };
24
+ if (name === 'pick' && /^[a-f0-9]{24}$/u.test(match[2] ?? '') && /^\d{1,2}$/u.test(match[3] ?? '')) {
25
+ return { token: match[2], number: Number(match[3]) };
26
+ }
27
+ return { error: true };
28
+ }
29
+
30
+ // A menu belongs to its actor and route. The exact displayed actions, including
31
+ // paginated choices, stay in memory; clients only send an opaque token + index.
32
+ export class QqMenuStore {
33
+ #entries = new Map();
34
+ #now;
35
+ constructor({ now = Date.now } = {}) { this.#now = now; }
36
+ #key(route, actor) { return `${route}\0${actor}`; }
37
+ has(route, actor) { return this.#entries.has(this.#key(route, actor)); }
38
+ tokenFor(route, actor) { return this.#entries.get(this.#key(route, actor))?.token; }
39
+ clear(route, actor) { this.#entries.delete(this.#key(route, actor)); }
40
+ begin(route, actor, context) {
41
+ const key = this.#key(route, actor);
42
+ const entry = { ...context, token: randomBytes(12).toString('hex'),
43
+ expiresAt: this.#now() + QQ_MENU_TTL_MS, used: false, view: null };
44
+ this.#entries.delete(key);
45
+ this.#entries.set(key, entry);
46
+ while (this.#entries.size > MAX_MENUS) this.#entries.delete(this.#entries.keys().next().value);
47
+ return entry;
48
+ }
49
+ publish(route, actor, entry, view) {
50
+ if (this.#entries.get(this.#key(route, actor)) !== entry) return false;
51
+ entry.view = view;
52
+ return true;
53
+ }
54
+ take(route, actor, number, token, context) {
55
+ const entry = this.#entries.get(this.#key(route, actor));
56
+ if (!entry || entry.used || !entry.view || entry.expiresAt <= this.#now()
57
+ || (token && token !== entry.token)) return { error: t('这个菜单已过期,请回复 /m 重新打开。') };
58
+ if (entry.workspace !== context.workspace || entry.sessionWorkspace !== context.sessionWorkspace
59
+ || entry.sessionId !== context.sessionId) {
60
+ entry.used = true;
61
+ return { error: t('会话或工作区已变化,请重新发送 /m。') };
62
+ }
63
+ const choice = entry.view.entries[number - 1];
64
+ if (!Number.isSafeInteger(number) || number < 1 || !choice) {
65
+ return { error: t('菜单没有这个编号,回复 /m 重新打开。') };
66
+ }
67
+ // Claim before awaiting anything: retries cannot repeat a destructive action.
68
+ entry.used = true;
69
+ return { action: choice.action, context: entry };
70
+ }
71
+ }
72
+
73
+ export function qqMenuPage(list, requestedPage = 0) {
74
+ const pages = Math.max(1, Math.ceil(list.choices.length / PAGE_SIZE));
75
+ const page = Math.min(Math.max(0, requestedPage), pages - 1);
76
+ const entries = list.choices.slice(page * PAGE_SIZE, (page + 1) * PAGE_SIZE);
77
+ if (page > 0) entries.push({ label: t('上一页'), action: { kind: 'page', list, page: page - 1 } });
78
+ if (page + 1 < pages) entries.push({ label: t('下一页'), action: { kind: 'page', list, page: page + 1 } });
79
+ entries.push(back());
80
+ return { title: list.title, detail: [list.detail, t('第 {page} / {pages} 页', { page: page + 1, pages }),
81
+ list.choices.length ? '' : t('暂无可用选项。')].filter(Boolean).join('\n'), entries, columns: 2 };
82
+ }
83
+
84
+ async function catalogFor(harness, sessionId, key, options) {
85
+ const session = sessionId ? harness.workspaceSession?.(sessionId, key) : null;
86
+ return typeof session?.models === 'function' ? session.models(options) : harness.listModels(options);
87
+ }
88
+
89
+ export async function qqMenuView(name, harness, state, key, { signal, busy = false } = {}) {
90
+ const workspace = harness.currentWorkspace?.();
91
+ const sessionWorkspace = typeof harness.currentConversationWorkspace === 'function'
92
+ ? harness.currentConversationWorkspace(key) : workspace;
93
+ const sessionId = state.sessionFor(key);
94
+ const options = { signal };
95
+ const archived = state.includesArchivedSessions?.() === true;
96
+ const visibleSessions = (listed) => (listed?.sessions ?? []).filter((item) => archived || item.archived !== true);
97
+ if (name === 'main' || name === 'status') {
98
+ if (name === 'status') await harness.ensureRunning(options);
99
+ const results = await Promise.allSettled([
100
+ harness.listWorkspaceSessions?.(name === 'main' ? sessionWorkspace : workspace, options),
101
+ catalogFor(harness, sessionId, key, options),
102
+ harness.agentPresetSettings?.(options),
103
+ ]);
104
+ signal?.throwIfAborted();
105
+ const [listed, catalog, settings] = results.map((r) => r.status === 'fulfilled' ? r.value : null);
106
+ const bound = listed?.sessions?.find((item) => item.sessionId === sessionId);
107
+ const presetId = settings?.agentPreset ?? settings?.agentPresetCatalog?.defaultId;
108
+ const preset = settings?.agentPresetCatalog?.items?.find((item) => item.id === presetId);
109
+ const model = catalog?.groups?.find((group) => group.id === catalog.current?.provider)
110
+ ?.models?.find((item) => item.id === catalog.current?.model);
111
+ const detail = [
112
+ name === 'status' ? t('QQ 机器人与 DeepSeek Harness 连接正常。') : '',
113
+ t('工作区:{workspace}', { workspace: clean(workspace) || t('未设置') }),
114
+ t('当前会话:{session}', { session: clean(bound?.title || sessionId) || t('新会话') }),
115
+ t('模式/预设:{preset}', { preset: clean(preset?.label || presetId) || t('跟随 Host 默认') }),
116
+ t('当前模型:{model}', { model: clean(model?.name || catalog?.current?.model) || t('暂不可用') }),
117
+ t('归档会话:{state}', { state: archived ? t('显示') : t('隐藏') }),
118
+ t('任务状态:{state}', { state: busy ? t('处理中') : t('空闲') }),
119
+ results.some((r) => r.status === 'rejected') ? t('部分设置暂未加载,可进入对应列表重试。') : '',
120
+ ].filter(Boolean).join('\n');
121
+ return { title: name === 'main' ? t('🤖 助手中心') : t('📊 状态'), detail,
122
+ columns: 3, entries: name === 'status' ? [back()] : [
123
+ section(t('💬 会话选择'), 'sessions'), section(t('📁 工作区'), 'workspaces'), section(t('🤖 模式/预设'), 'presets'),
124
+ section(t('🧠 模型'), 'models'), command(t('🆕 新会话'), '/new'), section(t('📋 会话列表'), 'sessions'),
125
+ command(t('⏹ 停止'), '/stop'), command(t('📐 压缩'), '/compact'), section(t('💬 补充指令'), 'steer'),
126
+ { label: t('切换归档显示'), action: { kind: 'archive', include: !archived } },
127
+ section(t('📊 状态'), 'status'), section(t('📖 帮助'), 'help'),
128
+ ] };
129
+ }
130
+ if (name === 'sessions') {
131
+ const listed = await harness.listWorkspaceSessions(sessionWorkspace, options);
132
+ return qqMenuPage({ title: t('📋 会话列表'), detail: clean(sessionWorkspace), choices: visibleSessions(listed).map((item) =>
133
+ command(`${item.sessionId === sessionId ? '✓ ' : ''}${clean(item.title || item.sessionId, 70)}${item.archived ? t('(已归档)') : ''}`, `/session ${item.sessionId}`)) });
134
+ }
135
+ if (name === 'workspaces') {
136
+ const { paths } = await workspacePathSnapshot(harness, options);
137
+ return qqMenuPage({ title: t('📁 工作区'), choices: paths.map((path) =>
138
+ command(`${path === workspace ? '✓ ' : ''}${clean(path)}`, `/workspace ${path}`)) });
139
+ }
140
+ if (name === 'models') {
141
+ const catalog = await catalogFor(harness, sessionId, key, options);
142
+ return qqMenuPage({ title: t('🧠 模型'), detail: catalog.failures?.length ? t('部分模型暂不可用,可稍后重试。') : '',
143
+ choices: catalog.groups.flatMap((group) => group.models.map((model) => command(
144
+ `${catalog.current?.provider === group.id && catalog.current?.model === model.id ? '✓ ' : ''}${clean(group.name, 30)} · ${clean(model.name, 60)}`,
145
+ `/model ${group.id}/${model.id}`))) });
146
+ }
147
+ if (name === 'presets') {
148
+ const settings = await harness.agentPresetSettings(options);
149
+ return qqMenuPage({ title: t('🤖 模式/预设'), detail: t('预设仅用于之后的新会话。'), choices: [
150
+ command(`${settings.agentPreset === null ? '✓ ' : ''}${t('跟随 Host 默认')}`, '/preset --default'),
151
+ ...settings.agentPresetCatalog.items.map((item) => command(
152
+ `${settings.agentPreset === item.id ? '✓ ' : ''}${clean(item.label || item.id, 70)}`,
153
+ `/preset ${/^\d+$/u.test(item.id) ? 'id:' : ''}${item.id}`)),
154
+ ] });
155
+ }
156
+ if (name === 'steer') return { title: t('💬 补充指令'), columns: 2,
157
+ detail: t('给当前正在运行的任务补充指令。'), entries: [
158
+ ...['继续', '加速运行', '总结当前进展', '更简洁些', '更详细些'].map((text) => command(t(text), `/steer ${t(text)}`)),
159
+ section(t('自定义指令'), 'custom'), back(),
160
+ ] };
161
+ if (name === 'custom') return { title: t('自定义指令'), detail: t('请发送 /steer 你的补充指令。'), entries: [back()] };
162
+ return { title: t('📖 帮助'), detail: [
163
+ t('发送 /m 打开助手中心,点击按钮或回复当前列表的数字选择。'),
164
+ t('设置:会话、工作区、模式/预设、模型。'),
165
+ t('会话:新会话、会话列表、归档显示切换。'),
166
+ t('任务:停止、压缩、快捷或自定义补充指令。'),
167
+ t('信息:状态、帮助。'),
168
+ t('菜单有效期为 15 分钟;发送普通消息会退出数字选择。'),
169
+ t('回答问题、审批和批量输入时,数字优先用于当前交互。'),
170
+ t('完整命令说明:/help'),
171
+ ].join('\n'), entries: [back()] };
172
+ }
173
+
174
+ export function qqMenuText(view, { fallback = false } = {}) {
175
+ return [view.title, view.detail, '', ...view.entries.map((entry, index) => `${index + 1}. ${entry.label}`), '',
176
+ fallback ? t('当前无法展示按钮,请回复数字选择。') : t('点击按钮或回复数字选择,/m 返回主菜单。'),
177
+ t('回答问题、审批和批量输入时,请先完成当前交互。'),
178
+ ].filter((line) => line !== undefined).join('\n');
179
+ }
180
+
181
+ export function qqMenuKeyboard(view, token) {
182
+ const buttons = view.entries.map((entry, index) => ({
183
+ id: `${token}_${index + 1}`,
184
+ render_data: { label: view.columns === 3
185
+ ? clean(entry.label, 24).replace(t('模式/预设'), t('预设')).replace(t('切换归档显示'), t('归档显示'))
186
+ : `${index + 1}. ${clean(entry.label, 32)}`, visited_label: t('已选择'), style: 0 },
187
+ action: { type: 2, permission: { type: 2 }, data: `/m pick ${token} ${index + 1}`, enter: true,
188
+ unsupport_tips: t('请回复数字选择,或发送 /m 重新打开菜单。') },
189
+ }));
190
+ const rows = [];
191
+ for (let i = 0; i < buttons.length; i += view.columns ?? 2) rows.push({ buttons: buttons.slice(i, i + (view.columns ?? 2)) });
192
+ return { content: { rows } };
193
+ }
194
+
195
+ export async function sendQqMenu(bot, target, view, token, { logger = console } = {}) {
196
+ if (typeof bot.send === 'function') {
197
+ try {
198
+ const content = qqMenuText(view).replace(/[\\`*_{}\[\]()<>#|!]/gu, '\\$&');
199
+ return await bot.send({ target, msgType: 2, markdown: { content }, keyboard: qqMenuKeyboard(view, token) });
200
+ } catch (error) {
201
+ // A timeout may have delivered already. Only a definite provider rejection
202
+ // permits another send, so a network fault never creates duplicate menus.
203
+ if (!(error instanceof ApiError) || error.httpStatus < 400 || error.httpStatus >= 500 || error.httpStatus === 429) throw error;
204
+ logger.warn?.('[dsh-im:qq] menu rejected; using numbered text:', error.bizCode);
205
+ }
206
+ }
207
+ return bot.sendText(target, qqMenuText(view, { fallback: true }));
208
+ }
@@ -0,0 +1,303 @@
1
+ import { extractConnectionEvidence, createConnectionDiagnostics, atConnectionStage } from '../shared/connection-error.mjs';
2
+ import { QQBot, contentSanitizer, typingIndicator } from '@tencent-connect/qqbot-nodejs';
3
+
4
+ import {
5
+ connectionTestTarget,
6
+ connectionTestTargetUnavailable,
7
+ } from '../shared/connection-test.mjs';
8
+ import { t } from '../shared/i18n.mjs';
9
+ import { evaluateInboundAccess } from '../shared/inbound-access.mjs';
10
+ import { createQqBridgeStatus, QqHarnessBridge } from './qq-bridge.mjs';
11
+
12
+ function timeoutError() {
13
+ const error = new Error('QQ WebSocket did not become ready in time');
14
+ error.code = 'connect-timeout';
15
+ return error;
16
+ }
17
+
18
+ export function createQqRuntimeStatus() {
19
+ return {
20
+ startedAt: null,
21
+ ready: false,
22
+ qqConnectionState: 'idle',
23
+ harnessReachable: false,
24
+ lastCheckedAt: null,
25
+ lastConnectedAt: null,
26
+ lastError: null,
27
+ ...createQqBridgeStatus(),
28
+ };
29
+ }
30
+
31
+ export class QqRuntime {
32
+ #config;
33
+ #appSecret;
34
+ #harness;
35
+ #state;
36
+ #contextEnhancement;
37
+ #accessPolicy;
38
+ #logger;
39
+ #diagnostics;
40
+ #replyTimeoutMs;
41
+ #connectTimeoutMs;
42
+ #createBot;
43
+ #typingMiddleware;
44
+ #status = createQqRuntimeStatus();
45
+ #bot = null;
46
+ #bridge = null;
47
+ #abortController = null;
48
+ #runTask = null;
49
+ #starting = null;
50
+
51
+ constructor({
52
+ config,
53
+ appSecret,
54
+ harness,
55
+ state,
56
+ contextEnhancement,
57
+ accessPolicy,
58
+ logger = console,
59
+ replyTimeoutMs = 600_000,
60
+ connectTimeoutMs = 20_000,
61
+ createBot = (options) => new QQBot(options),
62
+ typingMiddleware = typingIndicator,
63
+ }) {
64
+ if (!config || !appSecret || !harness || !state) {
65
+ throw new TypeError('QqRuntime requires config, app secret, Harness, and state');
66
+ }
67
+ this.#config = config;
68
+ this.#appSecret = appSecret;
69
+ this.#harness = harness;
70
+ this.#state = state;
71
+ this.#contextEnhancement = contextEnhancement;
72
+ this.#accessPolicy = accessPolicy;
73
+ this.#logger = logger; this.#diagnostics = createConnectionDiagnostics({ channel: 'qq', logger });
74
+ this.#replyTimeoutMs = replyTimeoutMs;
75
+ this.#connectTimeoutMs = connectTimeoutMs;
76
+ this.#createBot = createBot;
77
+ this.#typingMiddleware = typingMiddleware;
78
+ }
79
+
80
+ get status() {
81
+ return structuredClone(this.#status);
82
+ }
83
+
84
+ async sendConnectionTest(text) {
85
+ if (!this.#status.ready || !this.#bot) {
86
+ throw connectionTestTargetUnavailable(t('QQ机器人'));
87
+ }
88
+ const ownerUserOpenid = typeof this.#config.ownerUserOpenid === 'string'
89
+ ? this.#config.ownerUserOpenid.trim()
90
+ : '';
91
+ const remembered = connectionTestTarget(this.#state);
92
+ const rememberedUserOpenid = remembered?.scope === 'c2c'
93
+ && typeof remembered.targetId === 'string'
94
+ ? remembered.targetId.trim()
95
+ : '';
96
+ const target = rememberedUserOpenid
97
+ ? { scope: 'c2c', targetId: rememberedUserOpenid }
98
+ : (ownerUserOpenid && ownerUserOpenid !== '*'
99
+ ? { scope: 'c2c', targetId: ownerUserOpenid }
100
+ : null);
101
+ if (!target) throw connectionTestTargetUnavailable(t('QQ机器人'));
102
+ await this.#bot.sendText(target, text);
103
+ return { sent: true };
104
+ }
105
+
106
+ async sendProactiveText(target, text, { signal } = {}) {
107
+ const nativeId = target?.kind === 'user'
108
+ ? (typeof target?.route?.userOpenId === 'string' ? target.route.userOpenId.trim() : '')
109
+ : target?.kind === 'group'
110
+ ? (typeof target?.route?.groupOpenId === 'string' ? target.route.groupOpenId.trim() : '')
111
+ : '';
112
+ if (!nativeId) {
113
+ const error = new TypeError('Invalid QQ proactive delivery target');
114
+ error.code = 'invalid-target';
115
+ throw error;
116
+ }
117
+ if (!this.#status.ready || !this.#bot) {
118
+ const error = new Error('QQ bot is not connected');
119
+ error.code = 'bot-not-connected';
120
+ throw error;
121
+ }
122
+ signal?.throwIfAborted();
123
+ return this.#bot.sendText({
124
+ scope: target.kind === 'user' ? 'c2c' : 'group',
125
+ targetId: nativeId,
126
+ }, text);
127
+ }
128
+
129
+ async start() {
130
+ if (this.#status.ready && this.#bot) return this.status;
131
+ if (this.#starting) return this.#starting;
132
+ this.#starting = this.#start().finally(() => {
133
+ this.#starting = null;
134
+ });
135
+ return this.#starting;
136
+ }
137
+
138
+ async #start() {
139
+ await this.stop();
140
+ this.#status.startedAt = new Date().toISOString();
141
+ this.#status.qqConnectionState = 'connecting';
142
+ this.#status.lastError = null; this.#status.error = null; this.#diagnostics.clear();
143
+ await atConnectionStage('harness.check', () => this.#harness.ensureRunning());
144
+ this.#status.harnessReachable = true;
145
+
146
+ const sdkLogger = {
147
+ error: (...args) => this.#logger.error?.(...args),
148
+ warn: (...args) => this.#logger.warn?.(...args),
149
+ info: (...args) => this.#logger.info?.(...args),
150
+ debug: () => {},
151
+ };
152
+ const bot = this.#createBot({
153
+ appId: this.#config.appId,
154
+ appSecret: this.#appSecret,
155
+ accountId: this.#config.botId,
156
+ logger: sdkLogger,
157
+ transport: 'websocket',
158
+ tokenPrefetch: 'sync',
159
+ // sendText is reserved for literal notices/connection tests. Markdown
160
+ // replies use the explicit msg_type=2 path in sendMarkdownReply().
161
+ markdownSupport: false,
162
+ });
163
+ if (!bot || typeof bot.start !== 'function' || typeof bot.stop !== 'function') {
164
+ throw new TypeError('QQ bot factory returned an invalid client');
165
+ }
166
+ const controller = new AbortController();
167
+ this.#abortController = controller;
168
+ this.#bot = bot;
169
+ this.#bridge = new QqHarnessBridge({
170
+ bot,
171
+ ownerUserOpenid: this.#config.ownerUserOpenid,
172
+ harness: this.#harness,
173
+ state: this.#state,
174
+ contextEnhancement: this.#contextEnhancement,
175
+ accessPolicy: this.#accessPolicy,
176
+ status: this.#status,
177
+ logger: this.#logger,
178
+ replyTimeoutMs: this.#replyTimeoutMs,
179
+ signal: controller.signal,
180
+ });
181
+ // QQ delivers emoji/face messages as opaque `<faceType=..,faceId="..",ext="..">`
182
+ // tags. Parse them into readable text so the Harness sees what the sender
183
+ // actually meant instead of an unusable markup fragment.
184
+ bot.use(contentSanitizer({ parseFaceTags: true }));
185
+ bot.use?.(this.#typingMiddleware({
186
+ keepAlive: true,
187
+ predicate: (ctx) => {
188
+ const message = ctx?.message;
189
+ if (!message || (message.kind === 'group'
190
+ && message.rawEventType !== 'GROUP_AT_MESSAGE_CREATE')) return false;
191
+ if (!this.#accessPolicy) {
192
+ return message.kind === 'group'
193
+ || this.#config.ownerUserOpenid === '*'
194
+ || message.senderId === this.#config.ownerUserOpenid;
195
+ }
196
+ return evaluateInboundAccess(this.#accessPolicy, {
197
+ conversationType: message.kind === 'c2c' ? 'direct' : 'group',
198
+ senderIds: message.senderId,
199
+ text: typeof message.content === 'string' ? message.content.trim() : '',
200
+ }).allowed;
201
+ },
202
+ }));
203
+
204
+ let readyResolve;
205
+ let readyReject;
206
+ const ready = new Promise((resolve, reject) => {
207
+ readyResolve = resolve;
208
+ readyReject = reject;
209
+ });
210
+ const onReady = () => {
211
+ const now = Date.now();
212
+ this.#status.ready = true;
213
+ this.#status.qqConnectionState = 'connected';
214
+ this.#status.lastCheckedAt = now;
215
+ this.#status.lastConnectedAt = now;
216
+ this.#status.lastError = null; this.#status.error = null; this.#diagnostics.clear();
217
+ readyResolve();
218
+ };
219
+ const onError = (error) => {
220
+ if (!this.#status.ready) readyReject(error);
221
+ else {
222
+ this.#status.error = this.#diagnostics.report(error, { operation: 'connection.monitor', botId: this.#config?.botId, automatic: true }).publicError;
223
+ this.#status.lastError = this.#status.error.message;
224
+ this.#logger.warn?.(`[dsh-im:qq] bot ${this.#config.botId} connection error:`, extractConnectionEvidence(error).details);
225
+ }
226
+ };
227
+ const onMessage = (_ctx, message) => {
228
+ const task = this.#bridge?.accept(message);
229
+ if (!task) return;
230
+ void task.catch((error) => {
231
+ if (controller.signal.aborted) return;
232
+ this.#logger.error?.(
233
+ `[dsh-im:qq] bot ${this.#config.botId} message handling failed:`,
234
+ extractConnectionEvidence(error).details,
235
+ );
236
+ });
237
+ };
238
+ bot.on('ready', onReady);
239
+ bot.on('resumed', onReady);
240
+ bot.on('error', onError);
241
+ bot.on('message', onMessage);
242
+
243
+ const runTask = Promise.resolve().then(() => bot.start(controller.signal));
244
+ this.#runTask = runTask;
245
+ runTask.catch((error) => {
246
+ if (controller.signal.aborted) return;
247
+ readyReject(error);
248
+ this.#status.ready = false;
249
+ this.#status.qqConnectionState = 'failed';
250
+ this.#status.error = this.#diagnostics.report(error, { operation: 'connection.monitor', botId: this.#config?.botId, automatic: true }).publicError;
251
+ this.#status.lastError = this.#status.error.message;
252
+ this.#logger.error?.(`[dsh-im:qq] bot ${this.#config.botId} connection stopped:`, extractConnectionEvidence(error).details);
253
+ });
254
+
255
+ let timer;
256
+ try {
257
+ await Promise.race([
258
+ ready,
259
+ new Promise((_, reject) => {
260
+ timer = setTimeout(() => reject(timeoutError()), this.#connectTimeoutMs);
261
+ }),
262
+ ]);
263
+ this.#status.ready = true;
264
+ this.#status.qqConnectionState = 'connected';
265
+ this.#status.lastCheckedAt = Date.now();
266
+ this.#status.lastConnectedAt = Date.now();
267
+ return this.status;
268
+ } catch (error) {
269
+ this.#status.ready = false;
270
+ this.#status.qqConnectionState = 'failed';
271
+ this.#status.error = this.#diagnostics.report(error, { operation: 'connection.monitor', botId: this.#config?.botId, automatic: true }).publicError;
272
+ this.#status.lastError = this.#status.error.message;
273
+ await this.stop();
274
+ throw error;
275
+ } finally {
276
+ clearTimeout(timer);
277
+ }
278
+ }
279
+
280
+ async stop() {
281
+ const bot = this.#bot;
282
+ const bridge = this.#bridge;
283
+ const runTask = this.#runTask;
284
+ this.#abortController?.abort();
285
+ this.#abortController = null;
286
+ this.#bot = null;
287
+ this.#bridge = null;
288
+ this.#runTask = null;
289
+ try {
290
+ bot?.stop();
291
+ } catch (error) {
292
+ this.#logger.warn?.(`[dsh-im:qq] bot ${this.#config.botId} failed to stop cleanly:`, extractConnectionEvidence(error).details);
293
+ }
294
+ await Promise.race([
295
+ runTask?.catch(() => undefined) ?? Promise.resolve(),
296
+ new Promise((resolve) => setTimeout(resolve, 2_000)),
297
+ ]);
298
+ await bridge?.waitForIdle();
299
+ this.#status.ready = false;
300
+ this.#status.qqConnectionState = 'idle';
301
+ return this.status;
302
+ }
303
+ }
@@ -0,0 +1,24 @@
1
+ import { startQrConnect } from '@tencent-connect/qqbot-connector';
2
+
3
+ export class QqQrAuth {
4
+ #start;
5
+ #source;
6
+
7
+ constructor({ start = startQrConnect, source = 'deepseek-harness' } = {}) {
8
+ if (typeof start !== 'function') throw new TypeError('QQ QR connector is required');
9
+ this.#start = start;
10
+ this.#source = source;
11
+ }
12
+
13
+ start(callbacks, { signal } = {}) {
14
+ if (!callbacks || typeof callbacks.onSuccess !== 'function'
15
+ || typeof callbacks.onFailure !== 'function') {
16
+ throw new TypeError('QQ QR callbacks are required');
17
+ }
18
+ return this.#start(callbacks, {
19
+ displayQrCodeToConsole: false,
20
+ source: this.#source,
21
+ signal,
22
+ });
23
+ }
24
+ }
@@ -0,0 +1,17 @@
1
+ import { t } from '../shared/i18n.mjs';
2
+
3
+ const MESSAGES = Object.freeze({
4
+ 'state-read-failed': '无法读取 QQ 本地状态,请检查数据目录及访问权限。',
5
+ 'state-backup-failed': 'QQ 本地状态已损坏,但无法备份,原文件已保留。',
6
+ 'state-write-failed': '无法保存 QQ 本地状态,请检查磁盘空间及目录写入权限。',
7
+ });
8
+
9
+ export function qqStateError(code, cause) {
10
+ return Object.assign(new Error('QQ state operation failed', { cause }), { code });
11
+ }
12
+
13
+ export function publicQqStateError(error) {
14
+ return Object.hasOwn(MESSAGES, error?.code ?? '')
15
+ ? { code: error.code, message: t(MESSAGES[error.code]) }
16
+ : null;
17
+ }