@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,495 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Feishu native Slash Command registration for the dsh-im Feishu channel.
|
|
3
|
+
*
|
|
4
|
+
* The Feishu client shows a "/" command panel in the chat input box. The
|
|
5
|
+
* command list is stored server-side per bot application and is NOT pushed
|
|
6
|
+
* by dsh/Harness. dsh-im holds its own static command manifest and calls the
|
|
7
|
+
* Feishu OpenAPI to register it, so users can discover commands by typing "/".
|
|
8
|
+
*
|
|
9
|
+
* Reference (official):
|
|
10
|
+
* https://open.feishu.cn/document/mcp_open_tools/agent-best-practices/agent-supports-slash-commands
|
|
11
|
+
*
|
|
12
|
+
* The registered command panel is only a client-side convenience: when a user
|
|
13
|
+
* taps a command, Feishu sends it to the bot as an ordinary text message via
|
|
14
|
+
* im.message.receive_v1. The bridge's #handle() command matcher therefore
|
|
15
|
+
* needs no changes as long as every registered command name matches the
|
|
16
|
+
* existing "/xxx" text commands.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import {
|
|
20
|
+
DEFAULT_SLASH_COMMAND_ICON,
|
|
21
|
+
SLASH_COMMAND_MANIFEST,
|
|
22
|
+
isCustomSlashPanel,
|
|
23
|
+
resolveSlashPanelManifest,
|
|
24
|
+
} from './slash-command-panel.mjs';
|
|
25
|
+
|
|
26
|
+
// The manifest describes the panel, so it lives with the panel's configuration;
|
|
27
|
+
// re-exported here because this is where callers have always imported it from.
|
|
28
|
+
export { SLASH_COMMAND_MANIFEST };
|
|
29
|
+
|
|
30
|
+
const SLASH_ENDPOINT = '/open-apis/application/v7/app_slash_commands';
|
|
31
|
+
const MISSING_PERMISSION_CODES = new Set(['99991640', '99991672']);
|
|
32
|
+
|
|
33
|
+
export const SLASH_COMMAND_TENANT_SCOPES = Object.freeze([
|
|
34
|
+
'application:app_slash_command:read',
|
|
35
|
+
'application:app_slash_command:write',
|
|
36
|
+
]);
|
|
37
|
+
|
|
38
|
+
const DEFAULT_ICON = DEFAULT_SLASH_COMMAND_ICON;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Feishu returns the panel in creation order (newest first, time to the
|
|
42
|
+
* second), so a chosen order is only reachable by creating the commands in
|
|
43
|
+
* reverse, one per second. Anything faster lands in the same second and the
|
|
44
|
+
* order degrades to the platform's tie-break.
|
|
45
|
+
*/
|
|
46
|
+
export const SLASH_PANEL_CREATE_INTERVAL_MS = 1_100;
|
|
47
|
+
|
|
48
|
+
// Commands that require a parameter are registered too, so the user can type
|
|
49
|
+
// "/watch <session ID>" from the panel. A leading placeholder hint is not part of
|
|
50
|
+
// the registered name; Feishu only allows a plain command token.
|
|
51
|
+
|
|
52
|
+
function endpointFor(domain, path) {
|
|
53
|
+
const origin = domain === 'lark' ? 'https://open.larksuite.com' : 'https://open.feishu.cn';
|
|
54
|
+
return new URL(path, origin);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function jsonResponse(body, operation) {
|
|
58
|
+
if (!body || typeof body !== 'object' || Array.isArray(body)) {
|
|
59
|
+
throw new Error(`${operation} returned a non-JSON response`);
|
|
60
|
+
}
|
|
61
|
+
if (body.code !== 0) {
|
|
62
|
+
const error = new Error(`${operation} failed: ${body.msg || `code ${body.code}`}`);
|
|
63
|
+
error.code = String(body.code);
|
|
64
|
+
error.msg = body.msg;
|
|
65
|
+
throw error;
|
|
66
|
+
}
|
|
67
|
+
return body;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function requestSignal(signal, timeoutMs) {
|
|
71
|
+
const timeout = AbortSignal.timeout(timeoutMs);
|
|
72
|
+
return signal ? AbortSignal.any([signal, timeout]) : timeout;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async function requestJson(httpInstance, options, operation) {
|
|
76
|
+
try {
|
|
77
|
+
return jsonResponse(await httpInstance.request(options), operation);
|
|
78
|
+
} catch (error) {
|
|
79
|
+
const body = error?.response?.data;
|
|
80
|
+
if (body && typeof body === 'object' && !Array.isArray(body)
|
|
81
|
+
&& Object.hasOwn(body, 'code')) {
|
|
82
|
+
return jsonResponse(body, operation);
|
|
83
|
+
}
|
|
84
|
+
throw error;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Fetch a tenant_access_token for the app. */
|
|
89
|
+
async function fetchTenantAccessToken({
|
|
90
|
+
appId, appSecret, domain, httpInstance, timeoutMs, signal,
|
|
91
|
+
}) {
|
|
92
|
+
if (!appId || !appSecret) throw new Error('Feishu slash registration requires app credentials');
|
|
93
|
+
if (!httpInstance || typeof httpInstance.request !== 'function') {
|
|
94
|
+
throw new TypeError('Feishu slash registration requires an HTTP instance');
|
|
95
|
+
}
|
|
96
|
+
const body = await requestJson(httpInstance, {
|
|
97
|
+
method: 'POST',
|
|
98
|
+
url: endpointFor(domain, '/open-apis/auth/v3/tenant_access_token/internal').href,
|
|
99
|
+
headers: { 'content-type': 'application/json; charset=utf-8' },
|
|
100
|
+
data: { app_id: appId, app_secret: appSecret },
|
|
101
|
+
signal: requestSignal(signal, timeoutMs),
|
|
102
|
+
timeout: timeoutMs,
|
|
103
|
+
}, 'Feishu authentication');
|
|
104
|
+
if (!body.tenant_access_token) {
|
|
105
|
+
throw new Error('Feishu authentication returned no tenant access token');
|
|
106
|
+
}
|
|
107
|
+
return body.tenant_access_token;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
async function listSlashCommandsWithToken({
|
|
111
|
+
tenantAccessToken, domain, httpInstance, timeoutMs, signal,
|
|
112
|
+
}) {
|
|
113
|
+
const body = await requestJson(httpInstance, {
|
|
114
|
+
method: 'GET',
|
|
115
|
+
url: endpointFor(domain, SLASH_ENDPOINT).href,
|
|
116
|
+
headers: {
|
|
117
|
+
authorization: `Bearer ${tenantAccessToken}`,
|
|
118
|
+
'content-type': 'application/json; charset=utf-8',
|
|
119
|
+
},
|
|
120
|
+
signal: requestSignal(signal, timeoutMs),
|
|
121
|
+
timeout: timeoutMs,
|
|
122
|
+
}, 'Feishu slash command list');
|
|
123
|
+
return Array.isArray(body.data?.items) ? body.data.items : [];
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** List every slash command currently registered for the app. */
|
|
127
|
+
export async function listSlashCommands({
|
|
128
|
+
appId, appSecret, domain = 'feishu', httpInstance, timeoutMs = 15000, signal,
|
|
129
|
+
}) {
|
|
130
|
+
const tenantAccessToken = await fetchTenantAccessToken({
|
|
131
|
+
appId, appSecret, domain, httpInstance, timeoutMs, signal,
|
|
132
|
+
});
|
|
133
|
+
return listSlashCommandsWithToken({
|
|
134
|
+
tenantAccessToken, domain, httpInstance, timeoutMs, signal,
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
async function createSlashCommandWithToken({
|
|
139
|
+
tenantAccessToken, domain, httpInstance, timeoutMs, signal,
|
|
140
|
+
command, description, icon = DEFAULT_ICON,
|
|
141
|
+
}) {
|
|
142
|
+
const data = { command };
|
|
143
|
+
if (description && (description.default_value || description.i18n)) {
|
|
144
|
+
data.description = description;
|
|
145
|
+
} else if (typeof description === 'string' && description.trim()) {
|
|
146
|
+
data.description = { default_value: description.trim() };
|
|
147
|
+
}
|
|
148
|
+
if (icon) data.description = { ...(data.description ?? {}), icon: { icon_key: icon } };
|
|
149
|
+
const body = await requestJson(httpInstance, {
|
|
150
|
+
method: 'POST',
|
|
151
|
+
url: endpointFor(domain, SLASH_ENDPOINT).href,
|
|
152
|
+
headers: {
|
|
153
|
+
authorization: `Bearer ${tenantAccessToken}`,
|
|
154
|
+
'content-type': 'application/json; charset=utf-8',
|
|
155
|
+
},
|
|
156
|
+
data,
|
|
157
|
+
signal: requestSignal(signal, timeoutMs),
|
|
158
|
+
timeout: timeoutMs,
|
|
159
|
+
}, `Feishu slash command create (/${command})`);
|
|
160
|
+
return body.data?.command_id ?? null;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** Register a single slash command. Returns the server-assigned command_id. */
|
|
164
|
+
export async function createSlashCommand({
|
|
165
|
+
appId, appSecret, domain = 'feishu', httpInstance, timeoutMs = 15000,
|
|
166
|
+
signal, command, description, icon = DEFAULT_ICON,
|
|
167
|
+
}) {
|
|
168
|
+
const tenantAccessToken = await fetchTenantAccessToken({
|
|
169
|
+
appId, appSecret, domain, httpInstance, timeoutMs, signal,
|
|
170
|
+
});
|
|
171
|
+
return createSlashCommandWithToken({
|
|
172
|
+
tenantAccessToken, domain, httpInstance, timeoutMs, signal,
|
|
173
|
+
command, description, icon,
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** Delete a registered slash command by its server command_id. */
|
|
178
|
+
export async function deleteSlashCommand({
|
|
179
|
+
appId, appSecret, domain = 'feishu', httpInstance, timeoutMs = 15000, signal, commandId,
|
|
180
|
+
}) {
|
|
181
|
+
const tenantAccessToken = await fetchTenantAccessToken({
|
|
182
|
+
appId, appSecret, domain, httpInstance, timeoutMs, signal,
|
|
183
|
+
});
|
|
184
|
+
await requestJson(httpInstance, {
|
|
185
|
+
method: 'DELETE',
|
|
186
|
+
url: endpointFor(domain, `${SLASH_ENDPOINT}/${commandId}`).href,
|
|
187
|
+
headers: { authorization: `Bearer ${tenantAccessToken}` },
|
|
188
|
+
signal: requestSignal(signal, timeoutMs),
|
|
189
|
+
timeout: timeoutMs,
|
|
190
|
+
}, 'Feishu slash command delete');
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Best-effort sync of the manifest into the app's registered slash commands.
|
|
195
|
+
* Creates any command in the manifest that is not yet registered and returns
|
|
196
|
+
* a structured report. This is idempotent (the API rejects duplicates with
|
|
197
|
+
* "command already exists", so we skip existing names).
|
|
198
|
+
*
|
|
199
|
+
* @returns {{ created: Array<{command,command_id}>, existing: string[], failed: Array<{command,error}> }}
|
|
200
|
+
*/
|
|
201
|
+
export async function registerSlashCommands({
|
|
202
|
+
appId, appSecret, domain = 'feishu', httpInstance, timeoutMs = 15000,
|
|
203
|
+
signal, manifest = SLASH_COMMAND_MANIFEST,
|
|
204
|
+
}) {
|
|
205
|
+
const tenantAccessToken = await fetchTenantAccessToken({
|
|
206
|
+
appId, appSecret, domain, httpInstance, timeoutMs, signal,
|
|
207
|
+
});
|
|
208
|
+
const existing = new Set((await listSlashCommandsWithToken({
|
|
209
|
+
tenantAccessToken, domain, httpInstance, timeoutMs, signal,
|
|
210
|
+
}))
|
|
211
|
+
.map((item) => item.command));
|
|
212
|
+
|
|
213
|
+
const created = [];
|
|
214
|
+
const failed = [];
|
|
215
|
+
for (const entry of manifest) {
|
|
216
|
+
const command = String(entry.command ?? '').replace(/^\//, '');
|
|
217
|
+
if (!command) continue;
|
|
218
|
+
if (existing.has(command)) continue;
|
|
219
|
+
try {
|
|
220
|
+
const description = {
|
|
221
|
+
default_value: entry.default ?? entry.en_us ?? command,
|
|
222
|
+
i18n: {
|
|
223
|
+
zh_cn: entry.default ?? command,
|
|
224
|
+
en_us: entry.en_us ?? entry.default ?? command,
|
|
225
|
+
},
|
|
226
|
+
};
|
|
227
|
+
const commandId = await createSlashCommandWithToken({
|
|
228
|
+
tenantAccessToken, domain, httpInstance, timeoutMs, signal,
|
|
229
|
+
command, description, icon: entry.icon ?? DEFAULT_ICON,
|
|
230
|
+
});
|
|
231
|
+
created.push({ command, command_id: commandId });
|
|
232
|
+
} catch (error) {
|
|
233
|
+
// "command already exists" can race with concurrent runs; treat as existing.
|
|
234
|
+
if (error?.code === '40000000' && /already exists/i.test(error?.msg ?? '')) {
|
|
235
|
+
existing.add(command);
|
|
236
|
+
continue;
|
|
237
|
+
}
|
|
238
|
+
if (MISSING_PERMISSION_CODES.has(error?.code)
|
|
239
|
+
|| /(?:lacks permission|access denied)/i.test(error?.msg ?? '')) {
|
|
240
|
+
// Missing app_slash_command:write permission; abort the batch.
|
|
241
|
+
failed.push({ command, error });
|
|
242
|
+
break;
|
|
243
|
+
}
|
|
244
|
+
failed.push({ command, error: error?.message ?? String(error) });
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
return {
|
|
249
|
+
created,
|
|
250
|
+
existing: [...existing].filter((c) => c !== null && c !== undefined),
|
|
251
|
+
failed,
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/** Delete one registered command with an already-fetched tenant token. */
|
|
256
|
+
async function deleteSlashCommandWithToken({
|
|
257
|
+
tenantAccessToken, domain, httpInstance, timeoutMs, signal, commandId,
|
|
258
|
+
}) {
|
|
259
|
+
await requestJson(httpInstance, {
|
|
260
|
+
method: 'DELETE',
|
|
261
|
+
url: endpointFor(domain, `${SLASH_ENDPOINT}/${commandId}`).href,
|
|
262
|
+
headers: { authorization: `Bearer ${tenantAccessToken}` },
|
|
263
|
+
signal: requestSignal(signal, timeoutMs),
|
|
264
|
+
timeout: timeoutMs,
|
|
265
|
+
}, 'Feishu slash command delete');
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/** Plain timer used between creates; tests inject their own. */
|
|
269
|
+
function defaultWait(ms, signal) {
|
|
270
|
+
return new Promise((resolve) => {
|
|
271
|
+
const timer = setTimeout(resolve, ms);
|
|
272
|
+
signal?.addEventListener('abort', () => {
|
|
273
|
+
clearTimeout(timer);
|
|
274
|
+
resolve();
|
|
275
|
+
}, { once: true });
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
function commandNameOf(item) {
|
|
280
|
+
return typeof item?.command === 'string' && item.command ? item.command : null;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* The commands of ours in panel order. Feishu returns the list newest-first
|
|
285
|
+
* with second precision, so `create_time` is the panel order and `command_id`
|
|
286
|
+
* only breaks ties inside the same second.
|
|
287
|
+
*/
|
|
288
|
+
function orderedOwnedNames(items, owned) {
|
|
289
|
+
return items
|
|
290
|
+
.map((item, index) => ({ item, index }))
|
|
291
|
+
.filter(({ item }) => owned.has(commandNameOf(item)))
|
|
292
|
+
.sort((a, b) => {
|
|
293
|
+
const aTime = Number(a.item.create_time);
|
|
294
|
+
const bTime = Number(b.item.create_time);
|
|
295
|
+
if (Number.isFinite(aTime) && Number.isFinite(bTime) && aTime !== bTime) return bTime - aTime;
|
|
296
|
+
const aId = Number(a.item.command_id);
|
|
297
|
+
const bId = Number(b.item.command_id);
|
|
298
|
+
if (Number.isFinite(aId) && Number.isFinite(bId) && aId !== bId) return bId - aId;
|
|
299
|
+
return a.index - b.index;
|
|
300
|
+
})
|
|
301
|
+
.map(({ item }) => commandNameOf(item))
|
|
302
|
+
.filter(Boolean);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
function slashPanelDescription(entry, command) {
|
|
306
|
+
return {
|
|
307
|
+
default_value: entry.default ?? entry.en_us ?? command,
|
|
308
|
+
i18n: {
|
|
309
|
+
zh_cn: entry.default ?? command,
|
|
310
|
+
en_us: entry.en_us ?? entry.default ?? command,
|
|
311
|
+
},
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Sync one bot's "/" panel with its configured manifest.
|
|
317
|
+
*
|
|
318
|
+
* `default` mode is the pre-configurable behaviour: create what is missing and
|
|
319
|
+
* leave everything else alone. `custom` mode converges the app to the
|
|
320
|
+
* configured commands *and order*:
|
|
321
|
+
*
|
|
322
|
+
* 1. read the registered commands (with command_id and create_time);
|
|
323
|
+
* 2. stop when the panel already holds exactly that order — Feishu reports the
|
|
324
|
+
* panel in creation order, so a matching order needs no calls at all;
|
|
325
|
+
* 3. otherwise delete our commands and recreate the configured ones
|
|
326
|
+
* newest-first, one per `intervalMs`, because the order *is* the creation
|
|
327
|
+
* order.
|
|
328
|
+
*
|
|
329
|
+
* Commands registered outside the manifest are never deleted: dsh-im does not
|
|
330
|
+
* own them, and their position is the platform's business.
|
|
331
|
+
*
|
|
332
|
+
* @returns {{ created: Array<{command,command_id}>, deleted: string[],
|
|
333
|
+
* existing: string[], external: string[], failed: Array<{command,error}>,
|
|
334
|
+
* changed: boolean, superseded: boolean }} `superseded` marks a run that
|
|
335
|
+
* stopped early because a newer panel config took over; it reports what it
|
|
336
|
+
* managed to do, and the newer run converges the panel from there.
|
|
337
|
+
*/
|
|
338
|
+
export async function syncSlashCommands({
|
|
339
|
+
appId, appSecret, domain = 'feishu', httpInstance, timeoutMs = 15000,
|
|
340
|
+
signal, manifest = SLASH_COMMAND_MANIFEST, config = null,
|
|
341
|
+
intervalMs = SLASH_PANEL_CREATE_INTERVAL_MS, wait = defaultWait,
|
|
342
|
+
}) {
|
|
343
|
+
// A superseded run — the runtime aborts this signal when a newer panel config
|
|
344
|
+
// arrives — must not send further create/delete calls: the newer run re-reads
|
|
345
|
+
// the panel and converges it, and letting both write would leave the older
|
|
346
|
+
// plan's commands behind. Requests already in flight are left to finish.
|
|
347
|
+
const stopped = () => signal?.aborted === true;
|
|
348
|
+
const tenantAccessToken = await fetchTenantAccessToken({
|
|
349
|
+
appId, appSecret, domain, httpInstance, timeoutMs, signal,
|
|
350
|
+
});
|
|
351
|
+
const items = await listSlashCommandsWithToken({
|
|
352
|
+
tenantAccessToken, domain, httpInstance, timeoutMs, signal,
|
|
353
|
+
});
|
|
354
|
+
const owned = new Set(manifest.map((entry) => commandNameOf(entry)).filter(Boolean));
|
|
355
|
+
const external = items
|
|
356
|
+
.map((item) => commandNameOf(item))
|
|
357
|
+
.filter((name) => name && !owned.has(name));
|
|
358
|
+
const registered = new Set(
|
|
359
|
+
items.map((item) => commandNameOf(item)).filter(Boolean),
|
|
360
|
+
);
|
|
361
|
+
|
|
362
|
+
if (!isCustomSlashPanel(config)) {
|
|
363
|
+
const created = [];
|
|
364
|
+
const failed = [];
|
|
365
|
+
for (const entry of resolveSlashPanelManifest(config, manifest)) {
|
|
366
|
+
if (stopped()) break;
|
|
367
|
+
const command = commandNameOf(entry);
|
|
368
|
+
if (!command || registered.has(command)) continue;
|
|
369
|
+
try {
|
|
370
|
+
const commandId = await createSlashCommandWithToken({
|
|
371
|
+
tenantAccessToken, domain, httpInstance, timeoutMs, signal,
|
|
372
|
+
command,
|
|
373
|
+
description: slashPanelDescription(entry, command),
|
|
374
|
+
icon: entry.icon ?? DEFAULT_ICON,
|
|
375
|
+
});
|
|
376
|
+
created.push({ command, command_id: commandId });
|
|
377
|
+
registered.add(command);
|
|
378
|
+
} catch (error) {
|
|
379
|
+
if (stopped()) break;
|
|
380
|
+
if (error?.code === '40000000' && /already exists/i.test(error?.msg ?? '')) {
|
|
381
|
+
registered.add(command);
|
|
382
|
+
continue;
|
|
383
|
+
}
|
|
384
|
+
failed.push({ command, error: error?.message ?? String(error) });
|
|
385
|
+
if (MISSING_PERMISSION_CODES.has(error?.code)
|
|
386
|
+
|| /(?:lacks permission|access denied)/i.test(error?.msg ?? '')) {
|
|
387
|
+
break;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
return {
|
|
392
|
+
created,
|
|
393
|
+
deleted: [],
|
|
394
|
+
existing: [...registered].filter(Boolean),
|
|
395
|
+
external,
|
|
396
|
+
failed,
|
|
397
|
+
changed: created.length > 0,
|
|
398
|
+
superseded: stopped(),
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
const current = orderedOwnedNames(items, owned);
|
|
403
|
+
const plannedEntries = resolveSlashPanelManifest(config, manifest)
|
|
404
|
+
.filter((entry) => commandNameOf(entry));
|
|
405
|
+
const plannedNames = plannedEntries.map((entry) => commandNameOf(entry));
|
|
406
|
+
if (current.length === plannedNames.length
|
|
407
|
+
&& plannedNames.every((name, index) => name === current[index])) {
|
|
408
|
+
return {
|
|
409
|
+
created: [], deleted: [], existing: current, external, failed: [], changed: false,
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
// A newer config took over while we were reading the panel: leave the rebuild
|
|
414
|
+
// to its run rather than deleting commands for a plan nobody wants anymore.
|
|
415
|
+
if (stopped()) {
|
|
416
|
+
return {
|
|
417
|
+
created: [], deleted: [], existing: current, external, failed: [], changed: false, superseded: true,
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
const byName = new Map(
|
|
422
|
+
items.map((item) => [commandNameOf(item), item]).filter(([name]) => name && owned.has(name)),
|
|
423
|
+
);
|
|
424
|
+
const deleted = [];
|
|
425
|
+
const failed = [];
|
|
426
|
+
for (const name of current) {
|
|
427
|
+
if (stopped()) break;
|
|
428
|
+
const item = byName.get(name);
|
|
429
|
+
if (!item?.command_id) {
|
|
430
|
+
failed.push({ command: name, error: 'the registered command reports no command_id' });
|
|
431
|
+
continue;
|
|
432
|
+
}
|
|
433
|
+
try {
|
|
434
|
+
await deleteSlashCommandWithToken({
|
|
435
|
+
tenantAccessToken, domain, httpInstance, timeoutMs, signal, commandId: item.command_id,
|
|
436
|
+
});
|
|
437
|
+
deleted.push(name);
|
|
438
|
+
} catch (error) {
|
|
439
|
+
if (stopped()) break;
|
|
440
|
+
failed.push({ command: name, error: error?.message ?? String(error) });
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
// A superseded run stops before rebuilding: the newer run owns the panel now.
|
|
444
|
+
if (stopped()) {
|
|
445
|
+
return {
|
|
446
|
+
created: [], deleted, existing: current, external, failed, changed: deleted.length > 0, superseded: true,
|
|
447
|
+
};
|
|
448
|
+
}
|
|
449
|
+
// Deleting only part of the panel would leave it half-converged, and
|
|
450
|
+
// recreating a name that still exists is rejected as a duplicate. Stop here;
|
|
451
|
+
// the next sync (config change, reconnect, or a later start) converges.
|
|
452
|
+
if (failed.length > 0) {
|
|
453
|
+
return { created: [], deleted, existing: current, external, failed, changed: deleted.length > 0 };
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
const created = [];
|
|
457
|
+
for (const [index, entry] of [...plannedEntries].reverse().entries()) {
|
|
458
|
+
if (stopped()) break;
|
|
459
|
+
const command = commandNameOf(entry);
|
|
460
|
+
try {
|
|
461
|
+
if (index > 0) {
|
|
462
|
+
await wait(intervalMs, signal);
|
|
463
|
+
// The wait wakes on abort as well, so re-check instead of recording a
|
|
464
|
+
// superseded run as a failed create.
|
|
465
|
+
if (stopped()) break;
|
|
466
|
+
}
|
|
467
|
+
const commandId = await createSlashCommandWithToken({
|
|
468
|
+
tenantAccessToken, domain, httpInstance, timeoutMs, signal,
|
|
469
|
+
command,
|
|
470
|
+
description: slashPanelDescription(entry, command),
|
|
471
|
+
icon: entry.icon ?? DEFAULT_ICON,
|
|
472
|
+
});
|
|
473
|
+
created.push({ command, command_id: commandId });
|
|
474
|
+
} catch (error) {
|
|
475
|
+
if (stopped()) break;
|
|
476
|
+
failed.push({ command, error: error?.message ?? String(error) });
|
|
477
|
+
if (MISSING_PERMISSION_CODES.has(error?.code)
|
|
478
|
+
|| /(?:lacks permission|access denied)/i.test(error?.msg ?? '')) {
|
|
479
|
+
break;
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
return {
|
|
485
|
+
created,
|
|
486
|
+
deleted,
|
|
487
|
+
existing: [],
|
|
488
|
+
external,
|
|
489
|
+
failed,
|
|
490
|
+
changed: created.length > 0 || deleted.length > 0,
|
|
491
|
+
superseded: stopped(),
|
|
492
|
+
};
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
export default registerSlashCommands;
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import { deferredStateAccess, normalizeDeferredState } from '../shared/deferred-state.mjs';
|
|
2
|
+
import { mkdir, readFile, rename, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { dirname } from 'node:path';
|
|
4
|
+
|
|
5
|
+
const EMPTY_STATE = Object.freeze({
|
|
6
|
+
version: 1,
|
|
7
|
+
sessions: {},
|
|
8
|
+
seenMessageIds: [],
|
|
9
|
+
watches: {},
|
|
10
|
+
deferred: {},
|
|
11
|
+
includeArchivedSessions: false,
|
|
12
|
+
topics: {},
|
|
13
|
+
mirrors: {},
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
/** One conversation key may watch at most this many sessions. */
|
|
17
|
+
export const MAX_WATCHES_PER_KEY = 20;
|
|
18
|
+
|
|
19
|
+
/** A persisted watch entry: the watched session plus its delivery target. */
|
|
20
|
+
function validWatchEntry(value) {
|
|
21
|
+
return value
|
|
22
|
+
&& typeof value === 'object'
|
|
23
|
+
&& typeof value.sessionId === 'string' && value.sessionId.length > 0
|
|
24
|
+
&& typeof value.chatId === 'string' && value.chatId.length > 0;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export class StateStore {
|
|
28
|
+
#path;
|
|
29
|
+
#state = structuredClone(EMPTY_STATE);
|
|
30
|
+
#writeQueue = Promise.resolve();
|
|
31
|
+
#deferred = deferredStateAccess(() => this.#state, () => this.#persist());
|
|
32
|
+
|
|
33
|
+
constructor(path) {
|
|
34
|
+
this.#path = path;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async load() {
|
|
38
|
+
try {
|
|
39
|
+
const parsed = JSON.parse(await readFile(this.#path, 'utf8'));
|
|
40
|
+
this.#state = {
|
|
41
|
+
version: 1,
|
|
42
|
+
sessions: parsed.sessions && typeof parsed.sessions === 'object' ? parsed.sessions : {},
|
|
43
|
+
seenMessageIds: Array.isArray(parsed.seenMessageIds) ? parsed.seenMessageIds.slice(-1000) : [],
|
|
44
|
+
watches: parsed.watches && typeof parsed.watches === 'object' ? parsed.watches : {},
|
|
45
|
+
deferred: normalizeDeferredState(parsed.deferred),
|
|
46
|
+
includeArchivedSessions: typeof parsed.includeArchivedSessions === 'boolean'
|
|
47
|
+
? parsed.includeArchivedSessions
|
|
48
|
+
: false,
|
|
49
|
+
topics: parsed.topics && typeof parsed.topics === 'object' ? parsed.topics : {},
|
|
50
|
+
mirrors: parsed.mirrors && typeof parsed.mirrors === 'object' ? parsed.mirrors : {},
|
|
51
|
+
};
|
|
52
|
+
} catch (error) {
|
|
53
|
+
if (error?.code !== 'ENOENT') throw error;
|
|
54
|
+
await this.#persist();
|
|
55
|
+
}
|
|
56
|
+
return this;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
deferredEntries() { return this.#deferred.entries(); }
|
|
60
|
+
putDeferred(entry) { return this.#deferred.put(entry); }
|
|
61
|
+
patchDeferred(id, patch) { return this.#deferred.patch(id, patch); }
|
|
62
|
+
removeDeferred(id) { return this.#deferred.remove(id); }
|
|
63
|
+
|
|
64
|
+
// ── Mirrors (persisted: open session-sync cards, recovered at startup) ──
|
|
65
|
+
setMirror(sessionId, entry) {
|
|
66
|
+
this.#state.mirrors[sessionId] = entry;
|
|
67
|
+
return this.#persist();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
clearMirror(sessionId) {
|
|
71
|
+
delete this.#state.mirrors[sessionId];
|
|
72
|
+
return this.#persist();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
mirrorEntries() {
|
|
76
|
+
return Object.entries(this.#state.mirrors ?? {});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
sessionFor(key) {
|
|
80
|
+
return this.#state.sessions[key] ?? null;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async setSession(key, sessionId) {
|
|
84
|
+
this.#state.sessions[key] = sessionId;
|
|
85
|
+
await this.#persist();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async clearSession(key) {
|
|
89
|
+
delete this.#state.sessions[key];
|
|
90
|
+
await this.#persist();
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async clearSessions() {
|
|
94
|
+
this.#state.sessions = {};
|
|
95
|
+
await this.#persist();
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
hasSeen(messageId) {
|
|
99
|
+
return this.#state.seenMessageIds.includes(messageId);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async markSeen(messageId) {
|
|
103
|
+
if (this.hasSeen(messageId)) return;
|
|
104
|
+
this.#state.seenMessageIds.push(messageId);
|
|
105
|
+
if (this.#state.seenMessageIds.length > 1000) {
|
|
106
|
+
this.#state.seenMessageIds.splice(0, this.#state.seenMessageIds.length - 1000);
|
|
107
|
+
}
|
|
108
|
+
await this.#persist();
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
snapshot() {
|
|
112
|
+
return structuredClone(this.#state);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// ── Watches (persisted: surviving restarts) ─────────────────────────────
|
|
116
|
+
|
|
117
|
+
watchEntries(key) {
|
|
118
|
+
const list = this.#state.watches[key];
|
|
119
|
+
return Array.isArray(list) ? list.filter(validWatchEntry) : [];
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
watchEntry(key, sessionId) {
|
|
123
|
+
return this.watchEntries(key).find((entry) => entry.sessionId === sessionId) ?? null;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async setWatch(key, entry) {
|
|
127
|
+
const list = this.#state.watches[key] ?? [];
|
|
128
|
+
const index = list.findIndex((existing) => existing.sessionId === entry.sessionId);
|
|
129
|
+
if (index === -1) {
|
|
130
|
+
if (list.length >= MAX_WATCHES_PER_KEY) list.shift();
|
|
131
|
+
list.push(entry);
|
|
132
|
+
} else {
|
|
133
|
+
list[index] = entry;
|
|
134
|
+
}
|
|
135
|
+
this.#state.watches[key] = list;
|
|
136
|
+
await this.#persist();
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
async removeWatch(key, sessionId) {
|
|
140
|
+
const list = this.#state.watches[key] ?? [];
|
|
141
|
+
this.#state.watches[key] = list.filter((entry) => entry.sessionId !== sessionId);
|
|
142
|
+
await this.#persist();
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
async clearWatches(key) {
|
|
146
|
+
delete this.#state.watches[key];
|
|
147
|
+
await this.#persist();
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** Every conversation key currently watching the given session. */
|
|
151
|
+
keysWatching(sessionId) {
|
|
152
|
+
return Object.entries(this.#state.watches)
|
|
153
|
+
.filter(([, list]) => Array.isArray(list) && list.some((entry) => validWatchEntry(entry) && entry.sessionId === sessionId))
|
|
154
|
+
.map(([key]) => key);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Unique watched session ids across all keys (restart compensation). */
|
|
158
|
+
watchedSessionIds() {
|
|
159
|
+
const ids = new Set();
|
|
160
|
+
for (const list of Object.values(this.#state.watches)) {
|
|
161
|
+
if (!Array.isArray(list)) continue;
|
|
162
|
+
for (const entry of list) if (validWatchEntry(entry)) ids.add(entry.sessionId);
|
|
163
|
+
}
|
|
164
|
+
return [...ids];
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// ── Deferred delivery (persisted: surviving restarts) ───────────────────
|
|
168
|
+
|
|
169
|
+
// ── Managed Feishu topics (thread_id → root message, persisted) ────────
|
|
170
|
+
|
|
171
|
+
topicRootFor(threadId) {
|
|
172
|
+
const entry = this.#state.topics[threadId];
|
|
173
|
+
return entry && typeof entry === 'object'
|
|
174
|
+
&& typeof entry.rootMessageId === 'string' && entry.rootMessageId.length > 0
|
|
175
|
+
&& typeof entry.chatId === 'string' && entry.chatId.length > 0
|
|
176
|
+
? { rootMessageId: entry.rootMessageId, chatId: entry.chatId }
|
|
177
|
+
: null;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
async setTopic(threadId, root) {
|
|
181
|
+
const validRoot = root && typeof root === 'object'
|
|
182
|
+
&& typeof root.rootMessageId === 'string' && root.rootMessageId.length > 0
|
|
183
|
+
&& typeof root.chatId === 'string' && root.chatId.length > 0;
|
|
184
|
+
if (!validRoot) throw new TypeError('Invalid Feishu topic root');
|
|
185
|
+
this.#state.topics[threadId] = {
|
|
186
|
+
rootMessageId: root.rootMessageId,
|
|
187
|
+
chatId: root.chatId,
|
|
188
|
+
};
|
|
189
|
+
await this.#persist();
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// ── Session-list archived policy (per bot) ───────────────────
|
|
193
|
+
|
|
194
|
+
includesArchivedSessions() {
|
|
195
|
+
return this.#state.includeArchivedSessions === true;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
async setIncludeArchivedSessions(include) {
|
|
199
|
+
this.#state.includeArchivedSessions = include === true;
|
|
200
|
+
await this.#persist();
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
async #persist() {
|
|
204
|
+
const snapshot = JSON.stringify(this.#state, null, 2) + '\n';
|
|
205
|
+
const operation = this.#writeQueue.then(async () => {
|
|
206
|
+
await mkdir(dirname(this.#path), { recursive: true, mode: 0o700 });
|
|
207
|
+
const temporary = `${this.#path}.tmp`;
|
|
208
|
+
await writeFile(temporary, snapshot, { encoding: 'utf8', mode: 0o600 });
|
|
209
|
+
await rename(temporary, this.#path);
|
|
210
|
+
});
|
|
211
|
+
this.#writeQueue = operation.then(() => undefined, () => undefined);
|
|
212
|
+
await operation;
|
|
213
|
+
}
|
|
214
|
+
}
|