@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,285 @@
|
|
|
1
|
+
import { SET_ALIAS_ENDPOINT, validAliasPayload } from '../shared/bot-alias-rpc.mjs';
|
|
2
|
+
import { registerManagementRpc } from '../../../management-rpc.mjs';
|
|
3
|
+
import QRCode from 'qrcode';
|
|
4
|
+
import { createWeixinDiagnostics, weixinStageError } from '../../../../src/channels/weixin/connection-error.mjs';
|
|
5
|
+
import { SET_CONTEXT_ENHANCEMENT_ENDPOINT, validContextEnhancementPayload } from '../shared/context-enhancement-rpc.mjs';
|
|
6
|
+
import { SET_ACCESS_POLICY_ENDPOINT, validAccessPolicyPayload } from '../shared/access-policy-rpc.mjs';
|
|
7
|
+
import { resolveRpcAuthority } from '../../rpc-authority.mjs';
|
|
8
|
+
import {
|
|
9
|
+
SET_WORKSPACE_ENDPOINT,
|
|
10
|
+
validWorkspacePayload,
|
|
11
|
+
} from '../shared/workspace-rpc.mjs';
|
|
12
|
+
import {
|
|
13
|
+
SET_AGENT_PRESET_ENDPOINT,
|
|
14
|
+
validAgentPresetPayload,
|
|
15
|
+
} from '../shared/agent-preset-rpc.mjs';
|
|
16
|
+
import { SET_MODEL_ENDPOINT, validModelPayload } from '../shared/model-setting-rpc.mjs';
|
|
17
|
+
import {
|
|
18
|
+
connectionTestTargetUnavailable,
|
|
19
|
+
publicConnectionTestResult,
|
|
20
|
+
} from '../../../../src/channels/shared/connection-test.mjs';
|
|
21
|
+
|
|
22
|
+
export const WEIXIN_RPC_CHANNEL = '/weixin';
|
|
23
|
+
export const WEIXIN_ENDPOINTS = Object.freeze({
|
|
24
|
+
status: 'connection.status',
|
|
25
|
+
beginProvisioning: 'provision.begin',
|
|
26
|
+
pollProvisioning: 'provision.poll',
|
|
27
|
+
submitVerification: 'provision.verify',
|
|
28
|
+
cancelProvisioning: 'provision.cancel',
|
|
29
|
+
reconnectBot: 'bot.reconnect',
|
|
30
|
+
deleteBot: 'bot.delete',
|
|
31
|
+
setWorkspace: SET_WORKSPACE_ENDPOINT,
|
|
32
|
+
setModel: SET_MODEL_ENDPOINT,
|
|
33
|
+
setAgentPreset: SET_AGENT_PRESET_ENDPOINT,
|
|
34
|
+
setContextEnhancement: SET_CONTEXT_ENHANCEMENT_ENDPOINT,
|
|
35
|
+
setAccessPolicy: SET_ACCESS_POLICY_ENDPOINT,
|
|
36
|
+
setAlias: SET_ALIAS_ENDPOINT,
|
|
37
|
+
});
|
|
38
|
+
export const WEIXIN_RPC_ENDPOINTS = Object.freeze(Object.values(WEIXIN_ENDPOINTS));
|
|
39
|
+
|
|
40
|
+
function isRecord(value) {
|
|
41
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function exactKeys(value, allowed) {
|
|
45
|
+
return isRecord(value) && Object.keys(value).every((key) => allowed.includes(key));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function validId(value) {
|
|
49
|
+
return typeof value === 'string' && /^[A-Za-z0-9_-]{1,128}$/.test(value);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function payloadFailure(endpoint, payload) {
|
|
53
|
+
if (!isRecord(payload)) return 'Payload must be an object.';
|
|
54
|
+
if (endpoint === WEIXIN_ENDPOINTS.status) {
|
|
55
|
+
return exactKeys(payload, []) ? null : 'connection.status does not accept fields.';
|
|
56
|
+
}
|
|
57
|
+
if (endpoint === WEIXIN_ENDPOINTS.beginProvisioning) {
|
|
58
|
+
return exactKeys(payload, ['locale']) && (payload.locale === undefined || payload.locale === 'zh-CN')
|
|
59
|
+
? null
|
|
60
|
+
: 'provision.begin received unsupported fields.';
|
|
61
|
+
}
|
|
62
|
+
if ([WEIXIN_ENDPOINTS.pollProvisioning, WEIXIN_ENDPOINTS.cancelProvisioning].includes(endpoint)) {
|
|
63
|
+
return exactKeys(payload, ['attemptId']) && validId(payload.attemptId)
|
|
64
|
+
? null
|
|
65
|
+
: `${endpoint} requires an attemptId.`;
|
|
66
|
+
}
|
|
67
|
+
if (endpoint === WEIXIN_ENDPOINTS.submitVerification) {
|
|
68
|
+
return exactKeys(payload, ['attemptId', 'verifyCode'])
|
|
69
|
+
&& validId(payload.attemptId)
|
|
70
|
+
&& typeof payload.verifyCode === 'string'
|
|
71
|
+
&& /^\d{4,8}$/.test(payload.verifyCode)
|
|
72
|
+
? null
|
|
73
|
+
: 'provision.verify requires an attemptId and a 4-to-8-digit code.';
|
|
74
|
+
}
|
|
75
|
+
if (endpoint === WEIXIN_ENDPOINTS.reconnectBot) {
|
|
76
|
+
return exactKeys(payload, ['botId', 'sendTest'])
|
|
77
|
+
&& validId(payload.botId)
|
|
78
|
+
&& (payload.sendTest === undefined || payload.sendTest === true)
|
|
79
|
+
? null
|
|
80
|
+
: 'bot.reconnect requires a botId and optional sendTest=true.';
|
|
81
|
+
}
|
|
82
|
+
if (endpoint === WEIXIN_ENDPOINTS.deleteBot) {
|
|
83
|
+
return exactKeys(payload, ['botId', 'confirm']) && validId(payload.botId) && payload.confirm === true
|
|
84
|
+
? null
|
|
85
|
+
: 'bot.delete requires a botId and confirm=true.';
|
|
86
|
+
}
|
|
87
|
+
if (endpoint === WEIXIN_ENDPOINTS.setWorkspace) {
|
|
88
|
+
return validWorkspacePayload(payload)
|
|
89
|
+
? null : '请输入工作区绝对路径。';
|
|
90
|
+
}
|
|
91
|
+
if (endpoint === WEIXIN_ENDPOINTS.setModel) {
|
|
92
|
+
return validModelPayload(payload) ? null : '请选择有效模型。';
|
|
93
|
+
}
|
|
94
|
+
if (endpoint === WEIXIN_ENDPOINTS.setAgentPreset) {
|
|
95
|
+
return validAgentPresetPayload(payload)
|
|
96
|
+
? null : '请选择 Agent Preset。';
|
|
97
|
+
}
|
|
98
|
+
if (endpoint === WEIXIN_ENDPOINTS.setContextEnhancement) {
|
|
99
|
+
return validContextEnhancementPayload(payload)
|
|
100
|
+
? null : '请提交有效的上下文增强设置。';
|
|
101
|
+
}
|
|
102
|
+
if (endpoint === WEIXIN_ENDPOINTS.setAccessPolicy) {
|
|
103
|
+
return validAccessPolicyPayload(payload)
|
|
104
|
+
? null : '请提交有效的访问设置。';
|
|
105
|
+
}
|
|
106
|
+
if (endpoint === WEIXIN_ENDPOINTS.setAlias) {
|
|
107
|
+
return validAliasPayload(payload)
|
|
108
|
+
? null : '请输入有效的别名(最多 80 个字符)。';
|
|
109
|
+
}
|
|
110
|
+
return 'Unknown Weixin endpoint.';
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function badRequest(message) {
|
|
114
|
+
return { ok: false, error: { code: 'bad-request', message } };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function cancelled() {
|
|
118
|
+
return { ok: false, error: { code: 'cancelled', message: 'The request was cancelled.' } };
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async function qrDataUrl(value) {
|
|
122
|
+
return QRCode.toDataURL(value, {
|
|
123
|
+
type: 'image/png',
|
|
124
|
+
errorCorrectionLevel: 'M',
|
|
125
|
+
margin: 2,
|
|
126
|
+
width: 320,
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
async function withEncodedQr(value, encodeQr) {
|
|
131
|
+
if (!value || !value.verificationUrl) return value;
|
|
132
|
+
return {
|
|
133
|
+
...value,
|
|
134
|
+
qrCodeDataUrl: await encodeQr(value.verificationUrl),
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
async function publicStatus(status, encodeQr) {
|
|
139
|
+
let safe;
|
|
140
|
+
try { safe = structuredClone(status); }
|
|
141
|
+
catch (error) { throw weixinStageError('status-read-failed', error); }
|
|
142
|
+
if (safe.provisioning) safe.provisioning = await withEncodedQr(safe.provisioning, encodeQr);
|
|
143
|
+
return safe;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function assertController(controller) {
|
|
147
|
+
if (!controller
|
|
148
|
+
|| typeof controller.status !== 'function'
|
|
149
|
+
|| typeof controller.startProvisioning !== 'function'
|
|
150
|
+
|| typeof controller.registrationStatus !== 'function'
|
|
151
|
+
|| typeof controller.submitVerification !== 'function'
|
|
152
|
+
|| typeof controller.cancelProvisioning !== 'function'
|
|
153
|
+
|| typeof controller.reconnectBot !== 'function'
|
|
154
|
+
|| typeof controller.deleteBot !== 'function') {
|
|
155
|
+
throw new TypeError('A complete Weixin controller is required');
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export function createWeixinRpcHandler(controller, { encodeQr = qrDataUrl, logger = console, diagnostics = createWeixinDiagnostics({ logger }) } = {}) {
|
|
160
|
+
assertController(controller);
|
|
161
|
+
const qrCache = new Map();
|
|
162
|
+
const cachedEncode = (url) => {
|
|
163
|
+
let encoded = qrCache.get(url);
|
|
164
|
+
if (!encoded) {
|
|
165
|
+
if (qrCache.size >= 16) qrCache.delete(qrCache.keys().next().value);
|
|
166
|
+
encoded = Promise.resolve().then(() => encodeQr(url)).catch(error => {
|
|
167
|
+
qrCache.delete(url);
|
|
168
|
+
throw weixinStageError('qr-encode-failed', error);
|
|
169
|
+
});
|
|
170
|
+
qrCache.set(url, encoded);
|
|
171
|
+
}
|
|
172
|
+
return encoded;
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
return async (endpoint, payload, signal) => {
|
|
176
|
+
if (signal?.aborted) return cancelled();
|
|
177
|
+
if (!WEIXIN_RPC_ENDPOINTS.includes(endpoint)) return badRequest('Unknown Weixin endpoint.');
|
|
178
|
+
const invalid = payloadFailure(endpoint, payload);
|
|
179
|
+
if (invalid) return badRequest(invalid);
|
|
180
|
+
|
|
181
|
+
try {
|
|
182
|
+
let value;
|
|
183
|
+
if (endpoint === WEIXIN_ENDPOINTS.status) {
|
|
184
|
+
value = await publicStatus(await controller.status(), cachedEncode);
|
|
185
|
+
} else if (endpoint === WEIXIN_ENDPOINTS.beginProvisioning) {
|
|
186
|
+
const started = await controller.startProvisioning();
|
|
187
|
+
if (signal?.aborted) {
|
|
188
|
+
await controller.cancelProvisioning(started.attemptId);
|
|
189
|
+
return cancelled();
|
|
190
|
+
}
|
|
191
|
+
value = await withEncodedQr(started, cachedEncode);
|
|
192
|
+
} else if (endpoint === WEIXIN_ENDPOINTS.pollProvisioning) {
|
|
193
|
+
const current = await controller.registrationStatus(payload.attemptId);
|
|
194
|
+
if (!current) throw weixinStageError('provision-attempt-not-found', undefined, 'qr.poll');
|
|
195
|
+
value = await withEncodedQr(current, cachedEncode);
|
|
196
|
+
} else if (endpoint === WEIXIN_ENDPOINTS.submitVerification) {
|
|
197
|
+
value = await withEncodedQr(
|
|
198
|
+
await controller.submitVerification(payload.attemptId, payload.verifyCode),
|
|
199
|
+
cachedEncode,
|
|
200
|
+
);
|
|
201
|
+
} else if (endpoint === WEIXIN_ENDPOINTS.cancelProvisioning) {
|
|
202
|
+
value = await controller.cancelProvisioning(payload.attemptId);
|
|
203
|
+
if (!value) throw weixinStageError('provision-attempt-not-found', undefined, 'qr.cancel');
|
|
204
|
+
} else if (endpoint === WEIXIN_ENDPOINTS.reconnectBot) {
|
|
205
|
+
const snapshot = await controller.reconnectBot(payload.botId);
|
|
206
|
+
if (signal?.aborted) return cancelled();
|
|
207
|
+
let testMessage;
|
|
208
|
+
if (payload.sendTest === true) {
|
|
209
|
+
const connected = snapshot?.bots?.some(
|
|
210
|
+
(bot) => bot?.botId === payload.botId && bot?.connected === true,
|
|
211
|
+
);
|
|
212
|
+
if (!connected || typeof controller.sendConnectionTest !== 'function') {
|
|
213
|
+
testMessage = publicConnectionTestResult(
|
|
214
|
+
connectionTestTargetUnavailable('微信机器人'),
|
|
215
|
+
);
|
|
216
|
+
} else {
|
|
217
|
+
try {
|
|
218
|
+
await controller.sendConnectionTest(payload.botId);
|
|
219
|
+
testMessage = publicConnectionTestResult();
|
|
220
|
+
} catch (error) {
|
|
221
|
+
testMessage = publicConnectionTestResult(error);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
value = await publicStatus({ ...snapshot, ...(testMessage ? { testMessage } : {}) }, cachedEncode);
|
|
226
|
+
} else if (endpoint === WEIXIN_ENDPOINTS.setWorkspace) {
|
|
227
|
+
if (typeof controller.updateWorkspace !== 'function') throw new Error('Workspace update is unavailable');
|
|
228
|
+
value = await publicStatus(
|
|
229
|
+
await controller.updateWorkspace(payload.botId, payload.workspace),
|
|
230
|
+
cachedEncode,
|
|
231
|
+
);
|
|
232
|
+
} else if (endpoint === WEIXIN_ENDPOINTS.setModel) {
|
|
233
|
+
if (typeof controller.updateModel !== 'function') throw new Error('Model update is unavailable');
|
|
234
|
+
value = await publicStatus(
|
|
235
|
+
await controller.updateModel(payload.botId, payload.model),
|
|
236
|
+
cachedEncode,
|
|
237
|
+
);
|
|
238
|
+
} else if (endpoint === WEIXIN_ENDPOINTS.setContextEnhancement) {
|
|
239
|
+
if (typeof controller.updateContextEnhancement !== 'function') throw new Error('Context enhancement update is unavailable');
|
|
240
|
+
value = await controller.updateContextEnhancement(
|
|
241
|
+
payload.botId, payload.config, (status) => publicStatus(status, cachedEncode),
|
|
242
|
+
);
|
|
243
|
+
} else if (endpoint === WEIXIN_ENDPOINTS.setAlias) {
|
|
244
|
+
if (typeof controller.updateAlias !== 'function') throw new Error('Alias update is unavailable');
|
|
245
|
+
value = await controller.updateAlias(
|
|
246
|
+
payload.botId, payload.alias, (status) => publicStatus(status, cachedEncode),
|
|
247
|
+
);
|
|
248
|
+
} else if (endpoint === WEIXIN_ENDPOINTS.setAccessPolicy) {
|
|
249
|
+
if (typeof controller.updateAccessPolicy !== 'function') throw new Error('Access policy update is unavailable');
|
|
250
|
+
value = await controller.updateAccessPolicy(
|
|
251
|
+
payload.botId, payload.policy, (status) => publicStatus(status, cachedEncode),
|
|
252
|
+
);
|
|
253
|
+
} else if (endpoint === WEIXIN_ENDPOINTS.setAgentPreset) {
|
|
254
|
+
if (typeof controller.updateAgentPreset !== 'function') throw new Error('Agent preset update is unavailable');
|
|
255
|
+
value = await publicStatus(
|
|
256
|
+
await controller.updateAgentPreset(payload.botId, payload.agentPreset),
|
|
257
|
+
cachedEncode,
|
|
258
|
+
);
|
|
259
|
+
} else {
|
|
260
|
+
value = await publicStatus(await controller.deleteBot(payload.botId), cachedEncode);
|
|
261
|
+
}
|
|
262
|
+
return signal?.aborted ? cancelled() : { ok: true, value };
|
|
263
|
+
} catch (error) {
|
|
264
|
+
if (signal?.aborted) return cancelled();
|
|
265
|
+
const context = {
|
|
266
|
+
'connection.status': ['status.read', 'status-read-failed'],
|
|
267
|
+
'provision.begin': ['qr.begin', 'qr-start-failed'],
|
|
268
|
+
'provision.poll': ['qr.poll'], 'provision.verify': ['qr.verify'], 'provision.cancel': ['qr.cancel'],
|
|
269
|
+
'bot.reconnect': ['connection.start', 'connection-start-failed'],
|
|
270
|
+
'bot.delete': ['account.remove'], 'bot.workspace.set': ['workspace.write', 'workspace-save-failed'],
|
|
271
|
+
}[endpoint] ?? ['workspace.write', 'workspace-save-failed'];
|
|
272
|
+
return { ok: false, error: diagnostics.report(error, {
|
|
273
|
+
operation: endpoint, stage: context[0], code: context[1], botId: payload.botId,
|
|
274
|
+
}).publicError };
|
|
275
|
+
}
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
export function installWeixinRpc(ctx, controller, options, authority) {
|
|
280
|
+
return registerManagementRpc(ctx,
|
|
281
|
+
WEIXIN_RPC_CHANNEL,
|
|
282
|
+
createWeixinRpcHandler(controller, options),
|
|
283
|
+
{ authority: resolveRpcAuthority(authority) },
|
|
284
|
+
);
|
|
285
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { createProductionController } from './production.mjs';
|
|
2
|
+
import { createWhatsappRpcHandler, installWhatsappRpc, WHATSAPP_RPC_CHANNEL } from './rpc.mjs';
|
|
3
|
+
import { installProductionChannel } from '../shared/startup.mjs';
|
|
4
|
+
|
|
5
|
+
export const name = 'dsh-im-whatsapp-host';
|
|
6
|
+
export const inject = ['connection', 'typertGateway'];
|
|
7
|
+
|
|
8
|
+
export async function apply(ctx, config = {}) {
|
|
9
|
+
if (config?.controller) {
|
|
10
|
+
return installWhatsappRpc(ctx, config.controller, config.rpcOptions, config.rpcAuthority);
|
|
11
|
+
}
|
|
12
|
+
return installProductionChannel(ctx, config, {
|
|
13
|
+
channel: 'whatsapp',
|
|
14
|
+
rpcChannel: WHATSAPP_RPC_CHANNEL,
|
|
15
|
+
createProduction: () => createProductionController(ctx, config, config.internals ?? {}),
|
|
16
|
+
createHandler: controller => createWhatsappRpcHandler(controller, config.rpcOptions),
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function createWhatsappHostPlugin(config) {
|
|
21
|
+
return Object.freeze({ name, inject, apply: (ctx) => apply(ctx, config) });
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export { createProductionController } from './production.mjs';
|
|
25
|
+
export {
|
|
26
|
+
WHATSAPP_ENDPOINTS,
|
|
27
|
+
WHATSAPP_RPC_CHANNEL,
|
|
28
|
+
WHATSAPP_RPC_ENDPOINTS,
|
|
29
|
+
createWhatsappRpcHandler,
|
|
30
|
+
installWhatsappRpc,
|
|
31
|
+
} from './rpc.mjs';
|
|
32
|
+
export { WhatsappController } from '../../../../src/channels/whatsapp/whatsapp-controller.mjs';
|
|
33
|
+
export { WhatsappRuntime } from '../../../../src/channels/whatsapp/whatsapp-runtime.mjs';
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import { getImageInputSettingsStore } from '../../../../src/channels/shared/image-input-settings-store.mjs';
|
|
2
|
+
import { rm, unlink } from 'node:fs/promises';
|
|
3
|
+
import { homedir } from 'node:os';
|
|
4
|
+
import { join, resolve } from 'node:path';
|
|
5
|
+
|
|
6
|
+
import { WhatsappConfigStore } from '../../../../src/channels/whatsapp/config-store.mjs';
|
|
7
|
+
import { WhatsappHarnessClient } from '../../../../src/channels/whatsapp/harness-client.mjs';
|
|
8
|
+
import { WhatsappStateStore } from '../../../../src/channels/whatsapp/state-store.mjs';
|
|
9
|
+
import { WhatsappController } from '../../../../src/channels/whatsapp/whatsapp-controller.mjs';
|
|
10
|
+
import {
|
|
11
|
+
WhatsappRuntime,
|
|
12
|
+
whatsappAccessPolicyIdsEqual,
|
|
13
|
+
} from '../../../../src/channels/whatsapp/whatsapp-runtime.mjs';
|
|
14
|
+
import { createWhatsappWebSession } from '../../../../src/channels/whatsapp/whatsapp-web-session.mjs';
|
|
15
|
+
import {
|
|
16
|
+
BotWorkspaceStore,
|
|
17
|
+
createBotWorkspaceScope,
|
|
18
|
+
createWorkspaceAwareController,
|
|
19
|
+
observeBotWorkspaceRemovals,
|
|
20
|
+
} from '../../../../src/channels/shared/bot-workspace-store.mjs';
|
|
21
|
+
import { prepareBotWorkspace } from '../../../../src/channels/shared/default-workspace.mjs';
|
|
22
|
+
import { listAgentPresetCatalog } from '../../../../src/channels/shared/agent-preset.mjs';
|
|
23
|
+
import { listModelCatalog } from '../../../../src/channels/shared/model-setting.mjs';
|
|
24
|
+
import { createDeliveryAdapter } from '../../delivery-adapter.mjs';
|
|
25
|
+
import { createTokenConnectionSupervisor } from '../shared/connection-supervisor.mjs';
|
|
26
|
+
import { createHarnessCommandExecutor } from '../../harness-command-executor.mjs';
|
|
27
|
+
import { harnessConnection } from '../../harness-connection.mjs';
|
|
28
|
+
import { createHarnessSessionExecutors } from '../../harness-session-coordinator.mjs';
|
|
29
|
+
import {
|
|
30
|
+
getInboundTtlRuntime,
|
|
31
|
+
registerInboundTtlWorkspaces,
|
|
32
|
+
} from '../../inbound-ttl-runtime.mjs';
|
|
33
|
+
import {
|
|
34
|
+
accessPolicyProvider,
|
|
35
|
+
initialAccessPolicyFor,
|
|
36
|
+
} from '../shared/access-policy-production.mjs';
|
|
37
|
+
|
|
38
|
+
const AUTH_DIRECTORY_PATTERN = /^[a-f0-9-]{36}$/;
|
|
39
|
+
|
|
40
|
+
function pluginPaths(config) {
|
|
41
|
+
const dshHome = resolve(config.dshHome ?? process.env.DSH_HOME ?? join(homedir(), '.dsh'));
|
|
42
|
+
const root = resolve(config.dataDir ?? join(dshHome, 'integrations', 'dsh-whatsapp'));
|
|
43
|
+
const authRoot = resolve(config.authDir ?? join(root, 'auth'));
|
|
44
|
+
const authPath = (name) => {
|
|
45
|
+
if (!AUTH_DIRECTORY_PATTERN.test(name ?? '')) throw new TypeError('Invalid WhatsApp auth directory');
|
|
46
|
+
return resolve(authRoot, name);
|
|
47
|
+
};
|
|
48
|
+
return {
|
|
49
|
+
config: resolve(config.configPath ?? join(root, 'config.json')),
|
|
50
|
+
bots: resolve(config.botsDir ?? join(root, 'bots')),
|
|
51
|
+
workspaces: resolve(config.workspacesPath ?? join(root, 'workspaces.json')),
|
|
52
|
+
authPath,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export async function createProductionController(ctx, config = {}, internals = {}) {
|
|
57
|
+
const connection = harnessConnection(ctx, config);
|
|
58
|
+
const logger = typeof ctx.logger === 'function'
|
|
59
|
+
? ctx.logger('dsh-im:whatsapp') : (ctx.logger ?? console);
|
|
60
|
+
const agentPresetCatalog = () => listAgentPresetCatalog(ctx);
|
|
61
|
+
const ConfigStore = internals.ConfigStore ?? WhatsappConfigStore;
|
|
62
|
+
const StateStore = internals.StateStore ?? WhatsappStateStore;
|
|
63
|
+
const Harness = internals.HarnessClient ?? WhatsappHarnessClient;
|
|
64
|
+
const Controller = internals.Controller ?? WhatsappController;
|
|
65
|
+
const Runtime = internals.Runtime ?? WhatsappRuntime;
|
|
66
|
+
const createSession = internals.createSession ?? createWhatsappWebSession;
|
|
67
|
+
const createSupervisor = internals.createConnectionSupervisor ?? createTokenConnectionSupervisor;
|
|
68
|
+
const paths = pluginPaths(config);
|
|
69
|
+
const configStore = await new ConfigStore(paths.config).load();
|
|
70
|
+
const { defaultWorkspace, ungroupedWorkspace } = await prepareBotWorkspace(config);
|
|
71
|
+
const WorkspaceStore = internals.WorkspaceStore ?? BotWorkspaceStore;
|
|
72
|
+
const workspaces = internals.workspaces
|
|
73
|
+
?? await new WorkspaceStore(paths.workspaces, { defaultWorkspace, ungroupedWorkspace }).load();
|
|
74
|
+
const configuredBots = configStore.list();
|
|
75
|
+
await workspaces.reconcile(configuredBots.map((bot) => bot.botId));
|
|
76
|
+
await Promise.all(configuredBots.map((bot) => workspaces.ensure(bot.botId, {
|
|
77
|
+
defaultAgentPreset: config.agentPreset,
|
|
78
|
+
initialAccessPolicy: initialAccessPolicyFor('whatsapp', bot),
|
|
79
|
+
})));
|
|
80
|
+
const observedConfigStore = typeof configStore.remove === 'function'
|
|
81
|
+
? observeBotWorkspaceRemovals(configStore, { workspaces })
|
|
82
|
+
: configStore;
|
|
83
|
+
const stateStores = new Map();
|
|
84
|
+
const statePath = (botId) => resolve(paths.bots, botId, 'state.json');
|
|
85
|
+
const stateFor = async (botId) => {
|
|
86
|
+
let state = stateStores.get(botId);
|
|
87
|
+
if (!state) {
|
|
88
|
+
state = await new StateStore(statePath(botId)).load();
|
|
89
|
+
stateStores.set(botId, state);
|
|
90
|
+
}
|
|
91
|
+
return state;
|
|
92
|
+
};
|
|
93
|
+
const commandExecutor = createHarnessCommandExecutor(ctx, internals.commandExecutor);
|
|
94
|
+
const inboundTtl = internals.inboundTtl ?? getInboundTtlRuntime(ctx, config);
|
|
95
|
+
const inboundTtlService = inboundTtl?.service ?? inboundTtl;
|
|
96
|
+
registerInboundTtlWorkspaces(ctx, inboundTtlService, {
|
|
97
|
+
workspaces,
|
|
98
|
+
configStore: observedConfigStore,
|
|
99
|
+
defaultWorkspace,
|
|
100
|
+
});
|
|
101
|
+
const { controlExecutor, sessionMaintenanceExecutor, fileIngressExecutor } = createHarnessSessionExecutors(ctx, {
|
|
102
|
+
controlExecutor: internals.controlExecutor,
|
|
103
|
+
sessionMaintenanceExecutor: internals.sessionMaintenanceExecutor,
|
|
104
|
+
fileIngressExecutor: internals.fileIngressExecutor,
|
|
105
|
+
inboundTtlService,
|
|
106
|
+
});
|
|
107
|
+
const harness = new Harness({
|
|
108
|
+
...connection,
|
|
109
|
+
workspace: defaultWorkspace,
|
|
110
|
+
ungroupedWorkspace,
|
|
111
|
+
autostart: false,
|
|
112
|
+
dshBin: config.dshBin ?? 'dsh',
|
|
113
|
+
...(commandExecutor ? { commandExecutor } : {}),
|
|
114
|
+
...(controlExecutor ? { controlExecutor } : {}),
|
|
115
|
+
...(sessionMaintenanceExecutor ? { sessionMaintenanceExecutor } : {}),
|
|
116
|
+
...(fileIngressExecutor ? { fileIngressExecutor } : {}),
|
|
117
|
+
imageInputPolicy: () => getImageInputSettingsStore(config).get(),
|
|
118
|
+
});
|
|
119
|
+
const modelCatalog = () => listModelCatalog(harness);
|
|
120
|
+
const coreController = new Controller({
|
|
121
|
+
configStore: observedConfigStore,
|
|
122
|
+
authPath: paths.authPath,
|
|
123
|
+
createSession,
|
|
124
|
+
logger,
|
|
125
|
+
createRuntime: async ({ botId, config: botConfig, authDir }) => {
|
|
126
|
+
const state = await stateFor(botId);
|
|
127
|
+
await workspaces.ensure(botId, {
|
|
128
|
+
defaultAgentPreset: config.agentPreset,
|
|
129
|
+
initialAccessPolicy: initialAccessPolicyFor('whatsapp', botConfig),
|
|
130
|
+
});
|
|
131
|
+
const workspaceScope = createBotWorkspaceScope(harness, {
|
|
132
|
+
botId, workspaces, state, agentPresetCatalog,
|
|
133
|
+
});
|
|
134
|
+
return new Runtime({
|
|
135
|
+
config: botConfig,
|
|
136
|
+
authDir,
|
|
137
|
+
harness: workspaceScope.harness,
|
|
138
|
+
state: workspaceScope.state,
|
|
139
|
+
contextEnhancement: { botId, getSettings: () => workspaces.contextEnhancementFor(botId) },
|
|
140
|
+
accessPolicy: accessPolicyProvider(workspaces, botId, {
|
|
141
|
+
channel: 'whatsapp', config: botConfig, equals: whatsappAccessPolicyIdsEqual,
|
|
142
|
+
}),
|
|
143
|
+
replyTimeoutMs: config.replyTimeoutMs ?? 600_000,
|
|
144
|
+
connectTimeoutMs: config.connectTimeoutMs ?? 30_000,
|
|
145
|
+
createSession,
|
|
146
|
+
logger: {
|
|
147
|
+
error: (...args) => logger.error?.(`[${botId}]`, ...args),
|
|
148
|
+
warn: (...args) => logger.warn?.(`[${botId}]`, ...args),
|
|
149
|
+
info: (...args) => logger.info?.(`[${botId}]`, ...args),
|
|
150
|
+
debug: (...args) => logger.debug?.(`[${botId}]`, ...args),
|
|
151
|
+
},
|
|
152
|
+
});
|
|
153
|
+
},
|
|
154
|
+
deleteAuth: (authDirectory) => rm(paths.authPath(authDirectory), {
|
|
155
|
+
recursive: true,
|
|
156
|
+
force: true,
|
|
157
|
+
}),
|
|
158
|
+
deleteState: async ({ botId }) => {
|
|
159
|
+
const state = stateStores.get(botId);
|
|
160
|
+
stateStores.delete(botId);
|
|
161
|
+
if (state && typeof state.remove === 'function') {
|
|
162
|
+
await state.remove();
|
|
163
|
+
} else {
|
|
164
|
+
try {
|
|
165
|
+
await unlink(statePath(botId));
|
|
166
|
+
} catch (error) {
|
|
167
|
+
if (error?.code !== 'ENOENT') throw error;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
},
|
|
171
|
+
});
|
|
172
|
+
const controller = createWorkspaceAwareController(coreController, {
|
|
173
|
+
workspaces,
|
|
174
|
+
stateFor,
|
|
175
|
+
agentPresetCatalog,
|
|
176
|
+
modelCatalog,
|
|
177
|
+
});
|
|
178
|
+
const supervisor = createSupervisor({
|
|
179
|
+
channel: 'whatsapp',
|
|
180
|
+
controller,
|
|
181
|
+
harness,
|
|
182
|
+
logger,
|
|
183
|
+
retryDelaysMs: config.retryDelaysMs,
|
|
184
|
+
healthyIntervalMs: config.healthyIntervalMs,
|
|
185
|
+
}).start();
|
|
186
|
+
return {
|
|
187
|
+
controller,
|
|
188
|
+
deliveryAdapter: createDeliveryAdapter({
|
|
189
|
+
channel: 'whatsapp', workspaces, coreController, stateFor,
|
|
190
|
+
}),
|
|
191
|
+
ready: supervisor.ready,
|
|
192
|
+
async close() {
|
|
193
|
+
await supervisor.close();
|
|
194
|
+
await controller.close();
|
|
195
|
+
harness.stopManagedProcess();
|
|
196
|
+
},
|
|
197
|
+
};
|
|
198
|
+
}
|