openai 7.25.0 → 7.27.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 +40 -0
- package/client.d.mts +5 -6
- package/client.d.mts.map +1 -1
- package/client.d.ts +5 -6
- package/client.d.ts.map +1 -1
- package/client.js +38 -10
- package/client.js.map +1 -1
- package/client.mjs +39 -11
- package/client.mjs.map +1 -1
- package/core/streaming.d.mts +4 -0
- package/core/streaming.d.mts.map +1 -1
- package/core/streaming.d.ts +4 -0
- package/core/streaming.d.ts.map +1 -1
- package/core/streaming.js +6 -0
- package/core/streaming.js.map +1 -1
- package/core/streaming.mjs +6 -0
- package/core/streaming.mjs.map +1 -1
- package/helpers/beta/agents/filesystem.d.mts +17 -0
- package/helpers/beta/agents/filesystem.d.mts.map +1 -0
- package/helpers/beta/agents/filesystem.d.ts +17 -0
- package/helpers/beta/agents/filesystem.d.ts.map +1 -0
- package/helpers/beta/agents/filesystem.js +168 -0
- package/helpers/beta/agents/filesystem.js.map +1 -0
- package/helpers/beta/agents/filesystem.mjs +162 -0
- package/helpers/beta/agents/filesystem.mjs.map +1 -0
- package/helpers/beta/agents/standard-schema.d.mts +7 -0
- package/helpers/beta/agents/standard-schema.d.mts.map +1 -0
- package/helpers/beta/agents/standard-schema.d.ts +7 -0
- package/helpers/beta/agents/standard-schema.d.ts.map +1 -0
- package/helpers/beta/agents/standard-schema.js +12 -0
- package/helpers/beta/agents/standard-schema.js.map +1 -0
- package/helpers/beta/agents/standard-schema.mjs +9 -0
- package/helpers/beta/agents/standard-schema.mjs.map +1 -0
- package/helpers/beta/agents/zod.d.mts +10 -0
- package/helpers/beta/agents/zod.d.mts.map +1 -0
- package/helpers/beta/agents/zod.d.ts +10 -0
- package/helpers/beta/agents/zod.d.ts.map +1 -0
- package/helpers/beta/agents/zod.js +12 -0
- package/helpers/beta/agents/zod.js.map +1 -0
- package/helpers/beta/agents/zod.mjs +9 -0
- package/helpers/beta/agents/zod.mjs.map +1 -0
- package/internal/realtime-credentials.d.mts +22 -1
- package/internal/realtime-credentials.d.mts.map +1 -1
- package/internal/realtime-credentials.d.ts +22 -1
- package/internal/realtime-credentials.d.ts.map +1 -1
- package/internal/realtime-credentials.js +48 -4
- package/internal/realtime-credentials.js.map +1 -1
- package/internal/realtime-credentials.mjs +45 -4
- package/internal/realtime-credentials.mjs.map +1 -1
- package/internal/responses-ws-credentials.d.mts +23 -0
- package/internal/responses-ws-credentials.d.mts.map +1 -0
- package/internal/responses-ws-credentials.d.ts +23 -0
- package/internal/responses-ws-credentials.d.ts.map +1 -0
- package/internal/responses-ws-credentials.js +108 -0
- package/internal/responses-ws-credentials.js.map +1 -0
- package/internal/responses-ws-credentials.mjs +104 -0
- package/internal/responses-ws-credentials.mjs.map +1 -0
- package/internal/shim-types.d.mts +9 -0
- package/internal/shim-types.d.mts.map +1 -1
- package/internal/shim-types.d.ts +9 -0
- package/internal/shim-types.d.ts.map +1 -1
- package/internal/tslib.js +4 -0
- package/internal/ws-adapter-node.d.mts +4 -0
- package/internal/ws-adapter-node.d.mts.map +1 -1
- package/internal/ws-adapter-node.d.ts +4 -0
- package/internal/ws-adapter-node.d.ts.map +1 -1
- package/internal/ws-adapter-node.js +56 -0
- package/internal/ws-adapter-node.js.map +1 -1
- package/internal/ws-adapter-node.mjs +55 -0
- package/internal/ws-adapter-node.mjs.map +1 -1
- package/internal/ws.d.mts +27 -4
- package/internal/ws.d.mts.map +1 -1
- package/internal/ws.d.ts +27 -4
- package/internal/ws.d.ts.map +1 -1
- package/internal/ws.js +105 -5
- package/internal/ws.js.map +1 -1
- package/internal/ws.mjs +102 -4
- package/internal/ws.mjs.map +1 -1
- package/lib/agents/agent-session-stream.d.mts +13 -4
- package/lib/agents/agent-session-stream.d.mts.map +1 -1
- package/lib/agents/agent-session-stream.d.ts +13 -4
- package/lib/agents/agent-session-stream.d.ts.map +1 -1
- package/lib/agents/agent-session-stream.js +24 -21
- package/lib/agents/agent-session-stream.js.map +1 -1
- package/lib/agents/agent-session-stream.mjs +24 -21
- package/lib/agents/agent-session-stream.mjs.map +1 -1
- package/lib/beta/agents/agent-session-create-stream.d.mts +15 -0
- package/lib/beta/agents/agent-session-create-stream.d.mts.map +1 -0
- package/lib/beta/agents/agent-session-create-stream.d.ts +15 -0
- package/lib/beta/agents/agent-session-create-stream.d.ts.map +1 -0
- package/lib/beta/agents/agent-session-create-stream.js +25 -0
- package/lib/beta/agents/agent-session-create-stream.js.map +1 -0
- package/lib/beta/agents/agent-session-create-stream.mjs +22 -0
- package/lib/beta/agents/agent-session-create-stream.mjs.map +1 -0
- package/lib/beta/agents/agent-turn-result-collector.d.mts +17 -0
- package/lib/beta/agents/agent-turn-result-collector.d.mts.map +1 -0
- package/lib/beta/agents/agent-turn-result-collector.d.ts +17 -0
- package/lib/beta/agents/agent-turn-result-collector.d.ts.map +1 -0
- package/lib/beta/agents/agent-turn-result-collector.js +99 -0
- package/lib/beta/agents/agent-turn-result-collector.js.map +1 -0
- package/lib/beta/agents/agent-turn-result-collector.mjs +95 -0
- package/lib/beta/agents/agent-turn-result-collector.mjs.map +1 -0
- package/lib/beta/agents/agent-turn-result-error.d.mts +16 -0
- package/lib/beta/agents/agent-turn-result-error.d.mts.map +1 -0
- package/lib/beta/agents/agent-turn-result-error.d.ts +16 -0
- package/lib/beta/agents/agent-turn-result-error.d.ts.map +1 -0
- package/lib/beta/agents/agent-turn-result-error.js +22 -0
- package/lib/beta/agents/agent-turn-result-error.js.map +1 -0
- package/lib/beta/agents/agent-turn-result-error.mjs +18 -0
- package/lib/beta/agents/agent-turn-result-error.mjs.map +1 -0
- package/lib/beta/agents/agent-turn-result.d.mts +12 -0
- package/lib/beta/agents/agent-turn-result.d.mts.map +1 -0
- package/lib/beta/agents/agent-turn-result.d.ts +12 -0
- package/lib/beta/agents/agent-turn-result.d.ts.map +1 -0
- package/lib/beta/agents/agent-turn-result.js +22 -0
- package/lib/beta/agents/agent-turn-result.js.map +1 -0
- package/lib/beta/agents/agent-turn-result.mjs +18 -0
- package/lib/beta/agents/agent-turn-result.mjs.map +1 -0
- package/lib/beta/agents/files.d.mts +33 -0
- package/lib/beta/agents/files.d.mts.map +1 -0
- package/lib/beta/agents/files.d.ts +33 -0
- package/lib/beta/agents/files.d.ts.map +1 -0
- package/lib/beta/agents/files.js +93 -0
- package/lib/beta/agents/files.js.map +1 -0
- package/lib/beta/agents/files.mjs +86 -0
- package/lib/beta/agents/files.mjs.map +1 -0
- package/lib/beta/agents/function-tool.d.mts +27 -0
- package/lib/beta/agents/function-tool.d.mts.map +1 -0
- package/lib/beta/agents/function-tool.d.ts +27 -0
- package/lib/beta/agents/function-tool.d.ts.map +1 -0
- package/lib/beta/agents/function-tool.js +48 -0
- package/lib/beta/agents/function-tool.js.map +1 -0
- package/lib/beta/agents/function-tool.mjs +45 -0
- package/lib/beta/agents/function-tool.mjs.map +1 -0
- package/lib/beta/agents/output-format-types.d.mts +11 -0
- package/lib/beta/agents/output-format-types.d.mts.map +1 -0
- package/lib/beta/agents/output-format-types.d.ts +11 -0
- package/lib/beta/agents/output-format-types.d.ts.map +1 -0
- package/lib/beta/agents/output-format-types.js +3 -0
- package/lib/beta/agents/output-format-types.js.map +1 -0
- package/lib/beta/agents/output-format-types.mjs +2 -0
- package/lib/beta/agents/output-format-types.mjs.map +1 -0
- package/lib/beta/agents/output-format.d.mts +8 -0
- package/lib/beta/agents/output-format.d.mts.map +1 -0
- package/lib/beta/agents/output-format.d.ts +8 -0
- package/lib/beta/agents/output-format.d.ts.map +1 -0
- package/lib/beta/agents/output-format.js +20 -0
- package/lib/beta/agents/output-format.js.map +1 -0
- package/lib/beta/agents/output-format.mjs +14 -0
- package/lib/beta/agents/output-format.mjs.map +1 -0
- package/lib/beta/agents/output-parse-error.d.mts +11 -0
- package/lib/beta/agents/output-parse-error.d.mts.map +1 -0
- package/lib/beta/agents/output-parse-error.d.ts +11 -0
- package/lib/beta/agents/output-parse-error.d.ts.map +1 -0
- package/lib/beta/agents/output-parse-error.js +22 -0
- package/lib/beta/agents/output-parse-error.js.map +1 -0
- package/lib/beta/agents/output-parse-error.mjs +18 -0
- package/lib/beta/agents/output-parse-error.mjs.map +1 -0
- package/lib/beta/agents/pages.d.mts +9 -0
- package/lib/beta/agents/pages.d.mts.map +1 -0
- package/lib/beta/agents/pages.d.ts +9 -0
- package/lib/beta/agents/pages.d.ts.map +1 -0
- package/lib/beta/agents/pages.js +32 -0
- package/lib/beta/agents/pages.js.map +1 -0
- package/lib/beta/agents/pages.mjs +29 -0
- package/lib/beta/agents/pages.mjs.map +1 -0
- package/lib/beta/agents/parse-result.d.mts +95 -0
- package/lib/beta/agents/parse-result.d.mts.map +1 -0
- package/lib/beta/agents/parse-result.d.ts +95 -0
- package/lib/beta/agents/parse-result.d.ts.map +1 -0
- package/lib/beta/agents/parse-result.js +116 -0
- package/lib/beta/agents/parse-result.js.map +1 -0
- package/lib/beta/agents/parse-result.mjs +110 -0
- package/lib/beta/agents/parse-result.mjs.map +1 -0
- package/lib/beta/agents/parsed-agent-turn-result.d.mts +8 -0
- package/lib/beta/agents/parsed-agent-turn-result.d.mts.map +1 -0
- package/lib/beta/agents/parsed-agent-turn-result.d.ts +8 -0
- package/lib/beta/agents/parsed-agent-turn-result.d.ts.map +1 -0
- package/lib/beta/agents/parsed-agent-turn-result.js +14 -0
- package/lib/beta/agents/parsed-agent-turn-result.js.map +1 -0
- package/lib/beta/agents/parsed-agent-turn-result.mjs +10 -0
- package/lib/beta/agents/parsed-agent-turn-result.mjs.map +1 -0
- package/lib/beta/agents/result-artifacts.d.mts +19 -0
- package/lib/beta/agents/result-artifacts.d.mts.map +1 -0
- package/lib/beta/agents/result-artifacts.d.ts +19 -0
- package/lib/beta/agents/result-artifacts.d.ts.map +1 -0
- package/lib/beta/agents/result-artifacts.js +67 -0
- package/lib/beta/agents/result-artifacts.js.map +1 -0
- package/lib/beta/agents/result-artifacts.mjs +63 -0
- package/lib/beta/agents/result-artifacts.mjs.map +1 -0
- package/lib/beta/agents/result-collection.d.mts +15 -0
- package/lib/beta/agents/result-collection.d.mts.map +1 -0
- package/lib/beta/agents/result-collection.d.ts +15 -0
- package/lib/beta/agents/result-collection.d.ts.map +1 -0
- package/lib/beta/agents/result-collection.js +110 -0
- package/lib/beta/agents/result-collection.js.map +1 -0
- package/lib/beta/agents/result-collection.mjs +106 -0
- package/lib/beta/agents/result-collection.mjs.map +1 -0
- package/lib/beta/agents/tool-output.d.mts +7 -0
- package/lib/beta/agents/tool-output.d.mts.map +1 -0
- package/lib/beta/agents/tool-output.d.ts +7 -0
- package/lib/beta/agents/tool-output.d.ts.map +1 -0
- package/lib/beta/agents/tool-output.js +26 -0
- package/lib/beta/agents/tool-output.js.map +1 -0
- package/lib/beta/agents/tool-output.mjs +23 -0
- package/lib/beta/agents/tool-output.mjs.map +1 -0
- package/lib/responses/responses-websocket-session.d.mts +1 -1
- package/lib/responses/responses-websocket-session.d.mts.map +1 -1
- package/lib/responses/responses-websocket-session.d.ts +1 -1
- package/lib/responses/responses-websocket-session.d.ts.map +1 -1
- package/lib/responses/responses-websocket-session.js +12 -2
- package/lib/responses/responses-websocket-session.js.map +1 -1
- package/lib/responses/responses-websocket-session.mjs +12 -2
- package/lib/responses/responses-websocket-session.mjs.map +1 -1
- package/package.json +1 -1
- package/resources/beta/agents/agents.d.mts +9 -0
- package/resources/beta/agents/agents.d.mts.map +1 -1
- package/resources/beta/agents/agents.d.ts +9 -0
- package/resources/beta/agents/agents.d.ts.map +1 -1
- package/resources/beta/agents/agents.js.map +1 -1
- package/resources/beta/agents/agents.mjs.map +1 -1
- package/resources/beta/agents/environments/files.d.mts +12 -0
- package/resources/beta/agents/environments/files.d.mts.map +1 -1
- package/resources/beta/agents/environments/files.d.ts +12 -0
- package/resources/beta/agents/environments/files.d.ts.map +1 -1
- package/resources/beta/agents/environments/files.js +9 -0
- package/resources/beta/agents/environments/files.js.map +1 -1
- package/resources/beta/agents/environments/files.mjs +9 -0
- package/resources/beta/agents/environments/files.mjs.map +1 -1
- package/resources/beta/agents/sessions/artifacts.d.mts +4 -0
- package/resources/beta/agents/sessions/artifacts.d.mts.map +1 -1
- package/resources/beta/agents/sessions/artifacts.d.ts +4 -0
- package/resources/beta/agents/sessions/artifacts.d.ts.map +1 -1
- package/resources/beta/agents/sessions/artifacts.js +5 -0
- package/resources/beta/agents/sessions/artifacts.js.map +1 -1
- package/resources/beta/agents/sessions/artifacts.mjs +5 -0
- package/resources/beta/agents/sessions/artifacts.mjs.map +1 -1
- package/resources/beta/agents/sessions/events.d.mts +6 -5
- package/resources/beta/agents/sessions/events.d.mts.map +1 -1
- package/resources/beta/agents/sessions/events.d.ts +6 -5
- package/resources/beta/agents/sessions/events.d.ts.map +1 -1
- package/resources/beta/agents/sessions/events.js +6 -5
- package/resources/beta/agents/sessions/events.js.map +1 -1
- package/resources/beta/agents/sessions/events.mjs +6 -5
- package/resources/beta/agents/sessions/events.mjs.map +1 -1
- package/resources/beta/agents/sessions/index.d.mts +1 -0
- package/resources/beta/agents/sessions/index.d.mts.map +1 -1
- package/resources/beta/agents/sessions/index.d.ts +1 -0
- package/resources/beta/agents/sessions/index.d.ts.map +1 -1
- package/resources/beta/agents/sessions/index.js +3 -1
- package/resources/beta/agents/sessions/index.js.map +1 -1
- package/resources/beta/agents/sessions/index.mjs +1 -0
- package/resources/beta/agents/sessions/index.mjs.map +1 -1
- package/resources/beta/agents/sessions/sessions.d.mts +16 -4
- package/resources/beta/agents/sessions/sessions.d.mts.map +1 -1
- package/resources/beta/agents/sessions/sessions.d.ts +16 -4
- package/resources/beta/agents/sessions/sessions.d.ts.map +1 -1
- package/resources/beta/agents/sessions/sessions.js +17 -4
- package/resources/beta/agents/sessions/sessions.js.map +1 -1
- package/resources/beta/agents/sessions/sessions.mjs +17 -4
- package/resources/beta/agents/sessions/sessions.mjs.map +1 -1
- package/resources/beta/agents/sessions/traces.d.mts +68 -0
- package/resources/beta/agents/sessions/traces.d.mts.map +1 -0
- package/resources/beta/agents/sessions/traces.d.ts +68 -0
- package/resources/beta/agents/sessions/traces.d.ts.map +1 -0
- package/resources/beta/agents/sessions/traces.js +81 -0
- package/resources/beta/agents/sessions/traces.js.map +1 -0
- package/resources/beta/agents/sessions/traces.mjs +77 -0
- package/resources/beta/agents/sessions/traces.mjs.map +1 -0
- package/resources/beta/responses/ws-base.d.mts +3 -1
- package/resources/beta/responses/ws-base.d.mts.map +1 -1
- package/resources/beta/responses/ws-base.d.ts +3 -1
- package/resources/beta/responses/ws-base.d.ts.map +1 -1
- package/resources/beta/responses/ws-base.js +35 -6
- package/resources/beta/responses/ws-base.js.map +1 -1
- package/resources/beta/responses/ws-base.mjs +35 -6
- package/resources/beta/responses/ws-base.mjs.map +1 -1
- package/resources/beta/responses/ws.d.mts +10 -0
- package/resources/beta/responses/ws.d.mts.map +1 -1
- package/resources/beta/responses/ws.d.ts +10 -0
- package/resources/beta/responses/ws.d.ts.map +1 -1
- package/resources/beta/responses/ws.js +23 -22
- package/resources/beta/responses/ws.js.map +1 -1
- package/resources/beta/responses/ws.mjs +24 -23
- package/resources/beta/responses/ws.mjs.map +1 -1
- package/resources/chat/completions/completions.d.mts +1 -1
- package/resources/chat/completions/completions.d.mts.map +1 -1
- package/resources/chat/completions/completions.d.ts +1 -1
- package/resources/chat/completions/completions.d.ts.map +1 -1
- package/resources/evals/runs/runs.js +1 -1
- package/resources/evals/runs/runs.js.map +1 -1
- package/resources/evals/runs/runs.mjs +1 -1
- package/resources/evals/runs/runs.mjs.map +1 -1
- package/resources/live/forks/forks.d.mts +5 -1
- package/resources/live/forks/forks.d.mts.map +1 -1
- package/resources/live/forks/forks.d.ts +5 -1
- package/resources/live/forks/forks.d.ts.map +1 -1
- package/resources/live/live.d.mts +5 -1
- package/resources/live/live.d.mts.map +1 -1
- package/resources/live/live.d.ts +5 -1
- package/resources/live/live.d.ts.map +1 -1
- package/resources/live/live.js.map +1 -1
- package/resources/live/live.mjs.map +1 -1
- package/resources/responses/ws-base.d.mts +3 -1
- package/resources/responses/ws-base.d.mts.map +1 -1
- package/resources/responses/ws-base.d.ts +3 -1
- package/resources/responses/ws-base.d.ts.map +1 -1
- package/resources/responses/ws-base.js +35 -6
- package/resources/responses/ws-base.js.map +1 -1
- package/resources/responses/ws-base.mjs +35 -6
- package/resources/responses/ws-base.mjs.map +1 -1
- package/resources/responses/ws.d.mts +10 -0
- package/resources/responses/ws.d.mts.map +1 -1
- package/resources/responses/ws.d.ts +10 -0
- package/resources/responses/ws.d.ts.map +1 -1
- package/resources/responses/ws.js +23 -22
- package/resources/responses/ws.js.map +1 -1
- package/resources/responses/ws.mjs +24 -23
- package/resources/responses/ws.mjs.map +1 -1
- package/src/client.ts +55 -19
- package/src/core/streaming.ts +9 -0
- package/src/helpers/beta/agents/filesystem.ts +187 -0
- package/src/helpers/beta/agents/standard-schema.ts +13 -0
- package/src/helpers/beta/agents/zod.ts +10 -0
- package/src/internal/realtime-credentials.ts +70 -4
- package/src/internal/responses-ws-credentials.ts +160 -0
- package/src/internal/shim-types.ts +17 -0
- package/src/internal/ws-adapter-node.ts +66 -0
- package/src/internal/ws.ts +139 -11
- package/src/lib/agents/agent-session-stream.ts +36 -24
- package/src/lib/beta/agents/agent-session-create-stream.ts +36 -0
- package/src/lib/beta/agents/agent-turn-result-collector.ts +122 -0
- package/src/lib/beta/agents/agent-turn-result-error.ts +33 -0
- package/src/lib/beta/agents/agent-turn-result.ts +22 -0
- package/src/lib/beta/agents/files.ts +119 -0
- package/src/lib/beta/agents/function-tool.ts +60 -0
- package/src/lib/beta/agents/output-format-types.ts +12 -0
- package/src/lib/beta/agents/output-format.ts +20 -0
- package/src/lib/beta/agents/output-parse-error.ts +16 -0
- package/src/lib/beta/agents/pages.ts +32 -0
- package/src/lib/beta/agents/parse-result.ts +133 -0
- package/src/lib/beta/agents/parsed-agent-turn-result.ts +12 -0
- package/src/lib/beta/agents/result-artifacts.ts +76 -0
- package/src/lib/beta/agents/result-collection.ts +115 -0
- package/src/lib/beta/agents/tool-output.ts +22 -0
- package/src/lib/responses/responses-websocket-session.ts +14 -3
- package/src/resources/beta/agents/agents.ts +11 -0
- package/src/resources/beta/agents/environments/files.ts +16 -0
- package/src/resources/beta/agents/sessions/artifacts.ts +7 -0
- package/src/resources/beta/agents/sessions/events.ts +6 -5
- package/src/resources/beta/agents/sessions/index.ts +1 -0
- package/src/resources/beta/agents/sessions/sessions.ts +48 -18
- package/src/resources/beta/agents/sessions/traces.ts +298 -0
- package/src/resources/beta/responses/ws-base.ts +40 -6
- package/src/resources/beta/responses/ws.ts +37 -27
- package/src/resources/chat/completions/completions.ts +1 -1
- package/src/resources/evals/runs/runs.ts +1 -1
- package/src/resources/live/forks/forks.ts +6 -1
- package/src/resources/live/live.ts +6 -1
- package/src/resources/responses/ws-base.ts +40 -6
- package/src/resources/responses/ws.ts +37 -27
- package/src/tsconfig.dist-src-node.d.ts +7 -0
- package/src/tsconfig.dist-src.d.ts +3 -1
- package/src/version.ts +1 -1
- package/version.d.mts +1 -1
- package/version.d.ts +1 -1
- package/version.js +1 -1
- package/version.mjs +1 -1
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AgentSession,
|
|
3
|
+
AgentSessionEvent,
|
|
4
|
+
AgentSessionAssistantMessage,
|
|
5
|
+
} from '../../../resources/beta/agents/agents';
|
|
6
|
+
import type { Turn } from '../../../resources/beta/agents/sessions/turns';
|
|
7
|
+
import { AgentTurnResult } from './agent-turn-result';
|
|
8
|
+
import { AgentTurnResultError } from './agent-turn-result-error';
|
|
9
|
+
|
|
10
|
+
/** Accumulates completed items independently of transport and tool execution.
|
|
11
|
+
* @internal
|
|
12
|
+
*/
|
|
13
|
+
export class AgentTurnResultCollector {
|
|
14
|
+
#sessionID: string | undefined;
|
|
15
|
+
#turn: Turn | undefined;
|
|
16
|
+
#messages = new Map<string, { index: number; message: AgentSessionAssistantMessage }>();
|
|
17
|
+
#requiredActions: AgentSession['required_actions'] = [];
|
|
18
|
+
#sessionFailed = false;
|
|
19
|
+
#terminal = false;
|
|
20
|
+
#idle = false;
|
|
21
|
+
|
|
22
|
+
constructor(sessionID?: string) {
|
|
23
|
+
this.#sessionID = sessionID;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
accept(event: AgentSessionEvent): void {
|
|
27
|
+
if (this.ready) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
if ('session' in event) {
|
|
31
|
+
this.#sessionID ??= event.session.id;
|
|
32
|
+
this.#requiredActions = structuredClone(event.session.required_actions ?? []);
|
|
33
|
+
this.#sessionFailed ||= event.type === 'agent.session.failed';
|
|
34
|
+
this.#idle ||= event.type === 'agent.session.idle' && this.#terminal;
|
|
35
|
+
}
|
|
36
|
+
if (event.type === 'agent.session.turn.created' && event.turn.subagent_id === null && !this.#turn) {
|
|
37
|
+
this.#turn = structuredClone(event.turn);
|
|
38
|
+
this.#sessionID = event.turn.session_id;
|
|
39
|
+
}
|
|
40
|
+
let turnID = 'turn_id' in event ? event.turn_id : undefined;
|
|
41
|
+
if ('item' in event) {
|
|
42
|
+
turnID = event.item.turn_id;
|
|
43
|
+
}
|
|
44
|
+
if (!this.#turn || turnID !== this.#turn.id) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
this.#acceptTurn(event);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
#acceptTurn(event: AgentSessionEvent): void {
|
|
51
|
+
if ('turn' in event) {
|
|
52
|
+
this.#turn = structuredClone(event.turn);
|
|
53
|
+
this.#terminal ||=
|
|
54
|
+
event.type === 'agent.session.turn.completed' ||
|
|
55
|
+
event.type === 'agent.session.turn.failed' ||
|
|
56
|
+
event.type === 'agent.session.turn.cancelled';
|
|
57
|
+
}
|
|
58
|
+
this.#acceptOutput(event);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
#acceptOutput(event: AgentSessionEvent): void {
|
|
62
|
+
if (
|
|
63
|
+
event.type !== 'agent.session.turn.item.done' ||
|
|
64
|
+
event.item.type !== 'message' ||
|
|
65
|
+
event.item.status !== 'completed' ||
|
|
66
|
+
event.item.phase === 'commentary' ||
|
|
67
|
+
this.#messages.has(event.item.id)
|
|
68
|
+
) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
this.#messages.set(event.item.id, { index: event.output_index, message: structuredClone(event.item) });
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
#finalMessages(): AgentSessionAssistantMessage[] {
|
|
75
|
+
return (
|
|
76
|
+
[...this.#messages.values()]
|
|
77
|
+
// oxlint-disable-next-line unicorn/no-array-sort -- Sort a fresh array; ES2020 declarations do not include toSorted.
|
|
78
|
+
.sort((a, b) => a.index - b.index)
|
|
79
|
+
.map(({ message }) => message)
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
error(reason: AgentTurnResultError['reason'], cause?: unknown): AgentTurnResultError {
|
|
84
|
+
return new AgentTurnResultError(
|
|
85
|
+
reason,
|
|
86
|
+
this.#sessionID,
|
|
87
|
+
this.#turn,
|
|
88
|
+
this.#finalMessages(),
|
|
89
|
+
this.#requiredActions,
|
|
90
|
+
cause,
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
checkAction(canHandle: (name: string) => boolean): void {
|
|
95
|
+
if (this.#sessionFailed || this.#turn?.status === 'failed') {
|
|
96
|
+
throw this.error('failed');
|
|
97
|
+
}
|
|
98
|
+
if (this.#turn?.status === 'cancelled') {
|
|
99
|
+
throw this.error('cancelled');
|
|
100
|
+
}
|
|
101
|
+
if (this.#requiredActions.some((action) => action.type !== 'function_call' || !canHandle(action.name))) {
|
|
102
|
+
throw this.error('requires_action');
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
get ready(): boolean {
|
|
107
|
+
return this.#terminal && this.#idle;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
release(): void {
|
|
111
|
+
this.#messages.clear();
|
|
112
|
+
this.#requiredActions = [];
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
finish(): AgentTurnResult {
|
|
116
|
+
this.checkAction(() => this.ready);
|
|
117
|
+
if (!this.#terminal || this.#turn?.status !== 'completed' || !this.#idle) {
|
|
118
|
+
throw this.error('observation');
|
|
119
|
+
}
|
|
120
|
+
return new AgentTurnResult(this.#turn, this.#finalMessages());
|
|
121
|
+
}
|
|
122
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { OpenAIError } from '../../../core/error';
|
|
2
|
+
import type { AgentSession, AgentSessionAssistantMessage } from '../../../resources/beta/agents/agents';
|
|
3
|
+
import type { Turn } from '../../../resources/beta/agents/sessions/turns';
|
|
4
|
+
|
|
5
|
+
/** Beta: final collection failed; partial state is evidence, not a successful result. */
|
|
6
|
+
export class AgentTurnResultError extends OpenAIError {
|
|
7
|
+
override name = 'AgentTurnResultError';
|
|
8
|
+
readonly reason: 'failed' | 'cancelled' | 'requires_action' | 'observation';
|
|
9
|
+
readonly session_id: string | undefined;
|
|
10
|
+
readonly turn: Turn | undefined;
|
|
11
|
+
readonly messages: AgentSessionAssistantMessage[];
|
|
12
|
+
readonly required_actions: AgentSession['required_actions'];
|
|
13
|
+
readonly cause: unknown;
|
|
14
|
+
constructor(
|
|
15
|
+
reason: 'failed' | 'cancelled' | 'requires_action' | 'observation',
|
|
16
|
+
session_id: string | undefined,
|
|
17
|
+
turn: Turn | undefined,
|
|
18
|
+
messages: AgentSessionAssistantMessage[],
|
|
19
|
+
required_actions: AgentSession['required_actions'] = [],
|
|
20
|
+
cause?: unknown,
|
|
21
|
+
) {
|
|
22
|
+
super(`Could not collect the agent turn result: ${reason}`);
|
|
23
|
+
this.reason = reason;
|
|
24
|
+
this.session_id = session_id;
|
|
25
|
+
this.turn = turn;
|
|
26
|
+
this.messages = messages;
|
|
27
|
+
this.required_actions = required_actions;
|
|
28
|
+
this.cause = cause;
|
|
29
|
+
}
|
|
30
|
+
get turn_id(): string | undefined {
|
|
31
|
+
return this.turn?.id;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { AgentSessionAssistantMessage } from '../../../resources/beta/agents/agents';
|
|
2
|
+
import type { Turn } from '../../../resources/beta/agents/sessions/turns';
|
|
3
|
+
import { outputText } from '../../agents/output-text';
|
|
4
|
+
|
|
5
|
+
/** Beta: the completed final assistant messages from one hosted root turn. */
|
|
6
|
+
export class AgentTurnResult {
|
|
7
|
+
readonly turn: Turn;
|
|
8
|
+
readonly messages: AgentSessionAssistantMessage[];
|
|
9
|
+
constructor(turn: Turn, messages: AgentSessionAssistantMessage[]) {
|
|
10
|
+
this.turn = turn;
|
|
11
|
+
this.messages = messages;
|
|
12
|
+
}
|
|
13
|
+
get session_id(): string {
|
|
14
|
+
return this.turn.session_id;
|
|
15
|
+
}
|
|
16
|
+
get turn_id(): string {
|
|
17
|
+
return this.turn.id;
|
|
18
|
+
}
|
|
19
|
+
get output_text(): string {
|
|
20
|
+
return this.messages.map(outputText).join('');
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import type { OpenAI } from '../../../client';
|
|
2
|
+
import { OpenAIError } from '../../../core/error';
|
|
3
|
+
import { buildHeaders } from '../../../internal/headers';
|
|
4
|
+
import type { RequestOptions } from '../../../internal/request-options';
|
|
5
|
+
import type { Uploadable } from '../../../internal/uploads';
|
|
6
|
+
import type { FileObject } from '../../../resources/files';
|
|
7
|
+
import type {
|
|
8
|
+
EnvironmentFile,
|
|
9
|
+
FileCreateParams,
|
|
10
|
+
Files,
|
|
11
|
+
} from '../../../resources/beta/agents/environments/files';
|
|
12
|
+
|
|
13
|
+
/** Beta: file references ready for a hosted environment, plus caller-owned uploads. */
|
|
14
|
+
export interface PreparedAgentFiles {
|
|
15
|
+
files: Extract<FileCreateParams, { type: 'file_id' }>[];
|
|
16
|
+
uploadedFiles: FileObject[];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Beta: uploaded file IDs remain available after partial preparation or staging failure. */
|
|
20
|
+
export class AgentFileUploadError extends OpenAIError {
|
|
21
|
+
override readonly name = 'AgentFileUploadError';
|
|
22
|
+
declare readonly cause: unknown;
|
|
23
|
+
readonly uploadedFiles: FileObject[];
|
|
24
|
+
constructor(uploadedFiles: FileObject[], cause: unknown) {
|
|
25
|
+
super('Agent file preparation or staging failed; uploaded files remain caller-owned.');
|
|
26
|
+
this.uploadedFiles = uploadedFiles;
|
|
27
|
+
Object.defineProperty(this, 'cause', { value: cause, configurable: true });
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** @internal */
|
|
32
|
+
export function validateAgentFilePath(path: string): void {
|
|
33
|
+
const parts = path.split('/');
|
|
34
|
+
const root = parts[2] ?? '';
|
|
35
|
+
if (
|
|
36
|
+
!path.startsWith('/workspace/') ||
|
|
37
|
+
path.includes('\\') ||
|
|
38
|
+
path.includes('\0') ||
|
|
39
|
+
parts.slice(1).some((part) => part === '' || part === '.' || part === '..') ||
|
|
40
|
+
root === '.codex' ||
|
|
41
|
+
root === '.managed-agents' ||
|
|
42
|
+
root.startsWith('.managed-agents-') ||
|
|
43
|
+
path === '/workspace/outputs'
|
|
44
|
+
) {
|
|
45
|
+
throw new OpenAIError('Agent files require a non-reserved absolute file path inside /workspace');
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function preflight(files: Record<string, Uploadable>, options: RequestOptions): [string, Uploadable][] {
|
|
50
|
+
const entries = Object.entries(files);
|
|
51
|
+
const headers = buildHeaders([options.headers]);
|
|
52
|
+
if (
|
|
53
|
+
entries.length > 1 &&
|
|
54
|
+
(headers.values.has('idempotency-key') ||
|
|
55
|
+
(!headers.nulls.has('idempotency-key') && options?.idempotencyKey !== undefined))
|
|
56
|
+
) {
|
|
57
|
+
throw new OpenAIError('Do not reuse an Idempotency-Key across multiple file uploads');
|
|
58
|
+
}
|
|
59
|
+
const paths = new Set(entries.map(([path]) => path));
|
|
60
|
+
for (const [path] of entries) {
|
|
61
|
+
validateAgentFilePath(path);
|
|
62
|
+
for (let slash = path.lastIndexOf('/'); slash > 0; slash = path.lastIndexOf('/', slash - 1)) {
|
|
63
|
+
if (paths.has(path.slice(0, slash))) {
|
|
64
|
+
throw new OpenAIError('Agent file destinations conflict');
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return entries;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** @internal */
|
|
72
|
+
export async function prepareAgentFiles(
|
|
73
|
+
client: OpenAI,
|
|
74
|
+
files: Record<string, Uploadable>,
|
|
75
|
+
options?: RequestOptions,
|
|
76
|
+
): Promise<PreparedAgentFiles> {
|
|
77
|
+
const requestOptions = { ...options };
|
|
78
|
+
// Internal SDK read: capture effective headers once, including defaults and explicit omissions.
|
|
79
|
+
const headers = buildHeaders([client['_options'].defaultHeaders, requestOptions.headers]);
|
|
80
|
+
requestOptions.headers = headers;
|
|
81
|
+
const entries = preflight(files, requestOptions);
|
|
82
|
+
if (entries.length > 1) {
|
|
83
|
+
// Keep a later mutation of client defaults from adding one key to this entire batch.
|
|
84
|
+
headers.nulls.add('idempotency-key');
|
|
85
|
+
}
|
|
86
|
+
const prepared: PreparedAgentFiles = { files: [], uploadedFiles: [] };
|
|
87
|
+
try {
|
|
88
|
+
for (const [path, file] of entries) {
|
|
89
|
+
// oxlint-disable-next-line no-await-in-loop -- Stop on the first failure and expose precisely the uploads already created.
|
|
90
|
+
const uploaded = await client.files.create({ file, purpose: 'user_data' }, requestOptions);
|
|
91
|
+
prepared.uploadedFiles.push(uploaded);
|
|
92
|
+
prepared.files.push({ type: 'file_id', file_id: uploaded.id, path });
|
|
93
|
+
}
|
|
94
|
+
return prepared;
|
|
95
|
+
} catch (error) {
|
|
96
|
+
throw new AgentFileUploadError(prepared.uploadedFiles, error);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** @internal */
|
|
101
|
+
export async function uploadAgentFile(
|
|
102
|
+
client: OpenAI,
|
|
103
|
+
resource: Files,
|
|
104
|
+
environmentID: string,
|
|
105
|
+
params: { file: Uploadable; path: string },
|
|
106
|
+
options?: RequestOptions,
|
|
107
|
+
): Promise<{ uploadedFile: FileObject; environmentFile: EnvironmentFile }> {
|
|
108
|
+
const prepared = await prepareAgentFiles(client, { [params.path]: params.file }, options);
|
|
109
|
+
const [reference] = prepared.files;
|
|
110
|
+
const [uploadedFile] = prepared.uploadedFiles;
|
|
111
|
+
if (!reference || !uploadedFile) {
|
|
112
|
+
throw new OpenAIError('Missing prepared agent file');
|
|
113
|
+
}
|
|
114
|
+
try {
|
|
115
|
+
return { uploadedFile, environmentFile: await resource.create(environmentID, reference, options) };
|
|
116
|
+
} catch (error) {
|
|
117
|
+
throw new AgentFileUploadError(prepared.uploadedFiles, error);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { OpenAIError } from '../../../core/error';
|
|
2
|
+
import type { AgentToolHandler } from '../../agents/agent-session-stream';
|
|
3
|
+
import type { AutoParseableResponseTool } from '../../ResponsesParser';
|
|
4
|
+
import type { AgentToolParam } from '../../../resources/beta/agents/agents';
|
|
5
|
+
import { isInputContent } from './tool-output';
|
|
6
|
+
|
|
7
|
+
/** A beta Agents function definition paired with its local, validating handler. */
|
|
8
|
+
export interface AgentFunctionTool {
|
|
9
|
+
/** Model-visible function name, also used as the key in `toolHandlers`. */
|
|
10
|
+
readonly name: string;
|
|
11
|
+
/** Hosted configuration for `agent.tools`; contains no local callback or parser. */
|
|
12
|
+
readonly definition: AgentToolParam.AgentToolConfigParamFunction;
|
|
13
|
+
/** Validates arguments before invoking the callback through the existing stream dispatcher. */
|
|
14
|
+
readonly handler: AgentToolHandler;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Adapts a `zodResponsesFunction` or `standardResponsesFunction` to beta Agents.
|
|
19
|
+
* Pass `definition` to `agent.tools` and register `handler` under `name` in
|
|
20
|
+
* `sessions.stream()`'s `toolHandlers`. Creating a definition does not execute it.
|
|
21
|
+
* The existing dispatcher handles callback results, failures, and submission retries.
|
|
22
|
+
* Scalar results and business-data arrays become JSON text; `undefined` becomes
|
|
23
|
+
* the text `undefined`. Supported text/image content arrays retain their format.
|
|
24
|
+
*
|
|
25
|
+
* @throws {OpenAIError} If the tool has no callback or argument schema.
|
|
26
|
+
*/
|
|
27
|
+
export function functionTool<Arguments>(
|
|
28
|
+
tool: AutoParseableResponseTool<{ name: string; arguments: Arguments }>,
|
|
29
|
+
): AgentFunctionTool {
|
|
30
|
+
const execute = tool.$callback;
|
|
31
|
+
const parse = tool.$parseRaw;
|
|
32
|
+
if (!execute || !tool.parameters) {
|
|
33
|
+
throw new OpenAIError('Agents function tools require a callback and argument schema');
|
|
34
|
+
}
|
|
35
|
+
return {
|
|
36
|
+
name: tool.name,
|
|
37
|
+
definition: {
|
|
38
|
+
type: 'function',
|
|
39
|
+
name: tool.name,
|
|
40
|
+
description: tool.description ?? '',
|
|
41
|
+
parameters: tool.parameters,
|
|
42
|
+
...(tool.defer_loading === undefined ? {} : { defer_loading: tool.defer_loading }),
|
|
43
|
+
},
|
|
44
|
+
handler: async (arguments_) => {
|
|
45
|
+
const result: unknown = await execute(parse(JSON.stringify(arguments_)));
|
|
46
|
+
if (Array.isArray(result)) {
|
|
47
|
+
return result.length > 0 && result.every(isInputContent) ? result : JSON.stringify(result);
|
|
48
|
+
}
|
|
49
|
+
// oxlint-disable-next-line anti-slop/no-runtime-typeof -- Existing schema-tool callbacks return arbitrary application values; adapt them to the Agents dispatcher's output contract.
|
|
50
|
+
if (typeof result === 'object' || typeof result === 'string') {
|
|
51
|
+
return result;
|
|
52
|
+
}
|
|
53
|
+
const text = result === undefined ? 'undefined' : JSON.stringify(result);
|
|
54
|
+
if (text === undefined) {
|
|
55
|
+
throw new OpenAIError('Tool output must be JSON serializable');
|
|
56
|
+
}
|
|
57
|
+
return text;
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { TextFormatParam } from '../../../resources/beta/agents/agents';
|
|
2
|
+
import type { AgentTurnResult } from './agent-turn-result';
|
|
3
|
+
import type { ParsedAgentTurnResult } from './parsed-agent-turn-result';
|
|
4
|
+
|
|
5
|
+
/** Beta: an Agents JSON Schema format with a local output validator. */
|
|
6
|
+
export interface AgentOutputFormat<T> extends TextFormatParam.TextFormatParamJSONSchema {
|
|
7
|
+
/** SDK-only parser; omitted from serialized requests. */
|
|
8
|
+
$parseRaw: (text: string) => T;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** Beta: raw results remain unchanged unless a typed format is supplied. */
|
|
12
|
+
export type AgentResult<T> = [T] extends [never] ? AgentTurnResult : ParsedAgentTurnResult<T>;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { AgentOutputFormat } from './output-format-types';
|
|
2
|
+
|
|
3
|
+
import type { JSONSchema } from '../../jsonschema';
|
|
4
|
+
|
|
5
|
+
export type { AgentOutputFormat, AgentResult } from './output-format-types';
|
|
6
|
+
|
|
7
|
+
export { ParsedAgentTurnResult } from './parsed-agent-turn-result';
|
|
8
|
+
export { AgentOutputParseError } from './output-parse-error';
|
|
9
|
+
|
|
10
|
+
/** Beta: bind a JSON schema to its local output parser; the API validates schema support. */
|
|
11
|
+
export function agentOutputFormat<T>(schema: JSONSchema, parse: (text: string) => T): AgentOutputFormat<T> {
|
|
12
|
+
// Keep spread-compatible parsing without exposing caller-owned serialization hooks.
|
|
13
|
+
const parser = Object.defineProperty((text: string) => parse(text), 'toJSON', {
|
|
14
|
+
value: () => {
|
|
15
|
+
/* Parser metadata is omitted from JSON requests. */
|
|
16
|
+
},
|
|
17
|
+
});
|
|
18
|
+
// SAFETY: The API schema type exposes JSON Schema keywords as a record; preserve the supplied schema unchanged.
|
|
19
|
+
return { type: 'json_schema', schema: schema as AgentOutputFormat<T>['schema'], $parseRaw: parser };
|
|
20
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { OpenAIError } from '../../../core/error';
|
|
2
|
+
import type { AgentTurnResult } from './agent-turn-result';
|
|
3
|
+
|
|
4
|
+
/** Beta: hosted execution completed, but local output parsing failed. */
|
|
5
|
+
export class AgentOutputParseError extends OpenAIError {
|
|
6
|
+
override name = 'AgentOutputParseError';
|
|
7
|
+
readonly #rawResult: AgentTurnResult;
|
|
8
|
+
constructor(result: AgentTurnResult) {
|
|
9
|
+
super('The completed agent output could not be parsed');
|
|
10
|
+
this.#rawResult = result;
|
|
11
|
+
}
|
|
12
|
+
/** Inspect the completed output explicitly; ordinary error logging omits it. */
|
|
13
|
+
get raw_result(): AgentTurnResult {
|
|
14
|
+
return this.#rawResult;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { OpenAIError } from '../../../core/error';
|
|
2
|
+
import type { CursorPage } from '../../../core/pagination';
|
|
3
|
+
import { isObj } from '../../../internal/utils/values';
|
|
4
|
+
|
|
5
|
+
/** Retain server cursor metadata for legacy Agents items with nullable IDs.
|
|
6
|
+
* @internal
|
|
7
|
+
* @yields {T} Each item in server order, failing if pagination cannot advance.
|
|
8
|
+
*/
|
|
9
|
+
export async function* agentItems<T extends { id?: string | null }>(
|
|
10
|
+
load: (after: string | undefined) => PromiseLike<CursorPage<T>>,
|
|
11
|
+
): AsyncGenerator<T, void, undefined> {
|
|
12
|
+
let after: string | undefined;
|
|
13
|
+
while (true) {
|
|
14
|
+
// oxlint-disable-next-line no-await-in-loop -- Each page determines the next cursor.
|
|
15
|
+
const page = await load(after);
|
|
16
|
+
yield* page.data;
|
|
17
|
+
if (!page.has_more) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
// Internal SDK read: CursorPage keeps raw metadata but does not expose last_id.
|
|
21
|
+
// oxlint-disable-next-line prefer-destructuring -- Bracket access intentionally reads a protected internal field.
|
|
22
|
+
const body = page['body'];
|
|
23
|
+
// oxlint-disable-next-line unicorn/prefer-at -- Keep published source compatible with ES2020 declarations.
|
|
24
|
+
const last = page.data[page.data.length - 1];
|
|
25
|
+
// oxlint-disable-next-line anti-slop/no-runtime-typeof -- Validate only the opaque cursor from response metadata.
|
|
26
|
+
const cursor = isObj(body) && typeof body['last_id'] === 'string' ? body['last_id'] : last?.id;
|
|
27
|
+
if (!cursor || cursor === after) {
|
|
28
|
+
throw new OpenAIError('Agent pagination cannot advance to the next page');
|
|
29
|
+
}
|
|
30
|
+
after = cursor;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import type { RequestOptions } from '../../../internal/request-options';
|
|
2
|
+
import { OpenAIError } from '../../../core/error';
|
|
3
|
+
import type { SessionCreateParams } from '../../../resources/beta/agents/sessions/sessions';
|
|
4
|
+
import type { AgentTurnResult } from './agent-turn-result';
|
|
5
|
+
import type { TextFormatParam } from '../../../resources/beta/agents/agents';
|
|
6
|
+
import type { AgentOutputFormat, AgentResult } from './output-format-types';
|
|
7
|
+
import { ParsedAgentTurnResult } from './parsed-agent-turn-result';
|
|
8
|
+
import { AgentOutputParseError } from './output-parse-error';
|
|
9
|
+
|
|
10
|
+
/** @internal */
|
|
11
|
+
export function parseAgentResult<T>(result: AgentTurnResult, format?: AgentOutputFormat<T>): AgentResult<T> {
|
|
12
|
+
try {
|
|
13
|
+
if (!format) {
|
|
14
|
+
// SAFETY: Absent formats use the default never/raw overload.
|
|
15
|
+
return result as AgentResult<T>;
|
|
16
|
+
}
|
|
17
|
+
let first: { value: T } | undefined;
|
|
18
|
+
for (const message of result.messages) {
|
|
19
|
+
for (const content of message.content) {
|
|
20
|
+
if (content.type === 'output_text') {
|
|
21
|
+
const value = format.$parseRaw(content.text);
|
|
22
|
+
first ??= { value };
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
if (!first) {
|
|
27
|
+
throw new AgentOutputParseError(result);
|
|
28
|
+
}
|
|
29
|
+
// SAFETY: Callers preserve the format's inferred T.
|
|
30
|
+
return new ParsedAgentTurnResult(result, first.value) as AgentResult<T>;
|
|
31
|
+
} catch {
|
|
32
|
+
throw new AgentOutputParseError(result);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Inspect only JSON-visible data fields; leave getters and inherited values to normal serialization.
|
|
37
|
+
function ownJSONValue<T extends object, K extends keyof T>(
|
|
38
|
+
object: T | null | undefined,
|
|
39
|
+
key: K,
|
|
40
|
+
): T[K] | undefined {
|
|
41
|
+
const descriptor = object && Object.getOwnPropertyDescriptor(object, key);
|
|
42
|
+
// SAFETY: The own data descriptor corresponds to the requested property in T.
|
|
43
|
+
return descriptor?.enumerable && 'value' in descriptor ? (descriptor.value as T[K]) : undefined;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** @internal */
|
|
47
|
+
export function agentFormatParser<T = unknown>(
|
|
48
|
+
format: TextFormatParam | null | undefined,
|
|
49
|
+
): AgentOutputFormat<T> | undefined {
|
|
50
|
+
if (!format) {
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
const descriptor = Object.getOwnPropertyDescriptor(format, '$parseRaw');
|
|
54
|
+
// oxlint-disable-next-line anti-slop/no-runtime-typeof -- An own parser data property is the explicit opt-in marker.
|
|
55
|
+
if (!descriptor || !('value' in descriptor) || typeof descriptor.value !== 'function') {
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
// SAFETY: Inspect the schema data descriptor without evaluating a caller getter; validate its discriminator below.
|
|
59
|
+
const schema = ownJSONValue(format as TextFormatParam.TextFormatParamJSONSchema, 'schema');
|
|
60
|
+
if (ownJSONValue(format, 'type') !== 'json_schema' || !schema) {
|
|
61
|
+
throw new OpenAIError('Typed agent formats require own enumerable type and schema data properties');
|
|
62
|
+
}
|
|
63
|
+
// SAFETY: The own descriptor was checked for a callable parser; retain its receiver and never reread the property.
|
|
64
|
+
const parse = descriptor.value as AgentOutputFormat<T>['$parseRaw'];
|
|
65
|
+
return { type: 'json_schema', schema, $parseRaw: (text) => parse.call(format, text) };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Materialize JSON-visible envelope getters once without evaluating unrelated properties.
|
|
69
|
+
function snapshotJSONProperty<T extends object, K extends keyof T>(
|
|
70
|
+
object: T,
|
|
71
|
+
key: K,
|
|
72
|
+
capture: (value: T[K]) => T[K] = (value) => value,
|
|
73
|
+
): T {
|
|
74
|
+
const descriptor = Object.getOwnPropertyDescriptor(object, key);
|
|
75
|
+
if (!descriptor?.enumerable) {
|
|
76
|
+
return object;
|
|
77
|
+
}
|
|
78
|
+
const value = capture(object[key]);
|
|
79
|
+
if ('value' in descriptor && value === descriptor.value) {
|
|
80
|
+
return object;
|
|
81
|
+
}
|
|
82
|
+
// SAFETY: Preserve the original properties/prototype, replacing only this captured property's value.
|
|
83
|
+
return Object.create(Object.getPrototypeOf(object), {
|
|
84
|
+
...Object.getOwnPropertyDescriptors(object),
|
|
85
|
+
[key]: { value, enumerable: true, configurable: descriptor.configurable, writable: true },
|
|
86
|
+
}) as T;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** @internal */
|
|
90
|
+
export function captureAgentOutput(input: SessionCreateParams, options?: RequestOptions) {
|
|
91
|
+
const body = snapshotJSONProperty(input, 'agent', (agent) =>
|
|
92
|
+
agent
|
|
93
|
+
? snapshotJSONProperty(agent, 'text', (text) => (text ? snapshotJSONProperty(text, 'format') : text))
|
|
94
|
+
: agent,
|
|
95
|
+
);
|
|
96
|
+
const agent = ownJSONValue(body, 'agent');
|
|
97
|
+
const text = ownJSONValue(agent, 'text');
|
|
98
|
+
const format = agentFormatParser(ownJSONValue(text, 'format'));
|
|
99
|
+
if (!format) {
|
|
100
|
+
return { body, options };
|
|
101
|
+
}
|
|
102
|
+
const schema = structuredClone(format.schema);
|
|
103
|
+
for (const envelope of [body, agent, text]) {
|
|
104
|
+
if (envelope && 'toJSON' in envelope) {
|
|
105
|
+
throw new OpenAIError('Typed agent requests cannot customize body, agent, or text serialization');
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
// Snapshot options once, matching the own enumerable fields native request spreading uses.
|
|
109
|
+
const capturedOptions = { ...options };
|
|
110
|
+
if (ownJSONValue(capturedOptions, 'body') !== undefined) {
|
|
111
|
+
throw new OpenAIError('Typed agent requests cannot override the body in request options');
|
|
112
|
+
}
|
|
113
|
+
delete capturedOptions.body;
|
|
114
|
+
return {
|
|
115
|
+
options: capturedOptions,
|
|
116
|
+
body: {
|
|
117
|
+
...body,
|
|
118
|
+
agent: {
|
|
119
|
+
...agent,
|
|
120
|
+
text: { ...text, format: { type: 'json_schema', schema } },
|
|
121
|
+
},
|
|
122
|
+
},
|
|
123
|
+
format,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** @internal */
|
|
128
|
+
export async function parseAgentResultPromise<T>(
|
|
129
|
+
result: Promise<AgentTurnResult>,
|
|
130
|
+
format?: AgentOutputFormat<T>,
|
|
131
|
+
): Promise<AgentResult<T>> {
|
|
132
|
+
return parseAgentResult(await result, format);
|
|
133
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { AgentTurnResult } from './agent-turn-result';
|
|
2
|
+
|
|
3
|
+
/** Beta: a completed hosted turn whose output passed the local parser. */
|
|
4
|
+
export class ParsedAgentTurnResult<T> extends AgentTurnResult {
|
|
5
|
+
readonly output_parsed: T;
|
|
6
|
+
readonly raw_result: AgentTurnResult;
|
|
7
|
+
constructor(result: AgentTurnResult, parsed: T) {
|
|
8
|
+
super(result.turn, result.messages);
|
|
9
|
+
this.raw_result = result;
|
|
10
|
+
this.output_parsed = parsed;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import type { WritableStream } from '../../../internal/shim-types';
|
|
2
|
+
import { agentItems } from './pages';
|
|
3
|
+
import { OpenAIError } from '../../../core/error';
|
|
4
|
+
import type { RequestOptions } from '../../../internal/request-options';
|
|
5
|
+
import type { Artifacts, SessionArtifact } from '../../../resources/beta/agents/sessions/artifacts';
|
|
6
|
+
import type { AgentTurnResult } from './agent-turn-result';
|
|
7
|
+
|
|
8
|
+
/** Beta: artifacts from the exact session and turn represented by a result. */
|
|
9
|
+
export class AgentResultArtifacts {
|
|
10
|
+
readonly #resource: Artifacts;
|
|
11
|
+
readonly #sessionID: string;
|
|
12
|
+
readonly #turnID: string;
|
|
13
|
+
constructor(resource: Artifacts, result: AgentTurnResult) {
|
|
14
|
+
this.#resource = resource;
|
|
15
|
+
this.#sessionID = result.session_id;
|
|
16
|
+
this.#turnID = result.turn_id;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Find one immutable artifact by its exact hosted path, across all pages. */
|
|
20
|
+
async retrieve(path: string, options?: RequestOptions): Promise<SessionArtifact> {
|
|
21
|
+
let selected: SessionArtifact | undefined;
|
|
22
|
+
for await (const artifact of agentItems((after) =>
|
|
23
|
+
this.#resource.list(
|
|
24
|
+
this.#sessionID,
|
|
25
|
+
{},
|
|
26
|
+
{ ...options, query: { ...options?.query, after, environment_id: undefined } },
|
|
27
|
+
),
|
|
28
|
+
)) {
|
|
29
|
+
if (
|
|
30
|
+
artifact.session_id === this.#sessionID &&
|
|
31
|
+
artifact.turn_id === this.#turnID &&
|
|
32
|
+
artifact.path === path
|
|
33
|
+
) {
|
|
34
|
+
if (selected) {
|
|
35
|
+
throw new OpenAIError('Multiple artifacts match this result and path');
|
|
36
|
+
}
|
|
37
|
+
selected = artifact;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
if (!selected) {
|
|
41
|
+
throw new OpenAIError('No artifact matches this result and path');
|
|
42
|
+
}
|
|
43
|
+
return selected;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Return the native binary response for this result's exact artifact path. */
|
|
47
|
+
async content(path: string, options?: RequestOptions): Promise<Response> {
|
|
48
|
+
const artifact = await this.retrieve(path, options);
|
|
49
|
+
return this.#resource.content(artifact.id, { session_id: this.#sessionID }, options);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Stream bytes to a caller-chosen destination; the hosted path never selects a local path. */
|
|
53
|
+
async download(
|
|
54
|
+
params: { path: string; to: WritableStream<Uint8Array> },
|
|
55
|
+
options?: RequestOptions,
|
|
56
|
+
): Promise<SessionArtifact> {
|
|
57
|
+
const { path, to } = params;
|
|
58
|
+
const artifact = await this.retrieve(path, options);
|
|
59
|
+
const response = await this.#resource.content(artifact.id, { session_id: this.#sessionID }, options);
|
|
60
|
+
if (!response.body) {
|
|
61
|
+
throw new OpenAIError('Artifact response has no content stream');
|
|
62
|
+
}
|
|
63
|
+
try {
|
|
64
|
+
await response.body.pipeTo(to, options?.signal ? { signal: options.signal } : {});
|
|
65
|
+
} catch (error) {
|
|
66
|
+
// pipeTo can reject before acquiring a reader, for example when the destination is locked.
|
|
67
|
+
try {
|
|
68
|
+
await response.body.cancel();
|
|
69
|
+
} catch {
|
|
70
|
+
/* already cancelled or owned by pipeTo */
|
|
71
|
+
}
|
|
72
|
+
throw error;
|
|
73
|
+
}
|
|
74
|
+
return artifact;
|
|
75
|
+
}
|
|
76
|
+
}
|