openai 7.5.0 → 7.7.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 +117 -0
- package/README.md +46 -2
- package/auth/index.d.mts +1 -1
- package/auth/index.d.mts.map +1 -1
- package/auth/index.d.ts +1 -1
- package/auth/index.d.ts.map +1 -1
- package/auth/index.js.map +1 -1
- package/auth/index.mjs.map +1 -1
- package/auth/subject-token-providers.d.mts.map +1 -1
- package/auth/subject-token-providers.d.ts.map +1 -1
- package/auth/subject-token-providers.js +176 -4
- package/auth/subject-token-providers.js.map +1 -1
- package/auth/subject-token-providers.mjs +176 -4
- package/auth/subject-token-providers.mjs.map +1 -1
- package/auth/types.d.mts +17 -0
- package/auth/types.d.mts.map +1 -1
- package/auth/types.d.ts +17 -0
- package/auth/types.d.ts.map +1 -1
- package/auth/workload-identity-auth.d.mts.map +1 -1
- package/auth/workload-identity-auth.d.ts.map +1 -1
- package/auth/workload-identity-auth.js +80 -7
- package/auth/workload-identity-auth.js.map +1 -1
- package/auth/workload-identity-auth.mjs +80 -7
- package/auth/workload-identity-auth.mjs.map +1 -1
- package/auth/x509-transport.d.mts +5 -0
- package/auth/x509-transport.d.mts.map +1 -0
- package/auth/x509-transport.d.ts +5 -0
- package/auth/x509-transport.d.ts.map +1 -0
- package/auth/x509-transport.js +39 -0
- package/auth/x509-transport.js.map +1 -0
- package/auth/x509-transport.mjs +36 -0
- package/auth/x509-transport.mjs.map +1 -0
- package/azure.d.mts +11 -2
- package/azure.d.mts.map +1 -1
- package/azure.d.ts +11 -2
- package/azure.d.ts.map +1 -1
- package/azure.js +7 -1
- package/azure.js.map +1 -1
- package/azure.mjs +7 -1
- package/azure.mjs.map +1 -1
- package/bedrock.d.mts +6 -2
- package/bedrock.d.mts.map +1 -1
- package/bedrock.d.ts +6 -2
- package/bedrock.d.ts.map +1 -1
- package/bedrock.js +34 -4
- package/bedrock.js.map +1 -1
- package/bedrock.mjs +35 -5
- package/bedrock.mjs.map +1 -1
- package/beta/realtime/internal-base.d.mts +2 -0
- package/beta/realtime/internal-base.d.mts.map +1 -1
- package/beta/realtime/internal-base.d.ts +2 -0
- package/beta/realtime/internal-base.d.ts.map +1 -1
- package/beta/realtime/internal-base.js +15 -0
- package/beta/realtime/internal-base.js.map +1 -1
- package/beta/realtime/internal-base.mjs +14 -0
- package/beta/realtime/internal-base.mjs.map +1 -1
- package/beta/realtime/websocket.d.mts +11 -5
- package/beta/realtime/websocket.d.mts.map +1 -1
- package/beta/realtime/websocket.d.ts +11 -5
- package/beta/realtime/websocket.d.ts.map +1 -1
- package/beta/realtime/websocket.js +106 -31
- package/beta/realtime/websocket.js.map +1 -1
- package/beta/realtime/websocket.mjs +107 -32
- package/beta/realtime/websocket.mjs.map +1 -1
- package/beta/realtime/ws.d.mts +0 -2
- package/beta/realtime/ws.d.mts.map +1 -1
- package/beta/realtime/ws.d.ts +0 -2
- package/beta/realtime/ws.d.ts.map +1 -1
- package/beta/realtime/ws.js +24 -5
- package/beta/realtime/ws.js.map +1 -1
- package/beta/realtime/ws.mjs +25 -6
- package/beta/realtime/ws.mjs.map +1 -1
- package/client.d.mts +17 -2
- package/client.d.mts.map +1 -1
- package/client.d.ts +17 -2
- package/client.d.ts.map +1 -1
- package/client.js +422 -70
- package/client.js.map +1 -1
- package/client.mjs +423 -71
- package/client.mjs.map +1 -1
- package/core/EventEmitter.d.mts.map +1 -1
- package/core/EventEmitter.d.ts.map +1 -1
- package/core/EventEmitter.js +67 -9
- package/core/EventEmitter.js.map +1 -1
- package/core/EventEmitter.mjs +68 -10
- package/core/EventEmitter.mjs.map +1 -1
- package/core/streaming.d.mts +1 -0
- package/core/streaming.d.mts.map +1 -1
- package/core/streaming.d.ts +1 -0
- package/core/streaming.d.ts.map +1 -1
- package/core/streaming.js +216 -26
- package/core/streaming.js.map +1 -1
- package/core/streaming.mjs +216 -26
- package/core/streaming.mjs.map +1 -1
- package/helpers/audio.d.mts +5 -5
- package/helpers/audio.d.mts.map +1 -1
- package/helpers/audio.d.ts +5 -5
- package/helpers/audio.d.ts.map +1 -1
- package/helpers/audio.js +93 -22
- package/helpers/audio.js.map +1 -1
- package/helpers/audio.mjs +93 -22
- package/helpers/audio.mjs.map +1 -1
- package/helpers/standard-schema.d.mts.map +1 -1
- package/helpers/standard-schema.d.ts.map +1 -1
- package/helpers/standard-schema.js +34 -14
- package/helpers/standard-schema.js.map +1 -1
- package/helpers/standard-schema.mjs +35 -15
- package/helpers/standard-schema.mjs.map +1 -1
- package/helpers/zod-v3-strict-schema.d.mts +7 -0
- package/helpers/zod-v3-strict-schema.d.mts.map +1 -0
- package/helpers/zod-v3-strict-schema.d.ts +7 -0
- package/helpers/zod-v3-strict-schema.d.ts.map +1 -0
- package/helpers/zod-v3-strict-schema.js +403 -0
- package/helpers/zod-v3-strict-schema.js.map +1 -0
- package/helpers/zod-v3-strict-schema.mjs +399 -0
- package/helpers/zod-v3-strict-schema.mjs.map +1 -0
- package/helpers/zod.d.mts.map +1 -1
- package/helpers/zod.d.ts.map +1 -1
- package/helpers/zod.js +11 -3
- package/helpers/zod.js.map +1 -1
- package/helpers/zod.mjs +12 -4
- package/helpers/zod.mjs.map +1 -1
- package/index.d.mts +1 -1
- package/index.d.mts.map +1 -1
- package/index.d.ts +1 -1
- package/index.d.ts.map +1 -1
- package/index.js.map +1 -1
- package/index.mjs.map +1 -1
- package/internal/assistant-stream-delta.d.mts.map +1 -1
- package/internal/assistant-stream-delta.d.ts.map +1 -1
- package/internal/assistant-stream-delta.js +28 -8
- package/internal/assistant-stream-delta.js.map +1 -1
- package/internal/assistant-stream-delta.mjs +28 -8
- package/internal/assistant-stream-delta.mjs.map +1 -1
- package/internal/auth/x509-token-exchange.d.mts +16 -0
- package/internal/auth/x509-token-exchange.d.mts.map +1 -0
- package/internal/auth/x509-token-exchange.d.ts +16 -0
- package/internal/auth/x509-token-exchange.d.ts.map +1 -0
- package/internal/auth/x509-token-exchange.js +246 -0
- package/internal/auth/x509-token-exchange.js.map +1 -0
- package/internal/auth/x509-token-exchange.mjs +243 -0
- package/internal/auth/x509-token-exchange.mjs.map +1 -0
- package/internal/auth/x509-transport-capability.d.mts +37 -0
- package/internal/auth/x509-transport-capability.d.mts.map +1 -0
- package/internal/auth/x509-transport-capability.d.ts +37 -0
- package/internal/auth/x509-transport-capability.d.ts.map +1 -0
- package/internal/auth/x509-transport-capability.js +169 -0
- package/internal/auth/x509-transport-capability.js.map +1 -0
- package/internal/auth/x509-transport-capability.mjs +164 -0
- package/internal/auth/x509-transport-capability.mjs.map +1 -0
- package/internal/auth/x509-transport-registry.d.mts +63 -0
- package/internal/auth/x509-transport-registry.d.mts.map +1 -0
- package/internal/auth/x509-transport-registry.d.ts +63 -0
- package/internal/auth/x509-transport-registry.d.ts.map +1 -0
- package/internal/auth/x509-transport-registry.js +53 -0
- package/internal/auth/x509-transport-registry.js.map +1 -0
- package/internal/auth/x509-transport-registry.mjs +48 -0
- package/internal/auth/x509-transport-registry.mjs.map +1 -0
- package/internal/auth/x509-transport-state-browser.d.mts +21 -0
- package/internal/auth/x509-transport-state-browser.d.mts.map +1 -0
- package/internal/auth/x509-transport-state-browser.d.ts +21 -0
- package/internal/auth/x509-transport-state-browser.d.ts.map +1 -0
- package/internal/auth/x509-transport-state-browser.js +30 -0
- package/internal/auth/x509-transport-state-browser.js.map +1 -0
- package/internal/auth/x509-transport-state-browser.mjs +27 -0
- package/internal/auth/x509-transport-state-browser.mjs.map +1 -0
- package/internal/auth/x509-transport-state.cjs +30 -0
- package/internal/auth/x509-transport-state.cjs.map +1 -0
- package/internal/auth/x509-transport-state.d.cts +21 -0
- package/internal/auth/x509-transport-state.d.cts.map +1 -0
- package/internal/auth/x509-workload-identity-auth.d.mts +128 -0
- package/internal/auth/x509-workload-identity-auth.d.mts.map +1 -0
- package/internal/auth/x509-workload-identity-auth.d.ts +128 -0
- package/internal/auth/x509-workload-identity-auth.d.ts.map +1 -0
- package/internal/auth/x509-workload-identity-auth.js +767 -0
- package/internal/auth/x509-workload-identity-auth.js.map +1 -0
- package/internal/auth/x509-workload-identity-auth.mjs +757 -0
- package/internal/auth/x509-workload-identity-auth.mjs.map +1 -0
- package/internal/bedrock.d.mts +2 -0
- package/internal/bedrock.d.mts.map +1 -1
- package/internal/bedrock.d.ts +2 -0
- package/internal/bedrock.d.ts.map +1 -1
- package/internal/bedrock.js +169 -5
- package/internal/bedrock.js.map +1 -1
- package/internal/bedrock.mjs +168 -5
- package/internal/bedrock.mjs.map +1 -1
- package/internal/data-residency.d.mts +12 -0
- package/internal/data-residency.d.mts.map +1 -0
- package/internal/data-residency.d.ts +12 -0
- package/internal/data-residency.d.ts.map +1 -0
- package/internal/data-residency.js +34 -0
- package/internal/data-residency.js.map +1 -0
- package/internal/data-residency.mjs +30 -0
- package/internal/data-residency.mjs.map +1 -0
- package/internal/multipart-encoding.d.mts +8 -0
- package/internal/multipart-encoding.d.mts.map +1 -0
- package/internal/multipart-encoding.d.ts +8 -0
- package/internal/multipart-encoding.d.ts.map +1 -0
- package/internal/multipart-encoding.js +51 -0
- package/internal/multipart-encoding.js.map +1 -0
- package/internal/multipart-encoding.mjs +48 -0
- package/internal/multipart-encoding.mjs.map +1 -0
- package/internal/qs/utils.d.mts.map +1 -1
- package/internal/qs/utils.d.ts.map +1 -1
- package/internal/qs/utils.js +307 -10
- package/internal/qs/utils.js.map +1 -1
- package/internal/qs/utils.mjs +307 -10
- package/internal/qs/utils.mjs.map +1 -1
- package/internal/request-options.d.mts +7 -0
- package/internal/request-options.d.mts.map +1 -1
- package/internal/request-options.d.ts +7 -0
- package/internal/request-options.d.ts.map +1 -1
- package/internal/request-options.js +43 -1
- package/internal/request-options.js.map +1 -1
- package/internal/request-options.mjs +42 -1
- package/internal/request-options.mjs.map +1 -1
- package/internal/responses/canonical-output-text.js +3 -3
- package/internal/responses/canonical-output-text.js.map +1 -1
- package/internal/responses/canonical-output-text.mjs +3 -3
- package/internal/responses/canonical-output-text.mjs.map +1 -1
- package/internal/responses/response-accumulator.d.mts +1 -1
- package/internal/responses/response-accumulator.d.mts.map +1 -1
- package/internal/responses/response-accumulator.d.ts +1 -1
- package/internal/responses/response-accumulator.d.ts.map +1 -1
- package/internal/responses/response-accumulator.js +331 -25
- package/internal/responses/response-accumulator.js.map +1 -1
- package/internal/responses/response-accumulator.mjs +331 -25
- package/internal/responses/response-accumulator.mjs.map +1 -1
- package/internal/tslib.js +6 -0
- package/internal/tslib.mjs +6 -1
- package/internal/uploads.d.mts +10 -6
- package/internal/uploads.d.mts.map +1 -1
- package/internal/uploads.d.ts +10 -6
- package/internal/uploads.d.ts.map +1 -1
- package/internal/uploads.js +65 -24
- package/internal/uploads.js.map +1 -1
- package/internal/uploads.mjs +65 -24
- package/internal/uploads.mjs.map +1 -1
- package/internal/utils/env.d.mts.map +1 -1
- package/internal/utils/env.d.ts.map +1 -1
- package/internal/utils/env.js +9 -4
- package/internal/utils/env.js.map +1 -1
- package/internal/utils/env.mjs +9 -4
- package/internal/utils/env.mjs.map +1 -1
- package/internal/ws.d.mts.map +1 -1
- package/internal/ws.d.ts.map +1 -1
- package/internal/ws.js +23 -1
- package/internal/ws.js.map +1 -1
- package/internal/ws.mjs +23 -1
- package/internal/ws.mjs.map +1 -1
- package/lib/AbstractChatCompletionRunner.d.mts.map +1 -1
- package/lib/AbstractChatCompletionRunner.d.ts.map +1 -1
- package/lib/AbstractChatCompletionRunner.js +32 -7
- package/lib/AbstractChatCompletionRunner.js.map +1 -1
- package/lib/AbstractChatCompletionRunner.mjs +34 -9
- package/lib/AbstractChatCompletionRunner.mjs.map +1 -1
- package/lib/AssistantStream.d.mts.map +1 -1
- package/lib/AssistantStream.d.ts.map +1 -1
- package/lib/AssistantStream.js +211 -23
- package/lib/AssistantStream.js.map +1 -1
- package/lib/AssistantStream.mjs +211 -23
- package/lib/AssistantStream.mjs.map +1 -1
- package/lib/ChatCompletionStream.d.mts +1 -1
- package/lib/ChatCompletionStream.d.mts.map +1 -1
- package/lib/ChatCompletionStream.d.ts +1 -1
- package/lib/ChatCompletionStream.d.ts.map +1 -1
- package/lib/ChatCompletionStream.js +1118 -67
- package/lib/ChatCompletionStream.js.map +1 -1
- package/lib/ChatCompletionStream.mjs +1119 -68
- package/lib/ChatCompletionStream.mjs.map +1 -1
- package/lib/EventEmitter.d.mts.map +1 -1
- package/lib/EventEmitter.d.ts.map +1 -1
- package/lib/EventEmitter.js +67 -9
- package/lib/EventEmitter.js.map +1 -1
- package/lib/EventEmitter.mjs +68 -10
- package/lib/EventEmitter.mjs.map +1 -1
- package/lib/EventStream.d.mts +4 -0
- package/lib/EventStream.d.mts.map +1 -1
- package/lib/EventStream.d.ts +4 -0
- package/lib/EventStream.d.ts.map +1 -1
- package/lib/EventStream.js +1096 -11
- package/lib/EventStream.js.map +1 -1
- package/lib/EventStream.mjs +1097 -12
- package/lib/EventStream.mjs.map +1 -1
- package/lib/ResponsesParser.d.mts.map +1 -1
- package/lib/ResponsesParser.d.ts.map +1 -1
- package/lib/ResponsesParser.js +1 -1
- package/lib/ResponsesParser.js.map +1 -1
- package/lib/ResponsesParser.mjs +1 -1
- package/lib/ResponsesParser.mjs.map +1 -1
- package/lib/Util.d.mts +2 -0
- package/lib/Util.d.mts.map +1 -1
- package/lib/Util.d.ts +2 -0
- package/lib/Util.d.ts.map +1 -1
- package/lib/Util.js +7 -4
- package/lib/Util.js.map +1 -1
- package/lib/Util.mjs +7 -4
- package/lib/Util.mjs.map +1 -1
- package/lib/assistant-run-polling.d.mts +12 -0
- package/lib/assistant-run-polling.d.mts.map +1 -0
- package/lib/assistant-run-polling.d.ts +12 -0
- package/lib/assistant-run-polling.d.ts.map +1 -0
- package/lib/assistant-run-polling.js +17 -0
- package/lib/assistant-run-polling.js.map +1 -0
- package/lib/assistant-run-polling.mjs +14 -0
- package/lib/assistant-run-polling.mjs.map +1 -0
- package/lib/embeddings.d.mts +18 -0
- package/lib/embeddings.d.mts.map +1 -0
- package/lib/embeddings.d.ts +18 -0
- package/lib/embeddings.d.ts.map +1 -0
- package/lib/embeddings.js +50 -0
- package/lib/embeddings.js.map +1 -0
- package/lib/embeddings.mjs +47 -0
- package/lib/embeddings.mjs.map +1 -0
- package/lib/file-processing.d.mts +10 -0
- package/lib/file-processing.d.mts.map +1 -0
- package/lib/file-processing.d.ts +10 -0
- package/lib/file-processing.d.ts.map +1 -0
- package/lib/file-processing.js +30 -0
- package/lib/file-processing.js.map +1 -0
- package/lib/file-processing.mjs +27 -0
- package/lib/file-processing.mjs.map +1 -0
- package/lib/parser.d.mts.map +1 -1
- package/lib/parser.d.ts.map +1 -1
- package/lib/parser.js +16 -4
- package/lib/parser.js.map +1 -1
- package/lib/parser.mjs +16 -4
- package/lib/parser.mjs.map +1 -1
- package/lib/polling.d.mts +20 -0
- package/lib/polling.d.mts.map +1 -0
- package/lib/polling.d.ts +20 -0
- package/lib/polling.d.ts.map +1 -0
- package/lib/polling.js +130 -0
- package/lib/polling.js.map +1 -0
- package/lib/polling.mjs +127 -0
- package/lib/polling.mjs.map +1 -0
- package/lib/responses/ResponseStream.d.mts.map +1 -1
- package/lib/responses/ResponseStream.d.ts.map +1 -1
- package/lib/responses/ResponseStream.js +21 -11
- package/lib/responses/ResponseStream.js.map +1 -1
- package/lib/responses/ResponseStream.mjs +21 -11
- package/lib/responses/ResponseStream.mjs.map +1 -1
- package/lib/vector-store-polling.d.mts +22 -0
- package/lib/vector-store-polling.d.mts.map +1 -0
- package/lib/vector-store-polling.d.ts +22 -0
- package/lib/vector-store-polling.d.ts.map +1 -0
- package/lib/vector-store-polling.js +24 -0
- package/lib/vector-store-polling.js.map +1 -0
- package/lib/vector-store-polling.mjs +20 -0
- package/lib/vector-store-polling.mjs.map +1 -0
- package/lib/vector-store-upload.d.mts +17 -0
- package/lib/vector-store-upload.d.mts.map +1 -0
- package/lib/vector-store-upload.d.ts +17 -0
- package/lib/vector-store-upload.d.ts.map +1 -0
- package/lib/vector-store-upload.js +37 -0
- package/lib/vector-store-upload.js.map +1 -0
- package/lib/vector-store-upload.mjs +34 -0
- package/lib/vector-store-upload.mjs.map +1 -0
- package/lib/webhook-signature.d.mts +15 -0
- package/lib/webhook-signature.d.mts.map +1 -0
- package/lib/webhook-signature.d.ts +15 -0
- package/lib/webhook-signature.d.ts.map +1 -0
- package/lib/webhook-signature.js +125 -0
- package/lib/webhook-signature.js.map +1 -0
- package/lib/webhook-signature.mjs +121 -0
- package/lib/webhook-signature.mjs.map +1 -0
- package/package.json +14 -1
- package/realtime/internal-base.d.mts +2 -0
- package/realtime/internal-base.d.mts.map +1 -1
- package/realtime/internal-base.d.ts +2 -0
- package/realtime/internal-base.d.ts.map +1 -1
- package/realtime/internal-base.js +15 -0
- package/realtime/internal-base.js.map +1 -1
- package/realtime/internal-base.mjs +14 -0
- package/realtime/internal-base.mjs.map +1 -1
- package/realtime/websocket.d.mts +3 -2
- package/realtime/websocket.d.mts.map +1 -1
- package/realtime/websocket.d.ts +3 -2
- package/realtime/websocket.d.ts.map +1 -1
- package/realtime/websocket.js +90 -26
- package/realtime/websocket.js.map +1 -1
- package/realtime/websocket.mjs +91 -27
- package/realtime/websocket.mjs.map +1 -1
- package/realtime/ws.d.mts.map +1 -1
- package/realtime/ws.d.ts.map +1 -1
- package/realtime/ws.js +9 -1
- package/realtime/ws.js.map +1 -1
- package/realtime/ws.mjs +10 -2
- package/realtime/ws.mjs.map +1 -1
- package/resources/admin/organization/index.d.mts +2 -2
- package/resources/admin/organization/index.d.mts.map +1 -1
- package/resources/admin/organization/index.d.ts +2 -2
- package/resources/admin/organization/index.d.ts.map +1 -1
- package/resources/admin/organization/index.js.map +1 -1
- package/resources/admin/organization/index.mjs.map +1 -1
- package/resources/admin/organization/organization.d.mts +4 -4
- package/resources/admin/organization/organization.d.mts.map +1 -1
- package/resources/admin/organization/organization.d.ts +4 -4
- package/resources/admin/organization/organization.d.ts.map +1 -1
- package/resources/admin/organization/organization.js.map +1 -1
- package/resources/admin/organization/organization.mjs.map +1 -1
- package/resources/admin/organization/projects/index.d.mts +1 -1
- package/resources/admin/organization/projects/index.d.mts.map +1 -1
- package/resources/admin/organization/projects/index.d.ts +1 -1
- package/resources/admin/organization/projects/index.d.ts.map +1 -1
- package/resources/admin/organization/projects/index.js.map +1 -1
- package/resources/admin/organization/projects/index.mjs.map +1 -1
- package/resources/admin/organization/projects/projects.d.mts +22 -5
- package/resources/admin/organization/projects/projects.d.mts.map +1 -1
- package/resources/admin/organization/projects/projects.d.ts +22 -5
- package/resources/admin/organization/projects/projects.d.ts.map +1 -1
- package/resources/admin/organization/projects/projects.js.map +1 -1
- package/resources/admin/organization/projects/projects.mjs.map +1 -1
- package/resources/admin/organization/usage.d.mts +62 -1
- package/resources/admin/organization/usage.d.mts.map +1 -1
- package/resources/admin/organization/usage.d.ts +62 -1
- package/resources/admin/organization/usage.d.ts.map +1 -1
- package/resources/admin/organization/usage.js.map +1 -1
- package/resources/admin/organization/usage.mjs.map +1 -1
- package/resources/beta/responses/internal-base.d.mts.map +1 -1
- package/resources/beta/responses/internal-base.d.ts.map +1 -1
- package/resources/beta/responses/internal-base.js +2 -0
- package/resources/beta/responses/internal-base.js.map +1 -1
- package/resources/beta/responses/internal-base.mjs +2 -0
- package/resources/beta/responses/internal-base.mjs.map +1 -1
- package/resources/beta/responses/responses.d.mts +17 -20
- package/resources/beta/responses/responses.d.mts.map +1 -1
- package/resources/beta/responses/responses.d.ts +17 -20
- package/resources/beta/responses/responses.d.ts.map +1 -1
- package/resources/beta/responses/responses.js.map +1 -1
- package/resources/beta/responses/responses.mjs.map +1 -1
- package/resources/beta/threads/runs/index.d.mts +1 -1
- package/resources/beta/threads/runs/index.d.mts.map +1 -1
- package/resources/beta/threads/runs/index.d.ts +1 -1
- package/resources/beta/threads/runs/index.d.ts.map +1 -1
- package/resources/beta/threads/runs/runs.d.mts.map +1 -1
- package/resources/beta/threads/runs/runs.d.ts.map +1 -1
- package/resources/beta/threads/runs/runs.js +2 -43
- package/resources/beta/threads/runs/runs.js.map +1 -1
- package/resources/beta/threads/runs/runs.mjs +2 -43
- package/resources/beta/threads/runs/runs.mjs.map +1 -1
- package/resources/chat/completions/completions.d.mts +6 -0
- package/resources/chat/completions/completions.d.mts.map +1 -1
- package/resources/chat/completions/completions.d.ts +6 -0
- package/resources/chat/completions/completions.d.ts.map +1 -1
- package/resources/chat/completions/completions.js.map +1 -1
- package/resources/chat/completions/completions.mjs.map +1 -1
- package/resources/embeddings.d.mts.map +1 -1
- package/resources/embeddings.d.ts.map +1 -1
- package/resources/embeddings.js +2 -36
- package/resources/embeddings.js.map +1 -1
- package/resources/embeddings.mjs +2 -36
- package/resources/embeddings.mjs.map +1 -1
- package/resources/files.d.mts.map +1 -1
- package/resources/files.d.ts.map +1 -1
- package/resources/files.js +2 -15
- package/resources/files.js.map +1 -1
- package/resources/files.mjs +2 -15
- package/resources/files.mjs.map +1 -1
- package/resources/images.d.mts +12 -22
- package/resources/images.d.mts.map +1 -1
- package/resources/images.d.ts +12 -22
- package/resources/images.d.ts.map +1 -1
- package/resources/realtime/calls.d.mts +23 -1
- package/resources/realtime/calls.d.mts.map +1 -1
- package/resources/realtime/calls.d.ts +23 -1
- package/resources/realtime/calls.d.ts.map +1 -1
- package/resources/realtime/calls.js +24 -0
- package/resources/realtime/calls.js.map +1 -1
- package/resources/realtime/calls.mjs +24 -0
- package/resources/realtime/calls.mjs.map +1 -1
- package/resources/realtime/index.d.mts +1 -1
- package/resources/realtime/index.d.mts.map +1 -1
- package/resources/realtime/index.d.ts +1 -1
- package/resources/realtime/index.d.ts.map +1 -1
- package/resources/realtime/index.js.map +1 -1
- package/resources/realtime/index.mjs +1 -1
- package/resources/realtime/index.mjs.map +1 -1
- package/resources/realtime/realtime.d.mts +5 -4
- package/resources/realtime/realtime.d.mts.map +1 -1
- package/resources/realtime/realtime.d.ts +5 -4
- package/resources/realtime/realtime.d.ts.map +1 -1
- package/resources/realtime/realtime.js.map +1 -1
- package/resources/realtime/realtime.mjs.map +1 -1
- package/resources/responses/internal-base.d.mts.map +1 -1
- package/resources/responses/internal-base.d.ts.map +1 -1
- package/resources/responses/internal-base.js +2 -0
- package/resources/responses/internal-base.js.map +1 -1
- package/resources/responses/internal-base.mjs +2 -0
- package/resources/responses/internal-base.mjs.map +1 -1
- package/resources/responses/responses.d.mts +17 -20
- package/resources/responses/responses.d.mts.map +1 -1
- package/resources/responses/responses.d.ts +17 -20
- package/resources/responses/responses.d.ts.map +1 -1
- package/resources/responses/responses.js.map +1 -1
- package/resources/responses/responses.mjs.map +1 -1
- package/resources/vector-stores/file-batches.d.mts.map +1 -1
- package/resources/vector-stores/file-batches.d.ts.map +1 -1
- package/resources/vector-stores/file-batches.js +4 -61
- package/resources/vector-stores/file-batches.js.map +1 -1
- package/resources/vector-stores/file-batches.mjs +4 -61
- package/resources/vector-stores/file-batches.mjs.map +1 -1
- package/resources/vector-stores/files.d.mts +1 -1
- package/resources/vector-stores/files.d.mts.map +1 -1
- package/resources/vector-stores/files.d.ts +1 -1
- package/resources/vector-stores/files.d.ts.map +1 -1
- package/resources/vector-stores/files.js +2 -35
- package/resources/vector-stores/files.js.map +1 -1
- package/resources/vector-stores/files.mjs +2 -35
- package/resources/vector-stores/files.mjs.map +1 -1
- package/resources/webhooks/webhooks.d.mts.map +1 -1
- package/resources/webhooks/webhooks.d.ts.map +1 -1
- package/resources/webhooks/webhooks.js +5 -43
- package/resources/webhooks/webhooks.js.map +1 -1
- package/resources/webhooks/webhooks.mjs +5 -43
- package/resources/webhooks/webhooks.mjs.map +1 -1
- package/src/auth/index.ts +6 -1
- package/src/auth/subject-token-providers.ts +196 -4
- package/src/auth/types.ts +24 -0
- package/src/auth/workload-identity-auth.ts +104 -7
- package/src/auth/x509-transport.ts +49 -0
- package/src/azure.ts +22 -1
- package/src/bedrock.ts +49 -5
- package/src/beta/realtime/internal-base.ts +15 -0
- package/src/beta/realtime/websocket.ts +148 -40
- package/src/beta/realtime/ws.ts +31 -13
- package/src/client.ts +536 -92
- package/src/core/EventEmitter.ts +91 -7
- package/src/core/streaming.ts +226 -30
- package/src/helpers/audio.ts +93 -22
- package/src/helpers/standard-schema.ts +50 -16
- package/src/helpers/zod-v3-strict-schema.ts +514 -0
- package/src/helpers/zod.ts +17 -4
- package/src/index.ts +1 -1
- package/src/internal/assistant-stream-delta.ts +33 -10
- package/src/internal/auth/x509-token-exchange.ts +310 -0
- package/src/internal/auth/x509-transport-capability.ts +219 -0
- package/src/internal/auth/x509-transport-registry.ts +118 -0
- package/src/internal/auth/x509-transport-state-browser.ts +36 -0
- package/src/internal/auth/x509-transport-state.cts +36 -0
- package/src/internal/auth/x509-workload-identity-auth.ts +958 -0
- package/src/internal/bedrock.ts +182 -5
- package/src/internal/data-residency.ts +44 -0
- package/src/internal/multipart-encoding.ts +53 -0
- package/src/internal/qs/utils.ts +375 -13
- package/src/internal/request-options.ts +52 -1
- package/src/internal/responses/canonical-output-text.ts +3 -3
- package/src/internal/responses/response-accumulator.ts +411 -24
- package/src/internal/uploads.ts +81 -26
- package/src/internal/utils/env.ts +9 -5
- package/src/internal/ws.ts +24 -3
- package/src/lib/AbstractChatCompletionRunner.ts +33 -6
- package/src/lib/AssistantStream.ts +262 -22
- package/src/lib/ChatCompletionStream.ts +1467 -103
- package/src/lib/EventEmitter.ts +91 -7
- package/src/lib/EventStream.ts +1483 -10
- package/src/lib/ResponsesParser.ts +4 -1
- package/src/lib/Util.ts +7 -5
- package/src/lib/assistant-run-polling.ts +27 -0
- package/src/lib/embeddings.ts +65 -0
- package/src/lib/file-processing.ts +36 -0
- package/src/lib/parser.ts +19 -4
- package/src/lib/polling.ts +144 -0
- package/src/lib/responses/ResponseStream.ts +22 -10
- package/src/lib/vector-store-polling.ts +46 -0
- package/src/lib/vector-store-upload.ts +53 -0
- package/src/lib/webhook-signature.ts +164 -0
- package/src/realtime/internal-base.ts +15 -0
- package/src/realtime/websocket.ts +130 -27
- package/src/realtime/ws.ts +15 -2
- package/src/resources/admin/organization/index.ts +2 -0
- package/src/resources/admin/organization/organization.ts +4 -0
- package/src/resources/admin/organization/projects/index.ts +1 -0
- package/src/resources/admin/organization/projects/projects.ts +37 -4
- package/src/resources/admin/organization/usage.ts +83 -0
- package/src/resources/beta/responses/internal-base.ts +2 -0
- package/src/resources/beta/responses/responses.ts +19 -22
- package/src/resources/beta/threads/runs/index.ts +1 -1
- package/src/resources/beta/threads/runs/runs.ts +2 -45
- package/src/resources/chat/completions/completions.ts +7 -0
- package/src/resources/embeddings.ts +2 -42
- package/src/resources/files.ts +2 -19
- package/src/resources/images.ts +12 -22
- package/src/resources/realtime/api.md +1 -0
- package/src/resources/realtime/calls.ts +46 -0
- package/src/resources/realtime/index.ts +7 -1
- package/src/resources/realtime/realtime.ts +5 -3
- package/src/resources/responses/internal-base.ts +2 -0
- package/src/resources/responses/responses.ts +19 -22
- package/src/resources/vector-stores/file-batches.ts +8 -77
- package/src/resources/vector-stores/files.ts +3 -43
- package/src/resources/webhooks/webhooks.ts +5 -61
- 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
|
@@ -1,16 +1,28 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var _ChatCompletionStream_instances, _ChatCompletionStream_params, _ChatCompletionStream_audioDoneChoiceIndexes, _ChatCompletionStream_choiceEventStates, _ChatCompletionStream_currentChatCompletionSnapshot, _ChatCompletionStream_beginRequest, _ChatCompletionStream_getChoiceEventState, _ChatCompletionStream_addChunk, _ChatCompletionStream_emitToolCallDoneEvent, _ChatCompletionStream_emitContentDoneEvents, _ChatCompletionStream_endRequest, _ChatCompletionStream_accumulateChatCompletion;
|
|
2
|
+
var _ChatCompletionStream_instances, _ChatCompletionStream_params, _ChatCompletionStream_audioDoneChoiceIndexes, _ChatCompletionStream_choiceEventStates, _ChatCompletionStream_currentChatCompletionSnapshot, _ChatCompletionStream_hasAutoParseableTool, _ChatCompletionStream_partialJSONParseBudget, _ChatCompletionStream_beginRequest, _ChatCompletionStream_getChoiceEventState, _ChatCompletionStream_addChunk, _ChatCompletionStream_emitToolCallDoneEvent, _ChatCompletionStream_emitContentDoneEvents, _ChatCompletionStream_validateStructuredSnapshots, _ChatCompletionStream_endRequest, _ChatCompletionStream_accumulateChatCompletion;
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
4
|
exports.ChatCompletionStream = void 0;
|
|
5
5
|
exports.makeChatCompletionReadableStreamMessageChunk = makeChatCompletionReadableStreamMessageChunk;
|
|
6
6
|
const tslib_1 = require("../internal/tslib.js");
|
|
7
7
|
const parser_1 = require("../_vendor/partial-json-parser/parser.js");
|
|
8
8
|
const error_1 = require("../error.js");
|
|
9
|
+
const request_options_1 = require("../internal/request-options.js");
|
|
9
10
|
const uuid_1 = require("../internal/utils/uuid.js");
|
|
10
11
|
const values_1 = require("../internal/utils/values.js");
|
|
11
12
|
const parser_2 = require("../lib/parser.js");
|
|
12
13
|
const streaming_1 = require("../streaming.js");
|
|
13
14
|
const AbstractChatCompletionRunner_1 = require("./AbstractChatCompletionRunner.js");
|
|
15
|
+
function parseStructuredStreamingJSON(content) {
|
|
16
|
+
try {
|
|
17
|
+
return (0, parser_1.partialParse)(content);
|
|
18
|
+
}
|
|
19
|
+
catch (error) {
|
|
20
|
+
if (error instanceof parser_1.MalformedJSON || error instanceof SyntaxError) {
|
|
21
|
+
return (0, parser_2.parseResponseFormatContent)({ type: 'json_schema', $parseRaw: undefined }, content);
|
|
22
|
+
}
|
|
23
|
+
throw error;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
14
26
|
// Keep message records readable as empty chunks by older SDKs. Their finalizer
|
|
15
27
|
// overwrites `object`, so the encoded payload does not leak into completions.
|
|
16
28
|
const CHAT_COMPLETION_READABLE_STREAM_MESSAGE_PREFIX = 'chat.completion.chunk.message:';
|
|
@@ -46,6 +58,232 @@ function getChatCompletionReadableStreamMessage(item) {
|
|
|
46
58
|
// conservative ceiling to prevent sparse tool-call arrays from growing unbounded.
|
|
47
59
|
const MAX_STREAM_CHOICES = 128;
|
|
48
60
|
const MAX_STREAM_TOOL_CALLS = 128;
|
|
61
|
+
const MAX_PARTIAL_JSON_BYTES = 16 * 1024 * 1024;
|
|
62
|
+
const MAX_PARTIAL_JSON_FRAGMENTS = 65536;
|
|
63
|
+
const MAX_PARTIAL_JSON_DEPTH = 128;
|
|
64
|
+
const MAX_PARTIAL_JSON_PARSE_WORK = 64 * 1024 * 1024;
|
|
65
|
+
const EAGER_PARTIAL_JSON_BYTES = 1024;
|
|
66
|
+
function createPartialJSONParseState() {
|
|
67
|
+
return {
|
|
68
|
+
bytes: 0,
|
|
69
|
+
depth: 0,
|
|
70
|
+
fragments: 0,
|
|
71
|
+
work: 0,
|
|
72
|
+
escaped: false,
|
|
73
|
+
has_non_whitespace: false,
|
|
74
|
+
in_string: false,
|
|
75
|
+
last_parsed_bytes: 0,
|
|
76
|
+
pending_high_surrogate: false,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
function recordPartialJSONFragment(state, budget, fragment, validationWorkBudget) {
|
|
80
|
+
if (budget.fragments >= MAX_PARTIAL_JSON_FRAGMENTS) {
|
|
81
|
+
throw new error_1.OpenAIError('Chat completion stream exceeded its structured JSON fragment limit');
|
|
82
|
+
}
|
|
83
|
+
let bytes = 0;
|
|
84
|
+
let { depth, escaped, has_non_whitespace: hasNonWhitespace, in_string: inString } = state;
|
|
85
|
+
let completed = false;
|
|
86
|
+
let firstCharacter = true;
|
|
87
|
+
for (const character of fragment) {
|
|
88
|
+
const previousBytes = bytes;
|
|
89
|
+
const codePoint = character.codePointAt(0);
|
|
90
|
+
if (firstCharacter && state.pending_high_surrogate && codePoint >= 56320 && codePoint <= 57343) {
|
|
91
|
+
bytes += 1;
|
|
92
|
+
}
|
|
93
|
+
else if (codePoint <= 0x7f) {
|
|
94
|
+
bytes += 1;
|
|
95
|
+
}
|
|
96
|
+
else if (codePoint <= 2047) {
|
|
97
|
+
bytes += 2;
|
|
98
|
+
}
|
|
99
|
+
else if (codePoint <= 65535) {
|
|
100
|
+
bytes += 3;
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
bytes += 4;
|
|
104
|
+
}
|
|
105
|
+
firstCharacter = false;
|
|
106
|
+
if (budget.bytes + bytes > MAX_PARTIAL_JSON_BYTES) {
|
|
107
|
+
throw new error_1.OpenAIError('Chat completion stream exceeded its structured JSON byte limit');
|
|
108
|
+
}
|
|
109
|
+
if (validationWorkBudget && validationWorkBudget.work + bytes > MAX_PARTIAL_JSON_PARSE_WORK) {
|
|
110
|
+
validationWorkBudget.work += previousBytes;
|
|
111
|
+
throw new error_1.OpenAIError('Chat completion stream exceeded its structured JSON parse-work limit');
|
|
112
|
+
}
|
|
113
|
+
if (character !== ' ' && character !== '\n' && character !== '\r' && character !== '\t') {
|
|
114
|
+
hasNonWhitespace = true;
|
|
115
|
+
}
|
|
116
|
+
if (inString) {
|
|
117
|
+
if (escaped) {
|
|
118
|
+
escaped = false;
|
|
119
|
+
}
|
|
120
|
+
else if (character === '\\') {
|
|
121
|
+
escaped = true;
|
|
122
|
+
}
|
|
123
|
+
else if (character === '"') {
|
|
124
|
+
inString = false;
|
|
125
|
+
completed || (completed = depth === 0);
|
|
126
|
+
}
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
if (character === '"') {
|
|
130
|
+
inString = true;
|
|
131
|
+
}
|
|
132
|
+
else if (character === '{' || character === '[') {
|
|
133
|
+
depth += 1;
|
|
134
|
+
if (depth > MAX_PARTIAL_JSON_DEPTH) {
|
|
135
|
+
throw new error_1.OpenAIError('Chat completion stream exceeded its structured JSON nesting depth limit');
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
else if ((character === '}' || character === ']') && depth > 0) {
|
|
139
|
+
depth -= 1;
|
|
140
|
+
completed || (completed = depth === 0);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
state.bytes += bytes;
|
|
144
|
+
state.fragments += 1;
|
|
145
|
+
state.depth = depth;
|
|
146
|
+
state.escaped = escaped;
|
|
147
|
+
state.has_non_whitespace = hasNonWhitespace;
|
|
148
|
+
state.in_string = inString;
|
|
149
|
+
if (fragment.length > 0) {
|
|
150
|
+
const finalCodeUnit = fragment.codePointAt(fragment.length - 1) ?? 0;
|
|
151
|
+
state.pending_high_surrogate = finalCodeUnit >= 55296 && finalCodeUnit <= 56319;
|
|
152
|
+
}
|
|
153
|
+
budget.bytes += bytes;
|
|
154
|
+
budget.fragments += 1;
|
|
155
|
+
if (validationWorkBudget) {
|
|
156
|
+
validationWorkBudget.work += bytes;
|
|
157
|
+
}
|
|
158
|
+
if (!hasNonWhitespace || bytes === 0) {
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
const minimumGrowth = Math.max(EAGER_PARTIAL_JSON_BYTES, Math.floor(state.last_parsed_bytes / 2));
|
|
162
|
+
if (state.bytes > EAGER_PARTIAL_JSON_BYTES &&
|
|
163
|
+
!completed &&
|
|
164
|
+
state.bytes - state.last_parsed_bytes < minimumGrowth) {
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
return true;
|
|
168
|
+
}
|
|
169
|
+
function reservePartialJSONParse(state, budget) {
|
|
170
|
+
if (budget.work + state.bytes > MAX_PARTIAL_JSON_PARSE_WORK) {
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
budget.work += state.bytes;
|
|
174
|
+
state.work += state.bytes;
|
|
175
|
+
state.last_parsed_bytes = state.bytes;
|
|
176
|
+
return true;
|
|
177
|
+
}
|
|
178
|
+
function captureStructuredJSONSnapshot(snapshot, property) {
|
|
179
|
+
const descriptor = Object.getOwnPropertyDescriptor(snapshot, property);
|
|
180
|
+
if (!descriptor) {
|
|
181
|
+
let prototype = Object.getPrototypeOf(snapshot);
|
|
182
|
+
for (let depth = 0; prototype !== null; depth += 1) {
|
|
183
|
+
if (depth >= MAX_PARTIAL_JSON_DEPTH || Object.getOwnPropertyDescriptor(prototype, property)) {
|
|
184
|
+
throw new error_1.OpenAIError('Chat completion stream contains an unsafe structured JSON snapshot');
|
|
185
|
+
}
|
|
186
|
+
prototype = Object.getPrototypeOf(prototype);
|
|
187
|
+
}
|
|
188
|
+
return undefined;
|
|
189
|
+
}
|
|
190
|
+
if (!('value' in descriptor) ||
|
|
191
|
+
(typeof descriptor.value !== 'string' && descriptor.value !== null && descriptor.value !== undefined)) {
|
|
192
|
+
throw new error_1.OpenAIError('Chat completion stream contains an unsafe structured JSON snapshot');
|
|
193
|
+
}
|
|
194
|
+
return descriptor.value;
|
|
195
|
+
}
|
|
196
|
+
function captureStructuredMessageSnapshot(choice) {
|
|
197
|
+
const descriptor = Object.getOwnPropertyDescriptor(choice, 'message');
|
|
198
|
+
if (!descriptor ||
|
|
199
|
+
!('value' in descriptor) ||
|
|
200
|
+
typeof descriptor.value !== 'object' ||
|
|
201
|
+
descriptor.value === null) {
|
|
202
|
+
throw new error_1.OpenAIError('Chat completion stream contains an unsafe structured JSON snapshot');
|
|
203
|
+
}
|
|
204
|
+
return descriptor.value;
|
|
205
|
+
}
|
|
206
|
+
function captureSnapshotArray(snapshot, property, maximum, kind) {
|
|
207
|
+
const descriptor = Object.getOwnPropertyDescriptor(snapshot, property);
|
|
208
|
+
if (!descriptor) {
|
|
209
|
+
let prototype = Object.getPrototypeOf(snapshot);
|
|
210
|
+
for (let depth = 0; prototype !== null; depth += 1) {
|
|
211
|
+
if (depth >= MAX_PARTIAL_JSON_DEPTH || Object.getOwnPropertyDescriptor(prototype, property)) {
|
|
212
|
+
throw new error_1.OpenAIError(`Chat completion stream contains an unsafe snapshot ${kind} collection`);
|
|
213
|
+
}
|
|
214
|
+
prototype = Object.getPrototypeOf(prototype);
|
|
215
|
+
}
|
|
216
|
+
return undefined;
|
|
217
|
+
}
|
|
218
|
+
if (!('value' in descriptor) || !Array.isArray(descriptor.value)) {
|
|
219
|
+
throw new error_1.OpenAIError(`Chat completion stream contains an unsafe snapshot ${kind} collection`);
|
|
220
|
+
}
|
|
221
|
+
const length = Object.getOwnPropertyDescriptor(descriptor.value, 'length');
|
|
222
|
+
if (!length || !('value' in length) || !Number.isSafeInteger(length.value) || length.value > maximum) {
|
|
223
|
+
throw new error_1.OpenAIError(`Chat completion stream exceeded its snapshot ${kind} limit`);
|
|
224
|
+
}
|
|
225
|
+
return descriptor.value;
|
|
226
|
+
}
|
|
227
|
+
function captureSnapshotArrayItem(array, index) {
|
|
228
|
+
const descriptor = Object.getOwnPropertyDescriptor(array, index);
|
|
229
|
+
if (!descriptor) {
|
|
230
|
+
return undefined;
|
|
231
|
+
}
|
|
232
|
+
if (!('value' in descriptor)) {
|
|
233
|
+
throw new error_1.OpenAIError('Chat completion stream contains an unsafe structured JSON snapshot');
|
|
234
|
+
}
|
|
235
|
+
return descriptor.value;
|
|
236
|
+
}
|
|
237
|
+
function mapCapturedSnapshotArray(array, maximum, kind, map) {
|
|
238
|
+
const descriptor = Object.getOwnPropertyDescriptor(array, 'length');
|
|
239
|
+
const length = descriptor && 'value' in descriptor ? descriptor.value : undefined;
|
|
240
|
+
if (typeof length !== 'number' || !Number.isSafeInteger(length) || length < 0 || length > maximum) {
|
|
241
|
+
throw new error_1.OpenAIError(`Chat completion stream exceeded its snapshot ${kind} limit`);
|
|
242
|
+
}
|
|
243
|
+
const mapped = [];
|
|
244
|
+
mapped.length = length;
|
|
245
|
+
for (let index = 0; index < length; index += 1) {
|
|
246
|
+
const item = Object.getOwnPropertyDescriptor(array, index);
|
|
247
|
+
if (!item) {
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
if (!('value' in item)) {
|
|
251
|
+
throw new error_1.OpenAIError('Chat completion stream contains an unsafe structured JSON snapshot');
|
|
252
|
+
}
|
|
253
|
+
mapped[index] = map(item.value, index);
|
|
254
|
+
}
|
|
255
|
+
return mapped;
|
|
256
|
+
}
|
|
257
|
+
function validateStructuredJSONSnapshot(value, budget, validationWorkBudget) {
|
|
258
|
+
const state = createPartialJSONParseState();
|
|
259
|
+
const parseBudget = budget ?? { bytes: 0, fragments: 0, work: 0 };
|
|
260
|
+
recordPartialJSONFragment(state, parseBudget, value, validationWorkBudget);
|
|
261
|
+
if (!reservePartialJSONParse(state, parseBudget)) {
|
|
262
|
+
throw new error_1.OpenAIError('Chat completion stream exceeded its structured JSON parse-work limit');
|
|
263
|
+
}
|
|
264
|
+
return value;
|
|
265
|
+
}
|
|
266
|
+
function ownFunctionToolIdentity(toolCall) {
|
|
267
|
+
const type = Object.getOwnPropertyDescriptor(toolCall, 'type');
|
|
268
|
+
const fn = Object.getOwnPropertyDescriptor(toolCall, 'function');
|
|
269
|
+
if (!type || !('value' in type) || type.value !== 'function' || !fn || !('value' in fn)) {
|
|
270
|
+
return undefined;
|
|
271
|
+
}
|
|
272
|
+
if (typeof fn.value !== 'object' || fn.value === null) {
|
|
273
|
+
return undefined;
|
|
274
|
+
}
|
|
275
|
+
const name = Object.getOwnPropertyDescriptor(fn.value, 'name');
|
|
276
|
+
if (!name || !('value' in name) || typeof name.value !== 'string' || name.value.length === 0) {
|
|
277
|
+
return undefined;
|
|
278
|
+
}
|
|
279
|
+
return { type: 'function', name: name.value };
|
|
280
|
+
}
|
|
281
|
+
function assertBoundToolCallIdentity(toolCall, identity) {
|
|
282
|
+
const current = ownFunctionToolIdentity(toolCall);
|
|
283
|
+
if (!current || current.name !== identity.name || current.type !== identity.type) {
|
|
284
|
+
throw new error_1.OpenAIError('Chat completion stream contains a changed tool call identity');
|
|
285
|
+
}
|
|
286
|
+
}
|
|
49
287
|
function assignOwnProperties(target, source) {
|
|
50
288
|
if (Object.prototype.propertyIsEnumerable.call(source, '__proto__') && !(0, values_1.hasOwn)(target, '__proto__')) {
|
|
51
289
|
Object.defineProperty(target, '__proto__', {
|
|
@@ -57,6 +295,474 @@ function assignOwnProperties(target, source) {
|
|
|
57
295
|
}
|
|
58
296
|
return Object.assign(target, source);
|
|
59
297
|
}
|
|
298
|
+
function cloneParserConfigObject(value, stableFields = []) {
|
|
299
|
+
const descriptors = Object.getOwnPropertyDescriptors(value);
|
|
300
|
+
for (const field of stableFields) {
|
|
301
|
+
const descriptor = descriptors[field];
|
|
302
|
+
if (!descriptor && !(field in value)) {
|
|
303
|
+
continue;
|
|
304
|
+
}
|
|
305
|
+
descriptors[field] = {
|
|
306
|
+
value: descriptor && 'value' in descriptor ? descriptor.value : Reflect.get(value, field, value),
|
|
307
|
+
enumerable: descriptor?.enumerable ?? false,
|
|
308
|
+
configurable: descriptor?.configurable ?? true,
|
|
309
|
+
writable: descriptor && 'writable' in descriptor ? descriptor.writable : false,
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
return Object.create(Object.getPrototypeOf(value), descriptors);
|
|
313
|
+
}
|
|
314
|
+
function snapshotChatCompletionParserParams(params) {
|
|
315
|
+
const snapshot = cloneParserConfigObject(params);
|
|
316
|
+
if (params.tools) {
|
|
317
|
+
const stableTools = [];
|
|
318
|
+
const lengthDescriptor = Object.getOwnPropertyDescriptor(params.tools, 'length');
|
|
319
|
+
const length = lengthDescriptor && 'value' in lengthDescriptor ? lengthDescriptor.value : undefined;
|
|
320
|
+
const toolCount = typeof length === 'number' && Number.isSafeInteger(length) && length >= 0
|
|
321
|
+
? Math.min(length, MAX_STREAM_TOOL_CALLS)
|
|
322
|
+
: 0;
|
|
323
|
+
for (let index = 0; index < toolCount; index += 1) {
|
|
324
|
+
const item = Object.getOwnPropertyDescriptor(params.tools, String(index));
|
|
325
|
+
if (!item || !('value' in item)) {
|
|
326
|
+
stableTools.length = index + 1;
|
|
327
|
+
continue;
|
|
328
|
+
}
|
|
329
|
+
const tool = item.value;
|
|
330
|
+
const stableTool = cloneParserConfigObject(tool, [
|
|
331
|
+
'type',
|
|
332
|
+
'$brand',
|
|
333
|
+
'$parseRaw',
|
|
334
|
+
'$callback',
|
|
335
|
+
'function',
|
|
336
|
+
]);
|
|
337
|
+
const descriptors = Object.getOwnPropertyDescriptors(stableTool);
|
|
338
|
+
if ((0, parser_2.isChatCompletionFunctionTool)(stableTool)) {
|
|
339
|
+
const descriptor = descriptors.function;
|
|
340
|
+
descriptors.function = {
|
|
341
|
+
...(descriptor && 'value' in descriptor
|
|
342
|
+
? descriptor
|
|
343
|
+
: { configurable: true, enumerable: true, writable: true }),
|
|
344
|
+
value: cloneParserConfigObject(stableTool.function, ['name', 'strict']),
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
stableTools[index] = Object.create(Object.getPrototypeOf(tool), descriptors);
|
|
348
|
+
}
|
|
349
|
+
snapshot.tools = stableTools;
|
|
350
|
+
}
|
|
351
|
+
if (params.response_format) {
|
|
352
|
+
snapshot.response_format = cloneParserConfigObject(params.response_format, [
|
|
353
|
+
'type',
|
|
354
|
+
'$brand',
|
|
355
|
+
'$parseRaw',
|
|
356
|
+
]);
|
|
357
|
+
}
|
|
358
|
+
return snapshot;
|
|
359
|
+
}
|
|
360
|
+
const MAX_SERIALIZED_PARSER_SCHEMA_NODES = 4096;
|
|
361
|
+
const stringifyParserSchemaValue = JSON.stringify;
|
|
362
|
+
const MAX_SERIALIZED_PARSER_SCHEMA_BYTES = 1024 * 1024;
|
|
363
|
+
const MAX_SERIALIZED_PARSER_SCHEMA_DEPTH = 64;
|
|
364
|
+
const OMITTED_SERIALIZED_PARSER_VALUE = Symbol('omitted serialized parser value');
|
|
365
|
+
const UNSAFE_SERIALIZED_PARSER_VALUE = Symbol('unsafe serialized parser value');
|
|
366
|
+
function canonicalSerializedParserSchema(value, budget) {
|
|
367
|
+
const ancestors = new WeakSet();
|
|
368
|
+
const charge = (bytes) => {
|
|
369
|
+
if (!Number.isSafeInteger(bytes) ||
|
|
370
|
+
bytes < 0 ||
|
|
371
|
+
budget.bytes + bytes > MAX_SERIALIZED_PARSER_SCHEMA_BYTES) {
|
|
372
|
+
return false;
|
|
373
|
+
}
|
|
374
|
+
budget.bytes += bytes;
|
|
375
|
+
return true;
|
|
376
|
+
};
|
|
377
|
+
const visit = (current, depth) => {
|
|
378
|
+
if (depth > MAX_SERIALIZED_PARSER_SCHEMA_DEPTH || budget.nodes >= MAX_SERIALIZED_PARSER_SCHEMA_NODES) {
|
|
379
|
+
return UNSAFE_SERIALIZED_PARSER_VALUE;
|
|
380
|
+
}
|
|
381
|
+
budget.nodes += 1;
|
|
382
|
+
if (current === undefined || typeof current === 'function' || typeof current === 'symbol') {
|
|
383
|
+
return OMITTED_SERIALIZED_PARSER_VALUE;
|
|
384
|
+
}
|
|
385
|
+
if (typeof current === 'bigint') {
|
|
386
|
+
return UNSAFE_SERIALIZED_PARSER_VALUE;
|
|
387
|
+
}
|
|
388
|
+
if (current === null || typeof current === 'boolean' || typeof current === 'number') {
|
|
389
|
+
const serialized = stringifyParserSchemaValue(current);
|
|
390
|
+
return typeof serialized === 'string' && charge(serialized.length)
|
|
391
|
+
? serialized
|
|
392
|
+
: UNSAFE_SERIALIZED_PARSER_VALUE;
|
|
393
|
+
}
|
|
394
|
+
if (typeof current === 'string') {
|
|
395
|
+
if (!charge(current.length * 6 + 2)) {
|
|
396
|
+
return UNSAFE_SERIALIZED_PARSER_VALUE;
|
|
397
|
+
}
|
|
398
|
+
return stringifyParserSchemaValue(current);
|
|
399
|
+
}
|
|
400
|
+
if (typeof current !== 'object' || ancestors.has(current)) {
|
|
401
|
+
return UNSAFE_SERIALIZED_PARSER_VALUE;
|
|
402
|
+
}
|
|
403
|
+
const array = Array.isArray(current);
|
|
404
|
+
const prototype = Object.getPrototypeOf(current);
|
|
405
|
+
if ((array && prototype !== Array.prototype) ||
|
|
406
|
+
(!array && prototype !== null && prototype !== Object.prototype)) {
|
|
407
|
+
return UNSAFE_SERIALIZED_PARSER_VALUE;
|
|
408
|
+
}
|
|
409
|
+
for (let owner = current; owner !== null; owner = Object.getPrototypeOf(owner)) {
|
|
410
|
+
const serializer = Object.getOwnPropertyDescriptor(owner, 'toJSON');
|
|
411
|
+
if (!serializer) {
|
|
412
|
+
continue;
|
|
413
|
+
}
|
|
414
|
+
if (!('value' in serializer) || typeof serializer.value === 'function') {
|
|
415
|
+
return UNSAFE_SERIALIZED_PARSER_VALUE;
|
|
416
|
+
}
|
|
417
|
+
break;
|
|
418
|
+
}
|
|
419
|
+
ancestors.add(current);
|
|
420
|
+
try {
|
|
421
|
+
if (!charge(2)) {
|
|
422
|
+
return UNSAFE_SERIALIZED_PARSER_VALUE;
|
|
423
|
+
}
|
|
424
|
+
if (array) {
|
|
425
|
+
const lengthDescriptor = Object.getOwnPropertyDescriptor(current, 'length');
|
|
426
|
+
const length = lengthDescriptor && 'value' in lengthDescriptor ? lengthDescriptor.value : undefined;
|
|
427
|
+
if (typeof length !== 'number' ||
|
|
428
|
+
!Number.isSafeInteger(length) ||
|
|
429
|
+
length < 0 ||
|
|
430
|
+
length > MAX_SERIALIZED_PARSER_SCHEMA_NODES - budget.nodes) {
|
|
431
|
+
return UNSAFE_SERIALIZED_PARSER_VALUE;
|
|
432
|
+
}
|
|
433
|
+
const items = [];
|
|
434
|
+
for (let index = 0; index < length; index += 1) {
|
|
435
|
+
const key = String(index);
|
|
436
|
+
const descriptor = Object.getOwnPropertyDescriptor(current, key);
|
|
437
|
+
if (!descriptor) {
|
|
438
|
+
if (Object.getOwnPropertyDescriptor(Array.prototype, key) ||
|
|
439
|
+
Object.getOwnPropertyDescriptor(Object.prototype, key)) {
|
|
440
|
+
return UNSAFE_SERIALIZED_PARSER_VALUE;
|
|
441
|
+
}
|
|
442
|
+
budget.nodes += 1;
|
|
443
|
+
if (!charge(4)) {
|
|
444
|
+
return UNSAFE_SERIALIZED_PARSER_VALUE;
|
|
445
|
+
}
|
|
446
|
+
items.push('null');
|
|
447
|
+
continue;
|
|
448
|
+
}
|
|
449
|
+
if (!('value' in descriptor)) {
|
|
450
|
+
return UNSAFE_SERIALIZED_PARSER_VALUE;
|
|
451
|
+
}
|
|
452
|
+
const item = visit(descriptor.value, depth + 1);
|
|
453
|
+
if (item === UNSAFE_SERIALIZED_PARSER_VALUE) {
|
|
454
|
+
return item;
|
|
455
|
+
}
|
|
456
|
+
items.push(item === OMITTED_SERIALIZED_PARSER_VALUE ? 'null' : item);
|
|
457
|
+
}
|
|
458
|
+
return `[${items.join(',')}]`;
|
|
459
|
+
}
|
|
460
|
+
const keys = Reflect.ownKeys(current);
|
|
461
|
+
if (keys.length > MAX_SERIALIZED_PARSER_SCHEMA_NODES - budget.nodes) {
|
|
462
|
+
return UNSAFE_SERIALIZED_PARSER_VALUE;
|
|
463
|
+
}
|
|
464
|
+
const entries = [];
|
|
465
|
+
for (const key of keys) {
|
|
466
|
+
if (typeof key !== 'string') {
|
|
467
|
+
continue;
|
|
468
|
+
}
|
|
469
|
+
const descriptor = Object.getOwnPropertyDescriptor(current, key);
|
|
470
|
+
if (!descriptor) {
|
|
471
|
+
return UNSAFE_SERIALIZED_PARSER_VALUE;
|
|
472
|
+
}
|
|
473
|
+
if (!descriptor.enumerable) {
|
|
474
|
+
continue;
|
|
475
|
+
}
|
|
476
|
+
if (!('value' in descriptor)) {
|
|
477
|
+
return UNSAFE_SERIALIZED_PARSER_VALUE;
|
|
478
|
+
}
|
|
479
|
+
entries.push([key, descriptor.value]);
|
|
480
|
+
}
|
|
481
|
+
entries.sort(([left], [right]) => {
|
|
482
|
+
if (left === right) {
|
|
483
|
+
return 0;
|
|
484
|
+
}
|
|
485
|
+
return left < right ? -1 : 1;
|
|
486
|
+
});
|
|
487
|
+
const fields = [];
|
|
488
|
+
for (const [key, entry] of entries) {
|
|
489
|
+
const normalized = visit(entry, depth + 1);
|
|
490
|
+
if (normalized === UNSAFE_SERIALIZED_PARSER_VALUE) {
|
|
491
|
+
return normalized;
|
|
492
|
+
}
|
|
493
|
+
if (normalized === OMITTED_SERIALIZED_PARSER_VALUE) {
|
|
494
|
+
continue;
|
|
495
|
+
}
|
|
496
|
+
if (!charge(key.length * 6 + 3)) {
|
|
497
|
+
return UNSAFE_SERIALIZED_PARSER_VALUE;
|
|
498
|
+
}
|
|
499
|
+
fields.push(`${stringifyParserSchemaValue(key)}:${normalized}`);
|
|
500
|
+
}
|
|
501
|
+
return `{${fields.join(',')}}`;
|
|
502
|
+
}
|
|
503
|
+
finally {
|
|
504
|
+
ancestors.delete(current);
|
|
505
|
+
}
|
|
506
|
+
};
|
|
507
|
+
try {
|
|
508
|
+
const normalized = visit(value, 0);
|
|
509
|
+
return typeof normalized === 'string' ? normalized : undefined;
|
|
510
|
+
}
|
|
511
|
+
catch {
|
|
512
|
+
return undefined;
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
function rememberSerializedParserSchema(signatures, source, holder, key) {
|
|
516
|
+
const parser = Object.getOwnPropertyDescriptor(source, '$parseRaw');
|
|
517
|
+
const schema = Object.getOwnPropertyDescriptor(holder, key);
|
|
518
|
+
if (!parser ||
|
|
519
|
+
!('value' in parser) ||
|
|
520
|
+
typeof parser.value !== 'function' ||
|
|
521
|
+
!schema ||
|
|
522
|
+
!('value' in schema)) {
|
|
523
|
+
return;
|
|
524
|
+
}
|
|
525
|
+
const normalized = canonicalSerializedParserSchema(schema.value, { nodes: 0, bytes: 0 });
|
|
526
|
+
if (normalized !== undefined) {
|
|
527
|
+
signatures.set(source, normalized);
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
function hasMatchingSerializedParserSchema(signatures, source, holder, key, value) {
|
|
531
|
+
const expected = source && signatures.get(source);
|
|
532
|
+
const descriptor = Object.getOwnPropertyDescriptor(holder, key);
|
|
533
|
+
return (expected !== undefined &&
|
|
534
|
+
descriptor !== undefined &&
|
|
535
|
+
'value' in descriptor &&
|
|
536
|
+
canonicalSerializedParserSchema(value, { nodes: 0, bytes: 0 }) === expected);
|
|
537
|
+
}
|
|
538
|
+
function serializedParserDescriptor(descriptor, value) {
|
|
539
|
+
return descriptor && 'value' in descriptor
|
|
540
|
+
? { ...descriptor, value }
|
|
541
|
+
: { configurable: true, enumerable: true, writable: true, value };
|
|
542
|
+
}
|
|
543
|
+
function shadowSerializedParserMetadata(descriptors, source, fields) {
|
|
544
|
+
for (const field of fields) {
|
|
545
|
+
const descriptor = descriptors[field];
|
|
546
|
+
if (!descriptor && !(field in source)) {
|
|
547
|
+
continue;
|
|
548
|
+
}
|
|
549
|
+
descriptors[field] =
|
|
550
|
+
descriptor && 'value' in descriptor
|
|
551
|
+
? { ...descriptor, value: undefined }
|
|
552
|
+
: {
|
|
553
|
+
configurable: descriptor?.configurable ?? true,
|
|
554
|
+
enumerable: descriptor?.enumerable ?? false,
|
|
555
|
+
writable: false,
|
|
556
|
+
value: undefined,
|
|
557
|
+
};
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
function snapshotSerializedParserTool(serialized) {
|
|
561
|
+
const source = serialized.source ??
|
|
562
|
+
{
|
|
563
|
+
type: serialized.type,
|
|
564
|
+
...(serialized.type === 'function' ? { function: {} } : {}),
|
|
565
|
+
};
|
|
566
|
+
const descriptors = Object.getOwnPropertyDescriptors(source);
|
|
567
|
+
descriptors.type = serializedParserDescriptor(descriptors.type, serialized.type);
|
|
568
|
+
if (serialized.type !== 'function' || !serialized.function) {
|
|
569
|
+
if (descriptors.function) {
|
|
570
|
+
descriptors.function = serializedParserDescriptor(descriptors.function, undefined);
|
|
571
|
+
}
|
|
572
|
+
shadowSerializedParserMetadata(descriptors, source, ['$brand', '$parseRaw', '$callback']);
|
|
573
|
+
return Object.create(Object.getPrototypeOf(source), descriptors);
|
|
574
|
+
}
|
|
575
|
+
const descriptor = descriptors.function;
|
|
576
|
+
const original = descriptor && 'value' in descriptor && typeof descriptor.value === 'object' && descriptor.value !== null
|
|
577
|
+
? descriptor.value
|
|
578
|
+
: {};
|
|
579
|
+
const functionDescriptors = Object.getOwnPropertyDescriptors(original);
|
|
580
|
+
functionDescriptors['name'] = serializedParserDescriptor(functionDescriptors['name'], serialized.function.name);
|
|
581
|
+
functionDescriptors['strict'] = serializedParserDescriptor(functionDescriptors['strict'], serialized.function.strict);
|
|
582
|
+
descriptors.function = serializedParserDescriptor(descriptor, Object.create(Object.getPrototypeOf(original), functionDescriptors));
|
|
583
|
+
if (!serialized.function.schemaMatches) {
|
|
584
|
+
shadowSerializedParserMetadata(descriptors, source, ['$brand', '$parseRaw', '$callback']);
|
|
585
|
+
}
|
|
586
|
+
return Object.create(Object.getPrototypeOf(source), descriptors);
|
|
587
|
+
}
|
|
588
|
+
function snapshotSerializedResponseFormat(serialized) {
|
|
589
|
+
const source = serialized.source ?? { type: serialized.type };
|
|
590
|
+
const descriptors = Object.getOwnPropertyDescriptors(source);
|
|
591
|
+
descriptors.type = serializedParserDescriptor(descriptors.type, serialized.type);
|
|
592
|
+
if (serialized.type !== 'json_schema' || !serialized.source || !serialized.schemaMatches) {
|
|
593
|
+
shadowSerializedParserMetadata(descriptors, source, ['$brand', '$parseRaw']);
|
|
594
|
+
}
|
|
595
|
+
return Object.create(Object.getPrototypeOf(source), descriptors);
|
|
596
|
+
}
|
|
597
|
+
function ownSerializedParserObject(holder, key) {
|
|
598
|
+
const descriptor = Object.getOwnPropertyDescriptor(holder, key);
|
|
599
|
+
if (!descriptor || !('value' in descriptor)) {
|
|
600
|
+
return undefined;
|
|
601
|
+
}
|
|
602
|
+
const { value } = descriptor;
|
|
603
|
+
return typeof value === 'object' && value !== null ? value : undefined;
|
|
604
|
+
}
|
|
605
|
+
function observeSerializedChatCompletionParserParams(body, initial, update) {
|
|
606
|
+
const originalToolOwners = new WeakMap();
|
|
607
|
+
const originalSchemaSignatures = new WeakMap();
|
|
608
|
+
// At most 128 tools plus one response format each receive an independent
|
|
609
|
+
// 4,096-node/1 MiB source and wire allowance (129 MiB maximum per pass).
|
|
610
|
+
if (body.tools) {
|
|
611
|
+
for (let index = 0; index < body.tools.length && index < MAX_STREAM_TOOL_CALLS; index += 1) {
|
|
612
|
+
const owner = ownSerializedParserObject(body.tools, String(index));
|
|
613
|
+
const source = initial.tools?.[index];
|
|
614
|
+
if (owner && source) {
|
|
615
|
+
originalToolOwners.set(owner, source);
|
|
616
|
+
const originalFunction = ownSerializedParserObject(source, 'function');
|
|
617
|
+
if (originalFunction) {
|
|
618
|
+
rememberSerializedParserSchema(originalSchemaSignatures, source, originalFunction, 'parameters');
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
if (initial.response_format) {
|
|
624
|
+
rememberSerializedParserSchema(originalSchemaSignatures, initial.response_format, initial.response_format, 'json_schema');
|
|
625
|
+
}
|
|
626
|
+
let root;
|
|
627
|
+
let tools;
|
|
628
|
+
let responseFormat;
|
|
629
|
+
let responseFrame;
|
|
630
|
+
let frames = [];
|
|
631
|
+
let toolFrames = new WeakMap();
|
|
632
|
+
let actualToolOwners = new Map();
|
|
633
|
+
let functionFrames = new WeakMap();
|
|
634
|
+
return (0, request_options_1.observeJSONRequestBody)(body, {
|
|
635
|
+
value(holder, key, value) {
|
|
636
|
+
if (!root && key === '' && typeof value === 'object' && value !== null) {
|
|
637
|
+
root = value;
|
|
638
|
+
tools = undefined;
|
|
639
|
+
responseFormat = undefined;
|
|
640
|
+
responseFrame = undefined;
|
|
641
|
+
frames = [];
|
|
642
|
+
toolFrames = new WeakMap();
|
|
643
|
+
actualToolOwners = new Map();
|
|
644
|
+
functionFrames = new WeakMap();
|
|
645
|
+
return;
|
|
646
|
+
}
|
|
647
|
+
if (holder === root && key === 'response_format') {
|
|
648
|
+
if (typeof value === 'object' && value !== null) {
|
|
649
|
+
responseFormat = value;
|
|
650
|
+
const owner = ownSerializedParserObject(holder, key);
|
|
651
|
+
responseFrame = {
|
|
652
|
+
source: owner === body.response_format ? initial.response_format : undefined,
|
|
653
|
+
schemaMatches: false,
|
|
654
|
+
};
|
|
655
|
+
}
|
|
656
|
+
return;
|
|
657
|
+
}
|
|
658
|
+
if (holder === root && key === 'tools') {
|
|
659
|
+
if (Array.isArray(value)) {
|
|
660
|
+
tools = new Proxy(value, {
|
|
661
|
+
get(target, property) {
|
|
662
|
+
const actual = Reflect.get(target, property, target);
|
|
663
|
+
if (typeof property === 'string') {
|
|
664
|
+
const index = Number(property);
|
|
665
|
+
if (Number.isSafeInteger(index) &&
|
|
666
|
+
index >= 0 &&
|
|
667
|
+
index < MAX_STREAM_TOOL_CALLS &&
|
|
668
|
+
String(index) === property) {
|
|
669
|
+
actualToolOwners.set(index, typeof actual === 'object' && actual !== null ? actual : undefined);
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
return actual;
|
|
673
|
+
},
|
|
674
|
+
});
|
|
675
|
+
return tools;
|
|
676
|
+
}
|
|
677
|
+
return;
|
|
678
|
+
}
|
|
679
|
+
if (holder === tools) {
|
|
680
|
+
const index = Number(key);
|
|
681
|
+
if (!Number.isSafeInteger(index) ||
|
|
682
|
+
index < 0 ||
|
|
683
|
+
index >= MAX_STREAM_TOOL_CALLS ||
|
|
684
|
+
typeof value !== 'object' ||
|
|
685
|
+
value === null) {
|
|
686
|
+
return;
|
|
687
|
+
}
|
|
688
|
+
const owner = actualToolOwners.get(index);
|
|
689
|
+
const source = owner ? originalToolOwners.get(owner) : undefined;
|
|
690
|
+
const frame = { source };
|
|
691
|
+
frames[index] = frame;
|
|
692
|
+
toolFrames.set(value, frame);
|
|
693
|
+
return;
|
|
694
|
+
}
|
|
695
|
+
const tool = toolFrames.get(holder);
|
|
696
|
+
if (tool) {
|
|
697
|
+
if (key === 'type' && typeof value === 'string') {
|
|
698
|
+
tool.type = value;
|
|
699
|
+
}
|
|
700
|
+
else if (key === 'function' && typeof value === 'object' && value !== null) {
|
|
701
|
+
const fn = { source: tool.source, schemaMatches: false };
|
|
702
|
+
tool.function = fn;
|
|
703
|
+
functionFrames.set(value, fn);
|
|
704
|
+
}
|
|
705
|
+
return;
|
|
706
|
+
}
|
|
707
|
+
if (holder === responseFormat && responseFrame) {
|
|
708
|
+
if (key === 'type' && typeof value === 'string') {
|
|
709
|
+
responseFrame.type = value;
|
|
710
|
+
}
|
|
711
|
+
else if (key === 'json_schema') {
|
|
712
|
+
responseFrame.schemaMatches = hasMatchingSerializedParserSchema(originalSchemaSignatures, responseFrame.source, holder, key, value);
|
|
713
|
+
}
|
|
714
|
+
return;
|
|
715
|
+
}
|
|
716
|
+
const fn = functionFrames.get(holder);
|
|
717
|
+
if (fn) {
|
|
718
|
+
if (key === 'name' && typeof value === 'string') {
|
|
719
|
+
fn.name = value;
|
|
720
|
+
}
|
|
721
|
+
else if (key === 'strict' && typeof value === 'boolean') {
|
|
722
|
+
fn.strict = value;
|
|
723
|
+
}
|
|
724
|
+
else if (key === 'parameters') {
|
|
725
|
+
fn.schemaMatches = hasMatchingSerializedParserSchema(originalSchemaSignatures, fn.source, holder, key, value);
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
return undefined;
|
|
729
|
+
},
|
|
730
|
+
complete() {
|
|
731
|
+
if (!root) {
|
|
732
|
+
return;
|
|
733
|
+
}
|
|
734
|
+
const snapshot = cloneParserConfigObject(initial);
|
|
735
|
+
if (tools) {
|
|
736
|
+
const serializedTools = [];
|
|
737
|
+
for (let index = 0; index < frames.length; index += 1) {
|
|
738
|
+
const frame = frames[index];
|
|
739
|
+
if (frame) {
|
|
740
|
+
serializedTools[index] = snapshotSerializedParserTool(frame);
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
snapshot.tools = serializedTools;
|
|
744
|
+
}
|
|
745
|
+
else {
|
|
746
|
+
delete snapshot.tools;
|
|
747
|
+
}
|
|
748
|
+
if (responseFrame) {
|
|
749
|
+
snapshot.response_format = snapshotSerializedResponseFormat(responseFrame);
|
|
750
|
+
}
|
|
751
|
+
else {
|
|
752
|
+
delete snapshot.response_format;
|
|
753
|
+
}
|
|
754
|
+
update(snapshot);
|
|
755
|
+
root = undefined;
|
|
756
|
+
tools = undefined;
|
|
757
|
+
responseFormat = undefined;
|
|
758
|
+
responseFrame = undefined;
|
|
759
|
+
frames = [];
|
|
760
|
+
toolFrames = new WeakMap();
|
|
761
|
+
actualToolOwners = new Map();
|
|
762
|
+
functionFrames = new WeakMap();
|
|
763
|
+
},
|
|
764
|
+
});
|
|
765
|
+
}
|
|
60
766
|
/** Streams chat completion chunks while accumulating snapshots, parsed output, and events. */
|
|
61
767
|
class ChatCompletionStream extends AbstractChatCompletionRunner_1.AbstractChatCompletionRunner {
|
|
62
768
|
/** Creates an unstarted stream, retaining request parameters for structured-output parsing. */
|
|
@@ -67,9 +773,30 @@ class ChatCompletionStream extends AbstractChatCompletionRunner_1.AbstractChatCo
|
|
|
67
773
|
_ChatCompletionStream_audioDoneChoiceIndexes.set(this, void 0);
|
|
68
774
|
_ChatCompletionStream_choiceEventStates.set(this, void 0);
|
|
69
775
|
_ChatCompletionStream_currentChatCompletionSnapshot.set(this, void 0);
|
|
776
|
+
_ChatCompletionStream_hasAutoParseableTool.set(this, void 0);
|
|
777
|
+
_ChatCompletionStream_partialJSONParseBudget.set(this, void 0);
|
|
70
778
|
tslib_1.__classPrivateFieldSet(this, _ChatCompletionStream_params, params, "f");
|
|
71
779
|
tslib_1.__classPrivateFieldSet(this, _ChatCompletionStream_audioDoneChoiceIndexes, new Set(), "f");
|
|
72
780
|
tslib_1.__classPrivateFieldSet(this, _ChatCompletionStream_choiceEventStates, [], "f");
|
|
781
|
+
tslib_1.__classPrivateFieldSet(this, _ChatCompletionStream_hasAutoParseableTool, false, "f");
|
|
782
|
+
const tools = params?.tools;
|
|
783
|
+
const lengthDescriptor = tools && Object.getOwnPropertyDescriptor(tools, 'length');
|
|
784
|
+
const length = lengthDescriptor && 'value' in lengthDescriptor ? lengthDescriptor.value : undefined;
|
|
785
|
+
if (tools && typeof length === 'number' && Number.isSafeInteger(length) && length >= 0) {
|
|
786
|
+
for (let index = 0; index < Math.min(length, MAX_STREAM_TOOL_CALLS); index += 1) {
|
|
787
|
+
const descriptor = Object.getOwnPropertyDescriptor(tools, String(index));
|
|
788
|
+
if (!descriptor || !('value' in descriptor)) {
|
|
789
|
+
continue;
|
|
790
|
+
}
|
|
791
|
+
const tool = descriptor.value;
|
|
792
|
+
if ((0, parser_2.isChatCompletionFunctionTool)(tool) &&
|
|
793
|
+
((0, parser_2.isAutoParsableTool)(tool) || tool.function.strict === true)) {
|
|
794
|
+
tslib_1.__classPrivateFieldSet(this, _ChatCompletionStream_hasAutoParseableTool, true, "f");
|
|
795
|
+
break;
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
tslib_1.__classPrivateFieldSet(this, _ChatCompletionStream_partialJSONParseBudget, { bytes: 0, fragments: 0, work: 0 }, "f");
|
|
73
800
|
}
|
|
74
801
|
/** The latest accumulated completion, or `undefined` before a chunk arrives or after finalization. */
|
|
75
802
|
get currentChatCompletionSnapshot() {
|
|
@@ -95,8 +822,25 @@ class ChatCompletionStream extends AbstractChatCompletionRunner_1.AbstractChatCo
|
|
|
95
822
|
}
|
|
96
823
|
async _createChatCompletion(client, params, options) {
|
|
97
824
|
this._listenForAbort(options?.signal);
|
|
825
|
+
const requestParams = { ...params, stream: true };
|
|
826
|
+
tslib_1.__classPrivateFieldSet(this, _ChatCompletionStream_params, requestParams, "f");
|
|
98
827
|
tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_beginRequest).call(this);
|
|
99
|
-
const
|
|
828
|
+
const parserParams = snapshotChatCompletionParserParams(requestParams);
|
|
829
|
+
tslib_1.__classPrivateFieldSet(this, _ChatCompletionStream_params, parserParams, "f");
|
|
830
|
+
tslib_1.__classPrivateFieldSet(this, _ChatCompletionStream_hasAutoParseableTool, parserParams.tools?.some((tool) => (0, parser_2.isChatCompletionFunctionTool)(tool) && ((0, parser_2.isAutoParsableTool)(tool) || tool.function.strict === true)) ?? false, "f");
|
|
831
|
+
const stopObserving = requestParams.tools || requestParams.response_format
|
|
832
|
+
? observeSerializedChatCompletionParserParams(requestParams, parserParams, (serialized) => {
|
|
833
|
+
tslib_1.__classPrivateFieldSet(this, _ChatCompletionStream_params, serialized, "f");
|
|
834
|
+
tslib_1.__classPrivateFieldSet(this, _ChatCompletionStream_hasAutoParseableTool, serialized.tools?.some((tool) => (0, parser_2.isChatCompletionFunctionTool)(tool) &&
|
|
835
|
+
((0, parser_2.isAutoParsableTool)(tool) || tool.function.strict === true)) ?? false, "f");
|
|
836
|
+
})
|
|
837
|
+
: undefined;
|
|
838
|
+
const stream = await client.chat.completions
|
|
839
|
+
.create(requestParams, {
|
|
840
|
+
...options,
|
|
841
|
+
signal: this.controller.signal,
|
|
842
|
+
})
|
|
843
|
+
.finally(stopObserving);
|
|
100
844
|
this._connected();
|
|
101
845
|
for await (const chunk of stream) {
|
|
102
846
|
tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_addChunk).call(this, chunk);
|
|
@@ -152,12 +896,13 @@ class ChatCompletionStream extends AbstractChatCompletionRunner_1.AbstractChatCo
|
|
|
152
896
|
throw new error_1.OpenAIError(`request ended without sending any chunks`);
|
|
153
897
|
}
|
|
154
898
|
/** Iterates over raw API chunks; stopping iteration early aborts the underlying request. */
|
|
155
|
-
[(_ChatCompletionStream_params = new WeakMap(), _ChatCompletionStream_audioDoneChoiceIndexes = new WeakMap(), _ChatCompletionStream_choiceEventStates = new WeakMap(), _ChatCompletionStream_currentChatCompletionSnapshot = new WeakMap(), _ChatCompletionStream_instances = new WeakSet(), _ChatCompletionStream_beginRequest = function _ChatCompletionStream_beginRequest() {
|
|
899
|
+
[(_ChatCompletionStream_params = new WeakMap(), _ChatCompletionStream_audioDoneChoiceIndexes = new WeakMap(), _ChatCompletionStream_choiceEventStates = new WeakMap(), _ChatCompletionStream_currentChatCompletionSnapshot = new WeakMap(), _ChatCompletionStream_hasAutoParseableTool = new WeakMap(), _ChatCompletionStream_partialJSONParseBudget = new WeakMap(), _ChatCompletionStream_instances = new WeakSet(), _ChatCompletionStream_beginRequest = function _ChatCompletionStream_beginRequest() {
|
|
156
900
|
if (this.ended) {
|
|
157
901
|
return;
|
|
158
902
|
}
|
|
159
903
|
tslib_1.__classPrivateFieldSet(this, _ChatCompletionStream_audioDoneChoiceIndexes, new Set(), "f");
|
|
160
904
|
tslib_1.__classPrivateFieldSet(this, _ChatCompletionStream_currentChatCompletionSnapshot, undefined, "f");
|
|
905
|
+
tslib_1.__classPrivateFieldSet(this, _ChatCompletionStream_partialJSONParseBudget, { bytes: 0, fragments: 0, work: 0 }, "f");
|
|
161
906
|
}, _ChatCompletionStream_getChoiceEventState = function _ChatCompletionStream_getChoiceEventState(choice) {
|
|
162
907
|
let state = tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_choiceEventStates, "f")[choice.index];
|
|
163
908
|
if (state) {
|
|
@@ -165,11 +910,14 @@ class ChatCompletionStream extends AbstractChatCompletionRunner_1.AbstractChatCo
|
|
|
165
910
|
}
|
|
166
911
|
state = {
|
|
167
912
|
content_done: false,
|
|
913
|
+
content_parse_state: undefined,
|
|
168
914
|
refusal_done: false,
|
|
169
915
|
logprobs_content_done: false,
|
|
170
916
|
logprobs_refusal_done: false,
|
|
171
917
|
done_tool_calls: new Set(),
|
|
172
918
|
current_tool_call_index: null,
|
|
919
|
+
tool_call_parse_states: new Map(),
|
|
920
|
+
tool_call_identities: new Map(),
|
|
173
921
|
};
|
|
174
922
|
tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_choiceEventStates, "f")[choice.index] = state;
|
|
175
923
|
return state;
|
|
@@ -177,36 +925,47 @@ class ChatCompletionStream extends AbstractChatCompletionRunner_1.AbstractChatCo
|
|
|
177
925
|
if (this.ended) {
|
|
178
926
|
return;
|
|
179
927
|
}
|
|
180
|
-
const
|
|
928
|
+
const capturedChoiceFrames = new WeakMap();
|
|
929
|
+
const completion = tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_accumulateChatCompletion).call(this, chunk, capturedChoiceFrames);
|
|
181
930
|
this._emit('chunk', chunk, completion);
|
|
182
931
|
for (const choice of chunk.choices) {
|
|
183
|
-
const
|
|
932
|
+
const capturedChoice = capturedChoiceFrames.get(choice);
|
|
933
|
+
const choiceSnapshot = completion.choices[capturedChoice?.index ?? choice.index];
|
|
934
|
+
const capturedToolCalls = capturedChoice?.tool_calls ?? [];
|
|
184
935
|
const { delta } = choice;
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
936
|
+
const structuredResponse = (0, parser_2.isParseableResponseFormat)(tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_params, "f")?.response_format);
|
|
937
|
+
const boundedSnapshot = structuredResponse || tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_hasAutoParseableTool, "f");
|
|
938
|
+
const messageSnapshot = boundedSnapshot
|
|
939
|
+
? captureStructuredMessageSnapshot(choiceSnapshot)
|
|
940
|
+
: choiceSnapshot.message;
|
|
941
|
+
const refusal = boundedSnapshot
|
|
942
|
+
? captureStructuredJSONSnapshot(messageSnapshot, 'refusal')
|
|
943
|
+
: messageSnapshot.refusal;
|
|
944
|
+
const parseableContent = !refusal && structuredResponse;
|
|
945
|
+
const messageContent = parseableContent
|
|
946
|
+
? captureStructuredJSONSnapshot(messageSnapshot, 'content')
|
|
947
|
+
: messageSnapshot.content;
|
|
948
|
+
if (delta?.content != null && messageSnapshot.role === 'assistant' && messageContent) {
|
|
949
|
+
this._emit('content', delta.content, messageContent);
|
|
189
950
|
this._emit('content.delta', {
|
|
190
951
|
delta: delta.content,
|
|
191
|
-
snapshot:
|
|
192
|
-
parsed:
|
|
952
|
+
snapshot: messageContent,
|
|
953
|
+
parsed: messageSnapshot.parsed,
|
|
193
954
|
});
|
|
194
955
|
}
|
|
195
|
-
if (delta?.refusal != null &&
|
|
196
|
-
choiceSnapshot.message?.role === 'assistant' &&
|
|
197
|
-
choiceSnapshot.message?.refusal) {
|
|
956
|
+
if (delta?.refusal != null && messageSnapshot.role === 'assistant' && refusal) {
|
|
198
957
|
this._emit('refusal.delta', {
|
|
199
958
|
delta: delta.refusal,
|
|
200
|
-
snapshot:
|
|
959
|
+
snapshot: refusal,
|
|
201
960
|
});
|
|
202
961
|
}
|
|
203
|
-
if (choice.logprobs?.content != null &&
|
|
962
|
+
if (choice.logprobs?.content != null && messageSnapshot.role === 'assistant') {
|
|
204
963
|
this._emit('logprobs.content.delta', {
|
|
205
964
|
content: choice.logprobs?.content,
|
|
206
965
|
snapshot: choiceSnapshot.logprobs?.content ?? [],
|
|
207
966
|
});
|
|
208
967
|
}
|
|
209
|
-
if (choice.logprobs?.refusal != null &&
|
|
968
|
+
if (choice.logprobs?.refusal != null && messageSnapshot.role === 'assistant') {
|
|
210
969
|
this._emit('logprobs.refusal.delta', {
|
|
211
970
|
refusal: choice.logprobs?.refusal,
|
|
212
971
|
snapshot: choiceSnapshot.logprobs?.refusal ?? [],
|
|
@@ -219,7 +978,7 @@ class ChatCompletionStream extends AbstractChatCompletionRunner_1.AbstractChatCo
|
|
|
219
978
|
tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_emitToolCallDoneEvent).call(this, choiceSnapshot, state.current_tool_call_index);
|
|
220
979
|
}
|
|
221
980
|
}
|
|
222
|
-
for (const toolCall of
|
|
981
|
+
for (const toolCall of capturedToolCalls) {
|
|
223
982
|
if (state.current_tool_call_index !== toolCall.index) {
|
|
224
983
|
tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_emitContentDoneEvents).call(this, choiceSnapshot);
|
|
225
984
|
// new tool call started, the previous one is done
|
|
@@ -229,18 +988,30 @@ class ChatCompletionStream extends AbstractChatCompletionRunner_1.AbstractChatCo
|
|
|
229
988
|
}
|
|
230
989
|
state.current_tool_call_index = toolCall.index;
|
|
231
990
|
}
|
|
232
|
-
for (const toolCallDelta of
|
|
233
|
-
const toolCallSnapshot =
|
|
991
|
+
for (const toolCallDelta of capturedToolCalls) {
|
|
992
|
+
const toolCallSnapshot = messageSnapshot.tool_calls?.[toolCallDelta.index];
|
|
234
993
|
if (!toolCallSnapshot?.type) {
|
|
235
994
|
continue;
|
|
236
995
|
}
|
|
237
996
|
if (toolCallSnapshot.type === 'function') {
|
|
997
|
+
const boundIdentity = state.tool_call_identities.get(toolCallDelta.index);
|
|
998
|
+
let argumentsSnapshot;
|
|
999
|
+
if (boundIdentity?.parseable) {
|
|
1000
|
+
const capturedArguments = captureStructuredJSONSnapshot(toolCallSnapshot.function, 'arguments');
|
|
1001
|
+
if (typeof capturedArguments !== 'string') {
|
|
1002
|
+
throw new error_1.OpenAIError('Chat completion stream contains an unsafe structured JSON snapshot');
|
|
1003
|
+
}
|
|
1004
|
+
argumentsSnapshot = capturedArguments;
|
|
1005
|
+
}
|
|
1006
|
+
else {
|
|
1007
|
+
argumentsSnapshot = toolCallSnapshot.function.arguments;
|
|
1008
|
+
}
|
|
238
1009
|
this._emit('tool_calls.function.arguments.delta', {
|
|
239
1010
|
name: toolCallSnapshot.function.name,
|
|
240
1011
|
index: toolCallDelta.index,
|
|
241
|
-
arguments:
|
|
1012
|
+
arguments: argumentsSnapshot,
|
|
242
1013
|
parsed_arguments: toolCallSnapshot.function.parsed_arguments,
|
|
243
|
-
arguments_delta: toolCallDelta.
|
|
1014
|
+
arguments_delta: toolCallDelta.arguments_delta,
|
|
244
1015
|
});
|
|
245
1016
|
}
|
|
246
1017
|
else if (toolCallSnapshot.type !== 'custom') {
|
|
@@ -254,26 +1025,48 @@ class ChatCompletionStream extends AbstractChatCompletionRunner_1.AbstractChatCo
|
|
|
254
1025
|
// we've already fired the done event
|
|
255
1026
|
return;
|
|
256
1027
|
}
|
|
257
|
-
const
|
|
1028
|
+
const messageSnapshot = tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_hasAutoParseableTool, "f")
|
|
1029
|
+
? captureStructuredMessageSnapshot(choiceSnapshot)
|
|
1030
|
+
: choiceSnapshot.message;
|
|
1031
|
+
const toolCallSnapshot = messageSnapshot.tool_calls?.[toolCallIndex];
|
|
258
1032
|
if (!toolCallSnapshot) {
|
|
259
1033
|
throw new Error('no tool call snapshot');
|
|
260
1034
|
}
|
|
1035
|
+
const boundIdentity = state.tool_call_identities.get(toolCallIndex);
|
|
1036
|
+
if (boundIdentity) {
|
|
1037
|
+
assertBoundToolCallIdentity(toolCallSnapshot, boundIdentity);
|
|
1038
|
+
}
|
|
261
1039
|
if (!toolCallSnapshot.type) {
|
|
262
1040
|
throw new Error('tool call snapshot missing `type`');
|
|
263
1041
|
}
|
|
264
1042
|
if (toolCallSnapshot.type === 'function') {
|
|
265
1043
|
const inputTool = tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_params, "f")?.tools?.find((tool) => (0, parser_2.isChatCompletionFunctionTool)(tool) && tool.function.name === toolCallSnapshot.function.name); // TS doesn't narrow based on isChatCompletionTool
|
|
266
1044
|
let parsedArguments = null;
|
|
1045
|
+
const parseable = (0, parser_2.isAutoParsableTool)(inputTool) || inputTool?.function.strict === true;
|
|
1046
|
+
let argumentsSnapshot;
|
|
1047
|
+
if (parseable) {
|
|
1048
|
+
if (tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_currentChatCompletionSnapshot, "f")) {
|
|
1049
|
+
tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_validateStructuredSnapshots).call(this, tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_currentChatCompletionSnapshot, "f"));
|
|
1050
|
+
}
|
|
1051
|
+
const capturedArguments = captureStructuredJSONSnapshot(toolCallSnapshot.function, 'arguments');
|
|
1052
|
+
if (typeof capturedArguments !== 'string') {
|
|
1053
|
+
throw new error_1.OpenAIError('Chat completion stream contains an unsafe structured JSON snapshot');
|
|
1054
|
+
}
|
|
1055
|
+
argumentsSnapshot = capturedArguments;
|
|
1056
|
+
}
|
|
1057
|
+
else {
|
|
1058
|
+
argumentsSnapshot = toolCallSnapshot.function.arguments;
|
|
1059
|
+
}
|
|
267
1060
|
if ((0, parser_2.isAutoParsableTool)(inputTool)) {
|
|
268
|
-
parsedArguments = inputTool.$parseRaw(
|
|
1061
|
+
parsedArguments = inputTool.$parseRaw(validateStructuredJSONSnapshot(argumentsSnapshot));
|
|
269
1062
|
}
|
|
270
1063
|
else if (inputTool?.function.strict) {
|
|
271
|
-
parsedArguments =
|
|
1064
|
+
parsedArguments = (0, parser_2.parseResponseFormatContent)({ type: 'json_schema', $parseRaw: undefined }, validateStructuredJSONSnapshot(argumentsSnapshot));
|
|
272
1065
|
}
|
|
273
1066
|
this._emit('tool_calls.function.arguments.done', {
|
|
274
1067
|
name: toolCallSnapshot.function.name,
|
|
275
1068
|
index: toolCallIndex,
|
|
276
|
-
arguments:
|
|
1069
|
+
arguments: argumentsSnapshot,
|
|
277
1070
|
parsed_arguments: parsedArguments,
|
|
278
1071
|
});
|
|
279
1072
|
}
|
|
@@ -282,23 +1075,36 @@ class ChatCompletionStream extends AbstractChatCompletionRunner_1.AbstractChatCo
|
|
|
282
1075
|
}
|
|
283
1076
|
}, _ChatCompletionStream_emitContentDoneEvents = function _ChatCompletionStream_emitContentDoneEvents(choiceSnapshot) {
|
|
284
1077
|
const state = tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_getChoiceEventState).call(this, choiceSnapshot);
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
1078
|
+
const structuredResponse = (0, parser_2.isParseableResponseFormat)(tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_params, "f")?.response_format);
|
|
1079
|
+
const boundedSnapshot = structuredResponse || tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_hasAutoParseableTool, "f");
|
|
1080
|
+
const messageSnapshot = boundedSnapshot
|
|
1081
|
+
? captureStructuredMessageSnapshot(choiceSnapshot)
|
|
1082
|
+
: choiceSnapshot.message;
|
|
1083
|
+
const refusal = boundedSnapshot
|
|
1084
|
+
? captureStructuredJSONSnapshot(messageSnapshot, 'refusal')
|
|
1085
|
+
: messageSnapshot.refusal;
|
|
1086
|
+
const parseableContent = !refusal && structuredResponse;
|
|
1087
|
+
const content = parseableContent
|
|
1088
|
+
? captureStructuredJSONSnapshot(messageSnapshot, 'content')
|
|
1089
|
+
: messageSnapshot.content;
|
|
1090
|
+
if (content != null &&
|
|
1091
|
+
(content !== '' ||
|
|
1092
|
+
(!refusal && !messageSnapshot.tool_calls?.length && !messageSnapshot.function_call)) &&
|
|
290
1093
|
!state.content_done) {
|
|
1094
|
+
if (parseableContent && tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_currentChatCompletionSnapshot, "f")) {
|
|
1095
|
+
tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_validateStructuredSnapshots).call(this, tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_currentChatCompletionSnapshot, "f"));
|
|
1096
|
+
}
|
|
291
1097
|
state.content_done = true;
|
|
292
1098
|
this._emit('content.done', {
|
|
293
|
-
content
|
|
294
|
-
parsed:
|
|
1099
|
+
content,
|
|
1100
|
+
parsed: refusal
|
|
295
1101
|
? null
|
|
296
|
-
: (0, parser_2.parseResponseFormatContent)(tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_params, "f")?.response_format,
|
|
1102
|
+
: (0, parser_2.parseResponseFormatContent)(tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_params, "f")?.response_format, parseableContent ? validateStructuredJSONSnapshot(content) : content),
|
|
297
1103
|
});
|
|
298
1104
|
}
|
|
299
|
-
if (
|
|
1105
|
+
if (refusal && !state.refusal_done) {
|
|
300
1106
|
state.refusal_done = true;
|
|
301
|
-
this._emit('refusal.done', { refusal
|
|
1107
|
+
this._emit('refusal.done', { refusal });
|
|
302
1108
|
}
|
|
303
1109
|
if (choiceSnapshot.logprobs?.content && !state.logprobs_content_done) {
|
|
304
1110
|
state.logprobs_content_done = true;
|
|
@@ -308,6 +1114,97 @@ class ChatCompletionStream extends AbstractChatCompletionRunner_1.AbstractChatCo
|
|
|
308
1114
|
state.logprobs_refusal_done = true;
|
|
309
1115
|
this._emit('logprobs.refusal.done', { refusal: choiceSnapshot.logprobs.refusal });
|
|
310
1116
|
}
|
|
1117
|
+
}, _ChatCompletionStream_validateStructuredSnapshots = function _ChatCompletionStream_validateStructuredSnapshots(snapshot) {
|
|
1118
|
+
const finalJSONBudget = { bytes: 0, fragments: 0, work: 0 };
|
|
1119
|
+
const parseableContent = (0, parser_2.isParseableResponseFormat)(tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_params, "f")?.response_format);
|
|
1120
|
+
const validatedMessages = new WeakMap();
|
|
1121
|
+
const choices = captureSnapshotArray(snapshot, 'choices', MAX_STREAM_CHOICES, 'choice');
|
|
1122
|
+
if (!choices) {
|
|
1123
|
+
throw new error_1.OpenAIError('Chat completion stream contains an unsafe snapshot choice collection');
|
|
1124
|
+
}
|
|
1125
|
+
for (let choiceIndex = 0; choiceIndex < choices.length; choiceIndex += 1) {
|
|
1126
|
+
const choice = captureSnapshotArrayItem(choices, choiceIndex);
|
|
1127
|
+
if (!choice) {
|
|
1128
|
+
continue;
|
|
1129
|
+
}
|
|
1130
|
+
const message = captureStructuredMessageSnapshot(choice);
|
|
1131
|
+
const refusal = captureStructuredJSONSnapshot(message, 'refusal');
|
|
1132
|
+
const content = captureStructuredJSONSnapshot(message, 'content');
|
|
1133
|
+
const validatedTools = new Map();
|
|
1134
|
+
const toolCalls = captureSnapshotArray(message, 'tool_calls', MAX_STREAM_TOOL_CALLS, 'tool-call');
|
|
1135
|
+
validatedMessages.set(choice, Object.freeze({
|
|
1136
|
+
message,
|
|
1137
|
+
content,
|
|
1138
|
+
refusal,
|
|
1139
|
+
toolCallCollection: toolCalls,
|
|
1140
|
+
toolCalls: validatedTools,
|
|
1141
|
+
}));
|
|
1142
|
+
const state = tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_choiceEventStates, "f")[choice.index];
|
|
1143
|
+
if (parseableContent && !refusal && typeof content === 'string') {
|
|
1144
|
+
validateStructuredJSONSnapshot(content, finalJSONBudget, tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_partialJSONParseBudget, "f"));
|
|
1145
|
+
}
|
|
1146
|
+
for (const [index, identity] of state?.tool_call_identities ?? []) {
|
|
1147
|
+
const toolCall = toolCalls && captureSnapshotArrayItem(toolCalls, index);
|
|
1148
|
+
if (!toolCall) {
|
|
1149
|
+
throw new error_1.OpenAIError('Chat completion stream contains a changed tool call identity');
|
|
1150
|
+
}
|
|
1151
|
+
assertBoundToolCallIdentity(toolCall, identity);
|
|
1152
|
+
}
|
|
1153
|
+
if (!tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_hasAutoParseableTool, "f")) {
|
|
1154
|
+
continue;
|
|
1155
|
+
}
|
|
1156
|
+
for (let toolCallIndex = 0; toolCallIndex < (toolCalls?.length ?? 0); toolCallIndex += 1) {
|
|
1157
|
+
const toolCall = captureSnapshotArrayItem(toolCalls, toolCallIndex);
|
|
1158
|
+
if (!toolCall) {
|
|
1159
|
+
continue;
|
|
1160
|
+
}
|
|
1161
|
+
const identity = ownFunctionToolIdentity(toolCall);
|
|
1162
|
+
if (!identity) {
|
|
1163
|
+
const type = Object.getOwnPropertyDescriptor(toolCall, 'type');
|
|
1164
|
+
if (type && !('value' in type)) {
|
|
1165
|
+
throw new error_1.OpenAIError('Chat completion stream contains an unsafe structured JSON snapshot');
|
|
1166
|
+
}
|
|
1167
|
+
if (type?.value !== 'function') {
|
|
1168
|
+
continue;
|
|
1169
|
+
}
|
|
1170
|
+
const fn = Object.getOwnPropertyDescriptor(toolCall, 'function');
|
|
1171
|
+
if (fn && !('value' in fn)) {
|
|
1172
|
+
throw new error_1.OpenAIError('Chat completion stream contains an unsafe structured JSON snapshot');
|
|
1173
|
+
}
|
|
1174
|
+
if (fn && typeof fn.value === 'object' && fn.value !== null) {
|
|
1175
|
+
const name = Object.getOwnPropertyDescriptor(fn.value, 'name');
|
|
1176
|
+
if (name && !('value' in name)) {
|
|
1177
|
+
throw new error_1.OpenAIError('Chat completion stream contains an unsafe structured JSON snapshot');
|
|
1178
|
+
}
|
|
1179
|
+
}
|
|
1180
|
+
continue;
|
|
1181
|
+
}
|
|
1182
|
+
if (!(0, parser_2.shouldParseToolCall)(tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_params, "f"), {
|
|
1183
|
+
type: identity.type,
|
|
1184
|
+
function: { name: identity.name },
|
|
1185
|
+
})) {
|
|
1186
|
+
continue;
|
|
1187
|
+
}
|
|
1188
|
+
const descriptor = Object.getOwnPropertyDescriptor(toolCall, 'function');
|
|
1189
|
+
if (!descriptor || !('value' in descriptor)) {
|
|
1190
|
+
throw new error_1.OpenAIError('Chat completion stream contains an unsafe structured JSON snapshot');
|
|
1191
|
+
}
|
|
1192
|
+
const fn = descriptor.value;
|
|
1193
|
+
const argumentsSnapshot = captureStructuredJSONSnapshot(fn, 'arguments');
|
|
1194
|
+
if (typeof argumentsSnapshot !== 'string') {
|
|
1195
|
+
throw new error_1.OpenAIError('Chat completion stream contains an unsafe structured JSON snapshot');
|
|
1196
|
+
}
|
|
1197
|
+
validateStructuredJSONSnapshot(argumentsSnapshot, finalJSONBudget, tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_partialJSONParseBudget, "f"));
|
|
1198
|
+
validatedTools.set(toolCallIndex, Object.freeze({
|
|
1199
|
+
tool: toolCall,
|
|
1200
|
+
function: fn,
|
|
1201
|
+
type: identity.type,
|
|
1202
|
+
name: identity.name,
|
|
1203
|
+
arguments: argumentsSnapshot,
|
|
1204
|
+
}));
|
|
1205
|
+
}
|
|
1206
|
+
}
|
|
1207
|
+
return validatedMessages;
|
|
311
1208
|
}, _ChatCompletionStream_endRequest = function _ChatCompletionStream_endRequest() {
|
|
312
1209
|
if (this.ended) {
|
|
313
1210
|
throw new error_1.OpenAIError(`stream has ended, this shouldn't happen`);
|
|
@@ -316,15 +1213,16 @@ class ChatCompletionStream extends AbstractChatCompletionRunner_1.AbstractChatCo
|
|
|
316
1213
|
if (!snapshot) {
|
|
317
1214
|
throw new error_1.OpenAIError(`request ended without sending any chunks`);
|
|
318
1215
|
}
|
|
1216
|
+
const validatedMessages = tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_validateStructuredSnapshots).call(this, snapshot);
|
|
319
1217
|
const audioDoneChoiceIndexes = tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_audioDoneChoiceIndexes, "f");
|
|
320
1218
|
tslib_1.__classPrivateFieldSet(this, _ChatCompletionStream_audioDoneChoiceIndexes, new Set(), "f");
|
|
321
1219
|
tslib_1.__classPrivateFieldSet(this, _ChatCompletionStream_currentChatCompletionSnapshot, undefined, "f");
|
|
322
1220
|
tslib_1.__classPrivateFieldSet(this, _ChatCompletionStream_choiceEventStates, [], "f");
|
|
323
|
-
return finalizeChatCompletion(snapshot, tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_params, "f"), audioDoneChoiceIndexes);
|
|
324
|
-
}, _ChatCompletionStream_accumulateChatCompletion = function _ChatCompletionStream_accumulateChatCompletion(chunk) {
|
|
1221
|
+
return finalizeChatCompletion(snapshot, tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_params, "f"), audioDoneChoiceIndexes, validatedMessages);
|
|
1222
|
+
}, _ChatCompletionStream_accumulateChatCompletion = function _ChatCompletionStream_accumulateChatCompletion(chunk, capturedChoiceFrames) {
|
|
325
1223
|
var _a, _b, _c, _d, _e;
|
|
326
1224
|
let snapshot = tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_currentChatCompletionSnapshot, "f");
|
|
327
|
-
const { choices, ...rest } = chunk;
|
|
1225
|
+
const { choices, obfuscation: _obfuscation, ...rest } = chunk;
|
|
328
1226
|
if (!snapshot) {
|
|
329
1227
|
const newSnapshot = {
|
|
330
1228
|
...rest,
|
|
@@ -342,16 +1240,22 @@ class ChatCompletionStream extends AbstractChatCompletionRunner_1.AbstractChatCo
|
|
|
342
1240
|
requestedChoiceCount > 0
|
|
343
1241
|
? Math.min(requestedChoiceCount, MAX_STREAM_CHOICES)
|
|
344
1242
|
: MAX_STREAM_CHOICES;
|
|
345
|
-
for (const
|
|
1243
|
+
for (const chunkChoice of chunk.choices) {
|
|
1244
|
+
const { delta, finish_reason, index, logprobs = null, ...other } = chunkChoice;
|
|
1245
|
+
const capturedToolCalls = [];
|
|
1246
|
+
capturedChoiceFrames.set(chunkChoice, Object.freeze({ index, tool_calls: capturedToolCalls }));
|
|
346
1247
|
if (!Number.isSafeInteger(index) || index < 0 || index >= maxChoices) {
|
|
347
1248
|
throw new error_1.OpenAIError(`Chat completion stream contains an invalid choice index: ${index}`);
|
|
348
1249
|
}
|
|
349
1250
|
let choice = snapshot.choices[index];
|
|
350
1251
|
if (!choice) {
|
|
351
|
-
const newChoice = { finish_reason, index, message: {}, logprobs, ...other };
|
|
1252
|
+
const newChoice = { finish_reason, index, message: {}, logprobs: null, ...other };
|
|
352
1253
|
snapshot.choices[index] = newChoice;
|
|
353
1254
|
choice = newChoice;
|
|
354
1255
|
}
|
|
1256
|
+
if ((0, parser_2.isParseableResponseFormat)(tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_params, "f")?.response_format) || tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_hasAutoParseableTool, "f")) {
|
|
1257
|
+
captureStructuredJSONSnapshot(captureStructuredMessageSnapshot(choice), 'refusal');
|
|
1258
|
+
}
|
|
355
1259
|
if (logprobs) {
|
|
356
1260
|
if (choice.logprobs) {
|
|
357
1261
|
const { content, refusal, ...rest } = logprobs;
|
|
@@ -368,6 +1272,12 @@ class ChatCompletionStream extends AbstractChatCompletionRunner_1.AbstractChatCo
|
|
|
368
1272
|
}
|
|
369
1273
|
else {
|
|
370
1274
|
choice.logprobs = { ...logprobs };
|
|
1275
|
+
if (logprobs.content) {
|
|
1276
|
+
choice.logprobs.content = [...logprobs.content];
|
|
1277
|
+
}
|
|
1278
|
+
if (logprobs.refusal) {
|
|
1279
|
+
choice.logprobs.refusal = [...logprobs.refusal];
|
|
1280
|
+
}
|
|
371
1281
|
}
|
|
372
1282
|
}
|
|
373
1283
|
if (finish_reason) {
|
|
@@ -383,10 +1293,13 @@ class ChatCompletionStream extends AbstractChatCompletionRunner_1.AbstractChatCo
|
|
|
383
1293
|
}
|
|
384
1294
|
assignOwnProperties(choice, other);
|
|
385
1295
|
if (!delta) {
|
|
1296
|
+
Object.freeze(capturedToolCalls);
|
|
386
1297
|
continue;
|
|
387
1298
|
} // Shouldn't happen; just in case.
|
|
388
1299
|
tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_audioDoneChoiceIndexes, "f").delete(index);
|
|
389
|
-
const { audio, content, refusal, function_call, role,
|
|
1300
|
+
const { audio, content, refusal, function_call, role, ...capturedDeltaFields } = delta;
|
|
1301
|
+
const { tool_calls: capturedToolCallDelta, ...rest } = capturedDeltaFields;
|
|
1302
|
+
const tool_calls = (0, values_1.hasOwn)(capturedDeltaFields, 'tool_calls') ? capturedToolCallDelta : delta.tool_calls;
|
|
390
1303
|
assertIsEmpty(rest);
|
|
391
1304
|
assignOwnProperties(choice.message, rest);
|
|
392
1305
|
if (audio?.expires_at != null &&
|
|
@@ -437,10 +1350,26 @@ class ChatCompletionStream extends AbstractChatCompletionRunner_1.AbstractChatCo
|
|
|
437
1350
|
}
|
|
438
1351
|
}
|
|
439
1352
|
if (content != null) {
|
|
440
|
-
choice.message.content = (choice.message.content || '') + content;
|
|
441
1353
|
if (!choice.message.refusal && (0, parser_2.isParseableResponseFormat)(tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_params, "f")?.response_format)) {
|
|
442
|
-
|
|
443
|
-
|
|
1354
|
+
const eventState = tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_getChoiceEventState).call(this, choice);
|
|
1355
|
+
const parseState = (eventState.content_parse_state ?? (eventState.content_parse_state = createPartialJSONParseState()));
|
|
1356
|
+
const shouldParse = recordPartialJSONFragment(parseState, tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_partialJSONParseBudget, "f"), content);
|
|
1357
|
+
choice.message.content = (captureStructuredJSONSnapshot(choice.message, 'content') || '') + content;
|
|
1358
|
+
// The partial parser does not accept whitespace-only input. Once output
|
|
1359
|
+
// grows, coalesce prefix reparses while preserving every raw snapshot.
|
|
1360
|
+
if (!parseState.has_non_whitespace) {
|
|
1361
|
+
choice.message.parsed = null;
|
|
1362
|
+
}
|
|
1363
|
+
else if (shouldParse && reservePartialJSONParse(parseState, tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_partialJSONParseBudget, "f"))) {
|
|
1364
|
+
tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_validateStructuredSnapshots).call(this, snapshot);
|
|
1365
|
+
choice.message.parsed = parseStructuredStreamingJSON(validateStructuredJSONSnapshot(choice.message.content));
|
|
1366
|
+
}
|
|
1367
|
+
else if (content.length > 0) {
|
|
1368
|
+
choice.message.parsed = null;
|
|
1369
|
+
}
|
|
1370
|
+
}
|
|
1371
|
+
else {
|
|
1372
|
+
choice.message.content = (choice.message.content || '') + content;
|
|
444
1373
|
}
|
|
445
1374
|
}
|
|
446
1375
|
if (tool_calls) {
|
|
@@ -448,11 +1377,23 @@ class ChatCompletionStream extends AbstractChatCompletionRunner_1.AbstractChatCo
|
|
|
448
1377
|
// entries are only partially filled in; they match `ChatCompletionSnapshot.Choice.Message.ToolCall`
|
|
449
1378
|
// once every delta for them has been accumulated.
|
|
450
1379
|
const toolCallSnapshots = ((_e = choice.message).tool_calls ?? (_e.tool_calls = []));
|
|
451
|
-
for (const
|
|
1380
|
+
for (const toolCallDelta of tool_calls) {
|
|
1381
|
+
const { index, id, type, function: fn, custom, ...rest } = toolCallDelta;
|
|
452
1382
|
if (!Number.isSafeInteger(index) || index < 0 || index >= MAX_STREAM_TOOL_CALLS) {
|
|
453
1383
|
throw new error_1.OpenAIError(`Chat completion stream contains an invalid tool call index: ${index}`);
|
|
454
1384
|
}
|
|
1385
|
+
let argumentsDelta = '';
|
|
455
1386
|
const tool_call = (toolCallSnapshots[index] ?? (toolCallSnapshots[index] = {}));
|
|
1387
|
+
const functionName = fn?.name;
|
|
1388
|
+
const eventState = tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_hasAutoParseableTool, "f") ? tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_getChoiceEventState).call(this, choice) : undefined;
|
|
1389
|
+
let boundIdentity = eventState?.tool_call_identities.get(index);
|
|
1390
|
+
if (boundIdentity) {
|
|
1391
|
+
assertBoundToolCallIdentity(tool_call, boundIdentity);
|
|
1392
|
+
if ((type !== undefined && type !== boundIdentity.type) ||
|
|
1393
|
+
(functionName !== undefined && functionName !== boundIdentity.name)) {
|
|
1394
|
+
throw new error_1.OpenAIError('Chat completion stream contains a changed tool call identity');
|
|
1395
|
+
}
|
|
1396
|
+
}
|
|
456
1397
|
assignOwnProperties(tool_call, rest);
|
|
457
1398
|
if (id) {
|
|
458
1399
|
tool_call.id = id;
|
|
@@ -470,19 +1411,69 @@ class ChatCompletionStream extends AbstractChatCompletionRunner_1.AbstractChatCo
|
|
|
470
1411
|
}
|
|
471
1412
|
}
|
|
472
1413
|
if (fn) {
|
|
473
|
-
const functionSnapshot = (tool_call.function ?? (tool_call.function = { name:
|
|
474
|
-
if (
|
|
475
|
-
functionSnapshot.name =
|
|
1414
|
+
const functionSnapshot = (tool_call.function ?? (tool_call.function = { name: functionName ?? '', arguments: '' }));
|
|
1415
|
+
if (functionName) {
|
|
1416
|
+
functionSnapshot.name = functionName;
|
|
476
1417
|
}
|
|
477
|
-
if (
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
1418
|
+
if (eventState && !boundIdentity) {
|
|
1419
|
+
const identity = ownFunctionToolIdentity(tool_call);
|
|
1420
|
+
const configuredTool = identity &&
|
|
1421
|
+
tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_params, "f")?.tools?.find((tool) => (0, parser_2.isChatCompletionFunctionTool)(tool) && tool.function.name === identity.name);
|
|
1422
|
+
if (identity) {
|
|
1423
|
+
boundIdentity = {
|
|
1424
|
+
...identity,
|
|
1425
|
+
parseable: configuredTool !== undefined &&
|
|
1426
|
+
(0, parser_2.shouldParseToolCall)(tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_params, "f"), {
|
|
1427
|
+
type: identity.type,
|
|
1428
|
+
function: { name: identity.name },
|
|
1429
|
+
}),
|
|
1430
|
+
};
|
|
1431
|
+
eventState.tool_call_identities.set(index, boundIdentity);
|
|
1432
|
+
if (!boundIdentity.parseable) {
|
|
1433
|
+
const provisionalState = eventState.tool_call_parse_states.get(index);
|
|
1434
|
+
if (provisionalState) {
|
|
1435
|
+
tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_partialJSONParseBudget, "f").bytes -= provisionalState.bytes;
|
|
1436
|
+
tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_partialJSONParseBudget, "f").fragments -= provisionalState.fragments;
|
|
1437
|
+
tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_partialJSONParseBudget, "f").work -= provisionalState.work;
|
|
1438
|
+
eventState.tool_call_parse_states.delete(index);
|
|
1439
|
+
}
|
|
1440
|
+
}
|
|
1441
|
+
}
|
|
1442
|
+
}
|
|
1443
|
+
const argumentFragment = fn.arguments;
|
|
1444
|
+
if (argumentFragment != null) {
|
|
1445
|
+
argumentsDelta = argumentFragment;
|
|
1446
|
+
if (eventState && boundIdentity?.parseable !== false) {
|
|
1447
|
+
let parseState = eventState.tool_call_parse_states.get(index);
|
|
1448
|
+
if (!parseState) {
|
|
1449
|
+
parseState = createPartialJSONParseState();
|
|
1450
|
+
eventState.tool_call_parse_states.set(index, parseState);
|
|
1451
|
+
}
|
|
1452
|
+
const shouldParse = recordPartialJSONFragment(parseState, tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_partialJSONParseBudget, "f"), argumentFragment);
|
|
1453
|
+
const previousArguments = captureStructuredJSONSnapshot(functionSnapshot, 'arguments');
|
|
1454
|
+
if (typeof previousArguments !== 'string') {
|
|
1455
|
+
throw new error_1.OpenAIError('Chat completion stream contains an unsafe structured JSON snapshot');
|
|
1456
|
+
}
|
|
1457
|
+
functionSnapshot.arguments = previousArguments + argumentFragment;
|
|
1458
|
+
if (shouldParse &&
|
|
1459
|
+
boundIdentity?.parseable === true &&
|
|
1460
|
+
reservePartialJSONParse(parseState, tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_partialJSONParseBudget, "f"))) {
|
|
1461
|
+
tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_validateStructuredSnapshots).call(this, snapshot);
|
|
1462
|
+
functionSnapshot.parsed_arguments = parseStructuredStreamingJSON(validateStructuredJSONSnapshot(functionSnapshot.arguments));
|
|
1463
|
+
}
|
|
1464
|
+
else if (argumentFragment.length > 0 && (0, values_1.hasOwn)(functionSnapshot, 'parsed_arguments')) {
|
|
1465
|
+
functionSnapshot.parsed_arguments = undefined;
|
|
1466
|
+
}
|
|
1467
|
+
}
|
|
1468
|
+
else {
|
|
1469
|
+
functionSnapshot.arguments += argumentFragment;
|
|
481
1470
|
}
|
|
482
1471
|
}
|
|
483
1472
|
}
|
|
1473
|
+
capturedToolCalls.push(Object.freeze({ index, arguments_delta: argumentsDelta }));
|
|
484
1474
|
}
|
|
485
1475
|
}
|
|
1476
|
+
Object.freeze(capturedToolCalls);
|
|
486
1477
|
}
|
|
487
1478
|
return snapshot;
|
|
488
1479
|
}, Symbol.asyncIterator)]() {
|
|
@@ -499,12 +1490,36 @@ class ChatCompletionStream extends AbstractChatCompletionRunner_1.AbstractChatCo
|
|
|
499
1490
|
}
|
|
500
1491
|
}
|
|
501
1492
|
exports.ChatCompletionStream = ChatCompletionStream;
|
|
502
|
-
function finalizeChatCompletion(snapshot, params, audioDoneChoiceIndexes) {
|
|
1493
|
+
function finalizeChatCompletion(snapshot, params, audioDoneChoiceIndexes, validatedMessages) {
|
|
503
1494
|
const { id, choices, created, model, system_fingerprint, ...rest } = snapshot;
|
|
504
1495
|
const completion = {
|
|
505
1496
|
...rest,
|
|
506
1497
|
id,
|
|
507
|
-
choices: choices
|
|
1498
|
+
choices: mapCapturedSnapshotArray(choices, MAX_STREAM_CHOICES, 'choice', (choice) => {
|
|
1499
|
+
const validated = validatedMessages.get(choice);
|
|
1500
|
+
if (!validated) {
|
|
1501
|
+
throw new error_1.OpenAIError('Chat completion stream contains an unsafe structured JSON snapshot');
|
|
1502
|
+
}
|
|
1503
|
+
const stableChoice = new Proxy(choice, {
|
|
1504
|
+
get(target, property, receiver) {
|
|
1505
|
+
return property === 'message' ? validated.message : Reflect.get(target, property, receiver);
|
|
1506
|
+
},
|
|
1507
|
+
});
|
|
1508
|
+
const { message: sourceMessage, finish_reason, index, logprobs, ...choiceRest } = stableChoice;
|
|
1509
|
+
const message = new Proxy(sourceMessage, {
|
|
1510
|
+
get(target, property, receiver) {
|
|
1511
|
+
if (property === 'content') {
|
|
1512
|
+
return validated.content;
|
|
1513
|
+
}
|
|
1514
|
+
if (property === 'refusal') {
|
|
1515
|
+
return validated.refusal;
|
|
1516
|
+
}
|
|
1517
|
+
if (property === 'tool_calls') {
|
|
1518
|
+
return validated.toolCallCollection;
|
|
1519
|
+
}
|
|
1520
|
+
return Reflect.get(target, property, receiver);
|
|
1521
|
+
},
|
|
1522
|
+
});
|
|
508
1523
|
const { content = null, function_call, tool_calls, audio, ...messageRest } = message;
|
|
509
1524
|
// Audio streams can end with an expires_at-only chunk after the
|
|
510
1525
|
// generated audio, without a separate finish_reason.
|
|
@@ -551,15 +1566,54 @@ function finalizeChatCompletion(snapshot, params, audioDoneChoiceIndexes) {
|
|
|
551
1566
|
role,
|
|
552
1567
|
content,
|
|
553
1568
|
refusal: message.refusal ?? null,
|
|
554
|
-
tool_calls: tool_calls
|
|
555
|
-
|
|
556
|
-
|
|
1569
|
+
tool_calls: mapCapturedSnapshotArray(tool_calls, MAX_STREAM_TOOL_CALLS, 'tool-call', (tool_call, i) => {
|
|
1570
|
+
const captured = validated.toolCalls.get(i);
|
|
1571
|
+
if (!captured) {
|
|
1572
|
+
const identity = ownFunctionToolIdentity(tool_call);
|
|
1573
|
+
if (identity &&
|
|
1574
|
+
(0, parser_2.shouldParseToolCall)(params, {
|
|
1575
|
+
type: identity.type,
|
|
1576
|
+
function: { name: identity.name },
|
|
1577
|
+
})) {
|
|
1578
|
+
throw new error_1.OpenAIError('Chat completion stream contains an unsafe structured JSON snapshot');
|
|
1579
|
+
}
|
|
557
1580
|
}
|
|
558
|
-
if (
|
|
559
|
-
|
|
1581
|
+
if (captured && captured.tool !== tool_call) {
|
|
1582
|
+
throw new error_1.OpenAIError('Chat completion stream contains a changed tool call identity');
|
|
1583
|
+
}
|
|
1584
|
+
const stableFunction = captured &&
|
|
1585
|
+
new Proxy(captured.function, {
|
|
1586
|
+
get(target, property, receiver) {
|
|
1587
|
+
if (property === 'arguments') {
|
|
1588
|
+
return captured.arguments;
|
|
1589
|
+
}
|
|
1590
|
+
if (property === 'name') {
|
|
1591
|
+
return captured.name;
|
|
1592
|
+
}
|
|
1593
|
+
return Reflect.get(target, property, receiver);
|
|
1594
|
+
},
|
|
1595
|
+
});
|
|
1596
|
+
const stableTool = captured && stableFunction
|
|
1597
|
+
? new Proxy(tool_call, {
|
|
1598
|
+
get(target, property, receiver) {
|
|
1599
|
+
if (property === 'type') {
|
|
1600
|
+
return captured.type;
|
|
1601
|
+
}
|
|
1602
|
+
if (property === 'function') {
|
|
1603
|
+
return stableFunction;
|
|
1604
|
+
}
|
|
1605
|
+
return Reflect.get(target, property, receiver);
|
|
1606
|
+
},
|
|
1607
|
+
})
|
|
1608
|
+
: tool_call;
|
|
1609
|
+
if (stableTool.type == null) {
|
|
1610
|
+
throw new error_1.OpenAIError(`missing choices[${index}].tool_calls[${i}].type`);
|
|
1611
|
+
}
|
|
1612
|
+
if (stableTool.type === 'custom') {
|
|
1613
|
+
const { custom, type, id, ...toolRest } = stableTool;
|
|
560
1614
|
const { input = '', name, ...customRest } = custom || {};
|
|
561
1615
|
if (name == null) {
|
|
562
|
-
throw new error_1.OpenAIError(`missing choices[${index}].tool_calls[${i}].custom.name
|
|
1616
|
+
throw new error_1.OpenAIError(`missing choices[${index}].tool_calls[${i}].custom.name`);
|
|
563
1617
|
}
|
|
564
1618
|
return {
|
|
565
1619
|
...toolRest,
|
|
@@ -568,13 +1622,13 @@ function finalizeChatCompletion(snapshot, params, audioDoneChoiceIndexes) {
|
|
|
568
1622
|
custom: { ...customRest, name, input },
|
|
569
1623
|
};
|
|
570
1624
|
}
|
|
571
|
-
const { function: fn, type, id, ...toolRest } =
|
|
1625
|
+
const { function: fn, type, id, ...toolRest } = stableTool;
|
|
572
1626
|
const { arguments: args, name, ...fnRest } = fn || {};
|
|
573
1627
|
if (name == null) {
|
|
574
|
-
throw new error_1.OpenAIError(`missing choices[${index}].tool_calls[${i}].function.name
|
|
1628
|
+
throw new error_1.OpenAIError(`missing choices[${index}].tool_calls[${i}].function.name`);
|
|
575
1629
|
}
|
|
576
1630
|
if (args == null) {
|
|
577
|
-
throw new error_1.OpenAIError(`missing choices[${index}].tool_calls[${i}].function.arguments
|
|
1631
|
+
throw new error_1.OpenAIError(`missing choices[${index}].tool_calls[${i}].function.arguments`);
|
|
578
1632
|
}
|
|
579
1633
|
return {
|
|
580
1634
|
...toolRest,
|
|
@@ -604,9 +1658,6 @@ function finalizeChatCompletion(snapshot, params, audioDoneChoiceIndexes) {
|
|
|
604
1658
|
function isCompleteAudio(audio) {
|
|
605
1659
|
return audio?.id != null && audio.data != null && audio.transcript != null && audio.expires_at != null;
|
|
606
1660
|
}
|
|
607
|
-
function str(x) {
|
|
608
|
-
return JSON.stringify(x);
|
|
609
|
-
}
|
|
610
1661
|
/**
|
|
611
1662
|
* Ensures the given argument is an empty object, useful for
|
|
612
1663
|
* asserting that all known properties on an object have been
|