stitchkit 0.133.2 → 0.134.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +115 -0
- package/README.md +14 -11
- package/dist/agent-executor/acp/v1-connect.d.ts +3 -0
- package/dist/agent-executor/acp/v1-connect.d.ts.map +1 -1
- package/dist/agent-executor/acp/v1-map.d.ts +13 -3
- package/dist/agent-executor/acp/v1-map.d.ts.map +1 -1
- package/dist/agent-executor/acp/v1-protocol.d.ts +59 -0
- package/dist/agent-executor/acp/v1-protocol.d.ts.map +1 -1
- package/dist/agent-executor/codex/connect.d.ts +2 -0
- package/dist/agent-executor/codex/connect.d.ts.map +1 -1
- package/dist/agent-executor/codex/driver.d.ts +3 -0
- package/dist/agent-executor/codex/driver.d.ts.map +1 -1
- package/dist/agent-executor/contract.d.ts +42 -0
- package/dist/agent-executor/contract.d.ts.map +1 -1
- package/dist/agent-executor/engine-process.d.ts +20 -0
- package/dist/agent-executor/engine-process.d.ts.map +1 -1
- package/dist/agent-executor/executor-contract.d.ts +42 -0
- package/dist/agent-executor/executor-contract.d.ts.map +1 -1
- package/dist/agent-executor/handoff-schemas.d.ts +30 -0
- package/dist/agent-executor/handoff-schemas.d.ts.map +1 -1
- package/dist/agent-executor/projection.d.ts.map +1 -1
- package/dist/agent-executor/session-primitives.d.ts +4 -1
- package/dist/agent-executor/session-primitives.d.ts.map +1 -1
- package/dist/agent-executor/vocabulary.d.ts +100 -0
- package/dist/agent-executor/vocabulary.d.ts.map +1 -1
- package/dist/agent-parts/model.d.ts +1559 -0
- package/dist/agent-parts/model.d.ts.map +1 -0
- package/dist/agent-parts/parts.d.ts +21 -0
- package/dist/agent-parts/parts.d.ts.map +1 -1
- package/dist/agent-runtime/ai-sdk-convert.d.ts +15 -0
- package/dist/agent-runtime/ai-sdk-convert.d.ts.map +1 -0
- package/dist/agent-runtime/ai-sdk.d.ts +31 -0
- package/dist/agent-runtime/ai-sdk.d.ts.map +1 -0
- package/dist/agent-runtime/anthropic-media.d.ts +11 -0
- package/dist/agent-runtime/anthropic-media.d.ts.map +1 -0
- package/dist/agent-runtime/anthropic-messages.d.ts +24 -0
- package/dist/agent-runtime/anthropic-messages.d.ts.map +1 -0
- package/dist/agent-runtime/anthropic-options.d.ts +79 -0
- package/dist/agent-runtime/anthropic-options.d.ts.map +1 -0
- package/dist/agent-runtime/anthropic-response.d.ts +19 -0
- package/dist/agent-runtime/anthropic-response.d.ts.map +1 -0
- package/dist/agent-runtime/anthropic-sse.d.ts +4 -0
- package/dist/agent-runtime/anthropic-sse.d.ts.map +1 -0
- package/dist/agent-runtime/anthropic.d.ts +27 -0
- package/dist/agent-runtime/anthropic.d.ts.map +1 -0
- package/dist/agent-runtime/branch-observations.d.ts +25 -25
- package/dist/agent-runtime/branch-provider.d.ts +86 -0
- package/dist/agent-runtime/branch-provider.d.ts.map +1 -1
- package/dist/agent-runtime/branch-replay.d.ts +30 -0
- package/dist/agent-runtime/branch-replay.d.ts.map +1 -1
- package/dist/agent-runtime/browser-control-contract.d.ts +204 -0
- package/dist/agent-runtime/browser-control-contract.d.ts.map +1 -1
- package/dist/agent-runtime/browser-controller-view.d.ts +44 -0
- package/dist/agent-runtime/browser-controller-view.d.ts.map +1 -0
- package/dist/agent-runtime/browser-controller.d.ts +12 -8
- package/dist/agent-runtime/browser-controller.d.ts.map +1 -1
- package/dist/agent-runtime/child-budget-policy.d.ts +2 -3
- package/dist/agent-runtime/child-budget-policy.d.ts.map +1 -1
- package/dist/agent-runtime/compaction.d.ts +27 -10
- package/dist/agent-runtime/compaction.d.ts.map +1 -1
- package/dist/agent-runtime/control-interaction-schema.d.ts +157 -0
- package/dist/agent-runtime/control-interaction-schema.d.ts.map +1 -0
- package/dist/agent-runtime/control-schema.d.ts +183 -27
- package/dist/agent-runtime/control-schema.d.ts.map +1 -1
- package/dist/agent-runtime/control-view.d.ts +64 -0
- package/dist/agent-runtime/control-view.d.ts.map +1 -0
- package/dist/agent-runtime/conversations.d.ts +10 -0
- package/dist/agent-runtime/conversations.d.ts.map +1 -1
- package/dist/agent-runtime/deferred-tool-selection.d.ts +2 -2
- package/dist/agent-runtime/deferred-tool-selection.d.ts.map +1 -1
- package/dist/agent-runtime/deferred-tool-types.d.ts +2 -2
- package/dist/agent-runtime/deferred-tool-types.d.ts.map +1 -1
- package/dist/agent-runtime/deferred-tools-internal.d.ts +8 -11
- package/dist/agent-runtime/deferred-tools-internal.d.ts.map +1 -1
- package/dist/agent-runtime/deferred-tools.d.ts.map +1 -1
- package/dist/agent-runtime/event-schema.d.ts +80 -0
- package/dist/agent-runtime/event-schema.d.ts.map +1 -1
- package/dist/agent-runtime/harness-contract.d.ts +2 -2
- package/dist/agent-runtime/harness-contract.d.ts.map +1 -1
- package/dist/agent-runtime/harness-control.d.ts.map +1 -1
- package/dist/agent-runtime/harness-realtime.d.ts.map +1 -1
- package/dist/agent-runtime/harness.d.ts +2 -2
- package/dist/agent-runtime/harness.d.ts.map +1 -1
- package/dist/agent-runtime/history.d.ts +25 -16
- package/dist/agent-runtime/history.d.ts.map +1 -1
- package/dist/agent-runtime/invocation-attempt.d.ts +7 -3
- package/dist/agent-runtime/invocation-attempt.d.ts.map +1 -1
- package/dist/agent-runtime/invocation-payload.d.ts +13 -7
- package/dist/agent-runtime/invocation-payload.d.ts.map +1 -1
- package/dist/agent-runtime/invocation-stream.d.ts +20 -0
- package/dist/agent-runtime/invocation-stream.d.ts.map +1 -0
- package/dist/agent-runtime/invocations.d.ts +2 -2
- package/dist/agent-runtime/invocations.d.ts.map +1 -1
- package/dist/agent-runtime/models-driver-step.d.ts +212 -0
- package/dist/agent-runtime/models-driver-step.d.ts.map +1 -0
- package/dist/agent-runtime/models-driver-telemetry.d.ts +42 -0
- package/dist/agent-runtime/models-driver-telemetry.d.ts.map +1 -0
- package/dist/agent-runtime/models-driver.d.ts +235 -0
- package/dist/agent-runtime/models-driver.d.ts.map +1 -0
- package/dist/agent-runtime/models.d.ts +3 -26
- package/dist/agent-runtime/models.d.ts.map +1 -1
- package/dist/agent-runtime/observability.d.ts +64 -0
- package/dist/agent-runtime/observability.d.ts.map +1 -1
- package/dist/agent-runtime/openai-messages.d.ts +11 -0
- package/dist/agent-runtime/openai-messages.d.ts.map +1 -0
- package/dist/agent-runtime/openai-options.d.ts +91 -0
- package/dist/agent-runtime/openai-options.d.ts.map +1 -0
- package/dist/agent-runtime/openai-response.d.ts +77 -0
- package/dist/agent-runtime/openai-response.d.ts.map +1 -0
- package/dist/agent-runtime/openai-sse-state.d.ts +54 -0
- package/dist/agent-runtime/openai-sse-state.d.ts.map +1 -0
- package/dist/agent-runtime/openai-sse.d.ts +4 -0
- package/dist/agent-runtime/openai-sse.d.ts.map +1 -0
- package/dist/agent-runtime/openai.d.ts +31 -0
- package/dist/agent-runtime/openai.d.ts.map +1 -0
- package/dist/agent-runtime/openrouter-admission.d.ts +3 -0
- package/dist/agent-runtime/openrouter-admission.d.ts.map +1 -1
- package/dist/agent-runtime/openrouter-benchmarks.d.ts +17 -0
- package/dist/agent-runtime/openrouter-benchmarks.d.ts.map +1 -0
- package/dist/agent-runtime/openrouter-catalog-cache.d.ts +70 -0
- package/dist/agent-runtime/openrouter-catalog-cache.d.ts.map +1 -0
- package/dist/agent-runtime/openrouter-catalog-query.d.ts +57 -0
- package/dist/agent-runtime/openrouter-catalog-query.d.ts.map +1 -0
- package/dist/agent-runtime/openrouter-catalog.d.ts +189 -14
- package/dist/agent-runtime/openrouter-catalog.d.ts.map +1 -1
- package/dist/agent-runtime/openrouter-models.d.ts +168 -31
- package/dist/agent-runtime/openrouter-models.d.ts.map +1 -1
- package/dist/agent-runtime/openrouter-options.d.ts +45 -15
- package/dist/agent-runtime/openrouter-options.d.ts.map +1 -1
- package/dist/agent-runtime/openrouter-response.d.ts +22 -0
- package/dist/agent-runtime/openrouter-response.d.ts.map +1 -0
- package/dist/agent-runtime/openrouter.d.ts +21 -24
- package/dist/agent-runtime/openrouter.d.ts.map +1 -1
- package/dist/agent-runtime/owned-provider-stream.d.ts +4 -4
- package/dist/agent-runtime/owned-provider-stream.d.ts.map +1 -1
- package/dist/agent-runtime/prompt.d.ts +3 -3
- package/dist/agent-runtime/prompt.d.ts.map +1 -1
- package/dist/agent-runtime/protocol.d.ts +28 -3
- package/dist/agent-runtime/protocol.d.ts.map +1 -1
- package/dist/agent-runtime/provider-cache.d.ts +30 -0
- package/dist/agent-runtime/provider-cache.d.ts.map +1 -0
- package/dist/agent-runtime/provider-content.d.ts +147 -0
- package/dist/agent-runtime/provider-content.d.ts.map +1 -1
- package/dist/agent-runtime/provider-failure.d.ts +51 -26
- package/dist/agent-runtime/provider-failure.d.ts.map +1 -1
- package/dist/agent-runtime/provider-history.d.ts +13 -0
- package/dist/agent-runtime/provider-history.d.ts.map +1 -0
- package/dist/agent-runtime/provider-http.d.ts +68 -0
- package/dist/agent-runtime/provider-http.d.ts.map +1 -0
- package/dist/agent-runtime/provider-journal.d.ts +149 -0
- package/dist/agent-runtime/provider-journal.d.ts.map +1 -1
- package/dist/agent-runtime/provider-json-schema-strip.d.ts +8 -0
- package/dist/agent-runtime/provider-json-schema-strip.d.ts.map +1 -0
- package/dist/agent-runtime/provider-json-schema.d.ts +28 -0
- package/dist/agent-runtime/provider-json-schema.d.ts.map +1 -0
- package/dist/agent-runtime/provider-media.d.ts +49 -0
- package/dist/agent-runtime/provider-media.d.ts.map +1 -0
- package/dist/agent-runtime/provider-openai-chat-helpers.d.ts +27 -0
- package/dist/agent-runtime/provider-openai-chat-helpers.d.ts.map +1 -0
- package/dist/agent-runtime/provider-openai-chat-sse.d.ts +25 -0
- package/dist/agent-runtime/provider-openai-chat-sse.d.ts.map +1 -0
- package/dist/agent-runtime/provider-openai-chat.d.ts +27 -0
- package/dist/agent-runtime/provider-openai-chat.d.ts.map +1 -0
- package/dist/agent-runtime/provider-origin.d.ts +3 -12
- package/dist/agent-runtime/provider-origin.d.ts.map +1 -1
- package/dist/agent-runtime/provider-sse.d.ts +30 -0
- package/dist/agent-runtime/provider-sse.d.ts.map +1 -0
- package/dist/agent-runtime/provider-stream-parts.d.ts +5 -0
- package/dist/agent-runtime/provider-stream-parts.d.ts.map +1 -0
- package/dist/agent-runtime/provider-stream-segments.d.ts +63 -0
- package/dist/agent-runtime/provider-stream-segments.d.ts.map +1 -0
- package/dist/agent-runtime/provider-usage.d.ts +24 -0
- package/dist/agent-runtime/provider-usage.d.ts.map +1 -0
- package/dist/agent-runtime/retry-policy.d.ts +19 -3
- package/dist/agent-runtime/retry-policy.d.ts.map +1 -1
- package/dist/agent-runtime/run-acquisition.d.ts +4 -4
- package/dist/agent-runtime/run-acquisition.d.ts.map +1 -1
- package/dist/agent-runtime/run-approval.d.ts +82 -0
- package/dist/agent-runtime/run-approval.d.ts.map +1 -0
- package/dist/agent-runtime/run-complete-retry.d.ts +24 -0
- package/dist/agent-runtime/run-complete-retry.d.ts.map +1 -0
- package/dist/agent-runtime/run-complete-stream.d.ts +9 -0
- package/dist/agent-runtime/run-complete-stream.d.ts.map +1 -0
- package/dist/agent-runtime/run-complete-tools.d.ts +21 -0
- package/dist/agent-runtime/run-complete-tools.d.ts.map +1 -0
- package/dist/agent-runtime/run-complete-types.d.ts +168 -0
- package/dist/agent-runtime/run-complete-types.d.ts.map +1 -0
- package/dist/agent-runtime/run-complete.d.ts +25 -0
- package/dist/agent-runtime/run-complete.d.ts.map +1 -0
- package/dist/agent-runtime/run-driver-compose.d.ts +21 -0
- package/dist/agent-runtime/run-driver-compose.d.ts.map +1 -0
- package/dist/agent-runtime/run-execution-state.d.ts +23 -10
- package/dist/agent-runtime/run-execution-state.d.ts.map +1 -1
- package/dist/agent-runtime/run-execution.d.ts +2 -2
- package/dist/agent-runtime/run-execution.d.ts.map +1 -1
- package/dist/agent-runtime/run-failure.d.ts +2 -2
- package/dist/agent-runtime/run-failure.d.ts.map +1 -1
- package/dist/agent-runtime/run-history-parts.d.ts +14 -0
- package/dist/agent-runtime/run-history-parts.d.ts.map +1 -0
- package/dist/agent-runtime/run-loop-events.d.ts +126 -0
- package/dist/agent-runtime/run-loop-events.d.ts.map +1 -0
- package/dist/agent-runtime/run-model-attempts.d.ts +2 -2
- package/dist/agent-runtime/run-model-attempts.d.ts.map +1 -1
- package/dist/agent-runtime/run-model-messages.d.ts +9 -0
- package/dist/agent-runtime/run-model-messages.d.ts.map +1 -0
- package/dist/agent-runtime/run-model-step.d.ts +43 -0
- package/dist/agent-runtime/run-model-step.d.ts.map +1 -0
- package/dist/agent-runtime/run-model-stream.d.ts +27 -0
- package/dist/agent-runtime/run-model-stream.d.ts.map +1 -0
- package/dist/agent-runtime/run-operation-lifecycle.d.ts +8 -4
- package/dist/agent-runtime/run-operation-lifecycle.d.ts.map +1 -1
- package/dist/agent-runtime/run-prompt-history.d.ts +8 -13
- package/dist/agent-runtime/run-prompt-history.d.ts.map +1 -1
- package/dist/agent-runtime/run-provider-ledger.d.ts +7 -4
- package/dist/agent-runtime/run-provider-ledger.d.ts.map +1 -1
- package/dist/agent-runtime/run-step-barriers.d.ts +58 -16
- package/dist/agent-runtime/run-step-barriers.d.ts.map +1 -1
- package/dist/agent-runtime/run-step-helpers.d.ts +49 -0
- package/dist/agent-runtime/run-step-helpers.d.ts.map +1 -0
- package/dist/agent-runtime/run-step-prepare.d.ts +34 -0
- package/dist/agent-runtime/run-step-prepare.d.ts.map +1 -0
- package/dist/agent-runtime/run-step-spend.d.ts +85 -10
- package/dist/agent-runtime/run-step-spend.d.ts.map +1 -1
- package/dist/agent-runtime/run-stream-parts.d.ts +13 -9
- package/dist/agent-runtime/run-stream-parts.d.ts.map +1 -1
- package/dist/agent-runtime/run-stream-recorded.d.ts +8 -0
- package/dist/agent-runtime/run-stream-recorded.d.ts.map +1 -0
- package/dist/agent-runtime/run-stream-step-text.d.ts +29 -0
- package/dist/agent-runtime/run-stream-step-text.d.ts.map +1 -0
- package/dist/agent-runtime/run-terminal.d.ts +3 -3
- package/dist/agent-runtime/run-terminal.d.ts.map +1 -1
- package/dist/agent-runtime/run-tool-approval-flow.d.ts +36 -0
- package/dist/agent-runtime/run-tool-approval-flow.d.ts.map +1 -0
- package/dist/agent-runtime/run-tool-approval-records.d.ts +24 -0
- package/dist/agent-runtime/run-tool-approval-records.d.ts.map +1 -0
- package/dist/agent-runtime/run-tool-call.d.ts +73 -0
- package/dist/agent-runtime/run-tool-call.d.ts.map +1 -0
- package/dist/agent-runtime/run-tool-errors.d.ts +29 -0
- package/dist/agent-runtime/run-tool-errors.d.ts.map +1 -0
- package/dist/agent-runtime/run-tool-executor.d.ts +34 -0
- package/dist/agent-runtime/run-tool-executor.d.ts.map +1 -0
- package/dist/agent-runtime/run-tool-progress.d.ts +2 -2
- package/dist/agent-runtime/run-tool-progress.d.ts.map +1 -1
- package/dist/agent-runtime/run-turn-preparation.d.ts +7 -6
- package/dist/agent-runtime/run-turn-preparation.d.ts.map +1 -1
- package/dist/agent-runtime/run-unacquired.d.ts +2 -2
- package/dist/agent-runtime/run-unacquired.d.ts.map +1 -1
- package/dist/agent-runtime/runtime-control.d.ts +4 -4
- package/dist/agent-runtime/runtime-control.d.ts.map +1 -1
- package/dist/agent-runtime/runtime-internals.d.ts +35 -44
- package/dist/agent-runtime/runtime-internals.d.ts.map +1 -1
- package/dist/agent-runtime/runtime-recovery.d.ts +3 -3
- package/dist/agent-runtime/runtime-recovery.d.ts.map +1 -1
- package/dist/agent-runtime/runtime-state.d.ts +3 -3
- package/dist/agent-runtime/runtime-state.d.ts.map +1 -1
- package/dist/agent-runtime/runtime-submit.d.ts +2 -2
- package/dist/agent-runtime/runtime-submit.d.ts.map +1 -1
- package/dist/agent-runtime/runtime.d.ts +51 -16
- package/dist/agent-runtime/runtime.d.ts.map +1 -1
- package/dist/agent-runtime/sandbox-process-linux.d.ts +8 -0
- package/dist/agent-runtime/sandbox-process-linux.d.ts.map +1 -0
- package/dist/agent-runtime/sandbox-process-macos.d.ts +21 -0
- package/dist/agent-runtime/sandbox-process-macos.d.ts.map +1 -0
- package/dist/agent-runtime/sandbox-process-spec.d.ts +114 -0
- package/dist/agent-runtime/sandbox-process-spec.d.ts.map +1 -0
- package/dist/agent-runtime/sandbox-process-wrapper.d.ts +19 -0
- package/dist/agent-runtime/sandbox-process-wrapper.d.ts.map +1 -0
- package/dist/agent-runtime/schemas-message.d.ts +10 -0
- package/dist/agent-runtime/schemas-message.d.ts.map +1 -1
- package/dist/agent-runtime/schemas-provider.d.ts +2 -0
- package/dist/agent-runtime/schemas-provider.d.ts.map +1 -1
- package/dist/agent-runtime/schemas-run.d.ts +10 -0
- package/dist/agent-runtime/schemas-run.d.ts.map +1 -1
- package/dist/agent-runtime/schemas-usage-state.d.ts +6 -0
- package/dist/agent-runtime/schemas-usage-state.d.ts.map +1 -0
- package/dist/agent-runtime/schemas-usage.d.ts +8 -0
- package/dist/agent-runtime/schemas-usage.d.ts.map +1 -1
- package/dist/agent-runtime/sql-rows.d.ts +20 -10
- package/dist/agent-runtime/sql-rows.d.ts.map +1 -1
- package/dist/agent-runtime/store-conformance-lifecycle.d.ts +40 -20
- package/dist/agent-runtime/store-conformance-lifecycle.d.ts.map +1 -1
- package/dist/agent-runtime/store-conformance-support.d.ts +20 -10
- package/dist/agent-runtime/store-conformance-support.d.ts.map +1 -1
- package/dist/agent-runtime/store-driver-contract.d.ts +70 -0
- package/dist/agent-runtime/store-driver-contract.d.ts.map +1 -1
- package/dist/agent-runtime/store-events.d.ts +120 -0
- package/dist/agent-runtime/store-events.d.ts.map +1 -1
- package/dist/agent-runtime/store.d.ts +140 -0
- package/dist/agent-runtime/store.d.ts.map +1 -1
- package/dist/agent-runtime/terminal-commit.d.ts +10 -0
- package/dist/agent-runtime/terminal-commit.d.ts.map +1 -1
- package/dist/agent-runtime/testing.d.ts +20 -0
- package/dist/agent-runtime/testing.d.ts.map +1 -1
- package/dist/browser/client-call-arguments.d.ts.map +1 -1
- package/dist/browser/client-executors.d.ts.map +1 -1
- package/dist/browser/client-url.d.ts.map +1 -1
- package/dist/browser/client.d.ts +6 -0
- package/dist/browser/client.d.ts.map +1 -1
- package/dist/browser/device-presence-client.d.ts +42 -0
- package/dist/browser/device-presence-client.d.ts.map +1 -0
- package/dist/browser/device-presence-ops.d.ts +47 -0
- package/dist/browser/device-presence-ops.d.ts.map +1 -0
- package/dist/browser/device-presence-relay-adapter.d.ts +37 -0
- package/dist/browser/device-presence-relay-adapter.d.ts.map +1 -0
- package/dist/browser/http-fetch.d.ts +7 -2
- package/dist/browser/http-fetch.d.ts.map +1 -1
- package/dist/browser/http-helpers.d.ts +15 -0
- package/dist/browser/http-helpers.d.ts.map +1 -0
- package/dist/browser/http.d.ts +8 -0
- package/dist/browser/http.d.ts.map +1 -1
- package/dist/contract/client-types.d.ts +8 -0
- package/dist/contract/client-types.d.ts.map +1 -1
- package/dist/contract/runtime-context.d.ts +13 -1
- package/dist/contract/runtime-context.d.ts.map +1 -1
- package/dist/device-relay/agent-handlers.d.ts +45 -0
- package/dist/device-relay/agent-handlers.d.ts.map +1 -0
- package/dist/device-relay/agent.d.ts +80 -0
- package/dist/device-relay/agent.d.ts.map +1 -0
- package/dist/device-relay/broker.d.ts +49 -0
- package/dist/device-relay/broker.d.ts.map +1 -0
- package/dist/device-relay/contract.d.ts +614 -0
- package/dist/device-relay/contract.d.ts.map +1 -0
- package/dist/device-relay/encoding.d.ts +30 -0
- package/dist/device-relay/encoding.d.ts.map +1 -0
- package/dist/device-relay/hub-connection.d.ts +43 -0
- package/dist/device-relay/hub-connection.d.ts.map +1 -0
- package/dist/device-relay/hub-dispatch.d.ts +19 -0
- package/dist/device-relay/hub-dispatch.d.ts.map +1 -0
- package/dist/device-relay/hub-handshake.d.ts +20 -0
- package/dist/device-relay/hub-handshake.d.ts.map +1 -0
- package/dist/device-relay/hub-shared-streams.d.ts +24 -0
- package/dist/device-relay/hub-shared-streams.d.ts.map +1 -0
- package/dist/device-relay/hub-streams.d.ts +57 -0
- package/dist/device-relay/hub-streams.d.ts.map +1 -0
- package/dist/device-relay/hub.d.ts +117 -0
- package/dist/device-relay/hub.d.ts.map +1 -0
- package/dist/entrypoints/agent-executor/acp.d.ts +1 -1
- package/dist/entrypoints/agent-executor/acp.d.ts.map +1 -1
- package/dist/entrypoints/agent-executor/acp.js +183 -29
- package/dist/entrypoints/agent-executor/claude-sdk.js +15 -15
- package/dist/entrypoints/agent-executor/claude.js +13 -15
- package/dist/entrypoints/agent-executor/codex.js +88 -48
- package/dist/entrypoints/agent-executor/testing/claude-sdk.js +5 -5
- package/dist/entrypoints/agent-executor/testing.js +31 -30
- package/dist/entrypoints/agent-executor.d.ts +2 -2
- package/dist/entrypoints/agent-executor.d.ts.map +1 -1
- package/dist/entrypoints/agent-executor.js +23 -17
- package/dist/entrypoints/agent-runtime/ai-sdk.d.ts +9 -0
- package/dist/entrypoints/agent-runtime/ai-sdk.d.ts.map +1 -0
- package/dist/entrypoints/agent-runtime/ai-sdk.js +821 -0
- package/dist/entrypoints/agent-runtime/anthropic.d.ts +4 -0
- package/dist/entrypoints/agent-runtime/anthropic.d.ts.map +1 -0
- package/dist/entrypoints/agent-runtime/anthropic.js +1260 -0
- package/dist/entrypoints/agent-runtime/browser.d.ts +4 -1
- package/dist/entrypoints/agent-runtime/browser.d.ts.map +1 -1
- package/dist/entrypoints/agent-runtime/browser.js +449 -50
- package/dist/entrypoints/agent-runtime/coding-tools.js +3 -3
- package/dist/entrypoints/agent-runtime/harness.js +24 -19
- package/dist/entrypoints/agent-runtime/openai.d.ts +4 -0
- package/dist/entrypoints/agent-runtime/openai.d.ts.map +1 -0
- package/dist/entrypoints/agent-runtime/openai.js +1246 -0
- package/dist/entrypoints/agent-runtime/openrouter.d.ts +5 -2
- package/dist/entrypoints/agent-runtime/openrouter.d.ts.map +1 -1
- package/dist/entrypoints/agent-runtime/openrouter.js +2355 -371
- package/dist/entrypoints/agent-runtime/postgres.js +12 -10
- package/dist/entrypoints/agent-runtime/realtime.js +11 -10
- package/dist/entrypoints/agent-runtime/sandbox.d.ts +3 -0
- package/dist/entrypoints/agent-runtime/sandbox.d.ts.map +1 -1
- package/dist/entrypoints/agent-runtime/sandbox.js +530 -7
- package/dist/entrypoints/agent-runtime/sqlite/bun.js +13 -11
- package/dist/entrypoints/agent-runtime/sqlite/node.js +13 -11
- package/dist/entrypoints/agent-runtime/testing.d.ts +1 -1
- package/dist/entrypoints/agent-runtime/testing.d.ts.map +1 -1
- package/dist/entrypoints/agent-runtime/testing.js +56 -15
- package/dist/entrypoints/agent-runtime.d.ts +14 -6
- package/dist/entrypoints/agent-runtime.d.ts.map +1 -1
- package/dist/entrypoints/agent-runtime.js +1412 -115
- package/dist/entrypoints/application/diagnostic-journal.js +6 -6
- package/dist/entrypoints/application/directory-inbox.js +6 -6
- package/dist/entrypoints/application/opentelemetry.js +3 -3
- package/dist/entrypoints/application.js +17 -17
- package/dist/entrypoints/cli/publish.js +8 -8
- package/dist/entrypoints/cli.js +14 -15
- package/dist/entrypoints/contract.d.ts +1 -0
- package/dist/entrypoints/contract.d.ts.map +1 -1
- package/dist/entrypoints/contract.js +13 -9
- package/dist/entrypoints/device-relay.d.ts +10 -0
- package/dist/entrypoints/device-relay.d.ts.map +1 -0
- package/dist/entrypoints/device-relay.js +2517 -0
- package/dist/entrypoints/files/packaging.js +3 -3
- package/dist/entrypoints/files.js +11 -11
- package/dist/entrypoints/geo.js +5 -5
- package/dist/entrypoints/google.js +1 -1
- package/dist/entrypoints/handler.js +6 -6
- package/dist/entrypoints/http.js +9 -9
- package/dist/entrypoints/index.js +21 -17
- package/dist/entrypoints/live.js +4 -4
- package/dist/entrypoints/node.js +13 -13
- package/dist/entrypoints/observability.js +9 -9
- package/dist/entrypoints/process/bootstrap.js +1 -1
- package/dist/entrypoints/process.js +5 -5
- package/dist/entrypoints/react.js +4 -4
- package/dist/entrypoints/realtime.d.ts +3 -0
- package/dist/entrypoints/realtime.d.ts.map +1 -1
- package/dist/entrypoints/realtime.js +413 -4
- package/dist/entrypoints/release.js +1 -1
- package/dist/entrypoints/remote.js +10 -10
- package/dist/entrypoints/server.d.ts +1 -0
- package/dist/entrypoints/server.d.ts.map +1 -1
- package/dist/entrypoints/server.js +546 -40
- package/dist/entrypoints/service-host/macos.js +10 -10
- package/dist/entrypoints/service-host/work.js +7 -7
- package/dist/entrypoints/service-host.js +15 -15
- package/dist/entrypoints/spend.js +3 -3
- package/dist/entrypoints/sync.js +4 -4
- package/dist/entrypoints/telegram/screens.js +6 -6
- package/dist/entrypoints/telegram.js +20 -20
- package/dist/entrypoints/testing.js +14 -14
- package/dist/entrypoints/tools/agent.d.ts +2 -1
- package/dist/entrypoints/tools/agent.d.ts.map +1 -1
- package/dist/entrypoints/tools/agent.js +8 -7
- package/dist/entrypoints/tools/connections.js +5 -5
- package/dist/entrypoints/tools/invoker.js +3 -4
- package/dist/entrypoints/tools/mcp.js +8 -9
- package/dist/entrypoints/tools/toolkit.js +32 -13
- package/dist/entrypoints/tools.d.ts +3 -1
- package/dist/entrypoints/tools.d.ts.map +1 -1
- package/dist/entrypoints/tools.js +37 -35
- package/dist/entrypoints/tracking/contract.js +1 -1
- package/dist/entrypoints/tracking/performance.js +4 -4
- package/dist/entrypoints/tracking.js +3 -3
- package/dist/{index-09f7w2qq.js → index-02kdtrcd.js} +1 -1
- package/dist/{index-y53cqwke.js → index-033tyx59.js} +1 -1
- package/dist/{index-9ft4n9j1.js → index-05b65gj4.js} +2 -2
- package/dist/{index-d04w81fz.js → index-0sxj3y45.js} +1 -1
- package/dist/{index-dm6nxkn1.js → index-18pf56dq.js} +4 -6
- package/dist/index-1gmgwfn3.js +439 -0
- package/dist/index-23wn8sgf.js +47 -0
- package/dist/{index-tqjamnax.js → index-27srek2g.js} +15 -13
- package/dist/{index-2nwvn7m2.js → index-29v1p1f4.js} +2 -2
- package/dist/index-36dmtzjr.js +194 -0
- package/dist/{index-ycr4pk0z.js → index-42n9jycn.js} +3 -3
- package/dist/{index-ke02kxsr.js → index-43k180c2.js} +1 -1
- package/dist/{index-pbekeqzm.js → index-5c4m8zdf.js} +2 -2
- package/dist/{index-qggqqja2.js → index-5dxgx3yj.js} +1 -1
- package/dist/{index-x5ayy5bn.js → index-5gdexvke.js} +3 -1
- package/dist/{index-t1a2jrg7.js → index-5xsxvk2x.js} +1 -1
- package/dist/{index-mbd92r1w.js → index-654rhe5d.js} +1 -1
- package/dist/{index-2dzc7mv9.js → index-67pnt4bh.js} +1 -1
- package/dist/index-6kpwkpgx.js +415 -0
- package/dist/{index-s43tr4my.js → index-6kwdfb9m.js} +18 -3
- package/dist/{index-1qwd54n3.js → index-6nzzj7qr.js} +1 -1
- package/dist/{index-rkjfzs0a.js → index-6p5pshcd.js} +2 -2
- package/dist/{index-118aebbg.js → index-6t4c98gr.js} +1 -1
- package/dist/{index-pws5c3x6.js → index-6xw1wf4m.js} +51 -32
- package/dist/{index-kwsr7j5k.js → index-6yb4stq3.js} +1 -1
- package/dist/{index-8jf4bt7v.js → index-76yhxa41.js} +8 -8
- package/dist/{index-7416mhyh.js → index-79vqbjcz.js} +1 -1
- package/dist/{index-13wmzpa6.js → index-7an2fs5h.js} +1 -1
- package/dist/index-7qza24g1.js +14 -0
- package/dist/{index-gtaaxe9d.js → index-8cek82ar.js} +1 -1
- package/dist/index-9f60gnfn.js +393 -0
- package/dist/{index-stpy56r3.js → index-aacwkve4.js} +209 -10
- package/dist/{index-rpyn7geq.js → index-ac3rfec5.js} +2 -2
- package/dist/{index-d2drhb9c.js → index-b0mh2t9x.js} +2 -2
- package/dist/index-b23s6e2c.js +158 -0
- package/dist/{index-an7vcmwf.js → index-bvpevwt6.js} +5 -5
- package/dist/{index-msxf2r8x.js → index-by8w2461.js} +1 -1
- package/dist/{index-8t9dr41h.js → index-c0yym4a0.js} +4 -4
- package/dist/{index-wddcfbng.js → index-c7fj4yw9.js} +1 -1
- package/dist/{index-tf70nm22.js → index-cb4nnvfr.js} +8 -6
- package/dist/{index-205456mf.js → index-d3s070eh.js} +2 -2
- package/dist/{index-apf5g7y9.js → index-dz6tstcp.js} +2 -2
- package/dist/{index-agy5fqgf.js → index-e1y74yra.js} +15 -8
- package/dist/index-edtg4tak.js +55 -0
- package/dist/{index-pq6g1k9t.js → index-ek9w11r0.js} +2 -2
- package/dist/{index-x46y9z26.js → index-epyj7ptp.js} +7 -5
- package/dist/{index-6kmjcm81.js → index-er483f3w.js} +93 -4
- package/dist/{index-827bgv33.js → index-f16b7rze.js} +1 -1
- package/dist/{index-gbhj370g.js → index-fec05c9k.js} +21 -21
- package/dist/{index-9wdrwx3b.js → index-fjac2w5e.js} +20 -22
- package/dist/{index-6918kxbf.js → index-fthw8dwa.js} +1 -0
- package/dist/{index-2mz8hj58.js → index-g04hszcm.js} +1 -1
- package/dist/{index-rw8ze745.js → index-ga6avd48.js} +1 -1
- package/dist/{index-paff5y0r.js → index-grhhsrd5.js} +9 -7
- package/dist/{index-mxt0wfjy.js → index-h20qcyh5.js} +5 -2
- package/dist/index-j3r7r1mh.js +150 -0
- package/dist/{index-vgytj9wp.js → index-jmaw365j.js} +1 -1
- package/dist/{index-cjytermm.js → index-jzdbkanz.js} +3 -3
- package/dist/index-k137ezb6.js +79 -0
- package/dist/{index-tazm4dxf.js → index-kdw0n92g.js} +1 -1
- package/dist/{index-wh70tmpd.js → index-ke6jtxkd.js} +1 -1
- package/dist/index-kgj8j7bv.js +83 -0
- package/dist/{index-9kpxz49v.js → index-kgkevngs.js} +1 -1
- package/dist/{index-hab790s6.js → index-kp1j7cc8.js} +4 -4
- package/dist/{index-jcge9tx3.js → index-krekhmp7.js} +142 -123
- package/dist/{index-3y2tzhr8.js → index-ky79wwgs.js} +4 -4
- package/dist/{index-39rmc00v.js → index-mfe9a266.js} +1 -1
- package/dist/{index-qpzyahe3.js → index-mgr0mdqp.js} +5 -2
- package/dist/{index-n20tybgk.js → index-mh3jdb25.js} +2 -2
- package/dist/{index-g5fgeb6v.js → index-mkmep637.js} +1 -1
- package/dist/{index-02r5sq9p.js → index-mrng24wq.js} +19 -21
- package/dist/index-mx2gya05.js +465 -0
- package/dist/{index-8yt3rwe7.js → index-n2t87ggz.js} +5 -5
- package/dist/index-nfeafbzh.js +7379 -0
- package/dist/{index-j0ef04jw.js → index-nfm15ej7.js} +4 -4
- package/dist/index-pd9e8rz4.js +193 -0
- package/dist/{index-zwgwyvdt.js → index-pdcbjd6a.js} +1 -1
- package/dist/{index-q427q23r.js → index-pjjh91vw.js} +2 -2
- package/dist/{index-gnjgkecx.js → index-q46b16xz.js} +1 -1
- package/dist/{index-9xr44d2g.js → index-qv5v6w9c.js} +330 -106
- package/dist/{index-tmv3ez1h.js → index-qymek778.js} +69 -12
- package/dist/{index-yrr3n391.js → index-ry8m2mhg.js} +2 -2
- package/dist/{index-93n6jwy0.js → index-s0n0nnz5.js} +3 -3
- package/dist/{index-1p8q0yrt.js → index-t5zp1jjj.js} +5 -7
- package/dist/{index-2rf33y2h.js → index-tav72wz5.js} +4 -2
- package/dist/{index-jpecjm30.js → index-tgehj4bp.js} +1 -1
- package/dist/{index-ma26mfnt.js → index-v5hmyef8.js} +1 -1
- package/dist/index-vebzvse6.js +230 -0
- package/dist/index-vv0hxrmy.js +295 -0
- package/dist/index-vxm0ya3f.js +707 -0
- package/dist/{index-t3qcac2a.js → index-w3s79kcb.js} +1 -1
- package/dist/{index-r97ppg0g.js → index-x1r7kxzd.js} +1 -1
- package/dist/{index-kynwa08q.js → index-xrsryvd2.js} +2 -2
- package/dist/{index-3mcbr8sx.js → index-xtxxt87z.js} +1 -1
- package/dist/index-zk839vzf.js +231 -0
- package/dist/{index-9c76vfxf.js → index-zyyerdny.js} +4 -4
- package/dist/internal/base64.d.ts +9 -0
- package/dist/internal/base64.d.ts.map +1 -0
- package/dist/internal/deadline-budget.d.ts +57 -0
- package/dist/internal/deadline-budget.d.ts.map +1 -0
- package/dist/internal/deadline.d.ts.map +1 -1
- package/dist/internal/timers.d.ts +2 -0
- package/dist/internal/timers.d.ts.map +1 -1
- package/dist/realtime/device-presence-contract.d.ts +194 -0
- package/dist/realtime/device-presence-contract.d.ts.map +1 -0
- package/dist/server/context-contribution.d.ts.map +1 -1
- package/dist/server/contract-method.d.ts.map +1 -1
- package/dist/server/device-presence-handlers.d.ts +54 -0
- package/dist/server/device-presence-handlers.d.ts.map +1 -0
- package/dist/server/device-presence-registry.d.ts +35 -0
- package/dist/server/device-presence-registry.d.ts.map +1 -0
- package/dist/server/device-presence.d.ts +45 -0
- package/dist/server/device-presence.d.ts.map +1 -0
- package/dist/server/endpoint-dispatch.d.ts.map +1 -1
- package/dist/server/middleware/cors.d.ts +3 -1
- package/dist/server/middleware/cors.d.ts.map +1 -1
- package/dist/server/types.d.ts +4 -11
- package/dist/server/types.d.ts.map +1 -1
- package/dist/service-host/systemd-render-shared.d.ts.map +1 -1
- package/dist/tools/agent-messages.d.ts +6 -0
- package/dist/tools/agent-messages.d.ts.map +1 -0
- package/dist/tools/agent-tool-error.d.ts +2 -2
- package/dist/tools/agent-tools.d.ts +153 -0
- package/dist/tools/agent-tools.d.ts.map +1 -0
- package/dist/tools/agent.d.ts +6 -56
- package/dist/tools/agent.d.ts.map +1 -1
- package/dist/tools/runtime-tool.d.ts +3 -4
- package/dist/tools/runtime-tool.d.ts.map +1 -1
- package/dist/tools/toolkit.d.ts +3 -1
- package/dist/tools/toolkit.d.ts.map +1 -1
- package/dist/tools/transfer/define-view-file-tool.d.ts.map +1 -1
- package/llms/agent-executor-acp.txt +2 -2
- package/llms/agent-executor-codex.txt +1 -1
- package/llms/agent-executor.txt +7 -3
- package/llms/agent-runtime-ai-sdk.txt +26 -0
- package/llms/agent-runtime-anthropic.txt +33 -0
- package/llms/agent-runtime-browser.txt +22 -8
- package/llms/agent-runtime-harness.txt +2 -2
- package/llms/agent-runtime-openai.txt +31 -0
- package/llms/agent-runtime-openrouter.txt +17 -7
- package/llms/agent-runtime-sandbox.txt +53 -1
- package/llms/agent-runtime-testing.txt +4 -2
- package/llms/agent-runtime.2.txt +158 -271
- package/llms/agent-runtime.3.txt +289 -243
- package/llms/agent-runtime.4.txt +273 -216
- package/llms/agent-runtime.5.txt +209 -2
- package/llms/agent-runtime.txt +238 -181
- package/llms/contract.txt +2 -1
- package/llms/device-relay.txt +34 -0
- package/llms/realtime.txt +20 -0
- package/llms/server.2.txt +125 -1
- package/llms/server.3.txt +111 -122
- package/llms/server.4.txt +1 -1
- package/llms/server.5.txt +3 -1
- package/llms/server.txt +31 -1
- package/llms/spend.txt +11 -6
- package/llms/stitchkit.2.txt +21 -43
- package/llms/stitchkit.3.txt +43 -0
- package/llms/stitchkit.txt +93 -84
- package/llms/tools-agent.txt +7 -5
- package/llms/tools-toolkit.txt +1 -1
- package/llms/tools.4.txt +7 -2
- package/llms/{upgrading-0.128.0-0.133.0.txt → upgrading-0.128.0-0.134.0.txt} +173 -3
- package/llms/upgrading.txt +1 -1
- package/llms-full.txt +981 -210
- package/llms.txt +21 -17
- package/package.json +18 -7
- package/dist/index-0p0s5hrj.js +0 -249
- package/dist/index-1dn2wjq6.js +0 -135
- package/dist/index-1psbyyhj.js +0 -21
- package/dist/index-34k82crg.js +0 -3961
- package/dist/index-5n3a8hd8.js +0 -102
- package/dist/index-97rp94ff.js +0 -8
- package/dist/index-bn2baakz.js +0 -336
- package/dist/index-eh71zr32.js +0 -14
- package/dist/index-emgrmavr.js +0 -1193
- package/dist/index-fzd477xk.js +0 -135
- package/dist/index-vgzhw2zk.js +0 -304
- package/dist/internal/ai-sdk-typed.d.ts +0 -14
- package/dist/internal/ai-sdk-typed.d.ts.map +0 -1
- package/dist/tools/runtime-call-context.d.ts +0 -15
- package/dist/tools/runtime-call-context.d.ts.map +0 -1
- package/dist/{index-vmbxj75t.js → index-jwtmw39p.js} +0 -0
- package/dist/{index-877e9y8r.js → index-sk03xe0n.js} +3 -3
- package/dist/{index-21cfxc4z.js → index-vh4291qh.js} +3 -3
|
@@ -0,0 +1,1246 @@
|
|
|
1
|
+
import"../../index-vebzvse6.js";
|
|
2
|
+
import {
|
|
3
|
+
redactProviderFailureText,
|
|
4
|
+
joinProviderUrl,
|
|
5
|
+
assertNoExtraBodyClash,
|
|
6
|
+
resolveProviderApiKey,
|
|
7
|
+
postProviderStream,
|
|
8
|
+
nonEmptyString2,
|
|
9
|
+
reportedInt,
|
|
10
|
+
buildProviderUsage,
|
|
11
|
+
normalizeProviderHistory,
|
|
12
|
+
parseSseStream,
|
|
13
|
+
parseSseJson
|
|
14
|
+
} from "../../index-vxm0ya3f.js";
|
|
15
|
+
import {
|
|
16
|
+
normalizeOpenAiJsonSchema
|
|
17
|
+
} from "../../index-1gmgwfn3.js";
|
|
18
|
+
import {
|
|
19
|
+
sanitizeSurrogates,
|
|
20
|
+
deterministicJson,
|
|
21
|
+
resolveFilePayload,
|
|
22
|
+
normalizeMime,
|
|
23
|
+
fallbackFilenameForMediaType,
|
|
24
|
+
AgentProviderError2,
|
|
25
|
+
AgentProviderStreamCutError2
|
|
26
|
+
} from "../../index-6kpwkpgx.js";
|
|
27
|
+
import {
|
|
28
|
+
isRecord
|
|
29
|
+
} from "../../index-ps3djm8d.js";
|
|
30
|
+
|
|
31
|
+
// src/agent-runtime/openai-options.ts
|
|
32
|
+
import { z } from "zod";
|
|
33
|
+
var ENCRYPTED_REASONING_INCLUDE = "reasoning.encrypted_content";
|
|
34
|
+
var MAX_PROMPT_CACHE_KEY_CHARS = 64;
|
|
35
|
+
var OpenAiReasoningPolicySchema = z.strictObject({
|
|
36
|
+
effort: z.enum(["none", "minimal", "low", "medium", "high", "xhigh"]),
|
|
37
|
+
summary: z.enum(["auto", "concise", "detailed"]).optional()
|
|
38
|
+
});
|
|
39
|
+
var OpenAiRequestPolicySchema = z.strictObject({
|
|
40
|
+
store: z.boolean().optional(),
|
|
41
|
+
reasoning: OpenAiReasoningPolicySchema.optional(),
|
|
42
|
+
serviceTier: z.enum(["auto", "default", "flex", "priority"]).optional(),
|
|
43
|
+
truncation: z.enum(["auto", "disabled"]).optional(),
|
|
44
|
+
promptCacheKey: z.string().min(1).max(64).optional(),
|
|
45
|
+
promptCacheRetention: z.enum(["in-memory", "24h"]).optional(),
|
|
46
|
+
parallelToolCalls: z.boolean().optional(),
|
|
47
|
+
strictTools: z.boolean().optional(),
|
|
48
|
+
include: z.array(z.string().min(1)).optional(),
|
|
49
|
+
user: z.string().min(1).max(256).optional()
|
|
50
|
+
});
|
|
51
|
+
function resolveOpenAiPolicy(raw = {}) {
|
|
52
|
+
const policy = OpenAiRequestPolicySchema.parse(raw);
|
|
53
|
+
return {
|
|
54
|
+
store: policy.store ?? false,
|
|
55
|
+
...policy.reasoning !== undefined && { reasoning: policy.reasoning },
|
|
56
|
+
...policy.serviceTier !== undefined && { serviceTier: policy.serviceTier },
|
|
57
|
+
...policy.truncation !== undefined && { truncation: policy.truncation },
|
|
58
|
+
...policy.promptCacheKey !== undefined && { promptCacheKey: policy.promptCacheKey },
|
|
59
|
+
...policy.promptCacheRetention !== undefined && {
|
|
60
|
+
promptCacheRetention: policy.promptCacheRetention
|
|
61
|
+
},
|
|
62
|
+
...policy.parallelToolCalls !== undefined && {
|
|
63
|
+
parallelToolCalls: policy.parallelToolCalls
|
|
64
|
+
},
|
|
65
|
+
strictTools: policy.strictTools ?? false,
|
|
66
|
+
include: policy.include !== undefined ? [...policy.include] : [],
|
|
67
|
+
...policy.user !== undefined && { user: policy.user }
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
function mergeOpenAiStepPolicy(base, providerOptions) {
|
|
71
|
+
if (!providerOptions || !isRecord(providerOptions.openai))
|
|
72
|
+
return base;
|
|
73
|
+
if (Object.keys(providerOptions.openai).length === 0)
|
|
74
|
+
return base;
|
|
75
|
+
const override = OpenAiRequestPolicySchema.parse(providerOptions.openai);
|
|
76
|
+
return resolveOpenAiPolicy({
|
|
77
|
+
store: override.store ?? base.store,
|
|
78
|
+
...override.reasoning !== undefined ? { reasoning: override.reasoning } : base.reasoning !== undefined && { reasoning: base.reasoning },
|
|
79
|
+
...override.serviceTier !== undefined ? { serviceTier: override.serviceTier } : base.serviceTier !== undefined && { serviceTier: base.serviceTier },
|
|
80
|
+
...override.truncation !== undefined ? { truncation: override.truncation } : base.truncation !== undefined && { truncation: base.truncation },
|
|
81
|
+
...override.promptCacheKey !== undefined ? { promptCacheKey: override.promptCacheKey } : base.promptCacheKey !== undefined && { promptCacheKey: base.promptCacheKey },
|
|
82
|
+
...override.promptCacheRetention !== undefined ? { promptCacheRetention: override.promptCacheRetention } : base.promptCacheRetention !== undefined && {
|
|
83
|
+
promptCacheRetention: base.promptCacheRetention
|
|
84
|
+
},
|
|
85
|
+
...override.parallelToolCalls !== undefined ? { parallelToolCalls: override.parallelToolCalls } : base.parallelToolCalls !== undefined && { parallelToolCalls: base.parallelToolCalls },
|
|
86
|
+
strictTools: override.strictTools ?? base.strictTools,
|
|
87
|
+
...override.include !== undefined ? { include: override.include } : base.include.length > 0 && { include: [...base.include] },
|
|
88
|
+
...override.user !== undefined ? { user: override.user } : base.user !== undefined && { user: base.user }
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
function assertOpenAiSupportedSettings(settings, policy) {
|
|
92
|
+
const unsupported = [
|
|
93
|
+
["topK", settings.topK],
|
|
94
|
+
["presencePenalty", settings.presencePenalty],
|
|
95
|
+
["frequencyPenalty", settings.frequencyPenalty],
|
|
96
|
+
["seed", settings.seed],
|
|
97
|
+
["stopSequences", settings.stopSequences]
|
|
98
|
+
];
|
|
99
|
+
for (const [name, value] of unsupported) {
|
|
100
|
+
if (value !== undefined) {
|
|
101
|
+
throw new TypeError(`openai: settings.${name} is not supported by the OpenAI Responses API`);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
if (policy.reasoning !== undefined && policy.reasoning.effort !== "none") {
|
|
105
|
+
if (settings.temperature !== undefined) {
|
|
106
|
+
throw new TypeError(`openai: settings.temperature cannot be set when reasoning.effort is "${policy.reasoning.effort}"`);
|
|
107
|
+
}
|
|
108
|
+
if (settings.topP !== undefined) {
|
|
109
|
+
throw new TypeError(`openai: settings.topP cannot be set when reasoning.effort is "${policy.reasoning.effort}"`);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
function deriveOpenAiPromptCacheKey(conversationId) {
|
|
114
|
+
const clean = sanitizeSurrogates(conversationId);
|
|
115
|
+
if (clean.length === 0)
|
|
116
|
+
return;
|
|
117
|
+
if (clean.length <= MAX_PROMPT_CACHE_KEY_CHARS)
|
|
118
|
+
return clean;
|
|
119
|
+
let h1 = 2166136261;
|
|
120
|
+
let h2 = 2654435769;
|
|
121
|
+
let h3 = 2246822507;
|
|
122
|
+
let h4 = 3266489909;
|
|
123
|
+
for (let i = 0;i < clean.length; i += 1) {
|
|
124
|
+
const code = clean.charCodeAt(i);
|
|
125
|
+
h1 = Math.imul(h1 ^ code, 16777619);
|
|
126
|
+
h2 = Math.imul(h2 ^ code + i, 2246822507);
|
|
127
|
+
h3 = Math.imul(h3 ^ code * 31 + i, 3266489909);
|
|
128
|
+
h4 = Math.imul(h4 ^ (code << 5) - code + i, 668265263);
|
|
129
|
+
}
|
|
130
|
+
const hex = (n) => (n >>> 0).toString(16).padStart(8, "0");
|
|
131
|
+
return `ck_${hex(h1)}${hex(h2)}${hex(h3)}${hex(h4)}`;
|
|
132
|
+
}
|
|
133
|
+
function modelMayEmitEncryptedReasoning(modelId) {
|
|
134
|
+
const lower = modelId.toLowerCase();
|
|
135
|
+
if (lower.includes("-chat"))
|
|
136
|
+
return false;
|
|
137
|
+
return /^o[134](?:$|[-._])/u.test(lower) || /^gpt-5(?:$|[-._])/u.test(lower) || lower.includes("codex");
|
|
138
|
+
}
|
|
139
|
+
function resolveOpenAiIncludeList(policy, modelId, sawOpenAiReasoning) {
|
|
140
|
+
const seen = new Set;
|
|
141
|
+
const include = [];
|
|
142
|
+
for (const entry of policy.include) {
|
|
143
|
+
if (!seen.has(entry)) {
|
|
144
|
+
seen.add(entry);
|
|
145
|
+
include.push(entry);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
const reasoningDisabled = policy.reasoning?.effort === "none";
|
|
149
|
+
const wantsEncryptedReasoning = policy.store === false && !reasoningDisabled && (policy.reasoning !== undefined || sawOpenAiReasoning || modelMayEmitEncryptedReasoning(modelId));
|
|
150
|
+
if (wantsEncryptedReasoning && !seen.has(ENCRYPTED_REASONING_INCLUDE)) {
|
|
151
|
+
include.push(ENCRYPTED_REASONING_INCLUDE);
|
|
152
|
+
}
|
|
153
|
+
return include;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// src/agent-runtime/openai-messages.ts
|
|
157
|
+
var RESPONSE_FORMAT_NAME_PATTERN = /^[a-zA-Z0-9_-]{1,64}$/u;
|
|
158
|
+
function readOpenAiNamespace(options) {
|
|
159
|
+
return options && isRecord(options.openai) ? options.openai : undefined;
|
|
160
|
+
}
|
|
161
|
+
function readImageDetail(providerOptions) {
|
|
162
|
+
const oa = readOpenAiNamespace(providerOptions);
|
|
163
|
+
const raw = oa?.imageDetail ?? oa?.detail;
|
|
164
|
+
return raw === "low" || raw === "high" || raw === "auto" ? raw : undefined;
|
|
165
|
+
}
|
|
166
|
+
function encodeMediaPart(data, mediaType, filename, providerOptions) {
|
|
167
|
+
const payload = resolveFilePayload(data, mediaType);
|
|
168
|
+
if (payload.kind === "text") {
|
|
169
|
+
return { type: "input_text", text: payload.text };
|
|
170
|
+
}
|
|
171
|
+
const resolvedMediaType = normalizeMime(payload.kind === "base64" ? payload.mediaType : mediaType);
|
|
172
|
+
if (resolvedMediaType.startsWith("image/")) {
|
|
173
|
+
const detail = readImageDetail(providerOptions);
|
|
174
|
+
if (payload.kind === "reference") {
|
|
175
|
+
const fileId = nonEmptyString2(payload.reference.openai);
|
|
176
|
+
if (fileId === undefined) {
|
|
177
|
+
throw new TypeError('openai: image file reference does not provide an "openai" file_id');
|
|
178
|
+
}
|
|
179
|
+
return {
|
|
180
|
+
type: "input_image",
|
|
181
|
+
file_id: fileId,
|
|
182
|
+
...detail !== undefined && { detail }
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
const imageUrl = payload.kind === "url" ? payload.url : `data:${resolvedMediaType};base64,${payload.base64}`;
|
|
186
|
+
return {
|
|
187
|
+
type: "input_image",
|
|
188
|
+
image_url: imageUrl,
|
|
189
|
+
...detail !== undefined && { detail }
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
if (payload.kind === "reference") {
|
|
193
|
+
const fileId = nonEmptyString2(payload.reference.openai);
|
|
194
|
+
if (fileId === undefined) {
|
|
195
|
+
throw new TypeError('openai: file reference does not provide an "openai" file_id');
|
|
196
|
+
}
|
|
197
|
+
return { type: "input_file", file_id: fileId };
|
|
198
|
+
}
|
|
199
|
+
if (payload.kind === "url") {
|
|
200
|
+
return { type: "input_file", file_url: payload.url };
|
|
201
|
+
}
|
|
202
|
+
const resolvedName = sanitizeSurrogates(filename ?? fallbackFilenameForMediaType(resolvedMediaType));
|
|
203
|
+
return {
|
|
204
|
+
type: "input_file",
|
|
205
|
+
filename: resolvedName,
|
|
206
|
+
file_data: `data:${resolvedMediaType};base64,${payload.base64}`
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
function encodeToolOutput(output) {
|
|
210
|
+
switch (output.type) {
|
|
211
|
+
case "text":
|
|
212
|
+
case "error-text":
|
|
213
|
+
return output.value;
|
|
214
|
+
case "json":
|
|
215
|
+
case "error-json":
|
|
216
|
+
return deterministicJson(output.value);
|
|
217
|
+
case "execution-denied":
|
|
218
|
+
return output.reason ?? "Tool execution denied";
|
|
219
|
+
case "content":
|
|
220
|
+
return output.value.map((part) => {
|
|
221
|
+
if (part.type === "text")
|
|
222
|
+
return { type: "input_text", text: part.text };
|
|
223
|
+
if (part.type === "file") {
|
|
224
|
+
return encodeMediaPart(part.data, part.mediaType, part.filename, part.providerOptions);
|
|
225
|
+
}
|
|
226
|
+
return { type: "input_text", text: deterministicJson(part) };
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
function encodeOpenAiReasoningItem(text, providerOptions, store) {
|
|
231
|
+
const oa = readOpenAiNamespace(providerOptions);
|
|
232
|
+
if (!oa)
|
|
233
|
+
return;
|
|
234
|
+
const id = nonEmptyString2(oa.id);
|
|
235
|
+
const encryptedContent = nonEmptyString2(oa.encrypted_content);
|
|
236
|
+
if (!store && encryptedContent === undefined) {
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
const hasExplicitSummary = Array.isArray(oa.summary);
|
|
240
|
+
const summary = [];
|
|
241
|
+
if (Array.isArray(oa.summary)) {
|
|
242
|
+
for (const entry of oa.summary) {
|
|
243
|
+
if (isRecord(entry) && typeof entry.text === "string") {
|
|
244
|
+
summary.push({ type: "summary_text", text: sanitizeSurrogates(entry.text) });
|
|
245
|
+
} else if (typeof entry === "string") {
|
|
246
|
+
summary.push({ type: "summary_text", text: sanitizeSurrogates(entry) });
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
} else if (text.length > 0) {
|
|
250
|
+
summary.push({ type: "summary_text", text });
|
|
251
|
+
}
|
|
252
|
+
if (encryptedContent === undefined && (id === undefined || !hasExplicitSummary && summary.length === 0)) {
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
return {
|
|
256
|
+
type: "reasoning",
|
|
257
|
+
...id !== undefined && { id },
|
|
258
|
+
summary,
|
|
259
|
+
...encryptedContent !== undefined && { encrypted_content: encryptedContent }
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
function encodeAssistantMessage(message, store) {
|
|
263
|
+
const items = [];
|
|
264
|
+
let sawOpenAiReasoning = false;
|
|
265
|
+
const messagePhase = nonEmptyString2(readOpenAiNamespace(message.providerOptions)?.phase);
|
|
266
|
+
if (typeof message.content === "string") {
|
|
267
|
+
if (message.content.length > 0) {
|
|
268
|
+
items.push({
|
|
269
|
+
role: "assistant",
|
|
270
|
+
content: [{ type: "output_text", text: message.content }],
|
|
271
|
+
...messagePhase !== undefined && { phase: messagePhase }
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
return { items, sawOpenAiReasoning };
|
|
275
|
+
}
|
|
276
|
+
let pendingText = [];
|
|
277
|
+
let pendingPhase;
|
|
278
|
+
const flushText = () => {
|
|
279
|
+
if (pendingText.length === 0)
|
|
280
|
+
return;
|
|
281
|
+
items.push({
|
|
282
|
+
role: "assistant",
|
|
283
|
+
content: pendingText,
|
|
284
|
+
...pendingPhase !== undefined && { phase: pendingPhase }
|
|
285
|
+
});
|
|
286
|
+
pendingText = [];
|
|
287
|
+
pendingPhase = undefined;
|
|
288
|
+
};
|
|
289
|
+
for (const part of message.content) {
|
|
290
|
+
if (part.type === "text") {
|
|
291
|
+
if (part.text.length === 0)
|
|
292
|
+
continue;
|
|
293
|
+
const partPhase = nonEmptyString2(readOpenAiNamespace(part.providerOptions)?.phase) ?? messagePhase;
|
|
294
|
+
if (pendingText.length > 0 && pendingPhase !== partPhase)
|
|
295
|
+
flushText();
|
|
296
|
+
pendingPhase = partPhase;
|
|
297
|
+
pendingText.push({ type: "output_text", text: part.text });
|
|
298
|
+
continue;
|
|
299
|
+
}
|
|
300
|
+
if (part.type === "reasoning") {
|
|
301
|
+
flushText();
|
|
302
|
+
const encoded = encodeOpenAiReasoningItem(part.text, part.providerOptions, store);
|
|
303
|
+
if (encoded !== undefined) {
|
|
304
|
+
sawOpenAiReasoning = true;
|
|
305
|
+
items.push(encoded);
|
|
306
|
+
}
|
|
307
|
+
continue;
|
|
308
|
+
}
|
|
309
|
+
if (part.type === "tool-call" && part.providerExecuted !== true) {
|
|
310
|
+
flushText();
|
|
311
|
+
const itemId = nonEmptyString2(readOpenAiNamespace(part.providerOptions)?.itemId);
|
|
312
|
+
items.push({
|
|
313
|
+
type: "function_call",
|
|
314
|
+
call_id: part.toolCallId,
|
|
315
|
+
name: part.toolName,
|
|
316
|
+
arguments: deterministicJson(part.input),
|
|
317
|
+
...store === true && itemId !== undefined && itemId.startsWith("fc_") && { id: itemId }
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
flushText();
|
|
322
|
+
return { items, sawOpenAiReasoning };
|
|
323
|
+
}
|
|
324
|
+
function encodeMessages(request, messages, store) {
|
|
325
|
+
const input = [];
|
|
326
|
+
let sawOpenAiReasoning = false;
|
|
327
|
+
for (const sys of request.instructions) {
|
|
328
|
+
const clean = sanitizeSurrogates(sys.content);
|
|
329
|
+
if (clean.length > 0)
|
|
330
|
+
input.push({ role: "developer", content: clean });
|
|
331
|
+
}
|
|
332
|
+
for (const message of messages) {
|
|
333
|
+
if (message.role === "system") {
|
|
334
|
+
input.push({ role: "developer", content: message.content });
|
|
335
|
+
continue;
|
|
336
|
+
}
|
|
337
|
+
if (message.role === "user") {
|
|
338
|
+
const content = typeof message.content === "string" ? [{ type: "input_text", text: message.content }] : message.content.map((part) => part.type === "text" ? { type: "input_text", text: part.text } : encodeMediaPart(part.data, part.mediaType, part.filename, part.providerOptions));
|
|
339
|
+
input.push({ role: "user", content });
|
|
340
|
+
continue;
|
|
341
|
+
}
|
|
342
|
+
if (message.role === "assistant") {
|
|
343
|
+
const encoded = encodeAssistantMessage(message, store);
|
|
344
|
+
if (encoded.sawOpenAiReasoning)
|
|
345
|
+
sawOpenAiReasoning = true;
|
|
346
|
+
input.push(...encoded.items);
|
|
347
|
+
continue;
|
|
348
|
+
}
|
|
349
|
+
for (const part of message.content) {
|
|
350
|
+
if (part.type !== "tool-result")
|
|
351
|
+
continue;
|
|
352
|
+
input.push({
|
|
353
|
+
type: "function_call_output",
|
|
354
|
+
call_id: part.toolCallId,
|
|
355
|
+
output: encodeToolOutput(part.output)
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
return { input, sawOpenAiReasoning };
|
|
360
|
+
}
|
|
361
|
+
function encodeTools(tools, defaultStrict) {
|
|
362
|
+
if (tools.length === 0)
|
|
363
|
+
return;
|
|
364
|
+
return tools.map((spec) => {
|
|
365
|
+
if (spec.type === "provider") {
|
|
366
|
+
throw new TypeError(`openai: unsupported provider tool "${spec.id}" (type: "provider")`);
|
|
367
|
+
}
|
|
368
|
+
const strict = spec.strict ?? defaultStrict;
|
|
369
|
+
return {
|
|
370
|
+
type: "function",
|
|
371
|
+
name: spec.name,
|
|
372
|
+
...spec.description !== undefined && {
|
|
373
|
+
description: sanitizeSurrogates(spec.description)
|
|
374
|
+
},
|
|
375
|
+
parameters: normalizeOpenAiJsonSchema(spec.inputSchema, strict),
|
|
376
|
+
strict
|
|
377
|
+
};
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
function encodeToolChoice(toolChoice) {
|
|
381
|
+
if (toolChoice === undefined)
|
|
382
|
+
return;
|
|
383
|
+
return typeof toolChoice === "string" ? toolChoice : { type: "function", name: toolChoice.toolName };
|
|
384
|
+
}
|
|
385
|
+
function encodeResponseFormat(format) {
|
|
386
|
+
if (format === undefined)
|
|
387
|
+
return;
|
|
388
|
+
if (format.type === "text")
|
|
389
|
+
return { format: { type: "text" } };
|
|
390
|
+
if (format.type === "json-object")
|
|
391
|
+
return { format: { type: "json_object" } };
|
|
392
|
+
if (!RESPONSE_FORMAT_NAME_PATTERN.test(format.name)) {
|
|
393
|
+
throw new TypeError(`openai: responseFormat.name "${format.name}" must match /^[a-zA-Z0-9_-]{1,64}$/`);
|
|
394
|
+
}
|
|
395
|
+
const strict = format.strict ?? true;
|
|
396
|
+
return {
|
|
397
|
+
format: {
|
|
398
|
+
type: "json_schema",
|
|
399
|
+
name: format.name,
|
|
400
|
+
...format.description !== undefined && {
|
|
401
|
+
description: sanitizeSurrogates(format.description)
|
|
402
|
+
},
|
|
403
|
+
schema: normalizeOpenAiJsonSchema(format.schema, strict),
|
|
404
|
+
strict
|
|
405
|
+
}
|
|
406
|
+
};
|
|
407
|
+
}
|
|
408
|
+
function buildOpenAiRequestBody(input) {
|
|
409
|
+
const { modelId, request, cachePerConversation, extraBody } = input;
|
|
410
|
+
const policy = mergeOpenAiStepPolicy(input.policy, request.providerOptions);
|
|
411
|
+
assertOpenAiSupportedSettings(request.settings, policy);
|
|
412
|
+
const normalizedMessages = normalizeProviderHistory(request.messages);
|
|
413
|
+
const { input: wireInput, sawOpenAiReasoning } = encodeMessages(request, normalizedMessages, policy.store);
|
|
414
|
+
const tools = encodeTools(request.tools, policy.strictTools);
|
|
415
|
+
const toolChoice = tools !== undefined ? encodeToolChoice(request.toolChoice) : undefined;
|
|
416
|
+
const text = encodeResponseFormat(request.responseFormat);
|
|
417
|
+
const include = resolveOpenAiIncludeList(policy, modelId, sawOpenAiReasoning);
|
|
418
|
+
const promptCacheKey = policy.promptCacheKey ?? (cachePerConversation ? deriveOpenAiPromptCacheKey(request.correlation.conversationId) : undefined);
|
|
419
|
+
return {
|
|
420
|
+
...extraBody ?? {},
|
|
421
|
+
model: modelId,
|
|
422
|
+
stream: true,
|
|
423
|
+
store: policy.store,
|
|
424
|
+
input: wireInput,
|
|
425
|
+
...tools !== undefined && { tools },
|
|
426
|
+
...toolChoice !== undefined && { tool_choice: toolChoice },
|
|
427
|
+
...text !== undefined && { text },
|
|
428
|
+
...policy.reasoning !== undefined && {
|
|
429
|
+
reasoning: {
|
|
430
|
+
effort: policy.reasoning.effort,
|
|
431
|
+
...policy.reasoning.summary !== undefined && { summary: policy.reasoning.summary }
|
|
432
|
+
}
|
|
433
|
+
},
|
|
434
|
+
...include.length > 0 && { include },
|
|
435
|
+
...policy.serviceTier !== undefined && { service_tier: policy.serviceTier },
|
|
436
|
+
...policy.truncation !== undefined && { truncation: policy.truncation },
|
|
437
|
+
...promptCacheKey !== undefined && { prompt_cache_key: promptCacheKey },
|
|
438
|
+
...policy.promptCacheRetention !== undefined && {
|
|
439
|
+
prompt_cache_retention: policy.promptCacheRetention
|
|
440
|
+
},
|
|
441
|
+
...policy.parallelToolCalls !== undefined && {
|
|
442
|
+
parallel_tool_calls: policy.parallelToolCalls
|
|
443
|
+
},
|
|
444
|
+
...policy.user !== undefined && { user: policy.user },
|
|
445
|
+
...request.settings.maxOutputTokens !== undefined && {
|
|
446
|
+
max_output_tokens: request.settings.maxOutputTokens
|
|
447
|
+
},
|
|
448
|
+
...request.settings.temperature !== undefined && {
|
|
449
|
+
temperature: request.settings.temperature
|
|
450
|
+
},
|
|
451
|
+
...request.settings.topP !== undefined && { top_p: request.settings.topP }
|
|
452
|
+
};
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
// src/agent-runtime/openai-response.ts
|
|
456
|
+
var OPENAI_CODE_TO_REASON = new Map([
|
|
457
|
+
["insufficient_quota", "insufficient-credits"],
|
|
458
|
+
["billing_hard_limit_reached", "insufficient-credits"],
|
|
459
|
+
["context_length_exceeded", "context-overflow"],
|
|
460
|
+
["model_context_window_exceeded", "context-overflow"],
|
|
461
|
+
["string_above_max_length", "context-overflow"],
|
|
462
|
+
["rate_limit_exceeded", "rate-limited"],
|
|
463
|
+
["rate_limit_error", "rate-limited"],
|
|
464
|
+
["server_error", "server-error"],
|
|
465
|
+
["internal_server_error", "server-error"],
|
|
466
|
+
["api_error", "server-error"],
|
|
467
|
+
["service_unavailable", "server-error"],
|
|
468
|
+
["model_not_found", "model-unavailable"]
|
|
469
|
+
]);
|
|
470
|
+
function normalizeOpenAiUsage(rawUsage) {
|
|
471
|
+
const raw = isRecord(rawUsage) ? rawUsage : {};
|
|
472
|
+
const inputDetails = isRecord(raw.input_tokens_details) ? raw.input_tokens_details : {};
|
|
473
|
+
const outputDetails = isRecord(raw.output_tokens_details) ? raw.output_tokens_details : {};
|
|
474
|
+
return buildProviderUsage({
|
|
475
|
+
inputTokens: reportedInt(raw.input_tokens),
|
|
476
|
+
outputTokens: reportedInt(raw.output_tokens),
|
|
477
|
+
cacheReadTokens: reportedInt(inputDetails.cached_tokens),
|
|
478
|
+
reasoningTokens: reportedInt(outputDetails.reasoning_tokens),
|
|
479
|
+
cacheWriteTokens: { provenance: "unavailable" },
|
|
480
|
+
cost: { provenance: "unavailable" }
|
|
481
|
+
});
|
|
482
|
+
}
|
|
483
|
+
function unwrapOpenAiErrorObject(payload) {
|
|
484
|
+
if (!isRecord(payload))
|
|
485
|
+
return { object: undefined, unwrapped: false };
|
|
486
|
+
if (isRecord(payload.error))
|
|
487
|
+
return { object: payload.error, unwrapped: true };
|
|
488
|
+
if (isRecord(payload.response) && isRecord(payload.response.error)) {
|
|
489
|
+
return { object: payload.response.error, unwrapped: true };
|
|
490
|
+
}
|
|
491
|
+
return { object: payload, unwrapped: false };
|
|
492
|
+
}
|
|
493
|
+
function parseOpenAiError(payload, rawText, status) {
|
|
494
|
+
const { object: err, unwrapped } = unwrapOpenAiErrorObject(payload);
|
|
495
|
+
const code = typeof err?.code === "string" && err.code.length > 0 ? err.code : typeof err?.code === "number" ? err.code : undefined;
|
|
496
|
+
const rawType = nonEmptyString2(err?.type);
|
|
497
|
+
const errorType = rawType !== undefined && (unwrapped || rawType !== "error") ? rawType : undefined;
|
|
498
|
+
const param = nonEmptyString2(err?.param);
|
|
499
|
+
const byCode = typeof code === "string" ? OPENAI_CODE_TO_REASON.get(code.toLowerCase()) : undefined;
|
|
500
|
+
const byType = errorType !== undefined ? OPENAI_CODE_TO_REASON.get(errorType.toLowerCase()) : undefined;
|
|
501
|
+
const failureReason = byCode ?? byType;
|
|
502
|
+
const message = nonEmptyString2(err?.message) ?? (rawText.trim().length > 0 ? rawText.trim() : `openai HTTP ${status}`);
|
|
503
|
+
return {
|
|
504
|
+
message,
|
|
505
|
+
...code !== undefined && { code },
|
|
506
|
+
...errorType !== undefined && { errorType },
|
|
507
|
+
...failureReason !== undefined && { failureReason },
|
|
508
|
+
...param !== undefined && { metadata: { param } }
|
|
509
|
+
};
|
|
510
|
+
}
|
|
511
|
+
function createOpenAiStreamError(payload, fallbackMessage, statusCode) {
|
|
512
|
+
const details = parseOpenAiError(payload, fallbackMessage, statusCode ?? 500);
|
|
513
|
+
const message = redactProviderFailureText(details.message || fallbackMessage);
|
|
514
|
+
return new AgentProviderError2(message, {
|
|
515
|
+
provider: "openai",
|
|
516
|
+
...statusCode !== undefined && { statusCode },
|
|
517
|
+
...details.code !== undefined && { code: details.code },
|
|
518
|
+
...details.errorType !== undefined && { errorType: details.errorType },
|
|
519
|
+
...details.failureReason !== undefined && { failureReason: details.failureReason },
|
|
520
|
+
...details.metadata !== undefined && { metadata: details.metadata }
|
|
521
|
+
});
|
|
522
|
+
}
|
|
523
|
+
function mapOpenAiIncompleteReason(reason) {
|
|
524
|
+
switch (reason) {
|
|
525
|
+
case "max_output_tokens":
|
|
526
|
+
return "length";
|
|
527
|
+
case "content_filter":
|
|
528
|
+
return "content-filter";
|
|
529
|
+
default:
|
|
530
|
+
return "other";
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
function resolveOpenAiFinishReason(input) {
|
|
534
|
+
if (input.status === "incomplete") {
|
|
535
|
+
const details = isRecord(input.incompleteDetails) ? input.incompleteDetails : undefined;
|
|
536
|
+
const reason = nonEmptyString2(details?.reason);
|
|
537
|
+
return {
|
|
538
|
+
finishReason: mapOpenAiIncompleteReason(reason),
|
|
539
|
+
rawFinishReason: reason ?? "incomplete"
|
|
540
|
+
};
|
|
541
|
+
}
|
|
542
|
+
const finishReason = input.hasToolCalls ? "tool-calls" : input.sawRefusal && !input.sawText ? "content-filter" : "stop";
|
|
543
|
+
return { finishReason, rawFinishReason: input.status ?? "completed" };
|
|
544
|
+
}
|
|
545
|
+
function extractOpenAiUrlCitation(annotation) {
|
|
546
|
+
if (!isRecord(annotation) || annotation.type !== "url_citation")
|
|
547
|
+
return;
|
|
548
|
+
const url = nonEmptyString2(annotation.url);
|
|
549
|
+
if (url === undefined || !URL.canParse(url))
|
|
550
|
+
return;
|
|
551
|
+
const title = nonEmptyString2(annotation.title);
|
|
552
|
+
return {
|
|
553
|
+
type: "source",
|
|
554
|
+
sourceId: crypto.randomUUID(),
|
|
555
|
+
url,
|
|
556
|
+
...title !== undefined && { title }
|
|
557
|
+
};
|
|
558
|
+
}
|
|
559
|
+
function extractOpenAiReasoningSummary(raw) {
|
|
560
|
+
if (!Array.isArray(raw))
|
|
561
|
+
return [];
|
|
562
|
+
const entries = [];
|
|
563
|
+
for (const item of raw) {
|
|
564
|
+
if (isRecord(item) && typeof item.text === "string") {
|
|
565
|
+
entries.push({ type: "summary_text", text: item.text });
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
return entries;
|
|
569
|
+
}
|
|
570
|
+
function buildOpenAiReasoningEnvelope(item) {
|
|
571
|
+
const summaryJson = item.summary.map((entry) => ({
|
|
572
|
+
type: entry.type,
|
|
573
|
+
text: entry.text
|
|
574
|
+
}));
|
|
575
|
+
const data = {
|
|
576
|
+
type: "reasoning",
|
|
577
|
+
...item.id !== undefined && item.id.length > 0 && { id: item.id },
|
|
578
|
+
summary: summaryJson,
|
|
579
|
+
...item.encryptedContent !== undefined && item.encryptedContent.length > 0 && {
|
|
580
|
+
encrypted_content: item.encryptedContent
|
|
581
|
+
}
|
|
582
|
+
};
|
|
583
|
+
return { schemaVersion: 1, provider: "openai", data };
|
|
584
|
+
}
|
|
585
|
+
function buildOpenAiPhaseEnvelope(phase) {
|
|
586
|
+
return { schemaVersion: 1, provider: "openai", data: { phase } };
|
|
587
|
+
}
|
|
588
|
+
function buildOpenAiToolCallEnvelope(itemId) {
|
|
589
|
+
return { schemaVersion: 1, provider: "openai", data: { itemId } };
|
|
590
|
+
}
|
|
591
|
+
function scanOpenAiMessageContent(content) {
|
|
592
|
+
let text = "";
|
|
593
|
+
let sawRefusal = false;
|
|
594
|
+
const sources = [];
|
|
595
|
+
for (const part of Array.isArray(content) ? content : []) {
|
|
596
|
+
if (!isRecord(part))
|
|
597
|
+
continue;
|
|
598
|
+
if (part.type === "output_text" && typeof part.text === "string") {
|
|
599
|
+
text += part.text;
|
|
600
|
+
if (Array.isArray(part.annotations)) {
|
|
601
|
+
for (const annotation of part.annotations) {
|
|
602
|
+
const source = extractOpenAiUrlCitation(annotation);
|
|
603
|
+
if (source !== undefined)
|
|
604
|
+
sources.push(source);
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
} else if (part.type === "refusal") {
|
|
608
|
+
sawRefusal = true;
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
return { text, sawRefusal, sources };
|
|
612
|
+
}
|
|
613
|
+
function buildOpenAiFinishPart(input) {
|
|
614
|
+
const { finishReason, rawFinishReason } = resolveOpenAiFinishReason(input);
|
|
615
|
+
return {
|
|
616
|
+
type: "finish",
|
|
617
|
+
finishReason,
|
|
618
|
+
rawFinishReason,
|
|
619
|
+
usage: normalizeOpenAiUsage(input.usage),
|
|
620
|
+
...(input.responseId !== undefined || input.responseModelId !== undefined || input.responseTimestamp !== undefined) && {
|
|
621
|
+
response: {
|
|
622
|
+
...input.responseId !== undefined && { id: input.responseId },
|
|
623
|
+
...input.responseModelId !== undefined && { modelId: input.responseModelId },
|
|
624
|
+
...input.responseTimestamp !== undefined && { timestamp: input.responseTimestamp }
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
};
|
|
628
|
+
}
|
|
629
|
+
function* decodeMessageOutputItem(item, index, flags) {
|
|
630
|
+
const textId = nonEmptyString2(item.id) ?? `text-${index}`;
|
|
631
|
+
const phase = nonEmptyString2(item.phase);
|
|
632
|
+
const scanned = scanOpenAiMessageContent(item.content);
|
|
633
|
+
if (scanned.sawRefusal)
|
|
634
|
+
flags.sawRefusal = true;
|
|
635
|
+
if (scanned.text.length > 0) {
|
|
636
|
+
flags.sawText = true;
|
|
637
|
+
yield { type: "text-start", id: textId };
|
|
638
|
+
yield { type: "text-delta", id: textId, delta: scanned.text };
|
|
639
|
+
yield {
|
|
640
|
+
type: "text-end",
|
|
641
|
+
id: textId,
|
|
642
|
+
...phase !== undefined && { provider: buildOpenAiPhaseEnvelope(phase) }
|
|
643
|
+
};
|
|
644
|
+
}
|
|
645
|
+
for (const source of scanned.sources)
|
|
646
|
+
yield { type: "source", source };
|
|
647
|
+
}
|
|
648
|
+
async function* decodeOpenAiJsonResponse(body) {
|
|
649
|
+
if (!isRecord(body)) {
|
|
650
|
+
throw new AgentProviderError2("openai returned an invalid JSON response", {
|
|
651
|
+
provider: "openai",
|
|
652
|
+
statusCode: 200
|
|
653
|
+
});
|
|
654
|
+
}
|
|
655
|
+
if ("error" in body && body.error !== undefined && body.error !== null || body.status === "failed") {
|
|
656
|
+
throw createOpenAiStreamError(body, "OpenAI response failed", 200);
|
|
657
|
+
}
|
|
658
|
+
const id = nonEmptyString2(body.id);
|
|
659
|
+
const modelId = nonEmptyString2(body.model);
|
|
660
|
+
const timestamp = typeof body.created_at === "number" && Number.isFinite(body.created_at) && body.created_at >= 0 ? new Date(body.created_at * 1000) : undefined;
|
|
661
|
+
if (id !== undefined || modelId !== undefined || timestamp !== undefined) {
|
|
662
|
+
yield {
|
|
663
|
+
type: "response-metadata",
|
|
664
|
+
...id !== undefined && { id },
|
|
665
|
+
...modelId !== undefined && { modelId },
|
|
666
|
+
...timestamp !== undefined && { timestamp }
|
|
667
|
+
};
|
|
668
|
+
}
|
|
669
|
+
const output = Array.isArray(body.output) ? body.output : [];
|
|
670
|
+
const flags = { sawText: false, sawRefusal: false };
|
|
671
|
+
let hasToolCalls = false;
|
|
672
|
+
for (let index = 0;index < output.length; index += 1) {
|
|
673
|
+
const item = output[index];
|
|
674
|
+
if (!isRecord(item))
|
|
675
|
+
continue;
|
|
676
|
+
if (item.type === "reasoning") {
|
|
677
|
+
const itemId = nonEmptyString2(item.id);
|
|
678
|
+
const reasoningId = itemId ?? `reasoning-${index}`;
|
|
679
|
+
const summary = extractOpenAiReasoningSummary(item.summary);
|
|
680
|
+
const encryptedContent = nonEmptyString2(item.encrypted_content);
|
|
681
|
+
const summaryText = summary.map((entry) => entry.text).join(`
|
|
682
|
+
`);
|
|
683
|
+
yield { type: "reasoning-start", id: reasoningId };
|
|
684
|
+
if (summaryText.length > 0) {
|
|
685
|
+
yield { type: "reasoning-delta", id: reasoningId, delta: summaryText };
|
|
686
|
+
}
|
|
687
|
+
yield {
|
|
688
|
+
type: "reasoning-end",
|
|
689
|
+
id: reasoningId,
|
|
690
|
+
provider: buildOpenAiReasoningEnvelope({
|
|
691
|
+
...itemId !== undefined && { id: itemId },
|
|
692
|
+
summary,
|
|
693
|
+
...encryptedContent !== undefined && { encryptedContent }
|
|
694
|
+
})
|
|
695
|
+
};
|
|
696
|
+
continue;
|
|
697
|
+
}
|
|
698
|
+
if (item.type === "message") {
|
|
699
|
+
yield* decodeMessageOutputItem(item, index, flags);
|
|
700
|
+
continue;
|
|
701
|
+
}
|
|
702
|
+
if (item.type === "function_call") {
|
|
703
|
+
hasToolCalls = true;
|
|
704
|
+
const toolCallId = nonEmptyString2(item.call_id) ?? crypto.randomUUID();
|
|
705
|
+
const itemId = nonEmptyString2(item.id);
|
|
706
|
+
const toolName = typeof item.name === "string" ? item.name : "";
|
|
707
|
+
const args = typeof item.arguments === "string" && item.arguments.trim().length > 0 ? item.arguments : "{}";
|
|
708
|
+
yield { type: "tool-input-start", toolCallId, toolName };
|
|
709
|
+
yield { type: "tool-input-delta", toolCallId, delta: args };
|
|
710
|
+
yield { type: "tool-input-end", toolCallId };
|
|
711
|
+
yield {
|
|
712
|
+
type: "tool-call",
|
|
713
|
+
toolCallId,
|
|
714
|
+
toolName,
|
|
715
|
+
input: args,
|
|
716
|
+
...itemId !== undefined && { provider: buildOpenAiToolCallEnvelope(itemId) }
|
|
717
|
+
};
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
yield buildOpenAiFinishPart({
|
|
721
|
+
status: nonEmptyString2(body.status),
|
|
722
|
+
incompleteDetails: body.incomplete_details,
|
|
723
|
+
usage: body.usage,
|
|
724
|
+
hasToolCalls,
|
|
725
|
+
sawText: flags.sawText,
|
|
726
|
+
sawRefusal: flags.sawRefusal,
|
|
727
|
+
...id !== undefined && { responseId: id },
|
|
728
|
+
...modelId !== undefined && { responseModelId: modelId },
|
|
729
|
+
...timestamp !== undefined && { responseTimestamp: timestamp }
|
|
730
|
+
});
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
// src/agent-runtime/openai-sse-state.ts
|
|
734
|
+
function intField(obj, key) {
|
|
735
|
+
const v = obj[key];
|
|
736
|
+
return typeof v === "number" && Number.isInteger(v) && v >= 0 ? v : undefined;
|
|
737
|
+
}
|
|
738
|
+
function lookupItem(state, itemId, outputIndex) {
|
|
739
|
+
const byId = itemId !== undefined ? state.itemsById.get(itemId) : undefined;
|
|
740
|
+
if (byId !== undefined)
|
|
741
|
+
return byId;
|
|
742
|
+
const key = outputIndex !== undefined ? state.keyByOutputIndex.get(outputIndex) : undefined;
|
|
743
|
+
return key !== undefined ? state.itemsById.get(key) : undefined;
|
|
744
|
+
}
|
|
745
|
+
function setItem(state, key, outputIndex, item) {
|
|
746
|
+
state.itemsById.set(key, item);
|
|
747
|
+
if (outputIndex !== undefined)
|
|
748
|
+
state.keyByOutputIndex.set(outputIndex, key);
|
|
749
|
+
}
|
|
750
|
+
function ensureTextItem(state, itemId, outputIndex) {
|
|
751
|
+
const existing = lookupItem(state, itemId, outputIndex);
|
|
752
|
+
if (existing?.kind === "message")
|
|
753
|
+
return existing;
|
|
754
|
+
const id = itemId ?? `text-${outputIndex ?? state.itemsById.size}`;
|
|
755
|
+
const created = {
|
|
756
|
+
kind: "message",
|
|
757
|
+
id,
|
|
758
|
+
segmentCount: 0,
|
|
759
|
+
openSegments: new Set
|
|
760
|
+
};
|
|
761
|
+
setItem(state, id, outputIndex, created);
|
|
762
|
+
return created;
|
|
763
|
+
}
|
|
764
|
+
function ensureReasoningItem(state, itemId, outputIndex) {
|
|
765
|
+
const existing = lookupItem(state, itemId, outputIndex);
|
|
766
|
+
if (existing?.kind === "reasoning")
|
|
767
|
+
return existing;
|
|
768
|
+
const id = itemId ?? `reasoning-${outputIndex ?? state.itemsById.size}`;
|
|
769
|
+
const created = {
|
|
770
|
+
kind: "reasoning",
|
|
771
|
+
id,
|
|
772
|
+
...itemId !== undefined && { itemId },
|
|
773
|
+
summaryTexts: [],
|
|
774
|
+
finalSummary: [],
|
|
775
|
+
opened: false,
|
|
776
|
+
closed: false,
|
|
777
|
+
pendingDone: false
|
|
778
|
+
};
|
|
779
|
+
setItem(state, id, outputIndex, created);
|
|
780
|
+
return created;
|
|
781
|
+
}
|
|
782
|
+
function resolveSummary(entry, raw) {
|
|
783
|
+
const fromWire = extractOpenAiReasoningSummary(raw);
|
|
784
|
+
if (fromWire.length > 0)
|
|
785
|
+
return fromWire;
|
|
786
|
+
const fromDeltas = entry.summaryTexts.filter((t) => t.length > 0).map((text) => ({ type: "summary_text", text }));
|
|
787
|
+
return fromDeltas.length > 0 ? fromDeltas : entry.finalSummary;
|
|
788
|
+
}
|
|
789
|
+
function* emitReasoningDelta(entry, delta) {
|
|
790
|
+
if (!entry.opened) {
|
|
791
|
+
entry.opened = true;
|
|
792
|
+
yield { type: "reasoning-start", id: entry.id };
|
|
793
|
+
}
|
|
794
|
+
if (delta.length > 0)
|
|
795
|
+
yield { type: "reasoning-delta", id: entry.id, delta };
|
|
796
|
+
}
|
|
797
|
+
function* closeTextSegment(entry, segmentId) {
|
|
798
|
+
if (!entry.openSegments.delete(segmentId))
|
|
799
|
+
return;
|
|
800
|
+
if (entry.activeSegmentId === segmentId)
|
|
801
|
+
entry.activeSegmentId = undefined;
|
|
802
|
+
yield {
|
|
803
|
+
type: "text-end",
|
|
804
|
+
id: segmentId,
|
|
805
|
+
...entry.phase !== undefined && { provider: buildOpenAiPhaseEnvelope(entry.phase) }
|
|
806
|
+
};
|
|
807
|
+
}
|
|
808
|
+
function* closeTextItem(entry) {
|
|
809
|
+
for (const segmentId of [...entry.openSegments])
|
|
810
|
+
yield* closeTextSegment(entry, segmentId);
|
|
811
|
+
}
|
|
812
|
+
function* closeReasoningItem(entry, summary, encryptedContent) {
|
|
813
|
+
entry.finalSummary = summary;
|
|
814
|
+
if (encryptedContent !== undefined)
|
|
815
|
+
entry.encryptedContent = encryptedContent;
|
|
816
|
+
if (!entry.opened) {
|
|
817
|
+
entry.opened = true;
|
|
818
|
+
yield { type: "reasoning-start", id: entry.id };
|
|
819
|
+
}
|
|
820
|
+
entry.closed = true;
|
|
821
|
+
entry.pendingDone = false;
|
|
822
|
+
yield {
|
|
823
|
+
type: "reasoning-end",
|
|
824
|
+
id: entry.id,
|
|
825
|
+
provider: buildOpenAiReasoningEnvelope({
|
|
826
|
+
...entry.itemId !== undefined && { id: entry.itemId },
|
|
827
|
+
summary,
|
|
828
|
+
...encryptedContent !== undefined && { encryptedContent }
|
|
829
|
+
})
|
|
830
|
+
};
|
|
831
|
+
}
|
|
832
|
+
function* emitMetadata(state, response) {
|
|
833
|
+
const id = nonEmptyString2(response?.id);
|
|
834
|
+
const modelId = nonEmptyString2(response?.model);
|
|
835
|
+
const createdAt = typeof response?.created_at === "number" && Number.isFinite(response.created_at) && response.created_at >= 0 ? new Date(response.created_at * 1000) : undefined;
|
|
836
|
+
if (id !== undefined)
|
|
837
|
+
state.responseId = id;
|
|
838
|
+
if (modelId !== undefined)
|
|
839
|
+
state.responseModelId = modelId;
|
|
840
|
+
if (createdAt !== undefined && state.responseTimestamp === undefined) {
|
|
841
|
+
state.responseTimestamp = createdAt;
|
|
842
|
+
}
|
|
843
|
+
const hasMeta = state.responseId !== undefined || state.responseModelId !== undefined || state.responseTimestamp !== undefined;
|
|
844
|
+
if (state.metadataEmitted || !hasMeta)
|
|
845
|
+
return;
|
|
846
|
+
state.metadataEmitted = true;
|
|
847
|
+
yield {
|
|
848
|
+
type: "response-metadata",
|
|
849
|
+
...state.responseId !== undefined && { id: state.responseId },
|
|
850
|
+
...state.responseModelId !== undefined && { modelId: state.responseModelId },
|
|
851
|
+
...state.responseTimestamp !== undefined && { timestamp: state.responseTimestamp }
|
|
852
|
+
};
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
// src/agent-runtime/openai-sse.ts
|
|
856
|
+
function* handleOutputItemAdded(state, payload) {
|
|
857
|
+
const item = isRecord(payload.item) ? payload.item : undefined;
|
|
858
|
+
if (!item)
|
|
859
|
+
return;
|
|
860
|
+
const outputIndex = intField(payload, "output_index");
|
|
861
|
+
const rawItemId = nonEmptyString2(item.id);
|
|
862
|
+
if (item.type === "message") {
|
|
863
|
+
const entry = ensureTextItem(state, rawItemId, outputIndex);
|
|
864
|
+
const phase = nonEmptyString2(item.phase);
|
|
865
|
+
if (phase !== undefined)
|
|
866
|
+
entry.phase = phase;
|
|
867
|
+
return;
|
|
868
|
+
}
|
|
869
|
+
if (item.type === "reasoning") {
|
|
870
|
+
const entry = ensureReasoningItem(state, rawItemId, outputIndex);
|
|
871
|
+
const initial = extractOpenAiReasoningSummary(item.summary);
|
|
872
|
+
const encrypted = nonEmptyString2(item.encrypted_content);
|
|
873
|
+
if (rawItemId !== undefined)
|
|
874
|
+
entry.itemId = rawItemId;
|
|
875
|
+
if (initial.length > 0) {
|
|
876
|
+
entry.finalSummary = initial;
|
|
877
|
+
entry.summaryTexts = initial.map((s) => s.text);
|
|
878
|
+
}
|
|
879
|
+
if (encrypted !== undefined)
|
|
880
|
+
entry.encryptedContent = encrypted;
|
|
881
|
+
if (!entry.opened) {
|
|
882
|
+
entry.opened = true;
|
|
883
|
+
yield { type: "reasoning-start", id: entry.id };
|
|
884
|
+
}
|
|
885
|
+
return;
|
|
886
|
+
}
|
|
887
|
+
if (item.type === "function_call") {
|
|
888
|
+
const key = rawItemId ?? `function_call-${outputIndex ?? state.itemsById.size}`;
|
|
889
|
+
const toolCallId = nonEmptyString2(item.call_id) ?? crypto.randomUUID();
|
|
890
|
+
const name = typeof item.name === "string" ? item.name : "";
|
|
891
|
+
const args = typeof item.arguments === "string" ? item.arguments : "";
|
|
892
|
+
setItem(state, key, outputIndex, {
|
|
893
|
+
kind: "function_call",
|
|
894
|
+
...rawItemId !== undefined && { itemId: rawItemId },
|
|
895
|
+
callId: toolCallId,
|
|
896
|
+
name,
|
|
897
|
+
arguments: args,
|
|
898
|
+
started: true
|
|
899
|
+
});
|
|
900
|
+
yield { type: "tool-input-start", toolCallId, toolName: name };
|
|
901
|
+
if (args.length > 0)
|
|
902
|
+
yield { type: "tool-input-delta", toolCallId, delta: args };
|
|
903
|
+
}
|
|
904
|
+
}
|
|
905
|
+
function* handleOutputItemDone(state, payload) {
|
|
906
|
+
const item = isRecord(payload.item) ? payload.item : undefined;
|
|
907
|
+
if (!item)
|
|
908
|
+
return;
|
|
909
|
+
const outputIndex = intField(payload, "output_index");
|
|
910
|
+
const rawItemId = nonEmptyString2(item.id);
|
|
911
|
+
if (item.type === "message") {
|
|
912
|
+
const entry = ensureTextItem(state, rawItemId, outputIndex);
|
|
913
|
+
const phase = nonEmptyString2(item.phase);
|
|
914
|
+
if (phase !== undefined)
|
|
915
|
+
entry.phase = phase;
|
|
916
|
+
const scanned = scanOpenAiMessageContent(item.content);
|
|
917
|
+
if (scanned.sawRefusal)
|
|
918
|
+
state.sawRefusal = true;
|
|
919
|
+
if (entry.segmentCount === 0 && scanned.text.length > 0) {
|
|
920
|
+
entry.segmentCount = 1;
|
|
921
|
+
entry.openSegments.add(entry.id);
|
|
922
|
+
state.sawText = true;
|
|
923
|
+
yield { type: "text-start", id: entry.id };
|
|
924
|
+
yield { type: "text-delta", id: entry.id, delta: scanned.text };
|
|
925
|
+
}
|
|
926
|
+
yield* closeTextItem(entry);
|
|
927
|
+
return;
|
|
928
|
+
}
|
|
929
|
+
if (item.type === "reasoning") {
|
|
930
|
+
const entry = ensureReasoningItem(state, rawItemId, outputIndex);
|
|
931
|
+
if (rawItemId !== undefined)
|
|
932
|
+
entry.itemId = rawItemId;
|
|
933
|
+
const summary = resolveSummary(entry, item.summary);
|
|
934
|
+
entry.finalSummary = summary;
|
|
935
|
+
const encrypted = nonEmptyString2(item.encrypted_content) ?? entry.encryptedContent;
|
|
936
|
+
if (encrypted !== undefined) {
|
|
937
|
+
yield* closeReasoningItem(entry, summary, encrypted);
|
|
938
|
+
} else {
|
|
939
|
+
if (!entry.opened) {
|
|
940
|
+
entry.opened = true;
|
|
941
|
+
yield { type: "reasoning-start", id: entry.id };
|
|
942
|
+
}
|
|
943
|
+
entry.pendingDone = true;
|
|
944
|
+
}
|
|
945
|
+
return;
|
|
946
|
+
}
|
|
947
|
+
if (item.type === "function_call") {
|
|
948
|
+
const found = lookupItem(state, rawItemId, outputIndex);
|
|
949
|
+
const entry = found?.kind === "function_call" ? found : undefined;
|
|
950
|
+
const toolCallId = nonEmptyString2(item.call_id) ?? entry?.callId ?? crypto.randomUUID();
|
|
951
|
+
const itemId = rawItemId ?? entry?.itemId;
|
|
952
|
+
const toolName = nonEmptyString2(item.name) ?? entry?.name ?? "";
|
|
953
|
+
const args = typeof item.arguments === "string" && item.arguments.trim().length > 0 ? item.arguments : entry?.arguments ?? "";
|
|
954
|
+
state.hasToolCalls = true;
|
|
955
|
+
if (!entry?.started)
|
|
956
|
+
yield { type: "tool-input-start", toolCallId, toolName };
|
|
957
|
+
yield { type: "tool-input-end", toolCallId };
|
|
958
|
+
yield {
|
|
959
|
+
type: "tool-call",
|
|
960
|
+
toolCallId,
|
|
961
|
+
toolName,
|
|
962
|
+
input: args.trim().length > 0 ? args : "{}",
|
|
963
|
+
...itemId !== undefined && { provider: buildOpenAiToolCallEnvelope(itemId) }
|
|
964
|
+
};
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
function* flushAndBackfill(state, response) {
|
|
968
|
+
for (const item of state.itemsById.values()) {
|
|
969
|
+
if (item.kind === "message")
|
|
970
|
+
yield* closeTextItem(item);
|
|
971
|
+
}
|
|
972
|
+
const output = Array.isArray(response?.output) ? response.output : [];
|
|
973
|
+
for (let index = 0;index < output.length; index += 1) {
|
|
974
|
+
const rawItem = output[index];
|
|
975
|
+
if (!isRecord(rawItem))
|
|
976
|
+
continue;
|
|
977
|
+
if (rawItem.type === "message") {
|
|
978
|
+
if (scanOpenAiMessageContent(rawItem.content).sawRefusal)
|
|
979
|
+
state.sawRefusal = true;
|
|
980
|
+
continue;
|
|
981
|
+
}
|
|
982
|
+
if (rawItem.type !== "reasoning")
|
|
983
|
+
continue;
|
|
984
|
+
const itemId = nonEmptyString2(rawItem.id);
|
|
985
|
+
const encrypted = nonEmptyString2(rawItem.encrypted_content);
|
|
986
|
+
const rawSummary = extractOpenAiReasoningSummary(rawItem.summary);
|
|
987
|
+
const existing = lookupItem(state, itemId, index);
|
|
988
|
+
if (existing?.kind === "reasoning") {
|
|
989
|
+
if (itemId !== undefined)
|
|
990
|
+
existing.itemId = itemId;
|
|
991
|
+
const summary = rawSummary.length > 0 ? rawSummary : resolveSummary(existing, undefined);
|
|
992
|
+
const merged = encrypted ?? existing.encryptedContent;
|
|
993
|
+
if (!existing.closed)
|
|
994
|
+
yield* closeReasoningItem(existing, summary, merged);
|
|
995
|
+
continue;
|
|
996
|
+
}
|
|
997
|
+
const created = ensureReasoningItem(state, itemId, index);
|
|
998
|
+
const summaryText = rawSummary.map((s) => s.text).join(`
|
|
999
|
+
`);
|
|
1000
|
+
if (summaryText.length > 0)
|
|
1001
|
+
yield* emitReasoningDelta(created, summaryText);
|
|
1002
|
+
yield* closeReasoningItem(created, rawSummary, encrypted);
|
|
1003
|
+
}
|
|
1004
|
+
for (const item of state.itemsById.values()) {
|
|
1005
|
+
if (item.kind === "reasoning" && (item.opened || item.pendingDone) && !item.closed) {
|
|
1006
|
+
yield* closeReasoningItem(item, resolveSummary(item, undefined), item.encryptedContent);
|
|
1007
|
+
}
|
|
1008
|
+
}
|
|
1009
|
+
}
|
|
1010
|
+
function* decodeOpenAiEvent(state, eventType, payload) {
|
|
1011
|
+
const itemId = nonEmptyString2(payload.item_id);
|
|
1012
|
+
const outputIndex = intField(payload, "output_index");
|
|
1013
|
+
switch (eventType) {
|
|
1014
|
+
case "response.created":
|
|
1015
|
+
case "response.in_progress":
|
|
1016
|
+
yield* emitMetadata(state, isRecord(payload.response) ? payload.response : undefined);
|
|
1017
|
+
return;
|
|
1018
|
+
case "response.output_item.added":
|
|
1019
|
+
yield* handleOutputItemAdded(state, payload);
|
|
1020
|
+
return;
|
|
1021
|
+
case "response.output_text.delta": {
|
|
1022
|
+
const entry = ensureTextItem(state, itemId, outputIndex);
|
|
1023
|
+
const contentIndex = intField(payload, "content_index");
|
|
1024
|
+
const segmentId = contentIndex !== undefined ? `${entry.id}:${contentIndex}` : entry.activeSegmentId ?? (entry.segmentCount === 0 ? entry.id : `${entry.id}:${entry.segmentCount}`);
|
|
1025
|
+
if (!entry.openSegments.has(segmentId)) {
|
|
1026
|
+
entry.openSegments.add(segmentId);
|
|
1027
|
+
entry.activeSegmentId = segmentId;
|
|
1028
|
+
entry.segmentCount += 1;
|
|
1029
|
+
yield { type: "text-start", id: segmentId };
|
|
1030
|
+
}
|
|
1031
|
+
if (typeof payload.delta === "string" && payload.delta.length > 0) {
|
|
1032
|
+
state.sawText = true;
|
|
1033
|
+
yield { type: "text-delta", id: segmentId, delta: payload.delta };
|
|
1034
|
+
}
|
|
1035
|
+
return;
|
|
1036
|
+
}
|
|
1037
|
+
case "response.output_text.done": {
|
|
1038
|
+
const found = lookupItem(state, itemId, outputIndex);
|
|
1039
|
+
if (found?.kind === "message") {
|
|
1040
|
+
const phase = nonEmptyString2(payload.phase);
|
|
1041
|
+
if (phase !== undefined)
|
|
1042
|
+
found.phase = phase;
|
|
1043
|
+
const contentIndex = intField(payload, "content_index");
|
|
1044
|
+
const targetId = contentIndex !== undefined ? `${found.id}:${contentIndex}` : found.activeSegmentId;
|
|
1045
|
+
if (targetId !== undefined)
|
|
1046
|
+
yield* closeTextSegment(found, targetId);
|
|
1047
|
+
}
|
|
1048
|
+
return;
|
|
1049
|
+
}
|
|
1050
|
+
case "response.output_text.annotation.added": {
|
|
1051
|
+
const source = extractOpenAiUrlCitation(payload.annotation);
|
|
1052
|
+
if (source !== undefined)
|
|
1053
|
+
yield { type: "source", source };
|
|
1054
|
+
return;
|
|
1055
|
+
}
|
|
1056
|
+
case "response.refusal.delta":
|
|
1057
|
+
case "response.refusal.done":
|
|
1058
|
+
state.sawRefusal = true;
|
|
1059
|
+
return;
|
|
1060
|
+
case "response.reasoning_summary_text.delta":
|
|
1061
|
+
case "response.reasoning_text.delta": {
|
|
1062
|
+
const entry = ensureReasoningItem(state, itemId, outputIndex);
|
|
1063
|
+
const delta = typeof payload.delta === "string" ? payload.delta : "";
|
|
1064
|
+
if (eventType === "response.reasoning_summary_text.delta" && delta.length > 0) {
|
|
1065
|
+
const idx = intField(payload, "summary_index") ?? 0;
|
|
1066
|
+
entry.summaryTexts[idx] = `${entry.summaryTexts[idx] ?? ""}${delta}`;
|
|
1067
|
+
}
|
|
1068
|
+
yield* emitReasoningDelta(entry, delta);
|
|
1069
|
+
return;
|
|
1070
|
+
}
|
|
1071
|
+
case "response.reasoning_summary_text.done": {
|
|
1072
|
+
const entry = ensureReasoningItem(state, itemId, outputIndex);
|
|
1073
|
+
const idx = intField(payload, "summary_index") ?? 0;
|
|
1074
|
+
if (typeof payload.text === "string") {
|
|
1075
|
+
if ((entry.summaryTexts[idx] ?? "").length === 0 && payload.text.length > 0) {
|
|
1076
|
+
yield* emitReasoningDelta(entry, payload.text);
|
|
1077
|
+
}
|
|
1078
|
+
entry.summaryTexts[idx] = payload.text;
|
|
1079
|
+
}
|
|
1080
|
+
return;
|
|
1081
|
+
}
|
|
1082
|
+
case "response.function_call_arguments.delta":
|
|
1083
|
+
case "response.function_call_arguments.done": {
|
|
1084
|
+
const found = lookupItem(state, itemId, outputIndex);
|
|
1085
|
+
if (found?.kind !== "function_call")
|
|
1086
|
+
return;
|
|
1087
|
+
if (eventType === "response.function_call_arguments.delta") {
|
|
1088
|
+
const delta = typeof payload.delta === "string" ? payload.delta : "";
|
|
1089
|
+
if (delta.length > 0) {
|
|
1090
|
+
found.arguments += delta;
|
|
1091
|
+
yield { type: "tool-input-delta", toolCallId: found.callId, delta };
|
|
1092
|
+
}
|
|
1093
|
+
} else if (typeof payload.arguments === "string" && found.arguments.length === 0) {
|
|
1094
|
+
found.arguments = payload.arguments;
|
|
1095
|
+
}
|
|
1096
|
+
return;
|
|
1097
|
+
}
|
|
1098
|
+
case "response.output_item.done":
|
|
1099
|
+
yield* handleOutputItemDone(state, payload);
|
|
1100
|
+
return;
|
|
1101
|
+
case "response.completed":
|
|
1102
|
+
case "response.incomplete": {
|
|
1103
|
+
const response = isRecord(payload.response) ? payload.response : undefined;
|
|
1104
|
+
state.sawTerminal = true;
|
|
1105
|
+
yield* emitMetadata(state, response);
|
|
1106
|
+
yield* flushAndBackfill(state, response);
|
|
1107
|
+
yield buildOpenAiFinishPart({
|
|
1108
|
+
status: eventType === "response.completed" ? "completed" : "incomplete",
|
|
1109
|
+
incompleteDetails: response?.incomplete_details,
|
|
1110
|
+
usage: response?.usage,
|
|
1111
|
+
hasToolCalls: state.hasToolCalls,
|
|
1112
|
+
sawText: state.sawText,
|
|
1113
|
+
sawRefusal: state.sawRefusal,
|
|
1114
|
+
...state.responseId !== undefined && { responseId: state.responseId },
|
|
1115
|
+
...state.responseModelId !== undefined && { responseModelId: state.responseModelId },
|
|
1116
|
+
...state.responseTimestamp !== undefined && {
|
|
1117
|
+
responseTimestamp: state.responseTimestamp
|
|
1118
|
+
}
|
|
1119
|
+
});
|
|
1120
|
+
return;
|
|
1121
|
+
}
|
|
1122
|
+
case "response.failed":
|
|
1123
|
+
case "error":
|
|
1124
|
+
state.sawTerminal = true;
|
|
1125
|
+
yield {
|
|
1126
|
+
type: "error",
|
|
1127
|
+
error: createOpenAiStreamError(payload, "OpenAI Responses stream failed")
|
|
1128
|
+
};
|
|
1129
|
+
return;
|
|
1130
|
+
}
|
|
1131
|
+
}
|
|
1132
|
+
async function* decodeOpenAiSseStream(body, signal) {
|
|
1133
|
+
const state = {
|
|
1134
|
+
metadataEmitted: false,
|
|
1135
|
+
sawText: false,
|
|
1136
|
+
sawRefusal: false,
|
|
1137
|
+
hasToolCalls: false,
|
|
1138
|
+
sawTerminal: false,
|
|
1139
|
+
itemsById: new Map,
|
|
1140
|
+
keyByOutputIndex: new Map
|
|
1141
|
+
};
|
|
1142
|
+
try {
|
|
1143
|
+
for await (const frame of parseSseStream(body, {
|
|
1144
|
+
provider: "openai",
|
|
1145
|
+
...signal !== undefined && { signal }
|
|
1146
|
+
})) {
|
|
1147
|
+
if (frame.kind === "ping") {
|
|
1148
|
+
yield { type: "ping" };
|
|
1149
|
+
continue;
|
|
1150
|
+
}
|
|
1151
|
+
if (frame.data.trim() === "[DONE]")
|
|
1152
|
+
continue;
|
|
1153
|
+
const parsed = parseSseJson("openai", frame.data);
|
|
1154
|
+
if (!isRecord(parsed))
|
|
1155
|
+
continue;
|
|
1156
|
+
yield* decodeOpenAiEvent(state, nonEmptyString2(parsed.type) ?? frame.event, parsed);
|
|
1157
|
+
}
|
|
1158
|
+
} catch (error) {
|
|
1159
|
+
if (signal?.aborted || error instanceof AgentProviderError2 || error instanceof AgentProviderStreamCutError2 || error instanceof Error && error.name === "AbortError") {
|
|
1160
|
+
throw error;
|
|
1161
|
+
}
|
|
1162
|
+
throw new AgentProviderStreamCutError2("OpenAI Responses SSE stream disconnected before a terminal event", { provider: "openai", cause: error });
|
|
1163
|
+
}
|
|
1164
|
+
if (!state.sawTerminal) {
|
|
1165
|
+
throw new AgentProviderStreamCutError2("OpenAI Responses SSE stream ended before a terminal event", { provider: "openai" });
|
|
1166
|
+
}
|
|
1167
|
+
}
|
|
1168
|
+
|
|
1169
|
+
// src/agent-runtime/openai.ts
|
|
1170
|
+
var DEFAULT_OPENAI_BASE_URL = "https://api.openai.com/v1";
|
|
1171
|
+
var OWNED_KEYS = [
|
|
1172
|
+
"model",
|
|
1173
|
+
"input",
|
|
1174
|
+
"instructions",
|
|
1175
|
+
"stream",
|
|
1176
|
+
"store",
|
|
1177
|
+
"reasoning",
|
|
1178
|
+
"include",
|
|
1179
|
+
"service_tier",
|
|
1180
|
+
"truncation",
|
|
1181
|
+
"prompt_cache_key",
|
|
1182
|
+
"prompt_cache_retention",
|
|
1183
|
+
"parallel_tool_calls",
|
|
1184
|
+
"tools",
|
|
1185
|
+
"tool_choice",
|
|
1186
|
+
"text",
|
|
1187
|
+
"max_output_tokens",
|
|
1188
|
+
"temperature",
|
|
1189
|
+
"top_p",
|
|
1190
|
+
"user"
|
|
1191
|
+
];
|
|
1192
|
+
function buildAuthHeaders(settings, apiKey) {
|
|
1193
|
+
return {
|
|
1194
|
+
authorization: `Bearer ${apiKey}`,
|
|
1195
|
+
...settings.organization !== undefined && {
|
|
1196
|
+
"OpenAI-Organization": settings.organization
|
|
1197
|
+
},
|
|
1198
|
+
...settings.project !== undefined && { "OpenAI-Project": settings.project }
|
|
1199
|
+
};
|
|
1200
|
+
}
|
|
1201
|
+
function openAiProvider(settings, access = {}) {
|
|
1202
|
+
assertNoExtraBodyClash("openAiProvider", settings.extraBody, OWNED_KEYS);
|
|
1203
|
+
const validatedDefaultPolicy = access.defaultPolicy !== undefined ? OpenAiRequestPolicySchema.parse(access.defaultPolicy) : undefined;
|
|
1204
|
+
const url = joinProviderUrl(settings.baseUrl ?? DEFAULT_OPENAI_BASE_URL, "/responses");
|
|
1205
|
+
const cachePerConversation = access.cachePerConversation === true;
|
|
1206
|
+
return {
|
|
1207
|
+
create(modelId) {
|
|
1208
|
+
return {
|
|
1209
|
+
provider: "openai",
|
|
1210
|
+
modelId,
|
|
1211
|
+
async stream(request, signal) {
|
|
1212
|
+
const policy = resolveOpenAiPolicy(access.resolvePolicy?.(modelId) ?? validatedDefaultPolicy ?? {});
|
|
1213
|
+
const body = buildOpenAiRequestBody({
|
|
1214
|
+
modelId,
|
|
1215
|
+
request,
|
|
1216
|
+
policy,
|
|
1217
|
+
cachePerConversation,
|
|
1218
|
+
...settings.extraBody !== undefined && { extraBody: settings.extraBody }
|
|
1219
|
+
});
|
|
1220
|
+
const apiKey = await resolveProviderApiKey(settings.apiKey);
|
|
1221
|
+
const response = await postProviderStream({
|
|
1222
|
+
provider: "openai",
|
|
1223
|
+
url,
|
|
1224
|
+
headers: buildAuthHeaders(settings, apiKey),
|
|
1225
|
+
...settings.headers !== undefined && { extraHeaders: settings.headers },
|
|
1226
|
+
...request.headers !== undefined && { requestHeaders: request.headers },
|
|
1227
|
+
body,
|
|
1228
|
+
signal,
|
|
1229
|
+
...settings.fetch !== undefined && { fetcher: settings.fetch },
|
|
1230
|
+
parseError: parseOpenAiError
|
|
1231
|
+
});
|
|
1232
|
+
if (response.kind === "json") {
|
|
1233
|
+
return { parts: decodeOpenAiJsonResponse(response.json) };
|
|
1234
|
+
}
|
|
1235
|
+
return { parts: decodeOpenAiSseStream(response.body, signal) };
|
|
1236
|
+
}
|
|
1237
|
+
};
|
|
1238
|
+
}
|
|
1239
|
+
};
|
|
1240
|
+
}
|
|
1241
|
+
export {
|
|
1242
|
+
OpenAiReasoningPolicySchema,
|
|
1243
|
+
OpenAiRequestPolicySchema,
|
|
1244
|
+
normalizeOpenAiUsage,
|
|
1245
|
+
openAiProvider
|
|
1246
|
+
};
|