@botharness/im-provider 4.32.0-botharness.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/PROVENANCE.json +25 -0
- package/README.en.md +445 -0
- package/README.md +448 -0
- package/THIRD_PARTY_NOTICES.md +93 -0
- package/assets/dingtalk-menu-template.json +6 -0
- package/assets/logo-dsh-im-chinese-readme-3x2.png +0 -0
- package/assets/logo-dsh-im-connecting-readme-3x2.png +0 -0
- package/assets/logo-dsh-im-connecting-square-v4.png +0 -0
- package/assets/logo-icon.png +0 -0
- package/assets/logo-plugin-message-link-gradient.webp +0 -0
- package/assets/logo-plugin-phone.png +0 -0
- package/assets/logo.png +0 -0
- package/assets/logo_cn.png +0 -0
- package/assets/screenshot-menu-card.png +0 -0
- package/cordis.patch.yml +3 -0
- package/lib/client.js +20330 -0
- package/lib/index.js +602 -0
- package/locale/en.json +5 -0
- package/locale/zh.json +5 -0
- package/package.json +181 -0
- package/plugin-src/client/access-policy-settings.js +362 -0
- package/plugin-src/client/agent-preset.js +119 -0
- package/plugin-src/client/bot-alias.js +169 -0
- package/plugin-src/client/build.mjs +38 -0
- package/plugin-src/client/channel-card-meta.js +132 -0
- package/plugin-src/client/channel-logos.js +190 -0
- package/plugin-src/client/channels/dingtalk/api.js +267 -0
- package/plugin-src/client/channels/dingtalk/index.js +954 -0
- package/plugin-src/client/channels/dingtalk/styles.js +136 -0
- package/plugin-src/client/channels/discord/api.js +11 -0
- package/plugin-src/client/channels/discord/index.js +25 -0
- package/plugin-src/client/channels/discord/styles.js +19 -0
- package/plugin-src/client/channels/email/api.js +52 -0
- package/plugin-src/client/channels/email/index.js +728 -0
- package/plugin-src/client/channels/email/styles.js +51 -0
- package/plugin-src/client/channels/feishu/api.js +405 -0
- package/plugin-src/client/channels/feishu/group-settings.js +581 -0
- package/plugin-src/client/channels/feishu/index.js +1583 -0
- package/plugin-src/client/channels/feishu/slash-panel-settings.js +322 -0
- package/plugin-src/client/channels/feishu/styles.js +573 -0
- package/plugin-src/client/channels/feishu/voice-editor.js +115 -0
- package/plugin-src/client/channels/feishu/voice-settings.js +71 -0
- package/plugin-src/client/channels/imessage/api.js +12 -0
- package/plugin-src/client/channels/imessage/index.js +46 -0
- package/plugin-src/client/channels/imessage/styles.js +19 -0
- package/plugin-src/client/channels/matrix/api.js +11 -0
- package/plugin-src/client/channels/matrix/index.js +151 -0
- package/plugin-src/client/channels/matrix/styles.js +36 -0
- package/plugin-src/client/channels/office/api.js +46 -0
- package/plugin-src/client/channels/office/index.js +153 -0
- package/plugin-src/client/channels/office/styles.js +50 -0
- package/plugin-src/client/channels/qq/api.js +171 -0
- package/plugin-src/client/channels/qq/index.js +593 -0
- package/plugin-src/client/channels/qq/styles.js +19 -0
- package/plugin-src/client/channels/shared/collapsible-account.js +103 -0
- package/plugin-src/client/channels/shared/token-api.js +124 -0
- package/plugin-src/client/channels/shared/token-channel.js +495 -0
- package/plugin-src/client/channels/slack/api.js +11 -0
- package/plugin-src/client/channels/slack/index.js +131 -0
- package/plugin-src/client/channels/slack/styles.js +34 -0
- package/plugin-src/client/channels/telegram/api.js +11 -0
- package/plugin-src/client/channels/telegram/index.js +28 -0
- package/plugin-src/client/channels/telegram/styles.js +31 -0
- package/plugin-src/client/channels/telegram/thinking-traces.js +25 -0
- package/plugin-src/client/channels/wecom/api.js +160 -0
- package/plugin-src/client/channels/wecom/index.js +625 -0
- package/plugin-src/client/channels/wecom/styles.js +19 -0
- package/plugin-src/client/channels/wecom-app/api.js +132 -0
- package/plugin-src/client/channels/wecom-app/index.js +634 -0
- package/plugin-src/client/channels/wecom-app/styles.js +25 -0
- package/plugin-src/client/channels/weixin/api.js +211 -0
- package/plugin-src/client/channels/weixin/connection-error.js +1 -0
- package/plugin-src/client/channels/weixin/index.js +801 -0
- package/plugin-src/client/channels/weixin/styles.js +115 -0
- package/plugin-src/client/channels/whatsapp/api.js +152 -0
- package/plugin-src/client/channels/whatsapp/index.js +609 -0
- package/plugin-src/client/channels/whatsapp/styles.js +19 -0
- package/plugin-src/client/connection-error.js +87 -0
- package/plugin-src/client/context-enhancement.js +415 -0
- package/plugin-src/client/credential-binding.js +118 -0
- package/plugin-src/client/delivery-settings.js +905 -0
- package/plugin-src/client/global-settings.js +433 -0
- package/plugin-src/client/i18n.js +1314 -0
- package/plugin-src/client/index.js +624 -0
- package/plugin-src/client/interface-language.js +89 -0
- package/plugin-src/client/last-message-error.js +18 -0
- package/plugin-src/client/lifecycle.js +86 -0
- package/plugin-src/client/loopback-recovery.js +75 -0
- package/plugin-src/client/model-setting.js +172 -0
- package/plugin-src/client/panel-error-boundary.js +21 -0
- package/plugin-src/client/plugin-page.js +51 -0
- package/plugin-src/client/session-channel-logos.js +243 -0
- package/plugin-src/client/styles.js +770 -0
- package/plugin-src/client/update-panel.js +14 -0
- package/plugin-src/client/workspace-directory-picker.js +280 -0
- package/plugin-src/client/workspace-editor.js +69 -0
- package/plugin-src/client/workspace-snapshot-fence.js +28 -0
- package/plugin-src/host/build.mjs +52 -0
- package/plugin-src/host/channels/dingtalk/connection-supervisor.mjs +130 -0
- package/plugin-src/host/channels/dingtalk/index.mjs +35 -0
- package/plugin-src/host/channels/dingtalk/production.mjs +193 -0
- package/plugin-src/host/channels/dingtalk/rpc.mjs +359 -0
- package/plugin-src/host/channels/discord/index.mjs +33 -0
- package/plugin-src/host/channels/discord/production.mjs +17 -0
- package/plugin-src/host/channels/discord/rpc.mjs +21 -0
- package/plugin-src/host/channels/email/availability.mjs +60 -0
- package/plugin-src/host/channels/email/index.mjs +36 -0
- package/plugin-src/host/channels/email/production.mjs +58 -0
- package/plugin-src/host/channels/email/rpc.mjs +163 -0
- package/plugin-src/host/channels/feishu/connection-supervisor.mjs +167 -0
- package/plugin-src/host/channels/feishu/controller.mjs +172 -0
- package/plugin-src/host/channels/feishu/credential-store.mjs +84 -0
- package/plugin-src/host/channels/feishu/index.mjs +69 -0
- package/plugin-src/host/channels/feishu/production.mjs +318 -0
- package/plugin-src/host/channels/feishu/rpc.mjs +863 -0
- package/plugin-src/host/channels/imessage/index.mjs +21 -0
- package/plugin-src/host/channels/imessage/production.mjs +13 -0
- package/plugin-src/host/channels/imessage/rpc.mjs +78 -0
- package/plugin-src/host/channels/matrix/index.mjs +31 -0
- package/plugin-src/host/channels/matrix/production.mjs +241 -0
- package/plugin-src/host/channels/matrix/rpc.mjs +228 -0
- package/plugin-src/host/channels/office/index.mjs +14 -0
- package/plugin-src/host/channels/office/production.mjs +45 -0
- package/plugin-src/host/channels/office/rpc.mjs +51 -0
- package/plugin-src/host/channels/qq/connection-supervisor.mjs +124 -0
- package/plugin-src/host/channels/qq/index.mjs +28 -0
- package/plugin-src/host/channels/qq/production.mjs +181 -0
- package/plugin-src/host/channels/qq/rpc.mjs +250 -0
- package/plugin-src/host/channels/shared/access-policy-production.mjs +137 -0
- package/plugin-src/host/channels/shared/access-policy-rpc.mjs +17 -0
- package/plugin-src/host/channels/shared/agent-preset-rpc.mjs +25 -0
- package/plugin-src/host/channels/shared/bot-alias-rpc.mjs +15 -0
- package/plugin-src/host/channels/shared/connection-supervisor.mjs +133 -0
- package/plugin-src/host/channels/shared/context-enhancement-rpc.mjs +17 -0
- package/plugin-src/host/channels/shared/model-setting-rpc.mjs +18 -0
- package/plugin-src/host/channels/shared/production.mjs +237 -0
- package/plugin-src/host/channels/shared/rpc.mjs +226 -0
- package/plugin-src/host/channels/shared/startup-error.mjs +55 -0
- package/plugin-src/host/channels/shared/startup.mjs +76 -0
- package/plugin-src/host/channels/shared/thinking-traces-rpc.mjs +11 -0
- package/plugin-src/host/channels/shared/workspace-rpc.mjs +31 -0
- package/plugin-src/host/channels/slack/index.mjs +31 -0
- package/plugin-src/host/channels/slack/production.mjs +176 -0
- package/plugin-src/host/channels/slack/rpc.mjs +221 -0
- package/plugin-src/host/channels/telegram/index.mjs +33 -0
- package/plugin-src/host/channels/telegram/production.mjs +19 -0
- package/plugin-src/host/channels/telegram/rpc.mjs +22 -0
- package/plugin-src/host/channels/wecom/connection-supervisor.mjs +124 -0
- package/plugin-src/host/channels/wecom/index.mjs +34 -0
- package/plugin-src/host/channels/wecom/production.mjs +187 -0
- package/plugin-src/host/channels/wecom/rpc.mjs +255 -0
- package/plugin-src/host/channels/wecom-app/index.mjs +33 -0
- package/plugin-src/host/channels/wecom-app/production.mjs +221 -0
- package/plugin-src/host/channels/wecom-app/rpc.mjs +235 -0
- package/plugin-src/host/channels/weixin/connection-supervisor.mjs +139 -0
- package/plugin-src/host/channels/weixin/index.mjs +44 -0
- package/plugin-src/host/channels/weixin/production.mjs +257 -0
- package/plugin-src/host/channels/weixin/rpc.mjs +285 -0
- package/plugin-src/host/channels/whatsapp/index.mjs +33 -0
- package/plugin-src/host/channels/whatsapp/production.mjs +198 -0
- package/plugin-src/host/channels/whatsapp/rpc.mjs +242 -0
- package/plugin-src/host/delivery-adapter.mjs +388 -0
- package/plugin-src/host/delivery-http.mjs +132 -0
- package/plugin-src/host/delivery-rpc.mjs +173 -0
- package/plugin-src/host/delivery-service.mjs +552 -0
- package/plugin-src/host/delivery-suggestions.mjs +198 -0
- package/plugin-src/host/harness-command-executor.mjs +48 -0
- package/plugin-src/host/harness-connection.mjs +20 -0
- package/plugin-src/host/harness-session-coordinator.mjs +183 -0
- package/plugin-src/host/host-language-rpc.mjs +71 -0
- package/plugin-src/host/host-language.mjs +157 -0
- package/plugin-src/host/image-input-rpc.mjs +24 -0
- package/plugin-src/host/inbound-ttl-rpc.mjs +86 -0
- package/plugin-src/host/inbound-ttl-runtime.mjs +85 -0
- package/plugin-src/host/inbound-ttl-service.mjs +179 -0
- package/plugin-src/host/index.mjs +224 -0
- package/plugin-src/host/injected-context.mjs +104 -0
- package/plugin-src/host/lark-sdk-handshake-patch.mjs +181 -0
- package/plugin-src/host/modern-harness-api.mjs +738 -0
- package/plugin-src/host/rpc-authority.mjs +12 -0
- package/plugin-src/host/session-sync-coordinator.mjs +233 -0
- package/plugin-src/host/session-title-prefix.mjs +122 -0
- package/plugin-src/host/update-policy.mjs +31 -0
- package/plugin-src/host/update-rpc.mjs +16 -0
- package/plugin-src/host/update-runtime.mjs +339 -0
- package/plugin-src/host/update-service.mjs +392 -0
- package/plugin-src/management-rpc.mjs +87 -0
- package/src/channels/dingtalk/config-store.mjs +284 -0
- package/src/channels/dingtalk/connection-error.mjs +292 -0
- package/src/channels/dingtalk/device-auth.mjs +240 -0
- package/src/channels/dingtalk/dingtalk-api.mjs +1140 -0
- package/src/channels/dingtalk/dingtalk-bridge.mjs +1889 -0
- package/src/channels/dingtalk/dingtalk-card-stream.mjs +242 -0
- package/src/channels/dingtalk/dingtalk-controller.mjs +852 -0
- package/src/channels/dingtalk/dingtalk-menu.mjs +95 -0
- package/src/channels/dingtalk/dingtalk-runtime.mjs +482 -0
- package/src/channels/dingtalk/harness-client.mjs +19 -0
- package/src/channels/dingtalk/state-store.mjs +325 -0
- package/src/channels/discord/config-store.mjs +25 -0
- package/src/channels/discord/discord-api.mjs +347 -0
- package/src/channels/discord/discord-bridge.mjs +16 -0
- package/src/channels/discord/discord-controller.mjs +16 -0
- package/src/channels/discord/discord-runtime.mjs +974 -0
- package/src/channels/discord/harness-client.mjs +11 -0
- package/src/channels/discord/state-store.mjs +3 -0
- package/src/channels/email/config-store.mjs +194 -0
- package/src/channels/email/email-api.mjs +20 -0
- package/src/channels/email/email-bridge.mjs +17 -0
- package/src/channels/email/email-controller.mjs +824 -0
- package/src/channels/email/email-runtime.mjs +699 -0
- package/src/channels/email/harness-client.mjs +7 -0
- package/src/channels/email/mail-format.mjs +91 -0
- package/src/channels/email/state-store.mjs +176 -0
- package/src/channels/email/transport.mjs +64 -0
- package/src/channels/email/transports/agent-mail.mjs +604 -0
- package/src/channels/email/transports/agently-cli.mjs +288 -0
- package/src/channels/email/transports/imap-smtp.mjs +165 -0
- package/src/channels/feishu/bridge.mjs +6286 -0
- package/src/channels/feishu/config.mjs +71 -0
- package/src/channels/feishu/external-consumer.mjs +77 -0
- package/src/channels/feishu/external-files.mjs +111 -0
- package/src/channels/feishu/feishu-app.mjs +54 -0
- package/src/channels/feishu/feishu-cards.mjs +1285 -0
- package/src/channels/feishu/feishu-channel.mjs +651 -0
- package/src/channels/feishu/feishu-runtime.mjs +950 -0
- package/src/channels/feishu/group-message-permission-manager.mjs +72 -0
- package/src/channels/feishu/group-response-mode.mjs +15 -0
- package/src/channels/feishu/harness-client.mjs +19 -0
- package/src/channels/feishu/history-reader.mjs +77 -0
- package/src/channels/feishu/live-cot.mjs +260 -0
- package/src/channels/feishu/message-utils.mjs +613 -0
- package/src/channels/feishu/multi-bot-controller.mjs +1588 -0
- package/src/channels/feishu/nearby-history.mjs +72 -0
- package/src/channels/feishu/plugin-config-store.mjs +216 -0
- package/src/channels/feishu/plugin-controller.mjs +254 -0
- package/src/channels/feishu/registration-manager.mjs +349 -0
- package/src/channels/feishu/repair-manager.mjs +138 -0
- package/src/channels/feishu/reply-context.mjs +22 -0
- package/src/channels/feishu/slash-command-panel.mjs +155 -0
- package/src/channels/feishu/slash-command-registry.mjs +495 -0
- package/src/channels/feishu/state-store.mjs +214 -0
- package/src/channels/feishu/step-push-mode.mjs +25 -0
- package/src/channels/feishu/voice-config.mjs +34 -0
- package/src/channels/feishu/voice.mjs +134 -0
- package/src/channels/imessage/config-store.mjs +24 -0
- package/src/channels/imessage/controller.mjs +37 -0
- package/src/channels/imessage/harness-client.mjs +7 -0
- package/src/channels/imessage/imessage-api.mjs +194 -0
- package/src/channels/imessage/imessage-bridge.mjs +16 -0
- package/src/channels/imessage/runtime.mjs +102 -0
- package/src/channels/imessage/state-store.mjs +3 -0
- package/src/channels/matrix/matrix-api.mjs +696 -0
- package/src/channels/matrix/matrix-bridge.mjs +20 -0
- package/src/channels/matrix/matrix-config-store.mjs +368 -0
- package/src/channels/matrix/matrix-controller.mjs +404 -0
- package/src/channels/matrix/matrix-crypto-store.mjs +279 -0
- package/src/channels/matrix/matrix-crypto.mjs +1014 -0
- package/src/channels/matrix/matrix-harness-client.mjs +11 -0
- package/src/channels/matrix/matrix-normalize.mjs +357 -0
- package/src/channels/matrix/matrix-rich-text.mjs +313 -0
- package/src/channels/matrix/matrix-room-history.mjs +316 -0
- package/src/channels/matrix/matrix-runtime.mjs +1014 -0
- package/src/channels/office/config-store.mjs +107 -0
- package/src/channels/office/office-controller.mjs +178 -0
- package/src/channels/office/office-job-executor.mjs +391 -0
- package/src/channels/office/office-runtime.mjs +175 -0
- package/src/channels/office/office-transport.mjs +171 -0
- package/src/channels/office/protocol.mjs +43 -0
- package/src/channels/qq/config-store.mjs +173 -0
- package/src/channels/qq/harness-client.mjs +11 -0
- package/src/channels/qq/markdown-reply.mjs +446 -0
- package/src/channels/qq/qq-bridge.mjs +1507 -0
- package/src/channels/qq/qq-controller.mjs +514 -0
- package/src/channels/qq/qq-menu.mjs +208 -0
- package/src/channels/qq/qq-runtime.mjs +303 -0
- package/src/channels/qq/qr-auth.mjs +24 -0
- package/src/channels/qq/state-error.mjs +17 -0
- package/src/channels/qq/state-store.mjs +143 -0
- package/src/channels/shared/access-policy.mjs +210 -0
- package/src/channels/shared/agent-preset.mjs +76 -0
- package/src/channels/shared/batch-input.mjs +229 -0
- package/src/channels/shared/bot-alias.mjs +29 -0
- package/src/channels/shared/bot-workspace-store.mjs +2383 -0
- package/src/channels/shared/command-catalog.mjs +107 -0
- package/src/channels/shared/command-permission.mjs +32 -0
- package/src/channels/shared/compact-command.mjs +109 -0
- package/src/channels/shared/config-read-error.mjs +24 -0
- package/src/channels/shared/connection-error.mjs +191 -0
- package/src/channels/shared/connection-test.mjs +58 -0
- package/src/channels/shared/context-enhancement.mjs +372 -0
- package/src/channels/shared/control-command.mjs +111 -0
- package/src/channels/shared/conversation-state-store.mjs +165 -0
- package/src/channels/shared/default-workspace.mjs +31 -0
- package/src/channels/shared/deferred-delivery-coordinator.mjs +241 -0
- package/src/channels/shared/deferred-delivery.mjs +161 -0
- package/src/channels/shared/deferred-state.mjs +53 -0
- package/src/channels/shared/diagnostic-details.mjs +95 -0
- package/src/channels/shared/editable-message-stream.mjs +100 -0
- package/src/channels/shared/exclusive-inbound-consumers.mjs +71 -0
- package/src/channels/shared/file-download.mjs +64 -0
- package/src/channels/shared/harness-approval.mjs +508 -0
- package/src/channels/shared/harness-client.mjs +2143 -0
- package/src/channels/shared/harness-mux.mjs +123 -0
- package/src/channels/shared/harness-question.mjs +95 -0
- package/src/channels/shared/harness-session-binding.mjs +119 -0
- package/src/channels/shared/history-command.mjs +193 -0
- package/src/channels/shared/i18n-en/diagnostics.mjs +55 -0
- package/src/channels/shared/i18n-en/dingtalk.mjs +81 -0
- package/src/channels/shared/i18n-en/discord.mjs +10 -0
- package/src/channels/shared/i18n-en/email.mjs +25 -0
- package/src/channels/shared/i18n-en/feishu.mjs +404 -0
- package/src/channels/shared/i18n-en/image-input.mjs +18 -0
- package/src/channels/shared/i18n-en/matrix.mjs +85 -0
- package/src/channels/shared/i18n-en/office.mjs +23 -0
- package/src/channels/shared/i18n-en/qq.mjs +96 -0
- package/src/channels/shared/i18n-en/shared-a.mjs +241 -0
- package/src/channels/shared/i18n-en/shared-b.mjs +263 -0
- package/src/channels/shared/i18n-en/shared-c.mjs +203 -0
- package/src/channels/shared/i18n-en/slack.mjs +27 -0
- package/src/channels/shared/i18n-en/telegram.mjs +27 -0
- package/src/channels/shared/i18n-en/wecom-app.mjs +33 -0
- package/src/channels/shared/i18n-en/wecom.mjs +62 -0
- package/src/channels/shared/i18n-en/weixin.mjs +88 -0
- package/src/channels/shared/i18n-en/whatsapp.mjs +17 -0
- package/src/channels/shared/i18n-en.mjs +45 -0
- package/src/channels/shared/i18n.mjs +78 -0
- package/src/channels/shared/im-source-guidance.mjs +65 -0
- package/src/channels/shared/image-input-policy.mjs +35 -0
- package/src/channels/shared/image-input-settings-store.mjs +60 -0
- package/src/channels/shared/image-input.mjs +124 -0
- package/src/channels/shared/image-prompt.mjs +360 -0
- package/src/channels/shared/inbound-access.mjs +43 -0
- package/src/channels/shared/inbound-file.mjs +323 -0
- package/src/channels/shared/inbound-ttl-store.mjs +110 -0
- package/src/channels/shared/inbound-ttl.mjs +22 -0
- package/src/channels/shared/injected-context.mjs +362 -0
- package/src/channels/shared/interface-language-store.mjs +127 -0
- package/src/channels/shared/interface-language.mjs +51 -0
- package/src/channels/shared/message-failure.mjs +257 -0
- package/src/channels/shared/model-command.mjs +741 -0
- package/src/channels/shared/model-setting.mjs +145 -0
- package/src/channels/shared/preset-command.mjs +308 -0
- package/src/channels/shared/semantic/artifact-delivery.mjs +177 -0
- package/src/channels/shared/semantic/artifact.mjs +750 -0
- package/src/channels/shared/semantic/delivery.mjs +193 -0
- package/src/channels/shared/semantic/reply-reference.mjs +148 -0
- package/src/channels/shared/session-binding-lock.mjs +40 -0
- package/src/channels/shared/session-channel-labels.mjs +28 -0
- package/src/channels/shared/session-reply-recovery.mjs +104 -0
- package/src/channels/shared/session-sync-registry.mjs +22 -0
- package/src/channels/shared/session-title.mjs +74 -0
- package/src/channels/shared/status-reaction.mjs +107 -0
- package/src/channels/shared/text-harness-bridge.mjs +1623 -0
- package/src/channels/shared/token-bot-controller.mjs +417 -0
- package/src/channels/shared/token-config-store.mjs +208 -0
- package/src/channels/shared/workspace-command.mjs +532 -0
- package/src/channels/shared/workspace-session.mjs +196 -0
- package/src/channels/slack/config-store.mjs +177 -0
- package/src/channels/slack/external-consumer.mjs +80 -0
- package/src/channels/slack/external-files.mjs +90 -0
- package/src/channels/slack/harness-client.mjs +11 -0
- package/src/channels/slack/history-reader.mjs +203 -0
- package/src/channels/slack/manifest.mjs +37 -0
- package/src/channels/slack/slack-api.mjs +636 -0
- package/src/channels/slack/slack-bridge.mjs +20 -0
- package/src/channels/slack/slack-controller.mjs +468 -0
- package/src/channels/slack/slack-runtime.mjs +879 -0
- package/src/channels/slack/state-store.mjs +3 -0
- package/src/channels/telegram/config-store.mjs +89 -0
- package/src/channels/telegram/harness-client.mjs +11 -0
- package/src/channels/telegram/state-store.mjs +3 -0
- package/src/channels/telegram/telegram-api.mjs +486 -0
- package/src/channels/telegram/telegram-bridge.mjs +81 -0
- package/src/channels/telegram/telegram-controller.mjs +52 -0
- package/src/channels/telegram/telegram-http.mjs +21 -0
- package/src/channels/telegram/telegram-rich-message.mjs +149 -0
- package/src/channels/telegram/telegram-runtime.mjs +1471 -0
- package/src/channels/wecom/config-store.mjs +172 -0
- package/src/channels/wecom/harness-client.mjs +11 -0
- package/src/channels/wecom/qr-auth.mjs +95 -0
- package/src/channels/wecom/send-error.mjs +31 -0
- package/src/channels/wecom/state-store.mjs +107 -0
- package/src/channels/wecom/wecom-bridge.mjs +1833 -0
- package/src/channels/wecom/wecom-cards.mjs +101 -0
- package/src/channels/wecom/wecom-controller.mjs +530 -0
- package/src/channels/wecom/wecom-runtime.mjs +269 -0
- package/src/channels/wecom-app/callback-server.mjs +471 -0
- package/src/channels/wecom-app/config-store.mjs +242 -0
- package/src/channels/wecom-app/harness-client.mjs +11 -0
- package/src/channels/wecom-app/state-store.mjs +107 -0
- package/src/channels/wecom-app/wecom-app-api.mjs +432 -0
- package/src/channels/wecom-app/wecom-app-bridge.mjs +1072 -0
- package/src/channels/wecom-app/wecom-app-controller.mjs +453 -0
- package/src/channels/wecom-app/wecom-app-runtime.mjs +222 -0
- package/src/channels/weixin/config-store.mjs +192 -0
- package/src/channels/weixin/connection-error.en.mjs +137 -0
- package/src/channels/weixin/connection-error.mjs +153 -0
- package/src/channels/weixin/diagnostic-details.mjs +1 -0
- package/src/channels/weixin/harness-client.mjs +19 -0
- package/src/channels/weixin/state-store.mjs +291 -0
- package/src/channels/weixin/weixin-api.mjs +949 -0
- package/src/channels/weixin/weixin-bridge.mjs +1537 -0
- package/src/channels/weixin/weixin-controller.mjs +669 -0
- package/src/channels/weixin/weixin-runtime.mjs +416 -0
- package/src/channels/whatsapp/config-store.mjs +210 -0
- package/src/channels/whatsapp/harness-client.mjs +11 -0
- package/src/channels/whatsapp/state-store.mjs +3 -0
- package/src/channels/whatsapp/whatsapp-bridge.mjs +18 -0
- package/src/channels/whatsapp/whatsapp-controller.mjs +470 -0
- package/src/channels/whatsapp/whatsapp-runtime.mjs +917 -0
- package/src/channels/whatsapp/whatsapp-web-session.mjs +214 -0
|
@@ -0,0 +1,392 @@
|
|
|
1
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
2
|
+
import { mkdir, open, readFile, rename, stat, unlink, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { isAbsolute, join } from 'node:path';
|
|
4
|
+
import semver from 'semver';
|
|
5
|
+
|
|
6
|
+
import manifest from '../../package.json' with { type: 'json' };
|
|
7
|
+
import { withSessionBindingLock } from '../../src/channels/shared/session-binding-lock.mjs';
|
|
8
|
+
import { NPM_REGISTRY, PACKAGE_NAME } from './update-runtime.mjs';
|
|
9
|
+
|
|
10
|
+
const ACTIVE_STATES = new Set(['installing', 'verifying']);
|
|
11
|
+
const JOB_STATES = new Set([...ACTIVE_STATES, 'restart-required', 'completed', 'failed', 'interrupted']);
|
|
12
|
+
const MAX_METADATA_BYTES = 256 * 1024;
|
|
13
|
+
const SNAPSHOT_FILES = ['package.json', 'pnpm-lock.yaml', 'pnpm-workspace.yaml'];
|
|
14
|
+
const NO_LOCK = Symbol('no update lock');
|
|
15
|
+
|
|
16
|
+
export function updateError(code) {
|
|
17
|
+
return Object.assign(new Error(code), { code });
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Read only the fixed npm package; neither RPC callers nor registry metadata choose a command. */
|
|
21
|
+
export async function fetchNpmRelease(fetchImpl = globalThis.fetch, timeoutMs = 10_000) {
|
|
22
|
+
let response;
|
|
23
|
+
try {
|
|
24
|
+
response = await fetchImpl(`${NPM_REGISTRY}${encodeURIComponent(PACKAGE_NAME)}/latest`, {
|
|
25
|
+
headers: { accept: 'application/json' },
|
|
26
|
+
redirect: 'error',
|
|
27
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
28
|
+
});
|
|
29
|
+
if (!response.ok) throw updateError('check-failed');
|
|
30
|
+
if (Number(response.headers.get('content-length')) > MAX_METADATA_BYTES) {
|
|
31
|
+
throw updateError('invalid-release');
|
|
32
|
+
}
|
|
33
|
+
const chunks = [];
|
|
34
|
+
let length = 0;
|
|
35
|
+
for await (const chunk of response.body) {
|
|
36
|
+
length += chunk.byteLength;
|
|
37
|
+
if (length > MAX_METADATA_BYTES) throw updateError('invalid-release');
|
|
38
|
+
chunks.push(Buffer.from(chunk));
|
|
39
|
+
}
|
|
40
|
+
const value = JSON.parse(Buffer.concat(chunks).toString('utf8'));
|
|
41
|
+
const version = value.version;
|
|
42
|
+
if (value.name !== PACKAGE_NAME || typeof version !== 'string'
|
|
43
|
+
|| semver.valid(version) !== version || semver.prerelease(version)) {
|
|
44
|
+
throw updateError('invalid-release');
|
|
45
|
+
}
|
|
46
|
+
const nodeRange = value.engines?.node;
|
|
47
|
+
if (nodeRange !== undefined && (typeof nodeRange !== 'string' || !semver.validRange(nodeRange))) {
|
|
48
|
+
throw updateError('invalid-release');
|
|
49
|
+
}
|
|
50
|
+
const tarball = new URL(value.dist?.tarball);
|
|
51
|
+
const integrity = value.dist?.integrity;
|
|
52
|
+
if (tarball.origin !== new URL(NPM_REGISTRY).origin || tarball.username || tarball.password
|
|
53
|
+
|| tarball.search || tarball.hash
|
|
54
|
+
|| tarball.pathname !== `/@xmanrui/dsh-im/-/dsh-im-${version}.tgz`
|
|
55
|
+
|| typeof integrity !== 'string' || !/^sha512-[A-Za-z0-9+/]{86}==$/.test(integrity)) {
|
|
56
|
+
throw updateError('invalid-release');
|
|
57
|
+
}
|
|
58
|
+
return { version, nodeRange: nodeRange ?? '*', integrity, tarball: tarball.href };
|
|
59
|
+
} catch (error) {
|
|
60
|
+
if (error?.code === 'invalid-release' || error instanceof SyntaxError || error instanceof TypeError && response?.ok) {
|
|
61
|
+
throw updateError('invalid-release');
|
|
62
|
+
}
|
|
63
|
+
throw updateError('check-failed');
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function pathsFor(environment) {
|
|
68
|
+
if (!isAbsolute(environment.homeDir ?? '') || !isAbsolute(environment.profileDir ?? '')) return null;
|
|
69
|
+
const key = createHash('sha256').update(environment.profileDir).digest('hex').slice(0, 24);
|
|
70
|
+
const directory = join(environment.homeDir, 'updates', 'dsh-im', key);
|
|
71
|
+
return {
|
|
72
|
+
directory,
|
|
73
|
+
state: join(directory, 'state.json'),
|
|
74
|
+
lock: join(directory, 'install.lock'),
|
|
75
|
+
backup: join(directory, 'before.json'),
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async function readJson(path, missing = null) {
|
|
80
|
+
try {
|
|
81
|
+
if ((await stat(path)).size > 10 * 1024 * 1024) throw updateError('state-unavailable');
|
|
82
|
+
return JSON.parse(await readFile(path, 'utf8'));
|
|
83
|
+
} catch (error) {
|
|
84
|
+
if (error.code === 'ENOENT') return missing;
|
|
85
|
+
throw updateError('state-unavailable');
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async function writeJson(path, value) {
|
|
90
|
+
const temporary = `${path}.${randomUUID()}.tmp`;
|
|
91
|
+
try {
|
|
92
|
+
await writeFile(temporary, `${JSON.stringify(value, null, 2)}\n`, { mode: 0o600, flag: 'wx' });
|
|
93
|
+
await rename(temporary, path);
|
|
94
|
+
} catch {
|
|
95
|
+
throw updateError('state-unavailable');
|
|
96
|
+
} finally {
|
|
97
|
+
await unlink(temporary).catch((error) => {
|
|
98
|
+
if (error.code !== 'ENOENT') throw updateError('state-unavailable');
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function processAlive(pid) {
|
|
104
|
+
if (!Number.isSafeInteger(pid) || pid <= 0) return false;
|
|
105
|
+
try {
|
|
106
|
+
process.kill(pid, 0);
|
|
107
|
+
return true;
|
|
108
|
+
} catch (error) {
|
|
109
|
+
return error.code !== 'ESRCH';
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function publicJob(job) {
|
|
114
|
+
if (!job) return null;
|
|
115
|
+
const { id, state, targetVersion, message } = job;
|
|
116
|
+
return { id, state, targetVersion, message, recoverable: job.recoverable === true };
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** One update job per profile. Persist intent before starting pnpm, and never apply a restart here. */
|
|
120
|
+
export function createUpdateService({
|
|
121
|
+
runtime,
|
|
122
|
+
runningVersion = manifest.version,
|
|
123
|
+
nodeVersion = process.versions.node,
|
|
124
|
+
fetchImpl = globalThis.fetch,
|
|
125
|
+
now = Date.now,
|
|
126
|
+
checkTimeoutMs = 10_000,
|
|
127
|
+
confirmationTtlMs = 10 * 60_000,
|
|
128
|
+
installTimeoutMs = 15 * 60_000,
|
|
129
|
+
} = {}) {
|
|
130
|
+
const queue = {};
|
|
131
|
+
let checked = null;
|
|
132
|
+
let checking = null;
|
|
133
|
+
let lastCheckAt = -Infinity;
|
|
134
|
+
let activeJob = null;
|
|
135
|
+
let activeTask = null;
|
|
136
|
+
let abortController = null;
|
|
137
|
+
let unsavedJob = null;
|
|
138
|
+
let disposed = false;
|
|
139
|
+
|
|
140
|
+
function assertActive() {
|
|
141
|
+
if (disposed) throw updateError('disposed');
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
async function readJob(environment) {
|
|
145
|
+
const paths = pathsFor(environment);
|
|
146
|
+
if (!paths) return null;
|
|
147
|
+
// A failed final write must not make this Host display the old "installing"
|
|
148
|
+
// record forever. Keep the lock and expose the outcome we actually observed.
|
|
149
|
+
if (unsavedJob?.statePath === paths.state) return unsavedJob.job;
|
|
150
|
+
let job = await readJson(paths.state);
|
|
151
|
+
const lock = await readJson(paths.lock, NO_LOCK);
|
|
152
|
+
if (!job) {
|
|
153
|
+
if (lock !== NO_LOCK) {
|
|
154
|
+
return { id: 'locked', state: 'interrupted', message: 'recovery-required', targetVersion: null };
|
|
155
|
+
}
|
|
156
|
+
return null;
|
|
157
|
+
}
|
|
158
|
+
if (!JOB_STATES.has(job.state) || typeof job.id !== 'string' || !semver.valid(job.targetVersion)) {
|
|
159
|
+
throw updateError('state-unavailable');
|
|
160
|
+
}
|
|
161
|
+
// Recovery is derived from the current installation, never a persisted flag.
|
|
162
|
+
job = { ...job, recoverable: false };
|
|
163
|
+
const historical = job.id !== activeJob?.id;
|
|
164
|
+
if (ACTIVE_STATES.has(job.state) && historical) {
|
|
165
|
+
if (lock === NO_LOCK || lock?.id !== job.id || !processAlive(lock?.pid)) {
|
|
166
|
+
job = { ...job, state: 'interrupted', message: 'recovery-required' };
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
if (historical && !ACTIVE_STATES.has(job.state) && lock !== NO_LOCK) {
|
|
170
|
+
return { ...job, state: 'interrupted', message: 'recovery-required' };
|
|
171
|
+
}
|
|
172
|
+
if (job.state === 'restart-required' || job.state === 'completed') {
|
|
173
|
+
if (environment.installedVersion !== job.targetVersion || environment.packageValid !== true) {
|
|
174
|
+
job = { ...job, state: 'interrupted', message: 'installation-changed' };
|
|
175
|
+
} else {
|
|
176
|
+
job = { ...job, state: runningVersion === job.targetVersion ? 'completed' : 'restart-required' };
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
// Reconcile after deriving the job state so an external upgrade or rollback
|
|
180
|
+
// can also recover. Keep history read-only and do not claim its target ran.
|
|
181
|
+
if (historical && lock === NO_LOCK && ['failed', 'interrupted'].includes(job.state)
|
|
182
|
+
&& environment.packageValid === true && environment.eligible === true
|
|
183
|
+
&& environment.installedVersion === runningVersion && !environment.blockedReason) {
|
|
184
|
+
return job.targetVersion === runningVersion
|
|
185
|
+
? { ...job, state: 'completed', message: 'recovered' }
|
|
186
|
+
: { ...job, recoverable: true };
|
|
187
|
+
}
|
|
188
|
+
return job;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function snapshot(environment, job) {
|
|
192
|
+
let blockedReason = environment.blockedReason ?? checked?.blockedReason ?? null;
|
|
193
|
+
if (job?.state === 'interrupted' && !job.recoverable
|
|
194
|
+
|| job?.state === 'failed' && environment.installedVersion !== runningVersion) {
|
|
195
|
+
blockedReason = 'recovery-required';
|
|
196
|
+
} else if (job?.state === 'restart-required' || environment.installedVersion && environment.installedVersion !== runningVersion) {
|
|
197
|
+
blockedReason = 'pending-restart';
|
|
198
|
+
}
|
|
199
|
+
const busy = ACTIVE_STATES.has(job?.state);
|
|
200
|
+
const canInstall = Boolean(environment.eligible && !blockedReason && !busy && checked?.checkId
|
|
201
|
+
&& now() < checked.expiresAt && checked.installationKey === environment.installationKey
|
|
202
|
+
&& semver.valid(runningVersion) && semver.gt(checked.release.version, runningVersion));
|
|
203
|
+
return {
|
|
204
|
+
runningVersion,
|
|
205
|
+
installedVersion: environment.installedVersion ?? null,
|
|
206
|
+
latestVersion: checked?.release.version ?? null,
|
|
207
|
+
profileName: environment.profileName ?? null,
|
|
208
|
+
environmentKind: environment.environmentKind ?? 'cli',
|
|
209
|
+
// Preserve source protection even when restart/recovery takes status priority.
|
|
210
|
+
sourceInstall: environment.sourceInstall === true || environment.blockedReason === 'source-install',
|
|
211
|
+
canInstall,
|
|
212
|
+
blockedReason,
|
|
213
|
+
checkedAt: checked?.checkedAt ?? null,
|
|
214
|
+
checkId: canInstall ? checked.checkId : null,
|
|
215
|
+
job: publicJob(job),
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
async function status() {
|
|
220
|
+
assertActive();
|
|
221
|
+
const environment = await runtime.inspect();
|
|
222
|
+
return snapshot(environment, await readJob(environment));
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
async function check() {
|
|
226
|
+
assertActive();
|
|
227
|
+
if (checking) return checking;
|
|
228
|
+
// Collapse double clicks without turning a failed request into a cached success.
|
|
229
|
+
if (checked?.checkId && now() - lastCheckAt < 2_000) return status();
|
|
230
|
+
lastCheckAt = now();
|
|
231
|
+
checking = (async () => {
|
|
232
|
+
try {
|
|
233
|
+
const environment = await runtime.inspect({ preflight: true });
|
|
234
|
+
const release = await fetchNpmRelease(fetchImpl, checkTimeoutMs);
|
|
235
|
+
assertActive();
|
|
236
|
+
checked = {
|
|
237
|
+
release,
|
|
238
|
+
checkId: randomUUID(),
|
|
239
|
+
checkedAt: now(),
|
|
240
|
+
expiresAt: now() + confirmationTtlMs,
|
|
241
|
+
installationKey: environment.installationKey,
|
|
242
|
+
profileDir: environment.profileDir,
|
|
243
|
+
blockedReason: environment.blockedReason
|
|
244
|
+
?? (!semver.satisfies(nodeVersion, release.nodeRange) ? 'incompatible-node' : null),
|
|
245
|
+
};
|
|
246
|
+
return snapshot(environment, await readJob(environment));
|
|
247
|
+
} catch (error) {
|
|
248
|
+
if (checked) checked = { ...checked, checkId: null, expiresAt: 0 };
|
|
249
|
+
throw error;
|
|
250
|
+
} finally {
|
|
251
|
+
checking = null;
|
|
252
|
+
}
|
|
253
|
+
})();
|
|
254
|
+
return checking;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
async function releaseLock(paths, id) {
|
|
258
|
+
const lock = await readJson(paths.lock);
|
|
259
|
+
if (lock?.id === id) await unlink(paths.lock);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
async function backupProfile(environment, paths, job) {
|
|
263
|
+
const files = {};
|
|
264
|
+
for (const filename of SNAPSHOT_FILES) {
|
|
265
|
+
try {
|
|
266
|
+
const path = join(environment.profileDir, filename);
|
|
267
|
+
if ((await stat(path)).size > 3 * 1024 * 1024) throw updateError('state-unavailable');
|
|
268
|
+
files[filename] = await readFile(path, 'utf8');
|
|
269
|
+
} catch (error) {
|
|
270
|
+
if (error.code !== 'ENOENT') throw updateError('state-unavailable');
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
// Keep only the previous attempt's small manifests, never credentials or the entire home.
|
|
274
|
+
await writeJson(paths.backup, { jobId: job.id, previousVersion: job.previousVersion, files });
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
function rememberUnsavedJob(paths) {
|
|
278
|
+
activeJob = { ...activeJob, state: 'interrupted', message: 'state-unavailable' };
|
|
279
|
+
unsavedJob = { statePath: paths.state, job: activeJob };
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
async function execute(paths, job, originalEnvironment) {
|
|
283
|
+
const deadline = AbortSignal.timeout(installTimeoutMs);
|
|
284
|
+
try {
|
|
285
|
+
await runtime.install(job.targetVersion, {
|
|
286
|
+
signal: AbortSignal.any([abortController.signal, deadline]),
|
|
287
|
+
expectedInstallationKey: originalEnvironment.installationKey,
|
|
288
|
+
});
|
|
289
|
+
if (disposed) throw updateError('interrupted');
|
|
290
|
+
activeJob = { ...activeJob, state: 'verifying', updatedAt: now() };
|
|
291
|
+
await writeJson(paths.state, activeJob);
|
|
292
|
+
const environment = await runtime.inspect();
|
|
293
|
+
if (environment.homeDir !== originalEnvironment.homeDir || environment.profileDir !== originalEnvironment.profileDir
|
|
294
|
+
|| environment.profileName !== originalEnvironment.profileName || environment.blockedReason === 'installation-changed') {
|
|
295
|
+
throw updateError('installation-changed');
|
|
296
|
+
}
|
|
297
|
+
if (environment.installedVersion !== job.targetVersion || environment.packageValid !== true) {
|
|
298
|
+
throw updateError('verify-failed');
|
|
299
|
+
}
|
|
300
|
+
if (environment.blockedReason && environment.blockedReason !== 'pending-restart') throw updateError('verify-failed');
|
|
301
|
+
activeJob = { ...activeJob, state: 'restart-required', message: null, updatedAt: now() };
|
|
302
|
+
await writeJson(paths.state, activeJob);
|
|
303
|
+
} catch (error) {
|
|
304
|
+
const timedOut = deadline.aborted || error.code === 'install-timeout';
|
|
305
|
+
const interrupted = disposed || abortController.signal.aborted
|
|
306
|
+
|| !timedOut && ['interrupted', 'install-interrupted'].includes(error.code);
|
|
307
|
+
activeJob = {
|
|
308
|
+
...activeJob,
|
|
309
|
+
state: interrupted ? 'interrupted' : 'failed',
|
|
310
|
+
message: interrupted ? 'interrupted' : timedOut ? 'install-timeout'
|
|
311
|
+
: ['verify-failed', 'state-unavailable', 'installation-changed', 'registry-conflict'].includes(error.code)
|
|
312
|
+
? error.code : 'install-failed',
|
|
313
|
+
updatedAt: now(),
|
|
314
|
+
};
|
|
315
|
+
try {
|
|
316
|
+
await writeJson(paths.state, activeJob);
|
|
317
|
+
} catch {
|
|
318
|
+
// Retain the lock when the final record cannot be saved; the next Host must inspect it.
|
|
319
|
+
rememberUnsavedJob(paths);
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
await releaseLock(paths, job.id);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
function install({ checkId, requestId }) {
|
|
327
|
+
return withSessionBindingLock(queue, 'install', async () => {
|
|
328
|
+
assertActive();
|
|
329
|
+
let environment = await runtime.inspect({ preflight: true });
|
|
330
|
+
const previous = await readJob(environment);
|
|
331
|
+
if (previous?.requestId === requestId) return snapshot(environment, previous);
|
|
332
|
+
if (ACTIVE_STATES.has(previous?.state) || previous?.state === 'restart-required') throw updateError('update-busy');
|
|
333
|
+
const confirmation = checked;
|
|
334
|
+
if (!confirmation || !checkId || confirmation.checkId !== checkId || now() >= confirmation.expiresAt) {
|
|
335
|
+
throw updateError('check-expired');
|
|
336
|
+
}
|
|
337
|
+
if (environment.profileDir !== confirmation.profileDir || environment.installationKey !== confirmation.installationKey) {
|
|
338
|
+
throw updateError('installation-changed');
|
|
339
|
+
}
|
|
340
|
+
if (!snapshot(environment, previous).canInstall) throw updateError(environment.blockedReason ?? 'update-busy');
|
|
341
|
+
const paths = pathsFor(environment);
|
|
342
|
+
if (!paths) throw updateError('state-unavailable');
|
|
343
|
+
const job = {
|
|
344
|
+
id: randomUUID(), requestId, state: 'installing', message: null,
|
|
345
|
+
targetVersion: confirmation.release.version, previousVersion: environment.installedVersion,
|
|
346
|
+
startedAt: now(), updatedAt: now(),
|
|
347
|
+
};
|
|
348
|
+
let locked = false;
|
|
349
|
+
try {
|
|
350
|
+
await mkdir(paths.directory, { recursive: true, mode: 0o700 });
|
|
351
|
+
const lock = await open(paths.lock, 'wx', 0o600);
|
|
352
|
+
locked = true;
|
|
353
|
+
try {
|
|
354
|
+
await lock.writeFile(JSON.stringify({ id: job.id, pid: process.pid, startedAt: now() }));
|
|
355
|
+
} finally {
|
|
356
|
+
await lock.close();
|
|
357
|
+
}
|
|
358
|
+
const currentRelease = await fetchNpmRelease(fetchImpl, checkTimeoutMs);
|
|
359
|
+
if (JSON.stringify(currentRelease) !== JSON.stringify(confirmation.release)) throw updateError('check-expired');
|
|
360
|
+
environment = await runtime.inspect({ preflight: true });
|
|
361
|
+
if (environment.profileDir !== confirmation.profileDir || environment.installationKey !== confirmation.installationKey) {
|
|
362
|
+
throw updateError('installation-changed');
|
|
363
|
+
}
|
|
364
|
+
if (!environment.eligible || environment.blockedReason) throw updateError(environment.blockedReason ?? 'update-busy');
|
|
365
|
+
assertActive();
|
|
366
|
+
await backupProfile(environment, paths, job);
|
|
367
|
+
await writeJson(paths.state, job);
|
|
368
|
+
assertActive();
|
|
369
|
+
} catch (error) {
|
|
370
|
+
if (locked) await releaseLock(paths, job.id);
|
|
371
|
+
if (error.code === 'EEXIST') throw updateError('update-busy');
|
|
372
|
+
if (error.code?.startsWith('E')) throw updateError('state-unavailable');
|
|
373
|
+
throw error;
|
|
374
|
+
}
|
|
375
|
+
activeJob = job;
|
|
376
|
+
abortController = new AbortController();
|
|
377
|
+
activeTask = execute(paths, job, environment).catch(() => {
|
|
378
|
+
// Keep unknown cleanup failures local and keep the on-disk lock for manual recovery.
|
|
379
|
+
rememberUnsavedJob(paths);
|
|
380
|
+
});
|
|
381
|
+
return snapshot(environment, job);
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
async function close() {
|
|
386
|
+
disposed = true;
|
|
387
|
+
abortController?.abort();
|
|
388
|
+
if (activeTask) await activeTask;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
return Object.freeze({ status, check, install, close });
|
|
392
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { resolveRpcAuthority } from './host/rpc-authority.mjs';
|
|
2
|
+
|
|
3
|
+
function rpcEndpoint(channel) {
|
|
4
|
+
if (!/^\/[A-Za-z0-9._~-]+$/.test(channel)) throw new TypeError('Invalid IM RPC channel');
|
|
5
|
+
return `dsh-im${channel}`;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
function isRecord(value) {
|
|
9
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function isLoopbackAuthority(authority) {
|
|
13
|
+
if (!authority) return false;
|
|
14
|
+
try {
|
|
15
|
+
const url = new URL(`http://${authority}`);
|
|
16
|
+
if (url.username || url.password || url.pathname !== '/' || url.search || url.hash) return false;
|
|
17
|
+
const hostname = url.hostname.replace(/\.$/, '');
|
|
18
|
+
return hostname === 'localhost' || hostname === '[::1]'
|
|
19
|
+
|| /^127\.\d+\.\d+\.\d+$/.test(hostname);
|
|
20
|
+
} catch { return false; }
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function isLoopbackRequest(request) {
|
|
24
|
+
if (!isLoopbackAuthority(request.headers.get('host'))) return false;
|
|
25
|
+
const origin = request.headers.get('origin');
|
|
26
|
+
if (origin === null) return true;
|
|
27
|
+
try {
|
|
28
|
+
const url = new URL(origin);
|
|
29
|
+
return ['http:', 'https:'].includes(url.protocol) && isLoopbackAuthority(url.host);
|
|
30
|
+
} catch { return false; }
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function reply(rpcId, result) {
|
|
34
|
+
// Older endpoint handlers omit details; Connection's client requires the field.
|
|
35
|
+
const value = result.ok === false
|
|
36
|
+
? { ...result, error: { ...result.error, details: result.error.details ?? {} } } : result;
|
|
37
|
+
return Response.json({ type: 'server-response', rpcId, result: value });
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Register an IM channel through the public /api carrier shared by supported DSH releases. */
|
|
41
|
+
export function registerManagementRpc(ctx, channel, handler, { authority } = {}) {
|
|
42
|
+
const policy = resolveRpcAuthority(authority);
|
|
43
|
+
const endpoint = rpcEndpoint(channel);
|
|
44
|
+
if (typeof ctx?.connection?.fetch?.register !== 'function') {
|
|
45
|
+
throw new TypeError('DSH Host Connection Fetch registry is required');
|
|
46
|
+
}
|
|
47
|
+
return ctx.connection.fetch.register({
|
|
48
|
+
path: `/api/${endpoint}`,
|
|
49
|
+
methods: ['POST'],
|
|
50
|
+
requestBody: 'buffered',
|
|
51
|
+
async fetch(request) {
|
|
52
|
+
// DSH applies browser authentication and Host/Origin trust before this handler.
|
|
53
|
+
if (policy === 'loopback' && !isLoopbackRequest(request)) return new Response('forbidden', { status: 403 });
|
|
54
|
+
if (request.method !== 'POST') return new Response('method not allowed', { status: 405 });
|
|
55
|
+
if (request.headers.get('content-type')?.split(';', 1)[0].trim().toLowerCase() !== 'application/json') {
|
|
56
|
+
return new Response('content type must be application/json', { status: 415 });
|
|
57
|
+
}
|
|
58
|
+
let message;
|
|
59
|
+
try { message = await request.json(); }
|
|
60
|
+
catch { return new Response('body is not JSON', { status: 400 }); }
|
|
61
|
+
const rpcId = typeof message?.rpcId === 'string' ? message.rpcId : 'invalid-request';
|
|
62
|
+
const call = message?.payload;
|
|
63
|
+
if (!isRecord(message) || message.type !== 'client-request' || typeof message.rpcId !== 'string'
|
|
64
|
+
|| message.method !== endpoint || !isRecord(call) || typeof call.method !== 'string'
|
|
65
|
+
|| !Object.hasOwn(call, 'payload')) {
|
|
66
|
+
return reply(rpcId, { ok: false, error: { code: 'gateway/bad-request', message: 'Invalid IM management request.' } });
|
|
67
|
+
}
|
|
68
|
+
try { return reply(rpcId, await handler(call.method, call.payload, request.signal)); }
|
|
69
|
+
catch { return new Response('IM management handler failed', { status: 500 }); }
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Keep channel UI callers unchanged while using DSH's native correlation and response validation. */
|
|
75
|
+
export async function callManagementRpc(connection, channel, method, payload, signal) {
|
|
76
|
+
try {
|
|
77
|
+
return await connection.rpc.call('/api', rpcEndpoint(channel), { method, payload }, signal);
|
|
78
|
+
} catch (cause) {
|
|
79
|
+
if (signal?.aborted || cause?.name === 'AbortError') throw cause;
|
|
80
|
+
const error = new Error('无法访问 DSH 管理接口,请检查 DSH 连接或重新登录后重试。', { cause });
|
|
81
|
+
error.code = 'management-unreachable';
|
|
82
|
+
error.details = { operation: method, stage: 'management.request', occurredAt: new Date().toISOString(),
|
|
83
|
+
...(Number.isInteger(cause?.status) && cause.status >= 100 && cause.status <= 599 ? { httpStatus: cause.status } : {}),
|
|
84
|
+
};
|
|
85
|
+
throw error;
|
|
86
|
+
}
|
|
87
|
+
}
|