@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,71 @@
|
|
|
1
|
+
function refusal(code) {
|
|
2
|
+
return Object.assign(new Error(code), { code });
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export class ExclusiveInboundConsumers {
|
|
6
|
+
#entries = new Map();
|
|
7
|
+
|
|
8
|
+
acceptsFiles(botId) { return this.#entries.get(botId)?.sourceFiles === true; }
|
|
9
|
+
acceptsOrdinary(botId) { return this.#entries.get(botId)?.ordinaryText === true; }
|
|
10
|
+
|
|
11
|
+
register(botId, { fingerprint, onEvent, signal, sourceFiles = false, ordinaryText = false, onEcho }) {
|
|
12
|
+
if (this.#entries.has(botId)) throw refusal('consumer-conflict');
|
|
13
|
+
if (!/^[a-f0-9]{64}$/.test(fingerprint ?? '') || typeof onEvent !== 'function' || typeof sourceFiles !== 'boolean' || typeof ordinaryText !== 'boolean' || (onEcho !== undefined && typeof onEcho !== 'function'))
|
|
14
|
+
throw refusal('bad-request');
|
|
15
|
+
signal?.throwIfAborted();
|
|
16
|
+
const controller = new AbortController();
|
|
17
|
+
const entry = { fingerprint, onEvent, onEcho, sourceFiles, ordinaryText, controller, dispose: undefined };
|
|
18
|
+
const dispose = () => {
|
|
19
|
+
if (this.#entries.get(botId) === entry) this.#entries.delete(botId);
|
|
20
|
+
controller.abort(refusal('consumer-unavailable'));
|
|
21
|
+
signal?.removeEventListener('abort', dispose);
|
|
22
|
+
};
|
|
23
|
+
entry.dispose = dispose;
|
|
24
|
+
this.#entries.set(botId, entry);
|
|
25
|
+
signal?.addEventListener('abort', dispose, { once: true });
|
|
26
|
+
return dispose;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async accept(botId, evidence, signal, echo = false) {
|
|
30
|
+
const entry = this.#entries.get(botId);
|
|
31
|
+
if (!entry) throw refusal('consumer-unavailable');
|
|
32
|
+
if (entry.fingerprint !== evidence.fingerprint) throw refusal('account-changed');
|
|
33
|
+
const deliverySignal = signal
|
|
34
|
+
? AbortSignal.any([signal, entry.controller.signal]) : entry.controller.signal;
|
|
35
|
+
deliverySignal.throwIfAborted();
|
|
36
|
+
const callback = echo ? entry.onEcho : entry.onEvent;
|
|
37
|
+
if (callback === undefined) return { accepted: true, ignored: true };
|
|
38
|
+
let abort;
|
|
39
|
+
const interrupted = new Promise((_, reject) => {
|
|
40
|
+
abort = () => reject(deliverySignal.reason);
|
|
41
|
+
deliverySignal.addEventListener('abort', abort, { once: true });
|
|
42
|
+
if (deliverySignal.aborted) abort();
|
|
43
|
+
});
|
|
44
|
+
let result;
|
|
45
|
+
try {
|
|
46
|
+
result = await Promise.race([callback(evidence, { signal: deliverySignal }), interrupted]);
|
|
47
|
+
} finally {
|
|
48
|
+
deliverySignal.removeEventListener('abort', abort);
|
|
49
|
+
}
|
|
50
|
+
deliverySignal.throwIfAborted();
|
|
51
|
+
if (this.#entries.get(botId) !== entry) throw refusal('consumer-unavailable');
|
|
52
|
+
if (result?.accepted !== true) throw refusal('ingress-not-accepted');
|
|
53
|
+
return { accepted: true };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
signalFor(botId, fingerprint) {
|
|
57
|
+
const entry = this.#entries.get(botId);
|
|
58
|
+
if (!entry) throw refusal('consumer-unavailable');
|
|
59
|
+
if (entry.fingerprint !== fingerprint) throw refusal('account-changed');
|
|
60
|
+
return entry.controller.signal;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
remove(botId) {
|
|
64
|
+
const entry = this.#entries.get(botId);
|
|
65
|
+
entry?.dispose();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
close() {
|
|
69
|
+
for (const botId of this.#entries.keys()) this.remove(botId);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
async function cancelResponseBody(response) {
|
|
2
|
+
try {
|
|
3
|
+
await response?.body?.cancel?.();
|
|
4
|
+
} catch {
|
|
5
|
+
// Preserve the original download failure.
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function hostedByMessagingPlatform(target, allowedHosts) {
|
|
10
|
+
return !Array.isArray(allowedHosts) || allowedHosts.some((rule) => (
|
|
11
|
+
typeof rule === 'string'
|
|
12
|
+
&& (target.hostname === rule
|
|
13
|
+
|| (rule.startsWith('.')
|
|
14
|
+
&& (target.hostname === rule.slice(1) || target.hostname.endsWith(rule))))
|
|
15
|
+
));
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Open a channel-hosted ordinary file as a stream.
|
|
20
|
+
*
|
|
21
|
+
* This deliberately has no plugin-defined size, type, count, or download-time
|
|
22
|
+
* limit. The caller owns cancellation through its AbortSignal and the channel
|
|
23
|
+
* remains the authority for its own file limits.
|
|
24
|
+
*/
|
|
25
|
+
export async function fetchFileStream(url, {
|
|
26
|
+
fetchImpl = fetch,
|
|
27
|
+
headers,
|
|
28
|
+
signal,
|
|
29
|
+
allowedHosts,
|
|
30
|
+
} = {}) {
|
|
31
|
+
const target = new URL(url);
|
|
32
|
+
if (target.protocol !== 'https:') throw new Error('File download URL must use HTTPS');
|
|
33
|
+
if (!hostedByMessagingPlatform(target, allowedHosts)) {
|
|
34
|
+
throw new Error('File download URL is not hosted by the messaging platform');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const response = await fetchImpl(target, {
|
|
38
|
+
method: 'GET',
|
|
39
|
+
headers,
|
|
40
|
+
signal,
|
|
41
|
+
redirect: 'manual',
|
|
42
|
+
});
|
|
43
|
+
if (Number.isInteger(response?.status) && response.status >= 300 && response.status < 400) {
|
|
44
|
+
await cancelResponseBody(response);
|
|
45
|
+
const error = new Error(`File download redirect was blocked (HTTP ${response.status})`);
|
|
46
|
+
error.code = 'file-redirect-blocked';
|
|
47
|
+
throw error;
|
|
48
|
+
}
|
|
49
|
+
if (!response?.ok) {
|
|
50
|
+
await cancelResponseBody(response);
|
|
51
|
+
const error = new Error(`File download failed with HTTP ${response?.status ?? 'unknown'}`);
|
|
52
|
+
error.code = 'file-http-error';
|
|
53
|
+
error.status = response?.status;
|
|
54
|
+
throw error;
|
|
55
|
+
}
|
|
56
|
+
if (response.body?.[Symbol.asyncIterator]) return { stream: response.body };
|
|
57
|
+
if (typeof response.arrayBuffer === 'function') {
|
|
58
|
+
const data = Buffer.from(await response.arrayBuffer());
|
|
59
|
+
return {
|
|
60
|
+
stream: (async function* fileBody() { yield data; }()),
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
throw new Error('File download returned no readable body');
|
|
64
|
+
}
|
|
@@ -0,0 +1,508 @@
|
|
|
1
|
+
import { t } from './i18n.mjs';
|
|
2
|
+
|
|
3
|
+
const APPROVAL_REPLIES = new Map([
|
|
4
|
+
['批准', 'allowed-once'],
|
|
5
|
+
['同意', 'allowed-once'],
|
|
6
|
+
['yes', 'allowed-once'],
|
|
7
|
+
['拒绝', 'rejected'],
|
|
8
|
+
['不同意', 'rejected'],
|
|
9
|
+
['no', 'rejected'],
|
|
10
|
+
]);
|
|
11
|
+
|
|
12
|
+
const APPROVAL_PROMPT = '请精准回复「批准」或「拒绝」(也支持:同意 / 不同意 / yes / no)。';
|
|
13
|
+
const APPROVAL_AFTER_QUESTION_PROMPT = '请先完成当前问题,再精准回复「批准」或「拒绝」。';
|
|
14
|
+
const APPROVAL_RESOLVED_TEXT = '该审批已处理,无需再次回复。';
|
|
15
|
+
const RESOLVED_ROUTE_TTL_MS = 5 * 60_000;
|
|
16
|
+
const MAX_RESOLVED_ROUTES = 2_048;
|
|
17
|
+
|
|
18
|
+
function cleanText(value) {
|
|
19
|
+
return typeof value === 'string' ? value.trim() : '';
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function printableText(value) {
|
|
23
|
+
return cleanText(value).replace(/[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f]/g, '');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function harnessApprovalDecision(text) {
|
|
27
|
+
return APPROVAL_REPLIES.get(cleanText(text).toLowerCase()) ?? null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function validHarnessApproval(payload) {
|
|
31
|
+
return payload?.type === 'approval/requested'
|
|
32
|
+
&& Boolean(cleanText(payload.sessionId))
|
|
33
|
+
&& Boolean(cleanText(payload.approvalId))
|
|
34
|
+
&& Boolean(cleanText(payload.toolName))
|
|
35
|
+
&& (payload.callId === undefined || Boolean(cleanText(payload.callId)))
|
|
36
|
+
&& (payload.reason === undefined || typeof payload.reason === 'string');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function toolArguments(toolCall) {
|
|
40
|
+
const source = toolCall?.arguments;
|
|
41
|
+
if (source !== null && typeof source === 'object') {
|
|
42
|
+
try {
|
|
43
|
+
return JSON.stringify(source, null, 2);
|
|
44
|
+
} catch {
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
if (typeof source !== 'string') return null;
|
|
49
|
+
const raw = printableText(source);
|
|
50
|
+
// Harness treats an empty tool argument string as an empty object.
|
|
51
|
+
if (!raw) return source === '' ? '{}' : null;
|
|
52
|
+
try {
|
|
53
|
+
return JSON.stringify(JSON.parse(raw), null, 2);
|
|
54
|
+
} catch {
|
|
55
|
+
return raw;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function harnessApprovalText(payload, {
|
|
60
|
+
toolCall,
|
|
61
|
+
requiresMention = false,
|
|
62
|
+
maxArgumentsLength = 6_000,
|
|
63
|
+
} = {}) {
|
|
64
|
+
if (!validHarnessApproval(payload)) return null;
|
|
65
|
+
const callId = cleanText(payload.callId);
|
|
66
|
+
if (!callId
|
|
67
|
+
|| cleanText(toolCall?.callId) !== callId
|
|
68
|
+
|| cleanText(toolCall?.name) !== cleanText(payload.toolName)) return null;
|
|
69
|
+
const operation = toolArguments(toolCall);
|
|
70
|
+
if (!operation || operation.length > maxArgumentsLength) return null;
|
|
71
|
+
|
|
72
|
+
const lines = [
|
|
73
|
+
t('DeepSeek Harness 需要你的审批:'),
|
|
74
|
+
'',
|
|
75
|
+
t('工具:{tool}', { tool: printableText(payload.toolName) }),
|
|
76
|
+
t('操作参数:'),
|
|
77
|
+
operation,
|
|
78
|
+
];
|
|
79
|
+
const reason = printableText(payload.reason);
|
|
80
|
+
if (reason) lines.push(t('原因:{reason}', { reason }));
|
|
81
|
+
lines.push('', t(APPROVAL_PROMPT));
|
|
82
|
+
if (requiresMention) lines.push('', t('群聊中请 @机器人 后发送审批决定。'));
|
|
83
|
+
return lines.join('\n');
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function approvalResult(pending, outcome) {
|
|
87
|
+
return {
|
|
88
|
+
ok: true,
|
|
89
|
+
value: {
|
|
90
|
+
sessionId: pending.sessionId,
|
|
91
|
+
approvalId: pending.approvalId,
|
|
92
|
+
outcome,
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function approvalOutcomeText(outcome) {
|
|
98
|
+
if (outcome === 'allowed-once') return t('已批准,仅对本次操作有效。');
|
|
99
|
+
if (outcome === 'rejected') return t('已拒绝此次操作。');
|
|
100
|
+
return t(APPROVAL_RESOLVED_TEXT);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export class HarnessApprovalQueue {
|
|
104
|
+
#label;
|
|
105
|
+
#logger;
|
|
106
|
+
#byId = new Map();
|
|
107
|
+
#routes = new Map();
|
|
108
|
+
#resolvedRoutes = new Map();
|
|
109
|
+
|
|
110
|
+
constructor({ label = 'IM', logger = console } = {}) {
|
|
111
|
+
this.#label = label;
|
|
112
|
+
this.#logger = logger;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
hasPending(key) {
|
|
116
|
+
return this.#routes.get(key)?.items.some((pending) => !pending.inactive) === true;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
claimReply({
|
|
120
|
+
key,
|
|
121
|
+
actor,
|
|
122
|
+
text,
|
|
123
|
+
addressed = true,
|
|
124
|
+
hasPendingQuestion = false,
|
|
125
|
+
questionCompletion,
|
|
126
|
+
isQuestionPending,
|
|
127
|
+
send,
|
|
128
|
+
}) {
|
|
129
|
+
const route = this.#routes.get(key);
|
|
130
|
+
const pending = route?.items[0];
|
|
131
|
+
const decision = harnessApprovalDecision(text);
|
|
132
|
+
const notice = (value, resolved = false) => ({
|
|
133
|
+
...(resolved ? { resolved: true } : {}),
|
|
134
|
+
process: async (before) => {
|
|
135
|
+
if (typeof before === 'function' && await before() === false) return;
|
|
136
|
+
await send(value);
|
|
137
|
+
},
|
|
138
|
+
});
|
|
139
|
+
// Match Harness' own interaction precedence: a live ask_user_question
|
|
140
|
+
// outranks sibling approvals. Otherwise a question answer such as "yes"
|
|
141
|
+
// could accidentally authorize a tool call.
|
|
142
|
+
const deferredByQuestion = hasPendingQuestion
|
|
143
|
+
&& pending
|
|
144
|
+
&& questionCompletion
|
|
145
|
+
&& typeof questionCompletion.then === 'function';
|
|
146
|
+
if (hasPendingQuestion && !deferredByQuestion) return null;
|
|
147
|
+
if (!pending || pending.inactive) {
|
|
148
|
+
const resolvedUntil = this.#resolvedRoutes.get(key) ?? 0;
|
|
149
|
+
if (resolvedUntil <= Date.now()) {
|
|
150
|
+
this.#resolvedRoutes.delete(key);
|
|
151
|
+
return null;
|
|
152
|
+
}
|
|
153
|
+
if (!decision) return null;
|
|
154
|
+
return notice(t(APPROVAL_RESOLVED_TEXT), true);
|
|
155
|
+
}
|
|
156
|
+
if (pending.actor !== actor || (pending.requiresMention && addressed !== true)) {
|
|
157
|
+
if (!decision) return null;
|
|
158
|
+
return notice(t('只有发起当前任务的用户可以处理这条审批。'));
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return {
|
|
162
|
+
process: async (before) => {
|
|
163
|
+
const presentedWhenClaimed = pending.presented;
|
|
164
|
+
const previous = pending.replyTail ?? Promise.resolve();
|
|
165
|
+
const task = previous
|
|
166
|
+
.catch(() => undefined)
|
|
167
|
+
.then(async () => {
|
|
168
|
+
if (typeof before === 'function' && await before() === false) return;
|
|
169
|
+
if (deferredByQuestion) {
|
|
170
|
+
await questionCompletion.catch(() => undefined);
|
|
171
|
+
if (pending.inactive || pending.resolving) return;
|
|
172
|
+
if (typeof isQuestionPending === 'function' && isQuestionPending()) {
|
|
173
|
+
await send(t(APPROVAL_AFTER_QUESTION_PROMPT));
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
await pending.activationTask?.catch(() => undefined);
|
|
178
|
+
await pending.presentationTask?.catch(() => undefined);
|
|
179
|
+
if (pending.inactive || pending.resolving) {
|
|
180
|
+
await send(t(APPROVAL_RESOLVED_TEXT));
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
pending.send = send;
|
|
184
|
+
// Never turn a decision sent before the operation was visibly
|
|
185
|
+
// presented into an approval. This also covers a failed presentation
|
|
186
|
+
// and the small FIFO promotion window before the next item is shown.
|
|
187
|
+
if (!presentedWhenClaimed || !pending.presented) {
|
|
188
|
+
if (!pending.presented) await this.#present(pending);
|
|
189
|
+
if (pending.inactive || pending.resolving) return;
|
|
190
|
+
await send(t(APPROVAL_PROMPT));
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
if (pending.submitting) {
|
|
194
|
+
await send(t('审批决定正在提交,请稍候。'));
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
if (!decision) {
|
|
198
|
+
await send(t(APPROVAL_PROMPT));
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
await this.#submit(pending, decision);
|
|
202
|
+
});
|
|
203
|
+
pending.replyTail = task;
|
|
204
|
+
try {
|
|
205
|
+
await task;
|
|
206
|
+
} finally {
|
|
207
|
+
if (pending.replyTail === task) pending.replyTail = null;
|
|
208
|
+
}
|
|
209
|
+
},
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
async handleRequested(interaction, context) {
|
|
214
|
+
if (interaction?.kind !== 'approval') return false;
|
|
215
|
+
const payload = interaction.payload;
|
|
216
|
+
const approvalId = cleanText(payload?.approvalId);
|
|
217
|
+
if (!cleanText(interaction.rpcId)
|
|
218
|
+
|| !cleanText(interaction.sessionId)
|
|
219
|
+
|| !approvalId
|
|
220
|
+
|| !validHarnessApproval(payload)
|
|
221
|
+
|| payload.sessionId !== interaction.sessionId
|
|
222
|
+
|| typeof interaction.respond !== 'function') {
|
|
223
|
+
this.#logger.warn?.(`[dsh-im:${this.#label}] ignored an invalid Harness approval`);
|
|
224
|
+
return true;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
if (interaction.recovered === true) {
|
|
228
|
+
await this.#rejectInteraction(interaction, payload);
|
|
229
|
+
return true;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const existing = this.#byId.get(approvalId);
|
|
233
|
+
if (existing) {
|
|
234
|
+
existing.interaction = interaction;
|
|
235
|
+
existing.toolCall = interaction.toolCall;
|
|
236
|
+
if (!existing.presented) await this.#present(existing);
|
|
237
|
+
return true;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const send = context?.send;
|
|
241
|
+
const key = cleanText(context?.key);
|
|
242
|
+
const actor = cleanText(context?.actor);
|
|
243
|
+
if (!key || !actor || typeof send !== 'function') {
|
|
244
|
+
this.#logger.warn?.(`[dsh-im:${this.#label}] ignored an approval without a reply route`);
|
|
245
|
+
await this.#rejectInteraction(interaction, payload);
|
|
246
|
+
return true;
|
|
247
|
+
}
|
|
248
|
+
// Optional channel-provided renderer. When present, the approval is shown
|
|
249
|
+
// as an interactive card (e.g. Feishu approve/reject buttons) instead of
|
|
250
|
+
// plain text. Channels that don't provide one keep the text-reply path.
|
|
251
|
+
const render = typeof context?.render === 'function' ? context.render : null;
|
|
252
|
+
|
|
253
|
+
const text = harnessApprovalText(payload, {
|
|
254
|
+
toolCall: interaction.toolCall,
|
|
255
|
+
requiresMention: context.requiresMention === true,
|
|
256
|
+
});
|
|
257
|
+
if (!text) {
|
|
258
|
+
const rejected = await this.#rejectInteraction(interaction, payload);
|
|
259
|
+
await send(rejected
|
|
260
|
+
? t('无法完整展示这次操作,已安全拒绝此次审批。')
|
|
261
|
+
: t(APPROVAL_RESOLVED_TEXT));
|
|
262
|
+
return true;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
const pending = {
|
|
266
|
+
approvalId,
|
|
267
|
+
sessionId: interaction.sessionId,
|
|
268
|
+
interaction,
|
|
269
|
+
toolCall: interaction.toolCall,
|
|
270
|
+
key,
|
|
271
|
+
actor,
|
|
272
|
+
requiresMention: context.requiresMention === true,
|
|
273
|
+
send,
|
|
274
|
+
render,
|
|
275
|
+
onResolved: typeof context.onResolved === 'function' ? context.onResolved : null,
|
|
276
|
+
text,
|
|
277
|
+
presented: false,
|
|
278
|
+
presentationTask: null,
|
|
279
|
+
deliveryCompleted: false,
|
|
280
|
+
replyTail: null,
|
|
281
|
+
submitting: false,
|
|
282
|
+
inactive: false,
|
|
283
|
+
resolving: false,
|
|
284
|
+
closedOutcome: null,
|
|
285
|
+
resolutionNotified: false,
|
|
286
|
+
activationTask: null,
|
|
287
|
+
};
|
|
288
|
+
this.#byId.set(approvalId, pending);
|
|
289
|
+
const route = this.#routes.get(key) ?? { items: [] };
|
|
290
|
+
route.items.push(pending);
|
|
291
|
+
this.#routes.set(key, route);
|
|
292
|
+
if (route.items[0] === pending) await this.#present(pending);
|
|
293
|
+
return true;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Submit an approval decision by id, as triggered by a channel card button
|
|
298
|
+
* (e.g. Feishu approve/reject). Returns false when no matching pending
|
|
299
|
+
* approval is found. Callers may pass the acting user to enforce that only
|
|
300
|
+
* the originating actor can decide.
|
|
301
|
+
*/
|
|
302
|
+
async submitByApprovalId(approvalId, outcome, { actor } = {}) {
|
|
303
|
+
const pending = this.#byId.get(cleanText(approvalId));
|
|
304
|
+
if (!pending || pending.inactive || pending.resolving || pending.submitting) return false;
|
|
305
|
+
if (actor !== undefined && pending.actor !== actor) return false;
|
|
306
|
+
await this.#submit(pending, outcome);
|
|
307
|
+
return true;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
async handleResolved(resolution) {
|
|
311
|
+
if (resolution?.kind !== 'approval') return false;
|
|
312
|
+
const pending = this.#byId.get(cleanText(resolution.interactionId));
|
|
313
|
+
if (!pending) return true;
|
|
314
|
+
// A queued item may already be the next route head while the previous
|
|
315
|
+
// item's confirmation is still in flight. Preserve that route barrier so
|
|
316
|
+
// resolving this item cannot expose a later approval out of order.
|
|
317
|
+
pending.resolving = true;
|
|
318
|
+
if (pending.activationTask) {
|
|
319
|
+
await pending.activationTask.catch(() => undefined);
|
|
320
|
+
}
|
|
321
|
+
if (pending.inactive || this.#byId.get(pending.approvalId) !== pending) return true;
|
|
322
|
+
const presentationTask = pending.presentationTask;
|
|
323
|
+
const shouldNotify = pending.presented || presentationTask;
|
|
324
|
+
const send = pending.send;
|
|
325
|
+
const next = this.#remove(pending);
|
|
326
|
+
await this.#transition(next, async () => {
|
|
327
|
+
let delivered = pending.presented;
|
|
328
|
+
if (presentationTask) {
|
|
329
|
+
delivered = await presentationTask.then(() => true, () => false);
|
|
330
|
+
}
|
|
331
|
+
if (shouldNotify && delivered) {
|
|
332
|
+
await this.#notifyResolved(pending, resolution.outcome, send);
|
|
333
|
+
}
|
|
334
|
+
});
|
|
335
|
+
return true;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
async closeRoute(key) {
|
|
339
|
+
const route = this.#routes.get(key);
|
|
340
|
+
if (!route) return;
|
|
341
|
+
const pendingItems = [...route.items];
|
|
342
|
+
for (const pending of pendingItems) this.#remove(pending);
|
|
343
|
+
await Promise.all(pendingItems.map(async (pending) => {
|
|
344
|
+
try {
|
|
345
|
+
await pending.interaction.respond(
|
|
346
|
+
approvalResult(pending, 'rejected'),
|
|
347
|
+
{ signal: AbortSignal.timeout(5_000) },
|
|
348
|
+
);
|
|
349
|
+
pending.closedOutcome = 'rejected';
|
|
350
|
+
if (pending.presented || pending.deliveryCompleted) {
|
|
351
|
+
await this.#notifyResolved(pending, 'rejected');
|
|
352
|
+
}
|
|
353
|
+
} catch (error) {
|
|
354
|
+
if (error?.code === 'interaction-not-pending') {
|
|
355
|
+
pending.closedOutcome = 'resolved';
|
|
356
|
+
if (pending.presented || pending.deliveryCompleted) {
|
|
357
|
+
await this.#notifyResolved(pending, 'resolved');
|
|
358
|
+
}
|
|
359
|
+
} else {
|
|
360
|
+
this.#logger.warn?.(`[dsh-im:${this.#label}] failed to reject a closing approval:`, error);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
}));
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
async #present(pending) {
|
|
367
|
+
if (this.#routes.get(pending.key)?.items[0] !== pending
|
|
368
|
+
|| pending.inactive || pending.resolving || pending.presented) return;
|
|
369
|
+
await pending.activationTask?.catch(() => undefined);
|
|
370
|
+
if (this.#routes.get(pending.key)?.items[0] !== pending
|
|
371
|
+
|| pending.inactive || pending.resolving || pending.presented) return;
|
|
372
|
+
if (pending.presentationTask) return pending.presentationTask;
|
|
373
|
+
// A channel-provided renderer shows the approval as an interactive card
|
|
374
|
+
// (e.g. approve/reject buttons); otherwise fall back to plain text.
|
|
375
|
+
const task = pending.render
|
|
376
|
+
? Promise.resolve().then(() => pending.render(pending, pending.send))
|
|
377
|
+
: Promise.resolve().then(() => pending.send(pending.text));
|
|
378
|
+
pending.presentationTask = task;
|
|
379
|
+
try {
|
|
380
|
+
await task;
|
|
381
|
+
pending.deliveryCompleted = true;
|
|
382
|
+
if (!pending.inactive) {
|
|
383
|
+
pending.presented = true;
|
|
384
|
+
} else if (pending.closedOutcome) {
|
|
385
|
+
await this.#notifyResolved(pending, pending.closedOutcome);
|
|
386
|
+
}
|
|
387
|
+
} finally {
|
|
388
|
+
if (pending.presentationTask === task) pending.presentationTask = null;
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
async #submit(pending, outcome) {
|
|
393
|
+
pending.submitting = true;
|
|
394
|
+
try {
|
|
395
|
+
await pending.interaction.respond(approvalResult(pending, outcome));
|
|
396
|
+
} catch (error) {
|
|
397
|
+
if (error?.code === 'interaction-not-pending') {
|
|
398
|
+
const send = pending.send;
|
|
399
|
+
const next = this.#remove(pending);
|
|
400
|
+
await this.#transition(next, async () => {
|
|
401
|
+
await this.#notifyResolved(pending, 'resolved', send);
|
|
402
|
+
});
|
|
403
|
+
return;
|
|
404
|
+
}
|
|
405
|
+
if (pending.inactive) return;
|
|
406
|
+
pending.submitting = false;
|
|
407
|
+
this.#logger.error?.(`[dsh-im:${this.#label}] failed to submit an approval:`, error);
|
|
408
|
+
await pending.send(t('审批提交失败,请重新回复「批准」或「拒绝」。')).catch(() => undefined);
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
const send = pending.send;
|
|
413
|
+
const next = this.#remove(pending);
|
|
414
|
+
await this.#transition(next, async () => {
|
|
415
|
+
await this.#notifyResolved(pending, outcome, send);
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
async #notifyResolved(pending, outcome, send = pending.send) {
|
|
420
|
+
if (pending.resolutionNotified) return;
|
|
421
|
+
// Claim notification before awaiting the channel, so a concurrent close or
|
|
422
|
+
// resolved event cannot replace the confirmed result or notify twice.
|
|
423
|
+
pending.resolutionNotified = true;
|
|
424
|
+
const text = approvalOutcomeText(outcome);
|
|
425
|
+
try {
|
|
426
|
+
await pending.onResolved?.(text);
|
|
427
|
+
} catch (error) {
|
|
428
|
+
this.#logger.warn?.(`[dsh-im:${this.#label}] failed to update a resolved approval:`, error);
|
|
429
|
+
}
|
|
430
|
+
await send(text).catch(() => undefined);
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
async #transition(next, work) {
|
|
434
|
+
let release;
|
|
435
|
+
const barrier = new Promise((resolve) => { release = resolve; });
|
|
436
|
+
if (next) next.activationTask = barrier;
|
|
437
|
+
try {
|
|
438
|
+
await work();
|
|
439
|
+
} finally {
|
|
440
|
+
release();
|
|
441
|
+
if (next?.activationTask === barrier) next.activationTask = null;
|
|
442
|
+
}
|
|
443
|
+
await this.#promote(next);
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
async #promote(pending) {
|
|
447
|
+
if (!pending) return;
|
|
448
|
+
try {
|
|
449
|
+
await this.#present(pending);
|
|
450
|
+
} catch (error) {
|
|
451
|
+
this.#logger.error?.(
|
|
452
|
+
`[dsh-im:${this.#label}] failed to present the next approval:`,
|
|
453
|
+
error,
|
|
454
|
+
);
|
|
455
|
+
try {
|
|
456
|
+
pending.interaction.reconnect?.();
|
|
457
|
+
} catch {
|
|
458
|
+
// A replay will retry presentation when the transport can reconnect.
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
#remove(pending) {
|
|
464
|
+
if (pending.inactive) return null;
|
|
465
|
+
pending.inactive = true;
|
|
466
|
+
this.#rememberResolvedRoute(pending.key);
|
|
467
|
+
this.#byId.delete(pending.approvalId);
|
|
468
|
+
const route = this.#routes.get(pending.key);
|
|
469
|
+
if (!route) return null;
|
|
470
|
+
const wasCurrent = route.items[0] === pending;
|
|
471
|
+
const index = route.items.indexOf(pending);
|
|
472
|
+
if (index !== -1) route.items.splice(index, 1);
|
|
473
|
+
if (route.items.length === 0) {
|
|
474
|
+
this.#routes.delete(pending.key);
|
|
475
|
+
return null;
|
|
476
|
+
}
|
|
477
|
+
return wasCurrent ? route.items[0] : null;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
#rememberResolvedRoute(key) {
|
|
481
|
+
const now = Date.now();
|
|
482
|
+
for (const [routeKey, expiresAt] of this.#resolvedRoutes) {
|
|
483
|
+
if (expiresAt <= now) this.#resolvedRoutes.delete(routeKey);
|
|
484
|
+
}
|
|
485
|
+
this.#resolvedRoutes.delete(key);
|
|
486
|
+
this.#resolvedRoutes.set(key, now + RESOLVED_ROUTE_TTL_MS);
|
|
487
|
+
while (this.#resolvedRoutes.size > MAX_RESOLVED_ROUTES) {
|
|
488
|
+
this.#resolvedRoutes.delete(this.#resolvedRoutes.keys().next().value);
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
async #rejectInteraction(interaction, payload) {
|
|
493
|
+
try {
|
|
494
|
+
await interaction.respond({
|
|
495
|
+
ok: true,
|
|
496
|
+
value: {
|
|
497
|
+
sessionId: interaction.sessionId,
|
|
498
|
+
approvalId: payload.approvalId,
|
|
499
|
+
outcome: 'rejected',
|
|
500
|
+
},
|
|
501
|
+
}, { signal: AbortSignal.timeout(5_000) });
|
|
502
|
+
return true;
|
|
503
|
+
} catch (error) {
|
|
504
|
+
if (error?.code === 'interaction-not-pending') return false;
|
|
505
|
+
throw error;
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
}
|