@botharness/im-provider 4.32.0-botharness.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/PROVENANCE.json +25 -0
- package/README.en.md +445 -0
- package/README.md +448 -0
- package/THIRD_PARTY_NOTICES.md +93 -0
- package/assets/dingtalk-menu-template.json +6 -0
- package/assets/logo-dsh-im-chinese-readme-3x2.png +0 -0
- package/assets/logo-dsh-im-connecting-readme-3x2.png +0 -0
- package/assets/logo-dsh-im-connecting-square-v4.png +0 -0
- package/assets/logo-icon.png +0 -0
- package/assets/logo-plugin-message-link-gradient.webp +0 -0
- package/assets/logo-plugin-phone.png +0 -0
- package/assets/logo.png +0 -0
- package/assets/logo_cn.png +0 -0
- package/assets/screenshot-menu-card.png +0 -0
- package/cordis.patch.yml +3 -0
- package/lib/client.js +20330 -0
- package/lib/index.js +602 -0
- package/locale/en.json +5 -0
- package/locale/zh.json +5 -0
- package/package.json +181 -0
- package/plugin-src/client/access-policy-settings.js +362 -0
- package/plugin-src/client/agent-preset.js +119 -0
- package/plugin-src/client/bot-alias.js +169 -0
- package/plugin-src/client/build.mjs +38 -0
- package/plugin-src/client/channel-card-meta.js +132 -0
- package/plugin-src/client/channel-logos.js +190 -0
- package/plugin-src/client/channels/dingtalk/api.js +267 -0
- package/plugin-src/client/channels/dingtalk/index.js +954 -0
- package/plugin-src/client/channels/dingtalk/styles.js +136 -0
- package/plugin-src/client/channels/discord/api.js +11 -0
- package/plugin-src/client/channels/discord/index.js +25 -0
- package/plugin-src/client/channels/discord/styles.js +19 -0
- package/plugin-src/client/channels/email/api.js +52 -0
- package/plugin-src/client/channels/email/index.js +728 -0
- package/plugin-src/client/channels/email/styles.js +51 -0
- package/plugin-src/client/channels/feishu/api.js +405 -0
- package/plugin-src/client/channels/feishu/group-settings.js +581 -0
- package/plugin-src/client/channels/feishu/index.js +1583 -0
- package/plugin-src/client/channels/feishu/slash-panel-settings.js +322 -0
- package/plugin-src/client/channels/feishu/styles.js +573 -0
- package/plugin-src/client/channels/feishu/voice-editor.js +115 -0
- package/plugin-src/client/channels/feishu/voice-settings.js +71 -0
- package/plugin-src/client/channels/imessage/api.js +12 -0
- package/plugin-src/client/channels/imessage/index.js +46 -0
- package/plugin-src/client/channels/imessage/styles.js +19 -0
- package/plugin-src/client/channels/matrix/api.js +11 -0
- package/plugin-src/client/channels/matrix/index.js +151 -0
- package/plugin-src/client/channels/matrix/styles.js +36 -0
- package/plugin-src/client/channels/office/api.js +46 -0
- package/plugin-src/client/channels/office/index.js +153 -0
- package/plugin-src/client/channels/office/styles.js +50 -0
- package/plugin-src/client/channels/qq/api.js +171 -0
- package/plugin-src/client/channels/qq/index.js +593 -0
- package/plugin-src/client/channels/qq/styles.js +19 -0
- package/plugin-src/client/channels/shared/collapsible-account.js +103 -0
- package/plugin-src/client/channels/shared/token-api.js +124 -0
- package/plugin-src/client/channels/shared/token-channel.js +495 -0
- package/plugin-src/client/channels/slack/api.js +11 -0
- package/plugin-src/client/channels/slack/index.js +131 -0
- package/plugin-src/client/channels/slack/styles.js +34 -0
- package/plugin-src/client/channels/telegram/api.js +11 -0
- package/plugin-src/client/channels/telegram/index.js +28 -0
- package/plugin-src/client/channels/telegram/styles.js +31 -0
- package/plugin-src/client/channels/telegram/thinking-traces.js +25 -0
- package/plugin-src/client/channels/wecom/api.js +160 -0
- package/plugin-src/client/channels/wecom/index.js +625 -0
- package/plugin-src/client/channels/wecom/styles.js +19 -0
- package/plugin-src/client/channels/wecom-app/api.js +132 -0
- package/plugin-src/client/channels/wecom-app/index.js +634 -0
- package/plugin-src/client/channels/wecom-app/styles.js +25 -0
- package/plugin-src/client/channels/weixin/api.js +211 -0
- package/plugin-src/client/channels/weixin/connection-error.js +1 -0
- package/plugin-src/client/channels/weixin/index.js +801 -0
- package/plugin-src/client/channels/weixin/styles.js +115 -0
- package/plugin-src/client/channels/whatsapp/api.js +152 -0
- package/plugin-src/client/channels/whatsapp/index.js +609 -0
- package/plugin-src/client/channels/whatsapp/styles.js +19 -0
- package/plugin-src/client/connection-error.js +87 -0
- package/plugin-src/client/context-enhancement.js +415 -0
- package/plugin-src/client/credential-binding.js +118 -0
- package/plugin-src/client/delivery-settings.js +905 -0
- package/plugin-src/client/global-settings.js +433 -0
- package/plugin-src/client/i18n.js +1314 -0
- package/plugin-src/client/index.js +624 -0
- package/plugin-src/client/interface-language.js +89 -0
- package/plugin-src/client/last-message-error.js +18 -0
- package/plugin-src/client/lifecycle.js +86 -0
- package/plugin-src/client/loopback-recovery.js +75 -0
- package/plugin-src/client/model-setting.js +172 -0
- package/plugin-src/client/panel-error-boundary.js +21 -0
- package/plugin-src/client/plugin-page.js +51 -0
- package/plugin-src/client/session-channel-logos.js +243 -0
- package/plugin-src/client/styles.js +770 -0
- package/plugin-src/client/update-panel.js +14 -0
- package/plugin-src/client/workspace-directory-picker.js +280 -0
- package/plugin-src/client/workspace-editor.js +69 -0
- package/plugin-src/client/workspace-snapshot-fence.js +28 -0
- package/plugin-src/host/build.mjs +52 -0
- package/plugin-src/host/channels/dingtalk/connection-supervisor.mjs +130 -0
- package/plugin-src/host/channels/dingtalk/index.mjs +35 -0
- package/plugin-src/host/channels/dingtalk/production.mjs +193 -0
- package/plugin-src/host/channels/dingtalk/rpc.mjs +359 -0
- package/plugin-src/host/channels/discord/index.mjs +33 -0
- package/plugin-src/host/channels/discord/production.mjs +17 -0
- package/plugin-src/host/channels/discord/rpc.mjs +21 -0
- package/plugin-src/host/channels/email/availability.mjs +60 -0
- package/plugin-src/host/channels/email/index.mjs +36 -0
- package/plugin-src/host/channels/email/production.mjs +58 -0
- package/plugin-src/host/channels/email/rpc.mjs +163 -0
- package/plugin-src/host/channels/feishu/connection-supervisor.mjs +167 -0
- package/plugin-src/host/channels/feishu/controller.mjs +172 -0
- package/plugin-src/host/channels/feishu/credential-store.mjs +84 -0
- package/plugin-src/host/channels/feishu/index.mjs +69 -0
- package/plugin-src/host/channels/feishu/production.mjs +318 -0
- package/plugin-src/host/channels/feishu/rpc.mjs +863 -0
- package/plugin-src/host/channels/imessage/index.mjs +21 -0
- package/plugin-src/host/channels/imessage/production.mjs +13 -0
- package/plugin-src/host/channels/imessage/rpc.mjs +78 -0
- package/plugin-src/host/channels/matrix/index.mjs +31 -0
- package/plugin-src/host/channels/matrix/production.mjs +241 -0
- package/plugin-src/host/channels/matrix/rpc.mjs +228 -0
- package/plugin-src/host/channels/office/index.mjs +14 -0
- package/plugin-src/host/channels/office/production.mjs +45 -0
- package/plugin-src/host/channels/office/rpc.mjs +51 -0
- package/plugin-src/host/channels/qq/connection-supervisor.mjs +124 -0
- package/plugin-src/host/channels/qq/index.mjs +28 -0
- package/plugin-src/host/channels/qq/production.mjs +181 -0
- package/plugin-src/host/channels/qq/rpc.mjs +250 -0
- package/plugin-src/host/channels/shared/access-policy-production.mjs +137 -0
- package/plugin-src/host/channels/shared/access-policy-rpc.mjs +17 -0
- package/plugin-src/host/channels/shared/agent-preset-rpc.mjs +25 -0
- package/plugin-src/host/channels/shared/bot-alias-rpc.mjs +15 -0
- package/plugin-src/host/channels/shared/connection-supervisor.mjs +133 -0
- package/plugin-src/host/channels/shared/context-enhancement-rpc.mjs +17 -0
- package/plugin-src/host/channels/shared/model-setting-rpc.mjs +18 -0
- package/plugin-src/host/channels/shared/production.mjs +237 -0
- package/plugin-src/host/channels/shared/rpc.mjs +226 -0
- package/plugin-src/host/channels/shared/startup-error.mjs +55 -0
- package/plugin-src/host/channels/shared/startup.mjs +76 -0
- package/plugin-src/host/channels/shared/thinking-traces-rpc.mjs +11 -0
- package/plugin-src/host/channels/shared/workspace-rpc.mjs +31 -0
- package/plugin-src/host/channels/slack/index.mjs +31 -0
- package/plugin-src/host/channels/slack/production.mjs +176 -0
- package/plugin-src/host/channels/slack/rpc.mjs +221 -0
- package/plugin-src/host/channels/telegram/index.mjs +33 -0
- package/plugin-src/host/channels/telegram/production.mjs +19 -0
- package/plugin-src/host/channels/telegram/rpc.mjs +22 -0
- package/plugin-src/host/channels/wecom/connection-supervisor.mjs +124 -0
- package/plugin-src/host/channels/wecom/index.mjs +34 -0
- package/plugin-src/host/channels/wecom/production.mjs +187 -0
- package/plugin-src/host/channels/wecom/rpc.mjs +255 -0
- package/plugin-src/host/channels/wecom-app/index.mjs +33 -0
- package/plugin-src/host/channels/wecom-app/production.mjs +221 -0
- package/plugin-src/host/channels/wecom-app/rpc.mjs +235 -0
- package/plugin-src/host/channels/weixin/connection-supervisor.mjs +139 -0
- package/plugin-src/host/channels/weixin/index.mjs +44 -0
- package/plugin-src/host/channels/weixin/production.mjs +257 -0
- package/plugin-src/host/channels/weixin/rpc.mjs +285 -0
- package/plugin-src/host/channels/whatsapp/index.mjs +33 -0
- package/plugin-src/host/channels/whatsapp/production.mjs +198 -0
- package/plugin-src/host/channels/whatsapp/rpc.mjs +242 -0
- package/plugin-src/host/delivery-adapter.mjs +388 -0
- package/plugin-src/host/delivery-http.mjs +132 -0
- package/plugin-src/host/delivery-rpc.mjs +173 -0
- package/plugin-src/host/delivery-service.mjs +552 -0
- package/plugin-src/host/delivery-suggestions.mjs +198 -0
- package/plugin-src/host/harness-command-executor.mjs +48 -0
- package/plugin-src/host/harness-connection.mjs +20 -0
- package/plugin-src/host/harness-session-coordinator.mjs +183 -0
- package/plugin-src/host/host-language-rpc.mjs +71 -0
- package/plugin-src/host/host-language.mjs +157 -0
- package/plugin-src/host/image-input-rpc.mjs +24 -0
- package/plugin-src/host/inbound-ttl-rpc.mjs +86 -0
- package/plugin-src/host/inbound-ttl-runtime.mjs +85 -0
- package/plugin-src/host/inbound-ttl-service.mjs +179 -0
- package/plugin-src/host/index.mjs +224 -0
- package/plugin-src/host/injected-context.mjs +104 -0
- package/plugin-src/host/lark-sdk-handshake-patch.mjs +181 -0
- package/plugin-src/host/modern-harness-api.mjs +738 -0
- package/plugin-src/host/rpc-authority.mjs +12 -0
- package/plugin-src/host/session-sync-coordinator.mjs +233 -0
- package/plugin-src/host/session-title-prefix.mjs +122 -0
- package/plugin-src/host/update-policy.mjs +31 -0
- package/plugin-src/host/update-rpc.mjs +16 -0
- package/plugin-src/host/update-runtime.mjs +339 -0
- package/plugin-src/host/update-service.mjs +392 -0
- package/plugin-src/management-rpc.mjs +87 -0
- package/src/channels/dingtalk/config-store.mjs +284 -0
- package/src/channels/dingtalk/connection-error.mjs +292 -0
- package/src/channels/dingtalk/device-auth.mjs +240 -0
- package/src/channels/dingtalk/dingtalk-api.mjs +1140 -0
- package/src/channels/dingtalk/dingtalk-bridge.mjs +1889 -0
- package/src/channels/dingtalk/dingtalk-card-stream.mjs +242 -0
- package/src/channels/dingtalk/dingtalk-controller.mjs +852 -0
- package/src/channels/dingtalk/dingtalk-menu.mjs +95 -0
- package/src/channels/dingtalk/dingtalk-runtime.mjs +482 -0
- package/src/channels/dingtalk/harness-client.mjs +19 -0
- package/src/channels/dingtalk/state-store.mjs +325 -0
- package/src/channels/discord/config-store.mjs +25 -0
- package/src/channels/discord/discord-api.mjs +347 -0
- package/src/channels/discord/discord-bridge.mjs +16 -0
- package/src/channels/discord/discord-controller.mjs +16 -0
- package/src/channels/discord/discord-runtime.mjs +974 -0
- package/src/channels/discord/harness-client.mjs +11 -0
- package/src/channels/discord/state-store.mjs +3 -0
- package/src/channels/email/config-store.mjs +194 -0
- package/src/channels/email/email-api.mjs +20 -0
- package/src/channels/email/email-bridge.mjs +17 -0
- package/src/channels/email/email-controller.mjs +824 -0
- package/src/channels/email/email-runtime.mjs +699 -0
- package/src/channels/email/harness-client.mjs +7 -0
- package/src/channels/email/mail-format.mjs +91 -0
- package/src/channels/email/state-store.mjs +176 -0
- package/src/channels/email/transport.mjs +64 -0
- package/src/channels/email/transports/agent-mail.mjs +604 -0
- package/src/channels/email/transports/agently-cli.mjs +288 -0
- package/src/channels/email/transports/imap-smtp.mjs +165 -0
- package/src/channels/feishu/bridge.mjs +6286 -0
- package/src/channels/feishu/config.mjs +71 -0
- package/src/channels/feishu/external-consumer.mjs +77 -0
- package/src/channels/feishu/external-files.mjs +111 -0
- package/src/channels/feishu/feishu-app.mjs +54 -0
- package/src/channels/feishu/feishu-cards.mjs +1285 -0
- package/src/channels/feishu/feishu-channel.mjs +651 -0
- package/src/channels/feishu/feishu-runtime.mjs +950 -0
- package/src/channels/feishu/group-message-permission-manager.mjs +72 -0
- package/src/channels/feishu/group-response-mode.mjs +15 -0
- package/src/channels/feishu/harness-client.mjs +19 -0
- package/src/channels/feishu/history-reader.mjs +77 -0
- package/src/channels/feishu/live-cot.mjs +260 -0
- package/src/channels/feishu/message-utils.mjs +613 -0
- package/src/channels/feishu/multi-bot-controller.mjs +1588 -0
- package/src/channels/feishu/nearby-history.mjs +72 -0
- package/src/channels/feishu/plugin-config-store.mjs +216 -0
- package/src/channels/feishu/plugin-controller.mjs +254 -0
- package/src/channels/feishu/registration-manager.mjs +349 -0
- package/src/channels/feishu/repair-manager.mjs +138 -0
- package/src/channels/feishu/reply-context.mjs +22 -0
- package/src/channels/feishu/slash-command-panel.mjs +155 -0
- package/src/channels/feishu/slash-command-registry.mjs +495 -0
- package/src/channels/feishu/state-store.mjs +214 -0
- package/src/channels/feishu/step-push-mode.mjs +25 -0
- package/src/channels/feishu/voice-config.mjs +34 -0
- package/src/channels/feishu/voice.mjs +134 -0
- package/src/channels/imessage/config-store.mjs +24 -0
- package/src/channels/imessage/controller.mjs +37 -0
- package/src/channels/imessage/harness-client.mjs +7 -0
- package/src/channels/imessage/imessage-api.mjs +194 -0
- package/src/channels/imessage/imessage-bridge.mjs +16 -0
- package/src/channels/imessage/runtime.mjs +102 -0
- package/src/channels/imessage/state-store.mjs +3 -0
- package/src/channels/matrix/matrix-api.mjs +696 -0
- package/src/channels/matrix/matrix-bridge.mjs +20 -0
- package/src/channels/matrix/matrix-config-store.mjs +368 -0
- package/src/channels/matrix/matrix-controller.mjs +404 -0
- package/src/channels/matrix/matrix-crypto-store.mjs +279 -0
- package/src/channels/matrix/matrix-crypto.mjs +1014 -0
- package/src/channels/matrix/matrix-harness-client.mjs +11 -0
- package/src/channels/matrix/matrix-normalize.mjs +357 -0
- package/src/channels/matrix/matrix-rich-text.mjs +313 -0
- package/src/channels/matrix/matrix-room-history.mjs +316 -0
- package/src/channels/matrix/matrix-runtime.mjs +1014 -0
- package/src/channels/office/config-store.mjs +107 -0
- package/src/channels/office/office-controller.mjs +178 -0
- package/src/channels/office/office-job-executor.mjs +391 -0
- package/src/channels/office/office-runtime.mjs +175 -0
- package/src/channels/office/office-transport.mjs +171 -0
- package/src/channels/office/protocol.mjs +43 -0
- package/src/channels/qq/config-store.mjs +173 -0
- package/src/channels/qq/harness-client.mjs +11 -0
- package/src/channels/qq/markdown-reply.mjs +446 -0
- package/src/channels/qq/qq-bridge.mjs +1507 -0
- package/src/channels/qq/qq-controller.mjs +514 -0
- package/src/channels/qq/qq-menu.mjs +208 -0
- package/src/channels/qq/qq-runtime.mjs +303 -0
- package/src/channels/qq/qr-auth.mjs +24 -0
- package/src/channels/qq/state-error.mjs +17 -0
- package/src/channels/qq/state-store.mjs +143 -0
- package/src/channels/shared/access-policy.mjs +210 -0
- package/src/channels/shared/agent-preset.mjs +76 -0
- package/src/channels/shared/batch-input.mjs +229 -0
- package/src/channels/shared/bot-alias.mjs +29 -0
- package/src/channels/shared/bot-workspace-store.mjs +2383 -0
- package/src/channels/shared/command-catalog.mjs +107 -0
- package/src/channels/shared/command-permission.mjs +32 -0
- package/src/channels/shared/compact-command.mjs +109 -0
- package/src/channels/shared/config-read-error.mjs +24 -0
- package/src/channels/shared/connection-error.mjs +191 -0
- package/src/channels/shared/connection-test.mjs +58 -0
- package/src/channels/shared/context-enhancement.mjs +372 -0
- package/src/channels/shared/control-command.mjs +111 -0
- package/src/channels/shared/conversation-state-store.mjs +165 -0
- package/src/channels/shared/default-workspace.mjs +31 -0
- package/src/channels/shared/deferred-delivery-coordinator.mjs +241 -0
- package/src/channels/shared/deferred-delivery.mjs +161 -0
- package/src/channels/shared/deferred-state.mjs +53 -0
- package/src/channels/shared/diagnostic-details.mjs +95 -0
- package/src/channels/shared/editable-message-stream.mjs +100 -0
- package/src/channels/shared/exclusive-inbound-consumers.mjs +71 -0
- package/src/channels/shared/file-download.mjs +64 -0
- package/src/channels/shared/harness-approval.mjs +508 -0
- package/src/channels/shared/harness-client.mjs +2143 -0
- package/src/channels/shared/harness-mux.mjs +123 -0
- package/src/channels/shared/harness-question.mjs +95 -0
- package/src/channels/shared/harness-session-binding.mjs +119 -0
- package/src/channels/shared/history-command.mjs +193 -0
- package/src/channels/shared/i18n-en/diagnostics.mjs +55 -0
- package/src/channels/shared/i18n-en/dingtalk.mjs +81 -0
- package/src/channels/shared/i18n-en/discord.mjs +10 -0
- package/src/channels/shared/i18n-en/email.mjs +25 -0
- package/src/channels/shared/i18n-en/feishu.mjs +404 -0
- package/src/channels/shared/i18n-en/image-input.mjs +18 -0
- package/src/channels/shared/i18n-en/matrix.mjs +85 -0
- package/src/channels/shared/i18n-en/office.mjs +23 -0
- package/src/channels/shared/i18n-en/qq.mjs +96 -0
- package/src/channels/shared/i18n-en/shared-a.mjs +241 -0
- package/src/channels/shared/i18n-en/shared-b.mjs +263 -0
- package/src/channels/shared/i18n-en/shared-c.mjs +203 -0
- package/src/channels/shared/i18n-en/slack.mjs +27 -0
- package/src/channels/shared/i18n-en/telegram.mjs +27 -0
- package/src/channels/shared/i18n-en/wecom-app.mjs +33 -0
- package/src/channels/shared/i18n-en/wecom.mjs +62 -0
- package/src/channels/shared/i18n-en/weixin.mjs +88 -0
- package/src/channels/shared/i18n-en/whatsapp.mjs +17 -0
- package/src/channels/shared/i18n-en.mjs +45 -0
- package/src/channels/shared/i18n.mjs +78 -0
- package/src/channels/shared/im-source-guidance.mjs +65 -0
- package/src/channels/shared/image-input-policy.mjs +35 -0
- package/src/channels/shared/image-input-settings-store.mjs +60 -0
- package/src/channels/shared/image-input.mjs +124 -0
- package/src/channels/shared/image-prompt.mjs +360 -0
- package/src/channels/shared/inbound-access.mjs +43 -0
- package/src/channels/shared/inbound-file.mjs +323 -0
- package/src/channels/shared/inbound-ttl-store.mjs +110 -0
- package/src/channels/shared/inbound-ttl.mjs +22 -0
- package/src/channels/shared/injected-context.mjs +362 -0
- package/src/channels/shared/interface-language-store.mjs +127 -0
- package/src/channels/shared/interface-language.mjs +51 -0
- package/src/channels/shared/message-failure.mjs +257 -0
- package/src/channels/shared/model-command.mjs +741 -0
- package/src/channels/shared/model-setting.mjs +145 -0
- package/src/channels/shared/preset-command.mjs +308 -0
- package/src/channels/shared/semantic/artifact-delivery.mjs +177 -0
- package/src/channels/shared/semantic/artifact.mjs +750 -0
- package/src/channels/shared/semantic/delivery.mjs +193 -0
- package/src/channels/shared/semantic/reply-reference.mjs +148 -0
- package/src/channels/shared/session-binding-lock.mjs +40 -0
- package/src/channels/shared/session-channel-labels.mjs +28 -0
- package/src/channels/shared/session-reply-recovery.mjs +104 -0
- package/src/channels/shared/session-sync-registry.mjs +22 -0
- package/src/channels/shared/session-title.mjs +74 -0
- package/src/channels/shared/status-reaction.mjs +107 -0
- package/src/channels/shared/text-harness-bridge.mjs +1623 -0
- package/src/channels/shared/token-bot-controller.mjs +417 -0
- package/src/channels/shared/token-config-store.mjs +208 -0
- package/src/channels/shared/workspace-command.mjs +532 -0
- package/src/channels/shared/workspace-session.mjs +196 -0
- package/src/channels/slack/config-store.mjs +177 -0
- package/src/channels/slack/external-consumer.mjs +80 -0
- package/src/channels/slack/external-files.mjs +90 -0
- package/src/channels/slack/harness-client.mjs +11 -0
- package/src/channels/slack/history-reader.mjs +203 -0
- package/src/channels/slack/manifest.mjs +37 -0
- package/src/channels/slack/slack-api.mjs +636 -0
- package/src/channels/slack/slack-bridge.mjs +20 -0
- package/src/channels/slack/slack-controller.mjs +468 -0
- package/src/channels/slack/slack-runtime.mjs +879 -0
- package/src/channels/slack/state-store.mjs +3 -0
- package/src/channels/telegram/config-store.mjs +89 -0
- package/src/channels/telegram/harness-client.mjs +11 -0
- package/src/channels/telegram/state-store.mjs +3 -0
- package/src/channels/telegram/telegram-api.mjs +486 -0
- package/src/channels/telegram/telegram-bridge.mjs +81 -0
- package/src/channels/telegram/telegram-controller.mjs +52 -0
- package/src/channels/telegram/telegram-http.mjs +21 -0
- package/src/channels/telegram/telegram-rich-message.mjs +149 -0
- package/src/channels/telegram/telegram-runtime.mjs +1471 -0
- package/src/channels/wecom/config-store.mjs +172 -0
- package/src/channels/wecom/harness-client.mjs +11 -0
- package/src/channels/wecom/qr-auth.mjs +95 -0
- package/src/channels/wecom/send-error.mjs +31 -0
- package/src/channels/wecom/state-store.mjs +107 -0
- package/src/channels/wecom/wecom-bridge.mjs +1833 -0
- package/src/channels/wecom/wecom-cards.mjs +101 -0
- package/src/channels/wecom/wecom-controller.mjs +530 -0
- package/src/channels/wecom/wecom-runtime.mjs +269 -0
- package/src/channels/wecom-app/callback-server.mjs +471 -0
- package/src/channels/wecom-app/config-store.mjs +242 -0
- package/src/channels/wecom-app/harness-client.mjs +11 -0
- package/src/channels/wecom-app/state-store.mjs +107 -0
- package/src/channels/wecom-app/wecom-app-api.mjs +432 -0
- package/src/channels/wecom-app/wecom-app-bridge.mjs +1072 -0
- package/src/channels/wecom-app/wecom-app-controller.mjs +453 -0
- package/src/channels/wecom-app/wecom-app-runtime.mjs +222 -0
- package/src/channels/weixin/config-store.mjs +192 -0
- package/src/channels/weixin/connection-error.en.mjs +137 -0
- package/src/channels/weixin/connection-error.mjs +153 -0
- package/src/channels/weixin/diagnostic-details.mjs +1 -0
- package/src/channels/weixin/harness-client.mjs +19 -0
- package/src/channels/weixin/state-store.mjs +291 -0
- package/src/channels/weixin/weixin-api.mjs +949 -0
- package/src/channels/weixin/weixin-bridge.mjs +1537 -0
- package/src/channels/weixin/weixin-controller.mjs +669 -0
- package/src/channels/weixin/weixin-runtime.mjs +416 -0
- package/src/channels/whatsapp/config-store.mjs +210 -0
- package/src/channels/whatsapp/harness-client.mjs +11 -0
- package/src/channels/whatsapp/state-store.mjs +3 -0
- package/src/channels/whatsapp/whatsapp-bridge.mjs +18 -0
- package/src/channels/whatsapp/whatsapp-controller.mjs +470 -0
- package/src/channels/whatsapp/whatsapp-runtime.mjs +917 -0
- package/src/channels/whatsapp/whatsapp-web-session.mjs +214 -0
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
import { deferredStateAccess, normalizeDeferredState } from '../shared/deferred-state.mjs';
|
|
2
|
+
import { randomUUID } from 'node:crypto';
|
|
3
|
+
import { mkdir, readFile, rename, unlink, writeFile } from 'node:fs/promises';
|
|
4
|
+
import { dirname } from 'node:path';
|
|
5
|
+
|
|
6
|
+
import { t } from '../shared/i18n.mjs';
|
|
7
|
+
|
|
8
|
+
const EMPTY_STATE = Object.freeze({
|
|
9
|
+
version: 1,
|
|
10
|
+
sessions: {},
|
|
11
|
+
seenMessageIds: [],
|
|
12
|
+
pendingSenders: {},
|
|
13
|
+
recentOutboundMessages: [],
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
export const DINGTALK_RECENT_OUTBOUND_LIMIT = 200;
|
|
17
|
+
export const DINGTALK_RECENT_OUTBOUND_TTL_MS = 30 * 24 * 60 * 60 * 1_000;
|
|
18
|
+
export const DINGTALK_RECENT_OUTBOUND_TEXT_LIMIT = 8_000;
|
|
19
|
+
export const DINGTALK_RECENT_OUTBOUND_MATCH_TOLERANCE_MS = 15_000;
|
|
20
|
+
|
|
21
|
+
function nonEmptyString(value) {
|
|
22
|
+
return typeof value === 'string' && value.trim() ? value.trim() : null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function displayName(value) {
|
|
26
|
+
return (nonEmptyString(value) ?? t('钉钉用户')).slice(0, 100);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function timestampMs(value) {
|
|
30
|
+
const number = typeof value === 'string' && value.trim() ? Number(value) : value;
|
|
31
|
+
if (!Number.isFinite(number) || number < 0) return null;
|
|
32
|
+
return Math.trunc(number < 10_000_000_000 ? number * 1_000 : number);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function providerMessageIds(value) {
|
|
36
|
+
if (!Array.isArray(value)) return [];
|
|
37
|
+
return [...new Set(value
|
|
38
|
+
.map((id) => (id === undefined || id === null ? null : nonEmptyString(String(id))))
|
|
39
|
+
.filter(Boolean))];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function truncateText(value) {
|
|
43
|
+
const text = nonEmptyString(value);
|
|
44
|
+
return text ? [...text].slice(0, DINGTALK_RECENT_OUTBOUND_TEXT_LIMIT).join('') : null;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function normalizeRecentOutboundMessage(value) {
|
|
48
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) return null;
|
|
49
|
+
const conversationKey = nonEmptyString(value.conversationKey);
|
|
50
|
+
const text = truncateText(value.text);
|
|
51
|
+
const sentAt = timestampMs(value.sentAt);
|
|
52
|
+
const completedAt = timestampMs(value.completedAt) ?? sentAt;
|
|
53
|
+
if (!conversationKey || !text || sentAt === null || completedAt === null) return null;
|
|
54
|
+
return {
|
|
55
|
+
conversationKey,
|
|
56
|
+
text,
|
|
57
|
+
sentAt,
|
|
58
|
+
completedAt: Math.max(sentAt, completedAt),
|
|
59
|
+
providerMessageIds: providerMessageIds(value.providerMessageIds),
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function recentOutboundMessages(value, now = Date.now()) {
|
|
64
|
+
if (!Array.isArray(value)) return [];
|
|
65
|
+
const cutoff = now - DINGTALK_RECENT_OUTBOUND_TTL_MS;
|
|
66
|
+
return value
|
|
67
|
+
.map(normalizeRecentOutboundMessage)
|
|
68
|
+
.filter((entry) => entry && entry.completedAt >= cutoff)
|
|
69
|
+
.slice(-DINGTALK_RECENT_OUTBOUND_LIMIT);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function normalizePendingSender(value, fallbackRequestId) {
|
|
73
|
+
if (!value || typeof value !== 'object') return null;
|
|
74
|
+
const requestId = nonEmptyString(value.requestId) ?? nonEmptyString(fallbackRequestId);
|
|
75
|
+
const staffId = nonEmptyString(value.staffId);
|
|
76
|
+
const requestedAt = nonEmptyString(value.requestedAt) ?? nonEmptyString(value.lastSeenAt);
|
|
77
|
+
const lastSeenAt = nonEmptyString(value.lastSeenAt) ?? requestedAt;
|
|
78
|
+
if (!requestId || !staffId || !requestedAt || !lastSeenAt) return null;
|
|
79
|
+
return {
|
|
80
|
+
requestId,
|
|
81
|
+
staffId,
|
|
82
|
+
displayName: displayName(value.displayName ?? value.nick),
|
|
83
|
+
requestedAt,
|
|
84
|
+
lastSeenAt,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function normalizeState(value) {
|
|
89
|
+
if (!value || typeof value !== 'object') return structuredClone(EMPTY_STATE);
|
|
90
|
+
const sessions = {};
|
|
91
|
+
if (value.sessions && typeof value.sessions === 'object' && !Array.isArray(value.sessions)) {
|
|
92
|
+
for (const [key, sessionId] of Object.entries(value.sessions)) {
|
|
93
|
+
const normalizedKey = nonEmptyString(key);
|
|
94
|
+
const normalizedSession = nonEmptyString(sessionId);
|
|
95
|
+
if (normalizedKey && normalizedSession) sessions[normalizedKey] = normalizedSession;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const pendingSenders = {};
|
|
100
|
+
const entries = Array.isArray(value.pendingSenders)
|
|
101
|
+
? value.pendingSenders.map((entry) => [entry?.requestId, entry])
|
|
102
|
+
: Object.entries(value.pendingSenders && typeof value.pendingSenders === 'object'
|
|
103
|
+
? value.pendingSenders
|
|
104
|
+
: {});
|
|
105
|
+
for (const [key, candidate] of entries) {
|
|
106
|
+
const pending = normalizePendingSender(candidate, key);
|
|
107
|
+
if (!pending) continue;
|
|
108
|
+
const duplicate = Object.values(pendingSenders).find((entry) => entry.staffId === pending.staffId);
|
|
109
|
+
if (!duplicate || duplicate.lastSeenAt < pending.lastSeenAt) {
|
|
110
|
+
if (duplicate) delete pendingSenders[duplicate.requestId];
|
|
111
|
+
pendingSenders[pending.requestId] = pending;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return {
|
|
116
|
+
version: 1,
|
|
117
|
+
sessions,
|
|
118
|
+
...(value.deferred ? { deferred: normalizeDeferredState(value.deferred) } : {}),
|
|
119
|
+
seenMessageIds: Array.isArray(value.seenMessageIds)
|
|
120
|
+
? [...new Set(value.seenMessageIds.map(nonEmptyString).filter(Boolean))].slice(-1_000)
|
|
121
|
+
: [],
|
|
122
|
+
pendingSenders,
|
|
123
|
+
recentOutboundMessages: recentOutboundMessages(value.recentOutboundMessages),
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export class DingtalkStateStore {
|
|
128
|
+
#path;
|
|
129
|
+
#state = structuredClone(EMPTY_STATE);
|
|
130
|
+
#writeQueue = Promise.resolve();
|
|
131
|
+
#deferred = deferredStateAccess(() => this.#state, () => this.#persist());
|
|
132
|
+
#idFactory;
|
|
133
|
+
#now;
|
|
134
|
+
|
|
135
|
+
constructor(path, { idFactory = randomUUID, now = () => new Date().toISOString() } = {}) {
|
|
136
|
+
if (!nonEmptyString(path)) throw new TypeError('state path is required');
|
|
137
|
+
if (typeof idFactory !== 'function' || typeof now !== 'function') {
|
|
138
|
+
throw new TypeError('idFactory and now must be functions');
|
|
139
|
+
}
|
|
140
|
+
this.#path = path;
|
|
141
|
+
this.#idFactory = idFactory;
|
|
142
|
+
this.#now = now;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
async load() {
|
|
146
|
+
try {
|
|
147
|
+
this.#state = normalizeState(JSON.parse(await readFile(this.#path, 'utf8')));
|
|
148
|
+
} catch (error) {
|
|
149
|
+
if (error?.code !== 'ENOENT') throw error;
|
|
150
|
+
this.#state = structuredClone(EMPTY_STATE);
|
|
151
|
+
await this.#persist();
|
|
152
|
+
}
|
|
153
|
+
return this;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
deferredEntries() { return this.#deferred.entries(); }
|
|
157
|
+
putDeferred(entry) { return this.#deferred.put(entry); }
|
|
158
|
+
patchDeferred(id, patch) { return this.#deferred.patch(id, patch); }
|
|
159
|
+
removeDeferred(id) { return this.#deferred.remove(id); }
|
|
160
|
+
|
|
161
|
+
sessionFor(key) {
|
|
162
|
+
return this.#state.sessions[key] ?? null;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
async setSession(key, sessionId) {
|
|
166
|
+
const normalizedKey = nonEmptyString(key);
|
|
167
|
+
const normalizedSession = nonEmptyString(sessionId);
|
|
168
|
+
if (!normalizedKey || !normalizedSession) throw new TypeError('key and sessionId are required');
|
|
169
|
+
this.#state.sessions[normalizedKey] = normalizedSession;
|
|
170
|
+
await this.#persist();
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
async clearSession(key) {
|
|
174
|
+
const normalizedKey = nonEmptyString(key);
|
|
175
|
+
if (!normalizedKey || !(normalizedKey in this.#state.sessions)) return;
|
|
176
|
+
delete this.#state.sessions[normalizedKey];
|
|
177
|
+
await this.#persist();
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
async clearSessions() {
|
|
181
|
+
this.#state.sessions = {};
|
|
182
|
+
await this.#persist();
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
hasSeen(messageId) {
|
|
186
|
+
const id = nonEmptyString(messageId);
|
|
187
|
+
return Boolean(id && this.#state.seenMessageIds.includes(id));
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
async markSeen(messageId) {
|
|
191
|
+
const id = nonEmptyString(messageId);
|
|
192
|
+
if (!id) throw new TypeError('messageId is required');
|
|
193
|
+
if (this.hasSeen(id)) return;
|
|
194
|
+
this.#state.seenMessageIds.push(id);
|
|
195
|
+
if (this.#state.seenMessageIds.length > 1_000) {
|
|
196
|
+
this.#state.seenMessageIds.splice(0, this.#state.seenMessageIds.length - 1_000);
|
|
197
|
+
}
|
|
198
|
+
await this.#persist();
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
async rememberOutboundMessage({
|
|
202
|
+
conversationKey,
|
|
203
|
+
text,
|
|
204
|
+
sentAt = Date.now(),
|
|
205
|
+
completedAt = Date.now(),
|
|
206
|
+
providerMessageIds: messageIds = [],
|
|
207
|
+
} = {}) {
|
|
208
|
+
const entry = normalizeRecentOutboundMessage({
|
|
209
|
+
conversationKey,
|
|
210
|
+
text,
|
|
211
|
+
sentAt,
|
|
212
|
+
completedAt,
|
|
213
|
+
providerMessageIds: messageIds,
|
|
214
|
+
});
|
|
215
|
+
if (!entry) throw new TypeError('Invalid DingTalk outbound message');
|
|
216
|
+
this.#state.recentOutboundMessages = recentOutboundMessages([
|
|
217
|
+
...this.#state.recentOutboundMessages,
|
|
218
|
+
entry,
|
|
219
|
+
]);
|
|
220
|
+
await this.#persist();
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
recentOutboundTextFor({
|
|
224
|
+
conversationKey,
|
|
225
|
+
processQueryKey,
|
|
226
|
+
messageId,
|
|
227
|
+
createdAt,
|
|
228
|
+
now = Date.now(),
|
|
229
|
+
} = {}) {
|
|
230
|
+
const key = nonEmptyString(conversationKey);
|
|
231
|
+
if (!key) return null;
|
|
232
|
+
const active = recentOutboundMessages(this.#state.recentOutboundMessages, now)
|
|
233
|
+
.filter((entry) => entry.conversationKey === key);
|
|
234
|
+
const quotedIds = providerMessageIds([processQueryKey, messageId]);
|
|
235
|
+
for (const quotedId of quotedIds) {
|
|
236
|
+
const exact = active.filter((entry) => entry.providerMessageIds.includes(quotedId));
|
|
237
|
+
if (exact.length === 1) return exact[0].text;
|
|
238
|
+
if (exact.length > 1) return null;
|
|
239
|
+
}
|
|
240
|
+
const quotedAt = timestampMs(createdAt);
|
|
241
|
+
if (quotedAt === null) return null;
|
|
242
|
+
const candidates = active.filter((entry) => (
|
|
243
|
+
quotedAt >= entry.sentAt - DINGTALK_RECENT_OUTBOUND_MATCH_TOLERANCE_MS
|
|
244
|
+
&& quotedAt <= entry.completedAt + DINGTALK_RECENT_OUTBOUND_MATCH_TOLERANCE_MS
|
|
245
|
+
));
|
|
246
|
+
return candidates.length === 1 ? candidates[0].text : null;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
pendingSenders() {
|
|
250
|
+
return Object.values(this.#state.pendingSenders)
|
|
251
|
+
.sort((left, right) => left.requestedAt.localeCompare(right.requestedAt))
|
|
252
|
+
.map((entry) => structuredClone(entry));
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
pendingSender(requestId) {
|
|
256
|
+
const id = nonEmptyString(requestId);
|
|
257
|
+
const entry = id ? this.#state.pendingSenders[id] : null;
|
|
258
|
+
return entry ? structuredClone(entry) : null;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
async recordPendingSender(staffIdOrEntry, name, seenAt) {
|
|
262
|
+
const input = staffIdOrEntry && typeof staffIdOrEntry === 'object'
|
|
263
|
+
? staffIdOrEntry
|
|
264
|
+
: { staffId: staffIdOrEntry, displayName: name, lastSeenAt: seenAt };
|
|
265
|
+
const staffId = nonEmptyString(input.staffId);
|
|
266
|
+
if (!staffId) throw new TypeError('staffId is required');
|
|
267
|
+
const timestamp = nonEmptyString(input.lastSeenAt) ?? nonEmptyString(input.requestedAt) ?? this.#now();
|
|
268
|
+
const existing = Object.values(this.#state.pendingSenders)
|
|
269
|
+
.find((entry) => entry.staffId === staffId);
|
|
270
|
+
const entry = {
|
|
271
|
+
requestId: existing?.requestId ?? `ding_sender_${this.#idFactory()}`,
|
|
272
|
+
staffId,
|
|
273
|
+
displayName: displayName(input.displayName ?? input.nick ?? name),
|
|
274
|
+
requestedAt: existing?.requestedAt ?? timestamp,
|
|
275
|
+
lastSeenAt: timestamp,
|
|
276
|
+
};
|
|
277
|
+
this.#state.pendingSenders[entry.requestId] = entry;
|
|
278
|
+
await this.#persist();
|
|
279
|
+
return structuredClone(entry);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
async removePendingSender(requestId) {
|
|
283
|
+
const id = nonEmptyString(requestId);
|
|
284
|
+
if (!id || !this.#state.pendingSenders[id]) return false;
|
|
285
|
+
delete this.#state.pendingSenders[id];
|
|
286
|
+
await this.#persist();
|
|
287
|
+
return true;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
async removePendingSenderByStaffId(staffId) {
|
|
291
|
+
const id = nonEmptyString(staffId);
|
|
292
|
+
const pending = id
|
|
293
|
+
? Object.values(this.#state.pendingSenders).find((entry) => entry.staffId === id)
|
|
294
|
+
: null;
|
|
295
|
+
return pending ? this.removePendingSender(pending.requestId) : false;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
snapshot() {
|
|
299
|
+
return structuredClone(this.#state);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
async remove() {
|
|
303
|
+
await this.#writeQueue;
|
|
304
|
+
try {
|
|
305
|
+
await unlink(this.#path);
|
|
306
|
+
} catch (error) {
|
|
307
|
+
if (error?.code !== 'ENOENT') throw error;
|
|
308
|
+
}
|
|
309
|
+
this.#state = structuredClone(EMPTY_STATE);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
async #persist() {
|
|
313
|
+
const snapshot = `${JSON.stringify(this.#state, null, 2)}\n`;
|
|
314
|
+
const operation = this.#writeQueue.then(async () => {
|
|
315
|
+
await mkdir(dirname(this.#path), { recursive: true, mode: 0o700 });
|
|
316
|
+
const temporary = `${this.#path}.tmp`;
|
|
317
|
+
await writeFile(temporary, snapshot, { encoding: 'utf8', mode: 0o600 });
|
|
318
|
+
await rename(temporary, this.#path);
|
|
319
|
+
});
|
|
320
|
+
this.#writeQueue = operation.then(() => undefined, () => undefined);
|
|
321
|
+
await operation;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
export const DingTalkStateStore = DingtalkStateStore;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import {
|
|
2
|
+
deriveTokenBotIdentity,
|
|
3
|
+
maskPlatformId,
|
|
4
|
+
TokenBotConfigStore,
|
|
5
|
+
} from '../shared/token-config-store.mjs';
|
|
6
|
+
import { t } from '../shared/i18n.mjs';
|
|
7
|
+
|
|
8
|
+
const IDENTITY_OPTIONS = Object.freeze({
|
|
9
|
+
botPrefix: 'discord',
|
|
10
|
+
tokenRefPrefix: 'DSH_DISCORD_BOT_TOKEN',
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
export function deriveDiscordBotIdentity(platformId) {
|
|
14
|
+
return deriveTokenBotIdentity(platformId, IDENTITY_OPTIONS);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function maskDiscordBotId(platformId) {
|
|
18
|
+
return maskPlatformId(platformId, t('Discord机器人'));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export class DiscordConfigStore extends TokenBotConfigStore {
|
|
22
|
+
constructor(path) {
|
|
23
|
+
super(path, { channel: 'Discord', ...IDENTITY_OPTIONS });
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { t } from '../shared/i18n.mjs';
|
|
3
|
+
|
|
4
|
+
const DEFAULT_BASE_URL = 'https://discord.com/api/v10/';
|
|
5
|
+
const DEFAULT_FILE_UPLOAD_TIMEOUT_MS = 120_000;
|
|
6
|
+
const DISCORD_PERMISSION_ERRORS = new Set([50001, 50013]);
|
|
7
|
+
const DISCORD_TOO_LARGE_ERRORS = new Set([40005]);
|
|
8
|
+
|
|
9
|
+
function cleanString(value) {
|
|
10
|
+
return typeof value === 'string' && value.trim() ? value.trim() : null;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function requestSignal(signal, timeoutMs) {
|
|
14
|
+
const timeout = AbortSignal.timeout(timeoutMs);
|
|
15
|
+
return signal ? AbortSignal.any([signal, timeout]) : timeout;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function abortReason(signal) {
|
|
19
|
+
return signal?.reason instanceof Error
|
|
20
|
+
? signal.reason
|
|
21
|
+
: new DOMException('The operation was aborted', 'AbortError');
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function positiveTimeout(value, name) {
|
|
25
|
+
if (!Number.isInteger(value) || value < 1) throw new TypeError(`${name} must be a positive integer`);
|
|
26
|
+
return value;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function preserveProviderMetadata(target, source) {
|
|
30
|
+
if (source?.providerCode !== undefined) target.providerCode = source.providerCode;
|
|
31
|
+
if (source?.retry_after !== undefined) {
|
|
32
|
+
target.retry_after = source.retry_after;
|
|
33
|
+
target.retryAfter = source.retry_after;
|
|
34
|
+
}
|
|
35
|
+
if (Number.isInteger(source?.status)) target.status = source.status;
|
|
36
|
+
return target;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function discordArtifactProviderError(cause) {
|
|
40
|
+
const providerCode = Number(cause?.providerCode);
|
|
41
|
+
const status = Number(cause?.status);
|
|
42
|
+
const message = cleanString(cause?.message) ?? '';
|
|
43
|
+
let code = 'artifact-provider-rejected';
|
|
44
|
+
let summary = 'Discord rejected the attachment.';
|
|
45
|
+
if (status === 401 || status === 403 || DISCORD_PERMISSION_ERRORS.has(providerCode)) {
|
|
46
|
+
code = 'artifact-permission-required';
|
|
47
|
+
summary = 'Discord denied permission to send the attachment.';
|
|
48
|
+
} else if (status === 413 || DISCORD_TOO_LARGE_ERRORS.has(providerCode)
|
|
49
|
+
|| /(?:request|attachment|file).{0,24}too large/i.test(message)) {
|
|
50
|
+
code = 'artifact-too-large';
|
|
51
|
+
summary = 'The attachment exceeds Discord\'s size limit.';
|
|
52
|
+
} else if (status === 429) {
|
|
53
|
+
code = 'artifact-rate-limited';
|
|
54
|
+
summary = 'Discord rate-limited attachment delivery.';
|
|
55
|
+
} else if (status >= 500) {
|
|
56
|
+
code = 'artifact-delivery-uncertain';
|
|
57
|
+
summary = 'Discord attachment delivery result is uncertain.';
|
|
58
|
+
}
|
|
59
|
+
const error = new Error(summary, { cause });
|
|
60
|
+
error.code = code;
|
|
61
|
+
return preserveProviderMetadata(error, cause);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function uncertainDiscordDelivery(cause) {
|
|
65
|
+
const error = new Error('Discord attachment delivery result is uncertain', { cause });
|
|
66
|
+
error.code = 'artifact-delivery-uncertain';
|
|
67
|
+
return preserveProviderMetadata(error, cause);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function delay(ms, signal) {
|
|
71
|
+
return new Promise((resolve, reject) => {
|
|
72
|
+
if (signal?.aborted) {
|
|
73
|
+
reject(signal.reason ?? new DOMException('Aborted', 'AbortError'));
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
const timer = setTimeout(resolve, ms);
|
|
77
|
+
timer?.unref?.();
|
|
78
|
+
signal?.addEventListener('abort', () => {
|
|
79
|
+
clearTimeout(timer);
|
|
80
|
+
reject(signal.reason ?? new DOMException('Aborted', 'AbortError'));
|
|
81
|
+
}, { once: true });
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function snowflake(value, name) {
|
|
86
|
+
const id = cleanString(value);
|
|
87
|
+
if (!id || !/^\d{5,30}$/.test(id)) throw new TypeError(`Invalid Discord ${name}`);
|
|
88
|
+
return id;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function reactionEmoji(value) {
|
|
92
|
+
const emoji = cleanString(value);
|
|
93
|
+
if (!emoji) throw new TypeError('A Discord reaction emoji is required');
|
|
94
|
+
return emoji;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function validDiscordToken(value) {
|
|
98
|
+
return typeof value === 'string'
|
|
99
|
+
&& /^[A-Za-z0-9_-]{20,}\.[A-Za-z0-9_-]{4,}\.[A-Za-z0-9_-]{20,}$/.test(value.trim());
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export class DiscordApi {
|
|
103
|
+
#token;
|
|
104
|
+
#fetch;
|
|
105
|
+
#baseUrl;
|
|
106
|
+
#fileUploadTimeoutMs;
|
|
107
|
+
|
|
108
|
+
constructor({
|
|
109
|
+
token,
|
|
110
|
+
fetchImpl = fetch,
|
|
111
|
+
baseUrl = DEFAULT_BASE_URL,
|
|
112
|
+
fileUploadTimeoutMs = DEFAULT_FILE_UPLOAD_TIMEOUT_MS,
|
|
113
|
+
}) {
|
|
114
|
+
if (!validDiscordToken(token)) throw new TypeError('Discord Bot Token is invalid');
|
|
115
|
+
if (typeof fetchImpl !== 'function') throw new TypeError('DiscordApi requires fetch');
|
|
116
|
+
this.#token = token.trim();
|
|
117
|
+
this.#fetch = fetchImpl;
|
|
118
|
+
this.#baseUrl = new URL(baseUrl);
|
|
119
|
+
this.#fileUploadTimeoutMs = positiveTimeout(fileUploadTimeoutMs, 'fileUploadTimeoutMs');
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
getCurrentUser(options = {}) {
|
|
123
|
+
return this.#request('users/@me', { ...options, method: 'GET' });
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
getGatewayBot(options = {}) {
|
|
127
|
+
return this.#request('gateway/bot', { ...options, method: 'GET' });
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
getChannel({ channelId, signal } = {}) {
|
|
131
|
+
return this.#request(`channels/${snowflake(channelId, 'channel id')}`, {
|
|
132
|
+
method: 'GET',
|
|
133
|
+
signal,
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
getMessage({ channelId, messageId, signal } = {}) {
|
|
138
|
+
return this.#request(
|
|
139
|
+
`channels/${snowflake(channelId, 'channel id')}/messages/${snowflake(messageId, 'message id')}`,
|
|
140
|
+
{ method: 'GET', signal },
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
startThreadFromMessage({ channelId, messageId, name, signal } = {}) {
|
|
145
|
+
const threadName = cleanString(name);
|
|
146
|
+
if (!threadName || [...threadName].length > 100) {
|
|
147
|
+
throw new TypeError('Discord thread name must contain 1-100 characters');
|
|
148
|
+
}
|
|
149
|
+
if (signal?.aborted) throw abortReason(signal);
|
|
150
|
+
return this.#request(
|
|
151
|
+
`channels/${snowflake(channelId, 'channel id')}/messages/${snowflake(messageId, 'message id')}/threads`,
|
|
152
|
+
{
|
|
153
|
+
method: 'POST',
|
|
154
|
+
signal,
|
|
155
|
+
body: { name: threadName },
|
|
156
|
+
},
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
createMessage({ channelId, content, replyToMessageId, signal }) {
|
|
161
|
+
return this.#request(`channels/${snowflake(channelId, 'channel id')}/messages`, {
|
|
162
|
+
method: 'POST',
|
|
163
|
+
signal,
|
|
164
|
+
body: {
|
|
165
|
+
content,
|
|
166
|
+
allowed_mentions: { parse: [], replied_user: false },
|
|
167
|
+
...(replyToMessageId ? {
|
|
168
|
+
message_reference: {
|
|
169
|
+
message_id: snowflake(replyToMessageId, 'message id'),
|
|
170
|
+
channel_id: snowflake(channelId, 'channel id'),
|
|
171
|
+
fail_if_not_exists: false,
|
|
172
|
+
},
|
|
173
|
+
} : {}),
|
|
174
|
+
},
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
async createFileMessage({ channelId, file, replyToMessageId, signal }) {
|
|
179
|
+
if (!file || typeof file !== 'object'
|
|
180
|
+
|| typeof file.fileName !== 'string' || !file.fileName
|
|
181
|
+
|| !Buffer.isBuffer(file.bytes)) {
|
|
182
|
+
throw new TypeError('A Discord attachment is required');
|
|
183
|
+
}
|
|
184
|
+
const deliverySeed = cleanString(file.deliveryKey) ?? cleanString(file.artifactId);
|
|
185
|
+
const nonce = deliverySeed
|
|
186
|
+
? createHash('sha256').update(deliverySeed).digest('hex').slice(0, 25)
|
|
187
|
+
: undefined;
|
|
188
|
+
const payload = new FormData();
|
|
189
|
+
payload.append('payload_json', JSON.stringify({
|
|
190
|
+
allowed_mentions: { parse: [], replied_user: false },
|
|
191
|
+
attachments: [{ id: 0, filename: file.fileName }],
|
|
192
|
+
...(nonce ? { nonce, enforce_nonce: true } : {}),
|
|
193
|
+
...(replyToMessageId ? {
|
|
194
|
+
message_reference: {
|
|
195
|
+
message_id: snowflake(replyToMessageId, 'message id'),
|
|
196
|
+
channel_id: snowflake(channelId, 'channel id'),
|
|
197
|
+
fail_if_not_exists: false,
|
|
198
|
+
},
|
|
199
|
+
} : {}),
|
|
200
|
+
}));
|
|
201
|
+
payload.append(
|
|
202
|
+
'files[0]',
|
|
203
|
+
new Blob([file.bytes], { type: file.mediaType ?? 'application/octet-stream' }),
|
|
204
|
+
file.fileName,
|
|
205
|
+
);
|
|
206
|
+
const targetChannelId = snowflake(channelId, 'channel id');
|
|
207
|
+
if (signal?.aborted) throw abortReason(signal);
|
|
208
|
+
const uploadSignal = requestSignal(signal, this.#fileUploadTimeoutMs);
|
|
209
|
+
try {
|
|
210
|
+
return await this.#request(`channels/${targetChannelId}/messages`, {
|
|
211
|
+
method: 'POST',
|
|
212
|
+
signal: uploadSignal,
|
|
213
|
+
timeoutMs: this.#fileUploadTimeoutMs,
|
|
214
|
+
body: payload,
|
|
215
|
+
multipart: true,
|
|
216
|
+
});
|
|
217
|
+
} catch (error) {
|
|
218
|
+
if (signal?.aborted) throw abortReason(signal);
|
|
219
|
+
if (error?.code?.startsWith?.('discord-')) {
|
|
220
|
+
throw discordArtifactProviderError(error);
|
|
221
|
+
}
|
|
222
|
+
throw uncertainDiscordDelivery(error);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
editMessage({ channelId, messageId, content, signal }) {
|
|
227
|
+
return this.#request(
|
|
228
|
+
`channels/${snowflake(channelId, 'channel id')}/messages/${snowflake(messageId, 'message id')}`,
|
|
229
|
+
{
|
|
230
|
+
method: 'PATCH',
|
|
231
|
+
signal,
|
|
232
|
+
body: { content, allowed_mentions: { parse: [], replied_user: false } },
|
|
233
|
+
},
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
sendTyping({ channelId, signal }) {
|
|
238
|
+
return this.#request(`channels/${snowflake(channelId, 'channel id')}/typing`, {
|
|
239
|
+
method: 'POST',
|
|
240
|
+
signal,
|
|
241
|
+
expectBody: false,
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
async addOwnReaction({ channelId, messageId, emoji, signal } = {}) {
|
|
246
|
+
const normalizedEmoji = reactionEmoji(emoji);
|
|
247
|
+
await this.#request(
|
|
248
|
+
`channels/${snowflake(channelId, 'channel id')}/messages/${snowflake(messageId, 'message id')}`
|
|
249
|
+
+ `/reactions/${encodeURIComponent(normalizedEmoji)}/@me`,
|
|
250
|
+
{
|
|
251
|
+
method: 'PUT',
|
|
252
|
+
signal,
|
|
253
|
+
expectBody: false,
|
|
254
|
+
retry: false,
|
|
255
|
+
},
|
|
256
|
+
);
|
|
257
|
+
return normalizedEmoji;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
removeOwnReaction({ channelId, messageId, emoji, signal } = {}) {
|
|
261
|
+
const normalizedEmoji = reactionEmoji(emoji);
|
|
262
|
+
return this.#request(
|
|
263
|
+
`channels/${snowflake(channelId, 'channel id')}/messages/${snowflake(messageId, 'message id')}`
|
|
264
|
+
+ `/reactions/${encodeURIComponent(normalizedEmoji)}/@me`,
|
|
265
|
+
{
|
|
266
|
+
method: 'DELETE',
|
|
267
|
+
signal,
|
|
268
|
+
expectBody: false,
|
|
269
|
+
retry: false,
|
|
270
|
+
},
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
async #request(path, {
|
|
275
|
+
method,
|
|
276
|
+
body,
|
|
277
|
+
signal,
|
|
278
|
+
timeoutMs = 15_000,
|
|
279
|
+
expectBody = true,
|
|
280
|
+
retry = true,
|
|
281
|
+
multipart = false,
|
|
282
|
+
}) {
|
|
283
|
+
let response;
|
|
284
|
+
try {
|
|
285
|
+
response = await this.#fetch(new URL(path, this.#baseUrl), {
|
|
286
|
+
method,
|
|
287
|
+
headers: {
|
|
288
|
+
authorization: `Bot ${this.#token}`,
|
|
289
|
+
...(multipart ? {} : { 'content-type': 'application/json' }),
|
|
290
|
+
'user-agent': 'DeepSeek-Harness-dsh-im (https://github.com/xmanrui/dsh-im, 1.1.0)',
|
|
291
|
+
},
|
|
292
|
+
...(body === undefined ? {} : { body: multipart ? body : JSON.stringify(body) }),
|
|
293
|
+
signal: requestSignal(signal, timeoutMs),
|
|
294
|
+
redirect: 'error',
|
|
295
|
+
});
|
|
296
|
+
} catch (error) {
|
|
297
|
+
if (error?.name === 'AbortError' || error?.name === 'TimeoutError') throw error;
|
|
298
|
+
throw new Error(`Discord ${method} transport failed`, { cause: error });
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
let parsed = null;
|
|
302
|
+
if (expectBody || response.status === 429 || !response.ok) {
|
|
303
|
+
try {
|
|
304
|
+
parsed = await response.json();
|
|
305
|
+
} catch (cause) {
|
|
306
|
+
if (expectBody) {
|
|
307
|
+
const error = new Error(`Discord ${method} returned invalid JSON`, { cause });
|
|
308
|
+
error.status = response?.status;
|
|
309
|
+
throw error;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
if (response.status === 429 && retry) {
|
|
314
|
+
const retryAfterMs = Math.min(10_000, Math.max(50, Number(parsed?.retry_after) * 1_000 || 1_000));
|
|
315
|
+
await delay(retryAfterMs, signal);
|
|
316
|
+
return this.#request(path, {
|
|
317
|
+
method, body, signal, timeoutMs, expectBody, retry: false, multipart,
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
if (!response.ok) {
|
|
321
|
+
const error = new Error(cleanString(parsed?.message) ?? `Discord API failed with HTTP ${response.status}`);
|
|
322
|
+
error.code = `discord-${response.status}`;
|
|
323
|
+
error.status = response.status;
|
|
324
|
+
if (Number.isInteger(parsed?.code) || typeof parsed?.code === 'string') {
|
|
325
|
+
error.providerCode = parsed.code;
|
|
326
|
+
}
|
|
327
|
+
const retryAfter = Number(parsed?.retry_after);
|
|
328
|
+
if (Number.isFinite(retryAfter) && retryAfter >= 0) {
|
|
329
|
+
error.retry_after = retryAfter;
|
|
330
|
+
error.retryAfter = retryAfter;
|
|
331
|
+
}
|
|
332
|
+
throw error;
|
|
333
|
+
}
|
|
334
|
+
return expectBody ? parsed : null;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
export async function inspectDiscordToken(token, options = {}) {
|
|
339
|
+
const api = new DiscordApi({ token, ...options });
|
|
340
|
+
const bot = await api.getCurrentUser();
|
|
341
|
+
if (!bot?.id || bot?.bot !== true) throw new Error('Discord token does not belong to a bot');
|
|
342
|
+
return {
|
|
343
|
+
platformId: String(bot.id),
|
|
344
|
+
name: cleanString(bot.global_name) ?? cleanString(bot.username) ?? t('Discord机器人'),
|
|
345
|
+
username: cleanString(bot.username),
|
|
346
|
+
};
|
|
347
|
+
}
|