@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.
- 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 +604 -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 +18 -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 +319 -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 +239 -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 +258 -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 +411 -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 +618 -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 +229 -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 +28 -0
- package/src/channels/discord/discord-api.mjs +423 -0
- package/src/channels/discord/discord-bridge.mjs +16 -0
- package/src/channels/discord/discord-controller.mjs +29 -0
- package/src/channels/discord/discord-runtime.mjs +1091 -0
- package/src/channels/discord/external-consumer.mjs +199 -0
- package/src/channels/discord/external-files.mjs +97 -0
- package/src/channels/discord/harness-client.mjs +11 -0
- package/src/channels/discord/history-reader.mjs +142 -0
- package/src/channels/discord/nearby-history.mjs +112 -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-cards.mjs +87 -0
- package/src/channels/feishu/external-consumer.mjs +89 -0
- package/src/channels/feishu/external-files.mjs +115 -0
- package/src/channels/feishu/external-images.mjs +50 -0
- package/src/channels/feishu/external-names.mjs +99 -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 +982 -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 +1619 -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 +91 -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 +529 -0
- package/src/channels/shared/token-config-store.mjs +208 -0
- package/src/channels/shared/video-media.mjs +9 -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 +93 -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 +644 -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 +895 -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 +194 -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/external-consumer.mjs +160 -0
- package/src/channels/weixin/external-files.mjs +84 -0
- package/src/channels/weixin/external-typing.mjs +120 -0
- package/src/channels/weixin/harness-client.mjs +19 -0
- package/src/channels/weixin/state-store.mjs +316 -0
- package/src/channels/weixin/weixin-api.mjs +1009 -0
- package/src/channels/weixin/weixin-bridge.mjs +1537 -0
- package/src/channels/weixin/weixin-controller.mjs +779 -0
- package/src/channels/weixin/weixin-runtime.mjs +507 -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,242 @@
|
|
|
1
|
+
import { createConnectionDiagnostics, diagnosticRpcResult } from '../../../../src/channels/shared/connection-error.mjs';
|
|
2
|
+
import { SET_ALIAS_ENDPOINT, validAliasPayload } from '../shared/bot-alias-rpc.mjs';
|
|
3
|
+
import { registerManagementRpc } from '../../../management-rpc.mjs';
|
|
4
|
+
import QRCode from 'qrcode';
|
|
5
|
+
|
|
6
|
+
import { publicConnectionTestResult } from '../../../../src/channels/shared/connection-test.mjs';
|
|
7
|
+
import { SET_ACCESS_POLICY_ENDPOINT, validAccessPolicyPayload } from '../shared/access-policy-rpc.mjs';
|
|
8
|
+
import { SET_CONTEXT_ENHANCEMENT_ENDPOINT, validContextEnhancementPayload } from '../shared/context-enhancement-rpc.mjs';
|
|
9
|
+
import { resolveRpcAuthority } from '../../rpc-authority.mjs';
|
|
10
|
+
import { publicWorkspaceError, SET_WORKSPACE_ENDPOINT, validWorkspacePayload } from '../shared/workspace-rpc.mjs';
|
|
11
|
+
import { SET_AGENT_PRESET_ENDPOINT, validAgentPresetPayload } from '../shared/agent-preset-rpc.mjs';
|
|
12
|
+
import { SET_MODEL_ENDPOINT, validModelPayload } from '../shared/model-setting-rpc.mjs';
|
|
13
|
+
|
|
14
|
+
export const WHATSAPP_RPC_CHANNEL = '/whatsapp';
|
|
15
|
+
export const WHATSAPP_ENDPOINTS = Object.freeze({
|
|
16
|
+
status: 'connection.status',
|
|
17
|
+
beginProvisioning: 'provision.begin',
|
|
18
|
+
pollProvisioning: 'provision.poll',
|
|
19
|
+
cancelProvisioning: 'provision.cancel',
|
|
20
|
+
reconnectBot: 'bot.reconnect',
|
|
21
|
+
deleteBot: 'bot.delete',
|
|
22
|
+
setAccessPolicy: SET_ACCESS_POLICY_ENDPOINT,
|
|
23
|
+
setAlias: SET_ALIAS_ENDPOINT,
|
|
24
|
+
setWorkspace: SET_WORKSPACE_ENDPOINT,
|
|
25
|
+
setModel: SET_MODEL_ENDPOINT,
|
|
26
|
+
setAgentPreset: SET_AGENT_PRESET_ENDPOINT,
|
|
27
|
+
setContextEnhancement: SET_CONTEXT_ENHANCEMENT_ENDPOINT,
|
|
28
|
+
});
|
|
29
|
+
export const WHATSAPP_RPC_ENDPOINTS = Object.freeze(Object.values(WHATSAPP_ENDPOINTS));
|
|
30
|
+
|
|
31
|
+
const FORBIDDEN_PUBLIC_KEYS = new Set(['qrValue', 'accountJid', 'authDirectory']);
|
|
32
|
+
|
|
33
|
+
function isRecord(value) {
|
|
34
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function exactKeys(value, allowed) {
|
|
38
|
+
return isRecord(value) && Object.keys(value).every((key) => allowed.includes(key));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function validId(value) {
|
|
42
|
+
return typeof value === 'string' && /^[A-Za-z0-9_-]{1,128}$/.test(value);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function payloadFailure(endpoint, payload) {
|
|
46
|
+
if (!isRecord(payload)) return 'Payload must be an object.';
|
|
47
|
+
if ([WHATSAPP_ENDPOINTS.status, WHATSAPP_ENDPOINTS.beginProvisioning].includes(endpoint)) {
|
|
48
|
+
return exactKeys(payload, []) ? null : `${endpoint} does not accept fields.`;
|
|
49
|
+
}
|
|
50
|
+
if ([WHATSAPP_ENDPOINTS.pollProvisioning, WHATSAPP_ENDPOINTS.cancelProvisioning].includes(endpoint)) {
|
|
51
|
+
return exactKeys(payload, ['attemptId']) && validId(payload.attemptId)
|
|
52
|
+
? null : `${endpoint} requires an attemptId.`;
|
|
53
|
+
}
|
|
54
|
+
if (endpoint === WHATSAPP_ENDPOINTS.reconnectBot) {
|
|
55
|
+
return exactKeys(payload, ['botId', 'sendTest']) && validId(payload.botId)
|
|
56
|
+
&& (payload.sendTest === undefined || typeof payload.sendTest === 'boolean')
|
|
57
|
+
? null : 'bot.reconnect requires a botId and optional sendTest flag.';
|
|
58
|
+
}
|
|
59
|
+
if (endpoint === WHATSAPP_ENDPOINTS.deleteBot) {
|
|
60
|
+
return exactKeys(payload, ['botId', 'confirm']) && validId(payload.botId)
|
|
61
|
+
&& payload.confirm === true ? null : 'bot.delete requires a botId and confirm=true.';
|
|
62
|
+
}
|
|
63
|
+
if (endpoint === WHATSAPP_ENDPOINTS.setAccessPolicy) {
|
|
64
|
+
return validAccessPolicyPayload(payload)
|
|
65
|
+
? null : '请提交有效的访问设置。';
|
|
66
|
+
}
|
|
67
|
+
if (endpoint === WHATSAPP_ENDPOINTS.setAlias) {
|
|
68
|
+
return validAliasPayload(payload)
|
|
69
|
+
? null : '请输入有效的别名(最多 80 个字符)。';
|
|
70
|
+
}
|
|
71
|
+
if (endpoint === WHATSAPP_ENDPOINTS.setWorkspace) {
|
|
72
|
+
return validWorkspacePayload(payload)
|
|
73
|
+
? null : '请输入工作区绝对路径。';
|
|
74
|
+
}
|
|
75
|
+
if (endpoint === WHATSAPP_ENDPOINTS.setModel) {
|
|
76
|
+
return validModelPayload(payload) ? null : '请选择有效模型。';
|
|
77
|
+
}
|
|
78
|
+
if (endpoint === WHATSAPP_ENDPOINTS.setAgentPreset) {
|
|
79
|
+
return validAgentPresetPayload(payload)
|
|
80
|
+
? null : '请选择 Agent Preset。';
|
|
81
|
+
}
|
|
82
|
+
if (endpoint === WHATSAPP_ENDPOINTS.setContextEnhancement) {
|
|
83
|
+
return validContextEnhancementPayload(payload)
|
|
84
|
+
? null : '请提交有效的上下文增强设置。';
|
|
85
|
+
}
|
|
86
|
+
return 'Unknown WhatsApp endpoint.';
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function sanitizePublic(value) {
|
|
90
|
+
if (Array.isArray(value)) return value.map(sanitizePublic);
|
|
91
|
+
if (!isRecord(value)) return value;
|
|
92
|
+
const safe = {};
|
|
93
|
+
for (const [key, child] of Object.entries(value)) {
|
|
94
|
+
if (!FORBIDDEN_PUBLIC_KEYS.has(key)) safe[key] = sanitizePublic(child);
|
|
95
|
+
}
|
|
96
|
+
return safe;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async function qrDataUrl(value) {
|
|
100
|
+
return QRCode.toDataURL(value, {
|
|
101
|
+
type: 'image/png',
|
|
102
|
+
errorCorrectionLevel: 'M',
|
|
103
|
+
margin: 2,
|
|
104
|
+
width: 320,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async function encodeAttempt(value, encodeQr) {
|
|
109
|
+
if (!value || typeof value.qrValue !== 'string') return sanitizePublic(value);
|
|
110
|
+
return sanitizePublic({ ...value, qrCodeDataUrl: await encodeQr(value.qrValue) });
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async function publicStatus(value, encodeQr) {
|
|
114
|
+
const snapshot = structuredClone(value);
|
|
115
|
+
if (snapshot?.provisioning) snapshot.provisioning = await encodeAttempt(snapshot.provisioning, encodeQr);
|
|
116
|
+
return sanitizePublic(snapshot);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function createWhatsappRpcHandler(controller, { encodeQr = qrDataUrl } = {}) {
|
|
120
|
+
const diagnostics = controller.diagnostics ?? createConnectionDiagnostics({ channel: 'whatsapp' });
|
|
121
|
+
for (const method of ['status', 'startProvisioning', 'registrationStatus', 'cancelProvisioning', 'reconnectBot', 'deleteBot']) {
|
|
122
|
+
if (typeof controller?.[method] !== 'function') {
|
|
123
|
+
throw new TypeError(`A complete WhatsApp controller is required (${method})`);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
const qrCache = new Map();
|
|
127
|
+
const cachedEncode = (value) => {
|
|
128
|
+
let encoded = qrCache.get(value);
|
|
129
|
+
if (!encoded) {
|
|
130
|
+
if (qrCache.size >= 16) qrCache.delete(qrCache.keys().next().value);
|
|
131
|
+
encoded = Promise.resolve().then(() => encodeQr(value));
|
|
132
|
+
qrCache.set(value, encoded);
|
|
133
|
+
}
|
|
134
|
+
return encoded;
|
|
135
|
+
};
|
|
136
|
+
return async (endpoint, payload, signal) => {
|
|
137
|
+
if (signal?.aborted) return { ok: false, error: { code: 'cancelled', message: 'The request was cancelled.' } };
|
|
138
|
+
if (!WHATSAPP_RPC_ENDPOINTS.includes(endpoint)) {
|
|
139
|
+
return { ok: false, error: { code: 'bad-request', message: 'Unknown WhatsApp endpoint.' } };
|
|
140
|
+
}
|
|
141
|
+
const invalid = payloadFailure(endpoint, payload);
|
|
142
|
+
if (invalid) return { ok: false, error: { code: 'bad-request', message: invalid } };
|
|
143
|
+
try {
|
|
144
|
+
let value;
|
|
145
|
+
if (endpoint === WHATSAPP_ENDPOINTS.status) {
|
|
146
|
+
value = await publicStatus(await controller.status(), cachedEncode);
|
|
147
|
+
} else if (endpoint === WHATSAPP_ENDPOINTS.beginProvisioning) {
|
|
148
|
+
value = await encodeAttempt(await controller.startProvisioning(), cachedEncode);
|
|
149
|
+
} else if (endpoint === WHATSAPP_ENDPOINTS.pollProvisioning) {
|
|
150
|
+
const attempt = await controller.registrationStatus(payload.attemptId);
|
|
151
|
+
if (!attempt) return { ok: false, error: { code: 'bad-request', message: 'The provisioning attempt no longer exists.' } };
|
|
152
|
+
value = await encodeAttempt(attempt, cachedEncode);
|
|
153
|
+
} else if (endpoint === WHATSAPP_ENDPOINTS.cancelProvisioning) {
|
|
154
|
+
value = sanitizePublic(await controller.cancelProvisioning(payload.attemptId));
|
|
155
|
+
} else if (endpoint === WHATSAPP_ENDPOINTS.reconnectBot) {
|
|
156
|
+
const checked = await controller.reconnectBot(payload.botId);
|
|
157
|
+
if (signal?.aborted) {
|
|
158
|
+
return { ok: false, error: { code: 'cancelled', message: 'The request was cancelled.' } };
|
|
159
|
+
}
|
|
160
|
+
value = await publicStatus(checked, cachedEncode);
|
|
161
|
+
if (payload.sendTest === true) {
|
|
162
|
+
let testError = null;
|
|
163
|
+
const connected = checked?.bots?.some(
|
|
164
|
+
(bot) => bot?.botId === payload.botId && bot.connected === true,
|
|
165
|
+
) === true;
|
|
166
|
+
if (!connected) {
|
|
167
|
+
testError = new Error('WhatsApp bot is not connected');
|
|
168
|
+
testError.code = 'test-target-unavailable';
|
|
169
|
+
} else {
|
|
170
|
+
try {
|
|
171
|
+
if (typeof controller.sendConnectionTest !== 'function') {
|
|
172
|
+
const unavailable = new Error('Connection test is unavailable');
|
|
173
|
+
unavailable.code = 'test-target-unavailable';
|
|
174
|
+
throw unavailable;
|
|
175
|
+
}
|
|
176
|
+
await controller.sendConnectionTest(payload.botId);
|
|
177
|
+
} catch (error) {
|
|
178
|
+
testError = error;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
value = { ...value, testMessage: publicConnectionTestResult(testError, { diagnostics, botId: payload.botId }) };
|
|
182
|
+
}
|
|
183
|
+
} else if (endpoint === WHATSAPP_ENDPOINTS.setWorkspace) {
|
|
184
|
+
if (typeof controller.updateWorkspace !== 'function') throw new Error('Workspace update is unavailable');
|
|
185
|
+
value = await publicStatus(
|
|
186
|
+
await controller.updateWorkspace(payload.botId, payload.workspace),
|
|
187
|
+
cachedEncode,
|
|
188
|
+
);
|
|
189
|
+
} else if (endpoint === WHATSAPP_ENDPOINTS.setModel) {
|
|
190
|
+
if (typeof controller.updateModel !== 'function') throw new Error('Model update is unavailable');
|
|
191
|
+
value = await publicStatus(
|
|
192
|
+
await controller.updateModel(payload.botId, payload.model),
|
|
193
|
+
cachedEncode,
|
|
194
|
+
);
|
|
195
|
+
} else if (endpoint === WHATSAPP_ENDPOINTS.setContextEnhancement) {
|
|
196
|
+
if (typeof controller.updateContextEnhancement !== 'function') throw new Error('Context enhancement update is unavailable');
|
|
197
|
+
value = await controller.updateContextEnhancement(
|
|
198
|
+
payload.botId, payload.config, (status) => publicStatus(status, cachedEncode),
|
|
199
|
+
);
|
|
200
|
+
} else if (endpoint === WHATSAPP_ENDPOINTS.setAgentPreset) {
|
|
201
|
+
if (typeof controller.updateAgentPreset !== 'function') throw new Error('Agent preset update is unavailable');
|
|
202
|
+
value = await publicStatus(
|
|
203
|
+
await controller.updateAgentPreset(payload.botId, payload.agentPreset),
|
|
204
|
+
cachedEncode,
|
|
205
|
+
);
|
|
206
|
+
} else if (endpoint === WHATSAPP_ENDPOINTS.setAlias) {
|
|
207
|
+
if (typeof controller.updateAlias !== 'function') throw new Error('Alias update is unavailable');
|
|
208
|
+
value = await controller.updateAlias(
|
|
209
|
+
payload.botId,
|
|
210
|
+
payload.alias,
|
|
211
|
+
(status) => publicStatus(status, cachedEncode),
|
|
212
|
+
);
|
|
213
|
+
} else if (endpoint === WHATSAPP_ENDPOINTS.setAccessPolicy) {
|
|
214
|
+
if (typeof controller.updateAccessPolicy !== 'function') throw new Error('Access policy update is unavailable');
|
|
215
|
+
value = await controller.updateAccessPolicy(
|
|
216
|
+
payload.botId,
|
|
217
|
+
payload.policy,
|
|
218
|
+
(status) => publicStatus(status, cachedEncode),
|
|
219
|
+
);
|
|
220
|
+
} else {
|
|
221
|
+
value = await publicStatus(await controller.deleteBot(payload.botId), cachedEncode);
|
|
222
|
+
}
|
|
223
|
+
return signal?.aborted
|
|
224
|
+
? { ok: false, error: { code: 'cancelled', message: 'The request was cancelled.' } }
|
|
225
|
+
: { ok: true, value };
|
|
226
|
+
} catch (error) {
|
|
227
|
+
const workspaceError = publicWorkspaceError(error);
|
|
228
|
+
return diagnosticRpcResult(diagnostics, error, signal?.aborted
|
|
229
|
+
? { ok: false, error: { code: 'cancelled', message: 'The request was cancelled.' } }
|
|
230
|
+
: { ok: false, error: workspaceError
|
|
231
|
+
?? { code: 'whatsapp-operation-failed', message: 'WhatsApp 操作失败,请稍后重试。' } }, { operation: endpoint, botId: payload?.botId });
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export function installWhatsappRpc(ctx, controller, options, authority) {
|
|
237
|
+
return registerManagementRpc(ctx,
|
|
238
|
+
WHATSAPP_RPC_CHANNEL,
|
|
239
|
+
createWhatsappRpcHandler(controller, options),
|
|
240
|
+
{ authority: resolveRpcAuthority(authority) },
|
|
241
|
+
);
|
|
242
|
+
}
|
|
@@ -0,0 +1,411 @@
|
|
|
1
|
+
import {
|
|
2
|
+
deliverySuggestionsFromSessions,
|
|
3
|
+
privateConversationKeyMatchesTarget,
|
|
4
|
+
resolvePrivateConversationKey,
|
|
5
|
+
} from './delivery-suggestions.mjs';
|
|
6
|
+
|
|
7
|
+
const CHANNELS = new Set([
|
|
8
|
+
'weixin',
|
|
9
|
+
'feishu',
|
|
10
|
+
'dingtalk',
|
|
11
|
+
'wecom',
|
|
12
|
+
'wecom-app',
|
|
13
|
+
'qq',
|
|
14
|
+
'slack',
|
|
15
|
+
'telegram',
|
|
16
|
+
'discord',
|
|
17
|
+
'whatsapp',
|
|
18
|
+
'imessage',
|
|
19
|
+
'email',
|
|
20
|
+
'matrix',
|
|
21
|
+
]);
|
|
22
|
+
|
|
23
|
+
export function supportsDeliveryChannel(channel) {
|
|
24
|
+
return CHANNELS.has(channel);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function invalidTarget(message) {
|
|
28
|
+
const error = new Error(message);
|
|
29
|
+
error.code = 'invalid-target';
|
|
30
|
+
return error;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function sessionSyncUnavailable(message = 'Session sync is unavailable for this target') {
|
|
34
|
+
const error = new Error(message);
|
|
35
|
+
error.code = 'session-sync-unavailable';
|
|
36
|
+
return error;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function isRecord(value) {
|
|
40
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function exactKeys(value, allowed, label) {
|
|
44
|
+
if (!isRecord(value)) throw invalidTarget(`${label} must be an object`);
|
|
45
|
+
const extra = Object.keys(value).find((key) => !allowed.includes(key));
|
|
46
|
+
if (extra !== undefined) throw invalidTarget(`${label} contains an unknown field`);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function requiredString(value, label) {
|
|
50
|
+
if (typeof value !== 'string' || !value || value.trim() !== value) {
|
|
51
|
+
throw invalidTarget(`${label} must be a non-empty string without surrounding whitespace`);
|
|
52
|
+
}
|
|
53
|
+
return value;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function targetName(value) {
|
|
57
|
+
if (typeof value !== 'string' || !value.trim() || value.trim().length > 80) {
|
|
58
|
+
throw invalidTarget('target.name must contain 1 to 80 characters');
|
|
59
|
+
}
|
|
60
|
+
return value.trim();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function routeWithStrings(route, fields) {
|
|
64
|
+
exactKeys(route, fields, 'route');
|
|
65
|
+
const normalized = {};
|
|
66
|
+
for (const field of fields) normalized[field] = requiredString(route[field], `route.${field}`);
|
|
67
|
+
return normalized;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function oneOf(value, choices) {
|
|
71
|
+
if (!choices.includes(value)) throw invalidTarget('target kind is not supported by this channel');
|
|
72
|
+
return value;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function normalizeRoute(channel, kind, route) {
|
|
76
|
+
switch (channel) {
|
|
77
|
+
case 'weixin':
|
|
78
|
+
oneOf(kind, ['user']);
|
|
79
|
+
return routeWithStrings(route, ['toUserId']);
|
|
80
|
+
case 'feishu':
|
|
81
|
+
oneOf(kind, ['user', 'group']);
|
|
82
|
+
if (kind === 'user' && route !== null && typeof route === 'object'
|
|
83
|
+
&& Object.hasOwn(route, 'chatId')) {
|
|
84
|
+
const normalized = routeWithStrings(route, ['openId', 'chatId']);
|
|
85
|
+
if (!/^oc_[A-Za-z0-9]+$/.test(normalized.chatId))
|
|
86
|
+
throw invalidTarget('route.chatId must identify a private Feishu/Lark chat');
|
|
87
|
+
return normalized;
|
|
88
|
+
}
|
|
89
|
+
return routeWithStrings(route, kind === 'user' ? ['openId'] : ['chatId']);
|
|
90
|
+
case 'dingtalk':
|
|
91
|
+
oneOf(kind, ['user', 'group']);
|
|
92
|
+
return routeWithStrings(route, kind === 'user' ? ['userId'] : ['openConversationId']);
|
|
93
|
+
case 'wecom':
|
|
94
|
+
oneOf(kind, ['user', 'group']);
|
|
95
|
+
return routeWithStrings(route, ['chatId']);
|
|
96
|
+
case 'wecom-app':
|
|
97
|
+
oneOf(kind, ['user']);
|
|
98
|
+
return routeWithStrings(route, ['chatId']);
|
|
99
|
+
case 'qq':
|
|
100
|
+
oneOf(kind, ['user', 'group']);
|
|
101
|
+
return routeWithStrings(route, kind === 'user' ? ['userOpenId'] : ['groupOpenId']);
|
|
102
|
+
case 'slack': {
|
|
103
|
+
oneOf(kind, ['conversation', 'thread']);
|
|
104
|
+
const normalized = routeWithStrings(
|
|
105
|
+
route,
|
|
106
|
+
kind === 'conversation' ? ['channelId'] : ['channelId', 'threadTs'],
|
|
107
|
+
);
|
|
108
|
+
return normalized;
|
|
109
|
+
}
|
|
110
|
+
case 'telegram': {
|
|
111
|
+
oneOf(kind, ['chat', 'topic']);
|
|
112
|
+
exactKeys(route, kind === 'chat' ? ['chatId'] : ['chatId', 'messageThreadId'], 'route');
|
|
113
|
+
const chatId = requiredString(route.chatId, 'route.chatId');
|
|
114
|
+
if (!/^-?\d+$/.test(chatId)) throw invalidTarget('route.chatId must be a decimal string');
|
|
115
|
+
if (kind === 'chat') return { chatId };
|
|
116
|
+
if (!Number.isSafeInteger(route.messageThreadId) || route.messageThreadId <= 0) {
|
|
117
|
+
throw invalidTarget('route.messageThreadId must be a positive integer');
|
|
118
|
+
}
|
|
119
|
+
return { chatId, messageThreadId: route.messageThreadId };
|
|
120
|
+
}
|
|
121
|
+
case 'discord':
|
|
122
|
+
oneOf(kind, ['channel']);
|
|
123
|
+
return routeWithStrings(route, ['channelId']);
|
|
124
|
+
case 'whatsapp': {
|
|
125
|
+
oneOf(kind, ['user', 'group']);
|
|
126
|
+
const normalized = routeWithStrings(route, ['jid']);
|
|
127
|
+
const valid = kind === 'user'
|
|
128
|
+
? /^\d{5,32}@(s\.whatsapp\.net|lid)$/.test(normalized.jid)
|
|
129
|
+
: /^\d{5,32}(?:-\d{1,32})?@g\.us$/.test(normalized.jid);
|
|
130
|
+
if (!valid) {
|
|
131
|
+
throw invalidTarget(`route.jid must be a ${kind} WhatsApp JID`);
|
|
132
|
+
}
|
|
133
|
+
return normalized;
|
|
134
|
+
}
|
|
135
|
+
case 'imessage':
|
|
136
|
+
oneOf(kind, ['user']);
|
|
137
|
+
return routeWithStrings(route, ['chatGuid']);
|
|
138
|
+
case 'email': {
|
|
139
|
+
oneOf(kind, ['user']);
|
|
140
|
+
const normalized = routeWithStrings(route, ['address']);
|
|
141
|
+
// Mail addresses are normalized to lower case everywhere so a target
|
|
142
|
+
// created from a mixed-case address still matches.
|
|
143
|
+
if (!/^[^\s@<>]+@[^\s@<>]+\.[^\s@<>]+$/.test(normalized.address)) {
|
|
144
|
+
throw invalidTarget('route.address must be a valid email address');
|
|
145
|
+
}
|
|
146
|
+
return { address: normalized.address.toLowerCase() };
|
|
147
|
+
}
|
|
148
|
+
case 'matrix': {
|
|
149
|
+
oneOf(kind, ['room', 'thread', 'dm']);
|
|
150
|
+
const normalized = routeWithStrings(
|
|
151
|
+
route,
|
|
152
|
+
kind === 'room' ? ['roomId'] : kind === 'thread' ? ['roomId', 'threadId'] : ['userId'],
|
|
153
|
+
);
|
|
154
|
+
if (kind === 'dm' && !/^@[^:\s]+:[^\s:]+(?::\d{1,5})?$/u.test(normalized.userId)) {
|
|
155
|
+
throw invalidTarget('route.userId must be a Matrix user id');
|
|
156
|
+
}
|
|
157
|
+
if (kind !== 'dm' && !/^[!][^:$\s]+:[^\s:$]+(?::\d{1,5})?$/.test(normalized.roomId)) {
|
|
158
|
+
throw invalidTarget('route.roomId must be a Matrix room id');
|
|
159
|
+
}
|
|
160
|
+
if (kind === 'thread' && !/^\$[^\s]+/.test(normalized.threadId)) {
|
|
161
|
+
throw invalidTarget('route.threadId must be a Matrix event id');
|
|
162
|
+
}
|
|
163
|
+
return normalized;
|
|
164
|
+
}
|
|
165
|
+
default:
|
|
166
|
+
throw new TypeError(`Unsupported delivery channel: ${channel}`);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** Strictly validate and copy one channel delivery target. */
|
|
171
|
+
export function normalizeDeliveryTarget(channel, value, { targetIdRequired = true } = {}) {
|
|
172
|
+
if (!supportsDeliveryChannel(channel)) throw new TypeError(`Unsupported delivery channel: ${channel}`);
|
|
173
|
+
exactKeys(
|
|
174
|
+
value,
|
|
175
|
+
targetIdRequired ? ['targetId', 'name', 'kind', 'route'] : ['name', 'kind', 'route'],
|
|
176
|
+
'target',
|
|
177
|
+
);
|
|
178
|
+
const normalized = {};
|
|
179
|
+
if (targetIdRequired) normalized.targetId = requiredString(value.targetId, 'target.targetId');
|
|
180
|
+
if (value.name !== undefined) normalized.name = targetName(value.name);
|
|
181
|
+
normalized.kind = requiredString(value.kind, 'target.kind');
|
|
182
|
+
normalized.route = normalizeRoute(channel, normalized.kind, value.route);
|
|
183
|
+
return normalized;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function targetWithoutSessionSync(target) {
|
|
187
|
+
return {
|
|
188
|
+
targetId: target.targetId,
|
|
189
|
+
...(target.name === undefined ? {} : { name: target.name }),
|
|
190
|
+
kind: target.kind,
|
|
191
|
+
route: target.route,
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function sessionSyncConfigs(workspaces, botId) {
|
|
196
|
+
if (typeof workspaces.listSessionSyncTargets !== 'function') return [];
|
|
197
|
+
const targets = workspaces.listSessionSyncTargets();
|
|
198
|
+
return botId === undefined ? targets : targets.filter((target) => target.botId === botId);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function stateSessions(state) {
|
|
202
|
+
if (!state || typeof state.snapshot !== 'function') return null;
|
|
203
|
+
const sessions = state.snapshot()?.sessions;
|
|
204
|
+
return sessions && typeof sessions === 'object' && !Array.isArray(sessions) ? sessions : null;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function sessionSyncView(channel, target, sessions, configured) {
|
|
208
|
+
if (configured) {
|
|
209
|
+
const valid = privateConversationKeyMatchesTarget(
|
|
210
|
+
channel,
|
|
211
|
+
configured.conversationKey,
|
|
212
|
+
target,
|
|
213
|
+
);
|
|
214
|
+
if (!valid || !sessions) return { enabled: true, state: 'unavailable' };
|
|
215
|
+
return {
|
|
216
|
+
enabled: true,
|
|
217
|
+
state: typeof sessions[configured.conversationKey] === 'string'
|
|
218
|
+
&& Boolean(sessions[configured.conversationKey])
|
|
219
|
+
? 'active'
|
|
220
|
+
: 'waiting',
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
const conversationKey = sessions
|
|
224
|
+
? resolvePrivateConversationKey(channel, sessions, target)
|
|
225
|
+
: null;
|
|
226
|
+
return { enabled: false, state: conversationKey ? 'off' : 'unavailable' };
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** Bind one channel's existing workspace store and unwrapped controller to DeliveryService. */
|
|
230
|
+
export function createDeliveryAdapter({ channel, workspaces, coreController, stateFor }) {
|
|
231
|
+
if (!supportsDeliveryChannel(channel)) throw new TypeError(`Unsupported delivery channel: ${channel}`);
|
|
232
|
+
if (!workspaces || typeof workspaces !== 'object') {
|
|
233
|
+
throw new TypeError('delivery adapter requires a workspace store');
|
|
234
|
+
}
|
|
235
|
+
if (!coreController || typeof coreController !== 'object') {
|
|
236
|
+
throw new TypeError('delivery adapter requires a proactive text controller');
|
|
237
|
+
}
|
|
238
|
+
if (typeof stateFor !== 'function') {
|
|
239
|
+
throw new TypeError('delivery adapter requires a bot state getter');
|
|
240
|
+
}
|
|
241
|
+
return Object.freeze({
|
|
242
|
+
channel,
|
|
243
|
+
consumeInbound: (botId, options) => {
|
|
244
|
+
if (typeof coreController.consumeInbound !== 'function')
|
|
245
|
+
throw Object.assign(new Error('capability-unavailable'), { code: 'capability-unavailable' });
|
|
246
|
+
return coreController.consumeInbound(botId, options);
|
|
247
|
+
},
|
|
248
|
+
approvalCardChecked: (botId, route, card, options) => {
|
|
249
|
+
if (channel !== 'feishu' || typeof coreController.approvalCardChecked !== 'function')
|
|
250
|
+
throw Object.assign(new Error('capability-unavailable'), { code: 'capability-unavailable' });
|
|
251
|
+
return coreController.approvalCardChecked(botId, route, card, options);
|
|
252
|
+
},
|
|
253
|
+
historyChecked: (botId, route, query, options) => {
|
|
254
|
+
if (typeof coreController.historyChecked !== 'function')
|
|
255
|
+
throw Object.assign(new Error('capability-unavailable'), { code: 'capability-unavailable' });
|
|
256
|
+
return coreController.historyChecked(botId, route, query, options);
|
|
257
|
+
},
|
|
258
|
+
externalFileChecked: (botId, route, value, options) => {
|
|
259
|
+
if (typeof coreController.externalFileChecked !== 'function')
|
|
260
|
+
throw Object.assign(new Error('capability-unavailable'), { code: 'capability-unavailable' });
|
|
261
|
+
return coreController.externalFileChecked(botId, route, value, options);
|
|
262
|
+
},
|
|
263
|
+
qualifyReplyChecked: (botId, route, options) => {
|
|
264
|
+
if (typeof coreController.qualifyReplyChecked !== 'function')
|
|
265
|
+
throw Object.assign(new Error('capability-unavailable'), { code: 'capability-unavailable' });
|
|
266
|
+
return coreController.qualifyReplyChecked(botId, route, options);
|
|
267
|
+
},
|
|
268
|
+
replyChecked: (botId, route, text, options) => {
|
|
269
|
+
if (typeof coreController.replyChecked !== 'function')
|
|
270
|
+
throw Object.assign(new Error('capability-unavailable'), { code: 'capability-unavailable' });
|
|
271
|
+
return coreController.replyChecked(botId, route, text, options);
|
|
272
|
+
},
|
|
273
|
+
beginTypingChecked: (botId, route, options) => {
|
|
274
|
+
if (typeof coreController.beginTypingChecked !== 'function')
|
|
275
|
+
throw Object.assign(new Error('capability-unavailable'), { code: 'capability-unavailable' });
|
|
276
|
+
return coreController.beginTypingChecked(botId, route, options);
|
|
277
|
+
},
|
|
278
|
+
describeAccount: (botId) => {
|
|
279
|
+
if (typeof coreController.describeDeliveryAccount !== 'function') {
|
|
280
|
+
const error = new Error('Verified account capability unavailable');
|
|
281
|
+
error.code = 'capability-unavailable';
|
|
282
|
+
throw error;
|
|
283
|
+
}
|
|
284
|
+
return coreController.describeDeliveryAccount(botId);
|
|
285
|
+
},
|
|
286
|
+
ownsBot: (botId) => workspaces.has(botId),
|
|
287
|
+
listBots: () => workspaces.listBotIds(),
|
|
288
|
+
async listTargets(botId) {
|
|
289
|
+
let targets = [...workspaces.listDeliveryTargets(botId)];
|
|
290
|
+
if (channel === 'weixin' && typeof coreController.describeDeliveryAccount === 'function') {
|
|
291
|
+
const paired = await coreController.describeDeliveryAccount(botId);
|
|
292
|
+
targets = targets.filter(target => target.kind === 'user' && target.route?.toUserId === paired?.account?.ownerUserId);
|
|
293
|
+
if (paired?.account?.ownerUserId && !targets.some(target => target.kind === 'user' && target.route?.toUserId === paired.account.ownerUserId))
|
|
294
|
+
targets.push({ targetId: 'paired-owner', name: '扫码绑定者私聊', kind: 'user', route: { toUserId: paired.account.ownerUserId } });
|
|
295
|
+
}
|
|
296
|
+
const configured = new Map(
|
|
297
|
+
sessionSyncConfigs(workspaces, botId).map((target) => [target.targetId, target]),
|
|
298
|
+
);
|
|
299
|
+
let sessions = null;
|
|
300
|
+
try {
|
|
301
|
+
sessions = stateSessions(await stateFor(botId));
|
|
302
|
+
} catch {
|
|
303
|
+
// Target CRUD remains usable when its persisted conversation state cannot be read.
|
|
304
|
+
}
|
|
305
|
+
return targets.map((target) => ({
|
|
306
|
+
...target,
|
|
307
|
+
sessionSync: sessionSyncView(channel, target, sessions, configured.get(target.targetId)),
|
|
308
|
+
}));
|
|
309
|
+
},
|
|
310
|
+
async listSuggestions(botId) {
|
|
311
|
+
const state = await stateFor(botId);
|
|
312
|
+
if (!state || typeof state.snapshot !== 'function') {
|
|
313
|
+
throw new TypeError('delivery suggestion state cannot be inspected');
|
|
314
|
+
}
|
|
315
|
+
const suggestions = deliverySuggestionsFromSessions(channel, state.snapshot()?.sessions);
|
|
316
|
+
return suggestions.map((suggestion) => normalizeDeliveryTarget(
|
|
317
|
+
channel,
|
|
318
|
+
suggestion,
|
|
319
|
+
{ targetIdRequired: false },
|
|
320
|
+
));
|
|
321
|
+
},
|
|
322
|
+
createTarget: (botId, target) => workspaces.createDeliveryTarget(
|
|
323
|
+
botId,
|
|
324
|
+
normalizeDeliveryTarget(channel, target),
|
|
325
|
+
),
|
|
326
|
+
updateTarget: (botId, targetId, replacement) => workspaces.updateDeliveryTarget(
|
|
327
|
+
botId,
|
|
328
|
+
targetId,
|
|
329
|
+
normalizeDeliveryTarget(channel, replacement, { targetIdRequired: false }),
|
|
330
|
+
),
|
|
331
|
+
deleteTarget: (botId, targetId) => workspaces.deleteDeliveryTarget(botId, targetId),
|
|
332
|
+
async sendText(botId, target, text, options = {}) {
|
|
333
|
+
const normalized = normalizeDeliveryTarget(channel, targetWithoutSessionSync(target));
|
|
334
|
+
if (typeof coreController.sendProactiveText !== 'function') {
|
|
335
|
+
throw new TypeError('delivery controller cannot send proactive text');
|
|
336
|
+
}
|
|
337
|
+
const result = await coreController.sendProactiveText(botId, normalized, text, options);
|
|
338
|
+
return options.receipt === true ? result : { sent: true };
|
|
339
|
+
},
|
|
340
|
+
async setSessionSync(botId, targetId, enabled) {
|
|
341
|
+
if (typeof workspaces.setDeliveryTargetSessionSync !== 'function') {
|
|
342
|
+
throw sessionSyncUnavailable();
|
|
343
|
+
}
|
|
344
|
+
const target = workspaces.deliveryTargetFor(botId, targetId);
|
|
345
|
+
if (!target) {
|
|
346
|
+
const error = new Error('Unknown target');
|
|
347
|
+
error.code = 'unknown-target';
|
|
348
|
+
throw error;
|
|
349
|
+
}
|
|
350
|
+
if (!enabled) {
|
|
351
|
+
await workspaces.setDeliveryTargetSessionSync(botId, targetId, null);
|
|
352
|
+
let sessions = null;
|
|
353
|
+
try {
|
|
354
|
+
sessions = stateSessions(await stateFor(botId));
|
|
355
|
+
} catch {
|
|
356
|
+
// The setting is already safely disabled; only its availability is unknown.
|
|
357
|
+
}
|
|
358
|
+
return sessionSyncView(channel, target, sessions, null);
|
|
359
|
+
}
|
|
360
|
+
const sessions = stateSessions(await stateFor(botId));
|
|
361
|
+
const conversationKey = resolvePrivateConversationKey(channel, sessions, target);
|
|
362
|
+
if (!conversationKey) throw sessionSyncUnavailable();
|
|
363
|
+
await workspaces.setDeliveryTargetSessionSync(botId, targetId, conversationKey);
|
|
364
|
+
return { enabled: true, state: 'active' };
|
|
365
|
+
},
|
|
366
|
+
async listSessionSyncTargets(sessionId) {
|
|
367
|
+
if (typeof sessionId !== 'string' || !sessionId) {
|
|
368
|
+
throw new TypeError('sessionId is required');
|
|
369
|
+
}
|
|
370
|
+
const matches = [];
|
|
371
|
+
for (const configured of sessionSyncConfigs(workspaces)) {
|
|
372
|
+
const target = workspaces.deliveryTargetFor(configured.botId, configured.targetId);
|
|
373
|
+
if (!target || !privateConversationKeyMatchesTarget(
|
|
374
|
+
channel,
|
|
375
|
+
configured.conversationKey,
|
|
376
|
+
target,
|
|
377
|
+
)) continue;
|
|
378
|
+
let sessions;
|
|
379
|
+
try {
|
|
380
|
+
sessions = stateSessions(await stateFor(configured.botId));
|
|
381
|
+
} catch {
|
|
382
|
+
continue;
|
|
383
|
+
}
|
|
384
|
+
if (sessions?.[configured.conversationKey] === sessionId) {
|
|
385
|
+
matches.push({ botId: configured.botId, targetId: configured.targetId });
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
return matches;
|
|
389
|
+
},
|
|
390
|
+
async sendSessionSyncText(botId, targetId, sessionId, text, options = {}) {
|
|
391
|
+
if (typeof text !== 'string' || !text.trim()) throw new TypeError('text is required');
|
|
392
|
+
const configured = sessionSyncConfigs(workspaces, botId)
|
|
393
|
+
.find((target) => target.targetId === targetId);
|
|
394
|
+
const target = workspaces.deliveryTargetFor(botId, targetId);
|
|
395
|
+
if (!configured || !target || !privateConversationKeyMatchesTarget(
|
|
396
|
+
channel,
|
|
397
|
+
configured.conversationKey,
|
|
398
|
+
target,
|
|
399
|
+
)) throw sessionSyncUnavailable('Session sync target is no longer enabled');
|
|
400
|
+
const sessions = stateSessions(await stateFor(botId));
|
|
401
|
+
if (sessions?.[configured.conversationKey] !== sessionId) {
|
|
402
|
+
throw sessionSyncUnavailable('Session sync target is no longer bound to this Session');
|
|
403
|
+
}
|
|
404
|
+
if (typeof coreController.sendProactiveText !== 'function') {
|
|
405
|
+
throw new TypeError('delivery controller cannot send proactive text');
|
|
406
|
+
}
|
|
407
|
+
await coreController.sendProactiveText(botId, target, text, options);
|
|
408
|
+
return { sent: true };
|
|
409
|
+
},
|
|
410
|
+
});
|
|
411
|
+
}
|