@botharness/im-provider 4.32.0-botharness.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/PROVENANCE.json +25 -0
- package/README.en.md +445 -0
- package/README.md +448 -0
- package/THIRD_PARTY_NOTICES.md +93 -0
- package/assets/dingtalk-menu-template.json +6 -0
- package/assets/logo-dsh-im-chinese-readme-3x2.png +0 -0
- package/assets/logo-dsh-im-connecting-readme-3x2.png +0 -0
- package/assets/logo-dsh-im-connecting-square-v4.png +0 -0
- package/assets/logo-icon.png +0 -0
- package/assets/logo-plugin-message-link-gradient.webp +0 -0
- package/assets/logo-plugin-phone.png +0 -0
- package/assets/logo.png +0 -0
- package/assets/logo_cn.png +0 -0
- package/assets/screenshot-menu-card.png +0 -0
- package/cordis.patch.yml +3 -0
- package/lib/client.js +20330 -0
- package/lib/index.js +602 -0
- package/locale/en.json +5 -0
- package/locale/zh.json +5 -0
- package/package.json +181 -0
- package/plugin-src/client/access-policy-settings.js +362 -0
- package/plugin-src/client/agent-preset.js +119 -0
- package/plugin-src/client/bot-alias.js +169 -0
- package/plugin-src/client/build.mjs +38 -0
- package/plugin-src/client/channel-card-meta.js +132 -0
- package/plugin-src/client/channel-logos.js +190 -0
- package/plugin-src/client/channels/dingtalk/api.js +267 -0
- package/plugin-src/client/channels/dingtalk/index.js +954 -0
- package/plugin-src/client/channels/dingtalk/styles.js +136 -0
- package/plugin-src/client/channels/discord/api.js +11 -0
- package/plugin-src/client/channels/discord/index.js +25 -0
- package/plugin-src/client/channels/discord/styles.js +19 -0
- package/plugin-src/client/channels/email/api.js +52 -0
- package/plugin-src/client/channels/email/index.js +728 -0
- package/plugin-src/client/channels/email/styles.js +51 -0
- package/plugin-src/client/channels/feishu/api.js +405 -0
- package/plugin-src/client/channels/feishu/group-settings.js +581 -0
- package/plugin-src/client/channels/feishu/index.js +1583 -0
- package/plugin-src/client/channels/feishu/slash-panel-settings.js +322 -0
- package/plugin-src/client/channels/feishu/styles.js +573 -0
- package/plugin-src/client/channels/feishu/voice-editor.js +115 -0
- package/plugin-src/client/channels/feishu/voice-settings.js +71 -0
- package/plugin-src/client/channels/imessage/api.js +12 -0
- package/plugin-src/client/channels/imessage/index.js +46 -0
- package/plugin-src/client/channels/imessage/styles.js +19 -0
- package/plugin-src/client/channels/matrix/api.js +11 -0
- package/plugin-src/client/channels/matrix/index.js +151 -0
- package/plugin-src/client/channels/matrix/styles.js +36 -0
- package/plugin-src/client/channels/office/api.js +46 -0
- package/plugin-src/client/channels/office/index.js +153 -0
- package/plugin-src/client/channels/office/styles.js +50 -0
- package/plugin-src/client/channels/qq/api.js +171 -0
- package/plugin-src/client/channels/qq/index.js +593 -0
- package/plugin-src/client/channels/qq/styles.js +19 -0
- package/plugin-src/client/channels/shared/collapsible-account.js +103 -0
- package/plugin-src/client/channels/shared/token-api.js +124 -0
- package/plugin-src/client/channels/shared/token-channel.js +495 -0
- package/plugin-src/client/channels/slack/api.js +11 -0
- package/plugin-src/client/channels/slack/index.js +131 -0
- package/plugin-src/client/channels/slack/styles.js +34 -0
- package/plugin-src/client/channels/telegram/api.js +11 -0
- package/plugin-src/client/channels/telegram/index.js +28 -0
- package/plugin-src/client/channels/telegram/styles.js +31 -0
- package/plugin-src/client/channels/telegram/thinking-traces.js +25 -0
- package/plugin-src/client/channels/wecom/api.js +160 -0
- package/plugin-src/client/channels/wecom/index.js +625 -0
- package/plugin-src/client/channels/wecom/styles.js +19 -0
- package/plugin-src/client/channels/wecom-app/api.js +132 -0
- package/plugin-src/client/channels/wecom-app/index.js +634 -0
- package/plugin-src/client/channels/wecom-app/styles.js +25 -0
- package/plugin-src/client/channels/weixin/api.js +211 -0
- package/plugin-src/client/channels/weixin/connection-error.js +1 -0
- package/plugin-src/client/channels/weixin/index.js +801 -0
- package/plugin-src/client/channels/weixin/styles.js +115 -0
- package/plugin-src/client/channels/whatsapp/api.js +152 -0
- package/plugin-src/client/channels/whatsapp/index.js +609 -0
- package/plugin-src/client/channels/whatsapp/styles.js +19 -0
- package/plugin-src/client/connection-error.js +87 -0
- package/plugin-src/client/context-enhancement.js +415 -0
- package/plugin-src/client/credential-binding.js +118 -0
- package/plugin-src/client/delivery-settings.js +905 -0
- package/plugin-src/client/global-settings.js +433 -0
- package/plugin-src/client/i18n.js +1314 -0
- package/plugin-src/client/index.js +624 -0
- package/plugin-src/client/interface-language.js +89 -0
- package/plugin-src/client/last-message-error.js +18 -0
- package/plugin-src/client/lifecycle.js +86 -0
- package/plugin-src/client/loopback-recovery.js +75 -0
- package/plugin-src/client/model-setting.js +172 -0
- package/plugin-src/client/panel-error-boundary.js +21 -0
- package/plugin-src/client/plugin-page.js +51 -0
- package/plugin-src/client/session-channel-logos.js +243 -0
- package/plugin-src/client/styles.js +770 -0
- package/plugin-src/client/update-panel.js +14 -0
- package/plugin-src/client/workspace-directory-picker.js +280 -0
- package/plugin-src/client/workspace-editor.js +69 -0
- package/plugin-src/client/workspace-snapshot-fence.js +28 -0
- package/plugin-src/host/build.mjs +52 -0
- package/plugin-src/host/channels/dingtalk/connection-supervisor.mjs +130 -0
- package/plugin-src/host/channels/dingtalk/index.mjs +35 -0
- package/plugin-src/host/channels/dingtalk/production.mjs +193 -0
- package/plugin-src/host/channels/dingtalk/rpc.mjs +359 -0
- package/plugin-src/host/channels/discord/index.mjs +33 -0
- package/plugin-src/host/channels/discord/production.mjs +17 -0
- package/plugin-src/host/channels/discord/rpc.mjs +21 -0
- package/plugin-src/host/channels/email/availability.mjs +60 -0
- package/plugin-src/host/channels/email/index.mjs +36 -0
- package/plugin-src/host/channels/email/production.mjs +58 -0
- package/plugin-src/host/channels/email/rpc.mjs +163 -0
- package/plugin-src/host/channels/feishu/connection-supervisor.mjs +167 -0
- package/plugin-src/host/channels/feishu/controller.mjs +172 -0
- package/plugin-src/host/channels/feishu/credential-store.mjs +84 -0
- package/plugin-src/host/channels/feishu/index.mjs +69 -0
- package/plugin-src/host/channels/feishu/production.mjs +318 -0
- package/plugin-src/host/channels/feishu/rpc.mjs +863 -0
- package/plugin-src/host/channels/imessage/index.mjs +21 -0
- package/plugin-src/host/channels/imessage/production.mjs +13 -0
- package/plugin-src/host/channels/imessage/rpc.mjs +78 -0
- package/plugin-src/host/channels/matrix/index.mjs +31 -0
- package/plugin-src/host/channels/matrix/production.mjs +241 -0
- package/plugin-src/host/channels/matrix/rpc.mjs +228 -0
- package/plugin-src/host/channels/office/index.mjs +14 -0
- package/plugin-src/host/channels/office/production.mjs +45 -0
- package/plugin-src/host/channels/office/rpc.mjs +51 -0
- package/plugin-src/host/channels/qq/connection-supervisor.mjs +124 -0
- package/plugin-src/host/channels/qq/index.mjs +28 -0
- package/plugin-src/host/channels/qq/production.mjs +181 -0
- package/plugin-src/host/channels/qq/rpc.mjs +250 -0
- package/plugin-src/host/channels/shared/access-policy-production.mjs +137 -0
- package/plugin-src/host/channels/shared/access-policy-rpc.mjs +17 -0
- package/plugin-src/host/channels/shared/agent-preset-rpc.mjs +25 -0
- package/plugin-src/host/channels/shared/bot-alias-rpc.mjs +15 -0
- package/plugin-src/host/channels/shared/connection-supervisor.mjs +133 -0
- package/plugin-src/host/channels/shared/context-enhancement-rpc.mjs +17 -0
- package/plugin-src/host/channels/shared/model-setting-rpc.mjs +18 -0
- package/plugin-src/host/channels/shared/production.mjs +237 -0
- package/plugin-src/host/channels/shared/rpc.mjs +226 -0
- package/plugin-src/host/channels/shared/startup-error.mjs +55 -0
- package/plugin-src/host/channels/shared/startup.mjs +76 -0
- package/plugin-src/host/channels/shared/thinking-traces-rpc.mjs +11 -0
- package/plugin-src/host/channels/shared/workspace-rpc.mjs +31 -0
- package/plugin-src/host/channels/slack/index.mjs +31 -0
- package/plugin-src/host/channels/slack/production.mjs +176 -0
- package/plugin-src/host/channels/slack/rpc.mjs +221 -0
- package/plugin-src/host/channels/telegram/index.mjs +33 -0
- package/plugin-src/host/channels/telegram/production.mjs +19 -0
- package/plugin-src/host/channels/telegram/rpc.mjs +22 -0
- package/plugin-src/host/channels/wecom/connection-supervisor.mjs +124 -0
- package/plugin-src/host/channels/wecom/index.mjs +34 -0
- package/plugin-src/host/channels/wecom/production.mjs +187 -0
- package/plugin-src/host/channels/wecom/rpc.mjs +255 -0
- package/plugin-src/host/channels/wecom-app/index.mjs +33 -0
- package/plugin-src/host/channels/wecom-app/production.mjs +221 -0
- package/plugin-src/host/channels/wecom-app/rpc.mjs +235 -0
- package/plugin-src/host/channels/weixin/connection-supervisor.mjs +139 -0
- package/plugin-src/host/channels/weixin/index.mjs +44 -0
- package/plugin-src/host/channels/weixin/production.mjs +257 -0
- package/plugin-src/host/channels/weixin/rpc.mjs +285 -0
- package/plugin-src/host/channels/whatsapp/index.mjs +33 -0
- package/plugin-src/host/channels/whatsapp/production.mjs +198 -0
- package/plugin-src/host/channels/whatsapp/rpc.mjs +242 -0
- package/plugin-src/host/delivery-adapter.mjs +388 -0
- package/plugin-src/host/delivery-http.mjs +132 -0
- package/plugin-src/host/delivery-rpc.mjs +173 -0
- package/plugin-src/host/delivery-service.mjs +552 -0
- package/plugin-src/host/delivery-suggestions.mjs +198 -0
- package/plugin-src/host/harness-command-executor.mjs +48 -0
- package/plugin-src/host/harness-connection.mjs +20 -0
- package/plugin-src/host/harness-session-coordinator.mjs +183 -0
- package/plugin-src/host/host-language-rpc.mjs +71 -0
- package/plugin-src/host/host-language.mjs +157 -0
- package/plugin-src/host/image-input-rpc.mjs +24 -0
- package/plugin-src/host/inbound-ttl-rpc.mjs +86 -0
- package/plugin-src/host/inbound-ttl-runtime.mjs +85 -0
- package/plugin-src/host/inbound-ttl-service.mjs +179 -0
- package/plugin-src/host/index.mjs +224 -0
- package/plugin-src/host/injected-context.mjs +104 -0
- package/plugin-src/host/lark-sdk-handshake-patch.mjs +181 -0
- package/plugin-src/host/modern-harness-api.mjs +738 -0
- package/plugin-src/host/rpc-authority.mjs +12 -0
- package/plugin-src/host/session-sync-coordinator.mjs +233 -0
- package/plugin-src/host/session-title-prefix.mjs +122 -0
- package/plugin-src/host/update-policy.mjs +31 -0
- package/plugin-src/host/update-rpc.mjs +16 -0
- package/plugin-src/host/update-runtime.mjs +339 -0
- package/plugin-src/host/update-service.mjs +392 -0
- package/plugin-src/management-rpc.mjs +87 -0
- package/src/channels/dingtalk/config-store.mjs +284 -0
- package/src/channels/dingtalk/connection-error.mjs +292 -0
- package/src/channels/dingtalk/device-auth.mjs +240 -0
- package/src/channels/dingtalk/dingtalk-api.mjs +1140 -0
- package/src/channels/dingtalk/dingtalk-bridge.mjs +1889 -0
- package/src/channels/dingtalk/dingtalk-card-stream.mjs +242 -0
- package/src/channels/dingtalk/dingtalk-controller.mjs +852 -0
- package/src/channels/dingtalk/dingtalk-menu.mjs +95 -0
- package/src/channels/dingtalk/dingtalk-runtime.mjs +482 -0
- package/src/channels/dingtalk/harness-client.mjs +19 -0
- package/src/channels/dingtalk/state-store.mjs +325 -0
- package/src/channels/discord/config-store.mjs +25 -0
- package/src/channels/discord/discord-api.mjs +347 -0
- package/src/channels/discord/discord-bridge.mjs +16 -0
- package/src/channels/discord/discord-controller.mjs +16 -0
- package/src/channels/discord/discord-runtime.mjs +974 -0
- package/src/channels/discord/harness-client.mjs +11 -0
- package/src/channels/discord/state-store.mjs +3 -0
- package/src/channels/email/config-store.mjs +194 -0
- package/src/channels/email/email-api.mjs +20 -0
- package/src/channels/email/email-bridge.mjs +17 -0
- package/src/channels/email/email-controller.mjs +824 -0
- package/src/channels/email/email-runtime.mjs +699 -0
- package/src/channels/email/harness-client.mjs +7 -0
- package/src/channels/email/mail-format.mjs +91 -0
- package/src/channels/email/state-store.mjs +176 -0
- package/src/channels/email/transport.mjs +64 -0
- package/src/channels/email/transports/agent-mail.mjs +604 -0
- package/src/channels/email/transports/agently-cli.mjs +288 -0
- package/src/channels/email/transports/imap-smtp.mjs +165 -0
- package/src/channels/feishu/bridge.mjs +6286 -0
- package/src/channels/feishu/config.mjs +71 -0
- package/src/channels/feishu/external-consumer.mjs +77 -0
- package/src/channels/feishu/external-files.mjs +111 -0
- package/src/channels/feishu/feishu-app.mjs +54 -0
- package/src/channels/feishu/feishu-cards.mjs +1285 -0
- package/src/channels/feishu/feishu-channel.mjs +651 -0
- package/src/channels/feishu/feishu-runtime.mjs +950 -0
- package/src/channels/feishu/group-message-permission-manager.mjs +72 -0
- package/src/channels/feishu/group-response-mode.mjs +15 -0
- package/src/channels/feishu/harness-client.mjs +19 -0
- package/src/channels/feishu/history-reader.mjs +77 -0
- package/src/channels/feishu/live-cot.mjs +260 -0
- package/src/channels/feishu/message-utils.mjs +613 -0
- package/src/channels/feishu/multi-bot-controller.mjs +1588 -0
- package/src/channels/feishu/nearby-history.mjs +72 -0
- package/src/channels/feishu/plugin-config-store.mjs +216 -0
- package/src/channels/feishu/plugin-controller.mjs +254 -0
- package/src/channels/feishu/registration-manager.mjs +349 -0
- package/src/channels/feishu/repair-manager.mjs +138 -0
- package/src/channels/feishu/reply-context.mjs +22 -0
- package/src/channels/feishu/slash-command-panel.mjs +155 -0
- package/src/channels/feishu/slash-command-registry.mjs +495 -0
- package/src/channels/feishu/state-store.mjs +214 -0
- package/src/channels/feishu/step-push-mode.mjs +25 -0
- package/src/channels/feishu/voice-config.mjs +34 -0
- package/src/channels/feishu/voice.mjs +134 -0
- package/src/channels/imessage/config-store.mjs +24 -0
- package/src/channels/imessage/controller.mjs +37 -0
- package/src/channels/imessage/harness-client.mjs +7 -0
- package/src/channels/imessage/imessage-api.mjs +194 -0
- package/src/channels/imessage/imessage-bridge.mjs +16 -0
- package/src/channels/imessage/runtime.mjs +102 -0
- package/src/channels/imessage/state-store.mjs +3 -0
- package/src/channels/matrix/matrix-api.mjs +696 -0
- package/src/channels/matrix/matrix-bridge.mjs +20 -0
- package/src/channels/matrix/matrix-config-store.mjs +368 -0
- package/src/channels/matrix/matrix-controller.mjs +404 -0
- package/src/channels/matrix/matrix-crypto-store.mjs +279 -0
- package/src/channels/matrix/matrix-crypto.mjs +1014 -0
- package/src/channels/matrix/matrix-harness-client.mjs +11 -0
- package/src/channels/matrix/matrix-normalize.mjs +357 -0
- package/src/channels/matrix/matrix-rich-text.mjs +313 -0
- package/src/channels/matrix/matrix-room-history.mjs +316 -0
- package/src/channels/matrix/matrix-runtime.mjs +1014 -0
- package/src/channels/office/config-store.mjs +107 -0
- package/src/channels/office/office-controller.mjs +178 -0
- package/src/channels/office/office-job-executor.mjs +391 -0
- package/src/channels/office/office-runtime.mjs +175 -0
- package/src/channels/office/office-transport.mjs +171 -0
- package/src/channels/office/protocol.mjs +43 -0
- package/src/channels/qq/config-store.mjs +173 -0
- package/src/channels/qq/harness-client.mjs +11 -0
- package/src/channels/qq/markdown-reply.mjs +446 -0
- package/src/channels/qq/qq-bridge.mjs +1507 -0
- package/src/channels/qq/qq-controller.mjs +514 -0
- package/src/channels/qq/qq-menu.mjs +208 -0
- package/src/channels/qq/qq-runtime.mjs +303 -0
- package/src/channels/qq/qr-auth.mjs +24 -0
- package/src/channels/qq/state-error.mjs +17 -0
- package/src/channels/qq/state-store.mjs +143 -0
- package/src/channels/shared/access-policy.mjs +210 -0
- package/src/channels/shared/agent-preset.mjs +76 -0
- package/src/channels/shared/batch-input.mjs +229 -0
- package/src/channels/shared/bot-alias.mjs +29 -0
- package/src/channels/shared/bot-workspace-store.mjs +2383 -0
- package/src/channels/shared/command-catalog.mjs +107 -0
- package/src/channels/shared/command-permission.mjs +32 -0
- package/src/channels/shared/compact-command.mjs +109 -0
- package/src/channels/shared/config-read-error.mjs +24 -0
- package/src/channels/shared/connection-error.mjs +191 -0
- package/src/channels/shared/connection-test.mjs +58 -0
- package/src/channels/shared/context-enhancement.mjs +372 -0
- package/src/channels/shared/control-command.mjs +111 -0
- package/src/channels/shared/conversation-state-store.mjs +165 -0
- package/src/channels/shared/default-workspace.mjs +31 -0
- package/src/channels/shared/deferred-delivery-coordinator.mjs +241 -0
- package/src/channels/shared/deferred-delivery.mjs +161 -0
- package/src/channels/shared/deferred-state.mjs +53 -0
- package/src/channels/shared/diagnostic-details.mjs +95 -0
- package/src/channels/shared/editable-message-stream.mjs +100 -0
- package/src/channels/shared/exclusive-inbound-consumers.mjs +71 -0
- package/src/channels/shared/file-download.mjs +64 -0
- package/src/channels/shared/harness-approval.mjs +508 -0
- package/src/channels/shared/harness-client.mjs +2143 -0
- package/src/channels/shared/harness-mux.mjs +123 -0
- package/src/channels/shared/harness-question.mjs +95 -0
- package/src/channels/shared/harness-session-binding.mjs +119 -0
- package/src/channels/shared/history-command.mjs +193 -0
- package/src/channels/shared/i18n-en/diagnostics.mjs +55 -0
- package/src/channels/shared/i18n-en/dingtalk.mjs +81 -0
- package/src/channels/shared/i18n-en/discord.mjs +10 -0
- package/src/channels/shared/i18n-en/email.mjs +25 -0
- package/src/channels/shared/i18n-en/feishu.mjs +404 -0
- package/src/channels/shared/i18n-en/image-input.mjs +18 -0
- package/src/channels/shared/i18n-en/matrix.mjs +85 -0
- package/src/channels/shared/i18n-en/office.mjs +23 -0
- package/src/channels/shared/i18n-en/qq.mjs +96 -0
- package/src/channels/shared/i18n-en/shared-a.mjs +241 -0
- package/src/channels/shared/i18n-en/shared-b.mjs +263 -0
- package/src/channels/shared/i18n-en/shared-c.mjs +203 -0
- package/src/channels/shared/i18n-en/slack.mjs +27 -0
- package/src/channels/shared/i18n-en/telegram.mjs +27 -0
- package/src/channels/shared/i18n-en/wecom-app.mjs +33 -0
- package/src/channels/shared/i18n-en/wecom.mjs +62 -0
- package/src/channels/shared/i18n-en/weixin.mjs +88 -0
- package/src/channels/shared/i18n-en/whatsapp.mjs +17 -0
- package/src/channels/shared/i18n-en.mjs +45 -0
- package/src/channels/shared/i18n.mjs +78 -0
- package/src/channels/shared/im-source-guidance.mjs +65 -0
- package/src/channels/shared/image-input-policy.mjs +35 -0
- package/src/channels/shared/image-input-settings-store.mjs +60 -0
- package/src/channels/shared/image-input.mjs +124 -0
- package/src/channels/shared/image-prompt.mjs +360 -0
- package/src/channels/shared/inbound-access.mjs +43 -0
- package/src/channels/shared/inbound-file.mjs +323 -0
- package/src/channels/shared/inbound-ttl-store.mjs +110 -0
- package/src/channels/shared/inbound-ttl.mjs +22 -0
- package/src/channels/shared/injected-context.mjs +362 -0
- package/src/channels/shared/interface-language-store.mjs +127 -0
- package/src/channels/shared/interface-language.mjs +51 -0
- package/src/channels/shared/message-failure.mjs +257 -0
- package/src/channels/shared/model-command.mjs +741 -0
- package/src/channels/shared/model-setting.mjs +145 -0
- package/src/channels/shared/preset-command.mjs +308 -0
- package/src/channels/shared/semantic/artifact-delivery.mjs +177 -0
- package/src/channels/shared/semantic/artifact.mjs +750 -0
- package/src/channels/shared/semantic/delivery.mjs +193 -0
- package/src/channels/shared/semantic/reply-reference.mjs +148 -0
- package/src/channels/shared/session-binding-lock.mjs +40 -0
- package/src/channels/shared/session-channel-labels.mjs +28 -0
- package/src/channels/shared/session-reply-recovery.mjs +104 -0
- package/src/channels/shared/session-sync-registry.mjs +22 -0
- package/src/channels/shared/session-title.mjs +74 -0
- package/src/channels/shared/status-reaction.mjs +107 -0
- package/src/channels/shared/text-harness-bridge.mjs +1623 -0
- package/src/channels/shared/token-bot-controller.mjs +417 -0
- package/src/channels/shared/token-config-store.mjs +208 -0
- package/src/channels/shared/workspace-command.mjs +532 -0
- package/src/channels/shared/workspace-session.mjs +196 -0
- package/src/channels/slack/config-store.mjs +177 -0
- package/src/channels/slack/external-consumer.mjs +80 -0
- package/src/channels/slack/external-files.mjs +90 -0
- package/src/channels/slack/harness-client.mjs +11 -0
- package/src/channels/slack/history-reader.mjs +203 -0
- package/src/channels/slack/manifest.mjs +37 -0
- package/src/channels/slack/slack-api.mjs +636 -0
- package/src/channels/slack/slack-bridge.mjs +20 -0
- package/src/channels/slack/slack-controller.mjs +468 -0
- package/src/channels/slack/slack-runtime.mjs +879 -0
- package/src/channels/slack/state-store.mjs +3 -0
- package/src/channels/telegram/config-store.mjs +89 -0
- package/src/channels/telegram/harness-client.mjs +11 -0
- package/src/channels/telegram/state-store.mjs +3 -0
- package/src/channels/telegram/telegram-api.mjs +486 -0
- package/src/channels/telegram/telegram-bridge.mjs +81 -0
- package/src/channels/telegram/telegram-controller.mjs +52 -0
- package/src/channels/telegram/telegram-http.mjs +21 -0
- package/src/channels/telegram/telegram-rich-message.mjs +149 -0
- package/src/channels/telegram/telegram-runtime.mjs +1471 -0
- package/src/channels/wecom/config-store.mjs +172 -0
- package/src/channels/wecom/harness-client.mjs +11 -0
- package/src/channels/wecom/qr-auth.mjs +95 -0
- package/src/channels/wecom/send-error.mjs +31 -0
- package/src/channels/wecom/state-store.mjs +107 -0
- package/src/channels/wecom/wecom-bridge.mjs +1833 -0
- package/src/channels/wecom/wecom-cards.mjs +101 -0
- package/src/channels/wecom/wecom-controller.mjs +530 -0
- package/src/channels/wecom/wecom-runtime.mjs +269 -0
- package/src/channels/wecom-app/callback-server.mjs +471 -0
- package/src/channels/wecom-app/config-store.mjs +242 -0
- package/src/channels/wecom-app/harness-client.mjs +11 -0
- package/src/channels/wecom-app/state-store.mjs +107 -0
- package/src/channels/wecom-app/wecom-app-api.mjs +432 -0
- package/src/channels/wecom-app/wecom-app-bridge.mjs +1072 -0
- package/src/channels/wecom-app/wecom-app-controller.mjs +453 -0
- package/src/channels/wecom-app/wecom-app-runtime.mjs +222 -0
- package/src/channels/weixin/config-store.mjs +192 -0
- package/src/channels/weixin/connection-error.en.mjs +137 -0
- package/src/channels/weixin/connection-error.mjs +153 -0
- package/src/channels/weixin/diagnostic-details.mjs +1 -0
- package/src/channels/weixin/harness-client.mjs +19 -0
- package/src/channels/weixin/state-store.mjs +291 -0
- package/src/channels/weixin/weixin-api.mjs +949 -0
- package/src/channels/weixin/weixin-bridge.mjs +1537 -0
- package/src/channels/weixin/weixin-controller.mjs +669 -0
- package/src/channels/weixin/weixin-runtime.mjs +416 -0
- package/src/channels/whatsapp/config-store.mjs +210 -0
- package/src/channels/whatsapp/harness-client.mjs +11 -0
- package/src/channels/whatsapp/state-store.mjs +3 -0
- package/src/channels/whatsapp/whatsapp-bridge.mjs +18 -0
- package/src/channels/whatsapp/whatsapp-controller.mjs +470 -0
- package/src/channels/whatsapp/whatsapp-runtime.mjs +917 -0
- package/src/channels/whatsapp/whatsapp-web-session.mjs +214 -0
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { workspacePathSnapshot } from '../shared/workspace-command.mjs';
|
|
3
|
+
import { t } from '../shared/i18n.mjs';
|
|
4
|
+
|
|
5
|
+
export const DINGTALK_MENU_TEMPLATE_ID = '9ca31362-e5b3-409c-a694-6993706a6004.schema';
|
|
6
|
+
export const DINGTALK_CARD_TOPIC = '/v1.0/card/instances/callback';
|
|
7
|
+
export const DINGTALK_MENU_BUTTONS = Object.freeze({
|
|
8
|
+
new: '/new', history: '/history', stop: '/stop', compact: '/compact',
|
|
9
|
+
status: '/status', help: '/help',
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
export function isDingtalkMenuCommand(text) {
|
|
13
|
+
return typeof text === 'string' && /^\/(?:m|menu)$/iu.test(text.trim());
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const presetCommand = (id) => `/preset ${/^\d+$/u.test(id) ? 'id:' : ''}${id}`;
|
|
17
|
+
|
|
18
|
+
// Keep exact commands alongside the displayed options. Never interpret a callback
|
|
19
|
+
// as a command supplied by the client or as a prompt for the model.
|
|
20
|
+
export async function dingtalkMenuSnapshot(harness, state, key, signal) {
|
|
21
|
+
const workspace = harness.currentWorkspace?.();
|
|
22
|
+
const currentSessionWorkspace = () => typeof harness.currentConversationWorkspace === 'function'
|
|
23
|
+
? harness.currentConversationWorkspace(key) : harness.currentWorkspace?.();
|
|
24
|
+
const sessionWorkspace = currentSessionWorkspace();
|
|
25
|
+
const sessionId = state.sessionFor(key);
|
|
26
|
+
const options = { signal };
|
|
27
|
+
const results = await Promise.allSettled([
|
|
28
|
+
workspacePathSnapshot(harness, options),
|
|
29
|
+
harness.listWorkspaceSessions?.(sessionWorkspace, options),
|
|
30
|
+
(async () => {
|
|
31
|
+
const session = sessionId ? harness.workspaceSession?.(sessionId, key) : null;
|
|
32
|
+
return typeof session?.models === 'function'
|
|
33
|
+
? session.models(options) : harness.listModels?.(options);
|
|
34
|
+
})(),
|
|
35
|
+
harness.agentPresetSettings?.(options),
|
|
36
|
+
]);
|
|
37
|
+
signal?.throwIfAborted();
|
|
38
|
+
if (harness.currentWorkspace?.() !== workspace
|
|
39
|
+
|| currentSessionWorkspace() !== sessionWorkspace || state.sessionFor(key) !== sessionId) {
|
|
40
|
+
throw new Error(t('会话或工作区已变化,请重新发送 /m。'));
|
|
41
|
+
}
|
|
42
|
+
const [paths, listed, catalog, settings] = results.map((r) => r.status === 'fulfilled' ? r.value : null);
|
|
43
|
+
const selections = {
|
|
44
|
+
session: [[t('新会话'), '/new'], ...(listed?.sessions ?? []).map((item) => [
|
|
45
|
+
item.title || item.sessionId, `/session ${item.sessionId}`,
|
|
46
|
+
])],
|
|
47
|
+
workspace: (paths?.paths ?? (workspace ? [workspace] : [])).map((path) => [path, `/workspace ${path}`]),
|
|
48
|
+
preset: [[t('跟随 Host 默认'), '/preset --default'], ...(settings?.agentPresetCatalog?.items ?? []).map((item) => [
|
|
49
|
+
item.label || item.id, presetCommand(item.id),
|
|
50
|
+
])],
|
|
51
|
+
model: (catalog?.groups ?? []).flatMap((group) => group.models.map((model) => [
|
|
52
|
+
`${model.name} (${group.id})`, `/model ${group.id}/${model.id}`,
|
|
53
|
+
])),
|
|
54
|
+
};
|
|
55
|
+
const current = {
|
|
56
|
+
session: sessionId ? `/session ${sessionId}` : '/new',
|
|
57
|
+
workspace: `/workspace ${workspace}`,
|
|
58
|
+
preset: settings?.agentPreset ? presetCommand(settings.agentPreset) : '/preset --default',
|
|
59
|
+
model: catalog?.current ? `/model ${catalog.current.provider}/${catalog.current.model}` : null,
|
|
60
|
+
};
|
|
61
|
+
const data = { revision: randomUUID(), notice: '' };
|
|
62
|
+
for (const [name, entries] of Object.entries(selections)) {
|
|
63
|
+
// Keep the active value visible even when the session listing is paginated.
|
|
64
|
+
if (current[name] && !entries.some(([, command]) => command === current[name])) {
|
|
65
|
+
entries.unshift([name === 'session' ? sessionId : current[name].replace(/^\/\w+\s+/u, ''), current[name]]);
|
|
66
|
+
}
|
|
67
|
+
data[`${name}_options`] = entries.map(([label], index) => ({
|
|
68
|
+
value: String(index), text: { zh_CN: label, en_US: label },
|
|
69
|
+
}));
|
|
70
|
+
data[`${name}_index`] = entries.findIndex(([, command]) => command === current[name]);
|
|
71
|
+
}
|
|
72
|
+
return { workspace, sessionWorkspace, sessionId, selections, data };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function dingtalkMenuCommand(entry, callback) {
|
|
76
|
+
let content;
|
|
77
|
+
try {
|
|
78
|
+
content = typeof callback?.content === 'string' ? JSON.parse(callback.content) : callback?.content;
|
|
79
|
+
} catch { return null; }
|
|
80
|
+
const { actionIds, params } = content?.cardPrivateData ?? {};
|
|
81
|
+
if (!Array.isArray(actionIds) || actionIds.length !== 1
|
|
82
|
+
|| params?.revision !== entry.data.revision) return null;
|
|
83
|
+
const action = actionIds[0];
|
|
84
|
+
if (Object.hasOwn(DINGTALK_MENU_BUTTONS, action)) return DINGTALK_MENU_BUTTONS[action];
|
|
85
|
+
const entries = entry.selections[action];
|
|
86
|
+
const index = params[action]?.index;
|
|
87
|
+
if (!Array.isArray(entries) || !Number.isSafeInteger(index) || index < 0) return null;
|
|
88
|
+
return entries[index]?.[1] ?? null;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function dingtalkMenuCardData(data) {
|
|
92
|
+
return { cardParamMap: Object.fromEntries(Object.entries(data).map(([key, value]) => [
|
|
93
|
+
key, typeof value === 'string' ? value : JSON.stringify(value),
|
|
94
|
+
])) };
|
|
95
|
+
}
|
|
@@ -0,0 +1,482 @@
|
|
|
1
|
+
import { atConnectionStage, createConnectionDiagnostics } from '../shared/connection-error.mjs';
|
|
2
|
+
import { createDingtalkApi } from './dingtalk-api.mjs';
|
|
3
|
+
import {
|
|
4
|
+
createDingtalkBridgeStatus,
|
|
5
|
+
DingtalkHarnessBridge,
|
|
6
|
+
} from './dingtalk-bridge.mjs';
|
|
7
|
+
import { sendRememberedConnectionTest } from '../shared/connection-test.mjs';
|
|
8
|
+
import { t } from '../shared/i18n.mjs';
|
|
9
|
+
import { captureContextEnhancement } from '../shared/context-enhancement.mjs';
|
|
10
|
+
import { dingtalkRuntimeStartError } from './connection-error.mjs';
|
|
11
|
+
import { DINGTALK_CARD_TOPIC } from './dingtalk-menu.mjs';
|
|
12
|
+
|
|
13
|
+
function nonEmptyString(value) {
|
|
14
|
+
return typeof value === 'string' && value.trim() ? value.trim() : null;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function approvedSenderIds(config) {
|
|
18
|
+
const entries = Array.isArray(config?.approvedSenders)
|
|
19
|
+
? config.approvedSenders
|
|
20
|
+
: config?.approvedSenders instanceof Set
|
|
21
|
+
? [...config.approvedSenders]
|
|
22
|
+
: [];
|
|
23
|
+
return new Set(entries.map((entry) => nonEmptyString(
|
|
24
|
+
typeof entry === 'string' ? entry : entry?.staffId,
|
|
25
|
+
)).filter(Boolean));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function approvedSenderCount(config) {
|
|
29
|
+
return approvedSenderIds(config).size;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function streamIsOpen(client) {
|
|
33
|
+
return client?.connected === true || client?.socket?.readyState === 1;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function abortable(promise, signal) {
|
|
37
|
+
return new Promise((resolve, reject) => {
|
|
38
|
+
if (signal.aborted) {
|
|
39
|
+
reject(signal.reason);
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
const onAbort = () => reject(signal.reason);
|
|
43
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
44
|
+
Promise.resolve(promise).then(
|
|
45
|
+
(value) => {
|
|
46
|
+
signal.removeEventListener('abort', onAbort);
|
|
47
|
+
resolve(value);
|
|
48
|
+
},
|
|
49
|
+
(error) => {
|
|
50
|
+
signal.removeEventListener('abort', onAbort);
|
|
51
|
+
reject(error);
|
|
52
|
+
},
|
|
53
|
+
);
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async function waitForStreamOpen(client, pollIntervalMs, signal) {
|
|
58
|
+
while (true) {
|
|
59
|
+
signal?.throwIfAborted();
|
|
60
|
+
if (streamIsOpen(client)) return;
|
|
61
|
+
await new Promise((resolve, reject) => {
|
|
62
|
+
const timer = setTimeout(() => {
|
|
63
|
+
signal?.removeEventListener('abort', onAbort);
|
|
64
|
+
resolve();
|
|
65
|
+
}, pollIntervalMs);
|
|
66
|
+
const onAbort = () => {
|
|
67
|
+
clearTimeout(timer);
|
|
68
|
+
reject(signal.reason ?? new DOMException('Aborted', 'AbortError'));
|
|
69
|
+
};
|
|
70
|
+
signal?.addEventListener('abort', onAbort, { once: true });
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async function connectStream(client, timeoutMs, pollIntervalMs, signal) {
|
|
76
|
+
const timeoutSignal = AbortSignal.timeout(timeoutMs);
|
|
77
|
+
const connectSignal = AbortSignal.any([signal, timeoutSignal]);
|
|
78
|
+
let connectSettled = false;
|
|
79
|
+
const connectTask = Promise.resolve()
|
|
80
|
+
.then(() => client.connect())
|
|
81
|
+
.finally(() => { connectSettled = true; });
|
|
82
|
+
try {
|
|
83
|
+
await abortable(connectTask, connectSignal);
|
|
84
|
+
await waitForStreamOpen(client, pollIntervalMs, connectSignal);
|
|
85
|
+
} catch (error) {
|
|
86
|
+
if (connectSignal.aborted) {
|
|
87
|
+
if (!connectSettled) {
|
|
88
|
+
void connectTask.then(() => client.disconnect()).catch(() => undefined);
|
|
89
|
+
}
|
|
90
|
+
if (signal.aborted) throw signal.reason;
|
|
91
|
+
throw Object.assign(new Error(`DingTalk Stream handshake timed out after ${timeoutMs}ms`), { code: 'ETIMEDOUT', timeoutMs, cause: error });
|
|
92
|
+
}
|
|
93
|
+
throw error;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async function defaultStreamFactory({ clientId, clientSecret }) {
|
|
98
|
+
const { DWClient, TOPIC_ROBOT } = await import('dingtalk-stream');
|
|
99
|
+
return {
|
|
100
|
+
client: new DWClient({
|
|
101
|
+
clientId,
|
|
102
|
+
clientSecret,
|
|
103
|
+
endpoint: 'https://api.dingtalk.com',
|
|
104
|
+
autoReconnect: false,
|
|
105
|
+
keepAlive: true,
|
|
106
|
+
debug: false,
|
|
107
|
+
}),
|
|
108
|
+
topic: TOPIC_ROBOT,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function createDingtalkRuntimeStatus({
|
|
113
|
+
pendingSenders = [],
|
|
114
|
+
approvedSenders = 0,
|
|
115
|
+
} = {}) {
|
|
116
|
+
return {
|
|
117
|
+
startedAt: null,
|
|
118
|
+
ready: false,
|
|
119
|
+
dingtalkStreamState: 'idle',
|
|
120
|
+
harnessReachable: false,
|
|
121
|
+
lastConnectedAt: null,
|
|
122
|
+
lastCheckedAt: null,
|
|
123
|
+
lastCallbackAt: null,
|
|
124
|
+
authorizationMode: 'sender-staff-id-approval',
|
|
125
|
+
approvedSenderCount: approvedSenders,
|
|
126
|
+
...createDingtalkBridgeStatus({ pendingSenders }),
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export class DingtalkRuntime {
|
|
131
|
+
#config;
|
|
132
|
+
#clientSecret;
|
|
133
|
+
#harness;
|
|
134
|
+
#state;
|
|
135
|
+
#contextEnhancement;
|
|
136
|
+
#accessPolicy;
|
|
137
|
+
#logger;
|
|
138
|
+
#diagnostics;
|
|
139
|
+
#replyTimeoutMs;
|
|
140
|
+
#maxMessageChars;
|
|
141
|
+
#connectTimeoutMs;
|
|
142
|
+
#connectPollIntervalMs;
|
|
143
|
+
#api;
|
|
144
|
+
#streamFactory;
|
|
145
|
+
#status;
|
|
146
|
+
#client = null;
|
|
147
|
+
#bridge = null;
|
|
148
|
+
#topic = null;
|
|
149
|
+
#starting = null;
|
|
150
|
+
#connectionMonitor = null;
|
|
151
|
+
#abortController = null;
|
|
152
|
+
#callbackTasks = new Set();
|
|
153
|
+
|
|
154
|
+
constructor({
|
|
155
|
+
config,
|
|
156
|
+
clientSecret,
|
|
157
|
+
harness,
|
|
158
|
+
state,
|
|
159
|
+
contextEnhancement,
|
|
160
|
+
accessPolicy,
|
|
161
|
+
logger = console,
|
|
162
|
+
replyTimeoutMs = 600_000,
|
|
163
|
+
maxMessageChars = 4_000,
|
|
164
|
+
connectTimeoutMs = 15_000,
|
|
165
|
+
connectPollIntervalMs = 25,
|
|
166
|
+
api = createDingtalkApi(),
|
|
167
|
+
streamFactory = defaultStreamFactory,
|
|
168
|
+
}) {
|
|
169
|
+
if (!config || !nonEmptyString(config.clientId) || !nonEmptyString(clientSecret)) {
|
|
170
|
+
throw new TypeError('DingtalkRuntime requires app credentials');
|
|
171
|
+
}
|
|
172
|
+
if (!harness || !state) throw new TypeError('DingtalkRuntime requires Harness and state');
|
|
173
|
+
if (typeof streamFactory !== 'function') throw new TypeError('streamFactory must be a function');
|
|
174
|
+
this.#config = config;
|
|
175
|
+
this.#clientSecret = clientSecret.trim();
|
|
176
|
+
this.#harness = harness;
|
|
177
|
+
this.#state = state;
|
|
178
|
+
this.#contextEnhancement = contextEnhancement;
|
|
179
|
+
this.#accessPolicy = accessPolicy;
|
|
180
|
+
this.#logger = logger;
|
|
181
|
+
this.#diagnostics = createConnectionDiagnostics({ channel: 'dingtalk', logger, prefix: 'DT-CONN' });
|
|
182
|
+
this.#replyTimeoutMs = replyTimeoutMs;
|
|
183
|
+
this.#maxMessageChars = maxMessageChars;
|
|
184
|
+
this.#connectTimeoutMs = connectTimeoutMs;
|
|
185
|
+
this.#connectPollIntervalMs = connectPollIntervalMs;
|
|
186
|
+
this.#api = api;
|
|
187
|
+
this.#streamFactory = streamFactory;
|
|
188
|
+
this.#status = createDingtalkRuntimeStatus({
|
|
189
|
+
pendingSenders: this.#pendingSenders(),
|
|
190
|
+
approvedSenders: approvedSenderCount(config),
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
get status() {
|
|
195
|
+
if (this.#bridge) {
|
|
196
|
+
const bridgeStatus = this.#bridge.status;
|
|
197
|
+
Object.assign(this.#status, bridgeStatus);
|
|
198
|
+
} else {
|
|
199
|
+
this.#status.pendingSenders = this.#pendingSenders();
|
|
200
|
+
}
|
|
201
|
+
return structuredClone(this.#status);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
pendingSender(requestId) {
|
|
205
|
+
return typeof this.#state.pendingSender === 'function'
|
|
206
|
+
? this.#state.pendingSender(requestId)
|
|
207
|
+
: null;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
pendingSenders() {
|
|
211
|
+
return this.#pendingSenders();
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
async start() {
|
|
215
|
+
if (this.#client && this.#status.ready) return this.status;
|
|
216
|
+
if (this.#starting) return this.#starting;
|
|
217
|
+
this.#starting = this.#start().finally(() => {
|
|
218
|
+
this.#starting = null;
|
|
219
|
+
});
|
|
220
|
+
return this.#starting;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
async #start() {
|
|
224
|
+
await this.stop();
|
|
225
|
+
const abortController = new AbortController();
|
|
226
|
+
this.#abortController = abortController;
|
|
227
|
+
const { signal } = abortController;
|
|
228
|
+
this.#status.startedAt = new Date().toISOString();
|
|
229
|
+
this.#status.dingtalkStreamState = 'connecting';
|
|
230
|
+
this.#status.lastError = null;
|
|
231
|
+
let startStage = 'dingtalk-harness-connect-failed';
|
|
232
|
+
|
|
233
|
+
try {
|
|
234
|
+
await atConnectionStage('harness.check', () => this.#harness.ensureRunning({ signal }));
|
|
235
|
+
this.#status.harnessReachable = true;
|
|
236
|
+
startStage = 'dingtalk-runtime-prepare-failed';
|
|
237
|
+
if (typeof this.#state.removePendingSenderByStaffId === 'function') {
|
|
238
|
+
for (const staffId of approvedSenderIds(this.#config)) {
|
|
239
|
+
await this.#state.removePendingSenderByStaffId(staffId);
|
|
240
|
+
}
|
|
241
|
+
this.#status.pendingSenders = this.#pendingSenders();
|
|
242
|
+
}
|
|
243
|
+
this.#bridge = new DingtalkHarnessBridge({
|
|
244
|
+
api: this.#api,
|
|
245
|
+
clientId: this.#config.clientId,
|
|
246
|
+
clientSecret: this.#clientSecret,
|
|
247
|
+
approvedSenders: this.#config.approvedSenders,
|
|
248
|
+
harness: this.#harness,
|
|
249
|
+
state: this.#state,
|
|
250
|
+
contextEnhancement: this.#contextEnhancement,
|
|
251
|
+
accessPolicy: this.#accessPolicy,
|
|
252
|
+
status: this.#status,
|
|
253
|
+
logger: this.#logger,
|
|
254
|
+
replyTimeoutMs: this.#replyTimeoutMs,
|
|
255
|
+
maxMessageChars: this.#maxMessageChars,
|
|
256
|
+
signal,
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
startStage = 'dingtalk-stream-client-load-failed';
|
|
260
|
+
const created = await this.#streamFactory({
|
|
261
|
+
clientId: this.#config.clientId,
|
|
262
|
+
clientSecret: this.#clientSecret,
|
|
263
|
+
});
|
|
264
|
+
signal.throwIfAborted();
|
|
265
|
+
this.#client = created?.client ?? created;
|
|
266
|
+
this.#topic = created?.topic ?? created?.TOPIC_ROBOT ?? '/v1.0/im/bot/messages/get';
|
|
267
|
+
if (!this.#client
|
|
268
|
+
|| typeof this.#client.registerCallbackListener !== 'function'
|
|
269
|
+
|| typeof this.#client.connect !== 'function'
|
|
270
|
+
|| typeof this.#client.disconnect !== 'function'
|
|
271
|
+
|| typeof this.#client.socketCallBackResponse !== 'function') {
|
|
272
|
+
throw new TypeError('streamFactory returned an invalid DingTalk Stream client');
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const client = this.#client;
|
|
276
|
+
const bridge = this.#bridge;
|
|
277
|
+
startStage = 'dingtalk-stream-listener-failed';
|
|
278
|
+
client.registerCallbackListener(DINGTALK_CARD_TOPIC, (response) => {
|
|
279
|
+
if (this.#client !== client || this.#bridge !== bridge) return;
|
|
280
|
+
const messageId = nonEmptyString(response?.headers?.messageId);
|
|
281
|
+
if (messageId) {
|
|
282
|
+
try { client.socketCallBackResponse(messageId, { success: true }); }
|
|
283
|
+
catch { this.#logger.warn?.('[dsh-dingtalk] unable to acknowledge a card callback'); }
|
|
284
|
+
}
|
|
285
|
+
const task = Promise.resolve().then(async () => {
|
|
286
|
+
if (this.#bridge !== bridge) return;
|
|
287
|
+
const callback = typeof response?.data === 'string' ? JSON.parse(response.data) : response?.data;
|
|
288
|
+
await bridge.acceptCard(callback, messageId);
|
|
289
|
+
}).catch(() => {
|
|
290
|
+
if (!signal.aborted) this.#logger.warn?.('[dsh-dingtalk] card callback processing failed');
|
|
291
|
+
}).finally(() => this.#callbackTasks.delete(task));
|
|
292
|
+
this.#callbackTasks.add(task);
|
|
293
|
+
});
|
|
294
|
+
client.registerCallbackListener(this.#topic, (response) => {
|
|
295
|
+
if (this.#client !== client || this.#bridge !== bridge) return;
|
|
296
|
+
const callbackMessageId = nonEmptyString(response?.headers?.messageId);
|
|
297
|
+
if (callbackMessageId) {
|
|
298
|
+
try {
|
|
299
|
+
client.socketCallBackResponse(callbackMessageId, { success: true });
|
|
300
|
+
} catch {
|
|
301
|
+
this.#logger.warn?.('[dsh-dingtalk] unable to acknowledge an inbound callback');
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// Retain the committed, read-only settings at receipt without moving
|
|
306
|
+
// JSON parsing out of the existing asynchronous callback path.
|
|
307
|
+
const contextEnhancement = this.#contextEnhancement;
|
|
308
|
+
let receivedSettings;
|
|
309
|
+
try {
|
|
310
|
+
receivedSettings = contextEnhancement?.getSettings?.();
|
|
311
|
+
} catch {
|
|
312
|
+
// Optional settings failures leave the original message path active.
|
|
313
|
+
}
|
|
314
|
+
const task = Promise.resolve().then(async () => {
|
|
315
|
+
if (this.#bridge !== bridge) return;
|
|
316
|
+
let message;
|
|
317
|
+
try {
|
|
318
|
+
message = typeof response?.data === 'string'
|
|
319
|
+
? JSON.parse(response.data)
|
|
320
|
+
: response?.data;
|
|
321
|
+
} catch {
|
|
322
|
+
this.#status.lastError = t('钉钉消息格式无效。');
|
|
323
|
+
this.#logger.warn?.('[dsh-dingtalk] ignored an invalid callback payload');
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
if (!message || typeof message !== 'object') return;
|
|
327
|
+
this.#status.lastCallbackAt = Date.now();
|
|
328
|
+
const contextSnapshot = captureContextEnhancement({
|
|
329
|
+
getSettings: () => receivedSettings,
|
|
330
|
+
get botId() { return contextEnhancement?.botId; },
|
|
331
|
+
}, message.conversationType === '1' || message.conversationType === 1 ? 'direct'
|
|
332
|
+
: message.conversationType === '2' || message.conversationType === 2 ? 'group' : null);
|
|
333
|
+
await bridge.accept(message, { contextSnapshot });
|
|
334
|
+
}).catch(() => {
|
|
335
|
+
if (signal.aborted || this.#bridge !== bridge) return;
|
|
336
|
+
this.#status.lastError = t('钉钉消息处理失败。');
|
|
337
|
+
this.#logger.error?.('[dsh-dingtalk] callback processing failed');
|
|
338
|
+
}).finally(() => this.#callbackTasks.delete(task));
|
|
339
|
+
this.#callbackTasks.add(task);
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
startStage = 'dingtalk-stream-connect-failed';
|
|
343
|
+
await connectStream(
|
|
344
|
+
client,
|
|
345
|
+
this.#connectTimeoutMs,
|
|
346
|
+
this.#connectPollIntervalMs,
|
|
347
|
+
signal,
|
|
348
|
+
);
|
|
349
|
+
this.#status.ready = true;
|
|
350
|
+
this.#status.dingtalkStreamState = 'connected';
|
|
351
|
+
this.#status.lastConnectedAt = Date.now();
|
|
352
|
+
this.#status.lastCheckedAt = Date.now();
|
|
353
|
+
this.#status.lastError = null;
|
|
354
|
+
this.#status.error = null;
|
|
355
|
+
this.#diagnostics.clear();
|
|
356
|
+
this.#connectionMonitor = setInterval(() => {
|
|
357
|
+
const connected = streamIsOpen(client);
|
|
358
|
+
this.#status.ready = connected;
|
|
359
|
+
this.#status.dingtalkStreamState = connected ? 'connected' : 'reconnecting';
|
|
360
|
+
this.#status.lastCheckedAt = Date.now();
|
|
361
|
+
if (connected) {
|
|
362
|
+
this.#status.lastError = null;
|
|
363
|
+
this.#status.error = null;
|
|
364
|
+
this.#diagnostics.clear();
|
|
365
|
+
} else {
|
|
366
|
+
this.#status.error = this.#diagnostics.report(null, { operation: 'connection.monitor', automatic: true, botId: this.#config.botId,
|
|
367
|
+
publicError: { code: 'stream-connect-failed', message: t('钉钉连接未就绪,请稍后重试。') } }).publicError;
|
|
368
|
+
this.#status.lastError = this.#status.error.message;
|
|
369
|
+
}
|
|
370
|
+
}, 1_000);
|
|
371
|
+
this.#connectionMonitor.unref?.();
|
|
372
|
+
return this.status;
|
|
373
|
+
} catch (error) {
|
|
374
|
+
const aborted = signal.aborted;
|
|
375
|
+
const failure = aborted ? error : dingtalkRuntimeStartError(startStage, error);
|
|
376
|
+
this.#status.ready = false;
|
|
377
|
+
this.#status.dingtalkStreamState = aborted ? 'idle' : 'failed';
|
|
378
|
+
this.#status.lastError = aborted ? null : t('钉钉连接未就绪,请稍后重试。');
|
|
379
|
+
await this.stop({ preserveError: !aborted });
|
|
380
|
+
throw failure;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
async stop({ preserveError = false } = {}) {
|
|
385
|
+
const lastError = preserveError ? this.#status.lastError : null;
|
|
386
|
+
const publicError = preserveError ? this.#status.error : null;
|
|
387
|
+
const abortController = this.#abortController;
|
|
388
|
+
this.#abortController = null;
|
|
389
|
+
abortController?.abort(new DOMException('DingTalk runtime stopped', 'AbortError'));
|
|
390
|
+
if (this.#connectionMonitor) clearInterval(this.#connectionMonitor);
|
|
391
|
+
this.#connectionMonitor = null;
|
|
392
|
+
this.#status.ready = false;
|
|
393
|
+
|
|
394
|
+
const client = this.#client;
|
|
395
|
+
this.#client = null;
|
|
396
|
+
this.#topic = null;
|
|
397
|
+
if (client) {
|
|
398
|
+
try {
|
|
399
|
+
await client.disconnect();
|
|
400
|
+
} catch {
|
|
401
|
+
this.#logger.warn?.('[dsh-dingtalk] DingTalk Stream disconnect failed');
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
await Promise.allSettled([...this.#callbackTasks]);
|
|
405
|
+
this.#callbackTasks.clear();
|
|
406
|
+
if (this.#bridge) await this.#bridge.waitForIdle();
|
|
407
|
+
this.#bridge = null;
|
|
408
|
+
this.#status.dingtalkStreamState = preserveError ? 'failed' : 'idle';
|
|
409
|
+
this.#status.lastError = lastError;
|
|
410
|
+
this.#status.error = publicError;
|
|
411
|
+
if (!preserveError) this.#diagnostics.clear();
|
|
412
|
+
return this.status;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
async sendConnectionTest(text) {
|
|
416
|
+
return sendRememberedConnectionTest({
|
|
417
|
+
state: this.#state,
|
|
418
|
+
text,
|
|
419
|
+
channelLabel: t('钉钉机器人'),
|
|
420
|
+
send: async ({ sessionWebhook }, content) => {
|
|
421
|
+
if (!this.#status.ready || !this.#abortController) {
|
|
422
|
+
throw new Error('DingTalk runtime is not connected');
|
|
423
|
+
}
|
|
424
|
+
await this.#api.sendText({
|
|
425
|
+
clientId: this.#config.clientId,
|
|
426
|
+
clientSecret: this.#clientSecret,
|
|
427
|
+
sessionWebhook,
|
|
428
|
+
text: content,
|
|
429
|
+
signal: this.#abortController.signal,
|
|
430
|
+
});
|
|
431
|
+
},
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
async sendProactiveText(target, text, { signal } = {}) {
|
|
436
|
+
const userId = typeof target?.route?.userId === 'string'
|
|
437
|
+
? target.route.userId.trim() : '';
|
|
438
|
+
const openConversationId = typeof target?.route?.openConversationId === 'string'
|
|
439
|
+
? target.route.openConversationId.trim() : '';
|
|
440
|
+
if ((target?.kind === 'user' && (!userId || openConversationId))
|
|
441
|
+
|| (target?.kind === 'group' && (!openConversationId || userId))
|
|
442
|
+
|| (target?.kind !== 'user' && target?.kind !== 'group')) {
|
|
443
|
+
const error = new TypeError('Invalid DingTalk proactive delivery target');
|
|
444
|
+
error.code = 'invalid-target';
|
|
445
|
+
throw error;
|
|
446
|
+
}
|
|
447
|
+
if (!this.#status.ready || !this.#abortController) {
|
|
448
|
+
const error = new Error('DingTalk runtime is not connected');
|
|
449
|
+
error.code = 'bot-not-connected';
|
|
450
|
+
throw error;
|
|
451
|
+
}
|
|
452
|
+
signal?.throwIfAborted();
|
|
453
|
+
try {
|
|
454
|
+
await this.#api.sendRobotText({
|
|
455
|
+
clientId: this.#config.clientId,
|
|
456
|
+
clientSecret: this.#clientSecret,
|
|
457
|
+
target: {
|
|
458
|
+
type: target.kind,
|
|
459
|
+
robotCode: this.#config.clientId,
|
|
460
|
+
...(target.kind === 'user' ? { userId } : { openConversationId }),
|
|
461
|
+
},
|
|
462
|
+
text,
|
|
463
|
+
signal: signal ?? this.#abortController.signal,
|
|
464
|
+
});
|
|
465
|
+
} catch (cause) {
|
|
466
|
+
if (cause?.code !== 'send-rejected') throw cause;
|
|
467
|
+
const error = new Error('DingTalk rejected the proactive delivery target', { cause });
|
|
468
|
+
error.code = 'target-rejected';
|
|
469
|
+
throw error;
|
|
470
|
+
}
|
|
471
|
+
return { sent: true };
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
#pendingSenders() {
|
|
475
|
+
return typeof this.#state.pendingSenders === 'function'
|
|
476
|
+
? this.#state.pendingSenders()
|
|
477
|
+
: [];
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
export const DingTalkRuntime = DingtalkRuntime;
|
|
482
|
+
export const createDingTalkRuntimeStatus = createDingtalkRuntimeStatus;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import {
|
|
2
|
+
HarnessClient as SharedHarnessClient,
|
|
3
|
+
} from '../shared/harness-client.mjs';
|
|
4
|
+
|
|
5
|
+
export {
|
|
6
|
+
HarnessInteractionError,
|
|
7
|
+
HarnessReplyTracker,
|
|
8
|
+
HarnessRpcError,
|
|
9
|
+
} from '../shared/harness-client.mjs';
|
|
10
|
+
|
|
11
|
+
export class HarnessClient extends SharedHarnessClient {
|
|
12
|
+
constructor(options) {
|
|
13
|
+
super({
|
|
14
|
+
...options,
|
|
15
|
+
rpcIdPrefix: 'dingtalk',
|
|
16
|
+
logPrefix: 'dsh-dingtalk',
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
}
|