@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,107 @@
|
|
|
1
|
+
import { mkdir, readFile, rename, unlink, writeFile } from 'node:fs/promises';
|
|
2
|
+
import { dirname, isAbsolute } from 'node:path';
|
|
3
|
+
|
|
4
|
+
import { normalizeOfficeBaseUrl, OFFICE_PROTOCOL_VERSION } from './protocol.mjs';
|
|
5
|
+
|
|
6
|
+
const ALIAS = /^[a-z][a-z0-9-]{1,63}$/;
|
|
7
|
+
const DEVICE_ID = /^[a-z0-9][a-z0-9_-]{2,63}$/i;
|
|
8
|
+
|
|
9
|
+
function cleanString(value) {
|
|
10
|
+
return typeof value === 'string' && value.trim() ? value.trim() : null;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function normalizeMap(value, { kind }) {
|
|
14
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) return null;
|
|
15
|
+
const output = {};
|
|
16
|
+
for (const [rawId, rawValue] of Object.entries(value)) {
|
|
17
|
+
const id = cleanString(rawId);
|
|
18
|
+
const item = cleanString(rawValue);
|
|
19
|
+
if (!id || !ALIAS.test(id) || !item) return null;
|
|
20
|
+
if (kind === 'workspace' && !isAbsolute(item)) return null;
|
|
21
|
+
if (kind === 'preset' && item.length > 8_000) return null;
|
|
22
|
+
output[id] = item;
|
|
23
|
+
}
|
|
24
|
+
return Object.freeze(output);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function normalizeOfficeConfig(value) {
|
|
28
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) return null;
|
|
29
|
+
let baseUrl;
|
|
30
|
+
try {
|
|
31
|
+
baseUrl = normalizeOfficeBaseUrl(value.baseUrl).origin;
|
|
32
|
+
} catch {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
const deviceId = cleanString(value.deviceId);
|
|
36
|
+
const deviceTokenRef = cleanString(value.deviceTokenRef);
|
|
37
|
+
const workspaces = normalizeMap(value.workspaces ?? {}, { kind: 'workspace' });
|
|
38
|
+
const instructionPresets = normalizeMap(value.instructionPresets ?? {}, { kind: 'preset' });
|
|
39
|
+
const maxConcurrency = Number(value.maxConcurrency ?? 1);
|
|
40
|
+
const heartbeatSeconds = Number(value.heartbeatSeconds ?? 30);
|
|
41
|
+
if (!deviceId || !DEVICE_ID.test(deviceId) || !deviceTokenRef
|
|
42
|
+
|| !/^DSH_OFFICE_DEVICE_TOKEN_[A-F0-9]{24}$/.test(deviceTokenRef)
|
|
43
|
+
|| !workspaces || !instructionPresets
|
|
44
|
+
|| !Number.isInteger(maxConcurrency) || maxConcurrency < 1 || maxConcurrency > 4
|
|
45
|
+
|| !Number.isInteger(heartbeatSeconds) || heartbeatSeconds < 10 || heartbeatSeconds > 300) {
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
return Object.freeze({
|
|
49
|
+
version: 1,
|
|
50
|
+
protocolVersion: OFFICE_PROTOCOL_VERSION,
|
|
51
|
+
baseUrl,
|
|
52
|
+
deviceId,
|
|
53
|
+
deviceTokenRef,
|
|
54
|
+
maxConcurrency,
|
|
55
|
+
heartbeatSeconds,
|
|
56
|
+
workspaces,
|
|
57
|
+
instructionPresets,
|
|
58
|
+
createdAt: cleanString(value.createdAt) ?? new Date().toISOString(),
|
|
59
|
+
updatedAt: cleanString(value.updatedAt) ?? new Date().toISOString(),
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export class OfficeConfigStore {
|
|
64
|
+
#path;
|
|
65
|
+
#value = null;
|
|
66
|
+
#queue = Promise.resolve();
|
|
67
|
+
|
|
68
|
+
constructor(path) { this.#path = path; }
|
|
69
|
+
|
|
70
|
+
async load() {
|
|
71
|
+
try {
|
|
72
|
+
const normalized = normalizeOfficeConfig(JSON.parse(await readFile(this.#path, 'utf8')));
|
|
73
|
+
if (!normalized) throw new Error('dsh-im AI Office config is invalid');
|
|
74
|
+
this.#value = normalized;
|
|
75
|
+
} catch (error) {
|
|
76
|
+
if (error?.code !== 'ENOENT') throw error;
|
|
77
|
+
this.#value = null;
|
|
78
|
+
}
|
|
79
|
+
return this;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
get() { return this.#value ? structuredClone(this.#value) : null; }
|
|
83
|
+
|
|
84
|
+
async save(value) {
|
|
85
|
+
const normalized = normalizeOfficeConfig(value);
|
|
86
|
+
if (!normalized) throw new Error('Refusing to persist invalid AI Office configuration');
|
|
87
|
+
const operation = this.#queue.then(async () => {
|
|
88
|
+
await mkdir(dirname(this.#path), { recursive: true, mode: 0o700 });
|
|
89
|
+
const temporary = `${this.#path}.tmp`;
|
|
90
|
+
await writeFile(temporary, `${JSON.stringify(normalized, null, 2)}\n`, { mode: 0o600 });
|
|
91
|
+
await rename(temporary, this.#path);
|
|
92
|
+
this.#value = normalized;
|
|
93
|
+
});
|
|
94
|
+
this.#queue = operation.then(() => undefined, () => undefined);
|
|
95
|
+
await operation;
|
|
96
|
+
return this.get();
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async clear() {
|
|
100
|
+
const operation = this.#queue.then(async () => {
|
|
101
|
+
try { await unlink(this.#path); } catch (error) { if (error?.code !== 'ENOENT') throw error; }
|
|
102
|
+
this.#value = null;
|
|
103
|
+
});
|
|
104
|
+
this.#queue = operation.then(() => undefined, () => undefined);
|
|
105
|
+
await operation;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { atConnectionStage, createConnectionDiagnostics } from '../shared/connection-error.mjs';
|
|
2
|
+
import { createHash } from 'node:crypto';
|
|
3
|
+
|
|
4
|
+
import { normalizeOfficeBaseUrl, officeHookUrls } from './protocol.mjs';
|
|
5
|
+
import { normalizeOfficeConfig } from './config-store.mjs';
|
|
6
|
+
import { OfficeRuntime } from './office-runtime.mjs';
|
|
7
|
+
|
|
8
|
+
function clean(value) { return typeof value === 'string' && value.trim() ? value.trim() : null; }
|
|
9
|
+
|
|
10
|
+
export function officeTokenRef(baseUrl, deviceId) {
|
|
11
|
+
const digest = createHash('sha256').update(`${baseUrl}\n${deviceId}`).digest('hex').slice(0, 24).toUpperCase();
|
|
12
|
+
return `DSH_OFFICE_DEVICE_TOKEN_${digest}`;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export class OfficeController {
|
|
16
|
+
#credentials;
|
|
17
|
+
#store;
|
|
18
|
+
#logger;
|
|
19
|
+
#diagnostics;
|
|
20
|
+
#createRuntime;
|
|
21
|
+
#runtime = null;
|
|
22
|
+
#transition = Promise.resolve();
|
|
23
|
+
|
|
24
|
+
constructor({ credentials, configStore, logger = console, createRuntime }) {
|
|
25
|
+
if (!credentials?.resolve || !credentials?.set || !credentials?.unset) {
|
|
26
|
+
throw new TypeError('AI Office requires the Harness credential provider');
|
|
27
|
+
}
|
|
28
|
+
if (!configStore?.get || !configStore?.save || !configStore?.clear) {
|
|
29
|
+
throw new TypeError('AI Office requires a config store');
|
|
30
|
+
}
|
|
31
|
+
this.#credentials = credentials;
|
|
32
|
+
this.#store = configStore;
|
|
33
|
+
this.#logger = logger;
|
|
34
|
+
this.#diagnostics = createConnectionDiagnostics({ channel: 'office', logger });
|
|
35
|
+
this.#createRuntime = createRuntime ?? ((options) => new OfficeRuntime(options));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
get diagnostics() { return this.#diagnostics; }
|
|
39
|
+
|
|
40
|
+
async initialize() {
|
|
41
|
+
const config = this.#store.get();
|
|
42
|
+
if (config) {
|
|
43
|
+
const credential = await atConnectionStage('credential.read', () => this.#credentials.resolve(config.deviceTokenRef), 'credential-store');
|
|
44
|
+
if (credential?.value) await this.#start(config, credential.value);
|
|
45
|
+
}
|
|
46
|
+
return this.status();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async configure(input = {}) {
|
|
50
|
+
return this.#serial(async () => {
|
|
51
|
+
const previous = this.#store.get();
|
|
52
|
+
const requestedBaseUrl = clean(input.baseUrl);
|
|
53
|
+
const deviceId = clean(input.deviceId);
|
|
54
|
+
if (!requestedBaseUrl || !deviceId) throw new TypeError('Office URL and Device ID are required');
|
|
55
|
+
const baseUrl = normalizeOfficeBaseUrl(requestedBaseUrl).origin;
|
|
56
|
+
const tokenRef = officeTokenRef(baseUrl, deviceId);
|
|
57
|
+
const suppliedToken = clean(input.deviceToken);
|
|
58
|
+
const priorCredential = await atConnectionStage('credential.read', () => this.#credentials.resolve(tokenRef), 'credential-store');
|
|
59
|
+
const token = suppliedToken ?? priorCredential?.value;
|
|
60
|
+
if (!token || token.length < 32) throw new TypeError('Device Token must contain at least 32 characters');
|
|
61
|
+
const now = new Date().toISOString();
|
|
62
|
+
const config = normalizeOfficeConfig({
|
|
63
|
+
version: 1,
|
|
64
|
+
baseUrl,
|
|
65
|
+
deviceId,
|
|
66
|
+
deviceTokenRef: tokenRef,
|
|
67
|
+
maxConcurrency: input.maxConcurrency,
|
|
68
|
+
heartbeatSeconds: input.heartbeatSeconds,
|
|
69
|
+
workspaces: input.workspaces,
|
|
70
|
+
instructionPresets: input.instructionPresets,
|
|
71
|
+
createdAt: previous?.createdAt ?? now,
|
|
72
|
+
updatedAt: now,
|
|
73
|
+
});
|
|
74
|
+
if (!config) throw new TypeError('AI Office connector configuration is invalid');
|
|
75
|
+
|
|
76
|
+
await atConnectionStage('credential.save', () => this.#credentials.set(tokenRef, token), 'credential-store');
|
|
77
|
+
try {
|
|
78
|
+
await atConnectionStage('account.save', () => this.#store.save(config), 'account-config');
|
|
79
|
+
} catch (error) {
|
|
80
|
+
if (priorCredential?.value) await atConnectionStage('credential.save', () => this.#credentials.set(tokenRef, priorCredential.value), 'credential-store').catch(() => undefined);
|
|
81
|
+
else await atConnectionStage('credential.remove', () => this.#credentials.unset(tokenRef), 'credential-store').catch(() => undefined);
|
|
82
|
+
throw error;
|
|
83
|
+
}
|
|
84
|
+
if (previous?.deviceTokenRef && previous.deviceTokenRef !== tokenRef) {
|
|
85
|
+
await atConnectionStage('credential.remove', () => this.#credentials.unset(previous.deviceTokenRef), 'credential-store');
|
|
86
|
+
}
|
|
87
|
+
await this.#start(config, token);
|
|
88
|
+
return this.status();
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async reconnect() {
|
|
93
|
+
return this.#serial(async () => {
|
|
94
|
+
const config = this.#store.get();
|
|
95
|
+
if (!config) throw new Error('AI Office connector is not configured');
|
|
96
|
+
await this.#start(config);
|
|
97
|
+
return this.status();
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async test() {
|
|
102
|
+
const config = this.#store.get();
|
|
103
|
+
if (!config) throw new Error('AI Office connector is not configured');
|
|
104
|
+
const token = await this.#resolveToken(config);
|
|
105
|
+
const runtime = this.#createRuntime({ config, token, logger: this.#logger });
|
|
106
|
+
try { await runtime.testConnection(AbortSignal.timeout(10_000)); } finally { await runtime.stop().catch(() => undefined); }
|
|
107
|
+
return { tested: true, snapshot: await this.status() };
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
async remove() {
|
|
111
|
+
return this.#serial(async () => {
|
|
112
|
+
const warnings = [];
|
|
113
|
+
const config = this.#store.get();
|
|
114
|
+
await this.#stop();
|
|
115
|
+
await atConnectionStage('account.remove', () => this.#store.clear(), 'account-config');
|
|
116
|
+
if (config?.deviceTokenRef) {
|
|
117
|
+
await atConnectionStage('credential.remove', () => this.#credentials.unset(config.deviceTokenRef), 'credential-store').catch(error => {
|
|
118
|
+
warnings.push(this.#diagnostics.report(error, { reuse: true, operation: 'connector.remove', warning: true,
|
|
119
|
+
publicError: { code: 'cleanup-failed', message: '连接已移除,但登录凭据清理失败。' } }).publicError);
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
return { ...await this.status(), ...(warnings.length ? { warnings } : {}) };
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async status() {
|
|
127
|
+
const config = this.#store.get();
|
|
128
|
+
if (!config) return { schemaVersion: 1, configured: false, connected: false, state: 'unconfigured' };
|
|
129
|
+
const credential = await atConnectionStage('credential.read', () => this.#credentials.resolve(config.deviceTokenRef), 'credential-store');
|
|
130
|
+
const runtime = this.#runtime?.status ?? null;
|
|
131
|
+
return {
|
|
132
|
+
schemaVersion: 1,
|
|
133
|
+
configured: true,
|
|
134
|
+
connected: runtime?.connected === true,
|
|
135
|
+
state: runtime?.state ?? (credential?.value ? 'idle' : 'missing-token'),
|
|
136
|
+
config: {
|
|
137
|
+
protocolVersion: config.protocolVersion,
|
|
138
|
+
baseUrl: config.baseUrl,
|
|
139
|
+
deviceId: config.deviceId,
|
|
140
|
+
maxConcurrency: config.maxConcurrency,
|
|
141
|
+
heartbeatSeconds: config.heartbeatSeconds,
|
|
142
|
+
workspaces: config.workspaces,
|
|
143
|
+
instructionPresets: config.instructionPresets,
|
|
144
|
+
hooks: officeHookUrls(config.baseUrl),
|
|
145
|
+
},
|
|
146
|
+
health: runtime,
|
|
147
|
+
tokenConfigured: Boolean(credential?.value),
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
async close() { await this.#transition.catch(() => undefined); await this.#stop(); }
|
|
152
|
+
|
|
153
|
+
async #resolveToken(config) {
|
|
154
|
+
const credential = await atConnectionStage('credential.read', () => this.#credentials.resolve(config.deviceTokenRef), 'credential-store');
|
|
155
|
+
if (!credential?.value) throw new Error('AI Office Device Token is missing');
|
|
156
|
+
return credential.value;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
async #start(config, knownToken) {
|
|
160
|
+
await this.#stop();
|
|
161
|
+
const token = knownToken ?? await this.#resolveToken(config);
|
|
162
|
+
const runtime = this.#createRuntime({ config, token, logger: this.#logger });
|
|
163
|
+
this.#runtime = runtime;
|
|
164
|
+
runtime.start();
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
async #stop() {
|
|
168
|
+
const runtime = this.#runtime;
|
|
169
|
+
this.#runtime = null;
|
|
170
|
+
if (runtime) await runtime.stop();
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
#serial(operation) {
|
|
174
|
+
const run = this.#transition.then(operation, operation);
|
|
175
|
+
this.#transition = run.then(() => undefined, () => undefined);
|
|
176
|
+
return run;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
import { setTimeout as sleep } from 'node:timers/promises';
|
|
2
|
+
|
|
3
|
+
import { t } from '../shared/i18n.mjs';
|
|
4
|
+
import { harnessApprovalText } from '../shared/harness-approval.mjs';
|
|
5
|
+
import {
|
|
6
|
+
harnessAnswerForQuestion,
|
|
7
|
+
harnessQuestionText,
|
|
8
|
+
validHarnessQuestion,
|
|
9
|
+
} from '../shared/harness-question.mjs';
|
|
10
|
+
|
|
11
|
+
const JOB_ID = /^job-[a-f0-9]{32}$/;
|
|
12
|
+
const RENEW_MS = 30_000;
|
|
13
|
+
const COMPLETED_LIMIT = 2_048;
|
|
14
|
+
|
|
15
|
+
function abortError() {
|
|
16
|
+
return new DOMException('Office Job was cancelled', 'AbortError');
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function clean(value, max = 8_000) {
|
|
20
|
+
return typeof value === 'string' ? value.trim().slice(0, max) : '';
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function safeFailure(error) {
|
|
24
|
+
if (error?.code === 'office-job-alias-invalid') return error.message;
|
|
25
|
+
if (error?.code === 'turn-stopped') return t('本机 Harness 已停止这次执行。');
|
|
26
|
+
if (error?.code === 'office-job-conflict') return t('Office Job 已被领取、取消或结束。');
|
|
27
|
+
return t('本机 Harness 未能完成任务;请检查 Harness 会话后重试。');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function renderPrompt(job, preset) {
|
|
31
|
+
return [
|
|
32
|
+
'# AI Office Handoff',
|
|
33
|
+
'',
|
|
34
|
+
t('你正在本机 DeepSeek Harness 中继续一个来自 AI Office 的任务。'),
|
|
35
|
+
t('只在当前 Workspace 内行动。完成后必须返回:结果摘要、改动文件、验证证据、未解决风险。'),
|
|
36
|
+
'',
|
|
37
|
+
t('## 本机 Instruction Preset'),
|
|
38
|
+
preset,
|
|
39
|
+
'',
|
|
40
|
+
...(clean(job.instruction) ? [t('## 本轮补充指令'), clean(job.instruction), ''] : []),
|
|
41
|
+
t('## Office 时间线'),
|
|
42
|
+
clean(job.markdown, 200_000),
|
|
43
|
+
].join('\n');
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function approvalRequest(interaction) {
|
|
47
|
+
const id = clean(interaction?.interactionId || interaction?.rpcId, 180);
|
|
48
|
+
if (!id || typeof interaction?.respond !== 'function') return null;
|
|
49
|
+
if (interaction.kind === 'approval') {
|
|
50
|
+
const prompt = harnessApprovalText(interaction.payload, {
|
|
51
|
+
toolCall: interaction.toolCall,
|
|
52
|
+
maxArgumentsLength: 10_000,
|
|
53
|
+
});
|
|
54
|
+
if (!prompt) return null;
|
|
55
|
+
return {
|
|
56
|
+
id,
|
|
57
|
+
kind: 'approval',
|
|
58
|
+
title: t('批准 {tool}', { tool: clean(interaction.payload?.toolName, 120) || t('Harness 工具') }),
|
|
59
|
+
prompt,
|
|
60
|
+
toolName: clean(interaction.payload?.toolName, 120),
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
if (interaction.kind !== 'question') return null;
|
|
64
|
+
const questions = interaction.payload?.questions;
|
|
65
|
+
if (!Array.isArray(questions) || questions.length === 0
|
|
66
|
+
|| questions.some((question) => !validHarnessQuestion(question))) return null;
|
|
67
|
+
return {
|
|
68
|
+
id,
|
|
69
|
+
kind: 'question',
|
|
70
|
+
title: questions.length > 1 ? t('Harness 需要 {count} 项补充信息', { count: questions.length }) : clean(questions[0].header || questions[0].question, 160),
|
|
71
|
+
prompt: questions.map((question, index) => harnessQuestionText(question, index, questions.length)).join('\n\n---\n\n'),
|
|
72
|
+
questions,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function waitForReply(entry, approvalId) {
|
|
77
|
+
return new Promise((resolve, reject) => {
|
|
78
|
+
if (entry.controller.signal.aborted) return reject(entry.controller.signal.reason ?? abortError());
|
|
79
|
+
const onAbort = () => {
|
|
80
|
+
entry.approvals.delete(approvalId);
|
|
81
|
+
reject(entry.controller.signal.reason ?? abortError());
|
|
82
|
+
};
|
|
83
|
+
entry.controller.signal.addEventListener('abort', onAbort, { once: true });
|
|
84
|
+
entry.approvals.set(approvalId, {
|
|
85
|
+
resolve(value) {
|
|
86
|
+
entry.controller.signal.removeEventListener('abort', onAbort);
|
|
87
|
+
entry.approvals.delete(approvalId);
|
|
88
|
+
resolve(value);
|
|
89
|
+
},
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export class OfficeJobExecutor {
|
|
95
|
+
#config;
|
|
96
|
+
#transport;
|
|
97
|
+
#createHarness;
|
|
98
|
+
#logger;
|
|
99
|
+
#sleep;
|
|
100
|
+
#active = new Map();
|
|
101
|
+
#queued = new Set();
|
|
102
|
+
#completed = new Set();
|
|
103
|
+
#closed = false;
|
|
104
|
+
#status = {
|
|
105
|
+
running: 0,
|
|
106
|
+
completed: 0,
|
|
107
|
+
failed: 0,
|
|
108
|
+
lastJobId: null,
|
|
109
|
+
lastJobAt: null,
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
constructor({
|
|
113
|
+
config,
|
|
114
|
+
transport,
|
|
115
|
+
createHarness,
|
|
116
|
+
logger = console,
|
|
117
|
+
sleepImpl = sleep,
|
|
118
|
+
}) {
|
|
119
|
+
if (!config || !transport || typeof createHarness !== 'function') {
|
|
120
|
+
throw new TypeError('OfficeJobExecutor requires config, transport, and createHarness');
|
|
121
|
+
}
|
|
122
|
+
this.#config = config;
|
|
123
|
+
this.#transport = transport;
|
|
124
|
+
this.#createHarness = createHarness;
|
|
125
|
+
this.#logger = logger;
|
|
126
|
+
this.#sleep = sleepImpl;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
get status() { return structuredClone(this.#status); }
|
|
130
|
+
|
|
131
|
+
offer(jobId) {
|
|
132
|
+
if (this.#closed || !JOB_ID.test(jobId) || this.#active.has(jobId)
|
|
133
|
+
|| this.#queued.has(jobId) || this.#completed.has(jobId)) return false;
|
|
134
|
+
this.#queued.add(jobId);
|
|
135
|
+
this.#drain();
|
|
136
|
+
return true;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
handleEvent(event) {
|
|
140
|
+
const jobId = clean(event?.data?.jobId, 80);
|
|
141
|
+
if (!JOB_ID.test(jobId)) return false;
|
|
142
|
+
if (event.type === 'job.available') return this.offer(jobId);
|
|
143
|
+
if (event.type === 'job.cancel') return this.cancel(jobId);
|
|
144
|
+
if (event.type === 'approval.reply') {
|
|
145
|
+
const entry = this.#active.get(jobId);
|
|
146
|
+
const approvalId = clean(event.data?.approvalId, 180);
|
|
147
|
+
const pending = entry?.approvals.get(approvalId);
|
|
148
|
+
if (!pending) return false;
|
|
149
|
+
pending.resolve({
|
|
150
|
+
decision: event.data?.decision === 'approved' ? 'approved' : 'rejected',
|
|
151
|
+
answer: clean(event.data?.answer),
|
|
152
|
+
});
|
|
153
|
+
return true;
|
|
154
|
+
}
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
cancel(jobId) {
|
|
159
|
+
this.#queued.delete(jobId);
|
|
160
|
+
const entry = this.#active.get(jobId);
|
|
161
|
+
if (!entry) return false;
|
|
162
|
+
entry.cancelled = true;
|
|
163
|
+
entry.controller.abort(abortError());
|
|
164
|
+
if (entry.sessionId && entry.harness) {
|
|
165
|
+
void entry.harness.rpc('session.cancel', {
|
|
166
|
+
sessionId: entry.sessionId,
|
|
167
|
+
keepInbox: true,
|
|
168
|
+
}, 10_000).catch(() => undefined);
|
|
169
|
+
}
|
|
170
|
+
return true;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
async close() {
|
|
174
|
+
this.#closed = true;
|
|
175
|
+
this.#queued.clear();
|
|
176
|
+
for (const entry of this.#active.values()) entry.controller.abort(abortError());
|
|
177
|
+
await Promise.allSettled([...this.#active.values()].map((entry) => entry.task));
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
#drain() {
|
|
181
|
+
while (!this.#closed && this.#active.size < this.#config.maxConcurrency && this.#queued.size > 0) {
|
|
182
|
+
const jobId = this.#queued.values().next().value;
|
|
183
|
+
this.#queued.delete(jobId);
|
|
184
|
+
const entry = {
|
|
185
|
+
controller: new AbortController(),
|
|
186
|
+
approvals: new Map(),
|
|
187
|
+
harness: null,
|
|
188
|
+
sessionId: null,
|
|
189
|
+
leaseToken: null,
|
|
190
|
+
cancelled: false,
|
|
191
|
+
lastProgressAt: 0,
|
|
192
|
+
lastProgress: '',
|
|
193
|
+
task: null,
|
|
194
|
+
};
|
|
195
|
+
entry.task = this.#run(jobId, entry).finally(() => {
|
|
196
|
+
this.#active.delete(jobId);
|
|
197
|
+
this.#status.running = this.#active.size;
|
|
198
|
+
this.#drain();
|
|
199
|
+
});
|
|
200
|
+
this.#active.set(jobId, entry);
|
|
201
|
+
this.#status.running = this.#active.size;
|
|
202
|
+
void entry.task.catch((error) => this.#logger.warn?.('[dsh-im:office] Job task ended:', error.message));
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
async #run(jobId, entry) {
|
|
207
|
+
const signal = entry.controller.signal;
|
|
208
|
+
let renewTask = null;
|
|
209
|
+
try {
|
|
210
|
+
const fetched = await this.#transport.getJob(jobId, { signal });
|
|
211
|
+
const job = fetched?.job;
|
|
212
|
+
if (!job || job.id !== jobId) throw new Error('Office returned an invalid Job payload');
|
|
213
|
+
const accepted = await this.#transport.acceptJob(jobId, { signal });
|
|
214
|
+
if (!clean(accepted?.leaseToken, 200)) throw new Error('Office returned an invalid Job lease');
|
|
215
|
+
entry.leaseToken = accepted.leaseToken;
|
|
216
|
+
renewTask = this.#renew(jobId, entry);
|
|
217
|
+
const workspace = this.#config.workspaces[job.workspaceAlias];
|
|
218
|
+
const preset = this.#config.instructionPresets[job.instructionPreset];
|
|
219
|
+
if (!workspace || !preset) {
|
|
220
|
+
const error = new Error(t('Office Job 引用了本机未配置的 Workspace/Preset alias。'));
|
|
221
|
+
error.code = 'office-job-alias-invalid';
|
|
222
|
+
throw error;
|
|
223
|
+
}
|
|
224
|
+
entry.harness = this.#createHarness({ workspace });
|
|
225
|
+
await this.#progress(jobId, entry, { kind: 'status', message: t('已领取 Job,准备 Workspace alias:{alias}', { alias: job.workspaceAlias }) }, true);
|
|
226
|
+
entry.sessionId = await entry.harness.createSession({ signal, workspace });
|
|
227
|
+
await this.#progress(jobId, entry, { kind: 'status', message: t('Harness Session 已创建。'), sessionId: entry.sessionId }, true);
|
|
228
|
+
const answer = await entry.harness.ask(entry.sessionId, renderPrompt(job, preset), {
|
|
229
|
+
timeoutMs: 30 * 60_000,
|
|
230
|
+
signal,
|
|
231
|
+
onUpdate: (update) => this.#handleUpdate(jobId, entry, update),
|
|
232
|
+
onInteraction: (interaction) => this.#handleInteraction(jobId, entry, interaction),
|
|
233
|
+
onInteractionResolved: (resolution) => {
|
|
234
|
+
const id = clean(resolution?.interactionId, 180);
|
|
235
|
+
entry.approvals.get(id)?.resolve({ decision: 'rejected', answer: '' });
|
|
236
|
+
},
|
|
237
|
+
});
|
|
238
|
+
if (signal.aborted) throw signal.reason ?? abortError();
|
|
239
|
+
await this.#transport.completeJob(jobId, entry.leaseToken, {
|
|
240
|
+
resultMarkdown: answer,
|
|
241
|
+
sessionId: entry.sessionId,
|
|
242
|
+
}, { signal });
|
|
243
|
+
this.#status.completed += 1;
|
|
244
|
+
this.#rememberCompleted(jobId);
|
|
245
|
+
} catch (error) {
|
|
246
|
+
if (!entry.cancelled && entry.leaseToken) {
|
|
247
|
+
await this.#transport.failJob(jobId, entry.leaseToken, {
|
|
248
|
+
error: safeFailure(error),
|
|
249
|
+
...(entry.sessionId ? { sessionId: entry.sessionId } : {}),
|
|
250
|
+
}).catch(() => undefined);
|
|
251
|
+
this.#status.failed += 1;
|
|
252
|
+
}
|
|
253
|
+
if (error?.code === 'office-job-conflict' || error?.code === 'office-hook-unavailable') {
|
|
254
|
+
this.#rememberCompleted(jobId);
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
if (!entry.cancelled && !signal.aborted) this.#logger.warn?.(`[dsh-im:office] Job ${jobId} failed:`, error.message);
|
|
258
|
+
} finally {
|
|
259
|
+
entry.controller.abort(abortError());
|
|
260
|
+
await renewTask?.catch(() => undefined);
|
|
261
|
+
this.#status.lastJobId = jobId;
|
|
262
|
+
this.#status.lastJobAt = new Date().toISOString();
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
async #renew(jobId, entry) {
|
|
267
|
+
while (!entry.controller.signal.aborted) {
|
|
268
|
+
try { await this.#sleep(RENEW_MS, undefined, { signal: entry.controller.signal }); }
|
|
269
|
+
catch { return; }
|
|
270
|
+
try {
|
|
271
|
+
await this.#transport.renewJob(jobId, entry.leaseToken, { signal: entry.controller.signal });
|
|
272
|
+
} catch (error) {
|
|
273
|
+
if (entry.controller.signal.aborted) return;
|
|
274
|
+
entry.cancelled = true;
|
|
275
|
+
entry.controller.abort(error);
|
|
276
|
+
if (entry.sessionId && entry.harness) {
|
|
277
|
+
void entry.harness.rpc('session.cancel', {
|
|
278
|
+
sessionId: entry.sessionId,
|
|
279
|
+
keepInbox: true,
|
|
280
|
+
}, 10_000).catch(() => undefined);
|
|
281
|
+
}
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
try {
|
|
285
|
+
const snapshot = await this.#transport.getJob(jobId, { signal: entry.controller.signal });
|
|
286
|
+
const approval = snapshot?.job?.approval;
|
|
287
|
+
if (approval && (approval.status === 'approved' || approval.status === 'rejected')) {
|
|
288
|
+
entry.approvals.get(approval.id)?.resolve({
|
|
289
|
+
decision: approval.status,
|
|
290
|
+
answer: clean(approval.answer),
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
} catch (error) {
|
|
294
|
+
if (entry.controller.signal.aborted) return;
|
|
295
|
+
this.#logger.warn?.(
|
|
296
|
+
`[dsh-im:office] Job ${jobId} approval poll failed; will retry after the next renewal:`,
|
|
297
|
+
error.message,
|
|
298
|
+
);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
async #handleUpdate(jobId, entry, update) {
|
|
304
|
+
const message = update?.type === 'tool' ? t('正在使用 {tool}…', { tool: clean(update.name, 160) || t('Harness 工具') })
|
|
305
|
+
: clean(update?.text, 4_000);
|
|
306
|
+
if (!message) return;
|
|
307
|
+
const key = `${update.type}:${message}`;
|
|
308
|
+
if (key === entry.lastProgress) return;
|
|
309
|
+
const now = Date.now();
|
|
310
|
+
if (update.type === 'text' && now - entry.lastProgressAt < 1_000) return;
|
|
311
|
+
entry.lastProgress = key;
|
|
312
|
+
entry.lastProgressAt = now;
|
|
313
|
+
await this.#progress(jobId, entry, {
|
|
314
|
+
kind: update.type === 'tool' ? 'tool' : update.type === 'text' ? 'text' : 'status',
|
|
315
|
+
message,
|
|
316
|
+
...(entry.sessionId ? { sessionId: entry.sessionId } : {}),
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
#progress(jobId, entry, value, required = false) {
|
|
321
|
+
const request = this.#transport.progressJob(jobId, entry.leaseToken, value, {
|
|
322
|
+
signal: entry.controller.signal,
|
|
323
|
+
});
|
|
324
|
+
return required ? request : request.catch((error) => {
|
|
325
|
+
this.#logger.debug?.('[dsh-im:office] ignored a progress delivery failure:', error.message);
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
async #handleInteraction(jobId, entry, interaction) {
|
|
330
|
+
const request = approvalRequest(interaction);
|
|
331
|
+
if (!request) {
|
|
332
|
+
if (interaction?.kind === 'approval' && typeof interaction.respond === 'function') {
|
|
333
|
+
await interaction.respond({
|
|
334
|
+
ok: true,
|
|
335
|
+
value: {
|
|
336
|
+
sessionId: interaction.sessionId,
|
|
337
|
+
approvalId: interaction.payload?.approvalId,
|
|
338
|
+
outcome: 'rejected',
|
|
339
|
+
},
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
344
|
+
const reply = waitForReply(entry, request.id);
|
|
345
|
+
try {
|
|
346
|
+
await this.#transport.requestApproval(jobId, entry.leaseToken, request, {
|
|
347
|
+
signal: entry.controller.signal,
|
|
348
|
+
});
|
|
349
|
+
const decision = await reply;
|
|
350
|
+
if (request.kind === 'approval') {
|
|
351
|
+
await interaction.respond({
|
|
352
|
+
ok: true,
|
|
353
|
+
value: {
|
|
354
|
+
sessionId: interaction.sessionId,
|
|
355
|
+
approvalId: interaction.payload.approvalId,
|
|
356
|
+
outcome: decision.decision === 'approved' ? 'allowed-once' : 'rejected',
|
|
357
|
+
},
|
|
358
|
+
});
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
if (decision.decision !== 'approved') {
|
|
362
|
+
await interaction.respond({
|
|
363
|
+
ok: false,
|
|
364
|
+
error: { code: 'cancelled', message: 'The AI Office user rejected this question.', details: {} },
|
|
365
|
+
});
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
const answers = clean(decision.answer).split(/\r?\n/);
|
|
369
|
+
await interaction.respond({
|
|
370
|
+
ok: true,
|
|
371
|
+
value: {
|
|
372
|
+
sessionId: interaction.sessionId,
|
|
373
|
+
answer: {
|
|
374
|
+
answers: request.questions.map((question, index) => (
|
|
375
|
+
harnessAnswerForQuestion(question, answers[index] || answers[0] || '')
|
|
376
|
+
)),
|
|
377
|
+
},
|
|
378
|
+
},
|
|
379
|
+
});
|
|
380
|
+
} catch (error) {
|
|
381
|
+
entry.approvals.delete(request.id);
|
|
382
|
+
throw error;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
#rememberCompleted(jobId) {
|
|
387
|
+
this.#completed.add(jobId);
|
|
388
|
+
if (this.#completed.size <= COMPLETED_LIMIT) return;
|
|
389
|
+
this.#completed.delete(this.#completed.values().next().value);
|
|
390
|
+
}
|
|
391
|
+
}
|