@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,199 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
|
|
3
|
+
export function discordRefusal(code) { return Object.assign(new Error(code), { code }); }
|
|
4
|
+
export function discordSnowflake(value) { return typeof value === 'string' && /^[0-9]{5,30}$/.test(value); }
|
|
5
|
+
const PUBLIC_THREAD = 11;
|
|
6
|
+
const TEXT_CHANNEL = 0;
|
|
7
|
+
const VIEW = 1n << 10n;
|
|
8
|
+
const SEND = 1n << 11n;
|
|
9
|
+
const ATTACH = 1n << 15n;
|
|
10
|
+
const HISTORY = 1n << 16n;
|
|
11
|
+
const THREAD_SEND = 1n << 38n;
|
|
12
|
+
const ADMIN = 1n << 3n;
|
|
13
|
+
|
|
14
|
+
export function verifiedDiscordAccount(user, application) {
|
|
15
|
+
if (!discordSnowflake(user?.id) || user.bot !== true || !discordSnowflake(application?.id)
|
|
16
|
+
|| application.bot?.id !== user.id)
|
|
17
|
+
throw discordRefusal('account-unverified');
|
|
18
|
+
const identity = { appId: application.id, userId: user.id };
|
|
19
|
+
return Object.freeze({ ...identity,
|
|
20
|
+
name: [user.global_name, user.username, application.name].find(value => typeof value === 'string' && value.trim()) ?? user.id,
|
|
21
|
+
fingerprint: createHash('sha256').update(JSON.stringify({ provider: 'discord', ...identity })).digest('hex'),
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function bits(value) {
|
|
26
|
+
if (typeof value !== 'string' || !/^\d+$/.test(value)) throw discordRefusal('reply-permission-denied');
|
|
27
|
+
return BigInt(value);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Official overwrite order: everyone, union of member roles, then the member. */
|
|
31
|
+
export function discordChannelPermissions(guild, member, channel, userId) {
|
|
32
|
+
if (!discordSnowflake(guild?.id) || channel?.guild_id !== guild.id || member?.user?.id !== userId
|
|
33
|
+
|| !Array.isArray(guild.roles) || !Array.isArray(member.roles)
|
|
34
|
+
|| !Array.isArray(channel.permission_overwrites)) throw discordRefusal('reply-permission-denied');
|
|
35
|
+
const everyone = guild.roles.find(role => role.id === guild.id);
|
|
36
|
+
if (!everyone) throw discordRefusal('reply-permission-denied');
|
|
37
|
+
let permissions = bits(everyone.permissions);
|
|
38
|
+
for (const id of member.roles) {
|
|
39
|
+
const role = guild.roles.find(candidate => candidate.id === id);
|
|
40
|
+
if (!role) throw discordRefusal('reply-permission-denied');
|
|
41
|
+
permissions |= bits(role.permissions);
|
|
42
|
+
}
|
|
43
|
+
if (guild.owner_id === userId || (permissions & ADMIN) !== 0n) return ~0n;
|
|
44
|
+
const apply = overwrite => {
|
|
45
|
+
if (overwrite) permissions = (permissions & ~bits(overwrite.deny)) | bits(overwrite.allow);
|
|
46
|
+
};
|
|
47
|
+
apply(channel.permission_overwrites.find(row => row.type === 0 && row.id === guild.id));
|
|
48
|
+
let allow = 0n; let deny = 0n;
|
|
49
|
+
for (const row of channel.permission_overwrites) {
|
|
50
|
+
if (row.type === 0 && member.roles.includes(row.id)) { allow |= bits(row.allow); deny |= bits(row.deny); }
|
|
51
|
+
}
|
|
52
|
+
permissions = (permissions & ~deny) | allow;
|
|
53
|
+
apply(channel.permission_overwrites.find(row => row.type === 1 && row.id === userId));
|
|
54
|
+
return permissions;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function nativeFailure(error, missing) {
|
|
58
|
+
if (error?.code && ['stale-route', 'reply-permission-denied', 'cancelled'].includes(error.code)) return error;
|
|
59
|
+
if (error?.status === 404) return discordRefusal(missing);
|
|
60
|
+
if (error?.status === 401 || error?.status === 403) return discordRefusal('reply-permission-denied');
|
|
61
|
+
if (error?.name === 'AbortError') return discordRefusal('cancelled');
|
|
62
|
+
return discordRefusal('source-unavailable');
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Only one guild text channel and its existing public threads; no channel creation. */
|
|
66
|
+
export async function inspectDiscordSourceChannel(api, channelId, account, signal, { forReply = false, forHistory = false, forFileReply = false } = {}) {
|
|
67
|
+
if (!discordSnowflake(channelId)) throw discordRefusal('stale-route');
|
|
68
|
+
signal?.throwIfAborted();
|
|
69
|
+
try {
|
|
70
|
+
const channel = await api.getChannel({ channelId, signal });
|
|
71
|
+
if (channel?.id !== channelId || !discordSnowflake(channel.guild_id)) throw discordRefusal('stale-route');
|
|
72
|
+
const isThread = channel.type === PUBLIC_THREAD;
|
|
73
|
+
if (channel.type !== TEXT_CHANNEL && !isThread) throw discordRefusal('reply-permission-denied');
|
|
74
|
+
const parent = isThread ? await api.getChannel({ channelId: channel.parent_id, signal }) : channel;
|
|
75
|
+
if (parent?.type !== TEXT_CHANNEL || parent.guild_id !== channel.guild_id
|
|
76
|
+
|| (isThread && (parent.id !== channel.parent_id || parent.id === channel.id))) throw discordRefusal('stale-route');
|
|
77
|
+
if (isThread && !forHistory && (!channel.thread_metadata || channel.thread_metadata.archived !== false
|
|
78
|
+
|| channel.thread_metadata.locked !== false)) throw discordRefusal('reply-permission-denied');
|
|
79
|
+
const [guild, member] = await Promise.all([
|
|
80
|
+
api.getGuild({ guildId: channel.guild_id, signal }),
|
|
81
|
+
api.getGuildMember({ guildId: channel.guild_id, userId: account.userId, signal }),
|
|
82
|
+
]);
|
|
83
|
+
if (guild?.id !== channel.guild_id) throw discordRefusal('stale-route');
|
|
84
|
+
const permissions = discordChannelPermissions(guild, member, parent, account.userId);
|
|
85
|
+
const required = (forFileReply ? ATTACH : 0n) | VIEW | (forHistory ? HISTORY : 0n) | (forReply ? HISTORY | (isThread ? THREAD_SEND : SEND) : 0n);
|
|
86
|
+
if ((permissions & required) !== required) throw discordRefusal('reply-permission-denied');
|
|
87
|
+
if (forReply && member.communication_disabled_until && Date.parse(member.communication_disabled_until) > Date.now())
|
|
88
|
+
throw discordRefusal('reply-permission-denied');
|
|
89
|
+
signal?.throwIfAborted();
|
|
90
|
+
const name = [guild.name, parent.name].every(value => typeof value === 'string' && value.trim())
|
|
91
|
+
? `${guild.name.trim()} #${parent.name.trim()}`.slice(0, 512) : undefined;
|
|
92
|
+
return { guildId: guild.id, conversationId: parent.id, channelId: channel.id, ...(name ? { name } : {}),
|
|
93
|
+
...(isThread ? { threadId: channel.id } : {}) };
|
|
94
|
+
} catch (error) { throw nativeFailure(error, 'source-not-found'); }
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function discordMessageContentAllowed(application) {
|
|
98
|
+
let flags;
|
|
99
|
+
if (application?.flags_new !== undefined) {
|
|
100
|
+
if (typeof application.flags_new !== 'string' || !/^[0-9]{1,128}$/.test(application.flags_new)) return false;
|
|
101
|
+
flags = BigInt(application.flags_new);
|
|
102
|
+
} else {
|
|
103
|
+
if (!Number.isSafeInteger(application?.flags) || application.flags < 0) return false;
|
|
104
|
+
flags = BigInt(application.flags);
|
|
105
|
+
}
|
|
106
|
+
return (flags & ((1n << 18n) | (1n << 19n))) !== 0n;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function normalizeDiscordExternalText(message, { botId, account, channel, eventId, ordinaryText = false }) {
|
|
110
|
+
return normalizeDiscordHumanText(message, { botId, account, channel, eventId }, ordinaryText !== true);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** History visibility never relaxes the mention-only live admission predicate. */
|
|
114
|
+
export function normalizeDiscordHistoryText(message, identity) {
|
|
115
|
+
return normalizeDiscordHumanText(message, identity, false);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function normalizeDiscordHumanText(message, { botId, account, channel, eventId }, requireMention) {
|
|
119
|
+
if (!discordSnowflake(message?.id) || !discordSnowflake(message?.author?.id)
|
|
120
|
+
|| message.author.bot === true || message.webhook_id || message.author.id === account.userId
|
|
121
|
+
|| ![0, 19].includes(message.type) || typeof message.content !== 'string' || !message.content.trim()
|
|
122
|
+
|| message.content.length > 16000 || !Array.isArray(message.mentions)) return null;
|
|
123
|
+
if (message.guild_id !== channel.guildId || message.channel_id !== channel.channelId)
|
|
124
|
+
throw discordRefusal('stale-route');
|
|
125
|
+
const mentionedAccount = message.mentions.some(user => user.id === account.userId);
|
|
126
|
+
if (requireMention && !mentionedAccount) return null;
|
|
127
|
+
const timestamp = Date.parse(message.timestamp);
|
|
128
|
+
if (!Number.isFinite(timestamp) || typeof eventId !== 'string' || !eventId || eventId.length > 512)
|
|
129
|
+
throw discordRefusal('invalid-inbound');
|
|
130
|
+
const name = [message.member?.nick, message.author.global_name, message.author.username]
|
|
131
|
+
.find(value => typeof value === 'string' && value.trim());
|
|
132
|
+
const mentions = message.mentions.filter(user => discordSnowflake(user.id)).slice(0, 100)
|
|
133
|
+
.map(user => ({ id: user.id, key: `<@${user.id}>`,
|
|
134
|
+
...(user.id === account.userId ? { name: account.name } : {}) }));
|
|
135
|
+
return { version: 1, channel: 'discord', botId, fingerprint: account.fingerprint,
|
|
136
|
+
eventId, messageId: message.id, actor: { kind: 'user', id: message.author.id, ...(name ? { name: name.slice(0, 512) } : {}) },
|
|
137
|
+
conversation: { kind: 'group', id: channel.conversationId, ...(channel.name ? { name: channel.name } : {}) }, mentions, mentionedAccount,
|
|
138
|
+
at: new Date(timestamp).toISOString(), text: message.content,
|
|
139
|
+
reply: { messageId: message.id, conversationId: channel.conversationId, actorId: message.author.id,
|
|
140
|
+
...(channel.threadId ? { threadId: channel.threadId } : {}) },
|
|
141
|
+
replay: { kind: 'provider-redelivery', resumeCursor: false, gapPossible: true },
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export async function qualifyDiscordReply(api, account, route, signal, permissions = { forReply: true }) {
|
|
146
|
+
if (!route || !discordSnowflake(route.messageId) || !discordSnowflake(route.actorId)
|
|
147
|
+
|| !discordSnowflake(route.conversationId) || route.rootId !== undefined || route.parentId !== undefined
|
|
148
|
+
|| (route.threadId !== undefined && !discordSnowflake(route.threadId))) throw discordRefusal('stale-route');
|
|
149
|
+
const channel = await inspectDiscordSourceChannel(api, route.threadId ?? route.conversationId, account, signal, permissions);
|
|
150
|
+
if (channel.conversationId !== route.conversationId || channel.threadId !== route.threadId)
|
|
151
|
+
throw discordRefusal('stale-route');
|
|
152
|
+
let source;
|
|
153
|
+
try { source = await api.getMessage({ channelId: channel.channelId, messageId: route.messageId, signal }); }
|
|
154
|
+
catch (error) { throw nativeFailure(error, 'source-not-found'); }
|
|
155
|
+
if (source?.id !== route.messageId || source.channel_id !== channel.channelId
|
|
156
|
+
|| source.author?.id !== route.actorId || source.author.bot === true || source.webhook_id
|
|
157
|
+
|| ![0, 19].includes(source.type)) throw discordRefusal('stale-route');
|
|
158
|
+
signal?.throwIfAborted();
|
|
159
|
+
return { channel, route: { ...route }, source };
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** Only the source author or people the source message mentions may be pinged back. */
|
|
163
|
+
export function discordReplyMentions(mentionUserIds, route, source) {
|
|
164
|
+
if (mentionUserIds === undefined) return [];
|
|
165
|
+
if (!Array.isArray(mentionUserIds) || mentionUserIds.length > 20
|
|
166
|
+
|| !mentionUserIds.every(discordSnowflake)) throw discordRefusal('bad-request');
|
|
167
|
+
const allowed = new Set([route.actorId, ...(Array.isArray(source?.mentions) ? source.mentions.map(user => user?.id) : [])]);
|
|
168
|
+
if (!mentionUserIds.every(id => allowed.has(id))) throw discordRefusal('bad-request');
|
|
169
|
+
return [...new Set(mentionUserIds)];
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export async function sendDiscordReply(api, account, route, text, { signal, beforeSend, assertCurrent = () => {}, receipt = false, mentionUserIds } = {}) {
|
|
173
|
+
if (typeof text !== 'string' || !text.trim() || text.length > 2000) throw discordRefusal('bad-request');
|
|
174
|
+
const checked = await qualifyDiscordReply(api, account, route, signal);
|
|
175
|
+
const mentions = discordReplyMentions(mentionUserIds, route, checked.source);
|
|
176
|
+
if (mentions.length * 23 + text.length > 2000) throw discordRefusal('bad-request');
|
|
177
|
+
signal?.throwIfAborted();
|
|
178
|
+
assertCurrent();
|
|
179
|
+
if (beforeSend && beforeSend() !== true) throw discordRefusal('stale-route');
|
|
180
|
+
signal?.throwIfAborted();
|
|
181
|
+
return sendDiscordCheckedText(api, account, checked.channel, text, { signal, receipt, replyToMessageId: route.messageId,
|
|
182
|
+
...(mentions.length ? { mentionUserIds: mentions } : {}) });
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export async function sendDiscordCheckedText(api, account, channel, text, { signal, receipt = false, replyToMessageId, mentionUserIds } = {}) {
|
|
186
|
+
if (typeof text !== 'string' || !text.trim() || text.length > 2000) throw discordRefusal('bad-request');
|
|
187
|
+
let sent;
|
|
188
|
+
try {
|
|
189
|
+
sent = await api.createMessage({ channelId: channel.channelId, content: text,
|
|
190
|
+
replyToMessageId, ...(mentionUserIds ? { mentionUserIds } : {}), signal, retry: false, failIfNotExists: true });
|
|
191
|
+
} catch (error) {
|
|
192
|
+
if ([400, 401, 403, 404, 429].includes(error?.status)) throw discordRefusal('reply-permission-denied');
|
|
193
|
+
throw discordRefusal('reply-result-unknown');
|
|
194
|
+
}
|
|
195
|
+
if (!discordSnowflake(sent?.id) || sent.channel_id !== channel.channelId
|
|
196
|
+
|| sent.author?.id !== account.userId || sent.author.bot !== true)
|
|
197
|
+
throw discordRefusal('reply-result-unknown');
|
|
198
|
+
return { sent: true, ...(receipt ? { receipt: { version: 1, messageId: sent.id, conversationId: channel.conversationId } } : {}) };
|
|
199
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { discordRefusal, discordSnowflake, qualifyDiscordReply } from './external-consumer.mjs';
|
|
3
|
+
|
|
4
|
+
const MAX_BYTES = 20 * 1024 * 1024;
|
|
5
|
+
const MIME = /^[a-zA-Z0-9!#$&^_.+-]+\/[a-zA-Z0-9!#$&^_.+-]+$/;
|
|
6
|
+
function safeName(value) {
|
|
7
|
+
return typeof value === 'string' && value.trim() && value.length <= 512 && !/[\x00-\x1f\x7f/\\]/.test(value);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function attachmentFor(source, route) {
|
|
11
|
+
if (source.attachments === undefined || source.attachments?.length === 0) return undefined;
|
|
12
|
+
if (!Array.isArray(source.attachments) || source.attachments.length !== 1)
|
|
13
|
+
throw discordRefusal('resource-unavailable');
|
|
14
|
+
const file = source.attachments[0];
|
|
15
|
+
if (!discordSnowflake(file?.id) || !safeName(file.filename) || file.ephemeral === true
|
|
16
|
+
|| !Number.isSafeInteger(file.size) || file.size < 1) throw discordRefusal('resource-unavailable');
|
|
17
|
+
if (file.size > MAX_BYTES) throw discordRefusal('artifact-too-large');
|
|
18
|
+
return Object.freeze({
|
|
19
|
+
id: createHash('sha256').update(JSON.stringify([route.conversationId, route.threadId ?? null, route.messageId, file.id])).digest('hex'),
|
|
20
|
+
messageId: route.messageId, resourceKey: file.id, name: file.filename, sizeBytes: file.size,
|
|
21
|
+
...(typeof file.content_type === 'string' && file.content_type.length <= 127 && MIME.test(file.content_type)
|
|
22
|
+
? { mediaType: file.content_type } : {}),
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Safe native metadata only; signed URLs never leave the Provider. */
|
|
27
|
+
export function externalAttachments(evidence, source) {
|
|
28
|
+
const attachment = attachmentFor(source, evidence.reply);
|
|
29
|
+
return attachment ? Object.freeze({ ...evidence, attachments: Object.freeze([attachment]) }) : evidence;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export async function readExternalFile(api, account, route, attachment, { signal, assertCurrent }) {
|
|
33
|
+
assertCurrent();
|
|
34
|
+
const checked = await qualifyDiscordReply(api, account, route, signal, { forHistory: true });
|
|
35
|
+
assertCurrent();
|
|
36
|
+
if (!checked.source.mentions?.some(user => user.id === account.userId)) throw discordRefusal('stale-route');
|
|
37
|
+
const current = attachmentFor(checked.source, route);
|
|
38
|
+
if (!current || !attachment || ['id', 'messageId', 'resourceKey', 'name', 'sizeBytes', 'mediaType']
|
|
39
|
+
.some(key => current[key] !== attachment[key])) throw discordRefusal('stale-route');
|
|
40
|
+
const { stream, length, cancel } = await api.downloadFileStream({ url: checked.source.attachments[0].url,
|
|
41
|
+
channelId: checked.channel.channelId, attachmentId: current.resourceKey, fileName: current.name, signal });
|
|
42
|
+
const iterator = stream?.[Symbol.asyncIterator]?.();
|
|
43
|
+
try {
|
|
44
|
+
assertCurrent();
|
|
45
|
+
if (!iterator || (length !== null && length !== undefined && (!/^\d+$/.test(length) || Number(length) !== current.sizeBytes)))
|
|
46
|
+
throw discordRefusal('resource-unavailable');
|
|
47
|
+
} catch (error) { await cancel?.(); throw error; }
|
|
48
|
+
return (async function* () {
|
|
49
|
+
let size = 0;
|
|
50
|
+
try {
|
|
51
|
+
assertCurrent();
|
|
52
|
+
while (true) {
|
|
53
|
+
assertCurrent();
|
|
54
|
+
const next = await iterator.next();
|
|
55
|
+
assertCurrent();
|
|
56
|
+
if (next.done) break;
|
|
57
|
+
const chunk = next.value;
|
|
58
|
+
if (!(chunk instanceof Uint8Array)) throw discordRefusal('resource-unavailable');
|
|
59
|
+
size += chunk.byteLength;
|
|
60
|
+
if (size > MAX_BYTES || size > current.sizeBytes) throw discordRefusal('artifact-too-large');
|
|
61
|
+
yield new Uint8Array(chunk);
|
|
62
|
+
}
|
|
63
|
+
assertCurrent();
|
|
64
|
+
if (size !== current.sizeBytes) throw discordRefusal('resource-unavailable');
|
|
65
|
+
} finally { await cancel?.(); await iterator.return?.(); }
|
|
66
|
+
})();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export async function replyExternalFile(api, account, route, file, { signal, beforeSend, assertCurrent }) {
|
|
70
|
+
if (typeof file?.id !== 'string' || !file.id || !safeName(file.name) || !(file.bytes instanceof Uint8Array)
|
|
71
|
+
|| file.bytes.byteLength < 1 || file.bytes.byteLength > MAX_BYTES
|
|
72
|
+
|| (file.mediaType !== undefined && (typeof file.mediaType !== 'string' || file.mediaType.length > 127 || !MIME.test(file.mediaType))))
|
|
73
|
+
throw discordRefusal('bad-request');
|
|
74
|
+
assertCurrent();
|
|
75
|
+
const checked = await qualifyDiscordReply(api, account, route, signal, { forReply: true, forFileReply: true });
|
|
76
|
+
assertCurrent();
|
|
77
|
+
if (beforeSend && beforeSend() !== true) throw discordRefusal('stale-route');
|
|
78
|
+
signal?.throwIfAborted();
|
|
79
|
+
let sent;
|
|
80
|
+
try {
|
|
81
|
+
sent = await api.createFileMessage({ channelId: checked.channel.channelId,
|
|
82
|
+
replyToMessageId: route.messageId, signal, retry: false, failIfNotExists: true,
|
|
83
|
+
file: { fileName: file.name, bytes: Buffer.from(file.bytes), mediaType: file.mediaType } });
|
|
84
|
+
} catch (error) {
|
|
85
|
+
if (error?.code === 'artifact-too-large') throw discordRefusal('artifact-too-large');
|
|
86
|
+
if (['artifact-permission-required', 'artifact-provider-rejected', 'artifact-rate-limited'].includes(error?.code))
|
|
87
|
+
throw discordRefusal('file-provider-rejected');
|
|
88
|
+
throw discordRefusal('reply-result-unknown');
|
|
89
|
+
}
|
|
90
|
+
const result = sent?.attachments?.[0];
|
|
91
|
+
if (!discordSnowflake(sent?.id) || sent.channel_id !== checked.channel.channelId
|
|
92
|
+
|| sent.author?.id !== account.userId || sent.author.bot !== true
|
|
93
|
+
|| sent.message_reference?.message_id !== route.messageId || sent.message_reference?.channel_id !== checked.channel.channelId
|
|
94
|
+
|| sent.attachments?.length !== 1 || !discordSnowflake(result?.id)
|
|
95
|
+
|| result.filename !== file.name || result.size !== file.bytes.byteLength) throw discordRefusal('reply-result-unknown');
|
|
96
|
+
return { sent: true };
|
|
97
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { createHash, createHmac, randomBytes, timingSafeEqual } from 'node:crypto';
|
|
2
|
+
import { discordRefusal, inspectDiscordSourceChannel, normalizeDiscordHistoryText,
|
|
3
|
+
verifiedDiscordAccount } from './external-consumer.mjs';
|
|
4
|
+
import { createDiscordNearbyReader } from './nearby-history.mjs';
|
|
5
|
+
|
|
6
|
+
const epoch = 1420070400000n;
|
|
7
|
+
const coverage = 'provider-visible-human-text';
|
|
8
|
+
const contentFlags = (1 << 18) | (1 << 19);
|
|
9
|
+
const snowflake = value => typeof value === 'string' && /^[1-9][0-9]{4,19}$/.test(value)
|
|
10
|
+
&& BigInt(value) <= (1n << 64n) - 1n;
|
|
11
|
+
|
|
12
|
+
/** Bounded native pages; runtime-local cursors contain IDs, never text or credentials. */
|
|
13
|
+
export function createDiscordHistoryReader({ now = Date.now } = {}) {
|
|
14
|
+
const nearby = createDiscordNearbyReader({ now });
|
|
15
|
+
const key = randomBytes(32);
|
|
16
|
+
const sign = body => createHmac('sha256', key).update(body).digest('base64url');
|
|
17
|
+
const encode = (binding, before) => {
|
|
18
|
+
const body = Buffer.from(JSON.stringify({ binding, before })).toString('base64url');
|
|
19
|
+
return `${body}.${sign(body)}`;
|
|
20
|
+
};
|
|
21
|
+
const decode = (cursor, binding) => {
|
|
22
|
+
try {
|
|
23
|
+
if (typeof cursor !== 'string' || !cursor || cursor.length > 4096) throw new Error();
|
|
24
|
+
const parts = cursor.split('.');
|
|
25
|
+
if (parts.length !== 2) throw new Error();
|
|
26
|
+
const expected = Buffer.from(sign(parts[0]));
|
|
27
|
+
const actual = Buffer.from(parts[1]);
|
|
28
|
+
if (expected.length !== actual.length || !timingSafeEqual(expected, actual)) throw new Error();
|
|
29
|
+
const value = JSON.parse(Buffer.from(parts[0], 'base64url').toString());
|
|
30
|
+
if (value.binding !== binding || !snowflake(value.before)) throw new Error();
|
|
31
|
+
return value.before;
|
|
32
|
+
} catch { throw discordRefusal('invalid-history-query'); }
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
return async function read(api, identity, route, query, signal, assertCurrent = () => {}) {
|
|
36
|
+
const check = () => {
|
|
37
|
+
if (signal?.aborted) throw discordRefusal('cancelled');
|
|
38
|
+
assertCurrent();
|
|
39
|
+
};
|
|
40
|
+
const checked = async operation => { check(); const result = await operation(); check(); return result; };
|
|
41
|
+
try {
|
|
42
|
+
check();
|
|
43
|
+
if (!query || !['group', 'thread', 'nearby'].includes(query.scope)
|
|
44
|
+
|| !Number.isInteger(query.limit) || query.limit < 1 || query.limit > 20
|
|
45
|
+
|| (query.scope !== 'nearby' && (query.beforeCount !== undefined || query.afterCount !== undefined))
|
|
46
|
+
|| ![query.beforeCount ?? 10, query.afterCount ?? 5].every(n => Number.isInteger(n) && n >= 0 && n <= 20))
|
|
47
|
+
throw discordRefusal('invalid-history-query');
|
|
48
|
+
if (!route || !snowflake(route.messageId) || !snowflake(route.actorId)
|
|
49
|
+
|| !snowflake(route.conversationId) || route.rootId !== undefined || route.parentId !== undefined
|
|
50
|
+
|| (route.threadId !== undefined && !snowflake(route.threadId))) throw discordRefusal('stale-route');
|
|
51
|
+
if (query.scope === 'thread' && !route.threadId) throw discordRefusal('thread-unavailable');
|
|
52
|
+
const sourceRoute = { messageId: route.messageId, actorId: route.actorId,
|
|
53
|
+
conversationId: route.conversationId, ...(route.threadId ? { threadId: route.threadId } : {}) };
|
|
54
|
+
const binding = createHash('sha256').update(JSON.stringify({ botId: identity.botId,
|
|
55
|
+
fingerprint: identity.account.fingerprint, route: sourceRoute,
|
|
56
|
+
scope: query.scope, limit: query.limit,
|
|
57
|
+
...(query.scope === 'nearby' ? { beforeCount: query.beforeCount ?? 10, afterCount: query.afterCount ?? 5 } : {}) })).digest('hex');
|
|
58
|
+
const state = query.scope === 'nearby' ? nearby.prepare(binding, route, query) : undefined;
|
|
59
|
+
const before = state ? undefined : query.cursor === undefined
|
|
60
|
+
? String((BigInt(now()) - epoch + 1n) << 22n) : decode(query.cursor, binding);
|
|
61
|
+
if (!state && !snowflake(before)) throw discordRefusal('history-unavailable');
|
|
62
|
+
// HTTP content censorship is independent of the Gateway identify mask. Refuse even on
|
|
63
|
+
// an empty/mention-only page when the native App cannot expose ordinary human content.
|
|
64
|
+
const user = await checked(() => api.getCurrentUser({ signal }));
|
|
65
|
+
const application = await checked(() => api.getCurrentApplication({ signal }));
|
|
66
|
+
const current = verifiedDiscordAccount(user, application);
|
|
67
|
+
if (current.fingerprint !== identity.account.fingerprint) throw discordRefusal('account-changed');
|
|
68
|
+
if (!Number.isSafeInteger(application.flags) || (application.flags & contentFlags) === 0)
|
|
69
|
+
throw discordRefusal('history-permission-denied');
|
|
70
|
+
const sourceChannel = await checked(() => inspectDiscordSourceChannel(api,
|
|
71
|
+
route.threadId ?? route.conversationId, current, signal, { forHistory: true }));
|
|
72
|
+
if (sourceChannel.conversationId !== route.conversationId || sourceChannel.threadId !== route.threadId)
|
|
73
|
+
throw discordRefusal('stale-route');
|
|
74
|
+
const source = await checked(() => api.getMessage({ channelId: sourceChannel.channelId,
|
|
75
|
+
messageId: route.messageId, signal }));
|
|
76
|
+
if (source?.id !== route.messageId || source.channel_id !== sourceChannel.channelId
|
|
77
|
+
|| (source.guild_id !== undefined && source.guild_id !== sourceChannel.guildId)
|
|
78
|
+
|| source.author?.id !== route.actorId || source.author.bot === true || source.webhook_id
|
|
79
|
+
|| ![0, 19].includes(source.type)) throw discordRefusal('stale-route');
|
|
80
|
+
const channel = query.scope === 'thread' || query.scope === 'nearby' ? sourceChannel : await checked(() =>
|
|
81
|
+
inspectDiscordSourceChannel(api, route.conversationId, current, signal, { forHistory: true }));
|
|
82
|
+
if (channel.guildId !== sourceChannel.guildId || channel.conversationId !== route.conversationId)
|
|
83
|
+
throw discordRefusal('stale-route');
|
|
84
|
+
const validate = message => {
|
|
85
|
+
if (!snowflake(message?.id) || message.channel_id !== channel.channelId
|
|
86
|
+
|| (message.guild_id !== undefined && message.guild_id !== channel.guildId)) throw discordRefusal('stale-route');
|
|
87
|
+
};
|
|
88
|
+
const normalize = message => { validate(message); return normalizeDiscordHistoryText({ ...message, guild_id: channel.guildId },
|
|
89
|
+
{ ...identity, channel, eventId: `history:${channel.channelId}:${message.id}` }); };
|
|
90
|
+
const page = async (boundary, limit) => {
|
|
91
|
+
const messages = await checked(() => api.getMessages({ channelId: channel.channelId, before: boundary, limit, signal }));
|
|
92
|
+
if (!Array.isArray(messages) || messages.length > limit) throw discordRefusal('history-unavailable');
|
|
93
|
+
let last = boundary;
|
|
94
|
+
for (const message of messages) { validate(message);
|
|
95
|
+
if (BigInt(message.id) >= BigInt(last)) throw discordRefusal('stale-route');
|
|
96
|
+
last = message.id;
|
|
97
|
+
}
|
|
98
|
+
return messages;
|
|
99
|
+
};
|
|
100
|
+
let result;
|
|
101
|
+
if (state) result = await nearby.read({ binding, state, query, route, source, page, normalize,
|
|
102
|
+
message: async id => {
|
|
103
|
+
let value;
|
|
104
|
+
try { value = await checked(() => api.getMessage({ channelId: channel.channelId, messageId: id, signal })); }
|
|
105
|
+
catch (error) { if (error?.status === 404) return null; throw error; }
|
|
106
|
+
validate(value);
|
|
107
|
+
if (value.id !== id) throw discordRefusal('stale-route');
|
|
108
|
+
return value;
|
|
109
|
+
} });
|
|
110
|
+
else {
|
|
111
|
+
const messages = await page(before, query.limit);
|
|
112
|
+
const events = messages.map(normalize).filter(Boolean);
|
|
113
|
+
const hasMore = messages.length === query.limit;
|
|
114
|
+
result = { version: 1, scope: query.scope, events, omitted: messages.length - events.length,
|
|
115
|
+
hasMore, ...(hasMore ? { nextCursor: encode(binding, messages.at(-1).id) } : {}), coverage };
|
|
116
|
+
}
|
|
117
|
+
// Recheck permission after the HTTP page: Discord can return 200 with no messages
|
|
118
|
+
// on loss of READ_MESSAGE_HISTORY, or censor content on loss of the App flag.
|
|
119
|
+
const finalApp = await checked(() => api.getCurrentApplication({ signal }));
|
|
120
|
+
if (verifiedDiscordAccount(user, finalApp).fingerprint !== identity.account.fingerprint)
|
|
121
|
+
throw discordRefusal('account-changed');
|
|
122
|
+
if (!Number.isSafeInteger(finalApp.flags) || (finalApp.flags & contentFlags) === 0)
|
|
123
|
+
throw discordRefusal('history-permission-denied');
|
|
124
|
+
const finalChannel = await checked(() => inspectDiscordSourceChannel(api,
|
|
125
|
+
channel.channelId, current, signal, { forHistory: true }));
|
|
126
|
+
if (finalChannel.guildId !== channel.guildId || finalChannel.conversationId !== channel.conversationId
|
|
127
|
+
|| finalChannel.threadId !== channel.threadId) throw discordRefusal('stale-route');
|
|
128
|
+
check();
|
|
129
|
+
return result;
|
|
130
|
+
} catch (error) {
|
|
131
|
+
check();
|
|
132
|
+
if (['invalid-history-query', 'stale-route', 'account-changed', 'account-unverified',
|
|
133
|
+
'history-permission-denied', 'history-unavailable', 'thread-unavailable', 'cancelled',
|
|
134
|
+
'capability-unavailable'].includes(error?.code)) throw error;
|
|
135
|
+
if (error?.code === 'reply-permission-denied' || error?.status === 401 || error?.status === 403)
|
|
136
|
+
throw discordRefusal('history-permission-denied');
|
|
137
|
+
if (error?.name === 'AbortError') throw discordRefusal('cancelled');
|
|
138
|
+
if (error?.status === 404 && query?.scope === 'thread') throw discordRefusal('thread-unavailable');
|
|
139
|
+
throw discordRefusal('history-unavailable');
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { createHmac, randomBytes, timingSafeEqual } from 'node:crypto';
|
|
2
|
+
import { discordRefusal } from './external-consumer.mjs';
|
|
3
|
+
|
|
4
|
+
const epoch = 1420070400000n;
|
|
5
|
+
const ttl = 30 * 60 * 1000;
|
|
6
|
+
const idAt = ms => String((BigInt(Math.max(Number(epoch), ms)) - epoch) << 22n);
|
|
7
|
+
const compare = (a, b) => BigInt(a) < BigInt(b) ? -1 : BigInt(a) > BigInt(b) ? 1 : 0;
|
|
8
|
+
|
|
9
|
+
/** Query state is runtime-local, signed and body-free; native text remains provider-owned. */
|
|
10
|
+
export function createDiscordNearbyReader({ now = Date.now } = {}) {
|
|
11
|
+
const key = randomBytes(32);
|
|
12
|
+
const sign = body => createHmac('sha256', key).update(body).digest('base64url');
|
|
13
|
+
const encode = (binding, state) => {
|
|
14
|
+
const body = Buffer.from(JSON.stringify({ binding, state: { ...state, expiresAt: now() + ttl } })).toString('base64url');
|
|
15
|
+
const cursor = `${body}.${sign(body)}`;
|
|
16
|
+
if (cursor.length > 4096) throw discordRefusal('history-unavailable');
|
|
17
|
+
return cursor;
|
|
18
|
+
};
|
|
19
|
+
function prepare(binding, route, query) {
|
|
20
|
+
if (query.cursor !== undefined) {
|
|
21
|
+
try {
|
|
22
|
+
if (typeof query.cursor !== 'string' || !query.cursor || query.cursor.length > 4096) throw new Error();
|
|
23
|
+
const parts = query.cursor.split('.');
|
|
24
|
+
if (parts.length !== 2) throw new Error();
|
|
25
|
+
const expected = Buffer.from(sign(parts[0])); const actual = Buffer.from(parts[1]);
|
|
26
|
+
if (expected.length !== actual.length || !timingSafeEqual(expected, actual)) throw new Error();
|
|
27
|
+
const value = JSON.parse(Buffer.from(parts[0], 'base64url').toString());
|
|
28
|
+
if (value.binding !== binding || value.state.expiresAt <= now()) throw new Error();
|
|
29
|
+
return value.state;
|
|
30
|
+
} catch { throw discordRefusal('invalid-history-query'); }
|
|
31
|
+
}
|
|
32
|
+
const center = Number((BigInt(route.messageId) >> 22n) + epoch);
|
|
33
|
+
const snapshot = idAt(now() + 1);
|
|
34
|
+
if (compare(route.messageId, snapshot) >= 0) throw discordRefusal('stale-route');
|
|
35
|
+
const low = idAt(center - 300000);
|
|
36
|
+
const high = idAt(center + 300001); // Exclusive boundary includes every Snowflake in the end millisecond.
|
|
37
|
+
const before = compare(snapshot, high) < 0 ? snapshot : high;
|
|
38
|
+
return { phase: 'window', low, high, snapshot, before, center,
|
|
39
|
+
beforeCount: 0, afterCount: 0, anchorSeen: false, candidates: [] };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async function read({ binding, state, query, route, source, page, message, normalize }) {
|
|
43
|
+
const minBefore = query.beforeCount ?? 10;
|
|
44
|
+
const minAfter = query.afterCount ?? 5;
|
|
45
|
+
const events = []; let omitted = 0;
|
|
46
|
+
const needsAfter = () => state.afterCount < minAfter && compare(state.high, state.snapshot) < 0;
|
|
47
|
+
const endWindow = () => {
|
|
48
|
+
state.phase = needsAfter() ? 'after' : 'done';
|
|
49
|
+
if (state.phase === 'after') state.before = state.snapshot;
|
|
50
|
+
};
|
|
51
|
+
if (state.phase === 'window') {
|
|
52
|
+
if (!state.anchorSeen) {
|
|
53
|
+
const anchor = normalize(source);
|
|
54
|
+
if (anchor) events.push(anchor); else omitted++;
|
|
55
|
+
state.anchorSeen = true;
|
|
56
|
+
}
|
|
57
|
+
const room = query.limit - events.length - omitted;
|
|
58
|
+
if (room > 0) {
|
|
59
|
+
const rows = await page(state.before, room);
|
|
60
|
+
let crossed = false;
|
|
61
|
+
for (const row of rows) {
|
|
62
|
+
if (row.id === route.messageId) continue;
|
|
63
|
+
const event = normalize(row);
|
|
64
|
+
if (compare(row.id, state.low) >= 0) {
|
|
65
|
+
if (event) {
|
|
66
|
+
if (compare(row.id, route.messageId) < 0) state.beforeCount = Math.min(20, state.beforeCount + 1);
|
|
67
|
+
else state.afterCount = Math.min(20, state.afterCount + 1);
|
|
68
|
+
events.push(event);
|
|
69
|
+
} else omitted++;
|
|
70
|
+
} else {
|
|
71
|
+
crossed = true;
|
|
72
|
+
if (event && state.beforeCount < minBefore) { state.beforeCount++; events.push(event); }
|
|
73
|
+
else omitted++;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (rows.length) state.before = rows.at(-1).id;
|
|
77
|
+
if (rows.length < room || (crossed && state.beforeCount >= minBefore)) endWindow();
|
|
78
|
+
}
|
|
79
|
+
} else if (state.phase === 'after') {
|
|
80
|
+
// Discord lists newest first. Scan toward the upper window edge, retaining only
|
|
81
|
+
// the closest required IDs; do not emit farther candidates before the scan settles.
|
|
82
|
+
const rows = await page(state.before, query.limit);
|
|
83
|
+
let crossed = false;
|
|
84
|
+
for (const row of rows) {
|
|
85
|
+
if (compare(row.id, state.high) < 0) { crossed = true; continue; }
|
|
86
|
+
const event = normalize(row);
|
|
87
|
+
if (!event) { omitted++; continue; }
|
|
88
|
+
state.candidates.push(row.id);
|
|
89
|
+
state.candidates.sort(compare);
|
|
90
|
+
state.candidates = state.candidates.slice(0, minAfter - state.afterCount);
|
|
91
|
+
}
|
|
92
|
+
if (rows.length) state.before = rows.at(-1).id;
|
|
93
|
+
if (rows.length < query.limit || crossed) {
|
|
94
|
+
state.candidates.sort((a, b) => compare(b, a));
|
|
95
|
+
state.phase = state.candidates.length ? 'emit' : 'done';
|
|
96
|
+
}
|
|
97
|
+
} else if (state.phase === 'emit') {
|
|
98
|
+
for (const id of state.candidates.splice(0, query.limit)) {
|
|
99
|
+
const row = await message(id);
|
|
100
|
+
const event = row ? normalize(row) : null;
|
|
101
|
+
if (event) events.push(event); else omitted++;
|
|
102
|
+
}
|
|
103
|
+
if (!state.candidates.length) state.phase = 'done';
|
|
104
|
+
}
|
|
105
|
+
const hasMore = state.phase !== 'done';
|
|
106
|
+
return { version: 1, scope: 'nearby', events, omitted, hasMore,
|
|
107
|
+
...(hasMore ? { nextCursor: encode(binding, state) } : {}),
|
|
108
|
+
window: { start: Math.floor((state.center - 300000) / 1000), end: Math.floor((state.center + 300000) / 1000) + 1 },
|
|
109
|
+
coverage: 'provider-visible-human-text' };
|
|
110
|
+
}
|
|
111
|
+
return { prepare, read };
|
|
112
|
+
}
|