@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,1619 @@
|
|
|
1
|
+
import { normalizeExternalCardAction } from './external-cards.mjs';
|
|
2
|
+
import { ExclusiveInboundConsumers, normalizeExternalMedia, normalizeOwnTextEcho } from './external-consumer.mjs';
|
|
3
|
+
import { atConnectionStage, createConnectionDiagnostics } from '../shared/connection-error.mjs';
|
|
4
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
5
|
+
import { connectionTestMessage } from '../shared/connection-test.mjs';
|
|
6
|
+
import { publicMessageFailure } from '../shared/message-failure.mjs';
|
|
7
|
+
import { RegistrationManager } from './registration-manager.mjs';
|
|
8
|
+
import {
|
|
9
|
+
CALLBACK_REPAIR_OPERATION,
|
|
10
|
+
CallbackRepairManager,
|
|
11
|
+
} from './repair-manager.mjs';
|
|
12
|
+
import {
|
|
13
|
+
GROUP_MESSAGE_PERMISSION_OPERATION,
|
|
14
|
+
GroupMessagePermissionManager,
|
|
15
|
+
} from './group-message-permission-manager.mjs';
|
|
16
|
+
import { REQUIRED_TENANT_SCOPES } from './plugin-controller.mjs';
|
|
17
|
+
import {
|
|
18
|
+
isFeishuGroupResponseMode,
|
|
19
|
+
normalizeFeishuGroupResponseMode,
|
|
20
|
+
} from './group-response-mode.mjs';
|
|
21
|
+
import {
|
|
22
|
+
DEFAULT_FEISHU_STEP_PUSH_MODE,
|
|
23
|
+
isFeishuStepPushMode,
|
|
24
|
+
normalizeFeishuStepPushMode,
|
|
25
|
+
} from './step-push-mode.mjs';
|
|
26
|
+
import { normalizeFeishuVoiceConfig } from './voice-config.mjs';
|
|
27
|
+
import {
|
|
28
|
+
isSlashPanelConfig,
|
|
29
|
+
normalizeSlashPanelConfig,
|
|
30
|
+
} from './slash-command-panel.mjs';
|
|
31
|
+
|
|
32
|
+
const ACTIVE_REGISTRATION_STATES = new Set([
|
|
33
|
+
'starting', 'qr_ready', 'polling', 'slow_down', 'domain_switched',
|
|
34
|
+
]);
|
|
35
|
+
const MUTABLE_REGISTRATION_STATES = new Set([...ACTIVE_REGISTRATION_STATES, 'saving']);
|
|
36
|
+
const TARGETED_APP_UPDATE_OPERATIONS = new Set([
|
|
37
|
+
CALLBACK_REPAIR_OPERATION,
|
|
38
|
+
GROUP_MESSAGE_PERMISSION_OPERATION,
|
|
39
|
+
]);
|
|
40
|
+
const ALL_VISIBLE_SENDERS = '*';
|
|
41
|
+
const DEFAULT_CALLBACK_PROBE_TIMEOUT_MS = 120_000;
|
|
42
|
+
const MAX_CALLBACK_PROBE_TIMEOUT_MS = 600_000;
|
|
43
|
+
|
|
44
|
+
function idleConnection() {
|
|
45
|
+
return {
|
|
46
|
+
ready: false,
|
|
47
|
+
feishuLongConnectionState: 'idle',
|
|
48
|
+
harnessReachable: false,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function connectionStatus(runtime) {
|
|
53
|
+
return runtime ? runtime.status : idleConnection();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function isConnected(connection) {
|
|
57
|
+
return connection.ready === true
|
|
58
|
+
&& connection.feishuLongConnectionState === 'connected'
|
|
59
|
+
&& connection.harnessReachable === true;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function maskedAppId(appId) {
|
|
63
|
+
return appId.length > 12
|
|
64
|
+
? `${appId.slice(0, 8)}••••${appId.slice(-4)}`
|
|
65
|
+
: 'cli_••••';
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function publicBot(config) {
|
|
69
|
+
return {
|
|
70
|
+
name: config.botName,
|
|
71
|
+
appIdMasked: maskedAppId(config.appId),
|
|
72
|
+
activated: config.activated,
|
|
73
|
+
domain: config.domain,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function botPhase({ connected, error, connection }) {
|
|
78
|
+
if (connected) return 'connected';
|
|
79
|
+
if (error || connection.feishuLongConnectionState === 'failed') return 'error';
|
|
80
|
+
return 'disconnected';
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function makeBotId() {
|
|
84
|
+
return `bot_${randomUUID().replaceAll('-', '')}`;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function makeRegistrationId() {
|
|
88
|
+
return `reg_${randomUUID().replaceAll('-', '')}`;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function secretRefFor(botId) {
|
|
92
|
+
return `DSH_FEISHU_APP_SECRET_${botId.slice(4).toUpperCase()}`;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function configuredBotFingerprint(config) {
|
|
96
|
+
return JSON.stringify({
|
|
97
|
+
id: config.id,
|
|
98
|
+
consumerMode: config.consumerMode ?? 'standalone',
|
|
99
|
+
appId: config.appId,
|
|
100
|
+
secretRef: config.secretRef,
|
|
101
|
+
ownerOpenIds: config.ownerOpenIds,
|
|
102
|
+
domain: config.domain,
|
|
103
|
+
botName: config.botName,
|
|
104
|
+
botOpenId: config.botOpenId,
|
|
105
|
+
activated: config.activated,
|
|
106
|
+
groupResponseMode: normalizeFeishuGroupResponseMode(config.groupResponseMode),
|
|
107
|
+
mentionTopicReply: config.mentionTopicReply !== false,
|
|
108
|
+
stepPush: config.stepPush === true,
|
|
109
|
+
stepPushMode: normalizeFeishuStepPushMode(config.stepPushMode),
|
|
110
|
+
voice: config.voice,
|
|
111
|
+
slashPanel: normalizeSlashPanelConfig(config.slashPanel),
|
|
112
|
+
groupMessagePermissionGranted: config.groupMessagePermissionGranted === true,
|
|
113
|
+
deletionPending: config.deletionPending === true,
|
|
114
|
+
connectedAt: config.connectedAt ?? null,
|
|
115
|
+
createdAt: config.createdAt ?? null,
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function optionalNonEmptyString(value) {
|
|
120
|
+
return typeof value === 'string' && value.trim() ? value.trim() : null;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Multi-account Feishu orchestration. Each bot owns its credential reference,
|
|
125
|
+
* runtime and session store. Config commits are serialized, while unrelated
|
|
126
|
+
* runtime lifecycles may proceed independently.
|
|
127
|
+
*/
|
|
128
|
+
export class MultiBotDshFeishuController {
|
|
129
|
+
#diagnostics;
|
|
130
|
+
#registerApp;
|
|
131
|
+
#verifyApp;
|
|
132
|
+
#credentials;
|
|
133
|
+
#configStore;
|
|
134
|
+
#createRuntime;
|
|
135
|
+
#deleteState;
|
|
136
|
+
#createBotId;
|
|
137
|
+
#createRegistrationId;
|
|
138
|
+
#runtimes = new Map();
|
|
139
|
+
#inboundConsumers = new ExclusiveInboundConsumers();
|
|
140
|
+
#botErrors = new Map();
|
|
141
|
+
#registrations = new Map();
|
|
142
|
+
#activeAppUpdates = new Map();
|
|
143
|
+
#botOwnership = new Map();
|
|
144
|
+
#latestRegistrationId = null;
|
|
145
|
+
#configTransition = Promise.resolve();
|
|
146
|
+
#botTransitions = new Map();
|
|
147
|
+
#revision = 1;
|
|
148
|
+
#callbackProbeTimeoutMs;
|
|
149
|
+
#closed = false;
|
|
150
|
+
|
|
151
|
+
constructor({
|
|
152
|
+
registerApp,
|
|
153
|
+
verifyApp,
|
|
154
|
+
logger = console,
|
|
155
|
+
credentials,
|
|
156
|
+
configStore,
|
|
157
|
+
createRuntime,
|
|
158
|
+
deleteState = async () => {},
|
|
159
|
+
createBotId = makeBotId,
|
|
160
|
+
createRegistrationId = makeRegistrationId,
|
|
161
|
+
callbackProbeTimeoutMs = DEFAULT_CALLBACK_PROBE_TIMEOUT_MS,
|
|
162
|
+
}) {
|
|
163
|
+
if (typeof registerApp !== 'function') throw new Error('registerApp is required');
|
|
164
|
+
if (typeof verifyApp !== 'function') throw new Error('verifyApp is required');
|
|
165
|
+
if (!credentials) throw new Error('credentials service is required');
|
|
166
|
+
if (!configStore || typeof configStore.list !== 'function') {
|
|
167
|
+
throw new Error('multi-bot config store is required');
|
|
168
|
+
}
|
|
169
|
+
if (typeof createRuntime !== 'function') throw new Error('createRuntime is required');
|
|
170
|
+
if (typeof deleteState !== 'function') throw new Error('deleteState must be a function');
|
|
171
|
+
if (!Number.isFinite(callbackProbeTimeoutMs)
|
|
172
|
+
|| callbackProbeTimeoutMs <= 0
|
|
173
|
+
|| callbackProbeTimeoutMs > MAX_CALLBACK_PROBE_TIMEOUT_MS) {
|
|
174
|
+
throw new TypeError('callbackProbeTimeoutMs must be between 1 and 600000ms');
|
|
175
|
+
}
|
|
176
|
+
this.#diagnostics = createConnectionDiagnostics({ channel: 'feishu', logger });
|
|
177
|
+
this.#registerApp = registerApp;
|
|
178
|
+
this.#verifyApp = verifyApp;
|
|
179
|
+
this.#credentials = credentials;
|
|
180
|
+
this.#configStore = configStore;
|
|
181
|
+
this.#createRuntime = createRuntime;
|
|
182
|
+
this.#deleteState = deleteState;
|
|
183
|
+
this.#createBotId = createBotId;
|
|
184
|
+
this.#createRegistrationId = createRegistrationId;
|
|
185
|
+
this.#callbackProbeTimeoutMs = callbackProbeTimeoutMs;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
async initialize() {
|
|
189
|
+
if (this.#closed) return this.status();
|
|
190
|
+
const bots = this.#configStore.list();
|
|
191
|
+
let attempted = false;
|
|
192
|
+
await Promise.allSettled(bots.map((config) => this.#withBotTransition(config.id, async () => {
|
|
193
|
+
const current = connectionStatus(this.#runtimes.get(config.id));
|
|
194
|
+
if (isConnected(current)
|
|
195
|
+
|| current.feishuLongConnectionState === 'connecting'
|
|
196
|
+
|| current.feishuLongConnectionState === 'reconnecting') {
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
attempted = true;
|
|
200
|
+
if (config.deletionPending) {
|
|
201
|
+
this.#botErrors.set(config.id, {
|
|
202
|
+
code: 'deletion_pending',
|
|
203
|
+
message: '机器人正在等待完成本地删除,请重试移除。',
|
|
204
|
+
});
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
let resolved;
|
|
208
|
+
try {
|
|
209
|
+
resolved = await atConnectionStage('credential.read', () => this.#credentials.resolve(config.secretRef), 'credential-store');
|
|
210
|
+
} catch (error) {
|
|
211
|
+
this.#botErrors.set(config.id, this.#diagnostics.report(error, { reuse: true, botId: config.id, publicError: {
|
|
212
|
+
code: 'missing_credentials',
|
|
213
|
+
message: '无法读取机器人凭据,请检查凭据存储。',
|
|
214
|
+
} }).publicError);
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
if (!resolved?.value) {
|
|
218
|
+
this.#botErrors.set(config.id, {
|
|
219
|
+
code: 'missing_credentials',
|
|
220
|
+
message: '机器人凭据缺失,请删除后重新扫码接入。',
|
|
221
|
+
});
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
try {
|
|
225
|
+
await this.#startRuntime(config, resolved.value);
|
|
226
|
+
this.#botErrors.delete(config.id);
|
|
227
|
+
} catch (error) {
|
|
228
|
+
this.#botErrors.set(config.id, this.#diagnostics.report(error, { reuse: true, botId: config.id, stage: 'connection.start', publicError: {
|
|
229
|
+
code: 'connection_failed',
|
|
230
|
+
message: '机器人暂时无法连接飞书,请重试。',
|
|
231
|
+
} }).publicError);
|
|
232
|
+
}
|
|
233
|
+
})));
|
|
234
|
+
if (attempted) this.#touch();
|
|
235
|
+
return this.status();
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
startRegistration() {
|
|
239
|
+
this.#assertOpen();
|
|
240
|
+
const id = this.#createRegistrationId();
|
|
241
|
+
if (typeof id !== 'string' || !/^[A-Za-z0-9_-]{1,128}$/.test(id) || this.#registrations.has(id)) {
|
|
242
|
+
throw new Error('Registration id generator returned an invalid or duplicate id');
|
|
243
|
+
}
|
|
244
|
+
const record = { id, manager: null, botId: null, createdNew: false, cancelled: false };
|
|
245
|
+
record.manager = new RegistrationManager({
|
|
246
|
+
diagnostics: this.#diagnostics,
|
|
247
|
+
registerApp: this.#registerApp,
|
|
248
|
+
onCredentials: (result) => this.#serializeConfig(() => this.#acceptCredentials(record, result)),
|
|
249
|
+
});
|
|
250
|
+
this.#registrations.set(id, record);
|
|
251
|
+
this.#latestRegistrationId = id;
|
|
252
|
+
this.#trimRegistrations();
|
|
253
|
+
record.manager.start({
|
|
254
|
+
source: 'deepseek-harness',
|
|
255
|
+
createOnly: true,
|
|
256
|
+
appPreset: {
|
|
257
|
+
name: '{user} 的北汇星河 AI 助手',
|
|
258
|
+
desc: '连接飞书与 DeepSeek Harness,在聊天中使用企业 AI 助手。',
|
|
259
|
+
},
|
|
260
|
+
addons: {
|
|
261
|
+
preset: false,
|
|
262
|
+
scopes: { tenant: [...REQUIRED_TENANT_SCOPES] },
|
|
263
|
+
events: { items: { tenant: ['im.message.receive_v1'] } },
|
|
264
|
+
callbacks: { items: ['card.action.trigger'] },
|
|
265
|
+
},
|
|
266
|
+
});
|
|
267
|
+
this.#touch();
|
|
268
|
+
return this.registrationStatus(id);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
startCallbackRepair(botId, { actorOpenId, chatId } = {}) {
|
|
272
|
+
this.#assertOpen();
|
|
273
|
+
const target = this.#requireBot(botId);
|
|
274
|
+
if (target.deletionPending) throw new Error('Cannot repair a Feishu bot pending deletion');
|
|
275
|
+
|
|
276
|
+
const activeId = this.#activeAppUpdates.get(botId);
|
|
277
|
+
const active = activeId ? this.#registrations.get(activeId) : null;
|
|
278
|
+
if (active && MUTABLE_REGISTRATION_STATES.has(active.manager.status().state)) {
|
|
279
|
+
if (active.operation !== CALLBACK_REPAIR_OPERATION) {
|
|
280
|
+
throw new Error('Another Feishu app update is already active for this bot');
|
|
281
|
+
}
|
|
282
|
+
return this.registrationStatus(active.id);
|
|
283
|
+
}
|
|
284
|
+
this.#activeAppUpdates.delete(botId);
|
|
285
|
+
|
|
286
|
+
const id = this.#createRegistrationId();
|
|
287
|
+
if (typeof id !== 'string' || !/^[A-Za-z0-9_-]{1,128}$/.test(id) || this.#registrations.has(id)) {
|
|
288
|
+
throw new Error('Registration id generator returned an invalid or duplicate id');
|
|
289
|
+
}
|
|
290
|
+
const record = {
|
|
291
|
+
id,
|
|
292
|
+
operation: CALLBACK_REPAIR_OPERATION,
|
|
293
|
+
manager: null,
|
|
294
|
+
botId,
|
|
295
|
+
createdNew: false,
|
|
296
|
+
cancelled: false,
|
|
297
|
+
remoteCommitted: false,
|
|
298
|
+
processing: null,
|
|
299
|
+
publicError: null,
|
|
300
|
+
stage: 'authorizing',
|
|
301
|
+
target: structuredClone(target),
|
|
302
|
+
targetFingerprint: configuredBotFingerprint(target),
|
|
303
|
+
initiator: {
|
|
304
|
+
actorOpenId: optionalNonEmptyString(actorOpenId),
|
|
305
|
+
chatId: optionalNonEmptyString(chatId),
|
|
306
|
+
},
|
|
307
|
+
};
|
|
308
|
+
record.manager = new CallbackRepairManager({
|
|
309
|
+
diagnostics: this.#diagnostics,
|
|
310
|
+
registerApp: this.#registerApp,
|
|
311
|
+
appId: target.appId,
|
|
312
|
+
domain: target.domain,
|
|
313
|
+
onCredentials: (result) => {
|
|
314
|
+
record.remoteCommitted = true;
|
|
315
|
+
const processing = this.#acceptCallbackRepair(record, result);
|
|
316
|
+
const tracked = processing.finally(() => {
|
|
317
|
+
if (record.processing === tracked) record.processing = null;
|
|
318
|
+
});
|
|
319
|
+
record.processing = tracked;
|
|
320
|
+
return tracked;
|
|
321
|
+
},
|
|
322
|
+
});
|
|
323
|
+
this.#registrations.set(id, record);
|
|
324
|
+
this.#activeAppUpdates.set(botId, id);
|
|
325
|
+
this.#latestRegistrationId = id;
|
|
326
|
+
this.#trimRegistrations();
|
|
327
|
+
record.manager.start();
|
|
328
|
+
this.#touch();
|
|
329
|
+
return this.registrationStatus(id);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
startGroupMessagePermission(botId) {
|
|
333
|
+
this.#assertOpen();
|
|
334
|
+
const target = this.#requireBot(botId);
|
|
335
|
+
if (target.deletionPending) {
|
|
336
|
+
throw new Error('Cannot update permissions for a Feishu bot pending deletion');
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
const activeId = this.#activeAppUpdates.get(botId);
|
|
340
|
+
const active = activeId ? this.#registrations.get(activeId) : null;
|
|
341
|
+
if (active && MUTABLE_REGISTRATION_STATES.has(active.manager.status().state)) {
|
|
342
|
+
if (active.operation !== GROUP_MESSAGE_PERMISSION_OPERATION) {
|
|
343
|
+
throw new Error('Another Feishu app update is already active for this bot');
|
|
344
|
+
}
|
|
345
|
+
return this.registrationStatus(active.id);
|
|
346
|
+
}
|
|
347
|
+
this.#activeAppUpdates.delete(botId);
|
|
348
|
+
|
|
349
|
+
const id = this.#createRegistrationId();
|
|
350
|
+
if (typeof id !== 'string' || !/^[A-Za-z0-9_-]{1,128}$/.test(id) || this.#registrations.has(id)) {
|
|
351
|
+
throw new Error('Registration id generator returned an invalid or duplicate id');
|
|
352
|
+
}
|
|
353
|
+
const record = {
|
|
354
|
+
id,
|
|
355
|
+
operation: GROUP_MESSAGE_PERMISSION_OPERATION,
|
|
356
|
+
manager: null,
|
|
357
|
+
botId,
|
|
358
|
+
createdNew: false,
|
|
359
|
+
cancelled: false,
|
|
360
|
+
remoteCommitted: false,
|
|
361
|
+
processing: null,
|
|
362
|
+
publicError: null,
|
|
363
|
+
stage: 'authorizing',
|
|
364
|
+
target: structuredClone(target),
|
|
365
|
+
targetFingerprint: configuredBotFingerprint(target),
|
|
366
|
+
initiator: { actorOpenId: null, chatId: null },
|
|
367
|
+
};
|
|
368
|
+
record.manager = new GroupMessagePermissionManager({
|
|
369
|
+
diagnostics: this.#diagnostics,
|
|
370
|
+
registerApp: this.#registerApp,
|
|
371
|
+
appId: target.appId,
|
|
372
|
+
domain: target.domain,
|
|
373
|
+
onCredentials: (result) => {
|
|
374
|
+
record.remoteCommitted = true;
|
|
375
|
+
const processing = this.#acceptGroupMessagePermission(record, result);
|
|
376
|
+
const tracked = processing.finally(() => {
|
|
377
|
+
if (record.processing === tracked) record.processing = null;
|
|
378
|
+
});
|
|
379
|
+
record.processing = tracked;
|
|
380
|
+
return tracked;
|
|
381
|
+
},
|
|
382
|
+
});
|
|
383
|
+
this.#registrations.set(id, record);
|
|
384
|
+
this.#activeAppUpdates.set(botId, id);
|
|
385
|
+
this.#latestRegistrationId = id;
|
|
386
|
+
this.#trimRegistrations();
|
|
387
|
+
record.manager.start();
|
|
388
|
+
this.#touch();
|
|
389
|
+
return this.registrationStatus(id);
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
hasRegistration(attemptId) {
|
|
393
|
+
return this.#registrations.has(attemptId);
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
registrationStatus(attemptId) {
|
|
397
|
+
const record = this.#registrations.get(attemptId);
|
|
398
|
+
if (!record) return null;
|
|
399
|
+
return this.#status({ registration: record, selectedBotId: record.botId });
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
async cancelRegistration(attemptId = this.#latestRegistrationId) {
|
|
403
|
+
const record = this.#registrations.get(attemptId);
|
|
404
|
+
if (!record) return this.status();
|
|
405
|
+
const state = record.manager.status().state;
|
|
406
|
+
// Once registerApp has returned, the platform-side update has committed.
|
|
407
|
+
// A repair must finish converging the returned credential and callback
|
|
408
|
+
// probe; cancelling here cannot roll that remote mutation back.
|
|
409
|
+
if (TARGETED_APP_UPDATE_OPERATIONS.has(record.operation) && state === 'saving') {
|
|
410
|
+
return this.registrationStatus(attemptId);
|
|
411
|
+
}
|
|
412
|
+
if (!MUTABLE_REGISTRATION_STATES.has(state)) {
|
|
413
|
+
return this.registrationStatus(attemptId);
|
|
414
|
+
}
|
|
415
|
+
record.cancelled = true;
|
|
416
|
+
record.manager.cancel();
|
|
417
|
+
await this.#serializeConfig(async () => {
|
|
418
|
+
if (record.createdNew && record.botId
|
|
419
|
+
&& this.#botOwnership.get(record.botId) === record.id
|
|
420
|
+
&& this.#configStore.getBot(record.botId)) {
|
|
421
|
+
await this.#withBotTransition(record.botId, () => this.#deleteBot(record.botId));
|
|
422
|
+
}
|
|
423
|
+
});
|
|
424
|
+
this.#touch();
|
|
425
|
+
return this.registrationStatus(attemptId) ?? this.status();
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
get diagnostics() { return this.#diagnostics; }
|
|
429
|
+
|
|
430
|
+
status(botId) {
|
|
431
|
+
return this.#status({
|
|
432
|
+
registration: this.#registrations.get(this.#latestRegistrationId) ?? null,
|
|
433
|
+
selectedBotId: botId,
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
async bindCredentials({ appId, appSecret, domain = 'feishu' } = {}) {
|
|
438
|
+
this.#assertOpen();
|
|
439
|
+
const normalizedAppId = typeof appId === 'string' ? appId.trim() : '';
|
|
440
|
+
const normalizedSecret = typeof appSecret === 'string' ? appSecret.trim() : '';
|
|
441
|
+
const normalizedDomain = domain === 'lark' ? 'lark' : 'feishu';
|
|
442
|
+
if (!normalizedAppId || !normalizedSecret) {
|
|
443
|
+
throw new TypeError('Feishu App ID and App Secret are required');
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
return this.#serializeConfig(async () => {
|
|
447
|
+
this.#assertOpen();
|
|
448
|
+
const bot = await this.#verifyApp({
|
|
449
|
+
appId: normalizedAppId,
|
|
450
|
+
appSecret: normalizedSecret,
|
|
451
|
+
domain: normalizedDomain,
|
|
452
|
+
});
|
|
453
|
+
this.#assertOpen();
|
|
454
|
+
const existing = this.#configStore.list().find(
|
|
455
|
+
(candidate) => candidate.appId === normalizedAppId,
|
|
456
|
+
);
|
|
457
|
+
const botId = existing?.id ?? this.#createBotId();
|
|
458
|
+
if (typeof botId !== 'string' || !/^[A-Za-z0-9_-]{1,128}$/.test(botId)
|
|
459
|
+
|| (!existing && this.#configStore.getBot(botId))) {
|
|
460
|
+
throw new Error('Bot id generator returned an invalid or duplicate id');
|
|
461
|
+
}
|
|
462
|
+
const secretRef = existing?.secretRef ?? secretRefFor(botId);
|
|
463
|
+
const previousSecret = await atConnectionStage('credential.read', () => this.#credentials.resolve(secretRef), 'credential-store').catch(() => undefined);
|
|
464
|
+
const config = {
|
|
465
|
+
...existing,
|
|
466
|
+
id: botId,
|
|
467
|
+
appId: normalizedAppId,
|
|
468
|
+
secretRef,
|
|
469
|
+
ownerOpenIds: existing?.ownerOpenIds?.length
|
|
470
|
+
? existing.ownerOpenIds
|
|
471
|
+
: [ALL_VISIBLE_SENDERS],
|
|
472
|
+
domain: normalizedDomain,
|
|
473
|
+
botName: bot.name,
|
|
474
|
+
botOpenId: bot.openId,
|
|
475
|
+
activated: bot.activated,
|
|
476
|
+
stepPush: existing?.stepPush ?? true,
|
|
477
|
+
stepPushMode: existing?.stepPushMode ?? DEFAULT_FEISHU_STEP_PUSH_MODE,
|
|
478
|
+
deletionPending: false,
|
|
479
|
+
connectedAt: new Date().toISOString(),
|
|
480
|
+
createdAt: existing?.createdAt ?? new Date().toISOString(),
|
|
481
|
+
};
|
|
482
|
+
|
|
483
|
+
await atConnectionStage('credential.save', () => this.#credentials.set(secretRef, normalizedSecret), 'credential-store');
|
|
484
|
+
let saved;
|
|
485
|
+
try {
|
|
486
|
+
saved = await this.#configStore.saveBot(config);
|
|
487
|
+
} catch (error) {
|
|
488
|
+
await this.#restoreCredential(secretRef, previousSecret);
|
|
489
|
+
throw error;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
await this.#withBotTransition(botId, async () => {
|
|
493
|
+
try {
|
|
494
|
+
await this.#startRuntime(saved, normalizedSecret);
|
|
495
|
+
this.#botErrors.delete(botId);
|
|
496
|
+
} catch (error) {
|
|
497
|
+
this.#botErrors.set(botId, this.#diagnostics.report(error, { reuse: true, botId: botId, publicError: {
|
|
498
|
+
code: 'connection_failed',
|
|
499
|
+
message: '机器人已经绑定,但长连接未就绪,请点击重试。',
|
|
500
|
+
} }).publicError);
|
|
501
|
+
}
|
|
502
|
+
});
|
|
503
|
+
this.#touch();
|
|
504
|
+
return this.status(botId);
|
|
505
|
+
});
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
async reconnectBot(botId) {
|
|
509
|
+
this.#assertOpen();
|
|
510
|
+
return this.#withBotTransition(botId, async () => {
|
|
511
|
+
const config = this.#requireBot(botId);
|
|
512
|
+
if (config.deletionPending) {
|
|
513
|
+
this.#botErrors.set(botId, {
|
|
514
|
+
code: 'deletion_pending',
|
|
515
|
+
message: '机器人正在等待完成本地删除,请重试移除。',
|
|
516
|
+
});
|
|
517
|
+
return this.status(botId);
|
|
518
|
+
}
|
|
519
|
+
if (isConnected(connectionStatus(this.#runtimes.get(botId)))) {
|
|
520
|
+
return this.status(botId);
|
|
521
|
+
}
|
|
522
|
+
let resolved;
|
|
523
|
+
try {
|
|
524
|
+
resolved = await atConnectionStage('credential.read', () => this.#credentials.resolve(config.secretRef), 'credential-store');
|
|
525
|
+
} catch (error) {
|
|
526
|
+
resolved = null;
|
|
527
|
+
}
|
|
528
|
+
if (!resolved?.value) {
|
|
529
|
+
this.#botErrors.set(botId, {
|
|
530
|
+
code: 'missing_credentials',
|
|
531
|
+
message: '机器人凭据缺失,请删除后重新扫码接入。',
|
|
532
|
+
});
|
|
533
|
+
this.#touch();
|
|
534
|
+
return this.status(botId);
|
|
535
|
+
}
|
|
536
|
+
try {
|
|
537
|
+
await this.#startRuntime(config, resolved.value);
|
|
538
|
+
this.#botErrors.delete(botId);
|
|
539
|
+
} catch (error) {
|
|
540
|
+
this.#botErrors.set(botId, this.#diagnostics.report(error, { reuse: true, botId: botId, stage: 'connection.start', publicError: {
|
|
541
|
+
code: 'connection_failed',
|
|
542
|
+
message: '机器人暂时无法连接飞书,请重试。',
|
|
543
|
+
} }).publicError);
|
|
544
|
+
}
|
|
545
|
+
this.#touch();
|
|
546
|
+
return this.status(botId);
|
|
547
|
+
});
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
async sendConnectionTest(botId) {
|
|
551
|
+
this.#assertOpen();
|
|
552
|
+
return this.#withBotTransition(botId, async () => {
|
|
553
|
+
const config = this.#requireBot(botId);
|
|
554
|
+
const runtime = this.#runtimes.get(botId);
|
|
555
|
+
if (!isConnected(connectionStatus(runtime))
|
|
556
|
+
|| typeof runtime.sendConnectionTest !== 'function') {
|
|
557
|
+
const error = new Error('飞书机器人尚未连接');
|
|
558
|
+
error.code = 'test-target-unavailable';
|
|
559
|
+
throw error;
|
|
560
|
+
}
|
|
561
|
+
return runtime.sendConnectionTest(
|
|
562
|
+
connectionTestMessage(
|
|
563
|
+
`${config.botName}(${maskedAppId(config.appId)})`,
|
|
564
|
+
'飞书机器人',
|
|
565
|
+
),
|
|
566
|
+
);
|
|
567
|
+
});
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
async #deliveryAccount(config) {
|
|
571
|
+
const resolved = await this.#credentials.resolve(config.secretRef);
|
|
572
|
+
if (!resolved?.value) {
|
|
573
|
+
const error = new Error('Account credentials unavailable');
|
|
574
|
+
error.code = 'account-unverified';
|
|
575
|
+
throw error;
|
|
576
|
+
}
|
|
577
|
+
const verified = await this.#verifyApp({ appId: config.appId, appSecret: resolved.value, domain: config.domain });
|
|
578
|
+
if (!verified?.openId || verified.openId !== config.botOpenId) {
|
|
579
|
+
const error = new Error('Authenticated account identity changed');
|
|
580
|
+
error.code = 'account-changed';
|
|
581
|
+
throw error;
|
|
582
|
+
}
|
|
583
|
+
const fingerprint = createHash('sha256').update(JSON.stringify({
|
|
584
|
+
provider: 'feishu', domain: config.domain, appId: config.appId, botOpenId: verified.openId,
|
|
585
|
+
})).digest('hex');
|
|
586
|
+
return { fingerprint, ...(verified.name ? { name: verified.name } : {}) };
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
async describeDeliveryAccount(botId) {
|
|
590
|
+
this.#assertOpen();
|
|
591
|
+
return this.#withBotTransition(botId, async () => {
|
|
592
|
+
this.#assertOpen();
|
|
593
|
+
const config = this.#requireBot(botId);
|
|
594
|
+
const account = await this.#deliveryAccount(config);
|
|
595
|
+
return { version: 1, botId, channel: 'feishu', account,
|
|
596
|
+
connected: isConnected(connectionStatus(this.#runtimes.get(botId))),
|
|
597
|
+
capabilities: ['proactive-text-checked', 'proactive-receipt-checked', 'own-text-echo', 'exclusive-text-consumer', 'reply-text-checked', 'reply-context-checked', 'reply-receipt-checked', 'reply-fence-checked', 'history-text-checked', 'thread-history-text-checked', 'source-file-checked', 'source-image-checked', 'reply-file-checked', 'approval-card-checked', 'approval-card-update-checked', 'approval-action-consumer'] };
|
|
598
|
+
});
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
async consumeInbound(botId, { expectedFingerprint, onEvent, signal, sourceFiles = false, sourceImages = false, onEcho, onAction } = {}) {
|
|
602
|
+
this.#assertOpen();
|
|
603
|
+
return this.#withBotTransition(botId, async () => {
|
|
604
|
+
this.#assertOpen();
|
|
605
|
+
signal?.throwIfAborted();
|
|
606
|
+
const config = this.#requireBot(botId);
|
|
607
|
+
const account = await this.#deliveryAccount(config);
|
|
608
|
+
if (account.fingerprint !== expectedFingerprint)
|
|
609
|
+
throw Object.assign(new Error('account-changed'), { code: 'account-changed' });
|
|
610
|
+
const dispose = this.#inboundConsumers.register(botId, { fingerprint: expectedFingerprint, onEvent, signal, sourceFiles, sourceImages, onEcho, onAction });
|
|
611
|
+
try {
|
|
612
|
+
const saved = await this.#configStore.saveBot({ ...config, consumerMode: 'external-consumer' });
|
|
613
|
+
const resolved = await this.#credentials.resolve(saved.secretRef);
|
|
614
|
+
if (!resolved?.value) throw Object.assign(new Error('account-unverified'), { code: 'account-unverified' });
|
|
615
|
+
await this.#startRuntime(saved, resolved.value);
|
|
616
|
+
signal?.throwIfAborted();
|
|
617
|
+
this.#touch();
|
|
618
|
+
return dispose;
|
|
619
|
+
} catch (error) {
|
|
620
|
+
dispose();
|
|
621
|
+
throw error;
|
|
622
|
+
}
|
|
623
|
+
});
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
async historyChecked(botId, route, query, { expectedFingerprint, signal } = {}) {
|
|
627
|
+
this.#assertOpen('capability-unavailable');
|
|
628
|
+
return this.#withBotTransition(botId, async () => {
|
|
629
|
+
this.#assertOpen('capability-unavailable');
|
|
630
|
+
signal?.throwIfAborted();
|
|
631
|
+
const config = this.#requireBot(botId);
|
|
632
|
+
const account = await this.#deliveryAccount(config);
|
|
633
|
+
this.#assertOpen('capability-unavailable');
|
|
634
|
+
signal?.throwIfAborted();
|
|
635
|
+
if (account.fingerprint !== expectedFingerprint)
|
|
636
|
+
throw Object.assign(new Error('account-changed'), { code: 'account-changed' });
|
|
637
|
+
const runtime = this.#runtimes.get(botId);
|
|
638
|
+
if (config.consumerMode !== 'external-consumer' || !isConnected(connectionStatus(runtime))
|
|
639
|
+
|| typeof runtime.historyChecked !== 'function')
|
|
640
|
+
throw Object.assign(new Error('capability-unavailable'), { code: 'capability-unavailable' });
|
|
641
|
+
const leaseSignal = this.#inboundConsumers.signalFor(botId, expectedFingerprint);
|
|
642
|
+
const readSignal = signal ? AbortSignal.any([signal, leaseSignal]) : leaseSignal;
|
|
643
|
+
const result = await runtime.historyChecked({ botId, appId: config.appId,
|
|
644
|
+
botOpenId: config.botOpenId, fingerprint: account.fingerprint }, route, query, { signal: readSignal });
|
|
645
|
+
this.#assertOpen('capability-unavailable');
|
|
646
|
+
readSignal.throwIfAborted();
|
|
647
|
+
return result;
|
|
648
|
+
});
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
async qualifyReplyChecked(botId, route, { expectedFingerprint, signal } = {}) {
|
|
652
|
+
this.#assertOpen();
|
|
653
|
+
return this.#withBotTransition(botId, async () => {
|
|
654
|
+
this.#assertOpen();
|
|
655
|
+
signal?.throwIfAborted();
|
|
656
|
+
const config = this.#requireBot(botId);
|
|
657
|
+
const account = await this.#deliveryAccount(config);
|
|
658
|
+
if (account.fingerprint !== expectedFingerprint)
|
|
659
|
+
throw Object.assign(new Error('account-changed'), { code: 'account-changed' });
|
|
660
|
+
const runtime = this.#runtimes.get(botId);
|
|
661
|
+
if (config.consumerMode !== 'external-consumer' || !isConnected(connectionStatus(runtime))
|
|
662
|
+
|| typeof runtime.qualifyReplyChecked !== 'function')
|
|
663
|
+
throw Object.assign(new Error('capability-unavailable'), { code: 'capability-unavailable' });
|
|
664
|
+
return runtime.qualifyReplyChecked(route, { signal });
|
|
665
|
+
});
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
async replyChecked(botId, route, text, { expectedFingerprint, signal, receipt = false, beforeSend } = {}) {
|
|
669
|
+
this.#assertOpen();
|
|
670
|
+
return this.#withBotTransition(botId, async () => {
|
|
671
|
+
this.#assertOpen();
|
|
672
|
+
const config = this.#requireBot(botId);
|
|
673
|
+
const account = await this.#deliveryAccount(config);
|
|
674
|
+
if (account.fingerprint !== expectedFingerprint)
|
|
675
|
+
throw Object.assign(new Error('account-changed'), { code: 'account-changed' });
|
|
676
|
+
signal?.throwIfAborted();
|
|
677
|
+
const runtime = this.#runtimes.get(botId);
|
|
678
|
+
if (config.consumerMode !== 'external-consumer' || !isConnected(connectionStatus(runtime))
|
|
679
|
+
|| typeof runtime.replyChecked !== 'function')
|
|
680
|
+
throw Object.assign(new Error('capability-unavailable'), { code: 'capability-unavailable' });
|
|
681
|
+
return runtime.replyChecked(route, text, { signal, receipt, beforeSend });
|
|
682
|
+
});
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
async approvalCardChecked(botId, route, card, { expectedFingerprint, signal, beforeSend, update = false } = {}) {
|
|
686
|
+
this.#assertOpen();
|
|
687
|
+
return this.#withBotTransition(botId, async () => {
|
|
688
|
+
this.#assertOpen();
|
|
689
|
+
signal?.throwIfAborted();
|
|
690
|
+
const config = this.#requireBot(botId);
|
|
691
|
+
const account = await this.#deliveryAccount(config);
|
|
692
|
+
if (account.fingerprint !== expectedFingerprint) throw Object.assign(new Error('account-changed'), { code: 'account-changed' });
|
|
693
|
+
const runtime = this.#runtimes.get(botId);
|
|
694
|
+
if (config.consumerMode !== 'external-consumer' || !isConnected(connectionStatus(runtime))
|
|
695
|
+
|| typeof runtime.approvalCardChecked !== 'function') throw Object.assign(new Error('capability-unavailable'), { code: 'capability-unavailable' });
|
|
696
|
+
const lease = this.#inboundConsumers.signalFor(botId, expectedFingerprint);
|
|
697
|
+
return runtime.approvalCardChecked({ appId: config.appId, botOpenId: config.botOpenId }, route, card,
|
|
698
|
+
{ signal: signal ? AbortSignal.any([signal, lease]) : lease, beforeSend, update });
|
|
699
|
+
});
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
async externalFileChecked(botId, route, value, { expectedFingerprint, signal, reply = false } = {}) {
|
|
703
|
+
this.#assertOpen();
|
|
704
|
+
return this.#withBotTransition(botId, async () => {
|
|
705
|
+
const config = this.#requireBot(botId);
|
|
706
|
+
const account = await this.#deliveryAccount(config);
|
|
707
|
+
if (account.fingerprint !== expectedFingerprint)
|
|
708
|
+
throw Object.assign(new Error('account-changed'), { code: 'account-changed' });
|
|
709
|
+
signal?.throwIfAborted();
|
|
710
|
+
const runtime = this.#runtimes.get(botId);
|
|
711
|
+
if (config.consumerMode !== 'external-consumer' || !isConnected(connectionStatus(runtime))
|
|
712
|
+
|| typeof runtime.externalFileChecked !== 'function')
|
|
713
|
+
throw Object.assign(new Error('capability-unavailable'), { code: 'capability-unavailable' });
|
|
714
|
+
return runtime.externalFileChecked(route, value, { signal, reply });
|
|
715
|
+
});
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
async sendProactiveText(botId, target, text, options = {}) {
|
|
719
|
+
this.#assertOpen();
|
|
720
|
+
return this.#withBotTransition(botId, async () => {
|
|
721
|
+
this.#assertOpen();
|
|
722
|
+
const config = this.#requireBot(botId);
|
|
723
|
+
if (options.expectedFingerprint !== undefined) {
|
|
724
|
+
const account = await this.#deliveryAccount(config);
|
|
725
|
+
if (account.fingerprint !== options.expectedFingerprint) {
|
|
726
|
+
const error = new Error('Authenticated account identity changed');
|
|
727
|
+
error.code = 'account-changed';
|
|
728
|
+
throw error;
|
|
729
|
+
}
|
|
730
|
+
options.signal?.throwIfAborted();
|
|
731
|
+
}
|
|
732
|
+
const runtime = this.#runtimes.get(botId);
|
|
733
|
+
if (!isConnected(connectionStatus(runtime))
|
|
734
|
+
|| typeof runtime.sendProactiveText !== 'function') {
|
|
735
|
+
const error = new Error('飞书机器人尚未连接');
|
|
736
|
+
error.code = 'bot-not-connected';
|
|
737
|
+
throw error;
|
|
738
|
+
}
|
|
739
|
+
return runtime.sendProactiveText(target, text, options);
|
|
740
|
+
});
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
async disconnectBot(botId) {
|
|
744
|
+
this.#assertOpen();
|
|
745
|
+
// An operational pause only: credentials/config remain durable, so the
|
|
746
|
+
// bot reconnects on the next Host start unless it is explicitly deleted.
|
|
747
|
+
return this.#withBotTransition(botId, async () => {
|
|
748
|
+
this.#requireBot(botId);
|
|
749
|
+
await this.#stopRuntime(botId);
|
|
750
|
+
this.#botErrors.delete(botId);
|
|
751
|
+
this.#touch();
|
|
752
|
+
return this.status(botId);
|
|
753
|
+
});
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
async updateGroupResponseMode(botId, groupResponseMode) {
|
|
757
|
+
this.#assertOpen();
|
|
758
|
+
if (!isFeishuGroupResponseMode(groupResponseMode)) {
|
|
759
|
+
throw new TypeError('Invalid Feishu group response mode');
|
|
760
|
+
}
|
|
761
|
+
return this.#serializeConfig(() => this.#withBotTransition(botId, async () => {
|
|
762
|
+
const config = this.#requireBot(botId);
|
|
763
|
+
if (groupResponseMode === 'all' && config.groupMessagePermissionGranted !== true) {
|
|
764
|
+
const error = new Error('Authorize im:message.group_msg before enabling all group messages');
|
|
765
|
+
error.code = 'group_message_permission_required';
|
|
766
|
+
throw error;
|
|
767
|
+
}
|
|
768
|
+
const saved = await this.#configStore.saveBot({ ...config, groupResponseMode });
|
|
769
|
+
this.#runtimes.get(botId)?.setGroupResponseMode?.(saved.groupResponseMode);
|
|
770
|
+
this.#touch();
|
|
771
|
+
return this.status(botId);
|
|
772
|
+
}));
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
async updateMentionTopicReply(botId, mentionTopicReply) {
|
|
776
|
+
this.#assertOpen();
|
|
777
|
+
if (typeof mentionTopicReply !== 'boolean') {
|
|
778
|
+
throw new TypeError('Invalid Feishu mention topic reply flag');
|
|
779
|
+
}
|
|
780
|
+
return this.#serializeConfig(() => this.#withBotTransition(botId, async () => {
|
|
781
|
+
const config = this.#requireBot(botId);
|
|
782
|
+
const saved = await this.#configStore.saveBot({ ...config, mentionTopicReply });
|
|
783
|
+
this.#runtimes.get(botId)?.setMentionTopicReply?.(saved.mentionTopicReply);
|
|
784
|
+
this.#touch();
|
|
785
|
+
return this.status(botId);
|
|
786
|
+
}));
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
async updateSlashPanel(botId, slashPanel) {
|
|
790
|
+
this.#assertOpen();
|
|
791
|
+
if (!isSlashPanelConfig(slashPanel)) {
|
|
792
|
+
throw new TypeError('Invalid Feishu slash panel config');
|
|
793
|
+
}
|
|
794
|
+
return this.#serializeConfig(() => this.#withBotTransition(botId, async () => {
|
|
795
|
+
const config = this.#requireBot(botId);
|
|
796
|
+
const saved = await this.#configStore.saveBot({ ...config, slashPanel });
|
|
797
|
+
// The panel lives on Feishu's side, so the runtime re-syncs it in the
|
|
798
|
+
// background; this call only records what the panel should be.
|
|
799
|
+
this.#runtimes.get(botId)?.setSlashPanel?.(saved.slashPanel);
|
|
800
|
+
this.#touch();
|
|
801
|
+
return this.status(botId);
|
|
802
|
+
}));
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
async updateStepPush(botId, stepPush) {
|
|
806
|
+
this.#assertOpen();
|
|
807
|
+
if (typeof stepPush !== 'boolean') {
|
|
808
|
+
throw new TypeError('Invalid Feishu step push flag');
|
|
809
|
+
}
|
|
810
|
+
return this.#serializeConfig(() => this.#withBotTransition(botId, async () => {
|
|
811
|
+
const config = this.#requireBot(botId);
|
|
812
|
+
const saved = await this.#configStore.saveBot({ ...config, stepPush });
|
|
813
|
+
this.#runtimes.get(botId)?.setStepPush?.(saved.stepPush);
|
|
814
|
+
this.#touch();
|
|
815
|
+
return this.status(botId);
|
|
816
|
+
}));
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
async updateStepPushMode(botId, stepPushMode) {
|
|
820
|
+
this.#assertOpen();
|
|
821
|
+
if (!isFeishuStepPushMode(stepPushMode)) {
|
|
822
|
+
throw new TypeError('Invalid Feishu step push mode');
|
|
823
|
+
}
|
|
824
|
+
return this.#serializeConfig(() => this.#withBotTransition(botId, async () => {
|
|
825
|
+
const config = this.#requireBot(botId);
|
|
826
|
+
const saved = await this.#configStore.saveBot({ ...config, stepPushMode });
|
|
827
|
+
this.#runtimes.get(botId)?.setStepPushMode?.(saved.stepPushMode);
|
|
828
|
+
this.#touch();
|
|
829
|
+
return this.status(botId);
|
|
830
|
+
}));
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
async updateVoice(botId, voice) {
|
|
834
|
+
this.#assertOpen();
|
|
835
|
+
if (voice !== null && (typeof voice !== 'object' || Array.isArray(voice))) {
|
|
836
|
+
throw new TypeError('Invalid Feishu voice configuration');
|
|
837
|
+
}
|
|
838
|
+
const normalized = normalizeFeishuVoiceConfig(voice);
|
|
839
|
+
return this.#serializeConfig(() => this.#withBotTransition(botId, async () => {
|
|
840
|
+
const config = this.#requireBot(botId);
|
|
841
|
+
const saved = await this.#configStore.saveBot({ ...config, voice: normalized });
|
|
842
|
+
// 语音是可选的渠道能力:凭据缺失时静默降级为禁用,不打断配置保存。
|
|
843
|
+
const secret = normalized
|
|
844
|
+
? await atConnectionStage('credential.read', () => this.#credentials.resolve(normalized.secretRef), 'credential-store').catch(() => null)
|
|
845
|
+
: null;
|
|
846
|
+
this.#runtimes.get(botId)?.setVoice?.({ config: normalized, secret: secret?.value ?? null });
|
|
847
|
+
this.#touch();
|
|
848
|
+
return this.status(botId);
|
|
849
|
+
}));
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
async deleteBot(botId) {
|
|
853
|
+
this.#assertOpen();
|
|
854
|
+
return this.#serializeConfig(() => this.#withBotTransition(botId, async () => {
|
|
855
|
+
this.#requireBot(botId);
|
|
856
|
+
await this.#deleteBot(botId);
|
|
857
|
+
this.#touch();
|
|
858
|
+
return this.status();
|
|
859
|
+
}));
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
// Compatibility methods for the original one-bot browser contract.
|
|
863
|
+
async reconnect() {
|
|
864
|
+
const bot = this.#configStore.list()[0];
|
|
865
|
+
return bot ? this.reconnectBot(bot.id) : this.status();
|
|
866
|
+
}
|
|
867
|
+
|
|
868
|
+
async disconnect() {
|
|
869
|
+
const bot = this.#configStore.list()[0];
|
|
870
|
+
return bot ? this.deleteBot(bot.id) : this.status();
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
async close() {
|
|
874
|
+
if (this.#closed) return;
|
|
875
|
+
this.#closed = true;
|
|
876
|
+
this.#inboundConsumers.close();
|
|
877
|
+
const appUpdateProcessing = [];
|
|
878
|
+
for (const record of this.#registrations.values()) {
|
|
879
|
+
const state = record.manager.status().state;
|
|
880
|
+
if (TARGETED_APP_UPDATE_OPERATIONS.has(record.operation) && state === 'saving') {
|
|
881
|
+
// Stop projecting an in-flight repair, but do not request its local
|
|
882
|
+
// credential rollback after the remote update has committed.
|
|
883
|
+
record.manager.cancel();
|
|
884
|
+
if (record.processing) appUpdateProcessing.push(record.processing);
|
|
885
|
+
} else if (MUTABLE_REGISTRATION_STATES.has(state)) {
|
|
886
|
+
record.cancelled = true;
|
|
887
|
+
record.manager.cancel();
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
await this.#configTransition;
|
|
891
|
+
await Promise.allSettled([...this.#botTransitions.values()]);
|
|
892
|
+
await Promise.allSettled([...this.#runtimes.keys()].map((id) => this.#stopRuntime(id)));
|
|
893
|
+
await Promise.allSettled(appUpdateProcessing);
|
|
894
|
+
// A committed repair can be between SDK completion and its serialized
|
|
895
|
+
// credential/runtime transition when close begins. Waiting for the repair
|
|
896
|
+
// can therefore create a replacement runtime after the first drain. Drain
|
|
897
|
+
// both transition queues again, then stop every runtime created by that
|
|
898
|
+
// late forward-convergence work.
|
|
899
|
+
await this.#configTransition;
|
|
900
|
+
await Promise.allSettled([...this.#botTransitions.values()]);
|
|
901
|
+
await Promise.allSettled([...this.#runtimes.keys()].map((id) => this.#stopRuntime(id)));
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
#status({ registration, selectedBotId } = {}) {
|
|
905
|
+
const bots = this.#configStore.list().map((config) => {
|
|
906
|
+
const connection = connectionStatus(this.#runtimes.get(config.id));
|
|
907
|
+
const connected = isConnected(connection);
|
|
908
|
+
const error = connectionStatus(this.#runtimes.get(config.id)).error ?? this.#botErrors.get(config.id) ?? null;
|
|
909
|
+
return {
|
|
910
|
+
botId: config.id,
|
|
911
|
+
phase: botPhase({ connected, error, connection }),
|
|
912
|
+
connected,
|
|
913
|
+
configured: true,
|
|
914
|
+
groupResponseMode: normalizeFeishuGroupResponseMode(config.groupResponseMode),
|
|
915
|
+
mentionTopicReply: config.mentionTopicReply !== false,
|
|
916
|
+
stepPush: config.stepPush === true,
|
|
917
|
+
stepPushMode: normalizeFeishuStepPushMode(config.stepPushMode),
|
|
918
|
+
voice: normalizeFeishuVoiceConfig(config.voice),
|
|
919
|
+
slashPanel: normalizeSlashPanelConfig(config.slashPanel),
|
|
920
|
+
groupMessagePermissionGranted: config.groupMessagePermissionGranted === true,
|
|
921
|
+
bot: publicBot(config),
|
|
922
|
+
connection,
|
|
923
|
+
lastMessageError: publicMessageFailure(connection.lastMessageError),
|
|
924
|
+
error,
|
|
925
|
+
};
|
|
926
|
+
});
|
|
927
|
+
const registrationSnapshot = registration ? this.#registrationSnapshot(registration) : {
|
|
928
|
+
state: 'idle', attempt: 0, updatedAt: Date.now(),
|
|
929
|
+
};
|
|
930
|
+
const registering = ACTIVE_REGISTRATION_STATES.has(registrationSnapshot.state);
|
|
931
|
+
const connecting = registrationSnapshot.state === 'saving';
|
|
932
|
+
const registrationOwnsProjection = Boolean(registration) && (registering || connecting);
|
|
933
|
+
const selected = bots.find((bot) => bot.botId === selectedBotId)
|
|
934
|
+
?? (registrationOwnsProjection ? null : (bots[0] ?? null));
|
|
935
|
+
const aggregateConnected = bots.some((bot) => bot.connected);
|
|
936
|
+
let phase = selected?.phase ?? 'unconfigured';
|
|
937
|
+
if (registering) phase = 'registering';
|
|
938
|
+
else if (connecting) phase = 'connecting';
|
|
939
|
+
else if (registrationSnapshot.state === 'error' && !selected) phase = 'error';
|
|
940
|
+
return {
|
|
941
|
+
schemaVersion: 2,
|
|
942
|
+
revision: this.#revision,
|
|
943
|
+
phase,
|
|
944
|
+
connected: selected?.connected ?? false,
|
|
945
|
+
configured: bots.length > 0,
|
|
946
|
+
bot: selected?.bot ?? null,
|
|
947
|
+
connection: selected?.connection ?? idleConnection(),
|
|
948
|
+
error: selected?.error ?? registrationSnapshot.error ?? null,
|
|
949
|
+
registration: registrationSnapshot,
|
|
950
|
+
bots,
|
|
951
|
+
totals: {
|
|
952
|
+
configured: bots.length,
|
|
953
|
+
connected: bots.filter((bot) => bot.connected).length,
|
|
954
|
+
},
|
|
955
|
+
anyConnected: aggregateConnected,
|
|
956
|
+
};
|
|
957
|
+
}
|
|
958
|
+
|
|
959
|
+
#registrationSnapshot(record) {
|
|
960
|
+
const snapshot = record.manager.status();
|
|
961
|
+
return {
|
|
962
|
+
...snapshot,
|
|
963
|
+
attempt: record.id,
|
|
964
|
+
...(record.botId ? { botId: record.botId } : {}),
|
|
965
|
+
...(record.operation ? { operation: record.operation } : {}),
|
|
966
|
+
...(record.stage ? { stage: record.stage } : {}),
|
|
967
|
+
...(snapshot.state === 'error' && record.publicError
|
|
968
|
+
? { error: { ...record.publicError } }
|
|
969
|
+
: {}),
|
|
970
|
+
};
|
|
971
|
+
}
|
|
972
|
+
|
|
973
|
+
async #acceptGroupMessagePermission(record, result) {
|
|
974
|
+
const appId = result.client_id;
|
|
975
|
+
const appSecret = result.client_secret;
|
|
976
|
+
const ownerOpenId = optionalNonEmptyString(result.user_info?.open_id);
|
|
977
|
+
const tenantBrand = result.user_info?.tenant_brand;
|
|
978
|
+
const target = record.target;
|
|
979
|
+
|
|
980
|
+
if (record.cancelled) {
|
|
981
|
+
throw this.#callbackRepairError(
|
|
982
|
+
record,
|
|
983
|
+
'abort',
|
|
984
|
+
'Group message permission update was cancelled before local activation.',
|
|
985
|
+
);
|
|
986
|
+
}
|
|
987
|
+
if (appId !== target.appId) {
|
|
988
|
+
throw this.#callbackRepairError(
|
|
989
|
+
record,
|
|
990
|
+
'repair_app_mismatch',
|
|
991
|
+
'Feishu returned credentials for a different app.',
|
|
992
|
+
);
|
|
993
|
+
}
|
|
994
|
+
if (!ownerOpenId) {
|
|
995
|
+
throw this.#callbackRepairError(
|
|
996
|
+
record,
|
|
997
|
+
'repair_owner_missing',
|
|
998
|
+
'Feishu returned no permission operator identity.',
|
|
999
|
+
);
|
|
1000
|
+
}
|
|
1001
|
+
if (tenantBrand !== undefined && tenantBrand !== target.domain) {
|
|
1002
|
+
throw this.#callbackRepairError(
|
|
1003
|
+
record,
|
|
1004
|
+
'repair_domain_mismatch',
|
|
1005
|
+
'Feishu returned credentials for a different account domain.',
|
|
1006
|
+
);
|
|
1007
|
+
}
|
|
1008
|
+
if (!target.ownerOpenIds.includes(ALL_VISIBLE_SENDERS)
|
|
1009
|
+
&& !target.ownerOpenIds.includes(ownerOpenId)) {
|
|
1010
|
+
throw this.#callbackRepairError(
|
|
1011
|
+
record,
|
|
1012
|
+
'repair_owner_mismatch',
|
|
1013
|
+
'The Feishu permission operator is not an owner of this configured bot.',
|
|
1014
|
+
);
|
|
1015
|
+
}
|
|
1016
|
+
|
|
1017
|
+
record.stage = 'verifying_identity';
|
|
1018
|
+
let verified;
|
|
1019
|
+
try {
|
|
1020
|
+
verified = await this.#verifyApp({ appId, appSecret, domain: target.domain });
|
|
1021
|
+
} catch (error) {
|
|
1022
|
+
throw this.#callbackRepairError(
|
|
1023
|
+
record,
|
|
1024
|
+
'repair_credentials_invalid',
|
|
1025
|
+
'Feishu could not verify the updated app credentials.',
|
|
1026
|
+
error,
|
|
1027
|
+
);
|
|
1028
|
+
}
|
|
1029
|
+
if (target.botOpenId && verified?.openId !== target.botOpenId) {
|
|
1030
|
+
throw this.#callbackRepairError(
|
|
1031
|
+
record,
|
|
1032
|
+
'repair_bot_mismatch',
|
|
1033
|
+
'The updated Feishu app belongs to a different bot identity.',
|
|
1034
|
+
);
|
|
1035
|
+
}
|
|
1036
|
+
|
|
1037
|
+
await this.#serializeConfig(() => this.#withBotTransition(record.botId, async () => {
|
|
1038
|
+
const current = this.#configStore.getBot(record.botId);
|
|
1039
|
+
if (!current
|
|
1040
|
+
|| current.deletionPending
|
|
1041
|
+
|| configuredBotFingerprint(current) !== record.targetFingerprint) {
|
|
1042
|
+
throw this.#callbackRepairError(
|
|
1043
|
+
record,
|
|
1044
|
+
'repair_target_changed',
|
|
1045
|
+
'The Feishu bot changed while its permission update was in progress.',
|
|
1046
|
+
);
|
|
1047
|
+
}
|
|
1048
|
+
|
|
1049
|
+
let previous;
|
|
1050
|
+
try {
|
|
1051
|
+
previous = await atConnectionStage('credential.read', () => this.#credentials.resolve(current.secretRef), 'credential-store');
|
|
1052
|
+
} catch (error) {
|
|
1053
|
+
throw this.#callbackRepairError(
|
|
1054
|
+
record,
|
|
1055
|
+
'credential_update_failed',
|
|
1056
|
+
'Unable to read the current Feishu credential.',
|
|
1057
|
+
error,
|
|
1058
|
+
);
|
|
1059
|
+
}
|
|
1060
|
+
if (previous?.value !== appSecret) {
|
|
1061
|
+
record.stage = 'persisting_secret';
|
|
1062
|
+
try {
|
|
1063
|
+
await atConnectionStage('credential.save', () => this.#credentials.set(current.secretRef, appSecret), 'credential-store');
|
|
1064
|
+
} catch (writeError) {
|
|
1065
|
+
const observed = await atConnectionStage('credential.read', () => this.#credentials.resolve(current.secretRef), 'credential-store').catch(() => null);
|
|
1066
|
+
if (observed?.value !== appSecret) {
|
|
1067
|
+
throw this.#callbackRepairError(
|
|
1068
|
+
record,
|
|
1069
|
+
'credential_update_failed',
|
|
1070
|
+
'Unable to store the updated Feishu credential.',
|
|
1071
|
+
writeError,
|
|
1072
|
+
);
|
|
1073
|
+
}
|
|
1074
|
+
}
|
|
1075
|
+
const persisted = await atConnectionStage('credential.read', () => this.#credentials.resolve(current.secretRef), 'credential-store').catch(() => null);
|
|
1076
|
+
if (persisted?.value !== appSecret) {
|
|
1077
|
+
throw this.#callbackRepairError(
|
|
1078
|
+
record,
|
|
1079
|
+
'credential_state_unknown',
|
|
1080
|
+
'The updated Feishu credential could not be confirmed after writing.',
|
|
1081
|
+
);
|
|
1082
|
+
}
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1085
|
+
record.stage = 'enabling_all_messages';
|
|
1086
|
+
let saved;
|
|
1087
|
+
try {
|
|
1088
|
+
saved = await this.#configStore.saveBot({
|
|
1089
|
+
...current,
|
|
1090
|
+
groupMessagePermissionGranted: true,
|
|
1091
|
+
groupResponseMode: 'all',
|
|
1092
|
+
});
|
|
1093
|
+
} catch (error) {
|
|
1094
|
+
throw this.#callbackRepairError(
|
|
1095
|
+
record,
|
|
1096
|
+
'group_message_permission_save_failed',
|
|
1097
|
+
'The permission was accepted, but all-message mode could not be saved.',
|
|
1098
|
+
error,
|
|
1099
|
+
);
|
|
1100
|
+
}
|
|
1101
|
+
|
|
1102
|
+
record.stage = 'restarting';
|
|
1103
|
+
try {
|
|
1104
|
+
await this.#startRuntime(saved, appSecret);
|
|
1105
|
+
} catch (error) {
|
|
1106
|
+
this.#botErrors.set(record.botId, this.#diagnostics.report(error, { reuse: true, botId: record.botId, stage: 'connection.start', publicError: {
|
|
1107
|
+
code: 'connection_failed',
|
|
1108
|
+
message: '群消息权限已开通,但机器人长连接未就绪,请点击重试。',
|
|
1109
|
+
} }).publicError);
|
|
1110
|
+
this.#touch();
|
|
1111
|
+
throw this.#callbackRepairError(
|
|
1112
|
+
record,
|
|
1113
|
+
'group_message_permission_connection_failed',
|
|
1114
|
+
'The permission was accepted, but the Feishu runtime could not restart.',
|
|
1115
|
+
error,
|
|
1116
|
+
);
|
|
1117
|
+
}
|
|
1118
|
+
this.#botErrors.delete(record.botId);
|
|
1119
|
+
record.stage = 'verified';
|
|
1120
|
+
record.publicError = null;
|
|
1121
|
+
this.#touch();
|
|
1122
|
+
}));
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
async #acceptCallbackRepair(record, result) {
|
|
1126
|
+
const appId = result.client_id;
|
|
1127
|
+
const appSecret = result.client_secret;
|
|
1128
|
+
const ownerOpenId = optionalNonEmptyString(result.user_info?.open_id);
|
|
1129
|
+
const tenantBrand = result.user_info?.tenant_brand;
|
|
1130
|
+
const target = record.target;
|
|
1131
|
+
|
|
1132
|
+
if (record.cancelled) {
|
|
1133
|
+
throw this.#callbackRepairError(
|
|
1134
|
+
record,
|
|
1135
|
+
'abort',
|
|
1136
|
+
'Callback repair was cancelled before local activation.',
|
|
1137
|
+
);
|
|
1138
|
+
}
|
|
1139
|
+
if (appId !== target.appId) {
|
|
1140
|
+
throw this.#callbackRepairError(
|
|
1141
|
+
record,
|
|
1142
|
+
'repair_app_mismatch',
|
|
1143
|
+
'Feishu returned credentials for a different app.',
|
|
1144
|
+
);
|
|
1145
|
+
}
|
|
1146
|
+
if (!ownerOpenId) {
|
|
1147
|
+
throw this.#callbackRepairError(
|
|
1148
|
+
record,
|
|
1149
|
+
'repair_owner_missing',
|
|
1150
|
+
'Feishu returned no repair operator identity.',
|
|
1151
|
+
);
|
|
1152
|
+
}
|
|
1153
|
+
if (tenantBrand !== undefined && tenantBrand !== target.domain) {
|
|
1154
|
+
throw this.#callbackRepairError(
|
|
1155
|
+
record,
|
|
1156
|
+
'repair_domain_mismatch',
|
|
1157
|
+
'Feishu returned credentials for a different account domain.',
|
|
1158
|
+
);
|
|
1159
|
+
}
|
|
1160
|
+
record.stage = 'verifying_identity';
|
|
1161
|
+
let verified;
|
|
1162
|
+
try {
|
|
1163
|
+
verified = await this.#verifyApp({
|
|
1164
|
+
appId,
|
|
1165
|
+
appSecret,
|
|
1166
|
+
domain: target.domain,
|
|
1167
|
+
});
|
|
1168
|
+
} catch (error) {
|
|
1169
|
+
throw this.#callbackRepairError(
|
|
1170
|
+
record,
|
|
1171
|
+
'repair_credentials_invalid',
|
|
1172
|
+
'Feishu could not verify the repaired app credentials.',
|
|
1173
|
+
error,
|
|
1174
|
+
);
|
|
1175
|
+
}
|
|
1176
|
+
if (target.botOpenId && verified?.openId !== target.botOpenId) {
|
|
1177
|
+
throw this.#callbackRepairError(
|
|
1178
|
+
record,
|
|
1179
|
+
'repair_bot_mismatch',
|
|
1180
|
+
'The repaired Feishu app belongs to a different bot identity.',
|
|
1181
|
+
);
|
|
1182
|
+
}
|
|
1183
|
+
|
|
1184
|
+
const runtime = await this.#serializeConfig(() => this.#withBotTransition(
|
|
1185
|
+
record.botId,
|
|
1186
|
+
async () => {
|
|
1187
|
+
const current = this.#configStore.getBot(record.botId);
|
|
1188
|
+
if (!current
|
|
1189
|
+
|| current.deletionPending
|
|
1190
|
+
|| configuredBotFingerprint(current) !== record.targetFingerprint) {
|
|
1191
|
+
throw this.#callbackRepairError(
|
|
1192
|
+
record,
|
|
1193
|
+
'repair_target_changed',
|
|
1194
|
+
'The Feishu bot changed while its callback repair was in progress.',
|
|
1195
|
+
);
|
|
1196
|
+
}
|
|
1197
|
+
|
|
1198
|
+
let previous;
|
|
1199
|
+
try {
|
|
1200
|
+
previous = await atConnectionStage('credential.read', () => this.#credentials.resolve(current.secretRef), 'credential-store');
|
|
1201
|
+
} catch (error) {
|
|
1202
|
+
throw this.#callbackRepairError(
|
|
1203
|
+
record,
|
|
1204
|
+
'credential_update_failed',
|
|
1205
|
+
'Unable to read the current Feishu credential.',
|
|
1206
|
+
error,
|
|
1207
|
+
);
|
|
1208
|
+
}
|
|
1209
|
+
const credentialChanged = previous?.value !== appSecret;
|
|
1210
|
+
if (credentialChanged) {
|
|
1211
|
+
record.stage = 'persisting_secret';
|
|
1212
|
+
try {
|
|
1213
|
+
await atConnectionStage('credential.save', () => this.#credentials.set(current.secretRef, appSecret), 'credential-store');
|
|
1214
|
+
} catch (writeError) {
|
|
1215
|
+
const observed = await atConnectionStage('credential.read', () => this.#credentials.resolve(current.secretRef), 'credential-store').catch(() => null);
|
|
1216
|
+
if (observed?.value !== appSecret) {
|
|
1217
|
+
throw this.#callbackRepairError(
|
|
1218
|
+
record,
|
|
1219
|
+
'credential_update_failed',
|
|
1220
|
+
'Unable to store the repaired Feishu credential.',
|
|
1221
|
+
writeError,
|
|
1222
|
+
);
|
|
1223
|
+
}
|
|
1224
|
+
}
|
|
1225
|
+
const persisted = await atConnectionStage('credential.read', () => this.#credentials.resolve(current.secretRef), 'credential-store').catch(() => null);
|
|
1226
|
+
if (persisted?.value !== appSecret) {
|
|
1227
|
+
throw this.#callbackRepairError(
|
|
1228
|
+
record,
|
|
1229
|
+
'credential_state_unknown',
|
|
1230
|
+
'The repaired Feishu credential could not be confirmed after writing.',
|
|
1231
|
+
);
|
|
1232
|
+
}
|
|
1233
|
+
}
|
|
1234
|
+
|
|
1235
|
+
let currentRuntime;
|
|
1236
|
+
// Callback subscriptions are delivered over the long connection.
|
|
1237
|
+
// Always replace it after the platform-side callback update commits,
|
|
1238
|
+
// even when registerApp returns the same secret and the old socket
|
|
1239
|
+
// still reports healthy, so the probe never runs on stale metadata.
|
|
1240
|
+
record.stage = 'restarting';
|
|
1241
|
+
try {
|
|
1242
|
+
await this.#startRuntime(current, appSecret);
|
|
1243
|
+
currentRuntime = this.#runtimes.get(record.botId);
|
|
1244
|
+
} catch (error) {
|
|
1245
|
+
// The returned credential was already verified and persisted. Do
|
|
1246
|
+
// not restore a potentially revoked old secret; reconnectBot can
|
|
1247
|
+
// safely retry this forward state later.
|
|
1248
|
+
this.#botErrors.set(record.botId, this.#diagnostics.report(error, { reuse: true, botId: record.botId, publicError: {
|
|
1249
|
+
code: 'connection_failed',
|
|
1250
|
+
message: '机器人回调修复已保存,但长连接未就绪,请点击重试。',
|
|
1251
|
+
} }).publicError);
|
|
1252
|
+
this.#touch();
|
|
1253
|
+
throw this.#callbackRepairError(
|
|
1254
|
+
record,
|
|
1255
|
+
'repair_connection_failed',
|
|
1256
|
+
'The repaired Feishu runtime could not be started.',
|
|
1257
|
+
error,
|
|
1258
|
+
);
|
|
1259
|
+
}
|
|
1260
|
+
if (!currentRuntime) {
|
|
1261
|
+
throw this.#callbackRepairError(
|
|
1262
|
+
record,
|
|
1263
|
+
'repair_connection_failed',
|
|
1264
|
+
'The repaired Feishu runtime is unavailable.',
|
|
1265
|
+
);
|
|
1266
|
+
}
|
|
1267
|
+
this.#botErrors.delete(record.botId);
|
|
1268
|
+
this.#touch();
|
|
1269
|
+
return currentRuntime;
|
|
1270
|
+
},
|
|
1271
|
+
));
|
|
1272
|
+
|
|
1273
|
+
if (typeof runtime.beginCardActionProbe !== 'function') {
|
|
1274
|
+
throw this.#callbackRepairError(
|
|
1275
|
+
record,
|
|
1276
|
+
'card_action_probe_unavailable',
|
|
1277
|
+
'The Feishu runtime cannot verify card callbacks.',
|
|
1278
|
+
);
|
|
1279
|
+
}
|
|
1280
|
+
record.stage = 'awaiting_callback';
|
|
1281
|
+
try {
|
|
1282
|
+
const proof = await runtime.beginCardActionProbe({
|
|
1283
|
+
expectedOperatorOpenId: record.initiator.actorOpenId ?? ownerOpenId,
|
|
1284
|
+
timeoutMs: this.#callbackProbeTimeoutMs,
|
|
1285
|
+
...(record.initiator.chatId ? { chatId: record.initiator.chatId } : {}),
|
|
1286
|
+
});
|
|
1287
|
+
if (proof?.verified !== true) {
|
|
1288
|
+
const error = new Error('Feishu runtime returned no callback proof');
|
|
1289
|
+
error.code = 'card_action_probe_failed';
|
|
1290
|
+
throw error;
|
|
1291
|
+
}
|
|
1292
|
+
} catch (error) {
|
|
1293
|
+
const code = error?.code === 'card_action_probe_timeout'
|
|
1294
|
+
? 'card_action_probe_timeout'
|
|
1295
|
+
: error?.code === 'card_action_probe_unavailable'
|
|
1296
|
+
? 'card_action_probe_unavailable'
|
|
1297
|
+
: error?.code === 'card_action_probe_send_failed'
|
|
1298
|
+
? 'card_action_probe_send_failed'
|
|
1299
|
+
: 'card_action_probe_failed';
|
|
1300
|
+
throw this.#callbackRepairError(
|
|
1301
|
+
record,
|
|
1302
|
+
code,
|
|
1303
|
+
code === 'card_action_probe_timeout'
|
|
1304
|
+
? 'Timed out waiting for the Feishu callback verification button.'
|
|
1305
|
+
: 'The Feishu card callback probe failed.',
|
|
1306
|
+
error,
|
|
1307
|
+
);
|
|
1308
|
+
}
|
|
1309
|
+
record.stage = 'verified';
|
|
1310
|
+
record.publicError = null;
|
|
1311
|
+
this.#touch();
|
|
1312
|
+
}
|
|
1313
|
+
|
|
1314
|
+
#callbackRepairError(record, code, message, cause) {
|
|
1315
|
+
record.publicError = { code, message };
|
|
1316
|
+
const error = new Error(message, cause ? { cause } : undefined);
|
|
1317
|
+
error.code = code;
|
|
1318
|
+
return error;
|
|
1319
|
+
}
|
|
1320
|
+
|
|
1321
|
+
async #acceptCredentials(record, result) {
|
|
1322
|
+
if (record.cancelled) throw new Error('Registration was cancelled');
|
|
1323
|
+
const appId = result.client_id;
|
|
1324
|
+
const appSecret = result.client_secret;
|
|
1325
|
+
const ownerOpenId = result.user_info?.open_id;
|
|
1326
|
+
const domain = result.user_info?.tenant_brand === 'lark' ? 'lark' : 'feishu';
|
|
1327
|
+
if (!ownerOpenId) throw new Error('Feishu registration returned no owner open_id');
|
|
1328
|
+
|
|
1329
|
+
const bot = await this.#verifyApp({ appId, appSecret, domain });
|
|
1330
|
+
if (record.cancelled) throw new Error('Registration was cancelled');
|
|
1331
|
+
const existing = this.#configStore.list().find((candidate) => candidate.appId === appId);
|
|
1332
|
+
const botId = existing?.id ?? this.#createBotId();
|
|
1333
|
+
if (typeof botId !== 'string' || !/^[A-Za-z0-9_-]{1,128}$/.test(botId)
|
|
1334
|
+
|| (!existing && this.#configStore.getBot(botId))) {
|
|
1335
|
+
throw new Error('Bot id generator returned an invalid or duplicate id');
|
|
1336
|
+
}
|
|
1337
|
+
const secretRef = existing?.secretRef ?? secretRefFor(botId);
|
|
1338
|
+
const previousOwnership = this.#botOwnership.get(botId);
|
|
1339
|
+
const previousSecret = await atConnectionStage('credential.read', () => this.#credentials.resolve(secretRef), 'credential-store').catch(() => undefined);
|
|
1340
|
+
await atConnectionStage('credential.save', () => this.#credentials.set(secretRef, appSecret), 'credential-store');
|
|
1341
|
+
let config;
|
|
1342
|
+
try {
|
|
1343
|
+
config = await this.#configStore.saveBot({
|
|
1344
|
+
...existing,
|
|
1345
|
+
id: botId,
|
|
1346
|
+
appId,
|
|
1347
|
+
secretRef,
|
|
1348
|
+
ownerOpenIds: [...new Set([...(existing?.ownerOpenIds ?? []), ownerOpenId])],
|
|
1349
|
+
domain,
|
|
1350
|
+
botName: bot.name,
|
|
1351
|
+
botOpenId: bot.openId,
|
|
1352
|
+
activated: bot.activated,
|
|
1353
|
+
// New connections start with the process-card presentation; existing
|
|
1354
|
+
// bots keep whatever they saved before (the spread above re-applies
|
|
1355
|
+
// their stored values, undefined falls through to the defaults here).
|
|
1356
|
+
stepPush: existing?.stepPush ?? true,
|
|
1357
|
+
stepPushMode: existing?.stepPushMode ?? DEFAULT_FEISHU_STEP_PUSH_MODE,
|
|
1358
|
+
deletionPending: false,
|
|
1359
|
+
connectedAt: new Date().toISOString(),
|
|
1360
|
+
createdAt: existing?.createdAt ?? new Date().toISOString(),
|
|
1361
|
+
});
|
|
1362
|
+
record.botId = botId;
|
|
1363
|
+
record.createdNew = !existing;
|
|
1364
|
+
this.#botOwnership.set(botId, record.id);
|
|
1365
|
+
} catch (error) {
|
|
1366
|
+
try {
|
|
1367
|
+
await this.#restoreCredential(secretRef, previousSecret);
|
|
1368
|
+
} catch (restoreError) {
|
|
1369
|
+
throw new Error('Unable to restore the Feishu credential after a config failure.', {
|
|
1370
|
+
cause: restoreError,
|
|
1371
|
+
});
|
|
1372
|
+
}
|
|
1373
|
+
throw error;
|
|
1374
|
+
}
|
|
1375
|
+
|
|
1376
|
+
if (record.cancelled) {
|
|
1377
|
+
if (record.createdNew) {
|
|
1378
|
+
await this.#withBotTransition(botId, () => this.#deleteBot(botId));
|
|
1379
|
+
} else {
|
|
1380
|
+
await this.#configStore.saveBot(existing);
|
|
1381
|
+
await this.#restoreCredential(secretRef, previousSecret);
|
|
1382
|
+
if (previousOwnership) this.#botOwnership.set(botId, previousOwnership);
|
|
1383
|
+
else this.#botOwnership.delete(botId);
|
|
1384
|
+
}
|
|
1385
|
+
throw new Error('Registration was cancelled');
|
|
1386
|
+
}
|
|
1387
|
+
let cancellationRolledBack = false;
|
|
1388
|
+
try {
|
|
1389
|
+
await this.#withBotTransition(botId, () => this.#startRuntime(config, appSecret));
|
|
1390
|
+
if (record.cancelled) {
|
|
1391
|
+
if (record.createdNew) {
|
|
1392
|
+
await this.#withBotTransition(botId, () => this.#deleteBot(botId));
|
|
1393
|
+
} else {
|
|
1394
|
+
await this.#configStore.saveBot(existing);
|
|
1395
|
+
await this.#restoreCredential(secretRef, previousSecret);
|
|
1396
|
+
if (previousOwnership) this.#botOwnership.set(botId, previousOwnership);
|
|
1397
|
+
else this.#botOwnership.delete(botId);
|
|
1398
|
+
if (previousSecret?.value && !existing.deletionPending) {
|
|
1399
|
+
await this.#withBotTransition(botId, () => this.#startRuntime(existing, previousSecret.value));
|
|
1400
|
+
} else {
|
|
1401
|
+
await this.#withBotTransition(botId, () => this.#stopRuntime(botId));
|
|
1402
|
+
}
|
|
1403
|
+
}
|
|
1404
|
+
cancellationRolledBack = true;
|
|
1405
|
+
throw new Error('Registration was cancelled');
|
|
1406
|
+
}
|
|
1407
|
+
this.#botErrors.delete(botId);
|
|
1408
|
+
this.#touch();
|
|
1409
|
+
} catch (error) {
|
|
1410
|
+
if (record.cancelled) {
|
|
1411
|
+
if (!cancellationRolledBack && record.createdNew && this.#configStore.getBot(botId)) {
|
|
1412
|
+
await this.#withBotTransition(botId, () => this.#deleteBot(botId));
|
|
1413
|
+
} else if (!cancellationRolledBack && existing) {
|
|
1414
|
+
await this.#configStore.saveBot(existing);
|
|
1415
|
+
await this.#restoreCredential(secretRef, previousSecret);
|
|
1416
|
+
if (previousOwnership) this.#botOwnership.set(botId, previousOwnership);
|
|
1417
|
+
else this.#botOwnership.delete(botId);
|
|
1418
|
+
if (!this.#closed && previousSecret?.value && !existing.deletionPending) {
|
|
1419
|
+
await this.#withBotTransition(botId, () => this.#startRuntime(existing, previousSecret.value));
|
|
1420
|
+
} else {
|
|
1421
|
+
await this.#withBotTransition(botId, () => this.#stopRuntime(botId));
|
|
1422
|
+
}
|
|
1423
|
+
}
|
|
1424
|
+
this.#touch();
|
|
1425
|
+
throw error;
|
|
1426
|
+
}
|
|
1427
|
+
if (existing && previousSecret?.value) {
|
|
1428
|
+
try {
|
|
1429
|
+
await this.#configStore.saveBot(existing);
|
|
1430
|
+
await this.#restoreCredential(secretRef, previousSecret);
|
|
1431
|
+
if (previousOwnership) this.#botOwnership.set(botId, previousOwnership);
|
|
1432
|
+
else this.#botOwnership.delete(botId);
|
|
1433
|
+
if (!existing.deletionPending) {
|
|
1434
|
+
await this.#withBotTransition(botId, () => this.#startRuntime(existing, previousSecret.value));
|
|
1435
|
+
this.#botErrors.delete(botId);
|
|
1436
|
+
} else {
|
|
1437
|
+
await this.#withBotTransition(botId, () => this.#stopRuntime(botId));
|
|
1438
|
+
this.#botErrors.set(botId, this.#diagnostics.report(error, { reuse: true, botId: botId, publicError: {
|
|
1439
|
+
code: 'deletion_pending',
|
|
1440
|
+
message: '机器人正在等待完成本地删除,请重试移除。',
|
|
1441
|
+
} }).publicError);
|
|
1442
|
+
}
|
|
1443
|
+
this.#touch();
|
|
1444
|
+
throw error;
|
|
1445
|
+
} catch (restoreError) {
|
|
1446
|
+
if (restoreError === error) throw error;
|
|
1447
|
+
this.#botErrors.set(botId, this.#diagnostics.report(restoreError, { reuse: true, botId: botId, publicError: {
|
|
1448
|
+
code: 'connection_failed',
|
|
1449
|
+
message: '机器人连接更新失败,且原连接无法恢复,请重试。',
|
|
1450
|
+
} }).publicError);
|
|
1451
|
+
this.#touch();
|
|
1452
|
+
throw new Error('Unable to restore the previous Feishu bot connection.', {
|
|
1453
|
+
cause: restoreError,
|
|
1454
|
+
});
|
|
1455
|
+
}
|
|
1456
|
+
}
|
|
1457
|
+
this.#botErrors.set(botId, this.#diagnostics.report(error, { reuse: true, botId: botId, publicError: {
|
|
1458
|
+
code: 'connection_failed',
|
|
1459
|
+
message: '机器人已经创建,但长连接未就绪,请点击重试。',
|
|
1460
|
+
} }).publicError);
|
|
1461
|
+
this.#touch();
|
|
1462
|
+
throw error;
|
|
1463
|
+
}
|
|
1464
|
+
}
|
|
1465
|
+
|
|
1466
|
+
async #startRuntime(config, appSecret) {
|
|
1467
|
+
await this.#stopRuntime(config.id);
|
|
1468
|
+
let runtime;
|
|
1469
|
+
runtime = await atConnectionStage('runtime.prepare', () => this.#createRuntime({
|
|
1470
|
+
botId: config.id,
|
|
1471
|
+
config,
|
|
1472
|
+
appSecret,
|
|
1473
|
+
repair: this.#runtimeRepairCapability(config.id),
|
|
1474
|
+
acceptExternalAction: (event, { signal } = {}) => this.#withBotTransition(config.id, async () => {
|
|
1475
|
+
this.#assertOpen();
|
|
1476
|
+
signal?.throwIfAborted();
|
|
1477
|
+
if (this.#runtimes.get(config.id) !== runtime) throw Object.assign(new Error('consumer-unavailable'), { code: 'consumer-unavailable' });
|
|
1478
|
+
const current = this.#requireBot(config.id);
|
|
1479
|
+
if (current.consumerMode !== 'external-consumer' || configuredBotFingerprint(current) !== configuredBotFingerprint(config))
|
|
1480
|
+
throw Object.assign(new Error('account-changed'), { code: 'account-changed' });
|
|
1481
|
+
const account = await this.#deliveryAccount(current);
|
|
1482
|
+
return normalizeExternalCardAction(event, { botId: current.id, appId: current.appId, fingerprint: account.fingerprint });
|
|
1483
|
+
}).then(evidence => evidence ? this.#inboundConsumers.acceptAction(config.id, evidence, signal)
|
|
1484
|
+
: { toast: { type: 'error', content: 'Unknown control / 未识别的按钮' } }),
|
|
1485
|
+
acceptExternal: (event, { signal } = {}) => this.#withBotTransition(config.id, async () => {
|
|
1486
|
+
signal?.throwIfAborted();
|
|
1487
|
+
if (this.#runtimes.get(config.id) !== runtime) throw Object.assign(new Error('consumer-unavailable'), { code: 'consumer-unavailable' });
|
|
1488
|
+
const current = this.#requireBot(config.id);
|
|
1489
|
+
if (current.consumerMode !== 'external-consumer'
|
|
1490
|
+
|| configuredBotFingerprint(current) !== configuredBotFingerprint(config))
|
|
1491
|
+
throw Object.assign(new Error('account-changed'), { code: 'account-changed' });
|
|
1492
|
+
const account = await this.#deliveryAccount(current);
|
|
1493
|
+
signal?.throwIfAborted();
|
|
1494
|
+
const echo = normalizeOwnTextEcho(event, { botId: current.id, appId: current.appId, botOpenId: current.botOpenId, fingerprint: account.fingerprint });
|
|
1495
|
+
if (echo) return this.#inboundConsumers.accept(config.id, echo, signal, true);
|
|
1496
|
+
const evidence = normalizeExternalMedia(event, { botId: current.id, appId: current.appId, botOpenId: current.botOpenId, fingerprint: account.fingerprint });
|
|
1497
|
+
if (evidence === null) return { accepted: true, ignored: true };
|
|
1498
|
+
if (evidence.attachments?.length && !this.#inboundConsumers.acceptsImages(config.id)) return { accepted: true, ignored: true };
|
|
1499
|
+
const named = typeof runtime.enrichExternalNames === 'function' ? await runtime.enrichExternalNames(evidence, { signal }) : evidence;
|
|
1500
|
+
const enriched = this.#inboundConsumers.acceptsFiles(config.id) && typeof runtime.enrichExternal === 'function' ? await runtime.enrichExternal(named, { signal }) : named;
|
|
1501
|
+
return this.#inboundConsumers.accept(config.id, enriched, signal);
|
|
1502
|
+
}),
|
|
1503
|
+
}));
|
|
1504
|
+
this.#runtimes.set(config.id, runtime);
|
|
1505
|
+
try {
|
|
1506
|
+
await runtime.start();
|
|
1507
|
+
} catch (error) {
|
|
1508
|
+
if (this.#runtimes.get(config.id) === runtime) this.#runtimes.delete(config.id);
|
|
1509
|
+
await runtime.stop({ preserveError: true }).catch(() => undefined);
|
|
1510
|
+
throw error;
|
|
1511
|
+
}
|
|
1512
|
+
}
|
|
1513
|
+
|
|
1514
|
+
#runtimeRepairCapability(botId) {
|
|
1515
|
+
const ownedAttempt = (attemptId) => {
|
|
1516
|
+
const record = this.#registrations.get(attemptId);
|
|
1517
|
+
return record?.operation === CALLBACK_REPAIR_OPERATION && record.botId === botId
|
|
1518
|
+
? record
|
|
1519
|
+
: null;
|
|
1520
|
+
};
|
|
1521
|
+
return Object.freeze({
|
|
1522
|
+
start: ({ actorOpenId, chatId } = {}) => this.startCallbackRepair(botId, {
|
|
1523
|
+
actorOpenId,
|
|
1524
|
+
chatId,
|
|
1525
|
+
}),
|
|
1526
|
+
status: ({ attemptId } = {}) => ownedAttempt(attemptId)
|
|
1527
|
+
? this.registrationStatus(attemptId)
|
|
1528
|
+
: null,
|
|
1529
|
+
cancel: async ({ attemptId } = {}) => ownedAttempt(attemptId)
|
|
1530
|
+
? this.cancelRegistration(attemptId)
|
|
1531
|
+
: this.status(botId),
|
|
1532
|
+
});
|
|
1533
|
+
}
|
|
1534
|
+
|
|
1535
|
+
async #stopRuntime(botId) {
|
|
1536
|
+
const runtime = this.#runtimes.get(botId);
|
|
1537
|
+
this.#runtimes.delete(botId);
|
|
1538
|
+
if (runtime) await runtime.stop();
|
|
1539
|
+
}
|
|
1540
|
+
|
|
1541
|
+
async #deleteBot(botId) {
|
|
1542
|
+
let config = this.#configStore.getBot(botId);
|
|
1543
|
+
if (!config) return;
|
|
1544
|
+
this.#inboundConsumers.remove(botId);
|
|
1545
|
+
if (!config.deletionPending) {
|
|
1546
|
+
config = await this.#configStore.saveBot({ ...config, deletionPending: true });
|
|
1547
|
+
}
|
|
1548
|
+
await this.#stopRuntime(botId);
|
|
1549
|
+
try {
|
|
1550
|
+
await atConnectionStage('credential.remove', () => this.#credentials.unset(config.secretRef), 'credential-store');
|
|
1551
|
+
} catch (error) {
|
|
1552
|
+
this.#botErrors.set(botId, this.#diagnostics.report(error, { reuse: true, botId: botId, publicError: {
|
|
1553
|
+
code: 'credential_removal_failed',
|
|
1554
|
+
message: '无法删除机器人凭据,请稍后重试。',
|
|
1555
|
+
} }).publicError);
|
|
1556
|
+
throw new Error('Unable to remove the Feishu credential.', { cause: error });
|
|
1557
|
+
}
|
|
1558
|
+
try {
|
|
1559
|
+
await this.#deleteState({ botId, config });
|
|
1560
|
+
} catch (error) {
|
|
1561
|
+
this.#botErrors.set(botId, this.#diagnostics.report(error, { reuse: true, botId: botId, publicError: {
|
|
1562
|
+
code: 'state_cleanup_failed',
|
|
1563
|
+
message: '无法删除机器人的本地会话数据,请稍后重试。',
|
|
1564
|
+
} }).publicError);
|
|
1565
|
+
throw new Error('Unable to remove the Feishu bot session state.', { cause: error });
|
|
1566
|
+
}
|
|
1567
|
+
await this.#configStore.removeBot(botId);
|
|
1568
|
+
this.#botErrors.delete(botId);
|
|
1569
|
+
this.#botOwnership.delete(botId);
|
|
1570
|
+
}
|
|
1571
|
+
|
|
1572
|
+
async #restoreCredential(secretRef, previous) {
|
|
1573
|
+
if (previous?.value) await atConnectionStage('credential.save', () => this.#credentials.set(secretRef, previous.value), 'credential-store');
|
|
1574
|
+
else await atConnectionStage('credential.remove', () => this.#credentials.unset(secretRef), 'credential-store');
|
|
1575
|
+
}
|
|
1576
|
+
|
|
1577
|
+
#requireBot(botId) {
|
|
1578
|
+
const config = this.#configStore.getBot(botId);
|
|
1579
|
+
if (!config) throw new Error('Unknown Feishu bot');
|
|
1580
|
+
return config;
|
|
1581
|
+
}
|
|
1582
|
+
|
|
1583
|
+
#assertOpen(code) {
|
|
1584
|
+
if (this.#closed) throw Object.assign(new Error('The Feishu controller is closed'), code ? { code } : {});
|
|
1585
|
+
}
|
|
1586
|
+
|
|
1587
|
+
#serializeConfig(operation) {
|
|
1588
|
+
const result = this.#configTransition.then(operation, operation);
|
|
1589
|
+
this.#configTransition = result.then(() => undefined, () => undefined);
|
|
1590
|
+
return result;
|
|
1591
|
+
}
|
|
1592
|
+
|
|
1593
|
+
#withBotTransition(botId, operation) {
|
|
1594
|
+
const previous = this.#botTransitions.get(botId) ?? Promise.resolve();
|
|
1595
|
+
const result = previous.then(operation, operation);
|
|
1596
|
+
const tail = result.then(() => undefined, () => undefined);
|
|
1597
|
+
this.#botTransitions.set(botId, tail);
|
|
1598
|
+
void tail.finally(() => {
|
|
1599
|
+
if (this.#botTransitions.get(botId) === tail) this.#botTransitions.delete(botId);
|
|
1600
|
+
});
|
|
1601
|
+
return result;
|
|
1602
|
+
}
|
|
1603
|
+
|
|
1604
|
+
#trimRegistrations() {
|
|
1605
|
+
if (this.#registrations.size <= 32) return;
|
|
1606
|
+
for (const [id, record] of this.#registrations) {
|
|
1607
|
+
if (id === this.#latestRegistrationId) continue;
|
|
1608
|
+
const state = record.manager.status().state;
|
|
1609
|
+
if (!ACTIVE_REGISTRATION_STATES.has(state) && state !== 'saving') {
|
|
1610
|
+
this.#registrations.delete(id);
|
|
1611
|
+
}
|
|
1612
|
+
if (this.#registrations.size <= 32) break;
|
|
1613
|
+
}
|
|
1614
|
+
}
|
|
1615
|
+
|
|
1616
|
+
#touch() {
|
|
1617
|
+
this.#revision += 1;
|
|
1618
|
+
}
|
|
1619
|
+
}
|