@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.
- package/LICENSE +21 -0
- package/PROVENANCE.json +25 -0
- package/README.en.md +445 -0
- package/README.md +448 -0
- package/THIRD_PARTY_NOTICES.md +93 -0
- package/assets/dingtalk-menu-template.json +6 -0
- package/assets/logo-dsh-im-chinese-readme-3x2.png +0 -0
- package/assets/logo-dsh-im-connecting-readme-3x2.png +0 -0
- package/assets/logo-dsh-im-connecting-square-v4.png +0 -0
- package/assets/logo-icon.png +0 -0
- package/assets/logo-plugin-message-link-gradient.webp +0 -0
- package/assets/logo-plugin-phone.png +0 -0
- package/assets/logo.png +0 -0
- package/assets/logo_cn.png +0 -0
- package/assets/screenshot-menu-card.png +0 -0
- package/cordis.patch.yml +3 -0
- package/lib/client.js +20330 -0
- package/lib/index.js +602 -0
- package/locale/en.json +5 -0
- package/locale/zh.json +5 -0
- package/package.json +181 -0
- package/plugin-src/client/access-policy-settings.js +362 -0
- package/plugin-src/client/agent-preset.js +119 -0
- package/plugin-src/client/bot-alias.js +169 -0
- package/plugin-src/client/build.mjs +38 -0
- package/plugin-src/client/channel-card-meta.js +132 -0
- package/plugin-src/client/channel-logos.js +190 -0
- package/plugin-src/client/channels/dingtalk/api.js +267 -0
- package/plugin-src/client/channels/dingtalk/index.js +954 -0
- package/plugin-src/client/channels/dingtalk/styles.js +136 -0
- package/plugin-src/client/channels/discord/api.js +11 -0
- package/plugin-src/client/channels/discord/index.js +25 -0
- package/plugin-src/client/channels/discord/styles.js +19 -0
- package/plugin-src/client/channels/email/api.js +52 -0
- package/plugin-src/client/channels/email/index.js +728 -0
- package/plugin-src/client/channels/email/styles.js +51 -0
- package/plugin-src/client/channels/feishu/api.js +405 -0
- package/plugin-src/client/channels/feishu/group-settings.js +581 -0
- package/plugin-src/client/channels/feishu/index.js +1583 -0
- package/plugin-src/client/channels/feishu/slash-panel-settings.js +322 -0
- package/plugin-src/client/channels/feishu/styles.js +573 -0
- package/plugin-src/client/channels/feishu/voice-editor.js +115 -0
- package/plugin-src/client/channels/feishu/voice-settings.js +71 -0
- package/plugin-src/client/channels/imessage/api.js +12 -0
- package/plugin-src/client/channels/imessage/index.js +46 -0
- package/plugin-src/client/channels/imessage/styles.js +19 -0
- package/plugin-src/client/channels/matrix/api.js +11 -0
- package/plugin-src/client/channels/matrix/index.js +151 -0
- package/plugin-src/client/channels/matrix/styles.js +36 -0
- package/plugin-src/client/channels/office/api.js +46 -0
- package/plugin-src/client/channels/office/index.js +153 -0
- package/plugin-src/client/channels/office/styles.js +50 -0
- package/plugin-src/client/channels/qq/api.js +171 -0
- package/plugin-src/client/channels/qq/index.js +593 -0
- package/plugin-src/client/channels/qq/styles.js +19 -0
- package/plugin-src/client/channels/shared/collapsible-account.js +103 -0
- package/plugin-src/client/channels/shared/token-api.js +124 -0
- package/plugin-src/client/channels/shared/token-channel.js +495 -0
- package/plugin-src/client/channels/slack/api.js +11 -0
- package/plugin-src/client/channels/slack/index.js +131 -0
- package/plugin-src/client/channels/slack/styles.js +34 -0
- package/plugin-src/client/channels/telegram/api.js +11 -0
- package/plugin-src/client/channels/telegram/index.js +28 -0
- package/plugin-src/client/channels/telegram/styles.js +31 -0
- package/plugin-src/client/channels/telegram/thinking-traces.js +25 -0
- package/plugin-src/client/channels/wecom/api.js +160 -0
- package/plugin-src/client/channels/wecom/index.js +625 -0
- package/plugin-src/client/channels/wecom/styles.js +19 -0
- package/plugin-src/client/channels/wecom-app/api.js +132 -0
- package/plugin-src/client/channels/wecom-app/index.js +634 -0
- package/plugin-src/client/channels/wecom-app/styles.js +25 -0
- package/plugin-src/client/channels/weixin/api.js +211 -0
- package/plugin-src/client/channels/weixin/connection-error.js +1 -0
- package/plugin-src/client/channels/weixin/index.js +801 -0
- package/plugin-src/client/channels/weixin/styles.js +115 -0
- package/plugin-src/client/channels/whatsapp/api.js +152 -0
- package/plugin-src/client/channels/whatsapp/index.js +609 -0
- package/plugin-src/client/channels/whatsapp/styles.js +19 -0
- package/plugin-src/client/connection-error.js +87 -0
- package/plugin-src/client/context-enhancement.js +415 -0
- package/plugin-src/client/credential-binding.js +118 -0
- package/plugin-src/client/delivery-settings.js +905 -0
- package/plugin-src/client/global-settings.js +433 -0
- package/plugin-src/client/i18n.js +1314 -0
- package/plugin-src/client/index.js +624 -0
- package/plugin-src/client/interface-language.js +89 -0
- package/plugin-src/client/last-message-error.js +18 -0
- package/plugin-src/client/lifecycle.js +86 -0
- package/plugin-src/client/loopback-recovery.js +75 -0
- package/plugin-src/client/model-setting.js +172 -0
- package/plugin-src/client/panel-error-boundary.js +21 -0
- package/plugin-src/client/plugin-page.js +51 -0
- package/plugin-src/client/session-channel-logos.js +243 -0
- package/plugin-src/client/styles.js +770 -0
- package/plugin-src/client/update-panel.js +14 -0
- package/plugin-src/client/workspace-directory-picker.js +280 -0
- package/plugin-src/client/workspace-editor.js +69 -0
- package/plugin-src/client/workspace-snapshot-fence.js +28 -0
- package/plugin-src/host/build.mjs +52 -0
- package/plugin-src/host/channels/dingtalk/connection-supervisor.mjs +130 -0
- package/plugin-src/host/channels/dingtalk/index.mjs +35 -0
- package/plugin-src/host/channels/dingtalk/production.mjs +193 -0
- package/plugin-src/host/channels/dingtalk/rpc.mjs +359 -0
- package/plugin-src/host/channels/discord/index.mjs +33 -0
- package/plugin-src/host/channels/discord/production.mjs +17 -0
- package/plugin-src/host/channels/discord/rpc.mjs +21 -0
- package/plugin-src/host/channels/email/availability.mjs +60 -0
- package/plugin-src/host/channels/email/index.mjs +36 -0
- package/plugin-src/host/channels/email/production.mjs +58 -0
- package/plugin-src/host/channels/email/rpc.mjs +163 -0
- package/plugin-src/host/channels/feishu/connection-supervisor.mjs +167 -0
- package/plugin-src/host/channels/feishu/controller.mjs +172 -0
- package/plugin-src/host/channels/feishu/credential-store.mjs +84 -0
- package/plugin-src/host/channels/feishu/index.mjs +69 -0
- package/plugin-src/host/channels/feishu/production.mjs +318 -0
- package/plugin-src/host/channels/feishu/rpc.mjs +863 -0
- package/plugin-src/host/channels/imessage/index.mjs +21 -0
- package/plugin-src/host/channels/imessage/production.mjs +13 -0
- package/plugin-src/host/channels/imessage/rpc.mjs +78 -0
- package/plugin-src/host/channels/matrix/index.mjs +31 -0
- package/plugin-src/host/channels/matrix/production.mjs +241 -0
- package/plugin-src/host/channels/matrix/rpc.mjs +228 -0
- package/plugin-src/host/channels/office/index.mjs +14 -0
- package/plugin-src/host/channels/office/production.mjs +45 -0
- package/plugin-src/host/channels/office/rpc.mjs +51 -0
- package/plugin-src/host/channels/qq/connection-supervisor.mjs +124 -0
- package/plugin-src/host/channels/qq/index.mjs +28 -0
- package/plugin-src/host/channels/qq/production.mjs +181 -0
- package/plugin-src/host/channels/qq/rpc.mjs +250 -0
- package/plugin-src/host/channels/shared/access-policy-production.mjs +137 -0
- package/plugin-src/host/channels/shared/access-policy-rpc.mjs +17 -0
- package/plugin-src/host/channels/shared/agent-preset-rpc.mjs +25 -0
- package/plugin-src/host/channels/shared/bot-alias-rpc.mjs +15 -0
- package/plugin-src/host/channels/shared/connection-supervisor.mjs +133 -0
- package/plugin-src/host/channels/shared/context-enhancement-rpc.mjs +17 -0
- package/plugin-src/host/channels/shared/model-setting-rpc.mjs +18 -0
- package/plugin-src/host/channels/shared/production.mjs +237 -0
- package/plugin-src/host/channels/shared/rpc.mjs +226 -0
- package/plugin-src/host/channels/shared/startup-error.mjs +55 -0
- package/plugin-src/host/channels/shared/startup.mjs +76 -0
- package/plugin-src/host/channels/shared/thinking-traces-rpc.mjs +11 -0
- package/plugin-src/host/channels/shared/workspace-rpc.mjs +31 -0
- package/plugin-src/host/channels/slack/index.mjs +31 -0
- package/plugin-src/host/channels/slack/production.mjs +176 -0
- package/plugin-src/host/channels/slack/rpc.mjs +221 -0
- package/plugin-src/host/channels/telegram/index.mjs +33 -0
- package/plugin-src/host/channels/telegram/production.mjs +19 -0
- package/plugin-src/host/channels/telegram/rpc.mjs +22 -0
- package/plugin-src/host/channels/wecom/connection-supervisor.mjs +124 -0
- package/plugin-src/host/channels/wecom/index.mjs +34 -0
- package/plugin-src/host/channels/wecom/production.mjs +187 -0
- package/plugin-src/host/channels/wecom/rpc.mjs +255 -0
- package/plugin-src/host/channels/wecom-app/index.mjs +33 -0
- package/plugin-src/host/channels/wecom-app/production.mjs +221 -0
- package/plugin-src/host/channels/wecom-app/rpc.mjs +235 -0
- package/plugin-src/host/channels/weixin/connection-supervisor.mjs +139 -0
- package/plugin-src/host/channels/weixin/index.mjs +44 -0
- package/plugin-src/host/channels/weixin/production.mjs +257 -0
- package/plugin-src/host/channels/weixin/rpc.mjs +285 -0
- package/plugin-src/host/channels/whatsapp/index.mjs +33 -0
- package/plugin-src/host/channels/whatsapp/production.mjs +198 -0
- package/plugin-src/host/channels/whatsapp/rpc.mjs +242 -0
- package/plugin-src/host/delivery-adapter.mjs +388 -0
- package/plugin-src/host/delivery-http.mjs +132 -0
- package/plugin-src/host/delivery-rpc.mjs +173 -0
- package/plugin-src/host/delivery-service.mjs +552 -0
- package/plugin-src/host/delivery-suggestions.mjs +198 -0
- package/plugin-src/host/harness-command-executor.mjs +48 -0
- package/plugin-src/host/harness-connection.mjs +20 -0
- package/plugin-src/host/harness-session-coordinator.mjs +183 -0
- package/plugin-src/host/host-language-rpc.mjs +71 -0
- package/plugin-src/host/host-language.mjs +157 -0
- package/plugin-src/host/image-input-rpc.mjs +24 -0
- package/plugin-src/host/inbound-ttl-rpc.mjs +86 -0
- package/plugin-src/host/inbound-ttl-runtime.mjs +85 -0
- package/plugin-src/host/inbound-ttl-service.mjs +179 -0
- package/plugin-src/host/index.mjs +224 -0
- package/plugin-src/host/injected-context.mjs +104 -0
- package/plugin-src/host/lark-sdk-handshake-patch.mjs +181 -0
- package/plugin-src/host/modern-harness-api.mjs +738 -0
- package/plugin-src/host/rpc-authority.mjs +12 -0
- package/plugin-src/host/session-sync-coordinator.mjs +233 -0
- package/plugin-src/host/session-title-prefix.mjs +122 -0
- package/plugin-src/host/update-policy.mjs +31 -0
- package/plugin-src/host/update-rpc.mjs +16 -0
- package/plugin-src/host/update-runtime.mjs +339 -0
- package/plugin-src/host/update-service.mjs +392 -0
- package/plugin-src/management-rpc.mjs +87 -0
- package/src/channels/dingtalk/config-store.mjs +284 -0
- package/src/channels/dingtalk/connection-error.mjs +292 -0
- package/src/channels/dingtalk/device-auth.mjs +240 -0
- package/src/channels/dingtalk/dingtalk-api.mjs +1140 -0
- package/src/channels/dingtalk/dingtalk-bridge.mjs +1889 -0
- package/src/channels/dingtalk/dingtalk-card-stream.mjs +242 -0
- package/src/channels/dingtalk/dingtalk-controller.mjs +852 -0
- package/src/channels/dingtalk/dingtalk-menu.mjs +95 -0
- package/src/channels/dingtalk/dingtalk-runtime.mjs +482 -0
- package/src/channels/dingtalk/harness-client.mjs +19 -0
- package/src/channels/dingtalk/state-store.mjs +325 -0
- package/src/channels/discord/config-store.mjs +25 -0
- package/src/channels/discord/discord-api.mjs +347 -0
- package/src/channels/discord/discord-bridge.mjs +16 -0
- package/src/channels/discord/discord-controller.mjs +16 -0
- package/src/channels/discord/discord-runtime.mjs +974 -0
- package/src/channels/discord/harness-client.mjs +11 -0
- package/src/channels/discord/state-store.mjs +3 -0
- package/src/channels/email/config-store.mjs +194 -0
- package/src/channels/email/email-api.mjs +20 -0
- package/src/channels/email/email-bridge.mjs +17 -0
- package/src/channels/email/email-controller.mjs +824 -0
- package/src/channels/email/email-runtime.mjs +699 -0
- package/src/channels/email/harness-client.mjs +7 -0
- package/src/channels/email/mail-format.mjs +91 -0
- package/src/channels/email/state-store.mjs +176 -0
- package/src/channels/email/transport.mjs +64 -0
- package/src/channels/email/transports/agent-mail.mjs +604 -0
- package/src/channels/email/transports/agently-cli.mjs +288 -0
- package/src/channels/email/transports/imap-smtp.mjs +165 -0
- package/src/channels/feishu/bridge.mjs +6286 -0
- package/src/channels/feishu/config.mjs +71 -0
- package/src/channels/feishu/external-consumer.mjs +77 -0
- package/src/channels/feishu/external-files.mjs +111 -0
- package/src/channels/feishu/feishu-app.mjs +54 -0
- package/src/channels/feishu/feishu-cards.mjs +1285 -0
- package/src/channels/feishu/feishu-channel.mjs +651 -0
- package/src/channels/feishu/feishu-runtime.mjs +950 -0
- package/src/channels/feishu/group-message-permission-manager.mjs +72 -0
- package/src/channels/feishu/group-response-mode.mjs +15 -0
- package/src/channels/feishu/harness-client.mjs +19 -0
- package/src/channels/feishu/history-reader.mjs +77 -0
- package/src/channels/feishu/live-cot.mjs +260 -0
- package/src/channels/feishu/message-utils.mjs +613 -0
- package/src/channels/feishu/multi-bot-controller.mjs +1588 -0
- package/src/channels/feishu/nearby-history.mjs +72 -0
- package/src/channels/feishu/plugin-config-store.mjs +216 -0
- package/src/channels/feishu/plugin-controller.mjs +254 -0
- package/src/channels/feishu/registration-manager.mjs +349 -0
- package/src/channels/feishu/repair-manager.mjs +138 -0
- package/src/channels/feishu/reply-context.mjs +22 -0
- package/src/channels/feishu/slash-command-panel.mjs +155 -0
- package/src/channels/feishu/slash-command-registry.mjs +495 -0
- package/src/channels/feishu/state-store.mjs +214 -0
- package/src/channels/feishu/step-push-mode.mjs +25 -0
- package/src/channels/feishu/voice-config.mjs +34 -0
- package/src/channels/feishu/voice.mjs +134 -0
- package/src/channels/imessage/config-store.mjs +24 -0
- package/src/channels/imessage/controller.mjs +37 -0
- package/src/channels/imessage/harness-client.mjs +7 -0
- package/src/channels/imessage/imessage-api.mjs +194 -0
- package/src/channels/imessage/imessage-bridge.mjs +16 -0
- package/src/channels/imessage/runtime.mjs +102 -0
- package/src/channels/imessage/state-store.mjs +3 -0
- package/src/channels/matrix/matrix-api.mjs +696 -0
- package/src/channels/matrix/matrix-bridge.mjs +20 -0
- package/src/channels/matrix/matrix-config-store.mjs +368 -0
- package/src/channels/matrix/matrix-controller.mjs +404 -0
- package/src/channels/matrix/matrix-crypto-store.mjs +279 -0
- package/src/channels/matrix/matrix-crypto.mjs +1014 -0
- package/src/channels/matrix/matrix-harness-client.mjs +11 -0
- package/src/channels/matrix/matrix-normalize.mjs +357 -0
- package/src/channels/matrix/matrix-rich-text.mjs +313 -0
- package/src/channels/matrix/matrix-room-history.mjs +316 -0
- package/src/channels/matrix/matrix-runtime.mjs +1014 -0
- package/src/channels/office/config-store.mjs +107 -0
- package/src/channels/office/office-controller.mjs +178 -0
- package/src/channels/office/office-job-executor.mjs +391 -0
- package/src/channels/office/office-runtime.mjs +175 -0
- package/src/channels/office/office-transport.mjs +171 -0
- package/src/channels/office/protocol.mjs +43 -0
- package/src/channels/qq/config-store.mjs +173 -0
- package/src/channels/qq/harness-client.mjs +11 -0
- package/src/channels/qq/markdown-reply.mjs +446 -0
- package/src/channels/qq/qq-bridge.mjs +1507 -0
- package/src/channels/qq/qq-controller.mjs +514 -0
- package/src/channels/qq/qq-menu.mjs +208 -0
- package/src/channels/qq/qq-runtime.mjs +303 -0
- package/src/channels/qq/qr-auth.mjs +24 -0
- package/src/channels/qq/state-error.mjs +17 -0
- package/src/channels/qq/state-store.mjs +143 -0
- package/src/channels/shared/access-policy.mjs +210 -0
- package/src/channels/shared/agent-preset.mjs +76 -0
- package/src/channels/shared/batch-input.mjs +229 -0
- package/src/channels/shared/bot-alias.mjs +29 -0
- package/src/channels/shared/bot-workspace-store.mjs +2383 -0
- package/src/channels/shared/command-catalog.mjs +107 -0
- package/src/channels/shared/command-permission.mjs +32 -0
- package/src/channels/shared/compact-command.mjs +109 -0
- package/src/channels/shared/config-read-error.mjs +24 -0
- package/src/channels/shared/connection-error.mjs +191 -0
- package/src/channels/shared/connection-test.mjs +58 -0
- package/src/channels/shared/context-enhancement.mjs +372 -0
- package/src/channels/shared/control-command.mjs +111 -0
- package/src/channels/shared/conversation-state-store.mjs +165 -0
- package/src/channels/shared/default-workspace.mjs +31 -0
- package/src/channels/shared/deferred-delivery-coordinator.mjs +241 -0
- package/src/channels/shared/deferred-delivery.mjs +161 -0
- package/src/channels/shared/deferred-state.mjs +53 -0
- package/src/channels/shared/diagnostic-details.mjs +95 -0
- package/src/channels/shared/editable-message-stream.mjs +100 -0
- package/src/channels/shared/exclusive-inbound-consumers.mjs +71 -0
- package/src/channels/shared/file-download.mjs +64 -0
- package/src/channels/shared/harness-approval.mjs +508 -0
- package/src/channels/shared/harness-client.mjs +2143 -0
- package/src/channels/shared/harness-mux.mjs +123 -0
- package/src/channels/shared/harness-question.mjs +95 -0
- package/src/channels/shared/harness-session-binding.mjs +119 -0
- package/src/channels/shared/history-command.mjs +193 -0
- package/src/channels/shared/i18n-en/diagnostics.mjs +55 -0
- package/src/channels/shared/i18n-en/dingtalk.mjs +81 -0
- package/src/channels/shared/i18n-en/discord.mjs +10 -0
- package/src/channels/shared/i18n-en/email.mjs +25 -0
- package/src/channels/shared/i18n-en/feishu.mjs +404 -0
- package/src/channels/shared/i18n-en/image-input.mjs +18 -0
- package/src/channels/shared/i18n-en/matrix.mjs +85 -0
- package/src/channels/shared/i18n-en/office.mjs +23 -0
- package/src/channels/shared/i18n-en/qq.mjs +96 -0
- package/src/channels/shared/i18n-en/shared-a.mjs +241 -0
- package/src/channels/shared/i18n-en/shared-b.mjs +263 -0
- package/src/channels/shared/i18n-en/shared-c.mjs +203 -0
- package/src/channels/shared/i18n-en/slack.mjs +27 -0
- package/src/channels/shared/i18n-en/telegram.mjs +27 -0
- package/src/channels/shared/i18n-en/wecom-app.mjs +33 -0
- package/src/channels/shared/i18n-en/wecom.mjs +62 -0
- package/src/channels/shared/i18n-en/weixin.mjs +88 -0
- package/src/channels/shared/i18n-en/whatsapp.mjs +17 -0
- package/src/channels/shared/i18n-en.mjs +45 -0
- package/src/channels/shared/i18n.mjs +78 -0
- package/src/channels/shared/im-source-guidance.mjs +65 -0
- package/src/channels/shared/image-input-policy.mjs +35 -0
- package/src/channels/shared/image-input-settings-store.mjs +60 -0
- package/src/channels/shared/image-input.mjs +124 -0
- package/src/channels/shared/image-prompt.mjs +360 -0
- package/src/channels/shared/inbound-access.mjs +43 -0
- package/src/channels/shared/inbound-file.mjs +323 -0
- package/src/channels/shared/inbound-ttl-store.mjs +110 -0
- package/src/channels/shared/inbound-ttl.mjs +22 -0
- package/src/channels/shared/injected-context.mjs +362 -0
- package/src/channels/shared/interface-language-store.mjs +127 -0
- package/src/channels/shared/interface-language.mjs +51 -0
- package/src/channels/shared/message-failure.mjs +257 -0
- package/src/channels/shared/model-command.mjs +741 -0
- package/src/channels/shared/model-setting.mjs +145 -0
- package/src/channels/shared/preset-command.mjs +308 -0
- package/src/channels/shared/semantic/artifact-delivery.mjs +177 -0
- package/src/channels/shared/semantic/artifact.mjs +750 -0
- package/src/channels/shared/semantic/delivery.mjs +193 -0
- package/src/channels/shared/semantic/reply-reference.mjs +148 -0
- package/src/channels/shared/session-binding-lock.mjs +40 -0
- package/src/channels/shared/session-channel-labels.mjs +28 -0
- package/src/channels/shared/session-reply-recovery.mjs +104 -0
- package/src/channels/shared/session-sync-registry.mjs +22 -0
- package/src/channels/shared/session-title.mjs +74 -0
- package/src/channels/shared/status-reaction.mjs +107 -0
- package/src/channels/shared/text-harness-bridge.mjs +1623 -0
- package/src/channels/shared/token-bot-controller.mjs +417 -0
- package/src/channels/shared/token-config-store.mjs +208 -0
- package/src/channels/shared/workspace-command.mjs +532 -0
- package/src/channels/shared/workspace-session.mjs +196 -0
- package/src/channels/slack/config-store.mjs +177 -0
- package/src/channels/slack/external-consumer.mjs +80 -0
- package/src/channels/slack/external-files.mjs +90 -0
- package/src/channels/slack/harness-client.mjs +11 -0
- package/src/channels/slack/history-reader.mjs +203 -0
- package/src/channels/slack/manifest.mjs +37 -0
- package/src/channels/slack/slack-api.mjs +636 -0
- package/src/channels/slack/slack-bridge.mjs +20 -0
- package/src/channels/slack/slack-controller.mjs +468 -0
- package/src/channels/slack/slack-runtime.mjs +879 -0
- package/src/channels/slack/state-store.mjs +3 -0
- package/src/channels/telegram/config-store.mjs +89 -0
- package/src/channels/telegram/harness-client.mjs +11 -0
- package/src/channels/telegram/state-store.mjs +3 -0
- package/src/channels/telegram/telegram-api.mjs +486 -0
- package/src/channels/telegram/telegram-bridge.mjs +81 -0
- package/src/channels/telegram/telegram-controller.mjs +52 -0
- package/src/channels/telegram/telegram-http.mjs +21 -0
- package/src/channels/telegram/telegram-rich-message.mjs +149 -0
- package/src/channels/telegram/telegram-runtime.mjs +1471 -0
- package/src/channels/wecom/config-store.mjs +172 -0
- package/src/channels/wecom/harness-client.mjs +11 -0
- package/src/channels/wecom/qr-auth.mjs +95 -0
- package/src/channels/wecom/send-error.mjs +31 -0
- package/src/channels/wecom/state-store.mjs +107 -0
- package/src/channels/wecom/wecom-bridge.mjs +1833 -0
- package/src/channels/wecom/wecom-cards.mjs +101 -0
- package/src/channels/wecom/wecom-controller.mjs +530 -0
- package/src/channels/wecom/wecom-runtime.mjs +269 -0
- package/src/channels/wecom-app/callback-server.mjs +471 -0
- package/src/channels/wecom-app/config-store.mjs +242 -0
- package/src/channels/wecom-app/harness-client.mjs +11 -0
- package/src/channels/wecom-app/state-store.mjs +107 -0
- package/src/channels/wecom-app/wecom-app-api.mjs +432 -0
- package/src/channels/wecom-app/wecom-app-bridge.mjs +1072 -0
- package/src/channels/wecom-app/wecom-app-controller.mjs +453 -0
- package/src/channels/wecom-app/wecom-app-runtime.mjs +222 -0
- package/src/channels/weixin/config-store.mjs +192 -0
- package/src/channels/weixin/connection-error.en.mjs +137 -0
- package/src/channels/weixin/connection-error.mjs +153 -0
- package/src/channels/weixin/diagnostic-details.mjs +1 -0
- package/src/channels/weixin/harness-client.mjs +19 -0
- package/src/channels/weixin/state-store.mjs +291 -0
- package/src/channels/weixin/weixin-api.mjs +949 -0
- package/src/channels/weixin/weixin-bridge.mjs +1537 -0
- package/src/channels/weixin/weixin-controller.mjs +669 -0
- package/src/channels/weixin/weixin-runtime.mjs +416 -0
- package/src/channels/whatsapp/config-store.mjs +210 -0
- package/src/channels/whatsapp/harness-client.mjs +11 -0
- package/src/channels/whatsapp/state-store.mjs +3 -0
- package/src/channels/whatsapp/whatsapp-bridge.mjs +18 -0
- package/src/channels/whatsapp/whatsapp-controller.mjs +470 -0
- package/src/channels/whatsapp/whatsapp-runtime.mjs +917 -0
- package/src/channels/whatsapp/whatsapp-web-session.mjs +214 -0
|
@@ -0,0 +1,552 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { normalizeDeliveryTarget } from './delivery-adapter.mjs';
|
|
3
|
+
|
|
4
|
+
const BOT_ID_PATTERN = /^[A-Za-z0-9_-]{1,128}$/;
|
|
5
|
+
const TARGET_ID_PATTERN = /^[A-Za-z0-9._:@-]{1,128}$/;
|
|
6
|
+
const CHANNEL_PATTERN = /^[a-z][a-z0-9-]{0,31}$/;
|
|
7
|
+
const DRAFT_TARGET_ID = '__test__';
|
|
8
|
+
|
|
9
|
+
const ADAPTER_METHODS = Object.freeze([
|
|
10
|
+
'ownsBot',
|
|
11
|
+
'listBots',
|
|
12
|
+
'listTargets',
|
|
13
|
+
'listSuggestions',
|
|
14
|
+
'createTarget',
|
|
15
|
+
'updateTarget',
|
|
16
|
+
'deleteTarget',
|
|
17
|
+
'sendText',
|
|
18
|
+
]);
|
|
19
|
+
|
|
20
|
+
const DELIVERY_ERROR_CODES = new Set([
|
|
21
|
+
'bad-request',
|
|
22
|
+
'account-unverified',
|
|
23
|
+
'account-changed',
|
|
24
|
+
'target-changed',
|
|
25
|
+
'capability-unavailable',
|
|
26
|
+
'unknown-bot',
|
|
27
|
+
'unknown-target',
|
|
28
|
+
'target-conflict',
|
|
29
|
+
'invalid-target',
|
|
30
|
+
'bot-not-connected',
|
|
31
|
+
'target-rejected',
|
|
32
|
+
'delivery-failed',
|
|
33
|
+
'session-sync-unavailable',
|
|
34
|
+
'cancelled',
|
|
35
|
+
'provider-unavailable',
|
|
36
|
+
'consumer-unavailable',
|
|
37
|
+
'consumer-conflict',
|
|
38
|
+
'ingress-not-accepted',
|
|
39
|
+
'invalid-inbound',
|
|
40
|
+
'stale-route',
|
|
41
|
+
'source-not-found',
|
|
42
|
+
'source-unavailable',
|
|
43
|
+
'reply-permission-denied',
|
|
44
|
+
'history-permission-denied',
|
|
45
|
+
'history-unavailable',
|
|
46
|
+
'thread-unavailable',
|
|
47
|
+
'untrusted-source',
|
|
48
|
+
'reply-result-unknown',
|
|
49
|
+
'send-result-unknown',
|
|
50
|
+
'file-upload-failed',
|
|
51
|
+
'file-provider-rejected',
|
|
52
|
+
'resource-unavailable',
|
|
53
|
+
'artifact-too-large',
|
|
54
|
+
]);
|
|
55
|
+
|
|
56
|
+
const SESSION_SYNC_METHODS = Object.freeze([
|
|
57
|
+
'setSessionSync',
|
|
58
|
+
'listSessionSyncTargets',
|
|
59
|
+
'sendSessionSyncText',
|
|
60
|
+
]);
|
|
61
|
+
|
|
62
|
+
function deliveryError(code, message = code, options) {
|
|
63
|
+
const error = new Error(message, options);
|
|
64
|
+
error.code = code;
|
|
65
|
+
return error;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function botIdOf(value) {
|
|
69
|
+
if (typeof value !== 'string' || !BOT_ID_PATTERN.test(value)) {
|
|
70
|
+
throw deliveryError('bad-request', 'Invalid bot id');
|
|
71
|
+
}
|
|
72
|
+
return value;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function targetIdOf(value) {
|
|
76
|
+
if (typeof value !== 'string' || !TARGET_ID_PATTERN.test(value)) {
|
|
77
|
+
throw deliveryError('bad-request', 'Invalid target id');
|
|
78
|
+
}
|
|
79
|
+
return value;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function targetObject(value, { includesTargetId } = {}) {
|
|
83
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
84
|
+
throw deliveryError('bad-request', 'Invalid target');
|
|
85
|
+
}
|
|
86
|
+
if (includesTargetId) targetIdOf(value.targetId);
|
|
87
|
+
else if (Object.hasOwn(value, 'targetId')) {
|
|
88
|
+
throw deliveryError('bad-request', 'A target id cannot be changed');
|
|
89
|
+
}
|
|
90
|
+
return value;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function draftTargetObject(value) {
|
|
94
|
+
targetObject(value);
|
|
95
|
+
const keys = Object.keys(value);
|
|
96
|
+
if (keys.length !== 2 || !keys.includes('kind') || !keys.includes('route')) {
|
|
97
|
+
throw deliveryError('bad-request', 'Invalid draft target');
|
|
98
|
+
}
|
|
99
|
+
return value;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function cancellation(signal) {
|
|
103
|
+
if (signal?.aborted) throw deliveryError('cancelled', 'Request cancelled');
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function publicOperationError(error, fallback = 'delivery-failed') {
|
|
107
|
+
if (error?.code === 'workspace-bot-not-found') {
|
|
108
|
+
return deliveryError('unknown-bot', 'Unknown bot', { cause: error });
|
|
109
|
+
}
|
|
110
|
+
if (DELIVERY_ERROR_CODES.has(error?.code)) return error;
|
|
111
|
+
return deliveryError(fallback, fallback, { cause: error });
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function validateAdapter(adapter) {
|
|
115
|
+
if (!adapter || typeof adapter !== 'object'
|
|
116
|
+
|| typeof adapter.channel !== 'string' || !CHANNEL_PATTERN.test(adapter.channel)) {
|
|
117
|
+
throw new TypeError('A delivery adapter with a valid channel is required');
|
|
118
|
+
}
|
|
119
|
+
for (const method of ADAPTER_METHODS) {
|
|
120
|
+
if (typeof adapter[method] !== 'function') {
|
|
121
|
+
throw new TypeError(`A complete delivery adapter is required (${method})`);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return adapter;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function sessionSyncState(value, available) {
|
|
128
|
+
const enabled = value?.enabled === true;
|
|
129
|
+
if (!available) return { enabled, state: 'unavailable' };
|
|
130
|
+
const state = value?.state;
|
|
131
|
+
if (['off', 'active', 'waiting', 'unavailable'].includes(state)) {
|
|
132
|
+
return { enabled, state };
|
|
133
|
+
}
|
|
134
|
+
return { enabled: false, state: 'unavailable' };
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export class DeliveryService {
|
|
138
|
+
#adapters = new Map();
|
|
139
|
+
#unavailableSessionSyncChannels;
|
|
140
|
+
|
|
141
|
+
constructor({ unavailableSessionSyncChannels = [] } = {}) {
|
|
142
|
+
if (!Array.isArray(unavailableSessionSyncChannels)
|
|
143
|
+
|| unavailableSessionSyncChannels.some((channel) => (
|
|
144
|
+
typeof channel !== 'string' || !CHANNEL_PATTERN.test(channel)
|
|
145
|
+
))) {
|
|
146
|
+
throw new TypeError('unavailableSessionSyncChannels must contain valid channel ids');
|
|
147
|
+
}
|
|
148
|
+
this.#unavailableSessionSyncChannels = new Set(unavailableSessionSyncChannels);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
registerAdapter(value) {
|
|
152
|
+
const adapter = validateAdapter(value);
|
|
153
|
+
this.#adapters.get(adapter.channel)?.controller.abort(deliveryError('provider-unavailable'));
|
|
154
|
+
const registration = Object.freeze({ adapter, controller: new AbortController() });
|
|
155
|
+
this.#adapters.set(adapter.channel, registration);
|
|
156
|
+
return () => {
|
|
157
|
+
if (this.#adapters.get(adapter.channel) !== registration) return false;
|
|
158
|
+
this.#adapters.delete(adapter.channel);
|
|
159
|
+
registration.controller.abort(deliveryError('provider-unavailable'));
|
|
160
|
+
return true;
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
async listTargets(botId) {
|
|
165
|
+
const id = botIdOf(botId);
|
|
166
|
+
const adapter = await this.#adapterFor(id);
|
|
167
|
+
try {
|
|
168
|
+
const targets = await adapter.listTargets(id);
|
|
169
|
+
if (!Array.isArray(targets)) throw new TypeError('Adapter returned invalid targets');
|
|
170
|
+
const available = this.#supportsSessionSync(adapter);
|
|
171
|
+
return {
|
|
172
|
+
botId: id,
|
|
173
|
+
channel: adapter.channel,
|
|
174
|
+
targets: targets.map((target) => ({
|
|
175
|
+
...target,
|
|
176
|
+
sessionSync: sessionSyncState(target?.sessionSync, available),
|
|
177
|
+
})),
|
|
178
|
+
};
|
|
179
|
+
} catch (error) {
|
|
180
|
+
throw publicOperationError(error);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
async listBots() {
|
|
185
|
+
const bots = [];
|
|
186
|
+
for (const { adapter } of this.#adapters.values()) {
|
|
187
|
+
try {
|
|
188
|
+
const ids = await adapter.listBots();
|
|
189
|
+
if (!Array.isArray(ids)) throw new TypeError('Adapter returned invalid bots');
|
|
190
|
+
for (const botId of ids) bots.push({ botId: botIdOf(botId), channel: adapter.channel });
|
|
191
|
+
} catch (error) {
|
|
192
|
+
throw publicOperationError(error);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return bots;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
async listSuggestions(botId) {
|
|
199
|
+
const id = botIdOf(botId);
|
|
200
|
+
const adapter = await this.#adapterFor(id);
|
|
201
|
+
try {
|
|
202
|
+
const suggestions = await adapter.listSuggestions(id);
|
|
203
|
+
if (!Array.isArray(suggestions)) throw new TypeError('Adapter returned invalid suggestions');
|
|
204
|
+
return {
|
|
205
|
+
botId: id,
|
|
206
|
+
channel: adapter.channel,
|
|
207
|
+
suggestions: suggestions.map((suggestion) => normalizeDeliveryTarget(
|
|
208
|
+
adapter.channel,
|
|
209
|
+
suggestion,
|
|
210
|
+
{ targetIdRequired: false },
|
|
211
|
+
)),
|
|
212
|
+
};
|
|
213
|
+
} catch (error) {
|
|
214
|
+
throw publicOperationError(error);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
async createTarget(botId, target) {
|
|
219
|
+
const id = botIdOf(botId);
|
|
220
|
+
targetObject(target, { includesTargetId: true });
|
|
221
|
+
const adapter = await this.#adapterFor(id);
|
|
222
|
+
try {
|
|
223
|
+
return await adapter.createTarget(id, target);
|
|
224
|
+
} catch (error) {
|
|
225
|
+
throw publicOperationError(error);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
async updateTarget(botId, targetId, replacement) {
|
|
230
|
+
const id = botIdOf(botId);
|
|
231
|
+
const targetKey = targetIdOf(targetId);
|
|
232
|
+
targetObject(replacement);
|
|
233
|
+
const adapter = await this.#adapterFor(id);
|
|
234
|
+
try {
|
|
235
|
+
return await adapter.updateTarget(id, targetKey, replacement);
|
|
236
|
+
} catch (error) {
|
|
237
|
+
throw publicOperationError(error);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
async deleteTarget(botId, targetId) {
|
|
242
|
+
const id = botIdOf(botId);
|
|
243
|
+
const targetKey = targetIdOf(targetId);
|
|
244
|
+
const adapter = await this.#adapterFor(id);
|
|
245
|
+
try {
|
|
246
|
+
await adapter.deleteTarget(id, targetKey);
|
|
247
|
+
return { deleted: true };
|
|
248
|
+
} catch (error) {
|
|
249
|
+
throw publicOperationError(error);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
async setSessionSync(botId, targetId, enabled) {
|
|
254
|
+
const id = botIdOf(botId);
|
|
255
|
+
const targetKey = targetIdOf(targetId);
|
|
256
|
+
if (typeof enabled !== 'boolean') throw deliveryError('bad-request', 'Invalid enabled state');
|
|
257
|
+
const adapter = await this.#adapterFor(id);
|
|
258
|
+
const hasMethods = SESSION_SYNC_METHODS.every((method) => typeof adapter[method] === 'function');
|
|
259
|
+
if (!hasMethods || (enabled && !this.#supportsSessionSync(adapter))) {
|
|
260
|
+
throw deliveryError('session-sync-unavailable', 'Session sync is unavailable');
|
|
261
|
+
}
|
|
262
|
+
try {
|
|
263
|
+
return await adapter.setSessionSync(id, targetKey, enabled);
|
|
264
|
+
} catch (error) {
|
|
265
|
+
throw publicOperationError(error);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
async listSessionSyncTargets(sessionId) {
|
|
270
|
+
if (typeof sessionId !== 'string' || !sessionId) {
|
|
271
|
+
throw deliveryError('bad-request', 'Invalid Session id');
|
|
272
|
+
}
|
|
273
|
+
const targets = [];
|
|
274
|
+
for (const { adapter } of this.#adapters.values()) {
|
|
275
|
+
if (!this.#supportsSessionSync(adapter)) continue;
|
|
276
|
+
try {
|
|
277
|
+
const listed = await adapter.listSessionSyncTargets(sessionId);
|
|
278
|
+
if (!Array.isArray(listed)) throw new TypeError('Adapter returned invalid sync targets');
|
|
279
|
+
for (const target of listed) {
|
|
280
|
+
targets.push({
|
|
281
|
+
channel: adapter.channel,
|
|
282
|
+
botId: botIdOf(target?.botId),
|
|
283
|
+
targetId: targetIdOf(target?.targetId),
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
} catch (error) {
|
|
287
|
+
console.warn(
|
|
288
|
+
`[dsh-im] ignored ${adapter.channel} Session sync target lookup failure`
|
|
289
|
+
+ ` (${error?.code ?? error?.name ?? 'unknown-error'})`,
|
|
290
|
+
);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
return targets;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
async sendSessionSyncText(botId, targetId, sessionId, text, { signal } = {}) {
|
|
297
|
+
const id = botIdOf(botId);
|
|
298
|
+
const targetKey = targetIdOf(targetId);
|
|
299
|
+
if (typeof sessionId !== 'string' || !sessionId
|
|
300
|
+
|| typeof text !== 'string' || !text.trim()) {
|
|
301
|
+
throw deliveryError('bad-request', 'Invalid session sync delivery');
|
|
302
|
+
}
|
|
303
|
+
cancellation(signal);
|
|
304
|
+
const adapter = await this.#adapterFor(id);
|
|
305
|
+
if (!this.#supportsSessionSync(adapter)) {
|
|
306
|
+
throw deliveryError('session-sync-unavailable', 'Session sync is unavailable');
|
|
307
|
+
}
|
|
308
|
+
try {
|
|
309
|
+
await adapter.sendSessionSyncText(id, targetKey, sessionId, text, { signal });
|
|
310
|
+
return { sent: true };
|
|
311
|
+
} catch (error) {
|
|
312
|
+
if (signal?.aborted || error?.name === 'AbortError' || error?.code === 'ABORT_ERR') {
|
|
313
|
+
throw deliveryError('cancelled', 'Request cancelled', { cause: error });
|
|
314
|
+
}
|
|
315
|
+
throw publicOperationError(error);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
#assertRegistered(registration) {
|
|
320
|
+
if (this.#adapters.get(registration.adapter.channel) !== registration) {
|
|
321
|
+
throw deliveryError('capability-unavailable');
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
async describeBot(botId) {
|
|
326
|
+
const id = botIdOf(botId);
|
|
327
|
+
const registration = await this.#checkedRegistrationFor(id);
|
|
328
|
+
const { adapter } = registration;
|
|
329
|
+
if (typeof adapter.describeAccount !== 'function') throw deliveryError('capability-unavailable');
|
|
330
|
+
this.#assertRegistered(registration);
|
|
331
|
+
try {
|
|
332
|
+
const account = await adapter.describeAccount(id);
|
|
333
|
+
this.#assertRegistered(registration);
|
|
334
|
+
return account;
|
|
335
|
+
} catch (error) { throw publicOperationError(error); }
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
async consumeInbound(botId, options = {}) {
|
|
339
|
+
const id = botIdOf(botId);
|
|
340
|
+
cancellation(options.signal);
|
|
341
|
+
if (!/^[a-f0-9]{64}$/.test(options.expectedFingerprint ?? '') || typeof options.onEvent !== 'function')
|
|
342
|
+
throw deliveryError('bad-request');
|
|
343
|
+
const registration = await this.#checkedRegistrationFor(id);
|
|
344
|
+
if (typeof registration.adapter.consumeInbound !== 'function' || typeof options.onEvent !== 'function')
|
|
345
|
+
throw deliveryError('capability-unavailable');
|
|
346
|
+
this.#assertRegistered(registration);
|
|
347
|
+
const dispose = await registration.adapter.consumeInbound(id, {
|
|
348
|
+
...options,
|
|
349
|
+
signal: options.signal ? AbortSignal.any([options.signal, registration.controller.signal]) : registration.controller.signal,
|
|
350
|
+
...(options.onEcho === undefined ? {} : { onEcho: async (evidence, context) => {
|
|
351
|
+
this.#assertRegistered(registration);
|
|
352
|
+
const result = await options.onEcho(evidence, context);
|
|
353
|
+
this.#assertRegistered(registration);
|
|
354
|
+
return result;
|
|
355
|
+
} }),
|
|
356
|
+
onEvent: async (evidence, context) => {
|
|
357
|
+
this.#assertRegistered(registration);
|
|
358
|
+
const result = await options.onEvent(evidence, context);
|
|
359
|
+
this.#assertRegistered(registration);
|
|
360
|
+
return result;
|
|
361
|
+
},
|
|
362
|
+
});
|
|
363
|
+
try { this.#assertRegistered(registration); } catch (error) { dispose(); throw error; }
|
|
364
|
+
return dispose;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
async historyChecked(botId, route, query, options = {}) {
|
|
368
|
+
const id = botIdOf(botId);
|
|
369
|
+
cancellation(options.signal);
|
|
370
|
+
if (!/^[a-f0-9]{64}$/.test(options.expectedFingerprint ?? '')) throw deliveryError('bad-request');
|
|
371
|
+
const registration = await this.#checkedRegistrationFor(id);
|
|
372
|
+
if (typeof registration.adapter.historyChecked !== 'function') throw deliveryError('capability-unavailable');
|
|
373
|
+
this.#assertRegistered(registration);
|
|
374
|
+
const signal = options.signal ? AbortSignal.any([options.signal, registration.controller.signal]) : registration.controller.signal;
|
|
375
|
+
try {
|
|
376
|
+
const result = await registration.adapter.historyChecked(id, structuredClone(route), structuredClone(query), { ...options, signal });
|
|
377
|
+
this.#assertRegistered(registration);
|
|
378
|
+
cancellation(options.signal);
|
|
379
|
+
return result;
|
|
380
|
+
} catch (error) {
|
|
381
|
+
this.#assertRegistered(registration);
|
|
382
|
+
if (signal.aborted || error?.name === 'AbortError') throw deliveryError('cancelled');
|
|
383
|
+
throw publicOperationError(error, 'history-unavailable');
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
async qualifyReplyChecked(botId, route, options = {}) {
|
|
388
|
+
const id = botIdOf(botId);
|
|
389
|
+
cancellation(options.signal);
|
|
390
|
+
if (!/^[a-f0-9]{64}$/.test(options.expectedFingerprint ?? '')) throw deliveryError('bad-request');
|
|
391
|
+
const registration = await this.#checkedRegistrationFor(id);
|
|
392
|
+
if (typeof registration.adapter.qualifyReplyChecked !== 'function') throw deliveryError('capability-unavailable');
|
|
393
|
+
this.#assertRegistered(registration);
|
|
394
|
+
const signal = options.signal ? AbortSignal.any([options.signal, registration.controller.signal]) : registration.controller.signal;
|
|
395
|
+
try {
|
|
396
|
+
const result = await registration.adapter.qualifyReplyChecked(id, structuredClone(route), { ...options, signal });
|
|
397
|
+
this.#assertRegistered(registration);
|
|
398
|
+
cancellation(signal);
|
|
399
|
+
return result;
|
|
400
|
+
}
|
|
401
|
+
catch (error) { const safe = publicOperationError(error); throw deliveryError(safe.code, safe.code); }
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
async replyChecked(botId, route, text, options = {}) {
|
|
405
|
+
const id = botIdOf(botId);
|
|
406
|
+
cancellation(options.signal);
|
|
407
|
+
if (!/^[a-f0-9]{64}$/.test(options.expectedFingerprint ?? '')
|
|
408
|
+
|| typeof text !== 'string' || !text.trim() || text.length > 4000)
|
|
409
|
+
throw deliveryError('bad-request');
|
|
410
|
+
const registration = await this.#checkedRegistrationFor(id);
|
|
411
|
+
if (typeof registration.adapter.replyChecked !== 'function') throw deliveryError('capability-unavailable');
|
|
412
|
+
this.#assertRegistered(registration);
|
|
413
|
+
try { return await registration.adapter.replyChecked(id, structuredClone(route), text, {
|
|
414
|
+
...options, signal: options.signal ? AbortSignal.any([options.signal, registration.controller.signal]) : registration.controller.signal,
|
|
415
|
+
}); }
|
|
416
|
+
catch (error) { throw publicOperationError(error); }
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
async externalFileChecked(botId, route, value, options = {}) {
|
|
420
|
+
const id = botIdOf(botId);
|
|
421
|
+
cancellation(options.signal);
|
|
422
|
+
if (!/^[a-f0-9]{64}$/.test(options.expectedFingerprint ?? '')) throw deliveryError('bad-request');
|
|
423
|
+
const registration = await this.#checkedRegistrationFor(id);
|
|
424
|
+
if (typeof registration.adapter.externalFileChecked !== 'function') throw deliveryError('capability-unavailable');
|
|
425
|
+
this.#assertRegistered(registration);
|
|
426
|
+
try {
|
|
427
|
+
return await registration.adapter.externalFileChecked(id, structuredClone(route), structuredClone(value), {
|
|
428
|
+
...options, signal: options.signal ? AbortSignal.any([options.signal, registration.controller.signal]) : registration.controller.signal,
|
|
429
|
+
});
|
|
430
|
+
} catch (error) { throw publicOperationError(error); }
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
async sendChecked(botId, targetId, text, { expectedFingerprint, expectedTargetDigest, signal, format = 'plain', receipt = false } = {}) {
|
|
434
|
+
const id = botIdOf(botId);
|
|
435
|
+
const key = targetIdOf(targetId);
|
|
436
|
+
if (typeof text !== 'string' || !text.trim() || !['plain', 'markdown'].includes(format) || typeof receipt !== 'boolean'
|
|
437
|
+
|| !/^[a-f0-9]{64}$/.test(expectedFingerprint ?? '') || !/^[a-f0-9]{64}$/.test(expectedTargetDigest ?? '')) {
|
|
438
|
+
throw deliveryError('bad-request');
|
|
439
|
+
}
|
|
440
|
+
cancellation(signal);
|
|
441
|
+
const registration = await this.#checkedRegistrationFor(id);
|
|
442
|
+
const { adapter } = registration;
|
|
443
|
+
if (typeof adapter.describeAccount !== 'function') throw deliveryError('capability-unavailable');
|
|
444
|
+
this.#assertRegistered(registration);
|
|
445
|
+
try {
|
|
446
|
+
const candidates = await adapter.listTargets(id);
|
|
447
|
+
const saved = candidates.find((target) => target?.targetId === key);
|
|
448
|
+
if (!saved) throw deliveryError('unknown-target');
|
|
449
|
+
const target = normalizeDeliveryTarget(adapter.channel, {
|
|
450
|
+
targetId: saved.targetId, kind: saved.kind, route: structuredClone(saved.route),
|
|
451
|
+
});
|
|
452
|
+
const digest = createHash('sha256').update(JSON.stringify({ kind: target.kind,
|
|
453
|
+
route: Object.fromEntries(Object.entries(target.route).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0)) })).digest('hex');
|
|
454
|
+
if (digest !== expectedTargetDigest) throw deliveryError('target-changed');
|
|
455
|
+
const account = await adapter.describeAccount(id);
|
|
456
|
+
if (account?.version !== 1 || !account.capabilities?.includes('proactive-text-checked')) {
|
|
457
|
+
throw deliveryError('capability-unavailable');
|
|
458
|
+
}
|
|
459
|
+
if (account.account?.fingerprint !== expectedFingerprint) throw deliveryError('account-changed');
|
|
460
|
+
const receiptConversation = adapter.channel === 'feishu' && target.kind === 'group'
|
|
461
|
+
? target.route.chatId : adapter.channel === 'slack' && target.kind === 'conversation'
|
|
462
|
+
? target.route.channelId : undefined;
|
|
463
|
+
if (receipt && (!account.capabilities?.includes('proactive-receipt-checked') || !receiptConversation))
|
|
464
|
+
throw deliveryError('capability-unavailable');
|
|
465
|
+
cancellation(signal);
|
|
466
|
+
this.#assertRegistered(registration);
|
|
467
|
+
const deliverySignal = signal ? AbortSignal.any([signal, registration.controller.signal]) : registration.controller.signal;
|
|
468
|
+
const result = await adapter.sendText(id, target, text, { signal: deliverySignal, expectedFingerprint,
|
|
469
|
+
...(receipt ? { receipt: true } : {}),
|
|
470
|
+
...(format === 'markdown' ? { format } : {}) });
|
|
471
|
+
if (!receipt) return { sent: true };
|
|
472
|
+
if (result?.sent !== true || result.receipt?.version !== 1
|
|
473
|
+
|| typeof result.receipt.messageId !== 'string' || !result.receipt.messageId || result.receipt.messageId.length > 512
|
|
474
|
+
|| result.receipt.conversationId !== receiptConversation)
|
|
475
|
+
throw deliveryError('send-result-unknown');
|
|
476
|
+
return { sent: true, receipt: { version: 1, messageId: result.receipt.messageId, conversationId: result.receipt.conversationId } };
|
|
477
|
+
} catch (error) { throw publicOperationError(error); }
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
async send(botId, targetIdOrDraft, text, { signal, format = 'plain' } = {}) {
|
|
481
|
+
const id = botIdOf(botId);
|
|
482
|
+
const targetKey = typeof targetIdOrDraft === 'string'
|
|
483
|
+
? targetIdOf(targetIdOrDraft)
|
|
484
|
+
: null;
|
|
485
|
+
const draft = targetKey === null ? draftTargetObject(targetIdOrDraft) : null;
|
|
486
|
+
if (typeof text !== 'string' || !text.trim()) {
|
|
487
|
+
throw deliveryError('bad-request', 'Message text is required');
|
|
488
|
+
}
|
|
489
|
+
if (format !== 'plain' && format !== 'markdown') {
|
|
490
|
+
throw deliveryError('bad-request', 'Message format must be plain or markdown');
|
|
491
|
+
}
|
|
492
|
+
cancellation(signal);
|
|
493
|
+
const adapter = await this.#adapterFor(id);
|
|
494
|
+
try {
|
|
495
|
+
let target;
|
|
496
|
+
if (draft) {
|
|
497
|
+
target = { targetId: DRAFT_TARGET_ID, ...draft };
|
|
498
|
+
} else {
|
|
499
|
+
const targets = await adapter.listTargets(id);
|
|
500
|
+
if (!Array.isArray(targets)) throw new TypeError('Adapter returned invalid targets');
|
|
501
|
+
target = targets.find((candidate) => candidate?.targetId === targetKey);
|
|
502
|
+
if (!target) throw deliveryError('unknown-target', 'Unknown target');
|
|
503
|
+
}
|
|
504
|
+
cancellation(signal);
|
|
505
|
+
await adapter.sendText(id, target, text, {
|
|
506
|
+
signal,
|
|
507
|
+
...(format === 'markdown' ? { format } : {}),
|
|
508
|
+
});
|
|
509
|
+
return { sent: true };
|
|
510
|
+
} catch (error) {
|
|
511
|
+
if (signal?.aborted || error?.name === 'AbortError' || error?.code === 'ABORT_ERR') {
|
|
512
|
+
throw deliveryError('cancelled', 'Request cancelled', { cause: error });
|
|
513
|
+
}
|
|
514
|
+
throw publicOperationError(error);
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
async #checkedRegistrationFor(botId) {
|
|
519
|
+
for (const registration of this.#adapters.values()) {
|
|
520
|
+
let ownsBot;
|
|
521
|
+
try { ownsBot = await registration.adapter.ownsBot(botId); }
|
|
522
|
+
catch (error) { throw publicOperationError(error); }
|
|
523
|
+
if (ownsBot) {
|
|
524
|
+
this.#assertRegistered(registration);
|
|
525
|
+
return registration;
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
throw deliveryError('unknown-bot', 'Unknown bot');
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
async #adapterFor(botId) {
|
|
532
|
+
for (const { adapter } of this.#adapters.values()) {
|
|
533
|
+
let ownsBot;
|
|
534
|
+
try {
|
|
535
|
+
ownsBot = await adapter.ownsBot(botId);
|
|
536
|
+
} catch (error) {
|
|
537
|
+
throw publicOperationError(error);
|
|
538
|
+
}
|
|
539
|
+
if (ownsBot) return adapter;
|
|
540
|
+
}
|
|
541
|
+
throw deliveryError('unknown-bot', 'Unknown bot');
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
#supportsSessionSync(adapter) {
|
|
545
|
+
return !this.#unavailableSessionSyncChannels.has(adapter.channel)
|
|
546
|
+
&& SESSION_SYNC_METHODS.every((method) => typeof adapter[method] === 'function');
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
export function createDeliveryService(options) {
|
|
551
|
+
return new DeliveryService(options);
|
|
552
|
+
}
|