@otium-team/negotium 0.0.0-stage → 0.23.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +329 -2
- package/bin/codex-sdk-window-hide.mjs +40 -0
- package/bin/negotium.mjs +22 -0
- package/dist/agent-helpers.js +23090 -0
- package/dist/agent-helpers.js.map +171 -0
- package/dist/background-bash.js +535 -0
- package/dist/background-bash.js.map +16 -0
- package/dist/browser-runtime.js +2334 -0
- package/dist/browser-runtime.js.map +35 -0
- package/dist/canonical-mcp-bridge.js +13 -0
- package/dist/canonical-mcp-bridge.js.map +9 -0
- package/dist/chunk-22kg3fsw.js +79 -0
- package/dist/chunk-22kg3fsw.js.map +9 -0
- package/dist/chunk-d4j77adx.js +336 -0
- package/dist/chunk-d4j77adx.js.map +13 -0
- package/dist/chunk-mk9x18jn.js +172 -0
- package/dist/chunk-mk9x18jn.js.map +12 -0
- package/dist/chunk-qd0vtfm2.js +3923 -0
- package/dist/chunk-qd0vtfm2.js.map +47 -0
- package/dist/chunk-wf7ny8eq.js +206 -0
- package/dist/chunk-wf7ny8eq.js.map +10 -0
- package/dist/chunk-ww8a4ej4.js +54 -0
- package/dist/chunk-ww8a4ej4.js.map +10 -0
- package/dist/cron.js +1 -0
- package/dist/hosted-agent.js +5982 -0
- package/dist/hosted-agent.js.map +62 -0
- package/dist/main.js +60229 -0
- package/dist/main.js.map +340 -0
- package/dist/mcp-catalog.js +39 -0
- package/dist/mcp-catalog.js.map +10 -0
- package/dist/mcp-factories.js +25767 -0
- package/dist/mcp-factories.js.map +181 -0
- package/dist/mcp-servers.js +50 -0
- package/dist/mcp-servers.js.map +10 -0
- package/dist/media.js +570 -0
- package/dist/media.js.map +15 -0
- package/dist/outbox.js +354 -0
- package/dist/outbox.js.map +15 -0
- package/dist/platform-runtime.js +73 -0
- package/dist/platform-runtime.js.map +12 -0
- package/dist/prompts.js +892 -0
- package/dist/prompts.js.map +15 -0
- package/dist/query-runtime.js +1071 -0
- package/dist/query-runtime.js.map +26 -0
- package/dist/registry.js +335 -0
- package/dist/registry.js.map +14 -0
- package/dist/rollout.js +57 -0
- package/dist/rollout.js.map +10 -0
- package/dist/runtime/cron/background-sessions.ts +119 -0
- package/dist/runtime/cron/context.ts +123 -0
- package/dist/runtime/cron/host.ts +67 -0
- package/dist/runtime/cron/index.ts +95 -0
- package/dist/runtime/cron/mcp-server.ts +556 -0
- package/dist/runtime/cron/module.ts +174 -0
- package/dist/runtime/cron/package.json +6 -0
- package/dist/runtime/cron/schedule.ts +211 -0
- package/dist/runtime/cron/scheduler.ts +444 -0
- package/dist/runtime/cron/scripts.ts +203 -0
- package/dist/runtime/cron/store.ts +1163 -0
- package/dist/runtime/cron/summarize.ts +115 -0
- package/dist/runtime/package.json +6 -0
- package/dist/runtime/scripts/faster-whisper-wrapper.py +87 -0
- package/dist/runtime/src/agents/api-topic-agent-switch.ts +179 -0
- package/dist/runtime/src/agents/archiver.ts +677 -0
- package/dist/runtime/src/agents/auth-check.ts +156 -0
- package/dist/runtime/src/agents/claude-notices.ts +130 -0
- package/dist/runtime/src/agents/claude-provider.ts +941 -0
- package/dist/runtime/src/agents/claude-registry.ts +117 -0
- package/dist/runtime/src/agents/claude-runtime-inspection.ts +43 -0
- package/dist/runtime/src/agents/codex-app-server.ts +138 -0
- package/dist/runtime/src/agents/codex-native-multi-agent.ts +360 -0
- package/dist/runtime/src/agents/codex-provider.ts +1193 -0
- package/dist/runtime/src/agents/codex-registry.ts +116 -0
- package/dist/runtime/src/agents/codex-tier-resolver.ts +74 -0
- package/dist/runtime/src/agents/codex-tree-kill.ts +54 -0
- package/dist/runtime/src/agents/codex-tree-manager.ts +230 -0
- package/dist/runtime/src/agents/codex-vault-hook-bridge.ts +240 -0
- package/dist/runtime/src/agents/codex-vault-hook.mjs +50 -0
- package/dist/runtime/src/agents/codex-windows-wrapper.ts +174 -0
- package/dist/runtime/src/agents/compaction-support.ts +75 -0
- package/dist/runtime/src/agents/compaction-tool-projection.ts +125 -0
- package/dist/runtime/src/agents/contracts.ts +75 -0
- package/dist/runtime/src/agents/deep-map.ts +13 -0
- package/dist/runtime/src/agents/execution-host.ts +118 -0
- package/dist/runtime/src/agents/explicit-agent-switch.ts +106 -0
- package/dist/runtime/src/agents/fixtures/claude-attachments.jsonl +2 -0
- package/dist/runtime/src/agents/fixtures/codex-shell.jsonl +5 -0
- package/dist/runtime/src/agents/fork.ts +100 -0
- package/dist/runtime/src/agents/hosted-agent.ts +78 -0
- package/dist/runtime/src/agents/idle-archiver.ts +225 -0
- package/dist/runtime/src/agents/idle-compact.ts +225 -0
- package/dist/runtime/src/agents/index.ts +225 -0
- package/dist/runtime/src/agents/maestro-provider.ts +246 -0
- package/dist/runtime/src/agents/maestro-registry.ts +189 -0
- package/dist/runtime/src/agents/mcp-tools/ask-user.ts +653 -0
- package/dist/runtime/src/agents/mcp-tools/common.ts +23 -0
- package/dist/runtime/src/agents/mcp-tools/inline-assets.ts +203 -0
- package/dist/runtime/src/agents/mcp-tools/publish-html.ts +183 -0
- package/dist/runtime/src/agents/mcp-tools/request-vault.ts +684 -0
- package/dist/runtime/src/agents/mcp-tools/self-config.ts +353 -0
- package/dist/runtime/src/agents/mcp-tools/spawn-subagent.ts +1349 -0
- package/dist/runtime/src/agents/mcp-tools/visual-compat.ts +18 -0
- package/dist/runtime/src/agents/mcp-tools/visuals.ts +60 -0
- package/dist/runtime/src/agents/memory-archive-policy.ts +54 -0
- package/dist/runtime/src/agents/model-catalog.ts +439 -0
- package/dist/runtime/src/agents/public-helpers.ts +149 -0
- package/dist/runtime/src/agents/registry.ts +35 -0
- package/dist/runtime/src/agents/rollout/claude.ts +336 -0
- package/dist/runtime/src/agents/rollout/codex.ts +910 -0
- package/dist/runtime/src/agents/rollout/index.ts +26 -0
- package/dist/runtime/src/agents/rollout/shared.ts +293 -0
- package/dist/runtime/src/agents/self-config-core.ts +704 -0
- package/dist/runtime/src/agents/task-events.ts +59 -0
- package/dist/runtime/src/agents/tool-format.ts +613 -0
- package/dist/runtime/src/agents/topic-agent-switch.ts +189 -0
- package/dist/runtime/src/agents/topic-cleanup.ts +285 -0
- package/dist/runtime/src/agents/vault-tool-policy.ts +36 -0
- package/dist/runtime/src/application/execute-external-user-turn.ts +23 -0
- package/dist/runtime/src/application/submit-runtime-gateway-turn.ts +484 -0
- package/dist/runtime/src/application/submit-user-message.ts +60 -0
- package/dist/runtime/src/application/switch-topic-effort.ts +48 -0
- package/dist/runtime/src/application/switch-topic-model.ts +79 -0
- package/dist/runtime/src/application/topic-service.ts +190 -0
- package/dist/runtime/src/application/turn-follow-ups.ts +291 -0
- package/dist/runtime/src/application/vault-command.ts +170 -0
- package/dist/runtime/src/bus.ts +409 -0
- package/dist/runtime/src/config-public.ts +2 -0
- package/dist/runtime/src/cron-host.ts +33 -0
- package/dist/runtime/src/cron-mcp-host.ts +5 -0
- package/dist/runtime/src/identifiers.ts +49 -0
- package/dist/runtime/src/index.ts +573 -0
- package/dist/runtime/src/mcp/agent-health-server.ts +8 -0
- package/dist/runtime/src/mcp/canonical-bridge-config.ts +84 -0
- package/dist/runtime/src/mcp/canonical-proxy-server.ts +308 -0
- package/dist/runtime/src/mcp/compaction-log-server.ts +25 -0
- package/dist/runtime/src/mcp/decision-server.ts +21 -0
- package/dist/runtime/src/mcp/dm-ipc.ts +60 -0
- package/dist/runtime/src/mcp/factories/agent-health.ts +394 -0
- package/dist/runtime/src/mcp/factories/compaction-log.ts +135 -0
- package/dist/runtime/src/mcp/factories/decision.ts +186 -0
- package/dist/runtime/src/mcp/factories/index.ts +73 -0
- package/dist/runtime/src/mcp/factories/session-comm.ts +184 -0
- package/dist/runtime/src/mcp/factories/session-history.ts +103 -0
- package/dist/runtime/src/mcp/factories/stdio-protection.ts +24 -0
- package/dist/runtime/src/mcp/factories/system-health.ts +259 -0
- package/dist/runtime/src/mcp/factories/task.ts +185 -0
- package/dist/runtime/src/mcp/factories/token-stats.ts +145 -0
- package/dist/runtime/src/mcp/factories/vault.ts +32 -0
- package/dist/runtime/src/mcp/mcp-helpers.ts +40 -0
- package/dist/runtime/src/mcp/peer-bridge.ts +119 -0
- package/dist/runtime/src/mcp/runtime-spec.ts +473 -0
- package/dist/runtime/src/mcp/session-comm/actor-policy.ts +160 -0
- package/dist/runtime/src/mcp/session-comm/bridge-ipc-config.ts +27 -0
- package/dist/runtime/src/mcp/session-comm/context.ts +113 -0
- package/dist/runtime/src/mcp/session-comm/default-host.ts +723 -0
- package/dist/runtime/src/mcp/session-comm/history-target.ts +89 -0
- package/dist/runtime/src/mcp/session-comm/history-tools.ts +113 -0
- package/dist/runtime/src/mcp/session-comm/hub-remote-session.ts +553 -0
- package/dist/runtime/src/mcp/session-comm/limits.ts +8 -0
- package/dist/runtime/src/mcp/session-comm/peer-forward.ts +251 -0
- package/dist/runtime/src/mcp/session-comm/runtime.ts +128 -0
- package/dist/runtime/src/mcp/session-comm/server.ts +1036 -0
- package/dist/runtime/src/mcp/session-comm/tell-permissions.ts +79 -0
- package/dist/runtime/src/mcp/session-comm/topic-catalog.ts +155 -0
- package/dist/runtime/src/mcp/session-comm/topic-config.ts +137 -0
- package/dist/runtime/src/mcp/session-comm/topics.ts +313 -0
- package/dist/runtime/src/mcp/session-history-limits.ts +36 -0
- package/dist/runtime/src/mcp/session-history-reader.ts +359 -0
- package/dist/runtime/src/mcp/session-history-server.ts +19 -0
- package/dist/runtime/src/mcp/stdio-protect.ts +7 -0
- package/dist/runtime/src/mcp/system-health-server.ts +5 -0
- package/dist/runtime/src/mcp/task-server.ts +17 -0
- package/dist/runtime/src/mcp/token-stats-server.ts +10 -0
- package/dist/runtime/src/mcp/vault-server.ts +12 -0
- package/dist/runtime/src/mcp/wiki-search-index.ts +359 -0
- package/dist/runtime/src/mcp/wiki-server.ts +2386 -0
- package/dist/runtime/src/mcp-runtime-host.ts +81 -0
- package/dist/runtime/src/media/file-events.ts +33 -0
- package/dist/runtime/src/media/text-extractor.ts +334 -0
- package/dist/runtime/src/media/video.ts +92 -0
- package/dist/runtime/src/node-host.ts +211 -0
- package/dist/runtime/src/outbox/coalescing-runner.ts +157 -0
- package/dist/runtime/src/outbox/file-ops.ts +267 -0
- package/dist/runtime/src/outbox/index.ts +26 -0
- package/dist/runtime/src/outbox/utils.ts +74 -0
- package/dist/runtime/src/platform/background-bash/context.ts +9 -0
- package/dist/runtime/src/platform/background-bash/manager.ts +292 -0
- package/dist/runtime/src/platform/background-bash/output-buffer.ts +323 -0
- package/dist/runtime/src/platform/config-helpers.ts +47 -0
- package/dist/runtime/src/platform/config.ts +618 -0
- package/dist/runtime/src/platform/constants.ts +30 -0
- package/dist/runtime/src/platform/debug-users.ts +14 -0
- package/dist/runtime/src/platform/delay.ts +1 -0
- package/dist/runtime/src/platform/error.ts +9 -0
- package/dist/runtime/src/platform/file-utils.ts +35 -0
- package/dist/runtime/src/platform/ipc.ts +55 -0
- package/dist/runtime/src/platform/jsonl.ts +277 -0
- package/dist/runtime/src/platform/lifecycle.ts +176 -0
- package/dist/runtime/src/platform/log-rotation.ts +45 -0
- package/dist/runtime/src/platform/logger.ts +42 -0
- package/dist/runtime/src/platform/maestro-bootstrap-env.ts +3 -0
- package/dist/runtime/src/platform/mcp-catalog-policy.ts +63 -0
- package/dist/runtime/src/platform/mcp-config.ts +1329 -0
- package/dist/runtime/src/platform/memoize-import.ts +26 -0
- package/dist/runtime/src/platform/modules.ts +145 -0
- package/dist/runtime/src/platform/node-plugins.ts +46 -0
- package/dist/runtime/src/platform/paths.ts +62 -0
- package/dist/runtime/src/platform/playwright/browser-processes.ts +300 -0
- package/dist/runtime/src/platform/playwright/capability.ts +5 -0
- package/dist/runtime/src/platform/playwright/headed-launch.ts +61 -0
- package/dist/runtime/src/platform/playwright/manager-utils.ts +128 -0
- package/dist/runtime/src/platform/playwright/manager.ts +1502 -0
- package/dist/runtime/src/platform/playwright/mcp-transport.ts +35 -0
- package/dist/runtime/src/platform/playwright/profile-management.ts +33 -0
- package/dist/runtime/src/platform/playwright/public-runtime.ts +52 -0
- package/dist/runtime/src/platform/playwright/transport-probe.ts +89 -0
- package/dist/runtime/src/platform/playwright/vault-broker.ts +261 -0
- package/dist/runtime/src/platform/public-runtime.ts +18 -0
- package/dist/runtime/src/prompts/agents/wiki-archiver.md +194 -0
- package/dist/runtime/src/prompts/builders.ts +696 -0
- package/dist/runtime/src/prompts/sessions/_shared-tools.md +38 -0
- package/dist/runtime/src/prompts/sessions/channel-system.md +13 -0
- package/dist/runtime/src/prompts/sessions/manager-system.md +34 -0
- package/dist/runtime/src/prompts/sessions/surfaces/cli.md +6 -0
- package/dist/runtime/src/prompts/sessions/surfaces/otium-manager.md +18 -0
- package/dist/runtime/src/prompts/sessions/surfaces/otium.md +6 -0
- package/dist/runtime/src/prompts/sessions/surfaces/telegram.md +5 -0
- package/dist/runtime/src/prompts/sessions/surfaces/terminal.md +6 -0
- package/dist/runtime/src/prompts/sessions/topic-system.md +9 -0
- package/dist/runtime/src/prompts/sessions/visual-design.md +38 -0
- package/dist/runtime/src/query/active-rooms.ts +514 -0
- package/dist/runtime/src/query/index.ts +23 -0
- package/dist/runtime/src/query/public-runtime.ts +28 -0
- package/dist/runtime/src/query/room-query-registry.ts +217 -0
- package/dist/runtime/src/query/session-inbox-cleanup.ts +45 -0
- package/dist/runtime/src/query/session-inbox-path.ts +42 -0
- package/dist/runtime/src/query/state.ts +116 -0
- package/dist/runtime/src/query/types.ts +8 -0
- package/dist/runtime/src/runtime/actor-topic-reach.ts +123 -0
- package/dist/runtime/src/runtime/actor-topic-scope.ts +255 -0
- package/dist/runtime/src/runtime/ask-callbacks.ts +217 -0
- package/dist/runtime/src/runtime/attachments.ts +149 -0
- package/dist/runtime/src/runtime/background-session-policy.ts +1 -0
- package/dist/runtime/src/runtime/background-sessions.ts +288 -0
- package/dist/runtime/src/runtime/bashrs-completions.ts +350 -0
- package/dist/runtime/src/runtime/channel-context.ts +115 -0
- package/dist/runtime/src/runtime/context-warning.ts +131 -0
- package/dist/runtime/src/runtime/cron-sessions.ts +35 -0
- package/dist/runtime/src/runtime/delivery-ack.ts +112 -0
- package/dist/runtime/src/runtime/errors.ts +70 -0
- package/dist/runtime/src/runtime/event-heartbeat.ts +77 -0
- package/dist/runtime/src/runtime/file-hooks.ts +81 -0
- package/dist/runtime/src/runtime/footer.ts +23 -0
- package/dist/runtime/src/runtime/gateway-capabilities.ts +22 -0
- package/dist/runtime/src/runtime/inbox.ts +1307 -0
- package/dist/runtime/src/runtime/playwright-turn-abort.ts +59 -0
- package/dist/runtime/src/runtime/public-helpers.ts +78 -0
- package/dist/runtime/src/runtime/remote-session-grant.ts +221 -0
- package/dist/runtime/src/runtime/remote-session-inbox.ts +943 -0
- package/dist/runtime/src/runtime/remote-session-reply-outbox.ts +168 -0
- package/dist/runtime/src/runtime/task-format.ts +18 -0
- package/dist/runtime/src/runtime/tasks.ts +53 -0
- package/dist/runtime/src/runtime/thread-context.ts +123 -0
- package/dist/runtime/src/runtime/thread-read.ts +67 -0
- package/dist/runtime/src/runtime/topic-config.ts +29 -0
- package/dist/runtime/src/runtime/turn-event-stream.ts +1051 -0
- package/dist/runtime/src/runtime/turn-runner.ts +2311 -0
- package/dist/runtime/src/runtime/turn-session.ts +116 -0
- package/dist/runtime/src/runtime/usage-alert.ts +51 -0
- package/dist/runtime/src/runtime/user-turn-envelope.ts +94 -0
- package/dist/runtime/src/runtime/visual-html.ts +175 -0
- package/dist/runtime/src/runtime/visual-store.ts +480 -0
- package/dist/runtime/src/runtime/visuals.ts +209 -0
- package/dist/runtime/src/runtime-gateway.ts +1045 -0
- package/dist/runtime/src/security/sanitize.ts +27 -0
- package/dist/runtime/src/security/sensitive-path.ts +41 -0
- package/dist/runtime/src/storage/activity-log.ts +167 -0
- package/dist/runtime/src/storage/api-messages.ts +766 -0
- package/dist/runtime/src/storage/api-topic-brief.ts +134 -0
- package/dist/runtime/src/storage/api-topic-config.ts +197 -0
- package/dist/runtime/src/storage/api-topics.ts +1647 -0
- package/dist/runtime/src/storage/app-settings.ts +50 -0
- package/dist/runtime/src/storage/ask-user-gates.ts +359 -0
- package/dist/runtime/src/storage/browser-profiles.ts +186 -0
- package/dist/runtime/src/storage/conversation-migration.ts +180 -0
- package/dist/runtime/src/storage/conversations.ts +377 -0
- package/dist/runtime/src/storage/decisions.ts +231 -0
- package/dist/runtime/src/storage/forum/agent-settings.ts +20 -0
- package/dist/runtime/src/storage/forum/index.ts +4 -0
- package/dist/runtime/src/storage/forum/repository.ts +283 -0
- package/dist/runtime/src/storage/forum/schema.ts +314 -0
- package/dist/runtime/src/storage/forum-db.ts +4 -0
- package/dist/runtime/src/storage/remote-session.ts +1028 -0
- package/dist/runtime/src/storage/runtime-events.ts +209 -0
- package/dist/runtime/src/storage/runtime-follow-ups.ts +212 -0
- package/dist/runtime/src/storage/runtime-gateway-submissions.ts +132 -0
- package/dist/runtime/src/storage/runtime-leases.ts +190 -0
- package/dist/runtime/src/storage/runtime-process-leases.ts +288 -0
- package/dist/runtime/src/storage/runtime-topic-state.ts +181 -0
- package/dist/runtime/src/storage/runtime-turn-requests.ts +846 -0
- package/dist/runtime/src/storage/self-schedules.ts +306 -0
- package/dist/runtime/src/storage/session-asks.ts +468 -0
- package/dist/runtime/src/storage/session-inbox-signal.ts +76 -0
- package/dist/runtime/src/storage/session-inbox.ts +161 -0
- package/dist/runtime/src/storage/sqlite.ts +154 -0
- package/dist/runtime/src/storage/storage-contract.ts +54 -0
- package/dist/runtime/src/storage/storage-host.ts +336 -0
- package/dist/runtime/src/storage/storage-public-host.ts +16 -0
- package/dist/runtime/src/storage/storage-public.ts +66 -0
- package/dist/runtime/src/storage/tasks.ts +172 -0
- package/dist/runtime/src/storage/token-stats.ts +432 -0
- package/dist/runtime/src/storage/topic-archive-state.ts +226 -0
- package/dist/runtime/src/storage/topic-archive.ts +169 -0
- package/dist/runtime/src/storage/topic-host-mcp-grants.ts +54 -0
- package/dist/runtime/src/storage/topic-link-records.ts +1085 -0
- package/dist/runtime/src/storage/topic-settings.ts +67 -0
- package/dist/runtime/src/storage/topic-tool-capabilities.ts +89 -0
- package/dist/runtime/src/storage/topic-transcript.ts +98 -0
- package/dist/runtime/src/storage/vault-crypto-core.ts +65 -0
- package/dist/runtime/src/storage/vault-crypto.ts +31 -0
- package/dist/runtime/src/storage/vault-public.ts +27 -0
- package/dist/runtime/src/storage/vault-request-gates.ts +359 -0
- package/dist/runtime/src/storage/vault.ts +388 -0
- package/dist/runtime/src/storage/wiki-summary-names.ts +52 -0
- package/dist/runtime/src/storage/wiki.ts +90 -0
- package/dist/runtime/src/topics/claim-abort.ts +190 -0
- package/dist/runtime/src/topics/create.ts +192 -0
- package/dist/runtime/src/topics/derive.ts +600 -0
- package/dist/runtime/src/topics/lifecycle.ts +425 -0
- package/dist/runtime/src/topics/links.ts +7 -0
- package/dist/runtime/src/topics/personal-general.ts +91 -0
- package/dist/runtime/src/topics/session.ts +1041 -0
- package/dist/runtime/src/topics/update.ts +287 -0
- package/dist/runtime/src/types/api.ts +591 -0
- package/dist/runtime/src/types.ts +418 -0
- package/dist/runtime/src/version.ts +1 -0
- package/dist/runtime/tsconfig.json +13 -0
- package/dist/runtime-gateway.js +514 -0
- package/dist/runtime-gateway.js.map +10 -0
- package/dist/runtime-helpers.js +1792 -0
- package/dist/runtime-helpers.js.map +40 -0
- package/dist/sqlite.js +66 -0
- package/dist/sqlite.js.map +10 -0
- package/dist/storage.js +5223 -0
- package/dist/storage.js.map +47 -0
- package/dist/types/apps/negotium/src/mcp-servers.d.ts +20 -0
- package/dist/types/packages/core/src/agents/api-topic-agent-switch.d.ts +38 -0
- package/dist/types/packages/core/src/agents/archiver.d.ts +87 -0
- package/dist/types/packages/core/src/agents/auth-check.d.ts +19 -0
- package/dist/types/packages/core/src/agents/claude-notices.d.ts +15 -0
- package/dist/types/packages/core/src/agents/claude-provider.d.ts +25 -0
- package/dist/types/packages/core/src/agents/claude-registry.d.ts +3 -0
- package/dist/types/packages/core/src/agents/claude-runtime-inspection.d.ts +9 -0
- package/dist/types/packages/core/src/agents/codex-app-server.d.ts +13 -0
- package/dist/types/packages/core/src/agents/codex-native-multi-agent.d.ts +20 -0
- package/dist/types/packages/core/src/agents/codex-provider.d.ts +64 -0
- package/dist/types/packages/core/src/agents/codex-registry.d.ts +3 -0
- package/dist/types/packages/core/src/agents/codex-tier-resolver.d.ts +10 -0
- package/dist/types/packages/core/src/agents/codex-tree-kill.d.ts +13 -0
- package/dist/types/packages/core/src/agents/codex-tree-manager.d.ts +28 -0
- package/dist/types/packages/core/src/agents/codex-vault-hook-bridge.d.ts +28 -0
- package/dist/types/packages/core/src/agents/codex-windows-wrapper.d.ts +30 -0
- package/dist/types/packages/core/src/agents/compaction-support.d.ts +5 -0
- package/dist/types/packages/core/src/agents/compaction-tool-projection.d.ts +9 -0
- package/dist/types/packages/core/src/agents/contracts.d.ts +68 -0
- package/dist/types/packages/core/src/agents/deep-map.d.ts +2 -0
- package/dist/types/packages/core/src/agents/execution-host.d.ts +33 -0
- package/dist/types/packages/core/src/agents/explicit-agent-switch.d.ts +26 -0
- package/dist/types/packages/core/src/agents/fork.d.ts +50 -0
- package/dist/types/packages/core/src/agents/hosted-agent.d.ts +15 -0
- package/dist/types/packages/core/src/agents/idle-archiver.d.ts +35 -0
- package/dist/types/packages/core/src/agents/idle-compact.d.ts +37 -0
- package/dist/types/packages/core/src/agents/index.d.ts +8 -0
- package/dist/types/packages/core/src/agents/maestro-provider.d.ts +75 -0
- package/dist/types/packages/core/src/agents/maestro-registry.d.ts +3 -0
- package/dist/types/packages/core/src/agents/mcp-tools/ask-user.d.ts +147 -0
- package/dist/types/packages/core/src/agents/mcp-tools/common.d.ts +14 -0
- package/dist/types/packages/core/src/agents/mcp-tools/inline-assets.d.ts +24 -0
- package/dist/types/packages/core/src/agents/mcp-tools/publish-html.d.ts +18 -0
- package/dist/types/packages/core/src/agents/mcp-tools/request-vault.d.ts +124 -0
- package/dist/types/packages/core/src/agents/mcp-tools/self-config.d.ts +45 -0
- package/dist/types/packages/core/src/agents/mcp-tools/spawn-subagent.d.ts +148 -0
- package/dist/types/packages/core/src/agents/mcp-tools/visual-compat.d.ts +55 -0
- package/dist/types/packages/core/src/agents/mcp-tools/visuals.d.ts +70 -0
- package/dist/types/packages/core/src/agents/memory-archive-policy.d.ts +24 -0
- package/dist/types/packages/core/src/agents/model-catalog.d.ts +132 -0
- package/dist/types/packages/core/src/agents/public-helpers.d.ts +17 -0
- package/dist/types/packages/core/src/agents/registry.d.ts +5 -0
- package/dist/types/packages/core/src/agents/rollout/claude.d.ts +77 -0
- package/dist/types/packages/core/src/agents/rollout/codex.d.ts +119 -0
- package/dist/types/packages/core/src/agents/rollout/index.d.ts +6 -0
- package/dist/types/packages/core/src/agents/rollout/shared.d.ts +56 -0
- package/dist/types/packages/core/src/agents/self-config-core.d.ts +199 -0
- package/dist/types/packages/core/src/agents/task-events.d.ts +19 -0
- package/dist/types/packages/core/src/agents/tool-format.d.ts +25 -0
- package/dist/types/packages/core/src/agents/topic-cleanup.d.ts +83 -0
- package/dist/types/packages/core/src/agents/vault-tool-policy.d.ts +2 -0
- package/dist/types/packages/core/src/bus.d.ts +141 -0
- package/dist/types/packages/core/src/identifiers.d.ts +27 -0
- package/dist/types/packages/core/src/mcp/canonical-bridge-config.d.ts +21 -0
- package/dist/types/packages/core/src/mcp/factories/agent-health.d.ts +5 -0
- package/dist/types/packages/core/src/mcp/factories/compaction-log.d.ts +8 -0
- package/dist/types/packages/core/src/mcp/factories/decision.d.ts +18 -0
- package/dist/types/packages/core/src/mcp/factories/index.d.ts +13 -0
- package/dist/types/packages/core/src/mcp/factories/session-comm.d.ts +38 -0
- package/dist/types/packages/core/src/mcp/factories/session-history.d.ts +11 -0
- package/dist/types/packages/core/src/mcp/factories/stdio-protection.d.ts +10 -0
- package/dist/types/packages/core/src/mcp/factories/system-health.d.ts +37 -0
- package/dist/types/packages/core/src/mcp/factories/task.d.ts +13 -0
- package/dist/types/packages/core/src/mcp/factories/token-stats.d.ts +20 -0
- package/dist/types/packages/core/src/mcp/factories/vault.d.ts +9 -0
- package/dist/types/packages/core/src/mcp/mcp-helpers.d.ts +26 -0
- package/dist/types/packages/core/src/mcp/peer-bridge.d.ts +92 -0
- package/dist/types/packages/core/src/mcp/runtime-spec.d.ts +147 -0
- package/dist/types/packages/core/src/mcp/session-comm/actor-policy.d.ts +120 -0
- package/dist/types/packages/core/src/mcp/session-comm/bridge-ipc-config.d.ts +6 -0
- package/dist/types/packages/core/src/mcp/session-comm/context.d.ts +44 -0
- package/dist/types/packages/core/src/mcp/session-comm/history-tools.d.ts +31 -0
- package/dist/types/packages/core/src/mcp/session-comm/hub-remote-session.d.ts +149 -0
- package/dist/types/packages/core/src/mcp/session-comm/limits.d.ts +8 -0
- package/dist/types/packages/core/src/mcp/session-comm/peer-forward.d.ts +85 -0
- package/dist/types/packages/core/src/mcp/session-comm/tell-permissions.d.ts +29 -0
- package/dist/types/packages/core/src/mcp/session-comm/topic-catalog.d.ts +59 -0
- package/dist/types/packages/core/src/mcp/session-history-limits.d.ts +20 -0
- package/dist/types/packages/core/src/mcp/session-history-reader.d.ts +35 -0
- package/dist/types/packages/core/src/mcp/wiki-search-index.d.ts +59 -0
- package/dist/types/packages/core/src/mcp/wiki-server.d.ts +53 -0
- package/dist/types/packages/core/src/media/file-events.d.ts +14 -0
- package/dist/types/packages/core/src/media/text-extractor.d.ts +34 -0
- package/dist/types/packages/core/src/outbox/coalescing-runner.d.ts +68 -0
- package/dist/types/packages/core/src/outbox/file-ops.d.ts +79 -0
- package/dist/types/packages/core/src/outbox/index.d.ts +3 -0
- package/dist/types/packages/core/src/outbox/utils.d.ts +16 -0
- package/dist/types/packages/core/src/platform/background-bash/context.d.ts +1 -0
- package/dist/types/packages/core/src/platform/background-bash/manager.d.ts +34 -0
- package/dist/types/packages/core/src/platform/config-helpers.d.ts +20 -0
- package/dist/types/packages/core/src/platform/config.d.ts +178 -0
- package/dist/types/packages/core/src/platform/constants.d.ts +20 -0
- package/dist/types/packages/core/src/platform/delay.d.ts +1 -0
- package/dist/types/packages/core/src/platform/error.d.ts +6 -0
- package/dist/types/packages/core/src/platform/file-utils.d.ts +16 -0
- package/dist/types/packages/core/src/platform/ipc.d.ts +32 -0
- package/dist/types/packages/core/src/platform/jsonl.d.ts +46 -0
- package/dist/types/packages/core/src/platform/lifecycle.d.ts +40 -0
- package/dist/types/packages/core/src/platform/logger.d.ts +20 -0
- package/dist/types/packages/core/src/platform/maestro-bootstrap-env.d.ts +0 -0
- package/dist/types/packages/core/src/platform/mcp-catalog-policy.d.ts +73 -0
- package/dist/types/packages/core/src/platform/mcp-config.d.ts +286 -0
- package/dist/types/packages/core/src/platform/memoize-import.d.ts +15 -0
- package/dist/types/packages/core/src/platform/paths.d.ts +18 -0
- package/dist/types/packages/core/src/platform/playwright/browser-processes.d.ts +60 -0
- package/dist/types/packages/core/src/platform/playwright/capability.d.ts +1 -0
- package/dist/types/packages/core/src/platform/playwright/headed-launch.d.ts +13 -0
- package/dist/types/packages/core/src/platform/playwright/manager-utils.d.ts +34 -0
- package/dist/types/packages/core/src/platform/playwright/manager.d.ts +194 -0
- package/dist/types/packages/core/src/platform/playwright/mcp-transport.d.ts +4 -0
- package/dist/types/packages/core/src/platform/playwright/profile-management.d.ts +8 -0
- package/dist/types/packages/core/src/platform/playwright/public-runtime.d.ts +6 -0
- package/dist/types/packages/core/src/platform/playwright/transport-probe.d.ts +13 -0
- package/dist/types/packages/core/src/platform/playwright/vault-broker.d.ts +6 -0
- package/dist/types/packages/core/src/platform/public-runtime.d.ts +3 -0
- package/dist/types/packages/core/src/prompts/builders.d.ts +78 -0
- package/dist/types/packages/core/src/query/active-rooms.d.ts +240 -0
- package/dist/types/packages/core/src/query/public-runtime.d.ts +4 -0
- package/dist/types/packages/core/src/query/room-query-registry.d.ts +71 -0
- package/dist/types/packages/core/src/query/session-inbox-cleanup.d.ts +5 -0
- package/dist/types/packages/core/src/query/session-inbox-path.d.ts +8 -0
- package/dist/types/packages/core/src/query/state.d.ts +25 -0
- package/dist/types/packages/core/src/query/types.d.ts +6 -0
- package/dist/types/packages/core/src/runtime/actor-topic-reach.d.ts +63 -0
- package/dist/types/packages/core/src/runtime/actor-topic-scope.d.ts +127 -0
- package/dist/types/packages/core/src/runtime/ask-callbacks.d.ts +53 -0
- package/dist/types/packages/core/src/runtime/attachments.d.ts +36 -0
- package/dist/types/packages/core/src/runtime/background-session-policy.d.ts +1 -0
- package/dist/types/packages/core/src/runtime/background-sessions.d.ts +27 -0
- package/dist/types/packages/core/src/runtime/bashrs-completions.d.ts +50 -0
- package/dist/types/packages/core/src/runtime/channel-context.d.ts +13 -0
- package/dist/types/packages/core/src/runtime/context-warning.d.ts +46 -0
- package/dist/types/packages/core/src/runtime/cron-sessions.d.ts +9 -0
- package/dist/types/packages/core/src/runtime/errors.d.ts +6 -0
- package/dist/types/packages/core/src/runtime/event-heartbeat.d.ts +17 -0
- package/dist/types/packages/core/src/runtime/file-hooks.d.ts +38 -0
- package/dist/types/packages/core/src/runtime/playwright-turn-abort.d.ts +12 -0
- package/dist/types/packages/core/src/runtime/public-helpers.d.ts +17 -0
- package/dist/types/packages/core/src/runtime/remote-session-grant.d.ts +91 -0
- package/dist/types/packages/core/src/runtime/remote-session-inbox.d.ts +206 -0
- package/dist/types/packages/core/src/runtime/remote-session-reply-outbox.d.ts +37 -0
- package/dist/types/packages/core/src/runtime/task-format.d.ts +4 -0
- package/dist/types/packages/core/src/runtime/tasks.d.ts +3 -0
- package/dist/types/packages/core/src/runtime/thread-context.d.ts +62 -0
- package/dist/types/packages/core/src/runtime/topic-config.d.ts +24 -0
- package/dist/types/packages/core/src/runtime/turn-event-stream.d.ts +47 -0
- package/dist/types/packages/core/src/runtime/turn-runner.d.ts +284 -0
- package/dist/types/packages/core/src/runtime/turn-session.d.ts +49 -0
- package/dist/types/packages/core/src/runtime/usage-alert.d.ts +11 -0
- package/dist/types/packages/core/src/runtime/user-turn-envelope.d.ts +37 -0
- package/dist/types/packages/core/src/runtime/visual-html.d.ts +13 -0
- package/dist/types/packages/core/src/runtime/visual-store.d.ts +71 -0
- package/dist/types/packages/core/src/runtime/visuals.d.ts +33 -0
- package/dist/types/packages/core/src/runtime-gateway.d.ts +282 -0
- package/dist/types/packages/core/src/security/sanitize.d.ts +16 -0
- package/dist/types/packages/core/src/security/sensitive-path.d.ts +1 -0
- package/dist/types/packages/core/src/storage/activity-log.d.ts +25 -0
- package/dist/types/packages/core/src/storage/api-messages.d.ts +156 -0
- package/dist/types/packages/core/src/storage/api-topic-brief.d.ts +39 -0
- package/dist/types/packages/core/src/storage/api-topic-config.d.ts +23 -0
- package/dist/types/packages/core/src/storage/api-topics.d.ts +274 -0
- package/dist/types/packages/core/src/storage/app-settings.d.ts +4 -0
- package/dist/types/packages/core/src/storage/ask-user-gates.d.ts +65 -0
- package/dist/types/packages/core/src/storage/browser-profiles.d.ts +31 -0
- package/dist/types/packages/core/src/storage/conversations.d.ts +149 -0
- package/dist/types/packages/core/src/storage/decisions.d.ts +45 -0
- package/dist/types/packages/core/src/storage/forum/agent-settings.d.ts +3 -0
- package/dist/types/packages/core/src/storage/forum/index.d.ts +4 -0
- package/dist/types/packages/core/src/storage/forum/repository.d.ts +42 -0
- package/dist/types/packages/core/src/storage/forum/schema.d.ts +38 -0
- package/dist/types/packages/core/src/storage/forum-db.d.ts +2 -0
- package/dist/types/packages/core/src/storage/remote-session.d.ts +318 -0
- package/dist/types/packages/core/src/storage/runtime-events.d.ts +33 -0
- package/dist/types/packages/core/src/storage/runtime-leases.d.ts +27 -0
- package/dist/types/packages/core/src/storage/runtime-process-leases.d.ts +47 -0
- package/dist/types/packages/core/src/storage/runtime-topic-state.d.ts +39 -0
- package/dist/types/packages/core/src/storage/runtime-turn-requests.d.ts +163 -0
- package/dist/types/packages/core/src/storage/self-schedules.d.ts +54 -0
- package/dist/types/packages/core/src/storage/session-asks.d.ts +78 -0
- package/dist/types/packages/core/src/storage/session-inbox-signal.d.ts +35 -0
- package/dist/types/packages/core/src/storage/session-inbox.d.ts +36 -0
- package/dist/types/packages/core/src/storage/sqlite.d.ts +20 -0
- package/dist/types/packages/core/src/storage/storage-contract.d.ts +46 -0
- package/dist/types/packages/core/src/storage/storage-host.d.ts +41 -0
- package/dist/types/packages/core/src/storage/storage-public-host.d.ts +7 -0
- package/dist/types/packages/core/src/storage/storage-public.d.ts +45 -0
- package/dist/types/packages/core/src/storage/tasks.d.ts +49 -0
- package/dist/types/packages/core/src/storage/token-stats.d.ts +69 -0
- package/dist/types/packages/core/src/storage/topic-archive-state.d.ts +33 -0
- package/dist/types/packages/core/src/storage/topic-archive.d.ts +42 -0
- package/dist/types/packages/core/src/storage/topic-host-mcp-grants.d.ts +5 -0
- package/dist/types/packages/core/src/storage/topic-link-records.d.ts +281 -0
- package/dist/types/packages/core/src/storage/topic-settings.d.ts +19 -0
- package/dist/types/packages/core/src/storage/topic-tool-capabilities.d.ts +37 -0
- package/dist/types/packages/core/src/storage/topic-transcript.d.ts +38 -0
- package/dist/types/packages/core/src/storage/vault-crypto-core.d.ts +6 -0
- package/dist/types/packages/core/src/storage/vault-crypto.d.ts +12 -0
- package/dist/types/packages/core/src/storage/vault-public.d.ts +3 -0
- package/dist/types/packages/core/src/storage/vault-request-gates.d.ts +69 -0
- package/dist/types/packages/core/src/storage/vault.d.ts +49 -0
- package/dist/types/packages/core/src/storage/wiki-summary-names.d.ts +13 -0
- package/dist/types/packages/core/src/storage/wiki.d.ts +15 -0
- package/dist/types/packages/core/src/topics/derive.d.ts +83 -0
- package/dist/types/packages/core/src/topics/lifecycle.d.ts +75 -0
- package/dist/types/packages/core/src/topics/links.d.ts +2 -0
- package/dist/types/packages/core/src/topics/personal-general.d.ts +15 -0
- package/dist/types/packages/core/src/topics/session.d.ts +70 -0
- package/dist/types/packages/core/src/types/api.d.ts +551 -0
- package/dist/types/packages/core/src/types.d.ts +405 -0
- package/dist/types/packages/core/src/version.d.ts +1 -0
- package/dist/types/packages/module-cron/src/background-sessions.d.ts +2 -0
- package/dist/types/packages/module-cron/src/context.d.ts +15 -0
- package/dist/types/packages/module-cron/src/host.d.ts +23 -0
- package/dist/types/packages/module-cron/src/index.d.ts +14 -0
- package/dist/types/packages/module-cron/src/module.d.ts +13 -0
- package/dist/types/packages/module-cron/src/schedule.d.ts +23 -0
- package/dist/types/packages/module-cron/src/scheduler.d.ts +46 -0
- package/dist/types/packages/module-cron/src/scripts.d.ts +29 -0
- package/dist/types/packages/module-cron/src/store.d.ts +152 -0
- package/dist/types/packages/module-cron/src/summarize.d.ts +4 -0
- package/dist/vault.js +848 -0
- package/dist/vault.js.map +18 -0
- package/install-bash-rs.mjs +90 -0
- package/install-browser-rs.mjs +83 -0
- package/package.json +171 -4
|
@@ -0,0 +1,1051 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { realpathSync, statSync } from "node:fs";
|
|
3
|
+
import { isAbsolute, resolve } from "node:path";
|
|
4
|
+
import {
|
|
5
|
+
CODEX_TIER_FALLBACKS,
|
|
6
|
+
type CodexTier,
|
|
7
|
+
resolveCodexTierModel,
|
|
8
|
+
} from "#agents/codex-tier-resolver";
|
|
9
|
+
import { estimateTextTokens } from "#agents/compaction-support";
|
|
10
|
+
import { cleanupAgentFork } from "#agents/fork";
|
|
11
|
+
import { scheduleIdleArchiveForTopic } from "#agents/idle-archiver";
|
|
12
|
+
import {
|
|
13
|
+
scheduleContextLimitCompactForTopic,
|
|
14
|
+
scheduleIdleCompactForTopic,
|
|
15
|
+
} from "#agents/idle-compact";
|
|
16
|
+
import { cancelPendingAskUserQuestions } from "#agents/mcp-tools/ask-user";
|
|
17
|
+
import { cancelPendingVaultSecretRequests } from "#agents/mcp-tools/request-vault";
|
|
18
|
+
import {
|
|
19
|
+
settleSubagentFailure,
|
|
20
|
+
settleSubagentSuccess,
|
|
21
|
+
takeSubagentWatch,
|
|
22
|
+
} from "#agents/mcp-tools/spawn-subagent";
|
|
23
|
+
import { resolveModelForAgent } from "#agents/model-catalog";
|
|
24
|
+
import { getRegistry } from "#agents/registry";
|
|
25
|
+
import { formatToolUse, summarizeToolInput } from "#agents/tool-format";
|
|
26
|
+
import { type MessageStreamStatus, WsHub } from "#bus";
|
|
27
|
+
import {
|
|
28
|
+
dispatchPeerRuntimeFile,
|
|
29
|
+
dispatchPeerRuntimeVisual,
|
|
30
|
+
flushPeerRuntimeEvents,
|
|
31
|
+
} from "#mcp/peer-bridge";
|
|
32
|
+
import { logger } from "#platform/logger";
|
|
33
|
+
import {
|
|
34
|
+
clearRoomQuery,
|
|
35
|
+
type DeferredInject,
|
|
36
|
+
getRoomQuery,
|
|
37
|
+
interSessionQueue,
|
|
38
|
+
type RoomQueryControl,
|
|
39
|
+
takeDeferredInject,
|
|
40
|
+
wsAbortReason,
|
|
41
|
+
} from "#query/active-rooms";
|
|
42
|
+
import { clearQueryState } from "#query/state";
|
|
43
|
+
import { AbortReason } from "#query/types";
|
|
44
|
+
import type { AskPending } from "#runtime/ask-callbacks";
|
|
45
|
+
import { workspaceCwdFor } from "#runtime/attachments";
|
|
46
|
+
import { isSessionExpiredError, stringifyError } from "#runtime/errors";
|
|
47
|
+
import { upsertTaskPanelMessage } from "#runtime/tasks";
|
|
48
|
+
import { getTopicConfig } from "#runtime/topic-config";
|
|
49
|
+
import { nextUsageAlert } from "#runtime/usage-alert";
|
|
50
|
+
import {
|
|
51
|
+
normalizeVisualTitle,
|
|
52
|
+
storeTopicMediaVisual,
|
|
53
|
+
storeTopicMermaidVisual,
|
|
54
|
+
storeTopicVisual,
|
|
55
|
+
topicVisualUrl,
|
|
56
|
+
} from "#runtime/visual-store";
|
|
57
|
+
import {
|
|
58
|
+
buildImageHtml,
|
|
59
|
+
buildMermaidHtml,
|
|
60
|
+
buildVideoHtml,
|
|
61
|
+
isPathInside,
|
|
62
|
+
isVisualsShowHtmlTool,
|
|
63
|
+
isVisualsShowImageTool,
|
|
64
|
+
isVisualsShowMermaidTool,
|
|
65
|
+
normalizeMermaidTheme,
|
|
66
|
+
normalizeToolUseId,
|
|
67
|
+
resolveVisualMediaInput,
|
|
68
|
+
stripMermaidFence,
|
|
69
|
+
} from "#runtime/visuals";
|
|
70
|
+
import { isSensitivePath } from "#security/sensitive-path";
|
|
71
|
+
import {
|
|
72
|
+
appendApiMessage,
|
|
73
|
+
softDeleteApiMessage,
|
|
74
|
+
updateApiMessageUsage,
|
|
75
|
+
} from "#storage/api-messages";
|
|
76
|
+
import { getTopic, setTopicSessionId } from "#storage/api-topics";
|
|
77
|
+
import {
|
|
78
|
+
getRuntimeUserTurnRequest,
|
|
79
|
+
markRuntimeUserTurnProviderSessionObserved,
|
|
80
|
+
} from "#storage/runtime-turn-requests";
|
|
81
|
+
import { recordUsage } from "#storage/token-stats";
|
|
82
|
+
import type {
|
|
83
|
+
AgentKind,
|
|
84
|
+
EffortLevel,
|
|
85
|
+
PeerRuntimeBridgeContext,
|
|
86
|
+
TokenUsage,
|
|
87
|
+
UnifiedEvent,
|
|
88
|
+
} from "#types";
|
|
89
|
+
import type { MessageDto } from "#types/api";
|
|
90
|
+
|
|
91
|
+
export type StreamAgentOutcome =
|
|
92
|
+
| { kind: "completed" }
|
|
93
|
+
| { kind: "aborted" }
|
|
94
|
+
| { kind: "budget-capped"; reason: "cost"; usage?: TokenUsage }
|
|
95
|
+
| { kind: "session-expired"; error: string }
|
|
96
|
+
| { kind: "provider-error"; error: string }
|
|
97
|
+
/**
|
|
98
|
+
* The provider's terminal result carried no assistant text at all — the
|
|
99
|
+
* "silent turn" bug: a session-level race (e.g. a queued background-task
|
|
100
|
+
* notification flushing into the CLI session at the same instant a new
|
|
101
|
+
* user turn is dispatched) makes the underlying CLI self-interrupt within
|
|
102
|
+
* ~1-2s and report a trivial zero-usage "success". Distinct from
|
|
103
|
+
* provider-error so the caller can retry once before surfacing a failure —
|
|
104
|
+
* a manual resend has reliably produced a normal reply in every observed
|
|
105
|
+
* case.
|
|
106
|
+
*/
|
|
107
|
+
| { kind: "empty-response"; error: string };
|
|
108
|
+
|
|
109
|
+
export interface TurnEventStreamHooks {
|
|
110
|
+
appendSystemMessage: (topicId: string, text: string) => MessageDto;
|
|
111
|
+
deliverAskCallbackToCaller: (
|
|
112
|
+
pending: AskPending,
|
|
113
|
+
sourceLabel: string,
|
|
114
|
+
body: string,
|
|
115
|
+
kind: "reply" | "error",
|
|
116
|
+
) => Promise<boolean>;
|
|
117
|
+
deliverAskError: (queryId: string, sourceLabel: string, error: string) => Promise<void>;
|
|
118
|
+
redispatchInject: (inject: DeferredInject) => void;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function sessionEventMatchesCurrentExecution(
|
|
122
|
+
topicId: string,
|
|
123
|
+
queryId: string,
|
|
124
|
+
agent: AgentKind,
|
|
125
|
+
model: string,
|
|
126
|
+
): boolean {
|
|
127
|
+
if (getRoomQuery(topicId)?.queryId !== queryId) return false;
|
|
128
|
+
const topic = getTopic(topicId);
|
|
129
|
+
if (!topic || topic.agent !== agent) return false;
|
|
130
|
+
const registry = getRegistry(agent);
|
|
131
|
+
const configuredModel = resolveModelForAgent(
|
|
132
|
+
agent,
|
|
133
|
+
getTopicConfig(topicId)?.model ?? topic.defaultModel,
|
|
134
|
+
registry,
|
|
135
|
+
);
|
|
136
|
+
return configuredModel === model;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export async function runTurnEventStream(
|
|
140
|
+
topicId: string,
|
|
141
|
+
topicTitle: string,
|
|
142
|
+
queryId: string,
|
|
143
|
+
events: AsyncGenerator<UnifiedEvent>,
|
|
144
|
+
control: RoomQueryControl,
|
|
145
|
+
agentType: AgentKind,
|
|
146
|
+
model: string,
|
|
147
|
+
_effort: EffortLevel | undefined,
|
|
148
|
+
userId: string,
|
|
149
|
+
hooks: TurnEventStreamHooks,
|
|
150
|
+
retryableSessionExpired = true,
|
|
151
|
+
onSessionId?: (sessionId: string) => void,
|
|
152
|
+
execution?: {
|
|
153
|
+
silent?: boolean;
|
|
154
|
+
actorUserId?: string;
|
|
155
|
+
peerBridge?: PeerRuntimeBridgeContext;
|
|
156
|
+
sourceNode?: string;
|
|
157
|
+
/** Answer inside this thread instead of the room's main flow (S-13). */
|
|
158
|
+
threadRootId?: string;
|
|
159
|
+
},
|
|
160
|
+
): Promise<StreamAgentOutcome> {
|
|
161
|
+
const { appendSystemMessage, deliverAskCallbackToCaller, deliverAskError, redispatchInject } =
|
|
162
|
+
hooks;
|
|
163
|
+
const abortController = control.abortController;
|
|
164
|
+
const roomId = control.roomId ?? topicId;
|
|
165
|
+
const hub = WsHub.get();
|
|
166
|
+
const silent = execution?.silent ?? control.injectParams?.silent ?? false;
|
|
167
|
+
const peerBridge = execution?.peerBridge ?? control.injectParams?.peerBridge;
|
|
168
|
+
let errorOccurred = false;
|
|
169
|
+
let terminalEmitted = false;
|
|
170
|
+
const streamStartedAt = Date.now();
|
|
171
|
+
let lastEventType: UnifiedEvent["type"] | null = null;
|
|
172
|
+
let pendingSawDelta = false;
|
|
173
|
+
let accumulatedText = "";
|
|
174
|
+
let pendingText = "";
|
|
175
|
+
// Display-only text from `text_preview`, shown after pendingText until the
|
|
176
|
+
// provider's completed `text` for that block arrives.
|
|
177
|
+
let previewText = "";
|
|
178
|
+
let pendingMessage: { id: string; createdAt: string } | null = null;
|
|
179
|
+
let previewTimer: ReturnType<typeof setTimeout> | null = null;
|
|
180
|
+
const clearPreviewTimer = (): void => {
|
|
181
|
+
if (previewTimer) clearTimeout(previewTimer);
|
|
182
|
+
previewTimer = null;
|
|
183
|
+
};
|
|
184
|
+
const scheduleTextPreview = (): void => {
|
|
185
|
+
if (silent || previewTimer || !(pendingText + previewText).trim()) return;
|
|
186
|
+
pendingMessage ??= { id: randomUUID(), createdAt: new Date().toISOString() };
|
|
187
|
+
previewTimer = setTimeout(() => {
|
|
188
|
+
previewTimer = null;
|
|
189
|
+
if (!pendingMessage || abortController.signal.aborted) return;
|
|
190
|
+
try {
|
|
191
|
+
hub.broadcastAiStatus(topicId, {
|
|
192
|
+
kind: "message_stream",
|
|
193
|
+
queryId,
|
|
194
|
+
messageId: pendingMessage.id,
|
|
195
|
+
text: pendingText + previewText,
|
|
196
|
+
createdAt: pendingMessage.createdAt,
|
|
197
|
+
...(execution?.threadRootId ? { threadRootId: execution.threadRootId } : {}),
|
|
198
|
+
} satisfies MessageStreamStatus);
|
|
199
|
+
} catch (error) {
|
|
200
|
+
control.abortReason = AbortReason.Infrastructure;
|
|
201
|
+
control.abortError = stringifyError(error);
|
|
202
|
+
abortController.abort();
|
|
203
|
+
}
|
|
204
|
+
}, 50);
|
|
205
|
+
previewTimer.unref?.();
|
|
206
|
+
};
|
|
207
|
+
let assistantContextTokens = 0;
|
|
208
|
+
let toolContextTokens = 0;
|
|
209
|
+
let lastContextProgressAt = 0;
|
|
210
|
+
let lastVisibleMessageId: string | null = null;
|
|
211
|
+
const visibleMessageIds: string[] = [];
|
|
212
|
+
let lastTaskPanelText: string | null = null;
|
|
213
|
+
let syntheticToolCounter = 0;
|
|
214
|
+
const providerToolIds = new Map<string, string>();
|
|
215
|
+
const anonymousToolIds: string[] = [];
|
|
216
|
+
const handledVisualToolResultIds = new Set<string>();
|
|
217
|
+
const toolSummaryOptions = {
|
|
218
|
+
cwd: control.injectParams?.cwd ?? workspaceCwdFor(topicId),
|
|
219
|
+
};
|
|
220
|
+
// Usage is billed against the real model Codex actually ran (resolved from
|
|
221
|
+
// a bare tier name like "terra" the same way codexProvider resolves it for
|
|
222
|
+
// the API call itself), not the tier name stored on the topic/turn.
|
|
223
|
+
const pricedModel =
|
|
224
|
+
agentType === "codex" && Object.hasOwn(CODEX_TIER_FALLBACKS, model)
|
|
225
|
+
? resolveCodexTierModel(model as CodexTier)
|
|
226
|
+
: model;
|
|
227
|
+
const recordEventUsage = (usage: TokenUsage): void => {
|
|
228
|
+
recordUsage(userId, topicTitle, usage, {
|
|
229
|
+
topicId,
|
|
230
|
+
...(control.sessionId ? { providerSessionId: control.sessionId } : {}),
|
|
231
|
+
agent: agentType,
|
|
232
|
+
model: pricedModel,
|
|
233
|
+
});
|
|
234
|
+
};
|
|
235
|
+
const broadcastContextProgress = (force = false): void => {
|
|
236
|
+
if (silent) return;
|
|
237
|
+
const now = Date.now();
|
|
238
|
+
if (!force && lastContextProgressAt > 0 && now - lastContextProgressAt < 250) return;
|
|
239
|
+
lastContextProgressAt = now;
|
|
240
|
+
hub.broadcastAiStatus(topicId, {
|
|
241
|
+
kind: "context_progress",
|
|
242
|
+
queryId,
|
|
243
|
+
assistantTokens: assistantContextTokens,
|
|
244
|
+
toolTokens: toolContextTokens,
|
|
245
|
+
});
|
|
246
|
+
};
|
|
247
|
+
const nextSyntheticToolUseId = () => `tool-${queryId}-${++syntheticToolCounter}`;
|
|
248
|
+
const bindToolUseId = (providerToolUseId?: string, fallback?: string): string => {
|
|
249
|
+
const providerId = normalizeToolUseId(providerToolUseId);
|
|
250
|
+
if (providerId) {
|
|
251
|
+
const existing = providerToolIds.get(providerId);
|
|
252
|
+
if (existing) return existing;
|
|
253
|
+
const clientId = fallback ?? providerId;
|
|
254
|
+
providerToolIds.set(providerId, clientId);
|
|
255
|
+
return clientId;
|
|
256
|
+
}
|
|
257
|
+
const clientId = fallback ?? nextSyntheticToolUseId();
|
|
258
|
+
anonymousToolIds.push(clientId);
|
|
259
|
+
return clientId;
|
|
260
|
+
};
|
|
261
|
+
const resolveToolResultId = (providerToolUseId: string): string => {
|
|
262
|
+
const providerId = normalizeToolUseId(providerToolUseId);
|
|
263
|
+
if (providerId) {
|
|
264
|
+
const existing = providerToolIds.get(providerId);
|
|
265
|
+
if (existing) return existing;
|
|
266
|
+
const anonymousClientId = anonymousToolIds.shift();
|
|
267
|
+
if (anonymousClientId) {
|
|
268
|
+
providerToolIds.set(providerId, anonymousClientId);
|
|
269
|
+
return anonymousClientId;
|
|
270
|
+
}
|
|
271
|
+
return providerId;
|
|
272
|
+
}
|
|
273
|
+
return anonymousToolIds.shift() ?? nextSyntheticToolUseId();
|
|
274
|
+
};
|
|
275
|
+
const markVisualToolResultHandled = (providerToolUseId?: string) => {
|
|
276
|
+
const providerId = normalizeToolUseId(providerToolUseId);
|
|
277
|
+
if (providerId) handledVisualToolResultIds.add(providerId);
|
|
278
|
+
};
|
|
279
|
+
const isVisualToolResultHandled = (providerToolUseId: string): boolean => {
|
|
280
|
+
const providerId = normalizeToolUseId(providerToolUseId);
|
|
281
|
+
return providerId ? handledVisualToolResultIds.has(providerId) : false;
|
|
282
|
+
};
|
|
283
|
+
const emitPendingAssistantMessage = (usage?: MessageDto["usage"]): MessageDto | null => {
|
|
284
|
+
clearPreviewTimer();
|
|
285
|
+
const identity = pendingMessage;
|
|
286
|
+
pendingMessage = null;
|
|
287
|
+
const text = pendingText.trimEnd();
|
|
288
|
+
pendingText = "";
|
|
289
|
+
previewText = "";
|
|
290
|
+
if (silent || !text.trim()) {
|
|
291
|
+
// A `text_preview` that never got a completed `text` may already be on
|
|
292
|
+
// screen; take it back like a discarded turn's preview.
|
|
293
|
+
if (identity && !silent) {
|
|
294
|
+
hub.broadcastMessageUpdated(topicId, identity.id, { deleted: true, text: "" });
|
|
295
|
+
}
|
|
296
|
+
return null;
|
|
297
|
+
}
|
|
298
|
+
const message: MessageDto = {
|
|
299
|
+
id: identity?.id ?? randomUUID(),
|
|
300
|
+
topicId,
|
|
301
|
+
authorId: "ai",
|
|
302
|
+
text,
|
|
303
|
+
queryId,
|
|
304
|
+
agentType,
|
|
305
|
+
model,
|
|
306
|
+
sourceNode: execution?.sourceNode,
|
|
307
|
+
...(execution?.threadRootId ? { threadRootId: execution.threadRootId } : {}),
|
|
308
|
+
usage,
|
|
309
|
+
createdAt: identity?.createdAt ?? new Date().toISOString(),
|
|
310
|
+
};
|
|
311
|
+
// A threaded turn answers in its thread and leaves the room's ordering and
|
|
312
|
+
// unread state alone, exactly like a human thread reply.
|
|
313
|
+
appendApiMessage(
|
|
314
|
+
message,
|
|
315
|
+
execution?.threadRootId ? { updateTopicLastMessageAt: false } : undefined,
|
|
316
|
+
);
|
|
317
|
+
hub.broadcastMessage(topicId, message);
|
|
318
|
+
lastVisibleMessageId = message.id;
|
|
319
|
+
visibleMessageIds.push(message.id);
|
|
320
|
+
return message;
|
|
321
|
+
};
|
|
322
|
+
const discardVisibleAssistantMessages = (): void => {
|
|
323
|
+
for (const messageId of visibleMessageIds.splice(0)) {
|
|
324
|
+
const deleted = softDeleteApiMessage(topicId, messageId);
|
|
325
|
+
if (deleted) {
|
|
326
|
+
hub.broadcastMessageUpdated(topicId, messageId, { deleted: true, text: "" });
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
lastVisibleMessageId = null;
|
|
330
|
+
};
|
|
331
|
+
const broadcastStoredVisual = (
|
|
332
|
+
event: Extract<UnifiedEvent, { type: "tool_use" }>,
|
|
333
|
+
vizId: number,
|
|
334
|
+
title: string | undefined,
|
|
335
|
+
kind: "html" | "mermaid",
|
|
336
|
+
): void => {
|
|
337
|
+
const toolUseId = bindToolUseId(event.toolUseId, `visual-${vizId}`);
|
|
338
|
+
hub.broadcastToolCall(
|
|
339
|
+
topicId,
|
|
340
|
+
queryId,
|
|
341
|
+
event.name,
|
|
342
|
+
summarizeToolInput(event.name, event.input, toolSummaryOptions),
|
|
343
|
+
formatToolUse(event.name, event.input),
|
|
344
|
+
toolUseId,
|
|
345
|
+
);
|
|
346
|
+
const url = topicVisualUrl(topicId, vizId);
|
|
347
|
+
hub.broadcastVisual(topicId, queryId, url, vizId, title ?? null, kind);
|
|
348
|
+
hub.broadcastToolOutput(topicId, queryId, toolUseId, `Displayed: ${url}`);
|
|
349
|
+
markVisualToolResultHandled(event.toolUseId);
|
|
350
|
+
};
|
|
351
|
+
const broadcastBridgedVisual = (
|
|
352
|
+
event: Extract<UnifiedEvent, { type: "tool_use" }>,
|
|
353
|
+
visual: { id: number; url: string },
|
|
354
|
+
): void => {
|
|
355
|
+
const toolUseId = bindToolUseId(event.toolUseId, `visual-${visual.id}`);
|
|
356
|
+
// The hub bridge already stored and broadcast the visual. Only complete
|
|
357
|
+
// the worker-side tool row; forwarding another visual would duplicate it.
|
|
358
|
+
hub.broadcastToolOutput(topicId, queryId, toolUseId, `Displayed: ${visual.url}`);
|
|
359
|
+
markVisualToolResultHandled(event.toolUseId);
|
|
360
|
+
};
|
|
361
|
+
const failBridgedVisual = (
|
|
362
|
+
event: Extract<UnifiedEvent, { type: "tool_use" }>,
|
|
363
|
+
kind: "html" | "mermaid" | "image" | "video",
|
|
364
|
+
reason: string,
|
|
365
|
+
): void => {
|
|
366
|
+
logger.warn(
|
|
367
|
+
{ topicId, queryId, toolName: event.name, error: reason },
|
|
368
|
+
"peer visual bridge failed",
|
|
369
|
+
);
|
|
370
|
+
hub.broadcastToolOutput(
|
|
371
|
+
topicId,
|
|
372
|
+
queryId,
|
|
373
|
+
bindToolUseId(event.toolUseId),
|
|
374
|
+
`Failed to display ${kind}: ${reason}`,
|
|
375
|
+
true,
|
|
376
|
+
);
|
|
377
|
+
markVisualToolResultHandled(event.toolUseId);
|
|
378
|
+
};
|
|
379
|
+
let outcome: StreamAgentOutcome = { kind: "completed" };
|
|
380
|
+
|
|
381
|
+
try {
|
|
382
|
+
for await (const event of events) {
|
|
383
|
+
if (abortController.signal.aborted) break;
|
|
384
|
+
lastEventType = event.type;
|
|
385
|
+
|
|
386
|
+
// A provider may publish its session id before it appends the user
|
|
387
|
+
// prompt to the native rollout. Only semantic provider output proves
|
|
388
|
+
// that the prompt made it into that session. This distinction matters
|
|
389
|
+
// when a second user message preempts the turn in that short window:
|
|
390
|
+
// the replacement must replay both messages instead of dropping the
|
|
391
|
+
// first one merely because a session event was seen.
|
|
392
|
+
if (
|
|
393
|
+
!control.providerTurnContentObserved &&
|
|
394
|
+
(event.type === "text_delta" ||
|
|
395
|
+
event.type === "text" ||
|
|
396
|
+
event.type === "reasoning" ||
|
|
397
|
+
event.type === "tool_use" ||
|
|
398
|
+
event.type === "tool_result" ||
|
|
399
|
+
event.type === "file" ||
|
|
400
|
+
event.type === "result")
|
|
401
|
+
) {
|
|
402
|
+
control.providerTurnContentObserved = true;
|
|
403
|
+
if (control.sessionId) {
|
|
404
|
+
for (const requestId of control.durableRequestIds ?? []) {
|
|
405
|
+
markRuntimeUserTurnProviderSessionObserved(topicId, requestId, control.sessionId);
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
switch (event.type) {
|
|
411
|
+
case "text_delta": {
|
|
412
|
+
pendingSawDelta = true;
|
|
413
|
+
const incoming = event.content;
|
|
414
|
+
accumulatedText += incoming;
|
|
415
|
+
pendingText += incoming;
|
|
416
|
+
scheduleTextPreview();
|
|
417
|
+
assistantContextTokens = estimateTextTokens(accumulatedText);
|
|
418
|
+
broadcastContextProgress();
|
|
419
|
+
break;
|
|
420
|
+
}
|
|
421
|
+
case "text_preview":
|
|
422
|
+
previewText += event.content;
|
|
423
|
+
scheduleTextPreview();
|
|
424
|
+
break;
|
|
425
|
+
case "text":
|
|
426
|
+
// The completed block replaces whatever preview was shown for it.
|
|
427
|
+
previewText = "";
|
|
428
|
+
if (pendingSawDelta) {
|
|
429
|
+
// Some providers emit tool_use before the completed copy of the
|
|
430
|
+
// streamed text block. A tool flush must not make that copy look
|
|
431
|
+
// like a new segment; consume it here as the delta terminator.
|
|
432
|
+
pendingSawDelta = false;
|
|
433
|
+
break;
|
|
434
|
+
}
|
|
435
|
+
{
|
|
436
|
+
const incoming = event.content;
|
|
437
|
+
accumulatedText += incoming;
|
|
438
|
+
pendingText += incoming;
|
|
439
|
+
scheduleTextPreview();
|
|
440
|
+
assistantContextTokens = estimateTextTokens(accumulatedText);
|
|
441
|
+
broadcastContextProgress();
|
|
442
|
+
}
|
|
443
|
+
break;
|
|
444
|
+
case "tool_use":
|
|
445
|
+
// Provider text is streamed before the tool event that follows it.
|
|
446
|
+
// Persist that completed segment first so every host receives the
|
|
447
|
+
// original assistant → tool → assistant ordering instead of a block
|
|
448
|
+
// of tools followed by one consolidated answer at turn completion.
|
|
449
|
+
emitPendingAssistantMessage();
|
|
450
|
+
toolContextTokens += estimateTextTokens(
|
|
451
|
+
`${event.name}\n${JSON.stringify(event.input ?? {})}`,
|
|
452
|
+
);
|
|
453
|
+
broadcastContextProgress(true);
|
|
454
|
+
// Match both the bare name (Codex reports MCP server/tool separately)
|
|
455
|
+
// and each provider's public MCP-name form.
|
|
456
|
+
if (isVisualsShowHtmlTool(event.name)) {
|
|
457
|
+
const input = event.input as { html?: unknown; title?: unknown };
|
|
458
|
+
if (typeof input.html !== "string" || input.html.trim().length === 0) {
|
|
459
|
+
logger.warn(
|
|
460
|
+
{ topicId, queryId, toolName: event.name },
|
|
461
|
+
"show_html tool_use missing html input; ignoring visual render",
|
|
462
|
+
);
|
|
463
|
+
break;
|
|
464
|
+
}
|
|
465
|
+
if (!silent) {
|
|
466
|
+
const title = normalizeVisualTitle(input.title);
|
|
467
|
+
if (peerBridge) {
|
|
468
|
+
hub.broadcastToolCall(
|
|
469
|
+
topicId,
|
|
470
|
+
queryId,
|
|
471
|
+
event.name,
|
|
472
|
+
summarizeToolInput(event.name, event.input, toolSummaryOptions),
|
|
473
|
+
formatToolUse(event.name, event.input),
|
|
474
|
+
bindToolUseId(event.toolUseId),
|
|
475
|
+
);
|
|
476
|
+
if (!(await flushPeerRuntimeEvents(topicId))) {
|
|
477
|
+
failBridgedVisual(event, "html", "ordered event delivery is blocked");
|
|
478
|
+
break;
|
|
479
|
+
}
|
|
480
|
+
const bridged = await dispatchPeerRuntimeVisual({
|
|
481
|
+
bridge: peerBridge,
|
|
482
|
+
userId,
|
|
483
|
+
agent: agentType,
|
|
484
|
+
model,
|
|
485
|
+
kind: "html",
|
|
486
|
+
title,
|
|
487
|
+
html: input.html,
|
|
488
|
+
});
|
|
489
|
+
if (!bridged?.ok) {
|
|
490
|
+
failBridgedVisual(event, "html", bridged?.error ?? "visual bridge unavailable");
|
|
491
|
+
break;
|
|
492
|
+
}
|
|
493
|
+
broadcastBridgedVisual(event, bridged);
|
|
494
|
+
} else {
|
|
495
|
+
const vizId = storeTopicVisual(topicId, input.html, title, userId);
|
|
496
|
+
broadcastStoredVisual(event, vizId, title, "html");
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
} else if (isVisualsShowMermaidTool(event.name)) {
|
|
500
|
+
const input = event.input as { code?: unknown; title?: unknown; theme?: unknown };
|
|
501
|
+
const code = typeof input.code === "string" ? stripMermaidFence(input.code) : "";
|
|
502
|
+
if (!code) {
|
|
503
|
+
logger.warn(
|
|
504
|
+
{ topicId, queryId, toolName: event.name },
|
|
505
|
+
"show_mermaid tool_use missing code input; ignoring visual render",
|
|
506
|
+
);
|
|
507
|
+
break;
|
|
508
|
+
}
|
|
509
|
+
if (!silent) {
|
|
510
|
+
const title = normalizeVisualTitle(input.title);
|
|
511
|
+
const theme = normalizeMermaidTheme(input.theme);
|
|
512
|
+
if (peerBridge) {
|
|
513
|
+
hub.broadcastToolCall(
|
|
514
|
+
topicId,
|
|
515
|
+
queryId,
|
|
516
|
+
event.name,
|
|
517
|
+
summarizeToolInput(event.name, event.input, toolSummaryOptions),
|
|
518
|
+
formatToolUse(event.name, event.input),
|
|
519
|
+
bindToolUseId(event.toolUseId),
|
|
520
|
+
);
|
|
521
|
+
if (!(await flushPeerRuntimeEvents(topicId))) {
|
|
522
|
+
failBridgedVisual(event, "mermaid", "ordered event delivery is blocked");
|
|
523
|
+
break;
|
|
524
|
+
}
|
|
525
|
+
const bridged = await dispatchPeerRuntimeVisual({
|
|
526
|
+
bridge: peerBridge,
|
|
527
|
+
userId,
|
|
528
|
+
agent: agentType,
|
|
529
|
+
model,
|
|
530
|
+
kind: "mermaid",
|
|
531
|
+
title,
|
|
532
|
+
code,
|
|
533
|
+
theme,
|
|
534
|
+
});
|
|
535
|
+
if (!bridged?.ok) {
|
|
536
|
+
failBridgedVisual(
|
|
537
|
+
event,
|
|
538
|
+
"mermaid",
|
|
539
|
+
bridged?.error ?? "visual bridge unavailable",
|
|
540
|
+
);
|
|
541
|
+
break;
|
|
542
|
+
}
|
|
543
|
+
broadcastBridgedVisual(event, bridged);
|
|
544
|
+
} else {
|
|
545
|
+
const html = buildMermaidHtml(code, theme);
|
|
546
|
+
const vizId = storeTopicMermaidVisual(topicId, code, html, title, userId);
|
|
547
|
+
broadcastStoredVisual(event, vizId, title, "mermaid");
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
} else if (isVisualsShowImageTool(event.name)) {
|
|
551
|
+
const input = event.input as {
|
|
552
|
+
file_path?: unknown;
|
|
553
|
+
file_id?: unknown;
|
|
554
|
+
title?: unknown;
|
|
555
|
+
alt?: unknown;
|
|
556
|
+
};
|
|
557
|
+
const kind = "image" as const;
|
|
558
|
+
const toolUseId = bindToolUseId(event.toolUseId);
|
|
559
|
+
const label = formatToolUse(event.name, event.input);
|
|
560
|
+
const failMediaVisual = (reason: string) => {
|
|
561
|
+
logger.warn(
|
|
562
|
+
{ topicId, queryId, toolName: event.name, error: reason },
|
|
563
|
+
"media visual tool_use could not render visual",
|
|
564
|
+
);
|
|
565
|
+
if (!silent) {
|
|
566
|
+
hub.broadcastToolOutput(
|
|
567
|
+
topicId,
|
|
568
|
+
queryId,
|
|
569
|
+
toolUseId,
|
|
570
|
+
`Failed to display ${kind}: ${reason}`,
|
|
571
|
+
true,
|
|
572
|
+
);
|
|
573
|
+
markVisualToolResultHandled(event.toolUseId);
|
|
574
|
+
}
|
|
575
|
+
};
|
|
576
|
+
if (!silent) {
|
|
577
|
+
hub.broadcastToolCall(
|
|
578
|
+
topicId,
|
|
579
|
+
queryId,
|
|
580
|
+
event.name,
|
|
581
|
+
summarizeToolInput(event.name, event.input, toolSummaryOptions),
|
|
582
|
+
label,
|
|
583
|
+
toolUseId,
|
|
584
|
+
);
|
|
585
|
+
}
|
|
586
|
+
if (silent) break;
|
|
587
|
+
const media = resolveVisualMediaInput(topicId, input);
|
|
588
|
+
if ("error" in media) {
|
|
589
|
+
failMediaVisual(media.error);
|
|
590
|
+
break;
|
|
591
|
+
}
|
|
592
|
+
if (!media.mimeType.startsWith("image/")) {
|
|
593
|
+
failMediaVisual(`expected image media, got ${media.mimeType}`);
|
|
594
|
+
break;
|
|
595
|
+
}
|
|
596
|
+
try {
|
|
597
|
+
const title = normalizeVisualTitle(input.title);
|
|
598
|
+
if (peerBridge) {
|
|
599
|
+
if (!(await flushPeerRuntimeEvents(topicId))) {
|
|
600
|
+
failMediaVisual("ordered event delivery is blocked");
|
|
601
|
+
break;
|
|
602
|
+
}
|
|
603
|
+
const bridged = await dispatchPeerRuntimeVisual({
|
|
604
|
+
bridge: peerBridge,
|
|
605
|
+
userId,
|
|
606
|
+
agent: agentType,
|
|
607
|
+
model,
|
|
608
|
+
kind,
|
|
609
|
+
title,
|
|
610
|
+
fileId: media.fileId,
|
|
611
|
+
mimeType: media.mimeType,
|
|
612
|
+
source: media.source,
|
|
613
|
+
});
|
|
614
|
+
if (!bridged?.ok) {
|
|
615
|
+
failMediaVisual(bridged?.error ?? "visual bridge unavailable");
|
|
616
|
+
break;
|
|
617
|
+
}
|
|
618
|
+
broadcastBridgedVisual(event, bridged);
|
|
619
|
+
break;
|
|
620
|
+
}
|
|
621
|
+
const html =
|
|
622
|
+
kind === "image"
|
|
623
|
+
? buildImageHtml(
|
|
624
|
+
typeof input.alt === "string" ? input.alt : (title ?? "Visual image"),
|
|
625
|
+
)
|
|
626
|
+
: buildVideoHtml(media.mimeType);
|
|
627
|
+
const vizId = storeTopicMediaVisual({
|
|
628
|
+
topicId,
|
|
629
|
+
kind,
|
|
630
|
+
html,
|
|
631
|
+
title,
|
|
632
|
+
fileId: media.fileId,
|
|
633
|
+
mimeType: media.mimeType,
|
|
634
|
+
source: media.source,
|
|
635
|
+
activeUserId: userId,
|
|
636
|
+
});
|
|
637
|
+
const url = topicVisualUrl(topicId, vizId);
|
|
638
|
+
hub.broadcastVisual(topicId, queryId, url, vizId, title ?? null, kind);
|
|
639
|
+
hub.broadcastToolOutput(topicId, queryId, toolUseId, `Displayed: ${url}`);
|
|
640
|
+
markVisualToolResultHandled(event.toolUseId);
|
|
641
|
+
} catch (err) {
|
|
642
|
+
const reason = err instanceof Error ? err.message : String(err);
|
|
643
|
+
failMediaVisual(reason);
|
|
644
|
+
}
|
|
645
|
+
} else {
|
|
646
|
+
// Label is computed server-side (single source of truth) so the
|
|
647
|
+
// client renders the same "Bash(npm test)" convention everywhere.
|
|
648
|
+
const label = formatToolUse(event.name, event.input);
|
|
649
|
+
const toolUseId = bindToolUseId(event.toolUseId);
|
|
650
|
+
if (!silent) {
|
|
651
|
+
hub.broadcastToolCall(
|
|
652
|
+
topicId,
|
|
653
|
+
queryId,
|
|
654
|
+
event.name,
|
|
655
|
+
summarizeToolInput(event.name, event.input, toolSummaryOptions),
|
|
656
|
+
label,
|
|
657
|
+
toolUseId,
|
|
658
|
+
);
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
break;
|
|
662
|
+
case "tool_result":
|
|
663
|
+
toolContextTokens += estimateTextTokens(event.content);
|
|
664
|
+
broadcastContextProgress(true);
|
|
665
|
+
if (isVisualToolResultHandled(event.toolUseId)) break;
|
|
666
|
+
if (!silent) {
|
|
667
|
+
hub.broadcastToolOutput(
|
|
668
|
+
topicId,
|
|
669
|
+
queryId,
|
|
670
|
+
resolveToolResultId(event.toolUseId),
|
|
671
|
+
event.content,
|
|
672
|
+
event.isError,
|
|
673
|
+
);
|
|
674
|
+
}
|
|
675
|
+
break;
|
|
676
|
+
case "file":
|
|
677
|
+
if (!silent && peerBridge) {
|
|
678
|
+
const cwd = workspaceCwdFor(topicId);
|
|
679
|
+
const path = isAbsolute(event.path) ? event.path : resolve(cwd, event.path);
|
|
680
|
+
if (!isPathInside(cwd, path)) {
|
|
681
|
+
logger.warn({ topicId, path }, "peer output file is outside the topic workspace");
|
|
682
|
+
break;
|
|
683
|
+
}
|
|
684
|
+
let safePath: string;
|
|
685
|
+
try {
|
|
686
|
+
safePath = realpathSync(path);
|
|
687
|
+
if (!statSync(safePath).isFile()) {
|
|
688
|
+
logger.warn({ topicId, path }, "peer output path is not a regular file");
|
|
689
|
+
break;
|
|
690
|
+
}
|
|
691
|
+
} catch (error) {
|
|
692
|
+
logger.warn({ topicId, path, error }, "peer output file is unavailable");
|
|
693
|
+
break;
|
|
694
|
+
}
|
|
695
|
+
if (isSensitivePath(safePath)) {
|
|
696
|
+
logger.warn({ topicId, path: safePath }, "peer output file is sensitive");
|
|
697
|
+
break;
|
|
698
|
+
}
|
|
699
|
+
const sent = await dispatchPeerRuntimeFile({
|
|
700
|
+
bridge: peerBridge,
|
|
701
|
+
userId,
|
|
702
|
+
agent: agentType,
|
|
703
|
+
model,
|
|
704
|
+
path: safePath,
|
|
705
|
+
source: event.source,
|
|
706
|
+
});
|
|
707
|
+
if (!sent?.ok)
|
|
708
|
+
logger.warn({ error: sent?.error, path }, "peer output file bridge failed");
|
|
709
|
+
} else if (!silent) {
|
|
710
|
+
hub.broadcastFileReady(topicId, queryId, event.path, event.source);
|
|
711
|
+
}
|
|
712
|
+
break;
|
|
713
|
+
case "result":
|
|
714
|
+
if (event.usage) recordEventUsage(event.usage);
|
|
715
|
+
// Most providers emit assistant text before the terminal result, but
|
|
716
|
+
// accept result-only providers too. Without this fallback a valid
|
|
717
|
+
// final payload was indistinguishable from the silent-turn bug.
|
|
718
|
+
if (!accumulatedText.trim() && event.content.trim()) {
|
|
719
|
+
accumulatedText = event.content;
|
|
720
|
+
pendingText = event.content;
|
|
721
|
+
}
|
|
722
|
+
{
|
|
723
|
+
const usage: MessageDto["usage"] = event.usage
|
|
724
|
+
? {
|
|
725
|
+
input: event.usage.inputTokens,
|
|
726
|
+
output: event.usage.outputTokens,
|
|
727
|
+
cachedInput: event.usage.cacheReadInputTokens,
|
|
728
|
+
context: event.usage.contextTokens,
|
|
729
|
+
contextWindow: event.usage.contextWindow,
|
|
730
|
+
}
|
|
731
|
+
: undefined;
|
|
732
|
+
const finalMessage = emitPendingAssistantMessage(usage);
|
|
733
|
+
// A tool-only ending has no trailing text segment to carry usage.
|
|
734
|
+
// Attach it to the latest visible segment without changing its
|
|
735
|
+
// timeline position; ai_done still carries the same usage to
|
|
736
|
+
// remote hosts that do not accept generic message patches.
|
|
737
|
+
if (!finalMessage && usage && lastVisibleMessageId) {
|
|
738
|
+
const updated = updateApiMessageUsage(topicId, lastVisibleMessageId, usage);
|
|
739
|
+
if (updated) {
|
|
740
|
+
hub.broadcastMessageUpdated(topicId, lastVisibleMessageId, { usage });
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
if (!accumulatedText.trim()) {
|
|
745
|
+
const error = "Provider completed without an assistant response";
|
|
746
|
+
terminalEmitted = true;
|
|
747
|
+
// Silent turns (asks, subagent calls) delivered their error to the
|
|
748
|
+
// caller synchronously below and have no user-facing chat to
|
|
749
|
+
// retry into, so they skip the retry path the caller applies to
|
|
750
|
+
// "empty-response" and go straight to a reported provider-error.
|
|
751
|
+
outcome = silent
|
|
752
|
+
? { kind: "provider-error", error }
|
|
753
|
+
: { kind: "empty-response", error };
|
|
754
|
+
logger.warn(
|
|
755
|
+
{ topicId, queryId, agentType, model, silent },
|
|
756
|
+
"ai: provider completed without assistant text",
|
|
757
|
+
);
|
|
758
|
+
if (silent) void deliverAskError(queryId, topicTitle, error);
|
|
759
|
+
return outcome;
|
|
760
|
+
}
|
|
761
|
+
if (!silent) {
|
|
762
|
+
scheduleIdleArchiveForTopic(topicId, execution?.actorUserId ?? userId);
|
|
763
|
+
scheduleIdleCompactForTopic(topicId, execution?.actorUserId ?? userId);
|
|
764
|
+
if (
|
|
765
|
+
event.usage?.contextTokens !== undefined &&
|
|
766
|
+
event.usage.contextWindow !== undefined
|
|
767
|
+
) {
|
|
768
|
+
scheduleContextLimitCompactForTopic(
|
|
769
|
+
topicId,
|
|
770
|
+
execution?.actorUserId ?? userId,
|
|
771
|
+
event.usage.contextTokens,
|
|
772
|
+
event.usage.contextWindow,
|
|
773
|
+
);
|
|
774
|
+
}
|
|
775
|
+
hub.broadcastDone(
|
|
776
|
+
topicId,
|
|
777
|
+
queryId,
|
|
778
|
+
event.usage
|
|
779
|
+
? {
|
|
780
|
+
input: event.usage.inputTokens,
|
|
781
|
+
output: event.usage.outputTokens,
|
|
782
|
+
cachedInput: event.usage.cacheReadInputTokens,
|
|
783
|
+
context: event.usage.contextTokens,
|
|
784
|
+
contextWindow: event.usage.contextWindow,
|
|
785
|
+
}
|
|
786
|
+
: undefined,
|
|
787
|
+
{ agent: agentType, model },
|
|
788
|
+
);
|
|
789
|
+
// Session-bloat notice: warn once when the provider reports at
|
|
790
|
+
// least 80% context occupancy. Skip subagent rooms (transient).
|
|
791
|
+
const finishedTopic = getTopic(topicId);
|
|
792
|
+
if (event.usage && finishedTopic?.kind !== "manager" && !finishedTopic?.isSubagent) {
|
|
793
|
+
const usageAlert = nextUsageAlert(userId, topicId, topicTitle, event.usage);
|
|
794
|
+
if (usageAlert) appendSystemMessage(topicId, usageAlert);
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
terminalEmitted = true;
|
|
798
|
+
|
|
799
|
+
// ── Ask callback: route reply to the caller topic ──────────
|
|
800
|
+
{
|
|
801
|
+
const { resolveAskCallback } = await import("#runtime/ask-callbacks");
|
|
802
|
+
const pending = resolveAskCallback(queryId);
|
|
803
|
+
if (pending) {
|
|
804
|
+
const sourceLabel = pending.sourceLabel ?? topicTitle;
|
|
805
|
+
const replyText = pending.timedOut
|
|
806
|
+
? `⚠️ [Timeout - reply from ${sourceLabel} took too long and was dropped]`
|
|
807
|
+
: accumulatedText.trim() || "(처리됨, 별도 응답 없음)";
|
|
808
|
+
await deliverAskCallbackToCaller(pending, sourceLabel, replyText, "reply");
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
// ── Subagent settlement: card → completed, result → parent room ──
|
|
813
|
+
{
|
|
814
|
+
const watch = takeSubagentWatch(queryId);
|
|
815
|
+
if (watch) void settleSubagentSuccess(watch, accumulatedText.trim());
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
break;
|
|
819
|
+
case "error":
|
|
820
|
+
if (event.usage) recordEventUsage(event.usage);
|
|
821
|
+
logger.warn(
|
|
822
|
+
{ topicId, queryId, agentType, model, silent, error: event.content },
|
|
823
|
+
"ai: provider returned error",
|
|
824
|
+
);
|
|
825
|
+
terminalEmitted = true;
|
|
826
|
+
errorOccurred = true;
|
|
827
|
+
if (event.code === "budget_exceeded") {
|
|
828
|
+
outcome = { kind: "budget-capped", reason: "cost", usage: event.usage };
|
|
829
|
+
return outcome;
|
|
830
|
+
}
|
|
831
|
+
if (retryableSessionExpired && isSessionExpiredError(event.content)) {
|
|
832
|
+
outcome = { kind: "session-expired", error: event.content };
|
|
833
|
+
return outcome;
|
|
834
|
+
}
|
|
835
|
+
outcome = { kind: "provider-error", error: event.content };
|
|
836
|
+
if (silent) deliverAskError(queryId, topicTitle, event.content);
|
|
837
|
+
return outcome;
|
|
838
|
+
case "status":
|
|
839
|
+
if (!silent) hub.broadcastToolStatus(topicId, queryId, "status", event.content);
|
|
840
|
+
break;
|
|
841
|
+
case "user_message":
|
|
842
|
+
break;
|
|
843
|
+
case "tool_progress":
|
|
844
|
+
if (!silent) {
|
|
845
|
+
const elapsed = Math.max(0, Math.round(event.elapsed));
|
|
846
|
+
// "thinking" is the provider's extended-thinking heartbeat, not a
|
|
847
|
+
// tool — label it as a live thinking state instead of "running".
|
|
848
|
+
const label =
|
|
849
|
+
event.toolName === "thinking"
|
|
850
|
+
? `Thinking… ${elapsed}s`
|
|
851
|
+
: event.toolName === "working"
|
|
852
|
+
? `Working… ${elapsed}s`
|
|
853
|
+
: `${event.toolName} running ${elapsed}s`;
|
|
854
|
+
hub.broadcastToolStatus(topicId, queryId, "progress", label, {
|
|
855
|
+
toolName: event.toolName,
|
|
856
|
+
elapsed,
|
|
857
|
+
});
|
|
858
|
+
}
|
|
859
|
+
break;
|
|
860
|
+
case "tool_use_summary":
|
|
861
|
+
if (!silent) hub.broadcastToolStatus(topicId, queryId, "summary", event.summary);
|
|
862
|
+
break;
|
|
863
|
+
case "reasoning":
|
|
864
|
+
if (!silent) hub.broadcastReasoning(topicId, queryId, event.content);
|
|
865
|
+
break;
|
|
866
|
+
case "tasks":
|
|
867
|
+
if (!silent && getRoomQuery(topicId)?.queryId === queryId) {
|
|
868
|
+
lastTaskPanelText = upsertTaskPanelMessage(
|
|
869
|
+
topicId,
|
|
870
|
+
queryId,
|
|
871
|
+
event.tasks,
|
|
872
|
+
lastTaskPanelText,
|
|
873
|
+
);
|
|
874
|
+
}
|
|
875
|
+
break;
|
|
876
|
+
case "session":
|
|
877
|
+
// Keep the control synchronized with the provider, not merely the
|
|
878
|
+
// topic row. A superseding user message can then resume the partial
|
|
879
|
+
// native rollout after this process has fully unwound.
|
|
880
|
+
control.sessionId = event.sessionId;
|
|
881
|
+
control.providerSessionObserved = true;
|
|
882
|
+
if (control.providerTurnContentObserved) {
|
|
883
|
+
for (const requestId of control.durableRequestIds ?? []) {
|
|
884
|
+
markRuntimeUserTurnProviderSessionObserved(topicId, requestId, event.sessionId);
|
|
885
|
+
}
|
|
886
|
+
}
|
|
887
|
+
// Persist visible topic turns only. Silent ask_session forks use
|
|
888
|
+
// temporary session ids and must not clobber the topic's durable
|
|
889
|
+
// session; visible tell/reply injects do belong to the main topic
|
|
890
|
+
// session, same as user messages.
|
|
891
|
+
if (!silent && onSessionId && getRoomQuery(topicId)?.queryId === queryId) {
|
|
892
|
+
try {
|
|
893
|
+
// A user turn may preempt and requeue this injected turn. Carry
|
|
894
|
+
// the newest provider session into that deferred copy.
|
|
895
|
+
if (control.injectParams) control.injectParams.sessionId = event.sessionId;
|
|
896
|
+
onSessionId(event.sessionId);
|
|
897
|
+
} catch (err) {
|
|
898
|
+
logger.warn(
|
|
899
|
+
{ err, topicId, queryId, agentType },
|
|
900
|
+
"ai: isolated session owner rejected provider session id",
|
|
901
|
+
);
|
|
902
|
+
}
|
|
903
|
+
} else if (
|
|
904
|
+
!silent &&
|
|
905
|
+
sessionEventMatchesCurrentExecution(topicId, queryId, agentType, model)
|
|
906
|
+
) {
|
|
907
|
+
setTopicSessionId(topicId, event.sessionId, {
|
|
908
|
+
reason: "provider-session-event",
|
|
909
|
+
queryId,
|
|
910
|
+
agent: agentType,
|
|
911
|
+
});
|
|
912
|
+
} else if (!silent) {
|
|
913
|
+
logger.info(
|
|
914
|
+
{ topicId, queryId, agentType, model, sessionId: event.sessionId.slice(0, 8) },
|
|
915
|
+
"ai: ignored stale provider session event after execution config changed",
|
|
916
|
+
);
|
|
917
|
+
}
|
|
918
|
+
break;
|
|
919
|
+
}
|
|
920
|
+
}
|
|
921
|
+
|
|
922
|
+
if (!errorOccurred && abortController.signal.aborted) {
|
|
923
|
+
// Keep text already spoken before either an explicit stop or a new user
|
|
924
|
+
// message supersedes the turn. Adapters clean up transient tool status
|
|
925
|
+
// separately when they receive ai_aborted.
|
|
926
|
+
emitPendingAssistantMessage();
|
|
927
|
+
terminalEmitted = true;
|
|
928
|
+
if (control.abortReason === AbortReason.Infrastructure) {
|
|
929
|
+
// Infrastructure loss is a failed turn, not a user stop. Defer the
|
|
930
|
+
// terminal event and settlement to turn-runner's provider-error path
|
|
931
|
+
// so visible turns, asks, subagents, and Cron all receive one
|
|
932
|
+
// consistent error outcome.
|
|
933
|
+
outcome = {
|
|
934
|
+
kind: "provider-error",
|
|
935
|
+
error: control.abortError ?? "Required browser infrastructure failed",
|
|
936
|
+
};
|
|
937
|
+
} else {
|
|
938
|
+
if (!silent) hub.broadcastAborted(topicId, queryId, wsAbortReason(control.abortReason));
|
|
939
|
+
outcome = { kind: "aborted" };
|
|
940
|
+
}
|
|
941
|
+
// Internal abort = this turn is being replaced and (if it was a session
|
|
942
|
+
// inject) re-queued — NOT a failure. Suppress the asker notification so
|
|
943
|
+
// the caller doesn't get a spurious "Aborted" followed by the real reply
|
|
944
|
+
// from the re-queued turn. Only External/stop notifies the asker.
|
|
945
|
+
if (
|
|
946
|
+
control.abortReason !== AbortReason.Internal &&
|
|
947
|
+
control.abortReason !== AbortReason.Infrastructure
|
|
948
|
+
) {
|
|
949
|
+
const abortError = control.abortError ?? "Aborted";
|
|
950
|
+
deliverAskError(queryId, topicTitle, abortError);
|
|
951
|
+
void settleSubagentFailure(queryId, "중지됨 (사용자가 실행을 멈췄습니다)");
|
|
952
|
+
} else {
|
|
953
|
+
// The replay receives a new queryId and registers a fresh callback.
|
|
954
|
+
// Drop the superseded mapping now so it cannot leak until TTL expiry.
|
|
955
|
+
if (control.abortReason === AbortReason.Internal) {
|
|
956
|
+
const { resolveAskCallback } = await import("#runtime/ask-callbacks");
|
|
957
|
+
resolveAskCallback(queryId);
|
|
958
|
+
}
|
|
959
|
+
}
|
|
960
|
+
}
|
|
961
|
+
} catch (err) {
|
|
962
|
+
const msg = stringifyError(err) || "Unknown agent error";
|
|
963
|
+
logger.warn({ err, topicId, queryId, agentType, model, silent }, "ai: stream failed");
|
|
964
|
+
terminalEmitted = true;
|
|
965
|
+
if (retryableSessionExpired && isSessionExpiredError(msg)) {
|
|
966
|
+
outcome = { kind: "session-expired", error: msg };
|
|
967
|
+
} else {
|
|
968
|
+
outcome = { kind: "provider-error", error: msg };
|
|
969
|
+
if (silent) deliverAskError(queryId, topicTitle, msg);
|
|
970
|
+
}
|
|
971
|
+
} finally {
|
|
972
|
+
clearPreviewTimer();
|
|
973
|
+
if (pendingMessage) {
|
|
974
|
+
hub.broadcastMessageUpdated(topicId, pendingMessage.id, { deleted: true, text: "" });
|
|
975
|
+
pendingMessage = null;
|
|
976
|
+
}
|
|
977
|
+
if (!terminalEmitted && !abortController.signal.aborted) {
|
|
978
|
+
const error = "Provider stream ended without a terminal event";
|
|
979
|
+
outcome = { kind: "provider-error", error };
|
|
980
|
+
logger.warn(
|
|
981
|
+
{
|
|
982
|
+
topicId,
|
|
983
|
+
queryId,
|
|
984
|
+
agentType,
|
|
985
|
+
model,
|
|
986
|
+
silent,
|
|
987
|
+
lastEventType,
|
|
988
|
+
hadAssistantOutput: visibleMessageIds.length > 0 || pendingText.length > 0,
|
|
989
|
+
hadToolActivity: syntheticToolCounter > 0 || providerToolIds.size > 0,
|
|
990
|
+
},
|
|
991
|
+
"ai: provider stream ended without result, error, or abort",
|
|
992
|
+
);
|
|
993
|
+
}
|
|
994
|
+
const discardIncompleteSegments =
|
|
995
|
+
outcome.kind === "session-expired" ||
|
|
996
|
+
outcome.kind === "provider-error" ||
|
|
997
|
+
outcome.kind === "empty-response" ||
|
|
998
|
+
(!terminalEmitted && !abortController.signal.aborted);
|
|
999
|
+
if (discardIncompleteSegments) discardVisibleAssistantMessages();
|
|
1000
|
+
// Release the room slot — but only if a newer turn hasn't already taken it
|
|
1001
|
+
// (abort-and-replace sets a fresh control synchronously before this dying
|
|
1002
|
+
// turn's generator unwinds here). Query-state cleanup follows the same
|
|
1003
|
+
// guard so a superseded turn cannot delete its replacement's state file.
|
|
1004
|
+
const stillCurrent = getRoomQuery(roomId)?.queryId === queryId;
|
|
1005
|
+
clearRoomQuery(roomId, queryId);
|
|
1006
|
+
cancelPendingAskUserQuestions(topicId, queryId);
|
|
1007
|
+
cancelPendingVaultSecretRequests(topicId, queryId);
|
|
1008
|
+
if (stillCurrent && roomId === topicId) clearQueryState(userId, topicId, topicTitle);
|
|
1009
|
+
// Stop typing only if this turn still owns the room. A superseded turn may
|
|
1010
|
+
// finish cleanup after its replacement already broadcast a fresh start.
|
|
1011
|
+
if (!silent && stillCurrent) hub.broadcastTyping(topicId, "");
|
|
1012
|
+
const forkHandle = control.injectParams?.forkHandle;
|
|
1013
|
+
const forkRequestId = control.injectParams?.requestId;
|
|
1014
|
+
if (
|
|
1015
|
+
forkHandle &&
|
|
1016
|
+
outcome.kind !== "session-expired" &&
|
|
1017
|
+
outcome.kind !== "empty-response" &&
|
|
1018
|
+
(!forkRequestId || !interSessionQueue.hasRequest(topicId, forkRequestId))
|
|
1019
|
+
) {
|
|
1020
|
+
cleanupAgentFork(forkHandle);
|
|
1021
|
+
}
|
|
1022
|
+
// Drain one deferred session-inject if the room is now idle. If a
|
|
1023
|
+
// replacement turn already occupies the slot, leave it queued — that
|
|
1024
|
+
// turn's finally will drain it when it completes.
|
|
1025
|
+
const queuedUserTurn = roomId === topicId ? getRuntimeUserTurnRequest(topicId) : null;
|
|
1026
|
+
const hasReplacementUserTurn = queuedUserTurn !== null && queuedUserTurn.requestId !== queryId;
|
|
1027
|
+
if (
|
|
1028
|
+
roomId === topicId &&
|
|
1029
|
+
outcome.kind !== "session-expired" &&
|
|
1030
|
+
outcome.kind !== "empty-response" &&
|
|
1031
|
+
!getRoomQuery(topicId) &&
|
|
1032
|
+
!hasReplacementUserTurn
|
|
1033
|
+
) {
|
|
1034
|
+
const next = takeDeferredInject(topicId);
|
|
1035
|
+
if (next) redispatchInject(next);
|
|
1036
|
+
}
|
|
1037
|
+
logger.info(
|
|
1038
|
+
{
|
|
1039
|
+
topicId,
|
|
1040
|
+
queryId,
|
|
1041
|
+
outcomeKind: outcome.kind,
|
|
1042
|
+
assistantMessageCount: visibleMessageIds.length,
|
|
1043
|
+
assistantTextChars: accumulatedText.length,
|
|
1044
|
+
durationMs: Date.now() - streamStartedAt,
|
|
1045
|
+
lastEventType,
|
|
1046
|
+
},
|
|
1047
|
+
"ai: turn settled",
|
|
1048
|
+
);
|
|
1049
|
+
}
|
|
1050
|
+
return outcome;
|
|
1051
|
+
}
|