@tea-agent/loop-agent 0.1.0-fe-test.4 → 0.1.0-fe-test.6
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/AGENTS.md +1 -1
- package/CHANGELOG.md +442 -2524
- package/README.md +19 -12
- package/dist/adapters/loop-agent.js +34 -0
- package/dist/application/dag/args.js +8 -0
- package/dist/application/dag/run-dag.js +5 -0
- package/dist/application/evaluation/budget.js +40 -1
- package/dist/application/evaluation/corpus-hash.js +10 -15
- package/dist/application/evaluation/corpus.js +2 -1
- package/dist/application/evaluation/frontend-browser-acceptance.js +77 -0
- package/dist/application/evaluation/frontend-corpus-browser-acceptance.js +175 -0
- package/dist/application/evaluation/frontend-gateway-evidence.js +226 -0
- package/dist/application/evaluation/frontend-pair-registration.js +143 -0
- package/dist/application/evaluation/frontend-paired-summary.js +150 -0
- package/dist/application/evaluation/frontend-run-observation.js +144 -0
- package/dist/application/evaluation/frontend-shared-evidence.js +105 -0
- package/dist/application/evaluation/types.js +45 -12
- package/dist/application/task-lifecycle/advance.js +64 -15
- package/dist/application/task-lifecycle/observe.js +69 -30
- package/dist/application/task-lifecycle/plan-transitions.js +5 -4
- package/dist/build-stamp.json +3 -3
- package/dist/cli/command-definitions.js +18 -5
- package/dist/cli/program.js +8 -5
- package/dist/cli/update/init-surface-notifier.js +2 -0
- package/dist/cli/update/policy.js +2 -0
- package/dist/commands/client-recovery.js +20 -55
- package/dist/commands/cursor-prompt.js +2 -0
- package/dist/commands/dag-approve.js +2 -0
- package/dist/commands/dag-final-verification.js +2 -0
- package/dist/commands/dag-init-hybrid.js +2 -0
- package/dist/commands/dag-reconcile-tasks.js +2 -0
- package/dist/commands/dag-reject.js +2 -0
- package/dist/commands/dag-report.js +2 -0
- package/dist/commands/dag-rerun.js +2 -0
- package/dist/commands/dag-resume.js +2 -0
- package/dist/commands/dag-workflow-compile.js +2 -0
- package/dist/commands/dag-workflow-plan.js +2 -0
- package/dist/commands/dag-workflow-validate.js +2 -0
- package/dist/commands/delegate.js +2 -0
- package/dist/commands/examples.js +2 -0
- package/dist/commands/import-prd.js +2 -0
- package/dist/commands/init-legacy-audit.js +330 -0
- package/dist/commands/init-model-catalog.js +2 -0
- package/dist/commands/init-upgrade.js +644 -104
- package/dist/commands/init.js +700 -338
- package/dist/commands/instructions.js +2 -0
- package/dist/commands/knowledge.js +2 -0
- package/dist/commands/loop-benchmark.js +2 -0
- package/dist/commands/operator-guide.js +99 -0
- package/dist/commands/operator.js +16 -3
- package/dist/commands/pi-prompt.js +2 -0
- package/dist/commands/pi-reuse-benchmark.js +2 -0
- package/dist/commands/promote-run.js +2 -0
- package/dist/commands/stats.js +1 -1
- package/dist/commands/study-init.js +2 -0
- package/dist/commands/task-advance.js +32 -0
- package/dist/commands/task-source-prepare.js +18 -2
- package/dist/commands/worktree-create.js +2 -0
- package/dist/commands/worktree-remove.js +2 -0
- package/dist/executors/dag-pi/guards.js +4 -0
- package/dist/executors/dag-pi/model-config.js +30 -0
- package/dist/executors/dag-pi/plan/fact-adoption.js +55 -0
- package/dist/executors/dag-pi/plan/facts.js +180 -0
- package/dist/executors/dag-pi/plan/finalize-tools.js +259 -0
- package/dist/executors/dag-pi/plan/ledger-contract.js +55 -0
- package/dist/executors/dag-pi/plan/record-tools.js +861 -0
- package/dist/executors/dag-pi/plan/schema.js +129 -0
- package/dist/executors/dag-pi/plan/tool-helpers.js +16 -0
- package/dist/executors/dag-pi/sessions/index.js +6 -0
- package/dist/executors/dag-pi/sessions/plan-batches.js +280 -0
- package/dist/executors/dag-pi/sessions/plan-prompts.js +367 -0
- package/dist/executors/dag-pi/sessions/scout-parallel.js +197 -0
- package/dist/executors/dag-pi/sessions/segmented-plan.js +1184 -0
- package/dist/executors/dag-pi/sessions/writer-evidence.js +60 -0
- package/dist/executors/dag-pi/tools/contract-tools.js +515 -0
- package/dist/executors/dag-pi/tools/design-terminal-tools.js +159 -0
- package/dist/executors/dag-pi/tools/plan-decision-tools.js +529 -0
- package/dist/executors/dag-pi/tools/review-terminal-tools.js +159 -0
- package/dist/executors/dag-pi/tools/scout-evidence-tools.js +270 -0
- package/dist/executors/dag-pi-executor.js +1517 -3180
- package/dist/executors/pi-executor.js +112 -4
- package/dist/executors/pi-sdk-executor.js +258 -44
- package/dist/executors/pi-writer-tool-policy.js +60 -4
- package/dist/executors/playwright-cli-launcher.js +31 -45
- package/dist/executors/shell-executor.js +358 -48
- package/dist/executors/shell-presets.js +1 -1
- package/dist/executors/shell-verification.js +3 -0
- package/dist/executors/shell-write-guard.js +77 -102
- package/dist/governance/exec-plans.js +16 -4
- package/dist/governance/harness.js +2 -7
- package/dist/governance/manifest-types.js +28 -9
- package/dist/governance/project-capability-scanner.js +263 -0
- package/dist/governance/project-config.js +96 -0
- package/dist/governance/resource-catalog.js +151 -0
- package/dist/infrastructure/console/app-data.js +12 -2
- package/dist/infrastructure/console/operation-store.js +48 -14
- package/dist/infrastructure/evaluation/corpus-store.js +37 -12
- package/dist/shared/artifacts-core.js +2 -0
- package/dist/shared/backend-dogfood-preflight.js +47 -0
- package/dist/shared/dag-failure-category.js +9 -0
- package/dist/shared/file-identity.js +23 -0
- package/dist/shared/frontend-execution-policy.js +23 -0
- package/dist/shared/git-progress.js +2 -0
- package/dist/shared/git-workspace-status.js +482 -0
- package/dist/shared/one-shot-prompt-args.js +2 -0
- package/dist/shared/operator/capabilities.js +185 -0
- package/dist/shared/package-metadata.js +29 -11
- package/dist/shared/path-safety.js +2 -2
- package/dist/shared/pi-context-pressure/extension.js +49 -16
- package/dist/shared/pi-context-pressure/index.js +1 -0
- package/dist/shared/pi-context-pressure/sift-bridge.js +1 -1
- package/dist/shared/pi-context-pressure/telemetry.js +4 -0
- package/dist/shared/pi-context-pressure/tool-result-pruner.js +98 -0
- package/dist/shared/pi-provider-recovery.js +182 -0
- package/dist/shared/pi-retry-settings.js +40 -0
- package/dist/shared/reference-context.js +2 -0
- package/dist/shared/update/npm-client.js +49 -1
- package/dist/shared/wizard-local-telemetry.js +51 -0
- package/dist/task/config-types.js +51 -2
- package/dist/task/contract/apply.js +36 -2
- package/dist/task/contract/project.js +22 -0
- package/dist/task/contract/recover.js +6 -0
- package/dist/task/contract/schema.js +12 -0
- package/dist/task/frontend-preflight.js +1 -1
- package/dist/task/source-prepare/artifact-meta.js +9 -2
- package/dist/task/source-prepare/build-draft.js +61 -0
- package/dist/task/source-prepare/fragment-inventory.js +4 -1
- package/dist/task/source-prepare/ledger-reconciliation.js +2 -2
- package/dist/task/source-prepare/ledger-review.js +6 -9
- package/dist/task/source-prepare/parse-intent.js +165 -14
- package/dist/task/source-prepare/path-policy.js +17 -7
- package/dist/task/source-prepare/prepare.js +6 -1
- package/dist/task/source-prepare/semantic-intake.js +33 -28
- package/dist/task/source-prepare/source-execution.js +65 -0
- package/dist/task/source-prepare/source-fidelity-pi.js +49 -17
- package/dist/task/source-prepare/source-provider-budget.js +290 -0
- package/dist/task/source-references.js +48 -23
- package/dist/task/task-demand-routing.js +5 -2
- package/dist/worker/cli.js +3 -5
- package/dist/worker/console/chat/assistant-content.js +5 -13
- package/dist/worker/console/chat/browser-automation.js +937 -0
- package/dist/worker/console/chat/browser-content.js +74 -0
- package/dist/worker/console/chat/browser-diagnostics.js +250 -0
- package/dist/worker/console/chat/browser-evaluation.js +58 -0
- package/dist/worker/console/chat/browser-operation-queue.js +19 -0
- package/dist/worker/console/chat/browser-pick.js +113 -0
- package/dist/worker/console/chat/browser-policy.js +113 -0
- package/dist/worker/console/chat/browser-routes.js +329 -0
- package/dist/worker/console/chat/browser-screenshot-result.js +47 -0
- package/dist/worker/console/chat/browser-storage.js +89 -0
- package/dist/worker/console/chat/chat-event-store.js +73 -18
- package/dist/worker/console/chat/context-insights.js +788 -0
- package/dist/worker/console/chat/conversation-nodes.js +383 -0
- package/dist/worker/console/chat/db-connections.js +854 -0
- package/dist/worker/console/chat/explore-tools.js +18 -2
- package/dist/worker/console/chat/pet/pet-affinity.js +169 -0
- package/dist/worker/console/chat/pet/pet-persist.js +109 -0
- package/dist/worker/console/chat/pet/pet-registry.js +142 -0
- package/dist/worker/console/chat/pet/pet-remarks.js +48 -0
- package/dist/worker/console/chat/pet/pet-routes.js +125 -0
- package/dist/worker/console/chat/pet/pet-service.js +262 -0
- package/dist/worker/console/chat/pet/pet-state.js +99 -0
- package/dist/worker/console/chat/pi-mode-loop-isolation.js +170 -0
- package/dist/worker/console/chat/pi-runtime/custom-tools/ask-user-question.js +44 -0
- package/dist/worker/console/chat/pi-runtime/custom-tools/browser-tools.js +135 -0
- package/dist/worker/console/chat/pi-runtime/custom-tools/explore-tools.js +202 -0
- package/dist/worker/console/chat/pi-runtime/custom-tools/goal-tools.js +79 -0
- package/dist/worker/console/chat/pi-runtime/custom-tools/operator-tools.js +94 -0
- package/dist/worker/console/chat/pi-runtime/custom-tools/scheduled-goal-tools.js +22 -0
- package/dist/worker/console/chat/pi-runtime/custom-tools/sql-tools.js +670 -0
- package/dist/worker/console/chat/pi-runtime/custom-tools/todo-write.js +38 -0
- package/dist/worker/console/chat/pi-runtime/inspection-types.js +28 -0
- package/dist/worker/console/chat/pi-runtime/inspection.js +439 -0
- package/dist/worker/console/chat/pi-runtime/progressive-tools.js +195 -0
- package/dist/worker/console/chat/pi-runtime/sdk-bindings.js +153 -0
- package/dist/worker/console/chat/pi-runtime/sdk-session.js +5 -0
- package/dist/worker/console/chat/pi-runtime.js +793 -1230
- package/dist/worker/console/chat/provider-error.js +0 -12
- package/dist/worker/console/chat/repo-browser.js +132 -6
- package/dist/worker/console/chat/repo-preview-kind.js +155 -0
- package/dist/worker/console/chat/resource-loader.js +4 -1
- package/dist/worker/console/chat/routes.js +861 -148
- package/dist/worker/console/chat/scheduled-goal-booking.js +59 -0
- package/dist/worker/console/chat/scheduled-goal-delivery.js +27 -0
- package/dist/worker/console/chat/scheduled-goal-request.js +198 -0
- package/dist/worker/console/chat/scm-routes.js +217 -0
- package/dist/worker/console/chat/scm-service.js +283 -0
- package/dist/worker/console/chat/scm-tools.js +111 -0
- package/dist/worker/console/chat/sdd-data-alignment.js +85 -41
- package/dist/worker/console/chat/session-catalog.js +32 -0
- package/dist/worker/console/chat/session-mode-view.js +49 -0
- package/dist/worker/console/chat/session-mode.js +224 -0
- package/dist/worker/console/chat/session-store.js +232 -32
- package/dist/worker/console/chat/shortcuts.js +13 -0
- package/dist/worker/console/chat/subagents/agent-tool.js +62 -0
- package/dist/worker/console/chat/subagents/explore-agent.js +169 -0
- package/dist/worker/console/chat/subagents/index.js +5 -0
- package/dist/worker/console/chat/subagents/orchestrator.js +273 -0
- package/dist/worker/console/chat/subagents/tool-policy.js +53 -0
- package/dist/worker/console/chat/subagents/types.js +13 -0
- package/dist/worker/console/chat/terminal-routes.js +216 -0
- package/dist/worker/console/chat/terminal-sessions.js +348 -0
- package/dist/worker/console/chat/terminal-tools.js +211 -0
- package/dist/worker/console/chat/timeline-message-order.js +22 -0
- package/dist/worker/console/chat/tool-preview.js +136 -0
- package/dist/worker/console/chat/tools.js +69 -1
- package/dist/worker/console/chat/turn-order.js +13 -0
- package/dist/worker/console/chat/turn-process.js +210 -36
- package/dist/worker/console/chat/user-turn-outline.js +19 -0
- package/dist/worker/console/console-shutdown.js +34 -0
- package/dist/worker/console/console-update-and-init.js +1 -19
- package/dist/worker/console/console-update-runtime.js +7 -22
- package/dist/worker/console/dag-confirmation.js +3 -0
- package/dist/worker/console/dag-execution-receipt.js +3 -0
- package/dist/worker/console/interview/tools.js +1 -0
- package/dist/worker/console/operator-actions.js +95 -4
- package/dist/worker/console/operator-user-error.js +28 -1
- package/dist/worker/console/pi-readiness.js +4 -4
- package/dist/worker/console/prd-intake-bridge.js +47 -1
- package/dist/worker/console/recovery-cta.js +14 -14
- package/dist/worker/console/routes.js +13 -0
- package/dist/worker/console/scheduled-goal-host.js +98 -0
- package/dist/worker/console/scheduled-goal-operation-adapter.js +127 -0
- package/dist/worker/console/server.js +33 -31
- package/dist/worker/console/static/app-icon.svg +39 -0
- package/dist/worker/console/static/assets/{abnfDiagram-N423BO3Z-BZogYNl1.js → abnfDiagram-N423BO3Z-DASc69AM.js} +1 -1
- package/dist/worker/console/static/assets/{arc-DcJTLe2v.js → arc-h09p-LAc.js} +1 -1
- package/dist/worker/console/static/assets/{architectureDiagram-T3A2C74G-B39EQQ9n.js → architectureDiagram-T3A2C74G-CZ3dtLkh.js} +1 -1
- package/dist/worker/console/static/assets/{blockDiagram-VBNYF7ZC-CitCGV56.js → blockDiagram-VBNYF7ZC-DMrRhMXG.js} +1 -1
- package/dist/worker/console/static/assets/{c4Diagram-5PPSVZJV-rvWuOHLG.js → c4Diagram-5PPSVZJV-DQxisHV6.js} +1 -1
- package/dist/worker/console/static/assets/channel-D1zgKZfg.js +1 -0
- package/dist/worker/console/static/assets/{chunk-2GRJ4B5K-DGXplIJs.js → chunk-2GRJ4B5K-CefVN9zh.js} +1 -1
- package/dist/worker/console/static/assets/{chunk-2Q5K7J3B-ur9OqjPn.js → chunk-2Q5K7J3B-DmR1Koq2.js} +1 -1
- package/dist/worker/console/static/assets/{chunk-5RXB4S5H-D1KqU_Vi.js → chunk-5RXB4S5H-NlzrSW7L.js} +1 -1
- package/dist/worker/console/static/assets/{chunk-5VM5RSS4-BlVZrbtM.js → chunk-5VM5RSS4-DfelJv-0.js} +1 -1
- package/dist/worker/console/static/assets/{chunk-6Q2QTUOP-BAzz3nQy.js → chunk-6Q2QTUOP-BK6RFy58.js} +1 -1
- package/dist/worker/console/static/assets/{chunk-GF5L2VYU-CP_nYuCV.js → chunk-GF5L2VYU-BAeh9lz4.js} +1 -1
- package/dist/worker/console/static/assets/{chunk-JWPE2WC7-CWvKQ80X.js → chunk-JWPE2WC7-ChPS5WzG.js} +1 -1
- package/dist/worker/console/static/assets/{chunk-KBJHAD2P-CyUki8wp.js → chunk-KBJHAD2P-BoVe5IM6.js} +1 -1
- package/dist/worker/console/static/assets/{chunk-RYQCIY6F-5NYzNKfY.js → chunk-RYQCIY6F-C4q_0Lw9.js} +1 -1
- package/dist/worker/console/static/assets/{chunk-XXDRQBXY-DHdUgjS-.js → chunk-XXDRQBXY-Bb9TvjeY.js} +1 -1
- package/dist/worker/console/static/assets/classDiagram-JCYQIIEL-CXNNsY2M.js +1 -0
- package/dist/worker/console/static/assets/classDiagram-v2-OCEON4UE-CXNNsY2M.js +1 -0
- package/dist/worker/console/static/assets/{cose-bilkent-JH36ORCC-ANQ5g_t6.js → cose-bilkent-JH36ORCC-DmNnL8Ff.js} +1 -1
- package/dist/worker/console/static/assets/{cynefin-VYW2F7L2-CruT_4-0.js → cynefin-VYW2F7L2-Contvx58.js} +1 -1
- package/dist/worker/console/static/assets/{cynefinDiagram-MW4NZA55-rbFo77ID.js → cynefinDiagram-MW4NZA55-Cigq7cjk.js} +1 -1
- package/dist/worker/console/static/assets/{dagre-VZM6K2ZE-asstxBQs.js → dagre-VZM6K2ZE-dEbC0nxb.js} +1 -1
- package/dist/worker/console/static/assets/{diagram-7IWD3JNH-DzTyBZwI.js → diagram-7IWD3JNH-CzIztESV.js} +1 -1
- package/dist/worker/console/static/assets/{diagram-B4RE2ZJO-gw8ccH3B.js → diagram-B4RE2ZJO-Bo14zbaY.js} +1 -1
- package/dist/worker/console/static/assets/{diagram-LBJQPF4R-B8RpZzOf.js → diagram-LBJQPF4R-C4xUyil6.js} +1 -1
- package/dist/worker/console/static/assets/{diagram-Q27KOJAE-B1zJkCS9.js → diagram-Q27KOJAE-CaBY_uYa.js} +1 -1
- package/dist/worker/console/static/assets/{diagram-UB23O5K3-B6JLWZmO.js → diagram-UB23O5K3-ClUXUeiu.js} +1 -1
- package/dist/worker/console/static/assets/{ebnfDiagram-BXEA7PRR-q2iZP-Ps.js → ebnfDiagram-BXEA7PRR-BbGCgjC6.js} +1 -1
- package/dist/worker/console/static/assets/{erDiagram-JOGREHBK-B3dbbktQ.js → erDiagram-JOGREHBK-Dr-AcwSl.js} +1 -1
- package/dist/worker/console/static/assets/{flowDiagram-UKHOOZJN-C-zc6-kB.js → flowDiagram-UKHOOZJN-BG8Gcfyc.js} +1 -1
- package/dist/worker/console/static/assets/{ganttDiagram-PKOTCBZU-BSolnot9.js → ganttDiagram-PKOTCBZU-CFVaPs5r.js} +1 -1
- package/dist/worker/console/static/assets/{gitGraphDiagram-DS77QQ5N-Bo8jGxTk.js → gitGraphDiagram-DS77QQ5N-ChJ8pm-O.js} +1 -1
- package/dist/worker/console/static/assets/index-B_5GhVIO.js +488 -0
- package/dist/worker/console/static/assets/index-C7YMBluX.css +1 -0
- package/dist/worker/console/static/assets/{infoDiagram-6WML65LV-DQaZqJOH.js → infoDiagram-6WML65LV-C5J9-uCZ.js} +1 -1
- package/dist/worker/console/static/assets/{ishikawaDiagram-WSZJBQD7-DsdX1WnE.js → ishikawaDiagram-WSZJBQD7-BcfjqzDv.js} +1 -1
- package/dist/worker/console/static/assets/{journeyDiagram-NVQOT4AX-CP5EYDZL.js → journeyDiagram-NVQOT4AX-BzzCO-rD.js} +1 -1
- package/dist/worker/console/static/assets/{kanban-definition-27J2QSJJ-DAm14Dr8.js → kanban-definition-27J2QSJJ-NCHUZ46h.js} +1 -1
- package/dist/worker/console/static/assets/{linear-gZECoLc5.js → linear-BKZlWCUU.js} +1 -1
- package/dist/worker/console/static/assets/{mermaid.core-BRJNIM6p.js → mermaid.core-DbTaQjFE.js} +5 -5
- package/dist/worker/console/static/assets/{mindmap-definition-FAOFIHXS-Cja3kLkR.js → mindmap-definition-FAOFIHXS-CroYl5xQ.js} +1 -1
- package/dist/worker/console/static/assets/{pegDiagram-VL7TDLO6-22ZGFisL.js → pegDiagram-VL7TDLO6-DgMBx-9k.js} +1 -1
- package/dist/worker/console/static/assets/{pieDiagram-7S7Q4E2Y-BXin0Y1o.js → pieDiagram-7S7Q4E2Y-CyWDmbq1.js} +1 -1
- package/dist/worker/console/static/assets/{quadrantDiagram-CIZ2JOQS-Cw6T69Yt.js → quadrantDiagram-CIZ2JOQS-BgGEOqSP.js} +1 -1
- package/dist/worker/console/static/assets/{railroadDiagram-AXF67PYL-C1-gTieN.js → railroadDiagram-AXF67PYL-BMZ7oeeV.js} +1 -1
- package/dist/worker/console/static/assets/{requirementDiagram-LRYGKXZP--H5N2iI1.js → requirementDiagram-LRYGKXZP-DxqyGhcc.js} +1 -1
- package/dist/worker/console/static/assets/{sankeyDiagram-W5VNT64P-CgWAPeRz.js → sankeyDiagram-W5VNT64P-DnSvNCG8.js} +1 -1
- package/dist/worker/console/static/assets/{sequenceDiagram-SI44F4Z6-CrKqzGvt.js → sequenceDiagram-SI44F4Z6-BiZh6f-R.js} +1 -1
- package/dist/worker/console/static/assets/{sizeCapture-X5ZJPWSS-BB3wG06K.js → sizeCapture-X5ZJPWSS-BKXSI8WB.js} +1 -1
- package/dist/worker/console/static/assets/{stateDiagram-OKZ733FA-DjsGgdgY.js → stateDiagram-OKZ733FA-CfSgoYFm.js} +1 -1
- package/dist/worker/console/static/assets/stateDiagram-v2-UEYNNEHI-DgHi3vjb.js +1 -0
- package/dist/worker/console/static/assets/{swimlanes-SLNWSIFB-DYKEbvAR.js → swimlanes-SLNWSIFB-CyGADcHe.js} +2 -2
- package/dist/worker/console/static/assets/swimlanesDiagram-ULZ7WXOC-Bgvdt1Y0.js +8 -0
- package/dist/worker/console/static/assets/{timeline-definition-Z64GVDOM-eQOb5k9Y.js → timeline-definition-Z64GVDOM-HmPaSPOK.js} +1 -1
- package/dist/worker/console/static/assets/{vennDiagram-T6HMQDX7-B2-l9FIu.js → vennDiagram-T6HMQDX7-VA2VBbez.js} +1 -1
- package/dist/worker/console/static/assets/{wardleyDiagram-T6FBY63Y-BX2Cda3l.js → wardleyDiagram-T6FBY63Y-BxjiBPZa.js} +1 -1
- package/dist/worker/console/static/assets/{xychartDiagram-ELKLHX3M-D9S892ph.js → xychartDiagram-ELKLHX3M--hUY-qFj.js} +1 -1
- package/dist/worker/console/static/index.html +5 -2
- package/dist/worker/console/static/manifest.webmanifest +19 -0
- package/dist/worker/console/static/pets/ember/pet.json +167 -0
- package/dist/worker/console/static/pets/ember/spritesheet.png +0 -0
- package/dist/worker/console/static-src/app/console-types.js +9 -1
- package/dist/worker/console/static-src/chat-view-types.js +1 -1
- package/dist/worker/console/static-src/operator-chat/active-turn-tail.js +10 -0
- package/dist/worker/console/static-src/operator-chat/animation-frame-batcher.js +38 -0
- package/dist/worker/console/static-src/operator-chat/chat-link.js +114 -0
- package/dist/worker/console/static-src/operator-chat/chat-scroll-position.js +24 -0
- package/dist/worker/console/static-src/operator-chat/chat-sse-events.js +360 -14
- package/dist/worker/console/static-src/operator-chat/chat-stream-continuity.js +101 -0
- package/dist/worker/console/static-src/operator-chat/compaction-message.js +10 -2
- package/dist/worker/console/static-src/operator-chat/composer-session-ledger.js +192 -0
- package/dist/worker/console/static-src/operator-chat/context-insights-store.js +64 -0
- package/dist/worker/console/static-src/operator-chat/details-rail-surfaces.js +99 -0
- package/dist/worker/console/static-src/operator-chat/file-palette-nav.js +81 -0
- package/dist/worker/console/static-src/operator-chat/open-preview-in-browser.js +57 -9
- package/dist/worker/console/static-src/operator-chat/pending-user-message.js +66 -0
- package/dist/worker/console/static-src/operator-chat/process-label.js +63 -0
- package/dist/worker/console/static-src/operator-chat/slash-palette-nav.js +15 -5
- package/dist/worker/console/static-src/operator-chat/timeline-merge.js +29 -0
- package/dist/worker/console/static-src/operator-chat/tools-catalog.js +36 -8
- package/dist/worker/console/static-src/operator-chat/transcript-mode.js +21 -0
- package/dist/worker/console/static-src/operator-chat/turn-group-equality.js +16 -0
- package/dist/worker/console/static-src/operator-chat/turn-process-disclosure.js +18 -0
- package/dist/worker/console/static-src/operator-chat/turn-state-projection.js +166 -0
- package/dist/worker/console/static-src/operator-chat/turn-stream-controller.js +58 -2
- package/dist/worker/console/static-src/operator-chat/use-searchable-hidden.js +51 -0
- package/dist/worker/console/static-src/operator-chat/use-stat-dialog.js +95 -0
- package/dist/worker/console/static-src/operator-chat/useChatSessions.js +338 -93
- package/dist/worker/console/static-src/operator-chat/useChatStream.js +310 -106
- package/dist/worker/console/static-src/operator-chat/useChatThread.js +179 -75
- package/dist/worker/console/static-src/operator-chat/useComposer.js +281 -79
- package/dist/worker/console/static-src/operator-chat/useRepoBrowser.js +21 -3
- package/dist/worker/console/static-src/operator-chat/useRuntimeControls.js +36 -17
- package/dist/worker/console/static-src/operator-chat/useRuntimeSnapshot.js +18 -0
- package/dist/worker/console/static-src/operator-chat/useWorkspaceBrowserGate.js +4 -0
- package/dist/worker/console/static-src/operator-chat/user-turn-anchor-equality.js +21 -0
- package/dist/worker/console/static-src/prd-file-import.js +3 -1
- package/dist/worker/console/static-src/shell/console-update-reload.js +1 -1
- package/dist/worker/console/workspace-context.js +54 -1
- package/dist/worker/console/workspace-initialization.js +218 -0
- package/dist/worker/delivery/git-transaction.js +2 -0
- package/dist/worker/delivery/package.js +4 -1
- package/dist/worker/feature/fullstack-validate.js +2 -0
- package/dist/worker/loop-agent/loop-agent-client.js +8 -2
- package/dist/worker/materialize/harness-task-lineage.js +4 -2
- package/dist/worker/materialize/harness-task-materializer.js +4 -1
- package/dist/worker/metrics/projector.js +1 -1
- package/dist/worker/observability/event-store.js +4 -1
- package/dist/worker/observability/read-model.js +27 -3
- package/dist/worker/observe/node-context-insights.js +479 -0
- package/dist/worker/observe/node-transparency.js +103 -72
- package/dist/worker/observe/routes.js +24 -1
- package/dist/worker/observe/static/console-theme.css +178 -0
- package/dist/worker/observe/static/console-theme.js +74 -0
- package/dist/worker/observe/static/dag-inspector-humanize.js +3 -0
- package/dist/worker/observe/static/dom.js +20 -1
- package/dist/worker/observe/static/format-pool.d.ts +2 -0
- package/dist/worker/observe/static/format-pool.js +6 -0
- package/dist/worker/observe/static/format.js +7 -0
- package/dist/worker/observe/static/inspect-workspace.js +34 -7
- package/dist/worker/observe/static/inspector-submission.js +32 -0
- package/dist/worker/observe/static/kpi.js +1 -0
- package/dist/worker/observe/static/operator-chrome.css +85 -0
- package/dist/worker/observe/static/operator-chrome.js +13 -1
- package/dist/worker/observe/static/relations.js +2 -0
- package/dist/worker/observe/static/router.js +13 -0
- package/dist/worker/observe/static/run-processing.js +2 -0
- package/dist/worker/observe/static/shell-chrome.js +36 -3
- package/dist/worker/observe/static/state.js +35 -2
- package/dist/worker/observe/static/styles.css +263 -39
- package/dist/worker/observe/static/task-failure-labels.d.ts +4 -0
- package/dist/worker/observe/static/task-failure-labels.js +67 -0
- package/dist/worker/observe/static/task-history.js +12 -0
- package/dist/worker/observe/static/views/batch.js +6 -13
- package/dist/worker/observe/static/views/dag-graph.js +50 -3
- package/dist/worker/observe/static/views/dag-inspector.js +742 -268
- package/dist/worker/observe/static/views/dag-trajectory.js +3 -0
- package/dist/worker/observe/static/views/dag.d.ts +6 -0
- package/dist/worker/observe/static/views/dag.js +49 -11
- package/dist/worker/observe/static/views/dags.js +2 -0
- package/dist/worker/observe/static/views/dashboard.js +21 -12
- package/dist/worker/observe/static/views/failures.js +21 -11
- package/dist/worker/observe/static/views/feature.js +11 -29
- package/dist/worker/observe/static/views/pool.js +37 -28
- package/dist/worker/observe/static/views/run.js +48 -5
- package/dist/worker/observe/static/views/session-timeline.js +189 -240
- package/dist/worker/observe/static/views/task.js +81 -62
- package/dist/worker/outcomes/declared-artifacts.js +2 -0
- package/dist/worker/preflight.js +3 -0
- package/dist/worker/scheduler/scheduled-goal-dispatch.js +118 -0
- package/dist/worker/scheduler/scheduled-goal-evidence.js +230 -0
- package/dist/worker/scheduler/scheduled-goal-recovery.js +62 -0
- package/dist/worker/scheduler/scheduled-goal-store.js +709 -0
- package/dist/worker/scheduler/scheduled-goal-supervisor.js +132 -0
- package/dist/worker/scheduler/scheduled-goal-time.js +102 -0
- package/dist/worker/scheduler/scheduled-goal-types.js +95 -0
- package/dist/worker/task-spec/validate.js +4 -1
- package/dist/workflows/dag/backend-test-case-coverage-analysis.js +3 -1
- package/dist/workflows/dag/backend-test-layout.js +27 -7
- package/dist/workflows/dag/backend-test-markdown-workflow.js +9 -25
- package/dist/workflows/dag/backend-test-plan-protocol.js +82 -5
- package/dist/workflows/dag/backend-test-pytest-collection.js +4 -1
- package/dist/workflows/dag/budget-enforcement.js +82 -3
- package/dist/workflows/dag/checkpoint.js +686 -0
- package/dist/workflows/dag/dag-retry-schema.js +14 -0
- package/dist/workflows/dag/decision-envelope.js +2 -0
- package/dist/workflows/dag/dynamic-runtime/map.js +15 -2
- package/dist/workflows/dag/dynamic-runtime/shared.js +2 -2
- package/dist/workflows/dag/final-verification.js +45 -10
- package/dist/workflows/dag/frontend-capacity.js +9 -0
- package/dist/workflows/dag/frontend-closeout.js +5 -1
- package/dist/workflows/dag/frontend-committed-facts.js +461 -0
- package/dist/workflows/dag/frontend-contract-facts.js +133 -0
- package/dist/workflows/dag/frontend-design-policy.js +107 -21
- package/dist/workflows/dag/frontend-durable-tools.js +225 -0
- package/dist/workflows/dag/frontend-execution-groups.js +24 -0
- package/dist/workflows/dag/frontend-implementation-contract.js +554 -26
- package/dist/workflows/dag/frontend-input-projection.js +76 -0
- package/dist/workflows/dag/frontend-plan-canary.js +103 -0
- package/dist/workflows/dag/frontend-plan-decision-contract.js +795 -0
- package/dist/workflows/dag/frontend-plan-progress.js +92 -0
- package/dist/workflows/dag/frontend-plan-recovery-policy.js +18 -0
- package/dist/workflows/dag/frontend-plan-render.js +21 -5
- package/dist/workflows/dag/frontend-provider-capability-matrix.js +8 -61
- package/dist/workflows/dag/frontend-recovery-controller.js +7 -7
- package/dist/workflows/dag/frontend-recovery-lineage.js +13 -0
- package/dist/workflows/dag/frontend-recovery-plan.js +10 -5
- package/dist/workflows/dag/frontend-recovery-run.js +148 -47
- package/dist/workflows/dag/frontend-repair-assertions.js +36 -0
- package/dist/workflows/dag/frontend-repair.js +79 -76
- package/dist/workflows/dag/frontend-review-context.js +71 -69
- package/dist/workflows/dag/frontend-review-scopes.js +237 -0
- package/dist/workflows/dag/frontend-risk.js +101 -12
- package/dist/workflows/dag/frontend-root-observation.js +261 -0
- package/dist/workflows/dag/frontend-session-budget.js +376 -0
- package/dist/workflows/dag/frontend-session-context.js +10 -0
- package/dist/workflows/dag/frontend-shape-facts.js +12 -2
- package/dist/workflows/dag/frontend-shape.js +27 -32
- package/dist/workflows/dag/frontend-test-case-checklist.js +14 -3
- package/dist/workflows/dag/frontend-test-case-generation-assessment.js +88 -0
- package/dist/workflows/dag/frontend-test-case-manifest.js +6 -1
- package/dist/workflows/dag/frontend-test-case-quality.js +3 -3
- package/dist/workflows/dag/frontend-test-environment-probe.js +0 -2
- package/dist/workflows/dag/frontend-test-execution-evidence.js +328 -0
- package/dist/workflows/dag/frontend-test-framework-adapters.js +309 -0
- package/dist/workflows/dag/frontend-test-layout.js +41 -12
- package/dist/workflows/dag/frontend-test-result-contract.js +15 -1
- package/dist/workflows/dag/frontend-typed-event-store.js +57 -26
- package/dist/workflows/dag/frontend-verification-trace.js +65 -48
- package/dist/workflows/dag/frontend-writer-admission.js +6 -48
- package/dist/workflows/dag/frontend-writer-status.js +0 -23
- package/dist/workflows/dag/hybrid/sources.js +1831 -0
- package/dist/workflows/dag/hybrid/templates/backend-test.js +1822 -0
- package/dist/workflows/dag/hybrid/templates/frontend-test.js +807 -0
- package/dist/workflows/dag/hybrid/templates/frontend.js +1529 -0
- package/dist/workflows/dag/hybrid/templates/index.js +8 -0
- package/dist/workflows/dag/hybrid/templates/kg-bootstrap.js +449 -0
- package/dist/workflows/dag/hybrid/templates/knowledge-sync.js +495 -0
- package/dist/workflows/dag/hybrid/templates/shared.js +101 -0
- package/dist/workflows/dag/hybrid/templates/standard.js +265 -0
- package/dist/workflows/dag/hybrid/types.js +32 -0
- package/dist/workflows/dag/init-hybrid.js +57 -7077
- package/dist/workflows/dag/interrupt-request.js +2 -0
- package/dist/workflows/dag/lifecycle.js +11 -2
- package/dist/workflows/dag/node-execution.js +257 -247
- package/dist/workflows/dag/output-protocol.js +1 -0
- package/dist/workflows/dag/prompt.js +4 -0
- package/dist/workflows/dag/reconcile-run.js +2 -0
- package/dist/workflows/dag/repair-artifact.js +3 -1
- package/dist/workflows/dag/report.js +2 -0
- package/dist/workflows/dag/rerun-feedback.js +36 -4
- package/dist/workflows/dag/rerun-plan.js +106 -4
- package/dist/workflows/dag/rerun-run.js +7 -0
- package/dist/workflows/dag/rerun-task.js +75 -1
- package/dist/workflows/dag/retry-policy.js +34 -10
- package/dist/workflows/dag/runner-exit-diagnostics.js +125 -0
- package/dist/workflows/dag/runner.js +38 -1074
- package/dist/workflows/dag/scheduler.js +2 -4
- package/dist/workflows/dag/skill-instructions.js +102 -32
- package/dist/workflows/dag/skill-snapshot.js +3 -0
- package/dist/workflows/dag/skills.js +7 -7
- package/dist/workflows/dag/structured-output-repair.js +4 -1
- package/dist/workflows/dag/terminal-status.js +502 -0
- package/dist/workflows/dag/test-artifact-namespace.js +73 -0
- package/dist/workflows/dag/types.js +79 -7
- package/dist/workflows/dag/validate.js +38 -13
- package/dist/workflows/dag/workspace-checkpoint.js +79 -1
- package/dist/workflows/loop/benchmark.js +11 -8
- package/docs/README.md +2 -1
- package/docs/architecture/dag-execution.md +4 -4
- package/docs/architecture/runtime-boundaries.md +29 -23
- package/docs/governance/README.md +1 -0
- package/docs/governance/harness-methodology-verification.md +11 -0
- package/docs/init-surface.manifest.json +44 -295
- package/docs/operations/README.md +4 -0
- package/docs/templates/README.md +5 -5
- package/docs/templates/agent-dag.base.json +9 -9
- package/docs/templates/agent-dag.final-verification.json +6 -6
- package/docs/templates/agent-dag.schema.json +2 -2
- package/docs/templates/agent-dag.supervised-implementation.json +12 -12
- package/docs/templates/backend-test-dag.generate-pytest.prompt.md +3 -3
- package/docs/templates/backend-test-dag.json +708 -703
- package/docs/templates/backend-test-dag.review-cases.prompt.md +3 -3
- package/docs/templates/frontend-design-contract.md +4 -4
- package/docs/templates/frontend-implementation-contract.schema.json +99 -22
- package/docs/templates/frontend-implementation-dag.json +7 -7
- package/docs/templates/frontend-test-case-checklist.md +2 -2
- package/docs/templates/frontend-test-case.example.md +2 -0
- package/docs/templates/frontend-test-dag.generate-cases.prompt.md +1 -1
- package/docs/templates/frontend-test-dag.json +48 -39
- package/docs/templates/frontend-test-dag.retrieve-context.prompt.md +2 -2
- package/docs/templates/frontend-test-dag.retrospect.prompt.md +1 -1
- package/docs/templates/frontend-test-dag.review-cases.prompt.md +1 -1
- package/docs/templates/hybrid-dag.json +9 -9
- package/docs/templates/knowledge-sync-dag.json +8 -8
- package/harness.json +1 -1
- package/package.json +27 -8
- package/resource-catalog.v1.json +1981 -0
- package/resources/agent-bridge/loop-agent/SKILL.md +40 -0
- package/resources/init-history/legacy-surface.v1.json +33 -0
- package/skills/fe-test-ui-scout/SKILL.md +4 -4
- package/skills/fe-test-ui-scout/references/ledger-schema.md +1 -1
- package/skills/frontend-bounded-implement/references/code-standards.md +3 -3
- package/skills/frontend-contract/SKILL.md +2 -1
- package/skills/frontend-contract/references/contract-protocol.md +22 -4
- package/skills/frontend-design-review/SKILL.md +16 -18
- package/skills/frontend-design-review/references/review-checklist.md +3 -4
- package/skills/frontend-plan/SKILL.md +25 -3
- package/skills/frontend-plan/references/decision-contract.md +117 -8
- package/skills/frontend-plan/references/design-decisions.md +32 -0
- package/skills/frontend-review/SKILL.md +29 -36
- package/skills/frontend-review/references/review-findings.md +8 -3
- package/skills/frontend-scout/references/scout-evidence.md +4 -0
- package/skills/init-update/SKILL.md +34 -0
- package/skills/init-update/agents/openai.yaml +6 -0
- package/skills/loop-agent/references/command-reference.md +8 -0
- package/skills/loop-agent/references/hybrid-dag.md +1 -1
- package/skills/playwright-cli/SKILL.md +1 -1
- package/skills/playwright-cli-case-generator/SKILL.md +7 -5
- package/dist/commands/new-task.js +0 -5
- package/dist/commands/status.js +0 -57
- package/dist/commands/task-contract.js +0 -269
- package/dist/shared/prompts.js +0 -26
- package/dist/task/config.js +0 -2
- package/dist/task/contract/validate-draft.js +0 -106
- package/dist/task/goal.js +0 -3
- package/dist/task/index.js +0 -12
- package/dist/task/lifecycle.js +0 -1
- package/dist/task/operator/capabilities.js +0 -6
- package/dist/task/operator/envelope.js +0 -2
- package/dist/task/operator/index.js +0 -5
- package/dist/task/operator/registry.js +0 -2
- package/dist/task/paths.js +0 -1
- package/dist/task/source-state.js +0 -1
- package/dist/task/subagent-guidance.js +0 -1
- package/dist/worker/console/chat/sift-bridge.js +0 -1
- package/dist/worker/console/index.js +0 -20
- package/dist/worker/console/static/assets/channel-CXqdP5pW.js +0 -1
- package/dist/worker/console/static/assets/classDiagram-JCYQIIEL-D9C3agF7.js +0 -1
- package/dist/worker/console/static/assets/classDiagram-v2-OCEON4UE-D9C3agF7.js +0 -1
- package/dist/worker/console/static/assets/index-CAZ2fC_X.css +0 -1
- package/dist/worker/console/static/assets/index-DhfjOafu.js +0 -449
- package/dist/worker/console/static/assets/stateDiagram-v2-UEYNNEHI-Tp7V0U4q.js +0 -1
- package/dist/worker/console/static/assets/swimlanesDiagram-ULZ7WXOC-DUZWZWi8.js +0 -8
- package/dist/worker/scheduler/index.js +0 -25
- package/dist/workflows/dag/facts.js +0 -4
- package/dist/workflows/dag/frontend-shadow-dual-write.js +0 -941
- package/dist/workflows/dag/index.js +0 -6
- package/dist/workflows/dynamic/index.js +0 -7
- /package/dist/{task/operator/types.js → executors/dag-pi/plan/session.js} +0 -0
|
@@ -15,17 +15,18 @@
|
|
|
15
15
|
import { randomUUID } from "node:crypto";
|
|
16
16
|
import { access } from "node:fs/promises";
|
|
17
17
|
import { parseAssistantContent } from "./assistant-content.js";
|
|
18
|
-
import { OPERATOR_CHAT_ALLOWED_TOOLS, OPERATOR_CHAT_DENIED_TOOLS, OPERATOR_CHAT_DENIED_OPERATOR_ACTIONS, } from "./tools.js";
|
|
18
|
+
import { OPERATOR_CHAT_ALLOWED_TOOLS, OPERATOR_CHAT_DENIED_TOOLS, OPERATOR_CHAT_DENIED_OPERATOR_ACTIONS, OPERATOR_CHAT_SQL_TOOL_CAPABILITIES, } from "./tools.js";
|
|
19
|
+
import { InvalidChatSessionModeError, SESSION_MODE_ACTION_DENIED_CODE, SESSION_MODE_ACTION_DENIED_MESSAGE, SESSION_MODE_IMMUTABLE_CODE, UnknownChatSessionModeError, isWorkflowSessionMode, parseCreateSessionMode, readStoredSessionMode, shouldAcceptOperatorSnapshot, } from "./session-mode.js";
|
|
19
20
|
import { buildModelCallableToolSchemas } from "./tool-adapter.js";
|
|
20
21
|
import { cleanChatTitle, acceptedOperationIdFromResult, isBlankChatSession } from "./session-store.js";
|
|
21
|
-
import {
|
|
22
|
+
import { projectUserTurnOutline } from "./user-turn-outline.js";
|
|
23
|
+
import { hashChatTurnPayload, projectChatToolEventPayload, projectUnknownChatEventPayload, projectCompactSnapshot, } from "./chat-event-store.js";
|
|
22
24
|
import { projectDeferredTurnSummary, projectDeferredTurnSummaries, } from "./deferred-turn.js";
|
|
23
25
|
import { THINKING_LEVELS, RuntimeSnapshotUnavailableError, } from "./pi-runtime.js";
|
|
24
26
|
import { ResourcePreferencesError, setSkillAutoInvocationPreference, } from "./resource-preferences-store.js";
|
|
25
27
|
import { ComposerDraftStore } from "./composer-draft-store.js";
|
|
26
|
-
import {
|
|
27
|
-
import {
|
|
28
|
-
import { listRepoDirectory, matchRepoHtmlPreviewRoute, readRepoHtmlAsset, readRepoPreview, RepoBrowserError, } from "./repo-browser.js";
|
|
28
|
+
import { scrubSecrets } from "./explore-tools.js";
|
|
29
|
+
import { listChatFilePalette, listRepoDirectory, matchRepoHtmlPreviewRoute, readRepoHtmlAsset, readRepoPreview, RepoBrowserError, } from "./repo-browser.js";
|
|
29
30
|
import { projectRuntimeContext } from "./runtime-context.js";
|
|
30
31
|
import { patchModelsConfig, readModelsConfig, readPackageInventory, } from "./pi-console-config.js";
|
|
31
32
|
import { ChatInterviewAdapter, projectChatInterviewState, } from "./interview-adapter.js";
|
|
@@ -35,6 +36,7 @@ import { dispatchOperatorAction } from "../operator-actions.js";
|
|
|
35
36
|
import { makeHumanGateCard, } from "./human-gate-card.js";
|
|
36
37
|
import { ContractApplyReceiptStore } from "./contract-apply-receipt-store.js";
|
|
37
38
|
import { projectTaskContext } from "./context-panel.js";
|
|
39
|
+
import { deriveContextInsights, estimateTokensFromText, } from "./context-insights.js";
|
|
38
40
|
import { deriveChatSessionTelemetry, deriveChatTurnTelemetry } from "./session-stats.js";
|
|
39
41
|
import { contractApplyPayloadHash, issueHumanGateToken, verifyHumanGateToken, } from "../human-gate-token.js";
|
|
40
42
|
import { sendJson } from "../routes.js";
|
|
@@ -44,11 +46,90 @@ import { buildGoalCompactionInstructions, buildGoalContinuationContext, } from "
|
|
|
44
46
|
import { classifyGoalRoundAdmission, } from "./goal-round-driver.js";
|
|
45
47
|
import { isTerminalOperationState } from "../../../infrastructure/console/operation-store.js";
|
|
46
48
|
import { SddDataAlignmentError, } from "./sdd-data-alignment.js";
|
|
49
|
+
import { TerminalSessionRegistry } from "./terminal-sessions.js";
|
|
50
|
+
import { handlePetRequest } from "./pet/pet-routes.js";
|
|
51
|
+
import { handleTerminalRequest } from "./terminal-routes.js";
|
|
52
|
+
import { handleScmRequest } from "./scm-routes.js";
|
|
53
|
+
import { handleBrowserRequest } from "./browser-routes.js";
|
|
54
|
+
import { DbConnectionError } from "./db-connections.js";
|
|
47
55
|
function projectBrowserChatSession(record) {
|
|
48
56
|
const { telemetrySessionId, ...projected } = record;
|
|
49
57
|
void telemetrySessionId;
|
|
50
58
|
return projected;
|
|
51
59
|
}
|
|
60
|
+
function reopenInitFromRecord(record) {
|
|
61
|
+
return {
|
|
62
|
+
sessionId: record.sessionId,
|
|
63
|
+
sessionFile: record.sessionFile,
|
|
64
|
+
...(record.modelProvider && record.modelId
|
|
65
|
+
? { model: { provider: record.modelProvider, modelId: record.modelId } }
|
|
66
|
+
: {}),
|
|
67
|
+
sessionMode: record.sessionMode,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
function workspaceProjectionFromRecord(record) {
|
|
71
|
+
return {
|
|
72
|
+
sessionMode: record.sessionMode,
|
|
73
|
+
...(record.modelProvider ? { modelProvider: record.modelProvider } : {}),
|
|
74
|
+
...(record.modelId ? { modelId: record.modelId } : {}),
|
|
75
|
+
...(record.thinkingLevel ? { thinkingLevel: record.thinkingLevel } : {}),
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
async function readChatSessionMode(deps, sessionId) {
|
|
79
|
+
let record;
|
|
80
|
+
try {
|
|
81
|
+
record = await deps.store.get(sessionId);
|
|
82
|
+
}
|
|
83
|
+
catch (error) {
|
|
84
|
+
if (error instanceof UnknownChatSessionModeError) {
|
|
85
|
+
return {
|
|
86
|
+
ok: false,
|
|
87
|
+
status: 409,
|
|
88
|
+
code: error.code,
|
|
89
|
+
message: error.message,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
throw error;
|
|
93
|
+
}
|
|
94
|
+
if (!record) {
|
|
95
|
+
return {
|
|
96
|
+
ok: false,
|
|
97
|
+
status: 404,
|
|
98
|
+
code: "NOT_FOUND",
|
|
99
|
+
message: `chat session not found: ${sessionId}`,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
// Normalize: the store contract always fills sessionMode, but handler-level
|
|
103
|
+
// fakes / partial records may omit it — treat missing as workflow (the
|
|
104
|
+
// stock-record read rule), while a present-but-unknown value still fails
|
|
105
|
+
// closed in the store layer.
|
|
106
|
+
return {
|
|
107
|
+
ok: true,
|
|
108
|
+
record,
|
|
109
|
+
sessionMode: readStoredSessionMode(record.sessionMode),
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
async function requireWorkflowSession(res, deps, sessionId) {
|
|
113
|
+
const resolved = await readChatSessionMode(deps, sessionId);
|
|
114
|
+
if (!resolved.ok) {
|
|
115
|
+
sendJson(res, resolved.status, {
|
|
116
|
+
ok: false,
|
|
117
|
+
error: { code: resolved.code, message: resolved.message },
|
|
118
|
+
});
|
|
119
|
+
return undefined;
|
|
120
|
+
}
|
|
121
|
+
if (!isWorkflowSessionMode(resolved.sessionMode)) {
|
|
122
|
+
sendJson(res, 409, {
|
|
123
|
+
ok: false,
|
|
124
|
+
error: {
|
|
125
|
+
code: SESSION_MODE_ACTION_DENIED_CODE,
|
|
126
|
+
message: SESSION_MODE_ACTION_DENIED_MESSAGE,
|
|
127
|
+
},
|
|
128
|
+
});
|
|
129
|
+
return undefined;
|
|
130
|
+
}
|
|
131
|
+
return resolved.record;
|
|
132
|
+
}
|
|
52
133
|
/**
|
|
53
134
|
* Reattach the durable event-ring order to session-store rows. The session
|
|
54
135
|
* store remains the content authority; the ring only supplies chronology.
|
|
@@ -64,6 +145,16 @@ export function projectStoredMessagesWithEventOrder(messages, events) {
|
|
|
64
145
|
if (event.kind !== expectedKind)
|
|
65
146
|
continue;
|
|
66
147
|
const data = event.data;
|
|
148
|
+
// Exact alignment wins: since 2026-09-13 message_end frames carry the
|
|
149
|
+
// durable assistant message id, so restore matches by id, never by
|
|
150
|
+
// text (duplicate assistant texts would mis-align within a turn).
|
|
151
|
+
if (typeof data.messageId === "string" && data.messageId) {
|
|
152
|
+
if (data.messageId === message.id) {
|
|
153
|
+
assistantCursor = index + 1;
|
|
154
|
+
return event;
|
|
155
|
+
}
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
67
158
|
const eventText = typeof data.text === "string"
|
|
68
159
|
? data.text
|
|
69
160
|
: typeof data.message === "string"
|
|
@@ -116,6 +207,16 @@ export function projectStoredMessagesWithEventOrder(messages, events) {
|
|
|
116
207
|
...(finish
|
|
117
208
|
? { finishedSeq: finish.seq, finishedAt: finish.at }
|
|
118
209
|
: {}),
|
|
210
|
+
...(start && typeof start.data.step === "number"
|
|
211
|
+
? { step: start.data.step }
|
|
212
|
+
: {}),
|
|
213
|
+
...(start &&
|
|
214
|
+
typeof start.data.parentToolCallId ===
|
|
215
|
+
"string"
|
|
216
|
+
? {
|
|
217
|
+
parentToolCallId: start.data.parentToolCallId,
|
|
218
|
+
}
|
|
219
|
+
: {}),
|
|
119
220
|
};
|
|
120
221
|
});
|
|
121
222
|
}
|
|
@@ -132,6 +233,43 @@ function turnSummary(turn) {
|
|
|
132
233
|
abortRequestedAt: turn.abortRequestedAt ?? null,
|
|
133
234
|
};
|
|
134
235
|
}
|
|
236
|
+
function turnStateSummary(turn) {
|
|
237
|
+
return {
|
|
238
|
+
turnId: turn.turnId,
|
|
239
|
+
state: turn.state === "queued" ? "running" : turn.state,
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
function turnStatesForIds(events, sessionId, turnIds) {
|
|
243
|
+
const projected = new Map();
|
|
244
|
+
for (const turnId of turnIds) {
|
|
245
|
+
if (!turnId || projected.has(turnId))
|
|
246
|
+
continue;
|
|
247
|
+
const turn = events.getTurn(sessionId, turnId);
|
|
248
|
+
if (turn)
|
|
249
|
+
projected.set(turnId, turnStateSummary(turn));
|
|
250
|
+
}
|
|
251
|
+
return [...projected.values()];
|
|
252
|
+
}
|
|
253
|
+
const MAX_STATE_QUERY_TURN_IDS = 8;
|
|
254
|
+
const MAX_STATE_QUERY_TURN_ID_CHARS = 240;
|
|
255
|
+
function requestedStateTurnIds(req) {
|
|
256
|
+
const result = [];
|
|
257
|
+
const seen = new Set();
|
|
258
|
+
const url = new URL(req.url ?? "/", "http://localhost");
|
|
259
|
+
for (const raw of url.searchParams.getAll("turnId")) {
|
|
260
|
+
const turnId = raw.trim();
|
|
261
|
+
if (!turnId ||
|
|
262
|
+
turnId.length > MAX_STATE_QUERY_TURN_ID_CHARS ||
|
|
263
|
+
seen.has(turnId)) {
|
|
264
|
+
continue;
|
|
265
|
+
}
|
|
266
|
+
seen.add(turnId);
|
|
267
|
+
result.push(turnId);
|
|
268
|
+
if (result.length >= MAX_STATE_QUERY_TURN_IDS)
|
|
269
|
+
break;
|
|
270
|
+
}
|
|
271
|
+
return result;
|
|
272
|
+
}
|
|
135
273
|
function isTerminalTurnState(state) {
|
|
136
274
|
return state === "settled" || state === "aborted" || state === "failed";
|
|
137
275
|
}
|
|
@@ -162,10 +300,99 @@ function writeChatSse(res, event) {
|
|
|
162
300
|
/** Map a runtime ChatTurnEvent to the persisted event-store partial.
|
|
163
301
|
*
|
|
164
302
|
* Returns undefined for heartbeat (not persisted — keep-alive only). */
|
|
165
|
-
function turnEventToStorePartial(event) {
|
|
303
|
+
export function turnEventToStorePartial(event) {
|
|
166
304
|
switch (event.type) {
|
|
167
305
|
case "agent_start":
|
|
168
306
|
return { kind: "agent_start", data: {} };
|
|
307
|
+
case "step_start":
|
|
308
|
+
case "step_end":
|
|
309
|
+
return {
|
|
310
|
+
kind: event.type,
|
|
311
|
+
data: {
|
|
312
|
+
step: Number(event.step ?? 1),
|
|
313
|
+
...(event.type === "step_end"
|
|
314
|
+
? { outcome: String(event.outcome ?? "completed") }
|
|
315
|
+
: {}),
|
|
316
|
+
},
|
|
317
|
+
};
|
|
318
|
+
case "model_retry":
|
|
319
|
+
{
|
|
320
|
+
const retryState = event.state === "completed" || event.state === "failed"
|
|
321
|
+
? event.state
|
|
322
|
+
: "scheduled";
|
|
323
|
+
return {
|
|
324
|
+
kind: "model_retry",
|
|
325
|
+
data: {
|
|
326
|
+
step: Number(event.step ?? 1),
|
|
327
|
+
attempt: Number(event.attempt ?? 1),
|
|
328
|
+
...(typeof event.maxAttempts === "number"
|
|
329
|
+
? { maxAttempts: event.maxAttempts }
|
|
330
|
+
: {}),
|
|
331
|
+
...(typeof event.delayMs === "number" ? { delayMs: event.delayMs } : {}),
|
|
332
|
+
...(typeof event.errorMessage === "string"
|
|
333
|
+
? { errorMessage: event.errorMessage }
|
|
334
|
+
: {}),
|
|
335
|
+
state: retryState,
|
|
336
|
+
...(typeof event.success === "boolean" ? { success: event.success } : {}),
|
|
337
|
+
},
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
case "turn_max_tokens":
|
|
341
|
+
return {
|
|
342
|
+
kind: "turn_max_tokens",
|
|
343
|
+
data: { step: Number(event.step ?? 1) },
|
|
344
|
+
};
|
|
345
|
+
case "turn_error":
|
|
346
|
+
return {
|
|
347
|
+
kind: "turn_error",
|
|
348
|
+
data: {
|
|
349
|
+
...(typeof event.step === "number" ? { step: event.step } : {}),
|
|
350
|
+
...(typeof event.code === "string" ? { code: event.code } : {}),
|
|
351
|
+
message: String(event.message ?? "模型请求失败"),
|
|
352
|
+
},
|
|
353
|
+
};
|
|
354
|
+
case "request_context":
|
|
355
|
+
return {
|
|
356
|
+
kind: "request_context",
|
|
357
|
+
data: {
|
|
358
|
+
requestId: String(event.requestId ?? ""),
|
|
359
|
+
step: Number(event.step ?? 1),
|
|
360
|
+
...(event.model && typeof event.model === "object"
|
|
361
|
+
? { model: event.model }
|
|
362
|
+
: {}),
|
|
363
|
+
...(typeof event.thinkingLevel === "string"
|
|
364
|
+
? { thinkingLevel: event.thinkingLevel }
|
|
365
|
+
: {}),
|
|
366
|
+
systemPromptHash: String(event.systemPromptHash ?? ""),
|
|
367
|
+
systemPromptChars: Number(event.systemPromptChars ?? 0),
|
|
368
|
+
activeTools: Array.isArray(event.activeTools) ? event.activeTools : [],
|
|
369
|
+
inputChars: Number(event.inputChars ?? 0),
|
|
370
|
+
imageCount: Number(event.imageCount ?? 0),
|
|
371
|
+
sources: Array.isArray(event.sources) ? event.sources : [],
|
|
372
|
+
},
|
|
373
|
+
};
|
|
374
|
+
case "command":
|
|
375
|
+
return {
|
|
376
|
+
kind: "command",
|
|
377
|
+
data: {
|
|
378
|
+
commandId: String(event.commandId ?? ""),
|
|
379
|
+
name: String(event.name ?? "unknown"),
|
|
380
|
+
...(typeof event.argsPreview === "string"
|
|
381
|
+
? { argsPreview: event.argsPreview }
|
|
382
|
+
: {}),
|
|
383
|
+
...(typeof event.action === "string" ? { action: event.action } : {}),
|
|
384
|
+
outcome: event.outcome === "failed" ? "failed" : "completed",
|
|
385
|
+
...(typeof event.message === "string" ? { message: event.message } : {}),
|
|
386
|
+
},
|
|
387
|
+
};
|
|
388
|
+
case "unknown":
|
|
389
|
+
return {
|
|
390
|
+
kind: "unknown",
|
|
391
|
+
data: projectUnknownChatEventPayload({
|
|
392
|
+
eventType: String(event.eventType ?? "unknown"),
|
|
393
|
+
value: event.value,
|
|
394
|
+
}),
|
|
395
|
+
};
|
|
169
396
|
case "message_update":
|
|
170
397
|
case "message_end": {
|
|
171
398
|
const content = parseAssistantContent(event.content);
|
|
@@ -201,6 +428,19 @@ function turnEventToStorePartial(event) {
|
|
|
201
428
|
text: String(event.text ?? ""),
|
|
202
429
|
...(content ? { content } : {}),
|
|
203
430
|
...(timing ? { timing } : {}),
|
|
431
|
+
// Durable assistant message id (2026-09-13): the session
|
|
432
|
+
// store persists message_end BEFORE framing, so a reconnecting
|
|
433
|
+
// client and the restore projection can align on this id
|
|
434
|
+
// instead of matching text within a time window.
|
|
435
|
+
...(typeof event.messageId ===
|
|
436
|
+
"string" &&
|
|
437
|
+
event.messageId
|
|
438
|
+
? { messageId: event.messageId }
|
|
439
|
+
: {}),
|
|
440
|
+
...(typeof event.step === "number" ? { step: event.step } : {}),
|
|
441
|
+
...(typeof event.stopReason === "string"
|
|
442
|
+
? { stopReason: event.stopReason }
|
|
443
|
+
: {}),
|
|
204
444
|
},
|
|
205
445
|
};
|
|
206
446
|
}
|
|
@@ -214,6 +454,24 @@ function turnEventToStorePartial(event) {
|
|
|
214
454
|
toolCallId: String(event.toolCallId ?? ""),
|
|
215
455
|
toolName: String(event.toolName ?? ""),
|
|
216
456
|
value: event.args,
|
|
457
|
+
...(typeof event.step === "number" ? { step: event.step } : {}),
|
|
458
|
+
...(typeof event.parentToolCallId === "string"
|
|
459
|
+
? { parentToolCallId: event.parentToolCallId }
|
|
460
|
+
: {}),
|
|
461
|
+
}),
|
|
462
|
+
};
|
|
463
|
+
case "tool_update":
|
|
464
|
+
return {
|
|
465
|
+
kind: "tool_update",
|
|
466
|
+
data: projectChatToolEventPayload({
|
|
467
|
+
kind: "tool_update",
|
|
468
|
+
toolCallId: String(event.toolCallId ?? ""),
|
|
469
|
+
toolName: String(event.toolName ?? ""),
|
|
470
|
+
value: event.partialResult,
|
|
471
|
+
...(typeof event.step === "number" ? { step: event.step } : {}),
|
|
472
|
+
...(typeof event.parentToolCallId === "string"
|
|
473
|
+
? { parentToolCallId: event.parentToolCallId }
|
|
474
|
+
: {}),
|
|
217
475
|
}),
|
|
218
476
|
};
|
|
219
477
|
case "tool_result": {
|
|
@@ -228,6 +486,10 @@ function turnEventToStorePartial(event) {
|
|
|
228
486
|
toolName: String(event.toolName ?? ""),
|
|
229
487
|
value: event.result,
|
|
230
488
|
isError: Boolean(event.isError),
|
|
489
|
+
...(typeof event.step === "number" ? { step: event.step } : {}),
|
|
490
|
+
...(typeof event.parentToolCallId === "string"
|
|
491
|
+
? { parentToolCallId: event.parentToolCallId }
|
|
492
|
+
: {}),
|
|
231
493
|
...(resultOperationId ? { operationId: resultOperationId } : {}),
|
|
232
494
|
}),
|
|
233
495
|
};
|
|
@@ -517,6 +779,15 @@ export async function handleChatCapabilities(_req, res, deps) {
|
|
|
517
779
|
deniedTools: OPERATOR_CHAT_DENIED_TOOLS,
|
|
518
780
|
deniedOperatorActions: OPERATOR_CHAT_DENIED_OPERATOR_ACTIONS,
|
|
519
781
|
tools: toolSchemas,
|
|
782
|
+
builtinCustomTools: OPERATOR_CHAT_SQL_TOOL_CAPABILITIES,
|
|
783
|
+
builtinSubagents: [
|
|
784
|
+
{
|
|
785
|
+
toolId: "Agent",
|
|
786
|
+
agent: "Explore",
|
|
787
|
+
kind: "subagent",
|
|
788
|
+
description: "Start the built-in read-only Explore subagent for codebase search, symbol lookup, and call-chain tracing.",
|
|
789
|
+
},
|
|
790
|
+
],
|
|
520
791
|
// ADR 0011: full Pi tools including bash are default-active.
|
|
521
792
|
hasBash: true,
|
|
522
793
|
});
|
|
@@ -557,6 +828,22 @@ workspaceId) {
|
|
|
557
828
|
});
|
|
558
829
|
return;
|
|
559
830
|
}
|
|
831
|
+
let sessionMode;
|
|
832
|
+
try {
|
|
833
|
+
sessionMode = parseCreateSessionMode(body.sessionMode);
|
|
834
|
+
}
|
|
835
|
+
catch (error) {
|
|
836
|
+
sendJson(res, 400, {
|
|
837
|
+
ok: false,
|
|
838
|
+
error: {
|
|
839
|
+
code: error instanceof InvalidChatSessionModeError
|
|
840
|
+
? error.code
|
|
841
|
+
: "INVALID_SESSION_MODE",
|
|
842
|
+
message: error instanceof Error ? error.message : String(error),
|
|
843
|
+
},
|
|
844
|
+
});
|
|
845
|
+
return;
|
|
846
|
+
}
|
|
560
847
|
const provider = typeof body.modelProvider === "string" ? body.modelProvider : undefined;
|
|
561
848
|
const modelId = typeof body.modelId === "string" ? body.modelId : undefined;
|
|
562
849
|
const snapshotInput = body.snapshot;
|
|
@@ -578,6 +865,16 @@ workspaceId) {
|
|
|
578
865
|
let createdSessionId;
|
|
579
866
|
let persisted = false;
|
|
580
867
|
try {
|
|
868
|
+
if (snapshotInput !== undefined && !shouldAcceptOperatorSnapshot(sessionMode)) {
|
|
869
|
+
sendJson(res, 409, {
|
|
870
|
+
ok: false,
|
|
871
|
+
error: {
|
|
872
|
+
code: SESSION_MODE_ACTION_DENIED_CODE,
|
|
873
|
+
message: "Operator snapshots cannot be imported into Pi sessions",
|
|
874
|
+
},
|
|
875
|
+
});
|
|
876
|
+
return;
|
|
877
|
+
}
|
|
581
878
|
const snapshot = snapshotInput === undefined ? undefined : projectCompactSnapshot(snapshotInput);
|
|
582
879
|
// A Console record promises that its Pi JSONL can be reopened after a
|
|
583
880
|
// Console restart. Do not make that promise until Pi has finished
|
|
@@ -585,6 +882,7 @@ workspaceId) {
|
|
|
585
882
|
// future JSONL path, leaving an unrecoverable orphan when init failed or
|
|
586
883
|
// the process restarted in that window.
|
|
587
884
|
const handle = await deps.runtime.createSession({
|
|
885
|
+
sessionMode,
|
|
588
886
|
...(provider && modelId ? { model: { provider, modelId } } : {}),
|
|
589
887
|
...(snapshot
|
|
590
888
|
? {
|
|
@@ -611,6 +909,7 @@ workspaceId) {
|
|
|
611
909
|
sessionFile: handle.sessionFile,
|
|
612
910
|
modelProvider: selection.model?.provider ?? handle.model?.provider,
|
|
613
911
|
modelId: selection.model?.modelId ?? handle.model?.modelId,
|
|
912
|
+
sessionMode,
|
|
614
913
|
// AC-R7-002: explicit parameter from the dispatcher — the runtime
|
|
615
914
|
// store.create wrapper is gone; body workspaceId (if any) only acts as
|
|
616
915
|
// a fallback for direct (non-dispatcher) handler tests.
|
|
@@ -678,24 +977,66 @@ export async function handleForkChatSession(req, res, deps, sourceSessionId) {
|
|
|
678
977
|
});
|
|
679
978
|
return;
|
|
680
979
|
}
|
|
980
|
+
let forkedHandle;
|
|
981
|
+
let forkPersisted = false;
|
|
681
982
|
try {
|
|
983
|
+
const body = await readJsonBody(req);
|
|
984
|
+
const throughMessageId = typeof body.throughMessageId === "string" && body.throughMessageId.trim()
|
|
985
|
+
? body.throughMessageId.trim()
|
|
986
|
+
: undefined;
|
|
987
|
+
// Accept a user or assistant message as the fork anchor. An assistant
|
|
988
|
+
// anchor resolves to the user message of its own turn so the runtime
|
|
989
|
+
// branch and the persisted copy both truncate at the same Q&A pair.
|
|
990
|
+
const anchorIndex = throughMessageId
|
|
991
|
+
? source.messages.findIndex((message) => message.id === throughMessageId)
|
|
992
|
+
: -1;
|
|
993
|
+
if (throughMessageId && anchorIndex < 0) {
|
|
994
|
+
throw new Error("FORK_MESSAGE_NOT_FOUND");
|
|
995
|
+
}
|
|
996
|
+
const throughUserMessageIndex = throughMessageId
|
|
997
|
+
? (() => {
|
|
998
|
+
const boundary = anchorIndex >= 0 && source.messages[anchorIndex]?.role === "assistant"
|
|
999
|
+
? (() => {
|
|
1000
|
+
let index = anchorIndex;
|
|
1001
|
+
while (index >= 0 && source.messages[index]?.role !== "user") {
|
|
1002
|
+
index -= 1;
|
|
1003
|
+
}
|
|
1004
|
+
return index;
|
|
1005
|
+
})()
|
|
1006
|
+
: anchorIndex;
|
|
1007
|
+
if (boundary < 0)
|
|
1008
|
+
throw new Error("FORK_MESSAGE_TURN_USER_NOT_FOUND");
|
|
1009
|
+
return source.messages
|
|
1010
|
+
.slice(0, boundary + 1)
|
|
1011
|
+
.filter((message) => message.role === "user").length - 1;
|
|
1012
|
+
})()
|
|
1013
|
+
: undefined;
|
|
682
1014
|
const handle = await deps.runtime.forkSession({
|
|
683
1015
|
sourceSessionFile: source.sessionFile ?? "",
|
|
1016
|
+
sessionMode: source.sessionMode,
|
|
684
1017
|
...(source.modelProvider && source.modelId
|
|
685
1018
|
? { model: { provider: source.modelProvider, modelId: source.modelId } }
|
|
686
1019
|
: {}),
|
|
1020
|
+
...(throughUserMessageIndex !== undefined
|
|
1021
|
+
? { throughUserMessageIndex }
|
|
1022
|
+
: {}),
|
|
687
1023
|
});
|
|
1024
|
+
forkedHandle = handle;
|
|
688
1025
|
await deps.store.forkFrom({
|
|
689
1026
|
sourceSessionId,
|
|
690
1027
|
targetSessionId: handle.sessionId,
|
|
691
1028
|
targetSessionFile: handle.sessionFile,
|
|
1029
|
+
...(throughMessageId ? { throughMessageId } : {}),
|
|
692
1030
|
});
|
|
1031
|
+
forkPersisted = true;
|
|
693
1032
|
await deps.attachSession?.(handle.sessionId).catch(() => undefined);
|
|
694
|
-
const copied =
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
1033
|
+
const copied = throughMessageId
|
|
1034
|
+
? { copiedCount: 0 }
|
|
1035
|
+
: deps.events.forkContextRefs({
|
|
1036
|
+
sourceSessionId,
|
|
1037
|
+
targetSessionId: handle.sessionId,
|
|
1038
|
+
targetTurnId: `${handle.sessionId}:fork`,
|
|
1039
|
+
});
|
|
699
1040
|
await deps.operationLinker.recoverSession(handle.sessionId);
|
|
700
1041
|
sendJson(res, 201, {
|
|
701
1042
|
ok: true,
|
|
@@ -709,6 +1050,12 @@ export async function handleForkChatSession(req, res, deps, sourceSessionId) {
|
|
|
709
1050
|
});
|
|
710
1051
|
}
|
|
711
1052
|
catch (error) {
|
|
1053
|
+
if (forkedHandle && !forkPersisted) {
|
|
1054
|
+
deps.runtime.dispose(forkedHandle.sessionId);
|
|
1055
|
+
await deps.store
|
|
1056
|
+
.discardUnownedSession(forkedHandle.sessionId, forkedHandle.sessionFile)
|
|
1057
|
+
.catch(() => undefined);
|
|
1058
|
+
}
|
|
712
1059
|
sendJson(res, 409, {
|
|
713
1060
|
ok: false,
|
|
714
1061
|
error: {
|
|
@@ -747,10 +1094,7 @@ export async function handleBranchChatSession(req, res, deps, sessionId) {
|
|
|
747
1094
|
}
|
|
748
1095
|
try {
|
|
749
1096
|
if (!deps.runtime.hasSession(sessionId)) {
|
|
750
|
-
const reopened = await deps.runtime.reopenSession(
|
|
751
|
-
sessionId,
|
|
752
|
-
sessionFile: record.sessionFile,
|
|
753
|
-
});
|
|
1097
|
+
const reopened = await deps.runtime.reopenSession(reopenInitFromRecord(record));
|
|
754
1098
|
if (!reopened.ok) {
|
|
755
1099
|
const mapped = await mapFailedReopen(deps, sessionId, reopened);
|
|
756
1100
|
sendJson(res, mapped.status, {
|
|
@@ -858,12 +1202,21 @@ export async function handleGetChatSession(_req, res, deps, sessionId) {
|
|
|
858
1202
|
const lastEvent = snapshot.at(-1);
|
|
859
1203
|
const activeTurn = deps.events.getActiveTurn(sessionId);
|
|
860
1204
|
const pendingQuestion = await deps.questionRegistry?.get(sessionId);
|
|
1205
|
+
const sddSessionIdentity = deps.sddDataAlignment && (await deps.sddDataAlignment.isEligible())
|
|
1206
|
+
? { telemetrySessionId: record.telemetrySessionId ?? null }
|
|
1207
|
+
: undefined;
|
|
1208
|
+
const projectedMessages = projectStoredMessagesWithEventOrder(messagePage.messages, snapshot);
|
|
1209
|
+
const turnStates = turnStatesForIds(deps.events, sessionId, [
|
|
1210
|
+
...projectedMessages.map((message) => message.turnId),
|
|
1211
|
+
...snapshot.map((event) => event.turnId),
|
|
1212
|
+
]);
|
|
861
1213
|
sendJson(res, 200, {
|
|
862
1214
|
ok: true,
|
|
863
1215
|
eventEpoch: deps.events.epoch(sessionId),
|
|
864
1216
|
session: {
|
|
865
1217
|
...projectBrowserChatSession(record),
|
|
866
|
-
messages:
|
|
1218
|
+
messages: projectedMessages,
|
|
1219
|
+
turnStates,
|
|
867
1220
|
composerDraft,
|
|
868
1221
|
activeTurnId: activeTurn?.turnId,
|
|
869
1222
|
turnPhase: activeTurn
|
|
@@ -883,6 +1236,10 @@ export async function handleGetChatSession(_req, res, deps, sessionId) {
|
|
|
883
1236
|
sessionStats: telemetry.stats,
|
|
884
1237
|
usage: telemetry.usage,
|
|
885
1238
|
turnTelemetry: deriveChatTurnTelemetry(snapshot),
|
|
1239
|
+
userTurnOutline: projectUserTurnOutline(record.messages ?? []),
|
|
1240
|
+
...(sddSessionIdentity
|
|
1241
|
+
? { sddDataAlignment: sddSessionIdentity }
|
|
1242
|
+
: {}),
|
|
886
1243
|
contextPressure: typeof deps.runtime.getContextPressureTelemetry === "function"
|
|
887
1244
|
? deps.runtime.getContextPressureTelemetry(sessionId)
|
|
888
1245
|
: undefined,
|
|
@@ -898,6 +1255,16 @@ export async function handleGetChatSession(_req, res, deps, sessionId) {
|
|
|
898
1255
|
turnId: event.turnId,
|
|
899
1256
|
data: event.data,
|
|
900
1257
|
})),
|
|
1258
|
+
conversationEvents: snapshot.filter((event) => [
|
|
1259
|
+
"step_start",
|
|
1260
|
+
"step_end",
|
|
1261
|
+
"model_retry",
|
|
1262
|
+
"turn_error",
|
|
1263
|
+
"turn_max_tokens",
|
|
1264
|
+
"request_context",
|
|
1265
|
+
"command",
|
|
1266
|
+
"unknown",
|
|
1267
|
+
].includes(event.kind)),
|
|
901
1268
|
lastEventId: lastEvent?.eventId ?? null,
|
|
902
1269
|
lastEventSeq: lastEvent?.seq ?? 0,
|
|
903
1270
|
});
|
|
@@ -1139,25 +1506,31 @@ export async function handleSteerDeferredTurn(req, res, deps, sessionId, clientR
|
|
|
1139
1506
|
}
|
|
1140
1507
|
}
|
|
1141
1508
|
async function persistAcceptedSteering(deps, sessionId, turnId, userMessage, clientRequestId) {
|
|
1509
|
+
emitUserMessageEvent(deps, sessionId, turnId, userMessage, clientRequestId, true);
|
|
1510
|
+
await emitDeferredTurnUpdate(deps, sessionId, turnId, undefined, 0);
|
|
1511
|
+
}
|
|
1512
|
+
function emitUserMessageEvent(deps, sessionId, turnId, userMessage, clientRequestId, steering = false) {
|
|
1142
1513
|
deps.events.append(sessionId, turnId, {
|
|
1143
1514
|
kind: "user_message",
|
|
1144
1515
|
data: {
|
|
1145
|
-
clientRequestId,
|
|
1146
|
-
steering: true,
|
|
1516
|
+
...(clientRequestId ? { clientRequestId } : {}),
|
|
1517
|
+
...(steering ? { steering: true } : {}),
|
|
1147
1518
|
message: {
|
|
1148
1519
|
id: userMessage.id,
|
|
1149
1520
|
text: userMessage.text,
|
|
1150
1521
|
at: userMessage.at,
|
|
1522
|
+
...(userMessage.attachments?.length
|
|
1523
|
+
? { attachments: userMessage.attachments }
|
|
1524
|
+
: {}),
|
|
1151
1525
|
},
|
|
1152
1526
|
},
|
|
1153
1527
|
});
|
|
1154
|
-
await emitDeferredTurnUpdate(deps, sessionId, turnId, undefined, 0);
|
|
1155
1528
|
}
|
|
1156
1529
|
/** Lightweight busy-truth endpoint for the client reconcile loop (pi-web
|
|
1157
1530
|
* get_state equivalent): exposes the active turn + the latest turn's durable
|
|
1158
1531
|
* outcome WITHOUT the full message payload, so the UI can poll it on an
|
|
1159
1532
|
* interval / visibility / online without paying for session detail. */
|
|
1160
|
-
export async function handleGetChatSessionState(
|
|
1533
|
+
export async function handleGetChatSessionState(req, res, deps, sessionId) {
|
|
1161
1534
|
const record = await deps.store.get(sessionId);
|
|
1162
1535
|
if (!record) {
|
|
1163
1536
|
sendJson(res, 404, {
|
|
@@ -1188,9 +1561,15 @@ export async function handleGetChatSessionState(_req, res, deps, sessionId) {
|
|
|
1188
1561
|
: runtimeBusy
|
|
1189
1562
|
? "running"
|
|
1190
1563
|
: "idle";
|
|
1564
|
+
const turnStates = turnStatesForIds(deps.events, sessionId, [
|
|
1565
|
+
active?.turnId,
|
|
1566
|
+
latest?.turnId,
|
|
1567
|
+
...requestedStateTurnIds(req),
|
|
1568
|
+
]);
|
|
1191
1569
|
sendJson(res, 200, {
|
|
1192
1570
|
ok: true,
|
|
1193
1571
|
state: {
|
|
1572
|
+
turnStates,
|
|
1194
1573
|
// Legacy flat fields retained for older clients.
|
|
1195
1574
|
activeTurnId: active?.turnId ?? null,
|
|
1196
1575
|
activeTurnState: active?.state ?? null,
|
|
@@ -1224,13 +1603,8 @@ export async function handleChatGoal(req, res, deps, sessionId) {
|
|
|
1224
1603
|
});
|
|
1225
1604
|
return;
|
|
1226
1605
|
}
|
|
1227
|
-
if (!(await deps
|
|
1228
|
-
sendJson(res, 404, {
|
|
1229
|
-
ok: false,
|
|
1230
|
-
error: { code: "NOT_FOUND", message: `chat session not found: ${sessionId}` },
|
|
1231
|
-
});
|
|
1606
|
+
if (!(await requireChatSession(res, deps, sessionId)))
|
|
1232
1607
|
return;
|
|
1233
|
-
}
|
|
1234
1608
|
if (req.method === "GET") {
|
|
1235
1609
|
sendJson(res, 200, { ok: true, goal: registry.get(sessionId) });
|
|
1236
1610
|
return;
|
|
@@ -1444,6 +1818,7 @@ export async function listChatSessionSummaries(deps, ownerWorkspaceId) {
|
|
|
1444
1818
|
...(session.thinkingLevel !== undefined
|
|
1445
1819
|
? { thinkingLevel: session.thinkingLevel }
|
|
1446
1820
|
: {}),
|
|
1821
|
+
sessionMode: session.sessionMode,
|
|
1447
1822
|
activeTurnId: active?.turnId,
|
|
1448
1823
|
turnPhase: active?.abortRequestedAt
|
|
1449
1824
|
? "stopping"
|
|
@@ -1467,6 +1842,16 @@ export async function handlePatchChatSession(req, res, deps, sessionId) {
|
|
|
1467
1842
|
}
|
|
1468
1843
|
try {
|
|
1469
1844
|
const body = await readJsonBody(req);
|
|
1845
|
+
if ("sessionMode" in body) {
|
|
1846
|
+
sendJson(res, 409, {
|
|
1847
|
+
ok: false,
|
|
1848
|
+
error: {
|
|
1849
|
+
code: SESSION_MODE_IMMUTABLE_CODE,
|
|
1850
|
+
message: "sessionMode cannot be changed after create",
|
|
1851
|
+
},
|
|
1852
|
+
});
|
|
1853
|
+
return;
|
|
1854
|
+
}
|
|
1470
1855
|
let record;
|
|
1471
1856
|
if (typeof body.title === "string")
|
|
1472
1857
|
record = await deps.store.rename(sessionId, body.title);
|
|
@@ -1525,13 +1910,7 @@ export async function handleReopenChatSession(req, res, deps, sessionId) {
|
|
|
1525
1910
|
});
|
|
1526
1911
|
return;
|
|
1527
1912
|
}
|
|
1528
|
-
const result = await deps.runtime.reopenSession(
|
|
1529
|
-
sessionId,
|
|
1530
|
-
sessionFile: record.sessionFile,
|
|
1531
|
-
...(record.modelProvider && record.modelId
|
|
1532
|
-
? { model: { provider: record.modelProvider, modelId: record.modelId } }
|
|
1533
|
-
: {}),
|
|
1534
|
-
});
|
|
1913
|
+
const result = await deps.runtime.reopenSession(reopenInitFromRecord(record));
|
|
1535
1914
|
if (!result.ok) {
|
|
1536
1915
|
const mapped = await mapFailedReopen(deps, sessionId, result);
|
|
1537
1916
|
sendJson(res, mapped.status, {
|
|
@@ -1611,13 +1990,7 @@ export async function handleChatPrompt(req, res, deps, sessionId) {
|
|
|
1611
1990
|
});
|
|
1612
1991
|
return;
|
|
1613
1992
|
}
|
|
1614
|
-
const reopen = await deps.runtime.reopenSession(
|
|
1615
|
-
sessionId,
|
|
1616
|
-
sessionFile: record.sessionFile,
|
|
1617
|
-
...(record.modelProvider && record.modelId
|
|
1618
|
-
? { model: { provider: record.modelProvider, modelId: record.modelId } }
|
|
1619
|
-
: {}),
|
|
1620
|
-
});
|
|
1993
|
+
const reopen = await deps.runtime.reopenSession(reopenInitFromRecord(record));
|
|
1621
1994
|
if (!reopen.ok) {
|
|
1622
1995
|
const mapped = await mapFailedReopen(deps, sessionId, reopen);
|
|
1623
1996
|
sendJson(res, mapped.status, {
|
|
@@ -1695,7 +2068,8 @@ export async function handleChatPrompt(req, res, deps, sessionId) {
|
|
|
1695
2068
|
text,
|
|
1696
2069
|
images,
|
|
1697
2070
|
signal: ac.signal,
|
|
1698
|
-
onUserMessagePersisted: (record) => {
|
|
2071
|
+
onUserMessagePersisted: (record, userMessage) => {
|
|
2072
|
+
emitUserMessageEvent(deps, sessionId, turnId, userMessage);
|
|
1699
2073
|
const users = record.messages.filter((message) => message.role === "user");
|
|
1700
2074
|
if (users.length !== 1)
|
|
1701
2075
|
return;
|
|
@@ -1880,13 +2254,7 @@ export async function handleCreateChatTurn(req, res, deps, sessionId, trustedPay
|
|
|
1880
2254
|
});
|
|
1881
2255
|
return;
|
|
1882
2256
|
}
|
|
1883
|
-
const reopen = await deps.runtime.reopenSession(
|
|
1884
|
-
sessionId,
|
|
1885
|
-
sessionFile: record.sessionFile,
|
|
1886
|
-
...(record.modelProvider && record.modelId
|
|
1887
|
-
? { model: { provider: record.modelProvider, modelId: record.modelId } }
|
|
1888
|
-
: {}),
|
|
1889
|
-
});
|
|
2257
|
+
const reopen = await deps.runtime.reopenSession(reopenInitFromRecord(record));
|
|
1890
2258
|
if (!reopen.ok) {
|
|
1891
2259
|
const mapped = await mapFailedReopen(deps, sessionId, reopen);
|
|
1892
2260
|
sendJson(res, mapped.status, {
|
|
@@ -2005,9 +2373,12 @@ export async function handleCreateChatTurn(req, res, deps, sessionId, trustedPay
|
|
|
2005
2373
|
sessionId,
|
|
2006
2374
|
turnId: turn.turnId,
|
|
2007
2375
|
text,
|
|
2376
|
+
clientRequestId,
|
|
2008
2377
|
images,
|
|
2009
2378
|
signal: controller.signal,
|
|
2010
2379
|
onUserMessagePersisted: async (record, userMessage) => {
|
|
2380
|
+
if (!trustedPayload)
|
|
2381
|
+
emitUserMessageEvent(deps, sessionId, turn.turnId, userMessage, clientRequestId);
|
|
2011
2382
|
await trustedPayload?.onUserMessagePersisted?.(turn.turnId, userMessage);
|
|
2012
2383
|
const users = record.messages.filter((message) => message.role === "user");
|
|
2013
2384
|
if (users.length !== 1)
|
|
@@ -2555,6 +2926,7 @@ export async function handleChatEventsStream(req, res, deps, sessionId) {
|
|
|
2555
2926
|
kind: "stream_ready",
|
|
2556
2927
|
sessionId,
|
|
2557
2928
|
epoch: deps.events.epoch(sessionId),
|
|
2929
|
+
lastSeq: deps.events.latestSeq(sessionId),
|
|
2558
2930
|
latestEventId: retainedTail?.eventId ?? null,
|
|
2559
2931
|
});
|
|
2560
2932
|
let closed = false;
|
|
@@ -2776,28 +3148,8 @@ async function handlePiConfig(req, res, deps, resource) {
|
|
|
2776
3148
|
async function handleChatFiles(res, deps, sessionId, query) {
|
|
2777
3149
|
if (!(await requireChatSession(res, deps, sessionId)))
|
|
2778
3150
|
return;
|
|
2779
|
-
const
|
|
2780
|
-
|
|
2781
|
-
skipSensitive: true,
|
|
2782
|
-
maxFiles: 5000,
|
|
2783
|
-
includeDirs: true,
|
|
2784
|
-
});
|
|
2785
|
-
const entries = walked
|
|
2786
|
-
.filter((entry) => !isSensitivePath(entry.repoRelative) &&
|
|
2787
|
-
(!q || entry.repoRelative.toLowerCase().includes(q)))
|
|
2788
|
-
.sort((a, b) => {
|
|
2789
|
-
const aDir = a.repoRelative.endsWith("/");
|
|
2790
|
-
const bDir = b.repoRelative.endsWith("/");
|
|
2791
|
-
if (aDir !== bDir)
|
|
2792
|
-
return aDir ? -1 : 1;
|
|
2793
|
-
return a.repoRelative.localeCompare(b.repoRelative);
|
|
2794
|
-
})
|
|
2795
|
-
.slice(0, 50)
|
|
2796
|
-
.map((entry) => ({
|
|
2797
|
-
path: entry.repoRelative,
|
|
2798
|
-
isDir: entry.repoRelative.endsWith("/"),
|
|
2799
|
-
}));
|
|
2800
|
-
sendJson(res, 200, { ok: true, files: entries });
|
|
3151
|
+
const files = await listChatFilePalette(deps.appData.repoRoot, query);
|
|
3152
|
+
sendJson(res, 200, { ok: true, files });
|
|
2801
3153
|
}
|
|
2802
3154
|
export async function handleChatCommands(res, deps, sessionId) {
|
|
2803
3155
|
try {
|
|
@@ -2809,6 +3161,7 @@ export async function handleChatCommands(res, deps, sessionId) {
|
|
|
2809
3161
|
const warning = typeof listed.warning === "string" && listed.warning.trim()
|
|
2810
3162
|
? listed.warning.trim().slice(0, 160)
|
|
2811
3163
|
: undefined;
|
|
3164
|
+
const resolvedMode = await readChatSessionMode(deps, sessionId);
|
|
2812
3165
|
res.statusCode = 200;
|
|
2813
3166
|
res.setHeader("content-type", "application/json; charset=utf-8");
|
|
2814
3167
|
res.end(JSON.stringify({
|
|
@@ -2816,18 +3169,25 @@ export async function handleChatCommands(res, deps, sessionId) {
|
|
|
2816
3169
|
commands,
|
|
2817
3170
|
capabilities: {
|
|
2818
3171
|
sddBingo: Boolean(await deps.sddDataAlignment?.isEligible()),
|
|
3172
|
+
...(resolvedMode.ok ? { sessionMode: resolvedMode.sessionMode } : {}),
|
|
2819
3173
|
},
|
|
2820
3174
|
...(warning ? { warning } : {}),
|
|
2821
3175
|
}));
|
|
2822
3176
|
}
|
|
2823
3177
|
catch (error) {
|
|
2824
3178
|
// Whole-list / readiness failures stay fail-soft with empty commands + warning.
|
|
3179
|
+
// Keep the two capability shapes symmetric: the fallback mode mirrors the
|
|
3180
|
+
// client-side prop fallback in useComposer.
|
|
3181
|
+
const fallbackMode = await readChatSessionMode(deps, sessionId).catch(() => undefined);
|
|
2825
3182
|
res.statusCode = 200;
|
|
2826
3183
|
res.setHeader("content-type", "application/json; charset=utf-8");
|
|
2827
3184
|
res.end(JSON.stringify({
|
|
2828
3185
|
ok: true,
|
|
2829
3186
|
commands: [],
|
|
2830
|
-
capabilities: {
|
|
3187
|
+
capabilities: {
|
|
3188
|
+
sddBingo: false,
|
|
3189
|
+
...(fallbackMode?.ok ? { sessionMode: fallbackMode.sessionMode } : {}),
|
|
3190
|
+
},
|
|
2831
3191
|
warning: error instanceof Error ? error.message : "commands unavailable",
|
|
2832
3192
|
}));
|
|
2833
3193
|
}
|
|
@@ -2850,8 +3210,33 @@ async function handleSddBingo(req, res, deps, sessionId) {
|
|
|
2850
3210
|
if (Object.keys(body).some((key) => key !== "skillName")) {
|
|
2851
3211
|
throw new SddDataAlignmentError("SDD_INVALID_SKILL_NAME", "请求体只允许 skillName 字段", 400);
|
|
2852
3212
|
}
|
|
2853
|
-
await deps.sddDataAlignment.report(sessionId, body.skillName);
|
|
2854
|
-
|
|
3213
|
+
const result = await deps.sddDataAlignment.report(sessionId, body.skillName);
|
|
3214
|
+
const turnId = deps.events.latestTurnId(sessionId) ?? `${sessionId}:operator-action`;
|
|
3215
|
+
deps.events.append(sessionId, turnId, {
|
|
3216
|
+
kind: "operator-action",
|
|
3217
|
+
data: {
|
|
3218
|
+
schemaVersion: 1,
|
|
3219
|
+
action: "sdd-bingo",
|
|
3220
|
+
skillName: result.skillName,
|
|
3221
|
+
telemetrySessionId: result.telemetrySessionId,
|
|
3222
|
+
},
|
|
3223
|
+
});
|
|
3224
|
+
deps.events.append(sessionId, `${sessionId}:command:${result.telemetrySessionId}`, {
|
|
3225
|
+
kind: "command",
|
|
3226
|
+
data: {
|
|
3227
|
+
commandId: `sdd-bingo:${result.telemetrySessionId}`,
|
|
3228
|
+
name: "sdd-bingo",
|
|
3229
|
+
...(result.skillName ? { argsPreview: result.skillName } : {}),
|
|
3230
|
+
action: "sdd-bingo",
|
|
3231
|
+
outcome: "completed",
|
|
3232
|
+
message: `Session: ${result.telemetrySessionId}`,
|
|
3233
|
+
},
|
|
3234
|
+
});
|
|
3235
|
+
sendJson(res, 200, {
|
|
3236
|
+
ok: true,
|
|
3237
|
+
skillName: result.skillName,
|
|
3238
|
+
telemetrySessionId: result.telemetrySessionId,
|
|
3239
|
+
});
|
|
2855
3240
|
}
|
|
2856
3241
|
catch (error) {
|
|
2857
3242
|
if (error instanceof SddDataAlignmentError) {
|
|
@@ -2868,20 +3253,25 @@ async function handleSddBingo(req, res, deps, sessionId) {
|
|
|
2868
3253
|
}
|
|
2869
3254
|
}
|
|
2870
3255
|
export async function handleRuntimeContext(res, deps, sessionId) {
|
|
2871
|
-
const
|
|
2872
|
-
if (!
|
|
2873
|
-
sendJson(res,
|
|
3256
|
+
const record = await deps.store.get(sessionId);
|
|
3257
|
+
if (!record) {
|
|
3258
|
+
sendJson(res, 404, {
|
|
3259
|
+
ok: false,
|
|
3260
|
+
error: {
|
|
3261
|
+
code: "PI_SESSION_NOT_FOUND",
|
|
3262
|
+
message: `chat session not found: ${sessionId}`,
|
|
3263
|
+
},
|
|
3264
|
+
});
|
|
2874
3265
|
return;
|
|
2875
3266
|
}
|
|
2876
|
-
//
|
|
2877
|
-
//
|
|
2878
|
-
//
|
|
2879
|
-
// length-limited projection. RF-02: a critical projection failure is 503
|
|
2880
|
-
// PI_RUNTIME_UNAVAILABLE — never a 500 leak or a fake-empty context.
|
|
3267
|
+
// Live Agent is preferred. Cold inspect uses workspace ResourceLoader /
|
|
3268
|
+
// ModelRuntime without SessionManager.open(jsonl); prompt still reopens.
|
|
3269
|
+
// RF-02: a critical projection failure is 503 PI_RUNTIME_UNAVAILABLE.
|
|
2881
3270
|
let snapshot;
|
|
2882
3271
|
try {
|
|
2883
3272
|
snapshot = await deps.runtime.getRuntimeSnapshot(sessionId, {
|
|
2884
3273
|
includeRaw: true,
|
|
3274
|
+
workspaceProjection: workspaceProjectionFromRecord(record),
|
|
2885
3275
|
});
|
|
2886
3276
|
}
|
|
2887
3277
|
catch (error) {
|
|
@@ -2931,19 +3321,94 @@ export async function handleRuntimeContext(res, deps, sessionId) {
|
|
|
2931
3321
|
}),
|
|
2932
3322
|
});
|
|
2933
3323
|
}
|
|
3324
|
+
/**
|
|
3325
|
+
* GET /sessions/:id/context-insights: dsh-context parity dashboard projection.
|
|
3326
|
+
* Folds the persisted event ring (works for idle/restored sessions) and, when
|
|
3327
|
+
* the Pi session is live, anchors the estimate with the runtime snapshot's
|
|
3328
|
+
* system-prompt size + active tool count. Read-only; never fabricates payload
|
|
3329
|
+
* text beyond the bounded previews already stored on the ring.
|
|
3330
|
+
*/
|
|
3331
|
+
export async function handleContextInsights(res, deps, sessionId) {
|
|
3332
|
+
const record = await deps.store.get(sessionId);
|
|
3333
|
+
if (!record) {
|
|
3334
|
+
sendJson(res, 404, {
|
|
3335
|
+
ok: false,
|
|
3336
|
+
error: { code: "NOT_FOUND", message: `chat session not found: ${sessionId}` },
|
|
3337
|
+
});
|
|
3338
|
+
return;
|
|
3339
|
+
}
|
|
3340
|
+
await deps.operationLinker.recoverSession(sessionId);
|
|
3341
|
+
const snapshot = deps.events.snapshot(sessionId);
|
|
3342
|
+
let systemPromptChars;
|
|
3343
|
+
let systemPromptPreview;
|
|
3344
|
+
let activeTools;
|
|
3345
|
+
let skillInjections;
|
|
3346
|
+
try {
|
|
3347
|
+
const runtimeSnapshot = await deps.runtime.getRuntimeSnapshot(sessionId, {
|
|
3348
|
+
includeRaw: true,
|
|
3349
|
+
workspaceProjection: workspaceProjectionFromRecord(record),
|
|
3350
|
+
});
|
|
3351
|
+
if (runtimeSnapshot) {
|
|
3352
|
+
systemPromptChars = runtimeSnapshot.systemPrompt.characters;
|
|
3353
|
+
const raw = runtimeSnapshot.systemPrompt.raw ??
|
|
3354
|
+
runtimeSnapshot.systemPrompt.text;
|
|
3355
|
+
if (typeof raw === "string" && raw) {
|
|
3356
|
+
systemPromptPreview = raw.slice(0, 120_000);
|
|
3357
|
+
}
|
|
3358
|
+
activeTools = runtimeSnapshot.activeTools;
|
|
3359
|
+
skillInjections = runtimeSnapshot.skills
|
|
3360
|
+
.slice(0, 50)
|
|
3361
|
+
.map((skill) => ({
|
|
3362
|
+
name: skill.name,
|
|
3363
|
+
tokens: estimateTokensFromText(skill.description ?? skill.name),
|
|
3364
|
+
}));
|
|
3365
|
+
}
|
|
3366
|
+
}
|
|
3367
|
+
catch {
|
|
3368
|
+
// Insights are derived from the durable ring; a live-snapshot miss only
|
|
3369
|
+
// drops the anchor figures / browser static categories, never the dashboard.
|
|
3370
|
+
}
|
|
3371
|
+
// Tool schemas: capabilities registry projection for whitelisted operator
|
|
3372
|
+
// actions, plus Pi builtins active on this session.
|
|
3373
|
+
const schemaTools = buildModelCallableToolSchemas().map((tool) => ({
|
|
3374
|
+
id: tool.toolId,
|
|
3375
|
+
description: tool.description,
|
|
3376
|
+
}));
|
|
3377
|
+
const activeSet = new Set((activeTools ?? []).map((t) => t.toLowerCase()));
|
|
3378
|
+
const builtinTools = (activeTools ?? [])
|
|
3379
|
+
.filter((name) => !schemaTools.some((t) => t.id === name))
|
|
3380
|
+
.map((name) => ({ id: name, description: "" }));
|
|
3381
|
+
const insights = deriveContextInsights(snapshot, {
|
|
3382
|
+
sessionId,
|
|
3383
|
+
...(systemPromptChars !== undefined ? { systemPromptChars } : {}),
|
|
3384
|
+
...(systemPromptPreview !== undefined
|
|
3385
|
+
? { systemPromptPreview }
|
|
3386
|
+
: {}),
|
|
3387
|
+
tools: [...schemaTools, ...builtinTools],
|
|
3388
|
+
...(skillInjections ? { skillInjections } : {}),
|
|
3389
|
+
});
|
|
3390
|
+
sendJson(res, 200, { ok: true, insights });
|
|
3391
|
+
}
|
|
2934
3392
|
// Exported for the direct handler-boundary tests (trace binding for
|
|
2935
3393
|
// vt-route-model-errors); router dispatch behavior is unchanged.
|
|
2936
3394
|
export async function handleModels(req, res, deps, sessionId) {
|
|
2937
|
-
// AC-01/AC-06: models (and model/thinking mutation) requests reuse the same
|
|
2938
|
-
// activation path. A missing/reopen-failed runtime is a STRUCTURED error —
|
|
2939
|
-
// never a fabricated empty model list.
|
|
2940
|
-
const ensured = await ensureActiveSession(deps, sessionId);
|
|
2941
|
-
if (!ensured.ok) {
|
|
2942
|
-
sendJson(res, ensured.status, { ok: false, error: { code: ensured.code, message: ensured.message } });
|
|
2943
|
-
return;
|
|
2944
|
-
}
|
|
2945
3395
|
try {
|
|
2946
3396
|
if ((req.method ?? "GET").toUpperCase() === "GET") {
|
|
3397
|
+
// Catalog is workspace-scoped (models.json / ModelRuntime). Do not wait
|
|
3398
|
+
// for Pi conversation reopen and do not start it here — prompt / reload
|
|
3399
|
+
// / model mutation still reopen, and concurrent reopen would stall this
|
|
3400
|
+
// cheap list on the single Node thread.
|
|
3401
|
+
const record = await deps.store.get(sessionId);
|
|
3402
|
+
if (!record) {
|
|
3403
|
+
sendJson(res, 404, {
|
|
3404
|
+
ok: false,
|
|
3405
|
+
error: {
|
|
3406
|
+
code: "PI_SESSION_NOT_FOUND",
|
|
3407
|
+
message: `chat session not found: ${sessionId}`,
|
|
3408
|
+
},
|
|
3409
|
+
});
|
|
3410
|
+
return;
|
|
3411
|
+
}
|
|
2947
3412
|
let models;
|
|
2948
3413
|
try {
|
|
2949
3414
|
models = await deps.runtime.listModels(sessionId);
|
|
@@ -2972,6 +3437,11 @@ export async function handleModels(req, res, deps, sessionId) {
|
|
|
2972
3437
|
});
|
|
2973
3438
|
return;
|
|
2974
3439
|
}
|
|
3440
|
+
const ensured = await ensureActiveSession(deps, sessionId);
|
|
3441
|
+
if (!ensured.ok) {
|
|
3442
|
+
sendJson(res, ensured.status, { ok: false, error: { code: ensured.code, message: ensured.message } });
|
|
3443
|
+
return;
|
|
3444
|
+
}
|
|
2975
3445
|
const gate = gateMutation(req, deps);
|
|
2976
3446
|
if (!gate.ok) {
|
|
2977
3447
|
sendJson(res, gate.status, { ok: false, error: { code: gate.code, message: gate.message } });
|
|
@@ -3027,18 +3497,24 @@ async function requireChatSession(res, deps, sessionId) {
|
|
|
3027
3497
|
});
|
|
3028
3498
|
return false;
|
|
3029
3499
|
}
|
|
3030
|
-
|
|
3031
|
-
* Unified session activation (design §2.1 / AC-01): models, runtime snapshot,
|
|
3032
|
-
* reload and model/thinking mutation requests all flow through this path.
|
|
3033
|
-
*
|
|
3034
|
-
* - runtime already holds the session → ok;
|
|
3035
|
-
* - only a persisted record exists → reopen from record.sessionFile;
|
|
3036
|
-
* - no record / missing session file → 404 PI_SESSION_NOT_FOUND;
|
|
3037
|
-
* - SDK reopen failure → 503 PI_RUNTIME_UNAVAILABLE.
|
|
3038
|
-
*
|
|
3039
|
-
* Never fabricates a fresh session or an empty model list.
|
|
3040
|
-
*/
|
|
3500
|
+
const ensureActiveSessionInFlight = new WeakMap();
|
|
3041
3501
|
export async function ensureActiveSession(deps, sessionId) {
|
|
3502
|
+
let bySession = ensureActiveSessionInFlight.get(deps);
|
|
3503
|
+
if (!bySession) {
|
|
3504
|
+
bySession = new Map();
|
|
3505
|
+
ensureActiveSessionInFlight.set(deps, bySession);
|
|
3506
|
+
}
|
|
3507
|
+
const existing = bySession.get(sessionId);
|
|
3508
|
+
if (existing)
|
|
3509
|
+
return existing;
|
|
3510
|
+
const pending = runEnsureActiveSession(deps, sessionId).finally(() => {
|
|
3511
|
+
if (bySession.get(sessionId) === pending)
|
|
3512
|
+
bySession.delete(sessionId);
|
|
3513
|
+
});
|
|
3514
|
+
bySession.set(sessionId, pending);
|
|
3515
|
+
return pending;
|
|
3516
|
+
}
|
|
3517
|
+
async function runEnsureActiveSession(deps, sessionId) {
|
|
3042
3518
|
// Wait for create-path materialization before treating the session as missing.
|
|
3043
3519
|
if (typeof deps.runtime.ensureSessionReady === "function") {
|
|
3044
3520
|
try {
|
|
@@ -3072,13 +3548,7 @@ export async function ensureActiveSession(deps, sessionId) {
|
|
|
3072
3548
|
message: `chat session not found: ${sessionId}`,
|
|
3073
3549
|
};
|
|
3074
3550
|
}
|
|
3075
|
-
const reopened = await deps.runtime.reopenSession(
|
|
3076
|
-
sessionId,
|
|
3077
|
-
sessionFile: record.sessionFile,
|
|
3078
|
-
...(record.modelProvider && record.modelId
|
|
3079
|
-
? { model: { provider: record.modelProvider, modelId: record.modelId } }
|
|
3080
|
-
: {}),
|
|
3081
|
-
});
|
|
3551
|
+
const reopened = await deps.runtime.reopenSession(reopenInitFromRecord(record));
|
|
3082
3552
|
if (!reopened.ok) {
|
|
3083
3553
|
return { ok: false, ...(await mapFailedReopen(deps, sessionId, reopened)) };
|
|
3084
3554
|
}
|
|
@@ -3290,19 +3760,28 @@ export async function handleReloadSession(req, res, deps, sessionId) {
|
|
|
3290
3760
|
});
|
|
3291
3761
|
}
|
|
3292
3762
|
/**
|
|
3293
|
-
* GET /sessions/:id/runtime-snapshot (AC-01):
|
|
3763
|
+
* GET /sessions/:id/runtime-snapshot (AC-01): live Agent when present,
|
|
3764
|
+
* otherwise workspace ResourceLoader projection (no JSONL reopen).
|
|
3294
3765
|
*/
|
|
3295
3766
|
export async function handleRuntimeSnapshot(_res, res, deps, sessionId) {
|
|
3296
|
-
const
|
|
3297
|
-
if (!
|
|
3298
|
-
sendJson(res,
|
|
3767
|
+
const record = await deps.store.get(sessionId);
|
|
3768
|
+
if (!record) {
|
|
3769
|
+
sendJson(res, 404, {
|
|
3770
|
+
ok: false,
|
|
3771
|
+
error: {
|
|
3772
|
+
code: "PI_SESSION_NOT_FOUND",
|
|
3773
|
+
message: `chat session not found: ${sessionId}`,
|
|
3774
|
+
},
|
|
3775
|
+
});
|
|
3299
3776
|
return;
|
|
3300
3777
|
}
|
|
3301
3778
|
// RF-02: a critical snapshot projection failure is 503 PI_RUNTIME_UNAVAILABLE
|
|
3302
3779
|
// — never a 200 fake-empty snapshot.
|
|
3303
3780
|
let snapshot;
|
|
3304
3781
|
try {
|
|
3305
|
-
snapshot = await deps.runtime.getRuntimeSnapshot(sessionId
|
|
3782
|
+
snapshot = await deps.runtime.getRuntimeSnapshot(sessionId, {
|
|
3783
|
+
workspaceProjection: workspaceProjectionFromRecord(record),
|
|
3784
|
+
});
|
|
3306
3785
|
}
|
|
3307
3786
|
catch (error) {
|
|
3308
3787
|
if (sendRuntimeUnavailable(res, error))
|
|
@@ -3407,7 +3886,7 @@ async function handleStartInterview(req, res, deps, sessionId) {
|
|
|
3407
3886
|
});
|
|
3408
3887
|
return;
|
|
3409
3888
|
}
|
|
3410
|
-
if (!(await
|
|
3889
|
+
if (!(await requireWorkflowSession(res, deps, sessionId)))
|
|
3411
3890
|
return;
|
|
3412
3891
|
try {
|
|
3413
3892
|
const body = await readJsonBody(req);
|
|
@@ -3449,7 +3928,7 @@ async function handleAnswerInterview(req, res, deps, sessionId, interviewSession
|
|
|
3449
3928
|
});
|
|
3450
3929
|
return;
|
|
3451
3930
|
}
|
|
3452
|
-
if (!(await
|
|
3931
|
+
if (!(await requireWorkflowSession(res, deps, sessionId)))
|
|
3453
3932
|
return;
|
|
3454
3933
|
try {
|
|
3455
3934
|
const body = await readJsonBody(req);
|
|
@@ -3485,7 +3964,7 @@ async function handleAnswerInterview(req, res, deps, sessionId, interviewSession
|
|
|
3485
3964
|
}
|
|
3486
3965
|
}
|
|
3487
3966
|
async function handleGetInterviewFact(res, deps, sessionId, interviewSessionId, fact) {
|
|
3488
|
-
if (!(await
|
|
3967
|
+
if (!(await requireWorkflowSession(res, deps, sessionId)))
|
|
3489
3968
|
return;
|
|
3490
3969
|
const state = await new ChatInterviewAdapter(deps.appData, deps.events).get(interviewSessionId);
|
|
3491
3970
|
if (!state || state.interview.operatorSessionId !== sessionId) {
|
|
@@ -3583,7 +4062,7 @@ async function handleContractApplyHumanGate(req, res, deps, sessionId, step) {
|
|
|
3583
4062
|
});
|
|
3584
4063
|
return;
|
|
3585
4064
|
}
|
|
3586
|
-
if (!(await
|
|
4065
|
+
if (!(await requireWorkflowSession(res, deps, sessionId)))
|
|
3587
4066
|
return;
|
|
3588
4067
|
const ctx = deps.runtime.actionContext;
|
|
3589
4068
|
if (!ctx) {
|
|
@@ -3765,7 +4244,7 @@ async function handleDagHumanGate(req, res, deps, sessionId, step) {
|
|
|
3765
4244
|
});
|
|
3766
4245
|
return;
|
|
3767
4246
|
}
|
|
3768
|
-
if (!(await
|
|
4247
|
+
if (!(await requireWorkflowSession(res, deps, sessionId)))
|
|
3769
4248
|
return;
|
|
3770
4249
|
const ctx = deps.runtime.actionContext;
|
|
3771
4250
|
if (!ctx) {
|
|
@@ -3785,7 +4264,7 @@ async function handleDagHumanGate(req, res, deps, sessionId, step) {
|
|
|
3785
4264
|
: step === "confirm"
|
|
3786
4265
|
? "confirmDagConfirmation"
|
|
3787
4266
|
: "runDag";
|
|
3788
|
-
const result = await dispatchOperatorAction(ctx, {
|
|
4267
|
+
const result = await dispatchOperatorAction({ ...ctx, chatSessionRef: sessionId }, {
|
|
3789
4268
|
action,
|
|
3790
4269
|
actionParams: body,
|
|
3791
4270
|
clientRequestId: typeof body.clientRequestId === "string"
|
|
@@ -3874,7 +4353,7 @@ async function handleMutationHumanGate(req, res, deps, sessionId) {
|
|
|
3874
4353
|
});
|
|
3875
4354
|
return;
|
|
3876
4355
|
}
|
|
3877
|
-
if (!(await
|
|
4356
|
+
if (!(await requireWorkflowSession(res, deps, sessionId)))
|
|
3878
4357
|
return;
|
|
3879
4358
|
const ctx = deps.runtime.actionContext;
|
|
3880
4359
|
if (!ctx) {
|
|
@@ -3963,7 +4442,7 @@ async function handleConfirmTaskKind(req, res, deps, sessionId, interviewSession
|
|
|
3963
4442
|
});
|
|
3964
4443
|
return;
|
|
3965
4444
|
}
|
|
3966
|
-
if (!(await
|
|
4445
|
+
if (!(await requireWorkflowSession(res, deps, sessionId)))
|
|
3967
4446
|
return;
|
|
3968
4447
|
try {
|
|
3969
4448
|
const body = await readJsonBody(req);
|
|
@@ -4019,13 +4498,7 @@ export async function handleChatCompact(req, res, deps, sessionId) {
|
|
|
4019
4498
|
return;
|
|
4020
4499
|
}
|
|
4021
4500
|
if (!deps.runtime.hasSession(sessionId)) {
|
|
4022
|
-
const reopened = await deps.runtime.reopenSession(
|
|
4023
|
-
sessionId,
|
|
4024
|
-
sessionFile: record.sessionFile,
|
|
4025
|
-
...(record.modelProvider && record.modelId
|
|
4026
|
-
? { model: { provider: record.modelProvider, modelId: record.modelId } }
|
|
4027
|
-
: {}),
|
|
4028
|
-
});
|
|
4501
|
+
const reopened = await deps.runtime.reopenSession(reopenInitFromRecord(record));
|
|
4029
4502
|
if (!reopened.ok) {
|
|
4030
4503
|
const mapped = await mapFailedReopen(deps, sessionId, reopened);
|
|
4031
4504
|
sendJson(res, mapped.status, {
|
|
@@ -4049,7 +4522,9 @@ export async function handleChatCompact(req, res, deps, sessionId) {
|
|
|
4049
4522
|
const rawInstructions = typeof body.instructions === "string" ? body.instructions : undefined;
|
|
4050
4523
|
const currentTodos = projectCurrentTodos(deps.events.snapshot(sessionId));
|
|
4051
4524
|
const todoInstructions = buildTodoCompactionInstructions(rawInstructions, currentTodos);
|
|
4052
|
-
const customInstructions =
|
|
4525
|
+
const customInstructions = isWorkflowSessionMode(record.sessionMode)
|
|
4526
|
+
? buildGoalCompactionInstructions(todoInstructions, deps.goalRegistry?.get(sessionId) ?? null)
|
|
4527
|
+
: todoInstructions;
|
|
4053
4528
|
const turnId = deps.events.latestTurnId(sessionId) ?? `${sessionId}:compact`;
|
|
4054
4529
|
const compactionId = `${sessionId}:compaction:${randomUUID()}`;
|
|
4055
4530
|
deps.events.append(sessionId, turnId, {
|
|
@@ -4064,27 +4539,42 @@ export async function handleChatCompact(req, res, deps, sessionId) {
|
|
|
4064
4539
|
});
|
|
4065
4540
|
try {
|
|
4066
4541
|
const snapshot = await deps.runtime.compact(sessionId, customInstructions);
|
|
4542
|
+
const completedAt = new Date().toISOString();
|
|
4543
|
+
const completedData = {
|
|
4544
|
+
schemaVersion: 1,
|
|
4545
|
+
compactionId,
|
|
4546
|
+
reason: "manual",
|
|
4547
|
+
status: "completed",
|
|
4548
|
+
summary: snapshot.summary,
|
|
4549
|
+
...(snapshot.firstKeptEntryId
|
|
4550
|
+
? { firstKeptEntryId: snapshot.firstKeptEntryId }
|
|
4551
|
+
: {}),
|
|
4552
|
+
...(snapshot.tokensBefore === undefined
|
|
4553
|
+
? {}
|
|
4554
|
+
: { tokensBefore: snapshot.tokensBefore }),
|
|
4555
|
+
...(snapshot.estimatedTokensAfter === undefined
|
|
4556
|
+
? {}
|
|
4557
|
+
: { estimatedTokensAfter: snapshot.estimatedTokensAfter }),
|
|
4558
|
+
willRetry: false,
|
|
4559
|
+
};
|
|
4067
4560
|
const event = deps.events.append(sessionId, turnId, {
|
|
4068
4561
|
kind: "compaction_end",
|
|
4069
|
-
|
|
4070
|
-
|
|
4071
|
-
|
|
4072
|
-
|
|
4073
|
-
|
|
4074
|
-
|
|
4075
|
-
|
|
4076
|
-
|
|
4077
|
-
|
|
4078
|
-
|
|
4079
|
-
|
|
4080
|
-
|
|
4081
|
-
|
|
4082
|
-
|
|
4083
|
-
: { estimatedTokensAfter: snapshot.estimatedTokensAfter }),
|
|
4084
|
-
willRetry: false,
|
|
4562
|
+
at: completedAt,
|
|
4563
|
+
data: completedData,
|
|
4564
|
+
});
|
|
4565
|
+
sendJson(res, 200, {
|
|
4566
|
+
ok: true,
|
|
4567
|
+
snapshot,
|
|
4568
|
+
eventId: event.eventId,
|
|
4569
|
+
compaction: {
|
|
4570
|
+
eventId: event.eventId,
|
|
4571
|
+
kind: "compaction_end",
|
|
4572
|
+
at: completedAt,
|
|
4573
|
+
seq: event.seq,
|
|
4574
|
+
turnId,
|
|
4575
|
+
data: completedData,
|
|
4085
4576
|
},
|
|
4086
4577
|
});
|
|
4087
|
-
sendJson(res, 200, { ok: true, snapshot, eventId: event.eventId });
|
|
4088
4578
|
}
|
|
4089
4579
|
catch (error) {
|
|
4090
4580
|
const classified = classifyChatCompactionFailure(error);
|
|
@@ -4114,7 +4604,14 @@ export async function handleChatMessages(res, deps, sessionId, url) {
|
|
|
4114
4604
|
limit: Number.isFinite(limit) ? limit : 30,
|
|
4115
4605
|
beforeId: url.searchParams.get("beforeId") ?? undefined,
|
|
4116
4606
|
});
|
|
4117
|
-
|
|
4607
|
+
const snapshot = deps.events.snapshot(sessionId);
|
|
4608
|
+
const messages = projectStoredMessagesWithEventOrder(page.messages, snapshot);
|
|
4609
|
+
sendJson(res, 200, {
|
|
4610
|
+
ok: true,
|
|
4611
|
+
...page,
|
|
4612
|
+
messages,
|
|
4613
|
+
turnStates: turnStatesForIds(deps.events, sessionId, messages.map((message) => message.turnId)),
|
|
4614
|
+
});
|
|
4118
4615
|
}
|
|
4119
4616
|
catch (error) {
|
|
4120
4617
|
sendJson(res, 404, {
|
|
@@ -4126,6 +4623,23 @@ export async function handleChatMessages(res, deps, sessionId, url) {
|
|
|
4126
4623
|
});
|
|
4127
4624
|
}
|
|
4128
4625
|
}
|
|
4626
|
+
export async function handleChatAttachment(res, deps, sessionId, attachmentId) {
|
|
4627
|
+
const attachment = await deps.store.readAttachment(sessionId, attachmentId);
|
|
4628
|
+
if (!attachment) {
|
|
4629
|
+
sendJson(res, 404, {
|
|
4630
|
+
ok: false,
|
|
4631
|
+
error: { code: "NOT_FOUND", message: "chat attachment not found" },
|
|
4632
|
+
});
|
|
4633
|
+
return;
|
|
4634
|
+
}
|
|
4635
|
+
res.writeHead(200, {
|
|
4636
|
+
"content-type": attachment.metadata.mimeType,
|
|
4637
|
+
"content-length": String(attachment.bytes.length),
|
|
4638
|
+
"cache-control": "private, max-age=31536000, immutable",
|
|
4639
|
+
"x-content-type-options": "nosniff",
|
|
4640
|
+
});
|
|
4641
|
+
res.end(attachment.bytes);
|
|
4642
|
+
}
|
|
4129
4643
|
/**
|
|
4130
4644
|
* Dispatch a chat HTTP request. Returns true if handled.
|
|
4131
4645
|
* Mounted under /api/operator/v1/chat/*.
|
|
@@ -4135,8 +4649,87 @@ export async function handleChatMessages(res, deps, sessionId, url) {
|
|
|
4135
4649
|
export async function handleChatRequest(req, res, deps, pathname, workspaceScope) {
|
|
4136
4650
|
if (!deps)
|
|
4137
4651
|
return false;
|
|
4652
|
+
try {
|
|
4653
|
+
return await dispatchChatRequest(req, res, deps, pathname, workspaceScope);
|
|
4654
|
+
}
|
|
4655
|
+
catch (error) {
|
|
4656
|
+
// Store-level fail-closed for corrupt sessionMode: bounded 409 instead of
|
|
4657
|
+
// a generic 500, for any handler that reads the record without its own
|
|
4658
|
+
// UnknownChatSessionModeError mapping (readChatSessionMode covers the rest).
|
|
4659
|
+
if (error instanceof UnknownChatSessionModeError) {
|
|
4660
|
+
sendJson(res, 409, {
|
|
4661
|
+
ok: false,
|
|
4662
|
+
error: { code: error.code, message: error.message },
|
|
4663
|
+
});
|
|
4664
|
+
return true;
|
|
4665
|
+
}
|
|
4666
|
+
throw error;
|
|
4667
|
+
}
|
|
4668
|
+
}
|
|
4669
|
+
function databaseConnectionInput(body, engine) {
|
|
4670
|
+
return {
|
|
4671
|
+
name: typeof body.name === "string" ? body.name : "",
|
|
4672
|
+
engine,
|
|
4673
|
+
...(typeof body.path === "string" ? { path: body.path } : {}),
|
|
4674
|
+
...(body.mode === "parameters" || body.mode === "url-env" ? { mode: body.mode } : {}),
|
|
4675
|
+
...(typeof body.urlEnv === "string" ? { urlEnv: body.urlEnv } : {}),
|
|
4676
|
+
...(typeof body.host === "string" ? { host: body.host } : {}),
|
|
4677
|
+
...(typeof body.port === "number" ? { port: body.port } : {}),
|
|
4678
|
+
...(typeof body.database === "string" ? { database: body.database } : {}),
|
|
4679
|
+
...(typeof body.username === "string" ? { username: body.username } : {}),
|
|
4680
|
+
...(body.passwordMode === "runtime" || body.passwordMode === "environment" || body.passwordMode === "encrypted" ? { passwordMode: body.passwordMode } : {}),
|
|
4681
|
+
...(typeof body.passwordRef === "string" ? { passwordRef: body.passwordRef } : {}),
|
|
4682
|
+
...(typeof body.passwordEnv === "string" ? { passwordEnv: body.passwordEnv } : {}),
|
|
4683
|
+
...(typeof body.password === "string" ? { password: body.password } : {}),
|
|
4684
|
+
...(typeof body.ssl === "boolean" ? { ssl: body.ssl } : {}),
|
|
4685
|
+
};
|
|
4686
|
+
}
|
|
4687
|
+
async function dispatchChatRequest(req, res, deps, pathname, workspaceScope) {
|
|
4688
|
+
// Terminal management API (model + Console UI shared PTY registry).
|
|
4689
|
+
if (await handleTerminalRequest(req, res, {
|
|
4690
|
+
terminals: deps.terminals ?? new TerminalSessionRegistry(),
|
|
4691
|
+
gateMutation: (r) => gateMutation(r, deps),
|
|
4692
|
+
// Terminals open in the workspace repo root, not the Console process cwd.
|
|
4693
|
+
defaultCwd: deps.appData.repoRoot,
|
|
4694
|
+
}, pathname)) {
|
|
4695
|
+
return true;
|
|
4696
|
+
}
|
|
4697
|
+
// Source control API (model + Console UI shared workspace ScmService).
|
|
4698
|
+
if (deps.scm) {
|
|
4699
|
+
if (await handleScmRequest(req, res, {
|
|
4700
|
+
scm: deps.scm,
|
|
4701
|
+
gateMutation: (r) => gateMutation(r, deps),
|
|
4702
|
+
}, pathname)) {
|
|
4703
|
+
return true;
|
|
4704
|
+
}
|
|
4705
|
+
}
|
|
4706
|
+
// Shared real-browser API for the Browser panel and chat browser tools.
|
|
4707
|
+
if (await handleBrowserRequest(req, res, {
|
|
4708
|
+
automation: deps.browserAutomation,
|
|
4709
|
+
requireWorkspace: () => workspaceScope
|
|
4710
|
+
? { ok: true, workspaceId: workspaceScope }
|
|
4711
|
+
: { ok: false },
|
|
4712
|
+
}, pathname)) {
|
|
4713
|
+
return true;
|
|
4714
|
+
}
|
|
4715
|
+
// Operator pet (dsh-pet port): state/interact API + atlas media.
|
|
4716
|
+
if (pathname.startsWith("/api/operator/v1/pet/") &&
|
|
4717
|
+
(req.method ?? "GET").toUpperCase() !== "GET") {
|
|
4718
|
+
const gate = gateMutation(req, deps);
|
|
4719
|
+
if (!gate.ok) {
|
|
4720
|
+
sendJson(res, gate.status, {
|
|
4721
|
+
ok: false,
|
|
4722
|
+
error: { code: gate.code, message: gate.message },
|
|
4723
|
+
});
|
|
4724
|
+
return true;
|
|
4725
|
+
}
|
|
4726
|
+
}
|
|
4727
|
+
if (await handlePetRequest(req, res, pathname, deps.pet)) {
|
|
4728
|
+
return true;
|
|
4729
|
+
}
|
|
4138
4730
|
if (!pathname.startsWith("/api/operator/v1/chat/") &&
|
|
4139
|
-
!pathname.startsWith("/api/operator/v1/pi/")
|
|
4731
|
+
!pathname.startsWith("/api/operator/v1/pi/") &&
|
|
4732
|
+
!pathname.startsWith("/api/operator/v1/pet/"))
|
|
4140
4733
|
return false;
|
|
4141
4734
|
const method = (req.method ?? "GET").toUpperCase();
|
|
4142
4735
|
const url = new URL(req.url ?? "/", "http://localhost");
|
|
@@ -4186,6 +4779,117 @@ export async function handleChatRequest(req, res, deps, pathname, workspaceScope
|
|
|
4186
4779
|
await handleChatCapabilities(req, res, deps);
|
|
4187
4780
|
return true;
|
|
4188
4781
|
}
|
|
4782
|
+
if (method === "GET" && pathname === "/api/operator/v1/chat/database-connections/export") {
|
|
4783
|
+
sendJson(res, 200, { ok: true, document: deps.runtime.exportDatabaseConnections() });
|
|
4784
|
+
return true;
|
|
4785
|
+
}
|
|
4786
|
+
if (method === "POST" && pathname === "/api/operator/v1/chat/database-connections/import") {
|
|
4787
|
+
const gate = gateMutation(req, deps);
|
|
4788
|
+
if (!gate.ok) {
|
|
4789
|
+
sendJson(res, gate.status, { ok: false, error: { code: gate.code, message: gate.message } });
|
|
4790
|
+
return true;
|
|
4791
|
+
}
|
|
4792
|
+
try {
|
|
4793
|
+
const body = await readJsonBody(req);
|
|
4794
|
+
const conflict = body.conflict === "overwrite" ? "overwrite" : "skip";
|
|
4795
|
+
const result = await deps.runtime.importDatabaseConnections(body.document, conflict);
|
|
4796
|
+
sendJson(res, 200, { ok: true, result });
|
|
4797
|
+
}
|
|
4798
|
+
catch (error) {
|
|
4799
|
+
const code = error instanceof DbConnectionError ? error.code : "DB_PERSISTENCE_ERROR";
|
|
4800
|
+
const status = code === "DB_PERSISTENCE_ERROR" ? 500 : 400;
|
|
4801
|
+
sendJson(res, status, { ok: false, error: { code, message: error instanceof Error ? error.message : String(error) } });
|
|
4802
|
+
}
|
|
4803
|
+
return true;
|
|
4804
|
+
}
|
|
4805
|
+
if (pathname === "/api/operator/v1/chat/database-connections" &&
|
|
4806
|
+
(method === "GET" || method === "POST")) {
|
|
4807
|
+
if (method === "GET") {
|
|
4808
|
+
sendJson(res, 200, { ok: true, connections: deps.runtime.listDatabaseConnections() });
|
|
4809
|
+
return true;
|
|
4810
|
+
}
|
|
4811
|
+
const gate = gateMutation(req, deps);
|
|
4812
|
+
if (!gate.ok) {
|
|
4813
|
+
sendJson(res, gate.status, { ok: false, error: { code: gate.code, message: gate.message } });
|
|
4814
|
+
return true;
|
|
4815
|
+
}
|
|
4816
|
+
try {
|
|
4817
|
+
const body = await readJsonBody(req);
|
|
4818
|
+
const engine = body.engine;
|
|
4819
|
+
if (engine !== "sqlite" && engine !== "postgres" && engine !== "mysql") {
|
|
4820
|
+
throw new DbConnectionError("DB_INVALID_CONFIG", "engine must be sqlite, postgres, or mysql");
|
|
4821
|
+
}
|
|
4822
|
+
const connection = await deps.runtime.addDatabaseConnection(databaseConnectionInput(body, engine));
|
|
4823
|
+
sendJson(res, 201, { ok: true, connection });
|
|
4824
|
+
}
|
|
4825
|
+
catch (error) {
|
|
4826
|
+
const code = error instanceof DbConnectionError ? error.code : "DB_PERSISTENCE_ERROR";
|
|
4827
|
+
const status = code === "DB_PERSISTENCE_ERROR" ? 500 : 400;
|
|
4828
|
+
sendJson(res, status, { ok: false, error: { code, message: error instanceof Error ? error.message : String(error) } });
|
|
4829
|
+
}
|
|
4830
|
+
return true;
|
|
4831
|
+
}
|
|
4832
|
+
const databaseBrowseRoute = pathname.match(/^\/api\/operator\/v1\/chat\/database-connections\/([^/]+)\/browse$/);
|
|
4833
|
+
if (databaseBrowseRoute && method === "GET") {
|
|
4834
|
+
const name = decodeURIComponent(databaseBrowseRoute[1]);
|
|
4835
|
+
try {
|
|
4836
|
+
const table = url.searchParams.get("table")?.trim();
|
|
4837
|
+
const database = url.searchParams.get("database")?.trim() || undefined;
|
|
4838
|
+
if (!table) {
|
|
4839
|
+
const databases = await deps.runtime.listDatabaseDatabases(name);
|
|
4840
|
+
const connection = deps.runtime.listDatabaseConnections().find((item) => item.name === name);
|
|
4841
|
+
const shouldLoadTables = connection?.engine === "sqlite" || Boolean(database || connection?.database);
|
|
4842
|
+
sendJson(res, 200, { ok: true, connection: name, database, databases, tables: shouldLoadTables ? await deps.runtime.listDatabaseTables(name, database) : [] });
|
|
4843
|
+
}
|
|
4844
|
+
else {
|
|
4845
|
+
const page = Number(url.searchParams.get("page") ?? "1");
|
|
4846
|
+
const pageSize = Number(url.searchParams.get("pageSize") ?? "50");
|
|
4847
|
+
sendJson(res, 200, { ok: true, result: await deps.runtime.browseDatabaseTable(name, table, page, pageSize, database) });
|
|
4848
|
+
}
|
|
4849
|
+
}
|
|
4850
|
+
catch (error) {
|
|
4851
|
+
const code = error instanceof DbConnectionError ? error.code : "DB_ENGINE_ERROR";
|
|
4852
|
+
const status = code === "DB_CONNECTION_NOT_FOUND" ? 404 : 400;
|
|
4853
|
+
sendJson(res, status, { ok: false, error: { code, message: error instanceof Error ? error.message : String(error) } });
|
|
4854
|
+
}
|
|
4855
|
+
return true;
|
|
4856
|
+
}
|
|
4857
|
+
const databaseConnectionRoute = pathname.match(/^\/api\/operator\/v1\/chat\/database-connections\/([^/]+)(?:\/(test))?$/);
|
|
4858
|
+
if (databaseConnectionRoute && (method === "DELETE" || method === "POST" || method === "PATCH")) {
|
|
4859
|
+
const gate = gateMutation(req, deps);
|
|
4860
|
+
if (!gate.ok) {
|
|
4861
|
+
sendJson(res, gate.status, { ok: false, error: { code: gate.code, message: gate.message } });
|
|
4862
|
+
return true;
|
|
4863
|
+
}
|
|
4864
|
+
const name = decodeURIComponent(databaseConnectionRoute[1]);
|
|
4865
|
+
try {
|
|
4866
|
+
if (method === "POST" && databaseConnectionRoute[2] === "test") {
|
|
4867
|
+
sendJson(res, 200, { ok: true, result: await deps.runtime.testDatabaseConnection(name) });
|
|
4868
|
+
}
|
|
4869
|
+
else if (method === "PATCH" && !databaseConnectionRoute[2]) {
|
|
4870
|
+
const body = await readJsonBody(req);
|
|
4871
|
+
const engine = body.engine;
|
|
4872
|
+
if (engine !== "sqlite" && engine !== "postgres" && engine !== "mysql") {
|
|
4873
|
+
throw new DbConnectionError("DB_INVALID_CONFIG", "engine must be sqlite, postgres, or mysql");
|
|
4874
|
+
}
|
|
4875
|
+
const connection = await deps.runtime.updateDatabaseConnection(name, databaseConnectionInput(body, engine));
|
|
4876
|
+
sendJson(res, 200, { ok: true, connection });
|
|
4877
|
+
}
|
|
4878
|
+
else if (method === "DELETE" && !databaseConnectionRoute[2]) {
|
|
4879
|
+
await deps.runtime.removeDatabaseConnection(name);
|
|
4880
|
+
sendJson(res, 200, { ok: true });
|
|
4881
|
+
}
|
|
4882
|
+
else {
|
|
4883
|
+
sendJson(res, 405, { ok: false, error: { code: "METHOD_NOT_ALLOWED", message: "method not allowed" } });
|
|
4884
|
+
}
|
|
4885
|
+
}
|
|
4886
|
+
catch (error) {
|
|
4887
|
+
const code = error instanceof DbConnectionError ? error.code : "DB_ENGINE_ERROR";
|
|
4888
|
+
const status = code === "DB_CONNECTION_NOT_FOUND" ? 404 : code === "DB_PERSISTENCE_ERROR" ? 500 : 400;
|
|
4889
|
+
sendJson(res, status, { ok: false, error: { code, message: error instanceof Error ? error.message : String(error) } });
|
|
4890
|
+
}
|
|
4891
|
+
return true;
|
|
4892
|
+
}
|
|
4189
4893
|
const htmlPreviewRoute = matchRepoHtmlPreviewRoute(pathname);
|
|
4190
4894
|
if (method === "GET" && htmlPreviewRoute) {
|
|
4191
4895
|
if (!htmlPreviewRoute.rawPath)
|
|
@@ -4271,7 +4975,7 @@ export async function handleChatRequest(req, res, deps, pathname, workspaceScope
|
|
|
4271
4975
|
await handleSddBingo(req, res, deps, decodeURIComponent(sddBingo[1]));
|
|
4272
4976
|
return true;
|
|
4273
4977
|
}
|
|
4274
|
-
const m4Route = pathname.match(/^\/api\/operator\/v1\/chat\/sessions\/([^/]+)\/(draft|files|runtime-context|runtime-snapshot|models|model|reload|commands)$/);
|
|
4978
|
+
const m4Route = pathname.match(/^\/api\/operator\/v1\/chat\/sessions\/([^/]+)\/(draft|files|runtime-context|runtime-snapshot|context-insights|models|model|reload|commands)$/);
|
|
4275
4979
|
if (m4Route) {
|
|
4276
4980
|
const id = decodeURIComponent(m4Route[1]);
|
|
4277
4981
|
const route = m4Route[2];
|
|
@@ -4291,6 +4995,10 @@ export async function handleChatRequest(req, res, deps, pathname, workspaceScope
|
|
|
4291
4995
|
await handleRuntimeSnapshot(req, res, deps, id);
|
|
4292
4996
|
return true;
|
|
4293
4997
|
}
|
|
4998
|
+
if (route === "context-insights" && method === "GET") {
|
|
4999
|
+
await handleContextInsights(res, deps, id);
|
|
5000
|
+
return true;
|
|
5001
|
+
}
|
|
4294
5002
|
if (route === "models" && method === "GET") {
|
|
4295
5003
|
await handleModels(req, res, deps, id);
|
|
4296
5004
|
return true;
|
|
@@ -4385,6 +5093,11 @@ export async function handleChatRequest(req, res, deps, pathname, workspaceScope
|
|
|
4385
5093
|
await handleChatMessages(res, deps, decodeURIComponent(sessionMessages[1]), url);
|
|
4386
5094
|
return true;
|
|
4387
5095
|
}
|
|
5096
|
+
const sessionAttachment = pathname.match(/^\/api\/operator\/v1\/chat\/sessions\/([^/]+)\/attachments\/([^/]+)$/);
|
|
5097
|
+
if (method === "GET" && sessionAttachment) {
|
|
5098
|
+
await handleChatAttachment(res, deps, decodeURIComponent(sessionAttachment[1]), decodeURIComponent(sessionAttachment[2]));
|
|
5099
|
+
return true;
|
|
5100
|
+
}
|
|
4388
5101
|
const sessionPrompt = pathname.match(/^\/api\/operator\/v1\/chat\/sessions\/([^/]+)\/prompt$/);
|
|
4389
5102
|
if (method === "POST" && sessionPrompt) {
|
|
4390
5103
|
await handleChatPrompt(req, res, deps, decodeURIComponent(sessionPrompt[1]));
|