@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,25 @@
1
+ export const FEISHU_STEP_PUSH_MODES = Object.freeze({
2
+ POST: 'post',
3
+ STREAMING_CARD: 'streaming_card',
4
+ LIVE_COT: 'live_cot',
5
+ });
6
+
7
+ /** New connections explicitly opt into the process-card presentation. */
8
+ export const DEFAULT_FEISHU_STEP_PUSH_MODE = FEISHU_STEP_PUSH_MODES.STREAMING_CARD;
9
+
10
+ export function normalizeFeishuStepPushMode(value) {
11
+ // Bots created before modes existed used posts when step push was enabled.
12
+ if (value === FEISHU_STEP_PUSH_MODES.STREAMING_CARD) {
13
+ return FEISHU_STEP_PUSH_MODES.STREAMING_CARD;
14
+ }
15
+ if (value === FEISHU_STEP_PUSH_MODES.LIVE_COT) {
16
+ return FEISHU_STEP_PUSH_MODES.LIVE_COT;
17
+ }
18
+ return FEISHU_STEP_PUSH_MODES.POST;
19
+ }
20
+
21
+ export function isFeishuStepPushMode(value) {
22
+ return value === FEISHU_STEP_PUSH_MODES.POST
23
+ || value === FEISHU_STEP_PUSH_MODES.STREAMING_CARD
24
+ || value === FEISHU_STEP_PUSH_MODES.LIVE_COT;
25
+ }
@@ -0,0 +1,34 @@
1
+ // voice-config.mjs — 飞书语音配置的纯数据归一化(浏览器安全,无 Node 内置模块)。
2
+ // 运行时转写/合成能力见 voice.mjs;此处只负责把每机器人配置校验为固定形状,
3
+ // 供 host 侧配置存储、RPC 校验与 client 侧设置页共用。
4
+ export const DEFAULT_VOICE_SECRET_REF = 'DASHSCOPE_API_KEY';
5
+ export const DEFAULT_ASR_BASE_URL = 'https://dashscope.aliyuncs.com/compatible-mode/v1';
6
+ export const DEFAULT_ASR_MODEL = 'qwen3-asr-flash';
7
+ export const DEFAULT_TTS_MODEL = 'qwen3-tts-flash';
8
+ export const DEFAULT_TTS_VOICE = 'Momo';
9
+ export const TTS_ENDPOINT = 'https://dashscope.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation';
10
+ export const MAX_TTS_CHARS = 600;
11
+
12
+ function cleanString(value) {
13
+ return typeof value === 'string' && value.trim() ? value.trim() : null;
14
+ }
15
+
16
+ /**
17
+ * 归一化每机器人语音配置。语音是可选的渠道能力:关闭或配置不完整时返回 null,
18
+ * 与未配置行为完全一致。secretRef 走插件凭据库(与飞书 app secret 相同的环境变量
19
+ * 命名规范),密钥本身绝不进配置。
20
+ */
21
+ export function normalizeFeishuVoiceConfig(value) {
22
+ if (value == null || typeof value !== 'object' || value.enabled !== true) return null;
23
+ const secretRef = cleanString(value.secretRef);
24
+ if (!secretRef || !/^[A-Za-z_][A-Za-z0-9_]*$/.test(secretRef)) return null;
25
+ return Object.freeze({
26
+ enabled: true,
27
+ secretRef,
28
+ asrModel: cleanString(value.asrModel) ?? DEFAULT_ASR_MODEL,
29
+ ttsModel: cleanString(value.ttsModel) ?? DEFAULT_TTS_MODEL,
30
+ ttsVoice: cleanString(value.ttsVoice) ?? DEFAULT_TTS_VOICE,
31
+ asrBaseUrl: cleanString(value.asrBaseUrl) ?? DEFAULT_ASR_BASE_URL,
32
+ ffmpeg: cleanString(value.ffmpeg),
33
+ });
34
+ }
@@ -0,0 +1,134 @@
1
+ // voice.mjs — 飞书渠道语音能力运行时(渠道能力:语音输入与语音回复)
2
+ // 入站:音频消息 opus → ffmpeg 转 wav 16k → DashScope qwen3-asr-flash 转文字,
3
+ // 回流入正常文本流水线;转写失败走明确降级(保持原有"仅支持文字/图片/文件"提示)。
4
+ // 出站:同回合文字回复 → qwen3-tts-flash → ffmpeg 转 opus → 飞书音频消息,
5
+ // 优先回复原语音消息,reply 不可用时降级为普通音频消息。
6
+ // 凭据经插件凭据库(secretRef)在运行时注入;本模块不落盘、不把密钥写进日志。
7
+ // 配置归一化(浏览器安全)在 voice-config.mjs,供 client 侧设置页与 host 侧共用。
8
+ import { spawn } from 'node:child_process';
9
+ import { randomUUID } from 'node:crypto';
10
+ import { mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
11
+ import { tmpdir } from 'node:os';
12
+ import { join } from 'node:path';
13
+ import { MAX_TTS_CHARS, TTS_ENDPOINT, normalizeFeishuVoiceConfig } from './voice-config.mjs';
14
+
15
+ function runFfmpeg(bin, args) {
16
+ return new Promise((resolve, reject) => {
17
+ const p = spawn(bin, ['-hide_banner', '-loglevel', 'error', '-y', ...args]);
18
+ let err = '';
19
+ p.stderr.on('data', (d) => { err += d; if (err.length > 2000) err = err.slice(-2000); });
20
+ // 启动失败(未安装 ffmpeg、配置路径不存在或不可执行)时 spawn 触发
21
+ // 'error' 事件而非 'close';不监听会让未处理的 ENOENT 异常击穿 Node 进程。
22
+ // 这里改为正常 reject,由外层的转写/合成失败降级统一兜底。
23
+ p.on('error', reject);
24
+ p.on('close', (code) => (code === 0 ? resolve() : reject(new Error(`ffmpeg exit ${code}: ${err.slice(-300)}`))));
25
+ });
26
+ }
27
+
28
+ async function readResourceToBuffer(resource) {
29
+ if (typeof resource?.writeFile === 'function') {
30
+ const path = join(tmpdir(), `dsh-voice-in-${randomUUID()}.opus`);
31
+ await resource.writeFile(path);
32
+ const buf = readFileSync(path);
33
+ rmSync(path, { force: true });
34
+ return buf;
35
+ }
36
+ if (resource?.getReadableStream) {
37
+ const chunks = [];
38
+ for await (const chunk of resource.getReadableStream()) {
39
+ chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
40
+ }
41
+ return Buffer.concat(chunks);
42
+ }
43
+ throw new Error('消息资源无可用读取接口');
44
+ }
45
+
46
+ /**
47
+ * 创建语音能力对象。settings 为 normalizeFeishuVoiceConfig 的产物,secret 为
48
+ * 凭据库解析出的 DashScope api_key。两者任一缺失即返回 { enabled: false },
49
+ * 桥接层据此完全跳过语音路径。
50
+ */
51
+ export function createVoice({ settings, secret, logger } = {}) {
52
+ const config = normalizeFeishuVoiceConfig(settings);
53
+ const apiKey = typeof secret === 'string' && secret.trim() ? secret.trim() : null;
54
+ if (!config || !apiKey) {
55
+ if (config) logger?.warn?.('[dsh-feishu-voice] 语音凭据缺失,语音已禁用');
56
+ return Object.freeze({ enabled: false });
57
+ }
58
+ const frozen = Object.freeze({
59
+ apiKey,
60
+ asrBaseUrl: config.asrBaseUrl,
61
+ asrModel: config.asrModel,
62
+ ttsModel: config.ttsModel,
63
+ ttsVoice: config.ttsVoice,
64
+ ffmpeg: config.ffmpeg ?? 'ffmpeg',
65
+ });
66
+
67
+ async function transcribeIncoming(event, client) {
68
+ const fileKey = JSON.parse(event.message.content).file_key;
69
+ if (!fileKey) return null;
70
+ const resource = await client.im.v1.messageResource.get({
71
+ params: { type: 'file' },
72
+ path: { message_id: event.message.message_id, file_key: fileKey },
73
+ });
74
+ const opusBuf = await readResourceToBuffer(resource);
75
+ const work = join(tmpdir(), `dsh-voice-${randomUUID()}`);
76
+ mkdirSync(work, { recursive: true });
77
+ try {
78
+ const opusIn = join(work, 'in.opus');
79
+ const wavIn = join(work, 'in.wav');
80
+ writeFileSync(opusIn, opusBuf);
81
+ await runFfmpeg(frozen.ffmpeg, ['-i', opusIn, '-ar', '16000', '-ac', '1', wavIn]);
82
+ const b64 = readFileSync(wavIn).toString('base64');
83
+ // qwen3-asr-flash 走 compatible-mode chat/completions + input_audio data URI
84
+ const res = await fetch(`${frozen.asrBaseUrl}/chat/completions`, {
85
+ method: 'POST',
86
+ headers: { Authorization: `Bearer ${frozen.apiKey}`, 'Content-Type': 'application/json' },
87
+ body: JSON.stringify({
88
+ model: frozen.asrModel,
89
+ messages: [{
90
+ role: 'user',
91
+ content: [{ type: 'input_audio', input_audio: { data: `data:audio/wav;base64,${b64}` } }],
92
+ }],
93
+ }),
94
+ });
95
+ const text = await res.text();
96
+ if (!res.ok) throw new Error(`dashscope ASR ${res.status}: ${text.slice(0, 200)}`);
97
+ const j = JSON.parse(text);
98
+ const out = j?.choices?.[0]?.message?.content;
99
+ if (typeof out !== 'string' || !out.trim()) throw new Error('ASR 无文本输出');
100
+ return out.trim();
101
+ } finally {
102
+ rmSync(work, { recursive: true, force: true });
103
+ }
104
+ }
105
+
106
+ async function synthesize(text) {
107
+ const clean = String(text ?? '').replace(/\s+/g, ' ').trim().slice(0, MAX_TTS_CHARS);
108
+ if (!clean) return null;
109
+ // qwen3-tts-flash 走 DashScope 原生 multimodal-generation 接口(compatible-mode 不支持 TTS)
110
+ const res = await fetch(TTS_ENDPOINT, {
111
+ method: 'POST',
112
+ headers: { Authorization: `Bearer ${frozen.apiKey}`, 'Content-Type': 'application/json' },
113
+ body: JSON.stringify({ model: frozen.ttsModel, input: { text: clean, voice: frozen.ttsVoice } }),
114
+ });
115
+ const j = await res.json().catch(() => null);
116
+ const url = j?.output?.audio?.url;
117
+ if (!url) throw new Error(`TTS 无音频:${res.status} ${JSON.stringify(j ?? {}).slice(0, 160)}`);
118
+ const aud = await fetch(url);
119
+ if (!aud.ok) throw new Error(`TTS 音频下载 ${aud.status}`);
120
+ const work = join(tmpdir(), `dsh-voice-${randomUUID()}`);
121
+ mkdirSync(work, { recursive: true });
122
+ try {
123
+ const wavOut = join(work, 'out.wav');
124
+ const opusOut = join(work, 'out.opus');
125
+ writeFileSync(wavOut, Buffer.from(await aud.arrayBuffer()));
126
+ await runFfmpeg(frozen.ffmpeg, ['-i', wavOut, '-c:a', 'libopus', '-b:a', '48k', opusOut]);
127
+ return readFileSync(opusOut);
128
+ } finally {
129
+ rmSync(work, { recursive: true, force: true });
130
+ }
131
+ }
132
+
133
+ return Object.freeze({ enabled: true, transcribeIncoming, synthesize });
134
+ }
@@ -0,0 +1,24 @@
1
+ import {
2
+ deriveTokenBotIdentity,
3
+ maskPlatformId,
4
+ TokenBotConfigStore,
5
+ } from '../shared/token-config-store.mjs';
6
+
7
+ const IDENTITY_OPTIONS = Object.freeze({
8
+ botPrefix: 'imessage',
9
+ tokenRefPrefix: 'DSH_IMESSAGE_PASSWORD',
10
+ });
11
+
12
+ export function deriveIMessageBotIdentity(platformId) {
13
+ return deriveTokenBotIdentity(platformId, IDENTITY_OPTIONS);
14
+ }
15
+
16
+ export function maskIMessageBotId(platformId) {
17
+ return maskPlatformId(platformId, 'iMessage 网关');
18
+ }
19
+
20
+ export class IMessageConfigStore extends TokenBotConfigStore {
21
+ constructor(path) {
22
+ super(path, { channel: 'iMessage', ...IDENTITY_OPTIONS });
23
+ }
24
+ }
@@ -0,0 +1,37 @@
1
+ import { TokenBotController } from '../shared/token-bot-controller.mjs';
2
+ import { MacOSMessagesApi } from './imessage-api.mjs';
3
+ import {
4
+ deriveIMessageBotIdentity,
5
+ maskIMessageBotId,
6
+ } from './config-store.mjs';
7
+ import { IMESSAGE_DESCRIPTOR } from './imessage-bridge.mjs';
8
+
9
+ const NATIVE_CREDENTIAL = 'macos-messages-native';
10
+
11
+ export async function inspectIMessageCredential(value, { api = new MacOSMessagesApi() } = {}) {
12
+ if (value !== NATIVE_CREDENTIAL) throw new TypeError('Invalid native iMessage credential');
13
+ const permissions = await api.getPermissions();
14
+ return { platformId: 'macos-messages', name: 'Mac Messages', permissions };
15
+ }
16
+
17
+ export class IMessageController extends TokenBotController {
18
+ constructor(options) {
19
+ super({
20
+ ...options,
21
+ descriptor: IMESSAGE_DESCRIPTOR,
22
+ inspectToken: options.inspectToken ?? inspectIMessageCredential,
23
+ deriveIdentity: deriveIMessageBotIdentity,
24
+ maskPlatformId: maskIMessageBotId,
25
+ });
26
+ }
27
+
28
+ async bindNative() {
29
+ return super.bindCredentials({ token: NATIVE_CREDENTIAL });
30
+ }
31
+
32
+ async permissions() {
33
+ return new MacOSMessagesApi().getPermissions();
34
+ }
35
+ }
36
+
37
+ export { NATIVE_CREDENTIAL };
@@ -0,0 +1,7 @@
1
+ import { HarnessClient } from '../shared/harness-client.mjs';
2
+
3
+ export class IMessageHarnessClient extends HarnessClient {
4
+ constructor(options) {
5
+ super({ ...options, rpcIdPrefix: 'imessage', logPrefix: 'dsh-imessage' });
6
+ }
7
+ }
@@ -0,0 +1,194 @@
1
+ import { execFile } from 'node:child_process';
2
+ import { promisify } from 'node:util';
3
+
4
+ const execFileAsync = promisify(execFile);
5
+ const DEFAULT_DB_PATH = `${process.env.HOME ?? ''}/Library/Messages/chat.db`;
6
+ const DEFAULT_TIMEOUT_MS = 15_000;
7
+ // Self-chat has no separate bot sender. Keep the reply marker in Messages itself
8
+ // so echoes are still recognizable after a Host restart or iCloud resync.
9
+ export const IMESSAGE_BOT_REPLY_PREFIX = '🤖 DSH\n';
10
+
11
+ function cleanString(value) {
12
+ return typeof value === 'string' && value.trim() ? value.trim() : null;
13
+ }
14
+
15
+ function normalizeChatGuid(value) {
16
+ const chatGuid = cleanString(value?.chatGuid ?? value);
17
+ if (!chatGuid || chatGuid.length > 512 || /[\r\n]/.test(chatGuid)) {
18
+ throw new TypeError('iMessage chatGuid is required');
19
+ }
20
+ return chatGuid;
21
+ }
22
+
23
+ function normalizeAddress(value) {
24
+ const address = cleanString(value);
25
+ if (!address || address.length > 512 || /[\r\n]/.test(address)) {
26
+ throw new TypeError('iMessage address is required');
27
+ }
28
+ return address;
29
+ }
30
+
31
+ function permissionError(kind, message, cause) {
32
+ const error = new Error(message, { cause });
33
+ error.code = kind;
34
+ return error;
35
+ }
36
+
37
+ function sqlString(value) {
38
+ return `'${String(value).replaceAll("'", "''")}'`;
39
+ }
40
+
41
+ function decodeRows(stdout) {
42
+ const text = String(stdout ?? '').trim();
43
+ if (!text) return [];
44
+ try {
45
+ const rows = JSON.parse(text);
46
+ return Array.isArray(rows) ? rows : [];
47
+ } catch (error) {
48
+ throw new Error('macOS Messages returned invalid database output', { cause: error });
49
+ }
50
+ }
51
+
52
+ function appleScriptString(value) {
53
+ return JSON.stringify(String(value));
54
+ }
55
+
56
+ export function normalizeIMessageTarget(value) {
57
+ return normalizeChatGuid(value);
58
+ }
59
+
60
+ export function normalizeIMessage(value, { botId } = {}) {
61
+ if (!value || typeof value !== 'object') return null;
62
+ if (value.serviceName !== undefined && value.serviceName !== 'iMessage') return null;
63
+ const guid = cleanString(value.guid ?? value.id);
64
+ const chatGuid = cleanString(value.chatGuid ?? value.chat_guid);
65
+ const text = cleanString(value.text);
66
+ const sender = cleanString(value.sender ?? value.handle_id);
67
+ if (!guid || !chatGuid || !text || !sender) return null;
68
+ if (text.startsWith(IMESSAGE_BOT_REPLY_PREFIX)) return null;
69
+ if (value.isFromMe === 1 || value.isFromMe === true) return null;
70
+ if (botId && sender === botId) return null;
71
+ return Object.freeze({
72
+ messageId: guid,
73
+ providerMessageId: guid,
74
+ conversationId: chatGuid,
75
+ kind: 'direct',
76
+ senderId: sender,
77
+ senderName: sender,
78
+ content: text,
79
+ addressed: true,
80
+ replyTarget: { chatGuid, address: sender, serviceName: 'iMessage' },
81
+ connectionTestTarget: { chatGuid, address: sender, serviceName: 'iMessage' },
82
+ ...(value.receivedAt ? { receivedAt: value.receivedAt } : {}),
83
+ });
84
+ }
85
+
86
+ export class MacOSMessagesApi {
87
+ #dbPath;
88
+ #execFile;
89
+ #execFileOptions;
90
+ #osascript;
91
+
92
+ constructor({ dbPath = DEFAULT_DB_PATH, execFileImpl = execFileAsync, osascriptImpl } = {}) {
93
+ this.#dbPath = dbPath;
94
+ this.#execFile = execFileImpl;
95
+ this.#execFileOptions = { timeout: DEFAULT_TIMEOUT_MS, maxBuffer: 4 * 1024 * 1024 };
96
+ this.#osascript = osascriptImpl ?? ((script) => this.#execFile('/usr/bin/osascript', ['-e', script], this.#execFileOptions));
97
+ if (typeof this.#execFile !== 'function' || typeof this.#osascript !== 'function') {
98
+ throw new TypeError('MacOSMessagesApi requires command runners');
99
+ }
100
+ }
101
+
102
+ async getPermissions() {
103
+ const result = { platform: process.platform, database: 'unknown', automation: 'unknown' };
104
+ if (process.platform !== 'darwin') {
105
+ return { ...result, database: 'unsupported', automation: 'unsupported' };
106
+ }
107
+ try {
108
+ await this.#execFile('/usr/bin/sqlite3', ['-json', this.#dbPath, 'SELECT 1 AS ok LIMIT 1;'], this.#execFileOptions);
109
+ result.database = 'granted';
110
+ } catch (error) {
111
+ result.database = /authorization denied|not authorized|unable to open database/i.test(String(error?.stderr ?? error))
112
+ ? 'required' : 'error';
113
+ }
114
+ try {
115
+ await this.#osascript('tell application "Messages" to get name');
116
+ result.automation = 'granted';
117
+ } catch (error) {
118
+ result.automation = /not authorized|(-1743)|assistive/i.test(String(error?.stderr ?? error))
119
+ ? 'required' : 'error';
120
+ }
121
+ return result;
122
+ }
123
+
124
+ async listMessages({ after = 0, limit = 50, chatGuid } = {}) {
125
+ const cursor = Number.isSafeInteger(after) && after >= 0 ? after : 0;
126
+ const boundedLimit = Math.max(1, Math.min(100, Number(limit) || 50));
127
+ const chatFilter = chatGuid ? ` AND c.guid = ${sqlString(normalizeChatGuid(chatGuid))}` : '';
128
+ // Messages delivers self-chat back as an incoming copy. Read that copy once,
129
+ // keeping all outgoing rows excluded, and filter bot replies by their marker.
130
+ const query = `SELECT m.ROWID AS rowid, m.guid AS guid, m.text AS text,
131
+ h.id AS sender, c.guid AS chatGuid, c.service_name AS serviceName,
132
+ m.is_from_me AS isFromMe,
133
+ datetime((m.date / 1000000000) + 978307200, 'unixepoch') AS receivedAt
134
+ FROM message m
135
+ JOIN chat_message_join cmj ON cmj.message_id = m.ROWID
136
+ JOIN chat c ON c.ROWID = cmj.chat_id
137
+ LEFT JOIN handle h ON h.ROWID = m.handle_id
138
+ WHERE m.ROWID > ${cursor} AND m.is_from_me = 0
139
+ AND m.text IS NOT NULL AND m.text != ''
140
+ AND c.service_name = 'iMessage'${chatFilter}
141
+ ORDER BY m.ROWID ASC LIMIT ${boundedLimit};`;
142
+ try {
143
+ const { stdout } = await this.#execFile('/usr/bin/sqlite3', ['-json', this.#dbPath, query], this.#execFileOptions);
144
+ return decodeRows(stdout);
145
+ } catch (error) {
146
+ if (/authorization denied|not authorized|unable to open database/i.test(String(error?.stderr ?? error))) {
147
+ throw permissionError('messages-database-permission-required', '请在系统设置中授予 DeepSeek Harness 完全磁盘访问权限。', error);
148
+ }
149
+ throw error;
150
+ }
151
+ }
152
+
153
+ async getLatestMessageRowId() {
154
+ if (process.platform !== 'darwin') return 0;
155
+ const query = `SELECT COALESCE(MAX(m.ROWID), 0) AS rowid
156
+ FROM message m
157
+ JOIN chat_message_join cmj ON cmj.message_id = m.ROWID
158
+ JOIN chat c ON c.ROWID = cmj.chat_id
159
+ WHERE c.service_name = 'iMessage';`;
160
+ const { stdout } = await this.#execFile(
161
+ '/usr/bin/sqlite3', ['-json', this.#dbPath, query], this.#execFileOptions,
162
+ );
163
+ const rows = decodeRows(stdout);
164
+ const rowid = Number(rows[0]?.rowid ?? 0);
165
+ return Number.isSafeInteger(rowid) && rowid >= 0 ? rowid : 0;
166
+ }
167
+
168
+ async sendText({ chatGuid, address, text } = {}) {
169
+ const target = normalizeChatGuid(chatGuid);
170
+ const recipient = address ? normalizeAddress(address) : target.split(';').at(-1) || target;
171
+ const content = cleanString(text);
172
+ if (!content) throw new TypeError('iMessage text is required');
173
+ const reply = content.startsWith(IMESSAGE_BOT_REPLY_PREFIX)
174
+ ? content : `${IMESSAGE_BOT_REPLY_PREFIX}${content}`;
175
+ const script = `tell application "Messages"
176
+ set serviceList to every service whose service type = iMessage
177
+ if (count of serviceList) is 0 then error "No iMessage service is available"
178
+ set targetService to item 1 of serviceList
179
+ set targetBuddy to buddy ${appleScriptString(recipient)} of targetService
180
+ send ${appleScriptString(reply)} to targetBuddy
181
+ end tell`;
182
+ try {
183
+ await this.#osascript(script);
184
+ return { sent: true };
185
+ } catch (error) {
186
+ if (/not authorized|(-1743)|assistive/i.test(String(error?.stderr ?? error))) {
187
+ throw permissionError('messages-automation-permission-required', '请在系统设置中允许 DeepSeek Harness 自动化控制 Messages。', error);
188
+ }
189
+ throw error;
190
+ }
191
+ }
192
+ }
193
+
194
+ export { DEFAULT_DB_PATH };
@@ -0,0 +1,16 @@
1
+ import { TextHarnessBridge, createTextBridgeStatus } from '../shared/text-harness-bridge.mjs';
2
+
3
+ export const IMESSAGE_DESCRIPTOR = Object.freeze({
4
+ key: 'imessage',
5
+ label: 'iMessage',
6
+ connectionLabel: ' macOS Messages 连接',
7
+ reactions: Object.freeze({ processing: '👀', success: '✅', error: '❌' }),
8
+ });
9
+
10
+ export class IMessageHarnessBridge extends TextHarnessBridge {
11
+ constructor(options) {
12
+ super({ ...options, descriptor: IMESSAGE_DESCRIPTOR });
13
+ }
14
+ }
15
+
16
+ export { createTextBridgeStatus as createIMessageBridgeStatus };
@@ -0,0 +1,102 @@
1
+ import { createConnectionDiagnostics, atConnectionStage } from '../shared/connection-error.mjs';
2
+ import { sendRememberedConnectionTest } from '../shared/connection-test.mjs';
3
+ import { MacOSMessagesApi, normalizeIMessage } from './imessage-api.mjs';
4
+ import { createIMessageBridgeStatus, IMessageHarnessBridge } from './imessage-bridge.mjs';
5
+
6
+ const DEFAULT_POLL_INTERVAL_MS = 2_000;
7
+
8
+ class IMessageBotClient {
9
+ #api;
10
+ #signal;
11
+ constructor(api, signal) { this.#api = api; this.#signal = signal; }
12
+ sendText(target, text) {
13
+ return this.#api.sendText({
14
+ chatGuid: target.chatGuid,
15
+ address: target.address,
16
+ text,
17
+ signal: this.#signal,
18
+ });
19
+ }
20
+ sendTyping() { return Promise.resolve(); }
21
+ }
22
+
23
+ export function createIMessageRuntimeStatus() {
24
+ return { startedAt: null, ready: false, connectionState: 'idle', harnessReachable: false,
25
+ lastCheckedAt: null, lastConnectedAt: null, lastError: null, ...createIMessageBridgeStatus() };
26
+ }
27
+
28
+ export class IMessageRuntime {
29
+ #config; #token; #harness; #state; #contextEnhancement; #accessPolicy; #logger;
30
+ #diagnostics;
31
+ #replyTimeoutMs; #pollIntervalMs; #createApi; #status = createIMessageRuntimeStatus();
32
+ #api; #bridge; #abortController; #timer; #polling; #stopped = true;
33
+ constructor({ config, token, harness, state, contextEnhancement, accessPolicy, logger = console,
34
+ replyTimeoutMs = 600_000, pollIntervalMs = DEFAULT_POLL_INTERVAL_MS,
35
+ createApi = (options) => new MacOSMessagesApi(options) }) {
36
+ if (!config || !token || !harness || !state) throw new TypeError('IMessageRuntime requires config, token, Harness, and state');
37
+ this.#config = config; this.#token = token; this.#harness = harness; this.#state = state;
38
+ this.#contextEnhancement = contextEnhancement; this.#accessPolicy = accessPolicy; this.#logger = logger; this.#diagnostics = createConnectionDiagnostics({ channel: 'imessage', logger });
39
+ this.#replyTimeoutMs = replyTimeoutMs; this.#pollIntervalMs = pollIntervalMs; this.#createApi = createApi;
40
+ }
41
+ get status() { return structuredClone(this.#status); }
42
+ async sendConnectionTest(text) {
43
+ if (!this.#status.ready || !this.#api) { const error = new Error('iMessage gateway is not connected'); error.code = 'test-target-unavailable'; throw error; }
44
+ await sendRememberedConnectionTest({
45
+ state: this.#state,
46
+ text,
47
+ channelLabel: 'iMessage',
48
+ send: (target, value) => this.#api.sendText({
49
+ chatGuid: target.chatGuid, address: target.address, text: value,
50
+ }),
51
+ });
52
+ }
53
+ async sendProactiveText(target, text, options = {}) {
54
+ if (!this.#status.ready || !this.#api) { const error = new Error('iMessage gateway is not connected'); error.code = 'bot-not-connected'; throw error; }
55
+ const chatGuid = target?.route?.chatGuid;
56
+ if (!chatGuid) { const error = new TypeError('Invalid iMessage proactive delivery target'); error.code = 'invalid-target'; throw error; }
57
+ await this.#api.sendText({ chatGuid, text, signal: options.signal });
58
+ return { sent: true };
59
+ }
60
+ async start() {
61
+ if (this.#status.ready) return this.status;
62
+ await this.stop(); this.#stopped = false; this.#status.startedAt = new Date().toISOString(); this.#status.connectionState = 'connecting';
63
+ this.#abortController = new AbortController();
64
+ try {
65
+ await atConnectionStage('harness.check', () => this.#harness.ensureRunning()); this.#status.harnessReachable = true;
66
+ this.#api = this.#createApi({ signal: this.#abortController.signal });
67
+ const permissions = await this.#api.getPermissions();
68
+ if (permissions.database !== 'granted' || permissions.automation !== 'granted') {
69
+ const error = new Error('macOS Messages permissions are required');
70
+ error.code = 'messages-permission-required';
71
+ error.permissions = permissions;
72
+ throw error;
73
+ }
74
+ if (this.#state.cursor() === null && typeof this.#api.getLatestMessageRowId === 'function') {
75
+ await this.#state.setCursor(await this.#api.getLatestMessageRowId());
76
+ }
77
+ const client = new IMessageBotClient(this.#api, this.#abortController.signal);
78
+ this.#bridge = new IMessageHarnessBridge({ bot: client, harness: this.#harness, state: this.#state,
79
+ contextEnhancement: this.#contextEnhancement, accessPolicy: this.#accessPolicy, status: this.#status,
80
+ logger: this.#logger, replyTimeoutMs: this.#replyTimeoutMs, signal: this.#abortController.signal });
81
+ this.#status.ready = true; this.#status.connectionState = 'connected'; this.#status.lastConnectedAt = Date.now();
82
+ this.#schedulePoll(0); return this.status;
83
+ } catch (error) { this.#status.ready = false; this.#status.connectionState = 'failed'; this.#status.error = this.#diagnostics.report(error, { operation: 'connection.restore', reuse: true, botId: this.#config?.botId, automatic: true }).publicError;
84
+ this.#status.lastError = this.#status.error.message; await this.stop(); throw error; }
85
+ }
86
+ async stop() { this.#stopped = true; clearTimeout(this.#timer); this.#timer = null; this.#abortController?.abort(); await this.#polling?.catch(() => {}); this.#polling = null; this.#bridge = null; this.#api = null; this.#status.ready = false; }
87
+ #schedulePoll(delay) { if (this.#stopped) return; this.#timer = setTimeout(() => { this.#polling = this.#poll().finally(() => this.#schedulePoll(this.#pollIntervalMs)); }, delay); this.#timer.unref?.(); }
88
+ async #poll() {
89
+ if (!this.#api || !this.#bridge || this.#stopped) return;
90
+ try {
91
+ const rows = await this.#api.listMessages({ after: this.#state.cursor() ?? 0, limit: 100 });
92
+ const messages = Array.isArray(rows) ? rows : rows?.messages ?? rows?.data ?? [];
93
+ for (const raw of messages) {
94
+ const message = normalizeIMessage(raw, { botId: this.#config.platformId });
95
+ if (message) await this.#bridge.accept(message);
96
+ if (Number.isSafeInteger(raw.rowid)) await this.#state.setCursor(raw.rowid);
97
+ }
98
+ this.#status.lastCheckedAt = Date.now(); this.#status.lastError = null; this.#status.error = null; this.#diagnostics.clear();
99
+ } catch (error) { if (!this.#stopped) { this.#status.error = this.#diagnostics.report(error, { operation: 'connection.monitor', botId: this.#config?.botId, automatic: true }).publicError;
100
+ this.#status.lastError = this.#status.error.message; } }
101
+ }
102
+ }
@@ -0,0 +1,3 @@
1
+ import { ConversationStateStore } from '../shared/conversation-state-store.mjs';
2
+
3
+ export class IMessageStateStore extends ConversationStateStore {}