@zvada/agent-server 0.3.18 → 0.3.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +11 -26
- package/CHANGELOG.md +1041 -1
- package/README.md +40 -40
- package/dist/types/accounts/chatgpt.d.ts +34 -0
- package/dist/types/accounts/chatgpt.d.ts.map +1 -0
- package/dist/types/accounts/check.d.ts +24 -0
- package/dist/types/accounts/check.d.ts.map +1 -0
- package/dist/types/accounts/claude.d.ts +21 -0
- package/dist/types/accounts/claude.d.ts.map +1 -0
- package/dist/types/accounts/endpoint.d.ts +50 -0
- package/dist/types/accounts/endpoint.d.ts.map +1 -0
- package/dist/types/accounts/http.d.ts +38 -0
- package/dist/types/accounts/http.d.ts.map +1 -0
- package/dist/types/accounts/index.d.ts +6 -0
- package/dist/types/accounts/index.d.ts.map +1 -0
- package/dist/types/client/client.d.ts +272 -0
- package/dist/types/client/client.d.ts.map +1 -0
- package/dist/types/client/connection.d.ts +160 -0
- package/dist/types/client/connection.d.ts.map +1 -0
- package/dist/types/client/history.d.ts +8 -0
- package/dist/types/client/history.d.ts.map +1 -0
- package/dist/types/client/index.d.ts +5 -0
- package/dist/types/client/index.d.ts.map +1 -0
- package/dist/types/client/listeners.d.ts +8 -0
- package/dist/types/client/listeners.d.ts.map +1 -0
- package/dist/types/client/logins.d.ts +35 -0
- package/dist/types/client/logins.d.ts.map +1 -0
- package/dist/types/client/rpc.d.ts +46 -0
- package/dist/types/client/rpc.d.ts.map +1 -0
- package/dist/types/client/streams.d.ts +150 -0
- package/dist/types/client/streams.d.ts.map +1 -0
- package/dist/types/client/transports.d.ts +35 -0
- package/dist/types/client/transports.d.ts.map +1 -0
- package/dist/types/client/turns.d.ts +81 -0
- package/dist/types/client/turns.d.ts.map +1 -0
- package/dist/types/core/agents/acp/acp-agent.d.ts +68 -0
- package/dist/types/core/agents/acp/acp-agent.d.ts.map +1 -0
- package/dist/types/core/agents/acp/adapter.d.ts +39 -0
- package/dist/types/core/agents/acp/adapter.d.ts.map +1 -0
- package/dist/types/core/agents/acp/client.d.ts +90 -0
- package/dist/types/core/agents/acp/client.d.ts.map +1 -0
- package/dist/types/core/agents/acp/known-agents.d.ts +35 -0
- package/dist/types/core/agents/acp/known-agents.d.ts.map +1 -0
- package/dist/types/core/agents/acp/mappings.d.ts +46 -0
- package/dist/types/core/agents/acp/mappings.d.ts.map +1 -0
- package/dist/types/core/agents/acp/requests.d.ts +26 -0
- package/dist/types/core/agents/acp/requests.d.ts.map +1 -0
- package/dist/types/core/agents/attachments.d.ts +11 -0
- package/dist/types/core/agents/attachments.d.ts.map +1 -0
- package/dist/types/core/agents/base.d.ts +227 -0
- package/dist/types/core/agents/base.d.ts.map +1 -0
- package/dist/types/core/agents/claude-code/adapter.d.ts +58 -0
- package/dist/types/core/agents/claude-code/adapter.d.ts.map +1 -0
- package/dist/types/core/agents/claude-code/approvals.d.ts +26 -0
- package/dist/types/core/agents/claude-code/approvals.d.ts.map +1 -0
- package/dist/types/core/agents/claude-code/claude-agent.d.ts +70 -0
- package/dist/types/core/agents/claude-code/claude-agent.d.ts.map +1 -0
- package/dist/types/core/agents/claude-code/credentials.d.ts +54 -0
- package/dist/types/core/agents/claude-code/credentials.d.ts.map +1 -0
- package/dist/types/core/agents/claude-code/events.d.ts +42 -0
- package/dist/types/core/agents/claude-code/events.d.ts.map +1 -0
- package/dist/types/core/agents/claude-code/login.d.ts +61 -0
- package/dist/types/core/agents/claude-code/login.d.ts.map +1 -0
- package/dist/types/core/agents/claude-code/mcp.d.ts +52 -0
- package/dist/types/core/agents/claude-code/mcp.d.ts.map +1 -0
- package/dist/types/core/agents/claude-code/models.d.ts +30 -0
- package/dist/types/core/agents/claude-code/models.d.ts.map +1 -0
- package/dist/types/core/agents/claude-code/options.d.ts +183 -0
- package/dist/types/core/agents/claude-code/options.d.ts.map +1 -0
- package/dist/types/core/agents/claude-code/outcome.d.ts +50 -0
- package/dist/types/core/agents/claude-code/outcome.d.ts.map +1 -0
- package/dist/types/core/agents/claude-code/session-pool.d.ts +36 -0
- package/dist/types/core/agents/claude-code/session-pool.d.ts.map +1 -0
- package/dist/types/core/agents/claude-code/session.d.ts +148 -0
- package/dist/types/core/agents/claude-code/session.d.ts.map +1 -0
- package/dist/types/core/agents/claude-code/spawn.d.ts +29 -0
- package/dist/types/core/agents/claude-code/spawn.d.ts.map +1 -0
- package/dist/types/core/agents/claude-code/spend.d.ts +58 -0
- package/dist/types/core/agents/claude-code/spend.d.ts.map +1 -0
- package/dist/types/core/agents/claude-code/tools.d.ts +4 -0
- package/dist/types/core/agents/claude-code/tools.d.ts.map +1 -0
- package/dist/types/core/agents/claude-code/turn-usage.d.ts +52 -0
- package/dist/types/core/agents/claude-code/turn-usage.d.ts.map +1 -0
- package/dist/types/core/agents/claude-code/turn.d.ts +121 -0
- package/dist/types/core/agents/claude-code/turn.d.ts.map +1 -0
- package/dist/types/core/agents/codex-app-server/adapter.d.ts +59 -0
- package/dist/types/core/agents/codex-app-server/adapter.d.ts.map +1 -0
- package/dist/types/core/agents/codex-app-server/client.d.ts +116 -0
- package/dist/types/core/agents/codex-app-server/client.d.ts.map +1 -0
- package/dist/types/core/agents/codex-app-server/codex-agent.d.ts +113 -0
- package/dist/types/core/agents/codex-app-server/codex-agent.d.ts.map +1 -0
- package/dist/types/core/agents/codex-app-server/commands.d.ts +66 -0
- package/dist/types/core/agents/codex-app-server/commands.d.ts.map +1 -0
- package/dist/types/core/agents/codex-app-server/credentials.d.ts +49 -0
- package/dist/types/core/agents/codex-app-server/credentials.d.ts.map +1 -0
- package/dist/types/core/agents/codex-app-server/effort.d.ts +12 -0
- package/dist/types/core/agents/codex-app-server/effort.d.ts.map +1 -0
- package/dist/types/core/agents/codex-app-server/events.d.ts +45 -0
- package/dist/types/core/agents/codex-app-server/events.d.ts.map +1 -0
- package/dist/types/core/agents/codex-app-server/homes.d.ts +20 -0
- package/dist/types/core/agents/codex-app-server/homes.d.ts.map +1 -0
- package/dist/types/core/agents/codex-app-server/input.d.ts +26 -0
- package/dist/types/core/agents/codex-app-server/input.d.ts.map +1 -0
- package/dist/types/core/agents/codex-app-server/items.d.ts +31 -0
- package/dist/types/core/agents/codex-app-server/items.d.ts.map +1 -0
- package/dist/types/core/agents/codex-app-server/login.d.ts +38 -0
- package/dist/types/core/agents/codex-app-server/login.d.ts.map +1 -0
- package/dist/types/core/agents/codex-app-server/mcp.d.ts +62 -0
- package/dist/types/core/agents/codex-app-server/mcp.d.ts.map +1 -0
- package/dist/types/core/agents/codex-app-server/models.d.ts +67 -0
- package/dist/types/core/agents/codex-app-server/models.d.ts.map +1 -0
- package/dist/types/core/agents/codex-app-server/options.d.ts +40 -0
- package/dist/types/core/agents/codex-app-server/options.d.ts.map +1 -0
- package/dist/types/core/agents/codex-app-server/plan-approval.d.ts +19 -0
- package/dist/types/core/agents/codex-app-server/plan-approval.d.ts.map +1 -0
- package/dist/types/core/agents/codex-app-server/protocol.d.ts +99 -0
- package/dist/types/core/agents/codex-app-server/protocol.d.ts.map +1 -0
- package/dist/types/core/agents/codex-app-server/requests.d.ts +42 -0
- package/dist/types/core/agents/codex-app-server/requests.d.ts.map +1 -0
- package/dist/types/core/agents/codex-app-server/session.d.ts +96 -0
- package/dist/types/core/agents/codex-app-server/session.d.ts.map +1 -0
- package/dist/types/core/agents/codex-app-server/subagents.d.ts +53 -0
- package/dist/types/core/agents/codex-app-server/subagents.d.ts.map +1 -0
- package/dist/types/core/agents/codex-app-server/thread-settings.d.ts +87 -0
- package/dist/types/core/agents/codex-app-server/thread-settings.d.ts.map +1 -0
- package/dist/types/core/agents/codex-app-server/turn-items.d.ts +33 -0
- package/dist/types/core/agents/codex-app-server/turn-items.d.ts.map +1 -0
- package/dist/types/core/agents/codex-app-server/turn.d.ts +163 -0
- package/dist/types/core/agents/codex-app-server/turn.d.ts.map +1 -0
- package/dist/types/core/agents/config-fingerprint.d.ts +5 -0
- package/dist/types/core/agents/config-fingerprint.d.ts.map +1 -0
- package/dist/types/core/agents/credentials.d.ts +28 -0
- package/dist/types/core/agents/credentials.d.ts.map +1 -0
- package/dist/types/core/agents/elicitation.d.ts +37 -0
- package/dist/types/core/agents/elicitation.d.ts.map +1 -0
- package/dist/types/core/agents/inherit-config.d.ts +18 -0
- package/dist/types/core/agents/inherit-config.d.ts.map +1 -0
- package/dist/types/core/agents/login.d.ts +67 -0
- package/dist/types/core/agents/login.d.ts.map +1 -0
- package/dist/types/core/agents/mcp-config.d.ts +57 -0
- package/dist/types/core/agents/mcp-config.d.ts.map +1 -0
- package/dist/types/core/agents/plan.d.ts +16 -0
- package/dist/types/core/agents/plan.d.ts.map +1 -0
- package/dist/types/core/agents/registry.d.ts +30 -0
- package/dist/types/core/agents/registry.d.ts.map +1 -0
- package/dist/types/core/agents/session-agent.d.ts +48 -0
- package/dist/types/core/agents/session-agent.d.ts.map +1 -0
- package/dist/types/core/agents/session-pool.d.ts +41 -0
- package/dist/types/core/agents/session-pool.d.ts.map +1 -0
- package/dist/types/core/agents/stderr-tail.d.ts +28 -0
- package/dist/types/core/agents/stderr-tail.d.ts.map +1 -0
- package/dist/types/core/agents/thinking-levels.d.ts +4 -0
- package/dist/types/core/agents/thinking-levels.d.ts.map +1 -0
- package/dist/types/core/agents/types.d.ts +124 -0
- package/dist/types/core/agents/types.d.ts.map +1 -0
- package/dist/types/core/diagnostics.d.ts +36 -0
- package/dist/types/core/diagnostics.d.ts.map +1 -0
- package/dist/types/core/index.d.ts +29 -0
- package/dist/types/core/index.d.ts.map +1 -0
- package/dist/types/core/presets.d.ts +80 -0
- package/dist/types/core/presets.d.ts.map +1 -0
- package/dist/types/core/processes/identity.d.ts +31 -0
- package/dist/types/core/processes/identity.d.ts.map +1 -0
- package/dist/types/core/processes/lifeline.d.ts +47 -0
- package/dist/types/core/processes/lifeline.d.ts.map +1 -0
- package/dist/types/core/processes/native-processes.d.ts +130 -0
- package/dist/types/core/processes/native-processes.d.ts.map +1 -0
- package/dist/types/core/processes/process-table.d.ts +43 -0
- package/dist/types/core/processes/process-table.d.ts.map +1 -0
- package/dist/types/core/provision/cache-trust.d.ts +13 -0
- package/dist/types/core/provision/cache-trust.d.ts.map +1 -0
- package/dist/types/core/provision/cache-usage.d.ts +14 -0
- package/dist/types/core/provision/cache-usage.d.ts.map +1 -0
- package/dist/types/core/provision/checksums.d.ts +46 -0
- package/dist/types/core/provision/checksums.d.ts.map +1 -0
- package/dist/types/core/provision/extract.d.ts +7 -0
- package/dist/types/core/provision/extract.d.ts.map +1 -0
- package/dist/types/core/provision/index.d.ts +5 -0
- package/dist/types/core/provision/index.d.ts.map +1 -0
- package/dist/types/core/provision/install-build.d.ts +36 -0
- package/dist/types/core/provision/install-build.d.ts.map +1 -0
- package/dist/types/core/provision/npm.d.ts +40 -0
- package/dist/types/core/provision/npm.d.ts.map +1 -0
- package/dist/types/core/provision/pins.d.ts +24 -0
- package/dist/types/core/provision/pins.d.ts.map +1 -0
- package/dist/types/core/provision/platform.d.ts +28 -0
- package/dist/types/core/provision/platform.d.ts.map +1 -0
- package/dist/types/core/provision/provisioner.d.ts +189 -0
- package/dist/types/core/provision/provisioner.d.ts.map +1 -0
- package/dist/types/core/provision/releases.d.ts +8 -0
- package/dist/types/core/provision/releases.d.ts.map +1 -0
- package/dist/types/core/provision/tools.d.ts +81 -0
- package/dist/types/core/provision/tools.d.ts.map +1 -0
- package/dist/types/core/proxy/anthropic-proxy.d.ts +52 -0
- package/dist/types/core/proxy/anthropic-proxy.d.ts.map +1 -0
- package/dist/types/core/proxy/api-key-store.d.ts +37 -0
- package/dist/types/core/proxy/api-key-store.d.ts.map +1 -0
- package/dist/types/core/proxy/index.d.ts +3 -0
- package/dist/types/core/proxy/index.d.ts.map +1 -0
- package/dist/types/core/proxy/loopback.d.ts +14 -0
- package/dist/types/core/proxy/loopback.d.ts.map +1 -0
- package/dist/types/core/runtime/agent-runtime.d.ts +181 -0
- package/dist/types/core/runtime/agent-runtime.d.ts.map +1 -0
- package/dist/types/core/runtime/event-processor.d.ts +87 -0
- package/dist/types/core/runtime/event-processor.d.ts.map +1 -0
- package/dist/types/core/runtime/event-sink.d.ts +44 -0
- package/dist/types/core/runtime/event-sink.d.ts.map +1 -0
- package/dist/types/core/runtime/harness-clis.d.ts +10 -0
- package/dist/types/core/runtime/harness-clis.d.ts.map +1 -0
- package/dist/types/core/runtime/harness-logins.d.ts +68 -0
- package/dist/types/core/runtime/harness-logins.d.ts.map +1 -0
- package/dist/types/core/runtime/login-reads.d.ts +28 -0
- package/dist/types/core/runtime/login-reads.d.ts.map +1 -0
- package/dist/types/core/runtime/model-lists.d.ts +37 -0
- package/dist/types/core/runtime/model-lists.d.ts.map +1 -0
- package/dist/types/core/runtime/permission-policy.d.ts +61 -0
- package/dist/types/core/runtime/permission-policy.d.ts.map +1 -0
- package/dist/types/core/runtime/request-broker.d.ts +49 -0
- package/dist/types/core/runtime/request-broker.d.ts.map +1 -0
- package/dist/types/core/runtime/steering.d.ts +38 -0
- package/dist/types/core/runtime/steering.d.ts.map +1 -0
- package/dist/types/core/runtime/turn-outcome.d.ts +21 -0
- package/dist/types/core/runtime/turn-outcome.d.ts.map +1 -0
- package/dist/types/core/runtime/turn.d.ts +121 -0
- package/dist/types/core/runtime/turn.d.ts.map +1 -0
- package/dist/types/core/utils/bounded.d.ts +6 -0
- package/dist/types/core/utils/bounded.d.ts.map +1 -0
- package/dist/types/core/utils/deadline.d.ts +14 -0
- package/dist/types/core/utils/deadline.d.ts.map +1 -0
- package/dist/types/core/utils/errors.d.ts +104 -0
- package/dist/types/core/utils/errors.d.ts.map +1 -0
- package/dist/types/core/utils/is-record.d.ts +3 -0
- package/dist/types/core/utils/is-record.d.ts.map +1 -0
- package/dist/types/core/utils/loopback.d.ts +6 -0
- package/dist/types/core/utils/loopback.d.ts.map +1 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/protocol/account.d.ts +26 -0
- package/dist/types/protocol/account.d.ts.map +1 -0
- package/dist/types/protocol/async-queue.d.ts +21 -0
- package/dist/types/protocol/async-queue.d.ts.map +1 -0
- package/dist/types/protocol/auth.d.ts +228 -0
- package/dist/types/protocol/auth.d.ts.map +1 -0
- package/dist/types/protocol/canonical.d.ts +9 -0
- package/dist/types/protocol/canonical.d.ts.map +1 -0
- package/dist/types/protocol/clis.d.ts +74 -0
- package/dist/types/protocol/clis.d.ts.map +1 -0
- package/dist/types/protocol/config.d.ts +200 -0
- package/dist/types/protocol/config.d.ts.map +1 -0
- package/dist/types/protocol/conversation.d.ts +942 -0
- package/dist/types/protocol/conversation.d.ts.map +1 -0
- package/dist/types/protocol/credentials.d.ts +145 -0
- package/dist/types/protocol/credentials.d.ts.map +1 -0
- package/dist/types/protocol/errors.d.ts +36 -0
- package/dist/types/protocol/errors.d.ts.map +1 -0
- package/dist/types/protocol/execution.d.ts +28 -0
- package/dist/types/protocol/execution.d.ts.map +1 -0
- package/dist/types/protocol/factories.d.ts +94 -0
- package/dist/types/protocol/factories.d.ts.map +1 -0
- package/dist/types/protocol/guards.d.ts +55 -0
- package/dist/types/protocol/guards.d.ts.map +1 -0
- package/dist/types/protocol/harness.d.ts +70 -0
- package/dist/types/protocol/harness.d.ts.map +1 -0
- package/dist/types/protocol/history.d.ts +318 -0
- package/dist/types/protocol/history.d.ts.map +1 -0
- package/dist/types/protocol/ids.d.ts +9 -0
- package/dist/types/protocol/ids.d.ts.map +1 -0
- package/dist/types/protocol/index.d.ts +35 -0
- package/dist/types/protocol/index.d.ts.map +1 -0
- package/dist/types/protocol/lifecycle.d.ts +1190 -0
- package/dist/types/protocol/lifecycle.d.ts.map +1 -0
- package/dist/types/protocol/meta-keys.d.ts +17 -0
- package/dist/types/protocol/meta-keys.d.ts.map +1 -0
- package/dist/types/protocol/meta.d.ts +13 -0
- package/dist/types/protocol/meta.d.ts.map +1 -0
- package/dist/types/protocol/model-catalog.d.ts +11 -0
- package/dist/types/protocol/model-catalog.d.ts.map +1 -0
- package/dist/types/protocol/models.d.ts +107 -0
- package/dist/types/protocol/models.d.ts.map +1 -0
- package/dist/types/protocol/part-input.d.ts +101 -0
- package/dist/types/protocol/part-input.d.ts.map +1 -0
- package/dist/types/protocol/parts.d.ts +357 -0
- package/dist/types/protocol/parts.d.ts.map +1 -0
- package/dist/types/protocol/reduce.d.ts +26 -0
- package/dist/types/protocol/reduce.d.ts.map +1 -0
- package/dist/types/protocol/selectors.d.ts +64 -0
- package/dist/types/protocol/selectors.d.ts.map +1 -0
- package/dist/types/protocol/seq-cursor.d.ts +66 -0
- package/dist/types/protocol/seq-cursor.d.ts.map +1 -0
- package/dist/types/protocol/stop-reasons.d.ts +33 -0
- package/dist/types/protocol/stop-reasons.d.ts.map +1 -0
- package/dist/types/protocol/thinking.d.ts +28 -0
- package/dist/types/protocol/thinking.d.ts.map +1 -0
- package/dist/types/protocol/time.d.ts +21 -0
- package/dist/types/protocol/time.d.ts.map +1 -0
- package/dist/types/protocol/tokens.d.ts +29 -0
- package/dist/types/protocol/tokens.d.ts.map +1 -0
- package/dist/types/protocol/tool-closure.d.ts +25 -0
- package/dist/types/protocol/tool-closure.d.ts.map +1 -0
- package/dist/types/protocol/tool-state.d.ts +209 -0
- package/dist/types/protocol/tool-state.d.ts.map +1 -0
- package/dist/types/protocol/verify.d.ts +52 -0
- package/dist/types/protocol/verify.d.ts.map +1 -0
- package/dist/types/protocol/vocabulary.d.ts +19 -0
- package/dist/types/protocol/vocabulary.d.ts.map +1 -0
- package/dist/types/protocol/wire-api.d.ts +1983 -0
- package/dist/types/protocol/wire-api.d.ts.map +1 -0
- package/dist/types/protocol/wire.d.ts +1758 -0
- package/dist/types/protocol/wire.d.ts.map +1 -0
- package/dist/types/server/acp/binding.d.ts +33 -0
- package/dist/types/server/acp/binding.d.ts.map +1 -0
- package/dist/types/server/acp/session-records.d.ts +17 -0
- package/dist/types/server/acp/session-records.d.ts.map +1 -0
- package/dist/types/server/acp/translate.d.ts +48 -0
- package/dist/types/server/acp/translate.d.ts.map +1 -0
- package/dist/types/server/agent-server.d.ts +121 -0
- package/dist/types/server/agent-server.d.ts.map +1 -0
- package/dist/types/server/feedback.d.ts +37 -0
- package/dist/types/server/feedback.d.ts.map +1 -0
- package/dist/types/server/history.d.ts +15 -0
- package/dist/types/server/history.d.ts.map +1 -0
- package/dist/types/server/index.d.ts +7 -0
- package/dist/types/server/index.d.ts.map +1 -0
- package/dist/types/server/install.d.ts +42 -0
- package/dist/types/server/install.d.ts.map +1 -0
- package/dist/types/server/open-parts.d.ts +27 -0
- package/dist/types/server/open-parts.d.ts.map +1 -0
- package/dist/types/server/routes.d.ts +61 -0
- package/dist/types/server/routes.d.ts.map +1 -0
- package/dist/types/server/secrets.d.ts +11 -0
- package/dist/types/server/secrets.d.ts.map +1 -0
- package/dist/types/server/session-gate.d.ts +32 -0
- package/dist/types/server/session-gate.d.ts.map +1 -0
- package/dist/types/server/session-log.d.ts +39 -0
- package/dist/types/server/session-log.d.ts.map +1 -0
- package/dist/types/server/session-store.d.ts +119 -0
- package/dist/types/server/session-store.d.ts.map +1 -0
- package/dist/types/server/subscriptions.d.ts +74 -0
- package/dist/types/server/subscriptions.d.ts.map +1 -0
- package/dist/types/server/transports.d.ts +74 -0
- package/dist/types/server/transports.d.ts.map +1 -0
- package/dist/types/server/wire-error.d.ts +9 -0
- package/dist/types/server/wire-error.d.ts.map +1 -0
- package/dist/types/sync/event-log.d.ts +45 -0
- package/dist/types/sync/event-log.d.ts.map +1 -0
- package/dist/types/sync/fit.d.ts +40 -0
- package/dist/types/sync/fit.d.ts.map +1 -0
- package/dist/types/sync/history-reader.d.ts +8 -0
- package/dist/types/sync/history-reader.d.ts.map +1 -0
- package/dist/types/sync/history-sqlite.d.ts +37 -0
- package/dist/types/sync/history-sqlite.d.ts.map +1 -0
- package/dist/types/sync/history.d.ts +70 -0
- package/dist/types/sync/history.d.ts.map +1 -0
- package/dist/types/sync/index.d.ts +4 -0
- package/dist/types/sync/index.d.ts.map +1 -0
- package/dist/types/sync/journal-sqlite.d.ts +51 -0
- package/dist/types/sync/journal-sqlite.d.ts.map +1 -0
- package/dist/types/sync/journal.d.ts +107 -0
- package/dist/types/sync/journal.d.ts.map +1 -0
- package/dist/types/sync/live-tails.d.ts +30 -0
- package/dist/types/sync/live-tails.d.ts.map +1 -0
- package/dist/types/sync/node-sqlite.d.ts +7 -0
- package/dist/types/sync/node-sqlite.d.ts.map +1 -0
- package/dist/types/sync/projection.d.ts +10 -0
- package/dist/types/sync/projection.d.ts.map +1 -0
- package/dist/types/sync/recovery.d.ts +25 -0
- package/dist/types/sync/recovery.d.ts.map +1 -0
- package/dist/types/sync/retry.d.ts +25 -0
- package/dist/types/sync/retry.d.ts.map +1 -0
- package/dist/types/sync/schema.d.ts +4 -0
- package/dist/types/sync/schema.d.ts.map +1 -0
- package/dist/types/sync/sql.d.ts +38 -0
- package/dist/types/sync/sql.d.ts.map +1 -0
- package/dist/types/sync/sqlite-file.d.ts +9 -0
- package/dist/types/sync/sqlite-file.d.ts.map +1 -0
- package/dist/types/sync/sqlite.d.ts +6 -0
- package/dist/types/sync/sqlite.d.ts.map +1 -0
- package/dist/types/sync/worker.d.ts +63 -0
- package/dist/types/sync/worker.d.ts.map +1 -0
- package/docs/consuming.md +452 -53
- package/docs/deploy.md +218 -47
- package/docs/{sync-experiment.md → durable-sessions.md} +77 -72
- package/docs/feedback.md +116 -0
- package/docs/harnesses.md +212 -88
- package/docs/sync-history.md +29 -13
- package/docs/sync-integration.md +52 -66
- package/package.json +45 -28
- package/src/accounts/chatgpt.ts +194 -0
- package/src/accounts/check.ts +112 -0
- package/src/accounts/claude.ts +111 -0
- package/src/accounts/endpoint.ts +151 -0
- package/src/accounts/http.ts +97 -0
- package/src/accounts/index.ts +19 -0
- package/src/client/client.ts +604 -670
- package/src/client/connection.ts +371 -0
- package/src/client/history.ts +29 -52
- package/src/client/index.ts +11 -2
- package/src/client/listeners.ts +20 -0
- package/src/client/logins.ts +105 -0
- package/src/client/rpc.ts +155 -0
- package/src/client/streams.ts +488 -0
- package/src/client/transports.ts +62 -8
- package/src/client/turns.ts +171 -0
- package/src/core/agents/acp/acp-agent.ts +94 -118
- package/src/core/agents/acp/adapter.ts +20 -9
- package/src/core/agents/acp/client.ts +125 -47
- package/src/core/agents/acp/known-agents.ts +36 -4
- package/src/core/agents/acp/mappings.ts +64 -18
- package/src/core/agents/acp/requests.ts +125 -0
- package/src/core/agents/attachments.ts +18 -0
- package/src/core/agents/base.ts +120 -42
- package/src/core/agents/claude-code/adapter.ts +177 -238
- package/src/core/agents/claude-code/approvals.ts +270 -0
- package/src/core/agents/claude-code/claude-agent.ts +175 -158
- package/src/core/agents/claude-code/credentials.ts +171 -0
- package/src/core/agents/claude-code/events.ts +55 -0
- package/src/core/agents/claude-code/login.ts +269 -0
- package/src/core/agents/claude-code/mcp.ts +206 -0
- package/src/core/agents/claude-code/models.ts +15 -9
- package/src/core/agents/claude-code/options.ts +326 -39
- package/src/core/agents/claude-code/outcome.ts +209 -0
- package/src/core/agents/claude-code/session-pool.ts +136 -0
- package/src/core/agents/claude-code/session.ts +513 -0
- package/src/core/agents/claude-code/spawn.ts +144 -0
- package/src/core/agents/claude-code/spend.ts +160 -0
- package/src/core/agents/claude-code/tools.ts +47 -0
- package/src/core/agents/claude-code/turn-usage.ts +135 -0
- package/src/core/agents/claude-code/turn.ts +232 -0
- package/src/core/agents/codex-app-server/adapter.ts +314 -117
- package/src/core/agents/codex-app-server/client.ts +186 -84
- package/src/core/agents/codex-app-server/codex-agent.ts +343 -0
- package/src/core/agents/codex-app-server/commands.ts +228 -0
- package/src/core/agents/codex-app-server/credentials.ts +126 -0
- package/src/core/agents/codex-app-server/effort.ts +29 -0
- package/src/core/agents/codex-app-server/events.ts +47 -0
- package/src/core/agents/codex-app-server/homes.ts +64 -0
- package/src/core/agents/codex-app-server/input.ts +60 -0
- package/src/core/agents/codex-app-server/items.ts +149 -0
- package/src/core/agents/codex-app-server/login.ts +180 -0
- package/src/core/agents/codex-app-server/mcp.ts +182 -37
- package/src/core/agents/codex-app-server/models.ts +99 -46
- package/src/core/agents/codex-app-server/options.ts +146 -0
- package/src/core/agents/codex-app-server/plan-approval.ts +135 -0
- package/src/core/agents/codex-app-server/protocol.ts +117 -0
- package/src/core/agents/codex-app-server/requests.ts +332 -0
- package/src/core/agents/codex-app-server/session.ts +285 -0
- package/src/core/agents/codex-app-server/subagents.ts +108 -42
- package/src/core/agents/codex-app-server/thread-settings.ts +279 -0
- package/src/core/agents/codex-app-server/turn-items.ts +88 -0
- package/src/core/agents/codex-app-server/turn.ts +463 -0
- package/src/core/agents/config-fingerprint.ts +7 -14
- package/src/core/agents/credentials.ts +98 -0
- package/src/core/agents/elicitation.ts +75 -0
- package/src/core/agents/inherit-config.ts +21 -0
- package/src/core/agents/login.ts +116 -0
- package/src/core/agents/mcp-config.ts +112 -8
- package/src/core/agents/plan.ts +25 -0
- package/src/core/agents/registry.ts +21 -4
- package/src/core/agents/session-agent.ts +96 -4
- package/src/core/agents/{session-store.ts → session-pool.ts} +11 -5
- package/src/core/agents/stderr-tail.ts +75 -0
- package/src/core/agents/types.ts +40 -5
- package/src/core/index.ts +58 -48
- package/src/core/presets.ts +100 -39
- package/src/core/processes/identity.ts +113 -0
- package/src/core/processes/lifeline.ts +170 -0
- package/src/core/processes/native-processes.ts +379 -0
- package/src/core/processes/process-table.ts +174 -0
- package/src/core/provision/cache-trust.ts +121 -0
- package/src/core/provision/cache-usage.ts +69 -0
- package/src/core/provision/checksums.ts +64 -0
- package/src/core/provision/extract.ts +11 -2
- package/src/core/provision/index.ts +4 -1
- package/src/core/provision/install-build.ts +136 -0
- package/src/core/provision/npm.ts +112 -17
- package/src/core/provision/pins.ts +9 -33
- package/src/core/provision/platform.ts +12 -1
- package/src/core/provision/provisioner.ts +329 -397
- package/src/core/provision/releases.ts +86 -0
- package/src/core/provision/tools.ts +329 -0
- package/src/core/proxy/anthropic-proxy.ts +55 -25
- package/src/core/proxy/api-key-store.ts +2 -12
- package/src/core/proxy/loopback.ts +106 -0
- package/src/core/runtime/agent-runtime.ts +327 -498
- package/src/core/runtime/event-processor.ts +197 -70
- package/src/core/runtime/harness-clis.ts +61 -0
- package/src/core/runtime/harness-logins.ts +328 -0
- package/src/core/runtime/login-reads.ts +78 -0
- package/src/core/runtime/model-lists.ts +33 -48
- package/src/core/runtime/permission-policy.ts +113 -0
- package/src/core/runtime/request-broker.ts +204 -0
- package/src/core/runtime/steering.ts +112 -0
- package/src/core/runtime/turn-outcome.ts +30 -0
- package/src/core/runtime/turn.ts +360 -0
- package/src/core/utils/bounded.ts +12 -0
- package/src/core/utils/deadline.ts +38 -0
- package/src/core/utils/errors.ts +113 -21
- package/src/core/utils/is-record.ts +4 -0
- package/src/core/utils/loopback.ts +10 -0
- package/src/protocol/account.ts +104 -0
- package/src/protocol/auth.ts +199 -0
- package/src/protocol/canonical.ts +16 -0
- package/src/protocol/clis.ts +69 -0
- package/src/protocol/config.ts +60 -6
- package/src/protocol/conversation.ts +272 -0
- package/src/protocol/credentials.ts +145 -0
- package/src/protocol/errors.ts +78 -20
- package/src/protocol/execution.ts +5 -2
- package/src/protocol/factories.ts +55 -7
- package/src/protocol/guards.ts +55 -2
- package/src/protocol/harness.ts +56 -6
- package/src/protocol/history.ts +72 -64
- package/src/protocol/ids.ts +0 -12
- package/src/protocol/index.ts +9 -0
- package/src/protocol/lifecycle.ts +250 -79
- package/src/protocol/meta-keys.ts +17 -0
- package/src/protocol/meta.ts +3 -3
- package/src/protocol/models.ts +2 -43
- package/src/protocol/part-input.ts +2 -1
- package/src/protocol/parts.ts +12 -3
- package/src/protocol/reduce.ts +302 -413
- package/src/protocol/selectors.ts +1 -20
- package/src/protocol/seq-cursor.ts +14 -0
- package/src/protocol/stop-reasons.ts +1 -1
- package/src/protocol/thinking.ts +1 -30
- package/src/protocol/time.ts +2 -2
- package/src/protocol/tool-closure.ts +93 -0
- package/src/protocol/tool-state.ts +19 -4
- package/src/protocol/verify.ts +222 -92
- package/src/protocol/vocabulary.ts +21 -5
- package/src/protocol/wire-api.ts +232 -0
- package/src/protocol/wire.ts +351 -64
- package/src/server/acp/binding.ts +63 -103
- package/src/server/acp/session-records.ts +53 -0
- package/src/server/acp/translate.ts +129 -20
- package/src/server/agent-server.ts +223 -387
- package/src/server/bin.ts +280 -78
- package/src/server/feedback.ts +204 -14
- package/src/server/history.ts +116 -122
- package/src/server/index.ts +3 -8
- package/src/server/install.ts +107 -8
- package/src/server/open-parts.ts +95 -0
- package/src/server/routes.ts +391 -0
- package/src/server/secrets.ts +24 -0
- package/src/server/session-gate.ts +69 -0
- package/src/server/session-log.ts +14 -2
- package/src/server/session-store.ts +136 -72
- package/src/server/subscriptions.ts +232 -0
- package/src/server/transports.ts +125 -25
- package/src/server/wire-error.ts +31 -45
- package/src/sync/event-log.ts +31 -3
- package/src/sync/fit.ts +27 -11
- package/src/sync/history-reader.ts +10 -5
- package/src/sync/history-sqlite.ts +37 -42
- package/src/sync/history.ts +9 -15
- package/src/sync/index.ts +17 -5
- package/src/sync/journal-sqlite.ts +162 -76
- package/src/sync/journal.ts +91 -32
- package/src/sync/live-tails.ts +107 -0
- package/src/sync/projection.ts +35 -0
- package/src/sync/recovery.ts +80 -51
- package/src/sync/schema.ts +27 -6
- package/docs/rfds/0001-deterministic-echo-ids.md +0 -44
- package/docs/rfds/0002-native-subagents.md +0 -197
- package/src/client/request.ts +0 -19
- package/src/core/agents/claude-code/generator-session.ts +0 -572
- package/src/core/agents/claude-code/session-manager.ts +0 -166
- package/src/core/agents/codex-app-server/codex-app-server-agent.ts +0 -962
- package/src/core/agents/codex-items.ts +0 -149
- package/src/core/agents/codex-sdk/adapter.ts +0 -237
- package/src/core/agents/codex-sdk/codex-sdk-agent.ts +0 -330
- package/src/core/agents/tool-meta.ts +0 -68
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
import type { AgentCapabilities, AgentInput, ModelDescriptor } from "../../../protocol/index.ts";
|
|
2
|
+
import { type NativeProcesses, sharedNativeProcesses } from "../../processes/native-processes.ts";
|
|
3
|
+
import { onAbort } from "../../utils/deadline.ts";
|
|
4
|
+
import type {
|
|
5
|
+
AgentExecuteOptions,
|
|
6
|
+
CancelResult,
|
|
7
|
+
ListModelsOptions,
|
|
8
|
+
RawAgentEvent,
|
|
9
|
+
} from "../base.ts";
|
|
10
|
+
import { configFingerprint } from "../config-fingerprint.ts";
|
|
11
|
+
import type { CredentialSource } from "../credentials.ts";
|
|
12
|
+
import type { InheritConfig } from "../inherit-config.ts";
|
|
13
|
+
import { type DeliverSteer, SessionAgent } from "../session-agent.ts";
|
|
14
|
+
import { IDLE_TIMEOUT_MS } from "../session-pool.ts";
|
|
15
|
+
import {
|
|
16
|
+
CodexAppServerClient,
|
|
17
|
+
type CodexAppServerClientOptions,
|
|
18
|
+
type CodexConnection,
|
|
19
|
+
} from "./client.ts";
|
|
20
|
+
import { TurnCommands } from "./commands.ts";
|
|
21
|
+
import { type CodexAuth, authFor, logIn } from "./credentials.ts";
|
|
22
|
+
import { toUserInput } from "./input.ts";
|
|
23
|
+
import { CodexCliLogin } from "./login.ts";
|
|
24
|
+
import { codexModelDescriptors, listCodexModels, readCodexListing } from "./models.ts";
|
|
25
|
+
import { CODEX_TOOLS_REQUIRED, clientOptionsFor } from "./options.ts";
|
|
26
|
+
import type { CodexTurnScope } from "./requests.ts";
|
|
27
|
+
import { type CodexSession, CodexSessions } from "./session.ts";
|
|
28
|
+
import { CodexTurnSubagents, ManagedStop } from "./subagents.ts";
|
|
29
|
+
import { TurnItems } from "./turn-items.ts";
|
|
30
|
+
import { CodexTurn, type CodexTurnTiming, InterruptStop, type TurnStop } from "./turn.ts";
|
|
31
|
+
|
|
32
|
+
const CAPABILITIES: AgentCapabilities = {
|
|
33
|
+
multiTurn: true,
|
|
34
|
+
sessionResume: true,
|
|
35
|
+
// Model/effort are overridable per turn/start, so no restart is needed.
|
|
36
|
+
modelSwitch: "in_session",
|
|
37
|
+
thinkingLevels: true,
|
|
38
|
+
images: true,
|
|
39
|
+
// Codex has no input for file content: text is inlined (`input.ts`).
|
|
40
|
+
files: ["text"],
|
|
41
|
+
mcpServers: true,
|
|
42
|
+
permissionRequests: true,
|
|
43
|
+
// Its approval's answer is a bare verdict.
|
|
44
|
+
rejectionMessages: false,
|
|
45
|
+
turnScopedSubagents: true,
|
|
46
|
+
steering: true,
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/** How long a retryable error may go unanswered before the turn is failed. */
|
|
50
|
+
const RETRY_STALL_TIMEOUT_MS = 120_000;
|
|
51
|
+
|
|
52
|
+
/** How long Codex has to confirm a stop, unless `cancelGraceMs` says otherwise. */
|
|
53
|
+
const CANCEL_GRACE_MS = 2_000;
|
|
54
|
+
|
|
55
|
+
export interface CodexAppServerAgentOptions {
|
|
56
|
+
/** Make the connection to `codex app-server` (a test's fake); a `CodexAppServerClient` by default. */
|
|
57
|
+
createClient?: (options: CodexAppServerClientOptions) => CodexConnection;
|
|
58
|
+
idleTimeoutMs?: number;
|
|
59
|
+
/** Fail a turn if the app-server goes silent this long after a retryable error. */
|
|
60
|
+
retryStallTimeoutMs?: number;
|
|
61
|
+
/**
|
|
62
|
+
* How long Codex has to confirm a stop (2 s by default): to acknowledge a
|
|
63
|
+
* cancel's `turn/interrupt` before the turn's process is ended, and to let
|
|
64
|
+
* the interrupted turn drain; to stop the commands the turn started, and
|
|
65
|
+
* for its own shutdown when they will not stop; to put a thread's settings
|
|
66
|
+
* back after a failed turn (`thread/settings/update`); and for each request
|
|
67
|
+
* of a managed turn's cleanup and its shutdown, five times it for the whole
|
|
68
|
+
* cleanup.
|
|
69
|
+
*/
|
|
70
|
+
cancelGraceMs?: number;
|
|
71
|
+
/**
|
|
72
|
+
* Resolve the `codex` binary to spawn (typically the provisioner's,
|
|
73
|
+
* memoized). Falls back to `$CODEX_CLI_PATH`, then PATH. Operator-level
|
|
74
|
+
* only — never sourced from the wire.
|
|
75
|
+
*/
|
|
76
|
+
resolveCliPath?: () => Promise<string | undefined>;
|
|
77
|
+
/**
|
|
78
|
+
* Credentials the product holds (LLP 0012), asked before each turn: an API
|
|
79
|
+
* key, or a ChatGPT token kept in the app-server's memory and asked for
|
|
80
|
+
* again when Codex needs a fresher one; none runs the turn as it would
|
|
81
|
+
* without them (its own `apiKey`, else the CLI's own login). Operator-level
|
|
82
|
+
* only.
|
|
83
|
+
*/
|
|
84
|
+
credentials?: CredentialSource;
|
|
85
|
+
/**
|
|
86
|
+
* Where the app-servers start: each in a group of its own, held by the
|
|
87
|
+
* lifeline and recorded for its session (`createAgentRegistry` passes the
|
|
88
|
+
* runtime's). Operator-level only.
|
|
89
|
+
*/
|
|
90
|
+
processes?: NativeProcesses;
|
|
91
|
+
/**
|
|
92
|
+
* What a thread loads by itself (LLP 0001, D17). At `all` (the default) a
|
|
93
|
+
* turn runs in the `CODEX_HOME` it names, else the process's. At `project`
|
|
94
|
+
* every turn must name a home of its session's own (absolute, not the
|
|
95
|
+
* shared one, one session to a home): the user's configuration stays out,
|
|
96
|
+
* the repository's `AGENTS.md` and skills load. `none` also keeps the
|
|
97
|
+
* repository's `AGENTS.md` out (`project_doc_max_bytes = 0`). Codex loads
|
|
98
|
+
* the repository's skills and `~/.agents/skills` at every level.
|
|
99
|
+
*/
|
|
100
|
+
inheritConfig?: InheritConfig;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Codex harness backed by a long-lived `codex app-server` subprocess speaking
|
|
105
|
+
* JSON-RPC. Unlike the SDK harness this streams token-level deltas. A client +
|
|
106
|
+
* thread is created per logical session and reused for multi-turn; model/effort
|
|
107
|
+
* are passed per `turn/start`. Resume uses `thread/resume` with the native id.
|
|
108
|
+
*/
|
|
109
|
+
export class CodexAppServerAgent extends SessionAgent {
|
|
110
|
+
readonly harness = "codex-app-server" as const;
|
|
111
|
+
readonly capabilities = CAPABILITIES;
|
|
112
|
+
/** Its CLIs start here: the runtime's, or this OS process's own when it was made without one. */
|
|
113
|
+
readonly processes: NativeProcesses;
|
|
114
|
+
private readonly timing: CodexTurnTiming;
|
|
115
|
+
private readonly sessions: CodexSessions;
|
|
116
|
+
/** A cancel's acknowledgement belongs to one invocation, even after it drains. */
|
|
117
|
+
private readonly turns = new WeakMap<AbortController, CodexTurn>();
|
|
118
|
+
readonly login = new CodexCliLogin({
|
|
119
|
+
connect: (env) =>
|
|
120
|
+
this.connect({ cwd: process.cwd(), ...(env && { env }) }, { experimentalApi: false }),
|
|
121
|
+
cliPath: async () => (await this.cliPath()) ?? "codex",
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
constructor(private readonly agentOptions: CodexAppServerAgentOptions = {}) {
|
|
125
|
+
super();
|
|
126
|
+
this.processes = agentOptions.processes ?? sharedNativeProcesses();
|
|
127
|
+
this.timing = {
|
|
128
|
+
graceMs: agentOptions.cancelGraceMs ?? CANCEL_GRACE_MS,
|
|
129
|
+
retryStallMs: agentOptions.retryStallTimeoutMs ?? RETRY_STALL_TIMEOUT_MS,
|
|
130
|
+
};
|
|
131
|
+
this.sessions = new CodexSessions(
|
|
132
|
+
// Plan mode is a collaboration mode, which Codex 0.160 takes only from a
|
|
133
|
+
// client that opted into its experimental API (as host-managed ChatGPT
|
|
134
|
+
// auth and turn-scoped subagents need anyway); a session can enter plan
|
|
135
|
+
// mode on any later turn.
|
|
136
|
+
(options, auth, signal) =>
|
|
137
|
+
this.connect(options, {
|
|
138
|
+
auth,
|
|
139
|
+
experimentalApi: true,
|
|
140
|
+
signal,
|
|
141
|
+
sessionId: options.sessionId,
|
|
142
|
+
}),
|
|
143
|
+
agentOptions.idleTimeoutMs ?? IDLE_TIMEOUT_MS,
|
|
144
|
+
agentOptions.inheritConfig,
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** On a product's credentials, each session may list as a different account. */
|
|
149
|
+
get modelsPerSession(): boolean {
|
|
150
|
+
return Boolean(this.agentOptions.credentials);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
protected override async *executeTurn(
|
|
154
|
+
input: AgentInput,
|
|
155
|
+
options: AgentExecuteOptions,
|
|
156
|
+
controller: AbortController,
|
|
157
|
+
): AsyncIterableIterator<RawAgentEvent> {
|
|
158
|
+
const { signal } = controller;
|
|
159
|
+
const scope: CodexTurnScope = {
|
|
160
|
+
onPermissionRequest: options.onPermissionRequest,
|
|
161
|
+
onElicitationRequest: options.onElicitationRequest,
|
|
162
|
+
signal,
|
|
163
|
+
items: new TurnItems(),
|
|
164
|
+
mode: options.permissionMode ?? "default",
|
|
165
|
+
};
|
|
166
|
+
let session: CodexSession | undefined;
|
|
167
|
+
let stop: TurnStop | undefined;
|
|
168
|
+
let turn: CodexTurn | undefined;
|
|
169
|
+
try {
|
|
170
|
+
signal.throwIfAborted();
|
|
171
|
+
// @ref LLP 0001#d5--messages-map-to-model-messages-no-silent-surface — no silent surface
|
|
172
|
+
// A turn Codex cannot run as asked fails before any process starts:
|
|
173
|
+
// its tools are not to be turned off, and an attachment it cannot take
|
|
174
|
+
// would go unread.
|
|
175
|
+
if (options.disableTools) throw new Error(CODEX_TOOLS_REQUIRED);
|
|
176
|
+
const userInput = toUserInput(input);
|
|
177
|
+
const predecessor = this.sessions.stopUnsettled(options.sessionId);
|
|
178
|
+
if (predecessor && !(await predecessor))
|
|
179
|
+
throw new Error("Previous Codex execution has not stopped");
|
|
180
|
+
const mcp = this.sessions.mcpFor(options);
|
|
181
|
+
scope.auth = await authFor({
|
|
182
|
+
credentials: this.agentOptions.credentials,
|
|
183
|
+
sessionId: options.sessionId,
|
|
184
|
+
turnId: options.turnId,
|
|
185
|
+
apiKey: options.apiKey,
|
|
186
|
+
signal,
|
|
187
|
+
});
|
|
188
|
+
signal.throwIfAborted();
|
|
189
|
+
session = await this.sessions.acquire(options, scope, mcp);
|
|
190
|
+
options.onNativeSession?.(session.threadId, {
|
|
191
|
+
resumed: session.threadId === options.resumeSessionId,
|
|
192
|
+
});
|
|
193
|
+
const commands = new TurnCommands(session.client, session.threadId, this.timing.graceMs);
|
|
194
|
+
stop = this.stopFor(session, scope, commands, options.sessionId);
|
|
195
|
+
signal.throwIfAborted();
|
|
196
|
+
const offPreparing = onAbort(signal, () => void stop?.cancel());
|
|
197
|
+
try {
|
|
198
|
+
await session.mcp.prepare(session.threadId, mcp, options.onWarning);
|
|
199
|
+
signal.throwIfAborted();
|
|
200
|
+
} finally {
|
|
201
|
+
offPreparing();
|
|
202
|
+
}
|
|
203
|
+
const steering = {
|
|
204
|
+
open: (deliver: DeliverSteer) => void this.openSteering(options, deliver),
|
|
205
|
+
hold: () => this.holdSteering(options),
|
|
206
|
+
};
|
|
207
|
+
turn = new CodexTurn(session, scope, options, commands, stop, steering, this.timing);
|
|
208
|
+
this.turns.set(controller, turn);
|
|
209
|
+
yield* turn.run(userInput);
|
|
210
|
+
} catch (error) {
|
|
211
|
+
if (!signal.aborted) throw error;
|
|
212
|
+
if (turn) yield* turn.endCancelled();
|
|
213
|
+
throw new Error("turn aborted");
|
|
214
|
+
} finally {
|
|
215
|
+
await stop?.dispose();
|
|
216
|
+
turn?.dispose();
|
|
217
|
+
if (session) this.sessions.leave(options.sessionId, session, scope);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/** How the turn stops: by Codex's interrupt, or, managed, with its native cleanup. */
|
|
222
|
+
private stopFor(
|
|
223
|
+
session: CodexSession,
|
|
224
|
+
scope: CodexTurnScope,
|
|
225
|
+
commands: TurnCommands,
|
|
226
|
+
sessionId: string,
|
|
227
|
+
): TurnStop {
|
|
228
|
+
const { client, threadId } = session;
|
|
229
|
+
const { graceMs } = this.timing;
|
|
230
|
+
if (session.subagentMode !== "turn")
|
|
231
|
+
return new InterruptStop(session, commands, scope.signal, graceMs);
|
|
232
|
+
scope.subagents = new CodexTurnSubagents(client, threadId, graceMs);
|
|
233
|
+
return new ManagedStop(scope.subagents, client, threadId, commands, scope.signal, graceMs, () =>
|
|
234
|
+
this.sessions.quarantine(sessionId, client),
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
protected override checkSteerInput(input: AgentInput): void {
|
|
239
|
+
toUserInput(input);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* The models Codex offers this login. Reuses the session's process when
|
|
244
|
+
* `sessionId` has one started with the same `env` (its `CODEX_HOME` decides
|
|
245
|
+
* the login and config); otherwise starts `codex app-server` just to ask,
|
|
246
|
+
* with the account the product's `credentials` hold for that session.
|
|
247
|
+
*/
|
|
248
|
+
async listModels(options: ListModelsOptions): Promise<ModelDescriptor[]> {
|
|
249
|
+
const live = options.sessionId ? this.sessions.get(options.sessionId) : undefined;
|
|
250
|
+
if (live && !live.client.closed && live.envFingerprint === configFingerprint(options.env)) {
|
|
251
|
+
const listing = await readCodexListing(live.client, options.cwd);
|
|
252
|
+
return codexModelDescriptors(listing.models, this.harness, listing.configuredModel);
|
|
253
|
+
}
|
|
254
|
+
// The account a turn with these options would log in as.
|
|
255
|
+
const auth = await authFor({
|
|
256
|
+
credentials: this.agentOptions.credentials,
|
|
257
|
+
sessionId: options.sessionId,
|
|
258
|
+
apiKey: options.apiKey,
|
|
259
|
+
...(options.signal && { signal: options.signal }),
|
|
260
|
+
});
|
|
261
|
+
options.signal?.throwIfAborted();
|
|
262
|
+
const client = await this.connect(options, {
|
|
263
|
+
auth,
|
|
264
|
+
experimentalApi: auth?.type === "chatgptAuthTokens",
|
|
265
|
+
});
|
|
266
|
+
const listing = await listCodexModels(client, options.cwd, {
|
|
267
|
+
signal: options.signal,
|
|
268
|
+
...(auth && { login: (connection) => logIn(connection, auth) }),
|
|
269
|
+
});
|
|
270
|
+
return codexModelDescriptors(listing.models, this.harness, listing.configuredModel);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
override async cancel(sessionId: string): Promise<CancelResult> {
|
|
274
|
+
// Snapshot the in-flight controllers before super.cancel() aborts them:
|
|
275
|
+
// the abort unblocks execute()'s queue and finally's endTurn drains the
|
|
276
|
+
// set on a later microtask, so reading afterwards would race that drain.
|
|
277
|
+
const controllers = [...(this.inflight.get(sessionId) ?? [])];
|
|
278
|
+
const base = await super.cancel(sessionId);
|
|
279
|
+
if (!base.hadTurn) {
|
|
280
|
+
const stopping = this.sessions.stopUnsettled(sessionId);
|
|
281
|
+
return stopping ? { confirmed: await stopping, hadTurn: false } : base;
|
|
282
|
+
}
|
|
283
|
+
const acknowledgements = await Promise.all(
|
|
284
|
+
controllers.map((controller) => this.turns.get(controller)?.acknowledgement ?? false),
|
|
285
|
+
);
|
|
286
|
+
return { confirmed: acknowledgements.every(Boolean), hadTurn: true };
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/** Unchanged MCP config reloads retain dead remote connections after VM resume. */
|
|
290
|
+
invalidateMcpConnections(): void {
|
|
291
|
+
this.sessions.invalidate();
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
loginChanged(): void {
|
|
295
|
+
this.sessions.loginChanged();
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
override async release(sessionId: string): Promise<void> {
|
|
299
|
+
// A host may release this harness before switching providers. Do not let
|
|
300
|
+
// that path discard ownership while managed child cleanup is unconfirmed.
|
|
301
|
+
if (this.sessions.managed(sessionId) && !(await this.cancel(sessionId)).confirmed)
|
|
302
|
+
throw new Error("Codex native cleanup was not confirmed; cannot release the session");
|
|
303
|
+
await super.release(sessionId);
|
|
304
|
+
this.sessions.release(sessionId);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
override async terminateAll(): Promise<void> {
|
|
308
|
+
await super.terminateAll();
|
|
309
|
+
this.sessions.terminateAll();
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/** The CLI to run: the provisioner's, else `$CODEX_CLI_PATH`; PATH's `codex` when neither. */
|
|
313
|
+
private async cliPath(): Promise<string | undefined> {
|
|
314
|
+
return (await this.agentOptions.resolveCliPath?.()) ?? process.env.CODEX_CLI_PATH;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* A connection to a new `codex app-server` for a folder, its `env` and a
|
|
319
|
+
* login; recorded for `sessionId`, which a model listing has none of.
|
|
320
|
+
*/
|
|
321
|
+
private async connect(
|
|
322
|
+
options: { cwd: string; env?: Record<string, string> },
|
|
323
|
+
spawn: {
|
|
324
|
+
/** The account it logs in as; none runs the store's own login. */
|
|
325
|
+
auth?: CodexAuth;
|
|
326
|
+
experimentalApi: boolean;
|
|
327
|
+
signal?: AbortSignal;
|
|
328
|
+
sessionId?: string;
|
|
329
|
+
},
|
|
330
|
+
): Promise<CodexConnection> {
|
|
331
|
+
const { auth, experimentalApi, signal, sessionId } = spawn;
|
|
332
|
+
const codexPath = (await this.cliPath()) ?? "codex";
|
|
333
|
+
signal?.throwIfAborted();
|
|
334
|
+
const clientOptions = {
|
|
335
|
+
...clientOptionsFor(codexPath, options, auth, experimentalApi),
|
|
336
|
+
processes: this.processes,
|
|
337
|
+
...(sessionId !== undefined && { sessionId }),
|
|
338
|
+
};
|
|
339
|
+
return (
|
|
340
|
+
this.agentOptions.createClient?.(clientOptions) ?? new CodexAppServerClient(clientOptions)
|
|
341
|
+
);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import { type CodexConnection, pages } from "./client.ts";
|
|
2
|
+
import { type CodexRawEvent, isSyntheticEvent } from "./events.ts";
|
|
3
|
+
import { type CodexItem, type CodexNotification, codexItem } from "./protocol.ts";
|
|
4
|
+
|
|
5
|
+
/** A command Codex keeps running for a thread in its own terminal (0.160). */
|
|
6
|
+
interface CodexBackgroundTerminal {
|
|
7
|
+
itemId?: string;
|
|
8
|
+
processId?: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** How many pages of a thread's terminals are read before Codex is taken to not know. */
|
|
12
|
+
const TERMINAL_PAGES = 20;
|
|
13
|
+
|
|
14
|
+
/** Every background terminal of a thread. Throws when Codex cannot say. */
|
|
15
|
+
export async function listBackgroundTerminals(
|
|
16
|
+
client: Pick<CodexConnection, "request">,
|
|
17
|
+
threadId: string,
|
|
18
|
+
timeoutMs: number,
|
|
19
|
+
): Promise<CodexBackgroundTerminal[]> {
|
|
20
|
+
const terminals: CodexBackgroundTerminal[] = [];
|
|
21
|
+
for await (const page of pages<CodexBackgroundTerminal>(
|
|
22
|
+
client,
|
|
23
|
+
"thread/backgroundTerminals/list",
|
|
24
|
+
{ threadId },
|
|
25
|
+
{ timeoutMs, maxPages: TERMINAL_PAGES },
|
|
26
|
+
))
|
|
27
|
+
terminals.push(...page);
|
|
28
|
+
return terminals;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** A command a turn started, which a cancel of that turn stops. */
|
|
32
|
+
interface TurnCommand {
|
|
33
|
+
/** Codex's process for it, when it runs in a terminal Codex keeps. */
|
|
34
|
+
processId?: string;
|
|
35
|
+
/** Its item completed: what it ran has ended. */
|
|
36
|
+
done: boolean;
|
|
37
|
+
/** Its completion reached the stream, which shows how it ended. */
|
|
38
|
+
shown: boolean;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Whether a command item starts something the turn owns. A poll of, or input
|
|
43
|
+
* to, a process an earlier turn started (`unifiedExecInteraction`) does not,
|
|
44
|
+
* and neither does the user's own shell command.
|
|
45
|
+
*/
|
|
46
|
+
function startsCommand(source: unknown): boolean {
|
|
47
|
+
return source !== "unifiedExecInteraction" && source !== "userShell";
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** How often a stop reads Codex's terminals again while one of the turn's is unaccounted for. */
|
|
51
|
+
const STOP_POLL_MS = 100;
|
|
52
|
+
|
|
53
|
+
// @ref LLP 0002#tools-a-turn-leaves-open — a cancel stops the commands the turn started
|
|
54
|
+
/**
|
|
55
|
+
* Stop the commands a cancelled turn started, and say which could not be
|
|
56
|
+
* confirmed stopped. Codex 0.160 ends a turn on `turn/interrupt` but keeps its
|
|
57
|
+
* running command alive as a background terminal, which can register after
|
|
58
|
+
* the turn ended. A command is stopped once its item completed, or once it was
|
|
59
|
+
* listed, terminated and is gone from a later list; the list is read again
|
|
60
|
+
* until each is, or `timeoutMs` passes. Terminals of earlier turns are left
|
|
61
|
+
* alone. Returns the item ids of the commands whose stop was not confirmed.
|
|
62
|
+
*/
|
|
63
|
+
async function stopTurnCommands(
|
|
64
|
+
client: Pick<CodexConnection, "request">,
|
|
65
|
+
threadId: string,
|
|
66
|
+
commands: ReadonlyMap<string, TurnCommand>,
|
|
67
|
+
timeoutMs: number,
|
|
68
|
+
): Promise<string[]> {
|
|
69
|
+
const deadline = Date.now() + timeoutMs;
|
|
70
|
+
const remaining = () => Math.max(1, deadline - Date.now());
|
|
71
|
+
/** The turn's commands seen running in a list: one gone from a later list stopped. */
|
|
72
|
+
const listed = new Set<string>();
|
|
73
|
+
for (;;) {
|
|
74
|
+
const open = [...commands].filter(([, command]) => !command.done).map(([itemId]) => itemId);
|
|
75
|
+
if (open.length === 0) return [];
|
|
76
|
+
const byProcess = new Map<string, string>();
|
|
77
|
+
for (const itemId of open) {
|
|
78
|
+
const processId = commands.get(itemId)?.processId;
|
|
79
|
+
if (processId) byProcess.set(processId, itemId);
|
|
80
|
+
}
|
|
81
|
+
let terminals: CodexBackgroundTerminal[];
|
|
82
|
+
try {
|
|
83
|
+
terminals = await listBackgroundTerminals(client, threadId, remaining());
|
|
84
|
+
} catch {
|
|
85
|
+
// Codex cannot say what still runs.
|
|
86
|
+
return open;
|
|
87
|
+
}
|
|
88
|
+
/** The turn's open commands running now, with their processes. */
|
|
89
|
+
const running = new Map<string, string | undefined>();
|
|
90
|
+
for (const terminal of terminals) {
|
|
91
|
+
const itemId =
|
|
92
|
+
terminal.itemId !== undefined && open.includes(terminal.itemId)
|
|
93
|
+
? terminal.itemId
|
|
94
|
+
: terminal.processId !== undefined
|
|
95
|
+
? byProcess.get(terminal.processId)
|
|
96
|
+
: undefined;
|
|
97
|
+
if (itemId) running.set(itemId, terminal.processId);
|
|
98
|
+
}
|
|
99
|
+
// Neither listed yet nor completed: it may still register, or complete.
|
|
100
|
+
const unaccounted = open.filter((itemId) => !running.has(itemId) && !listed.has(itemId));
|
|
101
|
+
if (running.size === 0 && unaccounted.length === 0) return [];
|
|
102
|
+
if (Date.now() >= deadline) return [...running.keys(), ...unaccounted];
|
|
103
|
+
for (const itemId of running.keys()) listed.add(itemId);
|
|
104
|
+
// One refused or unanswered terminate leaves that command running, no other.
|
|
105
|
+
await Promise.allSettled(
|
|
106
|
+
[...running.values()].map((processId) =>
|
|
107
|
+
processId === undefined
|
|
108
|
+
? undefined
|
|
109
|
+
: client.request(
|
|
110
|
+
"thread/backgroundTerminals/terminate",
|
|
111
|
+
{ threadId, processId },
|
|
112
|
+
remaining(),
|
|
113
|
+
),
|
|
114
|
+
),
|
|
115
|
+
);
|
|
116
|
+
await new Promise((done) => setTimeout(done, Math.min(STOP_POLL_MS, remaining())));
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* The commands a turn started, by item id: which still run, which the stream
|
|
122
|
+
* showed ending, and what a cancel of the turn could not stop. When a turn
|
|
123
|
+
* ends cancelled, a command's outcome is unknown when its stop could not be
|
|
124
|
+
* confirmed, or, with nothing stopped, when the stream never showed its end.
|
|
125
|
+
*/
|
|
126
|
+
export class TurnCommands {
|
|
127
|
+
private readonly commands = new Map<string, TurnCommand>();
|
|
128
|
+
/** Completions that came after Codex's turn ended, which its events no longer take. */
|
|
129
|
+
private readonly late: CodexNotification[] = [];
|
|
130
|
+
/** Codex's turn was interrupted: what it started is to stop. */
|
|
131
|
+
private interrupting = false;
|
|
132
|
+
private stopping?: Promise<string[]>;
|
|
133
|
+
|
|
134
|
+
constructor(
|
|
135
|
+
private readonly client: CodexConnection,
|
|
136
|
+
private readonly threadId: string,
|
|
137
|
+
/** How long a stop has to confirm the commands stopped (`cancelGraceMs`). */
|
|
138
|
+
private readonly timeoutMs: number,
|
|
139
|
+
) {}
|
|
140
|
+
|
|
141
|
+
/** Note an item Codex started: a command the turn owns, unless it is another's. */
|
|
142
|
+
started(item: CodexItem | undefined): void {
|
|
143
|
+
if (item?.type !== "commandExecution" || !startsCommand(item.source)) return;
|
|
144
|
+
this.commands.set(item.id, {
|
|
145
|
+
...(typeof item.processId === "string" && { processId: item.processId }),
|
|
146
|
+
done: false,
|
|
147
|
+
shown: false,
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** Note an item Codex completed: whether it was one of the turn's commands. */
|
|
152
|
+
completed(item: CodexItem | undefined): boolean {
|
|
153
|
+
const command = item && this.commands.get(item.id);
|
|
154
|
+
if (command) command.done = true;
|
|
155
|
+
return Boolean(command);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** Note an event reaching the stream: a command's completion shows how it ended. */
|
|
159
|
+
shown(event: CodexRawEvent): void {
|
|
160
|
+
if (isSyntheticEvent(event) || event.method !== "item/completed") return;
|
|
161
|
+
const item = codexItem(event);
|
|
162
|
+
const command = item && this.commands.get(item.id);
|
|
163
|
+
if (command) command.shown = true;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** Keep a completion that came after Codex's turn ended, for the stream to show later. */
|
|
167
|
+
holdLate(n: CodexNotification): void {
|
|
168
|
+
this.late.push(n);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* The completions kept since Codex's turn ended, to the stream, until none
|
|
173
|
+
* is left: shown before anything that follows. Once Codex's turn was
|
|
174
|
+
* interrupted there are none to show: a kill's completion is no news.
|
|
175
|
+
*/
|
|
176
|
+
*drainLate(): Generator<CodexRawEvent> {
|
|
177
|
+
if (this.interrupting) return;
|
|
178
|
+
for (let done = this.late.shift(); done; done = this.late.shift()) {
|
|
179
|
+
this.shown(done);
|
|
180
|
+
yield done;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** Whether a command of the turn still runs, as far as Codex said. */
|
|
185
|
+
get running(): boolean {
|
|
186
|
+
return [...this.commands.values()].some((command) => !command.done);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** Codex's turn was interrupted, by a cancel or by Codex itself: what it started is to stop. */
|
|
190
|
+
interrupted(): void {
|
|
191
|
+
this.interrupting = true;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Stop what the turn started, once Codex's turn has ended, at most once.
|
|
196
|
+
* Codex's own shutdown (EOF) stops every terminal it keeps: the last
|
|
197
|
+
* resort, which also ends this session's process (the next turn resumes
|
|
198
|
+
* the thread). Resolves the commands whose stop was not confirmed.
|
|
199
|
+
*/
|
|
200
|
+
stop(): Promise<string[]> {
|
|
201
|
+
this.interrupting = true;
|
|
202
|
+
this.stopping ??= (async () => {
|
|
203
|
+
const unstopped = await stopTurnCommands(
|
|
204
|
+
this.client,
|
|
205
|
+
this.threadId,
|
|
206
|
+
this.commands,
|
|
207
|
+
this.timeoutMs,
|
|
208
|
+
);
|
|
209
|
+
return unstopped.length && (await this.client.closeAndWait(this.timeoutMs)) ? [] : unstopped;
|
|
210
|
+
})();
|
|
211
|
+
return this.stopping;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* The commands whose outcome a turn that ended cancelled leaves unknown:
|
|
216
|
+
* the ones a stop could not confirm stopped, or, when nothing was to stop,
|
|
217
|
+
* the ones whose end the stream never showed, left running on purpose.
|
|
218
|
+
*/
|
|
219
|
+
async outcomeUnknown(): Promise<string[]> {
|
|
220
|
+
if (this.interrupting) return this.stop();
|
|
221
|
+
return [...this.commands].filter(([, command]) => !command.shown).map(([itemId]) => itemId);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** The commands whose item never completed. */
|
|
225
|
+
notDone(): string[] {
|
|
226
|
+
return [...this.commands].filter(([, command]) => !command.done).map(([itemId]) => itemId);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { type CodexCredential, tokenDigest } from "../../../protocol/index.ts";
|
|
2
|
+
import { configFingerprint } from "../config-fingerprint.ts";
|
|
3
|
+
import { type CredentialSource, credentialFor } from "../credentials.ts";
|
|
4
|
+
import type { CodexConnection } from "./client.ts";
|
|
5
|
+
import type { CodexSession } from "./session.ts";
|
|
6
|
+
|
|
7
|
+
/** ChatGPT tokens as Codex's app-server takes them (`chatgptAuthTokens`). */
|
|
8
|
+
interface ChatGptTokens {
|
|
9
|
+
accessToken: string;
|
|
10
|
+
chatgptAccountId: string;
|
|
11
|
+
chatgptPlanType?: string | null;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* What a Codex process logs in with when the engine chooses: a key, or
|
|
16
|
+
* ChatGPT tokens held in its memory and how to get fresher ones. None: the
|
|
17
|
+
* CLI's own login.
|
|
18
|
+
*/
|
|
19
|
+
export type CodexAuth =
|
|
20
|
+
| { type: "apiKey"; apiKey: string }
|
|
21
|
+
| { type: "chatgptAuthTokens"; tokens: ChatGptTokens; refresh(): Promise<ChatGptTokens> };
|
|
22
|
+
|
|
23
|
+
const chatGptTokens = (credential: Extract<CodexCredential, { type: "chatgpt" }>) => ({
|
|
24
|
+
accessToken: credential.accessToken,
|
|
25
|
+
chatgptAccountId: credential.accountId,
|
|
26
|
+
...(credential.plan !== undefined && { chatgptPlanType: credential.plan }),
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
// @ref LLP 0012#what-the-engine-holds-for-the-third — no refresh token enters the app-server
|
|
30
|
+
/**
|
|
31
|
+
* The login a turn or a model list runs Codex on: the credential the product
|
|
32
|
+
* holds (`credentials`), an API key or a ChatGPT access token kept in the
|
|
33
|
+
* app-server's memory, which asks the product again (with the token it
|
|
34
|
+
* refused) when it needs a fresher one; without one, the turn's own `apiKey`;
|
|
35
|
+
* without that, the CLI's own login.
|
|
36
|
+
*/
|
|
37
|
+
export async function authFor(ask: {
|
|
38
|
+
credentials: CredentialSource | undefined;
|
|
39
|
+
/** Absent for a model listing of no session: no credential is asked for. */
|
|
40
|
+
sessionId: string | undefined;
|
|
41
|
+
turnId?: string;
|
|
42
|
+
apiKey: string | undefined;
|
|
43
|
+
signal?: AbortSignal;
|
|
44
|
+
}): Promise<CodexAuth | undefined> {
|
|
45
|
+
const { credentials, sessionId, turnId, apiKey, signal } = ask;
|
|
46
|
+
const ownKey = (): CodexAuth | undefined => (apiKey ? { type: "apiKey", apiKey } : undefined);
|
|
47
|
+
if (!credentials || !sessionId) return ownKey();
|
|
48
|
+
const request = { harness: "codex-app-server" as const, sessionId, ...(turnId && { turnId }) };
|
|
49
|
+
const credential = await credentialFor(credentials, request, signal);
|
|
50
|
+
if (!credential) return ownKey();
|
|
51
|
+
if (credential.type === "api_key") return { type: "apiKey", apiKey: credential.apiKey };
|
|
52
|
+
let current = credential;
|
|
53
|
+
return {
|
|
54
|
+
type: "chatgptAuthTokens",
|
|
55
|
+
tokens: chatGptTokens(current),
|
|
56
|
+
refresh: async () => {
|
|
57
|
+
const fresher = await credentialFor(credentials, {
|
|
58
|
+
...request,
|
|
59
|
+
rejectedSha256: await tokenDigest(current.accessToken),
|
|
60
|
+
});
|
|
61
|
+
if (fresher?.type !== "chatgpt") {
|
|
62
|
+
throw new Error("The product holds no ChatGPT credential for this session any more");
|
|
63
|
+
}
|
|
64
|
+
current = fresher;
|
|
65
|
+
return chatGptTokens(current);
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Who a process is logged in as: an account or key change needs a new process. */
|
|
71
|
+
export function authFingerprint(auth: CodexAuth | undefined): string | undefined {
|
|
72
|
+
if (!auth) return undefined;
|
|
73
|
+
return configFingerprint(
|
|
74
|
+
auth.type === "apiKey" ? auth : { type: auth.type, accountId: auth.tokens.chatgptAccountId },
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Log the account in on Codex's process: without one, it runs the native login. */
|
|
79
|
+
export async function logIn(
|
|
80
|
+
client: Pick<CodexConnection, "request">,
|
|
81
|
+
auth: CodexAuth | undefined,
|
|
82
|
+
): Promise<void> {
|
|
83
|
+
if (!auth) return;
|
|
84
|
+
await client.request(
|
|
85
|
+
"account/login/start",
|
|
86
|
+
auth.type === "apiKey"
|
|
87
|
+
? { type: "apiKey", apiKey: auth.apiKey }
|
|
88
|
+
: {
|
|
89
|
+
type: "chatgptAuthTokens",
|
|
90
|
+
accessToken: auth.tokens.accessToken,
|
|
91
|
+
chatgptAccountId: auth.tokens.chatgptAccountId,
|
|
92
|
+
chatgptPlanType: auth.tokens.chatgptPlanType ?? null,
|
|
93
|
+
},
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Codex asks for fresh ChatGPT tokens (`account/chatgptAuthTokens/refresh`):
|
|
99
|
+
* the product's source renews them, for the account the turn runs as and
|
|
100
|
+
* never another, and only while that turn is the session's.
|
|
101
|
+
*/
|
|
102
|
+
export async function refreshTokens(
|
|
103
|
+
session: CodexSession,
|
|
104
|
+
params: Record<string, unknown>,
|
|
105
|
+
): Promise<Record<string, unknown>> {
|
|
106
|
+
const turn = session.turn;
|
|
107
|
+
if (turn?.auth?.type !== "chatgptAuthTokens" || turn.signal.aborted || session.client.closed) {
|
|
108
|
+
throw new Error("Codex ChatGPT refresh has no active authenticated turn");
|
|
109
|
+
}
|
|
110
|
+
const accountId = turn.auth.tokens.chatgptAccountId;
|
|
111
|
+
if (params.previousAccountId != null && params.previousAccountId !== accountId) {
|
|
112
|
+
throw new Error("Codex ChatGPT refresh account does not match the selected account");
|
|
113
|
+
}
|
|
114
|
+
const tokens = await turn.auth.refresh();
|
|
115
|
+
if (session.turn !== turn || turn.signal.aborted || session.client.closed) {
|
|
116
|
+
throw new Error("Codex ChatGPT refresh outlived its turn");
|
|
117
|
+
}
|
|
118
|
+
if (tokens.chatgptAccountId !== accountId) {
|
|
119
|
+
throw new Error("Codex ChatGPT refresh returned a different account");
|
|
120
|
+
}
|
|
121
|
+
return {
|
|
122
|
+
accessToken: tokens.accessToken,
|
|
123
|
+
chatgptAccountId: tokens.chatgptAccountId,
|
|
124
|
+
chatgptPlanType: tokens.chatgptPlanType ?? null,
|
|
125
|
+
};
|
|
126
|
+
}
|