@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,323 @@
|
|
|
1
|
+
import { createWriteStream } from 'node:fs';
|
|
2
|
+
import { mkdir, mkdtemp, readdir, realpath, rm, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { isAbsolute, join, relative, resolve } from 'node:path';
|
|
4
|
+
import { pipeline } from 'node:stream/promises';
|
|
5
|
+
|
|
6
|
+
import { t } from './i18n.mjs';
|
|
7
|
+
|
|
8
|
+
const FILES_DIRECTORY = join('.dsh-im', 'inbound');
|
|
9
|
+
const RETENTIONS = new Set(['turn', 'persistent']);
|
|
10
|
+
const DIRECTORY_TIMESTAMP_PATTERN = /^(\d{4})(\d{2})(\d{2})-(\d{2})(\d{2})(\d{2})/;
|
|
11
|
+
|
|
12
|
+
function padded(value) {
|
|
13
|
+
return String(value).padStart(2, '0');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Timestamp prefix (`yyyyMMdd-HHmmss-`) handed to mkdtemp for random suffixes. */
|
|
17
|
+
function inboundDirectoryPrefix(now = new Date()) {
|
|
18
|
+
return `${now.getFullYear()}${padded(now.getMonth() + 1)}${padded(now.getDate())}`
|
|
19
|
+
+ `-${padded(now.getHours())}${padded(now.getMinutes())}${padded(now.getSeconds())}-`;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Parse the local-time instant encoded in a staged inbound directory name,
|
|
24
|
+
* returning null for legacy `turn-` names and anything unparseable.
|
|
25
|
+
*/
|
|
26
|
+
export function parseInboundDirectoryName(name) {
|
|
27
|
+
if (typeof name !== 'string') return null;
|
|
28
|
+
const match = DIRECTORY_TIMESTAMP_PATTERN.exec(name);
|
|
29
|
+
if (!match) return null;
|
|
30
|
+
const [year, month, day, hour, minute, second] = match.slice(1).map(Number);
|
|
31
|
+
const date = new Date(year, month - 1, day, hour, minute, second);
|
|
32
|
+
if (date.getFullYear() !== year || date.getMonth() !== month - 1 || date.getDate() !== day
|
|
33
|
+
|| date.getHours() !== hour || date.getMinutes() !== minute || date.getSeconds() !== second) {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
return date;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export class InboundFileError extends Error {
|
|
40
|
+
constructor(code, message, userMessage = t('文件接收失败,请重新发送后再试。'), options = {}) {
|
|
41
|
+
super(message, options);
|
|
42
|
+
this.name = 'InboundFileError';
|
|
43
|
+
this.code = code;
|
|
44
|
+
this.userMessage = userMessage;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function fileSources(message) {
|
|
49
|
+
return Array.isArray(message?.files) ? message.files.filter(Boolean) : [];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function displayName(value, fallback) {
|
|
53
|
+
if (typeof value !== 'string') return fallback;
|
|
54
|
+
const cleaned = value
|
|
55
|
+
.replaceAll('\\', '/')
|
|
56
|
+
.split('/')
|
|
57
|
+
.at(-1)
|
|
58
|
+
?.replace(/[\u0000-\u001f\u007f]/g, '')
|
|
59
|
+
.trim();
|
|
60
|
+
return cleaned || fallback;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function storageName(value, index) {
|
|
64
|
+
const cleaned = displayName(value, 'file')
|
|
65
|
+
.replace(/[^\p{L}\p{N}._ -]/gu, '_')
|
|
66
|
+
.replace(/^\.+/, '')
|
|
67
|
+
.slice(0, 160) || 'file';
|
|
68
|
+
return `${String(index + 1).padStart(2, '0')}-${cleaned}`;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function loadedFile(value) {
|
|
72
|
+
if (Buffer.isBuffer(value) || value instanceof Uint8Array) {
|
|
73
|
+
return { data: Buffer.from(value) };
|
|
74
|
+
}
|
|
75
|
+
const raw = value?.data ?? value?.buffer;
|
|
76
|
+
if (Buffer.isBuffer(raw) || raw instanceof Uint8Array) {
|
|
77
|
+
return {
|
|
78
|
+
data: Buffer.from(raw),
|
|
79
|
+
name: value?.name ?? value?.filename,
|
|
80
|
+
mediaType: value?.mediaType ?? value?.mimetype,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
const stream = value?.stream ?? value;
|
|
84
|
+
if (stream && typeof stream[Symbol.asyncIterator] === 'function') {
|
|
85
|
+
return {
|
|
86
|
+
stream,
|
|
87
|
+
name: value?.name ?? value?.filename,
|
|
88
|
+
mediaType: value?.mediaType ?? value?.mimetype,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function hasInboundFiles(message) {
|
|
95
|
+
return fileSources(message).length > 0;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Start provider downloads immediately while preserving the lazy file-source contract. */
|
|
99
|
+
export function prefetchInboundFiles(message, { signal } = {}) {
|
|
100
|
+
const sources = fileSources(message);
|
|
101
|
+
if (sources.length === 0) return message;
|
|
102
|
+
return {
|
|
103
|
+
...message,
|
|
104
|
+
files: sources.map((source) => {
|
|
105
|
+
if (source?.data !== undefined || typeof source?.load !== 'function') return source;
|
|
106
|
+
let download;
|
|
107
|
+
try {
|
|
108
|
+
download = Promise.resolve(source.load({ signal }));
|
|
109
|
+
} catch (error) {
|
|
110
|
+
download = Promise.reject(error);
|
|
111
|
+
}
|
|
112
|
+
download.catch(() => undefined);
|
|
113
|
+
return {
|
|
114
|
+
...source,
|
|
115
|
+
async load({ signal: loadSignal } = {}) {
|
|
116
|
+
loadSignal?.throwIfAborted();
|
|
117
|
+
const result = await download;
|
|
118
|
+
loadSignal?.throwIfAborted();
|
|
119
|
+
return result;
|
|
120
|
+
},
|
|
121
|
+
};
|
|
122
|
+
}),
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export async function stageInboundFiles(message, {
|
|
127
|
+
workspace,
|
|
128
|
+
signal,
|
|
129
|
+
retention = 'turn',
|
|
130
|
+
onStagedDirectory = null,
|
|
131
|
+
} = {}) {
|
|
132
|
+
if (!RETENTIONS.has(retention)) {
|
|
133
|
+
throw new InboundFileError(
|
|
134
|
+
'inbound-file-retention-invalid',
|
|
135
|
+
`Unknown inbound file retention: ${String(retention)}`,
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
if (onStagedDirectory !== null && typeof onStagedDirectory !== 'function') {
|
|
139
|
+
throw new TypeError('onStagedDirectory must be a function');
|
|
140
|
+
}
|
|
141
|
+
const sources = fileSources(message);
|
|
142
|
+
if (sources.length === 0) return null;
|
|
143
|
+
if (typeof workspace !== 'string' || !isAbsolute(workspace)) {
|
|
144
|
+
throw new InboundFileError(
|
|
145
|
+
'inbound-file-workspace-unavailable',
|
|
146
|
+
'The Harness Session workspace is unavailable for inbound files.',
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
signal?.throwIfAborted();
|
|
151
|
+
const root = resolve(workspace, FILES_DIRECTORY);
|
|
152
|
+
await mkdir(root, { recursive: true, mode: 0o700 });
|
|
153
|
+
const directory = await mkdtemp(join(root, inboundDirectoryPrefix()));
|
|
154
|
+
// Register the batch before any file is written so a concurrent TTL=0
|
|
155
|
+
// sweep can never observe the directory as untracked mid-staging.
|
|
156
|
+
onStagedDirectory?.(directory);
|
|
157
|
+
const files = [];
|
|
158
|
+
|
|
159
|
+
try {
|
|
160
|
+
for (const [index, source] of sources.entries()) {
|
|
161
|
+
signal?.throwIfAborted();
|
|
162
|
+
let value;
|
|
163
|
+
try {
|
|
164
|
+
value = source?.data === undefined
|
|
165
|
+
? await source?.load?.({ signal })
|
|
166
|
+
: source.data;
|
|
167
|
+
} catch (error) {
|
|
168
|
+
if (signal?.aborted) throw error;
|
|
169
|
+
throw new InboundFileError(
|
|
170
|
+
'inbound-file-download-failed',
|
|
171
|
+
`Unable to download inbound file ${index + 1}: ${error?.message ?? String(error)}`,
|
|
172
|
+
t('文件下载失败,请重新发送后再试。'),
|
|
173
|
+
{ cause: error },
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const loaded = loadedFile(value);
|
|
178
|
+
if (!loaded) {
|
|
179
|
+
throw new InboundFileError(
|
|
180
|
+
'inbound-file-data-invalid',
|
|
181
|
+
`Inbound file ${index + 1} returned no readable data.`,
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
const name = displayName(loaded.name ?? source?.name, `file-${index + 1}`);
|
|
185
|
+
const path = join(directory, storageName(name, index));
|
|
186
|
+
if (loaded.data) {
|
|
187
|
+
await writeFile(path, loaded.data, { mode: 0o600, signal });
|
|
188
|
+
} else {
|
|
189
|
+
try {
|
|
190
|
+
await pipeline(
|
|
191
|
+
loaded.stream,
|
|
192
|
+
createWriteStream(path, { flags: 'wx', mode: 0o600 }),
|
|
193
|
+
{ signal },
|
|
194
|
+
);
|
|
195
|
+
} catch (error) {
|
|
196
|
+
if (signal?.aborted) throw error;
|
|
197
|
+
throw new InboundFileError(
|
|
198
|
+
'inbound-file-download-failed',
|
|
199
|
+
`Unable to stream inbound file ${index + 1}: ${error?.message ?? String(error)}`,
|
|
200
|
+
t('文件下载失败,请重新发送后再试。'),
|
|
201
|
+
{ cause: error },
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
const relativePath = relative(resolve(workspace), path);
|
|
206
|
+
if (!relativePath || relativePath.startsWith('..') || isAbsolute(relativePath)) {
|
|
207
|
+
throw new InboundFileError(
|
|
208
|
+
'inbound-file-path-invalid',
|
|
209
|
+
'The staged inbound file escaped the Harness Session workspace.',
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
files.push(Object.freeze({
|
|
213
|
+
name,
|
|
214
|
+
path: relativePath,
|
|
215
|
+
...(typeof (loaded.mediaType ?? source?.mediaType) === 'string'
|
|
216
|
+
&& (loaded.mediaType ?? source.mediaType).trim()
|
|
217
|
+
? { mediaType: (loaded.mediaType ?? source.mediaType).trim() }
|
|
218
|
+
: {}),
|
|
219
|
+
}));
|
|
220
|
+
}
|
|
221
|
+
return Object.freeze({
|
|
222
|
+
files: Object.freeze(files),
|
|
223
|
+
directory,
|
|
224
|
+
retention,
|
|
225
|
+
async cleanup() {
|
|
226
|
+
// Persistent retention leaves the directory to the global TTL sweeper.
|
|
227
|
+
if (retention !== 'turn') return;
|
|
228
|
+
await rm(directory, { recursive: true, force: true });
|
|
229
|
+
},
|
|
230
|
+
});
|
|
231
|
+
} catch (error) {
|
|
232
|
+
await rm(directory, { recursive: true, force: true }).catch(() => undefined);
|
|
233
|
+
throw error;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Delete expired staged inbound directories under one workspace. Only
|
|
239
|
+
* directories whose names parse as `yyyyMMdd-HHmmss` timestamps are managed;
|
|
240
|
+
* legacy `turn-` names and anything unparseable stay untouched. Directories
|
|
241
|
+
* reported as tracked by an in-flight turn are always skipped.
|
|
242
|
+
*/
|
|
243
|
+
export async function sweepInboundAttachments(workspace, ttlHours, {
|
|
244
|
+
now = () => new Date(),
|
|
245
|
+
isTracked,
|
|
246
|
+
} = {}) {
|
|
247
|
+
if (typeof workspace !== 'string' || !workspace) {
|
|
248
|
+
throw new TypeError('sweepInboundAttachments requires a workspace');
|
|
249
|
+
}
|
|
250
|
+
// An unparsable TTL must never widen into unconditional deletion.
|
|
251
|
+
if (ttlHours !== 0 && !(Number.isInteger(ttlHours) && ttlHours > 0)) {
|
|
252
|
+
return { deleted: 0 };
|
|
253
|
+
}
|
|
254
|
+
const root = resolve(workspace, FILES_DIRECTORY);
|
|
255
|
+
let canonicalWorkspace;
|
|
256
|
+
let canonicalRoot;
|
|
257
|
+
try {
|
|
258
|
+
[canonicalWorkspace, canonicalRoot] = await Promise.all([
|
|
259
|
+
realpath(workspace),
|
|
260
|
+
realpath(root),
|
|
261
|
+
]);
|
|
262
|
+
} catch (error) {
|
|
263
|
+
if (error?.code === 'ENOENT') return { deleted: 0 };
|
|
264
|
+
throw error;
|
|
265
|
+
}
|
|
266
|
+
const rootInWorkspace = relative(canonicalWorkspace, canonicalRoot);
|
|
267
|
+
if (!rootInWorkspace || rootInWorkspace.startsWith('..') || isAbsolute(rootInWorkspace)) {
|
|
268
|
+
throw new InboundFileError(
|
|
269
|
+
'inbound-file-root-outside-workspace',
|
|
270
|
+
'The inbound attachment directory does not resolve inside the Harness Session workspace.',
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
let entries;
|
|
274
|
+
try {
|
|
275
|
+
entries = await readdir(canonicalRoot, { withFileTypes: true });
|
|
276
|
+
} catch (error) {
|
|
277
|
+
if (error?.code === 'ENOENT') return { deleted: 0 };
|
|
278
|
+
throw error;
|
|
279
|
+
}
|
|
280
|
+
const current = now();
|
|
281
|
+
let deleted = 0;
|
|
282
|
+
for (const entry of entries) {
|
|
283
|
+
if (!entry.isDirectory()) continue;
|
|
284
|
+
const stagedAt = parseInboundDirectoryName(entry.name);
|
|
285
|
+
if (stagedAt === null) continue;
|
|
286
|
+
const directory = join(root, entry.name);
|
|
287
|
+
const canonicalDirectory = join(canonicalRoot, entry.name);
|
|
288
|
+
if (typeof isTracked === 'function'
|
|
289
|
+
&& (isTracked(directory) || (directory !== canonicalDirectory && isTracked(canonicalDirectory)))) {
|
|
290
|
+
continue;
|
|
291
|
+
}
|
|
292
|
+
const ageHours = (current - stagedAt) / 3_600_000;
|
|
293
|
+
if (ttlHours === 0 || ageHours >= ttlHours) {
|
|
294
|
+
try {
|
|
295
|
+
await rm(canonicalDirectory, { recursive: true, force: true });
|
|
296
|
+
deleted += 1;
|
|
297
|
+
} catch {
|
|
298
|
+
// One undeletable directory must not abort the remaining sweep.
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
return { deleted };
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
export function appendInboundFilesToPrompt(prompt, staged) {
|
|
306
|
+
if (!staged?.files?.length) return prompt;
|
|
307
|
+
const manifest = [
|
|
308
|
+
'<dsh_im_files>',
|
|
309
|
+
JSON.stringify({
|
|
310
|
+
description: 'Files uploaded with this user message. Paths are relative to the current Harness workspace.',
|
|
311
|
+
files: staged.files,
|
|
312
|
+
}),
|
|
313
|
+
'</dsh_im_files>',
|
|
314
|
+
].join('\n');
|
|
315
|
+
|
|
316
|
+
if (Array.isArray(prompt)) return [...prompt, { type: 'text', text: manifest }];
|
|
317
|
+
const text = typeof prompt === 'string' ? prompt.trim() : '';
|
|
318
|
+
return text ? `${text}\n\n${manifest}` : manifest;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
export function inboundFileUserMessage(error) {
|
|
322
|
+
return error instanceof InboundFileError ? error.userMessage : null;
|
|
323
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { mkdir, readdir, readFile, rename, unlink, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { basename, dirname, join } from 'node:path';
|
|
4
|
+
|
|
5
|
+
import { DEFAULT_INBOUND_TTL_HOURS, normalizeInboundTtlHours } from './inbound-ttl.mjs';
|
|
6
|
+
|
|
7
|
+
const DOCUMENT_VERSION = 1;
|
|
8
|
+
// Fail-safe TTL for documents that exist but cannot be trusted (damage or an
|
|
9
|
+
// unknown future version, e.g. after a rollback). TTL 0 would sweep every
|
|
10
|
+
// untracked directory on the next pass, so keep-forever is the safe fallback.
|
|
11
|
+
const UNREADABLE_DOCUMENT_TTL_HOURS = -1;
|
|
12
|
+
|
|
13
|
+
function invalidTtlError() {
|
|
14
|
+
const error = new Error('Invalid inbound attachment TTL hours.');
|
|
15
|
+
error.code = 'inbound-ttl-invalid';
|
|
16
|
+
return error;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// Mirrors the atomic settings writes of the bot workspace store and the
|
|
20
|
+
// update service: create a private temporary file, then rename it into place.
|
|
21
|
+
async function writeSettingsDocument(path, document) {
|
|
22
|
+
await mkdir(dirname(path), { recursive: true, mode: 0o700 });
|
|
23
|
+
const temporary = `${path}.${randomUUID()}.tmp`;
|
|
24
|
+
try {
|
|
25
|
+
await writeFile(temporary, `${JSON.stringify(document, null, 2)}\n`, {
|
|
26
|
+
encoding: 'utf8',
|
|
27
|
+
mode: 0o600,
|
|
28
|
+
flag: 'wx',
|
|
29
|
+
});
|
|
30
|
+
await rename(temporary, path);
|
|
31
|
+
} finally {
|
|
32
|
+
await unlink(temporary).catch((error) => {
|
|
33
|
+
if (error.code !== 'ENOENT') throw error;
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Durable host-side settings for the global inbound attachment TTL. */
|
|
39
|
+
export class InboundTtlStore {
|
|
40
|
+
#path;
|
|
41
|
+
#ttlHours = DEFAULT_INBOUND_TTL_HOURS;
|
|
42
|
+
|
|
43
|
+
constructor(path) {
|
|
44
|
+
if (typeof path !== 'string' || !path) throw new TypeError('inbound TTL store path is required');
|
|
45
|
+
this.#path = path;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* A missing file means first run (default TTL). A damaged or future
|
|
50
|
+
* document falls back to keep-forever so an unreadable intent can never
|
|
51
|
+
* widen into deletion; the value stays visible in the settings UI.
|
|
52
|
+
*/
|
|
53
|
+
async load() {
|
|
54
|
+
let raw;
|
|
55
|
+
try {
|
|
56
|
+
raw = await readFile(this.#path, 'utf8');
|
|
57
|
+
} catch (error) {
|
|
58
|
+
if (error?.code !== 'ENOENT') throw error;
|
|
59
|
+
this.#ttlHours = DEFAULT_INBOUND_TTL_HOURS;
|
|
60
|
+
await this.#removeStaleTemporaries();
|
|
61
|
+
return this;
|
|
62
|
+
}
|
|
63
|
+
this.#ttlHours = this.#readTtlHours(raw) ?? UNREADABLE_DOCUMENT_TTL_HOURS;
|
|
64
|
+
await this.#removeStaleTemporaries();
|
|
65
|
+
return this;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Crash leftovers from an interrupted atomic write are unreferenced by
|
|
69
|
+
// anyone; remove them so the settings directory stays clean.
|
|
70
|
+
async #removeStaleTemporaries() {
|
|
71
|
+
const directory = dirname(this.#path);
|
|
72
|
+
const prefix = `${basename(this.#path)}.`;
|
|
73
|
+
try {
|
|
74
|
+
const entries = await readdir(directory);
|
|
75
|
+
await Promise.all(entries
|
|
76
|
+
.filter((name) => name.startsWith(prefix) && name.endsWith('.tmp'))
|
|
77
|
+
.map((name) => unlink(join(directory, name)).catch(() => {})));
|
|
78
|
+
} catch {
|
|
79
|
+
// A missing directory or concurrent removal is fine; cleanup is best-effort.
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
#readTtlHours(raw) {
|
|
84
|
+
let document;
|
|
85
|
+
try {
|
|
86
|
+
document = JSON.parse(raw);
|
|
87
|
+
} catch {
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
if (!document || typeof document !== 'object' || Array.isArray(document)) return null;
|
|
91
|
+
if (document.version !== DOCUMENT_VERSION) return null;
|
|
92
|
+
if (document.inboundAttachmentTtlHours === undefined) return DEFAULT_INBOUND_TTL_HOURS;
|
|
93
|
+
return normalizeInboundTtlHours(document.inboundAttachmentTtlHours);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
getTtlHours() {
|
|
97
|
+
return this.#ttlHours;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async setTtlHours(value) {
|
|
101
|
+
const ttlHours = normalizeInboundTtlHours(value);
|
|
102
|
+
if (ttlHours === null) throw invalidTtlError();
|
|
103
|
+
await writeSettingsDocument(this.#path, {
|
|
104
|
+
version: DOCUMENT_VERSION,
|
|
105
|
+
inboundAttachmentTtlHours: ttlHours,
|
|
106
|
+
});
|
|
107
|
+
this.#ttlHours = ttlHours;
|
|
108
|
+
return ttlHours;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export const DEFAULT_INBOUND_TTL_HOURS = 168;
|
|
2
|
+
export const INBOUND_TTL_MAX_HOURS = 8760;
|
|
3
|
+
|
|
4
|
+
const INTEGER_STRING = /^[+-]?\d+$/;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Normalize a global inbound attachment TTL. Valid values are -1 (keep
|
|
8
|
+
* forever), 0 (delete once the owning turn ends), and whole hours 1..8760.
|
|
9
|
+
* Integer strings are tolerated; every other input returns null.
|
|
10
|
+
*/
|
|
11
|
+
export function normalizeInboundTtlHours(value) {
|
|
12
|
+
let candidate = value;
|
|
13
|
+
if (typeof candidate === 'string') {
|
|
14
|
+
const trimmed = candidate.trim();
|
|
15
|
+
if (!INTEGER_STRING.test(trimmed)) return null;
|
|
16
|
+
candidate = Number(trimmed);
|
|
17
|
+
}
|
|
18
|
+
if (typeof candidate !== 'number' || !Number.isInteger(candidate)) return null;
|
|
19
|
+
if (candidate === -1) return -1;
|
|
20
|
+
if (candidate >= 0 && candidate <= INBOUND_TTL_MAX_HOURS) return candidate;
|
|
21
|
+
return null;
|
|
22
|
+
}
|