@qlairoslabs/acp-client 0.12.0 → 0.13.0-alpha.1
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/CHANGELOG-consumer-fixes.md +17 -3
- package/CHANGELOG-controller-interface.md +258 -0
- package/CHANGELOG-metering.md +29 -0
- package/CHANGELOG-operation-lifecycle.md +15 -1
- package/CONSUMER_GUIDE.md +847 -7
- package/MIGRATION-0.10-to-0.12.md +4 -1
- package/MIGRATION-0.12-to-0.13.md +275 -0
- package/README.md +17 -16
- package/package.json +4 -2
- package/src/acp-client-handlers.ts +48 -0
- package/src/activity.ts +3 -0
- package/src/adapters.ts +30 -0
- package/src/agent/adoption/provider-attachment.ts +33 -0
- package/src/agent/adoption/provider-session-delete.ts +1 -1
- package/src/agent/adoption/provider-sessions.ts +3 -2
- package/src/agent/append-assistant-message.ts +91 -0
- package/src/agent/config.ts +54 -6
- package/src/agent/conversation-manager.ts +94 -158
- package/src/agent/manager-settings.ts +1 -0
- package/src/agent/native-channel-lifecycle.ts +111 -0
- package/src/agent/persisted-lifecycle.ts +44 -4
- package/src/agent/persistence-policy/context.ts +13 -0
- package/src/agent/persistence-policy/resolver.ts +7 -0
- package/src/agent/persistence-policy/types.ts +1 -0
- package/src/agent/record-capture.ts +1 -0
- package/src/agent-adapter-registry.ts +33 -7
- package/src/agent-conversation-contracts.ts +14 -1
- package/src/agent-conversation-errors.ts +8 -0
- package/src/agent-conversation-owner.ts +9 -4
- package/src/agent-session-config.ts +135 -0
- package/src/agent-session.ts +439 -934
- package/src/agent.ts +73 -0
- package/src/capabilities.ts +2 -0
- package/src/checkpointing/recovery.ts +10 -0
- package/src/cli/index.ts +3 -3
- package/src/cli/managed.ts +2 -2
- package/src/client.ts +68 -98
- package/src/connection-attempt.ts +95 -0
- package/src/controller-connection.ts +114 -0
- package/src/controller.ts +156 -148
- package/src/conversation-controller/capabilities.ts +88 -0
- package/src/conversation-controller/contracts.ts +208 -0
- package/src/conversation-controller/factory.ts +39 -0
- package/src/conversation-controller/guards.ts +98 -0
- package/src/conversation-controller/index.ts +20 -0
- package/src/conversation-startup.ts +35 -0
- package/src/conversation-turns/backend-turns.ts +373 -0
- package/src/conversation-turns/clear-operations.ts +37 -0
- package/src/conversation-turns/context.ts +135 -0
- package/src/conversation-turns/index.ts +4 -0
- package/src/conversation-turns/record-helpers.ts +75 -0
- package/src/conversation-turns/recording.ts +235 -0
- package/src/conversation-turns/run-turn.ts +367 -0
- package/src/conversation-turns/turn-run.ts +57 -0
- package/src/errors.ts +41 -0
- package/src/handoff-options.ts +81 -0
- package/src/identity/checkpoint-backed.ts +1 -1
- package/src/index.ts +58 -3
- package/src/internal-inference/coordination/manager.ts +11 -0
- package/src/internal-inference/runtime/harness-runtime.ts +4 -4
- package/src/launcher.ts +35 -2
- package/src/media/parts.ts +5 -0
- package/src/message-status.ts +83 -0
- package/src/metering/engine.ts +39 -64
- package/src/metering/measurement-provenance.ts +36 -0
- package/src/metering/native/durability.ts +115 -0
- package/src/metering/native/ledger.ts +104 -0
- package/src/metering/native/operation.ts +85 -0
- package/src/metering/native/projection.ts +114 -0
- package/src/metering/native/proof.ts +51 -0
- package/src/metering/native/requirements.ts +10 -0
- package/src/metering/native/retirement.ts +92 -0
- package/src/metering/native/source.ts +90 -0
- package/src/metering/record.ts +38 -1
- package/src/metering/state.ts +13 -3
- package/src/native/approvals.ts +169 -0
- package/src/native/capabilities.ts +17 -0
- package/src/native/channel-changes.ts +211 -0
- package/src/native/connection.ts +56 -0
- package/src/native/controller.ts +591 -0
- package/src/native/delegation-sender.ts +26 -0
- package/src/native/errors.ts +35 -0
- package/src/native/functions.ts +156 -0
- package/src/native/host-config.ts +59 -0
- package/src/native/host.ts +909 -0
- package/src/native/item-receipts.ts +15 -0
- package/src/native/metering.ts +12 -0
- package/src/native/records.ts +166 -0
- package/src/native/recovery.ts +186 -0
- package/src/native/reminders/fixtures/schema21.sqlite.gz +0 -0
- package/src/native/reminders/idle.ts +70 -0
- package/src/native/reminders/limits.ts +19 -0
- package/src/native/reminders/notifications.ts +29 -0
- package/src/native/reminders/reminders.test.ts +553 -0
- package/src/native/reminders/routine-config.typecheck.ts +13 -0
- package/src/native/reminders/routine-guard.ts +66 -0
- package/src/native/reminders/routine-schedule.ts +25 -0
- package/src/native/reminders/routine-wire.ts +21 -0
- package/src/native/reminders/schedule.ts +97 -0
- package/src/native/reminders/service.ts +280 -0
- package/src/native/reminders/supervisor.ts +91 -0
- package/src/native/reminders/sync.ts +301 -0
- package/src/native/reminders/wire.ts +197 -0
- package/src/native/reminders/wire.typecheck.ts +13 -0
- package/src/native/submission-withdrawal.ts +65 -0
- package/src/native/usage.ts +15 -0
- package/src/native/worker-delivery.ts +61 -0
- package/src/native-harness/.res/README.md +1 -0
- package/src/native-harness/.res/architecture/README.md +21 -0
- package/src/native-harness/.res/architecture/context-and-data.md +31 -0
- package/src/native-harness/.res/architecture/execution.md +42 -0
- package/src/native-harness/.res/architecture/overview.md +44 -0
- package/src/native-harness/.res/architecture/persistence.md +56 -0
- package/src/native-harness/.res/architecture/suspend-and-resume.md +45 -0
- package/src/native-harness/.res/architecture/technical-architecture.md +50 -0
- package/src/native-harness/.res/decisions/0001-basic-loop.md +11 -0
- package/src/native-harness/.res/decisions/0002-channel-input.md +17 -0
- package/src/native-harness/.res/decisions/0003-inference-inspection.md +14 -0
- package/src/native-harness/.res/decisions/0004-shared-context.md +21 -0
- package/src/native-harness/.res/decisions/0005-notice-inbox.md +20 -0
- package/src/native-harness/.res/decisions/0006-explicit-notices.md +12 -0
- package/src/native-harness/.res/decisions/0007-channel-output.md +12 -0
- package/src/native-harness/.res/decisions/0008-browser-client.md +16 -0
- package/src/native-harness/.res/decisions/0009-scripted-model.md +18 -0
- package/src/native-harness/.res/decisions/0010-inprocess-protocol.md +19 -0
- package/src/native-harness/.res/decisions/0011-stdio-transport.md +21 -0
- package/src/native-harness/.res/decisions/0012-session-storage-contract.md +37 -0
- package/src/native-harness/.res/decisions/0013-durable-delivery-contract.md +64 -0
- package/src/native-harness/.res/decisions/0014-channel-controls-contract.md +73 -0
- package/src/native-harness/.res/decisions/0015-session-file-contract.md +72 -0
- package/src/native-harness/.res/decisions/0016-configuration-permissions.md +159 -0
- package/src/native-harness/.res/decisions/0017-openrouter-managed-config.md +69 -0
- package/src/native-harness/.res/decisions/0018-tool-dispatch-evidence.md +78 -0
- package/src/native-harness/.res/decisions/0019-model-execution.md +188 -0
- package/src/native-harness/.res/decisions/0020-startup-channel-changes.md +71 -0
- package/src/native-harness/.res/decisions/0021-tools-and-host-callbacks.md +100 -0
- package/src/native-harness/.res/decisions/0022-operator-controls.md +115 -0
- package/src/native-harness/.res/decisions/0023-long-running-mock.md +82 -0
- package/src/native-harness/.res/decisions/0024-callback-policy-names.md +46 -0
- package/src/native-harness/.res/decisions/0025-external-notices.md +364 -0
- package/src/native-harness/.res/decisions/0026-bounded-context.md +81 -0
- package/src/native-harness/.res/decisions/0027-development-instructions.md +22 -0
- package/src/native-harness/.res/decisions/0028-debug-tools.md +16 -0
- package/src/native-harness/.res/decisions/0029-bounded-model-call-pages.md +37 -0
- package/src/native-harness/.res/decisions/0030-timeline-retrieval.md +145 -0
- package/src/native-harness/.res/decisions/0031-one-off-reminders.md +173 -0
- package/src/native-harness/.res/docs/11-15.md +148 -0
- package/src/native-harness/.res/docs/BUSINESS_RULES.md +111 -0
- package/src/native-harness/.res/docs/WORK.md +65 -0
- package/src/native-harness/.res/docs/_/11-16-persistence/LOG.md +312 -0
- package/src/native-harness/.res/docs/_/11-16-persistence/PLAN.md +77 -0
- package/src/native-harness/.res/docs/_/11-16-persistence/SPEC.md +313 -0
- package/src/native-harness/.res/docs/_/11-19-channel-controls/LOG.md +160 -0
- package/src/native-harness/.res/docs/_/11-19-channel-controls/PLAN.md +89 -0
- package/src/native-harness/.res/docs/_/11-19-channel-controls/SPEC.md +135 -0
- package/src/native-harness/.res/docs/_/11-19-config-permissions/LOG.md +143 -0
- package/src/native-harness/.res/docs/_/11-19-config-permissions/PLAN.md +93 -0
- package/src/native-harness/.res/docs/_/11-19-config-permissions/SPEC.md +181 -0
- package/src/native-harness/.res/docs/acp-client-integration/0001-DESIGN/11-20-LOG.md +433 -0
- package/src/native-harness/.res/docs/acp-client-integration/0001-DESIGN/PLAN.md +60 -0
- package/src/native-harness/.res/docs/acp-client-integration/0001-DESIGN/PROTOCOL.md +141 -0
- package/src/native-harness/.res/docs/acp-client-integration/0001-DESIGN/SPEC.md +168 -0
- package/src/native-harness/.res/docs/attachments/0001-DESIGN/SPEC.md +111 -0
- package/src/native-harness/.res/docs/bounded-context/0001-DESIGN/PLAN.md +129 -0
- package/src/native-harness/.res/docs/bounded-context/0001-DESIGN/SPEC.md +135 -0
- package/src/native-harness/.res/docs/channel-messaging/0001-DESIGN-external-notices/LOG.md +437 -0
- package/src/native-harness/.res/docs/channel-messaging/0001-DESIGN-external-notices/PLAN.md +127 -0
- package/src/native-harness/.res/docs/channel-messaging/0001-DESIGN-external-notices/SPEC.md +203 -0
- package/src/native-harness/.res/docs/long-running-mock/0001-DESIGN/LOG.md +292 -0
- package/src/native-harness/.res/docs/long-running-mock/0001-DESIGN/PLAN.md +91 -0
- package/src/native-harness/.res/docs/long-running-mock/0001-DESIGN/SPEC.md +187 -0
- package/src/native-harness/.res/docs/model-execution/0001-DESIGN/LOG.md +148 -0
- package/src/native-harness/.res/docs/model-execution/0001-DESIGN/PLAN.md +95 -0
- package/src/native-harness/.res/docs/model-execution/0001-DESIGN/SPEC.md +200 -0
- package/src/native-harness/.res/docs/model-execution/0002-AMENDMENT-accounting/SPEC.md +80 -0
- package/src/native-harness/.res/docs/operator-controls/0001-DESIGN/LOG.md +206 -0
- package/src/native-harness/.res/docs/operator-controls/0001-DESIGN/PLAN.md +106 -0
- package/src/native-harness/.res/docs/operator-controls/0001-DESIGN/SPEC.md +297 -0
- package/src/native-harness/.res/docs/persistence/0002-AMENDMENT-session-file/LOG.md +41 -0
- package/src/native-harness/.res/docs/persistence/0002-AMENDMENT-session-file/PLAN.md +36 -0
- package/src/native-harness/.res/docs/persistence/0002-AMENDMENT-session-file/SPEC.md +84 -0
- package/src/native-harness/.res/docs/shell-execution/0001-DESIGN/SPEC.md +147 -0
- package/src/native-harness/.res/docs/timeline-retrieval/0001-DESIGN/LOG.md +15 -0
- package/src/native-harness/.res/docs/timeline-retrieval/0001-DESIGN/PLAN.md +107 -0
- package/src/native-harness/.res/docs/timeline-retrieval/0001-DESIGN/SPEC.md +114 -0
- package/src/native-harness/.res/docs/todos/11-19-TODO-auto_review-approve.md +24 -0
- package/src/native-harness/.res/docs/todos/11-20-TODO-user-entered-tool-code.md +23 -0
- package/src/native-harness/.res/docs/todos/11-21-TODO-node-resource-coordination.md +103 -0
- package/src/native-harness/.res/docs/todos/11-22-TODO-harness-m1-sequence.md +50 -0
- package/src/native-harness/.res/docs/tools-and-callbacks/0001-DESIGN/LOG.md +337 -0
- package/src/native-harness/.res/docs/tools-and-callbacks/0001-DESIGN/PLAN.md +76 -0
- package/src/native-harness/.res/docs/tools-and-callbacks/0001-DESIGN/SPEC.md +204 -0
- package/src/native-harness/.res/docs/tools-and-callbacks/0002-AMENDMENT-callback-policy/LOG.md +73 -0
- package/src/native-harness/.res/docs/tools-and-callbacks/0002-AMENDMENT-callback-policy/SPEC.md +64 -0
- package/src/native-harness/.res/docs/tools-and-callbacks/0003-DESIGN-attachment-tools/SPEC.md +58 -0
- package/src/native-harness/.res/docs/tools-and-callbacks/0004-DESIGN-reminder-tools/LOG.md +45 -0
- package/src/native-harness/.res/docs/tools-and-callbacks/0004-DESIGN-reminder-tools/PLAN.md +108 -0
- package/src/native-harness/.res/docs/tools-and-callbacks/0004-DESIGN-reminder-tools/SPEC.md +169 -0
- package/src/native-harness/.res/docs/web-tools/0001-DESIGN/SPEC.md +128 -0
- package/src/native-harness/.res/marketing-draft.md +22 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/README.md +285 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-1.md +19 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-10.md +138 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-11.md +148 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-12.md +172 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-13.md +194 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-14.md +233 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-15.md +249 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-16.md +265 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-17.md +279 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-18.md +293 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-19.md +307 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-2.md +29 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-20.md +317 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-21.md +327 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-3.md +40 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-4.md +59 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-5.md +69 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-6.md +79 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-7.md +89 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-8.md +102 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-9.md +116 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation.jsonl +49 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/doc.md +87 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/harness.py +289 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/local/harness copy 2.py +221 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/local/harness copy 3.py +287 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/local/harness copy.py +165 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/state.json +22 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/system-prompt.md +2 -0
- package/src/native-harness/AGENTS.md +70 -0
- package/src/native-harness/ARCHITECTURE.md +249 -0
- package/src/native-harness/README.md +726 -0
- package/src/native-harness/bun.lock +26 -0
- package/src/native-harness/package.json +24 -0
- package/src/native-harness/src/capabilities/context/build.ts +59 -0
- package/src/native-harness/src/capabilities/context/instructions/agent-demo.md +11 -0
- package/src/native-harness/src/capabilities/context/instructions/agent.md +5 -0
- package/src/native-harness/src/capabilities/context/instructions/harness.md +7 -0
- package/src/native-harness/src/capabilities/context/load-instructions.ts +22 -0
- package/src/native-harness/src/capabilities/context/session.ts +92 -0
- package/src/native-harness/src/capabilities/model/attempt.ts +27 -0
- package/src/native-harness/src/capabilities/model/openrouter-stream.ts +72 -0
- package/src/native-harness/src/capabilities/model/openrouter.ts +62 -0
- package/src/native-harness/src/capabilities/model/runner.ts +116 -0
- package/src/native-harness/src/capabilities/model/scenario-attempt.ts +50 -0
- package/src/native-harness/src/capabilities/model/scenario-file.ts +125 -0
- package/src/native-harness/src/capabilities/model/scripted-error.ts +6 -0
- package/src/native-harness/src/capabilities/model/scripted.ts +81 -0
- package/src/native-harness/src/capabilities/model/usage.ts +15 -0
- package/src/native-harness/src/capabilities/tools/builtin/current-time.ts +28 -0
- package/src/native-harness/src/capabilities/tools/builtin/notify-channel.ts +17 -0
- package/src/native-harness/src/capabilities/tools/debug/index.ts +13 -0
- package/src/native-harness/src/capabilities/tools/debug/result.ts +12 -0
- package/src/native-harness/src/capabilities/tools/debug/test-echo.ts +16 -0
- package/src/native-harness/src/capabilities/tools/debug/test-fail.ts +14 -0
- package/src/native-harness/src/capabilities/tools/debug/test-wait.ts +19 -0
- package/src/native-harness/src/capabilities/tools/reminders/index.ts +75 -0
- package/src/native-harness/src/capabilities/tools/runtime/catalog.ts +103 -0
- package/src/native-harness/src/capabilities/tools/runtime/functions.ts +130 -0
- package/src/native-harness/src/capabilities/tools/runtime/invocations.ts +48 -0
- package/src/native-harness/src/capabilities/tools/testing/echo.ts +18 -0
- package/src/native-harness/src/capabilities/tools/testing/record.ts +47 -0
- package/src/native-harness/src/capabilities/tools/timeline/index.ts +41 -0
- package/src/native-harness/src/client.ts +18 -0
- package/src/native-harness/src/contracts/agent.ts +105 -0
- package/src/native-harness/src/contracts/config-permissions.ts +47 -0
- package/src/native-harness/src/contracts/context.ts +112 -0
- package/src/native-harness/src/contracts/events.ts +47 -0
- package/src/native-harness/src/contracts/input-limits.ts +7 -0
- package/src/native-harness/src/contracts/inspection.ts +22 -0
- package/src/native-harness/src/contracts/loop.ts +31 -0
- package/src/native-harness/src/contracts/model-execution.ts +83 -0
- package/src/native-harness/src/contracts/operator-controls.ts +106 -0
- package/src/native-harness/src/contracts/output.ts +13 -0
- package/src/native-harness/src/contracts/protocol.ts +320 -0
- package/src/native-harness/src/contracts/reminders.ts +132 -0
- package/src/native-harness/src/contracts/scenario.ts +42 -0
- package/src/native-harness/src/contracts/session-file.ts +19 -0
- package/src/native-harness/src/contracts/session.ts +184 -0
- package/src/native-harness/src/contracts/storage.ts +596 -0
- package/src/native-harness/src/contracts/timeline-retrieval.ts +40 -0
- package/src/native-harness/src/contracts/tools.ts +133 -0
- package/src/native-harness/src/governance/permissions/gate.ts +129 -0
- package/src/native-harness/src/governance/policy/resolve.ts +32 -0
- package/src/native-harness/src/index.ts +318 -0
- package/src/native-harness/src/interfaces/cli/chat.ts +83 -0
- package/src/native-harness/src/interfaces/cli/development.ts +145 -0
- package/src/native-harness/src/interfaces/cli/format-reply.ts +27 -0
- package/src/native-harness/src/interfaces/cli/listeners.ts +74 -0
- package/src/native-harness/src/interfaces/cli/main.ts +47 -0
- package/src/native-harness/src/interfaces/cli/scenario.ts +37 -0
- package/src/native-harness/src/interfaces/cli/serve.ts +21 -0
- package/src/native-harness/src/interfaces/cli/start.ts +106 -0
- package/src/native-harness/src/interfaces/client.ts +225 -0
- package/src/native-harness/src/interfaces/inspector/index.html +283 -0
- package/src/native-harness/src/interfaces/inspector/observe.ts +35 -0
- package/src/native-harness/src/interfaces/inspector/server.ts +56 -0
- package/src/native-harness/src/interfaces/inspector/state.ts +92 -0
- package/src/native-harness/src/interfaces/protocol/channel-changes.ts +153 -0
- package/src/native-harness/src/interfaces/protocol/framing.ts +47 -0
- package/src/native-harness/src/interfaces/protocol/functions.ts +87 -0
- package/src/native-harness/src/interfaces/protocol/in-process.ts +67 -0
- package/src/native-harness/src/interfaces/protocol/live-queue.ts +55 -0
- package/src/native-harness/src/interfaces/protocol/load.ts +60 -0
- package/src/native-harness/src/interfaces/protocol/local.ts +120 -0
- package/src/native-harness/src/interfaces/protocol/operator.ts +120 -0
- package/src/native-harness/src/interfaces/protocol/output.ts +59 -0
- package/src/native-harness/src/interfaces/protocol/server.ts +517 -0
- package/src/native-harness/src/interfaces/protocol/stdio-client.ts +119 -0
- package/src/native-harness/src/interfaces/protocol/stdio-server.ts +82 -0
- package/src/native-harness/src/interfaces/protocol/subscribe.ts +41 -0
- package/src/native-harness/src/interfaces/protocol/transfers.ts +146 -0
- package/src/native-harness/src/interfaces/web-chat/index.html +94 -0
- package/src/native-harness/src/interfaces/web-chat/server.ts +34 -0
- package/src/native-harness/src/kernel/channels.ts +405 -0
- package/src/native-harness/src/kernel/loop.ts +45 -0
- package/src/native-harness/src/runtime/config/development.ts +23 -0
- package/src/native-harness/src/runtime/config/managed.ts +2 -0
- package/src/native-harness/src/runtime/config/paths.ts +27 -0
- package/src/native-harness/src/runtime/reminder-timer.ts +42 -0
- package/src/native-harness/src/runtime/reminder-validation.ts +104 -0
- package/src/native-harness/src/runtime/reminders.ts +66 -0
- package/src/native-harness/src/state/storage/archive.ts +239 -0
- package/src/native-harness/src/state/storage/engine.ts +1013 -0
- package/src/native-harness/src/state/storage/inference.ts +124 -0
- package/src/native-harness/src/state/storage/memory-reminders.ts +78 -0
- package/src/native-harness/src/state/storage/memory.ts +341 -0
- package/src/native-harness/src/state/storage/policy-metadata.ts +32 -0
- package/src/native-harness/src/state/storage/reminder-archive.ts +63 -0
- package/src/native-harness/src/state/storage/reminder-change.ts +92 -0
- package/src/native-harness/src/state/storage/reminder-tables.ts +27 -0
- package/src/native-harness/src/state/storage/retrieval.ts +122 -0
- package/src/native-harness/src/state/storage/script-identity.ts +7 -0
- package/src/native-harness/src/state/storage/session-file.ts +85 -0
- package/src/native-harness/src/state/storage/sqlite-reminders.ts +134 -0
- package/src/native-harness/src/state/storage/sqlite.ts +625 -0
- package/src/native-harness/src/testing.ts +190 -0
- package/src/native-harness/src/util/config.ts +58 -0
- package/src/native-harness/src/util/json.ts +41 -0
- package/src/native-harness/src/util/message-position.ts +3 -0
- package/src/native-harness/src/util/reminder-schedule.ts +44 -0
- package/src/native-harness/src/util/schema.ts +40 -0
- package/src/native-harness/src/util/timeline-text.ts +72 -0
- package/src/native-harness/src/util/timeline-tool-names.ts +3 -0
- package/src/native-harness/test/agent.test.ts +130 -0
- package/src/native-harness/test/approval-gate.test.ts +255 -0
- package/src/native-harness/test/approval-protocol.test.ts +94 -0
- package/src/native-harness/test/boundaries.test.ts +18 -0
- package/src/native-harness/test/channel-controls.test.ts +322 -0
- package/src/native-harness/test/client-abort.test.ts +136 -0
- package/src/native-harness/test/config-policy.test.ts +301 -0
- package/src/native-harness/test/conformance/storage.ts +1141 -0
- package/src/native-harness/test/context.test.ts +177 -0
- package/src/native-harness/test/crash.test.ts +187 -0
- package/src/native-harness/test/debug-tools.test.ts +67 -0
- package/src/native-harness/test/development-cli.test.ts +67 -0
- package/src/native-harness/test/development.test.ts +171 -0
- package/src/native-harness/test/dispatch-evidence.test.ts +58 -0
- package/src/native-harness/test/e2e.test.ts +186 -0
- package/src/native-harness/test/external-notices.test.ts +101 -0
- package/src/native-harness/test/fixtures/approval-e2e.jsonl +4 -0
- package/src/native-harness/test/fixtures/crash-scenario.jsonl +5 -0
- package/src/native-harness/test/fixtures/development-lifecycle.jsonl +10 -0
- package/src/native-harness/test/fixtures/echo-scenario.jsonl +3 -0
- package/src/native-harness/test/fixtures/format2-schema.ts +27 -0
- package/src/native-harness/test/fixtures/format3-schema.ts +16 -0
- package/src/native-harness/test/fixtures/hold-scenario.jsonl +4 -0
- package/src/native-harness/test/fixtures/model-e2e.jsonl +3 -0
- package/src/native-harness/test/fixtures/storage-owner.ts +20 -0
- package/src/native-harness/test/format-reply.test.ts +10 -0
- package/src/native-harness/test/helpers/operator-view.ts +41 -0
- package/src/native-harness/test/helpers/scenario.ts +9 -0
- package/src/native-harness/test/helpers/session.ts +38 -0
- package/src/native-harness/test/helpers/stdio.ts +46 -0
- package/src/native-harness/test/inspector.test.ts +77 -0
- package/src/native-harness/test/listeners.test.ts +49 -0
- package/src/native-harness/test/loop.test.ts +88 -0
- package/src/native-harness/test/model-channel-double.test.ts +87 -0
- package/src/native-harness/test/model-crash.test.ts +62 -0
- package/src/native-harness/test/model-delivery-regressions.test.ts +55 -0
- package/src/native-harness/test/model-e2e.test.ts +71 -0
- package/src/native-harness/test/model-final-filter.test.ts +30 -0
- package/src/native-harness/test/model-large-reply.test.ts +28 -0
- package/src/native-harness/test/model-live-queue.test.ts +33 -0
- package/src/native-harness/test/model-runner.test.ts +117 -0
- package/src/native-harness/test/model-storage.test.ts +99 -0
- package/src/native-harness/test/model-stream-adapter.test.ts +26 -0
- package/src/native-harness/test/model-stream-public.test.ts +77 -0
- package/src/native-harness/test/notices.test.ts +64 -0
- package/src/native-harness/test/openrouter-config.test.ts +218 -0
- package/src/native-harness/test/operator-controls.test.ts +132 -0
- package/src/native-harness/test/output.test.ts +44 -0
- package/src/native-harness/test/owner-config.test.ts +64 -0
- package/src/native-harness/test/permissions-e2e.test.ts +68 -0
- package/src/native-harness/test/persistence.test.ts +236 -0
- package/src/native-harness/test/portability.test.ts +337 -0
- package/src/native-harness/test/protocol.test.ts +160 -0
- package/src/native-harness/test/reminder-clock.test.ts +61 -0
- package/src/native-harness/test/reminder-stdio.test.ts +140 -0
- package/src/native-harness/test/reminder-storage.test.ts +124 -0
- package/src/native-harness/test/scenario-jsonl.test.ts +112 -0
- package/src/native-harness/test/scripted.test.ts +92 -0
- package/src/native-harness/test/session-file.test.ts +51 -0
- package/src/native-harness/test/startup-channel-changes.test.ts +256 -0
- package/src/native-harness/test/stdio.test.ts +132 -0
- package/src/native-harness/test/storage-memory.test.ts +4 -0
- package/src/native-harness/test/storage-sqlite.test.ts +420 -0
- package/src/native-harness/test/suspension.test.ts +430 -0
- package/src/native-harness/test/timeline-retrieval.test.ts +211 -0
- package/src/native-harness/test/tools-catalog.test.ts +468 -0
- package/src/native-harness/test/tools-protocol.test.ts +418 -0
- package/src/native-harness/test/transfers.test.ts +94 -0
- package/src/native-harness/test/web-chat.test.ts +35 -0
- package/src/native-harness/tsconfig.json +20 -0
- package/src/observation/agent-event-bus.ts +44 -18
- package/src/observation/index.ts +1 -0
- package/src/operations/contracts.ts +11 -3
- package/src/operations/errors.ts +5 -0
- package/src/operations/stale-policy.ts +1 -1
- package/src/operations/supervisor.ts +34 -10
- package/src/orchestrator/acp-delivery.ts +68 -0
- package/src/orchestrator/assignments.ts +27 -0
- package/src/orchestrator/busy.ts +15 -0
- package/src/orchestrator/config.ts +41 -0
- package/src/orchestrator/consumer-api.ts +24 -0
- package/src/orchestrator/contracts.ts +71 -0
- package/src/orchestrator/durable-assignments.ts +111 -0
- package/src/orchestrator/native-delegation.ts +103 -0
- package/src/orchestrator/orchestrator.ts +318 -0
- package/src/orchestrator/permissions.ts +25 -0
- package/src/orchestrator/policy.ts +44 -0
- package/src/orchestrator/pool.ts +123 -0
- package/src/orchestrator/tool-descriptions.ts +11 -0
- package/src/permissions/acp.ts +18 -0
- package/src/permissions/arrival.ts +13 -0
- package/src/permissions/consumer-config.ts +19 -0
- package/src/permissions/conversation.ts +280 -0
- package/src/permissions/matcher.ts +85 -0
- package/src/permissions/outcome.ts +15 -0
- package/src/permissions/policy.ts +81 -0
- package/src/permissions/request.ts +26 -0
- package/src/persistence/activity-projection.ts +1 -0
- package/src/persistence/backend.ts +34 -1
- package/src/persistence/channels.ts +23 -0
- package/src/persistence/index.ts +4 -0
- package/src/persistence/protocol-method.ts +5 -0
- package/src/persistence/reminder-wake.ts +49 -0
- package/src/persistence/reminders.ts +385 -0
- package/src/persistence/sqlite-backend.ts +84 -6
- package/src/persistence/sqlite-channels.ts +65 -0
- package/src/persistence/sqlite-reminders.ts +85 -0
- package/src/persistence/sqlite-schema.ts +108 -3
- package/src/persistence/sqlite-worker-delivery.ts +374 -0
- package/src/persistence/sqlite-workers.ts +59 -0
- package/src/persistence/worker-delivery-contract.ts +104 -0
- package/src/persistence/worker-terminal-reason.ts +29 -0
- package/src/persistence/workers.ts +19 -0
- package/src/provider-notices.ts +137 -0
- package/src/repl/commands.ts +2 -2
- package/src/steering/combine.ts +3 -1
- package/src/steering/contracts.ts +11 -2
- package/src/steering/controller.ts +3 -2
- package/src/transport/acp-transport.ts +24 -14
- package/src/types.ts +15 -12
- package/src/updates.ts +14 -2
- package/src/wire-log.ts +1 -1
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Turns the backend starts on its own (controller-interface spec, How a turn runs after the refactor):
|
|
3
|
+
* a reported `turn_started` with `sentBySession: false` joins the conversation's operation queue as an
|
|
4
|
+
* item, so it never overlaps another operation, and runs when its turn comes: `turn_started` is emitted
|
|
5
|
+
* when the item runs, then `runTurn` without the ACP content phase (no user record, no ACP input, the
|
|
6
|
+
* session's own recording handlers; supervised and metered like any turn), then completion after
|
|
7
|
+
* finalization. The controller holds the turn's output until its prepared turn's `prompt()` is called.
|
|
8
|
+
* No ACP path reports such a turn; the fake controller and, later, the native controller do.
|
|
9
|
+
*
|
|
10
|
+
* A backend-scheduled controller (`nativeSteering`) also decides which turn takes which message: the
|
|
11
|
+
* session hands each message over with `send`, whose queue operation ends when the backend has accepted
|
|
12
|
+
* it, and the message's promise settles with the turn that takes it (named by that turn's cause, extended
|
|
13
|
+
* by `turn_took`): its finalized result when the turn completes, a rejection when it fails.
|
|
14
|
+
*/
|
|
15
|
+
import type { AgentSessionPromptResult } from "../agent-conversation-contracts.ts";
|
|
16
|
+
import type { AgentSessionEvent } from "../agent-conversation-contracts.ts";
|
|
17
|
+
import type { ConversationController, TurnCause, TurnStarted, TurnTook } from "../conversation-controller/contracts.ts";
|
|
18
|
+
import { cancelledPromptResult } from "../steering/controller.ts";
|
|
19
|
+
import type { Logger } from "../logger.ts";
|
|
20
|
+
import type { PromptHandlers } from "../types.ts";
|
|
21
|
+
import type { PromptQueueItem, TurnContext } from "./context.ts";
|
|
22
|
+
import { copyPromptResult } from "./record-helpers.ts";
|
|
23
|
+
import { turnRunOf } from "./turn-run.ts";
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* A reported turn waiting in the queue for its turn to run. It remembers the controller that reported it: the
|
|
27
|
+
* turn and its held output belong to that controller, which a handoff queued ahead of it may replace.
|
|
28
|
+
* `cause` and `messageIds` grow when the backend folds more into the turn (`turn_took`).
|
|
29
|
+
*/
|
|
30
|
+
export interface BackendTurnItem {
|
|
31
|
+
readonly kind: "backend_turn";
|
|
32
|
+
readonly turnId: string;
|
|
33
|
+
cause: TurnCause;
|
|
34
|
+
/** The messages the session sent that this turn took: from its cause and from `turn_took`. */
|
|
35
|
+
readonly messageIds: Set<string>;
|
|
36
|
+
readonly controller: ConversationController;
|
|
37
|
+
readonly permissionHandler?: PromptHandlers["onPermissionRequest"];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** The backend-turn path's state: reported turns not yet ended, and messages waiting for the turn that takes them. */
|
|
41
|
+
/**
|
|
42
|
+
* Where a message handed to a backend-scheduled controller is: `sending` (`send` has not resolved yet), `waiting` (accepted,
|
|
43
|
+
* no turn has claimed it: the only state a local withdraw or `clearQueue` may cancel), or `taken` (a turn's cause or a
|
|
44
|
+
* `turn_took` names it: that turn owns its settlement).
|
|
45
|
+
*/
|
|
46
|
+
type MessageState = "sending" | "waiting" | "taken";
|
|
47
|
+
|
|
48
|
+
export class BackendTurns {
|
|
49
|
+
readonly #turns = new Map<string, BackendTurnItem>();
|
|
50
|
+
readonly #messages = new Map<string, { readonly item: PromptQueueItem; state: MessageState; offered?: true; release?: () => void }>();
|
|
51
|
+
/** The backend turn running now (it holds the session's queue), if any. */
|
|
52
|
+
running: string | null = null;
|
|
53
|
+
|
|
54
|
+
/** A message is handed to a backend-scheduled controller: `send` is in flight. */
|
|
55
|
+
wait(item: PromptQueueItem, release?: () => void): void { this.#messages.set(item.turnId, { item, state: "sending", release }); }
|
|
56
|
+
/** `send` resolved: the message is accepted (unless a turn already claimed it meanwhile). */
|
|
57
|
+
accepted(messageId: string): void {
|
|
58
|
+
const message = this.#messages.get(messageId);
|
|
59
|
+
if (message?.state === "sending") message.state = "waiting";
|
|
60
|
+
}
|
|
61
|
+
unwait(messageId: string): boolean {
|
|
62
|
+
this.#messages.get(messageId)?.release?.();
|
|
63
|
+
return this.#messages.delete(messageId);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
beginInput(messageId: string): boolean {
|
|
67
|
+
const message = this.#messages.get(messageId);
|
|
68
|
+
if (!message) return false;
|
|
69
|
+
message.offered = true;
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
withdrawable(messageId: string): "local" | "submitted" | undefined {
|
|
74
|
+
const message = this.#messages.get(messageId);
|
|
75
|
+
if (message && message.state !== "taken") return message.offered ? "submitted" : "local";
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Allow a single grace period for all submitted, unclaimed native inputs before stopping. */
|
|
79
|
+
async withdrawBeforeStop(controller: ConversationController): Promise<void> {
|
|
80
|
+
const withdraw = controller.withdrawInput?.bind(controller);
|
|
81
|
+
if (!withdraw) return;
|
|
82
|
+
const submitted = [...this.#messages.keys()].filter(id => this.withdrawable(id) === "submitted");
|
|
83
|
+
if (submitted.length === 0) return;
|
|
84
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
85
|
+
try {
|
|
86
|
+
await Promise.race([
|
|
87
|
+
Promise.allSettled(submitted.map(async id => withdraw(id, turnId => this.noteTaken(id, turnId)))),
|
|
88
|
+
new Promise<void>(resolve => { timer = setTimeout(() => resolve(), 1_000); }),
|
|
89
|
+
]);
|
|
90
|
+
} finally {
|
|
91
|
+
clearTimeout(timer);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
noteTaken(id: string, turnId?: string): void {
|
|
96
|
+
this.#claim([id]);
|
|
97
|
+
const turn = turnId && this.#turns.get(turnId);
|
|
98
|
+
if (turn) {
|
|
99
|
+
turn.messageIds.add(id);
|
|
100
|
+
turn.cause = mergeCause(turn.cause, { messageIds: [id] });
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
confirmWithdrawal(messageId: string, drop: (messages: PromptQueueItem[]) => void): boolean {
|
|
105
|
+
const message = this.#messages.get(messageId);
|
|
106
|
+
if (!message || message.state === "taken") return false;
|
|
107
|
+
this.unwait(messageId);
|
|
108
|
+
drop([{ ...message.item, waited: true }]);
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
report(item: BackendTurnItem): void {
|
|
113
|
+
this.#turns.set(item.turnId, item);
|
|
114
|
+
this.#claim(item.messageIds);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** A turn names these messages: they are its to settle now, and no local withdraw or clear may cancel them. */
|
|
118
|
+
#claim(ids: Iterable<string>): void {
|
|
119
|
+
for (const id of ids) {
|
|
120
|
+
const message = this.#messages.get(id);
|
|
121
|
+
if (message) message.state = "taken";
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** `turn_took`: more folded into a turn that has not ended. False for an unknown or ended turn. */
|
|
126
|
+
took(event: TurnTook): boolean {
|
|
127
|
+
const turn = this.#turns.get(event.turnId);
|
|
128
|
+
if (!turn) return false;
|
|
129
|
+
for (const id of event.cause.messageIds ?? []) turn.messageIds.add(id);
|
|
130
|
+
this.#claim(event.cause.messageIds ?? []);
|
|
131
|
+
turn.cause = mergeCause(turn.cause, event.cause);
|
|
132
|
+
return true;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** The turn ended (or was abandoned): the messages it took settle with `outcome`, and it is forgotten. */
|
|
136
|
+
end(turn: BackendTurnItem, outcome: { result: AgentSessionPromptResult } | { error: unknown }, log?: Logger): void {
|
|
137
|
+
this.#turns.delete(turn.turnId);
|
|
138
|
+
for (const id of turn.messageIds) {
|
|
139
|
+
const message = this.#messages.get(id);
|
|
140
|
+
if (!message) {
|
|
141
|
+
log?.warn("backend_turn_names_unknown_message", { turnId: turn.turnId, messageId: id }); // recorded, not trusted for settlement
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
this.unwait(id);
|
|
145
|
+
if ("result" in outcome) message.item.resolve(outcome.result);
|
|
146
|
+
else message.item.reject(outcome.error);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* The handlers a turn that took these messages runs with: those of the first message, in the order
|
|
152
|
+
* given, that brought any. A message folded in later (`turn_took`) does not change them.
|
|
153
|
+
*/
|
|
154
|
+
handlersOf(ids: Iterable<string>): PromptHandlers | undefined {
|
|
155
|
+
for (const id of ids) {
|
|
156
|
+
const handlers = this.#messages.get(id)?.item.handlers;
|
|
157
|
+
if (handlers) return handlers;
|
|
158
|
+
}
|
|
159
|
+
return undefined;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
permissionHandlerOf(ids: Iterable<string>): PromptHandlers["onPermissionRequest"] {
|
|
163
|
+
for (const id of ids) {
|
|
164
|
+
const handler = this.#messages.get(id)?.item.handlers?.onPermissionRequest;
|
|
165
|
+
if (handler) return handler;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** One accepted, unclaimed message leaves (to be dropped as cancelled); undefined for any other (in flight, taken, unknown). */
|
|
170
|
+
take(messageId: string): PromptQueueItem | undefined {
|
|
171
|
+
const message = this.#messages.get(messageId);
|
|
172
|
+
if (message?.state !== "waiting") return undefined;
|
|
173
|
+
this.#messages.delete(messageId);
|
|
174
|
+
return message.item;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* On native interrupt, locally held items leave and submitted items keep following the harness.
|
|
179
|
+
* On native stop, the active turn still owns its taken inputs and finalizes their result.
|
|
180
|
+
*/
|
|
181
|
+
clear(stopping: boolean, native = false): PromptQueueItem[] {
|
|
182
|
+
if (stopping && native) return this.#clearNativeStop();
|
|
183
|
+
const leaving = [...this.#messages].filter(([, message]) => stopping || (native ? !message.offered && message.state !== "taken" : message.state === "waiting"));
|
|
184
|
+
for (const [id] of leaving) this.unwait(id);
|
|
185
|
+
if (stopping) this.#turns.clear();
|
|
186
|
+
return leaving.map(([, message]) => message.item);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
#clearNativeStop(): PromptQueueItem[] {
|
|
190
|
+
const active = this.running === null ? undefined : this.#turns.get(this.running);
|
|
191
|
+
const untaken: PromptQueueItem[] = [];
|
|
192
|
+
for (const [id, message] of this.#messages) {
|
|
193
|
+
if (active?.messageIds.has(id)) continue;
|
|
194
|
+
this.unwait(id);
|
|
195
|
+
// Local input and held turns without a prepared result keep legacy cancellation.
|
|
196
|
+
if (!message.offered || message.state === "taken") message.item.resolve(cancelledPromptResult(message.item));
|
|
197
|
+
else untaken.push(message.item);
|
|
198
|
+
}
|
|
199
|
+
for (const id of this.#turns.keys()) if (id !== this.running) this.#turns.delete(id);
|
|
200
|
+
return untaken;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** The turn's cause after `took`: both lists grow (a cause can hold messages and items together). */
|
|
205
|
+
function mergeCause(cause: TurnCause, took: TurnCause): TurnCause {
|
|
206
|
+
const messageIds = [...new Set([...(cause.messageIds ?? []), ...(took.messageIds ?? [])])];
|
|
207
|
+
const items = [...(cause.items ?? []), ...(took.items ?? [])];
|
|
208
|
+
return { ...(messageIds.length > 0 ? { messageIds } : {}), ...(items.length > 0 ? { items } : {}) } as TurnCause;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** What the backend-turn path needs from the session. */
|
|
212
|
+
export interface BackendTurnHost {
|
|
213
|
+
readonly turns: BackendTurns;
|
|
214
|
+
readonly log: Logger;
|
|
215
|
+
/** The message left the queue and was handed to the backend. */
|
|
216
|
+
markSent(messageId: string): void;
|
|
217
|
+
/** The backend durably accepted the message (a native push's answer). */
|
|
218
|
+
received?(messageId: string): void;
|
|
219
|
+
/** The conversation is stopped (or stopping): a waiting turn is abandoned, not run. */
|
|
220
|
+
readonly stopped: boolean;
|
|
221
|
+
emit(event: AgentSessionEvent): void;
|
|
222
|
+
/** Queues the item and wakes the queue. */
|
|
223
|
+
enqueue(item: BackendTurnItem): void;
|
|
224
|
+
/** Runs the turn through `runTurn` (the session's `runPrompt`). */
|
|
225
|
+
run(turn: TurnContext): Promise<AgentSessionPromptResult>;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* The prompt item a turn without messages is run with: it carries nothing the turn would write or send
|
|
230
|
+
* (no text, no handlers), only what `runTurn`'s metering and persistence need to name the operation.
|
|
231
|
+
*/
|
|
232
|
+
export function backendTurnItem(turnId: string, handlers?: PromptHandlers): PromptQueueItem {
|
|
233
|
+
return {
|
|
234
|
+
kind: "prompt",
|
|
235
|
+
turnId,
|
|
236
|
+
operationKind: "prompt",
|
|
237
|
+
delivery: "queue",
|
|
238
|
+
waited: false,
|
|
239
|
+
text: "",
|
|
240
|
+
...(handlers ? { handlers } : {}),
|
|
241
|
+
resolve: () => undefined,
|
|
242
|
+
reject: () => undefined,
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** The controller reported a turn the session didn't send: queue it (or abandon it if the conversation is stopped). */
|
|
247
|
+
export function reportBackendTurn(host: BackendTurnHost, controller: ConversationController, event: TurnStarted): void {
|
|
248
|
+
if (host.stopped) {
|
|
249
|
+
if (controller.withdrawInput) for (const id of event.cause.messageIds ?? []) host.turns.noteTaken(id);
|
|
250
|
+
controller.abandonTurn(event.turnId, new Error("the conversation is stopped"));
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
const item: BackendTurnItem = {
|
|
254
|
+
kind: "backend_turn",
|
|
255
|
+
turnId: event.turnId,
|
|
256
|
+
cause: event.cause,
|
|
257
|
+
messageIds: new Set(event.cause.messageIds ?? []),
|
|
258
|
+
permissionHandler: host.turns.permissionHandlerOf(event.cause.messageIds ?? []),
|
|
259
|
+
controller,
|
|
260
|
+
};
|
|
261
|
+
host.turns.report(item);
|
|
262
|
+
host.enqueue(item);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
const isBackendTurn = (item: { readonly kind: string }): item is BackendTurnItem => item.kind === "backend_turn";
|
|
266
|
+
|
|
267
|
+
/** A turn started earlier took more: extend it. One that is unknown to the session, or has ended, is logged and ignored. */
|
|
268
|
+
export function reportTurnTook(host: BackendTurnHost, event: TurnTook): void {
|
|
269
|
+
if (!host.turns.took(event)) host.log.warn("controller_turn_took_ignored", { turnId: event.turnId });
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Hands one message to a backend-scheduled controller: no turn id (the backend assigns turn ids), and the queue
|
|
274
|
+
* operation ends when `send` resolves. The message waits for the turn that takes it; a rejected acceptance rejects it.
|
|
275
|
+
*/
|
|
276
|
+
export async function sendToBackend(host: BackendTurnHost, controller: ConversationController, message: PromptQueueItem): Promise<void> {
|
|
277
|
+
const release = controller.withdrawInput ? Promise.withResolvers<void>() : undefined;
|
|
278
|
+
host.turns.wait(message, release?.resolve);
|
|
279
|
+
try {
|
|
280
|
+
// No turn id: the backend assigns turn ids. The message goes as the queue holds it.
|
|
281
|
+
const { turnId: _sessionTurnId, ...run } = turnRunOf(message);
|
|
282
|
+
const send = controller.send(release ? { ...run, beginInput: id => !host.stopped && host.turns.beginInput(id) } : run);
|
|
283
|
+
await (release ? Promise.race([send, release.promise]) : send);
|
|
284
|
+
host.turns.accepted(message.turnId);
|
|
285
|
+
host.received?.(message.turnId);
|
|
286
|
+
} catch (error) {
|
|
287
|
+
if ((!release || host.turns.withdrawable(message.turnId)) && host.turns.unwait(message.turnId)) message.reject(error);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* Withdraws a message the backend accepted and no turn has taken yet: it resolves as cancelled and the wait ends. Local only
|
|
293
|
+
* (the backend is not told; a turn that later names the id finds it unknown and ignores it). False when it isn't waiting.
|
|
294
|
+
*/
|
|
295
|
+
export async function withdrawBackendInput(
|
|
296
|
+
turns: BackendTurns, controller: ConversationController, id: string, drop: (messages: PromptQueueItem[]) => void,
|
|
297
|
+
shouldSettle: () => boolean,
|
|
298
|
+
): Promise<boolean> {
|
|
299
|
+
if (!controller.withdrawInput) return withdrawWaiting(turns, id, drop);
|
|
300
|
+
const state = turns.withdrawable(id);
|
|
301
|
+
if (state === "local") return turns.confirmWithdrawal(id, drop);
|
|
302
|
+
if (state !== "submitted" || !await controller.withdrawInput(id)) return false;
|
|
303
|
+
if (shouldSettle()) turns.confirmWithdrawal(id, drop);
|
|
304
|
+
return true;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
export function withdrawWaiting(turns: BackendTurns, messageId: string, drop: (messages: PromptQueueItem[]) => void): boolean {
|
|
308
|
+
const message = turns.take(messageId);
|
|
309
|
+
if (message) drop([message]);
|
|
310
|
+
return message !== undefined;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Native steering: while a backend turn runs, a steer message goes to the backend at once, to be folded into that turn,
|
|
315
|
+
* instead of waiting behind it in the queue. Returns whether it was handed over (otherwise the caller queues it).
|
|
316
|
+
*/
|
|
317
|
+
export function steerIntoRunningTurn(host: BackendTurnHost, controller: ConversationController, message: PromptQueueItem): boolean {
|
|
318
|
+
if (message.delivery !== "steer" || host.turns.running === null || !controller.conversationCapabilities.nativeSteering) return false;
|
|
319
|
+
host.markSent(message.turnId);
|
|
320
|
+
void sendToBackend(host, controller, { ...message, waited: false });
|
|
321
|
+
return true;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Runs one queued backend turn: events as for any turn, a failure abandons it. `current` is the conversation's live
|
|
326
|
+
* controller: a turn whose controller was replaced while it waited (a handoff queued ahead of it) is abandoned on the
|
|
327
|
+
* controller that reported it, never run on the new one, so nothing stays held. The messages it took settle with it.
|
|
328
|
+
*/
|
|
329
|
+
export async function runBackendTurn(host: BackendTurnHost, current: ConversationController, item: BackendTurnItem): Promise<void> {
|
|
330
|
+
const controller = item.controller;
|
|
331
|
+
if (host.stopped || current !== controller) {
|
|
332
|
+
const error = new Error(host.stopped ? "the conversation stopped before the turn ran" : "the controller was replaced before the turn ran");
|
|
333
|
+
controller.abandonTurn(item.turnId, error);
|
|
334
|
+
host.turns.end(item, { error }, host.log);
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
host.turns.running = item.turnId;
|
|
338
|
+
try {
|
|
339
|
+
host.emit({ kind: "turn_started", turnId: item.turnId, cause: item.cause });
|
|
340
|
+
// The consumer's own handlers, when a message the turn took brought them: its stream and its
|
|
341
|
+
// permission requests reach the consumer that sent the message, as for a turn the session sends.
|
|
342
|
+
const streamHandlers = host.turns.handlersOf(item.messageIds);
|
|
343
|
+
const handlers = streamHandlers && { ...streamHandlers, onPermissionRequest: item.permissionHandler };
|
|
344
|
+
const result = await host.run({ turnId: item.turnId, get cause() { return item.cause; }, ...(handlers ? { handlers } : {}) });
|
|
345
|
+
host.emit({ kind: "turn_completed", turnId: item.turnId, result: copyPromptResult(result) });
|
|
346
|
+
host.turns.end(item, { result }, host.log);
|
|
347
|
+
} catch (error) {
|
|
348
|
+
// A turn that never reached its prepared turn's `prompt()` is released; for one that did, this does nothing.
|
|
349
|
+
controller.abandonTurn(item.turnId, error);
|
|
350
|
+
host.emit({ kind: "turn_failed", turnId: item.turnId, error: String(error) });
|
|
351
|
+
host.turns.end(item, { error }, host.log);
|
|
352
|
+
} finally {
|
|
353
|
+
host.turns.running = null;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* Queue clearing (`interrupt({ clearQueue })`, `stop`): a turn the backend already started is not a queued message.
|
|
359
|
+
* It stays queued and runs, except on stop, where it is abandoned (and forgotten). Returns what is left to drop.
|
|
360
|
+
*/
|
|
361
|
+
export function settleClearedBackendTurns<T extends { readonly kind: string }>(
|
|
362
|
+
cleared: readonly T[],
|
|
363
|
+
stopping: boolean,
|
|
364
|
+
requeue: (turn: BackendTurnItem) => void,
|
|
365
|
+
): Array<Exclude<T, BackendTurnItem>> {
|
|
366
|
+
const others: Array<Exclude<T, BackendTurnItem>> = [];
|
|
367
|
+
for (const item of cleared) {
|
|
368
|
+
if (!isBackendTurn(item)) others.push(item as Exclude<T, BackendTurnItem>);
|
|
369
|
+
else if (stopping) item.controller.abandonTurn(item.turnId, new Error("the conversation stopped"));
|
|
370
|
+
else requeue(item);
|
|
371
|
+
}
|
|
372
|
+
return others;
|
|
373
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { NativeInputStoppedError } from "../agent-conversation-errors.ts";
|
|
2
|
+
import type { PromptQueueItem } from "./context.ts";
|
|
3
|
+
|
|
4
|
+
type ClearedOperation = PromptQueueItem | {
|
|
5
|
+
readonly kind: "handoff" | "set_config" | "resolve_config" | "resolve_recovery" | "set_mode";
|
|
6
|
+
reject(error: unknown): void;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export function dropClearedOperations(
|
|
10
|
+
operations: readonly ClearedOperation[],
|
|
11
|
+
backend: readonly PromptQueueItem[],
|
|
12
|
+
drop: (messages: readonly PromptQueueItem[], error?: Error) => void,
|
|
13
|
+
cancellation?: Error,
|
|
14
|
+
stoppingNative = false,
|
|
15
|
+
): void {
|
|
16
|
+
if (stoppingNative) for (const item of backend) item.reject(new NativeInputStoppedError(item.turnId));
|
|
17
|
+
// A prompt cancelled before dispatch consumes no provider usage.
|
|
18
|
+
drop([...operations.filter((item): item is PromptQueueItem => item.kind === "prompt"), ...(stoppingNative ? [] : backend)], cancellation);
|
|
19
|
+
for (const item of operations) {
|
|
20
|
+
if (item.kind === "prompt") continue;
|
|
21
|
+
const label = item.kind === "handoff"
|
|
22
|
+
? "handoff"
|
|
23
|
+
: item.kind === "set_config" || item.kind === "resolve_config"
|
|
24
|
+
? "configuration change"
|
|
25
|
+
: item.kind === "resolve_recovery"
|
|
26
|
+
? "recovery resolution"
|
|
27
|
+
: "mode change";
|
|
28
|
+
item.reject(cancellation ?? new Error(`${label} cancelled before it started`));
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function captureStopFailure(stop: () => void, failures: unknown[]): () => void {
|
|
33
|
+
return () => {
|
|
34
|
+
try { stop(); }
|
|
35
|
+
catch (error) { failures.push(error); }
|
|
36
|
+
};
|
|
37
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import type { ConversationPermissions } from "../permissions/conversation.ts";
|
|
2
|
+
import type { ConversationPermissionConfig } from "../permissions/policy.ts";
|
|
3
|
+
/**
|
|
4
|
+
* The narrow context `runTurn` receives from `AgentSession` (controller-interface spec, Brief A2
|
|
5
|
+
* step 1). It names exactly what the turn path reads and changes in the session, grouped as the
|
|
6
|
+
* spec lists them, instead of passing the session itself. Members that the session can replace
|
|
7
|
+
* while it lives (the controller, the transcript runtime, the attachment state, ...) are getters,
|
|
8
|
+
* so the turn reads them at the same moments it did when it was a session method.
|
|
9
|
+
*/
|
|
10
|
+
import type { AggregatedRecord } from "../activity.ts";
|
|
11
|
+
import type { AgentConversationOwnerLifecycle } from "../agent-conversation-owner.ts";
|
|
12
|
+
import type { AgentConversationPresentation } from "../agent-conversation-presentation.ts";
|
|
13
|
+
import type { AgentConversationId, AgentSessionId } from "../agent-ids.ts";
|
|
14
|
+
import type { ConversationController, TurnCause } from "../conversation-controller/contracts.ts";
|
|
15
|
+
import type { Logger } from "../logger.ts";
|
|
16
|
+
import type { ResolvedPart } from "../media/parts.ts";
|
|
17
|
+
import type { NativeTurnMetering } from "../metering/native/source.ts";
|
|
18
|
+
import type { OpenOperation } from "../metering/engine.ts";
|
|
19
|
+
import type { SupervisedProvider } from "../operations/contracts.ts";
|
|
20
|
+
import type { MeteredOperationKind, OperationMeteringRecord } from "../metering/record.ts";
|
|
21
|
+
import type { NewConversationRecord, ParticipantRef } from "../persistence/backend.ts";
|
|
22
|
+
import type { ProviderAttachmentState } from "../persistence/provider-attachment.ts";
|
|
23
|
+
import type { PersistenceStateRuntime } from "../persistence/state-runtime.ts";
|
|
24
|
+
import type { SteeringController } from "../steering/controller.ts";
|
|
25
|
+
import type { SteeringPrompt } from "../steering/contracts.ts";
|
|
26
|
+
import type { TranscriptLine } from "../transcript.ts";
|
|
27
|
+
import type { PromptHandlers, PromptResult, TokenUsage } from "../types.ts";
|
|
28
|
+
|
|
29
|
+
/** A queued prompt operation: one message, or a combined steer run. */
|
|
30
|
+
export interface PromptQueueItem extends SteeringPrompt {
|
|
31
|
+
/** How this operation is classified in its metering record. */
|
|
32
|
+
operationKind: MeteredOperationKind;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The turn `runTurn` runs: its id and cause, and, when the session sent it, the originating queued
|
|
37
|
+
* messages with their handlers (one prompt item; for a combined steer run, the combined item whose
|
|
38
|
+
* `parts` are the original messages). A turn the backend started has no `messages`: it runs without the
|
|
39
|
+
* ACP content phase, writes no user record and uses the session's own recording handlers.
|
|
40
|
+
*/
|
|
41
|
+
export interface TurnContext {
|
|
42
|
+
readonly turnId: string;
|
|
43
|
+
/** Live for backend turns so finalization can see messages added by `turn_took`. */
|
|
44
|
+
readonly cause: TurnCause;
|
|
45
|
+
readonly messages?: PromptQueueItem;
|
|
46
|
+
/**
|
|
47
|
+
* For a turn the backend started that took messages this session sent: the handlers of the first of
|
|
48
|
+
* those messages that brought any (the rule a combined steer run uses). They receive the turn's stream
|
|
49
|
+
* and answer its permission requests; without them the session's defaults apply. Never set with `messages`.
|
|
50
|
+
*/
|
|
51
|
+
readonly handlers?: PromptHandlers;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface TurnRunContext {
|
|
55
|
+
readonly log: Logger;
|
|
56
|
+
|
|
57
|
+
/** The provider controller (replaced by handoffs and mode switches). */
|
|
58
|
+
readonly controller: ConversationController;
|
|
59
|
+
/** Runs one provider call under the operation supervisor, with the stall limits of `kind`. */
|
|
60
|
+
supervisedPrompt(
|
|
61
|
+
controller: ConversationController,
|
|
62
|
+
operationId: string,
|
|
63
|
+
kind: "turn",
|
|
64
|
+
provider: SupervisedProvider,
|
|
65
|
+
): Promise<PromptResult>;
|
|
66
|
+
/** Whether an operation supervisor is attached (it then routes activity to steering itself). */
|
|
67
|
+
readonly supervising: boolean;
|
|
68
|
+
|
|
69
|
+
readonly persistence: {
|
|
70
|
+
/** Managed mode's v2 transcript runtime, or null. */
|
|
71
|
+
readonly v2Transcript: PersistenceStateRuntime | null;
|
|
72
|
+
/** Detached mode's legacy JSONL transcript. */
|
|
73
|
+
appendTranscript(line: TranscriptLine): Promise<void>;
|
|
74
|
+
persist(): Promise<void>;
|
|
75
|
+
finishV2Prompt(
|
|
76
|
+
status: PromptResult["status"],
|
|
77
|
+
record: OperationMeteringRecord,
|
|
78
|
+
errorCode?: "unknown",
|
|
79
|
+
): Promise<void>;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
readonly permissions: ConversationPermissions;
|
|
83
|
+
readonly presentation: AgentConversationPresentation;
|
|
84
|
+
|
|
85
|
+
readonly metering: {
|
|
86
|
+
open(item: PromptQueueItem): Promise<OpenOperation>;
|
|
87
|
+
close(
|
|
88
|
+
operation: OpenOperation,
|
|
89
|
+
outcome: "completed" | "cancelled" | "error",
|
|
90
|
+
reportedCumulative: TokenUsage | null,
|
|
91
|
+
usageFailureReason?: string,
|
|
92
|
+
nativeMetering?: NativeTurnMetering,
|
|
93
|
+
): Promise<OperationMeteringRecord>;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
readonly steering: Pick<SteeringController<{ readonly kind: string }>, "observeActivity" | "resultFields">;
|
|
97
|
+
|
|
98
|
+
readonly owner: {
|
|
99
|
+
/** Set for conversations of an `Agent` (Agent-owned mode). */
|
|
100
|
+
readonly lifecycle: AgentConversationOwnerLifecycle | undefined;
|
|
101
|
+
readonly agentSessionId: AgentSessionId | null;
|
|
102
|
+
readonly agentConversationId: AgentConversationId;
|
|
103
|
+
/** A stable stand-in for the provider context before one exists, minted once per session. */
|
|
104
|
+
contextNonce(): string;
|
|
105
|
+
queueRecord(record: NewConversationRecord, parts?: readonly ResolvedPart[]): void;
|
|
106
|
+
recordNow(record: NewConversationRecord): Promise<void>;
|
|
107
|
+
/** The tail of the owned-record write chain (reassigned by every queued write). */
|
|
108
|
+
readonly recordWrite: Promise<void>;
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
readonly turn: {
|
|
112
|
+
begin(turnId: string): void;
|
|
113
|
+
end(turnId: string, handlers?: PromptHandlers): void;
|
|
114
|
+
queueActivityRecords(turnId: string, records: readonly AggregatedRecord[]): void;
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
/** Session state the turn reads or updates. */
|
|
118
|
+
readonly session: {
|
|
119
|
+
readonly cwd: string | undefined;
|
|
120
|
+
readonly permissionPolicy: ConversationPermissionConfig;
|
|
121
|
+
readonly internalSessionId: string | null;
|
|
122
|
+
setInternalSessionId(next: string | null): void;
|
|
123
|
+
title: string | undefined;
|
|
124
|
+
/** Adds a participant the conversation does not know yet. */
|
|
125
|
+
learnParticipant(participant: ParticipantRef): void;
|
|
126
|
+
providerAttachment: ProviderAttachmentState;
|
|
127
|
+
installProviderAttachment(
|
|
128
|
+
internalSessionId: string,
|
|
129
|
+
kind: "runtime_only" | "recorded",
|
|
130
|
+
created: boolean,
|
|
131
|
+
options?: { readonly acquiredAt?: string; readonly transitionId?: string },
|
|
132
|
+
): void;
|
|
133
|
+
lastRecordedInternalSessionId: string | null;
|
|
134
|
+
};
|
|
135
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { BackendTurns, reportBackendTurn, reportTurnTook, runBackendTurn, sendToBackend, settleClearedBackendTurns, steerIntoRunningTurn, withdrawWaiting, type BackendTurnHost, type BackendTurnItem } from "./backend-turns.ts";
|
|
2
|
+
export { runTurn } from "./run-turn.ts";
|
|
3
|
+
export { checkTurnRun, turnRunOf, type RunMessageState } from "./turn-run.ts";
|
|
4
|
+
export type { PromptQueueItem, TurnContext, TurnRunContext } from "./context.ts";
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure helpers the turn path uses to shape records, moved unchanged from `agent-session.ts`
|
|
3
|
+
* (controller-interface spec, Brief A2 step 1). `agent-session.ts` imports the ones it still uses.
|
|
4
|
+
*/
|
|
5
|
+
import { attachmentParts } from "../media/acp.ts";
|
|
6
|
+
import { transcriptParts, type MessagePart } from "../media/parts.ts";
|
|
7
|
+
import type { MeteredOperationKind } from "../metering/record.ts";
|
|
8
|
+
import type { PersistenceJsonValue } from "../persistence/backend.ts";
|
|
9
|
+
import type { OperationKind } from "../persistence/records.ts";
|
|
10
|
+
import type {
|
|
11
|
+
Attachment,
|
|
12
|
+
ResponseMessage,
|
|
13
|
+
PromptResult,
|
|
14
|
+
} from "../types.ts";
|
|
15
|
+
|
|
16
|
+
/** The older JSONL transcript has no media store: a message with parts keeps a line per part. */
|
|
17
|
+
export function legacyTranscriptText(
|
|
18
|
+
text: string,
|
|
19
|
+
parts?: readonly MessagePart[],
|
|
20
|
+
attachments?: readonly Attachment[],
|
|
21
|
+
): string {
|
|
22
|
+
if (!parts && !attachments?.length) return text;
|
|
23
|
+
return transcriptParts([...(parts ?? [{ kind: "text", text }]), ...attachmentParts(attachments)]);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** The older JSONL transcript's view of response messages, with a line per media part. */
|
|
27
|
+
export function legacyTranscriptMessages(messages: readonly ResponseMessage[], text: string) {
|
|
28
|
+
const messageText = (message: ResponseMessage) => (message.parts ? transcriptParts(message.parts) : message.text);
|
|
29
|
+
return {
|
|
30
|
+
text: messages.some((message) => message.parts) ? messages.map(messageText).join("\n\n") : text,
|
|
31
|
+
messages: messages.map((message) => ({
|
|
32
|
+
...(message.messageId ? { messageId: message.messageId } : {}),
|
|
33
|
+
text: messageText(message),
|
|
34
|
+
})),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function persistenceOperationKind(kind: MeteredOperationKind): OperationKind {
|
|
39
|
+
if (kind === "internal_command") return "internal_session_command";
|
|
40
|
+
if (
|
|
41
|
+
kind === "conversation_checkpoint"
|
|
42
|
+
|| kind === "conversation_recovery"
|
|
43
|
+
|| kind === "identity_update"
|
|
44
|
+
) return "other";
|
|
45
|
+
return kind;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function toPersistenceJsonValue(value: unknown): PersistenceJsonValue {
|
|
49
|
+
if (value === undefined) return null;
|
|
50
|
+
const serialized = JSON.stringify(value);
|
|
51
|
+
return serialized === undefined ? String(value) : JSON.parse(serialized) as PersistenceJsonValue;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function snapshotConsumerMetadata(
|
|
55
|
+
value: PersistenceJsonValue | undefined,
|
|
56
|
+
owned: boolean,
|
|
57
|
+
): PersistenceJsonValue | undefined {
|
|
58
|
+
if (value === undefined) return undefined;
|
|
59
|
+
if (!owned) throw new Error("consumerMetadata requires an Agent-owned conversation");
|
|
60
|
+
const serialized = JSON.stringify(value);
|
|
61
|
+
if (serialized === undefined) throw new Error("consumerMetadata must be a JSON value");
|
|
62
|
+
return JSON.parse(serialized) as PersistenceJsonValue;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Do not let one notification observer mutate the PromptResult another observer sees. */
|
|
66
|
+
export function copyPromptResult(result: PromptResult): PromptResult {
|
|
67
|
+
const messages = result.messages ?? (result.text ? [{ text: result.text }] : []);
|
|
68
|
+
return Object.freeze({
|
|
69
|
+
...result,
|
|
70
|
+
messages: Object.freeze(
|
|
71
|
+
messages.map((message) => Object.freeze({ ...message })),
|
|
72
|
+
),
|
|
73
|
+
usage: result.usage ? Object.freeze({ ...result.usage }) : null,
|
|
74
|
+
});
|
|
75
|
+
}
|