@botharness/im-provider 4.32.0-botharness.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/PROVENANCE.json +25 -0
- package/README.en.md +445 -0
- package/README.md +448 -0
- package/THIRD_PARTY_NOTICES.md +93 -0
- package/assets/dingtalk-menu-template.json +6 -0
- package/assets/logo-dsh-im-chinese-readme-3x2.png +0 -0
- package/assets/logo-dsh-im-connecting-readme-3x2.png +0 -0
- package/assets/logo-dsh-im-connecting-square-v4.png +0 -0
- package/assets/logo-icon.png +0 -0
- package/assets/logo-plugin-message-link-gradient.webp +0 -0
- package/assets/logo-plugin-phone.png +0 -0
- package/assets/logo.png +0 -0
- package/assets/logo_cn.png +0 -0
- package/assets/screenshot-menu-card.png +0 -0
- package/cordis.patch.yml +3 -0
- package/lib/client.js +20330 -0
- package/lib/index.js +604 -0
- package/locale/en.json +5 -0
- package/locale/zh.json +5 -0
- package/package.json +181 -0
- package/plugin-src/client/access-policy-settings.js +362 -0
- package/plugin-src/client/agent-preset.js +119 -0
- package/plugin-src/client/bot-alias.js +169 -0
- package/plugin-src/client/build.mjs +38 -0
- package/plugin-src/client/channel-card-meta.js +132 -0
- package/plugin-src/client/channel-logos.js +190 -0
- package/plugin-src/client/channels/dingtalk/api.js +267 -0
- package/plugin-src/client/channels/dingtalk/index.js +954 -0
- package/plugin-src/client/channels/dingtalk/styles.js +136 -0
- package/plugin-src/client/channels/discord/api.js +11 -0
- package/plugin-src/client/channels/discord/index.js +25 -0
- package/plugin-src/client/channels/discord/styles.js +19 -0
- package/plugin-src/client/channels/email/api.js +52 -0
- package/plugin-src/client/channels/email/index.js +728 -0
- package/plugin-src/client/channels/email/styles.js +51 -0
- package/plugin-src/client/channels/feishu/api.js +405 -0
- package/plugin-src/client/channels/feishu/group-settings.js +581 -0
- package/plugin-src/client/channels/feishu/index.js +1583 -0
- package/plugin-src/client/channels/feishu/slash-panel-settings.js +322 -0
- package/plugin-src/client/channels/feishu/styles.js +573 -0
- package/plugin-src/client/channels/feishu/voice-editor.js +115 -0
- package/plugin-src/client/channels/feishu/voice-settings.js +71 -0
- package/plugin-src/client/channels/imessage/api.js +12 -0
- package/plugin-src/client/channels/imessage/index.js +46 -0
- package/plugin-src/client/channels/imessage/styles.js +19 -0
- package/plugin-src/client/channels/matrix/api.js +11 -0
- package/plugin-src/client/channels/matrix/index.js +151 -0
- package/plugin-src/client/channels/matrix/styles.js +36 -0
- package/plugin-src/client/channels/office/api.js +46 -0
- package/plugin-src/client/channels/office/index.js +153 -0
- package/plugin-src/client/channels/office/styles.js +50 -0
- package/plugin-src/client/channels/qq/api.js +171 -0
- package/plugin-src/client/channels/qq/index.js +593 -0
- package/plugin-src/client/channels/qq/styles.js +19 -0
- package/plugin-src/client/channels/shared/collapsible-account.js +103 -0
- package/plugin-src/client/channels/shared/token-api.js +124 -0
- package/plugin-src/client/channels/shared/token-channel.js +495 -0
- package/plugin-src/client/channels/slack/api.js +11 -0
- package/plugin-src/client/channels/slack/index.js +131 -0
- package/plugin-src/client/channels/slack/styles.js +34 -0
- package/plugin-src/client/channels/telegram/api.js +11 -0
- package/plugin-src/client/channels/telegram/index.js +28 -0
- package/plugin-src/client/channels/telegram/styles.js +31 -0
- package/plugin-src/client/channels/telegram/thinking-traces.js +25 -0
- package/plugin-src/client/channels/wecom/api.js +160 -0
- package/plugin-src/client/channels/wecom/index.js +625 -0
- package/plugin-src/client/channels/wecom/styles.js +19 -0
- package/plugin-src/client/channels/wecom-app/api.js +132 -0
- package/plugin-src/client/channels/wecom-app/index.js +634 -0
- package/plugin-src/client/channels/wecom-app/styles.js +25 -0
- package/plugin-src/client/channels/weixin/api.js +211 -0
- package/plugin-src/client/channels/weixin/connection-error.js +1 -0
- package/plugin-src/client/channels/weixin/index.js +801 -0
- package/plugin-src/client/channels/weixin/styles.js +115 -0
- package/plugin-src/client/channels/whatsapp/api.js +152 -0
- package/plugin-src/client/channels/whatsapp/index.js +609 -0
- package/plugin-src/client/channels/whatsapp/styles.js +19 -0
- package/plugin-src/client/connection-error.js +87 -0
- package/plugin-src/client/context-enhancement.js +415 -0
- package/plugin-src/client/credential-binding.js +118 -0
- package/plugin-src/client/delivery-settings.js +905 -0
- package/plugin-src/client/global-settings.js +433 -0
- package/plugin-src/client/i18n.js +1314 -0
- package/plugin-src/client/index.js +624 -0
- package/plugin-src/client/interface-language.js +89 -0
- package/plugin-src/client/last-message-error.js +18 -0
- package/plugin-src/client/lifecycle.js +86 -0
- package/plugin-src/client/loopback-recovery.js +75 -0
- package/plugin-src/client/model-setting.js +172 -0
- package/plugin-src/client/panel-error-boundary.js +21 -0
- package/plugin-src/client/plugin-page.js +51 -0
- package/plugin-src/client/session-channel-logos.js +243 -0
- package/plugin-src/client/styles.js +770 -0
- package/plugin-src/client/update-panel.js +14 -0
- package/plugin-src/client/workspace-directory-picker.js +280 -0
- package/plugin-src/client/workspace-editor.js +69 -0
- package/plugin-src/client/workspace-snapshot-fence.js +28 -0
- package/plugin-src/host/build.mjs +52 -0
- package/plugin-src/host/channels/dingtalk/connection-supervisor.mjs +130 -0
- package/plugin-src/host/channels/dingtalk/index.mjs +35 -0
- package/plugin-src/host/channels/dingtalk/production.mjs +193 -0
- package/plugin-src/host/channels/dingtalk/rpc.mjs +359 -0
- package/plugin-src/host/channels/discord/index.mjs +33 -0
- package/plugin-src/host/channels/discord/production.mjs +18 -0
- package/plugin-src/host/channels/discord/rpc.mjs +21 -0
- package/plugin-src/host/channels/email/availability.mjs +60 -0
- package/plugin-src/host/channels/email/index.mjs +36 -0
- package/plugin-src/host/channels/email/production.mjs +58 -0
- package/plugin-src/host/channels/email/rpc.mjs +163 -0
- package/plugin-src/host/channels/feishu/connection-supervisor.mjs +167 -0
- package/plugin-src/host/channels/feishu/controller.mjs +172 -0
- package/plugin-src/host/channels/feishu/credential-store.mjs +84 -0
- package/plugin-src/host/channels/feishu/index.mjs +69 -0
- package/plugin-src/host/channels/feishu/production.mjs +319 -0
- package/plugin-src/host/channels/feishu/rpc.mjs +863 -0
- package/plugin-src/host/channels/imessage/index.mjs +21 -0
- package/plugin-src/host/channels/imessage/production.mjs +13 -0
- package/plugin-src/host/channels/imessage/rpc.mjs +78 -0
- package/plugin-src/host/channels/matrix/index.mjs +31 -0
- package/plugin-src/host/channels/matrix/production.mjs +241 -0
- package/plugin-src/host/channels/matrix/rpc.mjs +228 -0
- package/plugin-src/host/channels/office/index.mjs +14 -0
- package/plugin-src/host/channels/office/production.mjs +45 -0
- package/plugin-src/host/channels/office/rpc.mjs +51 -0
- package/plugin-src/host/channels/qq/connection-supervisor.mjs +124 -0
- package/plugin-src/host/channels/qq/index.mjs +28 -0
- package/plugin-src/host/channels/qq/production.mjs +181 -0
- package/plugin-src/host/channels/qq/rpc.mjs +250 -0
- package/plugin-src/host/channels/shared/access-policy-production.mjs +137 -0
- package/plugin-src/host/channels/shared/access-policy-rpc.mjs +17 -0
- package/plugin-src/host/channels/shared/agent-preset-rpc.mjs +25 -0
- package/plugin-src/host/channels/shared/bot-alias-rpc.mjs +15 -0
- package/plugin-src/host/channels/shared/connection-supervisor.mjs +133 -0
- package/plugin-src/host/channels/shared/context-enhancement-rpc.mjs +17 -0
- package/plugin-src/host/channels/shared/model-setting-rpc.mjs +18 -0
- package/plugin-src/host/channels/shared/production.mjs +239 -0
- package/plugin-src/host/channels/shared/rpc.mjs +226 -0
- package/plugin-src/host/channels/shared/startup-error.mjs +55 -0
- package/plugin-src/host/channels/shared/startup.mjs +76 -0
- package/plugin-src/host/channels/shared/thinking-traces-rpc.mjs +11 -0
- package/plugin-src/host/channels/shared/workspace-rpc.mjs +31 -0
- package/plugin-src/host/channels/slack/index.mjs +31 -0
- package/plugin-src/host/channels/slack/production.mjs +176 -0
- package/plugin-src/host/channels/slack/rpc.mjs +221 -0
- package/plugin-src/host/channels/telegram/index.mjs +33 -0
- package/plugin-src/host/channels/telegram/production.mjs +19 -0
- package/plugin-src/host/channels/telegram/rpc.mjs +22 -0
- package/plugin-src/host/channels/wecom/connection-supervisor.mjs +124 -0
- package/plugin-src/host/channels/wecom/index.mjs +34 -0
- package/plugin-src/host/channels/wecom/production.mjs +187 -0
- package/plugin-src/host/channels/wecom/rpc.mjs +255 -0
- package/plugin-src/host/channels/wecom-app/index.mjs +33 -0
- package/plugin-src/host/channels/wecom-app/production.mjs +221 -0
- package/plugin-src/host/channels/wecom-app/rpc.mjs +235 -0
- package/plugin-src/host/channels/weixin/connection-supervisor.mjs +139 -0
- package/plugin-src/host/channels/weixin/index.mjs +44 -0
- package/plugin-src/host/channels/weixin/production.mjs +258 -0
- package/plugin-src/host/channels/weixin/rpc.mjs +285 -0
- package/plugin-src/host/channels/whatsapp/index.mjs +33 -0
- package/plugin-src/host/channels/whatsapp/production.mjs +198 -0
- package/plugin-src/host/channels/whatsapp/rpc.mjs +242 -0
- package/plugin-src/host/delivery-adapter.mjs +411 -0
- package/plugin-src/host/delivery-http.mjs +132 -0
- package/plugin-src/host/delivery-rpc.mjs +173 -0
- package/plugin-src/host/delivery-service.mjs +618 -0
- package/plugin-src/host/delivery-suggestions.mjs +198 -0
- package/plugin-src/host/harness-command-executor.mjs +48 -0
- package/plugin-src/host/harness-connection.mjs +20 -0
- package/plugin-src/host/harness-session-coordinator.mjs +183 -0
- package/plugin-src/host/host-language-rpc.mjs +71 -0
- package/plugin-src/host/host-language.mjs +157 -0
- package/plugin-src/host/image-input-rpc.mjs +24 -0
- package/plugin-src/host/inbound-ttl-rpc.mjs +86 -0
- package/plugin-src/host/inbound-ttl-runtime.mjs +85 -0
- package/plugin-src/host/inbound-ttl-service.mjs +179 -0
- package/plugin-src/host/index.mjs +229 -0
- package/plugin-src/host/injected-context.mjs +104 -0
- package/plugin-src/host/lark-sdk-handshake-patch.mjs +181 -0
- package/plugin-src/host/modern-harness-api.mjs +738 -0
- package/plugin-src/host/rpc-authority.mjs +12 -0
- package/plugin-src/host/session-sync-coordinator.mjs +233 -0
- package/plugin-src/host/session-title-prefix.mjs +122 -0
- package/plugin-src/host/update-policy.mjs +31 -0
- package/plugin-src/host/update-rpc.mjs +16 -0
- package/plugin-src/host/update-runtime.mjs +339 -0
- package/plugin-src/host/update-service.mjs +392 -0
- package/plugin-src/management-rpc.mjs +87 -0
- package/src/channels/dingtalk/config-store.mjs +284 -0
- package/src/channels/dingtalk/connection-error.mjs +292 -0
- package/src/channels/dingtalk/device-auth.mjs +240 -0
- package/src/channels/dingtalk/dingtalk-api.mjs +1140 -0
- package/src/channels/dingtalk/dingtalk-bridge.mjs +1889 -0
- package/src/channels/dingtalk/dingtalk-card-stream.mjs +242 -0
- package/src/channels/dingtalk/dingtalk-controller.mjs +852 -0
- package/src/channels/dingtalk/dingtalk-menu.mjs +95 -0
- package/src/channels/dingtalk/dingtalk-runtime.mjs +482 -0
- package/src/channels/dingtalk/harness-client.mjs +19 -0
- package/src/channels/dingtalk/state-store.mjs +325 -0
- package/src/channels/discord/config-store.mjs +28 -0
- package/src/channels/discord/discord-api.mjs +423 -0
- package/src/channels/discord/discord-bridge.mjs +16 -0
- package/src/channels/discord/discord-controller.mjs +29 -0
- package/src/channels/discord/discord-runtime.mjs +1091 -0
- package/src/channels/discord/external-consumer.mjs +199 -0
- package/src/channels/discord/external-files.mjs +97 -0
- package/src/channels/discord/harness-client.mjs +11 -0
- package/src/channels/discord/history-reader.mjs +142 -0
- package/src/channels/discord/nearby-history.mjs +112 -0
- package/src/channels/discord/state-store.mjs +3 -0
- package/src/channels/email/config-store.mjs +194 -0
- package/src/channels/email/email-api.mjs +20 -0
- package/src/channels/email/email-bridge.mjs +17 -0
- package/src/channels/email/email-controller.mjs +824 -0
- package/src/channels/email/email-runtime.mjs +699 -0
- package/src/channels/email/harness-client.mjs +7 -0
- package/src/channels/email/mail-format.mjs +91 -0
- package/src/channels/email/state-store.mjs +176 -0
- package/src/channels/email/transport.mjs +64 -0
- package/src/channels/email/transports/agent-mail.mjs +604 -0
- package/src/channels/email/transports/agently-cli.mjs +288 -0
- package/src/channels/email/transports/imap-smtp.mjs +165 -0
- package/src/channels/feishu/bridge.mjs +6286 -0
- package/src/channels/feishu/config.mjs +71 -0
- package/src/channels/feishu/external-cards.mjs +87 -0
- package/src/channels/feishu/external-consumer.mjs +89 -0
- package/src/channels/feishu/external-files.mjs +115 -0
- package/src/channels/feishu/external-images.mjs +50 -0
- package/src/channels/feishu/external-names.mjs +99 -0
- package/src/channels/feishu/feishu-app.mjs +54 -0
- package/src/channels/feishu/feishu-cards.mjs +1285 -0
- package/src/channels/feishu/feishu-channel.mjs +651 -0
- package/src/channels/feishu/feishu-runtime.mjs +982 -0
- package/src/channels/feishu/group-message-permission-manager.mjs +72 -0
- package/src/channels/feishu/group-response-mode.mjs +15 -0
- package/src/channels/feishu/harness-client.mjs +19 -0
- package/src/channels/feishu/history-reader.mjs +77 -0
- package/src/channels/feishu/live-cot.mjs +260 -0
- package/src/channels/feishu/message-utils.mjs +613 -0
- package/src/channels/feishu/multi-bot-controller.mjs +1619 -0
- package/src/channels/feishu/nearby-history.mjs +72 -0
- package/src/channels/feishu/plugin-config-store.mjs +216 -0
- package/src/channels/feishu/plugin-controller.mjs +254 -0
- package/src/channels/feishu/registration-manager.mjs +349 -0
- package/src/channels/feishu/repair-manager.mjs +138 -0
- package/src/channels/feishu/reply-context.mjs +22 -0
- package/src/channels/feishu/slash-command-panel.mjs +155 -0
- package/src/channels/feishu/slash-command-registry.mjs +495 -0
- package/src/channels/feishu/state-store.mjs +214 -0
- package/src/channels/feishu/step-push-mode.mjs +25 -0
- package/src/channels/feishu/voice-config.mjs +34 -0
- package/src/channels/feishu/voice.mjs +134 -0
- package/src/channels/imessage/config-store.mjs +24 -0
- package/src/channels/imessage/controller.mjs +37 -0
- package/src/channels/imessage/harness-client.mjs +7 -0
- package/src/channels/imessage/imessage-api.mjs +194 -0
- package/src/channels/imessage/imessage-bridge.mjs +16 -0
- package/src/channels/imessage/runtime.mjs +102 -0
- package/src/channels/imessage/state-store.mjs +3 -0
- package/src/channels/matrix/matrix-api.mjs +696 -0
- package/src/channels/matrix/matrix-bridge.mjs +20 -0
- package/src/channels/matrix/matrix-config-store.mjs +368 -0
- package/src/channels/matrix/matrix-controller.mjs +404 -0
- package/src/channels/matrix/matrix-crypto-store.mjs +279 -0
- package/src/channels/matrix/matrix-crypto.mjs +1014 -0
- package/src/channels/matrix/matrix-harness-client.mjs +11 -0
- package/src/channels/matrix/matrix-normalize.mjs +357 -0
- package/src/channels/matrix/matrix-rich-text.mjs +313 -0
- package/src/channels/matrix/matrix-room-history.mjs +316 -0
- package/src/channels/matrix/matrix-runtime.mjs +1014 -0
- package/src/channels/office/config-store.mjs +107 -0
- package/src/channels/office/office-controller.mjs +178 -0
- package/src/channels/office/office-job-executor.mjs +391 -0
- package/src/channels/office/office-runtime.mjs +175 -0
- package/src/channels/office/office-transport.mjs +171 -0
- package/src/channels/office/protocol.mjs +43 -0
- package/src/channels/qq/config-store.mjs +173 -0
- package/src/channels/qq/harness-client.mjs +11 -0
- package/src/channels/qq/markdown-reply.mjs +446 -0
- package/src/channels/qq/qq-bridge.mjs +1507 -0
- package/src/channels/qq/qq-controller.mjs +514 -0
- package/src/channels/qq/qq-menu.mjs +208 -0
- package/src/channels/qq/qq-runtime.mjs +303 -0
- package/src/channels/qq/qr-auth.mjs +24 -0
- package/src/channels/qq/state-error.mjs +17 -0
- package/src/channels/qq/state-store.mjs +143 -0
- package/src/channels/shared/access-policy.mjs +210 -0
- package/src/channels/shared/agent-preset.mjs +76 -0
- package/src/channels/shared/batch-input.mjs +229 -0
- package/src/channels/shared/bot-alias.mjs +29 -0
- package/src/channels/shared/bot-workspace-store.mjs +2383 -0
- package/src/channels/shared/command-catalog.mjs +107 -0
- package/src/channels/shared/command-permission.mjs +32 -0
- package/src/channels/shared/compact-command.mjs +109 -0
- package/src/channels/shared/config-read-error.mjs +24 -0
- package/src/channels/shared/connection-error.mjs +191 -0
- package/src/channels/shared/connection-test.mjs +58 -0
- package/src/channels/shared/context-enhancement.mjs +372 -0
- package/src/channels/shared/control-command.mjs +111 -0
- package/src/channels/shared/conversation-state-store.mjs +165 -0
- package/src/channels/shared/default-workspace.mjs +31 -0
- package/src/channels/shared/deferred-delivery-coordinator.mjs +241 -0
- package/src/channels/shared/deferred-delivery.mjs +161 -0
- package/src/channels/shared/deferred-state.mjs +53 -0
- package/src/channels/shared/diagnostic-details.mjs +95 -0
- package/src/channels/shared/editable-message-stream.mjs +100 -0
- package/src/channels/shared/exclusive-inbound-consumers.mjs +91 -0
- package/src/channels/shared/file-download.mjs +64 -0
- package/src/channels/shared/harness-approval.mjs +508 -0
- package/src/channels/shared/harness-client.mjs +2143 -0
- package/src/channels/shared/harness-mux.mjs +123 -0
- package/src/channels/shared/harness-question.mjs +95 -0
- package/src/channels/shared/harness-session-binding.mjs +119 -0
- package/src/channels/shared/history-command.mjs +193 -0
- package/src/channels/shared/i18n-en/diagnostics.mjs +55 -0
- package/src/channels/shared/i18n-en/dingtalk.mjs +81 -0
- package/src/channels/shared/i18n-en/discord.mjs +10 -0
- package/src/channels/shared/i18n-en/email.mjs +25 -0
- package/src/channels/shared/i18n-en/feishu.mjs +404 -0
- package/src/channels/shared/i18n-en/image-input.mjs +18 -0
- package/src/channels/shared/i18n-en/matrix.mjs +85 -0
- package/src/channels/shared/i18n-en/office.mjs +23 -0
- package/src/channels/shared/i18n-en/qq.mjs +96 -0
- package/src/channels/shared/i18n-en/shared-a.mjs +241 -0
- package/src/channels/shared/i18n-en/shared-b.mjs +263 -0
- package/src/channels/shared/i18n-en/shared-c.mjs +203 -0
- package/src/channels/shared/i18n-en/slack.mjs +27 -0
- package/src/channels/shared/i18n-en/telegram.mjs +27 -0
- package/src/channels/shared/i18n-en/wecom-app.mjs +33 -0
- package/src/channels/shared/i18n-en/wecom.mjs +62 -0
- package/src/channels/shared/i18n-en/weixin.mjs +88 -0
- package/src/channels/shared/i18n-en/whatsapp.mjs +17 -0
- package/src/channels/shared/i18n-en.mjs +45 -0
- package/src/channels/shared/i18n.mjs +78 -0
- package/src/channels/shared/im-source-guidance.mjs +65 -0
- package/src/channels/shared/image-input-policy.mjs +35 -0
- package/src/channels/shared/image-input-settings-store.mjs +60 -0
- package/src/channels/shared/image-input.mjs +124 -0
- package/src/channels/shared/image-prompt.mjs +360 -0
- package/src/channels/shared/inbound-access.mjs +43 -0
- package/src/channels/shared/inbound-file.mjs +323 -0
- package/src/channels/shared/inbound-ttl-store.mjs +110 -0
- package/src/channels/shared/inbound-ttl.mjs +22 -0
- package/src/channels/shared/injected-context.mjs +362 -0
- package/src/channels/shared/interface-language-store.mjs +127 -0
- package/src/channels/shared/interface-language.mjs +51 -0
- package/src/channels/shared/message-failure.mjs +257 -0
- package/src/channels/shared/model-command.mjs +741 -0
- package/src/channels/shared/model-setting.mjs +145 -0
- package/src/channels/shared/preset-command.mjs +308 -0
- package/src/channels/shared/semantic/artifact-delivery.mjs +177 -0
- package/src/channels/shared/semantic/artifact.mjs +750 -0
- package/src/channels/shared/semantic/delivery.mjs +193 -0
- package/src/channels/shared/semantic/reply-reference.mjs +148 -0
- package/src/channels/shared/session-binding-lock.mjs +40 -0
- package/src/channels/shared/session-channel-labels.mjs +28 -0
- package/src/channels/shared/session-reply-recovery.mjs +104 -0
- package/src/channels/shared/session-sync-registry.mjs +22 -0
- package/src/channels/shared/session-title.mjs +74 -0
- package/src/channels/shared/status-reaction.mjs +107 -0
- package/src/channels/shared/text-harness-bridge.mjs +1623 -0
- package/src/channels/shared/token-bot-controller.mjs +529 -0
- package/src/channels/shared/token-config-store.mjs +208 -0
- package/src/channels/shared/video-media.mjs +9 -0
- package/src/channels/shared/workspace-command.mjs +532 -0
- package/src/channels/shared/workspace-session.mjs +196 -0
- package/src/channels/slack/config-store.mjs +177 -0
- package/src/channels/slack/external-consumer.mjs +93 -0
- package/src/channels/slack/external-files.mjs +90 -0
- package/src/channels/slack/harness-client.mjs +11 -0
- package/src/channels/slack/history-reader.mjs +203 -0
- package/src/channels/slack/manifest.mjs +37 -0
- package/src/channels/slack/slack-api.mjs +644 -0
- package/src/channels/slack/slack-bridge.mjs +20 -0
- package/src/channels/slack/slack-controller.mjs +468 -0
- package/src/channels/slack/slack-runtime.mjs +895 -0
- package/src/channels/slack/state-store.mjs +3 -0
- package/src/channels/telegram/config-store.mjs +89 -0
- package/src/channels/telegram/harness-client.mjs +11 -0
- package/src/channels/telegram/state-store.mjs +3 -0
- package/src/channels/telegram/telegram-api.mjs +486 -0
- package/src/channels/telegram/telegram-bridge.mjs +81 -0
- package/src/channels/telegram/telegram-controller.mjs +52 -0
- package/src/channels/telegram/telegram-http.mjs +21 -0
- package/src/channels/telegram/telegram-rich-message.mjs +149 -0
- package/src/channels/telegram/telegram-runtime.mjs +1471 -0
- package/src/channels/wecom/config-store.mjs +172 -0
- package/src/channels/wecom/harness-client.mjs +11 -0
- package/src/channels/wecom/qr-auth.mjs +95 -0
- package/src/channels/wecom/send-error.mjs +31 -0
- package/src/channels/wecom/state-store.mjs +107 -0
- package/src/channels/wecom/wecom-bridge.mjs +1833 -0
- package/src/channels/wecom/wecom-cards.mjs +101 -0
- package/src/channels/wecom/wecom-controller.mjs +530 -0
- package/src/channels/wecom/wecom-runtime.mjs +269 -0
- package/src/channels/wecom-app/callback-server.mjs +471 -0
- package/src/channels/wecom-app/config-store.mjs +242 -0
- package/src/channels/wecom-app/harness-client.mjs +11 -0
- package/src/channels/wecom-app/state-store.mjs +107 -0
- package/src/channels/wecom-app/wecom-app-api.mjs +432 -0
- package/src/channels/wecom-app/wecom-app-bridge.mjs +1072 -0
- package/src/channels/wecom-app/wecom-app-controller.mjs +453 -0
- package/src/channels/wecom-app/wecom-app-runtime.mjs +222 -0
- package/src/channels/weixin/config-store.mjs +194 -0
- package/src/channels/weixin/connection-error.en.mjs +137 -0
- package/src/channels/weixin/connection-error.mjs +153 -0
- package/src/channels/weixin/diagnostic-details.mjs +1 -0
- package/src/channels/weixin/external-consumer.mjs +160 -0
- package/src/channels/weixin/external-files.mjs +84 -0
- package/src/channels/weixin/external-typing.mjs +120 -0
- package/src/channels/weixin/harness-client.mjs +19 -0
- package/src/channels/weixin/state-store.mjs +316 -0
- package/src/channels/weixin/weixin-api.mjs +1009 -0
- package/src/channels/weixin/weixin-bridge.mjs +1537 -0
- package/src/channels/weixin/weixin-controller.mjs +779 -0
- package/src/channels/weixin/weixin-runtime.mjs +507 -0
- package/src/channels/whatsapp/config-store.mjs +210 -0
- package/src/channels/whatsapp/harness-client.mjs +11 -0
- package/src/channels/whatsapp/state-store.mjs +3 -0
- package/src/channels/whatsapp/whatsapp-bridge.mjs +18 -0
- package/src/channels/whatsapp/whatsapp-controller.mjs +470 -0
- package/src/channels/whatsapp/whatsapp-runtime.mjs +917 -0
- package/src/channels/whatsapp/whatsapp-web-session.mjs +214 -0
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { withSessionBindingLock } from './session-binding-lock.mjs';
|
|
2
|
+
import { initialSessionTitle } from './session-title.mjs';
|
|
3
|
+
|
|
4
|
+
export const WORKSPACE_SESSION_STALE = 'workspace-session-stale';
|
|
5
|
+
|
|
6
|
+
function workspaceSessionStaleError() {
|
|
7
|
+
const error = new Error('The conversation workspace changed before the prompt was sent.');
|
|
8
|
+
error.code = WORKSPACE_SESSION_STALE;
|
|
9
|
+
return error;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Read the conversation's effective-workspace generation. A bot-scoped Harness
|
|
14
|
+
* exposes it; anything else (plain fixtures, older Harnesses) yields null and
|
|
15
|
+
* disables the conversation-level fence without changing existing behavior.
|
|
16
|
+
*/
|
|
17
|
+
function readConversationGeneration(harness, conversationKey) {
|
|
18
|
+
return typeof harness?.conversationWorkspaceGeneration === 'function'
|
|
19
|
+
? harness.conversationWorkspaceGeneration(conversationKey) ?? null
|
|
20
|
+
: null;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function conversationGenerationMoved(harness, conversationKey, generation) {
|
|
24
|
+
if (generation === null) return false;
|
|
25
|
+
return readConversationGeneration(harness, conversationKey) !== generation;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Wait for a conversation-level workspace switch that is still committing.
|
|
30
|
+
* An explicit /conv publishes its fence before it persists, so a message that
|
|
31
|
+
* is already in flight must settle on the new workspace instead of resolving a
|
|
32
|
+
* session in the one being left behind.
|
|
33
|
+
*/
|
|
34
|
+
async function awaitPendingConversationSwitch(harness, conversationKey) {
|
|
35
|
+
if (typeof harness?.pendingConversationWorkspaceSwitch !== 'function') return;
|
|
36
|
+
const pending = harness.pendingConversationWorkspaceSwitch(conversationKey);
|
|
37
|
+
if (pending && typeof pending.then === 'function') await pending.catch(() => undefined);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function workspaceSession(harness, sessionId, conversationKey) {
|
|
41
|
+
if (typeof harness.workspaceSession === 'function') {
|
|
42
|
+
return conversationKey
|
|
43
|
+
? harness.workspaceSession(sessionId, conversationKey)
|
|
44
|
+
: harness.workspaceSession(sessionId);
|
|
45
|
+
}
|
|
46
|
+
const session = {
|
|
47
|
+
sessionId,
|
|
48
|
+
sessionExists: (...args) => harness.sessionExists(sessionId, ...args),
|
|
49
|
+
models: (...args) => harness.getSessionModels(sessionId, ...args),
|
|
50
|
+
selectModel: (...args) => harness.selectSessionModel(sessionId, ...args),
|
|
51
|
+
isRunning: (...args) => harness.isSessionRunning(sessionId, ...args),
|
|
52
|
+
hasActiveTurn: (...args) => harness.hasActiveTurn(sessionId, ...args),
|
|
53
|
+
stopActiveTurn: (...args) => harness.stopActiveTurn(sessionId, ...args),
|
|
54
|
+
stopDeferredTurn: (...args) => harness.stopDeferredTurn?.(sessionId, ...args) ?? false,
|
|
55
|
+
steerActiveTurn: (...args) => harness.steerActiveTurn(sessionId, ...args),
|
|
56
|
+
ask: (...args) => harness.ask(sessionId, ...args),
|
|
57
|
+
};
|
|
58
|
+
if (typeof harness.renameSession === 'function') {
|
|
59
|
+
session.renameTitle = (...args) => harness.renameSession(sessionId, ...args);
|
|
60
|
+
}
|
|
61
|
+
return Object.freeze(session);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async function sessionExists(session, options) {
|
|
65
|
+
return options === undefined
|
|
66
|
+
? session.sessionExists()
|
|
67
|
+
: session.sessionExists(options);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async function createSession(harness, options) {
|
|
71
|
+
return options === undefined
|
|
72
|
+
? harness.createSession()
|
|
73
|
+
: harness.createSession(options);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Resolve, persist, and ask through a session that belongs to the bot's
|
|
78
|
+
* current workspace. A concurrent workspace switch invalidates the scoped
|
|
79
|
+
* session and retries before any prompt is sent to the stale session.
|
|
80
|
+
*
|
|
81
|
+
* `titleText` names the conversation title when the prompt itself is not the
|
|
82
|
+
* user's own words -- a batch submission composes dsh-im's framing sentence and
|
|
83
|
+
* message labels into one prompt, and only the collected text may name the
|
|
84
|
+
* conversation.
|
|
85
|
+
*
|
|
86
|
+
* `sourceGuidance` is the guidance the channel's captured enhancement settings
|
|
87
|
+
* applied, carried to the Host out of band so it can materialize it as session
|
|
88
|
+
* prompt context. It is never re-derived from the prompt, which also carries
|
|
89
|
+
* whatever the user typed.
|
|
90
|
+
*/
|
|
91
|
+
export async function askInWorkspaceSession({
|
|
92
|
+
harness,
|
|
93
|
+
state,
|
|
94
|
+
key,
|
|
95
|
+
text,
|
|
96
|
+
content,
|
|
97
|
+
prepareContent,
|
|
98
|
+
titleText,
|
|
99
|
+
sourceGuidance,
|
|
100
|
+
contextEnhanced = false,
|
|
101
|
+
createOptions,
|
|
102
|
+
existsOptions,
|
|
103
|
+
askOptions,
|
|
104
|
+
deferredDelivery,
|
|
105
|
+
}) {
|
|
106
|
+
const initialTitle = contextEnhanced
|
|
107
|
+
? initialSessionTitle({
|
|
108
|
+
text: titleText ?? text,
|
|
109
|
+
content,
|
|
110
|
+
files: typeof askOptions === 'object' ? askOptions?.files : undefined,
|
|
111
|
+
})
|
|
112
|
+
: null;
|
|
113
|
+
const renameSignal = createOptions?.signal
|
|
114
|
+
?? (typeof askOptions === 'object' ? askOptions?.signal : undefined);
|
|
115
|
+
const renameOptions = renameSignal ? { signal: renameSignal } : undefined;
|
|
116
|
+
while (true) {
|
|
117
|
+
try {
|
|
118
|
+
const binding = await withSessionBindingLock(state, key, async () => {
|
|
119
|
+
await awaitPendingConversationSwitch(harness, key);
|
|
120
|
+
let sessionId = state.sessionFor(key);
|
|
121
|
+
let session = sessionId ? workspaceSession(harness, sessionId, key) : null;
|
|
122
|
+
if (!session || !(await sessionExists(session, existsOptions))) {
|
|
123
|
+
sessionId = await createSession(harness, {
|
|
124
|
+
conversationKey: key,
|
|
125
|
+
...(createOptions ?? {}),
|
|
126
|
+
});
|
|
127
|
+
if (await state.setSession(key, sessionId) === false) return null;
|
|
128
|
+
// Binding committed: capture the conversation's effective-workspace
|
|
129
|
+
// generation together with the session, so the prompt below is fenced
|
|
130
|
+
// against a conversation switch that only commits after the bind.
|
|
131
|
+
const conversationGeneration = readConversationGeneration(harness, key);
|
|
132
|
+
session = workspaceSession(harness, sessionId, key);
|
|
133
|
+
if (initialTitle && typeof session.renameTitle === 'function') {
|
|
134
|
+
try {
|
|
135
|
+
await session.renameTitle(initialTitle, renameOptions);
|
|
136
|
+
} catch (error) {
|
|
137
|
+
if (error?.code === WORKSPACE_SESSION_STALE || renameOptions?.signal?.aborted) {
|
|
138
|
+
throw error;
|
|
139
|
+
}
|
|
140
|
+
console.warn('[dsh-im] unable to set the initial Session title:', error?.message ?? error);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
// A switch that committed while the title was being set has already
|
|
144
|
+
// cleared the mapping and must not receive this prompt.
|
|
145
|
+
if (conversationGenerationMoved(harness, key, conversationGeneration)) {
|
|
146
|
+
throw workspaceSessionStaleError();
|
|
147
|
+
}
|
|
148
|
+
return { sessionId, session };
|
|
149
|
+
}
|
|
150
|
+
return { sessionId, session };
|
|
151
|
+
});
|
|
152
|
+
if (!binding) continue;
|
|
153
|
+
const artifacts = [];
|
|
154
|
+
const originalOnArtifact = typeof askOptions === 'object'
|
|
155
|
+
&& typeof askOptions?.onArtifact === 'function'
|
|
156
|
+
? askOptions.onArtifact
|
|
157
|
+
: null;
|
|
158
|
+
const artifactOptions = typeof askOptions === 'number'
|
|
159
|
+
? { timeoutMs: askOptions }
|
|
160
|
+
: { ...askOptions };
|
|
161
|
+
// The guidance the channel's captured settings applied, carried out of
|
|
162
|
+
// band so the Host never has to read configuration out of the prompt.
|
|
163
|
+
artifactOptions.sourceGuidance = sourceGuidance;
|
|
164
|
+
artifactOptions.onArtifact = async (artifact) => {
|
|
165
|
+
artifacts.push(artifact);
|
|
166
|
+
await originalOnArtifact?.(artifact);
|
|
167
|
+
};
|
|
168
|
+
let answer;
|
|
169
|
+
// Prepare session-dependent context after binding, outside the binding
|
|
170
|
+
// lock. A stale-workspace retry must prepare it for the new session too.
|
|
171
|
+
const prompt = prepareContent
|
|
172
|
+
? await prepareContent({ sessionId: binding.sessionId, content, text })
|
|
173
|
+
: content ?? text;
|
|
174
|
+
try {
|
|
175
|
+
answer = await binding.session.ask(prompt, artifactOptions);
|
|
176
|
+
} catch (error) {
|
|
177
|
+
if (error?.code === 'harness-reply-timeout' && deferredDelivery) {
|
|
178
|
+
try {
|
|
179
|
+
const { coordinator, ...destination } = deferredDelivery();
|
|
180
|
+
await coordinator.trackTimeout(error, { ...destination, key, sessionId: binding.sessionId });
|
|
181
|
+
} catch (registrationError) {
|
|
182
|
+
console.warn('[dsh-im] unable to persist deferred delivery:', registrationError.message);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
throw error;
|
|
186
|
+
}
|
|
187
|
+
return {
|
|
188
|
+
sessionId: binding.sessionId,
|
|
189
|
+
answer,
|
|
190
|
+
...(artifacts.length > 0 ? { artifacts } : {}),
|
|
191
|
+
};
|
|
192
|
+
} catch (error) {
|
|
193
|
+
if (error?.code !== WORKSPACE_SESSION_STALE) throw error;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { mkdir, readFile, rename, unlink, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { dirname } from 'node:path';
|
|
4
|
+
|
|
5
|
+
import { t } from '../shared/i18n.mjs';
|
|
6
|
+
|
|
7
|
+
const EMPTY_DOCUMENT = Object.freeze({ version: 1, bots: Object.freeze([]) });
|
|
8
|
+
const BOT_ID_PATTERN = /^slack_[a-f0-9]{24}$/;
|
|
9
|
+
const BOT_TOKEN_REF_PATTERN = /^DSH_SLACK_BOT_TOKEN_[A-F0-9]{24}$/;
|
|
10
|
+
const APP_TOKEN_REF_PATTERN = /^DSH_SLACK_APP_TOKEN_[A-F0-9]{24}$/;
|
|
11
|
+
|
|
12
|
+
function cleanString(value) {
|
|
13
|
+
return typeof value === 'string' && value.trim() ? value.trim() : null;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function deriveSlackBotIdentity(platformId) {
|
|
17
|
+
const raw = cleanString(platformId);
|
|
18
|
+
if (!raw) throw new TypeError('platformId is required');
|
|
19
|
+
const digest = createHash('sha256').update(raw).digest('hex').slice(0, 24);
|
|
20
|
+
const suffix = digest.toUpperCase();
|
|
21
|
+
return {
|
|
22
|
+
botId: `slack_${digest}`,
|
|
23
|
+
botTokenRef: `DSH_SLACK_BOT_TOKEN_${suffix}`,
|
|
24
|
+
appTokenRef: `DSH_SLACK_APP_TOKEN_${suffix}`,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function maskSlackBotId(platformId) {
|
|
29
|
+
const value = cleanString(platformId) ?? '';
|
|
30
|
+
const [teamId, userId] = value.split(':');
|
|
31
|
+
if (teamId && userId) return `${teamId.slice(0, 5)}••• · ${userId.slice(0, 5)}•••`;
|
|
32
|
+
return value ? `${value.slice(0, 6)}••••` : t('Slack机器人');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export class SlackConfigStore {
|
|
36
|
+
#path;
|
|
37
|
+
#value = EMPTY_DOCUMENT;
|
|
38
|
+
#writeQueue = Promise.resolve();
|
|
39
|
+
|
|
40
|
+
constructor(path) {
|
|
41
|
+
this.#path = path;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async load() {
|
|
45
|
+
try {
|
|
46
|
+
const normalized = this.#normalizeDocument(JSON.parse(await readFile(this.#path, 'utf8')));
|
|
47
|
+
if (!normalized) throw new Error('dsh-im Slack config contains invalid bot data');
|
|
48
|
+
this.#value = normalized;
|
|
49
|
+
} catch (error) {
|
|
50
|
+
if (error?.code !== 'ENOENT') throw error;
|
|
51
|
+
this.#value = EMPTY_DOCUMENT;
|
|
52
|
+
}
|
|
53
|
+
return this;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
list() {
|
|
57
|
+
return structuredClone(this.#value.bots);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
get(botId) {
|
|
61
|
+
const bot = this.#value.bots.find((candidate) => candidate.botId === botId);
|
|
62
|
+
return bot ? structuredClone(bot) : null;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
getByPlatformId(platformId) {
|
|
66
|
+
const bot = this.#value.bots.find((candidate) => candidate.platformId === platformId);
|
|
67
|
+
return bot ? structuredClone(bot) : null;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async save(value) {
|
|
71
|
+
const normalized = this.#normalizeBot(value);
|
|
72
|
+
if (!normalized) throw new Error('Refusing to persist incomplete Slack bot data');
|
|
73
|
+
return this.#mutate((bots) => {
|
|
74
|
+
const collision = bots.find((bot) => (
|
|
75
|
+
bot.botId !== normalized.botId
|
|
76
|
+
&& (bot.platformId === normalized.platformId
|
|
77
|
+
|| bot.botTokenRef === normalized.botTokenRef
|
|
78
|
+
|| bot.appTokenRef === normalized.appTokenRef)
|
|
79
|
+
));
|
|
80
|
+
if (collision) throw new Error('Duplicate Slack bot identity');
|
|
81
|
+
const index = bots.findIndex((bot) => bot.botId === normalized.botId);
|
|
82
|
+
if (index === -1) bots.push(normalized);
|
|
83
|
+
else bots[index] = normalized;
|
|
84
|
+
return structuredClone(normalized);
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async remove(botId) {
|
|
89
|
+
if (!BOT_ID_PATTERN.test(botId)) throw new TypeError('Invalid Slack bot id');
|
|
90
|
+
return this.#mutate((bots) => {
|
|
91
|
+
const index = bots.findIndex((bot) => bot.botId === botId);
|
|
92
|
+
if (index === -1) return null;
|
|
93
|
+
return structuredClone(bots.splice(index, 1)[0]);
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async clear() {
|
|
98
|
+
const operation = this.#writeQueue.then(async () => {
|
|
99
|
+
try {
|
|
100
|
+
await unlink(this.#path);
|
|
101
|
+
} catch (error) {
|
|
102
|
+
if (error?.code !== 'ENOENT') throw error;
|
|
103
|
+
}
|
|
104
|
+
this.#value = EMPTY_DOCUMENT;
|
|
105
|
+
});
|
|
106
|
+
this.#writeQueue = operation.then(() => undefined, () => undefined);
|
|
107
|
+
await operation;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
#normalizeBot(value) {
|
|
111
|
+
if (!value || typeof value !== 'object') return null;
|
|
112
|
+
if (value.consumerMode !== undefined && !['external-consumer', 'standalone-session'].includes(value.consumerMode)) return null;
|
|
113
|
+
const botId = cleanString(value.botId);
|
|
114
|
+
const platformId = cleanString(value.platformId);
|
|
115
|
+
const botTokenRef = cleanString(value.botTokenRef);
|
|
116
|
+
const appTokenRef = cleanString(value.appTokenRef);
|
|
117
|
+
const name = cleanString(value.name);
|
|
118
|
+
if (!botId || !platformId || !botTokenRef || !appTokenRef || !name
|
|
119
|
+
|| !BOT_ID_PATTERN.test(botId)
|
|
120
|
+
|| !BOT_TOKEN_REF_PATTERN.test(botTokenRef)
|
|
121
|
+
|| !APP_TOKEN_REF_PATTERN.test(appTokenRef)) return null;
|
|
122
|
+
const derived = deriveSlackBotIdentity(platformId);
|
|
123
|
+
if (derived.botId !== botId
|
|
124
|
+
|| derived.botTokenRef !== botTokenRef
|
|
125
|
+
|| derived.appTokenRef !== appTokenRef) return null;
|
|
126
|
+
return Object.freeze({
|
|
127
|
+
botId,
|
|
128
|
+
platformId,
|
|
129
|
+
botTokenRef,
|
|
130
|
+
appTokenRef,
|
|
131
|
+
name,
|
|
132
|
+
username: cleanString(value.username),
|
|
133
|
+
teamId: cleanString(value.teamId),
|
|
134
|
+
teamName: cleanString(value.teamName),
|
|
135
|
+
consumerMode: value.consumerMode === 'external-consumer' ? 'external-consumer' : 'standalone-session',
|
|
136
|
+
createdAt: cleanString(value.createdAt) ?? new Date().toISOString(),
|
|
137
|
+
connectedAt: cleanString(value.connectedAt),
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
#normalizeDocument(value) {
|
|
142
|
+
if (!value || value.version !== 1 || !Array.isArray(value.bots)) return null;
|
|
143
|
+
const bots = value.bots.map((bot) => this.#normalizeBot(bot));
|
|
144
|
+
if (bots.some((bot) => bot === null)) return null;
|
|
145
|
+
const ids = new Set();
|
|
146
|
+
const platformIds = new Set();
|
|
147
|
+
const refs = new Set();
|
|
148
|
+
for (const bot of bots) {
|
|
149
|
+
if (ids.has(bot.botId) || platformIds.has(bot.platformId)
|
|
150
|
+
|| refs.has(bot.botTokenRef) || refs.has(bot.appTokenRef)) return null;
|
|
151
|
+
ids.add(bot.botId);
|
|
152
|
+
platformIds.add(bot.platformId);
|
|
153
|
+
refs.add(bot.botTokenRef);
|
|
154
|
+
refs.add(bot.appTokenRef);
|
|
155
|
+
}
|
|
156
|
+
return Object.freeze({ version: 1, bots: Object.freeze(bots) });
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
async #mutate(mutator) {
|
|
160
|
+
let result;
|
|
161
|
+
const operation = this.#writeQueue.then(async () => {
|
|
162
|
+
const bots = [...this.#value.bots];
|
|
163
|
+
result = mutator(bots);
|
|
164
|
+
const document = Object.freeze({ version: 1, bots: Object.freeze(bots) });
|
|
165
|
+
await mkdir(dirname(this.#path), { recursive: true, mode: 0o700 });
|
|
166
|
+
const temporary = `${this.#path}.tmp`;
|
|
167
|
+
await writeFile(temporary, `${JSON.stringify(document, null, 2)}\n`, {
|
|
168
|
+
encoding: 'utf8', mode: 0o600,
|
|
169
|
+
});
|
|
170
|
+
await rename(temporary, this.#path);
|
|
171
|
+
this.#value = document;
|
|
172
|
+
});
|
|
173
|
+
this.#writeQueue = operation.then(() => undefined, () => undefined);
|
|
174
|
+
await operation;
|
|
175
|
+
return result;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
|
|
3
|
+
export function slackRefusal(code) {
|
|
4
|
+
return Object.assign(new Error(code), { code });
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function slackTimestamp(value) {
|
|
8
|
+
return typeof value === 'string' && /^\d{10,16}\.\d{6}$/.test(value);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function slackId(value, prefix) {
|
|
12
|
+
return typeof value === 'string' && new RegExp(`^[${prefix}][A-Z0-9]{4,30}$`).test(value);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function verifiedSlackAccount(identity, bot) {
|
|
16
|
+
if (!slackId(identity?.team_id, 'T') || !slackId(identity?.user_id, 'UW')
|
|
17
|
+
|| !slackId(identity?.bot_id, 'B') || bot?.id !== identity.bot_id
|
|
18
|
+
|| bot?.user_id !== identity.user_id || !slackId(bot?.app_id, 'A') || bot.deleted === true)
|
|
19
|
+
throw slackRefusal('account-unverified');
|
|
20
|
+
const account = { teamId: identity.team_id, userId: identity.user_id,
|
|
21
|
+
botId: identity.bot_id, appId: bot.app_id };
|
|
22
|
+
return Object.freeze({ ...account,
|
|
23
|
+
fingerprint: createHash('sha256').update(JSON.stringify({ provider: 'slack', ...account })).digest('hex'),
|
|
24
|
+
...(typeof bot.name === 'string' && bot.name ? { name: bot.name.slice(0, 512) } : {}),
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Only the source author or people the source message mentions may be pinged back. */
|
|
29
|
+
export function slackReplyMentions(mentionUserIds, route, source) {
|
|
30
|
+
if (mentionUserIds === undefined) return [];
|
|
31
|
+
if (!Array.isArray(mentionUserIds) || mentionUserIds.length > 20
|
|
32
|
+
|| !mentionUserIds.every(id => slackId(id, 'UW'))) throw slackRefusal('bad-request');
|
|
33
|
+
const mentioned = new Set([route.actorId,
|
|
34
|
+
...(typeof source?.text === 'string' ? [...source.text.matchAll(/<@([UW][A-Z0-9]{4,30})>/g)].map(([, id]) => id) : [])]);
|
|
35
|
+
if (!mentionUserIds.every(id => mentioned.has(id))) throw slackRefusal('bad-request');
|
|
36
|
+
return [...new Set(mentionUserIds)];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Native channel and timestamp identities stay separate from event delivery IDs. */
|
|
40
|
+
export function normalizeSlackExternalText(payload, { botId, account, sourceFiles = false, ordinaryText = false }) {
|
|
41
|
+
if (payload?.api_app_id !== account.appId || payload?.team_id !== account.teamId)
|
|
42
|
+
throw slackRefusal('account-changed');
|
|
43
|
+
const event = payload.event;
|
|
44
|
+
const mention = event?.type === 'app_mention';
|
|
45
|
+
const ordinary = ordinaryText && event?.type === 'message' && event.channel_type === 'channel';
|
|
46
|
+
const direct = event?.type === 'message' && event.channel_type === 'im' && slackId(event.channel, 'D');
|
|
47
|
+
if ((!mention && !ordinary && !direct) || event.bot_id || event.app_id
|
|
48
|
+
|| (event.subtype && !((mention || direct) && sourceFiles && event.subtype === 'file_share'))
|
|
49
|
+
|| (ordinary && Array.isArray(event.files) && event.files.length > 0)
|
|
50
|
+
|| (!sourceFiles && Array.isArray(event.files) && event.files.length > 0)
|
|
51
|
+
|| event.user === account.userId) return null;
|
|
52
|
+
if (!slackId(event.channel, direct ? 'D' : 'C') || !slackId(event.user, 'UW')
|
|
53
|
+
|| !slackTimestamp(event.ts) || (event.thread_ts !== undefined && !slackTimestamp(event.thread_ts))
|
|
54
|
+
|| typeof payload.event_id !== 'string' || !/^Ev[A-Za-z0-9]{4,126}$/.test(payload.event_id)
|
|
55
|
+
|| typeof event.text !== 'string' || !event.text.trim() || event.text.length > 16000) throw slackRefusal('invalid-inbound');
|
|
56
|
+
const normalized = normalizeText(event, { botId, account, eventId: payload.event_id, requireMention: mention, direct });
|
|
57
|
+
// app_mention is the sole own-mention delivery path when both subscriptions overlap.
|
|
58
|
+
return ordinary && normalized?.mentionedAccount ? null : normalized;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function normalizeText(event, { botId, account, eventId, requireMention, direct = false }) {
|
|
62
|
+
const mentions = [...event.text.matchAll(/<@([UW][A-Z0-9]{4,30})>/g)]
|
|
63
|
+
.map(([key, id]) => Object.freeze({ key, id,
|
|
64
|
+
...(id === account.userId && account.name ? { name: account.name } : {}) }));
|
|
65
|
+
if (mentions.length > 100) throw slackRefusal('invalid-inbound');
|
|
66
|
+
const mentionedAccount = mentions.some(mention => mention.id === account.userId);
|
|
67
|
+
if (requireMention && !mentionedAccount) return null;
|
|
68
|
+
const at = new Date(Number(event.ts) * 1000);
|
|
69
|
+
if (!Number.isFinite(at.getTime())) throw slackRefusal('invalid-inbound');
|
|
70
|
+
const threadId = event.thread_ts ?? event.ts;
|
|
71
|
+
return Object.freeze({ version: 1, channel: 'slack', botId, fingerprint: account.fingerprint,
|
|
72
|
+
eventId: eventId, messageId: event.ts,
|
|
73
|
+
actor: Object.freeze({ kind: 'user', id: event.user }),
|
|
74
|
+
conversation: Object.freeze({ kind: direct ? 'dm' : 'group', id: event.channel }),
|
|
75
|
+
mentions: Object.freeze(mentions), mentionedAccount, at: at.toISOString(), text: event.text,
|
|
76
|
+
reply: Object.freeze({ messageId: event.ts, conversationId: event.channel, actorId: event.user,
|
|
77
|
+
threadId, rootId: threadId }),
|
|
78
|
+
replay: Object.freeze({ kind: 'provider-redelivery', resumeCursor: false, gapPossible: true }),
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** A historical read is evidence, never a Socket delivery or an Inbox admission. */
|
|
83
|
+
export function normalizeSlackHistoryText(message, { botId, account, conversationId }) {
|
|
84
|
+
if (message?.type !== 'message' || message.deleted === true || message.bot_id || message.app_id || message.subtype
|
|
85
|
+
|| (Array.isArray(message.files) && message.files.length > 0)
|
|
86
|
+
|| message.user === account.userId) return null;
|
|
87
|
+
if (!slackId(message.user, 'UW') || !slackTimestamp(message.ts)
|
|
88
|
+
|| (message.thread_ts !== undefined && !slackTimestamp(message.thread_ts))
|
|
89
|
+
|| typeof message.text !== 'string' || !message.text.trim() || message.text.length > 16000)
|
|
90
|
+
return null;
|
|
91
|
+
return normalizeText({ ...message, channel: conversationId }, { botId, account,
|
|
92
|
+
eventId: `history:${conversationId}:${message.ts}`, requireMention: false, direct: conversationId.startsWith('D') });
|
|
93
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { slackRefusal } from './external-consumer.mjs';
|
|
3
|
+
|
|
4
|
+
const MAX_BYTES = 25 * 1024 * 1024;
|
|
5
|
+
|
|
6
|
+
function attachmentFor(source, route) {
|
|
7
|
+
if (!Array.isArray(source.files) || source.files.length === 0) return undefined;
|
|
8
|
+
// The checked source-file contract currently represents one file per Source Event.
|
|
9
|
+
if (source.files.length !== 1) throw slackRefusal('resource-unavailable');
|
|
10
|
+
const file = source.files[0];
|
|
11
|
+
if (!/^F[A-Z0-9]{4,30}$/.test(file?.id ?? '') || typeof file.name !== 'string'
|
|
12
|
+
|| !file.name.trim() || file.name.length > 512 || /[\x00-\x1f\x7f]/.test(file.name)
|
|
13
|
+
|| file.deleted === true || (file.mode !== undefined && file.mode !== 'hosted'))
|
|
14
|
+
throw slackRefusal('resource-unavailable');
|
|
15
|
+
if (Number.isFinite(file.size) && file.size > MAX_BYTES) throw slackRefusal('artifact-too-large');
|
|
16
|
+
return Object.freeze({
|
|
17
|
+
id: createHash('sha256').update(JSON.stringify([route.conversationId, route.messageId, file.id])).digest('hex'),
|
|
18
|
+
messageId: route.messageId, resourceKey: file.id, name: file.name,
|
|
19
|
+
...(Number.isSafeInteger(file.size) && file.size > 0 ? { sizeBytes: file.size } : {}),
|
|
20
|
+
...(typeof file.mimetype === 'string' && file.mimetype.length <= 127
|
|
21
|
+
&& /^[a-zA-Z0-9!#$&^_.+-]+\/[a-zA-Z0-9!#$&^_.+-]+$/.test(file.mimetype) ? { mediaType: file.mimetype } : {}),
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Only safe native file identity is persisted; private download URLs remain inside the Provider. */
|
|
26
|
+
export async function externalAttachments(evidence, source) {
|
|
27
|
+
const attachment = attachmentFor(await source(), evidence.reply);
|
|
28
|
+
return attachment ? Object.freeze({ ...evidence, attachments: Object.freeze([attachment]) }) : evidence;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export async function readExternalFile(api, route, attachment, { signal, assertCurrent, source }) {
|
|
32
|
+
assertCurrent();
|
|
33
|
+
const current = attachmentFor(await source(), route);
|
|
34
|
+
if (!current || !attachment || current.id !== attachment.id || current.messageId !== attachment.messageId
|
|
35
|
+
|| current.resourceKey !== attachment.resourceKey || current.name !== attachment.name
|
|
36
|
+
|| current.sizeBytes !== attachment.sizeBytes || current.mediaType !== attachment.mediaType)
|
|
37
|
+
throw slackRefusal('stale-route');
|
|
38
|
+
assertCurrent();
|
|
39
|
+
const file = await api.fileInfo({ fileId: current.resourceKey, signal });
|
|
40
|
+
assertCurrent();
|
|
41
|
+
if (file?.id !== current.resourceKey || file.deleted === true || file.mode !== 'hosted'
|
|
42
|
+
|| file.name !== current.name || !Number.isSafeInteger(file.size) || file.size < 1)
|
|
43
|
+
throw slackRefusal('resource-unavailable');
|
|
44
|
+
if (file.size > MAX_BYTES) throw slackRefusal('artifact-too-large');
|
|
45
|
+
const { stream } = await api.downloadFileStream({ url: file.url_private_download ?? file.url_private, signal });
|
|
46
|
+
assertCurrent();
|
|
47
|
+
if (!stream?.[Symbol.asyncIterator]) throw slackRefusal('resource-unavailable');
|
|
48
|
+
return (async function* () {
|
|
49
|
+
let size = 0;
|
|
50
|
+
try {
|
|
51
|
+
assertCurrent();
|
|
52
|
+
for await (const chunk of stream) {
|
|
53
|
+
assertCurrent();
|
|
54
|
+
if (!(chunk instanceof Uint8Array)) throw slackRefusal('resource-unavailable');
|
|
55
|
+
size += chunk.byteLength;
|
|
56
|
+
if (size > MAX_BYTES || size > file.size) throw slackRefusal('artifact-too-large');
|
|
57
|
+
yield new Uint8Array(chunk);
|
|
58
|
+
}
|
|
59
|
+
assertCurrent();
|
|
60
|
+
if (size !== file.size) throw slackRefusal('resource-unavailable');
|
|
61
|
+
} finally {
|
|
62
|
+
stream.destroy?.();
|
|
63
|
+
await stream.cancel?.().catch(() => undefined);
|
|
64
|
+
}
|
|
65
|
+
})();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export async function replyExternalFile(api, route, file, { signal, assertCurrent, source }) {
|
|
69
|
+
if (typeof file?.id !== 'string' || !file.id || typeof file.name !== 'string' || !file.name.trim()
|
|
70
|
+
|| file.name.length > 512 || /[\x00-\x1f\x7f]/.test(file.name) || !(file.bytes instanceof Uint8Array)
|
|
71
|
+
|| file.bytes.byteLength < 1 || file.bytes.byteLength > MAX_BYTES) throw slackRefusal('bad-request');
|
|
72
|
+
assertCurrent();
|
|
73
|
+
await source();
|
|
74
|
+
assertCurrent();
|
|
75
|
+
try {
|
|
76
|
+
await api.uploadFile({ channelId: route.conversationId, threadTs: route.threadId,
|
|
77
|
+
file: { fileName: file.name, bytes: Buffer.from(file.bytes) }, signal,
|
|
78
|
+
beforeSend: async () => {
|
|
79
|
+
await source();
|
|
80
|
+
assertCurrent();
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
return { sent: true };
|
|
84
|
+
} catch (error) {
|
|
85
|
+
if (error?.code === 'artifact-provider-failed') throw slackRefusal('file-upload-failed');
|
|
86
|
+
if (['artifact-permission-required', 'artifact-too-large', 'artifact-provider-rejected', 'artifact-rate-limited'].includes(error?.code))
|
|
87
|
+
throw slackRefusal('file-provider-rejected');
|
|
88
|
+
throw error;
|
|
89
|
+
}
|
|
90
|
+
}
|