@gaodefa/daocore 2026.5.29 → 2026.5.30
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/dist/abort-DI3P6TIb.js +277 -0
- package/dist/abort.runtime-B6Z7lDEa.js +2 -0
- package/dist/abort.runtime.js +1 -1
- package/dist/account-inspect-BFz7X0TX.js +173 -0
- package/dist/accounts-BoYYrY7x.js +107 -0
- package/dist/accounts-Busq29zW.js +107 -0
- package/dist/accounts-CuvH2tuN.js +119 -0
- package/dist/accounts-DkIcqvf7.js +2 -0
- package/dist/acp-runtime-BA8lzXzf.js +26 -0
- package/dist/acp-spawn-DPz2aX3A.js +2 -0
- package/dist/acp-spawn-DvVCsWYV.js +1275 -0
- package/dist/acp-stateful-target-driver-DcC76aZW.js +89 -0
- package/dist/action-kill-D-dVisIX.js +33 -0
- package/dist/action-runtime-BEiqwxDD.js +469 -0
- package/dist/action-runtime-api-1LzcGjcX.js +2 -0
- package/dist/action-send-c3CnDuPg.js +39 -0
- package/dist/action-spawn-Dj4kwpWs.js +47 -0
- package/dist/actions-qLccmQBc.js +161 -0
- package/dist/actions.runtime-B5vYAgNi.js +5 -0
- package/dist/agent-command-D6Ds198G.js +1367 -0
- package/dist/agent-components.runtime-xquK0B3L.js +10 -0
- package/dist/agent-components.runtime.js +1 -1
- package/dist/agent-harness-runtime-Dq1fCBOM.js +180 -0
- package/dist/agent-harness-task-runtime-DvSEqFkD.js +140 -0
- package/dist/agent-lm5ZYOFy.js +3 -0
- package/dist/agent-runner-execution-Dbh2pDhQ.js +1713 -0
- package/dist/agent-runner-utils-Di3r2T1T.js +266 -0
- package/dist/agent-runner.runtime-BW90EcGW.js +3455 -0
- package/dist/agent-runner.runtime.js +1 -1
- package/dist/agent-runtime-BgIT5Ytc.js +229 -0
- package/dist/agent-via-gateway-DMMS8-hK.js +463 -0
- package/dist/agent-ylolD8-V.js +2 -0
- package/dist/api-BB4vXLtW.js +2 -0
- package/dist/api-CUhM3KNB.js +134 -0
- package/dist/api-CewRHSbT.js +6 -0
- package/dist/api-CfKjNJlW.js +2 -0
- package/dist/api-Rsnqui1u.js +3 -0
- package/dist/api-k7AvxwHr.js +639 -0
- package/dist/apply-DQ4TQNMA.js +54 -0
- package/dist/apply-S_75Py4Y.js +41 -0
- package/dist/approval-handler.runtime-Cthxrstu.js +130 -0
- package/dist/assistant-Cp9pGaBw.js +291 -0
- package/dist/attachment-normalize-5R0bLLOj.js +225 -0
- package/dist/attempt-execution-DuyyZYsU.js +558 -0
- package/dist/attempt-execution.runtime-DF2GPgNh.js +3 -0
- package/dist/attempt-execution.runtime.js +1 -1
- package/dist/attempt-execution.shared-CwdukA3t.js +38 -0
- package/dist/attempt.prompt-helpers-CUqKtIa5.js +475 -0
- package/dist/attempt.tool-run-context-BbN4_BYO.js +2094 -0
- package/dist/binding-routing-C6Ex2Blu.js +113 -0
- package/dist/binding-targets-BqREncSL.js +121 -0
- package/dist/bot-DGMEkaIJ.js +7894 -0
- package/dist/bot-deps-BltYjeMX.js +2 -0
- package/dist/bot-deps-XvGCor9f.js +747 -0
- package/dist/bot-message-context.runtime-kK-xvLV7.js +7 -0
- package/dist/bot-message-context.runtime.js +1 -1
- package/dist/bot-message-context.session.runtime-DpOIJUae.js +12 -0
- package/dist/bot-message-context.session.runtime.js +1 -1
- package/dist/bot-native-commands.delivery.runtime-D49p5Lc1.js +4 -0
- package/dist/bot-native-commands.delivery.runtime.js +1 -1
- package/dist/bot-native-commands.runtime-8XBdSpkQ.js +13 -0
- package/dist/bot-native-commands.runtime.js +1 -1
- package/dist/bridge-server-y_XGduPx.js +113 -0
- package/dist/browser-cli-B2jmmTT3.js +230 -0
- package/dist/browser-cli-actions-input-JQt5ZQcn.js +473 -0
- package/dist/browser-cli-actions-observe-CrfKh6AX.js +81 -0
- package/dist/browser-cli-debug-DQACWlvC.js +137 -0
- package/dist/browser-cli-inspect-DDnxx0Xw.js +104 -0
- package/dist/browser-cli-lEuOnZzs.js +2 -0
- package/dist/browser-cli-manage-DogmwtYt.js +443 -0
- package/dist/browser-cli-resize-BHuV71VZ.js +26 -0
- package/dist/browser-cli-shared-CidShgoF.js +50 -0
- package/dist/browser-cli-state-i8cCGbiQ.js +337 -0
- package/dist/browser-control-auth-CKfXdXWj.js +2 -0
- package/dist/browser-profiles-ATBTvUGh.js +2 -0
- package/dist/browser-runtime-KmCT6FuL.js +384 -0
- package/dist/build-DOOT6f62.js +257 -0
- package/dist/build-info.json +3 -3
- package/dist/bundled/boot-md/handler.js +2 -2
- package/dist/bundled/session-memory/handler.js +1 -1
- package/dist/canvas-host/a2ui/.bundle.hash +1 -1
- package/dist/capability-cli-DwvrwB-g.js +1782 -0
- package/dist/channel-B-wgK1bK.js +481 -0
- package/dist/channel-B1e_k1P0.js +867 -0
- package/dist/channel-BRDfa5tc.js +1777 -0
- package/dist/channel-BVEO1O4p.js +376 -0
- package/dist/channel-BYlO5l1d.js +508 -0
- package/dist/channel-Bl0sPoNx.js +653 -0
- package/dist/channel-C84xtDBy.js +1134 -0
- package/dist/channel-CLgKq4Ds.js +1556 -0
- package/dist/channel-CZT0qxLh.js +1249 -0
- package/dist/channel-CffBHWkb.js +955 -0
- package/dist/channel-D34ZJvVB.js +2126 -0
- package/dist/channel-DcdXLo_W.js +238 -0
- package/dist/channel-DnK5TrXV.js +362 -0
- package/dist/channel-Dzj4RHY2.js +740 -0
- package/dist/channel-OhZzLZCj.js +562 -0
- package/dist/channel-actions.runtime-Cdvhqc9s.js +265 -0
- package/dist/channel-actions.runtime.js +1 -1
- package/dist/channel-core-Dz9I_R4j.js +5 -0
- package/dist/channel-inbound-COGoV_aS.js +80 -0
- package/dist/channel-mP6HCIMr.js +1496 -0
- package/dist/channel-plugin-runtime-C0R46KeG.js +998 -0
- package/dist/channel-quGdgWel.js +808 -0
- package/dist/channel-runtime-FvPQoIF9.js +408 -0
- package/dist/channel.runtime-2vUz3gij.js +109 -0
- package/dist/channel.runtime-BZLDSNuQ.js +254 -0
- package/dist/channel.runtime-Bo5jO0Hj.js +4 -0
- package/dist/channel.runtime-CDgvrjM2.js +652 -0
- package/dist/channel.runtime-COf8SDFn.js +88 -0
- package/dist/channel.runtime-C_C1f8lu.js +733 -0
- package/dist/channel.runtime-CqPbsg8t.js +2528 -0
- package/dist/channel.runtime-DtV85Khx.js +1008 -0
- package/dist/channel.runtime-Spt1ukGW.js +21009 -0
- package/dist/channel.setup-Bc0LUQS1.js +1098 -0
- package/dist/channel.setup-ChTkj2hq.js +343 -0
- package/dist/channel.setup-CpMD9F8i.js +10 -0
- package/dist/chat-mUsjD5YN.js +2666 -0
- package/dist/chrome-u314zcbM.js +1503 -0
- package/dist/cli/run-main.js +5 -5
- package/dist/cli-CJMoqXrc.js +1341 -0
- package/dist/cli-compaction-qGYB7suI.js +347 -0
- package/dist/cli-metadata-BBJFpNU9.js +22 -0
- package/dist/cli-runner-C0kcts0I.js +540 -0
- package/dist/cli-runner-CTm8K6s8.js +2 -0
- package/dist/cli-runner.runtime-C8deL5FL.js +3 -0
- package/dist/cli-runner.runtime-DDAg1D0L.js +4 -0
- package/dist/cli-runner.runtime.js +1 -1
- package/dist/cli-startup-metadata.json +8 -8
- package/dist/client-CPxpgs4m.js +650 -0
- package/dist/client-adapter-CLlqOqfu.js +897 -0
- package/dist/client-factory-R7Rr2jwn.js +9 -0
- package/dist/command-auth-CkQP1buP.js +135 -0
- package/dist/command-handlers-CJEjFyMk.js +1609 -0
- package/dist/command-registry-BDgyOqDw.js +4 -0
- package/dist/command-registry-BVi5Jn1S.js +9 -0
- package/dist/command-registry-core-JCfsVRtz.js +110 -0
- package/dist/command-status.runtime-CM1JLzi6.js +90 -0
- package/dist/command-status.runtime.js +1 -1
- package/dist/commands-acp-C1-I78U6.js +74 -0
- package/dist/commands-compact.runtime-DOsNavwp.js +10 -0
- package/dist/commands-compact.runtime.js +1 -1
- package/dist/commands-handlers.runtime-BD1n2WGw.js +6154 -0
- package/dist/commands-handlers.runtime.js +1 -1
- package/dist/commands-status-DU_UTQz1.js +16 -0
- package/dist/commands-status-DejfTakp.js +3 -0
- package/dist/commands-status.runtime-DejfTakp.js +3 -0
- package/dist/commands-status.runtime.js +1 -1
- package/dist/commands-subagents-control.runtime-D7vo_2Y_.js +3 -0
- package/dist/commands-subagents-control.runtime-DGybTjJ6.js +2 -0
- package/dist/commands-subagents-control.runtime.js +1 -1
- package/dist/commands-system-prompt-BEBXAMea.js +2 -0
- package/dist/commands-system-prompt-gj3oQpQO.js +162 -0
- package/dist/commands.runtime-CgXX70IN.js +176 -0
- package/dist/commands.runtime.js +1 -1
- package/dist/commitments/runtime.js +1 -1
- package/dist/compact-C41XxMDt.js +480 -0
- package/dist/compact-DUVAF9EC.js +1141 -0
- package/dist/compact.runtime-COMRg6s3.js +12 -0
- package/dist/compact.runtime.js +1 -1
- package/dist/completion-cli-CmEYoB9a.js +315 -0
- package/dist/computer-use-BwJHu1QF.js +367 -0
- package/dist/config-ATBTvUGh.js +2 -0
- package/dist/config-DlNgjF8W.js +373 -0
- package/dist/config-mutations-tl_OqI6d.js +159 -0
- package/dist/context-engine-host-compat-BNPAIK9F.js +288 -0
- package/dist/context-engine-host-compat-C69CK7zS.js +2 -0
- package/dist/context-engine-lifecycle-iQ8f5a_-.js +1274 -0
- package/dist/control-auth-vVxyGKxY.js +114 -0
- package/dist/control-service-73nj79EW.js +145 -0
- package/dist/control-ui/assets/agents-DdWIz8Vk.js +1008 -0
- package/dist/control-ui/assets/channel-config-extras-C7ELhuDi.js +2 -0
- package/dist/control-ui/assets/channels-Bsoj-9BS.js +367 -0
- package/dist/control-ui/assets/cron-BRTEw1tT.js +1013 -0
- package/dist/control-ui/assets/debug-EKl_F8_L.js +97 -0
- package/dist/control-ui/assets/index-DSbSO7ys.js +7370 -0
- package/dist/control-ui/assets/instances-B1AoYwI5.js +57 -0
- package/dist/control-ui/assets/logs-BChOznzh.js +74 -0
- package/dist/control-ui/assets/nodes-5yebKh9k.js +436 -0
- package/dist/control-ui/assets/sessions-BlDYNThc.js +399 -0
- package/dist/control-ui/assets/skills-shared-CNFnf7KU.js +11 -0
- package/dist/control-ui/assets/skills-uxTDRmLx.js +314 -0
- package/dist/control-ui/index.html +1 -1
- package/dist/control-ui/sw.js +1 -1
- package/dist/conversation-binding-runtime-CmoTKcY1.js +4 -0
- package/dist/conversation-runtime-BxOHxuin.js +31 -0
- package/dist/core-D0Y2H55f.js +282 -0
- package/dist/core-api-BKlJUtDt.js +5 -0
- package/dist/core-api-_fiftUHn.js +2 -0
- package/dist/crestodian/crestodian.js +1 -1
- package/dist/crestodian/rescue-message.js +1 -1
- package/dist/crestodian-BJifoQiR.js +55 -0
- package/dist/daocore-tools-BMdF8RwK.js +11727 -0
- package/dist/delivery-e2ci_bcX.js +1002 -0
- package/dist/dialogue-BXayGhnz.js +37 -0
- package/dist/dir-fetch-tool-Cara5mZh.js +565 -0
- package/dist/dir-list-tool-CQ8Bo5Ez.js +100 -0
- package/dist/direct-dm-CPk6Qm9D.js +64 -0
- package/dist/directive-handling.fast-lane-BQ9aXqk8.js +68 -0
- package/dist/directive-handling.impl--a5AGurq.js +818 -0
- package/dist/directive-handling.impl-C4JqMwle.js +2 -0
- package/dist/directive-handling.model-selection-pyTYMIXn.js +122 -0
- package/dist/directive-handling.persist.runtime-B7vWT1r9.js +263 -0
- package/dist/directive-handling.persist.runtime.js +1 -1
- package/dist/dispatch-3Ru_eGq1.js +1640 -0
- package/dist/dispatch-acp-transcript.runtime-CYMP-5oE.js +40 -0
- package/dist/dispatch-acp-transcript.runtime.js +1 -1
- package/dist/dispatch-acp.runtime-BVCiklXk.js +18 -0
- package/dist/dispatch-acp.runtime.js +1 -1
- package/dist/doctor-B8mR3Sgr.js +2 -0
- package/dist/doctor-BJ6DuerV.js +6 -0
- package/dist/doctor-config-flow-BdOGs_zn.js +1741 -0
- package/dist/doctor-core-checks-CDf0stz-.js +2 -0
- package/dist/doctor-core-checks-Cpdnu2XI.js +573 -0
- package/dist/doctor-health-0qv_acBu.js +65 -0
- package/dist/doctor-health-contributions-Db1FQgFw.js +696 -0
- package/dist/doctor-lint-C1g4Llgl.js +94 -0
- package/dist/doctor-state-integrity-C8iXFLJ7.js +1231 -0
- package/dist/doctor-update-CsPu6p3d.js +58 -0
- package/dist/dynamic-tools-YQ0z0YKH.js +486 -0
- package/dist/embedded-backend-S0cahIZw.js +579 -0
- package/dist/embedded-gateway-stub.runtime-Cjw12ALm.js +12 -0
- package/dist/embedded-gateway-stub.runtime.js +1 -1
- package/dist/exec-approvals-ArHHjoE-.js +149 -0
- package/dist/extensionAPI.js +1 -1
- package/dist/extensions/active-memory/index.js +1 -1
- package/dist/extensions/admin-http-rpc/index.js +1 -1
- package/dist/extensions/browser/browser-bridge.js +1 -1
- package/dist/extensions/browser/browser-config.js +4 -4
- package/dist/extensions/browser/browser-control-auth.js +2 -2
- package/dist/extensions/browser/browser-doctor.js +2 -2
- package/dist/extensions/browser/browser-maintenance.js +1 -1
- package/dist/extensions/browser/browser-profiles.js +2 -2
- package/dist/extensions/browser/browser-runtime-api.js +11 -11
- package/dist/extensions/browser/cli-metadata.js +1 -1
- package/dist/extensions/browser/index.js +1 -1
- package/dist/extensions/browser/plugin-registration.js +1 -1
- package/dist/extensions/browser/register.runtime.js +4 -4
- package/dist/extensions/browser/runtime-api.js +13 -13
- package/dist/extensions/canvas/index.js +1 -1
- package/dist/extensions/clickclack/api.js +2 -2
- package/dist/extensions/clickclack/channel-plugin-api.js +1 -1
- package/dist/extensions/clickclack/runtime-api.js +2 -2
- package/dist/extensions/device-pair/api.js +1 -1
- package/dist/extensions/device-pair/pair-command-approve.js +1 -1
- package/dist/extensions/file-transfer/index.js +4 -4
- package/dist/extensions/imessage/api.js +2 -2
- package/dist/extensions/imessage/channel-plugin-api.js +1 -1
- package/dist/extensions/imessage/message-tool-api.d.ts +1 -1
- package/dist/extensions/imessage/runtime-api.js +3 -3
- package/dist/extensions/irc/api.js +2 -2
- package/dist/extensions/irc/channel-plugin-api.js +1 -1
- package/dist/extensions/llm-task/index.js +1 -1
- package/dist/extensions/mattermost/api.js +1 -1
- package/dist/extensions/mattermost/channel-plugin-api.js +1 -1
- package/dist/extensions/mattermost/channel-plugin-runtime.js +1 -1
- package/dist/extensions/mattermost/policy-api.js +1 -1
- package/dist/extensions/mattermost/runtime-api.js +2 -2
- package/dist/extensions/mattermost/slash-route-api.js +1 -1
- package/dist/extensions/memory-core/cli-metadata.js +1 -1
- package/dist/extensions/migrate-claude/apply.js +1 -1
- package/dist/extensions/migrate-claude/index.js +1 -1
- package/dist/extensions/migrate-claude/plan.js +1 -1
- package/dist/extensions/migrate-claude/provider.js +1 -1
- package/dist/extensions/migrate-claude/targets.js +1 -1
- package/dist/extensions/migrate-hermes/apply.js +1 -1
- package/dist/extensions/migrate-hermes/index.js +1 -1
- package/dist/extensions/migrate-hermes/model.js +1 -1
- package/dist/extensions/migrate-hermes/plan.js +1 -1
- package/dist/extensions/migrate-hermes/provider.js +1 -1
- package/dist/extensions/migrate-hermes/secrets.js +1 -1
- package/dist/extensions/migrate-hermes/targets.js +1 -1
- package/dist/extensions/policy/api.js +1 -1
- package/dist/extensions/policy/index.js +2 -2
- package/dist/extensions/signal/api.js +6 -6
- package/dist/extensions/signal/channel-plugin-api.js +1 -1
- package/dist/extensions/signal/reaction-runtime-api.js +1 -1
- package/dist/extensions/signal/runtime-api.js +7 -7
- package/dist/extensions/skill-workshop/api.js +1 -1
- package/dist/extensions/skill-workshop/index.js +2 -2
- package/dist/extensions/telegram/account-inspect-api.js +1 -1
- package/dist/extensions/telegram/api.js +11 -11
- package/dist/extensions/telegram/channel-plugin-api.js +2 -2
- package/dist/extensions/telegram/contract-api.js +3 -3
- package/dist/extensions/telegram/runtime-api.js +7 -7
- package/dist/extensions/telegram/security-audit-contract-api.js +1 -1
- package/dist/extensions/telegram/setup-plugin-api.js +1 -1
- package/dist/extensions/telegram/test-api.js +2 -2
- package/dist/extensions/webhooks/api.js +1 -1
- package/dist/extensions/webhooks/index.js +1 -1
- package/dist/extensions/xai/index.js +4 -4
- package/dist/extensions/xai/realtime-transcription-provider.js +1 -1
- package/dist/extensions/xai/speech-provider.js +1 -1
- package/dist/extensions/xai/test-api.js +1 -1
- package/dist/extensions/xai/tts.js +1 -1
- package/dist/extensions/xai/web-search.js +1 -1
- package/dist/extensions/xai/xai-oauth.js +1 -1
- package/dist/file-fetch-tool-DQpAtjX4.js +124 -0
- package/dist/file-write-tool-CkB0oYwe.js +127 -0
- package/dist/format-DUJUpMFQ.js +1145 -0
- package/dist/gateway-cli-CQo_E4Ka.js +435 -0
- package/dist/gateway-method-runtime-1QPgVJH7.js +21 -0
- package/dist/get-reply-DfUcsC-O.js +4689 -0
- package/dist/get-reply-from-config.runtime-C95MG73O.js +2 -0
- package/dist/get-reply-from-config.runtime.js +1 -1
- package/dist/graph-users-DPJe0fPs.js +1419 -0
- package/dist/group-access-DefaNJ6L.js +112 -0
- package/dist/handle-action.guild-admin-C1sy1kGJ.js +288 -0
- package/dist/harness-BgPRdmNK.js +61 -0
- package/dist/health-DFR9A1YV.js +4 -0
- package/dist/heartbeat-runner-BiYVPs25.js +5 -0
- package/dist/heartbeat-runner.runtime-Bmaq8WUA.js +4 -0
- package/dist/heartbeat-runner.runtime.js +1 -1
- package/dist/hooks-CNJDuJUm.js +534 -0
- package/dist/inbound-direct-dm-runtime-DFt58p71.js +2 -0
- package/dist/inbound-reply-dispatch-kgSDM8ax.js +148 -0
- package/dist/index.js +1 -1
- package/dist/init-B0zO_U4r.js +59 -0
- package/dist/inline-buttons-B6zgQW3J.js +40 -0
- package/dist/internal-events-C3Q5t6hg.js +90 -0
- package/dist/isolated-agent-CaSU5GCJ.js +2 -0
- package/dist/isolated-agent-CnsD2TmP.js +1118 -0
- package/dist/lifecycle-i3WZDaIn.js +571 -0
- package/dist/list.probe-g0rUiVXW.js +449 -0
- package/dist/list.status-command-DG3PrmeO.js +789 -0
- package/dist/llm-slug-generator-CHlf69FU.js +78 -0
- package/dist/llm-slug-generator.js +1 -1
- package/dist/local-dispatch.runtime-CDeHUp_A.js +9 -0
- package/dist/local-dispatch.runtime.js +1 -1
- package/dist/manager.runtime-BlLMPUTq.js +2714 -0
- package/dist/manager.runtime.js +1 -1
- package/dist/markdown-to-line-r_mBHMdy.js +811 -0
- package/dist/mcp-http-C5UPIJGR.js +555 -0
- package/dist/mcp-http-CTy9gpfA.js +2 -0
- package/dist/media-understanding-provider-BT6TOuVa.js +339 -0
- package/dist/message-actions-BdcjjwFt.js +145 -0
- package/dist/message-handler-B29Tj2p2.js +1715 -0
- package/dist/message-handler-BhHb1FHI.js +384 -0
- package/dist/message-handler.preflight-ouZ_-3o8.js +1125 -0
- package/dist/message-handler.process-CrYiBx_F.js +1484 -0
- package/dist/model-CLbtocQX.js +74 -0
- package/dist/model-selection-BG_vU9Kl.js +272 -0
- package/dist/models-4KERn3ya.js +104 -0
- package/dist/models-BCa-ipxi.js +2 -0
- package/dist/models-cli-DtpGXEsf.js +256 -0
- package/dist/monitor-B-egsvmI.js +1657 -0
- package/dist/monitor-BJX9Yv7k.js +2 -0
- package/dist/monitor-BPrIy3pV.js +715 -0
- package/dist/monitor-BWknZZ7N.js +4377 -0
- package/dist/monitor-C3y_PtI1.js +60 -0
- package/dist/monitor-DCzfdqE4.js +1370 -0
- package/dist/monitor-P24epSwE.js +2788 -0
- package/dist/monitor-auth-CEG_xldE.js +179 -0
- package/dist/monitor-jEePJbbS.js +834 -0
- package/dist/monitor-polling.runtime-BzlHK11u.js +883 -0
- package/dist/monitor-polling.runtime.js +1 -1
- package/dist/monitor-webhook.runtime-vFEhk_Zj.js +387 -0
- package/dist/monitor-webhook.runtime.js +1 -1
- package/dist/monitor.account-BUIfgWXg.js +5233 -0
- package/dist/monitor.runtime-Cp7W4fhb.js +2 -0
- package/dist/monitor.runtime.js +1 -1
- package/dist/monitor.webhook-DzrbeR4T.js +180 -0
- package/dist/node-cli-sessions-CLbI14UA.js +1228 -0
- package/dist/openai-http-BM9BIA_g.js +824 -0
- package/dist/openresponses-http-CNoW9kk8.js +1173 -0
- package/dist/operations-DT4bsxV6.js +805 -0
- package/dist/outbound-adapter-CZRUOgY0.js +543 -0
- package/dist/outbound-session-route-DneMsGNx.js +45 -0
- package/dist/outbound.runtime-CHuoKJJI.js +2 -0
- package/dist/outbound.runtime.js +1 -1
- package/dist/pi-embedded-1-rsueO_.js +3796 -0
- package/dist/pi-embedded-BRfDncVO.js +4 -0
- package/dist/pi-embedded.runtime-DEYoa6bW.js +4 -0
- package/dist/pi-embedded.runtime.js +1 -1
- package/dist/pi-tools-C0P3NgP5.js +2413 -0
- package/dist/plan-BQUkjBMY.js +81 -0
- package/dist/plan-CRO9_2_k.js +112 -0
- package/dist/plugin-BJCmWhnh.js +12396 -0
- package/dist/plugin-app-cache-key-CXAg0F7A.js +46 -0
- package/dist/plugin-enabled-D93Z9LSo.js +233 -0
- package/dist/plugin-registration-BBrTcZnf.js +88 -0
- package/dist/plugin-sdk/.boundary-entry-shims.stamp +1 -1
- package/dist/plugin-sdk/acp-runtime-backend.js +1 -1
- package/dist/plugin-sdk/acp-runtime.js +2 -2
- package/dist/plugin-sdk/agent-harness-runtime.js +6 -6
- package/dist/plugin-sdk/agent-harness-task-runtime.js +1 -1
- package/dist/plugin-sdk/agent-harness.js +7 -7
- package/dist/plugin-sdk/agent-runtime.js +2 -2
- package/dist/plugin-sdk/channel-core.js +2 -2
- package/dist/plugin-sdk/channel-inbound.js +2 -2
- package/dist/plugin-sdk/channel-test-helpers.js +1 -1
- package/dist/plugin-sdk/command-auth.js +1 -1
- package/dist/plugin-sdk/command-status-runtime.js +1 -1
- package/dist/plugin-sdk/compat.js +1 -1
- package/dist/plugin-sdk/conversation-binding-runtime.js +2 -2
- package/dist/plugin-sdk/conversation-runtime.js +3 -3
- package/dist/plugin-sdk/core.js +2 -2
- package/dist/plugin-sdk/direct-dm.js +1 -1
- package/dist/plugin-sdk/gateway-method-runtime.js +1 -1
- package/dist/plugin-sdk/health.js +2 -2
- package/dist/plugin-sdk/inbound-reply-dispatch.js +1 -1
- package/dist/plugin-sdk/index.js +1 -1
- package/dist/plugin-sdk/mattermost.js +1 -1
- package/dist/plugin-sdk/plugin-test-contracts.js +2 -2
- package/dist/plugin-sdk/provider-test-contracts.js +4 -4
- package/dist/plugin-sdk/reply-runtime.js +4 -4
- package/dist/plugin-sdk/testing.js +2 -2
- package/dist/plugin-sdk/zalouser.js +1 -1
- package/dist/plugin-service-DnZEPBEA.js +1229 -0
- package/dist/plugins/runtime/index.js +4 -4
- package/dist/policy-CMv5QMPG.js +138 -0
- package/dist/policy-CeoP4t9J.js +680 -0
- package/dist/prepare.runtime-DZ55JiTt.js +732 -0
- package/dist/prepare.runtime.js +1 -1
- package/dist/preview-warnings-O08PZRxu.js +392 -0
- package/dist/probe-C6w3bP8X.js +682 -0
- package/dist/probe-Cs1_X1NA.js +47 -0
- package/dist/probe-Cuee_jyM.js +2204 -0
- package/dist/probe-DZxCnDzn.js +2 -0
- package/dist/program-C2dT8VHB.js +131 -0
- package/dist/provider-C2_vSzYh.js +32 -0
- package/dist/provider-DSfTDQbj.js +152 -0
- package/dist/provider-U8hrm9h-.js +32 -0
- package/dist/provider-_v910H8y.js +8735 -0
- package/dist/provider-dispatcher-C3kmmIEC.js +22 -0
- package/dist/provider-dispatcher.runtime.js +1 -1
- package/dist/provider-session.runtime-Mg0fx5hH.js +9 -0
- package/dist/provider-session.runtime.js +1 -1
- package/dist/provider.runtime-pSTXuHrq.js +2 -0
- package/dist/provider.runtime.js +1 -1
- package/dist/public-surface-loader-09t4TTue.js +114 -0
- package/dist/pw-ai-DjNqHV3_.js +3029 -0
- package/dist/pw-role-snapshot-DoGefdv7.js +333 -0
- package/dist/reaction-level-C8Mqxizo.js +19 -0
- package/dist/reaction-runtime-api-DOP6xOsm.js +116 -0
- package/dist/realtime-transcription-provider-BTGEXXna.js +205 -0
- package/dist/register-CbZPpqI3.js +2178 -0
- package/dist/register.agent-DrBFuPKh.js +156 -0
- package/dist/register.crestodian-DwTbmaK8.js +24 -0
- package/dist/register.maintenance-BIeoYBub.js +83 -0
- package/dist/register.runtime-B5V9_Dwz.js +54 -0
- package/dist/register.subclis-CJE9ISZY.js +31 -0
- package/dist/register.subclis-YQsKas8N.js +3 -0
- package/dist/register.subclis-core-CjwdGeGU.js +273 -0
- package/dist/repair-sequencing-C0jrL5pw.js +640 -0
- package/dist/reply-delivery-B28mTIpb.js +196 -0
- package/dist/reply-runtime-BqQX7HHw.js +11 -0
- package/dist/reply.runtime-C95MG73O.js +2 -0
- package/dist/reply.runtime.js +1 -1
- package/dist/request-BqoMvXxH.js +54 -0
- package/dist/resolve-allowlist-hbKKd_kq.js +220 -0
- package/dist/result-fallback-classifier-BrORPoxj.js +79 -0
- package/dist/route-HHRgSzVy.js +469 -0
- package/dist/route-resolution-C7bafjVc.js +274 -0
- package/dist/routes-86XGeo7a.js +2 -0
- package/dist/routes-BhVGr9ie.js +3602 -0
- package/dist/run-Bb5QP5JV.js +1162 -0
- package/dist/run-attempt-CLi0wV7j.js +7704 -0
- package/dist/run-command-Be6GAxiW.js +2 -0
- package/dist/run-command-Ds4hr3iU.js +23 -0
- package/dist/run-embedded.runtime-wX9ORojX.js +4 -0
- package/dist/run-embedded.runtime.js +1 -1
- package/dist/run-execution-cli.runtime-BmNZVB2N.js +4 -0
- package/dist/run-execution-cli.runtime.js +1 -1
- package/dist/run-executor.runtime.js +1 -1
- package/dist/run-subagent-registry.runtime-Uq5sjftu.js +2 -0
- package/dist/run-subagent-registry.runtime.js +1 -1
- package/dist/runtime-BktLQ75j.js +1287 -0
- package/dist/runtime-C9XdR62_.js +438 -0
- package/dist/runtime-CTvWDPx9.js +6179 -0
- package/dist/runtime-api-BojB3MUw.js +21 -0
- package/dist/runtime-api-C92MlAwp.js +13 -0
- package/dist/runtime-api-CFNZE9Xt.js +13 -0
- package/dist/runtime-api-Cimpgm5_.js +3 -0
- package/dist/runtime-api-D8qG_lUm.js +17 -0
- package/dist/runtime-api-DMDMnQEG.js +4 -0
- package/dist/runtime-api-DupQdIlE.js +24 -0
- package/dist/runtime-api.actions-B8i3elqu.js +3 -0
- package/dist/runtime-api.monitor-BntFzf_M.js +6 -0
- package/dist/runtime-api.send-CPlmi1jc.js +4 -0
- package/dist/runtime-api.threads-D-H_r_Nl.js +2 -0
- package/dist/runtime-channel-BGhTxnXZ.js +150 -0
- package/dist/runtime-channel-CPFxNapZ.js +2 -0
- package/dist/runtime-embedded-pi.runtime-BjnMTurB.js +2 -0
- package/dist/runtime-embedded-pi.runtime.js +1 -1
- package/dist/sanitize-outbound-BMUv1NeS.js +127 -0
- package/dist/sdk-setup-tools-Do3DkQi_.js +8 -0
- package/dist/secrets-B8YVPHhk.js +113 -0
- package/dist/security-audit-7Gg_nL4o.js +122 -0
- package/dist/security-audit-Bk3CREj-.js +118 -0
- package/dist/security-audit.runtime-BmSJrdeq.js +2 -0
- package/dist/security-audit.runtime.js +1 -1
- package/dist/selection-BFGde9-_.js +3 -0
- package/dist/selection-DrLFlbgG.js +16157 -0
- package/dist/send-4dLa-BQo.js +1631 -0
- package/dist/send-CV6gCwYP.js +192 -0
- package/dist/send-aGiK3Efu.js +143 -0
- package/dist/send-dB3CehAN.js +2 -0
- package/dist/send.components-BcG9BlkC.js +2 -0
- package/dist/send.components-rLjDlfYU.js +500 -0
- package/dist/send.runtime-D20k2MSr.js +2 -0
- package/dist/send.runtime.js +1 -1
- package/dist/server-BTRx_U1s.js +73 -0
- package/dist/server-CY0ymhBg.js +24 -0
- package/dist/server-close.runtime.js +1 -1
- package/dist/server-context-BxGT8wbK.js +955 -0
- package/dist/server-context-DYlbhAaT.js +2 -0
- package/dist/server-cron-BB_15lcP.js +2 -0
- package/dist/server-cron-CfyJX_a4.js +2989 -0
- package/dist/server-methods-Ck2ab6TC.js +16494 -0
- package/dist/server-node-events-BdRizF-5.js +596 -0
- package/dist/server-plugin-bootstrap-CPUr8NFE.js +70 -0
- package/dist/server-plugins-__5AxjzH.js +432 -0
- package/dist/server-reload-handlers-BMl9Fx9b.js +714 -0
- package/dist/server-restart-sentinel-D11M_ZsH.js +747 -0
- package/dist/server-restart-sentinel-DGptQ27c.js +2 -0
- package/dist/server-runtime-services-BwtLs-8I.js +2 -0
- package/dist/server-runtime-services-aJydixRu.js +267 -0
- package/dist/server-startup-plugins-m_P3gY4m.js +113 -0
- package/dist/server-startup-post-attach-DuXQ1AXL.js +716 -0
- package/dist/server-ws-runtime-mXmxB1W8.js +349 -0
- package/dist/server.impl-fuV8aKSP.js +2586 -0
- package/dist/service-a8cTFka4.js +1446 -0
- package/dist/session-binding-BIycxo9u.js +2 -0
- package/dist/session-binding-xHwQr9vM.js +219 -0
- package/dist/session-kill-http-DiUZhcoN.js +121 -0
- package/dist/session-reset-service-DkepDoob.js +625 -0
- package/dist/session-route-Ba5u7ecb.js +93 -0
- package/dist/session-status.runtime-Cb1X69Rl.js +2 -0
- package/dist/session-status.runtime.js +1 -1
- package/dist/session-subagent-reactivation.runtime-rbqmto4G.js +2 -0
- package/dist/session-subagent-reactivation.runtime.js +1 -1
- package/dist/session-tab-registry-DSKOoJta.js +521 -0
- package/dist/sessions-history-http-CWP88I_C.js +430 -0
- package/dist/sessions.runtime-1q1OwLn3.js +2 -0
- package/dist/sessions.runtime.js +1 -1
- package/dist/setup-api-DtrOOYeP.js +29 -0
- package/dist/setup-core-DMrJS6LP.js +174 -0
- package/dist/setup-surface-BFPNu1qA.js +405 -0
- package/dist/setup-surface-CedShqhO.js +288 -0
- package/dist/setup-surface-DLOBH1Vf.js +320 -0
- package/dist/setup-surface-MhCeEWSj.js +221 -0
- package/dist/shared-DamL-e8D.js +121 -0
- package/dist/shared-client-B48JoAsz.js +2 -0
- package/dist/shared-client-BoSiDnUo.js +629 -0
- package/dist/side-question-DQoQ_ETb.js +683 -0
- package/dist/skill-tool-dispatch.runtime-CWLloYA8.js +143 -0
- package/dist/skill-tool-dispatch.runtime.js +1 -1
- package/dist/slash-state-DrrWJQfv.js +2166 -0
- package/dist/speech-provider-8Hx1uw41.js +184 -0
- package/dist/src-BbUYOPwU.js +4256 -0
- package/dist/startup-context-opAJAamX.js +313 -0
- package/dist/status-subagents.runtime-DSo7ZK_Z.js +18 -0
- package/dist/status-subagents.runtime.js +1 -1
- package/dist/status-text-AyPuDzrO.js +296 -0
- package/dist/sticker-cache-91Tp8mLP.js +206 -0
- package/dist/sticker-vision.runtime-DjleFXi5.js +17 -0
- package/dist/sticker-vision.runtime.js +1 -1
- package/dist/subagent-announce-delivery-DS_2W9oZ.js +958 -0
- package/dist/subagent-announce-hatA_PZ2.js +354 -0
- package/dist/subagent-control-IyFIvc7T.js +508 -0
- package/dist/subagent-hooks-BCj5eB0Z.js +2 -0
- package/dist/subagent-hooks-BWjTBkox.js +146 -0
- package/dist/subagent-hooks-CdGLK9_3.js +230 -0
- package/dist/subagent-hooks-EH0tBkw9.js +2 -0
- package/dist/subagent-hooks-api-BhYaXQJV.js +23 -0
- package/dist/subagent-hooks-api-uGsV8Xn5.js +22 -0
- package/dist/subagent-hooks-api-wfJFKIPx.js +23 -0
- package/dist/subagent-hooks-l8Xd94dt.js +2 -0
- package/dist/subagent-hooks-m2eHUO_d.js +116 -0
- package/dist/subagent-orphan-recovery-Bq9zvX3L.js +352 -0
- package/dist/subagent-registry-Bcb3zy6b.js +3 -0
- package/dist/subagent-registry-CoZNTFx-.js +2351 -0
- package/dist/subagent-registry.runtime.js +1 -1
- package/dist/subagent-session-cleanup--00EjDLe.js +525 -0
- package/dist/subagent-spawn-ClJJkz3D.js +1164 -0
- package/dist/target-id-BsZLiF3x.js +107 -0
- package/dist/targets-CQMe4AZP.js +44 -0
- package/dist/targets-SmL1AU-H.js +19 -0
- package/dist/targets-Y-roiA47.js +19 -0
- package/dist/task-registry-control.runtime.js +1 -1
- package/dist/telegram/token.js +1 -1
- package/dist/testing-B5hNJDuF.js +267 -0
- package/dist/thread-bindings-BD3eE0kK.js +232 -0
- package/dist/thread-bindings-C9NNiv8K.js +571 -0
- package/dist/thread-bindings-Dl4PN1vZ.js +228 -0
- package/dist/thread-bindings-uK1LLAwy.js +8 -0
- package/dist/thread-bindings.discord-api-BHNEUbnk.js +187 -0
- package/dist/thread-bindings.manager-B0LXiH3F.js +2 -0
- package/dist/thread-bindings.manager-BTlH4XMG.js +536 -0
- package/dist/thread-lifecycle-DG8uyPHd.js +1614 -0
- package/dist/token-BbGCANnE.js +134 -0
- package/dist/tool-actions.runtime-oUUrD8HB.js +534 -0
- package/dist/tool-actions.runtime.js +1 -1
- package/dist/tool-bqg-_vYv.js +139 -0
- package/dist/tool-resolution-CSZsa3kP.js +149 -0
- package/dist/tools-effective-inventory-0CqFp-vq.js +204 -0
- package/dist/tools-invoke-http-DhYHY0Yh.js +67 -0
- package/dist/tools-invoke-shared-CbVJJUj7.js +200 -0
- package/dist/tts-C2qD-ad6.js +66 -0
- package/dist/tui-CzGY254f.js +4709 -0
- package/dist/tui-backend-fzoeoSN0.js +256 -0
- package/dist/tui-cli-B2RWb2gM.js +37 -0
- package/dist/tui-dgS8hdXU.js +2 -0
- package/dist/update-cli-BmiBDf3c.js +3664 -0
- package/dist/update-runner-CM7QVX6u.js +2379 -0
- package/dist/vision-tools-BzoBZnIc.js +1409 -0
- package/dist/web-search-DmfOd66S.js +62 -0
- package/dist/web-search-provider.runtime-3C5-pODD.js +328 -0
- package/dist/web-search-provider.runtime-Doh-Z9We.js +2 -0
- package/dist/web-search-provider.runtime.js +1 -1
- package/dist/xai-oauth-BLvQ43hx.js +479 -0
- package/dist/xai-user-agent-BdMQ_0_O.js +32 -0
- package/package.json +1 -1
- package/dist/abort-DeEb_wKd.js +0 -277
- package/dist/abort.runtime-CoYXQGPK.js +0 -2
- package/dist/account-inspect-D_06-CYS.js +0 -173
- package/dist/accounts-BCTqtj4Y.js +0 -107
- package/dist/accounts-BudLl8P2.js +0 -107
- package/dist/accounts-CUReBDA0.js +0 -2
- package/dist/accounts-PCioSzzr.js +0 -119
- package/dist/acp-runtime-B2t9_BHD.js +0 -26
- package/dist/acp-spawn-0x4Iiu1o.js +0 -2
- package/dist/acp-spawn-B2Ghe0jJ.js +0 -1275
- package/dist/acp-stateful-target-driver-C4VgcSyz.js +0 -89
- package/dist/action-kill-ClYsXEz7.js +0 -33
- package/dist/action-runtime-DrLPkNvU.js +0 -469
- package/dist/action-runtime-api-BqSflr9q.js +0 -2
- package/dist/action-send-CIy9cziD.js +0 -39
- package/dist/action-spawn-BjV6lFnX.js +0 -47
- package/dist/actions-D-QH7wq5.js +0 -161
- package/dist/actions.runtime-hzDtXQGU.js +0 -5
- package/dist/agent-Bb1DTBgv.js +0 -3
- package/dist/agent-Cnqj258q.js +0 -2
- package/dist/agent-command-BRx6ZNgJ.js +0 -1367
- package/dist/agent-components.runtime-D-g6xXJ0.js +0 -10
- package/dist/agent-harness-runtime-dKUQFuOc.js +0 -180
- package/dist/agent-harness-task-runtime-v2crUm3i.js +0 -140
- package/dist/agent-runner-execution-AOzdxvWR.js +0 -1713
- package/dist/agent-runner-utils-Byv50B4Z.js +0 -266
- package/dist/agent-runner.runtime-hhgRE0Z-.js +0 -3455
- package/dist/agent-runtime-BHmh3i0o.js +0 -229
- package/dist/agent-via-gateway-DnCqaK4a.js +0 -463
- package/dist/api-Bzyb8sFU.js +0 -3
- package/dist/api-CIEDFXZs.js +0 -2
- package/dist/api-D_dTQAu8.js +0 -2
- package/dist/api-DilBjZ9Q.js +0 -6
- package/dist/api-E5zoQMmh.js +0 -134
- package/dist/api-OJZug8gQ.js +0 -639
- package/dist/apply-C0eV5T0O.js +0 -41
- package/dist/apply-alsYvxLE.js +0 -54
- package/dist/approval-handler.runtime-C6jqZXSN.js +0 -130
- package/dist/assistant-DDXnAAB5.js +0 -291
- package/dist/attachment-normalize-Cv-_4DWU.js +0 -225
- package/dist/attempt-execution-DECEU59r.js +0 -558
- package/dist/attempt-execution.runtime-kasMxuER.js +0 -3
- package/dist/attempt-execution.shared-ClUxk52p.js +0 -38
- package/dist/attempt.prompt-helpers-Bxlv9VSu.js +0 -475
- package/dist/attempt.tool-run-context-CplQWX6g.js +0 -2094
- package/dist/binding-routing-0Obpp-Ij.js +0 -113
- package/dist/binding-targets-CZHQaZL4.js +0 -121
- package/dist/bot-BPbQ0840.js +0 -7894
- package/dist/bot-deps-Cs1M9USs.js +0 -2
- package/dist/bot-deps-xAcaOtTZ.js +0 -747
- package/dist/bot-message-context.runtime-Bji78Cbn.js +0 -7
- package/dist/bot-message-context.session.runtime-C3dyOIYH.js +0 -12
- package/dist/bot-native-commands.delivery.runtime-WshH99fy.js +0 -4
- package/dist/bot-native-commands.runtime-C1L364gU.js +0 -13
- package/dist/bridge-server-DPlM8_Lk.js +0 -113
- package/dist/browser-cli-DFNQE98N.js +0 -230
- package/dist/browser-cli-Drb5E5--.js +0 -2
- package/dist/browser-cli-actions-input-Dx-1OXmE.js +0 -473
- package/dist/browser-cli-actions-observe-BMDfE7xU.js +0 -81
- package/dist/browser-cli-debug-nu7Ih09g.js +0 -137
- package/dist/browser-cli-inspect-D5X2wohg.js +0 -104
- package/dist/browser-cli-manage-DkzYwph4.js +0 -443
- package/dist/browser-cli-resize-B30Avedl.js +0 -26
- package/dist/browser-cli-shared-cMgQoQzF.js +0 -50
- package/dist/browser-cli-state-CQLDvDy7.js +0 -337
- package/dist/browser-control-auth-D7ArmHUt.js +0 -2
- package/dist/browser-profiles-B39SIZNb.js +0 -2
- package/dist/browser-runtime-DUbSAOOS.js +0 -384
- package/dist/build-D25KqC92.js +0 -257
- package/dist/capability-cli-CuyXrlAB.js +0 -1782
- package/dist/channel-0N3YGMGg.js +0 -1496
- package/dist/channel-1UyKoLyp.js +0 -481
- package/dist/channel-BG87pSEW.js +0 -740
- package/dist/channel-BPGSaZW7.js +0 -1249
- package/dist/channel-BQMPh1J_.js +0 -376
- package/dist/channel-Bd_8V6zn.js +0 -1134
- package/dist/channel-BtBjh_ij.js +0 -362
- package/dist/channel-CpFBlVH6.js +0 -562
- package/dist/channel-CwuTrIrF.js +0 -508
- package/dist/channel-Cxl4sJA-.js +0 -1777
- package/dist/channel-D-VfU4Z2.js +0 -2126
- package/dist/channel-D9q8aYrN.js +0 -867
- package/dist/channel-DKSvVvZh.js +0 -238
- package/dist/channel-DvoFfWLx.js +0 -808
- package/dist/channel-GktTcGHm.js +0 -955
- package/dist/channel-actions.runtime-CKcRA0GW.js +0 -265
- package/dist/channel-core-nm8s1qFZ.js +0 -5
- package/dist/channel-d3t2ESlE.js +0 -653
- package/dist/channel-inbound-DKz40dq-.js +0 -80
- package/dist/channel-plugin-runtime-DKIGZWfW.js +0 -998
- package/dist/channel-runtime-Bh8_GY12.js +0 -408
- package/dist/channel-yQ8jCOb9.js +0 -1556
- package/dist/channel.runtime-BG4mM5cQ.js +0 -652
- package/dist/channel.runtime-BS6PyFFa.js +0 -1008
- package/dist/channel.runtime-CE_xECqN.js +0 -88
- package/dist/channel.runtime-Cd1Sw8U4.js +0 -254
- package/dist/channel.runtime-DIq3XOEe.js +0 -733
- package/dist/channel.runtime-Dy1cx35I.js +0 -21009
- package/dist/channel.runtime-NyIMjLnP.js +0 -109
- package/dist/channel.runtime-Zx5mfE2V.js +0 -4
- package/dist/channel.runtime-rJRibGfN.js +0 -2528
- package/dist/channel.setup--4ACadmF.js +0 -10
- package/dist/channel.setup-DzwqIlo3.js +0 -1098
- package/dist/channel.setup-FKYSJwXR.js +0 -343
- package/dist/chat-CKxSm7r1.js +0 -2666
- package/dist/chrome-rMubJwRG.js +0 -1503
- package/dist/cli-CbTod55I.js +0 -1341
- package/dist/cli-compaction-BbHgjJyW.js +0 -347
- package/dist/cli-metadata-DkOWLC_p.js +0 -22
- package/dist/cli-runner-Cwzv_RKf.js +0 -540
- package/dist/cli-runner-DdnUsgPl.js +0 -2
- package/dist/cli-runner.runtime-BlrSgbEW.js +0 -3
- package/dist/cli-runner.runtime-D8kVfvFH.js +0 -4
- package/dist/client-ClvxsWgL.js +0 -650
- package/dist/client-adapter-CsrIIjK1.js +0 -897
- package/dist/client-factory-DAYClhwm.js +0 -9
- package/dist/command-auth-CCha2ofd.js +0 -135
- package/dist/command-handlers-DCWJzHyB.js +0 -1609
- package/dist/command-registry-BIf61QNz.js +0 -9
- package/dist/command-registry-DJROBy4h.js +0 -4
- package/dist/command-registry-core-BQOWqi6S.js +0 -110
- package/dist/command-status.runtime-D88CUglL.js +0 -90
- package/dist/commands-acp-B-8dHX4Z.js +0 -74
- package/dist/commands-compact.runtime-ComX5mUk.js +0 -10
- package/dist/commands-handlers.runtime-DR3wjXHX.js +0 -6154
- package/dist/commands-status-CDyGrwsI.js +0 -3
- package/dist/commands-status-DV-i_ZIK.js +0 -16
- package/dist/commands-status.runtime-CDyGrwsI.js +0 -3
- package/dist/commands-subagents-control.runtime-B029cXAS.js +0 -3
- package/dist/commands-subagents-control.runtime-DR8Qspe0.js +0 -2
- package/dist/commands-system-prompt-C10ctsG3.js +0 -162
- package/dist/commands-system-prompt-DQlRWwnk.js +0 -2
- package/dist/commands.runtime-BYmIAuN0.js +0 -176
- package/dist/compact-14Ljaaeu.js +0 -480
- package/dist/compact-B--ovdkx.js +0 -1141
- package/dist/compact.runtime-E0Idf2Dq.js +0 -12
- package/dist/completion-cli-DMvvQGSk.js +0 -315
- package/dist/computer-use-CcLwX5SR.js +0 -367
- package/dist/config-B39SIZNb.js +0 -2
- package/dist/config-D4rsGOyV.js +0 -373
- package/dist/config-mutations-CsI3YJu7.js +0 -159
- package/dist/context-engine-host-compat-BZpDFiMJ.js +0 -2
- package/dist/context-engine-host-compat-runUdES5.js +0 -288
- package/dist/context-engine-lifecycle-D6odtNrn.js +0 -1274
- package/dist/control-auth-BPGpPtfz.js +0 -114
- package/dist/control-service-C-OmdPCe.js +0 -145
- package/dist/control-ui/assets/agents-GeyOHPuW.js +0 -1008
- package/dist/control-ui/assets/channel-config-extras-D7en6iUg.js +0 -2
- package/dist/control-ui/assets/channels-DkEyr1w5.js +0 -367
- package/dist/control-ui/assets/cron-C-wThQ1Q.js +0 -1013
- package/dist/control-ui/assets/debug-83AFRtIX.js +0 -97
- package/dist/control-ui/assets/index-CiGEhMOA.js +0 -7370
- package/dist/control-ui/assets/instances-CVl0t-1h.js +0 -57
- package/dist/control-ui/assets/logs-eybVEXxg.js +0 -74
- package/dist/control-ui/assets/nodes-C_A7eoU2.js +0 -436
- package/dist/control-ui/assets/sessions-Cq4Nc69u.js +0 -399
- package/dist/control-ui/assets/skills-ZN6hroIh.js +0 -314
- package/dist/control-ui/assets/skills-shared-DIWGwmdC.js +0 -11
- package/dist/conversation-binding-runtime-C2U1JElL.js +0 -4
- package/dist/conversation-runtime-NmIUd3Zu.js +0 -31
- package/dist/core-DGKJP1dm.js +0 -282
- package/dist/core-api-1fA4sNeC.js +0 -5
- package/dist/core-api-BY822Quq.js +0 -2
- package/dist/crestodian-Bp-NXiBC.js +0 -55
- package/dist/daocore-tools-Defpam0j.js +0 -11727
- package/dist/delivery-CEdVAUGB.js +0 -1002
- package/dist/dialogue-CZ69INPq.js +0 -37
- package/dist/dir-fetch-tool-BCicHhQE.js +0 -565
- package/dist/dir-list-tool-Cv_WktsJ.js +0 -100
- package/dist/direct-dm-Bz89rM8x.js +0 -64
- package/dist/directive-handling.fast-lane-DDnbcc5S.js +0 -68
- package/dist/directive-handling.impl-B0H52Ymr.js +0 -818
- package/dist/directive-handling.impl-CgKpwEW-.js +0 -2
- package/dist/directive-handling.model-selection-_cXAr0vt.js +0 -122
- package/dist/directive-handling.persist.runtime-Dw0mfzXT.js +0 -263
- package/dist/dispatch-C3AeYvyP.js +0 -1640
- package/dist/dispatch-acp-transcript.runtime-CP8pqBwS.js +0 -40
- package/dist/dispatch-acp.runtime-Cdap-AZI.js +0 -18
- package/dist/doctor-BZwVH97p.js +0 -2
- package/dist/doctor-BfkGBii1.js +0 -6
- package/dist/doctor-config-flow-D3JASGDt.js +0 -1741
- package/dist/doctor-core-checks-DH5AIT0Q.js +0 -573
- package/dist/doctor-core-checks-DiNu7VSh.js +0 -2
- package/dist/doctor-health-BDIJ-Nro.js +0 -65
- package/dist/doctor-health-contributions-BgmF7w8X.js +0 -696
- package/dist/doctor-lint-uIxkMUSO.js +0 -94
- package/dist/doctor-state-integrity-ws1b_BGk.js +0 -1231
- package/dist/doctor-update-Cx4CqxaX.js +0 -58
- package/dist/dynamic-tools-DiqOxhJh.js +0 -486
- package/dist/embedded-backend-C634irMl.js +0 -579
- package/dist/embedded-gateway-stub.runtime-CCfrTOeN.js +0 -12
- package/dist/exec-approvals-D85KThSg.js +0 -149
- package/dist/file-fetch-tool-BERaGYCT.js +0 -124
- package/dist/file-write-tool-BjHrMyfe.js +0 -127
- package/dist/format-DE9PuPg1.js +0 -1145
- package/dist/gateway-cli-B5hjhbUy.js +0 -435
- package/dist/gateway-method-runtime-Bu0E9Eki.js +0 -21
- package/dist/get-reply-6PtPz9hv.js +0 -4689
- package/dist/get-reply-from-config.runtime-CwprvhoR.js +0 -2
- package/dist/graph-users-DUp1kgnS.js +0 -1419
- package/dist/group-access-D5GBQ3w6.js +0 -112
- package/dist/handle-action.guild-admin-CA0Y-buD.js +0 -288
- package/dist/harness-B5nOFVfm.js +0 -61
- package/dist/health-D5heIDj3.js +0 -4
- package/dist/heartbeat-runner-BzFkIFHh.js +0 -5
- package/dist/heartbeat-runner.runtime-p6H1_xMV.js +0 -4
- package/dist/hooks-BvTyQ14X.js +0 -534
- package/dist/inbound-direct-dm-runtime-DwiDXi8L.js +0 -2
- package/dist/inbound-reply-dispatch-YoDUOC6C.js +0 -148
- package/dist/init-CPtcV5Xu.js +0 -59
- package/dist/inline-buttons-DeD5d42c.js +0 -40
- package/dist/internal-events-DEb50Sw3.js +0 -90
- package/dist/isolated-agent-CcxFFZ-Z.js +0 -1118
- package/dist/isolated-agent-CpHzq5qr.js +0 -2
- package/dist/lifecycle-DNel-oMe.js +0 -571
- package/dist/list.probe-DCa3N25d.js +0 -449
- package/dist/list.status-command-CyeAJmZW.js +0 -789
- package/dist/llm-slug-generator-CyqTY37Z.js +0 -78
- package/dist/local-dispatch.runtime-nE39kFIP.js +0 -9
- package/dist/manager.runtime-CpAPB8D5.js +0 -2714
- package/dist/markdown-to-line-Ci6TRmoZ.js +0 -811
- package/dist/mcp-http-BR0vmxFL.js +0 -2
- package/dist/mcp-http-C0oniA8h.js +0 -555
- package/dist/media-understanding-provider-BwA0XqC3.js +0 -339
- package/dist/message-actions-C9C_Ngkd.js +0 -145
- package/dist/message-handler-CUUKPC5n.js +0 -384
- package/dist/message-handler-xRTfIXWV.js +0 -1715
- package/dist/message-handler.preflight-Ch2Q7V3M.js +0 -1125
- package/dist/message-handler.process-HM3mK93q.js +0 -1484
- package/dist/model-CB8Ex5xS.js +0 -74
- package/dist/model-selection-2s0Dinux.js +0 -272
- package/dist/models-Cn-6DJ41.js +0 -2
- package/dist/models-CzszJD__.js +0 -104
- package/dist/models-cli-CbhElnA5.js +0 -256
- package/dist/monitor-7aprTsMV.js +0 -715
- package/dist/monitor-BuVhqwDS.js +0 -1370
- package/dist/monitor-C4H_YkgY.js +0 -2
- package/dist/monitor-CPQsMxgv.js +0 -1657
- package/dist/monitor-Ce0V1PiR.js +0 -4377
- package/dist/monitor-D0bcGJWI.js +0 -60
- package/dist/monitor-DEjlJqzh.js +0 -834
- package/dist/monitor-auth-DjJZsjgV.js +0 -179
- package/dist/monitor-ikSYagv3.js +0 -2788
- package/dist/monitor-polling.runtime-D7yEflMM.js +0 -883
- package/dist/monitor-webhook.runtime-CIZt-biI.js +0 -387
- package/dist/monitor.account-C2sMOBCS.js +0 -5233
- package/dist/monitor.runtime-BAi6zmcn.js +0 -2
- package/dist/monitor.webhook-BcjpfRCX.js +0 -180
- package/dist/node-cli-sessions-CIhkJRU4.js +0 -1228
- package/dist/openai-http-Dj21RwSj.js +0 -824
- package/dist/openresponses-http-COnnr1ox.js +0 -1173
- package/dist/operations-H3vUh0lM.js +0 -805
- package/dist/outbound-adapter-WLtWE7wv.js +0 -543
- package/dist/outbound-session-route-CZtd64EH.js +0 -45
- package/dist/outbound.runtime-D98P0sN1.js +0 -2
- package/dist/pi-embedded-BWJzd4mK.js +0 -4
- package/dist/pi-embedded-DeNsSqQQ.js +0 -3796
- package/dist/pi-embedded.runtime-CZnNwFpc.js +0 -4
- package/dist/pi-tools-CNHSpjBa.js +0 -2413
- package/dist/plan-B0reFFlM.js +0 -81
- package/dist/plan-CrfF-TH8.js +0 -112
- package/dist/plugin-DI_8eYOe.js +0 -12396
- package/dist/plugin-app-cache-key-WaTUD3e-.js +0 -46
- package/dist/plugin-enabled-aWLXgGGi.js +0 -233
- package/dist/plugin-registration-ByjRIVJm.js +0 -88
- package/dist/plugin-service-VQm_241d.js +0 -1229
- package/dist/policy-BmJH-swe.js +0 -680
- package/dist/policy-DE-bO1zn.js +0 -138
- package/dist/prepare.runtime-bSj3-res.js +0 -732
- package/dist/preview-warnings-CbuGYsF9.js +0 -392
- package/dist/probe-C_rWg7_m.js +0 -682
- package/dist/probe-CagOFfx6.js +0 -2
- package/dist/probe-thX1HqOh.js +0 -2204
- package/dist/probe-zU6B6gFt.js +0 -47
- package/dist/program-BtVdH743.js +0 -131
- package/dist/provider-CDoD7tO0.js +0 -8735
- package/dist/provider-CK1gMdJ2.js +0 -32
- package/dist/provider-DJqr9djy.js +0 -32
- package/dist/provider-DsOJp5bK.js +0 -152
- package/dist/provider-dispatcher-UNQ-LSx9.js +0 -22
- package/dist/provider-session.runtime-DMrkMb4x.js +0 -9
- package/dist/provider.runtime-C2-t3zm8.js +0 -2
- package/dist/public-surface-loader-hTeyyHcd.js +0 -114
- package/dist/pw-ai-BaL3eVYx.js +0 -3029
- package/dist/pw-role-snapshot-DKeBqhN1.js +0 -333
- package/dist/reaction-level-BV2potsD.js +0 -19
- package/dist/reaction-runtime-api-CCBxgM10.js +0 -116
- package/dist/realtime-transcription-provider-CHYtIXZm.js +0 -205
- package/dist/register-BJf28G9B.js +0 -2178
- package/dist/register.agent-oXAHwNQv.js +0 -156
- package/dist/register.crestodian-CQ0sqdV2.js +0 -24
- package/dist/register.maintenance-CylVRs5M.js +0 -83
- package/dist/register.runtime-CcDgwy0X.js +0 -54
- package/dist/register.subclis-CpBYNw2X.js +0 -3
- package/dist/register.subclis-N2CvieOL.js +0 -31
- package/dist/register.subclis-core-BHaGvbzg.js +0 -273
- package/dist/repair-sequencing-EBBnBdIB.js +0 -640
- package/dist/reply-delivery-BLoPALlI.js +0 -196
- package/dist/reply-runtime-D4asNTSa.js +0 -11
- package/dist/reply.runtime-CwprvhoR.js +0 -2
- package/dist/request-DlWPRUDt.js +0 -54
- package/dist/resolve-allowlist-BukQf58x.js +0 -220
- package/dist/result-fallback-classifier-DyHFnqfV.js +0 -79
- package/dist/route-BMf4keN5.js +0 -469
- package/dist/route-resolution-DWNi1QAu.js +0 -274
- package/dist/routes-C20LC8c4.js +0 -3602
- package/dist/routes-CkK7Vv_m.js +0 -2
- package/dist/run-KDF_AEza.js +0 -1162
- package/dist/run-attempt-JHYCfonU.js +0 -7704
- package/dist/run-command-3PVihYIy.js +0 -2
- package/dist/run-command-QHAXnyKY.js +0 -23
- package/dist/run-embedded.runtime-BJw-2vss.js +0 -4
- package/dist/run-execution-cli.runtime-GHClMn2g.js +0 -4
- package/dist/run-subagent-registry.runtime-BLLMrhtE.js +0 -2
- package/dist/runtime-C2u-dC1r.js +0 -1287
- package/dist/runtime-ClyfkDWT.js +0 -6179
- package/dist/runtime-WXCarlPc.js +0 -438
- package/dist/runtime-api-3QhGLpZf.js +0 -13
- package/dist/runtime-api-DRAu3mjv.js +0 -4
- package/dist/runtime-api-JE07pOem.js +0 -13
- package/dist/runtime-api-JuKylHvw.js +0 -24
- package/dist/runtime-api-bcbk4yax.js +0 -21
- package/dist/runtime-api-dDe9U2_V.js +0 -17
- package/dist/runtime-api-uMexLqGS.js +0 -3
- package/dist/runtime-api.actions-Bo7TLDFl.js +0 -3
- package/dist/runtime-api.monitor-7q78L1Em.js +0 -6
- package/dist/runtime-api.send-Bf9RR4nV.js +0 -4
- package/dist/runtime-api.threads-Dj2QuhHj.js +0 -2
- package/dist/runtime-channel-k1C0Satl.js +0 -2
- package/dist/runtime-channel-qt9t0J-J.js +0 -150
- package/dist/runtime-embedded-pi.runtime-BPB9NlTS.js +0 -2
- package/dist/sanitize-outbound-BLH_SQvg.js +0 -127
- package/dist/sdk-setup-tools-BshlBPau.js +0 -8
- package/dist/secrets-CsHXv7d3.js +0 -113
- package/dist/security-audit-CUXBQXNX.js +0 -122
- package/dist/security-audit-CzdXbRma.js +0 -118
- package/dist/security-audit.runtime-Ca0FMFJG.js +0 -2
- package/dist/selection-BmvQJlSD.js +0 -3
- package/dist/selection-BplDXc9w.js +0 -16157
- package/dist/send--qFg8a9v.js +0 -1631
- package/dist/send-BPdryPPA.js +0 -192
- package/dist/send-Bl4YaRkS.js +0 -143
- package/dist/send-Ccm7RMXC.js +0 -2
- package/dist/send.components-c3OUPSmR.js +0 -2
- package/dist/send.components-rhQJYoXs.js +0 -500
- package/dist/send.runtime-dfUkAp5d.js +0 -2
- package/dist/server-DRhyTMLe.js +0 -73
- package/dist/server-Dv4BzjGn.js +0 -24
- package/dist/server-context-4HRJy2vp.js +0 -955
- package/dist/server-context-DlxYb1G5.js +0 -2
- package/dist/server-cron-Bt8Pkc3i.js +0 -2989
- package/dist/server-cron-CtNWsa4Z.js +0 -2
- package/dist/server-methods-rye_okUW.js +0 -16494
- package/dist/server-node-events-C1yZ5a1u.js +0 -596
- package/dist/server-plugin-bootstrap-DUMyVtlP.js +0 -70
- package/dist/server-plugins-B5ZWWkRE.js +0 -432
- package/dist/server-reload-handlers-CTnMXNjT.js +0 -714
- package/dist/server-restart-sentinel-CAYxtwmY.js +0 -747
- package/dist/server-restart-sentinel-DqrtSIvy.js +0 -2
- package/dist/server-runtime-services-CuptQTe5.js +0 -2
- package/dist/server-runtime-services-aaS2IiW3.js +0 -267
- package/dist/server-startup-plugins-D0ymJgQT.js +0 -113
- package/dist/server-startup-post-attach-DlrN2uyt.js +0 -716
- package/dist/server-ws-runtime-CdpLbv4o.js +0 -349
- package/dist/server.impl-Cb4qcMTg.js +0 -2586
- package/dist/service-qxvDlMQE.js +0 -1446
- package/dist/session-binding-6bLobLHL.js +0 -219
- package/dist/session-binding-Bm6fCpoY.js +0 -2
- package/dist/session-kill-http-B6WjVo8V.js +0 -121
- package/dist/session-reset-service-GUVIhxp4.js +0 -625
- package/dist/session-route-BcRDnvzG.js +0 -93
- package/dist/session-status.runtime-BcjOunV4.js +0 -2
- package/dist/session-subagent-reactivation.runtime-BSmk_KYn.js +0 -2
- package/dist/session-tab-registry-BBYzbSOu.js +0 -521
- package/dist/sessions-history-http-bLJJfgLm.js +0 -430
- package/dist/sessions.runtime-DNRtQzCk.js +0 -2
- package/dist/setup-api-CcolVVJs.js +0 -29
- package/dist/setup-core-eJD18F3B.js +0 -174
- package/dist/setup-surface-B75C2Qtz.js +0 -221
- package/dist/setup-surface-CFuPfVHp.js +0 -405
- package/dist/setup-surface-Dyy-Mzyz.js +0 -288
- package/dist/setup-surface-ZsVF_g8W.js +0 -320
- package/dist/shared-BkCNrcLX.js +0 -121
- package/dist/shared-client-B7zqC9p2.js +0 -2
- package/dist/shared-client-yXjKgZBz.js +0 -629
- package/dist/side-question-DU3gESCb.js +0 -683
- package/dist/skill-tool-dispatch.runtime-CPBuqyvk.js +0 -143
- package/dist/slash-state-Do3bAahA.js +0 -2166
- package/dist/speech-provider-Bg9los3C.js +0 -184
- package/dist/src-D7LzUctH.js +0 -4256
- package/dist/startup-context-CCF2gIOl.js +0 -313
- package/dist/status-subagents.runtime-DX5FTymp.js +0 -18
- package/dist/status-text-B-1u5dSV.js +0 -296
- package/dist/sticker-cache-DwpU_9RJ.js +0 -206
- package/dist/sticker-vision.runtime-Bbldi_YL.js +0 -17
- package/dist/subagent-announce-BCo0VHVL.js +0 -354
- package/dist/subagent-announce-delivery-JcnuDN_N.js +0 -958
- package/dist/subagent-control-BzbA3Suz.js +0 -508
- package/dist/subagent-hooks-BIGZQWrG.js +0 -2
- package/dist/subagent-hooks-Bj4qYZfv.js +0 -230
- package/dist/subagent-hooks-C-rvhVBv.js +0 -2
- package/dist/subagent-hooks-P01_AFl5.js +0 -116
- package/dist/subagent-hooks-VEfak8nl.js +0 -2
- package/dist/subagent-hooks-api-BPnSxxN4.js +0 -23
- package/dist/subagent-hooks-api-DwIAvMoS.js +0 -22
- package/dist/subagent-hooks-api-kgyR9FOb.js +0 -23
- package/dist/subagent-hooks-oED56wqq.js +0 -146
- package/dist/subagent-orphan-recovery-D79ZzwKN.js +0 -352
- package/dist/subagent-registry-D0soBT5j.js +0 -2351
- package/dist/subagent-registry-DRJDkmty.js +0 -3
- package/dist/subagent-session-cleanup-9eAO1aJe.js +0 -525
- package/dist/subagent-spawn-D80vbogm.js +0 -1164
- package/dist/target-id-COLv5LsJ.js +0 -107
- package/dist/targets-CDW5IQ6a.js +0 -44
- package/dist/targets-Ci6O1ZdP.js +0 -19
- package/dist/targets-CsaWFBg1.js +0 -19
- package/dist/testing-BoJit-h1.js +0 -267
- package/dist/thread-bindings-CGCvw0KT.js +0 -571
- package/dist/thread-bindings-ClCTNacX.js +0 -228
- package/dist/thread-bindings-DRb7BMZ6.js +0 -8
- package/dist/thread-bindings-Xc6smav0.js +0 -232
- package/dist/thread-bindings.discord-api-DC467oeF.js +0 -187
- package/dist/thread-bindings.manager-C_mpTDIZ.js +0 -536
- package/dist/thread-bindings.manager-DkCRs612.js +0 -2
- package/dist/thread-lifecycle-DpqCXlx9.js +0 -1614
- package/dist/token-CI6HjbTA.js +0 -134
- package/dist/tool-BMe7hjBK.js +0 -139
- package/dist/tool-actions.runtime-D-h5PI_m.js +0 -534
- package/dist/tool-resolution-CnLx0CHe.js +0 -149
- package/dist/tools-effective-inventory-BR6MUMtE.js +0 -204
- package/dist/tools-invoke-http-CTTs2yMT.js +0 -67
- package/dist/tools-invoke-shared-9B1EjXWf.js +0 -200
- package/dist/tts-eGOviZ5c.js +0 -66
- package/dist/tui-Bw0HqKd7.js +0 -4709
- package/dist/tui-CIZJnPzj.js +0 -2
- package/dist/tui-backend-BJ_r7tcF.js +0 -256
- package/dist/tui-cli-BaSMBpuA.js +0 -37
- package/dist/update-cli-zuCybGNR.js +0 -3664
- package/dist/update-runner-Dek2BHmQ.js +0 -2379
- package/dist/vision-tools-1ps0BEE5.js +0 -1409
- package/dist/web-search-B7EziZXE.js +0 -62
- package/dist/web-search-provider.runtime-DGTCvGch.js +0 -328
- package/dist/web-search-provider.runtime-DWL5t39M.js +0 -2
- package/dist/xai-oauth-CRtsj2Gs.js +0 -479
- package/dist/xai-user-agent-Dndwzw2S.js +0 -32
- /package/dist/{acp-runtime-backend-DSDBcyh9.js → acp-runtime-backend-Cxo7eBHf.js} +0 -0
- /package/dist/{channel-actions-FV66JqtI.js → channel-actions-UDeVjgGz.js} +0 -0
- /package/dist/{command-status-runtime-BRWKSoG7.js → command-status-runtime-CoHd4Fws.js} +0 -0
- /package/dist/{delegate-DGqKhwB4.js → delegate-B-2xZ77o.js} +0 -0
- /package/dist/{dispatch-acp-Ceoxja_Z.js → dispatch-acp-DLPkmK7K.js} +0 -0
- /package/dist/{heartbeat-runner-CJHvr5pG.js → heartbeat-runner-D2j6JwOI.js} +0 -0
- /package/dist/{library-Bq3aDek_.js → library-DBT0cIPP.js} +0 -0
- /package/dist/{run-executor.runtime-DNJhGPbA.js → run-executor.runtime-DE4J7f4M.js} +0 -0
- /package/dist/{shared-CYxmRpq1.js → shared-xeo8Yh5n.js} +0 -0
|
@@ -0,0 +1,4377 @@
|
|
|
1
|
+
import { a as normalizeLowercaseStringOrEmpty, c as normalizeOptionalString, d as normalizeStringifiedOptionalString } from "./string-coerce-DyL154ka.js";
|
|
2
|
+
import { b as escapeRegExp } from "./utils-CNnMhEDp.js";
|
|
3
|
+
import { F as resolveSessionStoreEntry } from "./store-load-B817r-3Z.js";
|
|
4
|
+
import { a as getReplyPayloadTtsSupplement, n as buildTtsSupplementMediaPayload } from "./reply-payload-CiT5mlcY.js";
|
|
5
|
+
import { i as writeJsonFileAtomically, n as readJsonFileWithFallback } from "./json-store-GaAZLixM.js";
|
|
6
|
+
import { a as createChannelProgressDraftGate, c as formatChannelProgressDraftText, f as mergeChannelProgressDraftLine, i as buildChannelProgressDraftLineForEntry, o as formatChannelProgressDraftLine, p as normalizeChannelProgressDraftLineIdentity, s as formatChannelProgressDraftLineForEntry, u as isChannelProgressDraftWorkToolName, y as resolveChannelProgressDraftMaxLines } from "./channel-streaming-BBW2i40H.js";
|
|
7
|
+
import { a as resolveInboundLastRouteSessionKey } from "./resolve-route-C0-Q-Gjg.js";
|
|
8
|
+
import { c as defineStableChannelIngressIdentity, n as createChannelIngressResolver } from "./runtime-W39pAdFK.js";
|
|
9
|
+
import { a as warnMissingProviderGroupPolicyFallbackOnce, n as resolveAllowlistProviderRuntimeGroupPolicy, r as resolveDefaultGroupPolicy, t as GROUP_POLICY_BLOCKED_LABEL } from "./runtime-group-policy-CmZDlIwd.js";
|
|
10
|
+
import { l as resolvePinnedMainDmOwnerFromAllowlist } from "./dm-policy-shared-Bj-e8bT2.js";
|
|
11
|
+
import { r as getAgentScopedMediaLocalRoots } from "./local-roots-CcT8yBYy.js";
|
|
12
|
+
import { t as resolveAckReaction } from "./identity-ZuObO2Tn.js";
|
|
13
|
+
import { a as deliverWithFinalizableLivePreviewAdapter, n as createPreviewMessageReceipt, r as defineFinalizableLivePreviewAdapter } from "./live-CF0ee90S.js";
|
|
14
|
+
import { c as resolveThreadBindingMaxAgeMsForChannel, o as resolveThreadBindingIdleTimeoutMsForChannel } from "./thread-bindings-policy-BZBsdp3j.js";
|
|
15
|
+
import "./string-coerce-runtime-DcopKqDR.js";
|
|
16
|
+
import "./routing-AC-P99jg.js";
|
|
17
|
+
import { o as buildInboundHistoryFromEntries } from "./history-CaPbjSZl.js";
|
|
18
|
+
import { n as createReplyPrefixOptions } from "./reply-prefix-BDa6jAfF.js";
|
|
19
|
+
import { t as createTypingCallbacks } from "./typing-C657onUT.js";
|
|
20
|
+
import { i as mergePairLoopGuardConfig } from "./pair-loop-guard-runtime-jabAgbOL.js";
|
|
21
|
+
import { n as hasFinalChannelTurnDispatch } from "./dispatch-result-BM3RYtx4.js";
|
|
22
|
+
import { t as evaluateSupplementalContextVisibility } from "./context-visibility-C9pX_aod.js";
|
|
23
|
+
import { a as waitUntilAbort } from "./channel-lifecycle.core-CDt0hRZR.js";
|
|
24
|
+
import { c as buildAllowlistResolutionSummary, d as patchAllowlistUsersInConfigEntries, f as summarizeMapping, l as canonicalizeAllowlistWithResolvedIds, s as addAllowlistUserEntriesFromConfigEntry } from "./allow-from-BsbZfqm-.js";
|
|
25
|
+
import { n as isDangerousNameMatchingEnabled } from "./dangerous-name-matching-jjAiM0PK.js";
|
|
26
|
+
import "./reply-history-jazYo7cq.js";
|
|
27
|
+
import { t as CHANNEL_APPROVAL_NATIVE_RUNTIME_CONTEXT_CAPABILITY } from "./approval-handler-adapter-runtime-uo1rB4v-.js";
|
|
28
|
+
import { r as registerChannelRuntimeContext } from "./channel-runtime-context-DnHILMVu.js";
|
|
29
|
+
import { t as resolveChannelContextVisibilityMode } from "./context-visibility-Si-TIKp_.js";
|
|
30
|
+
import "./inbound-reply-dispatch-kgSDM8ax.js";
|
|
31
|
+
import "./security-runtime-Q5KQBgu1.js";
|
|
32
|
+
import { n as createTransportActivityStatusPatch, t as createConnectedChannelStatusPatch } from "./gateway-runtime-raEVqCxi.js";
|
|
33
|
+
import { n as logInboundDrop, r as logTypingFailure } from "./logging-Dr3f0mtP.js";
|
|
34
|
+
import "./channel-feedback-66g2TOgS.js";
|
|
35
|
+
import { n as toLocationContext, t as formatLocationText } from "./location-B0jdizlz.js";
|
|
36
|
+
import "./channel-lifecycle-DDnbT5P-.js";
|
|
37
|
+
import "./channel-ingress-runtime-BZvnfysR.js";
|
|
38
|
+
import "./channel-message-YAmKOwAT.js";
|
|
39
|
+
import { t as loadSessionStore } from "./session-store-runtime-BZIAo9w7.js";
|
|
40
|
+
import "./text-utility-runtime-DMDtY7pX.js";
|
|
41
|
+
import { t as isMatrixQualifiedUserId } from "./target-ids-1sTIwV96.js";
|
|
42
|
+
import { a as resolveMatrixAccountConfig, i as resolveMatrixAccountAllowlistConfig } from "./account-config-wMhN5mBi.js";
|
|
43
|
+
import { t as getMatrixRuntime } from "./runtime-ChnGj6Xz.js";
|
|
44
|
+
import { n as resolveConfiguredMatrixBotUserIds } from "./accounts-DQa5og50.js";
|
|
45
|
+
import { n as normalizeMatrixUserId, r as resolveMatrixAllowListMatch, t as normalizeMatrixAllowList } from "./allowlist-wB-C9Dsd.js";
|
|
46
|
+
import { r as isMatrixNotFoundError, t as formatMatrixErrorMessage } from "./errors-BDE_mkHE.js";
|
|
47
|
+
import { n as resolveMatrixRoomConfig, t as resolveMatrixStoredSessionMeta } from "./session-store-metadata-lBuKAvwl.js";
|
|
48
|
+
import { i as resolveMatrixStateFilePath } from "./storage-xeGtUCQp.js";
|
|
49
|
+
import { a as sendMessageMatrix, g as isPollStartType, h as isPollEventType, p as formatPollAsText, t as chunkMatrixText, v as parsePollStartContent } from "./send-CQTcckeM.js";
|
|
50
|
+
import { o as MATRIX_DAOCORE_FINALIZED_PREVIEW_KEY, r as promoteMatrixDirectRoomCandidate } from "./direct-management-BE4UOcoE.js";
|
|
51
|
+
import { o as readJoinedMatrixMembers, r as isStrictDirectMembership, t as hasDirectMatrixMemberFlag } from "./direct-room-BifRU7rS.js";
|
|
52
|
+
import { t as createMatrixThreadBindingManager } from "./thread-bindings-CWBbfbdg.js";
|
|
53
|
+
import { t as createAsyncLock } from "./async-lock-BcLS4KOc.js";
|
|
54
|
+
import { n as LogService } from "./logger-eLmZej6S.js";
|
|
55
|
+
import { i as isMatrixMediaSizeLimitError, r as MatrixMediaSizeLimitError } from "./http-client-BWQMBBCb.js";
|
|
56
|
+
import { i as throwIfMatrixStartupAborted, r as isMatrixStartupAbortError } from "./startup-abort-B-YL8CNT.js";
|
|
57
|
+
import { n as isMatrixReadySyncState, r as isMatrixTerminalSyncState, t as isMatrixDisconnectedSyncState } from "./sync-state-CWbp0QSY.js";
|
|
58
|
+
import { a as resolveMatrixMessageBody, i as resolveMatrixMessageAttachment, n as formatMatrixMediaUnavailableText, r as formatMatrixMessageText, s as fetchMatrixPollSnapshot, t as formatMatrixMediaTooLargeText } from "./media-text-CrxLrJF8.js";
|
|
59
|
+
import { n as setActiveMatrixClient } from "./active-client-oYl5IAAm.js";
|
|
60
|
+
import { t as isBunRuntime } from "./runtime-BefyhPWv.js";
|
|
61
|
+
import { n as resolveMatrixAuth, r as resolveMatrixAuthContext, t as backfillMatrixAuthDeviceIdAfterStartup } from "./config-C1dtmXuT.js";
|
|
62
|
+
import { i as resolveSharedMatrixClient, n as releaseSharedClientInstance } from "./shared-B5q4S_AJ.js";
|
|
63
|
+
import "./client-XHdBH-Lw.js";
|
|
64
|
+
import "./runtime-api-D8qG_lUm.js";
|
|
65
|
+
import { t as resolveMatrixTargets } from "./resolve-targets-BAoMcmkx.js";
|
|
66
|
+
import { t as formatMatrixEncryptedEventDisabledWarning } from "./encryption-guidance-x0hOzav7.js";
|
|
67
|
+
import { a as EventType, i as resolveMatrixThreadRouting, n as resolveMatrixReplyToEventId, o as RelationType, r as resolveMatrixThreadRootId, t as resolveMatrixInboundRoute } from "./route-Cjk2ddiT.js";
|
|
68
|
+
import { format } from "node:util";
|
|
69
|
+
//#region extensions/matrix/src/matrix/monitor/auto-join.ts
|
|
70
|
+
function registerMatrixAutoJoin(params) {
|
|
71
|
+
const { client, accountConfig, runtime } = params;
|
|
72
|
+
const core = getMatrixRuntime();
|
|
73
|
+
const logVerbose = (message) => {
|
|
74
|
+
if (!core.logging.shouldLogVerbose()) return;
|
|
75
|
+
runtime.log?.(message);
|
|
76
|
+
};
|
|
77
|
+
const autoJoin = accountConfig.autoJoin ?? "off";
|
|
78
|
+
const rawAllowlist = (accountConfig.autoJoinAllowlist ?? []).map((entry) => normalizeStringifiedOptionalString(entry)).filter((entry) => Boolean(entry));
|
|
79
|
+
const autoJoinAllowlist = new Set(rawAllowlist);
|
|
80
|
+
const allowedRoomIds = new Set(rawAllowlist.filter((entry) => entry.startsWith("!")));
|
|
81
|
+
const allowedAliases = rawAllowlist.filter((entry) => entry.startsWith("#"));
|
|
82
|
+
const resolvedAliasRoomIds = /* @__PURE__ */ new Map();
|
|
83
|
+
if (autoJoin === "off") return;
|
|
84
|
+
if (autoJoin === "always") logVerbose("matrix: auto-join enabled for all invites");
|
|
85
|
+
else logVerbose("matrix: auto-join enabled for allowlist invites");
|
|
86
|
+
const resolveAllowedAliasRoomId = async (alias) => {
|
|
87
|
+
if (resolvedAliasRoomIds.has(alias)) return resolvedAliasRoomIds.get(alias) ?? null;
|
|
88
|
+
const resolved = await params.client.resolveRoom(alias);
|
|
89
|
+
if (resolved) resolvedAliasRoomIds.set(alias, resolved);
|
|
90
|
+
return resolved;
|
|
91
|
+
};
|
|
92
|
+
const resolveAllowedAliasRoomIds = async () => {
|
|
93
|
+
return (await Promise.all(allowedAliases.map(async (alias) => {
|
|
94
|
+
try {
|
|
95
|
+
return await resolveAllowedAliasRoomId(alias);
|
|
96
|
+
} catch (err) {
|
|
97
|
+
runtime.error?.(`matrix: failed resolving allowlisted alias ${alias}: ${String(err)}`);
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
}))).filter((roomId) => Boolean(roomId));
|
|
101
|
+
};
|
|
102
|
+
client.on("room.invite", async (roomId, _inviteEvent) => {
|
|
103
|
+
if (autoJoin === "allowlist") {
|
|
104
|
+
const allowedAliasRoomIds = await resolveAllowedAliasRoomIds();
|
|
105
|
+
if (!(autoJoinAllowlist.has("*") || allowedRoomIds.has(roomId) || allowedAliasRoomIds.some((resolvedRoomId) => resolvedRoomId === roomId))) {
|
|
106
|
+
logVerbose(`matrix: invite ignored (not in allowlist) room=${roomId}`);
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
try {
|
|
111
|
+
await client.joinRoom(roomId);
|
|
112
|
+
logVerbose(`matrix: joined room ${roomId}`);
|
|
113
|
+
} catch (err) {
|
|
114
|
+
runtime.error?.(`matrix: failed to join room ${roomId}: ${String(err)}`);
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
//#endregion
|
|
119
|
+
//#region extensions/matrix/src/matrix/monitor/config.ts
|
|
120
|
+
function normalizeMatrixUserLookupEntry(raw) {
|
|
121
|
+
return raw.replace(/^matrix:/i, "").replace(/^user:/i, "").trim();
|
|
122
|
+
}
|
|
123
|
+
function normalizeMatrixRoomLookupEntry(raw) {
|
|
124
|
+
return raw.replace(/^matrix:/i, "").replace(/^(room|channel):/i, "").trim();
|
|
125
|
+
}
|
|
126
|
+
function filterResolvedMatrixAllowlistEntries(entries) {
|
|
127
|
+
return entries.filter((entry) => {
|
|
128
|
+
const trimmed = entry.trim();
|
|
129
|
+
if (!trimmed) return false;
|
|
130
|
+
if (trimmed === "*") return true;
|
|
131
|
+
return isMatrixQualifiedUserId(normalizeMatrixUserLookupEntry(trimmed));
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
function filterFailClosedMatrixAllowlistEntries(entries) {
|
|
135
|
+
return entries.filter((entry) => entry.trim().length > 0);
|
|
136
|
+
}
|
|
137
|
+
function listResolvedMatrixAllowlistEntries(params) {
|
|
138
|
+
const resolvedEntries = [];
|
|
139
|
+
const seen = /* @__PURE__ */ new Set();
|
|
140
|
+
for (const entry of params.entries) {
|
|
141
|
+
const input = String(entry).trim();
|
|
142
|
+
if (!input || seen.has(input)) continue;
|
|
143
|
+
seen.add(input);
|
|
144
|
+
const resolved = params.resolvedMap.get(input);
|
|
145
|
+
if (!resolved?.resolved || !resolved.id) continue;
|
|
146
|
+
const id = normalizeMatrixUserId(resolved.id);
|
|
147
|
+
if (isMatrixQualifiedUserId(id)) resolvedEntries.push({
|
|
148
|
+
input,
|
|
149
|
+
id
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
return resolvedEntries;
|
|
153
|
+
}
|
|
154
|
+
function normalizeConfiguredMatrixAllowlistEntries(entries) {
|
|
155
|
+
const normalized = [];
|
|
156
|
+
for (const entry of entries ?? []) {
|
|
157
|
+
const trimmed = String(entry).trim();
|
|
158
|
+
if (trimmed) normalized.push(trimmed);
|
|
159
|
+
}
|
|
160
|
+
return normalized;
|
|
161
|
+
}
|
|
162
|
+
function isMatrixDangerousNameMatchingEnabled(params) {
|
|
163
|
+
return isDangerousNameMatchingEnabled(resolveMatrixAccountConfig({
|
|
164
|
+
cfg: params.cfg,
|
|
165
|
+
accountId: params.accountId
|
|
166
|
+
}));
|
|
167
|
+
}
|
|
168
|
+
function addUniqueMatrixAllowlistEntry(params) {
|
|
169
|
+
const trimmed = params.entry.trim();
|
|
170
|
+
if (!trimmed) return;
|
|
171
|
+
const key = trimmed.toLowerCase();
|
|
172
|
+
if (params.seen.has(key)) return;
|
|
173
|
+
params.seen.add(key);
|
|
174
|
+
params.entries.push(trimmed);
|
|
175
|
+
}
|
|
176
|
+
function resolveStableMatrixMonitorUserEntries(entries) {
|
|
177
|
+
const directMatches = [];
|
|
178
|
+
for (const entry of entries) {
|
|
179
|
+
const input = String(entry).trim();
|
|
180
|
+
if (!input) continue;
|
|
181
|
+
const query = normalizeMatrixUserLookupEntry(input);
|
|
182
|
+
if (!query || query === "*") continue;
|
|
183
|
+
directMatches.push(isMatrixQualifiedUserId(query) ? {
|
|
184
|
+
input,
|
|
185
|
+
resolved: true,
|
|
186
|
+
id: normalizeMatrixUserId(query)
|
|
187
|
+
} : {
|
|
188
|
+
input,
|
|
189
|
+
resolved: false
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
return buildAllowlistResolutionSummary(directMatches);
|
|
193
|
+
}
|
|
194
|
+
function logStableMatrixAllowlistUnresolved(params) {
|
|
195
|
+
if (params.unresolved.length === 0) return;
|
|
196
|
+
summarizeMapping(params.label, [], params.unresolved, params.runtime);
|
|
197
|
+
params.runtime.log?.(`${params.label} entries must be full Matrix IDs (example: @user:server). Unresolved entries will not match any sender. To match Matrix display names, set channels.matrix.dangerouslyAllowNameMatching=true.`);
|
|
198
|
+
}
|
|
199
|
+
function resolveStableMatrixMonitorUserAllowlist(params) {
|
|
200
|
+
const allowList = params.allowList;
|
|
201
|
+
const resolution = resolveStableMatrixMonitorUserEntries(allowList);
|
|
202
|
+
const canonicalized = canonicalizeAllowlistWithResolvedIds({
|
|
203
|
+
existing: allowList,
|
|
204
|
+
resolvedMap: resolution.resolvedMap
|
|
205
|
+
});
|
|
206
|
+
logStableMatrixAllowlistUnresolved({
|
|
207
|
+
label: params.label,
|
|
208
|
+
unresolved: resolution.unresolved,
|
|
209
|
+
runtime: params.runtime
|
|
210
|
+
});
|
|
211
|
+
return {
|
|
212
|
+
entries: params.failClosedOnUnresolved ? filterFailClosedMatrixAllowlistEntries(canonicalized) : filterResolvedMatrixAllowlistEntries(canonicalized),
|
|
213
|
+
resolvedEntries: listResolvedMatrixAllowlistEntries({
|
|
214
|
+
entries: allowList,
|
|
215
|
+
resolvedMap: resolution.resolvedMap
|
|
216
|
+
})
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
async function resolveMatrixMonitorUserEntries(params) {
|
|
220
|
+
const directMatches = [];
|
|
221
|
+
const pending = [];
|
|
222
|
+
for (const entry of params.entries) {
|
|
223
|
+
const input = String(entry).trim();
|
|
224
|
+
if (!input) continue;
|
|
225
|
+
const query = normalizeMatrixUserLookupEntry(input);
|
|
226
|
+
if (!query || query === "*") continue;
|
|
227
|
+
if (isMatrixQualifiedUserId(query)) {
|
|
228
|
+
directMatches.push({
|
|
229
|
+
input,
|
|
230
|
+
resolved: true,
|
|
231
|
+
id: normalizeMatrixUserId(query)
|
|
232
|
+
});
|
|
233
|
+
continue;
|
|
234
|
+
}
|
|
235
|
+
pending.push({
|
|
236
|
+
input,
|
|
237
|
+
query
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
(pending.length === 0 ? [] : await params.resolveTargets({
|
|
241
|
+
cfg: params.cfg,
|
|
242
|
+
accountId: params.accountId,
|
|
243
|
+
inputs: pending.map((entry) => entry.query),
|
|
244
|
+
kind: "user",
|
|
245
|
+
runtime: params.runtime
|
|
246
|
+
})).forEach((entry, index) => {
|
|
247
|
+
const source = pending[index];
|
|
248
|
+
if (!source) return;
|
|
249
|
+
directMatches.push({
|
|
250
|
+
input: source.input,
|
|
251
|
+
resolved: entry.resolved,
|
|
252
|
+
id: entry.id ? normalizeMatrixUserId(entry.id) : void 0
|
|
253
|
+
});
|
|
254
|
+
});
|
|
255
|
+
return buildAllowlistResolutionSummary(directMatches);
|
|
256
|
+
}
|
|
257
|
+
async function resolveMatrixMonitorUserAllowlist(params) {
|
|
258
|
+
const allowList = (params.list ?? []).map(String);
|
|
259
|
+
if (allowList.length === 0) return {
|
|
260
|
+
entries: allowList,
|
|
261
|
+
resolvedEntries: []
|
|
262
|
+
};
|
|
263
|
+
if (!isMatrixDangerousNameMatchingEnabled({
|
|
264
|
+
cfg: params.cfg,
|
|
265
|
+
accountId: params.accountId
|
|
266
|
+
})) return resolveStableMatrixMonitorUserAllowlist({
|
|
267
|
+
allowList,
|
|
268
|
+
failClosedOnUnresolved: params.failClosedOnUnresolved,
|
|
269
|
+
label: params.label,
|
|
270
|
+
runtime: params.runtime
|
|
271
|
+
});
|
|
272
|
+
const resolution = await resolveMatrixMonitorUserEntries({
|
|
273
|
+
cfg: params.cfg,
|
|
274
|
+
accountId: params.accountId,
|
|
275
|
+
entries: allowList,
|
|
276
|
+
runtime: params.runtime,
|
|
277
|
+
resolveTargets: params.resolveTargets
|
|
278
|
+
});
|
|
279
|
+
const canonicalized = canonicalizeAllowlistWithResolvedIds({
|
|
280
|
+
existing: allowList,
|
|
281
|
+
resolvedMap: resolution.resolvedMap
|
|
282
|
+
});
|
|
283
|
+
summarizeMapping(params.label, resolution.mapping, resolution.unresolved, params.runtime);
|
|
284
|
+
if (resolution.unresolved.length > 0) params.runtime.log?.(`${params.label} entries must be full Matrix IDs (example: @user:server). Unresolved entries will not match any sender.`);
|
|
285
|
+
return {
|
|
286
|
+
entries: params.failClosedOnUnresolved ? filterFailClosedMatrixAllowlistEntries(canonicalized) : filterResolvedMatrixAllowlistEntries(canonicalized),
|
|
287
|
+
resolvedEntries: listResolvedMatrixAllowlistEntries({
|
|
288
|
+
entries: allowList,
|
|
289
|
+
resolvedMap: resolution.resolvedMap
|
|
290
|
+
})
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
async function resolveMatrixMonitorLiveUserAllowlist(params) {
|
|
294
|
+
const liveEntries = normalizeConfiguredMatrixAllowlistEntries(params.entries);
|
|
295
|
+
if (liveEntries.length === 0) return [];
|
|
296
|
+
const allowNameMatching = isMatrixDangerousNameMatchingEnabled({
|
|
297
|
+
cfg: params.cfg,
|
|
298
|
+
accountId: params.accountId
|
|
299
|
+
});
|
|
300
|
+
const effective = [];
|
|
301
|
+
const seen = /* @__PURE__ */ new Set();
|
|
302
|
+
const startupByInput = new Map((params.startupResolvedEntries ?? []).map((entry) => [entry.input, entry.id]));
|
|
303
|
+
const pending = [];
|
|
304
|
+
for (const entry of liveEntries) {
|
|
305
|
+
const query = normalizeMatrixUserLookupEntry(entry);
|
|
306
|
+
if (entry === "*") {
|
|
307
|
+
addUniqueMatrixAllowlistEntry({
|
|
308
|
+
entries: effective,
|
|
309
|
+
seen,
|
|
310
|
+
entry
|
|
311
|
+
});
|
|
312
|
+
continue;
|
|
313
|
+
}
|
|
314
|
+
if (isMatrixQualifiedUserId(query)) {
|
|
315
|
+
addUniqueMatrixAllowlistEntry({
|
|
316
|
+
entries: effective,
|
|
317
|
+
seen,
|
|
318
|
+
entry: normalizeMatrixUserId(query)
|
|
319
|
+
});
|
|
320
|
+
continue;
|
|
321
|
+
}
|
|
322
|
+
const startupId = startupByInput.get(entry);
|
|
323
|
+
if (allowNameMatching && startupId) {
|
|
324
|
+
addUniqueMatrixAllowlistEntry({
|
|
325
|
+
entries: effective,
|
|
326
|
+
seen,
|
|
327
|
+
entry: startupId
|
|
328
|
+
});
|
|
329
|
+
continue;
|
|
330
|
+
}
|
|
331
|
+
if (allowNameMatching) {
|
|
332
|
+
pending.push(entry);
|
|
333
|
+
continue;
|
|
334
|
+
}
|
|
335
|
+
if (params.failClosedOnUnresolved) addUniqueMatrixAllowlistEntry({
|
|
336
|
+
entries: effective,
|
|
337
|
+
seen,
|
|
338
|
+
entry
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
if (pending.length === 0) return effective;
|
|
342
|
+
const canonicalized = canonicalizeAllowlistWithResolvedIds({
|
|
343
|
+
existing: pending,
|
|
344
|
+
resolvedMap: (await resolveMatrixMonitorUserEntries({
|
|
345
|
+
cfg: params.cfg,
|
|
346
|
+
accountId: params.accountId,
|
|
347
|
+
entries: pending,
|
|
348
|
+
runtime: params.runtime,
|
|
349
|
+
resolveTargets: params.resolveTargets ?? resolveMatrixTargets
|
|
350
|
+
})).resolvedMap
|
|
351
|
+
});
|
|
352
|
+
const resolvedEntries = params.failClosedOnUnresolved ? filterFailClosedMatrixAllowlistEntries(canonicalized) : filterResolvedMatrixAllowlistEntries(canonicalized);
|
|
353
|
+
for (const entry of resolvedEntries) addUniqueMatrixAllowlistEntry({
|
|
354
|
+
entries: effective,
|
|
355
|
+
seen,
|
|
356
|
+
entry
|
|
357
|
+
});
|
|
358
|
+
return effective;
|
|
359
|
+
}
|
|
360
|
+
async function resolveMatrixMonitorRoomsConfig(params) {
|
|
361
|
+
const roomsConfig = params.roomsConfig;
|
|
362
|
+
if (!roomsConfig || Object.keys(roomsConfig).length === 0) return roomsConfig;
|
|
363
|
+
const mapping = [];
|
|
364
|
+
const unresolved = [];
|
|
365
|
+
const nextRooms = {};
|
|
366
|
+
const allowNameMatching = isMatrixDangerousNameMatchingEnabled({
|
|
367
|
+
cfg: params.cfg,
|
|
368
|
+
accountId: params.accountId
|
|
369
|
+
});
|
|
370
|
+
if (roomsConfig["*"]) nextRooms["*"] = roomsConfig["*"];
|
|
371
|
+
const pending = [];
|
|
372
|
+
for (const [entry, roomConfig] of Object.entries(roomsConfig)) {
|
|
373
|
+
if (entry === "*") continue;
|
|
374
|
+
const input = entry.trim();
|
|
375
|
+
if (!input) continue;
|
|
376
|
+
const cleaned = normalizeMatrixRoomLookupEntry(input);
|
|
377
|
+
if (!cleaned) {
|
|
378
|
+
unresolved.push(entry);
|
|
379
|
+
continue;
|
|
380
|
+
}
|
|
381
|
+
if (cleaned.startsWith("!") && cleaned.includes(":")) {
|
|
382
|
+
if (!nextRooms[cleaned]) nextRooms[cleaned] = roomConfig;
|
|
383
|
+
if (cleaned !== input) mapping.push(`${input}→${cleaned}`);
|
|
384
|
+
continue;
|
|
385
|
+
}
|
|
386
|
+
if (!cleaned.startsWith("#") && !allowNameMatching) {
|
|
387
|
+
unresolved.push(input);
|
|
388
|
+
continue;
|
|
389
|
+
}
|
|
390
|
+
pending.push({
|
|
391
|
+
input,
|
|
392
|
+
query: cleaned,
|
|
393
|
+
config: roomConfig
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
if (pending.length > 0) (await params.resolveTargets({
|
|
397
|
+
cfg: params.cfg,
|
|
398
|
+
accountId: params.accountId,
|
|
399
|
+
inputs: pending.map((entry) => entry.query),
|
|
400
|
+
kind: "group",
|
|
401
|
+
runtime: params.runtime
|
|
402
|
+
})).forEach((entry, index) => {
|
|
403
|
+
const source = pending[index];
|
|
404
|
+
if (!source) return;
|
|
405
|
+
if (entry.resolved && entry.id) {
|
|
406
|
+
const roomKey = normalizeMatrixRoomLookupEntry(entry.id);
|
|
407
|
+
if (!nextRooms[roomKey]) nextRooms[roomKey] = source.config;
|
|
408
|
+
mapping.push(`${source.input}→${roomKey}`);
|
|
409
|
+
} else unresolved.push(source.input);
|
|
410
|
+
});
|
|
411
|
+
summarizeMapping("matrix rooms", mapping, unresolved, params.runtime);
|
|
412
|
+
if (unresolved.length > 0) params.runtime.log?.("matrix rooms must be room IDs or aliases (example: !room:server or #alias:server). Unresolved entries are ignored.");
|
|
413
|
+
const roomUsers = /* @__PURE__ */ new Set();
|
|
414
|
+
for (const roomConfig of Object.values(nextRooms)) addAllowlistUserEntriesFromConfigEntry(roomUsers, roomConfig);
|
|
415
|
+
if (roomUsers.size === 0) return nextRooms;
|
|
416
|
+
if (!allowNameMatching) {
|
|
417
|
+
const resolution = resolveStableMatrixMonitorUserEntries(Array.from(roomUsers));
|
|
418
|
+
logStableMatrixAllowlistUnresolved({
|
|
419
|
+
label: "matrix room users",
|
|
420
|
+
unresolved: resolution.unresolved,
|
|
421
|
+
runtime: params.runtime
|
|
422
|
+
});
|
|
423
|
+
return patchAllowlistUsersInConfigEntries({
|
|
424
|
+
entries: nextRooms,
|
|
425
|
+
resolvedMap: resolution.resolvedMap,
|
|
426
|
+
strategy: "canonicalize"
|
|
427
|
+
});
|
|
428
|
+
}
|
|
429
|
+
const resolution = await resolveMatrixMonitorUserEntries({
|
|
430
|
+
cfg: params.cfg,
|
|
431
|
+
accountId: params.accountId,
|
|
432
|
+
entries: Array.from(roomUsers),
|
|
433
|
+
runtime: params.runtime,
|
|
434
|
+
resolveTargets: params.resolveTargets
|
|
435
|
+
});
|
|
436
|
+
summarizeMapping("matrix room users", resolution.mapping, resolution.unresolved, params.runtime);
|
|
437
|
+
if (resolution.unresolved.length > 0) params.runtime.log?.("matrix room users entries must be full Matrix IDs (example: @user:server). Unresolved entries will not match any sender.");
|
|
438
|
+
return patchAllowlistUsersInConfigEntries({
|
|
439
|
+
entries: nextRooms,
|
|
440
|
+
resolvedMap: resolution.resolvedMap,
|
|
441
|
+
strategy: "canonicalize"
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
async function resolveMatrixMonitorConfig(params) {
|
|
445
|
+
const resolveTargets = params.resolveTargets ?? resolveMatrixTargets;
|
|
446
|
+
const [allowFrom, groupAllowFrom, roomsConfig] = await Promise.all([
|
|
447
|
+
resolveMatrixMonitorUserAllowlist({
|
|
448
|
+
cfg: params.cfg,
|
|
449
|
+
accountId: params.accountId,
|
|
450
|
+
label: "matrix dm allowlist",
|
|
451
|
+
list: params.allowFrom,
|
|
452
|
+
runtime: params.runtime,
|
|
453
|
+
resolveTargets
|
|
454
|
+
}),
|
|
455
|
+
resolveMatrixMonitorUserAllowlist({
|
|
456
|
+
cfg: params.cfg,
|
|
457
|
+
accountId: params.accountId,
|
|
458
|
+
label: "matrix group allowlist",
|
|
459
|
+
list: params.groupAllowFrom,
|
|
460
|
+
failClosedOnUnresolved: true,
|
|
461
|
+
runtime: params.runtime,
|
|
462
|
+
resolveTargets
|
|
463
|
+
}),
|
|
464
|
+
resolveMatrixMonitorRoomsConfig({
|
|
465
|
+
cfg: params.cfg,
|
|
466
|
+
accountId: params.accountId,
|
|
467
|
+
roomsConfig: params.roomsConfig,
|
|
468
|
+
runtime: params.runtime,
|
|
469
|
+
resolveTargets
|
|
470
|
+
})
|
|
471
|
+
]);
|
|
472
|
+
return {
|
|
473
|
+
allowFrom: allowFrom.entries,
|
|
474
|
+
allowFromResolvedEntries: allowFrom.resolvedEntries,
|
|
475
|
+
groupAllowFrom: groupAllowFrom.entries,
|
|
476
|
+
groupAllowFromResolvedEntries: groupAllowFrom.resolvedEntries,
|
|
477
|
+
roomsConfig
|
|
478
|
+
};
|
|
479
|
+
}
|
|
480
|
+
//#endregion
|
|
481
|
+
//#region extensions/matrix/src/matrix/monitor/direct.ts
|
|
482
|
+
const DM_CACHE_TTL_MS = 3e4;
|
|
483
|
+
const RECENT_INVITE_TTL_MS = 3e4;
|
|
484
|
+
const MAX_TRACKED_DM_ROOMS = 1024;
|
|
485
|
+
const MAX_TRACKED_DM_MEMBER_FLAGS = 2048;
|
|
486
|
+
function rememberBounded$1(map, key, value, maxSize = MAX_TRACKED_DM_ROOMS) {
|
|
487
|
+
map.set(key, value);
|
|
488
|
+
if (map.size > maxSize) {
|
|
489
|
+
const oldest = map.keys().next().value;
|
|
490
|
+
if (typeof oldest === "string") map.delete(oldest);
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
function createDirectRoomTracker(client, opts = {}) {
|
|
494
|
+
const log = opts.log ?? (() => {});
|
|
495
|
+
let lastDmUpdateMs = 0;
|
|
496
|
+
let hasSeededDmCache = false;
|
|
497
|
+
let cachedSelfUserId = null;
|
|
498
|
+
const joinedMembersCache = /* @__PURE__ */ new Map();
|
|
499
|
+
const directMemberFlagCache = /* @__PURE__ */ new Map();
|
|
500
|
+
const recentInviteCandidates = /* @__PURE__ */ new Map();
|
|
501
|
+
const locallyPromotedDirectRooms = /* @__PURE__ */ new Map();
|
|
502
|
+
const ensureSelfUserId = async () => {
|
|
503
|
+
if (cachedSelfUserId) return cachedSelfUserId;
|
|
504
|
+
try {
|
|
505
|
+
cachedSelfUserId = await client.getUserId();
|
|
506
|
+
} catch {
|
|
507
|
+
cachedSelfUserId = null;
|
|
508
|
+
}
|
|
509
|
+
return cachedSelfUserId;
|
|
510
|
+
};
|
|
511
|
+
const refreshDmCache = async () => {
|
|
512
|
+
const now = Date.now();
|
|
513
|
+
if (now - lastDmUpdateMs < DM_CACHE_TTL_MS) return;
|
|
514
|
+
lastDmUpdateMs = now;
|
|
515
|
+
hasSeededDmCache = await client.dms.update() || hasSeededDmCache;
|
|
516
|
+
};
|
|
517
|
+
const resolveJoinedMembers = async (roomId) => {
|
|
518
|
+
const cached = joinedMembersCache.get(roomId);
|
|
519
|
+
const now = Date.now();
|
|
520
|
+
if (cached && now - cached.ts < DM_CACHE_TTL_MS) return cached.members;
|
|
521
|
+
try {
|
|
522
|
+
const normalized = await readJoinedMatrixMembers(client, roomId);
|
|
523
|
+
if (!normalized) throw new Error("membership unavailable");
|
|
524
|
+
rememberBounded$1(joinedMembersCache, roomId, {
|
|
525
|
+
members: normalized,
|
|
526
|
+
ts: now
|
|
527
|
+
});
|
|
528
|
+
return normalized;
|
|
529
|
+
} catch (err) {
|
|
530
|
+
log(`matrix: dm member lookup failed room=${roomId} (${String(err)})`);
|
|
531
|
+
return null;
|
|
532
|
+
}
|
|
533
|
+
};
|
|
534
|
+
const resolveDirectMemberFlag = async (roomId, userId) => {
|
|
535
|
+
const normalizedUserId = userId?.trim();
|
|
536
|
+
if (!normalizedUserId) return null;
|
|
537
|
+
const cacheKey = `${roomId}\n${normalizedUserId}`;
|
|
538
|
+
const cached = directMemberFlagCache.get(cacheKey);
|
|
539
|
+
const now = Date.now();
|
|
540
|
+
if (cached && now - cached.ts < DM_CACHE_TTL_MS) return cached.isDirect;
|
|
541
|
+
const isDirect = await hasDirectMatrixMemberFlag(client, roomId, normalizedUserId);
|
|
542
|
+
rememberBounded$1(directMemberFlagCache, cacheKey, {
|
|
543
|
+
isDirect,
|
|
544
|
+
ts: now
|
|
545
|
+
}, MAX_TRACKED_DM_MEMBER_FLAGS);
|
|
546
|
+
return isDirect;
|
|
547
|
+
};
|
|
548
|
+
const hasRecentInviteCandidate = (roomId, remoteUserId) => {
|
|
549
|
+
const normalizedRemoteUserId = remoteUserId?.trim();
|
|
550
|
+
if (!normalizedRemoteUserId) return false;
|
|
551
|
+
const cached = recentInviteCandidates.get(roomId);
|
|
552
|
+
if (!cached) return false;
|
|
553
|
+
if (Date.now() - cached.ts >= RECENT_INVITE_TTL_MS) {
|
|
554
|
+
recentInviteCandidates.delete(roomId);
|
|
555
|
+
return false;
|
|
556
|
+
}
|
|
557
|
+
return cached.remoteUserId === normalizedRemoteUserId;
|
|
558
|
+
};
|
|
559
|
+
const canPromoteRecentInvite = async (roomId) => {
|
|
560
|
+
try {
|
|
561
|
+
return await opts.canPromoteRecentInvite?.(roomId) ?? true;
|
|
562
|
+
} catch (err) {
|
|
563
|
+
log(`matrix: recent invite promotion veto failed room=${roomId} (${String(err)})`);
|
|
564
|
+
return false;
|
|
565
|
+
}
|
|
566
|
+
};
|
|
567
|
+
const canPromoteUnmappedStrictRoom = async (roomId) => {
|
|
568
|
+
try {
|
|
569
|
+
return await opts.canPromoteUnmappedStrictRoom?.(roomId) ?? false;
|
|
570
|
+
} catch (err) {
|
|
571
|
+
log(`matrix: unmapped strict room promotion veto failed room=${roomId} (${String(err)})`);
|
|
572
|
+
return false;
|
|
573
|
+
}
|
|
574
|
+
};
|
|
575
|
+
const shouldKeepLocallyPromotedDirectRoom = async (roomId) => {
|
|
576
|
+
try {
|
|
577
|
+
return await opts.shouldKeepLocallyPromotedDirectRoom?.(roomId);
|
|
578
|
+
} catch (err) {
|
|
579
|
+
log(`matrix: local promotion keep-check failed room=${roomId} (${String(err)})`);
|
|
580
|
+
return;
|
|
581
|
+
}
|
|
582
|
+
};
|
|
583
|
+
const isExplicitlyConfiguredRoom = async (roomId) => {
|
|
584
|
+
try {
|
|
585
|
+
return await opts.isExplicitlyConfiguredRoom?.(roomId) ?? false;
|
|
586
|
+
} catch (err) {
|
|
587
|
+
log(`matrix: configured room check failed room=${roomId} (${String(err)})`);
|
|
588
|
+
return true;
|
|
589
|
+
}
|
|
590
|
+
};
|
|
591
|
+
const hasLocallyPromotedDirectRoom = (roomId, remoteUserId) => {
|
|
592
|
+
const normalizedRemoteUserId = remoteUserId?.trim();
|
|
593
|
+
if (!normalizedRemoteUserId) return false;
|
|
594
|
+
return locallyPromotedDirectRooms.get(roomId)?.remoteUserId === normalizedRemoteUserId;
|
|
595
|
+
};
|
|
596
|
+
const rememberLocallyPromotedDirectRoom = (roomId, remoteUserId) => {
|
|
597
|
+
const normalizedRemoteUserId = remoteUserId.trim();
|
|
598
|
+
if (!normalizedRemoteUserId) return;
|
|
599
|
+
rememberBounded$1(locallyPromotedDirectRooms, roomId, { remoteUserId: normalizedRemoteUserId });
|
|
600
|
+
};
|
|
601
|
+
return {
|
|
602
|
+
invalidateRoom: (roomId) => {
|
|
603
|
+
joinedMembersCache.delete(roomId);
|
|
604
|
+
for (const key of directMemberFlagCache.keys()) if (key.startsWith(`${roomId}\n`)) directMemberFlagCache.delete(key);
|
|
605
|
+
lastDmUpdateMs = 0;
|
|
606
|
+
log(`matrix: invalidated dm cache room=${roomId}`);
|
|
607
|
+
},
|
|
608
|
+
rememberInvite: (roomId, remoteUserId) => {
|
|
609
|
+
const normalizedRemoteUserId = remoteUserId.trim();
|
|
610
|
+
if (!normalizedRemoteUserId) return;
|
|
611
|
+
rememberBounded$1(recentInviteCandidates, roomId, {
|
|
612
|
+
remoteUserId: normalizedRemoteUserId,
|
|
613
|
+
ts: Date.now()
|
|
614
|
+
});
|
|
615
|
+
log(`matrix: remembered invite candidate room=${roomId} sender=${normalizedRemoteUserId}`);
|
|
616
|
+
},
|
|
617
|
+
isDirectMessage: async (params) => {
|
|
618
|
+
const { roomId, senderId } = params;
|
|
619
|
+
if (await isExplicitlyConfiguredRoom(roomId)) {
|
|
620
|
+
log(`matrix: dm rejected via explicit room config room=${roomId}`);
|
|
621
|
+
return false;
|
|
622
|
+
}
|
|
623
|
+
const selfUserId = params.selfUserId ?? await ensureSelfUserId();
|
|
624
|
+
const joinedMembers = await resolveJoinedMembers(roomId);
|
|
625
|
+
const strictDirectMembership = isStrictDirectMembership({
|
|
626
|
+
selfUserId,
|
|
627
|
+
remoteUserId: senderId,
|
|
628
|
+
joinedMembers
|
|
629
|
+
});
|
|
630
|
+
try {
|
|
631
|
+
await refreshDmCache();
|
|
632
|
+
} catch (err) {
|
|
633
|
+
log(`matrix: dm cache refresh failed (${String(err)})`);
|
|
634
|
+
}
|
|
635
|
+
if (client.dms.isDm(roomId)) {
|
|
636
|
+
if (strictDirectMembership) {
|
|
637
|
+
log(`matrix: dm detected via m.direct room=${roomId}`);
|
|
638
|
+
return true;
|
|
639
|
+
}
|
|
640
|
+
log(`matrix: ignoring stale m.direct classification room=${roomId}`);
|
|
641
|
+
}
|
|
642
|
+
if (strictDirectMembership) {
|
|
643
|
+
const directViaSelf = await resolveDirectMemberFlag(roomId, selfUserId);
|
|
644
|
+
if (directViaSelf === true) {
|
|
645
|
+
log(`matrix: dm detected via member state room=${roomId}`);
|
|
646
|
+
return true;
|
|
647
|
+
}
|
|
648
|
+
if (directViaSelf === false) {
|
|
649
|
+
log(`matrix: dm rejected via member state room=${roomId}`);
|
|
650
|
+
return false;
|
|
651
|
+
}
|
|
652
|
+
if (!hasSeededDmCache) {
|
|
653
|
+
log(`matrix: dm detected via exact 2-member fallback before dm cache seed room=${roomId}`);
|
|
654
|
+
return true;
|
|
655
|
+
}
|
|
656
|
+
if (hasLocallyPromotedDirectRoom(roomId, senderId)) {
|
|
657
|
+
if (await shouldKeepLocallyPromotedDirectRoom(roomId) !== false) {
|
|
658
|
+
log(`matrix: dm detected via local promotion room=${roomId}`);
|
|
659
|
+
return true;
|
|
660
|
+
}
|
|
661
|
+
locallyPromotedDirectRooms.delete(roomId);
|
|
662
|
+
log(`matrix: local promotion cleared room=${roomId}`);
|
|
663
|
+
}
|
|
664
|
+
if (hasRecentInviteCandidate(roomId, senderId) && await canPromoteRecentInvite(roomId)) {
|
|
665
|
+
const promotion = await promoteMatrixDirectRoomCandidate({
|
|
666
|
+
client,
|
|
667
|
+
remoteUserId: senderId ?? "",
|
|
668
|
+
roomId,
|
|
669
|
+
selfUserId
|
|
670
|
+
});
|
|
671
|
+
if (promotion.classifyAsDirect) {
|
|
672
|
+
rememberLocallyPromotedDirectRoom(roomId, senderId ?? "");
|
|
673
|
+
log(`matrix: dm detected via recent invite room=${roomId} reason=${promotion.reason} repaired=${String(promotion.repaired)}`);
|
|
674
|
+
return true;
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
if (await canPromoteUnmappedStrictRoom(roomId)) {
|
|
678
|
+
const promotion = await promoteMatrixDirectRoomCandidate({
|
|
679
|
+
client,
|
|
680
|
+
remoteUserId: senderId ?? "",
|
|
681
|
+
roomId,
|
|
682
|
+
selfUserId
|
|
683
|
+
});
|
|
684
|
+
if (promotion.classifyAsDirect) {
|
|
685
|
+
rememberLocallyPromotedDirectRoom(roomId, senderId ?? "");
|
|
686
|
+
log(`matrix: dm detected via per-room strict fallback room=${roomId} reason=${promotion.reason} repaired=${String(promotion.repaired)}`);
|
|
687
|
+
return true;
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
log(`matrix: dm check room=${roomId} result=group members=${joinedMembers?.length ?? "unknown"}`);
|
|
692
|
+
return false;
|
|
693
|
+
}
|
|
694
|
+
};
|
|
695
|
+
}
|
|
696
|
+
//#endregion
|
|
697
|
+
//#region extensions/matrix/src/matrix/monitor/access-state.ts
|
|
698
|
+
function normalizeMatrixEntry(raw) {
|
|
699
|
+
return normalizeMatrixAllowList([raw ?? ""])[0] ?? null;
|
|
700
|
+
}
|
|
701
|
+
const matrixIngressIdentity = defineStableChannelIngressIdentity({
|
|
702
|
+
key: "sender-id",
|
|
703
|
+
normalize: normalizeMatrixEntry,
|
|
704
|
+
matchEntry({ subject, entry }) {
|
|
705
|
+
const senderId = subject.identifiers[0]?.value;
|
|
706
|
+
return entry.value === "*" || resolveMatrixAllowListMatch({
|
|
707
|
+
allowList: [entry.value],
|
|
708
|
+
userId: senderId ?? ""
|
|
709
|
+
}).allowed;
|
|
710
|
+
}
|
|
711
|
+
});
|
|
712
|
+
function resolveMatrixGroupIngress(params) {
|
|
713
|
+
if (params.groupPolicy === "disabled") return {
|
|
714
|
+
groupPolicy: "disabled",
|
|
715
|
+
groupAllowFrom: []
|
|
716
|
+
};
|
|
717
|
+
if (params.effectiveRoomUsers.length > 0) return {
|
|
718
|
+
groupPolicy: "allowlist",
|
|
719
|
+
groupAllowFrom: params.effectiveRoomUsers
|
|
720
|
+
};
|
|
721
|
+
if (params.groupPolicy === "allowlist" && params.effectiveGroupAllowFrom.length > 0) return {
|
|
722
|
+
groupPolicy: "allowlist",
|
|
723
|
+
groupAllowFrom: params.effectiveGroupAllowFrom
|
|
724
|
+
};
|
|
725
|
+
return {
|
|
726
|
+
groupPolicy: "open",
|
|
727
|
+
groupAllowFrom: []
|
|
728
|
+
};
|
|
729
|
+
}
|
|
730
|
+
async function resolveMatrixMonitorAccessState(params) {
|
|
731
|
+
const dmPolicy = params.dmPolicy ?? "pairing";
|
|
732
|
+
const groupPolicy = params.groupPolicy ?? "open";
|
|
733
|
+
const effectiveGroupAllowFrom = normalizeMatrixAllowList(params.groupAllowFrom);
|
|
734
|
+
const effectiveRoomUsers = normalizeMatrixAllowList(params.roomUsers);
|
|
735
|
+
const groupIngress = resolveMatrixGroupIngress({
|
|
736
|
+
groupPolicy,
|
|
737
|
+
effectiveGroupAllowFrom,
|
|
738
|
+
effectiveRoomUsers
|
|
739
|
+
});
|
|
740
|
+
const accountId = params.accountId ?? "default";
|
|
741
|
+
const eventKind = params.eventKind ?? "message";
|
|
742
|
+
return {
|
|
743
|
+
effectiveGroupAllowFrom,
|
|
744
|
+
effectiveRoomUsers,
|
|
745
|
+
messageIngress: await createChannelIngressResolver({
|
|
746
|
+
channelId: "matrix",
|
|
747
|
+
accountId,
|
|
748
|
+
identity: matrixIngressIdentity,
|
|
749
|
+
readStoreAllowFrom: async () => params.storeAllowFrom
|
|
750
|
+
}).message({
|
|
751
|
+
subject: { stableId: params.senderId },
|
|
752
|
+
conversation: {
|
|
753
|
+
kind: params.isRoom ? "group" : "direct",
|
|
754
|
+
id: params.isRoom ? "matrix-room" : "matrix-dm"
|
|
755
|
+
},
|
|
756
|
+
event: {
|
|
757
|
+
kind: eventKind,
|
|
758
|
+
authMode: "inbound",
|
|
759
|
+
mayPair: params.isRoom ? false : eventKind === "message"
|
|
760
|
+
},
|
|
761
|
+
dmPolicy,
|
|
762
|
+
groupPolicy: params.isRoom ? groupIngress.groupPolicy : "disabled",
|
|
763
|
+
policy: { groupAllowFromFallbackToAllowFrom: false },
|
|
764
|
+
allowFrom: params.allowFrom,
|
|
765
|
+
...params.isRoom ? { groupAllowFrom: groupIngress.groupAllowFrom } : {}
|
|
766
|
+
}),
|
|
767
|
+
accountId,
|
|
768
|
+
senderId: params.senderId,
|
|
769
|
+
isRoom: params.isRoom
|
|
770
|
+
};
|
|
771
|
+
}
|
|
772
|
+
async function resolveMatrixMonitorCommandAccess(state, params) {
|
|
773
|
+
const commandAllowFrom = state.isRoom ? [] : state.messageIngress.senderAccess.effectiveAllowFrom;
|
|
774
|
+
const commandGroupAllowFrom = state.effectiveRoomUsers.length > 0 ? state.effectiveRoomUsers : state.effectiveGroupAllowFrom;
|
|
775
|
+
return (await createChannelIngressResolver({
|
|
776
|
+
channelId: "matrix",
|
|
777
|
+
accountId: state.accountId,
|
|
778
|
+
identity: matrixIngressIdentity
|
|
779
|
+
}).command({
|
|
780
|
+
subject: { stableId: state.senderId },
|
|
781
|
+
conversation: {
|
|
782
|
+
kind: state.isRoom ? "group" : "direct",
|
|
783
|
+
id: state.isRoom ? "matrix-room" : "matrix-dm"
|
|
784
|
+
},
|
|
785
|
+
dmPolicy: "allowlist",
|
|
786
|
+
groupPolicy: "allowlist",
|
|
787
|
+
policy: { groupAllowFromFallbackToAllowFrom: false },
|
|
788
|
+
allowFrom: commandAllowFrom,
|
|
789
|
+
groupAllowFrom: commandGroupAllowFrom,
|
|
790
|
+
command: {
|
|
791
|
+
useAccessGroups: params.useAccessGroups,
|
|
792
|
+
allowTextCommands: params.allowTextCommands,
|
|
793
|
+
hasControlCommand: params.hasControlCommand,
|
|
794
|
+
groupOwnerAllowFrom: "none",
|
|
795
|
+
commandGroupAllowFromFallbackToAllowFrom: false
|
|
796
|
+
}
|
|
797
|
+
})).commandAccess;
|
|
798
|
+
}
|
|
799
|
+
//#endregion
|
|
800
|
+
//#region extensions/matrix/src/matrix/monitor/verification-utils.ts
|
|
801
|
+
const VERIFICATION_EVENT_PREFIX = "m.key.verification.";
|
|
802
|
+
const VERIFICATION_REQUEST_MSGTYPE = "m.key.verification.request";
|
|
803
|
+
const VERIFICATION_NOTICE_PREFIXES = [
|
|
804
|
+
"Matrix verification request received from ",
|
|
805
|
+
"Matrix verification is ready with ",
|
|
806
|
+
"Matrix verification started with ",
|
|
807
|
+
"Matrix verification completed with ",
|
|
808
|
+
"Matrix verification cancelled by ",
|
|
809
|
+
"Matrix verification SAS with "
|
|
810
|
+
];
|
|
811
|
+
function trimMaybeString$1(input) {
|
|
812
|
+
return normalizeOptionalString(input) ?? "";
|
|
813
|
+
}
|
|
814
|
+
function isMatrixVerificationEventType(type) {
|
|
815
|
+
return trimMaybeString$1(type).startsWith(VERIFICATION_EVENT_PREFIX);
|
|
816
|
+
}
|
|
817
|
+
function isMatrixVerificationRequestMsgType(msgtype) {
|
|
818
|
+
return trimMaybeString$1(msgtype) === VERIFICATION_REQUEST_MSGTYPE;
|
|
819
|
+
}
|
|
820
|
+
function isMatrixVerificationNoticeBody(body) {
|
|
821
|
+
const text = trimMaybeString$1(body);
|
|
822
|
+
return VERIFICATION_NOTICE_PREFIXES.some((prefix) => text.startsWith(prefix));
|
|
823
|
+
}
|
|
824
|
+
function isMatrixVerificationRoomMessage(content) {
|
|
825
|
+
return isMatrixVerificationRequestMsgType(content.msgtype) || trimMaybeString$1(content.msgtype) === "m.notice" && isMatrixVerificationNoticeBody(content.body);
|
|
826
|
+
}
|
|
827
|
+
const matrixVerificationConstants = {
|
|
828
|
+
eventPrefix: VERIFICATION_EVENT_PREFIX,
|
|
829
|
+
requestMsgtype: VERIFICATION_REQUEST_MSGTYPE
|
|
830
|
+
};
|
|
831
|
+
//#endregion
|
|
832
|
+
//#region extensions/matrix/src/matrix/monitor/verification-events.ts
|
|
833
|
+
const MAX_TRACKED_VERIFICATION_EVENTS = 1024;
|
|
834
|
+
const SAS_NOTICE_RETRY_DELAY_MS = 750;
|
|
835
|
+
const VERIFICATION_EVENT_STARTUP_GRACE_MS = 3e4;
|
|
836
|
+
let matrixDirectRoomDepsPromise;
|
|
837
|
+
async function loadMatrixDirectRoomDeps() {
|
|
838
|
+
matrixDirectRoomDepsPromise ??= Promise.all([import("./direct-management-COFmFhJf.js"), import("./direct-room-o6lvHwv6.js")]).then(([directManagementModule, directRoomModule]) => ({
|
|
839
|
+
inspectMatrixDirectRooms: directManagementModule.inspectMatrixDirectRooms,
|
|
840
|
+
isStrictDirectRoom: directRoomModule.isStrictDirectRoom
|
|
841
|
+
}));
|
|
842
|
+
return await matrixDirectRoomDepsPromise;
|
|
843
|
+
}
|
|
844
|
+
function trimMaybeString(input) {
|
|
845
|
+
if (typeof input !== "string") return null;
|
|
846
|
+
const trimmed = input.trim();
|
|
847
|
+
return trimmed.length > 0 ? trimmed : null;
|
|
848
|
+
}
|
|
849
|
+
function readVerificationSignal(event) {
|
|
850
|
+
const type = trimMaybeString(event?.type) ?? "";
|
|
851
|
+
const content = event?.content ?? {};
|
|
852
|
+
const msgtype = trimMaybeString(content.msgtype) ?? "";
|
|
853
|
+
const relatedEventId = trimMaybeString(content["m.relates_to"]?.event_id);
|
|
854
|
+
const transactionId = trimMaybeString(content.transaction_id);
|
|
855
|
+
if (type === EventType.RoomMessage && isMatrixVerificationRequestMsgType(msgtype)) return {
|
|
856
|
+
stage: "request",
|
|
857
|
+
flowId: trimMaybeString(event.event_id) ?? transactionId ?? relatedEventId
|
|
858
|
+
};
|
|
859
|
+
if (!isMatrixVerificationEventType(type)) return null;
|
|
860
|
+
const flowId = transactionId ?? relatedEventId ?? trimMaybeString(event.event_id);
|
|
861
|
+
if (type === `${matrixVerificationConstants.eventPrefix}request`) return {
|
|
862
|
+
stage: "request",
|
|
863
|
+
flowId
|
|
864
|
+
};
|
|
865
|
+
if (type === `${matrixVerificationConstants.eventPrefix}ready`) return {
|
|
866
|
+
stage: "ready",
|
|
867
|
+
flowId
|
|
868
|
+
};
|
|
869
|
+
if (type === "m.key.verification.start") return {
|
|
870
|
+
stage: "start",
|
|
871
|
+
flowId
|
|
872
|
+
};
|
|
873
|
+
if (type === "m.key.verification.cancel") return {
|
|
874
|
+
stage: "cancel",
|
|
875
|
+
flowId
|
|
876
|
+
};
|
|
877
|
+
if (type === "m.key.verification.done") return {
|
|
878
|
+
stage: "done",
|
|
879
|
+
flowId
|
|
880
|
+
};
|
|
881
|
+
return {
|
|
882
|
+
stage: "other",
|
|
883
|
+
flowId
|
|
884
|
+
};
|
|
885
|
+
}
|
|
886
|
+
function formatVerificationStageNotice(params) {
|
|
887
|
+
const { stage, senderId, event } = params;
|
|
888
|
+
const content = event.content;
|
|
889
|
+
switch (stage) {
|
|
890
|
+
case "request": return `Matrix verification request received from ${senderId}. Open "Verify by emoji" in your Matrix client to continue.`;
|
|
891
|
+
case "ready": return `Matrix verification is ready with ${senderId}. Choose "Verify by emoji" to reveal the emoji sequence.`;
|
|
892
|
+
case "start": return `Matrix verification started with ${senderId}.`;
|
|
893
|
+
case "done": return `Matrix verification completed with ${senderId}.`;
|
|
894
|
+
case "cancel": {
|
|
895
|
+
const code = trimMaybeString(content.code);
|
|
896
|
+
const reason = trimMaybeString(content.reason);
|
|
897
|
+
if (code && reason) return `Matrix verification cancelled by ${senderId} (${code}: ${reason}).`;
|
|
898
|
+
if (reason) return `Matrix verification cancelled by ${senderId} (${reason}).`;
|
|
899
|
+
return `Matrix verification cancelled by ${senderId}.`;
|
|
900
|
+
}
|
|
901
|
+
default: return null;
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
function formatVerificationSasNotice(summary) {
|
|
905
|
+
const sas = summary.sas;
|
|
906
|
+
if (!sas) return null;
|
|
907
|
+
const emojiLine = Array.isArray(sas.emoji) && sas.emoji.length > 0 ? `SAS emoji: ${sas.emoji.map(([emoji, name]) => `${trimMaybeString(emoji) ?? "?"} ${trimMaybeString(name) ?? "?"}`).join(" | ")}` : null;
|
|
908
|
+
const decimalLine = Array.isArray(sas.decimal) && sas.decimal.length === 3 ? `SAS decimal: ${sas.decimal.join(" ")}` : null;
|
|
909
|
+
if (!emojiLine && !decimalLine) return null;
|
|
910
|
+
const lines = [`Matrix verification SAS with ${summary.otherUserId}:`];
|
|
911
|
+
if (emojiLine) lines.push(emojiLine);
|
|
912
|
+
if (decimalLine) lines.push(decimalLine);
|
|
913
|
+
lines.push("If both sides match, choose 'They match' in your Matrix app.");
|
|
914
|
+
return lines.join("\n");
|
|
915
|
+
}
|
|
916
|
+
function resolveVerificationFlowCandidates(params) {
|
|
917
|
+
const { event, flowId } = params;
|
|
918
|
+
const content = event.content;
|
|
919
|
+
const candidates = /* @__PURE__ */ new Set();
|
|
920
|
+
const add = (value) => {
|
|
921
|
+
const normalized = trimMaybeString(value);
|
|
922
|
+
if (normalized) candidates.add(normalized);
|
|
923
|
+
};
|
|
924
|
+
add(flowId);
|
|
925
|
+
add(event.event_id);
|
|
926
|
+
add(content.transaction_id);
|
|
927
|
+
add(content["m.relates_to"]?.event_id);
|
|
928
|
+
return Array.from(candidates);
|
|
929
|
+
}
|
|
930
|
+
function resolveSummaryRecency(summary) {
|
|
931
|
+
const ts = Date.parse(summary.updatedAt ?? "");
|
|
932
|
+
return Number.isFinite(ts) ? ts : 0;
|
|
933
|
+
}
|
|
934
|
+
function isActiveVerificationSummary(summary) {
|
|
935
|
+
if (summary.completed === true) return false;
|
|
936
|
+
if (summary.phaseName === "cancelled" || summary.phaseName === "done") return false;
|
|
937
|
+
if (typeof summary.phase === "number" && summary.phase >= 4) return false;
|
|
938
|
+
return true;
|
|
939
|
+
}
|
|
940
|
+
async function resolveVerificationSummaryForSignal(client, params) {
|
|
941
|
+
if (!client.crypto) return null;
|
|
942
|
+
await client.crypto.ensureVerificationDmTracked({
|
|
943
|
+
roomId: params.roomId,
|
|
944
|
+
userId: params.senderId
|
|
945
|
+
}).catch(() => null);
|
|
946
|
+
const list = await client.crypto.listVerifications();
|
|
947
|
+
if (list.length === 0) return null;
|
|
948
|
+
const candidates = resolveVerificationFlowCandidates({
|
|
949
|
+
event: params.event,
|
|
950
|
+
flowId: params.flowId
|
|
951
|
+
});
|
|
952
|
+
const byTransactionId = list.find((entry) => candidates.some((candidate) => entry.transactionId === candidate));
|
|
953
|
+
if (byTransactionId) return byTransactionId;
|
|
954
|
+
const { inspectMatrixDirectRooms, isStrictDirectRoom } = await loadMatrixDirectRoomDeps();
|
|
955
|
+
const activeRoomId = trimMaybeString((await inspectMatrixDirectRooms({
|
|
956
|
+
client,
|
|
957
|
+
remoteUserId: params.senderId
|
|
958
|
+
}).catch(() => null))?.activeRoomId);
|
|
959
|
+
if (activeRoomId) {
|
|
960
|
+
if (activeRoomId !== params.roomId) return null;
|
|
961
|
+
} else if (!await isStrictDirectRoom({
|
|
962
|
+
client,
|
|
963
|
+
roomId: params.roomId,
|
|
964
|
+
remoteUserId: params.senderId
|
|
965
|
+
})) return null;
|
|
966
|
+
const activeByUser = list.filter((entry) => entry.otherUserId === params.senderId && isActiveVerificationSummary(entry)).toSorted((a, b) => resolveSummaryRecency(b) - resolveSummaryRecency(a));
|
|
967
|
+
const activeInRoom = activeByUser.filter((entry) => {
|
|
968
|
+
return trimMaybeString(entry.roomId) === params.roomId;
|
|
969
|
+
});
|
|
970
|
+
if (activeInRoom.length > 0) return activeInRoom[0] ?? null;
|
|
971
|
+
return activeByUser[0] ?? null;
|
|
972
|
+
}
|
|
973
|
+
async function resolveVerificationSasNoticeForSignal(client, params) {
|
|
974
|
+
const summary = await resolveVerificationSummaryForSignal(client, params);
|
|
975
|
+
const immediateNotice = summary && isActiveVerificationSummary(summary) ? formatVerificationSasNotice(summary) : null;
|
|
976
|
+
if (immediateNotice || params.stage !== "ready" && params.stage !== "start") return {
|
|
977
|
+
summary,
|
|
978
|
+
sasNotice: immediateNotice
|
|
979
|
+
};
|
|
980
|
+
await new Promise((resolve) => setTimeout(resolve, params.sasNoticeRetryDelayMs ?? SAS_NOTICE_RETRY_DELAY_MS));
|
|
981
|
+
const retriedSummary = await resolveVerificationSummaryForSignal(client, params);
|
|
982
|
+
return {
|
|
983
|
+
summary: retriedSummary,
|
|
984
|
+
sasNotice: retriedSummary && isActiveVerificationSummary(retriedSummary) ? formatVerificationSasNotice(retriedSummary) : null
|
|
985
|
+
};
|
|
986
|
+
}
|
|
987
|
+
function trackBounded(set, value) {
|
|
988
|
+
if (!value || set.has(value)) return false;
|
|
989
|
+
set.add(value);
|
|
990
|
+
if (set.size > MAX_TRACKED_VERIFICATION_EVENTS) {
|
|
991
|
+
const oldest = set.values().next().value;
|
|
992
|
+
if (typeof oldest === "string") set.delete(oldest);
|
|
993
|
+
}
|
|
994
|
+
return true;
|
|
995
|
+
}
|
|
996
|
+
async function sendVerificationNotice(params) {
|
|
997
|
+
const roomId = trimMaybeString(params.roomId);
|
|
998
|
+
if (!roomId) return;
|
|
999
|
+
try {
|
|
1000
|
+
await params.client.sendMessage(roomId, {
|
|
1001
|
+
msgtype: "m.notice",
|
|
1002
|
+
body: params.body
|
|
1003
|
+
});
|
|
1004
|
+
} catch (err) {
|
|
1005
|
+
params.logVerboseMessage(`matrix: failed sending verification notice room=${roomId}: ${String(err)}`);
|
|
1006
|
+
}
|
|
1007
|
+
}
|
|
1008
|
+
async function isVerificationNoticeAuthorized(params) {
|
|
1009
|
+
if (!params.dmEnabled || params.dmPolicy === "disabled") {
|
|
1010
|
+
params.logVerboseMessage(`matrix: blocked verification sender ${params.senderId} (dmPolicy=${params.dmPolicy}, dmEnabled=${String(params.dmEnabled)})`);
|
|
1011
|
+
return false;
|
|
1012
|
+
}
|
|
1013
|
+
const storeAllowFrom = params.dmPolicy !== "allowlist" && params.dmPolicy !== "open" ? await params.readStoreAllowFrom() : [];
|
|
1014
|
+
if ((await resolveMatrixMonitorAccessState({
|
|
1015
|
+
allowFrom: params.allowFrom,
|
|
1016
|
+
storeAllowFrom,
|
|
1017
|
+
dmPolicy: params.dmPolicy,
|
|
1018
|
+
groupPolicy: "open",
|
|
1019
|
+
groupAllowFrom: [],
|
|
1020
|
+
roomUsers: [],
|
|
1021
|
+
senderId: params.senderId,
|
|
1022
|
+
isRoom: false
|
|
1023
|
+
})).messageIngress.senderAccess.decision === "allow") return true;
|
|
1024
|
+
params.logVerboseMessage(`matrix: blocked verification sender ${params.senderId} (dmPolicy=${params.dmPolicy})`);
|
|
1025
|
+
return false;
|
|
1026
|
+
}
|
|
1027
|
+
function createMatrixVerificationEventRouter(params) {
|
|
1028
|
+
const routerStartedAtMs = Date.now();
|
|
1029
|
+
const routedVerificationEvents = /* @__PURE__ */ new Set();
|
|
1030
|
+
const routedVerificationSasFingerprints = /* @__PURE__ */ new Set();
|
|
1031
|
+
const routedVerificationStageNotices = /* @__PURE__ */ new Set();
|
|
1032
|
+
const verificationFlowRooms = /* @__PURE__ */ new Map();
|
|
1033
|
+
const verificationUserRooms = /* @__PURE__ */ new Map();
|
|
1034
|
+
async function resolveActiveDirectRoomId(remoteUserId) {
|
|
1035
|
+
const { inspectMatrixDirectRooms } = await loadMatrixDirectRoomDeps();
|
|
1036
|
+
return trimMaybeString((await inspectMatrixDirectRooms({
|
|
1037
|
+
client: params.client,
|
|
1038
|
+
remoteUserId
|
|
1039
|
+
}).catch(() => null))?.activeRoomId);
|
|
1040
|
+
}
|
|
1041
|
+
function shouldEmitVerificationEventNotice(event) {
|
|
1042
|
+
const eventTs = typeof event.origin_server_ts === "number" && Number.isFinite(event.origin_server_ts) ? event.origin_server_ts : null;
|
|
1043
|
+
if (eventTs === null) return true;
|
|
1044
|
+
return eventTs >= routerStartedAtMs - VERIFICATION_EVENT_STARTUP_GRACE_MS;
|
|
1045
|
+
}
|
|
1046
|
+
function rememberVerificationRoom(roomId, event, flowId) {
|
|
1047
|
+
for (const candidate of resolveVerificationFlowCandidates({
|
|
1048
|
+
event,
|
|
1049
|
+
flowId
|
|
1050
|
+
})) {
|
|
1051
|
+
verificationFlowRooms.set(candidate, roomId);
|
|
1052
|
+
if (verificationFlowRooms.size > MAX_TRACKED_VERIFICATION_EVENTS) {
|
|
1053
|
+
const oldest = verificationFlowRooms.keys().next().value;
|
|
1054
|
+
if (typeof oldest === "string") verificationFlowRooms.delete(oldest);
|
|
1055
|
+
}
|
|
1056
|
+
}
|
|
1057
|
+
}
|
|
1058
|
+
function rememberVerificationUserRoom(remoteUserId, roomId) {
|
|
1059
|
+
const normalizedUserId = trimMaybeString(remoteUserId);
|
|
1060
|
+
const normalizedRoomId = trimMaybeString(roomId);
|
|
1061
|
+
if (!normalizedUserId || !normalizedRoomId) return;
|
|
1062
|
+
verificationUserRooms.delete(normalizedUserId);
|
|
1063
|
+
verificationUserRooms.set(normalizedUserId, normalizedRoomId);
|
|
1064
|
+
if (verificationUserRooms.size > MAX_TRACKED_VERIFICATION_EVENTS) {
|
|
1065
|
+
const oldest = verificationUserRooms.keys().next().value;
|
|
1066
|
+
if (typeof oldest === "string") verificationUserRooms.delete(oldest);
|
|
1067
|
+
}
|
|
1068
|
+
}
|
|
1069
|
+
async function resolveSummaryRoomId(summary) {
|
|
1070
|
+
const mappedRoomId = trimMaybeString(summary.roomId) ?? trimMaybeString(summary.transactionId ? verificationFlowRooms.get(summary.transactionId) : null) ?? trimMaybeString(verificationFlowRooms.get(summary.id));
|
|
1071
|
+
if (mappedRoomId) return mappedRoomId;
|
|
1072
|
+
const remoteUserId = trimMaybeString(summary.otherUserId);
|
|
1073
|
+
if (!remoteUserId) return null;
|
|
1074
|
+
const recentRoomId = trimMaybeString(verificationUserRooms.get(remoteUserId));
|
|
1075
|
+
const activeRoomId = await resolveActiveDirectRoomId(remoteUserId);
|
|
1076
|
+
if (recentRoomId && activeRoomId && recentRoomId === activeRoomId) return recentRoomId;
|
|
1077
|
+
if (activeRoomId) return activeRoomId;
|
|
1078
|
+
if (recentRoomId && await (await loadMatrixDirectRoomDeps()).isStrictDirectRoom({
|
|
1079
|
+
client: params.client,
|
|
1080
|
+
roomId: recentRoomId,
|
|
1081
|
+
remoteUserId
|
|
1082
|
+
})) return recentRoomId;
|
|
1083
|
+
return null;
|
|
1084
|
+
}
|
|
1085
|
+
async function routeVerificationSummary(summary) {
|
|
1086
|
+
const roomId = await resolveSummaryRoomId(summary);
|
|
1087
|
+
if (!roomId || !isActiveVerificationSummary(summary)) return;
|
|
1088
|
+
if (!await (await loadMatrixDirectRoomDeps()).isStrictDirectRoom({
|
|
1089
|
+
client: params.client,
|
|
1090
|
+
roomId,
|
|
1091
|
+
remoteUserId: summary.otherUserId
|
|
1092
|
+
})) {
|
|
1093
|
+
params.logVerboseMessage(`matrix: ignoring verification summary outside strict DM room=${roomId} sender=${summary.otherUserId}`);
|
|
1094
|
+
return;
|
|
1095
|
+
}
|
|
1096
|
+
if (!await isVerificationNoticeAuthorized({
|
|
1097
|
+
senderId: summary.otherUserId,
|
|
1098
|
+
allowFrom: params.allowFrom,
|
|
1099
|
+
dmEnabled: params.dmEnabled,
|
|
1100
|
+
dmPolicy: params.dmPolicy,
|
|
1101
|
+
readStoreAllowFrom: params.readStoreAllowFrom,
|
|
1102
|
+
logVerboseMessage: params.logVerboseMessage
|
|
1103
|
+
})) return;
|
|
1104
|
+
const sasNotice = formatVerificationSasNotice(summary);
|
|
1105
|
+
if (!sasNotice) return;
|
|
1106
|
+
if (!trackBounded(routedVerificationSasFingerprints, `${summary.id}:${JSON.stringify(summary.sas)}`)) return;
|
|
1107
|
+
await sendVerificationNotice({
|
|
1108
|
+
client: params.client,
|
|
1109
|
+
roomId,
|
|
1110
|
+
body: sasNotice,
|
|
1111
|
+
logVerboseMessage: params.logVerboseMessage
|
|
1112
|
+
});
|
|
1113
|
+
}
|
|
1114
|
+
function routeVerificationEvent(roomId, event) {
|
|
1115
|
+
const senderId = trimMaybeString(event?.sender);
|
|
1116
|
+
if (!senderId) return false;
|
|
1117
|
+
const signal = readVerificationSignal(event);
|
|
1118
|
+
if (!signal) return false;
|
|
1119
|
+
rememberVerificationRoom(roomId, event, signal.flowId);
|
|
1120
|
+
const routeTask = async () => {
|
|
1121
|
+
if (!shouldEmitVerificationEventNotice(event)) {
|
|
1122
|
+
params.logVerboseMessage(`matrix: ignoring historical verification event room=${roomId} id=${event.event_id ?? "unknown"} type=${event.type ?? "unknown"}`);
|
|
1123
|
+
return;
|
|
1124
|
+
}
|
|
1125
|
+
const flowId = signal.flowId;
|
|
1126
|
+
const sourceFingerprint = trimMaybeString(event?.event_id) ?? `${senderId}:${event.type}:${flowId ?? "none"}`;
|
|
1127
|
+
if (!await (await loadMatrixDirectRoomDeps()).isStrictDirectRoom({
|
|
1128
|
+
client: params.client,
|
|
1129
|
+
roomId,
|
|
1130
|
+
remoteUserId: senderId
|
|
1131
|
+
})) {
|
|
1132
|
+
params.logVerboseMessage(`matrix: ignoring verification event outside strict DM room=${roomId} sender=${senderId}`);
|
|
1133
|
+
return;
|
|
1134
|
+
}
|
|
1135
|
+
if (!await isVerificationNoticeAuthorized({
|
|
1136
|
+
senderId,
|
|
1137
|
+
allowFrom: params.allowFrom,
|
|
1138
|
+
dmEnabled: params.dmEnabled,
|
|
1139
|
+
dmPolicy: params.dmPolicy,
|
|
1140
|
+
readStoreAllowFrom: params.readStoreAllowFrom,
|
|
1141
|
+
logVerboseMessage: params.logVerboseMessage
|
|
1142
|
+
})) return;
|
|
1143
|
+
rememberVerificationUserRoom(senderId, roomId);
|
|
1144
|
+
if (!trackBounded(routedVerificationEvents, sourceFingerprint)) return;
|
|
1145
|
+
const stageNotice = formatVerificationStageNotice({
|
|
1146
|
+
stage: signal.stage,
|
|
1147
|
+
senderId,
|
|
1148
|
+
event
|
|
1149
|
+
});
|
|
1150
|
+
const { summary, sasNotice } = await resolveVerificationSasNoticeForSignal(params.client, {
|
|
1151
|
+
roomId,
|
|
1152
|
+
event,
|
|
1153
|
+
senderId,
|
|
1154
|
+
flowId,
|
|
1155
|
+
stage: signal.stage,
|
|
1156
|
+
sasNoticeRetryDelayMs: params.sasNoticeRetryDelayMs
|
|
1157
|
+
}).catch(() => ({
|
|
1158
|
+
summary: null,
|
|
1159
|
+
sasNotice: null
|
|
1160
|
+
}));
|
|
1161
|
+
const notices = [];
|
|
1162
|
+
if (stageNotice) {
|
|
1163
|
+
if (trackBounded(routedVerificationStageNotices, `${roomId}:${senderId}:${flowId ?? sourceFingerprint}:${signal.stage}`)) notices.push(stageNotice);
|
|
1164
|
+
}
|
|
1165
|
+
if (summary && sasNotice) {
|
|
1166
|
+
if (trackBounded(routedVerificationSasFingerprints, `${summary.id}:${JSON.stringify(summary.sas)}`)) notices.push(sasNotice);
|
|
1167
|
+
}
|
|
1168
|
+
if (notices.length === 0) return;
|
|
1169
|
+
for (const body of notices) await sendVerificationNotice({
|
|
1170
|
+
client: params.client,
|
|
1171
|
+
roomId,
|
|
1172
|
+
body,
|
|
1173
|
+
logVerboseMessage: params.logVerboseMessage
|
|
1174
|
+
});
|
|
1175
|
+
};
|
|
1176
|
+
if (params.runDetachedTask) params.runDetachedTask(`verification event handler room=${roomId} id=${event.event_id ?? "unknown"}`, routeTask);
|
|
1177
|
+
else routeTask().catch((err) => {
|
|
1178
|
+
params.logVerboseMessage(`matrix: failed routing verification event: ${String(err)}`);
|
|
1179
|
+
});
|
|
1180
|
+
return true;
|
|
1181
|
+
}
|
|
1182
|
+
return {
|
|
1183
|
+
routeVerificationEvent,
|
|
1184
|
+
routeVerificationSummary
|
|
1185
|
+
};
|
|
1186
|
+
}
|
|
1187
|
+
//#endregion
|
|
1188
|
+
//#region extensions/matrix/src/matrix/monitor/events.ts
|
|
1189
|
+
const MATRIX_POST_HEALTHY_SYNC_DECRYPT_FAILURE_WINDOW_MS = 2 * 6e4;
|
|
1190
|
+
const MATRIX_POST_HEALTHY_SYNC_DECRYPT_FAILURE_THRESHOLD = 3;
|
|
1191
|
+
const MATRIX_POST_HEALTHY_SYNC_DECRYPT_FAILURE_SAMPLE_LIMIT = 3;
|
|
1192
|
+
function formatMatrixPostHealthySyncDecryptionHint(accountId) {
|
|
1193
|
+
return `matrix: repeated fresh encrypted messages are still failing to decrypt after Matrix resumed healthy sync. This device may still be missing new room keys. Check 'daocore matrix verify status --verbose --account ${accountId}' and 'daocore matrix devices list --account ${accountId}'.`;
|
|
1194
|
+
}
|
|
1195
|
+
function isFreshPostHealthySyncDecryptFailure(params) {
|
|
1196
|
+
const { event, healthySyncSinceMs, graceMs = 0, nowMs } = params;
|
|
1197
|
+
if (typeof healthySyncSinceMs !== "number" || !Number.isFinite(healthySyncSinceMs)) return false;
|
|
1198
|
+
const eventTs = event.origin_server_ts;
|
|
1199
|
+
if (!Number.isFinite(eventTs) || eventTs <= 0) return false;
|
|
1200
|
+
if (eventTs < healthySyncSinceMs + graceMs) return false;
|
|
1201
|
+
if (eventTs > nowMs + 6e4) return false;
|
|
1202
|
+
return true;
|
|
1203
|
+
}
|
|
1204
|
+
function createMatrixPostHealthySyncDecryptFailureTracker(params) {
|
|
1205
|
+
let observations = [];
|
|
1206
|
+
let warningEmitted = false;
|
|
1207
|
+
let trackedHealthySyncSinceMs;
|
|
1208
|
+
const resetObservations = () => {
|
|
1209
|
+
observations = [];
|
|
1210
|
+
warningEmitted = false;
|
|
1211
|
+
};
|
|
1212
|
+
const pruneObservations = (nowMs) => {
|
|
1213
|
+
observations = observations.filter((entry) => nowMs - entry.eventTs <= MATRIX_POST_HEALTHY_SYNC_DECRYPT_FAILURE_WINDOW_MS);
|
|
1214
|
+
if (observations.length === 0) warningEmitted = false;
|
|
1215
|
+
};
|
|
1216
|
+
return { recordFailure(roomId, event, error) {
|
|
1217
|
+
const nowMs = Date.now();
|
|
1218
|
+
const healthySyncSinceMs = params.getHealthySyncSinceMs?.();
|
|
1219
|
+
if (healthySyncSinceMs !== trackedHealthySyncSinceMs) {
|
|
1220
|
+
trackedHealthySyncSinceMs = healthySyncSinceMs;
|
|
1221
|
+
resetObservations();
|
|
1222
|
+
}
|
|
1223
|
+
if (!isFreshPostHealthySyncDecryptFailure({
|
|
1224
|
+
event,
|
|
1225
|
+
healthySyncSinceMs,
|
|
1226
|
+
graceMs: params.startupGraceMs,
|
|
1227
|
+
nowMs
|
|
1228
|
+
})) return {
|
|
1229
|
+
freshAfterHealthySync: false,
|
|
1230
|
+
failureCount: 0
|
|
1231
|
+
};
|
|
1232
|
+
pruneObservations(nowMs);
|
|
1233
|
+
const key = `${roomId}|${event.event_id}`;
|
|
1234
|
+
if (!observations.some((entry) => entry.key === key)) observations.push({
|
|
1235
|
+
key,
|
|
1236
|
+
roomId,
|
|
1237
|
+
eventId: event.event_id,
|
|
1238
|
+
sender: typeof event.sender === "string" ? event.sender : null,
|
|
1239
|
+
eventTs: event.origin_server_ts,
|
|
1240
|
+
error: error.message
|
|
1241
|
+
});
|
|
1242
|
+
const failureCount = observations.length;
|
|
1243
|
+
if (warningEmitted || failureCount < MATRIX_POST_HEALTHY_SYNC_DECRYPT_FAILURE_THRESHOLD) return {
|
|
1244
|
+
freshAfterHealthySync: true,
|
|
1245
|
+
failureCount
|
|
1246
|
+
};
|
|
1247
|
+
warningEmitted = true;
|
|
1248
|
+
const rooms = [...new Set(observations.map((entry) => entry.roomId))].slice(0, MATRIX_POST_HEALTHY_SYNC_DECRYPT_FAILURE_SAMPLE_LIMIT);
|
|
1249
|
+
const senders = [...new Set(observations.map((entry) => entry.sender).filter(Boolean))].slice(0, MATRIX_POST_HEALTHY_SYNC_DECRYPT_FAILURE_SAMPLE_LIMIT);
|
|
1250
|
+
const eventIds = observations.slice(-3).map((entry) => entry.eventId);
|
|
1251
|
+
const latestError = observations.at(-1)?.error ?? error.message;
|
|
1252
|
+
return {
|
|
1253
|
+
freshAfterHealthySync: true,
|
|
1254
|
+
failureCount,
|
|
1255
|
+
warning: {
|
|
1256
|
+
rooms,
|
|
1257
|
+
roomCount: new Set(observations.map((entry) => entry.roomId)).size,
|
|
1258
|
+
senders,
|
|
1259
|
+
senderCount: new Set(observations.map((entry) => entry.sender).filter(Boolean)).size,
|
|
1260
|
+
eventIds,
|
|
1261
|
+
latestError,
|
|
1262
|
+
windowMs: MATRIX_POST_HEALTHY_SYNC_DECRYPT_FAILURE_WINDOW_MS
|
|
1263
|
+
}
|
|
1264
|
+
};
|
|
1265
|
+
} };
|
|
1266
|
+
}
|
|
1267
|
+
function formatMatrixSelfDecryptionHint(accountId) {
|
|
1268
|
+
return `matrix: failed to decrypt a message from this same Matrix user. This usually means another Matrix device did not share the room key, or another DaoCore runtime is using the same account. Check 'daocore matrix verify status --verbose --account ${accountId}' and 'daocore matrix devices list --account ${accountId}'.`;
|
|
1269
|
+
}
|
|
1270
|
+
async function resolveMatrixSelfUserId(client, logVerboseMessage) {
|
|
1271
|
+
if (typeof client.getUserId !== "function") return null;
|
|
1272
|
+
try {
|
|
1273
|
+
return await client.getUserId() ?? null;
|
|
1274
|
+
} catch (err) {
|
|
1275
|
+
logVerboseMessage(`matrix: failed resolving self user id for decrypt warning: ${String(err)}`);
|
|
1276
|
+
return null;
|
|
1277
|
+
}
|
|
1278
|
+
}
|
|
1279
|
+
function registerMatrixMonitorEvents(params) {
|
|
1280
|
+
const { cfg, client, auth, allowFrom, dmEnabled, dmPolicy, readStoreAllowFrom, directTracker, logVerboseMessage, warnedEncryptedRooms, warnedCryptoMissingRooms, logger, startupGraceMs, getHealthySyncSinceMs, formatNativeDependencyHint, onRoomMessage, runDetachedTask, sasNoticeRetryDelayMs } = params;
|
|
1281
|
+
const postHealthySyncDecryptFailureTracker = createMatrixPostHealthySyncDecryptFailureTracker({
|
|
1282
|
+
getHealthySyncSinceMs,
|
|
1283
|
+
startupGraceMs
|
|
1284
|
+
});
|
|
1285
|
+
const { routeVerificationEvent, routeVerificationSummary } = createMatrixVerificationEventRouter({
|
|
1286
|
+
client,
|
|
1287
|
+
allowFrom,
|
|
1288
|
+
dmEnabled,
|
|
1289
|
+
dmPolicy,
|
|
1290
|
+
readStoreAllowFrom,
|
|
1291
|
+
logVerboseMessage,
|
|
1292
|
+
runDetachedTask,
|
|
1293
|
+
sasNoticeRetryDelayMs
|
|
1294
|
+
});
|
|
1295
|
+
const runMonitorTask = (label, task) => {
|
|
1296
|
+
if (runDetachedTask) return runDetachedTask(label, task);
|
|
1297
|
+
return Promise.resolve().then(task).catch((error) => {
|
|
1298
|
+
logVerboseMessage(`matrix: ${label} failed (${String(error)})`);
|
|
1299
|
+
});
|
|
1300
|
+
};
|
|
1301
|
+
client.on("room.message", (roomId, event) => {
|
|
1302
|
+
if (routeVerificationEvent(roomId, event)) return;
|
|
1303
|
+
runMonitorTask(`room message handler room=${roomId} id=${event.event_id ?? "unknown"}`, async () => {
|
|
1304
|
+
await onRoomMessage(roomId, event);
|
|
1305
|
+
});
|
|
1306
|
+
});
|
|
1307
|
+
client.on("room.encrypted_event", (roomId, event) => {
|
|
1308
|
+
const eventId = event?.event_id ?? "unknown";
|
|
1309
|
+
logVerboseMessage(`matrix: encrypted event room=${roomId} type=${event?.type ?? "unknown"} id=${eventId}`);
|
|
1310
|
+
});
|
|
1311
|
+
client.on("room.decrypted_event", (roomId, event) => {
|
|
1312
|
+
const eventId = event?.event_id ?? "unknown";
|
|
1313
|
+
const eventType = event?.type ?? "unknown";
|
|
1314
|
+
logVerboseMessage(`matrix: decrypted event room=${roomId} type=${eventType} id=${eventId}`);
|
|
1315
|
+
if (routeVerificationEvent(roomId, event)) return;
|
|
1316
|
+
if (eventType !== EventType.RoomMessage) return;
|
|
1317
|
+
runMonitorTask(`decrypted room message handler room=${roomId} id=${event.event_id ?? "unknown"}`, async () => {
|
|
1318
|
+
await onRoomMessage(roomId, event);
|
|
1319
|
+
});
|
|
1320
|
+
});
|
|
1321
|
+
client.on("room.failed_decryption", async (roomId, event, error) => {
|
|
1322
|
+
const failureState = postHealthySyncDecryptFailureTracker.recordFailure(roomId, event, error);
|
|
1323
|
+
const selfUserId = await resolveMatrixSelfUserId(client, logVerboseMessage);
|
|
1324
|
+
const sender = typeof event.sender === "string" ? event.sender : null;
|
|
1325
|
+
const senderMatchesOwnUser = Boolean(selfUserId && sender && selfUserId === sender);
|
|
1326
|
+
logger.warn(failureState.freshAfterHealthySync ? "Failed to decrypt fresh post-healthy-sync message" : "Failed to decrypt message", {
|
|
1327
|
+
roomId,
|
|
1328
|
+
eventId: event.event_id,
|
|
1329
|
+
sender,
|
|
1330
|
+
senderMatchesOwnUser,
|
|
1331
|
+
error: error.message,
|
|
1332
|
+
freshAfterHealthySync: failureState.freshAfterHealthySync,
|
|
1333
|
+
...failureState.freshAfterHealthySync ? { postHealthySyncFailureCount: failureState.failureCount } : {}
|
|
1334
|
+
});
|
|
1335
|
+
if (failureState.warning) logger.warn(formatMatrixPostHealthySyncDecryptionHint(auth.accountId), {
|
|
1336
|
+
roomId,
|
|
1337
|
+
eventId: event.event_id,
|
|
1338
|
+
failureCount: failureState.failureCount,
|
|
1339
|
+
roomCount: failureState.warning.roomCount,
|
|
1340
|
+
rooms: failureState.warning.rooms,
|
|
1341
|
+
senderCount: failureState.warning.senderCount,
|
|
1342
|
+
senders: failureState.warning.senders,
|
|
1343
|
+
sampleEventIds: failureState.warning.eventIds,
|
|
1344
|
+
latestError: failureState.warning.latestError,
|
|
1345
|
+
windowMs: failureState.warning.windowMs
|
|
1346
|
+
});
|
|
1347
|
+
if (senderMatchesOwnUser) logger.warn(formatMatrixSelfDecryptionHint(auth.accountId), {
|
|
1348
|
+
roomId,
|
|
1349
|
+
eventId: event.event_id,
|
|
1350
|
+
sender
|
|
1351
|
+
});
|
|
1352
|
+
logVerboseMessage(`matrix: failed decrypt room=${roomId} id=${event.event_id ?? "unknown"} freshAfterHealthySync=${String(failureState.freshAfterHealthySync)} error=${error.message}`);
|
|
1353
|
+
});
|
|
1354
|
+
client.on("verification.summary", (summary) => {
|
|
1355
|
+
runMonitorTask("verification summary handler", async () => {
|
|
1356
|
+
await routeVerificationSummary(summary);
|
|
1357
|
+
});
|
|
1358
|
+
});
|
|
1359
|
+
client.on("room.invite", (roomId, event) => {
|
|
1360
|
+
directTracker?.invalidateRoom(roomId);
|
|
1361
|
+
const eventId = event?.event_id ?? "unknown";
|
|
1362
|
+
const sender = event?.sender ?? "unknown";
|
|
1363
|
+
const invitee = normalizeOptionalString(event?.state_key) ?? "";
|
|
1364
|
+
const senderIsInvitee = Boolean(invitee) && (normalizeOptionalString(event?.sender) ?? "") === invitee;
|
|
1365
|
+
const isDirect = (event?.content)?.is_direct === true;
|
|
1366
|
+
const rememberedSender = normalizeOptionalString(event?.sender);
|
|
1367
|
+
if (rememberedSender && !senderIsInvitee) directTracker?.rememberInvite?.(roomId, rememberedSender);
|
|
1368
|
+
logVerboseMessage(`matrix: invite room=${roomId} sender=${sender} direct=${String(isDirect)} id=${eventId}`);
|
|
1369
|
+
});
|
|
1370
|
+
client.on("room.join", (roomId, event) => {
|
|
1371
|
+
directTracker?.invalidateRoom(roomId);
|
|
1372
|
+
logVerboseMessage(`matrix: join room=${roomId} id=${event?.event_id ?? "unknown"}`);
|
|
1373
|
+
});
|
|
1374
|
+
client.on("room.event", (roomId, event) => {
|
|
1375
|
+
const eventType = event?.type ?? "unknown";
|
|
1376
|
+
if (eventType === EventType.RoomMessageEncrypted) {
|
|
1377
|
+
logVerboseMessage(`matrix: encrypted raw event room=${roomId} id=${event?.event_id ?? "unknown"}`);
|
|
1378
|
+
if (auth.encryption !== true && !warnedEncryptedRooms.has(roomId)) {
|
|
1379
|
+
warnedEncryptedRooms.add(roomId);
|
|
1380
|
+
const warning = formatMatrixEncryptedEventDisabledWarning(cfg, auth.accountId);
|
|
1381
|
+
logger.warn(warning, { roomId });
|
|
1382
|
+
}
|
|
1383
|
+
if (auth.encryption === true && !client.crypto && !warnedCryptoMissingRooms.has(roomId)) {
|
|
1384
|
+
warnedCryptoMissingRooms.add(roomId);
|
|
1385
|
+
const warning = `matrix: encryption enabled but crypto is unavailable; ${formatNativeDependencyHint({
|
|
1386
|
+
packageName: "@matrix-org/matrix-sdk-crypto-nodejs",
|
|
1387
|
+
manager: "pnpm",
|
|
1388
|
+
downloadCommand: "node node_modules/@matrix-org/matrix-sdk-crypto-nodejs/download-lib.js"
|
|
1389
|
+
})}`;
|
|
1390
|
+
logger.warn(warning, { roomId });
|
|
1391
|
+
}
|
|
1392
|
+
return;
|
|
1393
|
+
}
|
|
1394
|
+
if (eventType === EventType.RoomMember) {
|
|
1395
|
+
directTracker?.invalidateRoom(roomId);
|
|
1396
|
+
const membership = (event?.content)?.membership;
|
|
1397
|
+
logVerboseMessage(`matrix: member event room=${roomId} stateKey=${event.state_key ?? ""} membership=${membership ?? "unknown"}`);
|
|
1398
|
+
}
|
|
1399
|
+
if (eventType === EventType.Reaction) {
|
|
1400
|
+
runMonitorTask(`reaction handler room=${roomId} id=${event.event_id ?? "unknown"}`, async () => {
|
|
1401
|
+
await onRoomMessage(roomId, event);
|
|
1402
|
+
});
|
|
1403
|
+
return;
|
|
1404
|
+
}
|
|
1405
|
+
routeVerificationEvent(roomId, event);
|
|
1406
|
+
});
|
|
1407
|
+
}
|
|
1408
|
+
//#endregion
|
|
1409
|
+
//#region extensions/matrix/src/matrix/monitor/ack-config.ts
|
|
1410
|
+
function resolveMatrixAckReactionConfig(params) {
|
|
1411
|
+
const matrixConfig = params.cfg.channels?.matrix;
|
|
1412
|
+
const accountConfig = resolveMatrixAccountConfig({
|
|
1413
|
+
cfg: params.cfg,
|
|
1414
|
+
accountId: params.accountId
|
|
1415
|
+
});
|
|
1416
|
+
return {
|
|
1417
|
+
ackReaction: resolveAckReaction(params.cfg, params.agentId, {
|
|
1418
|
+
channel: "matrix",
|
|
1419
|
+
accountId: params.accountId ?? void 0
|
|
1420
|
+
}).trim(),
|
|
1421
|
+
ackReactionScope: accountConfig.ackReactionScope ?? matrixConfig?.ackReactionScope ?? params.cfg.messages?.ackReactionScope ?? "group-mentions"
|
|
1422
|
+
};
|
|
1423
|
+
}
|
|
1424
|
+
//#endregion
|
|
1425
|
+
//#region extensions/matrix/src/matrix/monitor/location.ts
|
|
1426
|
+
function decodeGeoUriParamValue(value) {
|
|
1427
|
+
try {
|
|
1428
|
+
return decodeURIComponent(value);
|
|
1429
|
+
} catch {
|
|
1430
|
+
return value;
|
|
1431
|
+
}
|
|
1432
|
+
}
|
|
1433
|
+
function parseGeoUri(value) {
|
|
1434
|
+
const trimmed = value.trim();
|
|
1435
|
+
if (!trimmed) return null;
|
|
1436
|
+
if (!normalizeLowercaseStringOrEmpty(trimmed).startsWith("geo:")) return null;
|
|
1437
|
+
const [coordsPart, ...paramParts] = trimmed.slice(4).split(";");
|
|
1438
|
+
const coords = coordsPart.split(",");
|
|
1439
|
+
if (coords.length < 2) return null;
|
|
1440
|
+
const latitude = Number.parseFloat(coords[0] ?? "");
|
|
1441
|
+
const longitude = Number.parseFloat(coords[1] ?? "");
|
|
1442
|
+
if (!Number.isFinite(latitude) || !Number.isFinite(longitude)) return null;
|
|
1443
|
+
const params = /* @__PURE__ */ new Map();
|
|
1444
|
+
for (const part of paramParts) {
|
|
1445
|
+
const segment = part.trim();
|
|
1446
|
+
if (!segment) continue;
|
|
1447
|
+
const eqIndex = segment.indexOf("=");
|
|
1448
|
+
const rawKey = eqIndex === -1 ? segment : segment.slice(0, eqIndex);
|
|
1449
|
+
const rawValue = eqIndex === -1 ? "" : segment.slice(eqIndex + 1);
|
|
1450
|
+
const key = normalizeLowercaseStringOrEmpty(rawKey);
|
|
1451
|
+
if (!key) continue;
|
|
1452
|
+
const valuePart = rawValue.trim();
|
|
1453
|
+
params.set(key, valuePart ? decodeGeoUriParamValue(valuePart) : "");
|
|
1454
|
+
}
|
|
1455
|
+
const accuracyRaw = params.get("u");
|
|
1456
|
+
const accuracy = accuracyRaw ? Number.parseFloat(accuracyRaw) : void 0;
|
|
1457
|
+
return {
|
|
1458
|
+
latitude,
|
|
1459
|
+
longitude,
|
|
1460
|
+
accuracy: Number.isFinite(accuracy) ? accuracy : void 0
|
|
1461
|
+
};
|
|
1462
|
+
}
|
|
1463
|
+
function resolveMatrixLocation(params) {
|
|
1464
|
+
const { eventType, content } = params;
|
|
1465
|
+
if (!(eventType === EventType.Location || eventType === EventType.RoomMessage && content.msgtype === EventType.Location)) return null;
|
|
1466
|
+
const geoUri = normalizeOptionalString(content.geo_uri) ?? "";
|
|
1467
|
+
if (!geoUri) return null;
|
|
1468
|
+
const parsed = parseGeoUri(geoUri);
|
|
1469
|
+
if (!parsed) return null;
|
|
1470
|
+
const caption = normalizeOptionalString(content.body) ?? "";
|
|
1471
|
+
const location = {
|
|
1472
|
+
latitude: parsed.latitude,
|
|
1473
|
+
longitude: parsed.longitude,
|
|
1474
|
+
accuracy: parsed.accuracy,
|
|
1475
|
+
caption: caption || void 0,
|
|
1476
|
+
source: "pin",
|
|
1477
|
+
isLive: false
|
|
1478
|
+
};
|
|
1479
|
+
return {
|
|
1480
|
+
text: formatLocationText(location),
|
|
1481
|
+
context: toLocationContext(location)
|
|
1482
|
+
};
|
|
1483
|
+
}
|
|
1484
|
+
//#endregion
|
|
1485
|
+
//#region extensions/matrix/src/matrix/monitor/media.ts
|
|
1486
|
+
const MATRIX_MEDIA_DOWNLOAD_IDLE_TIMEOUT_MS = 3e4;
|
|
1487
|
+
async function fetchMatrixMediaBuffer(params) {
|
|
1488
|
+
try {
|
|
1489
|
+
return { buffer: await params.client.downloadContent(params.mxcUrl, {
|
|
1490
|
+
maxBytes: params.maxBytes,
|
|
1491
|
+
readIdleTimeoutMs: MATRIX_MEDIA_DOWNLOAD_IDLE_TIMEOUT_MS
|
|
1492
|
+
}) };
|
|
1493
|
+
} catch (err) {
|
|
1494
|
+
if (isMatrixMediaSizeLimitError(err)) throw err;
|
|
1495
|
+
throw new Error(`Matrix media download failed: ${String(err)}`, { cause: err });
|
|
1496
|
+
}
|
|
1497
|
+
}
|
|
1498
|
+
/**
|
|
1499
|
+
* Download and decrypt encrypted media from a Matrix room.
|
|
1500
|
+
* Uses the Matrix crypto adapter's decryptMedia helper.
|
|
1501
|
+
*/
|
|
1502
|
+
async function fetchEncryptedMediaBuffer(params) {
|
|
1503
|
+
if (!params.client.crypto) throw new Error("Cannot decrypt media: crypto not enabled");
|
|
1504
|
+
const decrypted = await params.client.crypto.decryptMedia(params.file, {
|
|
1505
|
+
maxBytes: params.maxBytes,
|
|
1506
|
+
readIdleTimeoutMs: MATRIX_MEDIA_DOWNLOAD_IDLE_TIMEOUT_MS
|
|
1507
|
+
});
|
|
1508
|
+
if (decrypted.byteLength > params.maxBytes) throw new MatrixMediaSizeLimitError();
|
|
1509
|
+
return { buffer: decrypted };
|
|
1510
|
+
}
|
|
1511
|
+
async function downloadMatrixMedia(params) {
|
|
1512
|
+
let fetched;
|
|
1513
|
+
if (typeof params.sizeBytes === "number" && params.sizeBytes > params.maxBytes) throw new MatrixMediaSizeLimitError();
|
|
1514
|
+
if (params.file) fetched = await fetchEncryptedMediaBuffer({
|
|
1515
|
+
client: params.client,
|
|
1516
|
+
file: params.file,
|
|
1517
|
+
maxBytes: params.maxBytes
|
|
1518
|
+
});
|
|
1519
|
+
else fetched = await fetchMatrixMediaBuffer({
|
|
1520
|
+
client: params.client,
|
|
1521
|
+
mxcUrl: params.mxcUrl,
|
|
1522
|
+
maxBytes: params.maxBytes
|
|
1523
|
+
});
|
|
1524
|
+
if (!fetched) return null;
|
|
1525
|
+
const headerType = params.contentType ?? void 0;
|
|
1526
|
+
const saved = await getMatrixRuntime().channel.media.saveMediaBuffer(fetched.buffer, headerType, "inbound", params.maxBytes, params.originalFilename);
|
|
1527
|
+
return {
|
|
1528
|
+
path: saved.path,
|
|
1529
|
+
contentType: saved.contentType,
|
|
1530
|
+
placeholder: "[matrix media]"
|
|
1531
|
+
};
|
|
1532
|
+
}
|
|
1533
|
+
//#endregion
|
|
1534
|
+
//#region extensions/matrix/src/matrix/monitor/mentions.ts
|
|
1535
|
+
const HTML_ENTITY_REPLACEMENTS = {
|
|
1536
|
+
amp: "&",
|
|
1537
|
+
apos: "'",
|
|
1538
|
+
gt: ">",
|
|
1539
|
+
lt: "<",
|
|
1540
|
+
nbsp: " ",
|
|
1541
|
+
quot: "\""
|
|
1542
|
+
};
|
|
1543
|
+
const MAX_UNICODE_SCALAR_VALUE = 1114111;
|
|
1544
|
+
function decodeNumericHtmlEntity(match, rawValue, radix) {
|
|
1545
|
+
const codePoint = Number.parseInt(rawValue, radix);
|
|
1546
|
+
if (!Number.isSafeInteger(codePoint) || codePoint < 0 || codePoint > MAX_UNICODE_SCALAR_VALUE || codePoint >= 55296 && codePoint <= 57343) return match;
|
|
1547
|
+
return String.fromCodePoint(codePoint);
|
|
1548
|
+
}
|
|
1549
|
+
function decodeHtmlEntities(value) {
|
|
1550
|
+
return value.replace(/&(#x?[0-9a-f]+|\w+);/gi, (match, entity) => {
|
|
1551
|
+
const normalized = normalizeLowercaseStringOrEmpty(entity);
|
|
1552
|
+
if (normalized.startsWith("#x")) return decodeNumericHtmlEntity(match, normalized.slice(2), 16);
|
|
1553
|
+
if (normalized.startsWith("#")) return decodeNumericHtmlEntity(match, normalized.slice(1), 10);
|
|
1554
|
+
return HTML_ENTITY_REPLACEMENTS[normalized] ?? match;
|
|
1555
|
+
});
|
|
1556
|
+
}
|
|
1557
|
+
function normalizeVisibleMentionText(value) {
|
|
1558
|
+
return normalizeLowercaseStringOrEmpty(decodeHtmlEntities(value.replace(/<[^>]+>/g, " ").replace(/[\u200b-\u200f\u202a-\u202e\u2060-\u206f]/g, "")).replace(/\s+/g, " "));
|
|
1559
|
+
}
|
|
1560
|
+
function extractVisibleMentionText(value) {
|
|
1561
|
+
return normalizeVisibleMentionText(value ?? "");
|
|
1562
|
+
}
|
|
1563
|
+
function resolveMatrixUserLocalpart(userId) {
|
|
1564
|
+
const trimmed = userId.trim();
|
|
1565
|
+
if (!trimmed.startsWith("@")) return null;
|
|
1566
|
+
const colonIndex = trimmed.indexOf(":");
|
|
1567
|
+
if (colonIndex <= 1) return null;
|
|
1568
|
+
return trimmed.slice(1, colonIndex).trim() || null;
|
|
1569
|
+
}
|
|
1570
|
+
function resolveMatrixMentionPrefixCandidates(params) {
|
|
1571
|
+
const candidates = [];
|
|
1572
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1573
|
+
const append = (candidate) => {
|
|
1574
|
+
const trimmed = candidate?.trim();
|
|
1575
|
+
if (!trimmed) return;
|
|
1576
|
+
const normalized = normalizeLowercaseStringOrEmpty(trimmed);
|
|
1577
|
+
if (seen.has(normalized)) return;
|
|
1578
|
+
seen.add(normalized);
|
|
1579
|
+
candidates.push(trimmed);
|
|
1580
|
+
};
|
|
1581
|
+
append(params.userId);
|
|
1582
|
+
const localpart = params.userId ? resolveMatrixUserLocalpart(params.userId) : null;
|
|
1583
|
+
append(localpart ? `@${localpart}` : null);
|
|
1584
|
+
append(params.displayName);
|
|
1585
|
+
append(params.displayName ? `@${params.displayName}` : null);
|
|
1586
|
+
return candidates;
|
|
1587
|
+
}
|
|
1588
|
+
function stripMatchedMatrixMentionPrefix(text, pattern) {
|
|
1589
|
+
const match = text.match(pattern);
|
|
1590
|
+
if (!match) return null;
|
|
1591
|
+
return text.slice(match[0].length).trimStart();
|
|
1592
|
+
}
|
|
1593
|
+
function stripNativeMatrixMentionPrefix(text, candidate) {
|
|
1594
|
+
return stripMatchedMatrixMentionPrefix(text, new RegExp(`^\\s*${escapeRegExp(candidate)}(?:\\s*[:,])?(?:\\s+|$)`, "i"));
|
|
1595
|
+
}
|
|
1596
|
+
function stripRegexMatrixMentionPrefix(text, pattern) {
|
|
1597
|
+
const flags = pattern.flags.replace(/[gy]/g, "");
|
|
1598
|
+
return stripMatchedMatrixMentionPrefix(text, new RegExp(`^\\s*(?:${pattern.source})(?:\\s*[:,])?(?:\\s+|$)`, flags));
|
|
1599
|
+
}
|
|
1600
|
+
function stripMatrixMentionPrefix(params) {
|
|
1601
|
+
const text = params.text;
|
|
1602
|
+
if (!text) return text;
|
|
1603
|
+
for (const candidate of resolveMatrixMentionPrefixCandidates(params)) {
|
|
1604
|
+
const stripped = stripNativeMatrixMentionPrefix(text, candidate);
|
|
1605
|
+
if (stripped !== null) return stripped;
|
|
1606
|
+
}
|
|
1607
|
+
for (const pattern of params.mentionRegexes ?? []) {
|
|
1608
|
+
const stripped = stripRegexMatrixMentionPrefix(text, pattern);
|
|
1609
|
+
if (stripped !== null) return stripped;
|
|
1610
|
+
}
|
|
1611
|
+
return text;
|
|
1612
|
+
}
|
|
1613
|
+
function isVisibleMentionLabel(params) {
|
|
1614
|
+
const cleaned = extractVisibleMentionText(params.text);
|
|
1615
|
+
if (!cleaned) return false;
|
|
1616
|
+
if (params.mentionRegexes.some((pattern) => pattern.test(cleaned))) return true;
|
|
1617
|
+
const localpart = resolveMatrixUserLocalpart(params.userId);
|
|
1618
|
+
return [
|
|
1619
|
+
extractVisibleMentionText(params.userId),
|
|
1620
|
+
localpart ? extractVisibleMentionText(localpart) : null,
|
|
1621
|
+
localpart ? extractVisibleMentionText(`@${localpart}`) : null,
|
|
1622
|
+
params.displayName ? extractVisibleMentionText(params.displayName) : null,
|
|
1623
|
+
params.displayName ? extractVisibleMentionText(`@${params.displayName}`) : null
|
|
1624
|
+
].filter((value) => Boolean(value)).includes(cleaned);
|
|
1625
|
+
}
|
|
1626
|
+
function hasVisibleRoomMention(value) {
|
|
1627
|
+
const cleaned = extractVisibleMentionText(value);
|
|
1628
|
+
return /(^|[^a-z0-9_])@room\b/i.test(cleaned);
|
|
1629
|
+
}
|
|
1630
|
+
/**
|
|
1631
|
+
* Check if formatted_body contains a matrix.to link whose visible label still
|
|
1632
|
+
* looks like a real mention for the given user. Do not trust href alone, since
|
|
1633
|
+
* senders can hide arbitrary matrix.to links behind unrelated link text.
|
|
1634
|
+
* Many Matrix clients (including Element) use HTML links in formatted_body instead of
|
|
1635
|
+
* or in addition to the m.mentions field.
|
|
1636
|
+
*/
|
|
1637
|
+
function checkFormattedBodyMention(params) {
|
|
1638
|
+
if (!params.formattedBody || !params.userId) return false;
|
|
1639
|
+
for (const match of params.formattedBody.matchAll(/<a\b[^>]*href=(["'])(https:\/\/matrix\.to\/#[^"']+)\1[^>]*>(.*?)<\/a>/gis)) {
|
|
1640
|
+
const href = match[2];
|
|
1641
|
+
const visibleLabel = match[3] ?? "";
|
|
1642
|
+
if (!href) continue;
|
|
1643
|
+
try {
|
|
1644
|
+
const parsed = new URL(href);
|
|
1645
|
+
if (decodeURIComponent(parsed.hash.replace(/^#\/?/, "").trim()) !== params.userId.trim()) continue;
|
|
1646
|
+
if (isVisibleMentionLabel({
|
|
1647
|
+
text: visibleLabel,
|
|
1648
|
+
userId: params.userId,
|
|
1649
|
+
mentionRegexes: params.mentionRegexes,
|
|
1650
|
+
displayName: params.displayName
|
|
1651
|
+
})) return true;
|
|
1652
|
+
} catch {
|
|
1653
|
+
continue;
|
|
1654
|
+
}
|
|
1655
|
+
}
|
|
1656
|
+
return false;
|
|
1657
|
+
}
|
|
1658
|
+
function resolveMentions(params) {
|
|
1659
|
+
const mentions = params.content["m.mentions"];
|
|
1660
|
+
const mentionedUsers = Array.isArray(mentions?.user_ids) ? new Set(mentions.user_ids) : /* @__PURE__ */ new Set();
|
|
1661
|
+
const textMentioned = getMatrixRuntime().channel.mentions.matchesMentionPatterns(params.text ?? "", params.mentionRegexes);
|
|
1662
|
+
const visibleRoomMention = hasVisibleRoomMention(params.text) || hasVisibleRoomMention(params.content.formatted_body);
|
|
1663
|
+
const mentionedInFormattedBody = params.userId ? checkFormattedBodyMention({
|
|
1664
|
+
formattedBody: params.content.formatted_body,
|
|
1665
|
+
userId: params.userId,
|
|
1666
|
+
displayName: params.displayName,
|
|
1667
|
+
mentionRegexes: params.mentionRegexes
|
|
1668
|
+
}) : false;
|
|
1669
|
+
const metadataBackedUserMention = Boolean(params.userId && mentionedUsers.has(params.userId) && (mentionedInFormattedBody || textMentioned));
|
|
1670
|
+
const metadataBackedRoomMention = Boolean(mentions?.room) && visibleRoomMention;
|
|
1671
|
+
const explicitMention = mentionedInFormattedBody || metadataBackedUserMention || metadataBackedRoomMention;
|
|
1672
|
+
return {
|
|
1673
|
+
wasMentioned: explicitMention || textMentioned || visibleRoomMention,
|
|
1674
|
+
hasExplicitMention: explicitMention
|
|
1675
|
+
};
|
|
1676
|
+
}
|
|
1677
|
+
//#endregion
|
|
1678
|
+
//#region extensions/matrix/src/matrix/monitor/replies.ts
|
|
1679
|
+
const THINKING_TAG_RE = /<\s*\/?\s*(?:think(?:ing)?|thought|antthinking)\b[^<>]*>/gi;
|
|
1680
|
+
const THINKING_BLOCK_RE = /<\s*(?:think(?:ing)?|thought|antthinking)\b[^<>]*>[\s\S]*?<\s*\/\s*(?:think(?:ing)?|thought|antthinking)\s*>/gi;
|
|
1681
|
+
function shouldSuppressReasoningReplyText(text) {
|
|
1682
|
+
if (typeof text !== "string") return false;
|
|
1683
|
+
const trimmedStart = text.trimStart();
|
|
1684
|
+
if (!trimmedStart) return false;
|
|
1685
|
+
if (normalizeLowercaseStringOrEmpty(trimmedStart).startsWith("reasoning:")) return true;
|
|
1686
|
+
THINKING_TAG_RE.lastIndex = 0;
|
|
1687
|
+
if (!THINKING_TAG_RE.test(text)) return false;
|
|
1688
|
+
THINKING_BLOCK_RE.lastIndex = 0;
|
|
1689
|
+
const withoutThinkingBlocks = text.replace(THINKING_BLOCK_RE, "");
|
|
1690
|
+
THINKING_TAG_RE.lastIndex = 0;
|
|
1691
|
+
return !withoutThinkingBlocks.replace(THINKING_TAG_RE, "").trim();
|
|
1692
|
+
}
|
|
1693
|
+
async function deliverMatrixReplies(params) {
|
|
1694
|
+
const core = getMatrixRuntime();
|
|
1695
|
+
const tableMode = params.tableMode ?? core.channel.text.resolveMarkdownTableMode({
|
|
1696
|
+
cfg: params.cfg,
|
|
1697
|
+
channel: "matrix",
|
|
1698
|
+
accountId: params.accountId
|
|
1699
|
+
});
|
|
1700
|
+
const logVerbose = (message) => {
|
|
1701
|
+
if (core.logging.shouldLogVerbose()) params.runtime.log?.(message);
|
|
1702
|
+
};
|
|
1703
|
+
let hasReplied = false;
|
|
1704
|
+
let deliveredAny = false;
|
|
1705
|
+
for (const reply of params.replies) {
|
|
1706
|
+
if (reply.isReasoning === true || shouldSuppressReasoningReplyText(reply.text)) {
|
|
1707
|
+
logVerbose("matrix reply suppressed as reasoning-only");
|
|
1708
|
+
continue;
|
|
1709
|
+
}
|
|
1710
|
+
const hasMedia = Boolean(reply?.mediaUrl) || (reply?.mediaUrls?.length ?? 0) > 0;
|
|
1711
|
+
if (!reply?.text && !hasMedia) {
|
|
1712
|
+
if (reply?.audioAsVoice) {
|
|
1713
|
+
logVerbose("matrix reply has audioAsVoice without media/text; skipping");
|
|
1714
|
+
continue;
|
|
1715
|
+
}
|
|
1716
|
+
params.runtime.error?.("matrix reply missing text/media");
|
|
1717
|
+
continue;
|
|
1718
|
+
}
|
|
1719
|
+
const replyToIdRaw = reply.replyToId?.trim();
|
|
1720
|
+
const replyToId = params.threadId || params.replyToMode === "off" ? void 0 : replyToIdRaw;
|
|
1721
|
+
const rawText = reply.text ?? "";
|
|
1722
|
+
const mediaList = reply.mediaUrls?.length ? reply.mediaUrls : reply.mediaUrl ? [reply.mediaUrl] : [];
|
|
1723
|
+
const shouldIncludeReply = (id) => Boolean(id) && (params.replyToMode === "all" || !hasReplied);
|
|
1724
|
+
const replyToIdForReply = shouldIncludeReply(replyToId) ? replyToId : void 0;
|
|
1725
|
+
if (mediaList.length === 0) {
|
|
1726
|
+
let sentTextChunk = false;
|
|
1727
|
+
const { chunks } = chunkMatrixText(rawText, {
|
|
1728
|
+
cfg: params.cfg,
|
|
1729
|
+
accountId: params.accountId,
|
|
1730
|
+
tableMode
|
|
1731
|
+
});
|
|
1732
|
+
for (const chunk of chunks) {
|
|
1733
|
+
const trimmed = chunk.trim();
|
|
1734
|
+
if (!trimmed) continue;
|
|
1735
|
+
await sendMessageMatrix(params.roomId, trimmed, {
|
|
1736
|
+
client: params.client,
|
|
1737
|
+
cfg: params.cfg,
|
|
1738
|
+
replyToId: replyToIdForReply,
|
|
1739
|
+
threadId: params.threadId,
|
|
1740
|
+
accountId: params.accountId
|
|
1741
|
+
});
|
|
1742
|
+
deliveredAny = true;
|
|
1743
|
+
sentTextChunk = true;
|
|
1744
|
+
}
|
|
1745
|
+
if (replyToIdForReply && !hasReplied && sentTextChunk) hasReplied = true;
|
|
1746
|
+
continue;
|
|
1747
|
+
}
|
|
1748
|
+
let first = true;
|
|
1749
|
+
for (const mediaUrl of mediaList) {
|
|
1750
|
+
const caption = first ? rawText : "";
|
|
1751
|
+
await sendMessageMatrix(params.roomId, caption, {
|
|
1752
|
+
client: params.client,
|
|
1753
|
+
cfg: params.cfg,
|
|
1754
|
+
mediaUrl,
|
|
1755
|
+
mediaLocalRoots: params.mediaLocalRoots,
|
|
1756
|
+
replyToId: replyToIdForReply,
|
|
1757
|
+
threadId: params.threadId,
|
|
1758
|
+
audioAsVoice: reply.audioAsVoice,
|
|
1759
|
+
accountId: params.accountId
|
|
1760
|
+
});
|
|
1761
|
+
deliveredAny = true;
|
|
1762
|
+
first = false;
|
|
1763
|
+
}
|
|
1764
|
+
if (replyToIdForReply && !hasReplied) hasReplied = true;
|
|
1765
|
+
}
|
|
1766
|
+
return deliveredAny;
|
|
1767
|
+
}
|
|
1768
|
+
//#endregion
|
|
1769
|
+
//#region extensions/matrix/src/matrix/monitor/context-summary.ts
|
|
1770
|
+
function trimMatrixMaybeString(value) {
|
|
1771
|
+
if (typeof value !== "string") return;
|
|
1772
|
+
return value.trim() || void 0;
|
|
1773
|
+
}
|
|
1774
|
+
function summarizeMatrixMessageContextEvent(event) {
|
|
1775
|
+
if (isPollStartType(event.type)) {
|
|
1776
|
+
const pollSummary = parsePollStartContent(event.content);
|
|
1777
|
+
if (pollSummary) return formatPollAsText(pollSummary);
|
|
1778
|
+
}
|
|
1779
|
+
const content = event.content;
|
|
1780
|
+
return formatMatrixMessageText({
|
|
1781
|
+
body: resolveMatrixMessageBody({
|
|
1782
|
+
body: trimMatrixMaybeString(content.body),
|
|
1783
|
+
filename: trimMatrixMaybeString(content.filename),
|
|
1784
|
+
msgtype: trimMatrixMaybeString(content.msgtype)
|
|
1785
|
+
}),
|
|
1786
|
+
attachment: resolveMatrixMessageAttachment({
|
|
1787
|
+
body: trimMatrixMaybeString(content.body),
|
|
1788
|
+
filename: trimMatrixMaybeString(content.filename),
|
|
1789
|
+
msgtype: trimMatrixMaybeString(content.msgtype)
|
|
1790
|
+
})
|
|
1791
|
+
});
|
|
1792
|
+
}
|
|
1793
|
+
//#endregion
|
|
1794
|
+
//#region extensions/matrix/src/matrix/monitor/reply-context.ts
|
|
1795
|
+
const MAX_CACHED_REPLY_CONTEXTS = 256;
|
|
1796
|
+
const MAX_REPLY_BODY_LENGTH = 500;
|
|
1797
|
+
function truncateReplyBody(value) {
|
|
1798
|
+
if (value.length <= MAX_REPLY_BODY_LENGTH) return value;
|
|
1799
|
+
return `${value.slice(0, MAX_REPLY_BODY_LENGTH - 3)}...`;
|
|
1800
|
+
}
|
|
1801
|
+
function summarizeMatrixReplyEvent(event) {
|
|
1802
|
+
const body = summarizeMatrixMessageContextEvent(event);
|
|
1803
|
+
return body ? truncateReplyBody(body) : void 0;
|
|
1804
|
+
}
|
|
1805
|
+
/**
|
|
1806
|
+
* Creates a cached resolver that fetches the body and sender of a replied-to
|
|
1807
|
+
* Matrix event. This allows the agent to see the content of the message being
|
|
1808
|
+
* replied to, not just its event ID.
|
|
1809
|
+
*/
|
|
1810
|
+
function createMatrixReplyContextResolver(params) {
|
|
1811
|
+
const cache = /* @__PURE__ */ new Map();
|
|
1812
|
+
const remember = (key, value) => {
|
|
1813
|
+
cache.set(key, value);
|
|
1814
|
+
if (cache.size > MAX_CACHED_REPLY_CONTEXTS) {
|
|
1815
|
+
const oldest = cache.keys().next().value;
|
|
1816
|
+
if (typeof oldest === "string") cache.delete(oldest);
|
|
1817
|
+
}
|
|
1818
|
+
return value;
|
|
1819
|
+
};
|
|
1820
|
+
return async (input) => {
|
|
1821
|
+
const cacheKey = `${input.roomId}:${input.eventId}`;
|
|
1822
|
+
const cached = cache.get(cacheKey);
|
|
1823
|
+
if (cached) {
|
|
1824
|
+
cache.delete(cacheKey);
|
|
1825
|
+
cache.set(cacheKey, cached);
|
|
1826
|
+
return cached;
|
|
1827
|
+
}
|
|
1828
|
+
const event = await params.client.getEvent(input.roomId, input.eventId).catch((err) => {
|
|
1829
|
+
params.logVerboseMessage(`matrix: failed resolving reply context room=${input.roomId} id=${input.eventId}: ${String(err)}`);
|
|
1830
|
+
return null;
|
|
1831
|
+
});
|
|
1832
|
+
if (!event) return {};
|
|
1833
|
+
const rawEvent = event;
|
|
1834
|
+
if (rawEvent.unsigned?.redacted_because) return remember(cacheKey, {});
|
|
1835
|
+
const replyToBody = summarizeMatrixReplyEvent(rawEvent);
|
|
1836
|
+
if (!replyToBody) return remember(cacheKey, {});
|
|
1837
|
+
const senderId = trimMatrixMaybeString(rawEvent.sender);
|
|
1838
|
+
return remember(cacheKey, {
|
|
1839
|
+
replyToBody,
|
|
1840
|
+
replyToSender: (senderId && await params.getMemberDisplayName(input.roomId, senderId).catch(() => void 0)) ?? senderId,
|
|
1841
|
+
replyToSenderId: senderId
|
|
1842
|
+
});
|
|
1843
|
+
};
|
|
1844
|
+
}
|
|
1845
|
+
//#endregion
|
|
1846
|
+
//#region extensions/matrix/src/matrix/monitor/room-history.ts
|
|
1847
|
+
/** Maximum entries retained per room (hard cap to bound memory). */
|
|
1848
|
+
const DEFAULT_MAX_QUEUE_SIZE = 200;
|
|
1849
|
+
/** Maximum number of rooms to retain queues for (FIFO eviction beyond this). */
|
|
1850
|
+
const DEFAULT_MAX_ROOM_QUEUES = 1e3;
|
|
1851
|
+
/** Maximum number of (agentId, roomId) watermark entries to retain. */
|
|
1852
|
+
const MAX_WATERMARK_ENTRIES = 5e3;
|
|
1853
|
+
/** Maximum prepared trigger snapshots retained per room for retry reuse. */
|
|
1854
|
+
const MAX_PREPARED_TRIGGER_ENTRIES = 500;
|
|
1855
|
+
function createRoomHistoryTrackerInternal(maxQueueSize = DEFAULT_MAX_QUEUE_SIZE, maxRoomQueues = DEFAULT_MAX_ROOM_QUEUES, maxWatermarkEntries = MAX_WATERMARK_ENTRIES, maxPreparedTriggerEntries = MAX_PREPARED_TRIGGER_ENTRIES) {
|
|
1856
|
+
const roomQueues = /* @__PURE__ */ new Map();
|
|
1857
|
+
/** Maps `${agentId}:${roomId}` → absolute consumed-up-to index */
|
|
1858
|
+
const agentWatermarks = /* @__PURE__ */ new Map();
|
|
1859
|
+
let nextQueueGeneration = 1;
|
|
1860
|
+
function clearRoomWatermarks(roomId) {
|
|
1861
|
+
const roomSuffix = `:${roomId}`;
|
|
1862
|
+
for (const key of agentWatermarks.keys()) if (key.endsWith(roomSuffix)) agentWatermarks.delete(key);
|
|
1863
|
+
}
|
|
1864
|
+
function getOrCreateQueue(roomId) {
|
|
1865
|
+
let queue = roomQueues.get(roomId);
|
|
1866
|
+
if (!queue) {
|
|
1867
|
+
queue = {
|
|
1868
|
+
entries: [],
|
|
1869
|
+
baseIndex: 0,
|
|
1870
|
+
generation: nextQueueGeneration++,
|
|
1871
|
+
preparedTriggers: /* @__PURE__ */ new Map()
|
|
1872
|
+
};
|
|
1873
|
+
roomQueues.set(roomId, queue);
|
|
1874
|
+
if (roomQueues.size > maxRoomQueues) {
|
|
1875
|
+
const oldest = roomQueues.keys().next().value;
|
|
1876
|
+
if (oldest !== void 0) {
|
|
1877
|
+
roomQueues.delete(oldest);
|
|
1878
|
+
clearRoomWatermarks(oldest);
|
|
1879
|
+
}
|
|
1880
|
+
}
|
|
1881
|
+
}
|
|
1882
|
+
return queue;
|
|
1883
|
+
}
|
|
1884
|
+
function appendToQueue(queue, entry) {
|
|
1885
|
+
queue.entries.push(entry);
|
|
1886
|
+
if (queue.entries.length > maxQueueSize) {
|
|
1887
|
+
const overflow = queue.entries.length - maxQueueSize;
|
|
1888
|
+
queue.entries.splice(0, overflow);
|
|
1889
|
+
queue.baseIndex += overflow;
|
|
1890
|
+
}
|
|
1891
|
+
return {
|
|
1892
|
+
snapshotIdx: queue.baseIndex + queue.entries.length,
|
|
1893
|
+
queueGeneration: queue.generation
|
|
1894
|
+
};
|
|
1895
|
+
}
|
|
1896
|
+
function wmKey(agentId, roomId) {
|
|
1897
|
+
return `${agentId}:${roomId}`;
|
|
1898
|
+
}
|
|
1899
|
+
function preparedTriggerKey(agentId, messageId) {
|
|
1900
|
+
if (!messageId?.trim()) return null;
|
|
1901
|
+
return `${agentId}:${messageId.trim()}`;
|
|
1902
|
+
}
|
|
1903
|
+
function rememberWatermark(key, snapshotIdx) {
|
|
1904
|
+
const nextSnapshotIdx = Math.max(agentWatermarks.get(key) ?? 0, snapshotIdx);
|
|
1905
|
+
if (agentWatermarks.has(key)) agentWatermarks.delete(key);
|
|
1906
|
+
agentWatermarks.set(key, nextSnapshotIdx);
|
|
1907
|
+
if (agentWatermarks.size > maxWatermarkEntries) {
|
|
1908
|
+
const oldest = agentWatermarks.keys().next().value;
|
|
1909
|
+
if (oldest !== void 0) agentWatermarks.delete(oldest);
|
|
1910
|
+
}
|
|
1911
|
+
}
|
|
1912
|
+
function rememberPreparedTrigger(queue, retryKey, prepared) {
|
|
1913
|
+
if (queue.preparedTriggers.has(retryKey)) queue.preparedTriggers.delete(retryKey);
|
|
1914
|
+
queue.preparedTriggers.set(retryKey, prepared);
|
|
1915
|
+
if (queue.preparedTriggers.size > maxPreparedTriggerEntries) {
|
|
1916
|
+
const oldest = queue.preparedTriggers.keys().next().value;
|
|
1917
|
+
if (oldest !== void 0) queue.preparedTriggers.delete(oldest);
|
|
1918
|
+
}
|
|
1919
|
+
return prepared;
|
|
1920
|
+
}
|
|
1921
|
+
function computePendingHistory(queue, agentId, roomId, limit) {
|
|
1922
|
+
if (limit <= 0 || queue.entries.length === 0) return [];
|
|
1923
|
+
const wm = agentWatermarks.get(wmKey(agentId, roomId)) ?? 0;
|
|
1924
|
+
const startRel = Math.max(wm, queue.baseIndex) - queue.baseIndex;
|
|
1925
|
+
const available = queue.entries.slice(startRel);
|
|
1926
|
+
return available.length > limit ? available.slice(-limit) : available;
|
|
1927
|
+
}
|
|
1928
|
+
return {
|
|
1929
|
+
recordPending(roomId, entry) {
|
|
1930
|
+
appendToQueue(getOrCreateQueue(roomId), entry);
|
|
1931
|
+
},
|
|
1932
|
+
getPendingHistory(agentId, roomId, limit) {
|
|
1933
|
+
const queue = roomQueues.get(roomId);
|
|
1934
|
+
if (!queue) return [];
|
|
1935
|
+
return computePendingHistory(queue, agentId, roomId, limit);
|
|
1936
|
+
},
|
|
1937
|
+
recordTrigger(roomId, entry) {
|
|
1938
|
+
return appendToQueue(getOrCreateQueue(roomId), entry);
|
|
1939
|
+
},
|
|
1940
|
+
prepareTrigger(agentId, roomId, limit, entry) {
|
|
1941
|
+
const queue = getOrCreateQueue(roomId);
|
|
1942
|
+
const retryKey = preparedTriggerKey(agentId, entry.messageId);
|
|
1943
|
+
if (retryKey) {
|
|
1944
|
+
const prepared = queue.preparedTriggers.get(retryKey);
|
|
1945
|
+
if (prepared) return rememberPreparedTrigger(queue, retryKey, prepared);
|
|
1946
|
+
}
|
|
1947
|
+
const prepared = {
|
|
1948
|
+
history: computePendingHistory(queue, agentId, roomId, limit),
|
|
1949
|
+
...appendToQueue(queue, entry)
|
|
1950
|
+
};
|
|
1951
|
+
if (retryKey) return rememberPreparedTrigger(queue, retryKey, prepared);
|
|
1952
|
+
return prepared;
|
|
1953
|
+
},
|
|
1954
|
+
consumeHistory(agentId, roomId, snapshot, messageId) {
|
|
1955
|
+
const key = wmKey(agentId, roomId);
|
|
1956
|
+
const queue = roomQueues.get(roomId);
|
|
1957
|
+
if (!queue) {
|
|
1958
|
+
agentWatermarks.delete(key);
|
|
1959
|
+
return;
|
|
1960
|
+
}
|
|
1961
|
+
if (queue.generation !== snapshot.queueGeneration) return;
|
|
1962
|
+
rememberWatermark(key, snapshot.snapshotIdx);
|
|
1963
|
+
const retryKey = preparedTriggerKey(agentId, messageId);
|
|
1964
|
+
if (queue && retryKey) queue.preparedTriggers.delete(retryKey);
|
|
1965
|
+
}
|
|
1966
|
+
};
|
|
1967
|
+
}
|
|
1968
|
+
function createRoomHistoryTracker(maxQueueSize = DEFAULT_MAX_QUEUE_SIZE, maxRoomQueues = DEFAULT_MAX_ROOM_QUEUES, maxWatermarkEntries = MAX_WATERMARK_ENTRIES, maxPreparedTriggerEntries = MAX_PREPARED_TRIGGER_ENTRIES) {
|
|
1969
|
+
const tracker = createRoomHistoryTrackerInternal(maxQueueSize, maxRoomQueues, maxWatermarkEntries, maxPreparedTriggerEntries);
|
|
1970
|
+
return {
|
|
1971
|
+
recordPending: tracker.recordPending,
|
|
1972
|
+
prepareTrigger: tracker.prepareTrigger,
|
|
1973
|
+
consumeHistory: tracker.consumeHistory
|
|
1974
|
+
};
|
|
1975
|
+
}
|
|
1976
|
+
//#endregion
|
|
1977
|
+
//#region extensions/matrix/src/matrix/monitor/thread-context.ts
|
|
1978
|
+
const MAX_TRACKED_THREAD_STARTERS = 256;
|
|
1979
|
+
const MAX_THREAD_STARTER_BODY_LENGTH = 500;
|
|
1980
|
+
function truncateThreadStarterBody(value) {
|
|
1981
|
+
if (value.length <= MAX_THREAD_STARTER_BODY_LENGTH) return value;
|
|
1982
|
+
return `${value.slice(0, MAX_THREAD_STARTER_BODY_LENGTH - 3)}...`;
|
|
1983
|
+
}
|
|
1984
|
+
function summarizeMatrixThreadStarterEvent(event) {
|
|
1985
|
+
const body = summarizeMatrixMessageContextEvent(event);
|
|
1986
|
+
if (body) return truncateThreadStarterBody(body);
|
|
1987
|
+
const content = event.content;
|
|
1988
|
+
const msgtype = trimMatrixMaybeString(content.msgtype);
|
|
1989
|
+
if (msgtype) return `Matrix ${msgtype} message`;
|
|
1990
|
+
const eventType = trimMatrixMaybeString(event.type);
|
|
1991
|
+
return eventType ? `Matrix ${eventType} event` : void 0;
|
|
1992
|
+
}
|
|
1993
|
+
function formatMatrixThreadStarterBody(params) {
|
|
1994
|
+
const senderLabel = params.senderName ?? params.senderId ?? "unknown sender";
|
|
1995
|
+
const lines = [`Matrix thread root ${params.threadRootId} from ${senderLabel}:`];
|
|
1996
|
+
if (params.summary) lines.push(params.summary);
|
|
1997
|
+
return lines.join("\n");
|
|
1998
|
+
}
|
|
1999
|
+
function createMatrixThreadContextResolver(params) {
|
|
2000
|
+
const cache = /* @__PURE__ */ new Map();
|
|
2001
|
+
const remember = (key, value) => {
|
|
2002
|
+
cache.set(key, value);
|
|
2003
|
+
if (cache.size > MAX_TRACKED_THREAD_STARTERS) {
|
|
2004
|
+
const oldest = cache.keys().next().value;
|
|
2005
|
+
if (typeof oldest === "string") cache.delete(oldest);
|
|
2006
|
+
}
|
|
2007
|
+
return value;
|
|
2008
|
+
};
|
|
2009
|
+
return async (input) => {
|
|
2010
|
+
const cacheKey = `${input.roomId}:${input.threadRootId}`;
|
|
2011
|
+
const cached = cache.get(cacheKey);
|
|
2012
|
+
if (cached) return cached;
|
|
2013
|
+
const rootEvent = await params.client.getEvent(input.roomId, input.threadRootId).catch((err) => {
|
|
2014
|
+
params.logVerboseMessage(`matrix: failed resolving thread root room=${input.roomId} id=${input.threadRootId}: ${String(err)}`);
|
|
2015
|
+
return null;
|
|
2016
|
+
});
|
|
2017
|
+
if (!rootEvent) return { threadStarterBody: `Matrix thread root ${input.threadRootId}` };
|
|
2018
|
+
const rawEvent = rootEvent;
|
|
2019
|
+
const senderId = trimMatrixMaybeString(rawEvent.sender);
|
|
2020
|
+
const senderName = senderId && await params.getMemberDisplayName(input.roomId, senderId).catch(() => void 0);
|
|
2021
|
+
const senderLabel = senderName ?? senderId;
|
|
2022
|
+
const summary = summarizeMatrixThreadStarterEvent(rawEvent);
|
|
2023
|
+
return remember(cacheKey, {
|
|
2024
|
+
threadStarterBody: formatMatrixThreadStarterBody({
|
|
2025
|
+
threadRootId: input.threadRootId,
|
|
2026
|
+
senderId,
|
|
2027
|
+
senderName,
|
|
2028
|
+
summary
|
|
2029
|
+
}),
|
|
2030
|
+
senderId,
|
|
2031
|
+
senderLabel,
|
|
2032
|
+
summary
|
|
2033
|
+
});
|
|
2034
|
+
};
|
|
2035
|
+
}
|
|
2036
|
+
//#endregion
|
|
2037
|
+
//#region extensions/matrix/src/matrix/monitor/handler.ts
|
|
2038
|
+
const ALLOW_FROM_STORE_CACHE_TTL_MS = 3e4;
|
|
2039
|
+
const PAIRING_REPLY_COOLDOWN_MS = 5 * 6e4;
|
|
2040
|
+
const MATRIX_TOOL_PROGRESS_MAX_CHARS = 300;
|
|
2041
|
+
let matrixSendModulePromise;
|
|
2042
|
+
let acpBindingRuntimePromise;
|
|
2043
|
+
let sessionBindingRuntimePromise;
|
|
2044
|
+
let matrixReactionEventsPromise;
|
|
2045
|
+
let matrixDraftStreamPromise;
|
|
2046
|
+
function loadMatrixSendModule() {
|
|
2047
|
+
matrixSendModulePromise ??= import("./send-emq66mun.js");
|
|
2048
|
+
return matrixSendModulePromise;
|
|
2049
|
+
}
|
|
2050
|
+
function loadAcpBindingRuntime() {
|
|
2051
|
+
acpBindingRuntimePromise ??= import("./plugin-sdk/acp-binding-runtime.js");
|
|
2052
|
+
return acpBindingRuntimePromise;
|
|
2053
|
+
}
|
|
2054
|
+
function loadSessionBindingRuntime() {
|
|
2055
|
+
sessionBindingRuntimePromise ??= import("./plugin-sdk/session-binding-runtime.js");
|
|
2056
|
+
return sessionBindingRuntimePromise;
|
|
2057
|
+
}
|
|
2058
|
+
function loadMatrixReactionEvents() {
|
|
2059
|
+
matrixReactionEventsPromise ??= import("./reaction-events-BcOb1yBT.js");
|
|
2060
|
+
return matrixReactionEventsPromise;
|
|
2061
|
+
}
|
|
2062
|
+
function loadMatrixDraftStream() {
|
|
2063
|
+
matrixDraftStreamPromise ??= import("./draft-stream-CovJKc0C.js");
|
|
2064
|
+
return matrixDraftStreamPromise;
|
|
2065
|
+
}
|
|
2066
|
+
const MAX_TRACKED_PAIRING_REPLY_SENDERS = 512;
|
|
2067
|
+
const MAX_TRACKED_SHARED_DM_CONTEXT_NOTICES = 512;
|
|
2068
|
+
var MatrixRetryableInboundError = class extends Error {
|
|
2069
|
+
constructor(message, options) {
|
|
2070
|
+
super(message, options);
|
|
2071
|
+
this.name = "MatrixRetryableInboundError";
|
|
2072
|
+
}
|
|
2073
|
+
};
|
|
2074
|
+
async function redactMatrixDraftEvent(client, roomId, draftEventId) {
|
|
2075
|
+
await client.redactEvent(roomId, draftEventId).catch(() => {});
|
|
2076
|
+
}
|
|
2077
|
+
function buildMatrixFinalizedPreviewContent() {
|
|
2078
|
+
return { [MATRIX_DAOCORE_FINALIZED_PREVIEW_KEY]: true };
|
|
2079
|
+
}
|
|
2080
|
+
function resolveMatrixMentionPrecheckText(params) {
|
|
2081
|
+
if (params.locationText?.trim()) return params.locationText.trim();
|
|
2082
|
+
if (typeof params.content.body === "string" && params.content.body.trim()) return params.content.body.trim();
|
|
2083
|
+
if (isPollStartType(params.eventType)) {
|
|
2084
|
+
const parsed = parsePollStartContent(params.content);
|
|
2085
|
+
if (parsed) return formatPollAsText(parsed);
|
|
2086
|
+
}
|
|
2087
|
+
return "";
|
|
2088
|
+
}
|
|
2089
|
+
function hasBundledMatrixReplacementRelation(event) {
|
|
2090
|
+
const relations = event.unsigned?.["m.relations"];
|
|
2091
|
+
if (!relations || typeof relations !== "object") return false;
|
|
2092
|
+
return relations[RelationType.Replace] !== void 0;
|
|
2093
|
+
}
|
|
2094
|
+
function resolveMatrixInboundBodyText(params) {
|
|
2095
|
+
if (params.mediaPlaceholder) return params.rawBody || params.mediaPlaceholder;
|
|
2096
|
+
if (!params.mediaDownloadFailed || !params.hadMediaUrl) return params.rawBody;
|
|
2097
|
+
if (params.mediaSizeLimitExceeded) return formatMatrixMediaTooLargeText({
|
|
2098
|
+
body: params.rawBody,
|
|
2099
|
+
filename: params.filename,
|
|
2100
|
+
msgtype: params.msgtype
|
|
2101
|
+
});
|
|
2102
|
+
return formatMatrixMediaUnavailableText({
|
|
2103
|
+
body: params.rawBody,
|
|
2104
|
+
filename: params.filename,
|
|
2105
|
+
msgtype: params.msgtype
|
|
2106
|
+
});
|
|
2107
|
+
}
|
|
2108
|
+
function markTrackedRoomIfFirst(set, roomId) {
|
|
2109
|
+
if (set.has(roomId)) return false;
|
|
2110
|
+
set.add(roomId);
|
|
2111
|
+
if (set.size > MAX_TRACKED_SHARED_DM_CONTEXT_NOTICES) {
|
|
2112
|
+
const oldest = set.keys().next().value;
|
|
2113
|
+
if (typeof oldest === "string") set.delete(oldest);
|
|
2114
|
+
}
|
|
2115
|
+
return true;
|
|
2116
|
+
}
|
|
2117
|
+
function resolveMatrixSharedDmContextNotice(params) {
|
|
2118
|
+
if ((params.dmSessionScope ?? "per-user") === "per-room") return null;
|
|
2119
|
+
if (params.sentRooms.has(params.roomId)) return null;
|
|
2120
|
+
try {
|
|
2121
|
+
const currentSession = resolveMatrixStoredSessionMeta(resolveSessionStoreEntry({
|
|
2122
|
+
store: loadSessionStore(params.storePath),
|
|
2123
|
+
sessionKey: params.sessionKey
|
|
2124
|
+
}).existing);
|
|
2125
|
+
if (!currentSession) return null;
|
|
2126
|
+
if (currentSession.channel && currentSession.channel !== "matrix") return null;
|
|
2127
|
+
if (currentSession.accountId && currentSession.accountId !== params.accountId) return null;
|
|
2128
|
+
if (!currentSession.directUserId) return null;
|
|
2129
|
+
if (!currentSession.roomId || currentSession.roomId === params.roomId) return null;
|
|
2130
|
+
return [
|
|
2131
|
+
"This Matrix DM is sharing a session with another Matrix DM room.",
|
|
2132
|
+
"Use /focus here for a one-off isolated thread session when thread bindings are enabled, or set",
|
|
2133
|
+
"channels.matrix.dm.sessionScope to per-room to isolate each Matrix DM room."
|
|
2134
|
+
].join(" ");
|
|
2135
|
+
} catch (err) {
|
|
2136
|
+
params.logVerboseMessage(`matrix: failed checking shared DM session notice room=${params.roomId} (${String(err)})`);
|
|
2137
|
+
return null;
|
|
2138
|
+
}
|
|
2139
|
+
}
|
|
2140
|
+
function resolveMatrixPendingHistoryText(params) {
|
|
2141
|
+
if (params.mentionPrecheckText) return params.mentionPrecheckText;
|
|
2142
|
+
if (!params.mediaUrl) return "";
|
|
2143
|
+
const body = typeof params.content.body === "string" ? params.content.body.trim() : void 0;
|
|
2144
|
+
const filename = typeof params.content.filename === "string" ? params.content.filename.trim() : void 0;
|
|
2145
|
+
const msgtype = typeof params.content.msgtype === "string" ? params.content.msgtype : void 0;
|
|
2146
|
+
return formatMatrixMessageText({
|
|
2147
|
+
body: resolveMatrixMessageBody({
|
|
2148
|
+
body,
|
|
2149
|
+
filename,
|
|
2150
|
+
msgtype
|
|
2151
|
+
}),
|
|
2152
|
+
attachment: resolveMatrixMessageAttachment({
|
|
2153
|
+
body,
|
|
2154
|
+
filename,
|
|
2155
|
+
msgtype
|
|
2156
|
+
})
|
|
2157
|
+
}) ?? "";
|
|
2158
|
+
}
|
|
2159
|
+
function resolveMatrixAllowBotsMode(value) {
|
|
2160
|
+
if (value === true) return "all";
|
|
2161
|
+
if (value === "mentions") return "mentions";
|
|
2162
|
+
return "off";
|
|
2163
|
+
}
|
|
2164
|
+
function formatMatrixToolProgressMarkdownCode(text) {
|
|
2165
|
+
return `\`${(text.length <= MATRIX_TOOL_PROGRESS_MAX_CHARS ? text : `${text.slice(0, MATRIX_TOOL_PROGRESS_MAX_CHARS - 1).trimEnd()}...`).replaceAll("`", "'")}\``;
|
|
2166
|
+
}
|
|
2167
|
+
function createMatrixRoomMessageHandler(params) {
|
|
2168
|
+
const { client, core, cfg, accountId, accountConfig, runtime, logger, logVerboseMessage, allowFromResolvedEntries = [], groupAllowFromResolvedEntries = [], roomsConfig, accountAllowBots, configuredBotUserIds = /* @__PURE__ */ new Set(), groupPolicy, replyToMode, threadReplies, dmThreadReplies, dmSessionScope, streaming, previewToolProgressEnabled, blockStreamingEnabled, dmEnabled, dmPolicy, textLimit, mediaMaxBytes, historyLimit, startupMs, startupGraceMs, dropPreStartupMessages, inboundDeduper, directTracker, getRoomInfo, getMemberDisplayName, needsRoomAliasesForConfig, resolveLiveUserAllowlist = resolveMatrixMonitorLiveUserAllowlist } = params;
|
|
2169
|
+
const contextVisibilityMode = resolveChannelContextVisibilityMode({
|
|
2170
|
+
cfg,
|
|
2171
|
+
channel: "matrix",
|
|
2172
|
+
accountId
|
|
2173
|
+
});
|
|
2174
|
+
let cachedStoreAllowFrom = null;
|
|
2175
|
+
let liveDmAllowlistCache = null;
|
|
2176
|
+
let liveGroupAllowlistCache = null;
|
|
2177
|
+
const resolveCachedLiveAllowlist = async (params) => {
|
|
2178
|
+
const accountConfig = resolveMatrixAccountConfig({
|
|
2179
|
+
cfg: params.cfg,
|
|
2180
|
+
accountId
|
|
2181
|
+
});
|
|
2182
|
+
const signature = JSON.stringify({
|
|
2183
|
+
entries: (params.entries ?? []).map((entry) => String(entry).trim()),
|
|
2184
|
+
failClosedOnUnresolved: params.failClosedOnUnresolved === true,
|
|
2185
|
+
dangerouslyAllowNameMatching: isDangerousNameMatchingEnabled(accountConfig)
|
|
2186
|
+
});
|
|
2187
|
+
if (params.cache?.signature === signature) return params.cache.entries;
|
|
2188
|
+
const entries = await resolveLiveUserAllowlist({
|
|
2189
|
+
cfg: params.cfg,
|
|
2190
|
+
accountId,
|
|
2191
|
+
entries: params.entries,
|
|
2192
|
+
failClosedOnUnresolved: params.failClosedOnUnresolved,
|
|
2193
|
+
startupResolvedEntries: params.startupResolvedEntries,
|
|
2194
|
+
runtime
|
|
2195
|
+
});
|
|
2196
|
+
const next = {
|
|
2197
|
+
signature,
|
|
2198
|
+
entries
|
|
2199
|
+
};
|
|
2200
|
+
params.updateCache(next);
|
|
2201
|
+
return entries;
|
|
2202
|
+
};
|
|
2203
|
+
const pairingReplySentAtMsBySender = /* @__PURE__ */ new Map();
|
|
2204
|
+
const resolveThreadContext = createMatrixThreadContextResolver({
|
|
2205
|
+
client,
|
|
2206
|
+
getMemberDisplayName,
|
|
2207
|
+
logVerboseMessage
|
|
2208
|
+
});
|
|
2209
|
+
const resolveReplyContext = createMatrixReplyContextResolver({
|
|
2210
|
+
client,
|
|
2211
|
+
getMemberDisplayName,
|
|
2212
|
+
logVerboseMessage
|
|
2213
|
+
});
|
|
2214
|
+
const roomHistoryTracker = createRoomHistoryTracker();
|
|
2215
|
+
const roomIngressTails = /* @__PURE__ */ new Map();
|
|
2216
|
+
const sharedDmContextNoticeRooms = /* @__PURE__ */ new Set();
|
|
2217
|
+
const readStoreAllowFrom = async () => {
|
|
2218
|
+
const now = Date.now();
|
|
2219
|
+
if (cachedStoreAllowFrom && now < cachedStoreAllowFrom.expiresAtMs) return cachedStoreAllowFrom.value;
|
|
2220
|
+
const value = await core.channel.pairing.readAllowFromStore({
|
|
2221
|
+
channel: "matrix",
|
|
2222
|
+
env: process.env,
|
|
2223
|
+
accountId
|
|
2224
|
+
}).catch(() => []);
|
|
2225
|
+
cachedStoreAllowFrom = {
|
|
2226
|
+
value,
|
|
2227
|
+
expiresAtMs: now + ALLOW_FROM_STORE_CACHE_TTL_MS
|
|
2228
|
+
};
|
|
2229
|
+
return value;
|
|
2230
|
+
};
|
|
2231
|
+
const shouldSendPairingReply = (senderId, created) => {
|
|
2232
|
+
const now = Date.now();
|
|
2233
|
+
if (created) {
|
|
2234
|
+
pairingReplySentAtMsBySender.set(senderId, now);
|
|
2235
|
+
return true;
|
|
2236
|
+
}
|
|
2237
|
+
const lastSentAtMs = pairingReplySentAtMsBySender.get(senderId);
|
|
2238
|
+
if (typeof lastSentAtMs === "number" && now - lastSentAtMs < PAIRING_REPLY_COOLDOWN_MS) return false;
|
|
2239
|
+
pairingReplySentAtMsBySender.set(senderId, now);
|
|
2240
|
+
if (pairingReplySentAtMsBySender.size > MAX_TRACKED_PAIRING_REPLY_SENDERS) {
|
|
2241
|
+
const oldestSender = pairingReplySentAtMsBySender.keys().next().value;
|
|
2242
|
+
if (typeof oldestSender === "string") pairingReplySentAtMsBySender.delete(oldestSender);
|
|
2243
|
+
}
|
|
2244
|
+
return true;
|
|
2245
|
+
};
|
|
2246
|
+
const runRoomIngress = async (roomId, task) => {
|
|
2247
|
+
const previous = roomIngressTails.get(roomId) ?? Promise.resolve();
|
|
2248
|
+
let releaseCurrent;
|
|
2249
|
+
const current = new Promise((resolve) => {
|
|
2250
|
+
releaseCurrent = resolve;
|
|
2251
|
+
});
|
|
2252
|
+
const chain = previous.catch(() => {}).then(() => current);
|
|
2253
|
+
roomIngressTails.set(roomId, chain);
|
|
2254
|
+
await previous.catch(() => {});
|
|
2255
|
+
try {
|
|
2256
|
+
return await task();
|
|
2257
|
+
} finally {
|
|
2258
|
+
releaseCurrent();
|
|
2259
|
+
if (roomIngressTails.get(roomId) === chain) roomIngressTails.delete(roomId);
|
|
2260
|
+
}
|
|
2261
|
+
};
|
|
2262
|
+
return async (roomId, event) => {
|
|
2263
|
+
const eventId = typeof event.event_id === "string" ? event.event_id.trim() : "";
|
|
2264
|
+
let claimedInboundEvent = false;
|
|
2265
|
+
let draftStreamRef;
|
|
2266
|
+
let draftConsumed = false;
|
|
2267
|
+
try {
|
|
2268
|
+
const eventType = event.type;
|
|
2269
|
+
if (eventType === EventType.RoomMessageEncrypted) return;
|
|
2270
|
+
const isPollEvent = isPollEventType(eventType);
|
|
2271
|
+
const isReactionEvent = eventType === EventType.Reaction;
|
|
2272
|
+
const locationContent = event.content;
|
|
2273
|
+
const isLocationEvent = eventType === EventType.Location || eventType === EventType.RoomMessage && locationContent.msgtype === EventType.Location;
|
|
2274
|
+
if (eventType !== EventType.RoomMessage && !isPollEvent && !isLocationEvent && !isReactionEvent) return;
|
|
2275
|
+
logVerboseMessage(`matrix: inbound event room=${roomId} type=${eventType} id=${event.event_id ?? "unknown"}`);
|
|
2276
|
+
if (event.unsigned?.redacted_because) return;
|
|
2277
|
+
const senderId = event.sender;
|
|
2278
|
+
if (!senderId) return;
|
|
2279
|
+
const eventTs = event.origin_server_ts;
|
|
2280
|
+
const eventAge = event.unsigned?.age;
|
|
2281
|
+
const commitInboundEventIfClaimed = async () => {
|
|
2282
|
+
if (!claimedInboundEvent || !inboundDeduper || !eventId) return;
|
|
2283
|
+
await inboundDeduper.commitEvent({
|
|
2284
|
+
roomId,
|
|
2285
|
+
eventId
|
|
2286
|
+
});
|
|
2287
|
+
claimedInboundEvent = false;
|
|
2288
|
+
};
|
|
2289
|
+
const readIngressPrefix = async () => {
|
|
2290
|
+
const selfUserId = await client.getUserId();
|
|
2291
|
+
if (senderId === selfUserId) return;
|
|
2292
|
+
if (dropPreStartupMessages) {
|
|
2293
|
+
if (typeof eventTs === "number" && eventTs < startupMs - startupGraceMs) return;
|
|
2294
|
+
if (typeof eventTs !== "number" && typeof eventAge === "number" && eventAge > startupGraceMs) return;
|
|
2295
|
+
}
|
|
2296
|
+
let content = event.content;
|
|
2297
|
+
if (eventType === EventType.RoomMessage && isMatrixVerificationRoomMessage({
|
|
2298
|
+
msgtype: content.msgtype,
|
|
2299
|
+
body: content.body
|
|
2300
|
+
})) {
|
|
2301
|
+
logVerboseMessage(`matrix: skip verification/system room message room=${roomId}`);
|
|
2302
|
+
return;
|
|
2303
|
+
}
|
|
2304
|
+
const locationPayload = resolveMatrixLocation({
|
|
2305
|
+
eventType,
|
|
2306
|
+
content
|
|
2307
|
+
});
|
|
2308
|
+
const relates = content["m.relates_to"];
|
|
2309
|
+
if (relates && "rel_type" in relates && relates.rel_type === RelationType.Replace) return;
|
|
2310
|
+
if (hasBundledMatrixReplacementRelation(event)) return;
|
|
2311
|
+
if (eventId && inboundDeduper) {
|
|
2312
|
+
claimedInboundEvent = inboundDeduper.claimEvent({
|
|
2313
|
+
roomId,
|
|
2314
|
+
eventId
|
|
2315
|
+
});
|
|
2316
|
+
if (!claimedInboundEvent) {
|
|
2317
|
+
logVerboseMessage(`matrix: skip duplicate inbound event room=${roomId} id=${eventId}`);
|
|
2318
|
+
return;
|
|
2319
|
+
}
|
|
2320
|
+
}
|
|
2321
|
+
return {
|
|
2322
|
+
content,
|
|
2323
|
+
isDirectMessage: await directTracker.isDirectMessage({
|
|
2324
|
+
roomId,
|
|
2325
|
+
senderId,
|
|
2326
|
+
selfUserId
|
|
2327
|
+
}),
|
|
2328
|
+
locationPayload,
|
|
2329
|
+
selfUserId
|
|
2330
|
+
};
|
|
2331
|
+
};
|
|
2332
|
+
const continueIngress = async (params) => {
|
|
2333
|
+
let content = params.content;
|
|
2334
|
+
const isDirectMessage = params.isDirectMessage;
|
|
2335
|
+
const isRoom = !isDirectMessage;
|
|
2336
|
+
const { locationPayload, selfUserId } = params;
|
|
2337
|
+
if (isRoom && groupPolicy === "disabled") {
|
|
2338
|
+
await commitInboundEventIfClaimed();
|
|
2339
|
+
return;
|
|
2340
|
+
}
|
|
2341
|
+
const roomInfoForConfig = isRoom && needsRoomAliasesForConfig ? await getRoomInfo(roomId, { includeAliases: true }) : void 0;
|
|
2342
|
+
const roomAliasesForConfig = roomInfoForConfig ? [roomInfoForConfig.canonicalAlias ?? "", ...roomInfoForConfig.altAliases].filter(Boolean) : [];
|
|
2343
|
+
const roomConfigInfo = isRoom ? resolveMatrixRoomConfig({
|
|
2344
|
+
rooms: roomsConfig,
|
|
2345
|
+
roomId,
|
|
2346
|
+
aliases: roomAliasesForConfig
|
|
2347
|
+
}) : void 0;
|
|
2348
|
+
const roomConfig = roomConfigInfo?.config;
|
|
2349
|
+
const allowBotsMode = resolveMatrixAllowBotsMode(roomConfig?.allowBots ?? accountAllowBots);
|
|
2350
|
+
const isConfiguredBotSender = configuredBotUserIds.has(senderId);
|
|
2351
|
+
const roomMatchMeta = roomConfigInfo ? `matchKey=${roomConfigInfo.matchKey ?? "none"} matchSource=${roomConfigInfo.matchSource ?? "none"}` : "matchKey=none matchSource=none";
|
|
2352
|
+
if (isConfiguredBotSender && allowBotsMode === "off") {
|
|
2353
|
+
logVerboseMessage(`matrix: drop configured bot sender=${senderId} (allowBots=false${isDirectMessage ? "" : `, ${roomMatchMeta}`})`);
|
|
2354
|
+
await commitInboundEventIfClaimed();
|
|
2355
|
+
return;
|
|
2356
|
+
}
|
|
2357
|
+
const botLoopProtection = isConfiguredBotSender && senderId !== selfUserId ? {
|
|
2358
|
+
scopeId: accountId,
|
|
2359
|
+
conversationId: roomId,
|
|
2360
|
+
senderId,
|
|
2361
|
+
receiverId: selfUserId,
|
|
2362
|
+
config: mergePairLoopGuardConfig(accountConfig?.botLoopProtection, roomConfig?.botLoopProtection),
|
|
2363
|
+
defaultsConfig: cfg.channels?.defaults?.botLoopProtection,
|
|
2364
|
+
defaultEnabled: true,
|
|
2365
|
+
nowMs: eventTs ?? void 0
|
|
2366
|
+
} : void 0;
|
|
2367
|
+
if (isRoom && roomConfig && !roomConfigInfo?.allowed) {
|
|
2368
|
+
logVerboseMessage(`matrix: room disabled room=${roomId} (${roomMatchMeta})`);
|
|
2369
|
+
await commitInboundEventIfClaimed();
|
|
2370
|
+
return;
|
|
2371
|
+
}
|
|
2372
|
+
if (isRoom && groupPolicy === "allowlist") {
|
|
2373
|
+
if (!roomConfigInfo?.allowlistConfigured) {
|
|
2374
|
+
logVerboseMessage(`matrix: drop room message (no allowlist, ${roomMatchMeta})`);
|
|
2375
|
+
await commitInboundEventIfClaimed();
|
|
2376
|
+
return;
|
|
2377
|
+
}
|
|
2378
|
+
if (!roomConfig) {
|
|
2379
|
+
logVerboseMessage(`matrix: drop room message (not in allowlist, ${roomMatchMeta})`);
|
|
2380
|
+
await commitInboundEventIfClaimed();
|
|
2381
|
+
return;
|
|
2382
|
+
}
|
|
2383
|
+
}
|
|
2384
|
+
let senderNamePromise = null;
|
|
2385
|
+
const getSenderName = async () => {
|
|
2386
|
+
senderNamePromise ??= getMemberDisplayName(roomId, senderId).catch(() => senderId);
|
|
2387
|
+
return await senderNamePromise;
|
|
2388
|
+
};
|
|
2389
|
+
const storeAllowFrom = isDirectMessage && dmPolicy !== "allowlist" && dmPolicy !== "open" ? await readStoreAllowFrom() : [];
|
|
2390
|
+
const roomUsers = roomConfig?.users ?? [];
|
|
2391
|
+
const liveCfg = core.config.current();
|
|
2392
|
+
const liveAccountAllowlists = resolveMatrixAccountAllowlistConfig({
|
|
2393
|
+
cfg: liveCfg,
|
|
2394
|
+
accountId
|
|
2395
|
+
});
|
|
2396
|
+
const accessState = await resolveMatrixMonitorAccessState({
|
|
2397
|
+
allowFrom: await resolveCachedLiveAllowlist({
|
|
2398
|
+
cfg: liveCfg,
|
|
2399
|
+
entries: liveAccountAllowlists.dmAllowFrom,
|
|
2400
|
+
startupResolvedEntries: allowFromResolvedEntries,
|
|
2401
|
+
cache: liveDmAllowlistCache,
|
|
2402
|
+
updateCache: (next) => {
|
|
2403
|
+
liveDmAllowlistCache = next;
|
|
2404
|
+
}
|
|
2405
|
+
}),
|
|
2406
|
+
storeAllowFrom,
|
|
2407
|
+
dmPolicy,
|
|
2408
|
+
groupPolicy,
|
|
2409
|
+
groupAllowFrom: await resolveCachedLiveAllowlist({
|
|
2410
|
+
cfg: liveCfg,
|
|
2411
|
+
entries: liveAccountAllowlists.groupAllowFrom,
|
|
2412
|
+
failClosedOnUnresolved: true,
|
|
2413
|
+
startupResolvedEntries: groupAllowFromResolvedEntries,
|
|
2414
|
+
cache: liveGroupAllowlistCache,
|
|
2415
|
+
updateCache: (next) => {
|
|
2416
|
+
liveGroupAllowlistCache = next;
|
|
2417
|
+
}
|
|
2418
|
+
}),
|
|
2419
|
+
roomUsers,
|
|
2420
|
+
senderId,
|
|
2421
|
+
isRoom,
|
|
2422
|
+
accountId,
|
|
2423
|
+
eventKind: isReactionEvent ? "reaction" : "message"
|
|
2424
|
+
});
|
|
2425
|
+
const { effectiveGroupAllowFrom, effectiveRoomUsers, messageIngress } = accessState;
|
|
2426
|
+
const ingressDecision = messageIngress.ingress;
|
|
2427
|
+
if (isDirectMessage) {
|
|
2428
|
+
if (!dmEnabled || dmPolicy === "disabled") {
|
|
2429
|
+
await commitInboundEventIfClaimed();
|
|
2430
|
+
return;
|
|
2431
|
+
}
|
|
2432
|
+
const senderReason = messageIngress.senderAccess.reasonCode;
|
|
2433
|
+
if (ingressDecision.decision !== "allow") {
|
|
2434
|
+
if (ingressDecision.admission === "pairing-required") {
|
|
2435
|
+
const senderName = await getSenderName();
|
|
2436
|
+
const { code, created } = await core.channel.pairing.upsertPairingRequest({
|
|
2437
|
+
channel: "matrix",
|
|
2438
|
+
id: senderId,
|
|
2439
|
+
accountId,
|
|
2440
|
+
meta: { name: senderName }
|
|
2441
|
+
});
|
|
2442
|
+
if (shouldSendPairingReply(senderId, created)) {
|
|
2443
|
+
const pairingReply = core.channel.pairing.buildPairingReply({
|
|
2444
|
+
channel: "matrix",
|
|
2445
|
+
idLine: `Your Matrix user id: ${senderId}`,
|
|
2446
|
+
code
|
|
2447
|
+
});
|
|
2448
|
+
logVerboseMessage(created ? `matrix pairing request sender=${senderId} name=${senderName ?? "unknown"} (reason=${senderReason})` : `matrix pairing reminder sender=${senderId} name=${senderName ?? "unknown"} (reason=${senderReason})`);
|
|
2449
|
+
try {
|
|
2450
|
+
const { sendMessageMatrix } = await loadMatrixSendModule();
|
|
2451
|
+
await sendMessageMatrix(`room:${roomId}`, created ? pairingReply : `${pairingReply}\n\nPairing request is still pending approval. Reusing existing code.`, {
|
|
2452
|
+
client,
|
|
2453
|
+
cfg,
|
|
2454
|
+
accountId
|
|
2455
|
+
});
|
|
2456
|
+
await commitInboundEventIfClaimed();
|
|
2457
|
+
} catch (err) {
|
|
2458
|
+
logVerboseMessage(`matrix pairing reply failed for ${senderId}: ${String(err)}`);
|
|
2459
|
+
return;
|
|
2460
|
+
}
|
|
2461
|
+
} else {
|
|
2462
|
+
logVerboseMessage(`matrix pairing reminder suppressed sender=${senderId} (cooldown)`);
|
|
2463
|
+
await commitInboundEventIfClaimed();
|
|
2464
|
+
}
|
|
2465
|
+
}
|
|
2466
|
+
if (isReactionEvent || dmPolicy !== "pairing") {
|
|
2467
|
+
logVerboseMessage(`matrix: blocked ${isReactionEvent ? "reaction" : "dm"} sender ${senderId} (dmPolicy=${dmPolicy}, reason=${senderReason})`);
|
|
2468
|
+
await commitInboundEventIfClaimed();
|
|
2469
|
+
}
|
|
2470
|
+
return;
|
|
2471
|
+
}
|
|
2472
|
+
}
|
|
2473
|
+
if (isRoom && ingressDecision.decision !== "allow") {
|
|
2474
|
+
logVerboseMessage(`matrix: blocked sender ${senderId} (ingress=${ingressDecision.reasonCode}, ${roomMatchMeta})`);
|
|
2475
|
+
await commitInboundEventIfClaimed();
|
|
2476
|
+
return;
|
|
2477
|
+
}
|
|
2478
|
+
if (isRoom) logVerboseMessage(`matrix: allow room ${roomId} (${roomMatchMeta})`);
|
|
2479
|
+
if (isReactionEvent) {
|
|
2480
|
+
const senderName = await getSenderName();
|
|
2481
|
+
const { handleInboundMatrixReaction } = await loadMatrixReactionEvents();
|
|
2482
|
+
await handleInboundMatrixReaction({
|
|
2483
|
+
client,
|
|
2484
|
+
core,
|
|
2485
|
+
cfg,
|
|
2486
|
+
accountId,
|
|
2487
|
+
roomId,
|
|
2488
|
+
event,
|
|
2489
|
+
senderId,
|
|
2490
|
+
senderLabel: senderName,
|
|
2491
|
+
selfUserId,
|
|
2492
|
+
isDirectMessage,
|
|
2493
|
+
logVerboseMessage
|
|
2494
|
+
});
|
|
2495
|
+
await commitInboundEventIfClaimed();
|
|
2496
|
+
return;
|
|
2497
|
+
}
|
|
2498
|
+
let pollSnapshotPromise = null;
|
|
2499
|
+
const getPollSnapshot = async () => {
|
|
2500
|
+
if (!isPollEvent) return null;
|
|
2501
|
+
pollSnapshotPromise ??= fetchMatrixPollSnapshot(client, roomId, event).catch((err) => {
|
|
2502
|
+
logVerboseMessage(`matrix: failed resolving poll snapshot room=${roomId} id=${event.event_id ?? "unknown"}: ${String(err)}`);
|
|
2503
|
+
return null;
|
|
2504
|
+
});
|
|
2505
|
+
return await pollSnapshotPromise;
|
|
2506
|
+
};
|
|
2507
|
+
const mentionPrecheckText = resolveMatrixMentionPrecheckText({
|
|
2508
|
+
eventType,
|
|
2509
|
+
content,
|
|
2510
|
+
locationText: locationPayload?.text
|
|
2511
|
+
});
|
|
2512
|
+
const contentUrl = "url" in content && typeof content.url === "string" ? content.url : void 0;
|
|
2513
|
+
const contentFile = "file" in content && content.file && typeof content.file === "object" ? content.file : void 0;
|
|
2514
|
+
const mediaUrl = contentUrl ?? contentFile?.url;
|
|
2515
|
+
const pendingHistoryText = resolveMatrixPendingHistoryText({
|
|
2516
|
+
mentionPrecheckText,
|
|
2517
|
+
content,
|
|
2518
|
+
mediaUrl
|
|
2519
|
+
});
|
|
2520
|
+
const pendingHistoryPollText = !pendingHistoryText && isPollEvent && historyLimit > 0 ? (await getPollSnapshot())?.text : "";
|
|
2521
|
+
if (!mentionPrecheckText && !mediaUrl && !isPollEvent) {
|
|
2522
|
+
await commitInboundEventIfClaimed();
|
|
2523
|
+
return;
|
|
2524
|
+
}
|
|
2525
|
+
const messageId = event.event_id ?? "";
|
|
2526
|
+
const threadRootId = resolveMatrixThreadRootId({
|
|
2527
|
+
event,
|
|
2528
|
+
content
|
|
2529
|
+
});
|
|
2530
|
+
const thread = resolveMatrixThreadRouting({
|
|
2531
|
+
isDirectMessage,
|
|
2532
|
+
threadReplies,
|
|
2533
|
+
dmThreadReplies,
|
|
2534
|
+
messageId,
|
|
2535
|
+
threadRootId
|
|
2536
|
+
});
|
|
2537
|
+
const { route: _route, configuredBinding: _configuredBinding, runtimeBindingId: _runtimeBindingId } = resolveMatrixInboundRoute({
|
|
2538
|
+
cfg,
|
|
2539
|
+
accountId,
|
|
2540
|
+
roomId,
|
|
2541
|
+
senderId,
|
|
2542
|
+
isDirectMessage,
|
|
2543
|
+
dmSessionScope,
|
|
2544
|
+
threadId: thread.threadId,
|
|
2545
|
+
eventTs: eventTs ?? void 0,
|
|
2546
|
+
resolveAgentRoute: core.channel.routing.resolveAgentRoute
|
|
2547
|
+
});
|
|
2548
|
+
const hasExplicitSessionBinding = _configuredBinding !== null || _runtimeBindingId !== null;
|
|
2549
|
+
const agentMentionRegexes = core.channel.mentions.buildMentionRegexes(cfg, _route.agentId);
|
|
2550
|
+
const selfDisplayName = content.formatted_body ? await getMemberDisplayName(roomId, selfUserId).catch(() => void 0) : void 0;
|
|
2551
|
+
const { wasMentioned, hasExplicitMention } = resolveMentions({
|
|
2552
|
+
content,
|
|
2553
|
+
userId: selfUserId,
|
|
2554
|
+
displayName: selfDisplayName,
|
|
2555
|
+
text: mentionPrecheckText,
|
|
2556
|
+
mentionRegexes: agentMentionRegexes
|
|
2557
|
+
});
|
|
2558
|
+
if (isConfiguredBotSender && allowBotsMode === "mentions" && !isDirectMessage && !wasMentioned) {
|
|
2559
|
+
logVerboseMessage(`matrix: drop configured bot sender=${senderId} (allowBots=mentions, missing mention, ${roomMatchMeta})`);
|
|
2560
|
+
await commitInboundEventIfClaimed();
|
|
2561
|
+
return;
|
|
2562
|
+
}
|
|
2563
|
+
const allowTextCommands = core.channel.commands.shouldHandleTextCommands({
|
|
2564
|
+
cfg,
|
|
2565
|
+
surface: "matrix"
|
|
2566
|
+
});
|
|
2567
|
+
const useAccessGroups = cfg.commands?.useAccessGroups !== false;
|
|
2568
|
+
const commandCheckText = stripMatrixMentionPrefix({
|
|
2569
|
+
text: mentionPrecheckText,
|
|
2570
|
+
userId: selfUserId,
|
|
2571
|
+
displayName: selfDisplayName,
|
|
2572
|
+
mentionRegexes: agentMentionRegexes
|
|
2573
|
+
});
|
|
2574
|
+
const hasControlCommandInMessage = core.channel.text.hasControlCommand(commandCheckText, cfg);
|
|
2575
|
+
const commandAccess = await resolveMatrixMonitorCommandAccess(accessState, {
|
|
2576
|
+
useAccessGroups,
|
|
2577
|
+
allowTextCommands,
|
|
2578
|
+
hasControlCommand: hasControlCommandInMessage
|
|
2579
|
+
});
|
|
2580
|
+
const commandAuthorized = commandAccess.authorized;
|
|
2581
|
+
if (isRoom && commandAccess.shouldBlockControlCommand) {
|
|
2582
|
+
logInboundDrop({
|
|
2583
|
+
log: logVerboseMessage,
|
|
2584
|
+
channel: "matrix",
|
|
2585
|
+
reason: "control command (unauthorized)",
|
|
2586
|
+
target: senderId
|
|
2587
|
+
});
|
|
2588
|
+
await commitInboundEventIfClaimed();
|
|
2589
|
+
return;
|
|
2590
|
+
}
|
|
2591
|
+
const shouldRequireMention = isRoom ? roomConfig?.autoReply === true ? false : roomConfig?.autoReply === false ? true : typeof roomConfig?.requireMention === "boolean" ? roomConfig?.requireMention : true : false;
|
|
2592
|
+
const shouldBypassMention = allowTextCommands && isRoom && shouldRequireMention && !wasMentioned && !hasExplicitMention && commandAuthorized && hasControlCommandInMessage;
|
|
2593
|
+
const canDetectMention = agentMentionRegexes.length > 0 || hasExplicitMention;
|
|
2594
|
+
if (isRoom && shouldRequireMention && !wasMentioned && !shouldBypassMention) {
|
|
2595
|
+
const pendingHistoryBody = pendingHistoryText || pendingHistoryPollText;
|
|
2596
|
+
if (historyLimit > 0 && pendingHistoryBody) {
|
|
2597
|
+
const pendingEntry = {
|
|
2598
|
+
sender: senderId,
|
|
2599
|
+
body: pendingHistoryBody,
|
|
2600
|
+
timestamp: eventTs ?? void 0,
|
|
2601
|
+
messageId
|
|
2602
|
+
};
|
|
2603
|
+
roomHistoryTracker.recordPending(roomId, pendingEntry);
|
|
2604
|
+
}
|
|
2605
|
+
logger.info("skipping room message", {
|
|
2606
|
+
roomId,
|
|
2607
|
+
reason: "no-mention"
|
|
2608
|
+
});
|
|
2609
|
+
await commitInboundEventIfClaimed();
|
|
2610
|
+
return;
|
|
2611
|
+
}
|
|
2612
|
+
if (isPollEvent) {
|
|
2613
|
+
const pollSnapshot = await getPollSnapshot();
|
|
2614
|
+
if (!pollSnapshot) return;
|
|
2615
|
+
content = {
|
|
2616
|
+
msgtype: "m.text",
|
|
2617
|
+
body: pollSnapshot.text
|
|
2618
|
+
};
|
|
2619
|
+
}
|
|
2620
|
+
let media = null;
|
|
2621
|
+
let mediaDownloadFailed = false;
|
|
2622
|
+
let mediaSizeLimitExceeded = false;
|
|
2623
|
+
const finalContentUrl = "url" in content && typeof content.url === "string" ? content.url : void 0;
|
|
2624
|
+
const finalContentFile = "file" in content && content.file && typeof content.file === "object" ? content.file : void 0;
|
|
2625
|
+
const finalMediaUrl = finalContentUrl ?? finalContentFile?.url;
|
|
2626
|
+
const contentBody = typeof content.body === "string" ? content.body.trim() : "";
|
|
2627
|
+
const originalFilename = (typeof content.filename === "string" ? content.filename.trim() : "") || contentBody || void 0;
|
|
2628
|
+
const contentInfo = "info" in content && content.info && typeof content.info === "object" ? content.info : void 0;
|
|
2629
|
+
const contentType = contentInfo?.mimetype;
|
|
2630
|
+
const contentSize = typeof contentInfo?.size === "number" ? contentInfo.size : void 0;
|
|
2631
|
+
if (finalMediaUrl?.startsWith("mxc://")) try {
|
|
2632
|
+
media = await downloadMatrixMedia({
|
|
2633
|
+
client,
|
|
2634
|
+
mxcUrl: finalMediaUrl,
|
|
2635
|
+
contentType,
|
|
2636
|
+
sizeBytes: contentSize,
|
|
2637
|
+
maxBytes: mediaMaxBytes,
|
|
2638
|
+
file: finalContentFile,
|
|
2639
|
+
originalFilename
|
|
2640
|
+
});
|
|
2641
|
+
} catch (err) {
|
|
2642
|
+
mediaDownloadFailed = true;
|
|
2643
|
+
if (isMatrixMediaSizeLimitError(err)) mediaSizeLimitExceeded = true;
|
|
2644
|
+
const errorText = formatMatrixErrorMessage(err);
|
|
2645
|
+
logVerboseMessage(`matrix: media download failed room=${roomId} id=${event.event_id ?? "unknown"} type=${content.msgtype} error=${errorText}`);
|
|
2646
|
+
logger.warn("matrix media download failed", {
|
|
2647
|
+
roomId,
|
|
2648
|
+
eventId: event.event_id,
|
|
2649
|
+
msgtype: content.msgtype,
|
|
2650
|
+
encrypted: Boolean(finalContentFile),
|
|
2651
|
+
error: errorText
|
|
2652
|
+
});
|
|
2653
|
+
}
|
|
2654
|
+
const bodyText = resolveMatrixInboundBodyText({
|
|
2655
|
+
rawBody: locationPayload?.text ?? contentBody,
|
|
2656
|
+
filename: typeof content.filename === "string" ? content.filename : void 0,
|
|
2657
|
+
mediaPlaceholder: media?.placeholder,
|
|
2658
|
+
msgtype: content.msgtype,
|
|
2659
|
+
hadMediaUrl: Boolean(finalMediaUrl),
|
|
2660
|
+
mediaDownloadFailed,
|
|
2661
|
+
mediaSizeLimitExceeded
|
|
2662
|
+
});
|
|
2663
|
+
if (!bodyText) {
|
|
2664
|
+
await commitInboundEventIfClaimed();
|
|
2665
|
+
return;
|
|
2666
|
+
}
|
|
2667
|
+
const commandBodyText = hasControlCommandInMessage ? commandCheckText : bodyText;
|
|
2668
|
+
const senderName = await getSenderName();
|
|
2669
|
+
if (_configuredBinding) {
|
|
2670
|
+
const { ensureConfiguredAcpBindingReady } = await loadAcpBindingRuntime();
|
|
2671
|
+
if (!(await ensureConfiguredAcpBindingReady({
|
|
2672
|
+
cfg,
|
|
2673
|
+
configuredBinding: _configuredBinding
|
|
2674
|
+
})).ok) {
|
|
2675
|
+
logInboundDrop({
|
|
2676
|
+
log: logVerboseMessage,
|
|
2677
|
+
channel: "matrix",
|
|
2678
|
+
reason: "configured ACP binding unavailable",
|
|
2679
|
+
target: _configuredBinding.spec.conversationId
|
|
2680
|
+
});
|
|
2681
|
+
return;
|
|
2682
|
+
}
|
|
2683
|
+
}
|
|
2684
|
+
if (_runtimeBindingId) {
|
|
2685
|
+
const { getSessionBindingService } = await loadSessionBindingRuntime();
|
|
2686
|
+
getSessionBindingService().touch(_runtimeBindingId, eventTs ?? void 0);
|
|
2687
|
+
}
|
|
2688
|
+
const preparedTrigger = isRoom && historyLimit > 0 ? roomHistoryTracker.prepareTrigger(_route.agentId, roomId, historyLimit, {
|
|
2689
|
+
sender: senderName,
|
|
2690
|
+
body: bodyText,
|
|
2691
|
+
timestamp: eventTs ?? void 0,
|
|
2692
|
+
messageId
|
|
2693
|
+
}) : void 0;
|
|
2694
|
+
return {
|
|
2695
|
+
route: _route,
|
|
2696
|
+
hasExplicitSessionBinding,
|
|
2697
|
+
roomConfig,
|
|
2698
|
+
isDirectMessage,
|
|
2699
|
+
isRoom,
|
|
2700
|
+
shouldRequireMention,
|
|
2701
|
+
wasMentioned,
|
|
2702
|
+
shouldBypassMention,
|
|
2703
|
+
canDetectMention,
|
|
2704
|
+
commandAuthorized,
|
|
2705
|
+
inboundHistory: preparedTrigger ? buildInboundHistoryFromEntries({
|
|
2706
|
+
entries: preparedTrigger.history,
|
|
2707
|
+
limit: historyLimit
|
|
2708
|
+
}) : void 0,
|
|
2709
|
+
senderName,
|
|
2710
|
+
bodyText,
|
|
2711
|
+
commandBodyText,
|
|
2712
|
+
media,
|
|
2713
|
+
locationPayload,
|
|
2714
|
+
messageId,
|
|
2715
|
+
triggerSnapshot: preparedTrigger,
|
|
2716
|
+
threadRootId,
|
|
2717
|
+
thread,
|
|
2718
|
+
botLoopProtection,
|
|
2719
|
+
effectiveGroupAllowFrom,
|
|
2720
|
+
effectiveRoomUsers
|
|
2721
|
+
};
|
|
2722
|
+
};
|
|
2723
|
+
const ingressResult = historyLimit > 0 ? await runRoomIngress(roomId, async () => {
|
|
2724
|
+
const prefix = await readIngressPrefix();
|
|
2725
|
+
if (!prefix) return;
|
|
2726
|
+
if (prefix.isDirectMessage) return { deferredPrefix: prefix };
|
|
2727
|
+
return { ingressResult: await continueIngress(prefix) };
|
|
2728
|
+
}) : void 0;
|
|
2729
|
+
const resolvedIngressResult = historyLimit > 0 ? ingressResult?.deferredPrefix ? await continueIngress(ingressResult.deferredPrefix) : ingressResult?.ingressResult : await (async () => {
|
|
2730
|
+
const prefix = await readIngressPrefix();
|
|
2731
|
+
if (!prefix) return;
|
|
2732
|
+
return await continueIngress(prefix);
|
|
2733
|
+
})();
|
|
2734
|
+
if (!resolvedIngressResult) return;
|
|
2735
|
+
const { route: _route, hasExplicitSessionBinding, roomConfig, isDirectMessage, isRoom, shouldRequireMention, wasMentioned, shouldBypassMention, canDetectMention, commandAuthorized, inboundHistory, senderName, bodyText, commandBodyText, media, locationPayload, messageId, triggerSnapshot, threadRootId, thread, botLoopProtection, effectiveGroupAllowFrom, effectiveRoomUsers } = resolvedIngressResult;
|
|
2736
|
+
const replyToEventId = resolveMatrixReplyToEventId(event.content);
|
|
2737
|
+
const threadTarget = thread.threadId;
|
|
2738
|
+
const isRoomContextSenderAllowed = (contextSenderId) => {
|
|
2739
|
+
if (!isRoom || !contextSenderId) return true;
|
|
2740
|
+
if (effectiveRoomUsers.length > 0) return resolveMatrixAllowListMatch({
|
|
2741
|
+
allowList: effectiveRoomUsers,
|
|
2742
|
+
userId: contextSenderId
|
|
2743
|
+
}).allowed;
|
|
2744
|
+
if (groupPolicy === "allowlist" && effectiveGroupAllowFrom.length > 0) return resolveMatrixAllowListMatch({
|
|
2745
|
+
allowList: effectiveGroupAllowFrom,
|
|
2746
|
+
userId: contextSenderId
|
|
2747
|
+
}).allowed;
|
|
2748
|
+
return true;
|
|
2749
|
+
};
|
|
2750
|
+
const shouldIncludeRoomContextSender = (kind, contextSenderId) => evaluateSupplementalContextVisibility({
|
|
2751
|
+
mode: contextVisibilityMode,
|
|
2752
|
+
kind,
|
|
2753
|
+
senderAllowed: isRoomContextSenderAllowed(contextSenderId)
|
|
2754
|
+
}).include;
|
|
2755
|
+
let threadContext = threadRootId ? await resolveThreadContext({
|
|
2756
|
+
roomId,
|
|
2757
|
+
threadRootId
|
|
2758
|
+
}) : void 0;
|
|
2759
|
+
let threadContextBlockedByPolicy = false;
|
|
2760
|
+
if (threadContext?.senderId && !shouldIncludeRoomContextSender("thread", threadContext.senderId)) {
|
|
2761
|
+
logVerboseMessage(`matrix: drop thread root context (mode=${contextVisibilityMode})`);
|
|
2762
|
+
threadContextBlockedByPolicy = true;
|
|
2763
|
+
threadContext = void 0;
|
|
2764
|
+
}
|
|
2765
|
+
let replyContext;
|
|
2766
|
+
if (replyToEventId && replyToEventId === threadRootId && threadContext?.summary) replyContext = {
|
|
2767
|
+
replyToBody: threadContext.summary,
|
|
2768
|
+
replyToSender: threadContext.senderLabel,
|
|
2769
|
+
replyToSenderId: threadContext.senderId
|
|
2770
|
+
};
|
|
2771
|
+
else if (replyToEventId && replyToEventId === threadRootId && threadContextBlockedByPolicy) replyContext = await resolveReplyContext({
|
|
2772
|
+
roomId,
|
|
2773
|
+
eventId: replyToEventId
|
|
2774
|
+
});
|
|
2775
|
+
else replyContext = replyToEventId ? await resolveReplyContext({
|
|
2776
|
+
roomId,
|
|
2777
|
+
eventId: replyToEventId
|
|
2778
|
+
}) : void 0;
|
|
2779
|
+
if (replyContext?.replyToSenderId && !shouldIncludeRoomContextSender("quote", replyContext.replyToSenderId)) {
|
|
2780
|
+
logVerboseMessage(`matrix: drop reply context (mode=${contextVisibilityMode})`);
|
|
2781
|
+
replyContext = void 0;
|
|
2782
|
+
}
|
|
2783
|
+
const roomName = (isRoom ? await getRoomInfo(roomId) : void 0)?.name;
|
|
2784
|
+
const envelopeFrom = isDirectMessage ? senderName : roomName ?? roomId;
|
|
2785
|
+
const textWithId = `${bodyText}\n[matrix event id: ${messageId} room: ${roomId}]`;
|
|
2786
|
+
const storePath = core.channel.session.resolveStorePath(cfg.session?.store, { agentId: _route.agentId });
|
|
2787
|
+
const envelopeOptions = core.channel.reply.resolveEnvelopeFormatOptions(cfg);
|
|
2788
|
+
const previousTimestamp = core.channel.session.readSessionUpdatedAt({
|
|
2789
|
+
storePath,
|
|
2790
|
+
sessionKey: _route.sessionKey
|
|
2791
|
+
});
|
|
2792
|
+
const sharedDmNoticeSessionKey = threadTarget ? _route.mainSessionKey || _route.sessionKey : _route.sessionKey;
|
|
2793
|
+
const sharedDmContextNotice = isDirectMessage ? hasExplicitSessionBinding ? null : resolveMatrixSharedDmContextNotice({
|
|
2794
|
+
storePath,
|
|
2795
|
+
sessionKey: sharedDmNoticeSessionKey,
|
|
2796
|
+
roomId,
|
|
2797
|
+
accountId: _route.accountId,
|
|
2798
|
+
dmSessionScope,
|
|
2799
|
+
sentRooms: sharedDmContextNoticeRooms,
|
|
2800
|
+
logVerboseMessage
|
|
2801
|
+
}) : null;
|
|
2802
|
+
const body = core.channel.reply.formatAgentEnvelope({
|
|
2803
|
+
channel: "Matrix",
|
|
2804
|
+
from: envelopeFrom,
|
|
2805
|
+
timestamp: eventTs ?? void 0,
|
|
2806
|
+
previousTimestamp,
|
|
2807
|
+
envelope: envelopeOptions,
|
|
2808
|
+
body: textWithId
|
|
2809
|
+
});
|
|
2810
|
+
const groupSystemPrompt = normalizeOptionalString(roomConfig?.systemPrompt);
|
|
2811
|
+
const ctxPayload = core.channel.reply.finalizeInboundContext({
|
|
2812
|
+
Body: body,
|
|
2813
|
+
RawBody: bodyText,
|
|
2814
|
+
CommandBody: commandBodyText,
|
|
2815
|
+
BodyForAgent: bodyText,
|
|
2816
|
+
BodyForCommands: commandBodyText,
|
|
2817
|
+
InboundHistory: inboundHistory && inboundHistory.length > 0 ? inboundHistory : void 0,
|
|
2818
|
+
From: isDirectMessage ? `matrix:${senderId}` : `matrix:channel:${roomId}`,
|
|
2819
|
+
To: `room:${roomId}`,
|
|
2820
|
+
SessionKey: _route.sessionKey,
|
|
2821
|
+
AccountId: _route.accountId,
|
|
2822
|
+
ChatType: isDirectMessage ? "direct" : "channel",
|
|
2823
|
+
ConversationLabel: envelopeFrom,
|
|
2824
|
+
SenderName: senderName,
|
|
2825
|
+
SenderId: senderId,
|
|
2826
|
+
SenderUsername: senderId.split(":")[0]?.replace(/^@/, ""),
|
|
2827
|
+
GroupSubject: isRoom ? roomName ?? roomId : void 0,
|
|
2828
|
+
GroupId: isRoom ? roomId : void 0,
|
|
2829
|
+
GroupChannel: isRoom ? roomId : void 0,
|
|
2830
|
+
GroupSystemPrompt: isRoom ? groupSystemPrompt : void 0,
|
|
2831
|
+
Provider: "matrix",
|
|
2832
|
+
Surface: "matrix",
|
|
2833
|
+
WasMentioned: isRoom ? wasMentioned : void 0,
|
|
2834
|
+
MessageSid: messageId,
|
|
2835
|
+
ReplyToId: threadTarget ? void 0 : replyToEventId ?? void 0,
|
|
2836
|
+
ReplyToBody: replyContext?.replyToBody,
|
|
2837
|
+
ReplyToSender: replyContext?.replyToSender,
|
|
2838
|
+
MessageThreadId: threadTarget,
|
|
2839
|
+
ThreadStarterBody: threadContext?.threadStarterBody,
|
|
2840
|
+
Timestamp: eventTs ?? void 0,
|
|
2841
|
+
MediaPath: media?.path,
|
|
2842
|
+
MediaType: media?.contentType,
|
|
2843
|
+
MediaUrl: media?.path,
|
|
2844
|
+
...locationPayload?.context,
|
|
2845
|
+
CommandAuthorized: commandAuthorized,
|
|
2846
|
+
CommandSource: "text",
|
|
2847
|
+
NativeChannelId: roomId,
|
|
2848
|
+
NativeDirectUserId: isDirectMessage ? senderId : void 0,
|
|
2849
|
+
OriginatingChannel: "matrix",
|
|
2850
|
+
OriginatingTo: `room:${roomId}`
|
|
2851
|
+
});
|
|
2852
|
+
logVerboseMessage(`matrix inbound: room=${roomId} from=${senderId} preview="${bodyText.slice(0, 200).replace(/\n/g, "\\n")}"`);
|
|
2853
|
+
const replyTarget = ctxPayload.To;
|
|
2854
|
+
if (!replyTarget) {
|
|
2855
|
+
runtime.error?.("matrix: missing reply target");
|
|
2856
|
+
return;
|
|
2857
|
+
}
|
|
2858
|
+
const { ackReaction, ackReactionScope: ackScope } = resolveMatrixAckReactionConfig({
|
|
2859
|
+
cfg,
|
|
2860
|
+
agentId: _route.agentId,
|
|
2861
|
+
accountId
|
|
2862
|
+
});
|
|
2863
|
+
const shouldAckReaction = () => Boolean(ackReaction && core.channel.reactions.shouldAckReaction({
|
|
2864
|
+
scope: ackScope,
|
|
2865
|
+
isDirect: isDirectMessage,
|
|
2866
|
+
isGroup: isRoom,
|
|
2867
|
+
isMentionableGroup: isRoom,
|
|
2868
|
+
requireMention: shouldRequireMention,
|
|
2869
|
+
canDetectMention,
|
|
2870
|
+
effectiveWasMentioned: wasMentioned || shouldBypassMention,
|
|
2871
|
+
shouldBypassMention
|
|
2872
|
+
}));
|
|
2873
|
+
if (shouldAckReaction() && messageId) loadMatrixSendModule().then(({ reactMatrixMessage }) => reactMatrixMessage(roomId, messageId, ackReaction, client)).catch((err) => {
|
|
2874
|
+
logVerboseMessage(`matrix react failed for room ${roomId}: ${String(err)}`);
|
|
2875
|
+
});
|
|
2876
|
+
if (messageId) loadMatrixSendModule().then(({ sendReadReceiptMatrix }) => sendReadReceiptMatrix(roomId, messageId, client)).catch((err) => {
|
|
2877
|
+
logVerboseMessage(`matrix: read receipt failed room=${roomId} id=${messageId}: ${String(err)}`);
|
|
2878
|
+
});
|
|
2879
|
+
const tableMode = core.channel.text.resolveMarkdownTableMode({
|
|
2880
|
+
cfg,
|
|
2881
|
+
channel: "matrix",
|
|
2882
|
+
accountId: _route.accountId
|
|
2883
|
+
});
|
|
2884
|
+
const mediaLocalRoots = getAgentScopedMediaLocalRoots(cfg, _route.agentId);
|
|
2885
|
+
let finalReplyDeliveryFailed = false;
|
|
2886
|
+
let nonFinalReplyDeliveryFailed = false;
|
|
2887
|
+
let retryableReplyDeliveryFailed = false;
|
|
2888
|
+
const { onModelSelected, ...prefixOptions } = createReplyPrefixOptions({
|
|
2889
|
+
cfg,
|
|
2890
|
+
agentId: _route.agentId,
|
|
2891
|
+
channel: "matrix",
|
|
2892
|
+
accountId: _route.accountId
|
|
2893
|
+
});
|
|
2894
|
+
const typingCallbacks = createTypingCallbacks({
|
|
2895
|
+
start: async () => {
|
|
2896
|
+
const { sendTypingMatrix } = await loadMatrixSendModule();
|
|
2897
|
+
await sendTypingMatrix(roomId, true, void 0, client);
|
|
2898
|
+
},
|
|
2899
|
+
stop: async () => {
|
|
2900
|
+
const { sendTypingMatrix } = await loadMatrixSendModule();
|
|
2901
|
+
await sendTypingMatrix(roomId, false, void 0, client);
|
|
2902
|
+
},
|
|
2903
|
+
onStartError: (err) => {
|
|
2904
|
+
logTypingFailure({
|
|
2905
|
+
log: logVerboseMessage,
|
|
2906
|
+
channel: "matrix",
|
|
2907
|
+
action: "start",
|
|
2908
|
+
target: roomId,
|
|
2909
|
+
error: err
|
|
2910
|
+
});
|
|
2911
|
+
},
|
|
2912
|
+
onStopError: (err) => {
|
|
2913
|
+
logTypingFailure({
|
|
2914
|
+
log: logVerboseMessage,
|
|
2915
|
+
channel: "matrix",
|
|
2916
|
+
action: "stop",
|
|
2917
|
+
target: roomId,
|
|
2918
|
+
error: err
|
|
2919
|
+
});
|
|
2920
|
+
}
|
|
2921
|
+
});
|
|
2922
|
+
const draftStreamingEnabled = streaming !== "off";
|
|
2923
|
+
const quietDraftStreaming = streaming === "quiet" || streaming === "progress";
|
|
2924
|
+
const progressDraftStreaming = streaming === "progress";
|
|
2925
|
+
const draftReplyToId = replyToMode !== "off" && !threadTarget ? messageId : void 0;
|
|
2926
|
+
const draftStream = draftStreamingEnabled ? await loadMatrixDraftStream().then(({ createMatrixDraftStream }) => createMatrixDraftStream({
|
|
2927
|
+
roomId,
|
|
2928
|
+
client,
|
|
2929
|
+
cfg,
|
|
2930
|
+
mode: quietDraftStreaming ? "quiet" : "partial",
|
|
2931
|
+
threadId: threadTarget,
|
|
2932
|
+
replyToId: draftReplyToId,
|
|
2933
|
+
preserveReplyId: replyToMode === "all",
|
|
2934
|
+
accountId: _route.accountId,
|
|
2935
|
+
log: logVerboseMessage
|
|
2936
|
+
})) : void 0;
|
|
2937
|
+
draftStreamRef = draftStream;
|
|
2938
|
+
const shouldStreamPreviewToolProgress = Boolean(draftStream) && previewToolProgressEnabled;
|
|
2939
|
+
const shouldSuppressDefaultToolProgressMessages = Boolean(draftStream) && (shouldStreamPreviewToolProgress || params.streaming === "progress");
|
|
2940
|
+
let currentDraftMessageGeneration = 0;
|
|
2941
|
+
let currentDraftBlockOffset = 0;
|
|
2942
|
+
let latestDraftFullText = "";
|
|
2943
|
+
const pendingDraftBoundaries = [];
|
|
2944
|
+
const latestQueuedDraftBoundaryOffsets = /* @__PURE__ */ new Map();
|
|
2945
|
+
let currentDraftReplyToId = draftReplyToId;
|
|
2946
|
+
let previewToolProgressSuppressed = false;
|
|
2947
|
+
let previewToolProgressLines = [];
|
|
2948
|
+
const progressConfigEntry = params.accountConfig ?? cfg.channels?.matrix;
|
|
2949
|
+
const progressSeed = `${_route.accountId}:${roomId}`;
|
|
2950
|
+
const renderProgressDraft = () => {
|
|
2951
|
+
if (!draftStream || !progressDraftStreaming) return;
|
|
2952
|
+
const previewText = formatChannelProgressDraftText({
|
|
2953
|
+
entry: progressConfigEntry,
|
|
2954
|
+
lines: previewToolProgressLines,
|
|
2955
|
+
seed: progressSeed,
|
|
2956
|
+
formatLine: formatMatrixToolProgressMarkdownCode,
|
|
2957
|
+
bullet: "-"
|
|
2958
|
+
});
|
|
2959
|
+
if (!previewText) return;
|
|
2960
|
+
draftStream.update(previewText);
|
|
2961
|
+
};
|
|
2962
|
+
const progressDraftGate = createChannelProgressDraftGate({ onStart: renderProgressDraft });
|
|
2963
|
+
const pushPreviewToolProgress = async (line, options) => {
|
|
2964
|
+
if (!draftStream) return;
|
|
2965
|
+
if (options?.toolName !== void 0 && !isChannelProgressDraftWorkToolName(options.toolName)) return;
|
|
2966
|
+
const normalized = normalizeChannelProgressDraftLineIdentity(line);
|
|
2967
|
+
const progressLine = typeof line === "object" && line !== void 0 ? line : normalized;
|
|
2968
|
+
if (!progressDraftStreaming) {
|
|
2969
|
+
if (!shouldStreamPreviewToolProgress || previewToolProgressSuppressed || !normalized) return;
|
|
2970
|
+
const nextLines = mergeChannelProgressDraftLine(previewToolProgressLines, progressLine, { maxLines: resolveChannelProgressDraftMaxLines(progressConfigEntry) });
|
|
2971
|
+
if (nextLines === previewToolProgressLines) return;
|
|
2972
|
+
previewToolProgressLines = nextLines;
|
|
2973
|
+
draftStream.update(formatChannelProgressDraftText({
|
|
2974
|
+
entry: progressConfigEntry,
|
|
2975
|
+
lines: previewToolProgressLines,
|
|
2976
|
+
seed: progressSeed,
|
|
2977
|
+
formatLine: formatMatrixToolProgressMarkdownCode,
|
|
2978
|
+
bullet: "-"
|
|
2979
|
+
}));
|
|
2980
|
+
return;
|
|
2981
|
+
}
|
|
2982
|
+
if (shouldStreamPreviewToolProgress && !previewToolProgressSuppressed && normalized) previewToolProgressLines = mergeChannelProgressDraftLine(previewToolProgressLines, progressLine, { maxLines: resolveChannelProgressDraftMaxLines(progressConfigEntry) });
|
|
2983
|
+
const alreadyStarted = progressDraftGate.hasStarted;
|
|
2984
|
+
await progressDraftGate.noteWork();
|
|
2985
|
+
if (alreadyStarted && progressDraftGate.hasStarted) renderProgressDraft();
|
|
2986
|
+
};
|
|
2987
|
+
const suppressPreviewToolProgressForAnswerText = (text) => {
|
|
2988
|
+
if (!text?.trim()) return;
|
|
2989
|
+
previewToolProgressSuppressed = true;
|
|
2990
|
+
previewToolProgressLines = [];
|
|
2991
|
+
};
|
|
2992
|
+
const resetPreviewToolProgress = () => {
|
|
2993
|
+
previewToolProgressSuppressed = false;
|
|
2994
|
+
previewToolProgressLines = [];
|
|
2995
|
+
};
|
|
2996
|
+
const buildPreviewToolProgressReplyOptions = () => {
|
|
2997
|
+
if (!shouldSuppressDefaultToolProgressMessages) return {};
|
|
2998
|
+
const options = { suppressDefaultToolProgressMessages: true };
|
|
2999
|
+
if (!shouldStreamPreviewToolProgress) return options;
|
|
3000
|
+
return {
|
|
3001
|
+
...options,
|
|
3002
|
+
onToolStart: async (payload) => {
|
|
3003
|
+
const toolName = payload.name?.trim();
|
|
3004
|
+
await pushPreviewToolProgress(formatChannelProgressDraftLineForEntry(progressConfigEntry, {
|
|
3005
|
+
event: "tool",
|
|
3006
|
+
name: toolName,
|
|
3007
|
+
phase: payload.phase,
|
|
3008
|
+
args: payload.args
|
|
3009
|
+
}, payload.detailMode ? { detailMode: payload.detailMode } : void 0), { toolName });
|
|
3010
|
+
},
|
|
3011
|
+
onItemEvent: async (payload) => {
|
|
3012
|
+
await pushPreviewToolProgress(buildChannelProgressDraftLineForEntry(progressConfigEntry, {
|
|
3013
|
+
event: "item",
|
|
3014
|
+
itemId: payload.itemId,
|
|
3015
|
+
itemKind: payload.kind,
|
|
3016
|
+
title: payload.title,
|
|
3017
|
+
name: payload.name,
|
|
3018
|
+
phase: payload.phase,
|
|
3019
|
+
status: payload.status,
|
|
3020
|
+
summary: payload.summary,
|
|
3021
|
+
progressText: payload.progressText,
|
|
3022
|
+
meta: payload.meta
|
|
3023
|
+
}));
|
|
3024
|
+
},
|
|
3025
|
+
onPlanUpdate: async (payload) => {
|
|
3026
|
+
if (payload.phase !== "update") return;
|
|
3027
|
+
await pushPreviewToolProgress(formatChannelProgressDraftLine({
|
|
3028
|
+
event: "plan",
|
|
3029
|
+
phase: payload.phase,
|
|
3030
|
+
title: payload.title,
|
|
3031
|
+
explanation: payload.explanation,
|
|
3032
|
+
steps: payload.steps
|
|
3033
|
+
}));
|
|
3034
|
+
},
|
|
3035
|
+
onApprovalEvent: async (payload) => {
|
|
3036
|
+
if (payload.phase !== "requested") return;
|
|
3037
|
+
await pushPreviewToolProgress(formatChannelProgressDraftLine({
|
|
3038
|
+
event: "approval",
|
|
3039
|
+
phase: payload.phase,
|
|
3040
|
+
title: payload.title,
|
|
3041
|
+
command: payload.command,
|
|
3042
|
+
reason: payload.reason,
|
|
3043
|
+
message: payload.message
|
|
3044
|
+
}));
|
|
3045
|
+
},
|
|
3046
|
+
onCommandOutput: async (payload) => {
|
|
3047
|
+
if (payload.phase !== "end") return;
|
|
3048
|
+
await pushPreviewToolProgress(formatChannelProgressDraftLine({
|
|
3049
|
+
event: "command-output",
|
|
3050
|
+
phase: payload.phase,
|
|
3051
|
+
title: payload.title,
|
|
3052
|
+
name: payload.name,
|
|
3053
|
+
status: payload.status,
|
|
3054
|
+
exitCode: payload.exitCode
|
|
3055
|
+
}));
|
|
3056
|
+
},
|
|
3057
|
+
onPatchSummary: async (payload) => {
|
|
3058
|
+
if (payload.phase !== "end") return;
|
|
3059
|
+
await pushPreviewToolProgress(formatChannelProgressDraftLine({
|
|
3060
|
+
event: "patch",
|
|
3061
|
+
phase: payload.phase,
|
|
3062
|
+
title: payload.title,
|
|
3063
|
+
name: payload.name,
|
|
3064
|
+
added: payload.added,
|
|
3065
|
+
modified: payload.modified,
|
|
3066
|
+
deleted: payload.deleted,
|
|
3067
|
+
summary: payload.summary
|
|
3068
|
+
}));
|
|
3069
|
+
}
|
|
3070
|
+
};
|
|
3071
|
+
};
|
|
3072
|
+
const getDisplayableDraftText = () => {
|
|
3073
|
+
const nextDraftBoundaryOffset = pendingDraftBoundaries.find((boundary) => boundary.messageGeneration === currentDraftMessageGeneration)?.endOffset;
|
|
3074
|
+
if (nextDraftBoundaryOffset === void 0) return latestDraftFullText.slice(currentDraftBlockOffset);
|
|
3075
|
+
return latestDraftFullText.slice(currentDraftBlockOffset, nextDraftBoundaryOffset);
|
|
3076
|
+
};
|
|
3077
|
+
const updateDraftFromLatestFullText = () => {
|
|
3078
|
+
const blockText = getDisplayableDraftText();
|
|
3079
|
+
if (blockText) draftStream?.update(blockText);
|
|
3080
|
+
};
|
|
3081
|
+
const queueDraftBlockBoundary = (payload, context) => {
|
|
3082
|
+
const payloadTextLength = payload.text?.length ?? 0;
|
|
3083
|
+
const messageGeneration = context?.assistantMessageIndex ?? currentDraftMessageGeneration;
|
|
3084
|
+
const nextDraftBoundaryOffset = (latestQueuedDraftBoundaryOffsets.get(messageGeneration) ?? 0) + payloadTextLength;
|
|
3085
|
+
latestQueuedDraftBoundaryOffsets.set(messageGeneration, nextDraftBoundaryOffset);
|
|
3086
|
+
pendingDraftBoundaries.push({
|
|
3087
|
+
messageGeneration,
|
|
3088
|
+
endOffset: nextDraftBoundaryOffset
|
|
3089
|
+
});
|
|
3090
|
+
};
|
|
3091
|
+
const advanceDraftBlockBoundary = (options) => {
|
|
3092
|
+
const completedBoundary = pendingDraftBoundaries.shift();
|
|
3093
|
+
if (completedBoundary) {
|
|
3094
|
+
if (!pendingDraftBoundaries.some((entry) => entry.messageGeneration === completedBoundary.messageGeneration)) latestQueuedDraftBoundaryOffsets.delete(completedBoundary.messageGeneration);
|
|
3095
|
+
if (completedBoundary.messageGeneration === currentDraftMessageGeneration) currentDraftBlockOffset = completedBoundary.endOffset;
|
|
3096
|
+
return;
|
|
3097
|
+
}
|
|
3098
|
+
if (options?.fallbackToLatestEnd) currentDraftBlockOffset = latestDraftFullText.length;
|
|
3099
|
+
};
|
|
3100
|
+
const resetDraftBlockOffsets = () => {
|
|
3101
|
+
currentDraftMessageGeneration += 1;
|
|
3102
|
+
currentDraftBlockOffset = 0;
|
|
3103
|
+
latestDraftFullText = "";
|
|
3104
|
+
};
|
|
3105
|
+
const { dispatcher, replyOptions, markDispatchIdle, markRunComplete } = core.channel.reply.createReplyDispatcherWithTyping({
|
|
3106
|
+
...prefixOptions,
|
|
3107
|
+
humanDelay: core.channel.reply.resolveHumanDelayConfig(cfg, _route.agentId),
|
|
3108
|
+
deliver: async (payload, info) => {
|
|
3109
|
+
if (draftStream && info.kind !== "tool" && !payload.isCompactionNotice) {
|
|
3110
|
+
const hasMedia = Boolean(payload.mediaUrl) || (payload.mediaUrls?.length ?? 0) > 0;
|
|
3111
|
+
const ttsSupplement = getReplyPayloadTtsSupplement(payload);
|
|
3112
|
+
const fallbackPayload = ttsSupplement && ttsSupplement.visibleTextAlreadyDelivered !== true && !payload.text?.trim() ? {
|
|
3113
|
+
...payload,
|
|
3114
|
+
text: ttsSupplement.spokenText
|
|
3115
|
+
} : payload;
|
|
3116
|
+
if (draftConsumed) {
|
|
3117
|
+
await draftStream.discardPending();
|
|
3118
|
+
await deliverMatrixReplies({
|
|
3119
|
+
cfg,
|
|
3120
|
+
replies: [fallbackPayload],
|
|
3121
|
+
roomId,
|
|
3122
|
+
client,
|
|
3123
|
+
runtime,
|
|
3124
|
+
textLimit,
|
|
3125
|
+
replyToMode,
|
|
3126
|
+
threadId: threadTarget,
|
|
3127
|
+
accountId: _route.accountId,
|
|
3128
|
+
mediaLocalRoots,
|
|
3129
|
+
tableMode
|
|
3130
|
+
});
|
|
3131
|
+
return;
|
|
3132
|
+
}
|
|
3133
|
+
const payloadReplyToId = normalizeOptionalString(payload.replyToId);
|
|
3134
|
+
const payloadReplyMismatch = replyToMode !== "off" && !threadTarget && payloadReplyToId !== currentDraftReplyToId;
|
|
3135
|
+
let mustDeliverFinalNormally = draftStream.mustDeliverFinalNormally();
|
|
3136
|
+
if (Boolean(payload.text?.trim()) && !payload.isError && !payloadReplyMismatch && !mustDeliverFinalNormally) {
|
|
3137
|
+
await draftStream.stop();
|
|
3138
|
+
mustDeliverFinalNormally = draftStream.mustDeliverFinalNormally();
|
|
3139
|
+
} else await draftStream.discardPending();
|
|
3140
|
+
const draftEventId = draftStream.eventId();
|
|
3141
|
+
if (draftEventId && payload.text && !payload.isError && !hasMedia && !payloadReplyMismatch && !mustDeliverFinalNormally) {
|
|
3142
|
+
const finalPreviewText = payload.text;
|
|
3143
|
+
await deliverWithFinalizableLivePreviewAdapter({
|
|
3144
|
+
kind: "final",
|
|
3145
|
+
payload,
|
|
3146
|
+
adapter: defineFinalizableLivePreviewAdapter({
|
|
3147
|
+
draft: {
|
|
3148
|
+
flush: async () => {},
|
|
3149
|
+
clear: async () => {},
|
|
3150
|
+
discardPending: async () => {},
|
|
3151
|
+
id: () => draftEventId
|
|
3152
|
+
},
|
|
3153
|
+
buildFinalEdit: () => ({
|
|
3154
|
+
text: finalPreviewText,
|
|
3155
|
+
finalizeLive: !(quietDraftStreaming || !draftStream.matchesPreparedText(finalPreviewText)),
|
|
3156
|
+
...quietDraftStreaming ? { extraContent: buildMatrixFinalizedPreviewContent() } : {}
|
|
3157
|
+
}),
|
|
3158
|
+
editFinal: async (_draftEventId, edit) => {
|
|
3159
|
+
if (edit.finalizeLive) {
|
|
3160
|
+
if (!await draftStream.finalizeLive()) throw new Error("Matrix draft live finalize failed");
|
|
3161
|
+
return;
|
|
3162
|
+
}
|
|
3163
|
+
const { editMessageMatrix } = await loadMatrixSendModule();
|
|
3164
|
+
await editMessageMatrix(roomId, _draftEventId, edit.text, {
|
|
3165
|
+
client,
|
|
3166
|
+
cfg,
|
|
3167
|
+
threadId: threadTarget,
|
|
3168
|
+
accountId: _route.accountId,
|
|
3169
|
+
extraContent: edit.extraContent
|
|
3170
|
+
});
|
|
3171
|
+
},
|
|
3172
|
+
createPreviewReceipt: (id) => createPreviewMessageReceipt({
|
|
3173
|
+
id,
|
|
3174
|
+
...threadTarget ? { threadId: threadTarget } : {},
|
|
3175
|
+
...currentDraftReplyToId ? { replyToId: currentDraftReplyToId } : {}
|
|
3176
|
+
}),
|
|
3177
|
+
logPreviewEditFailure: (err) => {
|
|
3178
|
+
logVerboseMessage(`matrix: preview final edit failed: ${String(err)}`);
|
|
3179
|
+
}
|
|
3180
|
+
}),
|
|
3181
|
+
deliverNormally: async () => {
|
|
3182
|
+
await redactMatrixDraftEvent(client, roomId, draftEventId);
|
|
3183
|
+
await deliverMatrixReplies({
|
|
3184
|
+
cfg,
|
|
3185
|
+
replies: [fallbackPayload],
|
|
3186
|
+
roomId,
|
|
3187
|
+
client,
|
|
3188
|
+
runtime,
|
|
3189
|
+
textLimit,
|
|
3190
|
+
replyToMode,
|
|
3191
|
+
threadId: threadTarget,
|
|
3192
|
+
accountId: _route.accountId,
|
|
3193
|
+
mediaLocalRoots,
|
|
3194
|
+
tableMode
|
|
3195
|
+
});
|
|
3196
|
+
}
|
|
3197
|
+
});
|
|
3198
|
+
draftConsumed = true;
|
|
3199
|
+
} else if (draftEventId && hasMedia && !payloadReplyMismatch) {
|
|
3200
|
+
let textEditOk = !mustDeliverFinalNormally;
|
|
3201
|
+
const payloadText = payload.text ?? ttsSupplement?.spokenText;
|
|
3202
|
+
const payloadTextMatchesDraft = typeof payloadText === "string" && draftStream.matchesPreparedText(payloadText);
|
|
3203
|
+
const reusesDraftTextUnchanged = typeof payloadText === "string" && Boolean(payloadText.trim()) && payloadTextMatchesDraft;
|
|
3204
|
+
if (textEditOk && payloadText && (quietDraftStreaming || typeof payloadText === "string" && !payloadTextMatchesDraft)) {
|
|
3205
|
+
const { editMessageMatrix } = await loadMatrixSendModule();
|
|
3206
|
+
textEditOk = await editMessageMatrix(roomId, draftEventId, payloadText, {
|
|
3207
|
+
client,
|
|
3208
|
+
cfg,
|
|
3209
|
+
threadId: threadTarget,
|
|
3210
|
+
accountId: _route.accountId,
|
|
3211
|
+
extraContent: quietDraftStreaming ? buildMatrixFinalizedPreviewContent() : void 0
|
|
3212
|
+
}).then(() => true, () => false);
|
|
3213
|
+
} else if (textEditOk && reusesDraftTextUnchanged) textEditOk = await draftStream.finalizeLive();
|
|
3214
|
+
const reusesDraftAsFinalText = Boolean(payloadText?.trim()) && textEditOk;
|
|
3215
|
+
if (!reusesDraftAsFinalText) await redactMatrixDraftEvent(client, roomId, draftEventId);
|
|
3216
|
+
await deliverMatrixReplies({
|
|
3217
|
+
cfg,
|
|
3218
|
+
replies: [ttsSupplement && reusesDraftAsFinalText ? buildTtsSupplementMediaPayload(payload) : {
|
|
3219
|
+
...payload,
|
|
3220
|
+
text: reusesDraftAsFinalText ? void 0 : payload.text ?? (ttsSupplement?.visibleTextAlreadyDelivered === true ? void 0 : ttsSupplement?.spokenText)
|
|
3221
|
+
}],
|
|
3222
|
+
roomId,
|
|
3223
|
+
client,
|
|
3224
|
+
runtime,
|
|
3225
|
+
textLimit,
|
|
3226
|
+
replyToMode,
|
|
3227
|
+
threadId: threadTarget,
|
|
3228
|
+
accountId: _route.accountId,
|
|
3229
|
+
mediaLocalRoots,
|
|
3230
|
+
tableMode
|
|
3231
|
+
});
|
|
3232
|
+
draftConsumed = true;
|
|
3233
|
+
} else {
|
|
3234
|
+
const draftRedacted = Boolean(draftEventId) && (payload.isError || payloadReplyMismatch || mustDeliverFinalNormally);
|
|
3235
|
+
if (draftRedacted && draftEventId) await redactMatrixDraftEvent(client, roomId, draftEventId);
|
|
3236
|
+
const deliveredFallback = await deliverMatrixReplies({
|
|
3237
|
+
cfg,
|
|
3238
|
+
replies: [fallbackPayload],
|
|
3239
|
+
roomId,
|
|
3240
|
+
client,
|
|
3241
|
+
runtime,
|
|
3242
|
+
textLimit,
|
|
3243
|
+
replyToMode,
|
|
3244
|
+
threadId: threadTarget,
|
|
3245
|
+
accountId: _route.accountId,
|
|
3246
|
+
mediaLocalRoots,
|
|
3247
|
+
tableMode
|
|
3248
|
+
});
|
|
3249
|
+
if (draftRedacted || deliveredFallback) draftConsumed = true;
|
|
3250
|
+
}
|
|
3251
|
+
if (info.kind === "block") {
|
|
3252
|
+
draftConsumed = false;
|
|
3253
|
+
advanceDraftBlockBoundary({ fallbackToLatestEnd: true });
|
|
3254
|
+
draftStream.reset();
|
|
3255
|
+
currentDraftReplyToId = replyToMode === "all" ? draftReplyToId : void 0;
|
|
3256
|
+
updateDraftFromLatestFullText();
|
|
3257
|
+
const { sendTypingMatrix } = await loadMatrixSendModule();
|
|
3258
|
+
await sendTypingMatrix(roomId, true, void 0, client).catch(() => {});
|
|
3259
|
+
}
|
|
3260
|
+
} else await deliverMatrixReplies({
|
|
3261
|
+
cfg,
|
|
3262
|
+
replies: [payload],
|
|
3263
|
+
roomId,
|
|
3264
|
+
client,
|
|
3265
|
+
runtime,
|
|
3266
|
+
textLimit,
|
|
3267
|
+
replyToMode,
|
|
3268
|
+
threadId: threadTarget,
|
|
3269
|
+
accountId: _route.accountId,
|
|
3270
|
+
mediaLocalRoots,
|
|
3271
|
+
tableMode
|
|
3272
|
+
});
|
|
3273
|
+
},
|
|
3274
|
+
onError: (err, info) => {
|
|
3275
|
+
if (err instanceof MatrixRetryableInboundError) retryableReplyDeliveryFailed = true;
|
|
3276
|
+
if (info.kind === "final") finalReplyDeliveryFailed = true;
|
|
3277
|
+
else nonFinalReplyDeliveryFailed = true;
|
|
3278
|
+
if (info.kind === "block") advanceDraftBlockBoundary({ fallbackToLatestEnd: true });
|
|
3279
|
+
runtime.error?.(`matrix ${info.kind} reply failed: ${String(err)}`);
|
|
3280
|
+
},
|
|
3281
|
+
onReplyStart: typingCallbacks.onReplyStart,
|
|
3282
|
+
onIdle: typingCallbacks.onIdle
|
|
3283
|
+
});
|
|
3284
|
+
const pinnedMainDmOwner = isDirectMessage ? await (async () => {
|
|
3285
|
+
const livePinnedCfg = core.config.current();
|
|
3286
|
+
const livePinnedDmAllowFrom = await resolveCachedLiveAllowlist({
|
|
3287
|
+
cfg: livePinnedCfg,
|
|
3288
|
+
entries: resolveMatrixAccountAllowlistConfig({
|
|
3289
|
+
cfg: livePinnedCfg,
|
|
3290
|
+
accountId
|
|
3291
|
+
}).dmAllowFrom,
|
|
3292
|
+
startupResolvedEntries: allowFromResolvedEntries,
|
|
3293
|
+
cache: liveDmAllowlistCache,
|
|
3294
|
+
updateCache: (next) => {
|
|
3295
|
+
liveDmAllowlistCache = next;
|
|
3296
|
+
}
|
|
3297
|
+
});
|
|
3298
|
+
return resolvePinnedMainDmOwnerFromAllowlist({
|
|
3299
|
+
dmScope: livePinnedCfg.session?.dmScope,
|
|
3300
|
+
allowFrom: livePinnedDmAllowFrom,
|
|
3301
|
+
normalizeEntry: normalizeMatrixUserId
|
|
3302
|
+
});
|
|
3303
|
+
})() : null;
|
|
3304
|
+
const inboundLastRouteSessionKey = resolveInboundLastRouteSessionKey({
|
|
3305
|
+
route: _route,
|
|
3306
|
+
sessionKey: _route.sessionKey
|
|
3307
|
+
});
|
|
3308
|
+
const turnResult = await core.channel.turn.run({
|
|
3309
|
+
channel: "matrix",
|
|
3310
|
+
accountId: _route.accountId,
|
|
3311
|
+
raw: event,
|
|
3312
|
+
adapter: {
|
|
3313
|
+
ingest: () => ({
|
|
3314
|
+
id: messageId,
|
|
3315
|
+
rawText: bodyText,
|
|
3316
|
+
textForAgent: ctxPayload.BodyForAgent,
|
|
3317
|
+
textForCommands: ctxPayload.CommandBody,
|
|
3318
|
+
raw: event
|
|
3319
|
+
}),
|
|
3320
|
+
resolveTurn: () => ({
|
|
3321
|
+
channel: "matrix",
|
|
3322
|
+
accountId: _route.accountId,
|
|
3323
|
+
routeSessionKey: _route.sessionKey,
|
|
3324
|
+
storePath,
|
|
3325
|
+
ctxPayload,
|
|
3326
|
+
recordInboundSession: core.channel.session.recordInboundSession,
|
|
3327
|
+
botLoopProtection,
|
|
3328
|
+
record: {
|
|
3329
|
+
updateLastRoute: isDirectMessage ? {
|
|
3330
|
+
sessionKey: inboundLastRouteSessionKey,
|
|
3331
|
+
channel: "matrix",
|
|
3332
|
+
to: `room:${roomId}`,
|
|
3333
|
+
accountId: _route.accountId,
|
|
3334
|
+
mainDmOwnerPin: inboundLastRouteSessionKey === _route.mainSessionKey && pinnedMainDmOwner ? {
|
|
3335
|
+
ownerRecipient: pinnedMainDmOwner,
|
|
3336
|
+
senderRecipient: normalizeMatrixUserId(senderId),
|
|
3337
|
+
onSkip: ({ ownerRecipient, senderRecipient }) => {
|
|
3338
|
+
logVerboseMessage(`matrix: skip main-session last route for ${senderRecipient} (pinned owner ${ownerRecipient})`);
|
|
3339
|
+
}
|
|
3340
|
+
} : void 0
|
|
3341
|
+
} : void 0,
|
|
3342
|
+
onRecordError: (err) => {
|
|
3343
|
+
logger.warn("failed updating session meta", {
|
|
3344
|
+
error: String(err),
|
|
3345
|
+
storePath,
|
|
3346
|
+
sessionKey: ctxPayload.SessionKey ?? _route.sessionKey
|
|
3347
|
+
});
|
|
3348
|
+
}
|
|
3349
|
+
},
|
|
3350
|
+
onPreDispatchFailure: () => core.channel.reply.settleReplyDispatcher({
|
|
3351
|
+
dispatcher,
|
|
3352
|
+
onSettled: () => {
|
|
3353
|
+
markRunComplete();
|
|
3354
|
+
markDispatchIdle();
|
|
3355
|
+
}
|
|
3356
|
+
}),
|
|
3357
|
+
runDispatch: async () => {
|
|
3358
|
+
if (sharedDmContextNotice && markTrackedRoomIfFirst(sharedDmContextNoticeRooms, roomId)) client.sendMessage(roomId, {
|
|
3359
|
+
msgtype: "m.notice",
|
|
3360
|
+
body: sharedDmContextNotice
|
|
3361
|
+
}).catch((err) => {
|
|
3362
|
+
logVerboseMessage(`matrix: failed sending shared DM session notice room=${roomId}: ${String(err)}`);
|
|
3363
|
+
});
|
|
3364
|
+
return await core.channel.reply.withReplyDispatcher({
|
|
3365
|
+
dispatcher,
|
|
3366
|
+
onSettled: () => {
|
|
3367
|
+
markDispatchIdle();
|
|
3368
|
+
},
|
|
3369
|
+
run: async () => {
|
|
3370
|
+
try {
|
|
3371
|
+
return await core.channel.reply.dispatchReplyFromConfig({
|
|
3372
|
+
ctx: ctxPayload,
|
|
3373
|
+
cfg,
|
|
3374
|
+
dispatcher,
|
|
3375
|
+
replyOptions: {
|
|
3376
|
+
...replyOptions,
|
|
3377
|
+
skillFilter: roomConfig?.skills,
|
|
3378
|
+
disableBlockStreaming: !blockStreamingEnabled,
|
|
3379
|
+
onPartialReply: draftStream ? (payload) => {
|
|
3380
|
+
if (progressDraftStreaming) return;
|
|
3381
|
+
latestDraftFullText = payload.text ?? "";
|
|
3382
|
+
suppressPreviewToolProgressForAnswerText(latestDraftFullText);
|
|
3383
|
+
updateDraftFromLatestFullText();
|
|
3384
|
+
} : void 0,
|
|
3385
|
+
onBlockReplyQueued: draftStream ? (payload, context) => {
|
|
3386
|
+
if (payload.isCompactionNotice === true) return;
|
|
3387
|
+
queueDraftBlockBoundary(payload, context);
|
|
3388
|
+
} : void 0,
|
|
3389
|
+
onAssistantMessageStart: draftStream ? () => {
|
|
3390
|
+
resetDraftBlockOffsets();
|
|
3391
|
+
resetPreviewToolProgress();
|
|
3392
|
+
} : void 0,
|
|
3393
|
+
...buildPreviewToolProgressReplyOptions(),
|
|
3394
|
+
onModelSelected
|
|
3395
|
+
}
|
|
3396
|
+
});
|
|
3397
|
+
} finally {
|
|
3398
|
+
progressDraftGate.cancel();
|
|
3399
|
+
markRunComplete();
|
|
3400
|
+
}
|
|
3401
|
+
}
|
|
3402
|
+
});
|
|
3403
|
+
}
|
|
3404
|
+
})
|
|
3405
|
+
}
|
|
3406
|
+
});
|
|
3407
|
+
if (!turnResult.dispatched) {
|
|
3408
|
+
if (turnResult.admission.kind === "drop" && turnResult.admission.reason === "bot-loop-protection") await commitInboundEventIfClaimed();
|
|
3409
|
+
return;
|
|
3410
|
+
}
|
|
3411
|
+
const { dispatchResult } = turnResult;
|
|
3412
|
+
const { queuedFinal, counts } = dispatchResult;
|
|
3413
|
+
if (finalReplyDeliveryFailed) {
|
|
3414
|
+
if (retryableReplyDeliveryFailed) {
|
|
3415
|
+
logVerboseMessage(`matrix: final reply delivery failed room=${roomId} id=${messageId}; leaving event uncommitted`);
|
|
3416
|
+
return;
|
|
3417
|
+
}
|
|
3418
|
+
logVerboseMessage(`matrix: final reply delivery failed room=${roomId} id=${messageId}; keeping replay committed`);
|
|
3419
|
+
await commitInboundEventIfClaimed();
|
|
3420
|
+
return;
|
|
3421
|
+
}
|
|
3422
|
+
if (!queuedFinal && nonFinalReplyDeliveryFailed) {
|
|
3423
|
+
if (retryableReplyDeliveryFailed) {
|
|
3424
|
+
logVerboseMessage(`matrix: non-final reply delivery failed room=${roomId} id=${messageId}; leaving event uncommitted`);
|
|
3425
|
+
return;
|
|
3426
|
+
}
|
|
3427
|
+
logVerboseMessage(`matrix: non-final reply delivery failed room=${roomId} id=${messageId}; keeping replay committed`);
|
|
3428
|
+
await commitInboundEventIfClaimed();
|
|
3429
|
+
return;
|
|
3430
|
+
}
|
|
3431
|
+
if (isRoom && triggerSnapshot) roomHistoryTracker.consumeHistory(_route.agentId, roomId, triggerSnapshot, messageId);
|
|
3432
|
+
if (!hasFinalChannelTurnDispatch({
|
|
3433
|
+
queuedFinal,
|
|
3434
|
+
counts
|
|
3435
|
+
})) {
|
|
3436
|
+
await commitInboundEventIfClaimed();
|
|
3437
|
+
return;
|
|
3438
|
+
}
|
|
3439
|
+
const finalCount = counts.final;
|
|
3440
|
+
logVerboseMessage(`matrix: delivered ${finalCount} reply${finalCount === 1 ? "" : "ies"} to ${replyTarget}`);
|
|
3441
|
+
await commitInboundEventIfClaimed();
|
|
3442
|
+
} catch (err) {
|
|
3443
|
+
runtime.error?.(`matrix handler failed: ${String(err)}`);
|
|
3444
|
+
} finally {
|
|
3445
|
+
if (draftStreamRef) {
|
|
3446
|
+
const draftEventId = await draftStreamRef.stop().catch(() => void 0);
|
|
3447
|
+
if (draftEventId && !draftConsumed) await redactMatrixDraftEvent(client, roomId, draftEventId);
|
|
3448
|
+
}
|
|
3449
|
+
if (claimedInboundEvent && inboundDeduper && eventId) inboundDeduper.releaseEvent({
|
|
3450
|
+
roomId,
|
|
3451
|
+
eventId
|
|
3452
|
+
});
|
|
3453
|
+
}
|
|
3454
|
+
};
|
|
3455
|
+
}
|
|
3456
|
+
//#endregion
|
|
3457
|
+
//#region extensions/matrix/src/matrix/monitor/inbound-dedupe.ts
|
|
3458
|
+
const INBOUND_DEDUPE_FILENAME = "inbound-dedupe.json";
|
|
3459
|
+
const STORE_VERSION = 1;
|
|
3460
|
+
const DEFAULT_MAX_ENTRIES = 2e4;
|
|
3461
|
+
const DEFAULT_TTL_MS = 720 * 60 * 60 * 1e3;
|
|
3462
|
+
const PERSIST_DEBOUNCE_MS = 250;
|
|
3463
|
+
function normalizeEventPart(value) {
|
|
3464
|
+
return value.trim();
|
|
3465
|
+
}
|
|
3466
|
+
function buildEventKey(params) {
|
|
3467
|
+
const roomId = normalizeEventPart(params.roomId);
|
|
3468
|
+
const eventId = normalizeEventPart(params.eventId);
|
|
3469
|
+
return roomId && eventId ? `${roomId}|${eventId}` : "";
|
|
3470
|
+
}
|
|
3471
|
+
function resolveInboundDedupeStatePath(params) {
|
|
3472
|
+
return resolveMatrixStateFilePath({
|
|
3473
|
+
auth: params.auth,
|
|
3474
|
+
env: params.env,
|
|
3475
|
+
stateDir: params.stateDir,
|
|
3476
|
+
filename: INBOUND_DEDUPE_FILENAME
|
|
3477
|
+
});
|
|
3478
|
+
}
|
|
3479
|
+
function normalizeTimestamp(raw) {
|
|
3480
|
+
if (typeof raw !== "number" || !Number.isFinite(raw)) return null;
|
|
3481
|
+
return Math.max(0, Math.floor(raw));
|
|
3482
|
+
}
|
|
3483
|
+
function pruneSeenEvents(params) {
|
|
3484
|
+
const { seen, ttlMs, maxEntries, nowMs } = params;
|
|
3485
|
+
if (ttlMs > 0) {
|
|
3486
|
+
const cutoff = nowMs - ttlMs;
|
|
3487
|
+
for (const [key, ts] of seen) if (ts < cutoff) seen.delete(key);
|
|
3488
|
+
}
|
|
3489
|
+
const max = Math.max(0, Math.floor(maxEntries));
|
|
3490
|
+
if (max <= 0) {
|
|
3491
|
+
seen.clear();
|
|
3492
|
+
return;
|
|
3493
|
+
}
|
|
3494
|
+
while (seen.size > max) {
|
|
3495
|
+
const oldestKey = seen.keys().next().value;
|
|
3496
|
+
if (typeof oldestKey !== "string") break;
|
|
3497
|
+
seen.delete(oldestKey);
|
|
3498
|
+
}
|
|
3499
|
+
}
|
|
3500
|
+
function toStoredState(params) {
|
|
3501
|
+
pruneSeenEvents(params);
|
|
3502
|
+
return {
|
|
3503
|
+
version: STORE_VERSION,
|
|
3504
|
+
entries: Array.from(params.seen.entries()).map(([key, ts]) => ({
|
|
3505
|
+
key,
|
|
3506
|
+
ts
|
|
3507
|
+
}))
|
|
3508
|
+
};
|
|
3509
|
+
}
|
|
3510
|
+
async function readStoredState(storagePath) {
|
|
3511
|
+
const { value } = await readJsonFileWithFallback(storagePath, null);
|
|
3512
|
+
if (value?.version !== STORE_VERSION || !Array.isArray(value.entries)) return null;
|
|
3513
|
+
return value;
|
|
3514
|
+
}
|
|
3515
|
+
async function createMatrixInboundEventDeduper(params) {
|
|
3516
|
+
const nowMs = params.nowMs ?? (() => Date.now());
|
|
3517
|
+
const ttlMs = typeof params.ttlMs === "number" && Number.isFinite(params.ttlMs) ? Math.max(0, Math.floor(params.ttlMs)) : DEFAULT_TTL_MS;
|
|
3518
|
+
const maxEntries = typeof params.maxEntries === "number" && Number.isFinite(params.maxEntries) ? Math.max(0, Math.floor(params.maxEntries)) : DEFAULT_MAX_ENTRIES;
|
|
3519
|
+
const storagePath = params.storagePath ?? resolveInboundDedupeStatePath({
|
|
3520
|
+
auth: params.auth,
|
|
3521
|
+
env: params.env,
|
|
3522
|
+
stateDir: params.stateDir
|
|
3523
|
+
});
|
|
3524
|
+
const seen = /* @__PURE__ */ new Map();
|
|
3525
|
+
const pending = /* @__PURE__ */ new Set();
|
|
3526
|
+
const persistLock = createAsyncLock();
|
|
3527
|
+
try {
|
|
3528
|
+
const stored = await readStoredState(storagePath);
|
|
3529
|
+
for (const entry of stored?.entries ?? []) {
|
|
3530
|
+
if (!entry || typeof entry.key !== "string") continue;
|
|
3531
|
+
const key = entry.key.trim();
|
|
3532
|
+
const ts = normalizeTimestamp(entry.ts);
|
|
3533
|
+
if (!key || ts === null) continue;
|
|
3534
|
+
seen.set(key, ts);
|
|
3535
|
+
}
|
|
3536
|
+
pruneSeenEvents({
|
|
3537
|
+
seen,
|
|
3538
|
+
ttlMs,
|
|
3539
|
+
maxEntries,
|
|
3540
|
+
nowMs: nowMs()
|
|
3541
|
+
});
|
|
3542
|
+
} catch (err) {
|
|
3543
|
+
LogService.warn("MatrixInboundDedupe", "Failed loading Matrix inbound dedupe store:", err);
|
|
3544
|
+
}
|
|
3545
|
+
let dirty = false;
|
|
3546
|
+
let persistTimer = null;
|
|
3547
|
+
let persistPromise = null;
|
|
3548
|
+
const persist = async () => {
|
|
3549
|
+
dirty = false;
|
|
3550
|
+
const payload = toStoredState({
|
|
3551
|
+
seen,
|
|
3552
|
+
ttlMs,
|
|
3553
|
+
maxEntries,
|
|
3554
|
+
nowMs: nowMs()
|
|
3555
|
+
});
|
|
3556
|
+
try {
|
|
3557
|
+
await persistLock(async () => {
|
|
3558
|
+
await writeJsonFileAtomically(storagePath, payload);
|
|
3559
|
+
});
|
|
3560
|
+
} catch (err) {
|
|
3561
|
+
dirty = true;
|
|
3562
|
+
throw err;
|
|
3563
|
+
}
|
|
3564
|
+
};
|
|
3565
|
+
const flush = async () => {
|
|
3566
|
+
if (persistTimer) {
|
|
3567
|
+
clearTimeout(persistTimer);
|
|
3568
|
+
persistTimer = null;
|
|
3569
|
+
}
|
|
3570
|
+
for (;;) {
|
|
3571
|
+
if (!dirty && !persistPromise) break;
|
|
3572
|
+
if (dirty && !persistPromise) persistPromise = persist().finally(() => {
|
|
3573
|
+
persistPromise = null;
|
|
3574
|
+
});
|
|
3575
|
+
await persistPromise;
|
|
3576
|
+
}
|
|
3577
|
+
};
|
|
3578
|
+
const schedulePersist = () => {
|
|
3579
|
+
dirty = true;
|
|
3580
|
+
if (persistTimer) return;
|
|
3581
|
+
persistTimer = setTimeout(() => {
|
|
3582
|
+
persistTimer = null;
|
|
3583
|
+
flush().catch((err) => {
|
|
3584
|
+
LogService.warn("MatrixInboundDedupe", "Failed persisting Matrix inbound dedupe store:", err);
|
|
3585
|
+
});
|
|
3586
|
+
}, PERSIST_DEBOUNCE_MS);
|
|
3587
|
+
persistTimer.unref?.();
|
|
3588
|
+
};
|
|
3589
|
+
return {
|
|
3590
|
+
claimEvent: ({ roomId, eventId }) => {
|
|
3591
|
+
const key = buildEventKey({
|
|
3592
|
+
roomId,
|
|
3593
|
+
eventId
|
|
3594
|
+
});
|
|
3595
|
+
if (!key) return true;
|
|
3596
|
+
pruneSeenEvents({
|
|
3597
|
+
seen,
|
|
3598
|
+
ttlMs,
|
|
3599
|
+
maxEntries,
|
|
3600
|
+
nowMs: nowMs()
|
|
3601
|
+
});
|
|
3602
|
+
if (seen.has(key) || pending.has(key)) return false;
|
|
3603
|
+
pending.add(key);
|
|
3604
|
+
return true;
|
|
3605
|
+
},
|
|
3606
|
+
commitEvent: async ({ roomId, eventId }) => {
|
|
3607
|
+
const key = buildEventKey({
|
|
3608
|
+
roomId,
|
|
3609
|
+
eventId
|
|
3610
|
+
});
|
|
3611
|
+
if (!key) return;
|
|
3612
|
+
pending.delete(key);
|
|
3613
|
+
const ts = nowMs();
|
|
3614
|
+
seen.delete(key);
|
|
3615
|
+
seen.set(key, ts);
|
|
3616
|
+
pruneSeenEvents({
|
|
3617
|
+
seen,
|
|
3618
|
+
ttlMs,
|
|
3619
|
+
maxEntries,
|
|
3620
|
+
nowMs: nowMs()
|
|
3621
|
+
});
|
|
3622
|
+
schedulePersist();
|
|
3623
|
+
},
|
|
3624
|
+
releaseEvent: ({ roomId, eventId }) => {
|
|
3625
|
+
const key = buildEventKey({
|
|
3626
|
+
roomId,
|
|
3627
|
+
eventId
|
|
3628
|
+
});
|
|
3629
|
+
if (!key) return;
|
|
3630
|
+
pending.delete(key);
|
|
3631
|
+
},
|
|
3632
|
+
flush,
|
|
3633
|
+
stop: async () => {
|
|
3634
|
+
try {
|
|
3635
|
+
await flush();
|
|
3636
|
+
} catch (err) {
|
|
3637
|
+
LogService.warn("MatrixInboundDedupe", "Failed to flush Matrix inbound dedupe store during stop():", err);
|
|
3638
|
+
}
|
|
3639
|
+
}
|
|
3640
|
+
};
|
|
3641
|
+
}
|
|
3642
|
+
//#endregion
|
|
3643
|
+
//#region extensions/matrix/src/matrix/monitor/recent-invite.ts
|
|
3644
|
+
function shouldPromoteRecentInviteRoom(params) {
|
|
3645
|
+
if (!params.roomInfo.nameResolved || !params.roomInfo.aliasesResolved) return false;
|
|
3646
|
+
const roomAliases = [params.roomInfo.canonicalAlias ?? "", ...params.roomInfo.altAliases].filter(Boolean);
|
|
3647
|
+
if ((params.roomInfo.name?.trim() ?? "") || roomAliases.length > 0) return false;
|
|
3648
|
+
return resolveMatrixRoomConfig({
|
|
3649
|
+
rooms: params.rooms,
|
|
3650
|
+
roomId: params.roomId,
|
|
3651
|
+
aliases: roomAliases
|
|
3652
|
+
}).matchSource === void 0;
|
|
3653
|
+
}
|
|
3654
|
+
//#endregion
|
|
3655
|
+
//#region extensions/matrix/src/matrix/monitor/room-info.ts
|
|
3656
|
+
const MAX_TRACKED_ROOM_INFO = 1024;
|
|
3657
|
+
const MAX_TRACKED_MEMBER_DISPLAY_NAMES = 4096;
|
|
3658
|
+
function rememberBounded(map, key, value, maxEntries) {
|
|
3659
|
+
map.set(key, value);
|
|
3660
|
+
if (map.size > maxEntries) {
|
|
3661
|
+
const oldest = map.keys().next().value;
|
|
3662
|
+
if (typeof oldest === "string") map.delete(oldest);
|
|
3663
|
+
}
|
|
3664
|
+
}
|
|
3665
|
+
function createMatrixRoomInfoResolver(client) {
|
|
3666
|
+
const roomNameCache = /* @__PURE__ */ new Map();
|
|
3667
|
+
const roomAliasCache = /* @__PURE__ */ new Map();
|
|
3668
|
+
const memberDisplayNameCache = /* @__PURE__ */ new Map();
|
|
3669
|
+
const getRoomName = async (roomId) => {
|
|
3670
|
+
if (roomNameCache.has(roomId)) return roomNameCache.get(roomId) ?? { nameResolved: false };
|
|
3671
|
+
let name;
|
|
3672
|
+
let nameResolved = false;
|
|
3673
|
+
try {
|
|
3674
|
+
const nameState = await client.getRoomStateEvent(roomId, "m.room.name", "");
|
|
3675
|
+
nameResolved = true;
|
|
3676
|
+
if (nameState && typeof nameState.name === "string") name = nameState.name;
|
|
3677
|
+
} catch (err) {
|
|
3678
|
+
if (isMatrixNotFoundError(err)) nameResolved = true;
|
|
3679
|
+
}
|
|
3680
|
+
const info = {
|
|
3681
|
+
name,
|
|
3682
|
+
nameResolved
|
|
3683
|
+
};
|
|
3684
|
+
if (nameResolved) rememberBounded(roomNameCache, roomId, info, MAX_TRACKED_ROOM_INFO);
|
|
3685
|
+
return info;
|
|
3686
|
+
};
|
|
3687
|
+
const getRoomAliases = async (roomId) => {
|
|
3688
|
+
const cached = roomAliasCache.get(roomId);
|
|
3689
|
+
if (cached) return cached;
|
|
3690
|
+
let canonicalAlias;
|
|
3691
|
+
let altAliases = [];
|
|
3692
|
+
let aliasesResolved = false;
|
|
3693
|
+
try {
|
|
3694
|
+
const aliasState = await client.getRoomStateEvent(roomId, "m.room.canonical_alias", "");
|
|
3695
|
+
aliasesResolved = true;
|
|
3696
|
+
if (aliasState && typeof aliasState.alias === "string") canonicalAlias = aliasState.alias;
|
|
3697
|
+
const rawAliases = aliasState?.alt_aliases;
|
|
3698
|
+
if (Array.isArray(rawAliases)) altAliases = rawAliases.filter((entry) => typeof entry === "string");
|
|
3699
|
+
} catch (err) {
|
|
3700
|
+
if (isMatrixNotFoundError(err)) aliasesResolved = true;
|
|
3701
|
+
}
|
|
3702
|
+
const info = {
|
|
3703
|
+
canonicalAlias,
|
|
3704
|
+
altAliases,
|
|
3705
|
+
aliasesResolved
|
|
3706
|
+
};
|
|
3707
|
+
if (aliasesResolved) rememberBounded(roomAliasCache, roomId, info, MAX_TRACKED_ROOM_INFO);
|
|
3708
|
+
return info;
|
|
3709
|
+
};
|
|
3710
|
+
const getRoomInfo = async (roomId, opts = {}) => {
|
|
3711
|
+
const { name, nameResolved } = await getRoomName(roomId);
|
|
3712
|
+
if (!opts.includeAliases) return {
|
|
3713
|
+
name,
|
|
3714
|
+
altAliases: [],
|
|
3715
|
+
nameResolved,
|
|
3716
|
+
aliasesResolved: false
|
|
3717
|
+
};
|
|
3718
|
+
return {
|
|
3719
|
+
name,
|
|
3720
|
+
nameResolved,
|
|
3721
|
+
...await getRoomAliases(roomId)
|
|
3722
|
+
};
|
|
3723
|
+
};
|
|
3724
|
+
const getMemberDisplayName = async (roomId, userId) => {
|
|
3725
|
+
const cacheKey = `${roomId}:${userId}`;
|
|
3726
|
+
if (memberDisplayNameCache.has(cacheKey)) return memberDisplayNameCache.get(cacheKey) ?? userId;
|
|
3727
|
+
const memberState = await client.getRoomStateEvent(roomId, "m.room.member", userId).catch(() => null);
|
|
3728
|
+
const displayName = memberState && typeof memberState.displayname === "string" ? memberState.displayname : userId;
|
|
3729
|
+
rememberBounded(memberDisplayNameCache, cacheKey, displayName, MAX_TRACKED_MEMBER_DISPLAY_NAMES);
|
|
3730
|
+
return displayName;
|
|
3731
|
+
};
|
|
3732
|
+
return {
|
|
3733
|
+
getRoomInfo,
|
|
3734
|
+
getMemberDisplayName
|
|
3735
|
+
};
|
|
3736
|
+
}
|
|
3737
|
+
//#endregion
|
|
3738
|
+
//#region extensions/matrix/src/matrix/monitor/startup.ts
|
|
3739
|
+
let matrixStartupMaintenanceDepsPromise;
|
|
3740
|
+
async function loadMatrixStartupMaintenanceDeps() {
|
|
3741
|
+
matrixStartupMaintenanceDepsPromise ??= Promise.all([
|
|
3742
|
+
import("./config-update-CD5XiLXT.js"),
|
|
3743
|
+
import("./device-health-CJqwWGmU.js"),
|
|
3744
|
+
import("./profile-Bca_U_rV.js"),
|
|
3745
|
+
import("./legacy-crypto-restore-D2CzFkbv.js"),
|
|
3746
|
+
import("./startup-verification-D7jkPyKf.js")
|
|
3747
|
+
]).then(([configUpdateModule, deviceHealthModule, profileModule, legacyCryptoRestoreModule, startupVerificationModule]) => ({
|
|
3748
|
+
updateMatrixAccountConfig: configUpdateModule.updateMatrixAccountConfig,
|
|
3749
|
+
summarizeMatrixDeviceHealth: deviceHealthModule.summarizeMatrixDeviceHealth,
|
|
3750
|
+
syncMatrixOwnProfile: profileModule.syncMatrixOwnProfile,
|
|
3751
|
+
maybeRestoreLegacyMatrixBackup: legacyCryptoRestoreModule.maybeRestoreLegacyMatrixBackup,
|
|
3752
|
+
ensureMatrixStartupVerification: startupVerificationModule.ensureMatrixStartupVerification
|
|
3753
|
+
}));
|
|
3754
|
+
return await matrixStartupMaintenanceDepsPromise;
|
|
3755
|
+
}
|
|
3756
|
+
async function runMatrixStartupMaintenance(params, deps) {
|
|
3757
|
+
const runtimeDeps = deps ?? await loadMatrixStartupMaintenanceDeps();
|
|
3758
|
+
throwIfMatrixStartupAborted(params.abortSignal);
|
|
3759
|
+
try {
|
|
3760
|
+
const profileSync = await runtimeDeps.syncMatrixOwnProfile({
|
|
3761
|
+
client: params.client,
|
|
3762
|
+
userId: params.auth.userId,
|
|
3763
|
+
displayName: params.accountConfig.name,
|
|
3764
|
+
avatarUrl: params.accountConfig.avatarUrl,
|
|
3765
|
+
loadAvatarFromUrl: async (url, maxBytes) => await params.loadWebMedia(url, maxBytes)
|
|
3766
|
+
});
|
|
3767
|
+
throwIfMatrixStartupAborted(params.abortSignal);
|
|
3768
|
+
if (profileSync.displayNameUpdated) params.logger.info(`matrix: profile display name updated for ${params.auth.userId}`);
|
|
3769
|
+
if (profileSync.avatarUpdated) params.logger.info(`matrix: profile avatar updated for ${params.auth.userId}`);
|
|
3770
|
+
if (profileSync.convertedAvatarFromHttp && profileSync.resolvedAvatarUrl && params.accountConfig.avatarUrl !== profileSync.resolvedAvatarUrl) {
|
|
3771
|
+
const latestCfg = params.getRuntimeConfig();
|
|
3772
|
+
const updatedCfg = runtimeDeps.updateMatrixAccountConfig(latestCfg, params.accountId, { avatarUrl: profileSync.resolvedAvatarUrl });
|
|
3773
|
+
await params.replaceConfigFile(updatedCfg);
|
|
3774
|
+
throwIfMatrixStartupAborted(params.abortSignal);
|
|
3775
|
+
params.logVerboseMessage(`matrix: persisted converted avatar URL for account ${params.accountId} (${profileSync.resolvedAvatarUrl})`);
|
|
3776
|
+
}
|
|
3777
|
+
} catch (err) {
|
|
3778
|
+
if (isMatrixStartupAbortError(err)) throw err;
|
|
3779
|
+
params.logger.warn("matrix: failed to sync profile from config", { error: String(err) });
|
|
3780
|
+
}
|
|
3781
|
+
if (!(params.auth.encryption && params.client.crypto)) return;
|
|
3782
|
+
try {
|
|
3783
|
+
throwIfMatrixStartupAborted(params.abortSignal);
|
|
3784
|
+
const deviceHealth = runtimeDeps.summarizeMatrixDeviceHealth(await params.client.listOwnDevices());
|
|
3785
|
+
if (deviceHealth.staleDaoCoreDevices.length > 0) params.logger.warn(`matrix: stale DaoCore devices detected for ${params.auth.userId}: ${deviceHealth.staleDaoCoreDevices.map((device) => device.deviceId).join(", ")}. Run 'daocore matrix devices prune-stale --account ${params.effectiveAccountId}' to keep encrypted-room trust healthy.`);
|
|
3786
|
+
} catch (err) {
|
|
3787
|
+
if (isMatrixStartupAbortError(err)) throw err;
|
|
3788
|
+
params.logger.debug?.("Failed to inspect matrix device hygiene (non-fatal)", { error: String(err) });
|
|
3789
|
+
}
|
|
3790
|
+
try {
|
|
3791
|
+
throwIfMatrixStartupAborted(params.abortSignal);
|
|
3792
|
+
const startupVerification = await runtimeDeps.ensureMatrixStartupVerification({
|
|
3793
|
+
client: params.client,
|
|
3794
|
+
auth: params.auth,
|
|
3795
|
+
accountConfig: params.accountConfig,
|
|
3796
|
+
env: params.env
|
|
3797
|
+
});
|
|
3798
|
+
throwIfMatrixStartupAborted(params.abortSignal);
|
|
3799
|
+
if (startupVerification.kind === "verified") params.logger.info("matrix: device is verified by its owner and ready for encrypted rooms");
|
|
3800
|
+
else if (startupVerification.kind === "disabled" || startupVerification.kind === "cooldown" || startupVerification.kind === "pending" || startupVerification.kind === "request-failed") {
|
|
3801
|
+
params.logger.info("matrix: device not verified — run 'daocore matrix verify device <key>' to enable E2EE");
|
|
3802
|
+
if (startupVerification.kind === "pending") params.logger.info("matrix: startup verification request is already pending; finish it in another Matrix client");
|
|
3803
|
+
else if (startupVerification.kind === "cooldown") params.logVerboseMessage(`matrix: skipped startup verification request due to cooldown (retryAfterMs=${startupVerification.retryAfterMs ?? 0})`);
|
|
3804
|
+
else if (startupVerification.kind === "request-failed") params.logger.debug?.("Matrix startup verification request failed (non-fatal)", { error: startupVerification.error ?? "unknown" });
|
|
3805
|
+
} else if (startupVerification.kind === "requested") params.logger.info("matrix: device not verified — requested verification in another Matrix client");
|
|
3806
|
+
} catch (err) {
|
|
3807
|
+
if (isMatrixStartupAbortError(err)) throw err;
|
|
3808
|
+
params.logger.debug?.("Failed to resolve matrix verification status (non-fatal)", { error: String(err) });
|
|
3809
|
+
}
|
|
3810
|
+
try {
|
|
3811
|
+
throwIfMatrixStartupAborted(params.abortSignal);
|
|
3812
|
+
const legacyCryptoRestore = await runtimeDeps.maybeRestoreLegacyMatrixBackup({
|
|
3813
|
+
client: params.client,
|
|
3814
|
+
auth: params.auth,
|
|
3815
|
+
env: params.env
|
|
3816
|
+
});
|
|
3817
|
+
throwIfMatrixStartupAborted(params.abortSignal);
|
|
3818
|
+
if (legacyCryptoRestore.kind === "restored") {
|
|
3819
|
+
params.logger.info(`matrix: restored ${legacyCryptoRestore.imported}/${legacyCryptoRestore.total} room key(s) from legacy encrypted-state backup`);
|
|
3820
|
+
if (legacyCryptoRestore.localOnlyKeys > 0) params.logger.warn(`matrix: ${legacyCryptoRestore.localOnlyKeys} legacy local-only room key(s) were never backed up and could not be restored automatically`);
|
|
3821
|
+
} else if (legacyCryptoRestore.kind === "failed") {
|
|
3822
|
+
params.logger.warn(`matrix: failed restoring room keys from legacy encrypted-state backup: ${legacyCryptoRestore.error}`);
|
|
3823
|
+
if (legacyCryptoRestore.localOnlyKeys > 0) params.logger.warn(`matrix: ${legacyCryptoRestore.localOnlyKeys} legacy local-only room key(s) were never backed up and may remain unavailable until manually recovered`);
|
|
3824
|
+
}
|
|
3825
|
+
} catch (err) {
|
|
3826
|
+
if (isMatrixStartupAbortError(err)) throw err;
|
|
3827
|
+
params.logger.warn("matrix: failed restoring legacy encrypted-state backup", { error: String(err) });
|
|
3828
|
+
}
|
|
3829
|
+
}
|
|
3830
|
+
//#endregion
|
|
3831
|
+
//#region extensions/matrix/src/matrix/monitor/status.ts
|
|
3832
|
+
function cloneLastDisconnect(value) {
|
|
3833
|
+
if (!value || typeof value === "string") return value ?? null;
|
|
3834
|
+
return { ...value };
|
|
3835
|
+
}
|
|
3836
|
+
function formatSyncError(error) {
|
|
3837
|
+
if (!error) return null;
|
|
3838
|
+
if (error instanceof Error) return error.message || error.name || "unknown";
|
|
3839
|
+
return formatMatrixErrorMessage(error);
|
|
3840
|
+
}
|
|
3841
|
+
function createMatrixMonitorStatusController(params) {
|
|
3842
|
+
const status = {
|
|
3843
|
+
accountId: params.accountId,
|
|
3844
|
+
...params.baseUrl ? { baseUrl: params.baseUrl } : {},
|
|
3845
|
+
connected: false,
|
|
3846
|
+
lastConnectedAt: null,
|
|
3847
|
+
lastDisconnect: null,
|
|
3848
|
+
lastError: null,
|
|
3849
|
+
healthState: "starting"
|
|
3850
|
+
};
|
|
3851
|
+
const emit = () => {
|
|
3852
|
+
params.statusSink?.({
|
|
3853
|
+
...status,
|
|
3854
|
+
lastDisconnect: cloneLastDisconnect(status.lastDisconnect)
|
|
3855
|
+
});
|
|
3856
|
+
};
|
|
3857
|
+
const noteConnected = (at = Date.now(), options) => {
|
|
3858
|
+
if (status.connected === true) status.lastEventAt = at;
|
|
3859
|
+
else Object.assign(status, createConnectedChannelStatusPatch(at));
|
|
3860
|
+
if (options?.transportActivity) Object.assign(status, createTransportActivityStatusPatch(at));
|
|
3861
|
+
status.lastError = null;
|
|
3862
|
+
status.lastDisconnect = null;
|
|
3863
|
+
status.healthState = "healthy";
|
|
3864
|
+
emit();
|
|
3865
|
+
};
|
|
3866
|
+
const noteDisconnected = (params) => {
|
|
3867
|
+
const at = params.at ?? Date.now();
|
|
3868
|
+
const error = formatSyncError(params.error);
|
|
3869
|
+
status.connected = false;
|
|
3870
|
+
status.lastEventAt = at;
|
|
3871
|
+
status.lastDisconnect = {
|
|
3872
|
+
at,
|
|
3873
|
+
...error ? { error } : {}
|
|
3874
|
+
};
|
|
3875
|
+
status.lastError = error;
|
|
3876
|
+
status.healthState = params.state.toLowerCase();
|
|
3877
|
+
emit();
|
|
3878
|
+
};
|
|
3879
|
+
emit();
|
|
3880
|
+
return {
|
|
3881
|
+
noteSyncState(state, error, at = Date.now()) {
|
|
3882
|
+
if (isMatrixReadySyncState(state)) {
|
|
3883
|
+
noteConnected(at, { transportActivity: state === "SYNCING" });
|
|
3884
|
+
return;
|
|
3885
|
+
}
|
|
3886
|
+
if (isMatrixDisconnectedSyncState(state)) {
|
|
3887
|
+
noteDisconnected({
|
|
3888
|
+
state,
|
|
3889
|
+
at,
|
|
3890
|
+
error
|
|
3891
|
+
});
|
|
3892
|
+
return;
|
|
3893
|
+
}
|
|
3894
|
+
status.lastEventAt = at;
|
|
3895
|
+
status.healthState = state.toLowerCase();
|
|
3896
|
+
emit();
|
|
3897
|
+
},
|
|
3898
|
+
noteUnexpectedError(error, at = Date.now()) {
|
|
3899
|
+
noteDisconnected({
|
|
3900
|
+
state: "ERROR",
|
|
3901
|
+
at,
|
|
3902
|
+
error
|
|
3903
|
+
});
|
|
3904
|
+
},
|
|
3905
|
+
markStopped(at = Date.now()) {
|
|
3906
|
+
status.connected = false;
|
|
3907
|
+
status.lastEventAt = at;
|
|
3908
|
+
if (status.healthState !== "error") status.healthState = "stopped";
|
|
3909
|
+
emit();
|
|
3910
|
+
}
|
|
3911
|
+
};
|
|
3912
|
+
}
|
|
3913
|
+
//#endregion
|
|
3914
|
+
//#region extensions/matrix/src/matrix/monitor/sync-lifecycle.ts
|
|
3915
|
+
function formatSyncLifecycleError(state, error) {
|
|
3916
|
+
if (error instanceof Error) return error;
|
|
3917
|
+
const message = typeof error === "string" && error.trim() ? error.trim() : void 0;
|
|
3918
|
+
if (state === "STOPPED") return new Error(message ?? "Matrix sync stopped unexpectedly");
|
|
3919
|
+
if (state === "ERROR") return new Error(message ?? "Matrix sync entered ERROR unexpectedly");
|
|
3920
|
+
return new Error(message ?? `Matrix sync entered ${state} unexpectedly`);
|
|
3921
|
+
}
|
|
3922
|
+
function createMatrixMonitorSyncLifecycle(params) {
|
|
3923
|
+
let fatalError = null;
|
|
3924
|
+
let resolveFatalWait = null;
|
|
3925
|
+
let rejectFatalWait = null;
|
|
3926
|
+
const settleFatal = (error) => {
|
|
3927
|
+
if (fatalError) return;
|
|
3928
|
+
fatalError = error;
|
|
3929
|
+
rejectFatalWait?.(error);
|
|
3930
|
+
resolveFatalWait = null;
|
|
3931
|
+
rejectFatalWait = null;
|
|
3932
|
+
};
|
|
3933
|
+
const onSyncState = (state, _prevState, error) => {
|
|
3934
|
+
if (isMatrixTerminalSyncState(state) && !params.isStopping?.()) {
|
|
3935
|
+
const fatalError = formatSyncLifecycleError(state, error);
|
|
3936
|
+
params.statusController.noteUnexpectedError(fatalError);
|
|
3937
|
+
settleFatal(fatalError);
|
|
3938
|
+
return;
|
|
3939
|
+
}
|
|
3940
|
+
if (fatalError) return;
|
|
3941
|
+
if (params.isStopping?.() && !isMatrixTerminalSyncState(state)) return;
|
|
3942
|
+
params.statusController.noteSyncState(state, error);
|
|
3943
|
+
};
|
|
3944
|
+
const onUnexpectedError = (error) => {
|
|
3945
|
+
if (params.isStopping?.()) return;
|
|
3946
|
+
params.statusController.noteUnexpectedError(error);
|
|
3947
|
+
settleFatal(error);
|
|
3948
|
+
};
|
|
3949
|
+
params.client.on("sync.state", onSyncState);
|
|
3950
|
+
params.client.on("sync.unexpected_error", onUnexpectedError);
|
|
3951
|
+
return {
|
|
3952
|
+
async waitForFatalStop() {
|
|
3953
|
+
if (fatalError) throw fatalError;
|
|
3954
|
+
if (resolveFatalWait || rejectFatalWait) throw new Error("Matrix fatal-stop wait already in progress");
|
|
3955
|
+
await new Promise((resolve, reject) => {
|
|
3956
|
+
resolveFatalWait = resolve;
|
|
3957
|
+
rejectFatalWait = (error) => reject(error);
|
|
3958
|
+
});
|
|
3959
|
+
},
|
|
3960
|
+
dispose() {
|
|
3961
|
+
resolveFatalWait?.();
|
|
3962
|
+
resolveFatalWait = null;
|
|
3963
|
+
rejectFatalWait = null;
|
|
3964
|
+
params.client.off("sync.state", onSyncState);
|
|
3965
|
+
params.client.off("sync.unexpected_error", onUnexpectedError);
|
|
3966
|
+
}
|
|
3967
|
+
};
|
|
3968
|
+
}
|
|
3969
|
+
//#endregion
|
|
3970
|
+
//#region extensions/matrix/src/matrix/monitor/task-runner.ts
|
|
3971
|
+
function createMatrixMonitorTaskRunner(params) {
|
|
3972
|
+
const inFlight = /* @__PURE__ */ new Set();
|
|
3973
|
+
const runDetachedTask = (label, task) => {
|
|
3974
|
+
let trackedTask;
|
|
3975
|
+
trackedTask = Promise.resolve().then(task).catch((error) => {
|
|
3976
|
+
const message = String(error);
|
|
3977
|
+
params.logVerboseMessage(`matrix: ${label} failed (${message})`);
|
|
3978
|
+
params.logger.warn("matrix background task failed", {
|
|
3979
|
+
task: label,
|
|
3980
|
+
error: message
|
|
3981
|
+
});
|
|
3982
|
+
}).finally(() => {
|
|
3983
|
+
inFlight.delete(trackedTask);
|
|
3984
|
+
});
|
|
3985
|
+
inFlight.add(trackedTask);
|
|
3986
|
+
return trackedTask;
|
|
3987
|
+
};
|
|
3988
|
+
const waitForIdle = async () => {
|
|
3989
|
+
while (inFlight.size > 0) await Promise.allSettled(Array.from(inFlight));
|
|
3990
|
+
};
|
|
3991
|
+
return {
|
|
3992
|
+
runDetachedTask,
|
|
3993
|
+
waitForIdle
|
|
3994
|
+
};
|
|
3995
|
+
}
|
|
3996
|
+
//#endregion
|
|
3997
|
+
//#region extensions/matrix/src/matrix/monitor/index.ts
|
|
3998
|
+
function isMatrixStreamingConfig(streaming) {
|
|
3999
|
+
return Boolean(streaming && typeof streaming === "object" && !Array.isArray(streaming));
|
|
4000
|
+
}
|
|
4001
|
+
function resolveMatrixStreamingMode(streaming) {
|
|
4002
|
+
if (streaming === true || streaming === "partial") return "partial";
|
|
4003
|
+
if (streaming === "quiet") return "quiet";
|
|
4004
|
+
if (streaming === "progress") return "progress";
|
|
4005
|
+
if (isMatrixStreamingConfig(streaming)) {
|
|
4006
|
+
if (streaming.mode === "partial" || streaming.mode === "quiet" || streaming.mode === "progress") return streaming.mode;
|
|
4007
|
+
}
|
|
4008
|
+
return "off";
|
|
4009
|
+
}
|
|
4010
|
+
function resolveMatrixPreviewToolProgress(streaming) {
|
|
4011
|
+
if (!isMatrixStreamingConfig(streaming)) return true;
|
|
4012
|
+
if (resolveMatrixStreamingMode(streaming) === "progress") return streaming.progress?.toolProgress ?? streaming.preview?.toolProgress ?? true;
|
|
4013
|
+
return streaming.preview?.toolProgress ?? true;
|
|
4014
|
+
}
|
|
4015
|
+
function resolveMatrixPreviewToolProgressEnabled(streaming) {
|
|
4016
|
+
return resolveMatrixStreamingMode(streaming) !== "off" && resolveMatrixPreviewToolProgress(streaming);
|
|
4017
|
+
}
|
|
4018
|
+
const DEFAULT_MEDIA_MAX_MB = 20;
|
|
4019
|
+
async function monitorMatrixProvider(opts = {}) {
|
|
4020
|
+
if (opts.abortSignal?.aborted) return;
|
|
4021
|
+
if (isBunRuntime()) throw new Error("Matrix provider requires Node (bun runtime not supported)");
|
|
4022
|
+
const core = getMatrixRuntime();
|
|
4023
|
+
let cfg = core.config.current();
|
|
4024
|
+
if (cfg.channels?.["matrix"]?.enabled === false) return;
|
|
4025
|
+
const logger = core.logging.getChildLogger({ module: "matrix-auto-reply" });
|
|
4026
|
+
const formatRuntimeMessage = (...args) => format(...args);
|
|
4027
|
+
const runtime = opts.runtime ?? {
|
|
4028
|
+
log: (...args) => {
|
|
4029
|
+
logger.info(formatRuntimeMessage(...args));
|
|
4030
|
+
},
|
|
4031
|
+
error: (...args) => {
|
|
4032
|
+
logger.error(formatRuntimeMessage(...args));
|
|
4033
|
+
},
|
|
4034
|
+
exit: (code) => {
|
|
4035
|
+
throw new Error(`exit ${code}`);
|
|
4036
|
+
}
|
|
4037
|
+
};
|
|
4038
|
+
const logVerboseMessage = (message) => {
|
|
4039
|
+
if (!core.logging.shouldLogVerbose()) return;
|
|
4040
|
+
logger.debug?.(message);
|
|
4041
|
+
};
|
|
4042
|
+
const effectiveAccountId = resolveMatrixAuthContext({
|
|
4043
|
+
cfg,
|
|
4044
|
+
accountId: opts.accountId
|
|
4045
|
+
}).accountId;
|
|
4046
|
+
const accountConfig = resolveMatrixAccountConfig({
|
|
4047
|
+
cfg,
|
|
4048
|
+
accountId: effectiveAccountId
|
|
4049
|
+
});
|
|
4050
|
+
const allowlistOnly = accountConfig.allowlistOnly === true;
|
|
4051
|
+
const accountAllowBots = accountConfig.allowBots;
|
|
4052
|
+
let allowFrom = (accountConfig.dm?.allowFrom ?? []).map(String);
|
|
4053
|
+
let groupAllowFrom = (accountConfig.groupAllowFrom ?? []).map(String);
|
|
4054
|
+
let allowFromResolvedEntries = [];
|
|
4055
|
+
let groupAllowFromResolvedEntries = [];
|
|
4056
|
+
let roomsConfig = accountConfig.groups ?? accountConfig.rooms;
|
|
4057
|
+
let needsRoomAliasesForConfig = false;
|
|
4058
|
+
const configuredBotUserIds = resolveConfiguredMatrixBotUserIds({
|
|
4059
|
+
cfg,
|
|
4060
|
+
accountId: effectiveAccountId
|
|
4061
|
+
});
|
|
4062
|
+
({allowFrom, allowFromResolvedEntries, groupAllowFrom, groupAllowFromResolvedEntries, roomsConfig} = await resolveMatrixMonitorConfig({
|
|
4063
|
+
cfg,
|
|
4064
|
+
accountId: effectiveAccountId,
|
|
4065
|
+
allowFrom,
|
|
4066
|
+
groupAllowFrom,
|
|
4067
|
+
roomsConfig,
|
|
4068
|
+
runtime
|
|
4069
|
+
}));
|
|
4070
|
+
needsRoomAliasesForConfig = Boolean(roomsConfig && Object.keys(roomsConfig).some((key) => key.trim().startsWith("#")));
|
|
4071
|
+
cfg = {
|
|
4072
|
+
...cfg,
|
|
4073
|
+
channels: {
|
|
4074
|
+
...cfg.channels,
|
|
4075
|
+
matrix: {
|
|
4076
|
+
...cfg.channels?.["matrix"],
|
|
4077
|
+
dm: {
|
|
4078
|
+
...cfg.channels?.["matrix"]?.dm,
|
|
4079
|
+
allowFrom
|
|
4080
|
+
},
|
|
4081
|
+
groupAllowFrom,
|
|
4082
|
+
...roomsConfig ? { groups: roomsConfig } : {}
|
|
4083
|
+
}
|
|
4084
|
+
}
|
|
4085
|
+
};
|
|
4086
|
+
const auth = await resolveMatrixAuth({
|
|
4087
|
+
cfg,
|
|
4088
|
+
accountId: effectiveAccountId
|
|
4089
|
+
});
|
|
4090
|
+
const resolvedInitialSyncLimit = typeof opts.initialSyncLimit === "number" ? Math.max(0, Math.floor(opts.initialSyncLimit)) : auth.initialSyncLimit;
|
|
4091
|
+
const authWithLimit = resolvedInitialSyncLimit === auth.initialSyncLimit ? auth : {
|
|
4092
|
+
...auth,
|
|
4093
|
+
initialSyncLimit: resolvedInitialSyncLimit
|
|
4094
|
+
};
|
|
4095
|
+
const statusController = createMatrixMonitorStatusController({
|
|
4096
|
+
accountId: auth.accountId,
|
|
4097
|
+
baseUrl: auth.homeserver,
|
|
4098
|
+
statusSink: opts.setStatus
|
|
4099
|
+
});
|
|
4100
|
+
let cleanedUp = false;
|
|
4101
|
+
let client = null;
|
|
4102
|
+
let threadBindingManager = null;
|
|
4103
|
+
let inboundDeduper = null;
|
|
4104
|
+
const monitorTaskRunner = createMatrixMonitorTaskRunner({
|
|
4105
|
+
logger,
|
|
4106
|
+
logVerboseMessage
|
|
4107
|
+
});
|
|
4108
|
+
let syncLifecycle = null;
|
|
4109
|
+
const cleanup = async (mode = "persist") => {
|
|
4110
|
+
if (cleanedUp) return;
|
|
4111
|
+
cleanedUp = true;
|
|
4112
|
+
try {
|
|
4113
|
+
client?.stopSyncWithoutPersist();
|
|
4114
|
+
if (client && mode === "persist") await client.drainPendingDecryptions("matrix monitor shutdown");
|
|
4115
|
+
if (mode === "persist") await monitorTaskRunner.waitForIdle();
|
|
4116
|
+
threadBindingManager?.stop();
|
|
4117
|
+
await inboundDeduper?.stop();
|
|
4118
|
+
if (client) await releaseSharedClientInstance(client, mode);
|
|
4119
|
+
} finally {
|
|
4120
|
+
client?.off("sync.state", onSyncState);
|
|
4121
|
+
syncLifecycle?.dispose();
|
|
4122
|
+
statusController.markStopped();
|
|
4123
|
+
setActiveMatrixClient(null, auth.accountId);
|
|
4124
|
+
}
|
|
4125
|
+
};
|
|
4126
|
+
const defaultGroupPolicy = resolveDefaultGroupPolicy(cfg);
|
|
4127
|
+
const { groupPolicy: groupPolicyRaw, providerMissingFallbackApplied } = resolveAllowlistProviderRuntimeGroupPolicy({
|
|
4128
|
+
providerConfigPresent: cfg.channels?.["matrix"] !== void 0,
|
|
4129
|
+
groupPolicy: accountConfig.groupPolicy,
|
|
4130
|
+
defaultGroupPolicy
|
|
4131
|
+
});
|
|
4132
|
+
warnMissingProviderGroupPolicyFallbackOnce({
|
|
4133
|
+
providerMissingFallbackApplied,
|
|
4134
|
+
providerKey: "matrix",
|
|
4135
|
+
accountId: effectiveAccountId,
|
|
4136
|
+
blockedLabel: GROUP_POLICY_BLOCKED_LABEL.room,
|
|
4137
|
+
log: (message) => logVerboseMessage(message)
|
|
4138
|
+
});
|
|
4139
|
+
const groupPolicy = allowlistOnly && groupPolicyRaw === "open" ? "allowlist" : groupPolicyRaw;
|
|
4140
|
+
const replyToMode = opts.replyToMode ?? accountConfig.replyToMode ?? "off";
|
|
4141
|
+
const threadReplies = accountConfig.threadReplies ?? "inbound";
|
|
4142
|
+
const dmThreadReplies = accountConfig.dm?.threadReplies;
|
|
4143
|
+
const threadBindingIdleTimeoutMs = resolveThreadBindingIdleTimeoutMsForChannel({
|
|
4144
|
+
cfg,
|
|
4145
|
+
channel: "matrix",
|
|
4146
|
+
accountId: effectiveAccountId
|
|
4147
|
+
});
|
|
4148
|
+
const threadBindingMaxAgeMs = resolveThreadBindingMaxAgeMsForChannel({
|
|
4149
|
+
cfg,
|
|
4150
|
+
channel: "matrix",
|
|
4151
|
+
accountId: effectiveAccountId
|
|
4152
|
+
});
|
|
4153
|
+
const dmConfig = accountConfig.dm;
|
|
4154
|
+
const dmEnabled = dmConfig?.enabled ?? true;
|
|
4155
|
+
const dmPolicyRaw = dmConfig?.policy ?? "pairing";
|
|
4156
|
+
const dmPolicy = allowlistOnly && dmPolicyRaw !== "disabled" ? "allowlist" : dmPolicyRaw;
|
|
4157
|
+
const dmSessionScope = dmConfig?.sessionScope ?? "per-user";
|
|
4158
|
+
const textLimit = core.channel.text.resolveTextChunkLimit(cfg, "matrix", effectiveAccountId);
|
|
4159
|
+
const globalGroupChatHistoryLimit = cfg.messages?.groupChat?.historyLimit;
|
|
4160
|
+
const historyLimit = Math.max(0, accountConfig.historyLimit ?? globalGroupChatHistoryLimit ?? 0);
|
|
4161
|
+
const mediaMaxMb = opts.mediaMaxMb ?? accountConfig.mediaMaxMb ?? DEFAULT_MEDIA_MAX_MB;
|
|
4162
|
+
const mediaMaxBytes = Math.max(1, mediaMaxMb) * 1024 * 1024;
|
|
4163
|
+
const streaming = resolveMatrixStreamingMode(accountConfig.streaming);
|
|
4164
|
+
const previewToolProgressEnabled = resolveMatrixPreviewToolProgressEnabled(accountConfig.streaming);
|
|
4165
|
+
const blockStreamingEnabled = accountConfig.blockStreaming === true;
|
|
4166
|
+
const startupMs = Date.now();
|
|
4167
|
+
const startupGraceMs = 0;
|
|
4168
|
+
const warnedEncryptedRooms = /* @__PURE__ */ new Set();
|
|
4169
|
+
const warnedCryptoMissingRooms = /* @__PURE__ */ new Set();
|
|
4170
|
+
let healthySyncSinceMs;
|
|
4171
|
+
const noteSyncHealthState = (state, at = Date.now()) => {
|
|
4172
|
+
if (isMatrixReadySyncState(state)) {
|
|
4173
|
+
healthySyncSinceMs ??= at;
|
|
4174
|
+
return;
|
|
4175
|
+
}
|
|
4176
|
+
if (isMatrixDisconnectedSyncState(state)) healthySyncSinceMs = void 0;
|
|
4177
|
+
};
|
|
4178
|
+
const onSyncState = (state) => {
|
|
4179
|
+
noteSyncHealthState(state);
|
|
4180
|
+
};
|
|
4181
|
+
try {
|
|
4182
|
+
client = await resolveSharedMatrixClient({
|
|
4183
|
+
cfg,
|
|
4184
|
+
auth: authWithLimit,
|
|
4185
|
+
startClient: false,
|
|
4186
|
+
accountId: auth.accountId
|
|
4187
|
+
});
|
|
4188
|
+
setActiveMatrixClient(client, auth.accountId);
|
|
4189
|
+
inboundDeduper = await createMatrixInboundEventDeduper({
|
|
4190
|
+
auth,
|
|
4191
|
+
env: process.env
|
|
4192
|
+
});
|
|
4193
|
+
syncLifecycle = createMatrixMonitorSyncLifecycle({
|
|
4194
|
+
client,
|
|
4195
|
+
statusController,
|
|
4196
|
+
isStopping: () => cleanedUp || opts.abortSignal?.aborted === true
|
|
4197
|
+
});
|
|
4198
|
+
client.on("sync.state", onSyncState);
|
|
4199
|
+
const dropPreStartupMessages = !client.hasPersistedSyncState();
|
|
4200
|
+
const { getRoomInfo, getMemberDisplayName } = createMatrixRoomInfoResolver(client);
|
|
4201
|
+
const isExplicitlyConfiguredRoom = async (roomId) => {
|
|
4202
|
+
const roomInfoForConfig = needsRoomAliasesForConfig ? await getRoomInfo(roomId, { includeAliases: true }) : void 0;
|
|
4203
|
+
const aliases = roomInfoForConfig ? [roomInfoForConfig.canonicalAlias ?? "", ...roomInfoForConfig.altAliases].filter(Boolean) : [];
|
|
4204
|
+
return resolveMatrixRoomConfig({
|
|
4205
|
+
rooms: roomsConfig,
|
|
4206
|
+
roomId,
|
|
4207
|
+
aliases
|
|
4208
|
+
}).matchSource === "direct";
|
|
4209
|
+
};
|
|
4210
|
+
const directTracker = createDirectRoomTracker(client, {
|
|
4211
|
+
log: logVerboseMessage,
|
|
4212
|
+
isExplicitlyConfiguredRoom,
|
|
4213
|
+
canPromoteRecentInvite: async (roomId) => shouldPromoteRecentInviteRoom({
|
|
4214
|
+
roomId,
|
|
4215
|
+
roomInfo: await getRoomInfo(roomId, { includeAliases: true }),
|
|
4216
|
+
rooms: roomsConfig
|
|
4217
|
+
}),
|
|
4218
|
+
...dmSessionScope === "per-room" ? { canPromoteUnmappedStrictRoom: async (roomId) => shouldPromoteRecentInviteRoom({
|
|
4219
|
+
roomId,
|
|
4220
|
+
roomInfo: await getRoomInfo(roomId, { includeAliases: true }),
|
|
4221
|
+
rooms: roomsConfig
|
|
4222
|
+
}) } : {},
|
|
4223
|
+
shouldKeepLocallyPromotedDirectRoom: async (roomId) => {
|
|
4224
|
+
try {
|
|
4225
|
+
const roomInfo = await getRoomInfo(roomId, { includeAliases: true });
|
|
4226
|
+
if (!roomInfo.nameResolved || !roomInfo.aliasesResolved) return;
|
|
4227
|
+
return shouldPromoteRecentInviteRoom({
|
|
4228
|
+
roomId,
|
|
4229
|
+
roomInfo,
|
|
4230
|
+
rooms: roomsConfig
|
|
4231
|
+
});
|
|
4232
|
+
} catch (err) {
|
|
4233
|
+
logVerboseMessage(`matrix: local promotion revalidation failed room=${roomId} (${String(err)})`);
|
|
4234
|
+
return;
|
|
4235
|
+
}
|
|
4236
|
+
}
|
|
4237
|
+
});
|
|
4238
|
+
registerMatrixAutoJoin({
|
|
4239
|
+
client,
|
|
4240
|
+
accountConfig,
|
|
4241
|
+
runtime
|
|
4242
|
+
});
|
|
4243
|
+
const handleRoomMessage = createMatrixRoomMessageHandler({
|
|
4244
|
+
client,
|
|
4245
|
+
core,
|
|
4246
|
+
cfg,
|
|
4247
|
+
accountId: effectiveAccountId,
|
|
4248
|
+
accountConfig,
|
|
4249
|
+
runtime,
|
|
4250
|
+
logger,
|
|
4251
|
+
logVerboseMessage,
|
|
4252
|
+
allowFrom,
|
|
4253
|
+
allowFromResolvedEntries,
|
|
4254
|
+
groupAllowFrom,
|
|
4255
|
+
groupAllowFromResolvedEntries,
|
|
4256
|
+
roomsConfig,
|
|
4257
|
+
accountAllowBots,
|
|
4258
|
+
configuredBotUserIds,
|
|
4259
|
+
groupPolicy,
|
|
4260
|
+
replyToMode,
|
|
4261
|
+
threadReplies,
|
|
4262
|
+
dmThreadReplies,
|
|
4263
|
+
dmSessionScope,
|
|
4264
|
+
streaming,
|
|
4265
|
+
previewToolProgressEnabled,
|
|
4266
|
+
blockStreamingEnabled,
|
|
4267
|
+
dmEnabled,
|
|
4268
|
+
dmPolicy,
|
|
4269
|
+
textLimit,
|
|
4270
|
+
mediaMaxBytes,
|
|
4271
|
+
historyLimit,
|
|
4272
|
+
startupMs,
|
|
4273
|
+
startupGraceMs,
|
|
4274
|
+
dropPreStartupMessages,
|
|
4275
|
+
inboundDeduper,
|
|
4276
|
+
directTracker,
|
|
4277
|
+
getRoomInfo,
|
|
4278
|
+
getMemberDisplayName,
|
|
4279
|
+
needsRoomAliasesForConfig
|
|
4280
|
+
});
|
|
4281
|
+
threadBindingManager = await createMatrixThreadBindingManager({
|
|
4282
|
+
cfg,
|
|
4283
|
+
accountId: effectiveAccountId,
|
|
4284
|
+
auth,
|
|
4285
|
+
client,
|
|
4286
|
+
env: process.env,
|
|
4287
|
+
idleTimeoutMs: threadBindingIdleTimeoutMs,
|
|
4288
|
+
maxAgeMs: threadBindingMaxAgeMs,
|
|
4289
|
+
logVerboseMessage
|
|
4290
|
+
});
|
|
4291
|
+
logVerboseMessage(`matrix: thread bindings ready account=${threadBindingManager.accountId} idleMs=${threadBindingIdleTimeoutMs} maxAgeMs=${threadBindingMaxAgeMs}`);
|
|
4292
|
+
registerMatrixMonitorEvents({
|
|
4293
|
+
cfg,
|
|
4294
|
+
client,
|
|
4295
|
+
auth,
|
|
4296
|
+
allowFrom,
|
|
4297
|
+
dmEnabled,
|
|
4298
|
+
dmPolicy,
|
|
4299
|
+
readStoreAllowFrom: async () => await core.channel.pairing.readAllowFromStore({
|
|
4300
|
+
channel: "matrix",
|
|
4301
|
+
env: process.env,
|
|
4302
|
+
accountId: effectiveAccountId
|
|
4303
|
+
}).catch(() => []),
|
|
4304
|
+
directTracker,
|
|
4305
|
+
logVerboseMessage,
|
|
4306
|
+
warnedEncryptedRooms,
|
|
4307
|
+
warnedCryptoMissingRooms,
|
|
4308
|
+
logger,
|
|
4309
|
+
startupGraceMs,
|
|
4310
|
+
getHealthySyncSinceMs: () => healthySyncSinceMs,
|
|
4311
|
+
formatNativeDependencyHint: core.system.formatNativeDependencyHint,
|
|
4312
|
+
onRoomMessage: handleRoomMessage,
|
|
4313
|
+
runDetachedTask: monitorTaskRunner.runDetachedTask
|
|
4314
|
+
});
|
|
4315
|
+
logVerboseMessage("matrix: starting client");
|
|
4316
|
+
await resolveSharedMatrixClient({
|
|
4317
|
+
cfg,
|
|
4318
|
+
auth: authWithLimit,
|
|
4319
|
+
accountId: auth.accountId,
|
|
4320
|
+
abortSignal: opts.abortSignal
|
|
4321
|
+
});
|
|
4322
|
+
logVerboseMessage("matrix: client started");
|
|
4323
|
+
logger.info(`matrix: logged in as ${auth.userId}`);
|
|
4324
|
+
backfillMatrixAuthDeviceIdAfterStartup({
|
|
4325
|
+
auth,
|
|
4326
|
+
env: process.env,
|
|
4327
|
+
abortSignal: opts.abortSignal
|
|
4328
|
+
}).catch((err) => {
|
|
4329
|
+
logVerboseMessage(`matrix: failed to backfill deviceId after startup (${String(err)})`);
|
|
4330
|
+
});
|
|
4331
|
+
registerChannelRuntimeContext({
|
|
4332
|
+
channelRuntime: opts.channelRuntime,
|
|
4333
|
+
channelId: "matrix",
|
|
4334
|
+
accountId: effectiveAccountId,
|
|
4335
|
+
capability: CHANNEL_APPROVAL_NATIVE_RUNTIME_CONTEXT_CAPABILITY,
|
|
4336
|
+
context: { client },
|
|
4337
|
+
abortSignal: opts.abortSignal
|
|
4338
|
+
});
|
|
4339
|
+
await runMatrixStartupMaintenance({
|
|
4340
|
+
client,
|
|
4341
|
+
auth,
|
|
4342
|
+
accountId: effectiveAccountId,
|
|
4343
|
+
effectiveAccountId,
|
|
4344
|
+
accountConfig,
|
|
4345
|
+
logger,
|
|
4346
|
+
logVerboseMessage,
|
|
4347
|
+
getRuntimeConfig: () => core.config.current(),
|
|
4348
|
+
replaceConfigFile: async (nextCfg) => {
|
|
4349
|
+
await core.config.replaceConfigFile({
|
|
4350
|
+
nextConfig: nextCfg,
|
|
4351
|
+
afterWrite: { mode: "auto" }
|
|
4352
|
+
});
|
|
4353
|
+
},
|
|
4354
|
+
loadWebMedia: async (url, maxBytes) => await core.media.loadWebMedia(url, maxBytes),
|
|
4355
|
+
env: process.env,
|
|
4356
|
+
abortSignal: opts.abortSignal
|
|
4357
|
+
});
|
|
4358
|
+
await Promise.race([waitUntilAbort(opts.abortSignal, async () => {
|
|
4359
|
+
try {
|
|
4360
|
+
logVerboseMessage("matrix: stopping client");
|
|
4361
|
+
await cleanup();
|
|
4362
|
+
} catch (err) {
|
|
4363
|
+
logger.warn("matrix: failed during monitor shutdown cleanup", { error: String(err) });
|
|
4364
|
+
}
|
|
4365
|
+
}), syncLifecycle.waitForFatalStop()]);
|
|
4366
|
+
} catch (err) {
|
|
4367
|
+
if (opts.abortSignal?.aborted === true && isMatrixStartupAbortError(err)) {
|
|
4368
|
+
await cleanup("stop");
|
|
4369
|
+
return;
|
|
4370
|
+
}
|
|
4371
|
+
statusController.noteUnexpectedError(err);
|
|
4372
|
+
await cleanup();
|
|
4373
|
+
throw err;
|
|
4374
|
+
}
|
|
4375
|
+
}
|
|
4376
|
+
//#endregion
|
|
4377
|
+
export { monitorMatrixProvider };
|