@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,471 @@
|
|
|
1
|
+
import { createServer } from 'node:http';
|
|
2
|
+
import { timingSafeEqual } from 'node:crypto';
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
WECOM_APP_STREAM_PLACEHOLDER,
|
|
6
|
+
buildEncryptedReply,
|
|
7
|
+
createUserCrypto,
|
|
8
|
+
isXmlFormat,
|
|
9
|
+
parseWecomAppPlainMessage,
|
|
10
|
+
parseWecomAppXmlBody,
|
|
11
|
+
} from './wecom-app-api.mjs';
|
|
12
|
+
|
|
13
|
+
const DEFAULT_STREAM_TTL_MS = 15 * 60_000;
|
|
14
|
+
const DEFAULT_MAX_STREAMS = 256;
|
|
15
|
+
const DEFAULT_MAX_BODY_BYTES = 1024 * 1024;
|
|
16
|
+
|
|
17
|
+
function safeEqualHex(expected, provided) {
|
|
18
|
+
const a = Buffer.from(String(expected ?? ''), 'utf8');
|
|
19
|
+
const b = Buffer.from(String(provided ?? ''), 'utf8');
|
|
20
|
+
if (a.length === 0 || a.length !== b.length) return false;
|
|
21
|
+
return timingSafeEqual(a, b);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function writeBody(res, status, body, headers = {}) {
|
|
25
|
+
if (res.writableEnded || res.destroyed) return;
|
|
26
|
+
res.writeHead(status, {
|
|
27
|
+
'content-length': String(Buffer.byteLength(body)),
|
|
28
|
+
'cache-control': 'no-store',
|
|
29
|
+
...headers,
|
|
30
|
+
});
|
|
31
|
+
res.end(body);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function writeText(res, status, text, headers = {}) {
|
|
35
|
+
writeBody(res, status, text, { 'content-type': 'text/plain; charset=utf-8', ...headers });
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function writeEmpty(res) {
|
|
39
|
+
if (res.writableEnded || res.destroyed) return;
|
|
40
|
+
res.statusCode = 200;
|
|
41
|
+
res.end();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// One shared HTTP listener for every wecom-app bot. Each bot registers a route
|
|
45
|
+
// keyed by its full callback path; the per-bot random path segment doubles as
|
|
46
|
+
// the first gate before the WeCom signature check.
|
|
47
|
+
export class WecomAppCallbackServer {
|
|
48
|
+
#host;
|
|
49
|
+
#port;
|
|
50
|
+
#logger;
|
|
51
|
+
#streamTtlMs;
|
|
52
|
+
#maxStreams;
|
|
53
|
+
#maxBodyBytes;
|
|
54
|
+
#server = null;
|
|
55
|
+
#routes = new Map();
|
|
56
|
+
#streams = new Map();
|
|
57
|
+
#streamByMsgId = new Map();
|
|
58
|
+
|
|
59
|
+
constructor({
|
|
60
|
+
host = '127.0.0.1',
|
|
61
|
+
port = 30987,
|
|
62
|
+
logger = console,
|
|
63
|
+
streamTtlMs = DEFAULT_STREAM_TTL_MS,
|
|
64
|
+
maxStreams = DEFAULT_MAX_STREAMS,
|
|
65
|
+
maxBodyBytes = DEFAULT_MAX_BODY_BYTES,
|
|
66
|
+
} = {}) {
|
|
67
|
+
this.#host = String(host ?? '127.0.0.1');
|
|
68
|
+
this.#port = Number(port);
|
|
69
|
+
if (!Number.isInteger(this.#port) || this.#port < 0 || this.#port > 65_535) {
|
|
70
|
+
throw new TypeError('WecomAppCallbackServer requires an integer port between 0 and 65535');
|
|
71
|
+
}
|
|
72
|
+
this.#logger = logger;
|
|
73
|
+
this.#streamTtlMs = streamTtlMs;
|
|
74
|
+
this.#maxStreams = maxStreams;
|
|
75
|
+
this.#maxBodyBytes = maxBodyBytes;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
get listening() {
|
|
79
|
+
return this.#server !== null && this.#server.listening === true;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
get address() {
|
|
83
|
+
return { host: this.#host, port: this.#port };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// The OS-assigned port after listen(0); falls back to the configured port.
|
|
87
|
+
get actualPort() {
|
|
88
|
+
const bound = this.#server?.address?.();
|
|
89
|
+
return typeof bound?.port === 'number' ? bound.port : this.#port;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
routePath({ botId, callbackSecret }) {
|
|
93
|
+
// Keep the callback prefix distinct from the GUI RPC channel path
|
|
94
|
+
// (/wecom-app/...): the reverse-proxy location must forward ONLY the
|
|
95
|
+
// callback prefix to this listener and leave RPC traffic to dsh web.
|
|
96
|
+
return `/wecom-app-callback/${botId}/${callbackSecret}`;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
registerRoute(route) {
|
|
100
|
+
if (!route?.botId || !route.callbackSecret || typeof route.cryptoFor !== 'function' || typeof route.onInbound !== 'function') {
|
|
101
|
+
throw new TypeError('A wecom-app route requires botId, callbackSecret, cryptoFor, and onInbound');
|
|
102
|
+
}
|
|
103
|
+
// Keyed by the full callback path: WeCom requests are dispatched on it.
|
|
104
|
+
this.#routes.set(this.routePath(route), Object.freeze({ ...route }));
|
|
105
|
+
return this;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
unregisterRoute(botId) {
|
|
109
|
+
for (const [path, route] of this.#routes.entries()) {
|
|
110
|
+
if (route.botId === botId) this.#routes.delete(path);
|
|
111
|
+
}
|
|
112
|
+
for (const [streamId, stream] of this.#streams.entries()) {
|
|
113
|
+
if (stream.botId === botId) {
|
|
114
|
+
this.#streams.delete(streamId);
|
|
115
|
+
if (stream.msgid) this.#streamByMsgId.delete(this.#msgKey(stream.botId, stream.msgid));
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return this;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async start() {
|
|
122
|
+
if (this.#server) return this.address;
|
|
123
|
+
const server = createServer((request, response) => {
|
|
124
|
+
this.#handleRequest(request, response).catch((error) => {
|
|
125
|
+
this.#logger.warn?.('[dsh-im:wecom-app] callback request failed:', error);
|
|
126
|
+
writeText(response, 500, 'internal error');
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
server.on('clientError', (_error, socket) => {
|
|
130
|
+
if (socket.writable) socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
|
|
131
|
+
});
|
|
132
|
+
await new Promise((resolve, reject) => {
|
|
133
|
+
const onceError = (error) => {
|
|
134
|
+
server.removeListener('listening', onceListening);
|
|
135
|
+
reject(error);
|
|
136
|
+
};
|
|
137
|
+
const onceListening = () => {
|
|
138
|
+
server.removeListener('error', onceError);
|
|
139
|
+
resolve();
|
|
140
|
+
};
|
|
141
|
+
server.once('error', onceError);
|
|
142
|
+
server.once('listening', onceListening);
|
|
143
|
+
server.listen(this.#port, this.#host);
|
|
144
|
+
});
|
|
145
|
+
this.#server = server;
|
|
146
|
+
return this.address;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
async stop() {
|
|
150
|
+
const server = this.#server;
|
|
151
|
+
this.#server = null;
|
|
152
|
+
this.#routes.clear();
|
|
153
|
+
this.#streams.clear();
|
|
154
|
+
this.#streamByMsgId.clear();
|
|
155
|
+
if (!server) return;
|
|
156
|
+
await new Promise((resolve) => {
|
|
157
|
+
server.close(() => resolve());
|
|
158
|
+
server.closeAllConnections?.();
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
routeCount() {
|
|
163
|
+
return this.#routes.size;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Stream dedup and lookup are scoped per bot: two self-built apps can share
|
|
167
|
+
// a MsgId, and a refresh signed by app B must never read app A's stream.
|
|
168
|
+
#msgKey(botId, msgid) {
|
|
169
|
+
return botId + ':' + msgid;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
createStream({ botId, msgid }) {
|
|
173
|
+
this.#pruneStreams();
|
|
174
|
+
if (this.#streams.size >= this.#maxStreams) {
|
|
175
|
+
const oldest = this.#streams.keys().next().value;
|
|
176
|
+
const evicted = this.#streams.get(oldest);
|
|
177
|
+
this.#streams.delete(oldest);
|
|
178
|
+
if (evicted?.msgid) this.#streamByMsgId.delete(this.#msgKey(evicted.botId, evicted.msgid));
|
|
179
|
+
}
|
|
180
|
+
const streamId = `${Date.now().toString(36)}${Math.random().toString(36).slice(2, 10)}`;
|
|
181
|
+
const state = {
|
|
182
|
+
botId,
|
|
183
|
+
streamId,
|
|
184
|
+
msgid: msgid ?? null,
|
|
185
|
+
content: '',
|
|
186
|
+
finished: false,
|
|
187
|
+
error: null,
|
|
188
|
+
refreshes: 0,
|
|
189
|
+
createdAt: Date.now(),
|
|
190
|
+
updatedAt: Date.now(),
|
|
191
|
+
};
|
|
192
|
+
this.#streams.set(streamId, state);
|
|
193
|
+
if (msgid) this.#streamByMsgId.set(this.#msgKey(botId, msgid), streamId);
|
|
194
|
+
return state;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
getStream(streamId) {
|
|
198
|
+
return this.#streams.get(streamId) ?? null;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
appendStream(streamId, chunk, { replace = false } = {}) {
|
|
202
|
+
const stream = this.#streams.get(streamId);
|
|
203
|
+
if (!stream || stream.finished) return false;
|
|
204
|
+
stream.content = replace ? String(chunk ?? '') : stream.content + String(chunk ?? '');
|
|
205
|
+
stream.updatedAt = Date.now();
|
|
206
|
+
return true;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
finishStream(streamId, { error } = {}) {
|
|
210
|
+
const stream = this.#streams.get(streamId);
|
|
211
|
+
if (!stream) return false;
|
|
212
|
+
if (error) stream.error = String(error);
|
|
213
|
+
stream.finished = true;
|
|
214
|
+
stream.updatedAt = Date.now();
|
|
215
|
+
return true;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
streamSnapshot(streamId) {
|
|
219
|
+
const stream = this.#streams.get(streamId);
|
|
220
|
+
if (!stream) return null;
|
|
221
|
+
return {
|
|
222
|
+
id: stream.streamId,
|
|
223
|
+
finish: stream.finished,
|
|
224
|
+
content: stream.content,
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
async #handleRequest(request, response) {
|
|
229
|
+
if (request.method !== 'GET' && request.method !== 'POST') {
|
|
230
|
+
writeText(response, 405, 'method not allowed', { allow: 'GET, POST' });
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
const url = new URL(request.url ?? '/', `http://${request.headers.host ?? 'localhost'}`);
|
|
234
|
+
const route = this.#routes.get(url.pathname) ?? null;
|
|
235
|
+
if (!route) {
|
|
236
|
+
writeText(response, 404, 'not found');
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
const query = url.searchParams;
|
|
240
|
+
if (request.method === 'GET') {
|
|
241
|
+
this.#verifyUrl(route, query, response);
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
await this.#handlePost(route, query, request, response);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
#verifyUrl(route, query, response) {
|
|
248
|
+
const timestamp = query.get('timestamp') ?? '';
|
|
249
|
+
const nonce = query.get('nonce') ?? '';
|
|
250
|
+
const echostr = query.get('echostr') ?? '';
|
|
251
|
+
const signature = query.get('msg_signature') ?? query.get('msgsignature') ?? query.get('signature') ?? '';
|
|
252
|
+
this.#logger.info?.('[dsh-im:wecom-app] URL verification attempt', route.botId, { timestamp, nonce });
|
|
253
|
+
if (!timestamp || !nonce || !echostr || !signature) {
|
|
254
|
+
this.#logger.warn?.('[dsh-im:wecom-app] URL verification rejected: missing query params', route.botId);
|
|
255
|
+
writeText(response, 400, 'missing query params');
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
let crypto;
|
|
259
|
+
try {
|
|
260
|
+
crypto = route.cryptoFor();
|
|
261
|
+
} catch (error) {
|
|
262
|
+
this.#logger.warn?.('[dsh-im:wecom-app] route credentials are incomplete:', error);
|
|
263
|
+
writeText(response, 503, 'route not ready');
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
try {
|
|
267
|
+
if (!safeEqualHex(crypto.computeSignature(timestamp, nonce, echostr), signature)) {
|
|
268
|
+
this.#logger.warn?.('[dsh-im:wecom-app] URL verification signature mismatch', route.botId);
|
|
269
|
+
writeText(response, 401, 'unauthorized');
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
const plaintext = crypto.decrypt(echostr);
|
|
273
|
+
this.#logger.info?.('[dsh-im:wecom-app] URL verification succeeded', route.botId);
|
|
274
|
+
writeText(response, 200, plaintext);
|
|
275
|
+
} catch (error) {
|
|
276
|
+
this.#logger.warn?.('[dsh-im:wecom-app] URL verification failed:', error);
|
|
277
|
+
writeText(response, 400, 'verification failed');
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
async #handlePost(route, query, request, response) {
|
|
282
|
+
const body = await this.#readBody(request, response);
|
|
283
|
+
if (body === null) return;
|
|
284
|
+
let envelope = {};
|
|
285
|
+
if (isXmlFormat(body.raw)) {
|
|
286
|
+
const xml = parseWecomAppXmlBody(body.raw);
|
|
287
|
+
envelope = {
|
|
288
|
+
encrypt: xml.Encrypt ?? '',
|
|
289
|
+
signature: xml.MsgSignature ?? query.get('msg_signature') ?? query.get('msgsignature') ?? '',
|
|
290
|
+
timestamp: xml.TimeStamp ?? query.get('timestamp') ?? '',
|
|
291
|
+
nonce: xml.Nonce ?? query.get('nonce') ?? '',
|
|
292
|
+
format: 'xml',
|
|
293
|
+
};
|
|
294
|
+
} else {
|
|
295
|
+
let parsed = {};
|
|
296
|
+
try {
|
|
297
|
+
parsed = JSON.parse(body.raw);
|
|
298
|
+
} catch {
|
|
299
|
+
writeText(response, 400, 'invalid payload');
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
envelope = {
|
|
303
|
+
encrypt: parsed.encrypt ?? '',
|
|
304
|
+
signature: parsed.msgsignature ?? parsed.MsgSignature ?? query.get('msg_signature') ?? '',
|
|
305
|
+
timestamp: String(parsed.timestamp ?? parsed.TimeStamp ?? query.get('timestamp') ?? ''),
|
|
306
|
+
nonce: String(parsed.nonce ?? parsed.Nonce ?? query.get('nonce') ?? ''),
|
|
307
|
+
format: 'json',
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
if (!envelope.encrypt || !envelope.signature || !envelope.timestamp || !envelope.nonce) {
|
|
311
|
+
writeText(response, 400, 'missing encrypted payload');
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
this.#logger.info?.('[dsh-im:wecom-app] callback POST received', route.botId, { format: envelope.format, bytes: body.raw.length });
|
|
315
|
+
let crypto;
|
|
316
|
+
try {
|
|
317
|
+
crypto = route.cryptoFor();
|
|
318
|
+
} catch (error) {
|
|
319
|
+
this.#logger.warn?.('[dsh-im:wecom-app] route credentials are incomplete:', error);
|
|
320
|
+
writeText(response, 503, 'route not ready');
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
let plaintext;
|
|
324
|
+
try {
|
|
325
|
+
if (!safeEqualHex(crypto.computeSignature(envelope.timestamp, envelope.nonce, envelope.encrypt), envelope.signature)) {
|
|
326
|
+
this.#logger.warn?.('[dsh-im:wecom-app] callback signature mismatch', route.botId);
|
|
327
|
+
writeText(response, 401, 'unauthorized');
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
plaintext = crypto.decrypt(envelope.encrypt);
|
|
331
|
+
} catch (error) {
|
|
332
|
+
this.#logger.warn?.('[dsh-im:wecom-app] callback signature or decryption failed:', error);
|
|
333
|
+
writeText(response, 401, 'unauthorized');
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
const message = parseWecomAppPlainMessage(plaintext);
|
|
337
|
+
if (!message) {
|
|
338
|
+
writeText(response, 400, 'invalid message');
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
this.#logger.info?.('[dsh-im:wecom-app] callback message accepted', route.botId, { msgtype: message.msgtype ?? null, msgid: message.msgid ?? null });
|
|
342
|
+
this.#dispatch(route, message, envelope, response);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
#readBody(request, response) {
|
|
346
|
+
return new Promise((resolve) => {
|
|
347
|
+
const chunks = [];
|
|
348
|
+
let length = 0;
|
|
349
|
+
request.on('data', (chunk) => {
|
|
350
|
+
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
|
|
351
|
+
length += buffer.length;
|
|
352
|
+
if (length > this.#maxBodyBytes) {
|
|
353
|
+
writeText(response, 413, 'payload too large');
|
|
354
|
+
request.destroy();
|
|
355
|
+
resolve(null);
|
|
356
|
+
return;
|
|
357
|
+
}
|
|
358
|
+
chunks.push(buffer);
|
|
359
|
+
});
|
|
360
|
+
request.on('end', () => {
|
|
361
|
+
const raw = Buffer.concat(chunks, length).toString('utf8');
|
|
362
|
+
if (!raw.trim()) {
|
|
363
|
+
writeText(response, 400, 'empty payload');
|
|
364
|
+
resolve(null);
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
resolve({ raw });
|
|
368
|
+
});
|
|
369
|
+
request.on('error', () => {
|
|
370
|
+
resolve(null);
|
|
371
|
+
});
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
#dispatch(route, message, envelope, response) {
|
|
376
|
+
const msgid = typeof message.msgid === 'string' ? message.msgid : null;
|
|
377
|
+
const isStreamRefresh = message.msgtype === 'stream' || message.event === 'stream_refresh';
|
|
378
|
+
if (isStreamRefresh) {
|
|
379
|
+
const streamId = message.stream?.id ?? null;
|
|
380
|
+
const stream = streamId ? this.#streams.get(streamId) : null;
|
|
381
|
+
const snapshot = stream && stream.botId === route.botId ? this.streamSnapshot(streamId) : null;
|
|
382
|
+
if (!snapshot) {
|
|
383
|
+
writeEmpty(response);
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
stream.refreshes += 1;
|
|
387
|
+
stream.updatedAt = Date.now();
|
|
388
|
+
this.#replyEncrypted(route, envelope, response, {
|
|
389
|
+
msgtype: 'stream',
|
|
390
|
+
stream: snapshot,
|
|
391
|
+
});
|
|
392
|
+
return;
|
|
393
|
+
}
|
|
394
|
+
// WeCom retries an unanswered POST with the same MsgId; reply with the
|
|
395
|
+
// current stream snapshot (including the final frame) so a client that
|
|
396
|
+
// lost an earlier response can still settle on the finished content.
|
|
397
|
+
const retryStreamId = msgid ? this.#streamByMsgId.get(this.#msgKey(route.botId, msgid)) : null;
|
|
398
|
+
if (retryStreamId) {
|
|
399
|
+
const snapshot = this.streamSnapshot(retryStreamId);
|
|
400
|
+
if (snapshot) {
|
|
401
|
+
this.#replyEncrypted(route, envelope, response, { msgtype: 'stream', stream: snapshot });
|
|
402
|
+
return;
|
|
403
|
+
}
|
|
404
|
+
writeEmpty(response);
|
|
405
|
+
return;
|
|
406
|
+
}
|
|
407
|
+
if (message.msgtype === 'event') {
|
|
408
|
+
writeEmpty(response);
|
|
409
|
+
route.onInbound({ kind: 'event', message }).catch((error) => {
|
|
410
|
+
this.#logger.warn?.('[dsh-im:wecom-app] event handling failed:', error);
|
|
411
|
+
});
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
let streamState = null;
|
|
415
|
+
let streamEnabled = false;
|
|
416
|
+
try {
|
|
417
|
+
streamEnabled = route.streamEnabled?.() === true;
|
|
418
|
+
} catch {
|
|
419
|
+
streamEnabled = false;
|
|
420
|
+
}
|
|
421
|
+
if (streamEnabled && msgid && (message.msgtype === 'text' || message.msgtype === 'image')) {
|
|
422
|
+
streamState = this.createStream({ botId: route.botId, msgid });
|
|
423
|
+
this.#replyEncrypted(route, envelope, response, {
|
|
424
|
+
msgtype: 'stream',
|
|
425
|
+
stream: {
|
|
426
|
+
id: streamState.streamId,
|
|
427
|
+
finish: false,
|
|
428
|
+
content: WECOM_APP_STREAM_PLACEHOLDER(),
|
|
429
|
+
},
|
|
430
|
+
});
|
|
431
|
+
} else {
|
|
432
|
+
writeEmpty(response);
|
|
433
|
+
}
|
|
434
|
+
route.onInbound({ kind: 'message', message, stream: streamState }).catch((error) => {
|
|
435
|
+
this.#logger.warn?.('[dsh-im:wecom-app] message handling failed:', error);
|
|
436
|
+
if (streamState) this.finishStream(streamState.streamId, { error: error?.message ?? String(error) });
|
|
437
|
+
});
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
#replyEncrypted(route, envelope, response, payload) {
|
|
441
|
+
let body;
|
|
442
|
+
try {
|
|
443
|
+
body = buildEncryptedReply({
|
|
444
|
+
format: envelope.format,
|
|
445
|
+
crypto: route.cryptoFor(),
|
|
446
|
+
plaintext: payload,
|
|
447
|
+
timestamp: envelope.timestamp,
|
|
448
|
+
nonce: envelope.nonce,
|
|
449
|
+
});
|
|
450
|
+
} catch (error) {
|
|
451
|
+
this.#logger.warn?.('[dsh-im:wecom-app] failed to encrypt the passive reply:', error);
|
|
452
|
+
writeEmpty(response);
|
|
453
|
+
return;
|
|
454
|
+
}
|
|
455
|
+
writeBody(response, 200, body, {
|
|
456
|
+
'content-type': envelope.format === 'json'
|
|
457
|
+
? 'application/json; charset=utf-8'
|
|
458
|
+
: 'application/xml; charset=utf-8',
|
|
459
|
+
});
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
#pruneStreams() {
|
|
463
|
+
const cutoff = Date.now() - this.#streamTtlMs;
|
|
464
|
+
for (const [streamId, stream] of this.#streams.entries()) {
|
|
465
|
+
if (stream.updatedAt < cutoff) {
|
|
466
|
+
this.#streams.delete(streamId);
|
|
467
|
+
if (stream.msgid) this.#streamByMsgId.delete(this.#msgKey(stream.botId, stream.msgid));
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
}
|