@myagentroam/agent 0.11.33 → 0.11.34
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/dist/cli/catalog-file.d.ts +78 -0
- package/dist/cli/catalog-file.js +102 -0
- package/dist/cli/codex-auth-file.d.ts +1 -1
- package/dist/cli/codex-auth-file.js +9 -10
- package/dist/cli/main.js +44 -54
- package/dist/error.d.ts +15 -0
- package/dist/error.js +74 -0
- package/dist/extensions/agents.d.ts +100 -0
- package/dist/extensions/agents.js +121 -0
- package/dist/extensions/capabilities.d.ts +4 -0
- package/dist/extensions/commands.d.ts +23 -0
- package/dist/extensions/commands.js +45 -0
- package/dist/extensions/compaction.d.ts +36 -0
- package/dist/extensions/contracts.d.ts +147 -0
- package/dist/extensions/data.d.ts +81 -0
- package/dist/extensions/data.js +78 -0
- package/dist/extensions/environment.d.ts +39 -0
- package/dist/extensions/environment.js +52 -0
- package/dist/extensions/files.d.ts +68 -0
- package/dist/extensions/files.js +26 -0
- package/dist/extensions/hooks.d.ts +32 -0
- package/dist/extensions/host-ports.d.ts +24 -0
- package/dist/extensions/manager.d.ts +42 -0
- package/dist/extensions/manager.js +457 -0
- package/dist/extensions/mcp.d.ts +116 -0
- package/dist/extensions/mcp.js +434 -0
- package/dist/extensions/model-request.d.ts +65 -0
- package/dist/extensions/model-request.js +197 -0
- package/dist/extensions/model-stream.d.ts +18 -0
- package/dist/extensions/model-stream.js +108 -0
- package/dist/extensions/models.d.ts +46 -0
- package/dist/extensions/models.js +7 -0
- package/dist/extensions/network.d.ts +40 -0
- package/dist/extensions/network.js +97 -0
- package/dist/extensions/operation.d.ts +32 -0
- package/dist/extensions/processes.d.ts +64 -0
- package/dist/extensions/processes.js +284 -0
- package/dist/extensions/prompt-composition.d.ts +38 -0
- package/dist/extensions/prompt-composition.js +100 -0
- package/dist/extensions/prompt-input.d.ts +30 -0
- package/dist/extensions/prompt-input.js +48 -0
- package/dist/extensions/prompts.d.ts +31 -0
- package/dist/extensions/prompts.js +88 -0
- package/dist/extensions/resources.d.ts +26 -0
- package/dist/extensions/resources.js +221 -0
- package/dist/extensions/services.d.ts +31 -0
- package/dist/extensions/services.js +61 -0
- package/dist/extensions/session-api.d.ts +6 -0
- package/dist/extensions/session-api.js +52 -0
- package/dist/extensions/session-input.d.ts +184 -0
- package/dist/extensions/session-input.js +422 -0
- package/dist/extensions/session-messages.d.ts +41 -0
- package/dist/extensions/session-messages.js +76 -0
- package/dist/extensions/session-tools.d.ts +32 -0
- package/dist/extensions/session-tools.js +208 -0
- package/dist/extensions/session.d.ts +60 -0
- package/dist/extensions/session.js +265 -0
- package/dist/extensions/settings.d.ts +76 -0
- package/dist/extensions/settings.js +115 -0
- package/dist/extensions/store.d.ts +10 -0
- package/dist/extensions/store.js +131 -0
- package/dist/extensions/tool-runtime.d.ts +16 -0
- package/dist/extensions/tools.d.ts +80 -0
- package/dist/extensions/tools.js +270 -0
- package/dist/extensions.d.ts +22 -0
- package/dist/host/contracts.d.ts +58 -0
- package/dist/host/contracts.js +18 -0
- package/dist/host/description.d.ts +19 -0
- package/dist/host/local-agent-host.d.ts +96 -0
- package/dist/host/local-agent-host.js +429 -0
- package/dist/host/local-exec-tool.d.ts +26 -0
- package/dist/host/local-exec-tool.js +686 -0
- package/dist/host/local-file-access.d.ts +30 -0
- package/dist/host/local-file-access.js +107 -0
- package/dist/host/local-network-access.d.ts +6 -0
- package/dist/host/local-network-access.js +101 -0
- package/dist/host/local-process-executor.d.ts +5 -0
- package/dist/host/local-process-executor.js +17 -0
- package/dist/host/local-session-environment.d.ts +18 -0
- package/dist/host/local-shell.d.ts +3 -0
- package/dist/host/local-tool-registry.d.ts +34 -0
- package/dist/host/local-tool-registry.js +134 -0
- package/dist/host/network.d.ts +5 -0
- package/dist/host/network.js +42 -0
- package/dist/host/process-control.d.ts +2 -0
- package/dist/host/process-control.js +26 -0
- package/dist/host/skill-contract.d.ts +6 -0
- package/dist/host.d.ts +17 -0
- package/dist/host.js +3 -0
- package/dist/index.d.ts +26 -51
- package/dist/index.js +9 -12
- package/dist/model/anthropic-messages.d.ts +10 -0
- package/dist/model/anthropic-messages.js +692 -0
- package/dist/model/anthropic-thinking.js +33 -0
- package/dist/model/configuration.d.ts +111 -0
- package/dist/model/configuration.js +201 -0
- package/dist/model/contracts.d.ts +229 -0
- package/dist/model/http.d.ts +13 -0
- package/dist/model/http.js +119 -0
- package/dist/model/image-contract.d.ts +10 -0
- package/dist/model/image.d.ts +9 -0
- package/dist/model/inline-media-budget.d.ts +7 -0
- package/dist/model/inline-media-budget.js +36 -0
- package/dist/model/input-images.d.ts +3 -0
- package/dist/model/input-images.js +16 -0
- package/dist/model/openai-responses.d.ts +59 -0
- package/dist/model/openai-responses.js +1586 -0
- package/dist/model/recovery.js +80 -0
- package/dist/model/request-configuration.d.ts +17 -0
- package/dist/model/request-configuration.js +47 -0
- package/dist/model/responses-event-projection.d.ts +24 -0
- package/dist/model/responses-event-projection.js +194 -0
- package/dist/model/responses-request.d.ts +5 -0
- package/dist/model/responses-request.js +363 -0
- package/dist/model/responses-session-cache.d.ts +33 -0
- package/dist/model/responses-session-cache.js +64 -0
- package/dist/model/runtime-credential.d.ts +1 -0
- package/dist/model/runtime-credential.js +1 -0
- package/dist/model/sse.js +281 -0
- package/dist/model/text-blocks.js +40 -0
- package/dist/model/tool-declarations.d.ts +4 -0
- package/dist/model/tool-definition-projection.d.ts +6 -0
- package/dist/model/tool-definition-projection.js +11 -0
- package/dist/model/tool-schema.d.ts +2 -0
- package/dist/model/tool-schema.js +17 -0
- package/dist/model/transport-test.d.ts +10 -0
- package/dist/model/transport-test.js +27 -0
- package/dist/model/usage-observer.d.ts +5 -0
- package/dist/model/usage.d.ts +40 -0
- package/dist/network/credential-runtime.d.ts +8 -0
- package/dist/network/credential-runtime.js +72 -0
- package/dist/network/credentials.d.ts +20 -0
- package/dist/network/credentials.js +18 -0
- package/dist/network/http.d.ts +20 -0
- package/dist/network/http.js +421 -0
- package/dist/network/product-user-agent.d.ts +5 -0
- package/dist/network/product-user-agent.js +22 -0
- package/dist/network/transport-error.d.ts +5 -0
- package/dist/network/transport-error.js +31 -0
- package/dist/process/contracts.d.ts +26 -0
- package/dist/prompts/index.d.ts +20 -0
- package/dist/prompts/index.js +49 -0
- package/dist/prompts/prompt-sections.d.ts +6 -0
- package/dist/prompts/prompt-sections.js +6 -0
- package/dist/prompts/read-context.js +152 -0
- package/dist/runtime/compaction/contracts.d.ts +23 -0
- package/dist/runtime/compaction/coordinator.js +88 -0
- package/dist/runtime/compaction/generate.d.ts +31 -0
- package/dist/runtime/compaction/generate.js +84 -0
- package/dist/runtime/compaction/history.d.ts +16 -0
- package/dist/runtime/compaction/operation.d.ts +30 -0
- package/dist/runtime/compaction/operation.js +131 -0
- package/dist/runtime/compaction/requests.d.ts +16 -0
- package/dist/runtime/compaction/requests.js +114 -0
- package/dist/runtime/compaction/retained-context.d.ts +6 -0
- package/dist/runtime/compaction/retained-context.js +48 -0
- package/dist/runtime/compaction/smart.d.ts +14 -0
- package/dist/runtime/compaction/smart.js +59 -0
- package/dist/runtime/context/contextual-blocks.d.ts +31 -0
- package/dist/runtime/context/contextual-blocks.js +158 -0
- package/dist/runtime/context/environment.d.ts +5 -0
- package/dist/runtime/context/gc-candidates-code.d.ts +6 -0
- package/dist/runtime/context/gc-candidates-code.js +102 -0
- package/dist/runtime/context/gc-candidates-exec.d.ts +6 -0
- package/dist/runtime/context/gc-candidates-exec.js +131 -0
- package/dist/runtime/context/gc-candidates-read-patch.d.ts +7 -0
- package/dist/runtime/context/gc-candidates-read-patch.js +118 -0
- package/dist/runtime/context/gc-candidates-subagent.d.ts +6 -0
- package/dist/runtime/context/gc-candidates-subagent.js +100 -0
- package/dist/runtime/context/gc-candidates-web.d.ts +6 -0
- package/dist/runtime/context/gc-candidates-web.js +58 -0
- package/dist/runtime/context/gc-candidates.d.ts +12 -0
- package/dist/runtime/context/gc-candidates.js +192 -0
- package/dist/runtime/context/gc-contracts.d.ts +57 -0
- package/dist/runtime/context/gc-economics.d.ts +48 -0
- package/dist/runtime/context/gc-economics.js +161 -0
- package/dist/runtime/context/gc-runtime.d.ts +43 -0
- package/dist/runtime/context/gc-tokens.d.ts +26 -0
- package/dist/runtime/context/gc-tokens.js +106 -0
- package/dist/runtime/context/gc.d.ts +7 -0
- package/dist/runtime/context/gc.js +552 -0
- package/dist/runtime/context/instructions.d.ts +37 -0
- package/dist/runtime/context/instructions.js +297 -0
- package/dist/runtime/context/normalize.d.ts +5 -0
- package/dist/runtime/context/projection.d.ts +23 -0
- package/dist/runtime/context/projection.js +284 -0
- package/dist/runtime/context/request-budget.d.ts +5 -0
- package/dist/runtime/context/request-budget.js +25 -0
- package/dist/runtime/context/restore.d.ts +27 -0
- package/dist/runtime/context/restore.js +186 -0
- package/dist/runtime/context/usage.d.ts +17 -0
- package/dist/runtime/context/usage.js +124 -0
- package/dist/runtime/context/world-state.d.ts +34 -0
- package/dist/runtime/context/world-state.js +232 -0
- package/dist/runtime/contracts.d.ts +29 -0
- package/dist/runtime/event-projection.d.ts +14 -0
- package/dist/runtime/event-projection.js +87 -0
- package/dist/runtime/events.d.ts +195 -0
- package/dist/runtime/execution-handle.d.ts +12 -0
- package/dist/runtime/execution-handle.js +1 -0
- package/dist/runtime/execution.d.ts +55 -0
- package/dist/runtime/execution.js +444 -0
- package/dist/runtime/model-progress.js +85 -0
- package/dist/runtime/resource-publication.d.ts +27 -0
- package/dist/runtime/resource-publication.js +60 -0
- package/dist/runtime/tool-round-contract.d.ts +19 -0
- package/dist/runtime/tool-round-contract.js +1 -0
- package/dist/runtime/wait.js +82 -0
- package/dist/sandbox/linux/bubblewrap-sandbox-executor.d.ts +1 -1
- package/dist/sandbox/linux/bubblewrap-sandbox-executor.js +8 -8
- package/dist/sdk/agent.d.ts +7 -0
- package/dist/sdk/agent.js +321 -0
- package/dist/sdk/execution-code-mode.d.ts +18 -0
- package/dist/sdk/execution-code-mode.js +51 -0
- package/dist/sdk/execution-compact-commit.d.ts +23 -0
- package/dist/sdk/execution-compact-commit.js +41 -0
- package/dist/sdk/execution-compact-extension.d.ts +12 -0
- package/dist/sdk/execution-compact-extension.js +46 -0
- package/dist/sdk/execution-compact-generation.d.ts +17 -0
- package/dist/sdk/execution-compact-generation.js +30 -0
- package/dist/sdk/execution-compaction-flight.d.ts +31 -0
- package/dist/sdk/execution-compaction-flight.js +50 -0
- package/dist/sdk/execution-compaction.d.ts +47 -0
- package/dist/sdk/execution-compaction.js +121 -0
- package/dist/sdk/execution-context-coordinator.d.ts +56 -0
- package/dist/sdk/execution-context-coordinator.js +102 -0
- package/dist/sdk/execution-input.d.ts +58 -0
- package/dist/sdk/execution-input.js +266 -0
- package/dist/sdk/execution-model-context.d.ts +52 -0
- package/dist/sdk/execution-model-context.js +156 -0
- package/dist/sdk/execution-model-lifecycle.d.ts +39 -0
- package/dist/sdk/execution-model-lifecycle.js +73 -0
- package/dist/sdk/execution-model-request.d.ts +40 -0
- package/dist/sdk/execution-model-request.js +45 -0
- package/dist/sdk/execution-round-preparation.d.ts +11 -0
- package/dist/sdk/execution-round-preparation.js +18 -0
- package/dist/sdk/execution-round-request.d.ts +30 -0
- package/dist/sdk/execution-round-request.js +31 -0
- package/dist/sdk/execution-tool-catalog.d.ts +46 -0
- package/dist/sdk/execution-tool-catalog.js +103 -0
- package/dist/sdk/execution-tool-scope.d.ts +67 -0
- package/dist/sdk/execution-tool-scope.js +147 -0
- package/dist/sdk/extension-agent-controller.d.ts +3 -0
- package/dist/sdk/extension-agent-controller.js +69 -0
- package/dist/sdk/extension-agents.d.ts +9 -0
- package/dist/sdk/extension-agents.js +58 -0
- package/dist/sdk/extension-models.d.ts +17 -0
- package/dist/sdk/extension-models.js +142 -0
- package/dist/sdk/extension-session-launch.d.ts +18 -0
- package/dist/sdk/extension-session-launch.js +73 -0
- package/dist/sdk/extension-session.d.ts +30 -0
- package/dist/sdk/extension-session.js +107 -0
- package/dist/sdk/extension-tool-runtime.d.ts +2 -0
- package/dist/sdk/extension-tool-runtime.js +14 -0
- package/dist/sdk/extension-tools.d.ts +16 -0
- package/dist/sdk/extension-tools.js +126 -0
- package/dist/sdk/model-input-projection.d.ts +4 -0
- package/dist/sdk/model-input-projection.js +27 -0
- package/dist/sdk/model-runtime.d.ts +21 -0
- package/dist/sdk/model-runtime.js +91 -0
- package/dist/sdk/native-steer.d.ts +20 -0
- package/dist/sdk/native-steer.js +84 -0
- package/dist/sdk/request-preparation.d.ts +9 -0
- package/dist/sdk/resource-input.d.ts +33 -0
- package/dist/sdk/resource-input.js +402 -0
- package/dist/sdk/session-catalog.d.ts +2 -0
- package/dist/sdk/session-catalog.js +12 -0
- package/dist/sdk/session-composition.d.ts +54 -0
- package/dist/sdk/session-composition.js +1 -0
- package/dist/sdk/session-execution-factory.d.ts +43 -0
- package/dist/sdk/session-execution-factory.js +937 -0
- package/dist/sdk/session-execution.d.ts +163 -0
- package/dist/sdk/session-execution.js +388 -0
- package/dist/sdk/session-extension-messages.d.ts +16 -0
- package/dist/sdk/session-extension-messages.js +64 -0
- package/dist/sdk/session-extension-prompt.d.ts +35 -0
- package/dist/sdk/session-extension-prompt.js +62 -0
- package/dist/sdk/session-maintenance.d.ts +25 -0
- package/dist/sdk/session-maintenance.js +205 -0
- package/dist/sdk/session-model-adapters.d.ts +28 -0
- package/dist/sdk/session-model-adapters.js +96 -0
- package/dist/sdk/session-open.d.ts +22 -0
- package/dist/sdk/session-open.js +44 -0
- package/dist/sdk/session-registry.d.ts +17 -0
- package/dist/sdk/session-registry.js +83 -0
- package/dist/sdk/session-runtime.d.ts +72 -0
- package/dist/sdk/session-runtime.js +205 -0
- package/dist/sdk/session.d.ts +3 -0
- package/dist/sdk/session.js +313 -0
- package/dist/sdk/subagent-backend.d.ts +3 -0
- package/dist/sdk/subagent-backend.js +49 -0
- package/dist/sdk/types.d.ts +80 -0
- package/dist/sdk/types.js +1 -0
- package/dist/session/catalog.d.ts +70 -0
- package/dist/session/catalog.js +194 -0
- package/dist/session/code-observation-contract.d.ts +40 -0
- package/dist/session/code-observation-contract.js +1 -0
- package/dist/session/code-observation-projection.d.ts +54 -0
- package/dist/session/code-observation-projection.js +165 -0
- package/dist/session/code-state.d.ts +27 -0
- package/dist/session/code-state.js +89 -0
- package/dist/session/context-gc-checkpoint.d.ts +26 -0
- package/dist/session/context-gc-checkpoint.js +935 -0
- package/dist/session/context-gc-contract.d.ts +141 -0
- package/dist/session/context-gc-contract.js +1 -0
- package/dist/session/context-gc-observations.d.ts +58 -0
- package/dist/session/context-gc-observations.js +187 -0
- package/dist/session/context-gc-replacements.d.ts +63 -0
- package/dist/session/context-gc-subagent.d.ts +54 -0
- package/dist/session/deferred-input.d.ts +5 -0
- package/dist/session/deferred-input.js +74 -0
- package/dist/session/extension-checkpoint.d.ts +8 -0
- package/dist/session/extension-checkpoint.js +30 -0
- package/dist/session/history-input-limits.d.ts +4 -0
- package/dist/session/history-input-limits.js +4 -0
- package/dist/session/inline-media-contract.d.ts +16 -0
- package/dist/session/inline-media-contract.js +1 -0
- package/dist/session/inline-media.d.ts +26 -0
- package/dist/session/inline-media.js +138 -0
- package/dist/session/model-request.d.ts +14 -0
- package/dist/session/model-request.js +1 -0
- package/dist/session/record-contract.d.ts +19 -0
- package/dist/session/record-contract.js +1 -0
- package/dist/session/record-projection.d.ts +6 -0
- package/dist/session/record-projection.js +80 -0
- package/dist/session/resource-contract.d.ts +9 -0
- package/dist/session/resource-contract.js +1 -0
- package/dist/session/rollback.d.ts +13 -0
- package/dist/session/rollback.js +46 -0
- package/dist/session/store/contracts.d.ts +160 -0
- package/dist/session/store/contracts.js +1 -0
- package/dist/session/store/current-state.d.ts +17 -0
- package/dist/session/store/current-state.js +53 -0
- package/dist/session/store/execution-state.d.ts +13 -0
- package/dist/session/store/execution-state.js +103 -0
- package/dist/session/store/index.d.ts +4 -0
- package/dist/session/store/index.js +2 -0
- package/dist/session/store/jsonl/directory-index.d.ts +26 -0
- package/dist/session/store/jsonl/directory-index.js +283 -0
- package/dist/session/store/jsonl/fs-durability.d.ts +3 -0
- package/dist/session/store/jsonl/fs-durability.js +17 -0
- package/dist/session/store/jsonl/history-replacement.d.ts +48 -0
- package/dist/session/store/jsonl/history-replacement.js +147 -0
- package/dist/session/store/jsonl/history.d.ts +25 -0
- package/dist/session/store/jsonl/history.js +181 -0
- package/dist/session/store/jsonl/images.d.ts +19 -0
- package/dist/session/store/jsonl/images.js +122 -0
- package/dist/session/store/jsonl/index-transaction.d.ts +13 -0
- package/dist/session/store/jsonl/index-transaction.js +87 -0
- package/dist/session/store/jsonl/jsonl-admission.d.ts +10 -0
- package/dist/session/store/jsonl/jsonl-admission.js +205 -0
- package/dist/session/store/jsonl/jsonl-codec.d.ts +11 -0
- package/dist/session/store/jsonl/jsonl-codec.js +138 -0
- package/dist/session/store/jsonl/jsonl-migration.d.ts +12 -0
- package/dist/session/store/jsonl/jsonl-migration.js +105 -0
- package/dist/session/store/jsonl/jsonl-store.d.ts +171 -0
- package/dist/session/store/jsonl/jsonl-store.js +2482 -0
- package/dist/session/store/jsonl/open-recovery.d.ts +13 -0
- package/dist/session/store/jsonl/open-recovery.js +131 -0
- package/dist/session/store/jsonl/read-pins.d.ts +4 -0
- package/dist/session/store/jsonl/read-pins.js +131 -0
- package/dist/session/store/jsonl/record-append.d.ts +16 -0
- package/dist/session/store/jsonl/record-append.js +49 -0
- package/dist/session/store/jsonl/records.d.ts +30 -0
- package/dist/session/store/jsonl/records.js +313 -0
- package/dist/session/store/jsonl/resources.d.ts +19 -0
- package/dist/session/store/jsonl/resources.js +209 -0
- package/dist/session/store/jsonl/store-pool.d.ts +12 -0
- package/dist/session/store/jsonl/store-pool.js +55 -0
- package/dist/session/store/jsonl/transaction-recovery.d.ts +48 -0
- package/dist/session/store/jsonl/transaction-recovery.js +374 -0
- package/dist/session/store/jsonl/tree-maintenance.d.ts +12 -0
- package/dist/session/store/jsonl/tree-maintenance.js +59 -0
- package/dist/session/store/types.d.ts +69 -0
- package/dist/session/store/types.js +1 -0
- package/dist/session/summary.d.ts +18 -0
- package/dist/session/summary.js +1 -0
- package/dist/session/todo.d.ts +11 -0
- package/dist/session/todo.js +30 -0
- package/dist/session/tool-result-resources.d.ts +27 -0
- package/dist/session/tool-result-resources.js +255 -0
- package/dist/session/types.js +1 -0
- package/dist/session/user-history.d.ts +17 -0
- package/dist/session/user-history.js +1 -0
- package/dist/session-store.d.ts +2 -0
- package/dist/session-store.js +1 -0
- package/dist/shared/auto-title.d.ts +3 -3
- package/dist/shared/auto-title.js +6 -6
- package/dist/shared/model-text.d.ts +8 -8
- package/dist/shared/model-text.js +8 -7
- package/dist/storage/file-lock.js +83 -0
- package/dist/subagent/contracts.d.ts +107 -0
- package/dist/subagent/contracts.js +1 -0
- package/dist/subagent/limits.d.ts +17 -0
- package/dist/subagent/limits.js +17 -0
- package/dist/subagent/model-update.js +81 -0
- package/dist/subagent/session-controller.d.ts +148 -0
- package/dist/subagent/session-controller.js +1761 -0
- package/dist/subagent/wait-targets.d.ts +10 -0
- package/dist/subagent/wait-targets.js +26 -0
- package/dist/subagent/wait.d.ts +26 -0
- package/dist/subagent/wait.js +73 -0
- package/dist/tools/agent/agent-cancel.d.ts +7 -0
- package/dist/tools/agent/agent-cancel.js +29 -0
- package/dist/tools/agent/agent-message.d.ts +7 -0
- package/dist/tools/agent/agent-message.js +57 -0
- package/dist/tools/agent/agent-start.d.ts +12 -0
- package/dist/tools/agent/agent-start.js +106 -0
- package/dist/tools/agent/agent-wait.d.ts +7 -0
- package/dist/tools/agent/agent-wait.js +91 -0
- package/dist/tools/agent/question.d.ts +7 -0
- package/dist/tools/agent/question.js +125 -0
- package/dist/tools/agent/skill.d.ts +11 -0
- package/dist/tools/agent/skill.js +38 -0
- package/dist/tools/agent/sleep.d.ts +9 -0
- package/dist/tools/agent/sleep.js +54 -0
- package/dist/tools/agent/smart-compact.d.ts +9 -0
- package/dist/tools/agent/smart-compact.js +23 -0
- package/dist/tools/agent/todo-read.d.ts +14 -0
- package/dist/tools/agent/todo-write.d.ts +14 -0
- package/dist/tools/agent/todo-write.js +66 -0
- package/dist/tools/agent/todo.d.ts +25 -0
- package/dist/tools/agent/todo.js +32 -0
- package/dist/tools/callers.d.ts +7 -0
- package/dist/tools/callers.js +39 -0
- package/dist/tools/code-mode/cells.d.ts +29 -0
- package/dist/tools/code-mode/cells.js +338 -0
- package/dist/tools/code-mode/dispatch.d.ts +20 -0
- package/dist/tools/code-mode/dispatch.js +80 -0
- package/dist/tools/code-mode/engine.d.ts +60 -0
- package/dist/tools/code-mode/engine.js +1 -0
- package/dist/tools/code-mode/index.d.ts +31 -0
- package/dist/tools/code-mode/index.js +751 -0
- package/dist/tools/code-mode/observations.d.ts +8 -0
- package/dist/tools/code-mode/observations.js +64 -0
- package/dist/tools/code-mode/worker.d.ts +1 -0
- package/dist/tools/contracts.d.ts +193 -0
- package/dist/tools/contracts.js +1 -0
- package/dist/tools/definition-projection.d.ts +1 -0
- package/dist/tools/definition-projection.js +1 -0
- package/dist/tools/execution-limits.d.ts +69 -0
- package/dist/tools/execution-limits.js +58 -0
- package/dist/tools/execution.d.ts +30 -0
- package/dist/tools/execution.js +191 -0
- package/dist/tools/local/apply-patch-parser.js +177 -0
- package/dist/tools/local/apply-patch.d.ts +19 -0
- package/dist/tools/local/apply-patch.js +489 -0
- package/dist/tools/local/exec.d.ts +28 -0
- package/dist/tools/local/exec.js +194 -0
- package/dist/tools/local/image-processing.js +146 -0
- package/dist/tools/local/read.d.ts +43 -0
- package/dist/tools/local/read.js +470 -0
- package/dist/tools/local/view-image.d.ts +15 -0
- package/dist/tools/local/view-image.js +317 -0
- package/dist/tools/mcp/catalog.js +220 -0
- package/dist/tools/mcp/history.d.ts +4 -0
- package/dist/tools/mcp/tool-search.d.ts +45 -0
- package/dist/tools/mcp/tool-search.js +294 -0
- package/dist/tools/output-projection.d.ts +18 -0
- package/dist/tools/output-projection.js +341 -0
- package/dist/tools/permission-contract.d.ts +25 -0
- package/dist/tools/permission-contract.js +1 -0
- package/dist/tools/permissions.d.ts +61 -0
- package/dist/tools/permissions.js +237 -0
- package/dist/tools/registry.d.ts +7 -0
- package/dist/tools/result-extension-contract.d.ts +37 -0
- package/dist/tools/result-extension-contract.js +32 -0
- package/dist/tools/result-extensions.d.ts +12 -0
- package/dist/tools/result-extensions.js +104 -0
- package/dist/tools/runtime-context.d.ts +89 -0
- package/dist/tools/runtime-context.js +1 -0
- package/dist/tools/runtime-resource-handles.js +28 -0
- package/dist/tools/runtime.js +77 -0
- package/dist/tools/scheduler.d.ts +12 -0
- package/dist/tools/shared/path-resolver.js +133 -0
- package/dist/tools/wait.d.ts +1 -0
- package/dist/tools/wait.js +1 -0
- package/package.json +15 -3
- package/dist/agent-core/error.d.ts +0 -15
- package/dist/agent-core/error.js +0 -74
- package/dist/agent-core/extensions/agents.d.ts +0 -79
- package/dist/agent-core/extensions/agents.js +0 -186
- package/dist/agent-core/extensions/compaction.d.ts +0 -48
- package/dist/agent-core/extensions/contracts.d.ts +0 -265
- package/dist/agent-core/extensions/data.d.ts +0 -84
- package/dist/agent-core/extensions/data.js +0 -83
- package/dist/agent-core/extensions/environment.d.ts +0 -27
- package/dist/agent-core/extensions/environment.js +0 -52
- package/dist/agent-core/extensions/files.d.ts +0 -64
- package/dist/agent-core/extensions/files.js +0 -26
- package/dist/agent-core/extensions/hooks.d.ts +0 -32
- package/dist/agent-core/extensions/manager.d.ts +0 -39
- package/dist/agent-core/extensions/manager.js +0 -458
- package/dist/agent-core/extensions/mcp.d.ts +0 -99
- package/dist/agent-core/extensions/mcp.js +0 -433
- package/dist/agent-core/extensions/model-request.d.ts +0 -56
- package/dist/agent-core/extensions/model-request.js +0 -197
- package/dist/agent-core/extensions/model-stream.d.ts +0 -9
- package/dist/agent-core/extensions/model-stream.js +0 -108
- package/dist/agent-core/extensions/models.d.ts +0 -43
- package/dist/agent-core/extensions/network.d.ts +0 -32
- package/dist/agent-core/extensions/network.js +0 -97
- package/dist/agent-core/extensions/operation.d.ts +0 -30
- package/dist/agent-core/extensions/processes.d.ts +0 -52
- package/dist/agent-core/extensions/processes.js +0 -284
- package/dist/agent-core/extensions/prompt-composition.d.ts +0 -24
- package/dist/agent-core/extensions/prompt-composition.js +0 -106
- package/dist/agent-core/extensions/prompt-input.d.ts +0 -23
- package/dist/agent-core/extensions/prompt-input.js +0 -48
- package/dist/agent-core/extensions/resources.d.ts +0 -27
- package/dist/agent-core/extensions/resources.js +0 -221
- package/dist/agent-core/extensions/services.d.ts +0 -31
- package/dist/agent-core/extensions/services.js +0 -61
- package/dist/agent-core/extensions/session-input.d.ts +0 -163
- package/dist/agent-core/extensions/session-input.js +0 -411
- package/dist/agent-core/extensions/session-messages.d.ts +0 -41
- package/dist/agent-core/extensions/session-messages.js +0 -76
- package/dist/agent-core/extensions/session.d.ts +0 -69
- package/dist/agent-core/extensions/session.js +0 -607
- package/dist/agent-core/extensions/settings.d.ts +0 -59
- package/dist/agent-core/extensions/settings.js +0 -115
- package/dist/agent-core/extensions/store.d.ts +0 -10
- package/dist/agent-core/extensions/store.js +0 -131
- package/dist/agent-core/extensions/tools.d.ts +0 -61
- package/dist/agent-core/extensions/tools.js +0 -269
- package/dist/agent-core/host/contracts.d.ts +0 -100
- package/dist/agent-core/host/contracts.js +0 -18
- package/dist/agent-core/host/local-agent-host.d.ts +0 -96
- package/dist/agent-core/host/local-agent-host.js +0 -425
- package/dist/agent-core/host/local-file-access.d.ts +0 -30
- package/dist/agent-core/host/local-file-access.js +0 -107
- package/dist/agent-core/host/local-network-access.d.ts +0 -6
- package/dist/agent-core/host/local-network-access.js +0 -101
- package/dist/agent-core/host/local-process-executor.d.ts +0 -29
- package/dist/agent-core/host/local-process-executor.js +0 -41
- package/dist/agent-core/host/local-session-environment.d.ts +0 -18
- package/dist/agent-core/host/local-shell.d.ts +0 -3
- package/dist/agent-core/host/network.d.ts +0 -4
- package/dist/agent-core/host/network.js +0 -6
- package/dist/agent-core/model/anthropic-messages.d.ts +0 -10
- package/dist/agent-core/model/anthropic-messages.js +0 -707
- package/dist/agent-core/model/anthropic-thinking.js +0 -33
- package/dist/agent-core/model/catalog-file.d.ts +0 -80
- package/dist/agent-core/model/catalog-file.js +0 -103
- package/dist/agent-core/model/configuration.d.ts +0 -127
- package/dist/agent-core/model/configuration.js +0 -168
- package/dist/agent-core/model/contracts.d.ts +0 -229
- package/dist/agent-core/model/http.d.ts +0 -27
- package/dist/agent-core/model/http.js +0 -534
- package/dist/agent-core/model/image.d.ts +0 -17
- package/dist/agent-core/model/openai-responses.d.ts +0 -77
- package/dist/agent-core/model/openai-responses.js +0 -2123
- package/dist/agent-core/model/product-user-agent.d.ts +0 -5
- package/dist/agent-core/model/product-user-agent.js +0 -22
- package/dist/agent-core/model/recovery.js +0 -80
- package/dist/agent-core/model/request-configuration.d.ts +0 -17
- package/dist/agent-core/model/request-configuration.js +0 -47
- package/dist/agent-core/model/runtime-credential.d.ts +0 -8
- package/dist/agent-core/model/runtime-credential.js +0 -72
- package/dist/agent-core/model/sse.js +0 -281
- package/dist/agent-core/model/text-blocks.js +0 -40
- package/dist/agent-core/model/tool-declarations.d.ts +0 -4
- package/dist/agent-core/model/transport-test.d.ts +0 -3
- package/dist/agent-core/model/transport-test.js +0 -23
- package/dist/agent-core/model/usage-observer.d.ts +0 -31
- package/dist/agent-core/prompts/index.d.ts +0 -20
- package/dist/agent-core/prompts/index.js +0 -49
- package/dist/agent-core/runtime/compaction/coordinator.js +0 -88
- package/dist/agent-core/runtime/compaction/generate.d.ts +0 -28
- package/dist/agent-core/runtime/compaction/generate.js +0 -84
- package/dist/agent-core/runtime/compaction/history.d.ts +0 -16
- package/dist/agent-core/runtime/compaction/operation.d.ts +0 -29
- package/dist/agent-core/runtime/compaction/operation.js +0 -131
- package/dist/agent-core/runtime/compaction/requests.d.ts +0 -16
- package/dist/agent-core/runtime/compaction/requests.js +0 -114
- package/dist/agent-core/runtime/compaction/retained-context.d.ts +0 -6
- package/dist/agent-core/runtime/compaction/retained-context.js +0 -47
- package/dist/agent-core/runtime/compaction/smart.d.ts +0 -14
- package/dist/agent-core/runtime/compaction/smart.js +0 -59
- package/dist/agent-core/runtime/context/contextual-blocks.d.ts +0 -30
- package/dist/agent-core/runtime/context/contextual-blocks.js +0 -146
- package/dist/agent-core/runtime/context/environment.d.ts +0 -5
- package/dist/agent-core/runtime/context/gc-runtime.d.ts +0 -38
- package/dist/agent-core/runtime/context/gc-tokens.d.ts +0 -25
- package/dist/agent-core/runtime/context/gc-tokens.js +0 -103
- package/dist/agent-core/runtime/context/gc.d.ts +0 -75
- package/dist/agent-core/runtime/context/gc.js +0 -1386
- package/dist/agent-core/runtime/context/instructions.d.ts +0 -37
- package/dist/agent-core/runtime/context/instructions.js +0 -297
- package/dist/agent-core/runtime/context/normalize.d.ts +0 -5
- package/dist/agent-core/runtime/context/projection.d.ts +0 -23
- package/dist/agent-core/runtime/context/projection.js +0 -284
- package/dist/agent-core/runtime/context/request-budget.d.ts +0 -4
- package/dist/agent-core/runtime/context/request-budget.js +0 -25
- package/dist/agent-core/runtime/context/restore.d.ts +0 -25
- package/dist/agent-core/runtime/context/restore.js +0 -186
- package/dist/agent-core/runtime/context/usage.d.ts +0 -17
- package/dist/agent-core/runtime/context/usage.js +0 -124
- package/dist/agent-core/runtime/context/world-state.d.ts +0 -33
- package/dist/agent-core/runtime/context/world-state.js +0 -232
- package/dist/agent-core/runtime/contracts.d.ts +0 -41
- package/dist/agent-core/runtime/event-projection.d.ts +0 -14
- package/dist/agent-core/runtime/event-projection.js +0 -87
- package/dist/agent-core/runtime/events.d.ts +0 -197
- package/dist/agent-core/runtime/execution-handle.d.ts +0 -12
- package/dist/agent-core/runtime/execution.d.ts +0 -50
- package/dist/agent-core/runtime/execution.js +0 -445
- package/dist/agent-core/runtime/model-progress.js +0 -85
- package/dist/agent-core/runtime/resource-publication.d.ts +0 -8
- package/dist/agent-core/runtime/resource-publication.js +0 -60
- package/dist/agent-core/sdk/agent.d.ts +0 -11
- package/dist/agent-core/sdk/agent.js +0 -474
- package/dist/agent-core/sdk/extension-agents.d.ts +0 -9
- package/dist/agent-core/sdk/extension-agents.js +0 -58
- package/dist/agent-core/sdk/extension-models.d.ts +0 -13
- package/dist/agent-core/sdk/extension-models.js +0 -137
- package/dist/agent-core/sdk/extension-session.d.ts +0 -30
- package/dist/agent-core/sdk/extension-session.js +0 -105
- package/dist/agent-core/sdk/extension-tools.d.ts +0 -16
- package/dist/agent-core/sdk/extension-tools.js +0 -126
- package/dist/agent-core/sdk/model-runtime.d.ts +0 -18
- package/dist/agent-core/sdk/model-runtime.js +0 -93
- package/dist/agent-core/sdk/native-steer.d.ts +0 -20
- package/dist/agent-core/sdk/native-steer.js +0 -84
- package/dist/agent-core/sdk/request-preparation.d.ts +0 -7
- package/dist/agent-core/sdk/resource-input.d.ts +0 -32
- package/dist/agent-core/sdk/resource-input.js +0 -403
- package/dist/agent-core/sdk/session.d.ts +0 -94
- package/dist/agent-core/sdk/session.js +0 -2377
- package/dist/agent-core/sdk/subagent-backend.d.ts +0 -3
- package/dist/agent-core/sdk/subagent-backend.js +0 -49
- package/dist/agent-core/sdk/types.d.ts +0 -81
- package/dist/agent-core/session/catalog.d.ts +0 -99
- package/dist/agent-core/session/catalog.js +0 -200
- package/dist/agent-core/session/context-gc-checkpoint.d.ts +0 -165
- package/dist/agent-core/session/context-gc-checkpoint.js +0 -935
- package/dist/agent-core/session/context-gc-observations.d.ts +0 -60
- package/dist/agent-core/session/context-gc-observations.js +0 -187
- package/dist/agent-core/session/context-gc-replacements.d.ts +0 -62
- package/dist/agent-core/session/context-gc-subagent.d.ts +0 -53
- package/dist/agent-core/session/deferred-input.d.ts +0 -5
- package/dist/agent-core/session/deferred-input.js +0 -74
- package/dist/agent-core/session/inline-media.d.ts +0 -45
- package/dist/agent-core/session/inline-media.js +0 -163
- package/dist/agent-core/session/rollback.d.ts +0 -13
- package/dist/agent-core/session/rollback.js +0 -46
- package/dist/agent-core/session/store/contracts.d.ts +0 -158
- package/dist/agent-core/session/store/current-state.d.ts +0 -17
- package/dist/agent-core/session/store/current-state.js +0 -53
- package/dist/agent-core/session/store/directory-index.d.ts +0 -18
- package/dist/agent-core/session/store/directory-index.js +0 -169
- package/dist/agent-core/session/store/execution-state.d.ts +0 -18
- package/dist/agent-core/session/store/execution-state.js +0 -175
- package/dist/agent-core/session/store/history.d.ts +0 -25
- package/dist/agent-core/session/store/history.js +0 -181
- package/dist/agent-core/session/store/images.d.ts +0 -19
- package/dist/agent-core/session/store/images.js +0 -110
- package/dist/agent-core/session/store/index.d.ts +0 -4
- package/dist/agent-core/session/store/index.js +0 -2
- package/dist/agent-core/session/store/jsonl-store.d.ts +0 -171
- package/dist/agent-core/session/store/jsonl-store.js +0 -2078
- package/dist/agent-core/session/store/records.d.ts +0 -23
- package/dist/agent-core/session/store/records.js +0 -261
- package/dist/agent-core/session/store/resources.d.ts +0 -26
- package/dist/agent-core/session/store/resources.js +0 -197
- package/dist/agent-core/session/store/types.d.ts +0 -82
- package/dist/agent-core/session/tool-result-resources.d.ts +0 -26
- package/dist/agent-core/session/tool-result-resources.js +0 -256
- package/dist/agent-core/session/transaction-recovery.d.ts +0 -16
- package/dist/agent-core/session/transaction-recovery.js +0 -135
- package/dist/agent-core/storage/file-lock.js +0 -83
- package/dist/agent-core/subagent/contracts.d.ts +0 -117
- package/dist/agent-core/subagent/model-update.js +0 -81
- package/dist/agent-core/subagent/scheduler.d.ts +0 -135
- package/dist/agent-core/subagent/scheduler.js +0 -369
- package/dist/agent-core/subagent/session-controller.d.ts +0 -138
- package/dist/agent-core/subagent/session-controller.js +0 -1594
- package/dist/agent-core/subagent/wait.d.ts +0 -14
- package/dist/agent-core/subagent/wait.js +0 -32
- package/dist/agent-core/tools/agent/agent-cancel.d.ts +0 -7
- package/dist/agent-core/tools/agent/agent-cancel.js +0 -29
- package/dist/agent-core/tools/agent/agent-message.d.ts +0 -7
- package/dist/agent-core/tools/agent/agent-message.js +0 -57
- package/dist/agent-core/tools/agent/agent-start.d.ts +0 -12
- package/dist/agent-core/tools/agent/agent-start.js +0 -106
- package/dist/agent-core/tools/agent/agent-wait.d.ts +0 -7
- package/dist/agent-core/tools/agent/agent-wait.js +0 -91
- package/dist/agent-core/tools/agent/question.d.ts +0 -7
- package/dist/agent-core/tools/agent/question.js +0 -125
- package/dist/agent-core/tools/agent/skill.d.ts +0 -11
- package/dist/agent-core/tools/agent/skill.js +0 -38
- package/dist/agent-core/tools/agent/sleep.d.ts +0 -12
- package/dist/agent-core/tools/agent/sleep.js +0 -54
- package/dist/agent-core/tools/agent/smart-compact.d.ts +0 -9
- package/dist/agent-core/tools/agent/smart-compact.js +0 -23
- package/dist/agent-core/tools/agent/todo-read.d.ts +0 -14
- package/dist/agent-core/tools/agent/todo-write.d.ts +0 -14
- package/dist/agent-core/tools/agent/todo-write.js +0 -66
- package/dist/agent-core/tools/agent/todo.d.ts +0 -26
- package/dist/agent-core/tools/agent/todo.js +0 -60
- package/dist/agent-core/tools/callers.d.ts +0 -6
- package/dist/agent-core/tools/callers.js +0 -39
- package/dist/agent-core/tools/code-mode/cells.d.ts +0 -29
- package/dist/agent-core/tools/code-mode/cells.js +0 -337
- package/dist/agent-core/tools/code-mode/dispatch.d.ts +0 -20
- package/dist/agent-core/tools/code-mode/dispatch.js +0 -80
- package/dist/agent-core/tools/code-mode/engine.d.ts +0 -60
- package/dist/agent-core/tools/code-mode/index.d.ts +0 -29
- package/dist/agent-core/tools/code-mode/index.js +0 -749
- package/dist/agent-core/tools/code-mode/observations.d.ts +0 -98
- package/dist/agent-core/tools/code-mode/observations.js +0 -228
- package/dist/agent-core/tools/code-mode/state.d.ts +0 -9
- package/dist/agent-core/tools/code-mode/state.js +0 -82
- package/dist/agent-core/tools/contracts.d.ts +0 -211
- package/dist/agent-core/tools/definition-projection.d.ts +0 -7
- package/dist/agent-core/tools/definition-projection.js +0 -26
- package/dist/agent-core/tools/execution-limits.d.ts +0 -69
- package/dist/agent-core/tools/execution-limits.js +0 -69
- package/dist/agent-core/tools/execution.d.ts +0 -46
- package/dist/agent-core/tools/execution.js +0 -191
- package/dist/agent-core/tools/local/apply-patch-parser.js +0 -177
- package/dist/agent-core/tools/local/apply-patch.d.ts +0 -19
- package/dist/agent-core/tools/local/apply-patch.js +0 -489
- package/dist/agent-core/tools/local/exec.d.ts +0 -53
- package/dist/agent-core/tools/local/exec.js +0 -877
- package/dist/agent-core/tools/local/image-processing.js +0 -146
- package/dist/agent-core/tools/local/read-context.js +0 -152
- package/dist/agent-core/tools/local/read.d.ts +0 -43
- package/dist/agent-core/tools/local/read.js +0 -470
- package/dist/agent-core/tools/local/registry.d.ts +0 -34
- package/dist/agent-core/tools/local/registry.js +0 -132
- package/dist/agent-core/tools/local/view-image.d.ts +0 -15
- package/dist/agent-core/tools/local/view-image.js +0 -317
- package/dist/agent-core/tools/local/workspace-tools.d.ts +0 -12
- package/dist/agent-core/tools/local/workspace-tools.js +0 -28
- package/dist/agent-core/tools/mcp/catalog.js +0 -220
- package/dist/agent-core/tools/mcp/history.d.ts +0 -4
- package/dist/agent-core/tools/mcp/tool-search.d.ts +0 -45
- package/dist/agent-core/tools/mcp/tool-search.js +0 -294
- package/dist/agent-core/tools/output-projection.d.ts +0 -18
- package/dist/agent-core/tools/output-projection.js +0 -341
- package/dist/agent-core/tools/permissions.d.ts +0 -85
- package/dist/agent-core/tools/permissions.js +0 -237
- package/dist/agent-core/tools/registry.d.ts +0 -7
- package/dist/agent-core/tools/result-extensions.d.ts +0 -46
- package/dist/agent-core/tools/result-extensions.js +0 -134
- package/dist/agent-core/tools/runtime-context.d.ts +0 -74
- package/dist/agent-core/tools/runtime-resource-handles.js +0 -28
- package/dist/agent-core/tools/runtime.js +0 -77
- package/dist/agent-core/tools/scheduler.d.ts +0 -12
- package/dist/agent-core/tools/shared/path-resolver.js +0 -133
- package/dist/agent-core/tools/wait.js +0 -82
- /package/dist/{agent-core/extensions/models.js → extensions/capabilities.js} +0 -0
- /package/dist/{agent-core/extensions → extensions}/compaction.js +0 -0
- /package/dist/{agent-core/model → extensions}/contracts.js +0 -0
- /package/dist/{agent-core/extensions → extensions}/hooks.js +0 -0
- /package/dist/{agent-core/runtime/context/contracts.js → extensions/host-ports.js} +0 -0
- /package/dist/{agent-core/extensions → extensions}/operation.js +0 -0
- /package/dist/{agent-core/runtime/contracts.js → extensions/tool-runtime.js} +0 -0
- /package/dist/{agent-core/extensions/contracts.js → extensions.js} +0 -0
- /package/dist/{agent-core/runtime/events.js → host/description.js} +0 -0
- /package/dist/{agent-core/host → host}/local-session-environment.js +0 -0
- /package/dist/{agent-core/host → host}/local-shell.js +0 -0
- /package/dist/{agent-core/runtime/execution-handle.js → host/skill-contract.js} +0 -0
- /package/dist/{agent-core/model → model}/anthropic-thinking.d.ts +0 -0
- /package/dist/{agent-core/model → model}/attempt-diagnostics.d.ts +0 -0
- /package/dist/{agent-core/model → model}/attempt-diagnostics.js +0 -0
- /package/dist/{agent-core/session/store → model}/contracts.js +0 -0
- /package/dist/{agent-core/sdk/types.js → model/image-contract.js} +0 -0
- /package/dist/{agent-core/model → model}/image.js +0 -0
- /package/dist/{agent-core/model → model}/native-context.d.ts +0 -0
- /package/dist/{agent-core/model → model}/native-context.js +0 -0
- /package/dist/{agent-core/model → model}/recovery.d.ts +0 -0
- /package/dist/{agent-core/model → model}/sse.d.ts +0 -0
- /package/dist/{agent-core/model → model}/text-blocks.d.ts +0 -0
- /package/dist/{agent-core/model → model}/tool-declarations.js +0 -0
- /package/dist/{agent-core/model → model}/tool-input.d.ts +0 -0
- /package/dist/{agent-core/model → model}/tool-input.js +0 -0
- /package/dist/{agent-core/model → model}/usage-observer.js +0 -0
- /package/dist/{agent-core/session/store/types.js → model/usage.js} +0 -0
- /package/dist/{agent-core/subagent → process}/contracts.js +0 -0
- /package/dist/{agent-core/prompts → prompts}/compact.d.ts +0 -0
- /package/dist/{agent-core/prompts → prompts}/compact.js +0 -0
- /package/dist/{agent-core/prompts → prompts}/core.d.ts +0 -0
- /package/dist/{agent-core/prompts → prompts}/core.js +0 -0
- /package/dist/{agent-core/prompts → prompts}/markup.d.ts +0 -0
- /package/dist/{agent-core/prompts → prompts}/markup.js +0 -0
- /package/dist/{agent-core/prompts → prompts}/modes.d.ts +0 -0
- /package/dist/{agent-core/prompts → prompts}/modes.js +0 -0
- /package/dist/{agent-core/prompts → prompts}/output.d.ts +0 -0
- /package/dist/{agent-core/prompts → prompts}/output.js +0 -0
- /package/dist/{agent-core/prompts → prompts}/permissions.d.ts +0 -0
- /package/dist/{agent-core/prompts → prompts}/permissions.js +0 -0
- /package/dist/{agent-core/tools/local → prompts}/read-context.d.ts +0 -0
- /package/dist/{agent-core/prompts → prompts}/resources.d.ts +0 -0
- /package/dist/{agent-core/prompts → prompts}/resources.js +0 -0
- /package/dist/{agent-core/prompts → prompts}/subagent.d.ts +0 -0
- /package/dist/{agent-core/prompts → prompts}/subagent.js +0 -0
- /package/dist/{agent-core/prompts → prompts}/workflow.d.ts +0 -0
- /package/dist/{agent-core/prompts → prompts}/workflow.js +0 -0
- /package/dist/{agent-core/runtime → runtime}/async-queue.d.ts +0 -0
- /package/dist/{agent-core/runtime → runtime}/async-queue.js +0 -0
- /package/dist/{agent-core/tools → runtime/compaction}/contracts.js +0 -0
- /package/dist/{agent-core/runtime → runtime}/compaction/coordinator.d.ts +0 -0
- /package/dist/{agent-core/runtime → runtime}/compaction/history.js +0 -0
- /package/dist/{agent-core/runtime → runtime}/context/contracts.d.ts +0 -0
- /package/dist/{agent-core/session/types.js → runtime/context/contracts.js} +0 -0
- /package/dist/{agent-core/runtime → runtime}/context/environment.js +0 -0
- /package/dist/{agent-core/tools/code-mode/engine.js → runtime/context/gc-contracts.js} +0 -0
- /package/dist/{agent-core/runtime → runtime}/context/gc-runtime.js +0 -0
- /package/dist/{agent-core/runtime → runtime}/context/normalize.js +0 -0
- /package/dist/{agent-core/tools/code-mode/worker.d.ts → runtime/contracts.js} +0 -0
- /package/dist/{agent-core/tools/runtime-context.js → runtime/events.js} +0 -0
- /package/dist/{agent-core/runtime → runtime}/execution-policy.d.ts +0 -0
- /package/dist/{agent-core/runtime → runtime}/execution-policy.js +0 -0
- /package/dist/{agent-core/runtime → runtime}/model-generation-timing.d.ts +0 -0
- /package/dist/{agent-core/runtime → runtime}/model-generation-timing.js +0 -0
- /package/dist/{agent-core/runtime → runtime}/model-progress.d.ts +0 -0
- /package/dist/{agent-core/runtime → runtime}/plan-output.d.ts +0 -0
- /package/dist/{agent-core/runtime → runtime}/plan-output.js +0 -0
- /package/dist/{agent-core/tools/public-metadata.d.ts → runtime/public-tool-metadata.d.ts} +0 -0
- /package/dist/{agent-core/tools/public-metadata.js → runtime/public-tool-metadata.js} +0 -0
- /package/dist/{agent-core/runtime → runtime}/token-budget.d.ts +0 -0
- /package/dist/{agent-core/runtime → runtime}/token-budget.js +0 -0
- /package/dist/{agent-core/tools → runtime}/wait.d.ts +0 -0
- /package/dist/{agent-core/sdk → sdk}/request-preparation.js +0 -0
- /package/dist/{agent-core/session → session}/context-gc-markup.d.ts +0 -0
- /package/dist/{agent-core/session → session}/context-gc-markup.js +0 -0
- /package/dist/{agent-core/session → session}/context-gc-replacements.js +0 -0
- /package/dist/{agent-core/session → session}/context-gc-subagent.js +0 -0
- /package/dist/{agent-core/session → session}/types.d.ts +0 -0
- /package/dist/{agent-core/storage → storage}/file-lock.d.ts +0 -0
- /package/dist/{agent-core/subagent → subagent}/model-update.d.ts +0 -0
- /package/dist/{agent-core/tools → tools}/agent/todo-read.js +0 -0
- /package/dist/{agent-core/tools → tools}/code-mode/quickjs-engine.d.ts +0 -0
- /package/dist/{agent-core/tools → tools}/code-mode/quickjs-engine.js +0 -0
- /package/dist/{agent-core/tools → tools}/code-mode/text-output.d.ts +0 -0
- /package/dist/{agent-core/tools → tools}/code-mode/text-output.js +0 -0
- /package/dist/{agent-core/tools → tools}/code-mode/worker.js +0 -0
- /package/dist/{agent-core/tools → tools}/file-change-diff.d.ts +0 -0
- /package/dist/{agent-core/tools → tools}/file-change-diff.js +0 -0
- /package/dist/{agent-core/tools → tools}/input-diagnostics/source-preview.d.ts +0 -0
- /package/dist/{agent-core/tools → tools}/input-diagnostics/source-preview.js +0 -0
- /package/dist/{agent-core/tools → tools}/input.d.ts +0 -0
- /package/dist/{agent-core/tools → tools}/input.js +0 -0
- /package/dist/{agent-core/tools → tools}/local/apply-patch-parser.d.ts +0 -0
- /package/dist/{agent-core/tools → tools}/local/image-processing.d.ts +0 -0
- /package/dist/{agent-core/tools → tools}/mcp/catalog.d.ts +0 -0
- /package/dist/{agent-core/tools → tools}/mcp/history.js +0 -0
- /package/dist/{agent-core/tools → tools}/registry.js +0 -0
- /package/dist/{agent-core/tools → tools}/runtime-resource-handles.d.ts +0 -0
- /package/dist/{agent-core/tools → tools}/runtime.d.ts +0 -0
- /package/dist/{agent-core/tools → tools}/scheduler.js +0 -0
- /package/dist/{agent-core/tools → tools}/shared/patch-result.d.ts +0 -0
- /package/dist/{agent-core/tools → tools}/shared/patch-result.js +0 -0
- /package/dist/{agent-core/tools → tools}/shared/path-resolver.d.ts +0 -0
|
@@ -0,0 +1,1586 @@
|
|
|
1
|
+
import { responsesRequestBody } from './responses-request.js';
|
|
2
|
+
export { openAiInput } from './responses-request.js';
|
|
3
|
+
import { ResponsesTextProjection, hostedWebActionInput, hostedWebActionSummary, responseCitations, responseEventId, responseItem } from './responses-event-projection.js';
|
|
4
|
+
import { isRealUserMessage } from './input-images.js';
|
|
5
|
+
import { observeModelRequestUsage } from './usage-observer.js';
|
|
6
|
+
import { isDeepStrictEqual } from 'node:util';
|
|
7
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
8
|
+
import { describeAgentError, AgentError } from '../error.js';
|
|
9
|
+
import { assertInlineMediaBudget } from './inline-media-budget.js';
|
|
10
|
+
import WebSocket from 'ws';
|
|
11
|
+
import { isRetryableUpstreamOverloadCode, number, parseSse, record, text, observeModelStreamEvent, modelStreamEventObservedAt } from './sse.js';
|
|
12
|
+
import { connectSocks5Target, fetchModelResponse, isRetryableModelHttpStatus, isRetryableModelTransportError, modelRetryMaxAttempts, isRetryableModelRequestError, modelRetryDelay, waitForModelRetry } from './http.js';
|
|
13
|
+
import { acquireModelCredential, appendModelCredentialHeaders, modelCredentialSensitiveValues } from './runtime-credential.js';
|
|
14
|
+
import { reportModelAttempt } from './attempt-diagnostics.js';
|
|
15
|
+
import { productUserAgent } from '../network/product-user-agent.js';
|
|
16
|
+
import { ResponsesSessionCache, clearResponsesContinuation, invalidateResponsesWebSocket, terminateWebSocket } from './responses-session-cache.js';
|
|
17
|
+
const WEBSOCKET_IDLE_TIMEOUT_MS = 300_000;
|
|
18
|
+
const WEBSOCKET_SEND_FRAME_BYTES = 1024 * 1024;
|
|
19
|
+
export const OPENAI_RESPONSES_EARLY_EOF_REASON = 'OPENAI_RESPONSES_EARLY_EOF';
|
|
20
|
+
export const OPENAI_RESPONSES_WEBSOCKET_CONNECTION_LIMIT_REASON = 'OPENAI_RESPONSES_WEBSOCKET_CONNECTION_LIMIT';
|
|
21
|
+
const X_CODEX_TURN_STATE = 'x-codex-turn-state';
|
|
22
|
+
export class OpenAiResponsesAdapter {
|
|
23
|
+
#cache = new ResponsesSessionCache();
|
|
24
|
+
#configuration;
|
|
25
|
+
constructor(configuration) {
|
|
26
|
+
this.#configuration = configuration;
|
|
27
|
+
}
|
|
28
|
+
get configuration() {
|
|
29
|
+
return this.#configuration;
|
|
30
|
+
}
|
|
31
|
+
prepareReconfigure(configuration) {
|
|
32
|
+
if (!sameResponsesAdapterConfiguration(this.#configuration, configuration))
|
|
33
|
+
return undefined;
|
|
34
|
+
return () => {
|
|
35
|
+
this.#configuration = configuration;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
async *start(request, signal) {
|
|
39
|
+
const turn = new OpenAiResponsesTurnSession(this, this.#cache, {}, false);
|
|
40
|
+
try {
|
|
41
|
+
yield* turn.start(request, signal);
|
|
42
|
+
}
|
|
43
|
+
finally {
|
|
44
|
+
await turn.close();
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
createTurnSession() {
|
|
48
|
+
const { state, lease } = this.#cache.checkout();
|
|
49
|
+
return new OpenAiResponsesTurnSession(this, this.#cache, state, lease);
|
|
50
|
+
}
|
|
51
|
+
async close() {
|
|
52
|
+
this.#cache.close();
|
|
53
|
+
}
|
|
54
|
+
async *startTurn(request, signal, turn, prepared) {
|
|
55
|
+
let useContinuation = prepared.continuation;
|
|
56
|
+
let requestBody = prepared.body;
|
|
57
|
+
let credentialReason = 'REQUEST';
|
|
58
|
+
let unauthorizedRetryUsed = false;
|
|
59
|
+
let invalidContinuationFallbackUsed = false;
|
|
60
|
+
const retryState = request.retryState ?? { attempts: 0 };
|
|
61
|
+
const maxAttempts = modelRetryMaxAttempts(retryState);
|
|
62
|
+
let resetOutputTiming = false;
|
|
63
|
+
while (retryState.attempts < maxAttempts) {
|
|
64
|
+
if (resetOutputTiming)
|
|
65
|
+
yield { type: 'output.reset' };
|
|
66
|
+
resetOutputTiming = false;
|
|
67
|
+
const attempt = retryState.attempts++;
|
|
68
|
+
const attemptNumber = attempt + 1;
|
|
69
|
+
const attemptStartedAt = Date.now();
|
|
70
|
+
const attemptContinuation = useContinuation;
|
|
71
|
+
const attemptCredentialReason = credentialReason;
|
|
72
|
+
const attemptTransport = this.configuration.responsesTransport?.transport === 'WEBSOCKET' ? 'WEBSOCKET' : 'HTTP';
|
|
73
|
+
const requestPrefixDiagnostic = request.onAttemptDiagnostic
|
|
74
|
+
? {
|
|
75
|
+
requestInputItems: prepared.inputItemCount
|
|
76
|
+
}
|
|
77
|
+
: {};
|
|
78
|
+
const prelude = [];
|
|
79
|
+
let sawSemanticEvent = false;
|
|
80
|
+
let firstSemanticEventType;
|
|
81
|
+
turn.beginAttempt();
|
|
82
|
+
await reportModelAttempt(request, {
|
|
83
|
+
protocol: 'OPENAI_RESPONSES',
|
|
84
|
+
transport: attemptTransport,
|
|
85
|
+
state: 'STARTED',
|
|
86
|
+
attempt: attemptNumber,
|
|
87
|
+
maxAttempts,
|
|
88
|
+
continuation: attemptContinuation,
|
|
89
|
+
credentialReason: attemptCredentialReason,
|
|
90
|
+
...requestPrefixDiagnostic
|
|
91
|
+
});
|
|
92
|
+
try {
|
|
93
|
+
for await (const event of observeModelRequestUsage(() => this.startAttempt(request, signal, requestBody, useContinuation, credentialReason, turn), request, this.configuration, signal)) {
|
|
94
|
+
if (event.type.startsWith('output.')) {
|
|
95
|
+
resetOutputTiming = true;
|
|
96
|
+
yield event;
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
if (!sawSemanticEvent && !isSemanticEvent(event)) {
|
|
100
|
+
prelude.push(event);
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
if (!sawSemanticEvent) {
|
|
104
|
+
sawSemanticEvent = true;
|
|
105
|
+
firstSemanticEventType = event.type;
|
|
106
|
+
for (const pending of prelude)
|
|
107
|
+
yield pending;
|
|
108
|
+
prelude.length = 0;
|
|
109
|
+
}
|
|
110
|
+
yield event;
|
|
111
|
+
}
|
|
112
|
+
await reportModelAttempt(request, {
|
|
113
|
+
protocol: 'OPENAI_RESPONSES',
|
|
114
|
+
transport: attemptTransport,
|
|
115
|
+
state: 'SUCCEEDED',
|
|
116
|
+
attempt: attemptNumber,
|
|
117
|
+
maxAttempts,
|
|
118
|
+
continuation: attemptContinuation,
|
|
119
|
+
credentialReason: attemptCredentialReason,
|
|
120
|
+
...requestPrefixDiagnostic,
|
|
121
|
+
semanticEventObserved: sawSemanticEvent,
|
|
122
|
+
...(firstSemanticEventType === undefined ? {} : { firstSemanticEventType }),
|
|
123
|
+
...turn.connectionDiagnostic(),
|
|
124
|
+
durationMs: Math.max(0, Date.now() - attemptStartedAt)
|
|
125
|
+
});
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
catch (error) {
|
|
129
|
+
const reportFailure = async (willRetry, options = {}) => reportModelAttempt(request, {
|
|
130
|
+
protocol: 'OPENAI_RESPONSES',
|
|
131
|
+
transport: attemptTransport,
|
|
132
|
+
state: 'FAILED',
|
|
133
|
+
attempt: attemptNumber,
|
|
134
|
+
maxAttempts,
|
|
135
|
+
continuation: attemptContinuation,
|
|
136
|
+
credentialReason: attemptCredentialReason,
|
|
137
|
+
...requestPrefixDiagnostic,
|
|
138
|
+
semanticEventObserved: sawSemanticEvent,
|
|
139
|
+
...(firstSemanticEventType === undefined ? {} : { firstSemanticEventType }),
|
|
140
|
+
...turn.connectionDiagnostic(),
|
|
141
|
+
durationMs: Math.max(0, Date.now() - attemptStartedAt),
|
|
142
|
+
...openAiResponsesAttemptError(error),
|
|
143
|
+
willRetry,
|
|
144
|
+
...(options.retryDelayMs === undefined ? {} : { retryDelayMs: options.retryDelayMs }),
|
|
145
|
+
...(options.retryReason === undefined ? {} : { retryReason: options.retryReason })
|
|
146
|
+
});
|
|
147
|
+
if (!signal.aborted &&
|
|
148
|
+
!sawSemanticEvent &&
|
|
149
|
+
useContinuation &&
|
|
150
|
+
!invalidContinuationFallbackUsed &&
|
|
151
|
+
isLocalContinuationReset(error)) {
|
|
152
|
+
invalidContinuationFallbackUsed = true;
|
|
153
|
+
useContinuation = false;
|
|
154
|
+
requestBody = prepared.fullBody;
|
|
155
|
+
turn.resetContinuation();
|
|
156
|
+
turn.invalidateWebSocket();
|
|
157
|
+
retryState.attempts = Math.max(0, retryState.attempts - 1);
|
|
158
|
+
credentialReason = 'RECONNECT';
|
|
159
|
+
await reportFailure(true, {
|
|
160
|
+
retryDelayMs: 0,
|
|
161
|
+
retryReason: 'INVALID_CONTINUATION'
|
|
162
|
+
});
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
if (!signal.aborted &&
|
|
166
|
+
attemptNumber < maxAttempts &&
|
|
167
|
+
!sawSemanticEvent &&
|
|
168
|
+
useContinuation &&
|
|
169
|
+
!invalidContinuationFallbackUsed &&
|
|
170
|
+
isInvalidPreviousResponseError(error)) {
|
|
171
|
+
invalidContinuationFallbackUsed = true;
|
|
172
|
+
useContinuation = false;
|
|
173
|
+
requestBody = prepared.fullBody;
|
|
174
|
+
turn.resetContinuation();
|
|
175
|
+
turn.invalidateWebSocket();
|
|
176
|
+
credentialReason = 'RECONNECT';
|
|
177
|
+
await reportFailure(true, {
|
|
178
|
+
retryDelayMs: 0,
|
|
179
|
+
retryReason: 'INVALID_CONTINUATION'
|
|
180
|
+
});
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
if (!signal.aborted &&
|
|
184
|
+
attemptNumber < maxAttempts &&
|
|
185
|
+
!sawSemanticEvent &&
|
|
186
|
+
!unauthorizedRetryUsed &&
|
|
187
|
+
this.configuration.credentialProvider !== undefined &&
|
|
188
|
+
isModelAuthenticationFailure(error)) {
|
|
189
|
+
const revision = credentialRevision(error);
|
|
190
|
+
if (revision !== undefined)
|
|
191
|
+
await this.configuration.credentialProvider.invalidate(revision);
|
|
192
|
+
unauthorizedRetryUsed = true;
|
|
193
|
+
credentialReason = 'UNAUTHORIZED';
|
|
194
|
+
await reportFailure(true, {
|
|
195
|
+
retryDelayMs: 0,
|
|
196
|
+
retryReason: 'UNAUTHORIZED'
|
|
197
|
+
});
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
const willRetry = !(signal.aborted ||
|
|
201
|
+
(sawSemanticEvent && request.retryState === undefined) ||
|
|
202
|
+
!(isRetryableModelRequestError(error) || isOpenAiResponsesRecoverableStreamError(error)) ||
|
|
203
|
+
attemptNumber >= maxAttempts);
|
|
204
|
+
if (!willRetry) {
|
|
205
|
+
await reportFailure(false);
|
|
206
|
+
throw error;
|
|
207
|
+
}
|
|
208
|
+
const delayMs = modelRetryDelay(retryAfter(error), attempt);
|
|
209
|
+
await reportFailure(true, {
|
|
210
|
+
retryDelayMs: delayMs,
|
|
211
|
+
retryReason: 'STREAM'
|
|
212
|
+
});
|
|
213
|
+
if (sawSemanticEvent) {
|
|
214
|
+
retryState.retryDelayMs = delayMs;
|
|
215
|
+
throw error;
|
|
216
|
+
}
|
|
217
|
+
credentialReason =
|
|
218
|
+
error instanceof AgentError && error.details?.transport === 'WEBSOCKET'
|
|
219
|
+
? 'RECONNECT'
|
|
220
|
+
: 'REQUEST';
|
|
221
|
+
useContinuation = false;
|
|
222
|
+
requestBody = prepared.fullBody;
|
|
223
|
+
if (error instanceof AgentError && error.details?.transport === 'WEBSOCKET')
|
|
224
|
+
turn.invalidateWebSocket();
|
|
225
|
+
await waitForModelRetry(retryAfter(error), attempt, signal, delayMs);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
async *startAttempt(request, signal, body, continuation, credentialReason, turn) {
|
|
230
|
+
let latestCredentialRevision;
|
|
231
|
+
let diagnosticSecrets = [];
|
|
232
|
+
const fetchResponse = async (requestBody) => {
|
|
233
|
+
const serializedBody = JSON.stringify(requestBody);
|
|
234
|
+
return await fetchModelResponse(responsesEndpoint(this.configuration.baseUrl), async () => {
|
|
235
|
+
const credential = await acquireResponsesCredential(this.configuration, credentialReason, signal);
|
|
236
|
+
latestCredentialRevision = credential.revision;
|
|
237
|
+
diagnosticSecrets = modelCredentialSensitiveValues(credential);
|
|
238
|
+
return {
|
|
239
|
+
request: {
|
|
240
|
+
method: 'POST',
|
|
241
|
+
headers: responsesHeaders(credential, request.promptCacheKey, turn.currentTurnState()),
|
|
242
|
+
body: serializedBody,
|
|
243
|
+
redirect: 'manual'
|
|
244
|
+
},
|
|
245
|
+
httpVersion: this.configuration.responsesTransport?.transport === 'HTTP'
|
|
246
|
+
? this.configuration.responsesTransport.httpVersion
|
|
247
|
+
: '1.1',
|
|
248
|
+
...(credential.socks5Url === undefined
|
|
249
|
+
? {}
|
|
250
|
+
: { socks5Url: credential.socks5Url.reveal() })
|
|
251
|
+
};
|
|
252
|
+
}, signal, 1);
|
|
253
|
+
};
|
|
254
|
+
let response;
|
|
255
|
+
let rawEvents;
|
|
256
|
+
try {
|
|
257
|
+
if (this.configuration.responsesTransport?.transport === 'WEBSOCKET')
|
|
258
|
+
rawEvents = webSocketResponseEvents({
|
|
259
|
+
configuration: this.configuration,
|
|
260
|
+
body,
|
|
261
|
+
promptCacheKey: request.promptCacheKey,
|
|
262
|
+
reason: credentialReason,
|
|
263
|
+
signal,
|
|
264
|
+
continuation,
|
|
265
|
+
turn,
|
|
266
|
+
credentialAcquired: (revision) => (latestCredentialRevision = revision)
|
|
267
|
+
});
|
|
268
|
+
else {
|
|
269
|
+
response = await fetchResponse(body);
|
|
270
|
+
turn.captureTurnState(response.headers.get(X_CODEX_TURN_STATE));
|
|
271
|
+
rawEvents = parseOpenAiResponsesSse(response, signal, continuation, diagnosticSecrets);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
catch (error) {
|
|
275
|
+
if (this.configuration.responsesTransport?.transport === 'WEBSOCKET')
|
|
276
|
+
turn.invalidateWebSocket();
|
|
277
|
+
throw withCredentialRevision(withResponsesAttemptDetails(error, continuation, this.configuration.responsesTransport?.transport), latestCredentialRevision);
|
|
278
|
+
}
|
|
279
|
+
let completed = false;
|
|
280
|
+
const functionCalls = new Map();
|
|
281
|
+
const customCalls = new Map();
|
|
282
|
+
const textProjection = new ResponsesTextProjection();
|
|
283
|
+
const emittedReasoning = new Set();
|
|
284
|
+
const emittedFunctionCalls = new Set();
|
|
285
|
+
const hostedWebCalls = new Map();
|
|
286
|
+
try {
|
|
287
|
+
for await (const raw of rawEvents) {
|
|
288
|
+
const event = record(raw);
|
|
289
|
+
const type = text(event.type) ?? 'unknown';
|
|
290
|
+
if (type === 'response.output_item.added' &&
|
|
291
|
+
['reasoning', 'message', 'function_call', 'custom_tool_call'].includes(text(record(event.item).type) ?? ''))
|
|
292
|
+
yield observeModelStreamEvent({ type: 'output.started' }, modelStreamEventObservedAt(raw));
|
|
293
|
+
if (type === 'response.web_search_call.searching')
|
|
294
|
+
yield observeModelStreamEvent({ type: 'output.paused' }, modelStreamEventObservedAt(raw));
|
|
295
|
+
if (type === 'response.output_item.done') {
|
|
296
|
+
const item = responseItem(event.item);
|
|
297
|
+
if (item)
|
|
298
|
+
yield observeModelStreamEvent({ type: 'response.item', provider: 'OPENAI_RESPONSES', item }, modelStreamEventObservedAt(raw));
|
|
299
|
+
}
|
|
300
|
+
const projectedText = textProjection.project(event, type);
|
|
301
|
+
if (projectedText.handled) {
|
|
302
|
+
if (projectedText.event)
|
|
303
|
+
yield observeModelStreamEvent(projectedText.event, modelStreamEventObservedAt(raw));
|
|
304
|
+
}
|
|
305
|
+
else if (type === 'response.output_item.added' &&
|
|
306
|
+
text(record(event.item).type) === 'function_call') {
|
|
307
|
+
const item = record(event.item);
|
|
308
|
+
const itemId = text(item.id);
|
|
309
|
+
if (itemId)
|
|
310
|
+
functionCalls.set(itemId, {
|
|
311
|
+
callId: text(item.call_id) ?? itemId,
|
|
312
|
+
name: text(item.name) ?? 'unknown'
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
else if (type === 'response.output_item.added' &&
|
|
316
|
+
text(record(event.item).type) === 'custom_tool_call') {
|
|
317
|
+
const item = record(event.item);
|
|
318
|
+
const itemId = text(item.id);
|
|
319
|
+
if (itemId)
|
|
320
|
+
customCalls.set(itemId, {
|
|
321
|
+
callId: text(item.call_id) ?? itemId,
|
|
322
|
+
name: text(item.name) ?? 'unknown',
|
|
323
|
+
input: text(item.input) ?? ''
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
else if ((type === 'response.output_item.added' || type === 'response.output_item.done') &&
|
|
327
|
+
text(record(event.item).type) === 'reasoning') {
|
|
328
|
+
// Native reasoning context is retained above; its lifecycle is not visible output.
|
|
329
|
+
continue;
|
|
330
|
+
}
|
|
331
|
+
else if (type === 'response.custom_tool_call_input.delta') {
|
|
332
|
+
const itemId = text(event.item_id) ?? '';
|
|
333
|
+
const known = customCalls.get(itemId);
|
|
334
|
+
const delta = text(event.delta) ?? '';
|
|
335
|
+
if (known)
|
|
336
|
+
known.input += delta;
|
|
337
|
+
if (delta.length > 0)
|
|
338
|
+
yield observeModelStreamEvent({ type: 'output.delta' }, modelStreamEventObservedAt(raw));
|
|
339
|
+
}
|
|
340
|
+
else if (type === 'response.function_call_arguments.delta' ||
|
|
341
|
+
type === 'response.reasoning_text.delta' ||
|
|
342
|
+
type === 'response.reasoning_summary_text.delta') {
|
|
343
|
+
if ((text(event.delta)?.length ?? 0) > 0)
|
|
344
|
+
yield observeModelStreamEvent({ type: 'output.delta' }, modelStreamEventObservedAt(raw));
|
|
345
|
+
}
|
|
346
|
+
else if (type === 'response.created')
|
|
347
|
+
yield observeModelStreamEvent({
|
|
348
|
+
type: 'response.started',
|
|
349
|
+
responseId: responseEventId(event) ?? 'unknown',
|
|
350
|
+
...(text(record(event.response).model)
|
|
351
|
+
? { modelId: text(record(event.response).model) }
|
|
352
|
+
: {})
|
|
353
|
+
}, modelStreamEventObservedAt(raw));
|
|
354
|
+
else if (type === 'response.reasoning_summary_text.done') {
|
|
355
|
+
const itemId = JSON.stringify([
|
|
356
|
+
text(event.item_id) ?? `reasoning-${event.output_index ?? 0}`,
|
|
357
|
+
event.summary_index ?? 0
|
|
358
|
+
]);
|
|
359
|
+
const summary = text(event.text) ?? '';
|
|
360
|
+
if (!summary.trim() || emittedReasoning.has(itemId))
|
|
361
|
+
continue;
|
|
362
|
+
emittedReasoning.add(itemId);
|
|
363
|
+
yield observeModelStreamEvent({
|
|
364
|
+
type: 'reasoning.completed',
|
|
365
|
+
itemId,
|
|
366
|
+
representation: 'summary',
|
|
367
|
+
text: summary
|
|
368
|
+
}, modelStreamEventObservedAt(raw));
|
|
369
|
+
}
|
|
370
|
+
else if (type === 'response.output_item.done' &&
|
|
371
|
+
text(record(event.item).type) === 'tool_search_call') {
|
|
372
|
+
const item = record(event.item);
|
|
373
|
+
const callId = text(item.call_id) ?? text(item.id) ?? 'unknown';
|
|
374
|
+
let arguments_ = item.arguments ?? {};
|
|
375
|
+
let argumentError;
|
|
376
|
+
if (typeof arguments_ === 'string')
|
|
377
|
+
try {
|
|
378
|
+
arguments_ = JSON.parse(arguments_);
|
|
379
|
+
}
|
|
380
|
+
catch {
|
|
381
|
+
argumentError = 'Tool Search arguments are invalid JSON.';
|
|
382
|
+
}
|
|
383
|
+
if (!emittedFunctionCalls.has(callId)) {
|
|
384
|
+
emittedFunctionCalls.add(callId);
|
|
385
|
+
yield observeModelStreamEvent({
|
|
386
|
+
type: 'client_tool.call',
|
|
387
|
+
callId,
|
|
388
|
+
name: 'tool_search',
|
|
389
|
+
arguments: arguments_,
|
|
390
|
+
...(argumentError ? { argumentError } : {})
|
|
391
|
+
}, modelStreamEventObservedAt(raw));
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
else if (type === 'response.function_call_arguments.done' ||
|
|
395
|
+
(type === 'response.output_item.done' &&
|
|
396
|
+
text(record(event.item).type) === 'function_call')) {
|
|
397
|
+
const item = record(event.item);
|
|
398
|
+
const known = functionCalls.get(text(event.item_id) ?? text(item.id) ?? '');
|
|
399
|
+
let args = {};
|
|
400
|
+
let argumentError;
|
|
401
|
+
try {
|
|
402
|
+
args = JSON.parse(text(event.arguments) ?? text(item.arguments) ?? '{}');
|
|
403
|
+
}
|
|
404
|
+
catch {
|
|
405
|
+
argumentError = 'Function arguments are invalid JSON.';
|
|
406
|
+
}
|
|
407
|
+
const callId = text(event.call_id) ??
|
|
408
|
+
text(item.call_id) ??
|
|
409
|
+
known?.callId ??
|
|
410
|
+
text(event.item_id) ??
|
|
411
|
+
text(item.id) ??
|
|
412
|
+
'unknown';
|
|
413
|
+
if (!emittedFunctionCalls.has(callId)) {
|
|
414
|
+
emittedFunctionCalls.add(callId);
|
|
415
|
+
yield observeModelStreamEvent({
|
|
416
|
+
type: 'client_tool.call',
|
|
417
|
+
callId,
|
|
418
|
+
name: text(event.name) ?? text(item.name) ?? known?.name ?? 'unknown',
|
|
419
|
+
arguments: args,
|
|
420
|
+
...(argumentError ? { argumentError } : {})
|
|
421
|
+
}, modelStreamEventObservedAt(raw));
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
else if (type === 'response.custom_tool_call_input.done' ||
|
|
425
|
+
(type === 'response.output_item.done' &&
|
|
426
|
+
text(record(event.item).type) === 'custom_tool_call')) {
|
|
427
|
+
const item = record(event.item);
|
|
428
|
+
const itemId = text(event.item_id) ?? text(item.id) ?? '';
|
|
429
|
+
const known = customCalls.get(itemId);
|
|
430
|
+
const callId = text(event.call_id) ?? text(item.call_id) ?? known?.callId ?? itemId;
|
|
431
|
+
if (!emittedFunctionCalls.has(callId)) {
|
|
432
|
+
emittedFunctionCalls.add(callId);
|
|
433
|
+
yield observeModelStreamEvent({
|
|
434
|
+
type: 'client_tool.call',
|
|
435
|
+
callId,
|
|
436
|
+
name: text(event.name) ?? text(item.name) ?? known?.name ?? 'unknown',
|
|
437
|
+
arguments: text(event.input) ?? text(item.input) ?? known?.input ?? '',
|
|
438
|
+
inputMode: 'freeform'
|
|
439
|
+
}, modelStreamEventObservedAt(raw));
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
else if (type.includes('web_search_call') ||
|
|
443
|
+
text(record(event.item).type) === 'web_search_call') {
|
|
444
|
+
const item = record(event.item);
|
|
445
|
+
const itemId = text(event.item_id) ?? text(item.id) ?? 'web';
|
|
446
|
+
const previous = hostedWebCalls.get(itemId);
|
|
447
|
+
const action = record(Object.keys(record(event.action)).length ? event.action : item.action);
|
|
448
|
+
const actionType = text(action.type);
|
|
449
|
+
const kind = actionType === 'open_page' || actionType === 'find_in_page'
|
|
450
|
+
? 'WEB_FETCH'
|
|
451
|
+
: (previous?.kind ?? 'WEB_SEARCH');
|
|
452
|
+
const summary = { ...previous?.summary, ...hostedWebActionSummary(action) };
|
|
453
|
+
const projected = hostedWebActionInput(action);
|
|
454
|
+
const input = { ...previous?.input, ...projected.input };
|
|
455
|
+
const truncated = previous?.truncated === true || projected.truncated;
|
|
456
|
+
hostedWebCalls.set(itemId, { kind, summary, input, truncated });
|
|
457
|
+
const output = {
|
|
458
|
+
...(summary.title === undefined ? {} : { title: summary.title }),
|
|
459
|
+
...(summary.sources === undefined ? {} : { sources: summary.sources }),
|
|
460
|
+
...(summary.sourcesTruncated === undefined
|
|
461
|
+
? {}
|
|
462
|
+
: { sourcesTruncated: summary.sourcesTruncated })
|
|
463
|
+
};
|
|
464
|
+
yield observeModelStreamEvent({
|
|
465
|
+
type: 'hosted_tool',
|
|
466
|
+
itemId,
|
|
467
|
+
kind,
|
|
468
|
+
toolName: 'web_search',
|
|
469
|
+
...(Object.keys(input).length ? { input } : {}),
|
|
470
|
+
...(Object.keys(output).length ? { output } : {}),
|
|
471
|
+
...(truncated || summary.sourcesTruncated === true ? { truncated: true } : {}),
|
|
472
|
+
status: type.endsWith('.failed')
|
|
473
|
+
? 'FAILED'
|
|
474
|
+
: type.endsWith('.completed') || type === 'response.output_item.done'
|
|
475
|
+
? 'COMPLETED'
|
|
476
|
+
: 'RUNNING',
|
|
477
|
+
summary
|
|
478
|
+
}, modelStreamEventObservedAt(raw));
|
|
479
|
+
}
|
|
480
|
+
else if (type === 'response.output_text.annotation.added') {
|
|
481
|
+
const annotation = record(event.annotation);
|
|
482
|
+
const url = text(annotation.url);
|
|
483
|
+
if (url)
|
|
484
|
+
yield observeModelStreamEvent({
|
|
485
|
+
type: 'citation',
|
|
486
|
+
citation: {
|
|
487
|
+
id: text(annotation.id) ??
|
|
488
|
+
`${text(event.item_id) ?? 'text'}-${number(event.annotation_index) ?? 0}`,
|
|
489
|
+
url,
|
|
490
|
+
...(text(annotation.title) ? { title: text(annotation.title) } : {})
|
|
491
|
+
}
|
|
492
|
+
}, modelStreamEventObservedAt(raw));
|
|
493
|
+
}
|
|
494
|
+
else if (type === 'response.completed' || type === 'response.incomplete') {
|
|
495
|
+
yield observeModelStreamEvent({ type: 'output.completed' }, modelStreamEventObservedAt(raw));
|
|
496
|
+
const responseData = record(event.response);
|
|
497
|
+
if (type === 'response.completed')
|
|
498
|
+
turn.captureCompletedResponseId(responseEventId(event));
|
|
499
|
+
for (const output of textProjection.finish())
|
|
500
|
+
yield observeModelStreamEvent(output, modelStreamEventObservedAt(raw));
|
|
501
|
+
for (const citation of responseCitations(responseData))
|
|
502
|
+
yield observeModelStreamEvent({ type: 'citation', citation }, modelStreamEventObservedAt(raw));
|
|
503
|
+
const usage = record(responseData.usage);
|
|
504
|
+
const inputDetails = record(usage.input_tokens_details);
|
|
505
|
+
const rolloutBudgetUnits = optionalRolloutBudgetUnits(usage.codex_rollout_budget_units);
|
|
506
|
+
yield observeModelStreamEvent({
|
|
507
|
+
type: 'usage',
|
|
508
|
+
final: true,
|
|
509
|
+
usage: {
|
|
510
|
+
inputTokens: number(usage.input_tokens),
|
|
511
|
+
totalInputTokens: number(usage.input_tokens),
|
|
512
|
+
reasoningTokens: number(record(usage.output_tokens_details).reasoning_tokens),
|
|
513
|
+
contextInputTokens: number(usage.input_tokens),
|
|
514
|
+
outputTokens: number(usage.output_tokens),
|
|
515
|
+
cacheReadTokens: number(inputDetails.cached_tokens),
|
|
516
|
+
cacheWriteTokens: number(inputDetails.cache_write_tokens),
|
|
517
|
+
...(rolloutBudgetUnits === undefined ? {} : { rolloutBudgetUnits })
|
|
518
|
+
}
|
|
519
|
+
}, modelStreamEventObservedAt(raw));
|
|
520
|
+
yield observeModelStreamEvent({
|
|
521
|
+
type: 'completed',
|
|
522
|
+
reason: type === 'response.incomplete' ? 'max_output' : 'completed'
|
|
523
|
+
}, modelStreamEventObservedAt(raw));
|
|
524
|
+
completed = true;
|
|
525
|
+
}
|
|
526
|
+
else if (type === 'error' ||
|
|
527
|
+
type === 'response.failed' ||
|
|
528
|
+
(event.type === undefined && event.error !== undefined)) {
|
|
529
|
+
const topLevel = record(event.error);
|
|
530
|
+
const nested = record(record(event.response).error);
|
|
531
|
+
const code = boundedError(text(event.code) ??
|
|
532
|
+
text(topLevel.code) ??
|
|
533
|
+
text(topLevel.type) ??
|
|
534
|
+
text(nested.code) ??
|
|
535
|
+
text(nested.type), 80);
|
|
536
|
+
const message = boundedError(text(event.message) ??
|
|
537
|
+
text(topLevel.message) ??
|
|
538
|
+
text(nested.message) ??
|
|
539
|
+
text(event.error), 1_000);
|
|
540
|
+
const status = upstreamHttpStatus(event.status, topLevel.status, record(event.response).status, nested.status);
|
|
541
|
+
const connectionLimit = this.configuration.responsesTransport?.transport === 'WEBSOCKET' &&
|
|
542
|
+
isWebSocketConnectionLimitCode(code);
|
|
543
|
+
const retryable = connectionLimit || isRetryableResponsesUpstreamError(code, message);
|
|
544
|
+
throw new AgentError(code ?? 'MAR_AGENT_MODEL_PROTOCOL_ERROR', message ?? 'OpenAI Responses request failed.', {
|
|
545
|
+
details: {
|
|
546
|
+
...(status === undefined ? {} : { status }),
|
|
547
|
+
...(connectionLimit
|
|
548
|
+
? { reason: OPENAI_RESPONSES_WEBSOCKET_CONNECTION_LIMIT_REASON }
|
|
549
|
+
: {}),
|
|
550
|
+
...(retryable ? { retryAfter: response?.headers.get('retry-after') ?? null } : {}),
|
|
551
|
+
continuation,
|
|
552
|
+
...(this.configuration.responsesTransport?.transport === 'WEBSOCKET'
|
|
553
|
+
? { transport: 'WEBSOCKET' }
|
|
554
|
+
: {})
|
|
555
|
+
},
|
|
556
|
+
...(retryable ? { retryable: true } : {})
|
|
557
|
+
});
|
|
558
|
+
}
|
|
559
|
+
else if (!isNonSemanticOpenAiResponsesEvent(type))
|
|
560
|
+
yield observeModelStreamEvent({ type: 'unknown', nativeType: type }, modelStreamEventObservedAt(raw));
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
catch (error) {
|
|
564
|
+
if (this.configuration.responsesTransport?.transport === 'WEBSOCKET')
|
|
565
|
+
turn.invalidateWebSocket();
|
|
566
|
+
throw withCredentialRevision(withResponsesAttemptDetails(error, continuation, this.configuration.responsesTransport?.transport), latestCredentialRevision);
|
|
567
|
+
}
|
|
568
|
+
if (!completed)
|
|
569
|
+
throw new AgentError('MAR_AGENT_MODEL_PROTOCOL_ERROR', 'OpenAI Responses stream ended without a terminal event.', {
|
|
570
|
+
details: {
|
|
571
|
+
reason: OPENAI_RESPONSES_EARLY_EOF_REASON,
|
|
572
|
+
retryAfter: response?.headers.get('retry-after') ?? null,
|
|
573
|
+
continuation,
|
|
574
|
+
...(this.configuration.responsesTransport?.transport === 'WEBSOCKET'
|
|
575
|
+
? { transport: 'WEBSOCKET' }
|
|
576
|
+
: {})
|
|
577
|
+
},
|
|
578
|
+
retryable: true
|
|
579
|
+
});
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
function sameResponsesAdapterConfiguration(current, next) {
|
|
583
|
+
const { apiKey: currentApiKey, credentialProvider: currentCredentialProvider, ...currentTransport } = current;
|
|
584
|
+
const { apiKey: nextApiKey, credentialProvider: nextCredentialProvider, ...nextTransport } = next;
|
|
585
|
+
const credentialsAreRebindable = (currentCredentialProvider !== undefined && nextCredentialProvider !== undefined) ||
|
|
586
|
+
(currentApiKey !== undefined &&
|
|
587
|
+
nextApiKey !== undefined &&
|
|
588
|
+
currentApiKey.reveal() === nextApiKey.reveal());
|
|
589
|
+
return credentialsAreRebindable && isDeepStrictEqual(currentTransport, nextTransport);
|
|
590
|
+
}
|
|
591
|
+
class OpenAiResponsesTurnSession {
|
|
592
|
+
adapter;
|
|
593
|
+
cache;
|
|
594
|
+
state;
|
|
595
|
+
cacheLease;
|
|
596
|
+
#active;
|
|
597
|
+
#abort;
|
|
598
|
+
#closePromise;
|
|
599
|
+
#closed = false;
|
|
600
|
+
#turnState;
|
|
601
|
+
#completedResponseId;
|
|
602
|
+
#connectionDiagnostic;
|
|
603
|
+
#webSocketDiagnostic;
|
|
604
|
+
constructor(adapter, cache, state, cacheLease) {
|
|
605
|
+
this.adapter = adapter;
|
|
606
|
+
this.cache = cache;
|
|
607
|
+
this.state = state;
|
|
608
|
+
this.cacheLease = cacheLease;
|
|
609
|
+
}
|
|
610
|
+
start(request, signal) {
|
|
611
|
+
const abort = new AbortController();
|
|
612
|
+
const iterator = this.run(request, AbortSignal.any([signal, abort.signal]));
|
|
613
|
+
let started = false;
|
|
614
|
+
let returnedBeforeStart = false;
|
|
615
|
+
return {
|
|
616
|
+
[Symbol.asyncIterator]: () => ({
|
|
617
|
+
next: () => {
|
|
618
|
+
if (returnedBeforeStart)
|
|
619
|
+
return iterator.next();
|
|
620
|
+
if (!started) {
|
|
621
|
+
if (this.#closed)
|
|
622
|
+
return Promise.reject(new AgentError('MAR_AGENT_DISPOSED', 'Model turn session is disposed.'));
|
|
623
|
+
if (this.#active !== undefined)
|
|
624
|
+
return Promise.reject(new AgentError('MAR_AGENT_MODEL_PROTOCOL_ERROR', 'Model turn session already has an active request.'));
|
|
625
|
+
started = true;
|
|
626
|
+
this.#active = iterator;
|
|
627
|
+
this.#abort = abort;
|
|
628
|
+
}
|
|
629
|
+
return iterator.next();
|
|
630
|
+
},
|
|
631
|
+
return: () => {
|
|
632
|
+
if (!started)
|
|
633
|
+
returnedBeforeStart = true;
|
|
634
|
+
return iterator.return(undefined);
|
|
635
|
+
},
|
|
636
|
+
throw: (error) => {
|
|
637
|
+
if (!started)
|
|
638
|
+
returnedBeforeStart = true;
|
|
639
|
+
return iterator.throw(error);
|
|
640
|
+
}
|
|
641
|
+
})
|
|
642
|
+
};
|
|
643
|
+
}
|
|
644
|
+
async *run(request, signal) {
|
|
645
|
+
let responseId;
|
|
646
|
+
const outputItems = [];
|
|
647
|
+
let completed = false;
|
|
648
|
+
let sawUnrepresentedOutput = false;
|
|
649
|
+
let outputMessageCount = 0;
|
|
650
|
+
let finished = false;
|
|
651
|
+
try {
|
|
652
|
+
const prepared = this.prepareRequest(request);
|
|
653
|
+
this.#completedResponseId = undefined;
|
|
654
|
+
for await (const event of this.adapter.startTurn(request, signal, this, prepared)) {
|
|
655
|
+
if (event.type === 'response.item' ||
|
|
656
|
+
event.type === 'text.completed' ||
|
|
657
|
+
event.type === 'client_tool.call')
|
|
658
|
+
outputMessageCount++;
|
|
659
|
+
if (event.type === 'response.started' && event.responseId !== 'unknown')
|
|
660
|
+
responseId = event.responseId;
|
|
661
|
+
else if (event.type === 'response.item')
|
|
662
|
+
outputItems.push(JSON.parse(JSON.stringify(event.item)));
|
|
663
|
+
else if (event.type === 'text.delta' ||
|
|
664
|
+
event.type === 'output.delta' ||
|
|
665
|
+
event.type === 'text.completed' ||
|
|
666
|
+
event.type === 'reasoning.completed' ||
|
|
667
|
+
event.type === 'client_tool.call' ||
|
|
668
|
+
event.type === 'hosted_tool' ||
|
|
669
|
+
event.type === 'citation' ||
|
|
670
|
+
event.type === 'unknown')
|
|
671
|
+
sawUnrepresentedOutput = true;
|
|
672
|
+
else if (event.type === 'completed')
|
|
673
|
+
completed = event.reason === 'completed' || event.reason === 'tool_use';
|
|
674
|
+
yield event;
|
|
675
|
+
}
|
|
676
|
+
responseId = this.#completedResponseId ?? responseId;
|
|
677
|
+
if (responsesContinuationEnabled(this.adapter.configuration) &&
|
|
678
|
+
completed &&
|
|
679
|
+
responseId !== undefined &&
|
|
680
|
+
(outputItems.length > 0 || !sawUnrepresentedOutput)) {
|
|
681
|
+
this.state.lastRequest =
|
|
682
|
+
this.adapter.configuration.responsesTransport?.transport === 'WEBSOCKET'
|
|
683
|
+
? { ...prepared.body, input: [] }
|
|
684
|
+
: prepared.fullBody;
|
|
685
|
+
this.state.lastMessageCount = request.messages.length + outputMessageCount;
|
|
686
|
+
this.state.lastInputItemCount = prepared.inputItemCount;
|
|
687
|
+
this.state.lastSystem = JSON.stringify([request.system, request.systemBlocks]);
|
|
688
|
+
this.state.lastResponse = { responseId, outputItems };
|
|
689
|
+
}
|
|
690
|
+
else
|
|
691
|
+
clearResponsesContinuation(this.state);
|
|
692
|
+
finished = true;
|
|
693
|
+
}
|
|
694
|
+
catch (error) {
|
|
695
|
+
clearResponsesContinuation(this.state);
|
|
696
|
+
throw error;
|
|
697
|
+
}
|
|
698
|
+
finally {
|
|
699
|
+
if (!finished)
|
|
700
|
+
invalidateResponsesWebSocket(this.state, true);
|
|
701
|
+
this.#completedResponseId = undefined;
|
|
702
|
+
this.#active = undefined;
|
|
703
|
+
this.#abort = undefined;
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
close() {
|
|
707
|
+
if (this.#closePromise !== undefined)
|
|
708
|
+
return this.#closePromise;
|
|
709
|
+
this.#closed = true;
|
|
710
|
+
const active = this.#active;
|
|
711
|
+
const abort = this.#abort;
|
|
712
|
+
this.#closePromise = Promise.resolve().then(async () => {
|
|
713
|
+
try {
|
|
714
|
+
if (active !== undefined) {
|
|
715
|
+
abort?.abort();
|
|
716
|
+
await active.return(undefined);
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
finally {
|
|
720
|
+
this.cache.release(this.state, this.cacheLease);
|
|
721
|
+
}
|
|
722
|
+
});
|
|
723
|
+
return this.#closePromise;
|
|
724
|
+
}
|
|
725
|
+
resetContinuation() {
|
|
726
|
+
clearResponsesContinuation(this.state);
|
|
727
|
+
}
|
|
728
|
+
continuationInputCount() {
|
|
729
|
+
return this.state.lastMessageCount ?? 0;
|
|
730
|
+
}
|
|
731
|
+
commitContinuationInput(messageCount) {
|
|
732
|
+
if (this.state.lastResponse !== undefined)
|
|
733
|
+
this.state.lastMessageCount = messageCount;
|
|
734
|
+
}
|
|
735
|
+
captureCompletedResponseId(responseId) {
|
|
736
|
+
if (responseId !== undefined)
|
|
737
|
+
this.#completedResponseId = responseId;
|
|
738
|
+
}
|
|
739
|
+
prepareRequest(request) {
|
|
740
|
+
assertInlineMediaBudget(request);
|
|
741
|
+
if (this.adapter.configuration.responsesTransport?.transport === 'WEBSOCKET' &&
|
|
742
|
+
this.state.socket !== undefined &&
|
|
743
|
+
this.state.socket.readyState !== WebSocket.OPEN) {
|
|
744
|
+
invalidateResponsesWebSocket(this.state, false);
|
|
745
|
+
clearResponsesContinuation(this.state);
|
|
746
|
+
}
|
|
747
|
+
// The continuation baseline must not share mutable schemas or items with callers.
|
|
748
|
+
const snapshot = (body) => JSON.parse(JSON.stringify(body));
|
|
749
|
+
let full;
|
|
750
|
+
const fullBody = () => (full ??= snapshot(responsesRequestBody(this.adapter.configuration, request, request.messages)));
|
|
751
|
+
if (responsesContinuationEnabled(this.adapter.configuration)) {
|
|
752
|
+
const websocket = this.adapter.configuration.responsesTransport?.transport === 'WEBSOCKET';
|
|
753
|
+
const suffixMessages = websocket && this.state.lastMessageCount !== undefined
|
|
754
|
+
? request.messages.slice(this.state.lastMessageCount)
|
|
755
|
+
: undefined;
|
|
756
|
+
const suffixBody = websocket &&
|
|
757
|
+
this.state.lastResponse !== undefined &&
|
|
758
|
+
suffixMessages !== undefined &&
|
|
759
|
+
this.state.lastRequest !== undefined &&
|
|
760
|
+
this.state.lastSystem === JSON.stringify([request.system, request.systemBlocks]) &&
|
|
761
|
+
// Top-level images attach to the last real user, which must be in this suffix.
|
|
762
|
+
(!request.images?.length || suffixMessages.some(isRealUserMessage))
|
|
763
|
+
? snapshot(responsesRequestBody(this.adapter.configuration, { ...request, system: '', systemBlocks: [] }, suffixMessages, this.state.lastResponse.outputItems))
|
|
764
|
+
: undefined;
|
|
765
|
+
const incremental = websocket
|
|
766
|
+
? suffixBody !== undefined &&
|
|
767
|
+
responsesContinuationPropertiesMatch(this.state.lastRequest, suffixBody)
|
|
768
|
+
? suffixBody.input
|
|
769
|
+
: undefined
|
|
770
|
+
: incrementalResponsesInput(this.state, fullBody());
|
|
771
|
+
if (incremental !== undefined) {
|
|
772
|
+
const responseId = this.state.lastResponse.responseId;
|
|
773
|
+
const inputItemCount = websocket
|
|
774
|
+
? (this.state.lastInputItemCount ?? 0) +
|
|
775
|
+
this.state.lastResponse.outputItems.length +
|
|
776
|
+
incremental.length
|
|
777
|
+
: fullBody().input.length;
|
|
778
|
+
delete this.state.lastResponse;
|
|
779
|
+
return {
|
|
780
|
+
get fullBody() {
|
|
781
|
+
return fullBody();
|
|
782
|
+
},
|
|
783
|
+
inputItemCount,
|
|
784
|
+
body: {
|
|
785
|
+
...(suffixBody ?? fullBody()),
|
|
786
|
+
input: incremental,
|
|
787
|
+
previous_response_id: responseId
|
|
788
|
+
},
|
|
789
|
+
continuation: true
|
|
790
|
+
};
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
return {
|
|
794
|
+
fullBody: fullBody(),
|
|
795
|
+
inputItemCount: fullBody().input.length,
|
|
796
|
+
body: fullBody(),
|
|
797
|
+
continuation: false
|
|
798
|
+
};
|
|
799
|
+
}
|
|
800
|
+
currentTurnState() {
|
|
801
|
+
return this.#turnState;
|
|
802
|
+
}
|
|
803
|
+
beginAttempt() {
|
|
804
|
+
this.#connectionDiagnostic = undefined;
|
|
805
|
+
this.#webSocketDiagnostic = undefined;
|
|
806
|
+
}
|
|
807
|
+
captureWebSocketDiagnostic(diagnostic) {
|
|
808
|
+
this.#webSocketDiagnostic = diagnostic;
|
|
809
|
+
}
|
|
810
|
+
connectionDiagnostic() {
|
|
811
|
+
return { ...this.#connectionDiagnostic, ...this.#webSocketDiagnostic };
|
|
812
|
+
}
|
|
813
|
+
captureTurnState(value) {
|
|
814
|
+
if (this.#turnState !== undefined || typeof value !== 'string')
|
|
815
|
+
return;
|
|
816
|
+
const bounded = boundedError(value.trim(), 8_192);
|
|
817
|
+
if (bounded)
|
|
818
|
+
this.#turnState = bounded;
|
|
819
|
+
}
|
|
820
|
+
withTurnStateMetadata(body) {
|
|
821
|
+
if (this.#turnState === undefined)
|
|
822
|
+
return body;
|
|
823
|
+
return {
|
|
824
|
+
...body,
|
|
825
|
+
client_metadata: {
|
|
826
|
+
...record(body.client_metadata),
|
|
827
|
+
[X_CODEX_TURN_STATE]: this.#turnState
|
|
828
|
+
}
|
|
829
|
+
};
|
|
830
|
+
}
|
|
831
|
+
invalidateWebSocket() {
|
|
832
|
+
invalidateResponsesWebSocket(this.state, true);
|
|
833
|
+
}
|
|
834
|
+
async webSocketForRequest(input) {
|
|
835
|
+
const endpoint = webSocketEndpoint(input.configuration.baseUrl);
|
|
836
|
+
const existingSocket = this.state.socket;
|
|
837
|
+
if (existingSocket !== undefined &&
|
|
838
|
+
(existingSocket.readyState !== WebSocket.OPEN || this.state.endpoint !== endpoint)) {
|
|
839
|
+
invalidateResponsesWebSocket(this.state, false);
|
|
840
|
+
if (input.continuation) {
|
|
841
|
+
clearResponsesContinuation(this.state);
|
|
842
|
+
throw localContinuationReset();
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
if (input.continuation && this.state.socket === undefined) {
|
|
846
|
+
clearResponsesContinuation(this.state);
|
|
847
|
+
throw localContinuationReset();
|
|
848
|
+
}
|
|
849
|
+
if (this.state.socket === undefined) {
|
|
850
|
+
const handshakeCredential = await acquireResponsesCredential(input.configuration, input.reason, input.signal);
|
|
851
|
+
input.credentialAcquired(handshakeCredential.revision);
|
|
852
|
+
await this.connectWebSocket(endpoint, handshakeCredential, input.promptCacheKey, input.signal);
|
|
853
|
+
}
|
|
854
|
+
const requestCredential = await acquireResponsesCredential(input.configuration, input.reason, input.signal);
|
|
855
|
+
input.credentialAcquired(requestCredential.revision);
|
|
856
|
+
if (requestCredential.revision !== this.state.credentialRevision ||
|
|
857
|
+
responsesCredentialFingerprint(requestCredential) !== this.state.credentialFingerprint) {
|
|
858
|
+
invalidateResponsesWebSocket(this.state, false);
|
|
859
|
+
if (input.continuation) {
|
|
860
|
+
clearResponsesContinuation(this.state);
|
|
861
|
+
throw localContinuationReset();
|
|
862
|
+
}
|
|
863
|
+
await this.connectWebSocket(endpoint, requestCredential, input.promptCacheKey, input.signal);
|
|
864
|
+
}
|
|
865
|
+
if (input.signal.aborted) {
|
|
866
|
+
invalidateResponsesWebSocket(this.state, true);
|
|
867
|
+
throw input.signal.reason;
|
|
868
|
+
}
|
|
869
|
+
const socket = this.state.socket;
|
|
870
|
+
if (socket?.readyState !== WebSocket.OPEN) {
|
|
871
|
+
invalidateResponsesWebSocket(this.state, true);
|
|
872
|
+
throw new AgentError('MAR_AGENT_MODEL_CONNECTION_FAILED', 'OpenAI Responses WebSocket connection is unavailable.', {
|
|
873
|
+
details: {
|
|
874
|
+
transport: 'WEBSOCKET',
|
|
875
|
+
continuation: input.continuation,
|
|
876
|
+
reason: 'MODEL_TRANSPORT_FAILURE'
|
|
877
|
+
},
|
|
878
|
+
retryable: true
|
|
879
|
+
});
|
|
880
|
+
}
|
|
881
|
+
if (this.#connectionDiagnostic === undefined &&
|
|
882
|
+
this.state.connectionId !== undefined &&
|
|
883
|
+
this.state.proxyType !== undefined)
|
|
884
|
+
this.#connectionDiagnostic = {
|
|
885
|
+
connectionId: this.state.connectionId,
|
|
886
|
+
connectionReused: true,
|
|
887
|
+
proxyType: this.state.proxyType
|
|
888
|
+
};
|
|
889
|
+
return socket;
|
|
890
|
+
}
|
|
891
|
+
async connectWebSocket(endpoint, credential, promptCacheKey, signal) {
|
|
892
|
+
const parsedEndpoint = new URL(endpoint);
|
|
893
|
+
const connectionId = randomUUID();
|
|
894
|
+
const proxyType = credential.socks5Url === undefined ? 'DIRECT' : 'SOCKS5';
|
|
895
|
+
this.#connectionDiagnostic = { connectionId, connectionReused: false, proxyType };
|
|
896
|
+
const socket = new WebSocket(parsedEndpoint, {
|
|
897
|
+
headers: Object.fromEntries(webSocketHeaders(credential, promptCacheKey)),
|
|
898
|
+
perMessageDeflate: false,
|
|
899
|
+
maxPayload: 16 * 1024 * 1024,
|
|
900
|
+
handshakeTimeout: 30_000,
|
|
901
|
+
...(credential.socks5Url === undefined
|
|
902
|
+
? {}
|
|
903
|
+
: {
|
|
904
|
+
createConnection: socks5WebSocketConnection(credential.socks5Url.reveal(), parsedEndpoint, signal)
|
|
905
|
+
})
|
|
906
|
+
});
|
|
907
|
+
try {
|
|
908
|
+
await waitForWebSocketOpen(socket, signal);
|
|
909
|
+
}
|
|
910
|
+
catch (error) {
|
|
911
|
+
terminateWebSocket(socket);
|
|
912
|
+
throw error;
|
|
913
|
+
}
|
|
914
|
+
this.state.socket = socket;
|
|
915
|
+
this.state.endpoint = endpoint;
|
|
916
|
+
this.state.credentialRevision = credential.revision;
|
|
917
|
+
this.state.credentialFingerprint = responsesCredentialFingerprint(credential);
|
|
918
|
+
this.state.connectionId = connectionId;
|
|
919
|
+
this.state.proxyType = proxyType;
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
function localContinuationReset() {
|
|
923
|
+
return new AgentError('MAR_AGENT_RESPONSES_CONTINUATION_RESET', 'The Responses connection changed before the incremental request was sent.');
|
|
924
|
+
}
|
|
925
|
+
function isLocalContinuationReset(error) {
|
|
926
|
+
return error instanceof AgentError && error.code === 'MAR_AGENT_RESPONSES_CONTINUATION_RESET';
|
|
927
|
+
}
|
|
928
|
+
function incrementalResponsesInput(state, current) {
|
|
929
|
+
const previous = state.lastRequest;
|
|
930
|
+
const completion = state.lastResponse;
|
|
931
|
+
if (previous === undefined || completion === undefined)
|
|
932
|
+
return undefined;
|
|
933
|
+
if (!responsesContinuationPropertiesMatch(previous, current))
|
|
934
|
+
return undefined;
|
|
935
|
+
const baseline = [...previous.input, ...completion.outputItems];
|
|
936
|
+
if (current.input.length < baseline.length)
|
|
937
|
+
return undefined;
|
|
938
|
+
for (let index = 0; index < baseline.length; index++)
|
|
939
|
+
if (JSON.stringify(baseline[index]) !== JSON.stringify(current.input[index]))
|
|
940
|
+
return undefined;
|
|
941
|
+
return current.input.slice(baseline.length);
|
|
942
|
+
}
|
|
943
|
+
function responsesContinuationPropertiesMatch(previous, current) {
|
|
944
|
+
return (JSON.stringify({ ...previous, input: undefined, previous_response_id: undefined }) ===
|
|
945
|
+
JSON.stringify({ ...current, input: undefined, previous_response_id: undefined }));
|
|
946
|
+
}
|
|
947
|
+
function hashResponsesValue(value) {
|
|
948
|
+
return createHash('sha256').update(JSON.stringify(value)).digest('hex');
|
|
949
|
+
}
|
|
950
|
+
function responsesContinuationEnabled(configuration) {
|
|
951
|
+
return configuration.responsesPreviousResponseId === true;
|
|
952
|
+
}
|
|
953
|
+
function responsesCredentialFingerprint(credential) {
|
|
954
|
+
return hashResponsesValue({
|
|
955
|
+
bearer: credential.bearer.reveal(),
|
|
956
|
+
headers: Object.fromEntries(Object.entries(credential.headers).map(([name, value]) => [
|
|
957
|
+
name,
|
|
958
|
+
typeof value === 'string' ? value : value.reveal()
|
|
959
|
+
])),
|
|
960
|
+
socks5Url: credential.socks5Url?.reveal()
|
|
961
|
+
});
|
|
962
|
+
}
|
|
963
|
+
function turnStateFromEvent(event) {
|
|
964
|
+
for (const headers of [
|
|
965
|
+
record(event.headers),
|
|
966
|
+
record(record(event.response).headers),
|
|
967
|
+
record(record(event.metadata).headers)
|
|
968
|
+
]) {
|
|
969
|
+
for (const [name, value] of Object.entries(headers))
|
|
970
|
+
if (name.toLowerCase() === X_CODEX_TURN_STATE && typeof value === 'string')
|
|
971
|
+
return value;
|
|
972
|
+
}
|
|
973
|
+
return undefined;
|
|
974
|
+
}
|
|
975
|
+
async function* parseOpenAiResponsesSse(response, signal, continuation, sensitiveValues) {
|
|
976
|
+
try {
|
|
977
|
+
yield* parseSse(response, signal, undefined, undefined, sensitiveValues);
|
|
978
|
+
}
|
|
979
|
+
catch (cause) {
|
|
980
|
+
if (response.ok && !signal.aborted && isRetryableOpenAiStreamReadError(cause))
|
|
981
|
+
throw new AgentError('MAR_AGENT_MODEL_PROTOCOL_ERROR', 'OpenAI Responses stream was interrupted.', {
|
|
982
|
+
details: {
|
|
983
|
+
reason: OPENAI_RESPONSES_EARLY_EOF_REASON,
|
|
984
|
+
retryAfter: response.headers.get('retry-after'),
|
|
985
|
+
continuation
|
|
986
|
+
},
|
|
987
|
+
retryable: true,
|
|
988
|
+
cause
|
|
989
|
+
});
|
|
990
|
+
throw cause;
|
|
991
|
+
}
|
|
992
|
+
}
|
|
993
|
+
async function* webSocketResponseEvents(input) {
|
|
994
|
+
const body = input.turn.withTurnStateMetadata(input.body);
|
|
995
|
+
const payload = Buffer.from(JSON.stringify({ type: 'response.create', ...body }), 'utf8');
|
|
996
|
+
const diagnostic = webSocketRequestDiagnostic(body, payload.length);
|
|
997
|
+
input.turn.captureWebSocketDiagnostic(diagnostic);
|
|
998
|
+
const socket = await input.turn.webSocketForRequest({
|
|
999
|
+
configuration: input.configuration,
|
|
1000
|
+
promptCacheKey: input.promptCacheKey,
|
|
1001
|
+
reason: input.reason,
|
|
1002
|
+
signal: input.signal,
|
|
1003
|
+
continuation: input.continuation,
|
|
1004
|
+
credentialAcquired: input.credentialAcquired
|
|
1005
|
+
});
|
|
1006
|
+
const messages = webSocketMessages(socket, input.signal, (code) => {
|
|
1007
|
+
diagnostic.webSocketCloseCode = code;
|
|
1008
|
+
});
|
|
1009
|
+
try {
|
|
1010
|
+
diagnostic.webSocketPhase = 'SEND';
|
|
1011
|
+
await sendWebSocketMessage(socket, payload, input.signal, () => {
|
|
1012
|
+
diagnostic.sentFrameCount = (diagnostic.sentFrameCount ?? 0) + 1;
|
|
1013
|
+
});
|
|
1014
|
+
diagnostic.webSocketPhase = 'RECEIVE';
|
|
1015
|
+
let terminal = false;
|
|
1016
|
+
for await (const item of messages) {
|
|
1017
|
+
const event = record(item);
|
|
1018
|
+
const type = text(event.type);
|
|
1019
|
+
input.turn.captureTurnState(turnStateFromEvent(event));
|
|
1020
|
+
if (type !== 'response.metadata' && type !== 'codex.response.metadata')
|
|
1021
|
+
yield item;
|
|
1022
|
+
if (type === 'response.completed' ||
|
|
1023
|
+
type === 'response.incomplete' ||
|
|
1024
|
+
type === 'response.failed' ||
|
|
1025
|
+
type === 'error') {
|
|
1026
|
+
terminal = true;
|
|
1027
|
+
return;
|
|
1028
|
+
}
|
|
1029
|
+
}
|
|
1030
|
+
// Intentionally treat a close without a terminal model event as retryable EOF,
|
|
1031
|
+
// including close code 1009. Close codes remain diagnostic evidence rather than
|
|
1032
|
+
// a separate model-error classification. The shared attempt budget still bounds
|
|
1033
|
+
// retries; this policy does not shrink the request or guarantee recovery.
|
|
1034
|
+
if (!terminal && !input.signal.aborted)
|
|
1035
|
+
throw new AgentError('MAR_AGENT_MODEL_PROTOCOL_ERROR', 'OpenAI Responses WebSocket closed before a terminal event.', {
|
|
1036
|
+
details: {
|
|
1037
|
+
reason: OPENAI_RESPONSES_EARLY_EOF_REASON,
|
|
1038
|
+
transport: 'WEBSOCKET',
|
|
1039
|
+
continuation: input.continuation
|
|
1040
|
+
},
|
|
1041
|
+
retryable: true
|
|
1042
|
+
});
|
|
1043
|
+
}
|
|
1044
|
+
finally {
|
|
1045
|
+
messages.dispose();
|
|
1046
|
+
}
|
|
1047
|
+
}
|
|
1048
|
+
function webSocketRequestDiagnostic(body, requestBytes) {
|
|
1049
|
+
let imageCount = 0;
|
|
1050
|
+
let imageBytes = 0;
|
|
1051
|
+
for (const item of body.input) {
|
|
1052
|
+
const entry = record(item);
|
|
1053
|
+
const parts = Array.isArray(entry.content)
|
|
1054
|
+
? entry.content
|
|
1055
|
+
: Array.isArray(entry.output)
|
|
1056
|
+
? entry.output
|
|
1057
|
+
: [];
|
|
1058
|
+
for (const value of parts) {
|
|
1059
|
+
const part = record(value);
|
|
1060
|
+
if (part.type !== 'input_image')
|
|
1061
|
+
continue;
|
|
1062
|
+
imageCount++;
|
|
1063
|
+
const url = text(part.image_url);
|
|
1064
|
+
if (url === undefined)
|
|
1065
|
+
continue;
|
|
1066
|
+
const marker = url.indexOf(';base64,');
|
|
1067
|
+
if (url.startsWith('data:') && marker >= 0)
|
|
1068
|
+
imageBytes += Buffer.byteLength(url.slice(marker + 8), 'base64');
|
|
1069
|
+
}
|
|
1070
|
+
}
|
|
1071
|
+
return {
|
|
1072
|
+
requestBytes,
|
|
1073
|
+
imageCount,
|
|
1074
|
+
imageBytes,
|
|
1075
|
+
requestFrameCount: Math.ceil(requestBytes / WEBSOCKET_SEND_FRAME_BYTES),
|
|
1076
|
+
sentFrameCount: 0,
|
|
1077
|
+
webSocketPhase: 'CONNECT'
|
|
1078
|
+
};
|
|
1079
|
+
}
|
|
1080
|
+
async function waitForWebSocketOpen(socket, signal) {
|
|
1081
|
+
if (signal.aborted) {
|
|
1082
|
+
terminateWebSocket(socket);
|
|
1083
|
+
throw signal.reason;
|
|
1084
|
+
}
|
|
1085
|
+
await new Promise((resolve, reject) => {
|
|
1086
|
+
const cleanup = () => {
|
|
1087
|
+
signal.removeEventListener('abort', abort);
|
|
1088
|
+
socket.off('open', open);
|
|
1089
|
+
socket.off('error', error);
|
|
1090
|
+
socket.off('close', close);
|
|
1091
|
+
socket.off('unexpected-response', unexpectedResponse);
|
|
1092
|
+
};
|
|
1093
|
+
const settle = (operation) => {
|
|
1094
|
+
cleanup();
|
|
1095
|
+
operation();
|
|
1096
|
+
};
|
|
1097
|
+
const abort = () => settle(() => {
|
|
1098
|
+
terminateWebSocket(socket);
|
|
1099
|
+
reject(signal.reason);
|
|
1100
|
+
});
|
|
1101
|
+
const open = () => settle(resolve);
|
|
1102
|
+
const error = (cause) => settle(() => reject(new AgentError('MAR_AGENT_MODEL_CONNECTION_FAILED', 'OpenAI Responses WebSocket connection failed.', {
|
|
1103
|
+
details: { reason: 'MODEL_TRANSPORT_FAILURE', transport: 'WEBSOCKET' },
|
|
1104
|
+
retryable: true,
|
|
1105
|
+
cause
|
|
1106
|
+
})));
|
|
1107
|
+
const close = () => settle(() => reject(new AgentError('MAR_AGENT_MODEL_CONNECTION_FAILED', 'OpenAI Responses WebSocket closed during connection.', {
|
|
1108
|
+
details: { reason: 'MODEL_TRANSPORT_FAILURE', transport: 'WEBSOCKET' },
|
|
1109
|
+
retryable: true
|
|
1110
|
+
})));
|
|
1111
|
+
const unexpectedResponse = (_request, response) => settle(() => {
|
|
1112
|
+
const status = response.statusCode ?? 0;
|
|
1113
|
+
response.resume();
|
|
1114
|
+
terminateWebSocket(socket);
|
|
1115
|
+
const retryable = status !== 401 && isRetryableModelHttpStatus(status);
|
|
1116
|
+
const retryAfter = response.headers['retry-after'];
|
|
1117
|
+
const retryAfterValue = Array.isArray(retryAfter) ? retryAfter[0] : retryAfter;
|
|
1118
|
+
reject(new AgentError(status === 401 ? 'MAR_AGENT_MODEL_AUTH_FAILED' : 'MAR_AGENT_MODEL_CONNECTION_FAILED', status === 401
|
|
1119
|
+
? 'OpenAI Responses WebSocket authentication failed.'
|
|
1120
|
+
: 'OpenAI Responses WebSocket handshake failed.', {
|
|
1121
|
+
details: {
|
|
1122
|
+
status,
|
|
1123
|
+
transport: 'WEBSOCKET',
|
|
1124
|
+
...(retryAfterValue === undefined ? {} : { retryAfter: retryAfterValue })
|
|
1125
|
+
},
|
|
1126
|
+
...(retryable ? { retryable: true } : {})
|
|
1127
|
+
}));
|
|
1128
|
+
});
|
|
1129
|
+
signal.addEventListener('abort', abort, { once: true });
|
|
1130
|
+
socket.once('open', open);
|
|
1131
|
+
socket.once('error', error);
|
|
1132
|
+
socket.once('close', close);
|
|
1133
|
+
socket.once('unexpected-response', unexpectedResponse);
|
|
1134
|
+
});
|
|
1135
|
+
}
|
|
1136
|
+
async function sendWebSocketMessage(socket, payload, signal, frameSent) {
|
|
1137
|
+
for (let offset = 0; offset < payload.length; offset += WEBSOCKET_SEND_FRAME_BYTES) {
|
|
1138
|
+
if (signal.aborted)
|
|
1139
|
+
throw signal.reason;
|
|
1140
|
+
const end = Math.min(payload.length, offset + WEBSOCKET_SEND_FRAME_BYTES);
|
|
1141
|
+
await new Promise((resolve, reject) => {
|
|
1142
|
+
let settled = false;
|
|
1143
|
+
const cleanup = () => {
|
|
1144
|
+
settled = true;
|
|
1145
|
+
signal.removeEventListener('abort', abort);
|
|
1146
|
+
socket.off('error', error);
|
|
1147
|
+
socket.off('close', close);
|
|
1148
|
+
};
|
|
1149
|
+
const abort = () => {
|
|
1150
|
+
cleanup();
|
|
1151
|
+
socket.terminate();
|
|
1152
|
+
reject(signal.reason);
|
|
1153
|
+
};
|
|
1154
|
+
const error = (cause) => {
|
|
1155
|
+
cleanup();
|
|
1156
|
+
reject(webSocketReceiveError(cause));
|
|
1157
|
+
};
|
|
1158
|
+
const close = () => error(new Error('WebSocket closed while sending the request.'));
|
|
1159
|
+
signal.addEventListener('abort', abort, { once: true });
|
|
1160
|
+
socket.once('error', error);
|
|
1161
|
+
socket.once('close', close);
|
|
1162
|
+
try {
|
|
1163
|
+
socket.send(payload.subarray(offset, end), { binary: false, fin: end === payload.length }, (cause) => {
|
|
1164
|
+
if (settled)
|
|
1165
|
+
return;
|
|
1166
|
+
cleanup();
|
|
1167
|
+
if (cause)
|
|
1168
|
+
reject(webSocketReceiveError(cause));
|
|
1169
|
+
else if (signal.aborted)
|
|
1170
|
+
reject(signal.reason);
|
|
1171
|
+
else {
|
|
1172
|
+
frameSent();
|
|
1173
|
+
resolve();
|
|
1174
|
+
}
|
|
1175
|
+
});
|
|
1176
|
+
}
|
|
1177
|
+
catch (cause) {
|
|
1178
|
+
cleanup();
|
|
1179
|
+
reject(webSocketReceiveError(cause));
|
|
1180
|
+
}
|
|
1181
|
+
});
|
|
1182
|
+
}
|
|
1183
|
+
}
|
|
1184
|
+
function webSocketMessages(socket, signal, closed) {
|
|
1185
|
+
const queue = [];
|
|
1186
|
+
let resume;
|
|
1187
|
+
const push = (item) => {
|
|
1188
|
+
queue.push(item);
|
|
1189
|
+
resume?.();
|
|
1190
|
+
resume = undefined;
|
|
1191
|
+
};
|
|
1192
|
+
const message = (value) => push({
|
|
1193
|
+
type: 'message',
|
|
1194
|
+
value,
|
|
1195
|
+
observedAt: performance.now()
|
|
1196
|
+
});
|
|
1197
|
+
const close = (code) => {
|
|
1198
|
+
closed(code);
|
|
1199
|
+
push({ type: 'closed' });
|
|
1200
|
+
};
|
|
1201
|
+
const error = (cause) => push({
|
|
1202
|
+
type: 'error',
|
|
1203
|
+
error: webSocketReceiveError(cause)
|
|
1204
|
+
});
|
|
1205
|
+
const abort = () => {
|
|
1206
|
+
terminateWebSocket(socket);
|
|
1207
|
+
push({ type: 'error', error: signal.reason });
|
|
1208
|
+
};
|
|
1209
|
+
if (signal.aborted) {
|
|
1210
|
+
terminateWebSocket(socket);
|
|
1211
|
+
throw signal.reason;
|
|
1212
|
+
}
|
|
1213
|
+
socket.on('message', message);
|
|
1214
|
+
socket.on('close', close);
|
|
1215
|
+
socket.on('error', error);
|
|
1216
|
+
signal.addEventListener('abort', abort, { once: true });
|
|
1217
|
+
const dispose = () => {
|
|
1218
|
+
signal.removeEventListener('abort', abort);
|
|
1219
|
+
socket.off('message', message);
|
|
1220
|
+
socket.off('close', close);
|
|
1221
|
+
socket.off('error', error);
|
|
1222
|
+
};
|
|
1223
|
+
return {
|
|
1224
|
+
dispose,
|
|
1225
|
+
async *[Symbol.asyncIterator]() {
|
|
1226
|
+
try {
|
|
1227
|
+
let idleDeadline = Date.now() + WEBSOCKET_IDLE_TIMEOUT_MS;
|
|
1228
|
+
while (true) {
|
|
1229
|
+
if (Date.now() >= idleDeadline)
|
|
1230
|
+
throw webSocketIdleError(socket);
|
|
1231
|
+
if (queue.length === 0)
|
|
1232
|
+
await new Promise((resolve, reject) => {
|
|
1233
|
+
const timer = setTimeout(() => {
|
|
1234
|
+
resume = undefined;
|
|
1235
|
+
reject(webSocketIdleError(socket));
|
|
1236
|
+
}, Math.max(0, idleDeadline - Date.now()));
|
|
1237
|
+
resume = () => {
|
|
1238
|
+
clearTimeout(timer);
|
|
1239
|
+
resolve();
|
|
1240
|
+
};
|
|
1241
|
+
});
|
|
1242
|
+
const item = queue.shift();
|
|
1243
|
+
if (item === undefined)
|
|
1244
|
+
continue;
|
|
1245
|
+
if (item.type === 'closed')
|
|
1246
|
+
return;
|
|
1247
|
+
if (item.type === 'error')
|
|
1248
|
+
throw item.error;
|
|
1249
|
+
const text = await webSocketMessageText(item.value);
|
|
1250
|
+
if (Buffer.byteLength(text) > 16 * 1024 * 1024)
|
|
1251
|
+
throw new AgentError('MAR_AGENT_MODEL_PROTOCOL_ERROR', 'Model response exceeded the size limit.');
|
|
1252
|
+
try {
|
|
1253
|
+
const event = JSON.parse(text);
|
|
1254
|
+
if (!isWebSocketNonProgressEvent(event))
|
|
1255
|
+
idleDeadline = Date.now() + WEBSOCKET_IDLE_TIMEOUT_MS;
|
|
1256
|
+
yield observeModelStreamEvent(event, item.observedAt);
|
|
1257
|
+
}
|
|
1258
|
+
catch (cause) {
|
|
1259
|
+
throw new AgentError('MAR_AGENT_MODEL_PROTOCOL_ERROR', 'Model WebSocket contained invalid JSON.', { cause });
|
|
1260
|
+
}
|
|
1261
|
+
}
|
|
1262
|
+
}
|
|
1263
|
+
finally {
|
|
1264
|
+
dispose();
|
|
1265
|
+
}
|
|
1266
|
+
}
|
|
1267
|
+
};
|
|
1268
|
+
}
|
|
1269
|
+
function webSocketIdleError(socket) {
|
|
1270
|
+
terminateWebSocket(socket);
|
|
1271
|
+
return new AgentError('MAR_AGENT_MODEL_TIMEOUT', 'OpenAI Responses WebSocket was idle for too long.', {
|
|
1272
|
+
details: { reason: OPENAI_RESPONSES_EARLY_EOF_REASON, transport: 'WEBSOCKET' },
|
|
1273
|
+
retryable: true
|
|
1274
|
+
});
|
|
1275
|
+
}
|
|
1276
|
+
function isWebSocketNonProgressEvent(value) {
|
|
1277
|
+
const type = text(record(value).type);
|
|
1278
|
+
return (type === 'response.metadata' ||
|
|
1279
|
+
type === 'codex.response.metadata' ||
|
|
1280
|
+
type === 'responsesapi.websocket_timing' ||
|
|
1281
|
+
type === 'response.queued' ||
|
|
1282
|
+
type === 'response.in_progress');
|
|
1283
|
+
}
|
|
1284
|
+
async function webSocketMessageText(value) {
|
|
1285
|
+
if (typeof value === 'string')
|
|
1286
|
+
return value;
|
|
1287
|
+
if (Buffer.isBuffer(value))
|
|
1288
|
+
return value.toString('utf8');
|
|
1289
|
+
if (value instanceof ArrayBuffer)
|
|
1290
|
+
return Buffer.from(value).toString('utf8');
|
|
1291
|
+
if (ArrayBuffer.isView(value))
|
|
1292
|
+
return Buffer.from(value.buffer, value.byteOffset, value.byteLength).toString('utf8');
|
|
1293
|
+
if (Array.isArray(value) && value.every((part) => Buffer.isBuffer(part)))
|
|
1294
|
+
return Buffer.concat(value).toString('utf8');
|
|
1295
|
+
throw new AgentError('MAR_AGENT_MODEL_PROTOCOL_ERROR', 'Model WebSocket returned an unsupported frame.');
|
|
1296
|
+
}
|
|
1297
|
+
function webSocketHeaders(credential, promptCacheKey) {
|
|
1298
|
+
const headers = responsesHeaders(credential, promptCacheKey);
|
|
1299
|
+
headers.delete('accept');
|
|
1300
|
+
headers.delete('content-type');
|
|
1301
|
+
headers.set('openai-beta', 'responses_websockets=2026-02-06');
|
|
1302
|
+
return headers;
|
|
1303
|
+
}
|
|
1304
|
+
function webSocketEndpoint(baseUrl) {
|
|
1305
|
+
const endpoint = new URL(responsesEndpoint(baseUrl));
|
|
1306
|
+
endpoint.protocol = endpoint.protocol === 'https:' ? 'wss:' : 'ws:';
|
|
1307
|
+
return endpoint.toString();
|
|
1308
|
+
}
|
|
1309
|
+
function socks5WebSocketConnection(socks5Url, endpoint, signal) {
|
|
1310
|
+
return ((_options, callback) => {
|
|
1311
|
+
if (callback === undefined)
|
|
1312
|
+
throw new AgentError('MAR_AGENT_MODEL_CONNECTION_FAILED', 'WebSocket connection callback is unavailable.');
|
|
1313
|
+
void connectSocks5Target({
|
|
1314
|
+
socks5Url,
|
|
1315
|
+
targetHost: endpoint.hostname,
|
|
1316
|
+
targetPort: Number(endpoint.port || (endpoint.protocol === 'wss:' ? 443 : 80)),
|
|
1317
|
+
tls: endpoint.protocol === 'wss:',
|
|
1318
|
+
servername: endpoint.hostname,
|
|
1319
|
+
...(endpoint.protocol === 'wss:' ? { alpnProtocols: ['http/1.1'] } : {}),
|
|
1320
|
+
signal
|
|
1321
|
+
}).then((socket) => callback(null, socket), (error) => callback(error instanceof Error
|
|
1322
|
+
? error
|
|
1323
|
+
: new Error('SOCKS5 WebSocket connection failed.', { cause: error })));
|
|
1324
|
+
return undefined;
|
|
1325
|
+
});
|
|
1326
|
+
}
|
|
1327
|
+
function webSocketReceiveError(cause) {
|
|
1328
|
+
const code = cause.code;
|
|
1329
|
+
if (typeof code === 'string' && code.startsWith('WS_ERR_'))
|
|
1330
|
+
return new AgentError('MAR_AGENT_MODEL_PROTOCOL_ERROR', code === 'WS_ERR_UNSUPPORTED_MESSAGE_LENGTH'
|
|
1331
|
+
? 'Model response exceeded the size limit.'
|
|
1332
|
+
: 'Model WebSocket protocol is invalid.', { cause });
|
|
1333
|
+
return new AgentError('MAR_AGENT_MODEL_PROTOCOL_ERROR', 'OpenAI Responses WebSocket was interrupted.', {
|
|
1334
|
+
details: {
|
|
1335
|
+
reason: OPENAI_RESPONSES_EARLY_EOF_REASON,
|
|
1336
|
+
transport: 'WEBSOCKET'
|
|
1337
|
+
},
|
|
1338
|
+
retryable: true,
|
|
1339
|
+
cause
|
|
1340
|
+
});
|
|
1341
|
+
}
|
|
1342
|
+
function isSemanticEvent(event) {
|
|
1343
|
+
if (event.type.startsWith('output.'))
|
|
1344
|
+
return false;
|
|
1345
|
+
if (event.type === 'response.started' || event.type === 'usage' || event.type === 'response.item')
|
|
1346
|
+
return false;
|
|
1347
|
+
if (event.type === 'text.delta')
|
|
1348
|
+
return event.delta.length > 0;
|
|
1349
|
+
if (event.type === 'text.completed' || event.type === 'reasoning.completed')
|
|
1350
|
+
return event.text.length > 0;
|
|
1351
|
+
return true;
|
|
1352
|
+
}
|
|
1353
|
+
function isNonSemanticOpenAiResponsesEvent(type) {
|
|
1354
|
+
return NON_SEMANTIC_OPENAI_RESPONSES_EVENTS.has(type);
|
|
1355
|
+
}
|
|
1356
|
+
const NON_SEMANTIC_OPENAI_RESPONSES_EVENTS = new Set([
|
|
1357
|
+
'codex.response.metadata',
|
|
1358
|
+
'response.content_part.added',
|
|
1359
|
+
'response.content_part.done',
|
|
1360
|
+
'response.in_progress',
|
|
1361
|
+
'response.metadata',
|
|
1362
|
+
'response.queued',
|
|
1363
|
+
'response.reasoning_summary_part.added',
|
|
1364
|
+
'response.reasoning_summary_part.done',
|
|
1365
|
+
'responsesapi.websocket_timing'
|
|
1366
|
+
]);
|
|
1367
|
+
function isRetryableResponsesUpstreamError(code, message) {
|
|
1368
|
+
if (isRetryableUpstreamOverloadCode(code))
|
|
1369
|
+
return true;
|
|
1370
|
+
if (code !== 'invalid_request_error')
|
|
1371
|
+
return false;
|
|
1372
|
+
// Exact compatibility exception, not a general access/authentication retry rule.
|
|
1373
|
+
const summary = message?.startsWith(`${code}: `) ? message.slice(code.length + 2) : message;
|
|
1374
|
+
return summary === 'Unable to verify Daybreak Blue access. Please try again.';
|
|
1375
|
+
}
|
|
1376
|
+
export function isOpenAiResponsesRecoverableStreamError(error) {
|
|
1377
|
+
return (error instanceof AgentError &&
|
|
1378
|
+
error.retryable &&
|
|
1379
|
+
(error.details?.reason === OPENAI_RESPONSES_EARLY_EOF_REASON ||
|
|
1380
|
+
error.details?.reason === OPENAI_RESPONSES_WEBSOCKET_CONNECTION_LIMIT_REASON ||
|
|
1381
|
+
isRetryableResponsesUpstreamError(error.code, error.message)));
|
|
1382
|
+
}
|
|
1383
|
+
function isWebSocketConnectionLimitCode(code) {
|
|
1384
|
+
return code?.trim().toLowerCase() === 'websocket_connection_limit_reached';
|
|
1385
|
+
}
|
|
1386
|
+
function isRetryableOpenAiStreamReadError(error) {
|
|
1387
|
+
return (isRetryableModelTransportError(error) ||
|
|
1388
|
+
(error instanceof AgentError &&
|
|
1389
|
+
error.code === 'MAR_AGENT_MODEL_TIMEOUT' &&
|
|
1390
|
+
error.details?.reason === 'MODEL_STREAM_IDLE_TIMEOUT'));
|
|
1391
|
+
}
|
|
1392
|
+
function retryAfter(error) {
|
|
1393
|
+
const value = error.details?.retryAfter;
|
|
1394
|
+
return typeof value === 'string' ? value : null;
|
|
1395
|
+
}
|
|
1396
|
+
function openAiResponsesAttemptError(error) {
|
|
1397
|
+
const diagnostic = describeAgentError(error, 'MAR_AGENT_MODEL_ATTEMPT_FAILED');
|
|
1398
|
+
const causeCode = deepestCauseCode(error);
|
|
1399
|
+
const reason = error instanceof AgentError && typeof error.details?.reason === 'string'
|
|
1400
|
+
? error.details.reason
|
|
1401
|
+
: undefined;
|
|
1402
|
+
return {
|
|
1403
|
+
errorCode: diagnostic.code,
|
|
1404
|
+
errorMessage: diagnostic.message,
|
|
1405
|
+
...(causeCode === undefined || causeCode === diagnostic.code ? {} : { causeCode }),
|
|
1406
|
+
...(reason === undefined ? {} : { reason }),
|
|
1407
|
+
retryable: error instanceof AgentError && error.retryable
|
|
1408
|
+
};
|
|
1409
|
+
}
|
|
1410
|
+
function deepestCauseCode(error) {
|
|
1411
|
+
const seen = new Set();
|
|
1412
|
+
let current = error;
|
|
1413
|
+
let code;
|
|
1414
|
+
while (current !== undefined && current !== null && !seen.has(current) && seen.size < 8) {
|
|
1415
|
+
seen.add(current);
|
|
1416
|
+
const candidate = current instanceof AgentError
|
|
1417
|
+
? current.code
|
|
1418
|
+
: typeof current === 'object' && 'code' in current
|
|
1419
|
+
? current.code
|
|
1420
|
+
: undefined;
|
|
1421
|
+
if (typeof candidate === 'string' && candidate.trim())
|
|
1422
|
+
code = boundedError(candidate, 80) ?? code;
|
|
1423
|
+
current =
|
|
1424
|
+
typeof current === 'object' && 'cause' in current
|
|
1425
|
+
? current.cause
|
|
1426
|
+
: undefined;
|
|
1427
|
+
}
|
|
1428
|
+
return code;
|
|
1429
|
+
}
|
|
1430
|
+
function responsesEndpoint(baseUrl) {
|
|
1431
|
+
const normalized = baseUrl.replace(/\/$/, '');
|
|
1432
|
+
if (new URL(normalized).pathname.endsWith('/backend-api/codex'))
|
|
1433
|
+
return `${normalized}/responses`;
|
|
1434
|
+
return `${normalized.endsWith('/v1') ? normalized : `${normalized}/v1`}/responses`;
|
|
1435
|
+
}
|
|
1436
|
+
async function acquireResponsesCredential(configuration, reason, signal) {
|
|
1437
|
+
return acquireModelCredential(configuration, reason, 'OpenAI Responses requires a model credential.', signal);
|
|
1438
|
+
}
|
|
1439
|
+
const RESERVED_CREDENTIAL_HEADERS = new Set([
|
|
1440
|
+
'accept',
|
|
1441
|
+
'authorization',
|
|
1442
|
+
'connection',
|
|
1443
|
+
'content-length',
|
|
1444
|
+
'content-type',
|
|
1445
|
+
'host',
|
|
1446
|
+
'openai-beta',
|
|
1447
|
+
'proxy-authorization',
|
|
1448
|
+
'sec-websocket-accept',
|
|
1449
|
+
'sec-websocket-extensions',
|
|
1450
|
+
'sec-websocket-key',
|
|
1451
|
+
'sec-websocket-protocol',
|
|
1452
|
+
'sec-websocket-version',
|
|
1453
|
+
'session-id',
|
|
1454
|
+
'thread-id',
|
|
1455
|
+
'transfer-encoding',
|
|
1456
|
+
'upgrade',
|
|
1457
|
+
'x-client-request-id',
|
|
1458
|
+
X_CODEX_TURN_STATE,
|
|
1459
|
+
'x-openai-internal-codex-responses-lite'
|
|
1460
|
+
]);
|
|
1461
|
+
function responsesHeaders(credential, promptCacheKey, turnState) {
|
|
1462
|
+
const headers = new Headers({
|
|
1463
|
+
authorization: `Bearer ${credential.bearer.reveal()}`,
|
|
1464
|
+
'content-type': 'application/json',
|
|
1465
|
+
accept: 'text/event-stream',
|
|
1466
|
+
'user-agent': productUserAgent(),
|
|
1467
|
+
'x-client-request-id': randomUUID(),
|
|
1468
|
+
...(promptCacheKey
|
|
1469
|
+
? {
|
|
1470
|
+
'session-id': promptCacheKey,
|
|
1471
|
+
'thread-id': promptCacheKey
|
|
1472
|
+
}
|
|
1473
|
+
: {})
|
|
1474
|
+
});
|
|
1475
|
+
if (turnState)
|
|
1476
|
+
headers.set(X_CODEX_TURN_STATE, turnState);
|
|
1477
|
+
return appendModelCredentialHeaders(headers, credential, RESERVED_CREDENTIAL_HEADERS);
|
|
1478
|
+
}
|
|
1479
|
+
function withCredentialRevision(error, revision) {
|
|
1480
|
+
if (!(error instanceof AgentError) || revision === undefined)
|
|
1481
|
+
return error;
|
|
1482
|
+
const prefix = `${error.code}: `;
|
|
1483
|
+
return new AgentError(error.code, error.message.startsWith(prefix) ? error.message.slice(prefix.length) : error.message, {
|
|
1484
|
+
details: { ...error.details, credentialRevision: revision },
|
|
1485
|
+
retryable: error.retryable,
|
|
1486
|
+
cause: error
|
|
1487
|
+
});
|
|
1488
|
+
}
|
|
1489
|
+
function withResponsesAttemptDetails(error, continuation, transport) {
|
|
1490
|
+
if (!(error instanceof AgentError))
|
|
1491
|
+
return error;
|
|
1492
|
+
const prefix = `${error.code}: `;
|
|
1493
|
+
return new AgentError(error.code, error.message.startsWith(prefix) ? error.message.slice(prefix.length) : error.message, {
|
|
1494
|
+
details: {
|
|
1495
|
+
...error.details,
|
|
1496
|
+
continuation,
|
|
1497
|
+
...(transport === 'WEBSOCKET' ? { transport: 'WEBSOCKET' } : {})
|
|
1498
|
+
},
|
|
1499
|
+
retryable: error.retryable,
|
|
1500
|
+
cause: error
|
|
1501
|
+
});
|
|
1502
|
+
}
|
|
1503
|
+
function isModelAuthenticationFailure(error) {
|
|
1504
|
+
if (!(error instanceof AgentError))
|
|
1505
|
+
return false;
|
|
1506
|
+
if (typeof error.details?.status === 'number')
|
|
1507
|
+
return error.details.status === 401;
|
|
1508
|
+
const code = error.code.toLowerCase();
|
|
1509
|
+
return code === 'unauthorized';
|
|
1510
|
+
}
|
|
1511
|
+
function credentialRevision(error) {
|
|
1512
|
+
const revision = error.details?.credentialRevision;
|
|
1513
|
+
return typeof revision === 'number' && Number.isSafeInteger(revision) && revision >= 0
|
|
1514
|
+
? revision
|
|
1515
|
+
: undefined;
|
|
1516
|
+
}
|
|
1517
|
+
function boundedError(value, maximum) {
|
|
1518
|
+
const normalized = value?.trim();
|
|
1519
|
+
if (!normalized)
|
|
1520
|
+
return undefined;
|
|
1521
|
+
return [...normalized].slice(0, maximum).join('');
|
|
1522
|
+
}
|
|
1523
|
+
function upstreamHttpStatus(...values) {
|
|
1524
|
+
for (const value of values) {
|
|
1525
|
+
const candidate = typeof value === 'string' && /^\d{3}$/u.test(value) ? Number(value) : number(value);
|
|
1526
|
+
if (candidate !== undefined &&
|
|
1527
|
+
Number.isInteger(candidate) &&
|
|
1528
|
+
candidate >= 100 &&
|
|
1529
|
+
candidate <= 599)
|
|
1530
|
+
return candidate;
|
|
1531
|
+
}
|
|
1532
|
+
return undefined;
|
|
1533
|
+
}
|
|
1534
|
+
function optionalRolloutBudgetUnits(value) {
|
|
1535
|
+
if (value === undefined)
|
|
1536
|
+
return undefined;
|
|
1537
|
+
const units = number(value);
|
|
1538
|
+
if (units !== undefined && units >= 0)
|
|
1539
|
+
return units;
|
|
1540
|
+
throw new AgentError('MAR_AGENT_MODEL_PROTOCOL_ERROR', 'Provider rollout budget units are invalid.');
|
|
1541
|
+
}
|
|
1542
|
+
function isInvalidPreviousResponseError(error) {
|
|
1543
|
+
if (!(error instanceof AgentError))
|
|
1544
|
+
return false;
|
|
1545
|
+
const status = error.details?.status;
|
|
1546
|
+
if (typeof status === 'number' && ![400, 404, 410].includes(status))
|
|
1547
|
+
return false;
|
|
1548
|
+
return isInvalidPreviousResponseFields(error.code, error.message);
|
|
1549
|
+
}
|
|
1550
|
+
function isInvalidPreviousResponseFields(code, message) {
|
|
1551
|
+
const normalizedCode = code?.trim().toLowerCase();
|
|
1552
|
+
// OpenAI Responses and compatible providers use these missing-reference codes.
|
|
1553
|
+
if (normalizedCode === 'previous_response_not_found' ||
|
|
1554
|
+
normalizedCode === 'invalid_previous_response_id')
|
|
1555
|
+
return true;
|
|
1556
|
+
const normalizedMessage = message?.toLowerCase().replaceAll('`', '') ?? '';
|
|
1557
|
+
// xAI/Grok WSS uses api_error with a gRPC prefix; HTTP may return a plain
|
|
1558
|
+
// error string, normalized by the SSE boundary to MAR_AGENT_MODEL_PROTOCOL_ERROR.
|
|
1559
|
+
// Do not treat account/model lookup failures as a missing response reference.
|
|
1560
|
+
if ((normalizedCode === 'api_error' ||
|
|
1561
|
+
normalizedCode === 'mar_agent_model_protocol_error' ||
|
|
1562
|
+
normalizedCode === undefined) &&
|
|
1563
|
+
/^(?:(?:mar_agent_model_protocol_error|api_error): )?(?:grpc error: )?response with id=\S+ not found[.!]?$/u.test(normalizedMessage))
|
|
1564
|
+
return true;
|
|
1565
|
+
// Compatibility gateways use these unsupported-parameter discriminators.
|
|
1566
|
+
if ((normalizedCode === 'unsupported_parameter' ||
|
|
1567
|
+
normalizedCode === 'unsupported_previous_response_id') &&
|
|
1568
|
+
normalizedMessage.includes('previous_response_id'))
|
|
1569
|
+
return true;
|
|
1570
|
+
// OpenAI-style request errors and generic compatibility envelopes may only
|
|
1571
|
+
// name the rejected parameter. Explicit auth, permission, overload or other
|
|
1572
|
+
// error codes take precedence even when their message mentions continuation.
|
|
1573
|
+
if (normalizedCode !== undefined &&
|
|
1574
|
+
normalizedCode !== 'invalid_request_error' &&
|
|
1575
|
+
normalizedCode !== 'api_error' &&
|
|
1576
|
+
normalizedCode !== 'mar_agent_model_protocol_error')
|
|
1577
|
+
return false;
|
|
1578
|
+
return (normalizedMessage.includes('previous_response_id') &&
|
|
1579
|
+
(normalizedMessage.includes('invalid') ||
|
|
1580
|
+
normalizedMessage.includes('not found') ||
|
|
1581
|
+
normalizedMessage.includes('does not exist') ||
|
|
1582
|
+
normalizedMessage.includes('expired') ||
|
|
1583
|
+
normalizedMessage.includes('not supported') ||
|
|
1584
|
+
normalizedMessage.includes('unsupported') ||
|
|
1585
|
+
normalizedMessage.includes('unavailable')));
|
|
1586
|
+
}
|