@zvada/agent-server 0.3.22 → 0.3.23
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.md +261 -0
- package/dist/types/accounts/chatgpt.d.ts +1 -1
- package/dist/types/accounts/chatgpt.d.ts.map +1 -1
- package/dist/types/accounts/http.d.ts +2 -4
- package/dist/types/accounts/http.d.ts.map +1 -1
- package/dist/types/client/client.d.ts +37 -11
- package/dist/types/client/client.d.ts.map +1 -1
- package/dist/types/client/connection.d.ts +64 -13
- package/dist/types/client/connection.d.ts.map +1 -1
- package/dist/types/client/index.d.ts +1 -1
- package/dist/types/client/index.d.ts.map +1 -1
- package/dist/types/client/logins.d.ts +2 -1
- package/dist/types/client/logins.d.ts.map +1 -1
- package/dist/types/client/streams.d.ts +9 -3
- package/dist/types/client/streams.d.ts.map +1 -1
- package/dist/types/client/turns.d.ts +1 -1
- package/dist/types/client/turns.d.ts.map +1 -1
- package/dist/types/core/agents/acp/acp-agent.d.ts +34 -1
- package/dist/types/core/agents/acp/acp-agent.d.ts.map +1 -1
- package/dist/types/core/agents/acp/adapter.d.ts.map +1 -1
- package/dist/types/core/agents/acp/client.d.ts +12 -3
- package/dist/types/core/agents/acp/client.d.ts.map +1 -1
- package/dist/types/core/agents/acp/mappings.d.ts +8 -2
- package/dist/types/core/agents/acp/mappings.d.ts.map +1 -1
- package/dist/types/core/agents/base.d.ts +22 -8
- package/dist/types/core/agents/base.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/adapter.d.ts +7 -0
- package/dist/types/core/agents/claude-code/adapter.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/approvals.d.ts +4 -1
- package/dist/types/core/agents/claude-code/approvals.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/claude-agent.d.ts +16 -5
- package/dist/types/core/agents/claude-code/claude-agent.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/events.d.ts +9 -1
- package/dist/types/core/agents/claude-code/events.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/login.d.ts +4 -1
- package/dist/types/core/agents/claude-code/login.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/mcp.d.ts +21 -7
- package/dist/types/core/agents/claude-code/mcp.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/models.d.ts +5 -1
- package/dist/types/core/agents/claude-code/models.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/options.d.ts +10 -4
- package/dist/types/core/agents/claude-code/options.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/outcome.d.ts +3 -5
- package/dist/types/core/agents/claude-code/outcome.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/results.d.ts +16 -0
- package/dist/types/core/agents/claude-code/results.d.ts.map +1 -0
- package/dist/types/core/agents/claude-code/sdk-pair.d.ts +13 -0
- package/dist/types/core/agents/claude-code/sdk-pair.d.ts.map +1 -0
- package/dist/types/core/agents/claude-code/session.d.ts +50 -12
- package/dist/types/core/agents/claude-code/session.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/spend.d.ts +2 -1
- package/dist/types/core/agents/claude-code/spend.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/tools.d.ts +26 -3
- package/dist/types/core/agents/claude-code/tools.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/adapter.d.ts +2 -2
- package/dist/types/core/agents/codex-app-server/adapter.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/client.d.ts +12 -2
- package/dist/types/core/agents/codex-app-server/client.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/codex-agent.d.ts +58 -13
- package/dist/types/core/agents/codex-app-server/codex-agent.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/commands.d.ts +2 -2
- package/dist/types/core/agents/codex-app-server/commands.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/credentials.d.ts +5 -1
- package/dist/types/core/agents/codex-app-server/credentials.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/effort.d.ts +2 -1
- package/dist/types/core/agents/codex-app-server/effort.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/homes.d.ts +19 -0
- package/dist/types/core/agents/codex-app-server/homes.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/items.d.ts +27 -2
- package/dist/types/core/agents/codex-app-server/items.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/login.d.ts +4 -1
- package/dist/types/core/agents/codex-app-server/login.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/mcp.d.ts +15 -30
- package/dist/types/core/agents/codex-app-server/mcp.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/models.d.ts +3 -10
- package/dist/types/core/agents/codex-app-server/models.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/options.d.ts +27 -4
- package/dist/types/core/agents/codex-app-server/options.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/plan-approval.d.ts +2 -1
- package/dist/types/core/agents/codex-app-server/plan-approval.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/protocol.d.ts +16 -3
- package/dist/types/core/agents/codex-app-server/protocol.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/requests.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/session.d.ts +68 -40
- package/dist/types/core/agents/codex-app-server/session.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/subagents.d.ts +9 -4
- package/dist/types/core/agents/codex-app-server/subagents.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/thread-settings.d.ts +3 -2
- package/dist/types/core/agents/codex-app-server/thread-settings.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/turn-items.d.ts +2 -1
- package/dist/types/core/agents/codex-app-server/turn-items.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/turn.d.ts +31 -8
- package/dist/types/core/agents/codex-app-server/turn.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/user-login.d.ts +28 -0
- package/dist/types/core/agents/codex-app-server/user-login.d.ts.map +1 -0
- package/dist/types/core/agents/credentials.d.ts +3 -1
- package/dist/types/core/agents/credentials.d.ts.map +1 -1
- package/dist/types/core/agents/diffs.d.ts +9 -0
- package/dist/types/core/agents/diffs.d.ts.map +1 -0
- package/dist/types/core/agents/elicitation.d.ts +3 -2
- package/dist/types/core/agents/elicitation.d.ts.map +1 -1
- package/dist/types/core/agents/login.d.ts +6 -0
- package/dist/types/core/agents/login.d.ts.map +1 -1
- package/dist/types/core/agents/mcp-config.d.ts +0 -4
- package/dist/types/core/agents/mcp-config.d.ts.map +1 -1
- package/dist/types/core/agents/process-pool.d.ts +193 -0
- package/dist/types/core/agents/process-pool.d.ts.map +1 -0
- package/dist/types/core/agents/scripted/adapter.d.ts +68 -0
- package/dist/types/core/agents/scripted/adapter.d.ts.map +1 -0
- package/dist/types/core/agents/scripted/mcp-call.d.ts +56 -0
- package/dist/types/core/agents/scripted/mcp-call.d.ts.map +1 -0
- package/dist/types/core/agents/scripted/scripted-agent.d.ts +73 -0
- package/dist/types/core/agents/scripted/scripted-agent.d.ts.map +1 -0
- package/dist/types/core/agents/scripted/scripts.d.ts +29 -0
- package/dist/types/core/agents/scripted/scripts.d.ts.map +1 -0
- package/dist/types/core/agents/scripted/steps.d.ts +132 -0
- package/dist/types/core/agents/scripted/steps.d.ts.map +1 -0
- package/dist/types/core/agents/session-agent.d.ts +49 -6
- package/dist/types/core/agents/session-agent.d.ts.map +1 -1
- package/dist/types/core/diagnostics.d.ts +4 -1
- package/dist/types/core/diagnostics.d.ts.map +1 -1
- package/dist/types/core/index.d.ts +5 -0
- package/dist/types/core/index.d.ts.map +1 -1
- package/dist/types/core/presets.d.ts +32 -6
- package/dist/types/core/presets.d.ts.map +1 -1
- package/dist/types/core/processes/lifeline.d.ts +0 -20
- package/dist/types/core/processes/lifeline.d.ts.map +1 -1
- package/dist/types/core/provision/index.d.ts +2 -2
- package/dist/types/core/provision/index.d.ts.map +1 -1
- package/dist/types/core/provision/install-build.d.ts +2 -1
- package/dist/types/core/provision/install-build.d.ts.map +1 -1
- package/dist/types/core/provision/npm.d.ts +20 -8
- package/dist/types/core/provision/npm.d.ts.map +1 -1
- package/dist/types/core/provision/provisioner.d.ts +4 -1
- package/dist/types/core/provision/provisioner.d.ts.map +1 -1
- package/dist/types/core/provision/tools.d.ts +2 -1
- package/dist/types/core/provision/tools.d.ts.map +1 -1
- package/dist/types/core/runtime/agent-runtime.d.ts +16 -8
- package/dist/types/core/runtime/agent-runtime.d.ts.map +1 -1
- package/dist/types/core/runtime/event-processor.d.ts +5 -1
- package/dist/types/core/runtime/event-processor.d.ts.map +1 -1
- package/dist/types/core/runtime/harness-clis.d.ts +9 -0
- package/dist/types/core/runtime/harness-clis.d.ts.map +1 -1
- package/dist/types/core/runtime/harness-logins.d.ts +33 -5
- package/dist/types/core/runtime/harness-logins.d.ts.map +1 -1
- package/dist/types/core/runtime/model-lists.d.ts +2 -0
- package/dist/types/core/runtime/model-lists.d.ts.map +1 -1
- package/dist/types/core/runtime/permission-policy.d.ts +5 -3
- package/dist/types/core/runtime/permission-policy.d.ts.map +1 -1
- package/dist/types/core/runtime/request-broker.d.ts +2 -1
- package/dist/types/core/runtime/request-broker.d.ts.map +1 -1
- package/dist/types/core/runtime/turn.d.ts +12 -1
- package/dist/types/core/runtime/turn.d.ts.map +1 -1
- package/dist/types/core/utils/deadline.d.ts +6 -0
- package/dist/types/core/utils/deadline.d.ts.map +1 -1
- package/dist/types/core/utils/errors.d.ts +7 -1
- package/dist/types/core/utils/errors.d.ts.map +1 -1
- package/dist/types/operator/operator.d.ts.map +1 -1
- package/dist/types/operator/render.d.ts +6 -4
- package/dist/types/operator/render.d.ts.map +1 -1
- package/dist/types/protocol/auth.d.ts +54 -24
- package/dist/types/protocol/auth.d.ts.map +1 -1
- package/dist/types/protocol/clis.d.ts +14 -7
- package/dist/types/protocol/clis.d.ts.map +1 -1
- package/dist/types/protocol/config.d.ts +2 -0
- package/dist/types/protocol/config.d.ts.map +1 -1
- package/dist/types/protocol/conversation.d.ts +40 -3
- package/dist/types/protocol/conversation.d.ts.map +1 -1
- package/dist/types/protocol/credentials.d.ts +7 -0
- package/dist/types/protocol/credentials.d.ts.map +1 -1
- package/dist/types/protocol/execution.d.ts +2 -1
- package/dist/types/protocol/execution.d.ts.map +1 -1
- package/dist/types/protocol/factories.d.ts +12 -2
- package/dist/types/protocol/factories.d.ts.map +1 -1
- package/dist/types/protocol/harness.d.ts +6 -2
- package/dist/types/protocol/harness.d.ts.map +1 -1
- package/dist/types/protocol/history.d.ts +19 -0
- package/dist/types/protocol/history.d.ts.map +1 -1
- package/dist/types/protocol/lifecycle.d.ts +47 -8
- package/dist/types/protocol/lifecycle.d.ts.map +1 -1
- package/dist/types/protocol/model-catalog.d.ts.map +1 -1
- package/dist/types/protocol/models.d.ts +3 -0
- package/dist/types/protocol/models.d.ts.map +1 -1
- package/dist/types/protocol/parts.d.ts +14 -0
- package/dist/types/protocol/parts.d.ts.map +1 -1
- package/dist/types/protocol/tool-state.d.ts +12 -0
- package/dist/types/protocol/tool-state.d.ts.map +1 -1
- package/dist/types/protocol/wire-api.d.ts +129 -23
- package/dist/types/protocol/wire-api.d.ts.map +1 -1
- package/dist/types/protocol/wire.d.ts +70 -10
- package/dist/types/protocol/wire.d.ts.map +1 -1
- package/dist/types/server/acp/binding.d.ts +6 -1
- package/dist/types/server/acp/binding.d.ts.map +1 -1
- package/dist/types/server/acp/translate.d.ts +33 -7
- package/dist/types/server/acp/translate.d.ts.map +1 -1
- package/dist/types/server/agent-server.d.ts +6 -0
- package/dist/types/server/agent-server.d.ts.map +1 -1
- package/dist/types/server/feedback.d.ts +2 -1
- package/dist/types/server/feedback.d.ts.map +1 -1
- package/dist/types/server/install.d.ts +11 -0
- package/dist/types/server/install.d.ts.map +1 -1
- package/dist/types/server/session-gate.d.ts +2 -1
- package/dist/types/server/session-gate.d.ts.map +1 -1
- package/dist/types/server/session-store.d.ts +1 -0
- package/dist/types/server/session-store.d.ts.map +1 -1
- package/dist/types/server/subscriptions.d.ts +2 -1
- package/dist/types/server/subscriptions.d.ts.map +1 -1
- package/dist/types/server/transports.d.ts.map +1 -1
- package/dist/types/sync/fit.d.ts +8 -7
- package/dist/types/sync/fit.d.ts.map +1 -1
- package/dist/types/sync/history-sqlite.d.ts.map +1 -1
- package/dist/types/sync/index.d.ts +1 -0
- package/dist/types/sync/index.d.ts.map +1 -1
- package/dist/types/sync/journal-sqlite.d.ts +2 -1
- package/dist/types/sync/journal-sqlite.d.ts.map +1 -1
- package/dist/types/sync/journal.d.ts +11 -3
- package/dist/types/sync/journal.d.ts.map +1 -1
- package/dist/types/sync/projection.d.ts +6 -0
- package/dist/types/sync/projection.d.ts.map +1 -1
- package/dist/types/sync/recovery.d.ts +2 -1
- package/dist/types/sync/recovery.d.ts.map +1 -1
- package/dist/types/sync/schema.d.ts +10 -0
- package/dist/types/sync/schema.d.ts.map +1 -1
- package/dist/types/sync/sql.d.ts +2 -1
- package/dist/types/sync/sql.d.ts.map +1 -1
- package/docs/consuming.md +278 -28
- package/docs/deploy.md +36 -8
- package/docs/durable-sessions.md +35 -5
- package/docs/harnesses.md +385 -24
- package/package.json +6 -1
- package/src/accounts/chatgpt.ts +2 -2
- package/src/accounts/http.ts +2 -16
- package/src/client/client.ts +92 -31
- package/src/client/connection.ts +103 -22
- package/src/client/index.ts +1 -0
- package/src/client/logins.ts +1 -1
- package/src/client/streams.ts +12 -4
- package/src/client/turns.ts +1 -1
- package/src/core/agents/acp/acp-agent.ts +181 -52
- package/src/core/agents/acp/adapter.ts +9 -1
- package/src/core/agents/acp/client.ts +16 -4
- package/src/core/agents/acp/mappings.ts +27 -0
- package/src/core/agents/base.ts +25 -10
- package/src/core/agents/claude-code/adapter.ts +47 -17
- package/src/core/agents/claude-code/approvals.ts +6 -5
- package/src/core/agents/claude-code/claude-agent.ts +60 -30
- package/src/core/agents/claude-code/events.ts +11 -1
- package/src/core/agents/claude-code/login.ts +14 -2
- package/src/core/agents/claude-code/mcp.ts +31 -15
- package/src/core/agents/claude-code/models.ts +7 -2
- package/src/core/agents/claude-code/options.ts +11 -3
- package/src/core/agents/claude-code/outcome.ts +10 -2
- package/src/core/agents/claude-code/results.ts +73 -0
- package/src/core/agents/claude-code/sdk-pair.ts +66 -0
- package/src/core/agents/claude-code/session.ts +166 -37
- package/src/core/agents/claude-code/spend.ts +1 -1
- package/src/core/agents/claude-code/tools.ts +108 -12
- package/src/core/agents/codex-app-server/adapter.ts +37 -12
- package/src/core/agents/codex-app-server/client.ts +37 -8
- package/src/core/agents/codex-app-server/codex-agent.ts +115 -50
- package/src/core/agents/codex-app-server/commands.ts +2 -2
- package/src/core/agents/codex-app-server/credentials.ts +10 -5
- package/src/core/agents/codex-app-server/effort.ts +1 -1
- package/src/core/agents/codex-app-server/homes.ts +43 -1
- package/src/core/agents/codex-app-server/items.ts +219 -31
- package/src/core/agents/codex-app-server/login.ts +14 -2
- package/src/core/agents/codex-app-server/mcp.ts +47 -32
- package/src/core/agents/codex-app-server/models.ts +9 -8
- package/src/core/agents/codex-app-server/options.ts +69 -10
- package/src/core/agents/codex-app-server/plan-approval.ts +1 -1
- package/src/core/agents/codex-app-server/protocol.ts +14 -4
- package/src/core/agents/codex-app-server/requests.ts +14 -6
- package/src/core/agents/codex-app-server/session.ts +151 -113
- package/src/core/agents/codex-app-server/subagents.ts +21 -14
- package/src/core/agents/codex-app-server/thread-settings.ts +18 -8
- package/src/core/agents/codex-app-server/turn-items.ts +1 -1
- package/src/core/agents/codex-app-server/turn.ts +79 -32
- package/src/core/agents/codex-app-server/user-login.ts +107 -0
- package/src/core/agents/credentials.ts +5 -2
- package/src/core/agents/diffs.ts +25 -0
- package/src/core/agents/elicitation.ts +2 -2
- package/src/core/agents/login.ts +6 -0
- package/src/core/agents/mcp-config.ts +2 -2
- package/src/core/agents/process-pool.ts +432 -0
- package/src/core/agents/scripted/adapter.ts +142 -0
- package/src/core/agents/scripted/mcp-call.ts +134 -0
- package/src/core/agents/scripted/scripted-agent.ts +418 -0
- package/src/core/agents/scripted/scripts.ts +171 -0
- package/src/core/agents/scripted/steps.ts +127 -0
- package/src/core/agents/session-agent.ts +105 -8
- package/src/core/diagnostics.ts +9 -1
- package/src/core/index.ts +11 -0
- package/src/core/presets.ts +99 -14
- package/src/core/processes/lifeline.ts +99 -25
- package/src/core/provision/index.ts +1 -2
- package/src/core/provision/install-build.ts +4 -1
- package/src/core/provision/npm.ts +309 -97
- package/src/core/provision/provisioner.ts +22 -8
- package/src/core/provision/tools.ts +44 -19
- package/src/core/runtime/agent-runtime.ts +48 -18
- package/src/core/runtime/event-processor.ts +18 -2
- package/src/core/runtime/harness-clis.ts +36 -1
- package/src/core/runtime/harness-logins.ts +82 -31
- package/src/core/runtime/model-lists.ts +1 -1
- package/src/core/runtime/permission-policy.ts +4 -3
- package/src/core/runtime/request-broker.ts +1 -1
- package/src/core/runtime/turn.ts +51 -3
- package/src/core/utils/deadline.ts +23 -0
- package/src/core/utils/errors.ts +11 -4
- package/src/operator/operator.ts +8 -4
- package/src/operator/render.ts +46 -8
- package/src/protocol/auth.ts +30 -23
- package/src/protocol/clis.ts +12 -4
- package/src/protocol/credentials.ts +19 -0
- package/src/protocol/execution.ts +5 -0
- package/src/protocol/factories.ts +25 -3
- package/src/protocol/harness.ts +11 -1
- package/src/protocol/history.ts +20 -0
- package/src/protocol/lifecycle.ts +10 -1
- package/src/protocol/model-catalog.ts +19 -8
- package/src/protocol/parts.ts +12 -1
- package/src/protocol/tool-state.ts +14 -1
- package/src/protocol/wire.ts +14 -0
- package/src/server/acp/binding.ts +113 -23
- package/src/server/acp/translate.ts +161 -8
- package/src/server/agent-server.ts +10 -1
- package/src/server/bin.ts +83 -9
- package/src/server/feedback.ts +1 -1
- package/src/server/install.ts +82 -4
- package/src/server/session-gate.ts +1 -1
- package/src/server/session-store.ts +5 -1
- package/src/server/subscriptions.ts +1 -1
- package/src/server/transports.ts +14 -0
- package/src/sync/fit.ts +115 -28
- package/src/sync/history-sqlite.ts +6 -2
- package/src/sync/index.ts +1 -0
- package/src/sync/journal-sqlite.ts +20 -2
- package/src/sync/journal.ts +10 -2
- package/src/sync/projection.ts +10 -0
- package/src/sync/recovery.ts +11 -4
- package/src/sync/schema.ts +7 -5
- package/src/sync/sql.ts +1 -1
- package/dist/types/core/agents/claude-code/session-pool.d.ts +0 -36
- package/dist/types/core/agents/claude-code/session-pool.d.ts.map +0 -1
- package/dist/types/core/agents/session-pool.d.ts +0 -41
- package/dist/types/core/agents/session-pool.d.ts.map +0 -1
- package/src/core/agents/claude-code/session-pool.ts +0 -136
- package/src/core/agents/session-pool.ts +0 -111
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
3
|
+
import type { ScriptSource } from "./scripted-agent.ts";
|
|
4
|
+
import { type ReadStep, type ScriptStep, readStep } from "./steps.ts";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Steps played one turn at a time: `end` closes a turn, and the next plays on
|
|
8
|
+
* from the step after it.
|
|
9
|
+
*/
|
|
10
|
+
class Queue {
|
|
11
|
+
/** A turn is playing: another that starts now would take its steps. */
|
|
12
|
+
private playing = false;
|
|
13
|
+
private at: number;
|
|
14
|
+
private readonly saved: (at: number) => void;
|
|
15
|
+
private readonly kept?: string;
|
|
16
|
+
|
|
17
|
+
constructor(
|
|
18
|
+
private readonly steps: readonly ReadStep[],
|
|
19
|
+
private readonly name: string,
|
|
20
|
+
options: {
|
|
21
|
+
/** Where an earlier engine left it. */
|
|
22
|
+
at?: number;
|
|
23
|
+
/** Called with the position after each step. */
|
|
24
|
+
saved?: (at: number) => void;
|
|
25
|
+
/** Where that position is kept, said when the queue runs out. */
|
|
26
|
+
kept?: string;
|
|
27
|
+
} = {},
|
|
28
|
+
) {
|
|
29
|
+
const at = options.at ?? 0;
|
|
30
|
+
// An engine stopped inside a turn left that turn unended: it is skipped,
|
|
31
|
+
// as a turn that stops early is.
|
|
32
|
+
const inTurn = at > 0 && at < steps.length && !("end" in (steps[at - 1] as ReadStep));
|
|
33
|
+
this.at = inTurn ? this.throughEnd(at) : at;
|
|
34
|
+
this.saved = options.saved ?? (() => {});
|
|
35
|
+
this.kept = options.kept;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* One turn's steps. A turn that stops before its `end` (a `fail`, a
|
|
40
|
+
* cancel) skips the rest of its steps through that `end`, so the next turn
|
|
41
|
+
* plays its own. A turn that starts while another plays fails.
|
|
42
|
+
*/
|
|
43
|
+
*turn(): Generator<ReadStep> {
|
|
44
|
+
if (this.playing) {
|
|
45
|
+
throw new Error(
|
|
46
|
+
`A turn is already playing ${this.name}: sessions that run at once need scriptedBySession`,
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
this.playing = true;
|
|
50
|
+
let ended = false;
|
|
51
|
+
try {
|
|
52
|
+
if (this.at >= this.steps.length) {
|
|
53
|
+
ended = true;
|
|
54
|
+
const kept = this.kept ? `; its position is kept in ${this.kept}` : "";
|
|
55
|
+
throw new Error(`The script ran out: ${this.name} has no step left for this turn${kept}`);
|
|
56
|
+
}
|
|
57
|
+
while (this.at < this.steps.length) {
|
|
58
|
+
const step = this.steps[this.at] as ReadStep;
|
|
59
|
+
this.move(this.at + 1);
|
|
60
|
+
ended = "end" in step;
|
|
61
|
+
yield step;
|
|
62
|
+
if (ended) return;
|
|
63
|
+
}
|
|
64
|
+
ended = true;
|
|
65
|
+
} finally {
|
|
66
|
+
if (!ended) this.move(this.throughEnd(this.at));
|
|
67
|
+
this.playing = false;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** The position after the next `end` from `from`, or the queue's end. */
|
|
72
|
+
private throughEnd(from: number): number {
|
|
73
|
+
let at = from;
|
|
74
|
+
while (at < this.steps.length && !("end" in (this.steps[at] as ReadStep))) at++;
|
|
75
|
+
return Math.min(at + 1, this.steps.length);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
private move(at: number): void {
|
|
79
|
+
this.at = at;
|
|
80
|
+
this.saved(at);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const readAll = (steps: readonly ScriptStep[], name: string) =>
|
|
85
|
+
steps.map((step, i) => readStep(step, `Step ${i + 1} of ${name}`));
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* One queue of steps for the whole runtime, consumed turn by turn whatever
|
|
89
|
+
* the session, one turn at a time: `{ end: true }` closes a turn, and the next
|
|
90
|
+
* turn plays on from the step after it. A turn that finds the queue empty
|
|
91
|
+
* fails, saying the script ran out, and so does one that starts while another
|
|
92
|
+
* plays (`scriptedBySession` serves sessions that run at once). Every step is
|
|
93
|
+
* read here: one the schema does not know throws now.
|
|
94
|
+
*/
|
|
95
|
+
export function scriptedQueue(steps: readonly ScriptStep[]): ScriptSource {
|
|
96
|
+
const queue = new Queue(readAll(steps, "the script"), "the script");
|
|
97
|
+
return () => queue.turn();
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* `scriptedQueue`, read from a JSONL file: one step per line (blank lines
|
|
102
|
+
* skipped), a bad line refused by its number. It plays from its start each
|
|
103
|
+
* time an engine reads it and writes nothing, unless `position` names a file
|
|
104
|
+
* that keeps how far it has played: then an engine restarted mid-script goes
|
|
105
|
+
* on where the last one stopped (inside a turn, from the next turn), and a
|
|
106
|
+
* changed script plays from its start.
|
|
107
|
+
*/
|
|
108
|
+
export function scriptedFile(path: string, options: { position?: string } = {}): ScriptSource {
|
|
109
|
+
const text = readFileSync(path, "utf8");
|
|
110
|
+
const steps = text.split("\n").flatMap((line, i): ReadStep[] => {
|
|
111
|
+
if (!line.trim()) return [];
|
|
112
|
+
const where = `Line ${i + 1} of ${path}`;
|
|
113
|
+
let value: unknown;
|
|
114
|
+
try {
|
|
115
|
+
value = JSON.parse(line);
|
|
116
|
+
} catch {
|
|
117
|
+
throw new Error(`${where} is not JSON: ${line.slice(0, 200)}`);
|
|
118
|
+
}
|
|
119
|
+
return [readStep(value, where)];
|
|
120
|
+
});
|
|
121
|
+
const { position } = options;
|
|
122
|
+
if (!position) {
|
|
123
|
+
const queue = new Queue(steps, path);
|
|
124
|
+
return () => queue.turn();
|
|
125
|
+
}
|
|
126
|
+
const sha256 = createHash("sha256").update(text).digest("hex");
|
|
127
|
+
const queue = new Queue(steps, path, {
|
|
128
|
+
at: playedSoFar(position, sha256, steps.length),
|
|
129
|
+
saved: (at) => writeFileSync(position, `${JSON.stringify({ sha256, at })}\n`),
|
|
130
|
+
kept: position,
|
|
131
|
+
});
|
|
132
|
+
return () => queue.turn();
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** Where an earlier engine left this script, or its start. */
|
|
136
|
+
function playedSoFar(file: string, sha256: string, length: number): number {
|
|
137
|
+
if (!existsSync(file)) return 0;
|
|
138
|
+
try {
|
|
139
|
+
const saved = JSON.parse(readFileSync(file, "utf8")) as { sha256?: unknown; at?: unknown };
|
|
140
|
+
const at = saved.at;
|
|
141
|
+
return saved.sha256 === sha256 && Number.isInteger(at) ? Math.min(at as number, length) : 0;
|
|
142
|
+
} catch {
|
|
143
|
+
return 0;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* A queue per session id, as `scriptedQueue` plays one; a session not named
|
|
149
|
+
* plays `otherwise` from its start, in a queue of its own. A session with
|
|
150
|
+
* neither fails its turn.
|
|
151
|
+
*/
|
|
152
|
+
export function scriptedBySession(
|
|
153
|
+
scripts: Readonly<Record<string, readonly ScriptStep[]>>,
|
|
154
|
+
otherwise?: readonly ScriptStep[],
|
|
155
|
+
): ScriptSource {
|
|
156
|
+
const named = new Map(
|
|
157
|
+
Object.entries(scripts).map(([id, steps]) => [id, readAll(steps, `session ${id}'s script`)]),
|
|
158
|
+
);
|
|
159
|
+
const fallback = otherwise && readAll(otherwise, "the default script");
|
|
160
|
+
const queues = new Map<string, Queue>();
|
|
161
|
+
return ({ sessionId }) => {
|
|
162
|
+
let queue = queues.get(sessionId);
|
|
163
|
+
if (!queue) {
|
|
164
|
+
const steps = named.get(sessionId) ?? fallback;
|
|
165
|
+
if (!steps) throw new Error(`No script for session ${sessionId}, and no default`);
|
|
166
|
+
queue = new Queue(steps, `session ${sessionId}'s script`);
|
|
167
|
+
queues.set(sessionId, queue);
|
|
168
|
+
}
|
|
169
|
+
return queue.turn();
|
|
170
|
+
};
|
|
171
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import {
|
|
3
|
+
type ElicitationOutcome,
|
|
4
|
+
ElicitationQuestionSchema,
|
|
5
|
+
ErrorCategorySchema,
|
|
6
|
+
StopReasonSchema,
|
|
7
|
+
TokenUsageSchema,
|
|
8
|
+
ToolKindSchema,
|
|
9
|
+
type ToolResultContent,
|
|
10
|
+
ToolResultContentSchema,
|
|
11
|
+
} from "../../../protocol/index.ts";
|
|
12
|
+
|
|
13
|
+
const Input = z.record(z.string(), z.unknown());
|
|
14
|
+
const Ms = z.number().int().nonnegative();
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* One step of a script: plain JSON, so a JSONL file is a script. Each form is
|
|
18
|
+
* strict, so a misspelt step or field is refused when the script is read,
|
|
19
|
+
* never skipped.
|
|
20
|
+
*/
|
|
21
|
+
export const ScriptStepSchema = z.union([
|
|
22
|
+
/** A complete text part. */
|
|
23
|
+
z.strictObject({ say: z.string() }),
|
|
24
|
+
/** A text part streamed a word at a time, `every` ms apart (0 by default), then its snapshot. */
|
|
25
|
+
z.strictObject({ stream: z.string(), every: Ms.optional() }),
|
|
26
|
+
/**
|
|
27
|
+
* A tool call the script answers itself: the part opens in progress and
|
|
28
|
+
* completes with `output` (empty by default), or fails with `error`. With
|
|
29
|
+
* `ask`, the engine's permission broker is asked first; a rejection fails
|
|
30
|
+
* the part as a rejected call and the turn goes on.
|
|
31
|
+
*
|
|
32
|
+
* What it acts on (`command`, `url`, `locations`) is on the part and on its
|
|
33
|
+
* request; `reason`, why it asks, is on the request only, so it needs
|
|
34
|
+
* `ask`. Its result's `metadata` (a command's `exitCode`) is kept completed
|
|
35
|
+
* or failed; its `content` (a `diff`) only when it completes, as a failed
|
|
36
|
+
* call shows nothing (LLP 0002, A tool call says what it acts on).
|
|
37
|
+
*/
|
|
38
|
+
z
|
|
39
|
+
.strictObject({
|
|
40
|
+
tool: z.string().min(1),
|
|
41
|
+
input: Input.optional(),
|
|
42
|
+
kind: ToolKindSchema.optional(),
|
|
43
|
+
mcp: z.strictObject({ server: z.string(), tool: z.string() }).optional(),
|
|
44
|
+
command: z.string().optional(),
|
|
45
|
+
url: z.string().optional(),
|
|
46
|
+
locations: z
|
|
47
|
+
.array(z.strictObject({ path: z.string(), line: z.number().optional() }))
|
|
48
|
+
.optional(),
|
|
49
|
+
reason: z.string().optional(),
|
|
50
|
+
output: z.string().optional(),
|
|
51
|
+
error: z.string().optional(),
|
|
52
|
+
content: z.array(ToolResultContentSchema).optional(),
|
|
53
|
+
metadata: z.record(z.string(), z.unknown()).optional(),
|
|
54
|
+
ask: z.literal(true).optional(),
|
|
55
|
+
})
|
|
56
|
+
.refine((step) => step.reason === undefined || step.ask, {
|
|
57
|
+
message: "`reason` is said on a permission request: it needs `ask`",
|
|
58
|
+
}),
|
|
59
|
+
/**
|
|
60
|
+
* A real call to a tool of the turn's own MCP server `server` (an `http`
|
|
61
|
+
* one), its result in the tool part. With `ask`, as for `tool`.
|
|
62
|
+
*/
|
|
63
|
+
z.strictObject({
|
|
64
|
+
call: z.strictObject({ server: z.string(), tool: z.string(), arguments: Input.optional() }),
|
|
65
|
+
ask: z.literal(true).optional(),
|
|
66
|
+
}),
|
|
67
|
+
/** Questions to the person through the engine's question broker; the answer is said back as JSON. */
|
|
68
|
+
z.strictObject({ ask: z.array(ElicitationQuestionSchema).min(1) }),
|
|
69
|
+
/** Says the turn's input as the model would read it, or the turn's config as JSON. */
|
|
70
|
+
z.strictObject({ echo: z.enum(["input", "config"]) }),
|
|
71
|
+
/** A pause of this many ms. */
|
|
72
|
+
z.strictObject({ wait: Ms }),
|
|
73
|
+
/** Runs until the turn is cancelled, then `stopTakes` ms more before it stops. */
|
|
74
|
+
z.strictObject({ hang: z.literal(true), stopTakes: Ms.optional() }),
|
|
75
|
+
/**
|
|
76
|
+
* Ends the turn in an error with this message: of `category` when given,
|
|
77
|
+
* else of whatever the engine's classifier makes of the text.
|
|
78
|
+
*/
|
|
79
|
+
z.strictObject({ fail: z.string(), category: ErrorCategorySchema.optional() }),
|
|
80
|
+
/**
|
|
81
|
+
* Ends the turn, `end_turn` with zero usage by default. In a queue of
|
|
82
|
+
* steps it is the turn's boundary: the next turn plays from the step after.
|
|
83
|
+
*/
|
|
84
|
+
z.strictObject({
|
|
85
|
+
end: z.literal(true),
|
|
86
|
+
stopReason: StopReasonSchema.optional(),
|
|
87
|
+
usage: TokenUsageSchema.optional(),
|
|
88
|
+
cost: z.number().nonnegative().optional(),
|
|
89
|
+
}),
|
|
90
|
+
]);
|
|
91
|
+
|
|
92
|
+
/** One step of a script, as a script writes it (see `ScriptStepSchema`). */
|
|
93
|
+
export type ScriptStep = z.input<typeof ScriptStepSchema>;
|
|
94
|
+
/**
|
|
95
|
+
* What a step did, handed back to the script as the value of its `yield`
|
|
96
|
+
* (the agent passes it to the next `next(outcome)`): a script written as a
|
|
97
|
+
* generator reads it; an array or a queue ignores it.
|
|
98
|
+
* - a `tool` or `call` step that completed: what its part completed with;
|
|
99
|
+
* - one that failed: its `error` and `metadata`, with `rejected` when the
|
|
100
|
+
* person denied it;
|
|
101
|
+
* - an `ask` step: the person's `answer`; `undefined` when the turn's client
|
|
102
|
+
* answers no questions, as for every other step.
|
|
103
|
+
* A permission request answered as a cancel ends the turn: nothing is handed back.
|
|
104
|
+
*/
|
|
105
|
+
export type ScriptStepOutcome =
|
|
106
|
+
| {
|
|
107
|
+
output: string;
|
|
108
|
+
content?: ToolResultContent[];
|
|
109
|
+
/** A `call`'s: the server's `structuredContent`, as its part has it. */
|
|
110
|
+
metadata?: Record<string, unknown> & { structuredContent?: Record<string, unknown> };
|
|
111
|
+
}
|
|
112
|
+
| { error: string; rejected?: true; metadata?: Record<string, unknown> }
|
|
113
|
+
| { answer: ElicitationOutcome }
|
|
114
|
+
| undefined;
|
|
115
|
+
|
|
116
|
+
/** A step once read: defaults filled in. */
|
|
117
|
+
export type ReadStep = z.output<typeof ScriptStepSchema>;
|
|
118
|
+
|
|
119
|
+
/** A step, read, or an error that says where it is and what is wrong with it. */
|
|
120
|
+
export function readStep(step: unknown, where: string): ReadStep {
|
|
121
|
+
const parsed = ScriptStepSchema.safeParse(step);
|
|
122
|
+
if (parsed.success) return parsed.data;
|
|
123
|
+
const shown = JSON.stringify(step) ?? String(step);
|
|
124
|
+
throw new Error(
|
|
125
|
+
`${where} is not a script step: ${shown.length > 200 ? `${shown.slice(0, 200)}…` : shown}`,
|
|
126
|
+
);
|
|
127
|
+
}
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import type { AgentInput } from "../../protocol/index.ts";
|
|
2
|
+
import { withTimeout } from "../utils/deadline.ts";
|
|
2
3
|
import { SessionResumeError, TurnActiveError } from "../utils/errors.ts";
|
|
3
4
|
import {
|
|
4
5
|
type AgentExecuteOptions,
|
|
5
6
|
BaseAgent,
|
|
7
|
+
CANCEL_CONFIRM_MS,
|
|
8
|
+
type CancelResult,
|
|
6
9
|
type RawAgentEvent,
|
|
7
10
|
type SteerOutcome,
|
|
8
11
|
type SteerRequest,
|
|
@@ -22,20 +25,59 @@ interface TurnSteering {
|
|
|
22
25
|
}
|
|
23
26
|
|
|
24
27
|
/**
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
* to
|
|
28
|
+
* How a running turn stops for a cancel: it sends the turn's native stop, if
|
|
29
|
+
* the turn's abort has not already, and resolves whether the agent confirmed
|
|
30
|
+
* that the turn stopped. It must be safe to call again: a cancel after one
|
|
31
|
+
* that was not confirmed calls it again (Claude then sends a new interrupt;
|
|
32
|
+
* Codex answers its one acknowledgement, ACP waits for its one prompt).
|
|
33
|
+
*/
|
|
34
|
+
export type StopTurn = () => Promise<boolean>;
|
|
35
|
+
|
|
36
|
+
/** A turn executing on its session: what a cancel of it waits for. */
|
|
37
|
+
interface RunningTurn {
|
|
38
|
+
turnId: string;
|
|
39
|
+
/** Settles once the turn's execution has ended, cleanup included. */
|
|
40
|
+
ended: Promise<void>;
|
|
41
|
+
/** The harness's stop, once the turn has one (`cancelWith`). */
|
|
42
|
+
stop?: StopTurn;
|
|
43
|
+
/** The answer of a cancel still in flight, which a cancel meanwhile shares. */
|
|
44
|
+
cancelling?: Promise<boolean>;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface SessionAgentOptions {
|
|
48
|
+
/**
|
|
49
|
+
* How long a cancel waits for the agent to confirm its turn stopped before
|
|
50
|
+
* it answers `confirmed: false` (`CANCEL_CONFIRM_MS` by default).
|
|
51
|
+
*/
|
|
52
|
+
cancelConfirmMs?: number;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* The execution lifecycle the engine's own harnesses extend: one turn owns a
|
|
57
|
+
* logical session from preparation through native cleanup, with steering,
|
|
58
|
+
* the conversation it resumes and the answer to its cancel. It is not
|
|
59
|
+
* exported while its design is a Draft (LLP 0016); a product's own harness
|
|
60
|
+
* extends `BaseAgent`, the public base.
|
|
28
61
|
*/
|
|
29
62
|
export abstract class SessionAgent extends BaseAgent {
|
|
30
63
|
private terminated = false;
|
|
31
|
-
/**
|
|
32
|
-
|
|
64
|
+
/**
|
|
65
|
+
* The turn each session executes. Cancel/release can return before
|
|
66
|
+
* execution drains; only finally relinquishes ownership.
|
|
67
|
+
*/
|
|
68
|
+
private readonly running = new Map<string, RunningTurn>();
|
|
33
69
|
/** Conversation identity outlives subprocess eviction and failed startup. */
|
|
34
70
|
private readonly conversations = new Map<string, { id: string | undefined; boundary: string }>();
|
|
35
71
|
/** The running turn's input, per session. */
|
|
36
72
|
private readonly steering = new Map<string, TurnSteering>();
|
|
37
73
|
/** The last turn each session executed: a steer for it comes too late. */
|
|
38
74
|
private readonly executed = new Map<string, string>();
|
|
75
|
+
protected readonly cancelConfirmMs: number;
|
|
76
|
+
|
|
77
|
+
constructor(options: SessionAgentOptions = {}) {
|
|
78
|
+
super();
|
|
79
|
+
this.cancelConfirmMs = options.cancelConfirmMs ?? CANCEL_CONFIRM_MS;
|
|
80
|
+
}
|
|
39
81
|
|
|
40
82
|
override async *execute(
|
|
41
83
|
input: AgentInput,
|
|
@@ -46,8 +88,12 @@ export abstract class SessionAgent extends BaseAgent {
|
|
|
46
88
|
if (options.subagentMode && !this.capabilities.turnScopedSubagents) {
|
|
47
89
|
throw new Error(`${this.harness} does not support turn-scoped subagents`);
|
|
48
90
|
}
|
|
49
|
-
if (this.
|
|
50
|
-
|
|
91
|
+
if (this.running.has(sessionId)) throw new TurnActiveError(sessionId);
|
|
92
|
+
let end = () => {};
|
|
93
|
+
const ended = new Promise<void>((resolve) => {
|
|
94
|
+
end = resolve;
|
|
95
|
+
});
|
|
96
|
+
this.running.set(sessionId, { turnId: options.turnId, ended });
|
|
51
97
|
const controller = this.trackTurn(sessionId, options.signal);
|
|
52
98
|
// Steers sent since the runtime admitted this turn wait in its record.
|
|
53
99
|
const waiting = this.steering.get(sessionId);
|
|
@@ -66,10 +112,61 @@ export abstract class SessionAgent extends BaseAgent {
|
|
|
66
112
|
this.executed.set(sessionId, options.turnId);
|
|
67
113
|
if (this.steering.get(sessionId) === steering) this.steering.delete(sessionId);
|
|
68
114
|
this.endTurn(sessionId, controller);
|
|
69
|
-
this.
|
|
115
|
+
this.running.delete(sessionId);
|
|
116
|
+
end();
|
|
70
117
|
}
|
|
71
118
|
}
|
|
72
119
|
|
|
120
|
+
// @ref LLP 0016.000#the-cancel-answer — one cancel answer, the harness says how its turn stops
|
|
121
|
+
/**
|
|
122
|
+
* Abort the session's turn, and answer whether it stopped within
|
|
123
|
+
* `cancelConfirmMs`: as its stop says (`cancelWith`), or, while it has
|
|
124
|
+
* none, by its end. A cancel while another is in flight shares its answer;
|
|
125
|
+
* one after it asks again, with a window of its own. With no turn, what
|
|
126
|
+
* the harness still has to stop (`stopWithoutTurn`) decides; with nothing
|
|
127
|
+
* to stop it is confirmed.
|
|
128
|
+
*/
|
|
129
|
+
override async cancel(sessionId: string): Promise<CancelResult> {
|
|
130
|
+
const turn = this.running.get(sessionId);
|
|
131
|
+
await super.cancel(sessionId);
|
|
132
|
+
if (!turn) {
|
|
133
|
+
const stopping = this.stopWithoutTurn(sessionId);
|
|
134
|
+
return { confirmed: stopping ? await this.confirmed(stopping) : true, hadTurn: false };
|
|
135
|
+
}
|
|
136
|
+
turn.cancelling ??= this.confirmed(
|
|
137
|
+
turn.stop ? turn.stop() : turn.ended.then(() => true),
|
|
138
|
+
).finally(() => {
|
|
139
|
+
turn.cancelling = undefined;
|
|
140
|
+
});
|
|
141
|
+
return { confirmed: await turn.cancelling, hadTurn: true };
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* How the turn stops for a cancel (`StopTurn`), from when it has a native
|
|
146
|
+
* stop to send. A cancel calls it once it aborted the turn; until a turn
|
|
147
|
+
* registers one, its cancel is confirmed by the turn's end.
|
|
148
|
+
*/
|
|
149
|
+
protected cancelWith(options: AgentExecuteOptions, stop: StopTurn): void {
|
|
150
|
+
const turn = this.running.get(options.sessionId);
|
|
151
|
+
if (turn?.turnId === options.turnId) turn.stop = stop;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* What a cancel with no turn running still has to stop, if anything:
|
|
156
|
+
* whether that stopped. Nothing by default.
|
|
157
|
+
*/
|
|
158
|
+
protected stopWithoutTurn(_sessionId: string): Promise<boolean> | undefined {
|
|
159
|
+
return undefined;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
private async confirmed(stopped: Promise<boolean>): Promise<boolean> {
|
|
163
|
+
const answer = await withTimeout(
|
|
164
|
+
stopped.catch(() => false),
|
|
165
|
+
this.cancelConfirmMs,
|
|
166
|
+
);
|
|
167
|
+
return answer?.value === true;
|
|
168
|
+
}
|
|
169
|
+
|
|
73
170
|
// @ref LLP 0001#d13--input-while-a-turn-runs-steers-it — held until the native turn takes it
|
|
74
171
|
/**
|
|
75
172
|
* Steer the session's turn. The runtime steers only a turn it admitted, so
|
package/src/core/diagnostics.ts
CHANGED
|
@@ -15,8 +15,16 @@
|
|
|
15
15
|
* sink and the turn continued.
|
|
16
16
|
* - `proxyUpstreamAuth` — the BYOK proxy's upstream rejected the real key
|
|
17
17
|
* (401/403): the stored key is invalid/expired, not the placeholder.
|
|
18
|
+
* - `claudeSdkMismatch` — the Claude SDK that loaded and the CLI build it
|
|
19
|
+
* drives are different releases (`detail: {sdkVersion, cliVersion,
|
|
20
|
+
* cliPath}`); said once per process for each pair, and the turn runs.
|
|
18
21
|
*/
|
|
19
|
-
export type DiagnosticKind =
|
|
22
|
+
export type DiagnosticKind =
|
|
23
|
+
| "interruptTimeout"
|
|
24
|
+
| "sinkError"
|
|
25
|
+
| "proxyUpstreamAuth"
|
|
26
|
+
| "claudeSdkMismatch"
|
|
27
|
+
| (string & {});
|
|
20
28
|
|
|
21
29
|
export interface EngineDiagnostic {
|
|
22
30
|
type: DiagnosticKind;
|
package/src/core/index.ts
CHANGED
|
@@ -97,6 +97,7 @@ export {
|
|
|
97
97
|
CodexAppServerAgent,
|
|
98
98
|
type CodexAppServerAgentOptions,
|
|
99
99
|
} from "./agents/codex-app-server/codex-agent.ts";
|
|
100
|
+
export type { CodexBuiltInTools } from "./agents/codex-app-server/options.ts";
|
|
100
101
|
export { createCodexAppServerTransformer } from "./agents/codex-app-server/adapter.ts";
|
|
101
102
|
export type { CodexConnection } from "./agents/codex-app-server/client.ts";
|
|
102
103
|
export type { CodexNotification } from "./agents/codex-app-server/protocol.ts";
|
|
@@ -106,6 +107,16 @@ export {
|
|
|
106
107
|
} from "./agents/acp/acp-agent.ts";
|
|
107
108
|
export { createAcpTransformer } from "./agents/acp/adapter.ts";
|
|
108
109
|
export type { AcpClientOptions, AcpLaunch } from "./agents/acp/client.ts";
|
|
110
|
+
export {
|
|
111
|
+
ScriptedAgent,
|
|
112
|
+
type ScriptedAgentOptions,
|
|
113
|
+
type ScriptedTurnConfig,
|
|
114
|
+
type ScriptedTurnRequest,
|
|
115
|
+
type ScriptSource,
|
|
116
|
+
} from "./agents/scripted/scripted-agent.ts";
|
|
117
|
+
export { type ScriptedEvent, createScriptedTransformer } from "./agents/scripted/adapter.ts";
|
|
118
|
+
export type { ScriptStep, ScriptStepOutcome } from "./agents/scripted/steps.ts";
|
|
119
|
+
export { scriptedBySession, scriptedFile, scriptedQueue } from "./agents/scripted/scripts.ts";
|
|
109
120
|
export {
|
|
110
121
|
KNOWN_ACP_AGENTS,
|
|
111
122
|
resolveAcpLaunch,
|
package/src/core/presets.ts
CHANGED
|
@@ -13,6 +13,8 @@ import {
|
|
|
13
13
|
import type { CredentialSource } from "./agents/credentials.ts";
|
|
14
14
|
import type { InheritConfig } from "./agents/inherit-config.ts";
|
|
15
15
|
import { AgentRegistry } from "./agents/registry.ts";
|
|
16
|
+
import { createScriptedTransformer } from "./agents/scripted/adapter.ts";
|
|
17
|
+
import { ScriptedAgent, type ScriptedAgentOptions } from "./agents/scripted/scripted-agent.ts";
|
|
16
18
|
import type { AdapterFactory } from "./agents/types.ts";
|
|
17
19
|
import type { DiagnosticHandler } from "./diagnostics.ts";
|
|
18
20
|
import { type NativeProcessOptions, NativeProcesses } from "./processes/native-processes.ts";
|
|
@@ -20,11 +22,22 @@ import { CliProvisioner, type ProvisionOptions } from "./provision/provisioner.t
|
|
|
20
22
|
import type { CliTool } from "./provision/tools.ts";
|
|
21
23
|
import { AgentRuntime } from "./runtime/agent-runtime.ts";
|
|
22
24
|
|
|
25
|
+
// @ref LLP 0016.000#options-every-harness-shares — an option every harness shares is said once
|
|
26
|
+
/** What the registry gives every harness that has the notion, and takes from none of their options. */
|
|
27
|
+
type SaidOnce =
|
|
28
|
+
| "processes"
|
|
29
|
+
| "resolveCliPath"
|
|
30
|
+
| "inheritConfig"
|
|
31
|
+
| "idleTimeoutMs"
|
|
32
|
+
| "credentials"
|
|
33
|
+
| "cancelConfirmMs"
|
|
34
|
+
| "cancelGraceMs";
|
|
35
|
+
|
|
23
36
|
export interface CreateRegistryOptions {
|
|
24
37
|
/**
|
|
25
38
|
* Which harnesses to register. Defaults to `claude-code` and
|
|
26
39
|
* `codex-app-server`, plus `acp` when `acp` config is provided (it has no
|
|
27
|
-
* default launch).
|
|
40
|
+
* default launch), and `scripted` when `scripted` is (it has no script).
|
|
28
41
|
*/
|
|
29
42
|
harnesses?: AgentHarness[];
|
|
30
43
|
/**
|
|
@@ -36,27 +49,46 @@ export interface CreateRegistryOptions {
|
|
|
36
49
|
* ACP harness configuration. Required to enable `acp`: the harness needs an
|
|
37
50
|
* operator-supplied launch command for the target agent (never wire-driven).
|
|
38
51
|
*/
|
|
39
|
-
acp?: Omit<AcpAgentOptions,
|
|
52
|
+
acp?: Omit<AcpAgentOptions, SaidOnce>;
|
|
53
|
+
/**
|
|
54
|
+
* The scripted harness's script (LLP 0016, The scripted harness): steps
|
|
55
|
+
* played instead of a model, under the harness's own name `scripted`.
|
|
56
|
+
* Required to enable it, so no engine answers from a script it was not given.
|
|
57
|
+
*/
|
|
58
|
+
scripted?: Omit<ScriptedAgentOptions, "processes">;
|
|
40
59
|
/**
|
|
41
60
|
* Claude harness embed-tier options (e.g. in-process `sdkMcpServers`).
|
|
42
61
|
* `resolveCliPath` stays provisioner-wired here.
|
|
43
62
|
*/
|
|
44
|
-
claudeCode?: Omit<ClaudeCodeAgentOptions,
|
|
63
|
+
claudeCode?: Omit<ClaudeCodeAgentOptions, SaidOnce>;
|
|
45
64
|
/**
|
|
46
65
|
* Credentials the product holds (LLP 0012), asked before each turn of
|
|
47
66
|
* every harness that takes them, and again with a token the harness
|
|
48
67
|
* refused: Claude Code runs on them through the engine's own proxy, Codex
|
|
49
68
|
* holds them in memory. `undefined` from it runs a turn on the CLI's own
|
|
50
|
-
* login. `credentialsFromUrl` asks a product's credential endpoint.
|
|
51
|
-
* harness's own `credentials` win.
|
|
69
|
+
* login. `credentialsFromUrl` asks a product's credential endpoint.
|
|
52
70
|
* Operator-level only.
|
|
53
71
|
*/
|
|
54
72
|
credentials?: CredentialSource;
|
|
73
|
+
/**
|
|
74
|
+
* How long a session's agent process may wait unused between turns before
|
|
75
|
+
* it is ended (5 minutes by default); 0 never ends one for idleness, and
|
|
76
|
+
* one that holds the only copy of its conversation is never ended for it.
|
|
77
|
+
*/
|
|
78
|
+
idleTimeoutMs?: number;
|
|
79
|
+
/**
|
|
80
|
+
* How long a cancel waits for the agent to confirm its turn stopped before
|
|
81
|
+
* it answers `confirmed: false` (`CANCEL_CONFIRM_MS`, 2 s, by default).
|
|
82
|
+
*/
|
|
83
|
+
cancelConfirmMs?: number;
|
|
84
|
+
/**
|
|
85
|
+
* How long after a cancel the engine ends an agent that has not stopped:
|
|
86
|
+
* 2 s on Codex and 10 s on an ACP agent by default. Claude is not ended
|
|
87
|
+
* for it (LLP 0016, decision 11).
|
|
88
|
+
*/
|
|
89
|
+
cancelGraceMs?: number;
|
|
55
90
|
/** Codex embed-tier authentication and process options; CLI resolution stays provisioner-owned. */
|
|
56
|
-
codexAppServer?: Omit<
|
|
57
|
-
CodexAppServerAgentOptions,
|
|
58
|
-
"resolveCliPath" | "processes" | "inheritConfig"
|
|
59
|
-
>;
|
|
91
|
+
codexAppServer?: Omit<CodexAppServerAgentOptions, SaidOnce>;
|
|
60
92
|
/**
|
|
61
93
|
* What ambient configuration a turn loads by itself when it starts
|
|
62
94
|
* (LLP 0001, D17): `all` (the default) the host user's and the
|
|
@@ -106,8 +138,10 @@ const HARNESSES: Record<
|
|
|
106
138
|
"claude-code": (opts, cliPath, processes) => [
|
|
107
139
|
new ClaudeCodeAgent({
|
|
108
140
|
onDiagnostic: opts.onDiagnostic,
|
|
109
|
-
...(opts.credentials && { credentials: opts.credentials }),
|
|
110
141
|
...opts.claudeCode,
|
|
142
|
+
credentials: opts.credentials,
|
|
143
|
+
idleTimeoutMs: opts.idleTimeoutMs,
|
|
144
|
+
cancelConfirmMs: opts.cancelConfirmMs,
|
|
111
145
|
inheritConfig: opts.inheritConfig,
|
|
112
146
|
resolveCliPath: cliPath("claude"),
|
|
113
147
|
processes,
|
|
@@ -116,8 +150,11 @@ const HARNESSES: Record<
|
|
|
116
150
|
],
|
|
117
151
|
"codex-app-server": (opts, cliPath, processes) => [
|
|
118
152
|
new CodexAppServerAgent({
|
|
119
|
-
...(opts.credentials && { credentials: opts.credentials }),
|
|
120
153
|
...opts.codexAppServer,
|
|
154
|
+
credentials: opts.credentials,
|
|
155
|
+
idleTimeoutMs: opts.idleTimeoutMs,
|
|
156
|
+
cancelConfirmMs: opts.cancelConfirmMs,
|
|
157
|
+
cancelGraceMs: opts.cancelGraceMs,
|
|
121
158
|
inheritConfig: opts.inheritConfig,
|
|
122
159
|
resolveCliPath: cliPath("codex"),
|
|
123
160
|
processes,
|
|
@@ -128,16 +165,64 @@ const HARNESSES: Record<
|
|
|
128
165
|
if (!opts.acp) {
|
|
129
166
|
throw new Error("the acp harness requires CreateRegistryOptions.acp (resolveLaunch)");
|
|
130
167
|
}
|
|
131
|
-
|
|
168
|
+
const { idleTimeoutMs, cancelConfirmMs, cancelGraceMs } = opts;
|
|
169
|
+
return [
|
|
170
|
+
new AcpAgent({ ...opts.acp, idleTimeoutMs, cancelConfirmMs, cancelGraceMs, processes }),
|
|
171
|
+
createAcpTransformer,
|
|
172
|
+
];
|
|
173
|
+
},
|
|
174
|
+
scripted: (opts, _cliPath, processes) => {
|
|
175
|
+
if (!opts.scripted) {
|
|
176
|
+
throw new Error("the scripted harness requires CreateRegistryOptions.scripted (turn)");
|
|
177
|
+
}
|
|
178
|
+
// @ref LLP 0007#what-each-harness-does — it starts none, so a restart vouches for it
|
|
179
|
+
return [new ScriptedAgent({ ...opts.scripted, processes }), createScriptedTransformer];
|
|
132
180
|
},
|
|
133
181
|
};
|
|
134
182
|
|
|
183
|
+
const atTheTop = (option: string) => `set ${option} at the top level of the registry's options`;
|
|
184
|
+
|
|
185
|
+
/** Each option the registry says once, and where a caller says it instead. */
|
|
186
|
+
const SAID_ONCE: Record<SaidOnce, string> = {
|
|
187
|
+
idleTimeoutMs: atTheTop("idleTimeoutMs"),
|
|
188
|
+
credentials: atTheTop("credentials"),
|
|
189
|
+
cancelConfirmMs: atTheTop("cancelConfirmMs"),
|
|
190
|
+
cancelGraceMs: atTheTop("cancelGraceMs"),
|
|
191
|
+
inheritConfig: atTheTop("inheritConfig"),
|
|
192
|
+
processes:
|
|
193
|
+
"the registry gives every harness its processes; say how they are owned with nativeProcesses",
|
|
194
|
+
resolveCliPath: "the registry's provisioner finds each CLI; name one with provision.overrides",
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Refuse an option the registry says once that a caller the compiler does
|
|
199
|
+
* not check still sets on a harness, where the registry would override it.
|
|
200
|
+
* The scripted harness takes only `processes` of them.
|
|
201
|
+
*/
|
|
202
|
+
function refuseSaidOnce(opts: CreateRegistryOptions): void {
|
|
203
|
+
for (const harness of ["claudeCode", "codexAppServer", "acp", "scripted"] as const) {
|
|
204
|
+
const own: Record<string, unknown> = opts[harness] ?? {};
|
|
205
|
+
const options = harness === "scripted" ? ["processes" as const] : Object.keys(SAID_ONCE);
|
|
206
|
+
const option = (options as SaidOnce[]).find((name) => own[name] !== undefined);
|
|
207
|
+
if (option)
|
|
208
|
+
throw new Error(
|
|
209
|
+
`${harness}.${option} is not taken: ${SAID_ONCE[option]} (LLP 0016, decision 5)`,
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
135
214
|
/** Build a registry with the standard harnesses wired to their adapters. */
|
|
136
215
|
export function createAgentRegistry(opts: CreateRegistryOptions = {}): AgentRegistry {
|
|
216
|
+
refuseSaidOnce(opts);
|
|
137
217
|
// Default: every harness that can run unconfigured — acp joins only when its
|
|
138
|
-
// operator launch config is present
|
|
218
|
+
// operator launch config is present, scripted only when its script is
|
|
219
|
+
// (each has nothing to run otherwise).
|
|
220
|
+
const configured: Partial<Record<AgentHarness, boolean>> = {
|
|
221
|
+
acp: Boolean(opts.acp),
|
|
222
|
+
scripted: Boolean(opts.scripted),
|
|
223
|
+
};
|
|
139
224
|
const enabled = new Set<AgentHarness>(
|
|
140
|
-
opts.harnesses ?? AGENT_HARNESSES.filter((h) => h
|
|
225
|
+
opts.harnesses ?? AGENT_HARNESSES.filter((h) => configured[h] ?? true),
|
|
141
226
|
);
|
|
142
227
|
// One provisioner per registry: resolution is memoized per tool, so every
|
|
143
228
|
// agent of a tool shares a single download and logs one source.
|