@songtonyli/dsh-cli 0.1.56 → 0.1.58
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/README.md +1 -1
- package/bin/dsh.mjs +1 -1
- package/node_modules/@aws-sdk/{credential-provider-sso → client-bedrock-runtime}/node_modules/@aws-sdk/token-providers/package.json +6 -6
- package/node_modules/@aws-sdk/token-providers/package.json +6 -6
- package/node_modules/@deepseek-ai/dsh-client-ui-sidebar/lib/client.js +1 -1
- package/node_modules/@deepseek-ai/dsh-client-ui-slots/package.json +2 -2
- package/node_modules/@deepseek-ai/dsh-llm-pi-ai/README.i18n.yaml +2 -2
- package/node_modules/@deepseek-ai/dsh-llm-pi-ai/README.md +1 -1
- package/node_modules/@deepseek-ai/dsh-llm-pi-ai/README.zh.md +1 -1
- package/node_modules/@deepseek-ai/dsh-llm-pi-ai/lib/index.js +1 -182
- package/node_modules/@deepseek-ai/dsh-llm-pi-ai/package.json +1 -1
- package/node_modules/@deepseek-ai/dsh-tui-app/README.i18n.yaml +6 -6
- package/node_modules/@deepseek-ai/dsh-tui-app/README.md +6 -5
- package/node_modules/@deepseek-ai/dsh-tui-app/README.zh.md +6 -5
- package/node_modules/@deepseek-ai/dsh-tui-app/lib/index.js +43 -32
- package/node_modules/@earendil-works/pi-ai/README.md +207 -27
- package/node_modules/@earendil-works/pi-ai/dist/api/anthropic-messages.js +6 -0
- package/node_modules/@earendil-works/pi-ai/dist/api/azure-openai-responses.js +6 -5
- package/node_modules/@earendil-works/pi-ai/dist/api/bedrock-converse-stream.js +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/api/cloudflare-workers-ai-system-one.js +43 -0
- package/node_modules/@earendil-works/pi-ai/dist/api/cloudflare-workers-ai-system-one.lazy.js +4 -0
- package/node_modules/@earendil-works/pi-ai/dist/api/cloudflare.js +2 -0
- package/node_modules/@earendil-works/pi-ai/dist/api/google-generative-ai.js +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/api/google-vertex.js +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/api/llama-cpp-classify.js +365 -0
- package/node_modules/@earendil-works/pi-ai/dist/api/llama-cpp-classify.lazy.js +4 -0
- package/node_modules/@earendil-works/pi-ai/dist/api/mistral-conversations.js +18 -17
- package/node_modules/@earendil-works/pi-ai/dist/api/openai-codex-responses.js +20 -4
- package/node_modules/@earendil-works/pi-ai/dist/api/openai-completions.js +4 -4
- package/node_modules/@earendil-works/pi-ai/dist/api/openai-responses-shared.js +14 -0
- package/node_modules/@earendil-works/pi-ai/dist/api/openai-responses.js +26 -9
- package/node_modules/@earendil-works/pi-ai/dist/api/openrouter-images.js +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/api/pi-messages.js +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/api/simple-options.js +2 -4
- package/node_modules/@earendil-works/pi-ai/dist/api/system-one-shared.js +183 -0
- package/node_modules/@earendil-works/pi-ai/dist/api/typesafe-system-one.js +19 -0
- package/node_modules/@earendil-works/pi-ai/dist/api/typesafe-system-one.lazy.js +4 -0
- package/node_modules/@earendil-works/pi-ai/dist/auth/helpers.js +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/auth/oauth/anthropic.js +19 -128
- package/node_modules/@earendil-works/pi-ai/dist/auth/oauth/callback-server.js +146 -0
- package/node_modules/@earendil-works/pi-ai/dist/auth/oauth/load.js +5 -0
- package/node_modules/@earendil-works/pi-ai/dist/auth/oauth/openai-chatgpt.js +266 -0
- package/node_modules/@earendil-works/pi-ai/dist/auth/oauth/openai-codex.js +20 -124
- package/node_modules/@earendil-works/pi-ai/dist/auth/oauth/openrouter.js +19 -138
- package/node_modules/@earendil-works/pi-ai/dist/auth/oauth/radius.js +21 -89
- package/node_modules/@earendil-works/pi-ai/dist/auth/resolve.js +3 -19
- package/node_modules/@earendil-works/pi-ai/dist/bun-oauth.js +2 -0
- package/node_modules/@earendil-works/pi-ai/dist/cli.js +3 -1
- package/node_modules/@earendil-works/pi-ai/dist/env-api-keys.js +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/image-models.js +14 -13
- package/node_modules/@earendil-works/pi-ai/dist/images.js +5 -0
- package/node_modules/@earendil-works/pi-ai/dist/index.js +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/model-catalog.js +14 -2
- package/node_modules/@earendil-works/pi-ai/dist/models.generated.js +131 -41
- package/node_modules/@earendil-works/pi-ai/dist/models.js +157 -30
- package/node_modules/@earendil-works/pi-ai/dist/providers/all.js +25 -21
- package/node_modules/@earendil-works/pi-ai/dist/providers/amazon-bedrock.models.js +4 -2
- package/node_modules/@earendil-works/pi-ai/dist/providers/ant-ling.models.js +4 -2
- package/node_modules/@earendil-works/pi-ai/dist/providers/anthropic.models.js +4 -2
- package/node_modules/@earendil-works/pi-ai/dist/providers/azure-openai-responses.models.js +4 -2
- package/node_modules/@earendil-works/pi-ai/dist/providers/baseten.models.js +4 -2
- package/node_modules/@earendil-works/pi-ai/dist/providers/cerebras.models.js +4 -2
- package/node_modules/@earendil-works/pi-ai/dist/providers/cloudflare-ai-gateway.models.js +4 -2
- package/node_modules/@earendil-works/pi-ai/dist/providers/cloudflare-stream.js +6 -0
- package/node_modules/@earendil-works/pi-ai/dist/providers/cloudflare-workers-ai.js +10 -3
- package/node_modules/@earendil-works/pi-ai/dist/providers/cloudflare-workers-ai.models.js +4 -2
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/.manifest.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/amazon-bedrock.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/ant-ling.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/anthropic.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/azure-openai-responses.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/baseten.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/cerebras.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/cloudflare-ai-gateway.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/cloudflare-workers-ai.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/deepseek.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/fireworks.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/github-copilot.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/google-vertex.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/google.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/groq.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/huggingface.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/kimi-coding.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/meta.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/minimax-cn.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/minimax.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/mistral.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/moonshotai-cn.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/moonshotai.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/nvidia.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/openai-codex.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/openai.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/opencode-go.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/opencode.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/openrouter.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/qwen-token-plan-cn.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/qwen-token-plan-individual.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/qwen-token-plan.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/radius.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/together.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/typesafe.json +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/vercel-ai-gateway.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/xai.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/xiaomi-token-plan-ams.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/xiaomi-token-plan-cn.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/xiaomi-token-plan-sgp.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/xiaomi.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/zai-coding-cn.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/zai.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/deepseek.models.js +4 -2
- package/node_modules/@earendil-works/pi-ai/dist/providers/fireworks.models.js +4 -2
- package/node_modules/@earendil-works/pi-ai/dist/providers/github-copilot.models.js +4 -2
- package/node_modules/@earendil-works/pi-ai/dist/providers/google-vertex.models.js +4 -2
- package/node_modules/@earendil-works/pi-ai/dist/providers/google.models.js +4 -2
- package/node_modules/@earendil-works/pi-ai/dist/providers/groq.models.js +4 -2
- package/node_modules/@earendil-works/pi-ai/dist/providers/huggingface.models.js +4 -2
- package/node_modules/@earendil-works/pi-ai/dist/providers/kimi-coding.models.js +4 -2
- package/node_modules/@earendil-works/pi-ai/dist/providers/meta.models.js +4 -2
- package/node_modules/@earendil-works/pi-ai/dist/providers/minimax-cn.models.js +4 -2
- package/node_modules/@earendil-works/pi-ai/dist/providers/minimax.models.js +4 -2
- package/node_modules/@earendil-works/pi-ai/dist/providers/mistral.models.js +4 -2
- package/node_modules/@earendil-works/pi-ai/dist/providers/moonshotai-cn.models.js +4 -2
- package/node_modules/@earendil-works/pi-ai/dist/providers/moonshotai.models.js +4 -2
- package/node_modules/@earendil-works/pi-ai/dist/providers/nvidia.models.js +4 -2
- package/node_modules/@earendil-works/pi-ai/dist/providers/openai-codex.js +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/openai-codex.models.js +4 -2
- package/node_modules/@earendil-works/pi-ai/dist/providers/openai.js +11 -2
- package/node_modules/@earendil-works/pi-ai/dist/providers/openai.models.js +4 -2
- package/node_modules/@earendil-works/pi-ai/dist/providers/opencode-go.models.js +4 -2
- package/node_modules/@earendil-works/pi-ai/dist/providers/opencode.js +5 -2
- package/node_modules/@earendil-works/pi-ai/dist/providers/opencode.models.js +4 -2
- package/node_modules/@earendil-works/pi-ai/dist/providers/openrouter.js +11 -2
- package/node_modules/@earendil-works/pi-ai/dist/providers/openrouter.models.js +4 -2
- package/node_modules/@earendil-works/pi-ai/dist/providers/qwen-token-plan-cn.models.js +4 -2
- package/node_modules/@earendil-works/pi-ai/dist/providers/qwen-token-plan-individual.models.js +4 -2
- package/node_modules/@earendil-works/pi-ai/dist/providers/qwen-token-plan.models.js +4 -2
- package/node_modules/@earendil-works/pi-ai/dist/providers/radius.models.js +4 -2
- package/node_modules/@earendil-works/pi-ai/dist/providers/together.models.js +4 -2
- package/node_modules/@earendil-works/pi-ai/dist/providers/typesafe.js +18 -0
- package/node_modules/@earendil-works/pi-ai/dist/providers/typesafe.models.js +8 -0
- package/node_modules/@earendil-works/pi-ai/dist/providers/vercel-ai-gateway.js +5 -2
- package/node_modules/@earendil-works/pi-ai/dist/providers/vercel-ai-gateway.models.js +4 -2
- package/node_modules/@earendil-works/pi-ai/dist/providers/xai.models.js +4 -2
- package/node_modules/@earendil-works/pi-ai/dist/providers/xiaomi-token-plan-ams.models.js +4 -2
- package/node_modules/@earendil-works/pi-ai/dist/providers/xiaomi-token-plan-cn.models.js +4 -2
- package/node_modules/@earendil-works/pi-ai/dist/providers/xiaomi-token-plan-sgp.models.js +4 -2
- package/node_modules/@earendil-works/pi-ai/dist/providers/xiaomi.models.js +4 -2
- package/node_modules/@earendil-works/pi-ai/dist/providers/zai-coding-cn.models.js +4 -2
- package/node_modules/@earendil-works/pi-ai/dist/providers/zai.models.js +4 -2
- package/node_modules/@earendil-works/pi-ai/dist/utils/headers.js +10 -8
- package/node_modules/@earendil-works/pi-ai/dist/utils/model-operations.js +47 -0
- package/node_modules/@earendil-works/pi-ai/dist/utils/models-error.js +19 -0
- package/node_modules/@earendil-works/pi-ai/dist/utils/retry.js +7 -0
- package/node_modules/@earendil-works/pi-ai/package.json +4 -4
- package/node_modules/@earendil-works/pi-telemetry/package.json +2 -2
- package/node_modules/openai/CHANGELOG.md +1089 -1
- package/node_modules/openai/README.md +201 -102
- package/node_modules/openai/_vendor/partial-json-parser/parser.js +55 -38
- package/node_modules/openai/_vendor/partial-json-parser/parser.mjs +55 -38
- package/node_modules/openai/_vendor/zod-to-json-schema/Options.js +11 -2
- package/node_modules/openai/_vendor/zod-to-json-schema/Options.mjs +11 -2
- package/node_modules/openai/_vendor/zod-to-json-schema/Refs.js +4 -4
- package/node_modules/openai/_vendor/zod-to-json-schema/Refs.mjs +4 -4
- package/node_modules/openai/_vendor/zod-to-json-schema/errorMessages.js +2 -1
- package/node_modules/openai/_vendor/zod-to-json-schema/errorMessages.mjs +2 -1
- package/node_modules/openai/_vendor/zod-to-json-schema/index.js +1 -1
- package/node_modules/openai/_vendor/zod-to-json-schema/index.mjs +1 -1
- package/node_modules/openai/_vendor/zod-to-json-schema/parseDef.js +139 -54
- package/node_modules/openai/_vendor/zod-to-json-schema/parseDef.mjs +139 -54
- package/node_modules/openai/_vendor/zod-to-json-schema/parsers/array.js +1 -1
- package/node_modules/openai/_vendor/zod-to-json-schema/parsers/array.mjs +1 -1
- package/node_modules/openai/_vendor/zod-to-json-schema/parsers/bigint.js +10 -4
- package/node_modules/openai/_vendor/zod-to-json-schema/parsers/bigint.mjs +10 -4
- package/node_modules/openai/_vendor/zod-to-json-schema/parsers/branded.js +2 -2
- package/node_modules/openai/_vendor/zod-to-json-schema/parsers/branded.mjs +2 -2
- package/node_modules/openai/_vendor/zod-to-json-schema/parsers/catch.js +1 -3
- package/node_modules/openai/_vendor/zod-to-json-schema/parsers/catch.mjs +1 -3
- package/node_modules/openai/_vendor/zod-to-json-schema/parsers/date.js +10 -5
- package/node_modules/openai/_vendor/zod-to-json-schema/parsers/date.mjs +10 -5
- package/node_modules/openai/_vendor/zod-to-json-schema/parsers/default.js +2 -2
- package/node_modules/openai/_vendor/zod-to-json-schema/parsers/default.mjs +2 -2
- package/node_modules/openai/_vendor/zod-to-json-schema/parsers/intersection.js +6 -5
- package/node_modules/openai/_vendor/zod-to-json-schema/parsers/intersection.mjs +6 -5
- package/node_modules/openai/_vendor/zod-to-json-schema/parsers/literal.js +5 -0
- package/node_modules/openai/_vendor/zod-to-json-schema/parsers/literal.mjs +5 -0
- package/node_modules/openai/_vendor/zod-to-json-schema/parsers/nativeEnum.js +7 -9
- package/node_modules/openai/_vendor/zod-to-json-schema/parsers/nativeEnum.mjs +7 -9
- package/node_modules/openai/_vendor/zod-to-json-schema/parsers/null.js +3 -2
- package/node_modules/openai/_vendor/zod-to-json-schema/parsers/null.mjs +3 -2
- package/node_modules/openai/_vendor/zod-to-json-schema/parsers/nullable.js +8 -3
- package/node_modules/openai/_vendor/zod-to-json-schema/parsers/nullable.mjs +8 -3
- package/node_modules/openai/_vendor/zod-to-json-schema/parsers/number.js +12 -5
- package/node_modules/openai/_vendor/zod-to-json-schema/parsers/number.mjs +12 -5
- package/node_modules/openai/_vendor/zod-to-json-schema/parsers/object.js +43 -38
- package/node_modules/openai/_vendor/zod-to-json-schema/parsers/object.mjs +43 -38
- package/node_modules/openai/_vendor/zod-to-json-schema/parsers/optional.js +11 -12
- package/node_modules/openai/_vendor/zod-to-json-schema/parsers/optional.mjs +11 -12
- package/node_modules/openai/_vendor/zod-to-json-schema/parsers/pipeline.js +3 -3
- package/node_modules/openai/_vendor/zod-to-json-schema/parsers/pipeline.mjs +3 -3
- package/node_modules/openai/_vendor/zod-to-json-schema/parsers/promise.js +2 -2
- package/node_modules/openai/_vendor/zod-to-json-schema/parsers/promise.mjs +2 -2
- package/node_modules/openai/_vendor/zod-to-json-schema/parsers/readonly.js +1 -3
- package/node_modules/openai/_vendor/zod-to-json-schema/parsers/readonly.mjs +1 -3
- package/node_modules/openai/_vendor/zod-to-json-schema/parsers/record.js +7 -5
- package/node_modules/openai/_vendor/zod-to-json-schema/parsers/record.mjs +7 -5
- package/node_modules/openai/_vendor/zod-to-json-schema/parsers/string.js +57 -34
- package/node_modules/openai/_vendor/zod-to-json-schema/parsers/string.mjs +57 -34
- package/node_modules/openai/_vendor/zod-to-json-schema/parsers/tuple.js +24 -19
- package/node_modules/openai/_vendor/zod-to-json-schema/parsers/tuple.mjs +24 -19
- package/node_modules/openai/_vendor/zod-to-json-schema/parsers/union.js +46 -24
- package/node_modules/openai/_vendor/zod-to-json-schema/parsers/union.mjs +46 -24
- package/node_modules/openai/_vendor/zod-to-json-schema/util.js +7 -5
- package/node_modules/openai/_vendor/zod-to-json-schema/util.mjs +7 -5
- package/node_modules/openai/_vendor/zod-to-json-schema/zodToJsonSchema.js +110 -38
- package/node_modules/openai/_vendor/zod-to-json-schema/zodToJsonSchema.mjs +110 -38
- package/node_modules/openai/auth/subject-token-providers.js +235 -10
- package/node_modules/openai/auth/subject-token-providers.mjs +235 -10
- package/node_modules/openai/auth/workload-identity-auth.js +185 -24
- package/node_modules/openai/auth/workload-identity-auth.mjs +186 -25
- package/node_modules/openai/auth/x509-transport.js +273 -0
- package/node_modules/openai/auth/x509-transport.mjs +268 -0
- package/node_modules/openai/azure.js +74 -21
- package/node_modules/openai/azure.mjs +75 -22
- package/node_modules/openai/bedrock.js +174 -0
- package/node_modules/openai/bedrock.mjs +169 -0
- package/node_modules/openai/beta/realtime/internal-base.js +71 -10
- package/node_modules/openai/beta/realtime/internal-base.mjs +70 -10
- package/node_modules/openai/beta/realtime/websocket.js +180 -33
- package/node_modules/openai/beta/realtime/websocket.mjs +181 -34
- package/node_modules/openai/beta/realtime/ws.js +93 -14
- package/node_modules/openai/beta/realtime/ws.mjs +94 -15
- package/node_modules/openai/client.js +745 -99
- package/node_modules/openai/client.mjs +747 -101
- package/node_modules/openai/core/EventEmitter.js +127 -16
- package/node_modules/openai/core/EventEmitter.mjs +128 -17
- package/node_modules/openai/core/api-promise.js +0 -1
- package/node_modules/openai/core/api-promise.mjs +0 -1
- package/node_modules/openai/core/error.js +5 -5
- package/node_modules/openai/core/error.mjs +5 -5
- package/node_modules/openai/core/pagination.js +33 -3
- package/node_modules/openai/core/pagination.mjs +31 -2
- package/node_modules/openai/core/resource.js +0 -1
- package/node_modules/openai/core/resource.mjs +0 -1
- package/node_modules/openai/core/streaming.js +453 -88
- package/node_modules/openai/core/streaming.mjs +454 -89
- package/node_modules/openai/core/uploads.js +3 -1
- package/node_modules/openai/core/uploads.mjs +1 -0
- package/node_modules/openai/helpers/audio.js +198 -28
- package/node_modules/openai/helpers/audio.mjs +199 -29
- package/node_modules/openai/helpers/live.js +6 -0
- package/node_modules/openai/helpers/live.mjs +2 -0
- package/node_modules/openai/helpers/standard-schema.js +393 -0
- package/node_modules/openai/helpers/standard-schema.mjs +387 -0
- package/node_modules/openai/helpers/zod-v3-strict-schema.js +408 -0
- package/node_modules/openai/helpers/zod-v3-strict-schema.mjs +404 -0
- package/node_modules/openai/helpers/zod.js +221 -24
- package/node_modules/openai/helpers/zod.mjs +222 -26
- package/node_modules/openai/index.js +8 -7
- package/node_modules/openai/index.mjs +2 -2
- package/node_modules/openai/internal/assistant-stream-delta.js +316 -0
- package/node_modules/openai/internal/assistant-stream-delta.mjs +308 -0
- package/node_modules/openai/internal/auth/x509-api-origin.js +28 -0
- package/node_modules/openai/internal/auth/x509-api-origin.mjs +24 -0
- package/node_modules/openai/internal/auth/x509-credential-options.js +125 -0
- package/node_modules/openai/internal/auth/x509-credential-options.mjs +121 -0
- package/node_modules/openai/internal/auth/x509-token-exchange.js +248 -0
- package/node_modules/openai/internal/auth/x509-token-exchange.mjs +245 -0
- package/node_modules/openai/internal/auth/x509-transport-capability.js +171 -0
- package/node_modules/openai/internal/auth/x509-transport-capability.mjs +166 -0
- package/node_modules/openai/internal/auth/x509-transport-registry.js +53 -0
- package/node_modules/openai/internal/auth/x509-transport-registry.mjs +48 -0
- package/node_modules/openai/internal/auth/x509-transport-state-browser.js +35 -0
- package/node_modules/openai/internal/auth/x509-transport-state-browser.mjs +32 -0
- package/node_modules/openai/internal/auth/x509-transport-state.cjs +35 -0
- package/node_modules/openai/internal/auth/x509-transport-state.js +36 -0
- package/node_modules/openai/internal/auth/x509-transport-state.mjs +20 -0
- package/node_modules/openai/internal/auth/x509-workload-identity-auth.js +787 -0
- package/node_modules/openai/internal/auth/x509-workload-identity-auth.mjs +778 -0
- package/node_modules/openai/internal/bedrock.js +438 -0
- package/node_modules/openai/internal/bedrock.mjs +424 -0
- package/node_modules/openai/internal/builtin-types.js +0 -1
- package/node_modules/openai/internal/builtin-types.mjs +0 -1
- package/node_modules/openai/internal/data-residency.js +34 -0
- package/node_modules/openai/internal/data-residency.mjs +30 -0
- package/node_modules/openai/internal/decoders/line.js +140 -54
- package/node_modules/openai/internal/decoders/line.mjs +141 -55
- package/node_modules/openai/internal/detect-platform.js +3 -4
- package/node_modules/openai/internal/detect-platform.mjs +3 -4
- package/node_modules/openai/internal/errors.js +3 -3
- package/node_modules/openai/internal/errors.mjs +3 -3
- package/node_modules/openai/internal/headers.js +11 -9
- package/node_modules/openai/internal/headers.mjs +11 -9
- package/node_modules/openai/internal/multipart-encoding.js +51 -0
- package/node_modules/openai/internal/multipart-encoding.mjs +48 -0
- package/node_modules/openai/internal/parse.js +35 -11
- package/node_modules/openai/internal/parse.mjs +35 -11
- package/node_modules/openai/internal/provider.js +50 -0
- package/node_modules/openai/internal/provider.mjs +46 -0
- package/node_modules/openai/internal/qs/formats.js +1 -2
- package/node_modules/openai/internal/qs/formats.mjs +1 -1
- package/node_modules/openai/internal/qs/stringify.js +60 -51
- package/node_modules/openai/internal/qs/stringify.mjs +60 -51
- package/node_modules/openai/internal/qs/utils.js +368 -50
- package/node_modules/openai/internal/qs/utils.mjs +368 -50
- package/node_modules/openai/internal/realtime-credentials.js +23 -0
- package/node_modules/openai/internal/realtime-credentials.mjs +19 -0
- package/node_modules/openai/internal/request-options.js +43 -2
- package/node_modules/openai/internal/request-options.mjs +42 -2
- package/node_modules/openai/internal/responses/canonical-output-text.js +123 -0
- package/node_modules/openai/internal/responses/canonical-output-text.mjs +115 -0
- package/node_modules/openai/internal/responses/output-text-index.js +77 -0
- package/node_modules/openai/internal/responses/output-text-index.mjs +73 -0
- package/node_modules/openai/internal/responses/response-accumulator.js +942 -0
- package/node_modules/openai/internal/responses/response-accumulator.mjs +938 -0
- package/node_modules/openai/internal/shim-types.js +6 -1
- package/node_modules/openai/internal/shim-types.mjs +6 -1
- package/node_modules/openai/internal/shims.js +6 -1
- package/node_modules/openai/internal/shims.mjs +6 -1
- package/node_modules/openai/internal/stream-utils.js +35 -7
- package/node_modules/openai/internal/stream-utils.mjs +35 -7
- package/node_modules/openai/internal/to-file.js +47 -24
- package/node_modules/openai/internal/to-file.mjs +47 -24
- package/node_modules/openai/internal/tslib.js +6 -0
- package/node_modules/openai/internal/tslib.mjs +6 -1
- package/node_modules/openai/internal/types.js +0 -1
- package/node_modules/openai/internal/types.mjs +0 -1
- package/node_modules/openai/internal/uploads.js +377 -43
- package/node_modules/openai/internal/uploads.mjs +376 -44
- package/node_modules/openai/internal/utils/abort.js +94 -0
- package/node_modules/openai/internal/utils/abort.mjs +90 -0
- package/node_modules/openai/internal/utils/base64.js +3 -1
- package/node_modules/openai/internal/utils/base64.mjs +3 -1
- package/node_modules/openai/internal/utils/bytes.js +5 -0
- package/node_modules/openai/internal/utils/bytes.mjs +5 -0
- package/node_modules/openai/internal/utils/env.js +9 -5
- package/node_modules/openai/internal/utils/env.mjs +9 -5
- package/node_modules/openai/internal/utils/log.js +73 -11
- package/node_modules/openai/internal/utils/log.mjs +70 -11
- package/node_modules/openai/internal/utils/path.js +50 -30
- package/node_modules/openai/internal/utils/path.mjs +50 -30
- package/node_modules/openai/internal/utils/query.js +0 -1
- package/node_modules/openai/internal/utils/query.mjs +0 -1
- package/node_modules/openai/internal/utils/sleep.js +51 -2
- package/node_modules/openai/internal/utils/sleep.mjs +51 -2
- package/node_modules/openai/internal/utils/uuid.js +0 -1
- package/node_modules/openai/internal/utils/uuid.mjs +0 -1
- package/node_modules/openai/internal/utils/values.js +0 -1
- package/node_modules/openai/internal/utils/values.mjs +0 -1
- package/node_modules/openai/internal/utils.js +0 -1
- package/node_modules/openai/internal/utils.mjs +0 -1
- package/node_modules/openai/internal/ws-adapter-browser.js +66 -21
- package/node_modules/openai/internal/ws-adapter-browser.mjs +66 -21
- package/node_modules/openai/internal/ws-adapter-node.js +90 -24
- package/node_modules/openai/internal/ws-adapter-node.mjs +90 -24
- package/node_modules/openai/internal/ws-adapter.js +4 -0
- package/node_modules/openai/internal/ws-adapter.mjs +4 -0
- package/node_modules/openai/internal/ws.js +193 -48
- package/node_modules/openai/internal/ws.mjs +188 -49
- package/node_modules/openai/lib/AbstractChatCompletionRunner.js +254 -72
- package/node_modules/openai/lib/AbstractChatCompletionRunner.mjs +256 -74
- package/node_modules/openai/lib/AssistantStream.js +430 -259
- package/node_modules/openai/lib/AssistantStream.mjs +432 -261
- package/node_modules/openai/lib/ChatCompletionRunner.js +12 -5
- package/node_modules/openai/lib/ChatCompletionRunner.mjs +13 -6
- package/node_modules/openai/lib/ChatCompletionStream.js +1430 -189
- package/node_modules/openai/lib/ChatCompletionStream.mjs +1433 -193
- package/node_modules/openai/lib/ChatCompletionStreamingRunner.js +40 -2
- package/node_modules/openai/lib/ChatCompletionStreamingRunner.mjs +41 -3
- package/node_modules/openai/lib/EventEmitter.js +124 -16
- package/node_modules/openai/lib/EventEmitter.mjs +125 -17
- package/node_modules/openai/lib/EventStream.js +1533 -55
- package/node_modules/openai/lib/EventStream.mjs +1534 -56
- package/node_modules/openai/lib/ResponsesParser.js +68 -29
- package/node_modules/openai/lib/ResponsesParser.mjs +69 -30
- package/node_modules/openai/lib/RunnableFunction.js +3 -0
- package/node_modules/openai/lib/RunnableFunction.mjs +3 -0
- package/node_modules/openai/lib/Util.js +7 -4
- package/node_modules/openai/lib/Util.mjs +7 -4
- package/node_modules/openai/lib/agents/agent-session-stream.js +274 -0
- package/node_modules/openai/lib/agents/agent-session-stream.mjs +270 -0
- package/node_modules/openai/lib/agents/output-text.js +8 -0
- package/node_modules/openai/lib/agents/output-text.mjs +5 -0
- package/node_modules/openai/lib/agents/turn-state.js +62 -0
- package/node_modules/openai/lib/agents/turn-state.mjs +58 -0
- package/node_modules/openai/lib/assistant-run-polling.js +17 -0
- package/node_modules/openai/lib/assistant-run-polling.mjs +14 -0
- package/node_modules/openai/lib/chatCompletionUtils.js +5 -6
- package/node_modules/openai/lib/chatCompletionUtils.mjs +5 -6
- package/node_modules/openai/lib/embeddings.js +58 -0
- package/node_modules/openai/lib/embeddings.mjs +55 -0
- package/node_modules/openai/lib/file-processing.js +31 -0
- package/node_modules/openai/lib/file-processing.mjs +28 -0
- package/node_modules/openai/lib/live/transcript-grouper.js +229 -0
- package/node_modules/openai/lib/live/transcript-grouper.mjs +225 -0
- package/node_modules/openai/lib/live/transcript-grouping.js +309 -0
- package/node_modules/openai/lib/live/transcript-grouping.mjs +305 -0
- package/node_modules/openai/lib/live/webrtc.js +16 -0
- package/node_modules/openai/lib/live/webrtc.mjs +10 -0
- package/node_modules/openai/lib/parser.js +142 -42
- package/node_modules/openai/lib/parser.mjs +140 -42
- package/node_modules/openai/lib/polling.js +131 -0
- package/node_modules/openai/lib/polling.mjs +128 -0
- package/node_modules/openai/lib/realtime/webrtc.js +16 -0
- package/node_modules/openai/lib/realtime/webrtc.mjs +10 -0
- package/node_modules/openai/lib/responses/ResponseAccumulator.js +15 -0
- package/node_modules/openai/lib/responses/ResponseAccumulator.mjs +12 -0
- package/node_modules/openai/lib/responses/ResponseInputItems.js +139 -0
- package/node_modules/openai/lib/responses/ResponseInputItems.mjs +135 -0
- package/node_modules/openai/lib/responses/ResponseStream.js +85 -159
- package/node_modules/openai/lib/responses/ResponseStream.mjs +86 -160
- package/node_modules/openai/lib/responses/responses-websocket-lane.js +314 -0
- package/node_modules/openai/lib/responses/responses-websocket-lane.mjs +309 -0
- package/node_modules/openai/lib/responses/responses-websocket-session.js +268 -0
- package/node_modules/openai/lib/responses/responses-websocket-session.mjs +263 -0
- package/node_modules/openai/lib/transform.js +1608 -82
- package/node_modules/openai/lib/transform.mjs +1602 -82
- package/node_modules/openai/lib/vector-store-polling.js +24 -0
- package/node_modules/openai/lib/vector-store-polling.mjs +20 -0
- package/node_modules/openai/lib/vector-store-upload.js +41 -0
- package/node_modules/openai/lib/vector-store-upload.mjs +38 -0
- package/node_modules/openai/lib/webhook-signature.js +125 -0
- package/node_modules/openai/lib/webhook-signature.mjs +121 -0
- package/node_modules/openai/lib/webrtc/connection.js +233 -0
- package/node_modules/openai/lib/webrtc/connection.mjs +229 -0
- package/node_modules/openai/lib/webrtc/data-channel.js +133 -0
- package/node_modules/openai/lib/webrtc/data-channel.mjs +129 -0
- package/node_modules/openai/lib/webrtc/errors.js +18 -0
- package/node_modules/openai/lib/webrtc/errors.mjs +14 -0
- package/node_modules/openai/lib/webrtc/subscriptions.js +64 -0
- package/node_modules/openai/lib/webrtc/subscriptions.mjs +60 -0
- package/node_modules/openai/lib/webrtc/types.js +3 -0
- package/node_modules/openai/lib/webrtc/types.mjs +2 -0
- package/node_modules/openai/live/webrtc.js +8 -0
- package/node_modules/openai/live/webrtc.mjs +2 -0
- package/node_modules/openai/package.json +71 -11
- package/node_modules/openai/providers/bedrock/aws.js +211 -0
- package/node_modules/openai/providers/bedrock/aws.mjs +207 -0
- package/node_modules/openai/providers/bedrock.js +43 -0
- package/node_modules/openai/providers/bedrock.mjs +39 -0
- package/node_modules/openai/realtime/internal-base.js +118 -11
- package/node_modules/openai/realtime/internal-base.mjs +116 -11
- package/node_modules/openai/realtime/webrtc.js +8 -0
- package/node_modules/openai/realtime/webrtc.mjs +2 -0
- package/node_modules/openai/realtime/websocket.js +174 -39
- package/node_modules/openai/realtime/websocket.mjs +175 -40
- package/node_modules/openai/realtime/ws.js +86 -18
- package/node_modules/openai/realtime/ws.mjs +87 -19
- package/node_modules/openai/resources/admin/admin.js +1 -1
- package/node_modules/openai/resources/admin/admin.mjs +1 -1
- package/node_modules/openai/resources/admin/index.js +1 -1
- package/node_modules/openai/resources/admin/index.mjs +1 -1
- package/node_modules/openai/resources/admin/organization/admin-api-keys.js +1 -1
- package/node_modules/openai/resources/admin/organization/admin-api-keys.mjs +1 -1
- package/node_modules/openai/resources/admin/organization/audit-logs.js +1 -1
- package/node_modules/openai/resources/admin/organization/audit-logs.mjs +1 -1
- package/node_modules/openai/resources/admin/organization/certificates.js +1 -1
- package/node_modules/openai/resources/admin/organization/certificates.mjs +1 -1
- package/node_modules/openai/resources/admin/organization/data-retention.js +1 -1
- package/node_modules/openai/resources/admin/organization/data-retention.mjs +1 -1
- package/node_modules/openai/resources/admin/organization/groups/groups.js +1 -1
- package/node_modules/openai/resources/admin/organization/groups/groups.mjs +1 -1
- package/node_modules/openai/resources/admin/organization/groups/index.js +1 -1
- package/node_modules/openai/resources/admin/organization/groups/index.mjs +1 -1
- package/node_modules/openai/resources/admin/organization/groups/roles.js +1 -1
- package/node_modules/openai/resources/admin/organization/groups/roles.mjs +1 -1
- package/node_modules/openai/resources/admin/organization/groups/users.js +1 -1
- package/node_modules/openai/resources/admin/organization/groups/users.mjs +1 -1
- package/node_modules/openai/resources/admin/organization/groups.js +1 -1
- package/node_modules/openai/resources/admin/organization/groups.mjs +1 -1
- package/node_modules/openai/resources/admin/organization/index.js +4 -2
- package/node_modules/openai/resources/admin/organization/index.mjs +2 -1
- package/node_modules/openai/resources/admin/organization/invites.js +1 -1
- package/node_modules/openai/resources/admin/organization/invites.mjs +1 -1
- package/node_modules/openai/resources/admin/organization/organization.js +5 -1
- package/node_modules/openai/resources/admin/organization/organization.mjs +5 -1
- package/node_modules/openai/resources/admin/organization/projects/api-keys.js +1 -1
- package/node_modules/openai/resources/admin/organization/projects/api-keys.mjs +1 -1
- package/node_modules/openai/resources/admin/organization/projects/certificates.js +1 -1
- package/node_modules/openai/resources/admin/organization/projects/certificates.mjs +1 -1
- package/node_modules/openai/resources/admin/organization/projects/data-retention.js +1 -1
- package/node_modules/openai/resources/admin/organization/projects/data-retention.mjs +1 -1
- package/node_modules/openai/resources/admin/organization/projects/groups/groups.js +1 -1
- package/node_modules/openai/resources/admin/organization/projects/groups/groups.mjs +1 -1
- package/node_modules/openai/resources/admin/organization/projects/groups/index.js +1 -1
- package/node_modules/openai/resources/admin/organization/projects/groups/index.mjs +1 -1
- package/node_modules/openai/resources/admin/organization/projects/groups/roles.js +1 -1
- package/node_modules/openai/resources/admin/organization/projects/groups/roles.mjs +1 -1
- package/node_modules/openai/resources/admin/organization/projects/groups.js +1 -1
- package/node_modules/openai/resources/admin/organization/projects/groups.mjs +1 -1
- package/node_modules/openai/resources/admin/organization/projects/hosted-tool-permissions.js +1 -1
- package/node_modules/openai/resources/admin/organization/projects/hosted-tool-permissions.mjs +1 -1
- package/node_modules/openai/resources/admin/organization/projects/index.js +8 -6
- package/node_modules/openai/resources/admin/organization/projects/index.mjs +3 -2
- package/node_modules/openai/resources/admin/organization/projects/model-permissions.js +1 -1
- package/node_modules/openai/resources/admin/organization/projects/model-permissions.mjs +1 -1
- package/node_modules/openai/resources/admin/organization/projects/projects.js +7 -3
- package/node_modules/openai/resources/admin/organization/projects/projects.mjs +7 -3
- package/node_modules/openai/resources/admin/organization/projects/rate-limits.js +1 -1
- package/node_modules/openai/resources/admin/organization/projects/rate-limits.mjs +1 -1
- package/node_modules/openai/resources/admin/organization/projects/roles.js +1 -1
- package/node_modules/openai/resources/admin/organization/projects/roles.mjs +1 -1
- package/node_modules/openai/resources/admin/organization/projects/service-accounts/api-keys.js +26 -0
- package/node_modules/openai/resources/admin/organization/projects/service-accounts/api-keys.mjs +22 -0
- package/node_modules/openai/resources/admin/organization/projects/service-accounts/index.js +9 -0
- package/node_modules/openai/resources/admin/organization/projects/service-accounts/index.mjs +4 -0
- package/node_modules/openai/resources/admin/organization/projects/service-accounts/service-accounts.js +109 -0
- package/node_modules/openai/resources/admin/organization/projects/service-accounts/service-accounts.mjs +104 -0
- package/node_modules/openai/resources/admin/organization/projects/service-accounts.js +3 -98
- package/node_modules/openai/resources/admin/organization/projects/service-accounts.mjs +2 -96
- package/node_modules/openai/resources/admin/organization/projects/spend-alerts.js +20 -1
- package/node_modules/openai/resources/admin/organization/projects/spend-alerts.mjs +20 -1
- package/node_modules/openai/resources/admin/organization/projects/spend-limit.js +67 -0
- package/node_modules/openai/resources/admin/organization/projects/spend-limit.mjs +63 -0
- package/node_modules/openai/resources/admin/organization/projects/users/index.js +1 -1
- package/node_modules/openai/resources/admin/organization/projects/users/index.mjs +1 -1
- package/node_modules/openai/resources/admin/organization/projects/users/roles.js +1 -1
- package/node_modules/openai/resources/admin/organization/projects/users/roles.mjs +1 -1
- package/node_modules/openai/resources/admin/organization/projects/users/users.js +1 -1
- package/node_modules/openai/resources/admin/organization/projects/users/users.mjs +1 -1
- package/node_modules/openai/resources/admin/organization/projects/users.js +1 -1
- package/node_modules/openai/resources/admin/organization/projects/users.mjs +1 -1
- package/node_modules/openai/resources/admin/organization/projects.js +1 -1
- package/node_modules/openai/resources/admin/organization/projects.mjs +1 -1
- package/node_modules/openai/resources/admin/organization/roles.js +1 -1
- package/node_modules/openai/resources/admin/organization/roles.mjs +1 -1
- package/node_modules/openai/resources/admin/organization/spend-alerts.js +18 -1
- package/node_modules/openai/resources/admin/organization/spend-alerts.mjs +18 -1
- package/node_modules/openai/resources/admin/organization/spend-limit.js +59 -0
- package/node_modules/openai/resources/admin/organization/spend-limit.mjs +55 -0
- package/node_modules/openai/resources/admin/organization/usage.js +1 -1
- package/node_modules/openai/resources/admin/organization/usage.mjs +1 -1
- package/node_modules/openai/resources/admin/organization/users/index.js +1 -1
- package/node_modules/openai/resources/admin/organization/users/index.mjs +1 -1
- package/node_modules/openai/resources/admin/organization/users/roles.js +1 -1
- package/node_modules/openai/resources/admin/organization/users/roles.mjs +1 -1
- package/node_modules/openai/resources/admin/organization/users/users.js +1 -1
- package/node_modules/openai/resources/admin/organization/users/users.mjs +1 -1
- package/node_modules/openai/resources/admin/organization/users.js +1 -1
- package/node_modules/openai/resources/admin/organization/users.mjs +1 -1
- package/node_modules/openai/resources/admin/organization.js +1 -1
- package/node_modules/openai/resources/admin/organization.mjs +1 -1
- package/node_modules/openai/resources/admin.js +1 -1
- package/node_modules/openai/resources/admin.mjs +1 -1
- package/node_modules/openai/resources/audio/audio.js +1 -1
- package/node_modules/openai/resources/audio/audio.mjs +1 -1
- package/node_modules/openai/resources/audio/index.js +1 -1
- package/node_modules/openai/resources/audio/index.mjs +1 -1
- package/node_modules/openai/resources/audio/speech.js +1 -1
- package/node_modules/openai/resources/audio/speech.mjs +1 -1
- package/node_modules/openai/resources/audio/transcriptions.js +1 -1
- package/node_modules/openai/resources/audio/transcriptions.mjs +1 -1
- package/node_modules/openai/resources/audio/translations.js +1 -1
- package/node_modules/openai/resources/audio/translations.mjs +1 -1
- package/node_modules/openai/resources/audio.js +1 -1
- package/node_modules/openai/resources/audio.mjs +1 -1
- package/node_modules/openai/resources/batches.js +1 -1
- package/node_modules/openai/resources/batches.mjs +1 -1
- package/node_modules/openai/resources/beta/agents/agents.js +118 -0
- package/node_modules/openai/resources/beta/agents/agents.mjs +113 -0
- package/node_modules/openai/resources/beta/agents/environments/environments.js +43 -0
- package/node_modules/openai/resources/beta/agents/environments/environments.mjs +38 -0
- package/node_modules/openai/resources/beta/agents/environments/files.js +61 -0
- package/node_modules/openai/resources/beta/agents/environments/files.mjs +57 -0
- package/node_modules/openai/resources/beta/agents/environments/index.js +11 -0
- package/node_modules/openai/resources/beta/agents/environments/index.mjs +5 -0
- package/node_modules/openai/resources/beta/agents/environments/templates.js +112 -0
- package/node_modules/openai/resources/beta/agents/environments/templates.mjs +108 -0
- package/node_modules/openai/resources/beta/agents/environments.js +6 -0
- package/node_modules/openai/resources/beta/agents/environments.mjs +3 -0
- package/node_modules/openai/resources/beta/agents/index.js +13 -0
- package/node_modules/openai/resources/beta/agents/index.mjs +6 -0
- package/node_modules/openai/resources/beta/agents/sessions/artifacts.js +106 -0
- package/node_modules/openai/resources/beta/agents/sessions/artifacts.mjs +102 -0
- package/node_modules/openai/resources/beta/agents/sessions/events.js +75 -0
- package/node_modules/openai/resources/beta/agents/sessions/events.mjs +71 -0
- package/node_modules/openai/resources/beta/agents/sessions/index.js +17 -0
- package/node_modules/openai/resources/beta/agents/sessions/index.mjs +8 -0
- package/node_modules/openai/resources/beta/agents/sessions/items.js +35 -0
- package/node_modules/openai/resources/beta/agents/sessions/items.mjs +31 -0
- package/node_modules/openai/resources/beta/agents/sessions/sessions.js +127 -0
- package/node_modules/openai/resources/beta/agents/sessions/sessions.mjs +122 -0
- package/node_modules/openai/resources/beta/agents/sessions/subagents/index.js +11 -0
- package/node_modules/openai/resources/beta/agents/sessions/subagents/index.mjs +5 -0
- package/node_modules/openai/resources/beta/agents/sessions/subagents/items.js +36 -0
- package/node_modules/openai/resources/beta/agents/sessions/subagents/items.mjs +32 -0
- package/node_modules/openai/resources/beta/agents/sessions/subagents/subagents.js +67 -0
- package/node_modules/openai/resources/beta/agents/sessions/subagents/subagents.mjs +62 -0
- package/node_modules/openai/resources/beta/agents/sessions/subagents/turns/index.js +9 -0
- package/node_modules/openai/resources/beta/agents/sessions/subagents/turns/index.mjs +4 -0
- package/node_modules/openai/resources/beta/agents/sessions/subagents/turns/items.js +36 -0
- package/node_modules/openai/resources/beta/agents/sessions/subagents/turns/items.mjs +32 -0
- package/node_modules/openai/resources/beta/agents/sessions/subagents/turns/turns.js +68 -0
- package/node_modules/openai/resources/beta/agents/sessions/subagents/turns/turns.mjs +63 -0
- package/node_modules/openai/resources/beta/agents/sessions/subagents/turns.js +6 -0
- package/node_modules/openai/resources/beta/agents/sessions/subagents/turns.mjs +3 -0
- package/node_modules/openai/resources/beta/agents/sessions/subagents.js +6 -0
- package/node_modules/openai/resources/beta/agents/sessions/subagents.mjs +3 -0
- package/node_modules/openai/resources/beta/agents/sessions/turns.js +57 -0
- package/node_modules/openai/resources/beta/agents/sessions/turns.mjs +53 -0
- package/node_modules/openai/resources/beta/agents/sessions.js +6 -0
- package/node_modules/openai/resources/beta/agents/sessions.mjs +3 -0
- package/node_modules/openai/resources/beta/agents/vaults/credentials.js +132 -0
- package/node_modules/openai/resources/beta/agents/vaults/credentials.mjs +128 -0
- package/node_modules/openai/resources/beta/agents/vaults/index.js +9 -0
- package/node_modules/openai/resources/beta/agents/vaults/index.mjs +4 -0
- package/node_modules/openai/resources/beta/agents/vaults/vaults.js +93 -0
- package/node_modules/openai/resources/beta/agents/vaults/vaults.mjs +88 -0
- package/node_modules/openai/resources/beta/agents/vaults.js +6 -0
- package/node_modules/openai/resources/beta/agents/vaults.mjs +3 -0
- package/node_modules/openai/resources/beta/agents.js +6 -0
- package/node_modules/openai/resources/beta/agents.mjs +3 -0
- package/node_modules/openai/resources/beta/assistants.js +1 -1
- package/node_modules/openai/resources/beta/assistants.mjs +1 -1
- package/node_modules/openai/resources/beta/beta.js +9 -1
- package/node_modules/openai/resources/beta/beta.mjs +9 -1
- package/node_modules/openai/resources/beta/chatkit/chatkit.js +1 -1
- package/node_modules/openai/resources/beta/chatkit/chatkit.mjs +1 -1
- package/node_modules/openai/resources/beta/chatkit/index.js +1 -1
- package/node_modules/openai/resources/beta/chatkit/index.mjs +1 -1
- package/node_modules/openai/resources/beta/chatkit/sessions.js +1 -1
- package/node_modules/openai/resources/beta/chatkit/sessions.mjs +1 -1
- package/node_modules/openai/resources/beta/chatkit/threads.js +1 -1
- package/node_modules/openai/resources/beta/chatkit/threads.mjs +1 -1
- package/node_modules/openai/resources/beta/chatkit.js +1 -1
- package/node_modules/openai/resources/beta/chatkit.mjs +1 -1
- package/node_modules/openai/resources/beta/index.js +12 -8
- package/node_modules/openai/resources/beta/index.mjs +3 -1
- package/node_modules/openai/resources/beta/realtime/index.js +0 -1
- package/node_modules/openai/resources/beta/realtime/index.mjs +0 -1
- package/node_modules/openai/resources/beta/realtime/realtime.js +0 -1
- package/node_modules/openai/resources/beta/realtime/realtime.mjs +0 -1
- package/node_modules/openai/resources/beta/realtime/sessions.js +0 -1
- package/node_modules/openai/resources/beta/realtime/sessions.mjs +0 -1
- package/node_modules/openai/resources/beta/realtime/transcription-sessions.js +0 -1
- package/node_modules/openai/resources/beta/realtime/transcription-sessions.mjs +0 -1
- package/node_modules/openai/resources/beta/realtime.js +0 -1
- package/node_modules/openai/resources/beta/realtime.mjs +0 -1
- package/node_modules/openai/resources/beta/responses/index.js +11 -0
- package/node_modules/openai/resources/beta/responses/index.mjs +5 -0
- package/node_modules/openai/resources/beta/responses/input-items.js +37 -0
- package/node_modules/openai/resources/beta/responses/input-items.mjs +33 -0
- package/node_modules/openai/resources/beta/responses/input-tokens.js +34 -0
- package/node_modules/openai/resources/beta/responses/input-tokens.mjs +30 -0
- package/node_modules/openai/resources/beta/responses/internal-base.js +66 -0
- package/node_modules/openai/resources/beta/responses/internal-base.mjs +60 -0
- package/node_modules/openai/resources/beta/responses/responses.js +121 -0
- package/node_modules/openai/resources/beta/responses/responses.mjs +116 -0
- package/node_modules/openai/resources/beta/responses/ws-base.js +566 -0
- package/node_modules/openai/resources/beta/responses/ws-base.mjs +560 -0
- package/node_modules/openai/resources/beta/responses/ws.js +47 -0
- package/node_modules/openai/resources/beta/responses/ws.mjs +42 -0
- package/node_modules/openai/resources/beta/responses.js +6 -0
- package/node_modules/openai/resources/beta/responses.mjs +3 -0
- package/node_modules/openai/resources/beta/threads/index.js +1 -1
- package/node_modules/openai/resources/beta/threads/index.mjs +1 -1
- package/node_modules/openai/resources/beta/threads/messages.js +1 -1
- package/node_modules/openai/resources/beta/threads/messages.mjs +1 -1
- package/node_modules/openai/resources/beta/threads/runs/index.js +1 -1
- package/node_modules/openai/resources/beta/threads/runs/index.mjs +1 -1
- package/node_modules/openai/resources/beta/threads/runs/runs.js +3 -44
- package/node_modules/openai/resources/beta/threads/runs/runs.mjs +3 -44
- package/node_modules/openai/resources/beta/threads/runs/steps.js +1 -1
- package/node_modules/openai/resources/beta/threads/runs/steps.mjs +1 -1
- package/node_modules/openai/resources/beta/threads/runs.js +1 -1
- package/node_modules/openai/resources/beta/threads/runs.mjs +1 -1
- package/node_modules/openai/resources/beta/threads/threads.js +1 -1
- package/node_modules/openai/resources/beta/threads/threads.mjs +1 -1
- package/node_modules/openai/resources/beta/threads.js +1 -1
- package/node_modules/openai/resources/beta/threads.mjs +1 -1
- package/node_modules/openai/resources/beta.js +1 -1
- package/node_modules/openai/resources/beta.mjs +1 -1
- package/node_modules/openai/resources/chat/chat.js +1 -1
- package/node_modules/openai/resources/chat/chat.mjs +1 -1
- package/node_modules/openai/resources/chat/completions/completions.js +2 -5
- package/node_modules/openai/resources/chat/completions/completions.mjs +2 -5
- package/node_modules/openai/resources/chat/completions/index.js +1 -1
- package/node_modules/openai/resources/chat/completions/index.mjs +1 -1
- package/node_modules/openai/resources/chat/completions/messages.js +1 -1
- package/node_modules/openai/resources/chat/completions/messages.mjs +1 -1
- package/node_modules/openai/resources/chat/completions.js +1 -1
- package/node_modules/openai/resources/chat/completions.mjs +1 -1
- package/node_modules/openai/resources/chat/index.js +1 -1
- package/node_modules/openai/resources/chat/index.mjs +1 -1
- package/node_modules/openai/resources/chat.js +1 -1
- package/node_modules/openai/resources/chat.mjs +1 -1
- package/node_modules/openai/resources/completions.js +1 -1
- package/node_modules/openai/resources/completions.mjs +1 -1
- package/node_modules/openai/resources/containers/containers.js +1 -1
- package/node_modules/openai/resources/containers/containers.mjs +1 -1
- package/node_modules/openai/resources/containers/files/content.js +1 -1
- package/node_modules/openai/resources/containers/files/content.mjs +1 -1
- package/node_modules/openai/resources/containers/files/files.js +1 -1
- package/node_modules/openai/resources/containers/files/files.mjs +1 -1
- package/node_modules/openai/resources/containers/files/index.js +1 -1
- package/node_modules/openai/resources/containers/files/index.mjs +1 -1
- package/node_modules/openai/resources/containers/files.js +1 -1
- package/node_modules/openai/resources/containers/files.mjs +1 -1
- package/node_modules/openai/resources/containers/index.js +1 -1
- package/node_modules/openai/resources/containers/index.mjs +1 -1
- package/node_modules/openai/resources/containers.js +1 -1
- package/node_modules/openai/resources/containers.mjs +1 -1
- package/node_modules/openai/resources/content-provenance-checks.js +24 -0
- package/node_modules/openai/resources/content-provenance-checks.mjs +20 -0
- package/node_modules/openai/resources/conversations/conversations.js +1 -1
- package/node_modules/openai/resources/conversations/conversations.mjs +1 -1
- package/node_modules/openai/resources/conversations/index.js +1 -1
- package/node_modules/openai/resources/conversations/index.mjs +1 -1
- package/node_modules/openai/resources/conversations/items.js +1 -1
- package/node_modules/openai/resources/conversations/items.mjs +1 -1
- package/node_modules/openai/resources/conversations.js +1 -1
- package/node_modules/openai/resources/conversations.mjs +1 -1
- package/node_modules/openai/resources/embeddings.js +3 -47
- package/node_modules/openai/resources/embeddings.mjs +3 -47
- package/node_modules/openai/resources/evals/evals.js +2 -2
- package/node_modules/openai/resources/evals/evals.mjs +2 -2
- package/node_modules/openai/resources/evals/index.js +1 -1
- package/node_modules/openai/resources/evals/index.mjs +1 -1
- package/node_modules/openai/resources/evals/runs/index.js +1 -1
- package/node_modules/openai/resources/evals/runs/index.mjs +1 -1
- package/node_modules/openai/resources/evals/runs/output-items.js +1 -1
- package/node_modules/openai/resources/evals/runs/output-items.mjs +1 -1
- package/node_modules/openai/resources/evals/runs/runs.js +1 -1
- package/node_modules/openai/resources/evals/runs/runs.mjs +1 -1
- package/node_modules/openai/resources/evals/runs.js +1 -1
- package/node_modules/openai/resources/evals/runs.mjs +1 -1
- package/node_modules/openai/resources/evals.js +1 -1
- package/node_modules/openai/resources/evals.mjs +1 -1
- package/node_modules/openai/resources/files.js +9 -22
- package/node_modules/openai/resources/files.mjs +9 -22
- package/node_modules/openai/resources/fine-tuning/alpha/alpha.js +1 -1
- package/node_modules/openai/resources/fine-tuning/alpha/alpha.mjs +1 -1
- package/node_modules/openai/resources/fine-tuning/alpha/graders.js +1 -1
- package/node_modules/openai/resources/fine-tuning/alpha/graders.mjs +1 -1
- package/node_modules/openai/resources/fine-tuning/alpha/index.js +1 -1
- package/node_modules/openai/resources/fine-tuning/alpha/index.mjs +1 -1
- package/node_modules/openai/resources/fine-tuning/alpha.js +1 -1
- package/node_modules/openai/resources/fine-tuning/alpha.mjs +1 -1
- package/node_modules/openai/resources/fine-tuning/checkpoints/checkpoints.js +1 -1
- package/node_modules/openai/resources/fine-tuning/checkpoints/checkpoints.mjs +1 -1
- package/node_modules/openai/resources/fine-tuning/checkpoints/index.js +1 -1
- package/node_modules/openai/resources/fine-tuning/checkpoints/index.mjs +1 -1
- package/node_modules/openai/resources/fine-tuning/checkpoints/permissions.js +9 -5
- package/node_modules/openai/resources/fine-tuning/checkpoints/permissions.mjs +9 -5
- package/node_modules/openai/resources/fine-tuning/checkpoints.js +1 -1
- package/node_modules/openai/resources/fine-tuning/checkpoints.mjs +1 -1
- package/node_modules/openai/resources/fine-tuning/fine-tuning.js +1 -1
- package/node_modules/openai/resources/fine-tuning/fine-tuning.mjs +1 -1
- package/node_modules/openai/resources/fine-tuning/index.js +1 -1
- package/node_modules/openai/resources/fine-tuning/index.mjs +1 -1
- package/node_modules/openai/resources/fine-tuning/jobs/checkpoints.js +1 -1
- package/node_modules/openai/resources/fine-tuning/jobs/checkpoints.mjs +1 -1
- package/node_modules/openai/resources/fine-tuning/jobs/index.js +1 -1
- package/node_modules/openai/resources/fine-tuning/jobs/index.mjs +1 -1
- package/node_modules/openai/resources/fine-tuning/jobs/jobs.js +3 -3
- package/node_modules/openai/resources/fine-tuning/jobs/jobs.mjs +3 -3
- package/node_modules/openai/resources/fine-tuning/jobs.js +1 -1
- package/node_modules/openai/resources/fine-tuning/jobs.mjs +1 -1
- package/node_modules/openai/resources/fine-tuning/methods.js +1 -1
- package/node_modules/openai/resources/fine-tuning/methods.mjs +1 -1
- package/node_modules/openai/resources/fine-tuning.js +1 -1
- package/node_modules/openai/resources/fine-tuning.mjs +1 -1
- package/node_modules/openai/resources/graders/grader-models.js +1 -1
- package/node_modules/openai/resources/graders/grader-models.mjs +1 -1
- package/node_modules/openai/resources/graders/graders.js +1 -1
- package/node_modules/openai/resources/graders/graders.mjs +1 -1
- package/node_modules/openai/resources/graders/index.js +1 -1
- package/node_modules/openai/resources/graders/index.mjs +1 -1
- package/node_modules/openai/resources/graders.js +1 -1
- package/node_modules/openai/resources/graders.mjs +1 -1
- package/node_modules/openai/resources/images.js +8 -2
- package/node_modules/openai/resources/images.mjs +8 -2
- package/node_modules/openai/resources/index.js +9 -3
- package/node_modules/openai/resources/index.mjs +5 -2
- package/node_modules/openai/resources/live/forks/forks.js +9 -0
- package/node_modules/openai/resources/live/forks/forks.mjs +5 -0
- package/node_modules/openai/resources/live/forks/index.js +7 -0
- package/node_modules/openai/resources/live/forks/index.mjs +3 -0
- package/node_modules/openai/resources/live/forks/internal-base.js +65 -0
- package/node_modules/openai/resources/live/forks/internal-base.mjs +59 -0
- package/node_modules/openai/resources/live/forks/ws-base.js +547 -0
- package/node_modules/openai/resources/live/forks/ws-base.mjs +543 -0
- package/node_modules/openai/resources/live/forks/ws.js +32 -0
- package/node_modules/openai/resources/live/forks/ws.mjs +27 -0
- package/node_modules/openai/resources/live/forks.js +6 -0
- package/node_modules/openai/resources/live/forks.mjs +3 -0
- package/node_modules/openai/resources/live/index.js +13 -0
- package/node_modules/openai/resources/live/index.mjs +6 -0
- package/node_modules/openai/resources/live/internal-base.js +64 -0
- package/node_modules/openai/resources/live/internal-base.mjs +58 -0
- package/node_modules/openai/resources/live/live.js +40 -0
- package/node_modules/openai/resources/live/live.mjs +35 -0
- package/node_modules/openai/resources/live/sessions.js +124 -0
- package/node_modules/openai/resources/live/sessions.mjs +120 -0
- package/node_modules/openai/resources/live/sideband/index.js +7 -0
- package/node_modules/openai/resources/live/sideband/index.mjs +3 -0
- package/node_modules/openai/resources/live/sideband/internal-base.js +65 -0
- package/node_modules/openai/resources/live/sideband/internal-base.mjs +59 -0
- package/node_modules/openai/resources/live/sideband/sideband.js +9 -0
- package/node_modules/openai/resources/live/sideband/sideband.mjs +5 -0
- package/node_modules/openai/resources/live/sideband/ws-base.js +547 -0
- package/node_modules/openai/resources/live/sideband/ws-base.mjs +543 -0
- package/node_modules/openai/resources/live/sideband/ws.js +32 -0
- package/node_modules/openai/resources/live/sideband/ws.mjs +27 -0
- package/node_modules/openai/resources/live/sideband.js +6 -0
- package/node_modules/openai/resources/live/sideband.mjs +3 -0
- package/node_modules/openai/resources/live/ws-base.js +547 -0
- package/node_modules/openai/resources/live/ws-base.mjs +543 -0
- package/node_modules/openai/resources/live/ws.js +32 -0
- package/node_modules/openai/resources/live/ws.mjs +27 -0
- package/node_modules/openai/resources/live.js +6 -0
- package/node_modules/openai/resources/live.mjs +3 -0
- package/node_modules/openai/resources/models.js +1 -1
- package/node_modules/openai/resources/models.mjs +1 -1
- package/node_modules/openai/resources/moderations.js +3 -2
- package/node_modules/openai/resources/moderations.mjs +3 -2
- package/node_modules/openai/resources/realtime/calls.js +25 -1
- package/node_modules/openai/resources/realtime/calls.mjs +25 -1
- package/node_modules/openai/resources/realtime/client-secrets.js +2 -2
- package/node_modules/openai/resources/realtime/client-secrets.mjs +2 -2
- package/node_modules/openai/resources/realtime/index.js +1 -1
- package/node_modules/openai/resources/realtime/index.mjs +2 -2
- package/node_modules/openai/resources/realtime/realtime.js +1 -1
- package/node_modules/openai/resources/realtime/realtime.mjs +1 -1
- package/node_modules/openai/resources/realtime.js +1 -1
- package/node_modules/openai/resources/realtime.mjs +1 -1
- package/node_modules/openai/resources/responses/index.js +1 -1
- package/node_modules/openai/resources/responses/index.mjs +1 -1
- package/node_modules/openai/resources/responses/input-items.js +1 -1
- package/node_modules/openai/resources/responses/input-items.mjs +1 -1
- package/node_modules/openai/resources/responses/input-tokens.js +1 -1
- package/node_modules/openai/resources/responses/input-tokens.mjs +1 -1
- package/node_modules/openai/resources/responses/internal-base.js +24 -6
- package/node_modules/openai/resources/responses/internal-base.mjs +24 -6
- package/node_modules/openai/resources/responses/responses.js +5 -5
- package/node_modules/openai/resources/responses/responses.mjs +5 -5
- package/node_modules/openai/resources/responses/ws-base.js +115 -23
- package/node_modules/openai/resources/responses/ws-base.mjs +115 -25
- package/node_modules/openai/resources/responses/ws.js +23 -7
- package/node_modules/openai/resources/responses/ws.mjs +23 -7
- package/node_modules/openai/resources/responses.js +1 -1
- package/node_modules/openai/resources/responses.mjs +1 -1
- package/node_modules/openai/resources/safety/alerts.js +16 -0
- package/node_modules/openai/resources/safety/alerts.mjs +12 -0
- package/node_modules/openai/resources/safety/index.js +9 -0
- package/node_modules/openai/resources/safety/index.mjs +4 -0
- package/node_modules/openai/resources/safety/safety.js +17 -0
- package/node_modules/openai/resources/safety/safety.mjs +12 -0
- package/node_modules/openai/resources/safety.js +6 -0
- package/node_modules/openai/resources/safety.mjs +3 -0
- package/node_modules/openai/resources/shared.js +1 -1
- package/node_modules/openai/resources/shared.mjs +1 -1
- package/node_modules/openai/resources/skills/content.js +1 -1
- package/node_modules/openai/resources/skills/content.mjs +1 -1
- package/node_modules/openai/resources/skills/index.js +1 -1
- package/node_modules/openai/resources/skills/index.mjs +1 -1
- package/node_modules/openai/resources/skills/skills.js +4 -2
- package/node_modules/openai/resources/skills/skills.mjs +4 -2
- package/node_modules/openai/resources/skills/versions/content.js +1 -1
- package/node_modules/openai/resources/skills/versions/content.mjs +1 -1
- package/node_modules/openai/resources/skills/versions/index.js +1 -1
- package/node_modules/openai/resources/skills/versions/index.mjs +1 -1
- package/node_modules/openai/resources/skills/versions/versions.js +4 -2
- package/node_modules/openai/resources/skills/versions/versions.mjs +4 -2
- package/node_modules/openai/resources/skills/versions.js +1 -1
- package/node_modules/openai/resources/skills/versions.mjs +1 -1
- package/node_modules/openai/resources/skills.js +1 -1
- package/node_modules/openai/resources/skills.mjs +1 -1
- package/node_modules/openai/resources/uploads/index.js +1 -1
- package/node_modules/openai/resources/uploads/index.mjs +1 -1
- package/node_modules/openai/resources/uploads/parts.js +6 -5
- package/node_modules/openai/resources/uploads/parts.mjs +6 -5
- package/node_modules/openai/resources/uploads/uploads.js +10 -10
- package/node_modules/openai/resources/uploads/uploads.mjs +10 -10
- package/node_modules/openai/resources/uploads.js +1 -1
- package/node_modules/openai/resources/uploads.mjs +1 -1
- package/node_modules/openai/resources/vector-stores/file-batches.js +6 -63
- package/node_modules/openai/resources/vector-stores/file-batches.mjs +6 -63
- package/node_modules/openai/resources/vector-stores/files.js +6 -39
- package/node_modules/openai/resources/vector-stores/files.mjs +6 -39
- package/node_modules/openai/resources/vector-stores/index.js +1 -1
- package/node_modules/openai/resources/vector-stores/index.mjs +1 -1
- package/node_modules/openai/resources/vector-stores/vector-stores.js +1 -1
- package/node_modules/openai/resources/vector-stores/vector-stores.mjs +1 -1
- package/node_modules/openai/resources/vector-stores.js +1 -1
- package/node_modules/openai/resources/vector-stores.mjs +1 -1
- package/node_modules/openai/resources/videos.js +24 -1
- package/node_modules/openai/resources/videos.mjs +24 -1
- package/node_modules/openai/resources/webhooks/event-types.js +21 -0
- package/node_modules/openai/resources/webhooks/event-types.mjs +17 -0
- package/node_modules/openai/resources/webhooks/index.js +4 -1
- package/node_modules/openai/resources/webhooks/index.mjs +2 -1
- package/node_modules/openai/resources/webhooks/webhooks.js +127 -43
- package/node_modules/openai/resources/webhooks/webhooks.mjs +127 -43
- package/node_modules/openai/resources/webhooks.js +1 -1
- package/node_modules/openai/resources/webhooks.mjs +1 -1
- package/node_modules/openai/src/_vendor/partial-json-parser/LICENSE +21 -0
- package/node_modules/openai/src/_vendor/partial-json-parser/README.md +11 -1
- package/node_modules/openai/src/_vendor/partial-json-parser/parser.ts +66 -35
- package/node_modules/openai/src/_vendor/zod-to-json-schema/Options.ts +32 -18
- package/node_modules/openai/src/_vendor/zod-to-json-schema/Refs.ts +7 -6
- package/node_modules/openai/src/_vendor/zod-to-json-schema/errorMessages.ts +5 -3
- package/node_modules/openai/src/_vendor/zod-to-json-schema/index.ts +3 -1
- package/node_modules/openai/src/_vendor/zod-to-json-schema/parseDef.ts +194 -79
- package/node_modules/openai/src/_vendor/zod-to-json-schema/parsers/array.ts +8 -5
- package/node_modules/openai/src/_vendor/zod-to-json-schema/parsers/bigint.ts +20 -12
- package/node_modules/openai/src/_vendor/zod-to-json-schema/parsers/branded.ts +4 -4
- package/node_modules/openai/src/_vendor/zod-to-json-schema/parsers/catch.ts +4 -5
- package/node_modules/openai/src/_vendor/zod-to-json-schema/parsers/date.ts +16 -10
- package/node_modules/openai/src/_vendor/zod-to-json-schema/parsers/default.ts +10 -5
- package/node_modules/openai/src/_vendor/zod-to-json-schema/parsers/effects.ts +4 -3
- package/node_modules/openai/src/_vendor/zod-to-json-schema/parsers/enum.ts +1 -1
- package/node_modules/openai/src/_vendor/zod-to-json-schema/parsers/intersection.ts +12 -9
- package/node_modules/openai/src/_vendor/zod-to-json-schema/parsers/literal.ts +12 -2
- package/node_modules/openai/src/_vendor/zod-to-json-schema/parsers/map.ts +6 -4
- package/node_modules/openai/src/_vendor/zod-to-json-schema/parsers/nativeEnum.ts +10 -11
- package/node_modules/openai/src/_vendor/zod-to-json-schema/parsers/null.ts +4 -3
- package/node_modules/openai/src/_vendor/zod-to-json-schema/parsers/nullable.ts +25 -10
- package/node_modules/openai/src/_vendor/zod-to-json-schema/parsers/number.ts +17 -8
- package/node_modules/openai/src/_vendor/zod-to-json-schema/parsers/object.ts +57 -54
- package/node_modules/openai/src/_vendor/zod-to-json-schema/parsers/optional.ts +25 -17
- package/node_modules/openai/src/_vendor/zod-to-json-schema/parsers/pipeline.ts +8 -6
- package/node_modules/openai/src/_vendor/zod-to-json-schema/parsers/promise.ts +10 -5
- package/node_modules/openai/src/_vendor/zod-to-json-schema/parsers/readonly.ts +4 -5
- package/node_modules/openai/src/_vendor/zod-to-json-schema/parsers/record.ts +22 -19
- package/node_modules/openai/src/_vendor/zod-to-json-schema/parsers/set.ts +6 -4
- package/node_modules/openai/src/_vendor/zod-to-json-schema/parsers/string.ts +62 -37
- package/node_modules/openai/src/_vendor/zod-to-json-schema/parsers/tuple.ts +32 -25
- package/node_modules/openai/src/_vendor/zod-to-json-schema/parsers/union.ts +51 -33
- package/node_modules/openai/src/_vendor/zod-to-json-schema/util.ts +11 -6
- package/node_modules/openai/src/_vendor/zod-to-json-schema/zodToJsonSchema.ts +133 -52
- package/node_modules/openai/src/auth/index.ts +7 -1
- package/node_modules/openai/src/auth/subject-token-providers.ts +281 -16
- package/node_modules/openai/src/auth/types.ts +48 -5
- package/node_modules/openai/src/auth/workload-identity-auth.ts +227 -26
- package/node_modules/openai/src/auth/x509-transport.ts +369 -0
- package/node_modules/openai/src/azure.ts +127 -28
- package/node_modules/openai/src/bedrock.ts +294 -0
- package/node_modules/openai/src/beta/realtime/internal-base.ts +136 -16
- package/node_modules/openai/src/beta/realtime/websocket.ts +264 -48
- package/node_modules/openai/src/beta/realtime/ws.ts +138 -25
- package/node_modules/openai/src/client.ts +975 -124
- package/node_modules/openai/src/core/EventEmitter.ts +162 -26
- package/node_modules/openai/src/core/api-promise.ts +0 -2
- package/node_modules/openai/src/core/error.ts +6 -8
- package/node_modules/openai/src/core/pagination.ts +67 -7
- package/node_modules/openai/src/core/resource.ts +0 -2
- package/node_modules/openai/src/core/streaming.ts +490 -100
- package/node_modules/openai/src/core/uploads.ts +6 -1
- package/node_modules/openai/src/helpers/audio.ts +215 -32
- package/node_modules/openai/src/helpers/live.ts +8 -0
- package/node_modules/openai/src/helpers/standard-schema.ts +819 -0
- package/node_modules/openai/src/helpers/zod-v3-strict-schema.ts +520 -0
- package/node_modules/openai/src/helpers/zod.ts +418 -56
- package/node_modules/openai/src/index.ts +4 -5
- package/node_modules/openai/src/internal/assistant-stream-delta.ts +440 -0
- package/node_modules/openai/src/internal/auth/x509-api-origin.ts +27 -0
- package/node_modules/openai/src/internal/auth/x509-credential-options.ts +149 -0
- package/node_modules/openai/src/internal/auth/x509-token-exchange.ts +319 -0
- package/node_modules/openai/src/internal/auth/x509-transport-capability.ts +227 -0
- package/node_modules/openai/src/internal/auth/x509-transport-registry.ts +127 -0
- package/node_modules/openai/src/internal/auth/x509-transport-state-browser.ts +43 -0
- package/node_modules/openai/src/internal/auth/x509-transport-state.cts +43 -0
- package/node_modules/openai/src/internal/auth/x509-workload-identity-auth.ts +972 -0
- package/node_modules/openai/src/internal/bedrock.ts +586 -0
- package/node_modules/openai/src/internal/builtin-types.ts +0 -2
- package/node_modules/openai/src/internal/data-residency.ts +44 -0
- package/node_modules/openai/src/internal/decoders/line.ts +145 -61
- package/node_modules/openai/src/internal/detect-platform.ts +3 -5
- package/node_modules/openai/src/internal/errors.ts +3 -4
- package/node_modules/openai/src/internal/headers.ts +14 -10
- package/node_modules/openai/src/internal/multipart-encoding.ts +53 -0
- package/node_modules/openai/src/internal/parse.ts +47 -19
- package/node_modules/openai/src/internal/provider.ts +92 -0
- package/node_modules/openai/src/internal/qs/formats.ts +1 -1
- package/node_modules/openai/src/internal/qs/stringify.ts +57 -53
- package/node_modules/openai/src/internal/qs/types.ts +1 -1
- package/node_modules/openai/src/internal/qs/utils.ts +443 -54
- package/node_modules/openai/src/internal/realtime-credentials.ts +27 -0
- package/node_modules/openai/src/internal/request-options.ts +52 -3
- package/node_modules/openai/src/internal/responses/canonical-output-text.ts +160 -0
- package/node_modules/openai/src/internal/responses/output-text-index.ts +84 -0
- package/node_modules/openai/src/internal/responses/response-accumulator.ts +1263 -0
- package/node_modules/openai/src/internal/shim-types.ts +3 -4
- package/node_modules/openai/src/internal/shims.ts +0 -2
- package/node_modules/openai/src/internal/stream-utils.ts +37 -5
- package/node_modules/openai/src/internal/to-file.ts +90 -35
- package/node_modules/openai/src/internal/types.ts +21 -29
- package/node_modules/openai/src/internal/uploads.ts +533 -59
- package/node_modules/openai/src/internal/utils/abort.ts +116 -0
- package/node_modules/openai/src/internal/utils/base64.ts +3 -2
- package/node_modules/openai/src/internal/utils/bytes.ts +7 -0
- package/node_modules/openai/src/internal/utils/env.ts +9 -7
- package/node_modules/openai/src/internal/utils/log.ts +77 -14
- package/node_modules/openai/src/internal/utils/path.ts +53 -32
- package/node_modules/openai/src/internal/utils/query.ts +0 -2
- package/node_modules/openai/src/internal/utils/sleep.ts +51 -2
- package/node_modules/openai/src/internal/utils/uuid.ts +0 -2
- package/node_modules/openai/src/internal/utils/values.ts +0 -2
- package/node_modules/openai/src/internal/utils.ts +0 -2
- package/node_modules/openai/src/internal/ws-adapter-browser.ts +83 -24
- package/node_modules/openai/src/internal/ws-adapter-node.ts +105 -26
- package/node_modules/openai/src/internal/ws-adapter.ts +23 -0
- package/node_modules/openai/src/internal/ws.ts +250 -51
- package/node_modules/openai/src/lib/.keep +2 -2
- package/node_modules/openai/src/lib/AbstractChatCompletionRunner.ts +349 -102
- package/node_modules/openai/src/lib/AssistantStream.ts +537 -268
- package/node_modules/openai/src/lib/ChatCompletionRunner.ts +75 -22
- package/node_modules/openai/src/lib/ChatCompletionStream.ts +2016 -268
- package/node_modules/openai/src/lib/ChatCompletionStreamingRunner.ts +115 -17
- package/node_modules/openai/src/lib/EventEmitter.ts +171 -26
- package/node_modules/openai/src/lib/EventStream.ts +1979 -64
- package/node_modules/openai/src/lib/ResponsesParser.ts +153 -56
- package/node_modules/openai/src/lib/RunnableFunction.ts +99 -37
- package/node_modules/openai/src/lib/Util.ts +7 -5
- package/node_modules/openai/src/lib/agents/agent-session-stream.ts +328 -0
- package/node_modules/openai/src/lib/agents/output-text.ts +6 -0
- package/node_modules/openai/src/lib/agents/turn-state.ts +61 -0
- package/node_modules/openai/src/lib/assistant-run-polling.ts +27 -0
- package/node_modules/openai/src/lib/chatCompletionUtils.ts +9 -10
- package/node_modules/openai/src/lib/embeddings.ts +73 -0
- package/node_modules/openai/src/lib/file-processing.ts +37 -0
- package/node_modules/openai/src/lib/jsonschema.ts +77 -34
- package/node_modules/openai/src/lib/live/transcript-grouper.ts +307 -0
- package/node_modules/openai/src/lib/live/transcript-grouping.ts +394 -0
- package/node_modules/openai/src/lib/live/webrtc.ts +23 -0
- package/node_modules/openai/src/lib/parser.ts +247 -69
- package/node_modules/openai/src/lib/polling.ts +145 -0
- package/node_modules/openai/src/lib/realtime/webrtc.ts +20 -0
- package/node_modules/openai/src/lib/responses/EventTypes.ts +9 -60
- package/node_modules/openai/src/lib/responses/ResponseAccumulator.ts +27 -0
- package/node_modules/openai/src/lib/responses/ResponseInputItems.ts +167 -0
- package/node_modules/openai/src/lib/responses/ResponseStream.ts +135 -179
- package/node_modules/openai/src/lib/responses/responses-websocket-lane.ts +388 -0
- package/node_modules/openai/src/lib/responses/responses-websocket-session.ts +317 -0
- package/node_modules/openai/src/lib/transform.ts +2015 -100
- package/node_modules/openai/src/lib/vector-store-polling.ts +46 -0
- package/node_modules/openai/src/lib/vector-store-upload.ts +57 -0
- package/node_modules/openai/src/lib/webhook-signature.ts +164 -0
- package/node_modules/openai/src/lib/webrtc/connection.ts +313 -0
- package/node_modules/openai/src/lib/webrtc/data-channel.ts +151 -0
- package/node_modules/openai/src/lib/webrtc/errors.ts +38 -0
- package/node_modules/openai/src/lib/webrtc/subscriptions.ts +65 -0
- package/node_modules/openai/src/lib/webrtc/types.ts +110 -0
- package/node_modules/openai/src/live/webrtc.ts +10 -0
- package/node_modules/openai/src/providers/bedrock/aws.ts +331 -0
- package/node_modules/openai/src/providers/bedrock.ts +51 -0
- package/node_modules/openai/src/realtime/internal-base.ts +265 -17
- package/node_modules/openai/src/realtime/webrtc.ts +10 -0
- package/node_modules/openai/src/realtime/websocket.ts +259 -54
- package/node_modules/openai/src/realtime/ws.ts +135 -29
- package/node_modules/openai/src/resources/admin/admin.ts +1 -1
- package/node_modules/openai/src/resources/admin/index.ts +1 -1
- package/node_modules/openai/src/resources/admin/organization/admin-api-keys.ts +18 -2
- package/node_modules/openai/src/resources/admin/organization/audit-logs.ts +327 -4
- package/node_modules/openai/src/resources/admin/organization/certificates.ts +1 -1
- package/node_modules/openai/src/resources/admin/organization/data-retention.ts +1 -1
- package/node_modules/openai/src/resources/admin/organization/groups/groups.ts +1 -1
- package/node_modules/openai/src/resources/admin/organization/groups/index.ts +1 -1
- package/node_modules/openai/src/resources/admin/organization/groups/roles.ts +1 -1
- package/node_modules/openai/src/resources/admin/organization/groups/users.ts +1 -1
- package/node_modules/openai/src/resources/admin/organization/groups.ts +1 -1
- package/node_modules/openai/src/resources/admin/organization/index.ts +9 -1
- package/node_modules/openai/src/resources/admin/organization/invites.ts +1 -1
- package/node_modules/openai/src/resources/admin/organization/organization.ts +21 -1
- package/node_modules/openai/src/resources/admin/organization/projects/api-keys.ts +22 -2
- package/node_modules/openai/src/resources/admin/organization/projects/certificates.ts +1 -1
- package/node_modules/openai/src/resources/admin/organization/projects/data-retention.ts +1 -1
- package/node_modules/openai/src/resources/admin/organization/projects/groups/groups.ts +1 -1
- package/node_modules/openai/src/resources/admin/organization/projects/groups/index.ts +1 -1
- package/node_modules/openai/src/resources/admin/organization/projects/groups/roles.ts +1 -1
- package/node_modules/openai/src/resources/admin/organization/projects/groups.ts +1 -1
- package/node_modules/openai/src/resources/admin/organization/projects/hosted-tool-permissions.ts +1 -1
- package/node_modules/openai/src/resources/admin/organization/projects/index.ts +10 -2
- package/node_modules/openai/src/resources/admin/organization/projects/model-permissions.ts +1 -1
- package/node_modules/openai/src/resources/admin/organization/projects/projects.ts +70 -19
- package/node_modules/openai/src/resources/admin/organization/projects/rate-limits.ts +1 -1
- package/node_modules/openai/src/resources/admin/organization/projects/roles.ts +1 -1
- package/node_modules/openai/src/resources/admin/organization/projects/service-accounts/api-keys.ts +91 -0
- package/node_modules/openai/src/resources/admin/organization/projects/service-accounts/index.ts +15 -0
- package/node_modules/openai/src/resources/admin/organization/projects/service-accounts/service-accounts.ts +299 -0
- package/node_modules/openai/src/resources/admin/organization/projects/service-accounts.ts +2 -265
- package/node_modules/openai/src/resources/admin/organization/projects/spend-alerts.ts +33 -1
- package/node_modules/openai/src/resources/admin/organization/projects/spend-limit.ts +156 -0
- package/node_modules/openai/src/resources/admin/organization/projects/users/index.ts +1 -1
- package/node_modules/openai/src/resources/admin/organization/projects/users/roles.ts +1 -1
- package/node_modules/openai/src/resources/admin/organization/projects/users/users.ts +1 -1
- package/node_modules/openai/src/resources/admin/organization/projects/users.ts +1 -1
- package/node_modules/openai/src/resources/admin/organization/projects.ts +1 -1
- package/node_modules/openai/src/resources/admin/organization/roles.ts +1 -1
- package/node_modules/openai/src/resources/admin/organization/spend-alerts.ts +19 -1
- package/node_modules/openai/src/resources/admin/organization/spend-limit.ts +144 -0
- package/node_modules/openai/src/resources/admin/organization/usage.ts +706 -89
- package/node_modules/openai/src/resources/admin/organization/users/index.ts +1 -1
- package/node_modules/openai/src/resources/admin/organization/users/roles.ts +1 -1
- package/node_modules/openai/src/resources/admin/organization/users/users.ts +1 -1
- package/node_modules/openai/src/resources/admin/organization/users.ts +1 -1
- package/node_modules/openai/src/resources/admin/organization.ts +1 -1
- package/node_modules/openai/src/resources/admin.ts +1 -1
- package/node_modules/openai/src/resources/audio/audio.ts +4 -1
- package/node_modules/openai/src/resources/audio/index.ts +2 -1
- package/node_modules/openai/src/resources/audio/speech.ts +4 -3
- package/node_modules/openai/src/resources/audio/transcriptions.ts +51 -13
- package/node_modules/openai/src/resources/audio/translations.ts +5 -3
- package/node_modules/openai/src/resources/audio.ts +1 -1
- package/node_modules/openai/src/resources/batches.ts +10 -9
- package/node_modules/openai/src/resources/beta/agents/agents.ts +4627 -0
- package/node_modules/openai/src/resources/beta/agents/environments/environments.ts +111 -0
- package/node_modules/openai/src/resources/beta/agents/environments/files.ts +169 -0
- package/node_modules/openai/src/resources/beta/agents/environments/index.ts +19 -0
- package/node_modules/openai/src/resources/beta/agents/environments/templates.ts +535 -0
- package/node_modules/openai/src/resources/beta/agents/environments.ts +3 -0
- package/node_modules/openai/src/resources/beta/agents/index.ts +127 -0
- package/node_modules/openai/src/resources/beta/agents/sessions/artifacts.ts +249 -0
- package/node_modules/openai/src/resources/beta/agents/sessions/events.ts +94 -0
- package/node_modules/openai/src/resources/beta/agents/sessions/index.ts +24 -0
- package/node_modules/openai/src/resources/beta/agents/sessions/items.ts +59 -0
- package/node_modules/openai/src/resources/beta/agents/sessions/sessions.ts +403 -0
- package/node_modules/openai/src/resources/beta/agents/sessions/subagents/index.ts +5 -0
- package/node_modules/openai/src/resources/beta/agents/sessions/subagents/items.ts +65 -0
- package/node_modules/openai/src/resources/beta/agents/sessions/subagents/subagents.ts +113 -0
- package/node_modules/openai/src/resources/beta/agents/sessions/subagents/turns/index.ts +4 -0
- package/node_modules/openai/src/resources/beta/agents/sessions/subagents/turns/items.ts +70 -0
- package/node_modules/openai/src/resources/beta/agents/sessions/subagents/turns/turns.ts +111 -0
- package/node_modules/openai/src/resources/beta/agents/sessions/subagents/turns.ts +3 -0
- package/node_modules/openai/src/resources/beta/agents/sessions/subagents.ts +3 -0
- package/node_modules/openai/src/resources/beta/agents/sessions/turns.ts +159 -0
- package/node_modules/openai/src/resources/beta/agents/sessions.ts +3 -0
- package/node_modules/openai/src/resources/beta/agents/vaults/credentials.ts +693 -0
- package/node_modules/openai/src/resources/beta/agents/vaults/index.ts +29 -0
- package/node_modules/openai/src/resources/beta/agents/vaults/vaults.ts +243 -0
- package/node_modules/openai/src/resources/beta/agents/vaults.ts +3 -0
- package/node_modules/openai/src/resources/beta/agents.ts +3 -0
- package/node_modules/openai/src/resources/beta/assistants.ts +154 -172
- package/node_modules/openai/src/resources/beta/beta.ts +578 -3
- package/node_modules/openai/src/resources/beta/chatkit/chatkit.ts +1 -1
- package/node_modules/openai/src/resources/beta/chatkit/index.ts +1 -1
- package/node_modules/openai/src/resources/beta/chatkit/sessions.ts +1 -1
- package/node_modules/openai/src/resources/beta/chatkit/threads.ts +1 -1
- package/node_modules/openai/src/resources/beta/chatkit.ts +1 -1
- package/node_modules/openai/src/resources/beta/index.ts +290 -1
- package/node_modules/openai/src/resources/beta/realtime/index.ts +0 -2
- package/node_modules/openai/src/resources/beta/realtime/realtime.ts +0 -2
- package/node_modules/openai/src/resources/beta/realtime/sessions.ts +0 -2
- package/node_modules/openai/src/resources/beta/realtime/transcription-sessions.ts +0 -2
- package/node_modules/openai/src/resources/beta/realtime.ts +0 -2
- package/node_modules/openai/src/resources/beta/responses/index.ts +189 -0
- package/node_modules/openai/src/resources/beta/responses/input-items.ts +105 -0
- package/node_modules/openai/src/resources/beta/responses/input-tokens.ts +249 -0
- package/node_modules/openai/src/resources/beta/responses/internal-base.ts +136 -0
- package/node_modules/openai/src/resources/beta/responses/responses.ts +15060 -0
- package/node_modules/openai/src/resources/beta/responses/ws-base.ts +729 -0
- package/node_modules/openai/src/resources/beta/responses/ws.ts +62 -0
- package/node_modules/openai/src/resources/beta/responses.ts +3 -0
- package/node_modules/openai/src/resources/beta/threads/index.ts +1 -1
- package/node_modules/openai/src/resources/beta/threads/messages.ts +30 -24
- package/node_modules/openai/src/resources/beta/threads/runs/index.ts +2 -2
- package/node_modules/openai/src/resources/beta/threads/runs/runs.ts +47 -93
- package/node_modules/openai/src/resources/beta/threads/runs/steps.ts +15 -16
- package/node_modules/openai/src/resources/beta/threads/runs.ts +1 -1
- package/node_modules/openai/src/resources/beta/threads/threads.ts +54 -51
- package/node_modules/openai/src/resources/beta/threads.ts +1 -1
- package/node_modules/openai/src/resources/beta.ts +1 -1
- package/node_modules/openai/src/resources/chat/chat.ts +1 -1
- package/node_modules/openai/src/resources/chat/completions/completions.ts +762 -108
- package/node_modules/openai/src/resources/chat/completions/index.ts +1 -1
- package/node_modules/openai/src/resources/chat/completions/messages.ts +1 -1
- package/node_modules/openai/src/resources/chat/completions.ts +1 -1
- package/node_modules/openai/src/resources/chat/index.ts +1 -1
- package/node_modules/openai/src/resources/chat.ts +1 -1
- package/node_modules/openai/src/resources/completions.ts +36 -15
- package/node_modules/openai/src/resources/containers/containers.ts +1 -1
- package/node_modules/openai/src/resources/containers/files/content.ts +1 -1
- package/node_modules/openai/src/resources/containers/files/files.ts +1 -1
- package/node_modules/openai/src/resources/containers/files/index.ts +1 -1
- package/node_modules/openai/src/resources/containers/files.ts +1 -1
- package/node_modules/openai/src/resources/containers/index.ts +1 -1
- package/node_modules/openai/src/resources/containers.ts +1 -1
- package/node_modules/openai/src/resources/content-provenance-checks.ts +132 -0
- package/node_modules/openai/src/resources/conversations/conversations.ts +23 -2
- package/node_modules/openai/src/resources/conversations/index.ts +1 -1
- package/node_modules/openai/src/resources/conversations/items.ts +93 -5
- package/node_modules/openai/src/resources/conversations.ts +1 -1
- package/node_modules/openai/src/resources/embeddings.ts +22 -49
- package/node_modules/openai/src/resources/evals/evals.ts +6 -6
- package/node_modules/openai/src/resources/evals/index.ts +1 -1
- package/node_modules/openai/src/resources/evals/runs/index.ts +1 -1
- package/node_modules/openai/src/resources/evals/runs/output-items.ts +1 -1
- package/node_modules/openai/src/resources/evals/runs/runs.ts +119 -191
- package/node_modules/openai/src/resources/evals/runs.ts +1 -1
- package/node_modules/openai/src/resources/evals.ts +1 -1
- package/node_modules/openai/src/resources/files.ts +9 -26
- package/node_modules/openai/src/resources/fine-tuning/alpha/alpha.ts +1 -1
- package/node_modules/openai/src/resources/fine-tuning/alpha/graders.ts +6 -5
- package/node_modules/openai/src/resources/fine-tuning/alpha/index.ts +1 -1
- package/node_modules/openai/src/resources/fine-tuning/alpha.ts +1 -1
- package/node_modules/openai/src/resources/fine-tuning/checkpoints/checkpoints.ts +1 -1
- package/node_modules/openai/src/resources/fine-tuning/checkpoints/index.ts +1 -1
- package/node_modules/openai/src/resources/fine-tuning/checkpoints/permissions.ts +9 -5
- package/node_modules/openai/src/resources/fine-tuning/checkpoints.ts +1 -1
- package/node_modules/openai/src/resources/fine-tuning/fine-tuning.ts +1 -1
- package/node_modules/openai/src/resources/fine-tuning/index.ts +1 -1
- package/node_modules/openai/src/resources/fine-tuning/jobs/checkpoints.ts +1 -1
- package/node_modules/openai/src/resources/fine-tuning/jobs/index.ts +1 -1
- package/node_modules/openai/src/resources/fine-tuning/jobs/jobs.ts +14 -13
- package/node_modules/openai/src/resources/fine-tuning/jobs.ts +1 -1
- package/node_modules/openai/src/resources/fine-tuning/methods.ts +1 -1
- package/node_modules/openai/src/resources/fine-tuning.ts +1 -1
- package/node_modules/openai/src/resources/graders/grader-models.ts +7 -14
- package/node_modules/openai/src/resources/graders/graders.ts +1 -1
- package/node_modules/openai/src/resources/graders/index.ts +1 -1
- package/node_modules/openai/src/resources/graders.ts +1 -1
- package/node_modules/openai/src/resources/images.ts +100 -85
- package/node_modules/openai/src/resources/index.ts +9 -2
- package/node_modules/openai/src/resources/live/api.md +87 -0
- package/node_modules/openai/src/resources/live/forks/forks.ts +169 -0
- package/node_modules/openai/src/resources/live/forks/index.ts +5 -0
- package/node_modules/openai/src/resources/live/forks/internal-base.ts +126 -0
- package/node_modules/openai/src/resources/live/forks/ws-base.ts +700 -0
- package/node_modules/openai/src/resources/live/forks/ws.ts +47 -0
- package/node_modules/openai/src/resources/live/forks.ts +3 -0
- package/node_modules/openai/src/resources/live/index.ts +20 -0
- package/node_modules/openai/src/resources/live/internal-base.ts +123 -0
- package/node_modules/openai/src/resources/live/live.ts +2296 -0
- package/node_modules/openai/src/resources/live/sessions.ts +343 -0
- package/node_modules/openai/src/resources/live/sideband/index.ts +10 -0
- package/node_modules/openai/src/resources/live/sideband/internal-base.ts +126 -0
- package/node_modules/openai/src/resources/live/sideband/sideband.ts +58 -0
- package/node_modules/openai/src/resources/live/sideband/ws-base.ts +710 -0
- package/node_modules/openai/src/resources/live/sideband/ws.ts +47 -0
- package/node_modules/openai/src/resources/live/sideband.ts +3 -0
- package/node_modules/openai/src/resources/live/ws-base.ts +696 -0
- package/node_modules/openai/src/resources/live/ws.ts +43 -0
- package/node_modules/openai/src/resources/live.ts +3 -0
- package/node_modules/openai/src/resources/models.ts +6 -1
- package/node_modules/openai/src/resources/moderations.ts +6 -5
- package/node_modules/openai/src/resources/realtime/api.md +1 -0
- package/node_modules/openai/src/resources/realtime/calls.ts +50 -2
- package/node_modules/openai/src/resources/realtime/client-secrets.ts +27 -10
- package/node_modules/openai/src/resources/realtime/index.ts +8 -2
- package/node_modules/openai/src/resources/realtime/realtime.ts +93 -35
- package/node_modules/openai/src/resources/realtime.ts +1 -1
- package/node_modules/openai/src/resources/responses/api.md +20 -0
- package/node_modules/openai/src/resources/responses/index.ts +1 -1
- package/node_modules/openai/src/resources/responses/input-items.ts +1 -1
- package/node_modules/openai/src/resources/responses/input-tokens.ts +26 -16
- package/node_modules/openai/src/resources/responses/internal-base.ts +33 -14
- package/node_modules/openai/src/resources/responses/responses.ts +4150 -792
- package/node_modules/openai/src/resources/responses/ws-base.ts +147 -31
- package/node_modules/openai/src/resources/responses/ws.ts +33 -9
- package/node_modules/openai/src/resources/responses.ts +1 -1
- package/node_modules/openai/src/resources/safety/alerts.ts +51 -0
- package/node_modules/openai/src/resources/safety/index.ts +4 -0
- package/node_modules/openai/src/resources/safety/safety.ts +15 -0
- package/node_modules/openai/src/resources/safety.ts +3 -0
- package/node_modules/openai/src/resources/shared.ts +98 -41
- package/node_modules/openai/src/resources/skills/content.ts +1 -1
- package/node_modules/openai/src/resources/skills/index.ts +1 -1
- package/node_modules/openai/src/resources/skills/skills.ts +4 -2
- package/node_modules/openai/src/resources/skills/versions/content.ts +1 -1
- package/node_modules/openai/src/resources/skills/versions/index.ts +1 -1
- package/node_modules/openai/src/resources/skills/versions/versions.ts +4 -2
- package/node_modules/openai/src/resources/skills/versions.ts +1 -1
- package/node_modules/openai/src/resources/skills.ts +1 -1
- package/node_modules/openai/src/resources/uploads/index.ts +1 -1
- package/node_modules/openai/src/resources/uploads/parts.ts +6 -5
- package/node_modules/openai/src/resources/uploads/uploads.ts +12 -12
- package/node_modules/openai/src/resources/uploads.ts +1 -1
- package/node_modules/openai/src/resources/vector-stores/file-batches.ts +24 -89
- package/node_modules/openai/src/resources/vector-stores/files.ts +12 -52
- package/node_modules/openai/src/resources/vector-stores/index.ts +1 -1
- package/node_modules/openai/src/resources/vector-stores/vector-stores.ts +4 -4
- package/node_modules/openai/src/resources/vector-stores.ts +1 -1
- package/node_modules/openai/src/resources/videos.ts +63 -2
- package/node_modules/openai/src/resources/webhooks/api.md +40 -17
- package/node_modules/openai/src/resources/webhooks/event-types.ts +21 -0
- package/node_modules/openai/src/resources/webhooks/index.ts +2 -1
- package/node_modules/openai/src/resources/webhooks/webhooks.ts +694 -69
- package/node_modules/openai/src/resources/webhooks.ts +1 -1
- package/node_modules/openai/src/tsconfig.json +6 -3
- package/node_modules/openai/src/version.ts +2 -1
- package/node_modules/openai/version.js +2 -1
- package/node_modules/openai/version.mjs +2 -1
- package/package.json +5 -5
- package/node_modules/@earendil-works/pi-ai/dist/image-models.generated.js +0 -832
- package/node_modules/@earendil-works/pi-ai/dist/images-models.js +0 -143
- package/node_modules/@earendil-works/pi-ai/dist/providers/openrouter-images.js +0 -22
- /package/node_modules/@aws-sdk/{credential-provider-sso → client-bedrock-runtime}/node_modules/@aws-sdk/token-providers/LICENSE +0 -0
- /package/node_modules/@aws-sdk/{credential-provider-sso → client-bedrock-runtime}/node_modules/@aws-sdk/token-providers/README.md +0 -0
- /package/node_modules/@aws-sdk/{credential-provider-sso → client-bedrock-runtime}/node_modules/@aws-sdk/token-providers/dist-cjs/index.js +0 -0
- /package/node_modules/@aws-sdk/{credential-provider-sso → client-bedrock-runtime}/node_modules/@aws-sdk/token-providers/dist-es/constants.js +0 -0
- /package/node_modules/@aws-sdk/{credential-provider-sso → client-bedrock-runtime}/node_modules/@aws-sdk/token-providers/dist-es/fromEnvSigningName.js +0 -0
- /package/node_modules/@aws-sdk/{credential-provider-sso → client-bedrock-runtime}/node_modules/@aws-sdk/token-providers/dist-es/fromSso.js +0 -0
- /package/node_modules/@aws-sdk/{credential-provider-sso → client-bedrock-runtime}/node_modules/@aws-sdk/token-providers/dist-es/fromStatic.js +0 -0
- /package/node_modules/@aws-sdk/{credential-provider-sso → client-bedrock-runtime}/node_modules/@aws-sdk/token-providers/dist-es/getNewSsoOidcToken.js +0 -0
- /package/node_modules/@aws-sdk/{credential-provider-sso → client-bedrock-runtime}/node_modules/@aws-sdk/token-providers/dist-es/getSsoOidcClient.js +0 -0
- /package/node_modules/@aws-sdk/{credential-provider-sso → client-bedrock-runtime}/node_modules/@aws-sdk/token-providers/dist-es/index.js +0 -0
- /package/node_modules/@aws-sdk/{credential-provider-sso → client-bedrock-runtime}/node_modules/@aws-sdk/token-providers/dist-es/nodeProvider.js +0 -0
- /package/node_modules/@aws-sdk/{credential-provider-sso → client-bedrock-runtime}/node_modules/@aws-sdk/token-providers/dist-es/validateTokenExpiry.js +0 -0
- /package/node_modules/@aws-sdk/{credential-provider-sso → client-bedrock-runtime}/node_modules/@aws-sdk/token-providers/dist-es/validateTokenKey.js +0 -0
- /package/node_modules/@aws-sdk/{credential-provider-sso → client-bedrock-runtime}/node_modules/@aws-sdk/token-providers/dist-es/writeSSOTokenToFile.js +0 -0
- /package/node_modules/@earendil-works/pi-ai/dist/{auth/oauth → utils}/oauth-page.js +0 -0
|
@@ -1,145 +1,1334 @@
|
|
|
1
|
-
import { partialParse } from '../_vendor/partial-json-parser/parser';
|
|
1
|
+
import { MalformedJSON, partialParse } from '../_vendor/partial-json-parser/parser';
|
|
2
|
+
import { APIError, ContentFilterFinishReasonError, LengthFinishReasonError, OpenAIError } from '../error';
|
|
3
|
+
import type OpenAI from '../index';
|
|
4
|
+
import { observeJSONRequestBody, type RequestOptions } from '../internal/request-options';
|
|
5
|
+
import type { ReadableStream } from '../internal/shim-types';
|
|
6
|
+
import { uuid4 } from '../internal/utils/uuid';
|
|
7
|
+
import { hasOwn } from '../internal/utils/values';
|
|
2
8
|
import {
|
|
3
|
-
APIUserAbortError,
|
|
4
|
-
ContentFilterFinishReasonError,
|
|
5
|
-
LengthFinishReasonError,
|
|
6
|
-
OpenAIError,
|
|
7
|
-
} from '../error';
|
|
8
|
-
import OpenAI from '../index';
|
|
9
|
-
import { RequestOptions } from '../internal/request-options';
|
|
10
|
-
import { type ReadableStream } from '../internal/shim-types';
|
|
11
|
-
import {
|
|
12
|
-
AutoParseableResponseFormat,
|
|
13
9
|
hasAutoParseableInput,
|
|
14
|
-
isAutoParsableResponseFormat,
|
|
15
10
|
isAutoParsableTool,
|
|
16
11
|
isChatCompletionFunctionTool,
|
|
12
|
+
isParseableResponseFormat,
|
|
17
13
|
maybeParseChatCompletion,
|
|
14
|
+
parseResponseFormatContent,
|
|
18
15
|
shouldParseToolCall,
|
|
19
16
|
} from '../lib/parser';
|
|
20
|
-
import { ChatCompletionFunctionTool, ParsedChatCompletion } from '../resources/chat/completions';
|
|
21
|
-
import {
|
|
17
|
+
import type { ChatCompletionFunctionTool, ParsedChatCompletion } from '../resources/chat/completions';
|
|
18
|
+
import type {
|
|
19
|
+
ChatCompletionAudio,
|
|
20
|
+
ChatCompletion,
|
|
21
|
+
ChatCompletionChunk,
|
|
22
|
+
ChatCompletionCreateParams,
|
|
23
|
+
ChatCompletionCreateParamsBase,
|
|
24
|
+
ChatCompletionCreateParamsStreaming,
|
|
25
|
+
ChatCompletionMessageParam,
|
|
26
|
+
ChatCompletionMessageToolCall,
|
|
27
|
+
ChatCompletionRole,
|
|
22
28
|
ChatCompletionTokenLogprob,
|
|
23
|
-
type ChatCompletion,
|
|
24
|
-
type ChatCompletionChunk,
|
|
25
|
-
type ChatCompletionCreateParams,
|
|
26
|
-
type ChatCompletionCreateParamsBase,
|
|
27
|
-
type ChatCompletionCreateParamsStreaming,
|
|
28
|
-
type ChatCompletionRole,
|
|
29
29
|
} from '../resources/chat/completions/completions';
|
|
30
30
|
import { Stream } from '../streaming';
|
|
31
|
-
import {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
31
|
+
import { AbstractChatCompletionRunner } from './AbstractChatCompletionRunner';
|
|
32
|
+
import type { AbstractChatCompletionRunnerEvents, RunnerOptions } from './AbstractChatCompletionRunner';
|
|
33
|
+
import type {
|
|
34
|
+
ChatCompletionRunner,
|
|
35
|
+
ChatCompletionToolRunnerParamsWithContext,
|
|
36
|
+
ChatCompletionToolRunnerParamsWithoutContext,
|
|
37
|
+
} from './ChatCompletionRunner';
|
|
38
|
+
import type {
|
|
39
|
+
ChatCompletionStreamingRunner,
|
|
40
|
+
ChatCompletionStreamingToolRunnerParamsWithContext,
|
|
41
|
+
ChatCompletionStreamingToolRunnerParamsWithoutContext,
|
|
42
|
+
} from './ChatCompletionStreamingRunner';
|
|
43
|
+
import type { BaseFunctionsArgs } from './RunnableFunction';
|
|
44
|
+
|
|
45
|
+
// oxlint-disable-next-line anti-slop/no-unknown-returns -- Partial JSON may have any shape; callers validate or parse it against their response schema.
|
|
46
|
+
function parseStructuredStreamingJSON(content: string): unknown {
|
|
47
|
+
try {
|
|
48
|
+
return partialParse(content);
|
|
49
|
+
} catch (error) {
|
|
50
|
+
if (error instanceof MalformedJSON || error instanceof SyntaxError) {
|
|
51
|
+
return parseResponseFormatContent({ type: 'json_schema', $parseRaw: undefined }, content);
|
|
52
|
+
}
|
|
35
53
|
|
|
54
|
+
throw error;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** An incremental assistant-text event and its accumulated state. */
|
|
36
59
|
export interface ContentDeltaEvent {
|
|
60
|
+
/** The new text received in this chunk. */
|
|
37
61
|
delta: string;
|
|
62
|
+
/** All assistant text received for this choice, including `delta`. */
|
|
38
63
|
snapshot: string;
|
|
64
|
+
/** The partially parsed structured output when an auto-parseable response format is supplied. */
|
|
39
65
|
parsed: unknown | null;
|
|
40
66
|
}
|
|
41
67
|
|
|
68
|
+
/** The completed assistant-text content and its fully parsed structured value. */
|
|
42
69
|
export interface ContentDoneEvent<ParsedT = null> {
|
|
70
|
+
/** The complete assistant text for the finished choice. */
|
|
43
71
|
content: string;
|
|
72
|
+
/** The fully parsed structured output, or `null` when no parser was supplied. */
|
|
44
73
|
parsed: ParsedT | null;
|
|
45
74
|
}
|
|
46
75
|
|
|
76
|
+
/** An incremental refusal-text event and its accumulated state. */
|
|
47
77
|
export interface RefusalDeltaEvent {
|
|
78
|
+
/** The new refusal text received in this chunk. */
|
|
48
79
|
delta: string;
|
|
80
|
+
/** All refusal text received for this choice, including `delta`. */
|
|
49
81
|
snapshot: string;
|
|
50
82
|
}
|
|
51
83
|
|
|
84
|
+
/** The complete refusal text emitted when the refusal finishes. */
|
|
52
85
|
export interface RefusalDoneEvent {
|
|
86
|
+
/** The model's complete refusal message. */
|
|
53
87
|
refusal: string;
|
|
54
88
|
}
|
|
55
89
|
|
|
90
|
+
/** An incremental function-tool argument event and its accumulated JSON state. */
|
|
56
91
|
export interface FunctionToolCallArgumentsDeltaEvent {
|
|
92
|
+
/** The name of the function being called. */
|
|
57
93
|
name: string;
|
|
58
94
|
|
|
95
|
+
/** The position of this tool call within the assistant message. */
|
|
59
96
|
index: number;
|
|
60
97
|
|
|
98
|
+
/** The complete argument JSON received so far, including `arguments_delta`. */
|
|
61
99
|
arguments: string;
|
|
62
100
|
|
|
101
|
+
/** The partially parsed arguments when the matching tool supports parsing. */
|
|
63
102
|
parsed_arguments: unknown;
|
|
64
103
|
|
|
104
|
+
/** The new argument JSON fragment received in this chunk. */
|
|
65
105
|
arguments_delta: string;
|
|
66
106
|
}
|
|
67
107
|
|
|
108
|
+
/** The final raw and parsed arguments for a completed function-tool call. */
|
|
68
109
|
export interface FunctionToolCallArgumentsDoneEvent {
|
|
110
|
+
/** The name of the function being called. */
|
|
69
111
|
name: string;
|
|
70
112
|
|
|
113
|
+
/** The position of this tool call within the assistant message. */
|
|
71
114
|
index: number;
|
|
72
115
|
|
|
116
|
+
/** The complete JSON argument string produced for the tool call. */
|
|
73
117
|
arguments: string;
|
|
74
118
|
|
|
119
|
+
/** The fully parsed arguments when the matching tool supports parsing. */
|
|
75
120
|
parsed_arguments: unknown;
|
|
76
121
|
}
|
|
77
122
|
|
|
123
|
+
/** Newly received assistant-content token probabilities and their accumulated snapshot. */
|
|
78
124
|
export interface LogProbsContentDeltaEvent {
|
|
79
|
-
|
|
80
|
-
|
|
125
|
+
/** Token probabilities received in the current chunk. */
|
|
126
|
+
content: ChatCompletionTokenLogprob[];
|
|
127
|
+
/** All assistant-content token probabilities received for this choice. */
|
|
128
|
+
snapshot: ChatCompletionTokenLogprob[];
|
|
81
129
|
}
|
|
82
130
|
|
|
131
|
+
/** The complete assistant-content token probabilities for a finished choice. */
|
|
83
132
|
export interface LogProbsContentDoneEvent {
|
|
84
|
-
content
|
|
133
|
+
/** Every assistant-content token probability produced for this choice. */
|
|
134
|
+
content: ChatCompletionTokenLogprob[];
|
|
85
135
|
}
|
|
86
136
|
|
|
137
|
+
/** Newly received refusal-token probabilities and their accumulated snapshot. */
|
|
87
138
|
export interface LogProbsRefusalDeltaEvent {
|
|
88
|
-
|
|
89
|
-
|
|
139
|
+
/** Refusal-token probabilities received in the current chunk. */
|
|
140
|
+
refusal: ChatCompletionTokenLogprob[];
|
|
141
|
+
/** All refusal-token probabilities received for this choice. */
|
|
142
|
+
snapshot: ChatCompletionTokenLogprob[];
|
|
90
143
|
}
|
|
91
144
|
|
|
145
|
+
/** The complete refusal-token probabilities for a finished choice. */
|
|
92
146
|
export interface LogProbsRefusalDoneEvent {
|
|
93
|
-
refusal
|
|
147
|
+
/** Every refusal-token probability produced for this choice. */
|
|
148
|
+
refusal: ChatCompletionTokenLogprob[];
|
|
94
149
|
}
|
|
95
150
|
|
|
151
|
+
/** Event listeners supported by a streamed Chat Completions helper. */
|
|
96
152
|
export interface ChatCompletionStreamEvents<ParsedT = null> extends AbstractChatCompletionRunnerEvents {
|
|
153
|
+
/** Called with each new text fragment and the complete text accumulated so far. */
|
|
97
154
|
content: (contentDelta: string, contentSnapshot: string) => void;
|
|
155
|
+
/** Called with each raw API chunk and its accumulated chat-completion snapshot. */
|
|
98
156
|
chunk: (chunk: ChatCompletionChunk, snapshot: ChatCompletionSnapshot) => void;
|
|
99
157
|
|
|
158
|
+
/** Called when assistant text arrives, including any partially parsed output. */
|
|
100
159
|
'content.delta': (props: ContentDeltaEvent) => void;
|
|
160
|
+
/** Called once the assistant text is complete and can be fully parsed. */
|
|
101
161
|
'content.done': (props: ContentDoneEvent<ParsedT>) => void;
|
|
102
162
|
|
|
163
|
+
/** Called when another fragment of a model refusal arrives. */
|
|
103
164
|
'refusal.delta': (props: RefusalDeltaEvent) => void;
|
|
165
|
+
/** Called once the model's complete refusal is available. */
|
|
104
166
|
'refusal.done': (props: RefusalDoneEvent) => void;
|
|
105
167
|
|
|
168
|
+
/** Called when another JSON argument fragment arrives for a function tool. */
|
|
106
169
|
'tool_calls.function.arguments.delta': (props: FunctionToolCallArgumentsDeltaEvent) => void;
|
|
170
|
+
/** Called once a function tool's complete arguments are available. */
|
|
107
171
|
'tool_calls.function.arguments.done': (props: FunctionToolCallArgumentsDoneEvent) => void;
|
|
108
172
|
|
|
173
|
+
/** Called when assistant-content token probabilities arrive. */
|
|
109
174
|
'logprobs.content.delta': (props: LogProbsContentDeltaEvent) => void;
|
|
175
|
+
/** Called once all assistant-content token probabilities are available. */
|
|
110
176
|
'logprobs.content.done': (props: LogProbsContentDoneEvent) => void;
|
|
111
177
|
|
|
178
|
+
/** Called when refusal-token probabilities arrive. */
|
|
112
179
|
'logprobs.refusal.delta': (props: LogProbsRefusalDeltaEvent) => void;
|
|
180
|
+
/** Called once all refusal-token probabilities are available. */
|
|
113
181
|
'logprobs.refusal.done': (props: LogProbsRefusalDoneEvent) => void;
|
|
114
182
|
}
|
|
115
183
|
|
|
184
|
+
/** Chat completion request parameters accepted by the streaming convenience helper. */
|
|
116
185
|
export type ChatCompletionStreamParams = Omit<ChatCompletionCreateParamsBase, 'stream'> & {
|
|
186
|
+
/** Streaming is always enabled by the helper and may be specified explicitly. */
|
|
117
187
|
stream?: true;
|
|
118
188
|
};
|
|
119
189
|
|
|
190
|
+
/** A conversation message embedded in a serialized chat completion stream. */
|
|
191
|
+
type ChatCompletionReadableStreamMessage = {
|
|
192
|
+
/** Identifies this readable-stream item as a serialized conversation message. */
|
|
193
|
+
type: 'message';
|
|
194
|
+
/** The conversation message to restore while replaying the serialized stream. */
|
|
195
|
+
message: ChatCompletionMessageParam;
|
|
196
|
+
/** Tool-call identifiers to restore on the preceding assistant completion. */
|
|
197
|
+
tool_call_ids?: string[];
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
// Keep message records readable as empty chunks by older SDKs. Their finalizer
|
|
201
|
+
// overwrites `object`, so the encoded payload does not leak into completions.
|
|
202
|
+
const CHAT_COMPLETION_READABLE_STREAM_MESSAGE_PREFIX = 'chat.completion.chunk.message:';
|
|
203
|
+
|
|
204
|
+
/** A serialized conversation message disguised as a backwards-compatible empty completion chunk. */
|
|
205
|
+
type ChatCompletionReadableStreamMessageChunk = Pick<ChatCompletionChunk, 'id' | 'created' | 'model'> & {
|
|
206
|
+
/** Empty choices keep the encoded message compatible with older completion-stream readers. */
|
|
207
|
+
choices: [];
|
|
208
|
+
/** Reserved object prefix followed by the JSON-encoded conversation-message payload. */
|
|
209
|
+
object: `${typeof CHAT_COMPLETION_READABLE_STREAM_MESSAGE_PREFIX}${string}`;
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
/** A raw completion chunk or serialized message preserved in a transportable stream. */
|
|
213
|
+
export type ChatCompletionReadableStreamItem =
|
|
214
|
+
| ChatCompletionChunk
|
|
215
|
+
| ChatCompletionReadableStreamMessage
|
|
216
|
+
| ChatCompletionReadableStreamMessageChunk;
|
|
217
|
+
|
|
218
|
+
/** Encodes a tool-result message as a backwards-compatible, empty completion chunk. */
|
|
219
|
+
export function makeChatCompletionReadableStreamMessageChunk(
|
|
220
|
+
chunk: ChatCompletionChunk,
|
|
221
|
+
message: ChatCompletionMessageParam,
|
|
222
|
+
toolCallIds?: string[],
|
|
223
|
+
): ChatCompletionReadableStreamMessageChunk {
|
|
224
|
+
const payload: ChatCompletionReadableStreamMessage = {
|
|
225
|
+
type: 'message',
|
|
226
|
+
message,
|
|
227
|
+
// Spread creates an own data property without invoking inherited setters or changing the object prototype.
|
|
228
|
+
...(toolCallIds ? { tool_call_ids: toolCallIds } : {}),
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
return {
|
|
232
|
+
id: chunk.id,
|
|
233
|
+
choices: [],
|
|
234
|
+
created: chunk.created,
|
|
235
|
+
model: chunk.model,
|
|
236
|
+
object: `${CHAT_COMPLETION_READABLE_STREAM_MESSAGE_PREFIX}${JSON.stringify(payload)}`,
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function isChatCompletionReadableStreamMessage(
|
|
241
|
+
item: ChatCompletionReadableStreamItem,
|
|
242
|
+
): item is ChatCompletionReadableStreamMessage | ChatCompletionReadableStreamMessageChunk {
|
|
243
|
+
return (
|
|
244
|
+
('type' in item && item.type === 'message' && 'message' in item) ||
|
|
245
|
+
('object' in item &&
|
|
246
|
+
typeof item.object === 'string' &&
|
|
247
|
+
item.object.startsWith(CHAT_COMPLETION_READABLE_STREAM_MESSAGE_PREFIX))
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function getChatCompletionReadableStreamMessage(
|
|
252
|
+
item: ChatCompletionReadableStreamMessage | ChatCompletionReadableStreamMessageChunk,
|
|
253
|
+
): ChatCompletionReadableStreamMessage {
|
|
254
|
+
if ('type' in item) {
|
|
255
|
+
return item;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// SAFETY: This decoder reads the SDK's tagged readable-stream envelope; JSON parsing restores its serialized message fields for the stream accumulator.
|
|
259
|
+
return JSON.parse(
|
|
260
|
+
item.object.slice(CHAT_COMPLETION_READABLE_STREAM_MESSAGE_PREFIX.length),
|
|
261
|
+
) as ChatCompletionReadableStreamMessage;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* A tool call snapshot while it is still being accumulated from stream chunks.
|
|
266
|
+
*
|
|
267
|
+
* Every property is optional because the deltas that make up a tool call arrive
|
|
268
|
+
* across chunks; once they have all been accumulated the entry satisfies
|
|
269
|
+
* {@link ChatCompletionSnapshot.Choice.Message.ToolCall}.
|
|
270
|
+
*/
|
|
271
|
+
type PartialToolCallSnapshot = {
|
|
272
|
+
id?: string;
|
|
273
|
+
type?: ChatCompletionSnapshot.Choice.Message.ToolCall['type'];
|
|
274
|
+
function?: ChatCompletionSnapshot.Choice.Message.ToolCall.Function;
|
|
275
|
+
custom?: ChatCompletionSnapshot.Choice.Message.ToolCall.CustomToolCall.Custom;
|
|
276
|
+
};
|
|
277
|
+
|
|
120
278
|
interface ChoiceEventState {
|
|
121
279
|
content_done: boolean;
|
|
280
|
+
content_parse_state: PartialJSONParseState | undefined;
|
|
122
281
|
refusal_done: boolean;
|
|
123
282
|
logprobs_content_done: boolean;
|
|
124
283
|
logprobs_refusal_done: boolean;
|
|
125
284
|
current_tool_call_index: number | null;
|
|
126
285
|
done_tool_calls: Set<number>;
|
|
286
|
+
tool_call_parse_states: Map<number, PartialJSONParseState>;
|
|
287
|
+
tool_call_identities: Map<number, ToolCallIdentity>;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
interface CapturedToolCallDeltaFrame {
|
|
291
|
+
readonly index: number;
|
|
292
|
+
readonly arguments_delta: string;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
interface CapturedChoiceToolCallFrames {
|
|
296
|
+
readonly index: number;
|
|
297
|
+
readonly tool_calls: readonly CapturedToolCallDeltaFrame[];
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
interface ValidatedChoiceSnapshot {
|
|
301
|
+
message: ChatCompletionSnapshot.Choice.Message;
|
|
302
|
+
content: string | null | undefined;
|
|
303
|
+
refusal: string | null | undefined;
|
|
304
|
+
toolCallCollection: ChatCompletionSnapshot.Choice.Message.ToolCall[] | undefined;
|
|
305
|
+
toolCalls: ReadonlyMap<number, ValidatedToolCallSnapshot>;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
interface ValidatedToolCallSnapshot {
|
|
309
|
+
tool: ChatCompletionSnapshot.Choice.Message.ToolCall;
|
|
310
|
+
function: ChatCompletionSnapshot.Choice.Message.ToolCall.Function;
|
|
311
|
+
type: 'function';
|
|
312
|
+
name: string;
|
|
313
|
+
arguments: string;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
interface ToolCallIdentity {
|
|
317
|
+
type: 'function';
|
|
318
|
+
name: string;
|
|
319
|
+
parseable: boolean;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
interface PartialJSONParseState {
|
|
323
|
+
bytes: number;
|
|
324
|
+
depth: number;
|
|
325
|
+
fragments: number;
|
|
326
|
+
work: number;
|
|
327
|
+
escaped: boolean;
|
|
328
|
+
has_non_whitespace: boolean;
|
|
329
|
+
in_string: boolean;
|
|
330
|
+
last_parsed_bytes: number;
|
|
331
|
+
pending_high_surrogate: boolean;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
interface PartialJSONParseBudget {
|
|
335
|
+
bytes: number;
|
|
336
|
+
fragments: number;
|
|
337
|
+
work: number;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
// The Chat Completions schema limits n to 128. Replayed streams do not retain
|
|
341
|
+
// request parameters, so apply that choice limit independently and use the same
|
|
342
|
+
// conservative ceiling to prevent sparse tool-call arrays from growing unbounded.
|
|
343
|
+
const MAX_STREAM_CHOICES = 128;
|
|
344
|
+
const MAX_STREAM_TOOL_CALLS = 128;
|
|
345
|
+
const MAX_PARTIAL_JSON_BYTES = 16 * 1024 * 1024;
|
|
346
|
+
const MAX_PARTIAL_JSON_FRAGMENTS = 65_536;
|
|
347
|
+
const MAX_PARTIAL_JSON_DEPTH = 128;
|
|
348
|
+
const MAX_PARTIAL_JSON_PARSE_WORK = 64 * 1024 * 1024;
|
|
349
|
+
const EAGER_PARTIAL_JSON_BYTES = 1024;
|
|
350
|
+
|
|
351
|
+
function createPartialJSONParseState(): PartialJSONParseState {
|
|
352
|
+
return {
|
|
353
|
+
bytes: 0,
|
|
354
|
+
depth: 0,
|
|
355
|
+
fragments: 0,
|
|
356
|
+
work: 0,
|
|
357
|
+
escaped: false,
|
|
358
|
+
has_non_whitespace: false,
|
|
359
|
+
in_string: false,
|
|
360
|
+
last_parsed_bytes: 0,
|
|
361
|
+
pending_high_surrogate: false,
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
function recordPartialJSONFragment(
|
|
366
|
+
state: PartialJSONParseState,
|
|
367
|
+
budget: PartialJSONParseBudget,
|
|
368
|
+
fragment: string,
|
|
369
|
+
validationWorkBudget?: PartialJSONParseBudget,
|
|
370
|
+
): boolean {
|
|
371
|
+
if (budget.fragments >= MAX_PARTIAL_JSON_FRAGMENTS) {
|
|
372
|
+
throw new OpenAIError('Chat completion stream exceeded its structured JSON fragment limit');
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
let bytes = 0;
|
|
376
|
+
let { depth, escaped, has_non_whitespace: hasNonWhitespace, in_string: inString } = state;
|
|
377
|
+
let completed = false;
|
|
378
|
+
let firstCharacter = true;
|
|
379
|
+
|
|
380
|
+
for (const character of fragment) {
|
|
381
|
+
const previousBytes = bytes;
|
|
382
|
+
const codePoint = character.codePointAt(0)!;
|
|
383
|
+
if (firstCharacter && state.pending_high_surrogate && codePoint >= 0xdc_00 && codePoint <= 0xdf_ff) {
|
|
384
|
+
bytes += 1;
|
|
385
|
+
} else if (codePoint <= 0x7f) {
|
|
386
|
+
bytes += 1;
|
|
387
|
+
} else if (codePoint <= 0x7_ff) {
|
|
388
|
+
bytes += 2;
|
|
389
|
+
} else if (codePoint <= 0xff_ff) {
|
|
390
|
+
bytes += 3;
|
|
391
|
+
} else {
|
|
392
|
+
bytes += 4;
|
|
393
|
+
}
|
|
394
|
+
firstCharacter = false;
|
|
395
|
+
if (budget.bytes + bytes > MAX_PARTIAL_JSON_BYTES) {
|
|
396
|
+
throw new OpenAIError('Chat completion stream exceeded its structured JSON byte limit');
|
|
397
|
+
}
|
|
398
|
+
if (validationWorkBudget && validationWorkBudget.work + bytes > MAX_PARTIAL_JSON_PARSE_WORK) {
|
|
399
|
+
validationWorkBudget.work += previousBytes;
|
|
400
|
+
throw new OpenAIError('Chat completion stream exceeded its structured JSON parse-work limit');
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
if (character !== ' ' && character !== '\n' && character !== '\r' && character !== '\t') {
|
|
404
|
+
hasNonWhitespace = true;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
if (inString) {
|
|
408
|
+
if (escaped) {
|
|
409
|
+
escaped = false;
|
|
410
|
+
} else if (character === '\\') {
|
|
411
|
+
escaped = true;
|
|
412
|
+
} else if (character === '"') {
|
|
413
|
+
inString = false;
|
|
414
|
+
completed ||= depth === 0;
|
|
415
|
+
}
|
|
416
|
+
continue;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
if (character === '"') {
|
|
420
|
+
inString = true;
|
|
421
|
+
} else if (character === '{' || character === '[') {
|
|
422
|
+
depth += 1;
|
|
423
|
+
if (depth > MAX_PARTIAL_JSON_DEPTH) {
|
|
424
|
+
throw new OpenAIError('Chat completion stream exceeded its structured JSON nesting depth limit');
|
|
425
|
+
}
|
|
426
|
+
} else if ((character === '}' || character === ']') && depth > 0) {
|
|
427
|
+
depth -= 1;
|
|
428
|
+
completed ||= depth === 0;
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
state.bytes += bytes;
|
|
433
|
+
state.fragments += 1;
|
|
434
|
+
state.depth = depth;
|
|
435
|
+
state.escaped = escaped;
|
|
436
|
+
state.has_non_whitespace = hasNonWhitespace;
|
|
437
|
+
state.in_string = inString;
|
|
438
|
+
if (fragment.length > 0) {
|
|
439
|
+
const finalCodeUnit = fragment.codePointAt(fragment.length - 1) ?? 0;
|
|
440
|
+
state.pending_high_surrogate = finalCodeUnit >= 0xd8_00 && finalCodeUnit <= 0xdb_ff;
|
|
441
|
+
}
|
|
442
|
+
budget.bytes += bytes;
|
|
443
|
+
budget.fragments += 1;
|
|
444
|
+
if (validationWorkBudget) {
|
|
445
|
+
validationWorkBudget.work += bytes;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
if (!hasNonWhitespace || bytes === 0) {
|
|
449
|
+
return false;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
const minimumGrowth = Math.max(EAGER_PARTIAL_JSON_BYTES, Math.floor(state.last_parsed_bytes / 2));
|
|
453
|
+
if (
|
|
454
|
+
state.bytes > EAGER_PARTIAL_JSON_BYTES &&
|
|
455
|
+
!completed &&
|
|
456
|
+
state.bytes - state.last_parsed_bytes < minimumGrowth
|
|
457
|
+
) {
|
|
458
|
+
return false;
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
return true;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
function reservePartialJSONParse(state: PartialJSONParseState, budget: PartialJSONParseBudget): boolean {
|
|
465
|
+
if (budget.work + state.bytes > MAX_PARTIAL_JSON_PARSE_WORK) {
|
|
466
|
+
return false;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
budget.work += state.bytes;
|
|
470
|
+
state.work += state.bytes;
|
|
471
|
+
state.last_parsed_bytes = state.bytes;
|
|
472
|
+
return true;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
function captureStructuredJSONSnapshot(
|
|
476
|
+
snapshot: ChatCompletionSnapshot.Choice.Message | ChatCompletionSnapshot.Choice.Message.ToolCall.Function,
|
|
477
|
+
property: 'content' | 'arguments' | 'refusal',
|
|
478
|
+
): string | null | undefined {
|
|
479
|
+
const descriptor = Object.getOwnPropertyDescriptor(snapshot, property);
|
|
480
|
+
if (!descriptor) {
|
|
481
|
+
// SAFETY: Object.getPrototypeOf returns an object or null; the following traversal checks inherited properties without assuming a specific prototype type.
|
|
482
|
+
let prototype = Object.getPrototypeOf(snapshot) as object | null;
|
|
483
|
+
for (let depth = 0; prototype !== null; depth += 1) {
|
|
484
|
+
if (depth >= MAX_PARTIAL_JSON_DEPTH || Object.getOwnPropertyDescriptor(prototype, property)) {
|
|
485
|
+
throw new OpenAIError('Chat completion stream contains an unsafe structured JSON snapshot');
|
|
486
|
+
}
|
|
487
|
+
// SAFETY: Object.getPrototypeOf returns an object or null; the following traversal checks inherited properties without assuming a specific prototype type.
|
|
488
|
+
prototype = Object.getPrototypeOf(prototype) as object | null;
|
|
489
|
+
}
|
|
490
|
+
return undefined;
|
|
491
|
+
}
|
|
492
|
+
if (
|
|
493
|
+
!('value' in descriptor) ||
|
|
494
|
+
(typeof descriptor.value !== 'string' && descriptor.value !== null && descriptor.value !== undefined)
|
|
495
|
+
) {
|
|
496
|
+
throw new OpenAIError('Chat completion stream contains an unsafe structured JSON snapshot');
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
// SAFETY: The own data descriptor was explicitly checked to contain only a string, null, or undefined.
|
|
500
|
+
return descriptor.value as string | null | undefined;
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
function captureStructuredMessageSnapshot(
|
|
504
|
+
choice: ChatCompletionSnapshot.Choice,
|
|
505
|
+
): ChatCompletionSnapshot.Choice.Message {
|
|
506
|
+
const descriptor = Object.getOwnPropertyDescriptor(choice, 'message');
|
|
507
|
+
if (
|
|
508
|
+
!descriptor ||
|
|
509
|
+
!('value' in descriptor) ||
|
|
510
|
+
typeof descriptor.value !== 'object' ||
|
|
511
|
+
descriptor.value === null
|
|
512
|
+
) {
|
|
513
|
+
throw new OpenAIError('Chat completion stream contains an unsafe structured JSON snapshot');
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
// SAFETY: The choice contract supplies a message; the own data descriptor check prevents getters from changing which object is captured.
|
|
517
|
+
return descriptor.value as ChatCompletionSnapshot.Choice.Message;
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
function captureSnapshotArray<Item>(
|
|
521
|
+
snapshot: ChatCompletionSnapshot | ChatCompletionSnapshot.Choice.Message,
|
|
522
|
+
property: 'choices' | 'tool_calls',
|
|
523
|
+
maximum: number,
|
|
524
|
+
kind: 'choice' | 'tool-call',
|
|
525
|
+
): Item[] | undefined {
|
|
526
|
+
const descriptor = Object.getOwnPropertyDescriptor(snapshot, property);
|
|
527
|
+
if (!descriptor) {
|
|
528
|
+
// SAFETY: Object.getPrototypeOf returns an object or null; the following traversal checks inherited properties without assuming a specific prototype type.
|
|
529
|
+
let prototype = Object.getPrototypeOf(snapshot) as object | null;
|
|
530
|
+
for (let depth = 0; prototype !== null; depth += 1) {
|
|
531
|
+
if (depth >= MAX_PARTIAL_JSON_DEPTH || Object.getOwnPropertyDescriptor(prototype, property)) {
|
|
532
|
+
throw new OpenAIError(`Chat completion stream contains an unsafe snapshot ${kind} collection`);
|
|
533
|
+
}
|
|
534
|
+
// SAFETY: Object.getPrototypeOf returns an object or null; the following traversal checks inherited properties without assuming a specific prototype type.
|
|
535
|
+
prototype = Object.getPrototypeOf(prototype) as object | null;
|
|
536
|
+
}
|
|
537
|
+
return undefined;
|
|
538
|
+
}
|
|
539
|
+
if (!('value' in descriptor) || !Array.isArray(descriptor.value)) {
|
|
540
|
+
throw new OpenAIError(`Chat completion stream contains an unsafe snapshot ${kind} collection`);
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
const length = Object.getOwnPropertyDescriptor(descriptor.value, 'length');
|
|
544
|
+
if (!length || !('value' in length) || !Number.isSafeInteger(length.value) || length.value > maximum) {
|
|
545
|
+
throw new OpenAIError(`Chat completion stream exceeded its snapshot ${kind} limit`);
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
// SAFETY: The captured data property is an array with a checked bounded length; Item comes from the owning snapshot collection's contract.
|
|
549
|
+
return descriptor.value as Item[];
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
function captureSnapshotArrayItem<Item>(array: Item[], index: number): Item | undefined {
|
|
553
|
+
const descriptor = Object.getOwnPropertyDescriptor(array, index);
|
|
554
|
+
if (!descriptor) {
|
|
555
|
+
return undefined;
|
|
556
|
+
}
|
|
557
|
+
if (!('value' in descriptor)) {
|
|
558
|
+
throw new OpenAIError('Chat completion stream contains an unsafe structured JSON snapshot');
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
// SAFETY: The descriptor is a data property of the typed Item array, so its value retains that array's element contract.
|
|
562
|
+
return descriptor.value as Item;
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
function mapCapturedSnapshotArray<Item, Mapped>(
|
|
566
|
+
array: Item[],
|
|
567
|
+
maximum: number,
|
|
568
|
+
kind: 'choice' | 'tool-call',
|
|
569
|
+
map: (item: Item, index: number) => Mapped,
|
|
570
|
+
): Mapped[] {
|
|
571
|
+
const descriptor = Object.getOwnPropertyDescriptor(array, 'length');
|
|
572
|
+
const length: unknown = descriptor && 'value' in descriptor ? descriptor.value : undefined;
|
|
573
|
+
if (typeof length !== 'number' || !Number.isSafeInteger(length) || length < 0 || length > maximum) {
|
|
574
|
+
throw new OpenAIError(`Chat completion stream exceeded its snapshot ${kind} limit`);
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
const mapped: Mapped[] = [];
|
|
578
|
+
mapped.length = length;
|
|
579
|
+
for (let index = 0; index < length; index += 1) {
|
|
580
|
+
const item = Object.getOwnPropertyDescriptor(array, index);
|
|
581
|
+
if (!item) {
|
|
582
|
+
continue;
|
|
583
|
+
}
|
|
584
|
+
if (!('value' in item)) {
|
|
585
|
+
throw new OpenAIError('Chat completion stream contains an unsafe structured JSON snapshot');
|
|
586
|
+
}
|
|
587
|
+
// SAFETY: The descriptor is a data property of the typed Item array, so its value retains that array's element contract.
|
|
588
|
+
mapped[index] = map(item.value as Item, index);
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
return mapped;
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
function validateStructuredJSONSnapshot(
|
|
595
|
+
value: string,
|
|
596
|
+
budget?: PartialJSONParseBudget,
|
|
597
|
+
validationWorkBudget?: PartialJSONParseBudget,
|
|
598
|
+
): string {
|
|
599
|
+
const state = createPartialJSONParseState();
|
|
600
|
+
const parseBudget = budget ?? { bytes: 0, fragments: 0, work: 0 };
|
|
601
|
+
recordPartialJSONFragment(state, parseBudget, value, validationWorkBudget);
|
|
602
|
+
if (!reservePartialJSONParse(state, parseBudget)) {
|
|
603
|
+
throw new OpenAIError('Chat completion stream exceeded its structured JSON parse-work limit');
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
return value;
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
function ownFunctionToolIdentity(
|
|
610
|
+
toolCall: PartialToolCallSnapshot,
|
|
611
|
+
): Pick<ToolCallIdentity, 'type' | 'name'> | undefined {
|
|
612
|
+
const type = Object.getOwnPropertyDescriptor(toolCall, 'type');
|
|
613
|
+
const fn = Object.getOwnPropertyDescriptor(toolCall, 'function');
|
|
614
|
+
if (!type || !('value' in type) || type.value !== 'function' || !fn || !('value' in fn)) {
|
|
615
|
+
return undefined;
|
|
616
|
+
}
|
|
617
|
+
if (typeof fn.value !== 'object' || fn.value === null) {
|
|
618
|
+
return undefined;
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
const name = Object.getOwnPropertyDescriptor(fn.value, 'name');
|
|
622
|
+
if (!name || !('value' in name) || typeof name.value !== 'string' || name.value.length === 0) {
|
|
623
|
+
return undefined;
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
return { type: 'function', name: name.value };
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
function assertBoundToolCallIdentity(toolCall: PartialToolCallSnapshot, identity: ToolCallIdentity): void {
|
|
630
|
+
const current = ownFunctionToolIdentity(toolCall);
|
|
631
|
+
if (!current || current.name !== identity.name || current.type !== identity.type) {
|
|
632
|
+
throw new OpenAIError('Chat completion stream contains a changed tool call identity');
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
// oxlint-disable-next-line anti-slop/no-object-parameters -- This assignment primitive copies own properties from heterogeneous snapshot and delta objects.
|
|
637
|
+
function assignOwnProperties<T extends object>(target: T, source: object): T {
|
|
638
|
+
if (Object.prototype.propertyIsEnumerable.call(source, '__proto__') && !hasOwn(target, '__proto__')) {
|
|
639
|
+
Object.defineProperty(target, '__proto__', {
|
|
640
|
+
value: undefined,
|
|
641
|
+
writable: true,
|
|
642
|
+
enumerable: true,
|
|
643
|
+
configurable: true,
|
|
644
|
+
});
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
return Object.assign(target, source);
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
function cloneParserConfigObject<Value extends object>(
|
|
651
|
+
value: Value,
|
|
652
|
+
stableFields: readonly PropertyKey[] = [],
|
|
653
|
+
): Value {
|
|
654
|
+
const descriptors: PropertyDescriptorMap = Object.getOwnPropertyDescriptors(value);
|
|
655
|
+
for (const field of stableFields) {
|
|
656
|
+
const descriptor = descriptors[field];
|
|
657
|
+
if (!descriptor && !(field in value)) {
|
|
658
|
+
continue;
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
descriptors[field] = {
|
|
662
|
+
// oxlint-disable-next-line anti-slop/no-reflect-get -- Generic config cloning must resolve inherited/accessor keys outside the declared config shape.
|
|
663
|
+
value: descriptor && 'value' in descriptor ? descriptor.value : Reflect.get(value, field, value),
|
|
664
|
+
enumerable: descriptor?.enumerable ?? false,
|
|
665
|
+
configurable: descriptor?.configurable ?? true,
|
|
666
|
+
writable: descriptor && 'writable' in descriptor ? descriptor.writable : false,
|
|
667
|
+
};
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
// SAFETY: The clone preserves the original prototype and descriptors, replacing only the parser metadata captured from that same value.
|
|
671
|
+
return Object.create(Object.getPrototypeOf(value), descriptors) as Value;
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
function snapshotChatCompletionParserParams(params: ChatCompletionCreateParams): ChatCompletionCreateParams {
|
|
675
|
+
const snapshot = cloneParserConfigObject(params);
|
|
676
|
+
|
|
677
|
+
if (params.tools) {
|
|
678
|
+
const stableTools: NonNullable<ChatCompletionCreateParams['tools']> = [];
|
|
679
|
+
const lengthDescriptor = Object.getOwnPropertyDescriptor(params.tools, 'length');
|
|
680
|
+
const length = lengthDescriptor && 'value' in lengthDescriptor ? lengthDescriptor.value : undefined;
|
|
681
|
+
const toolCount =
|
|
682
|
+
typeof length === 'number' && Number.isSafeInteger(length) && length >= 0
|
|
683
|
+
? Math.min(length, MAX_STREAM_TOOL_CALLS)
|
|
684
|
+
: 0;
|
|
685
|
+
|
|
686
|
+
for (let index = 0; index < toolCount; index += 1) {
|
|
687
|
+
const item = Object.getOwnPropertyDescriptor(params.tools, String(index));
|
|
688
|
+
if (!item || !('value' in item)) {
|
|
689
|
+
stableTools.length = index + 1;
|
|
690
|
+
continue;
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
// SAFETY: This own data descriptor comes from the request's typed tools array; the following code captures its parser metadata.
|
|
694
|
+
const tool = item.value as NonNullable<ChatCompletionCreateParams['tools']>[number];
|
|
695
|
+
const stableTool = cloneParserConfigObject(tool, [
|
|
696
|
+
'type',
|
|
697
|
+
'$brand',
|
|
698
|
+
'$parseRaw',
|
|
699
|
+
'$callback',
|
|
700
|
+
'function',
|
|
701
|
+
]);
|
|
702
|
+
const descriptors = Object.getOwnPropertyDescriptors(stableTool);
|
|
703
|
+
|
|
704
|
+
if (isChatCompletionFunctionTool(stableTool)) {
|
|
705
|
+
const descriptor = descriptors.function;
|
|
706
|
+
descriptors.function = {
|
|
707
|
+
...(descriptor && 'value' in descriptor
|
|
708
|
+
? descriptor
|
|
709
|
+
: { configurable: true, enumerable: true, writable: true }),
|
|
710
|
+
value: cloneParserConfigObject(stableTool.function, ['name', 'strict']),
|
|
711
|
+
};
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
// SAFETY: The cloned tool retains the original prototype and descriptors while substituting its captured parser configuration.
|
|
715
|
+
stableTools[index] = Object.create(Object.getPrototypeOf(tool), descriptors) as typeof tool;
|
|
716
|
+
}
|
|
717
|
+
snapshot.tools = stableTools;
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
if (params.response_format) {
|
|
721
|
+
snapshot.response_format = cloneParserConfigObject(params.response_format, [
|
|
722
|
+
'type',
|
|
723
|
+
'$brand',
|
|
724
|
+
'$parseRaw',
|
|
725
|
+
]);
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
return snapshot;
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
type ChatCompletionInputTool = NonNullable<ChatCompletionCreateParams['tools']>[number];
|
|
732
|
+
type ChatCompletionResponseFormat = NonNullable<ChatCompletionCreateParams['response_format']>;
|
|
733
|
+
|
|
734
|
+
interface SerializedFunctionParserConfig {
|
|
735
|
+
source: ChatCompletionInputTool | undefined;
|
|
736
|
+
schemaMatches: boolean;
|
|
737
|
+
name?: string;
|
|
738
|
+
strict?: boolean;
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
interface SerializedToolParserConfig {
|
|
742
|
+
source: ChatCompletionInputTool | undefined;
|
|
743
|
+
type?: string;
|
|
744
|
+
function?: SerializedFunctionParserConfig;
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
interface SerializedResponseParserConfig {
|
|
748
|
+
source: ChatCompletionResponseFormat | undefined;
|
|
749
|
+
schemaMatches: boolean;
|
|
750
|
+
type?: string;
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
interface SerializedParserSchemaBudget {
|
|
754
|
+
nodes: number;
|
|
755
|
+
bytes: number;
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
const MAX_SERIALIZED_PARSER_SCHEMA_NODES = 4096;
|
|
759
|
+
const stringifyParserSchemaValue = JSON.stringify;
|
|
760
|
+
const MAX_SERIALIZED_PARSER_SCHEMA_BYTES = 1024 * 1024;
|
|
761
|
+
const MAX_SERIALIZED_PARSER_SCHEMA_DEPTH = 64;
|
|
762
|
+
const OMITTED_SERIALIZED_PARSER_VALUE = Symbol('omitted serialized parser value');
|
|
763
|
+
const UNSAFE_SERIALIZED_PARSER_VALUE = Symbol('unsafe serialized parser value');
|
|
764
|
+
|
|
765
|
+
type CanonicalSerializedParserValue =
|
|
766
|
+
| string
|
|
767
|
+
| typeof OMITTED_SERIALIZED_PARSER_VALUE
|
|
768
|
+
| typeof UNSAFE_SERIALIZED_PARSER_VALUE;
|
|
769
|
+
|
|
770
|
+
function canonicalSerializedParserSchema(
|
|
771
|
+
value: unknown,
|
|
772
|
+
budget: SerializedParserSchemaBudget,
|
|
773
|
+
): string | undefined {
|
|
774
|
+
const ancestors = new WeakSet<object>();
|
|
775
|
+
|
|
776
|
+
const charge = (bytes: number): boolean => {
|
|
777
|
+
if (
|
|
778
|
+
!Number.isSafeInteger(bytes) ||
|
|
779
|
+
bytes < 0 ||
|
|
780
|
+
budget.bytes + bytes > MAX_SERIALIZED_PARSER_SCHEMA_BYTES
|
|
781
|
+
) {
|
|
782
|
+
return false;
|
|
783
|
+
}
|
|
784
|
+
budget.bytes += bytes;
|
|
785
|
+
return true;
|
|
786
|
+
};
|
|
787
|
+
|
|
788
|
+
// SAFETY: Object.getPrototypeOf returns an object or null; the following traversal checks inherited properties without assuming a specific prototype type.
|
|
789
|
+
const visit = (current: unknown, depth: number): CanonicalSerializedParserValue => {
|
|
790
|
+
if (depth > MAX_SERIALIZED_PARSER_SCHEMA_DEPTH || budget.nodes >= MAX_SERIALIZED_PARSER_SCHEMA_NODES) {
|
|
791
|
+
return UNSAFE_SERIALIZED_PARSER_VALUE;
|
|
792
|
+
}
|
|
793
|
+
budget.nodes += 1;
|
|
794
|
+
|
|
795
|
+
if (current === undefined || typeof current === 'function' || typeof current === 'symbol') {
|
|
796
|
+
return OMITTED_SERIALIZED_PARSER_VALUE;
|
|
797
|
+
}
|
|
798
|
+
if (typeof current === 'bigint') {
|
|
799
|
+
return UNSAFE_SERIALIZED_PARSER_VALUE;
|
|
800
|
+
}
|
|
801
|
+
if (current === null || typeof current === 'boolean' || typeof current === 'number') {
|
|
802
|
+
const serialized = stringifyParserSchemaValue(current);
|
|
803
|
+
return typeof serialized === 'string' && charge(serialized.length)
|
|
804
|
+
? serialized
|
|
805
|
+
: UNSAFE_SERIALIZED_PARSER_VALUE;
|
|
806
|
+
}
|
|
807
|
+
if (typeof current === 'string') {
|
|
808
|
+
if (!charge(current.length * 6 + 2)) {
|
|
809
|
+
return UNSAFE_SERIALIZED_PARSER_VALUE;
|
|
810
|
+
}
|
|
811
|
+
return stringifyParserSchemaValue(current);
|
|
812
|
+
}
|
|
813
|
+
if (typeof current !== 'object' || ancestors.has(current)) {
|
|
814
|
+
return UNSAFE_SERIALIZED_PARSER_VALUE;
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
const array = Array.isArray(current);
|
|
818
|
+
// SAFETY: Object.getPrototypeOf returns an object or null; the following traversal checks inherited properties without assuming a specific prototype type.
|
|
819
|
+
const prototype = Object.getPrototypeOf(current) as object | null;
|
|
820
|
+
if (
|
|
821
|
+
(array && prototype !== Array.prototype) ||
|
|
822
|
+
(!array && prototype !== null && prototype !== Object.prototype)
|
|
823
|
+
) {
|
|
824
|
+
return UNSAFE_SERIALIZED_PARSER_VALUE;
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
for (
|
|
828
|
+
let owner: object | null = current;
|
|
829
|
+
owner !== null;
|
|
830
|
+
owner = Object.getPrototypeOf(owner) as object | null
|
|
831
|
+
) {
|
|
832
|
+
const serializer = Object.getOwnPropertyDescriptor(owner, 'toJSON');
|
|
833
|
+
if (!serializer) {
|
|
834
|
+
continue;
|
|
835
|
+
}
|
|
836
|
+
if (!('value' in serializer) || typeof serializer.value === 'function') {
|
|
837
|
+
return UNSAFE_SERIALIZED_PARSER_VALUE;
|
|
838
|
+
}
|
|
839
|
+
break;
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
ancestors.add(current);
|
|
843
|
+
try {
|
|
844
|
+
if (!charge(2)) {
|
|
845
|
+
return UNSAFE_SERIALIZED_PARSER_VALUE;
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
if (array) {
|
|
849
|
+
const lengthDescriptor = Object.getOwnPropertyDescriptor(current, 'length');
|
|
850
|
+
const length = lengthDescriptor && 'value' in lengthDescriptor ? lengthDescriptor.value : undefined;
|
|
851
|
+
if (
|
|
852
|
+
typeof length !== 'number' ||
|
|
853
|
+
!Number.isSafeInteger(length) ||
|
|
854
|
+
length < 0 ||
|
|
855
|
+
length > MAX_SERIALIZED_PARSER_SCHEMA_NODES - budget.nodes
|
|
856
|
+
) {
|
|
857
|
+
return UNSAFE_SERIALIZED_PARSER_VALUE;
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
const items: string[] = [];
|
|
861
|
+
for (let index = 0; index < length; index += 1) {
|
|
862
|
+
const key = String(index);
|
|
863
|
+
const descriptor = Object.getOwnPropertyDescriptor(current, key);
|
|
864
|
+
if (!descriptor) {
|
|
865
|
+
if (
|
|
866
|
+
Object.getOwnPropertyDescriptor(Array.prototype, key) ||
|
|
867
|
+
Object.getOwnPropertyDescriptor(Object.prototype, key)
|
|
868
|
+
) {
|
|
869
|
+
return UNSAFE_SERIALIZED_PARSER_VALUE;
|
|
870
|
+
}
|
|
871
|
+
budget.nodes += 1;
|
|
872
|
+
if (!charge(4)) {
|
|
873
|
+
return UNSAFE_SERIALIZED_PARSER_VALUE;
|
|
874
|
+
}
|
|
875
|
+
items.push('null');
|
|
876
|
+
continue;
|
|
877
|
+
}
|
|
878
|
+
if (!('value' in descriptor)) {
|
|
879
|
+
return UNSAFE_SERIALIZED_PARSER_VALUE;
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
const item = visit(descriptor.value, depth + 1);
|
|
883
|
+
if (item === UNSAFE_SERIALIZED_PARSER_VALUE) {
|
|
884
|
+
return item;
|
|
885
|
+
}
|
|
886
|
+
items.push(item === OMITTED_SERIALIZED_PARSER_VALUE ? 'null' : item);
|
|
887
|
+
}
|
|
888
|
+
return `[${items.join(',')}]`;
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
const keys = Reflect.ownKeys(current);
|
|
892
|
+
if (keys.length > MAX_SERIALIZED_PARSER_SCHEMA_NODES - budget.nodes) {
|
|
893
|
+
return UNSAFE_SERIALIZED_PARSER_VALUE;
|
|
894
|
+
}
|
|
895
|
+
|
|
896
|
+
const entries: [string, unknown][] = [];
|
|
897
|
+
for (const key of keys) {
|
|
898
|
+
if (typeof key !== 'string') {
|
|
899
|
+
continue;
|
|
900
|
+
}
|
|
901
|
+
const descriptor = Object.getOwnPropertyDescriptor(current, key);
|
|
902
|
+
if (!descriptor) {
|
|
903
|
+
return UNSAFE_SERIALIZED_PARSER_VALUE;
|
|
904
|
+
}
|
|
905
|
+
if (!descriptor.enumerable) {
|
|
906
|
+
continue;
|
|
907
|
+
}
|
|
908
|
+
if (!('value' in descriptor)) {
|
|
909
|
+
return UNSAFE_SERIALIZED_PARSER_VALUE;
|
|
910
|
+
}
|
|
911
|
+
entries.push([key, descriptor.value]);
|
|
912
|
+
}
|
|
913
|
+
entries.sort(([left], [right]) => {
|
|
914
|
+
if (left === right) {
|
|
915
|
+
return 0;
|
|
916
|
+
}
|
|
917
|
+
return left < right ? -1 : 1;
|
|
918
|
+
});
|
|
919
|
+
|
|
920
|
+
const fields: string[] = [];
|
|
921
|
+
for (const [key, entry] of entries) {
|
|
922
|
+
const normalized = visit(entry, depth + 1);
|
|
923
|
+
if (normalized === UNSAFE_SERIALIZED_PARSER_VALUE) {
|
|
924
|
+
return normalized;
|
|
925
|
+
}
|
|
926
|
+
if (normalized === OMITTED_SERIALIZED_PARSER_VALUE) {
|
|
927
|
+
continue;
|
|
928
|
+
}
|
|
929
|
+
if (!charge(key.length * 6 + 3)) {
|
|
930
|
+
return UNSAFE_SERIALIZED_PARSER_VALUE;
|
|
931
|
+
}
|
|
932
|
+
fields.push(`${stringifyParserSchemaValue(key)}:${normalized}`);
|
|
933
|
+
}
|
|
934
|
+
return `{${fields.join(',')}}`;
|
|
935
|
+
} finally {
|
|
936
|
+
ancestors.delete(current);
|
|
937
|
+
}
|
|
938
|
+
};
|
|
939
|
+
|
|
940
|
+
try {
|
|
941
|
+
const normalized = visit(value, 0);
|
|
942
|
+
return typeof normalized === 'string' ? normalized : undefined;
|
|
943
|
+
} catch {
|
|
944
|
+
return undefined;
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
|
|
948
|
+
function rememberSerializedParserSchema(
|
|
949
|
+
signatures: WeakMap<object, string>,
|
|
950
|
+
// oxlint-disable-next-line anti-slop/no-object-parameters -- Parser owners are tracked by identity before their metadata descriptors are validated.
|
|
951
|
+
source: object,
|
|
952
|
+
// oxlint-disable-next-line anti-slop/no-object-parameters -- Serialized schema holders may be arbitrary objects with hostile accessors or prototypes.
|
|
953
|
+
holder: object,
|
|
954
|
+
key: string,
|
|
955
|
+
): void {
|
|
956
|
+
const parser = Object.getOwnPropertyDescriptor(source, '$parseRaw');
|
|
957
|
+
const schema = Object.getOwnPropertyDescriptor(holder, key);
|
|
958
|
+
if (
|
|
959
|
+
!parser ||
|
|
960
|
+
!('value' in parser) ||
|
|
961
|
+
typeof parser.value !== 'function' ||
|
|
962
|
+
!schema ||
|
|
963
|
+
!('value' in schema)
|
|
964
|
+
) {
|
|
965
|
+
return;
|
|
966
|
+
}
|
|
967
|
+
|
|
968
|
+
const normalized = canonicalSerializedParserSchema(schema.value, { nodes: 0, bytes: 0 });
|
|
969
|
+
if (normalized !== undefined) {
|
|
970
|
+
signatures.set(source, normalized);
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
|
|
974
|
+
function hasMatchingSerializedParserSchema(
|
|
975
|
+
signatures: WeakMap<object, string>,
|
|
976
|
+
// oxlint-disable-next-line anti-slop/no-object-parameters -- Parser signatures belong to the original object identity, independent of its fields.
|
|
977
|
+
source: object | undefined,
|
|
978
|
+
// oxlint-disable-next-line anti-slop/no-object-parameters -- The schema holder is inspected through own descriptors before its contents are trusted.
|
|
979
|
+
holder: object,
|
|
980
|
+
key: string,
|
|
981
|
+
value: unknown,
|
|
982
|
+
): boolean {
|
|
983
|
+
const expected = source && signatures.get(source);
|
|
984
|
+
const descriptor = Object.getOwnPropertyDescriptor(holder, key);
|
|
985
|
+
return (
|
|
986
|
+
expected !== undefined &&
|
|
987
|
+
descriptor !== undefined &&
|
|
988
|
+
'value' in descriptor &&
|
|
989
|
+
canonicalSerializedParserSchema(value, { nodes: 0, bytes: 0 }) === expected
|
|
990
|
+
);
|
|
991
|
+
}
|
|
992
|
+
|
|
993
|
+
function serializedParserDescriptor(
|
|
994
|
+
descriptor: PropertyDescriptor | undefined,
|
|
995
|
+
value: unknown,
|
|
996
|
+
): PropertyDescriptor {
|
|
997
|
+
return descriptor && 'value' in descriptor
|
|
998
|
+
? { ...descriptor, value }
|
|
999
|
+
: { configurable: true, enumerable: true, writable: true, value };
|
|
1000
|
+
}
|
|
1001
|
+
|
|
1002
|
+
function shadowSerializedParserMetadata(
|
|
1003
|
+
descriptors: PropertyDescriptorMap,
|
|
1004
|
+
source: ChatCompletionInputTool | ChatCompletionResponseFormat,
|
|
1005
|
+
fields: readonly string[],
|
|
1006
|
+
): void {
|
|
1007
|
+
for (const field of fields) {
|
|
1008
|
+
const descriptor = descriptors[field];
|
|
1009
|
+
if (!descriptor && !(field in source)) {
|
|
1010
|
+
continue;
|
|
1011
|
+
}
|
|
1012
|
+
descriptors[field] =
|
|
1013
|
+
descriptor && 'value' in descriptor
|
|
1014
|
+
? { ...descriptor, value: undefined }
|
|
1015
|
+
: {
|
|
1016
|
+
configurable: descriptor?.configurable ?? true,
|
|
1017
|
+
enumerable: descriptor?.enumerable ?? false,
|
|
1018
|
+
writable: false,
|
|
1019
|
+
value: undefined,
|
|
1020
|
+
};
|
|
1021
|
+
}
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
function snapshotSerializedParserTool(serialized: SerializedToolParserConfig): ChatCompletionInputTool {
|
|
1025
|
+
// SAFETY: The fallback is a serialization scaffold: the code below installs the captured wire fields before the tool is returned.
|
|
1026
|
+
const source =
|
|
1027
|
+
serialized.source ??
|
|
1028
|
+
({
|
|
1029
|
+
type: serialized.type,
|
|
1030
|
+
...(serialized.type === 'function' ? { function: {} } : {}),
|
|
1031
|
+
} as ChatCompletionInputTool);
|
|
1032
|
+
const descriptors = Object.getOwnPropertyDescriptors(source);
|
|
1033
|
+
descriptors.type = serializedParserDescriptor(descriptors.type, serialized.type);
|
|
1034
|
+
|
|
1035
|
+
if (serialized.type !== 'function' || !serialized.function) {
|
|
1036
|
+
if (descriptors.function) {
|
|
1037
|
+
descriptors.function = serializedParserDescriptor(descriptors.function, undefined);
|
|
1038
|
+
}
|
|
1039
|
+
shadowSerializedParserMetadata(descriptors, source, ['$brand', '$parseRaw', '$callback']);
|
|
1040
|
+
// SAFETY: The descriptors reconstruct the tool's captured serialized fields and shadow parser metadata while retaining its original prototype.
|
|
1041
|
+
return Object.create(Object.getPrototypeOf(source), descriptors) as ChatCompletionInputTool;
|
|
1042
|
+
}
|
|
1043
|
+
|
|
1044
|
+
const descriptor = descriptors.function;
|
|
1045
|
+
// SAFETY: The preceding guard proves this data descriptor contains a non-null object; no more specific type is assumed.
|
|
1046
|
+
const original =
|
|
1047
|
+
descriptor && 'value' in descriptor && typeof descriptor.value === 'object' && descriptor.value !== null
|
|
1048
|
+
? (descriptor.value as object)
|
|
1049
|
+
: {};
|
|
1050
|
+
const functionDescriptors = Object.getOwnPropertyDescriptors(original);
|
|
1051
|
+
functionDescriptors['name'] = serializedParserDescriptor(
|
|
1052
|
+
functionDescriptors['name'],
|
|
1053
|
+
serialized.function.name,
|
|
1054
|
+
);
|
|
1055
|
+
functionDescriptors['strict'] = serializedParserDescriptor(
|
|
1056
|
+
functionDescriptors['strict'],
|
|
1057
|
+
serialized.function.strict,
|
|
1058
|
+
);
|
|
1059
|
+
descriptors.function = serializedParserDescriptor(
|
|
1060
|
+
descriptor,
|
|
1061
|
+
Object.create(Object.getPrototypeOf(original), functionDescriptors),
|
|
1062
|
+
);
|
|
1063
|
+
if (!serialized.function.schemaMatches) {
|
|
1064
|
+
shadowSerializedParserMetadata(descriptors, source, ['$brand', '$parseRaw', '$callback']);
|
|
1065
|
+
}
|
|
1066
|
+
|
|
1067
|
+
// SAFETY: The descriptors reconstruct the tool's captured serialized fields and shadow parser metadata while retaining its original prototype.
|
|
1068
|
+
return Object.create(Object.getPrototypeOf(source), descriptors) as ChatCompletionInputTool;
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1071
|
+
function snapshotSerializedResponseFormat(
|
|
1072
|
+
serialized: SerializedResponseParserConfig,
|
|
1073
|
+
): ChatCompletionResponseFormat {
|
|
1074
|
+
// SAFETY: The fallback seeds only the discriminator; the captured response-format fields are installed below before returning it.
|
|
1075
|
+
const source = serialized.source ?? ({ type: serialized.type } as ChatCompletionResponseFormat);
|
|
1076
|
+
const descriptors = Object.getOwnPropertyDescriptors(source);
|
|
1077
|
+
descriptors.type = serializedParserDescriptor(descriptors.type, serialized.type);
|
|
1078
|
+
if (serialized.type !== 'json_schema' || !serialized.source || !serialized.schemaMatches) {
|
|
1079
|
+
shadowSerializedParserMetadata(descriptors, source, ['$brand', '$parseRaw']);
|
|
1080
|
+
}
|
|
1081
|
+
// SAFETY: The descriptors restore the captured response-format fields and parser metadata on the original prototype.
|
|
1082
|
+
return Object.create(Object.getPrototypeOf(source), descriptors) as ChatCompletionResponseFormat;
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1085
|
+
// oxlint-disable-next-line anti-slop/no-object-parameters -- The serialization visitor accepts arbitrary object and array holders, inspecting own descriptors only.
|
|
1086
|
+
function ownSerializedParserObject(holder: object, key: string): object | undefined {
|
|
1087
|
+
const descriptor = Object.getOwnPropertyDescriptor(holder, key);
|
|
1088
|
+
if (!descriptor || !('value' in descriptor)) {
|
|
1089
|
+
return undefined;
|
|
1090
|
+
}
|
|
1091
|
+
const { value } = descriptor;
|
|
1092
|
+
return typeof value === 'object' && value !== null ? value : undefined;
|
|
1093
|
+
}
|
|
1094
|
+
|
|
1095
|
+
function observeSerializedChatCompletionParserParams(
|
|
1096
|
+
body: ChatCompletionCreateParams,
|
|
1097
|
+
initial: ChatCompletionCreateParams,
|
|
1098
|
+
update: (params: ChatCompletionCreateParams) => void,
|
|
1099
|
+
): () => void {
|
|
1100
|
+
const originalToolOwners = new WeakMap<object, ChatCompletionInputTool>();
|
|
1101
|
+
const originalSchemaSignatures = new WeakMap<object, string>();
|
|
1102
|
+
// At most 128 tools plus one response format each receive an independent
|
|
1103
|
+
// 4,096-node/1 MiB source and wire allowance (129 MiB maximum per pass).
|
|
1104
|
+
if (body.tools) {
|
|
1105
|
+
for (let index = 0; index < body.tools.length && index < MAX_STREAM_TOOL_CALLS; index += 1) {
|
|
1106
|
+
const owner = ownSerializedParserObject(body.tools, String(index));
|
|
1107
|
+
const source = initial.tools?.[index];
|
|
1108
|
+
if (owner && source) {
|
|
1109
|
+
originalToolOwners.set(owner, source);
|
|
1110
|
+
const originalFunction = ownSerializedParserObject(source, 'function');
|
|
1111
|
+
if (originalFunction) {
|
|
1112
|
+
rememberSerializedParserSchema(originalSchemaSignatures, source, originalFunction, 'parameters');
|
|
1113
|
+
}
|
|
1114
|
+
}
|
|
1115
|
+
}
|
|
1116
|
+
}
|
|
1117
|
+
if (initial.response_format) {
|
|
1118
|
+
rememberSerializedParserSchema(
|
|
1119
|
+
originalSchemaSignatures,
|
|
1120
|
+
initial.response_format,
|
|
1121
|
+
initial.response_format,
|
|
1122
|
+
'json_schema',
|
|
1123
|
+
);
|
|
1124
|
+
}
|
|
1125
|
+
let root: object | undefined;
|
|
1126
|
+
let tools: object | undefined;
|
|
1127
|
+
let responseFormat: object | undefined;
|
|
1128
|
+
let responseFrame: SerializedResponseParserConfig | undefined;
|
|
1129
|
+
let frames: (SerializedToolParserConfig | undefined)[] = [];
|
|
1130
|
+
let toolFrames = new WeakMap<object, SerializedToolParserConfig>();
|
|
1131
|
+
let actualToolOwners = new Map<number, object | undefined>();
|
|
1132
|
+
let functionFrames = new WeakMap<object, SerializedFunctionParserConfig>();
|
|
1133
|
+
|
|
1134
|
+
return observeJSONRequestBody(body, {
|
|
1135
|
+
value(holder, key, value) {
|
|
1136
|
+
if (!root && key === '' && typeof value === 'object' && value !== null) {
|
|
1137
|
+
root = value;
|
|
1138
|
+
tools = undefined;
|
|
1139
|
+
responseFormat = undefined;
|
|
1140
|
+
responseFrame = undefined;
|
|
1141
|
+
frames = [];
|
|
1142
|
+
toolFrames = new WeakMap();
|
|
1143
|
+
actualToolOwners = new Map();
|
|
1144
|
+
functionFrames = new WeakMap();
|
|
1145
|
+
return;
|
|
1146
|
+
}
|
|
1147
|
+
|
|
1148
|
+
if (holder === root && key === 'response_format') {
|
|
1149
|
+
if (typeof value === 'object' && value !== null) {
|
|
1150
|
+
responseFormat = value;
|
|
1151
|
+
const owner = ownSerializedParserObject(holder, key);
|
|
1152
|
+
responseFrame = {
|
|
1153
|
+
source: owner === body.response_format ? initial.response_format : undefined,
|
|
1154
|
+
schemaMatches: false,
|
|
1155
|
+
};
|
|
1156
|
+
}
|
|
1157
|
+
return;
|
|
1158
|
+
}
|
|
1159
|
+
|
|
1160
|
+
if (holder === root && key === 'tools') {
|
|
1161
|
+
if (Array.isArray(value)) {
|
|
1162
|
+
tools = new Proxy(value, {
|
|
1163
|
+
get(target, property) {
|
|
1164
|
+
// SAFETY: Proxy property values may be arbitrary; unknown preserves that uncertainty before the property-specific checks below.
|
|
1165
|
+
// oxlint-disable-next-line anti-slop/no-reflect-get -- Proxy forwarding must preserve arbitrary keys with the original target as accessor receiver.
|
|
1166
|
+
const actual = Reflect.get(target, property, target) as unknown;
|
|
1167
|
+
if (typeof property === 'string') {
|
|
1168
|
+
const index = Number(property);
|
|
1169
|
+
if (
|
|
1170
|
+
Number.isSafeInteger(index) &&
|
|
1171
|
+
index >= 0 &&
|
|
1172
|
+
index < MAX_STREAM_TOOL_CALLS &&
|
|
1173
|
+
String(index) === property
|
|
1174
|
+
) {
|
|
1175
|
+
actualToolOwners.set(
|
|
1176
|
+
index,
|
|
1177
|
+
typeof actual === 'object' && actual !== null ? actual : undefined,
|
|
1178
|
+
);
|
|
1179
|
+
}
|
|
1180
|
+
}
|
|
1181
|
+
return actual;
|
|
1182
|
+
},
|
|
1183
|
+
});
|
|
1184
|
+
return tools;
|
|
1185
|
+
}
|
|
1186
|
+
return;
|
|
1187
|
+
}
|
|
1188
|
+
|
|
1189
|
+
if (holder === tools) {
|
|
1190
|
+
const index = Number(key);
|
|
1191
|
+
if (
|
|
1192
|
+
!Number.isSafeInteger(index) ||
|
|
1193
|
+
index < 0 ||
|
|
1194
|
+
index >= MAX_STREAM_TOOL_CALLS ||
|
|
1195
|
+
typeof value !== 'object' ||
|
|
1196
|
+
value === null
|
|
1197
|
+
) {
|
|
1198
|
+
return;
|
|
1199
|
+
}
|
|
1200
|
+
const owner = actualToolOwners.get(index);
|
|
1201
|
+
const source = owner ? originalToolOwners.get(owner) : undefined;
|
|
1202
|
+
const frame: SerializedToolParserConfig = { source };
|
|
1203
|
+
frames[index] = frame;
|
|
1204
|
+
toolFrames.set(value, frame);
|
|
1205
|
+
return;
|
|
1206
|
+
}
|
|
1207
|
+
|
|
1208
|
+
const tool = toolFrames.get(holder);
|
|
1209
|
+
if (tool) {
|
|
1210
|
+
if (key === 'type' && typeof value === 'string') {
|
|
1211
|
+
tool.type = value;
|
|
1212
|
+
} else if (key === 'function' && typeof value === 'object' && value !== null) {
|
|
1213
|
+
const fn: SerializedFunctionParserConfig = { source: tool.source, schemaMatches: false };
|
|
1214
|
+
tool.function = fn;
|
|
1215
|
+
functionFrames.set(value, fn);
|
|
1216
|
+
}
|
|
1217
|
+
return;
|
|
1218
|
+
}
|
|
1219
|
+
|
|
1220
|
+
if (holder === responseFormat && responseFrame) {
|
|
1221
|
+
if (key === 'type' && typeof value === 'string') {
|
|
1222
|
+
responseFrame.type = value;
|
|
1223
|
+
} else if (key === 'json_schema') {
|
|
1224
|
+
responseFrame.schemaMatches = hasMatchingSerializedParserSchema(
|
|
1225
|
+
originalSchemaSignatures,
|
|
1226
|
+
responseFrame.source,
|
|
1227
|
+
holder,
|
|
1228
|
+
key,
|
|
1229
|
+
value,
|
|
1230
|
+
);
|
|
1231
|
+
}
|
|
1232
|
+
return;
|
|
1233
|
+
}
|
|
1234
|
+
|
|
1235
|
+
const fn = functionFrames.get(holder);
|
|
1236
|
+
if (fn) {
|
|
1237
|
+
if (key === 'name' && typeof value === 'string') {
|
|
1238
|
+
fn.name = value;
|
|
1239
|
+
} else if (key === 'strict' && typeof value === 'boolean') {
|
|
1240
|
+
fn.strict = value;
|
|
1241
|
+
} else if (key === 'parameters') {
|
|
1242
|
+
fn.schemaMatches = hasMatchingSerializedParserSchema(
|
|
1243
|
+
originalSchemaSignatures,
|
|
1244
|
+
fn.source,
|
|
1245
|
+
holder,
|
|
1246
|
+
key,
|
|
1247
|
+
value,
|
|
1248
|
+
);
|
|
1249
|
+
}
|
|
1250
|
+
}
|
|
1251
|
+
return undefined;
|
|
1252
|
+
},
|
|
1253
|
+
complete() {
|
|
1254
|
+
if (!root) {
|
|
1255
|
+
return;
|
|
1256
|
+
}
|
|
1257
|
+
const snapshot = cloneParserConfigObject(initial);
|
|
1258
|
+
if (tools) {
|
|
1259
|
+
const serializedTools: ChatCompletionInputTool[] = [];
|
|
1260
|
+
for (let index = 0; index < frames.length; index += 1) {
|
|
1261
|
+
const frame = frames[index];
|
|
1262
|
+
if (frame) {
|
|
1263
|
+
serializedTools[index] = snapshotSerializedParserTool(frame);
|
|
1264
|
+
}
|
|
1265
|
+
}
|
|
1266
|
+
snapshot.tools = serializedTools;
|
|
1267
|
+
} else {
|
|
1268
|
+
delete snapshot.tools;
|
|
1269
|
+
}
|
|
1270
|
+
if (responseFrame) {
|
|
1271
|
+
snapshot.response_format = snapshotSerializedResponseFormat(responseFrame);
|
|
1272
|
+
} else {
|
|
1273
|
+
delete snapshot.response_format;
|
|
1274
|
+
}
|
|
1275
|
+
update(snapshot);
|
|
1276
|
+
root = undefined;
|
|
1277
|
+
tools = undefined;
|
|
1278
|
+
responseFormat = undefined;
|
|
1279
|
+
responseFrame = undefined;
|
|
1280
|
+
frames = [];
|
|
1281
|
+
toolFrames = new WeakMap();
|
|
1282
|
+
actualToolOwners = new Map();
|
|
1283
|
+
functionFrames = new WeakMap();
|
|
1284
|
+
},
|
|
1285
|
+
});
|
|
127
1286
|
}
|
|
128
1287
|
|
|
1288
|
+
/** Streams chat completion chunks while accumulating snapshots, parsed output, and events. */
|
|
129
1289
|
export class ChatCompletionStream<ParsedT = null>
|
|
130
1290
|
extends AbstractChatCompletionRunner<ChatCompletionStreamEvents<ParsedT>, ParsedT>
|
|
131
1291
|
implements AsyncIterable<ChatCompletionChunk>
|
|
132
1292
|
{
|
|
133
1293
|
#params: ChatCompletionCreateParams | null;
|
|
1294
|
+
#rejectsUnfinishedTurns = false;
|
|
1295
|
+
#audioDoneChoiceIndexes: Set<number>;
|
|
134
1296
|
#choiceEventStates: ChoiceEventState[];
|
|
135
1297
|
#currentChatCompletionSnapshot: ChatCompletionSnapshot | undefined;
|
|
1298
|
+
#hasAutoParseableTool: boolean;
|
|
1299
|
+
#partialJSONParseBudget: PartialJSONParseBudget;
|
|
136
1300
|
|
|
1301
|
+
/** Creates an unstarted stream, retaining request parameters for structured-output parsing. */
|
|
137
1302
|
constructor(params: ChatCompletionCreateParams | null) {
|
|
138
1303
|
super();
|
|
139
1304
|
this.#params = params;
|
|
1305
|
+
this.#audioDoneChoiceIndexes = new Set();
|
|
140
1306
|
this.#choiceEventStates = [];
|
|
1307
|
+
this.#hasAutoParseableTool = false;
|
|
1308
|
+
const tools = params?.tools;
|
|
1309
|
+
const lengthDescriptor = tools && Object.getOwnPropertyDescriptor(tools, 'length');
|
|
1310
|
+
const length = lengthDescriptor && 'value' in lengthDescriptor ? lengthDescriptor.value : undefined;
|
|
1311
|
+
if (tools && typeof length === 'number' && Number.isSafeInteger(length) && length >= 0) {
|
|
1312
|
+
for (let index = 0; index < Math.min(length, MAX_STREAM_TOOL_CALLS); index += 1) {
|
|
1313
|
+
const descriptor = Object.getOwnPropertyDescriptor(tools, String(index));
|
|
1314
|
+
if (!descriptor || !('value' in descriptor)) {
|
|
1315
|
+
continue;
|
|
1316
|
+
}
|
|
1317
|
+
// SAFETY: The descriptor is read from the typed request tools array and checked as an own data property before accessing the tool.
|
|
1318
|
+
const tool = descriptor.value as ChatCompletionInputTool;
|
|
1319
|
+
if (
|
|
1320
|
+
isChatCompletionFunctionTool(tool) &&
|
|
1321
|
+
(isAutoParsableTool(tool) || tool.function.strict === true)
|
|
1322
|
+
) {
|
|
1323
|
+
this.#hasAutoParseableTool = true;
|
|
1324
|
+
break;
|
|
1325
|
+
}
|
|
1326
|
+
}
|
|
1327
|
+
}
|
|
1328
|
+
this.#partialJSONParseBudget = { bytes: 0, fragments: 0, work: 0 };
|
|
141
1329
|
}
|
|
142
1330
|
|
|
1331
|
+
/** The latest accumulated completion, or `undefined` before a chunk arrives or after finalization. */
|
|
143
1332
|
get currentChatCompletionSnapshot(): ChatCompletionSnapshot | undefined {
|
|
144
1333
|
return this.#currentChatCompletionSnapshot;
|
|
145
1334
|
}
|
|
@@ -148,8 +1337,8 @@ export class ChatCompletionStream<ParsedT = null>
|
|
|
148
1337
|
* Intended for use on the frontend, consuming a stream produced with
|
|
149
1338
|
* `.toReadableStream()` on the backend.
|
|
150
1339
|
*
|
|
151
|
-
*
|
|
152
|
-
*
|
|
1340
|
+
* Original input messages are not included in the serialized stream. Tool-result
|
|
1341
|
+
* messages explicitly serialized by a streaming tool runner are replayed.
|
|
153
1342
|
*/
|
|
154
1343
|
static fromReadableStream(stream: ReadableStream): ChatCompletionStream<null> {
|
|
155
1344
|
const runner = new ChatCompletionStream(null);
|
|
@@ -157,25 +1346,31 @@ export class ChatCompletionStream<ParsedT = null>
|
|
|
157
1346
|
return runner;
|
|
158
1347
|
}
|
|
159
1348
|
|
|
1349
|
+
/** Starts a streaming chat completion request and returns its event-driven helper. */
|
|
160
1350
|
static createChatCompletion<ParsedT>(
|
|
161
1351
|
client: OpenAI,
|
|
162
1352
|
params: ChatCompletionStreamParams,
|
|
163
1353
|
options?: RequestOptions,
|
|
164
1354
|
): ChatCompletionStream<ParsedT> {
|
|
1355
|
+
// SAFETY: The runner forces stream: true when sending this request; the same parameters retain the caller's parsing configuration.
|
|
165
1356
|
const runner = new ChatCompletionStream<ParsedT>(params as ChatCompletionCreateParamsStreaming);
|
|
166
1357
|
runner._run(() =>
|
|
167
1358
|
runner._runChatCompletion(
|
|
168
1359
|
client,
|
|
169
1360
|
{ ...params, stream: true },
|
|
170
|
-
{ ...options,
|
|
1361
|
+
{ ...options, __metadata: { ...options?.__metadata, helperMethod: 'stream' } },
|
|
171
1362
|
),
|
|
172
1363
|
);
|
|
173
1364
|
return runner;
|
|
174
1365
|
}
|
|
175
1366
|
|
|
176
1367
|
#beginRequest() {
|
|
177
|
-
if (this.ended)
|
|
1368
|
+
if (this.ended) {
|
|
1369
|
+
return;
|
|
1370
|
+
}
|
|
1371
|
+
this.#audioDoneChoiceIndexes = new Set();
|
|
178
1372
|
this.#currentChatCompletionSnapshot = undefined;
|
|
1373
|
+
this.#partialJSONParseBudget = { bytes: 0, fragments: 0, work: 0 };
|
|
179
1374
|
}
|
|
180
1375
|
|
|
181
1376
|
#getChoiceEventState(choice: ChatCompletionSnapshot.Choice): ChoiceEventState {
|
|
@@ -186,57 +1381,70 @@ export class ChatCompletionStream<ParsedT = null>
|
|
|
186
1381
|
|
|
187
1382
|
state = {
|
|
188
1383
|
content_done: false,
|
|
1384
|
+
content_parse_state: undefined,
|
|
189
1385
|
refusal_done: false,
|
|
190
1386
|
logprobs_content_done: false,
|
|
191
1387
|
logprobs_refusal_done: false,
|
|
192
1388
|
done_tool_calls: new Set(),
|
|
193
1389
|
current_tool_call_index: null,
|
|
1390
|
+
tool_call_parse_states: new Map(),
|
|
1391
|
+
tool_call_identities: new Map(),
|
|
194
1392
|
};
|
|
195
1393
|
this.#choiceEventStates[choice.index] = state;
|
|
196
1394
|
return state;
|
|
197
1395
|
}
|
|
198
1396
|
|
|
199
1397
|
#addChunk(this: ChatCompletionStream<ParsedT>, chunk: ChatCompletionChunk) {
|
|
200
|
-
if (this.ended)
|
|
1398
|
+
if (this.ended) {
|
|
1399
|
+
return;
|
|
1400
|
+
}
|
|
201
1401
|
|
|
202
|
-
const
|
|
1402
|
+
const capturedChoiceFrames = new WeakMap<object, CapturedChoiceToolCallFrames>();
|
|
1403
|
+
const completion = this.#accumulateChatCompletion(chunk, capturedChoiceFrames);
|
|
203
1404
|
this._emit('chunk', chunk, completion);
|
|
204
1405
|
|
|
205
1406
|
for (const choice of chunk.choices) {
|
|
206
|
-
const
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
1407
|
+
const capturedChoice = capturedChoiceFrames.get(choice);
|
|
1408
|
+
const choiceSnapshot = completion.choices[capturedChoice?.index ?? choice.index]!;
|
|
1409
|
+
const capturedToolCalls = capturedChoice?.tool_calls ?? [];
|
|
1410
|
+
const { delta } = choice;
|
|
1411
|
+
const structuredResponse = isParseableResponseFormat(this.#params?.response_format);
|
|
1412
|
+
const boundedSnapshot = structuredResponse || this.#hasAutoParseableTool;
|
|
1413
|
+
const messageSnapshot = boundedSnapshot
|
|
1414
|
+
? captureStructuredMessageSnapshot(choiceSnapshot)
|
|
1415
|
+
: choiceSnapshot.message;
|
|
1416
|
+
const refusal = boundedSnapshot
|
|
1417
|
+
? captureStructuredJSONSnapshot(messageSnapshot, 'refusal')
|
|
1418
|
+
: messageSnapshot.refusal;
|
|
1419
|
+
const parseableContent = !refusal && structuredResponse;
|
|
1420
|
+
const messageContent = parseableContent
|
|
1421
|
+
? captureStructuredJSONSnapshot(messageSnapshot, 'content')
|
|
1422
|
+
: messageSnapshot.content;
|
|
1423
|
+
|
|
1424
|
+
if (delta?.content != null && messageSnapshot.role === 'assistant' && messageContent) {
|
|
1425
|
+
this._emit('content', delta.content, messageContent);
|
|
214
1426
|
this._emit('content.delta', {
|
|
215
|
-
delta:
|
|
216
|
-
snapshot:
|
|
217
|
-
parsed:
|
|
1427
|
+
delta: delta.content,
|
|
1428
|
+
snapshot: messageContent,
|
|
1429
|
+
parsed: messageSnapshot.parsed,
|
|
218
1430
|
});
|
|
219
1431
|
}
|
|
220
1432
|
|
|
221
|
-
if (
|
|
222
|
-
choice.delta.refusal != null &&
|
|
223
|
-
choiceSnapshot.message?.role === 'assistant' &&
|
|
224
|
-
choiceSnapshot.message?.refusal
|
|
225
|
-
) {
|
|
1433
|
+
if (delta?.refusal != null && messageSnapshot.role === 'assistant' && refusal) {
|
|
226
1434
|
this._emit('refusal.delta', {
|
|
227
|
-
delta:
|
|
228
|
-
snapshot:
|
|
1435
|
+
delta: delta.refusal,
|
|
1436
|
+
snapshot: refusal,
|
|
229
1437
|
});
|
|
230
1438
|
}
|
|
231
1439
|
|
|
232
|
-
if (choice.logprobs?.content != null &&
|
|
1440
|
+
if (choice.logprobs?.content != null && messageSnapshot.role === 'assistant') {
|
|
233
1441
|
this._emit('logprobs.content.delta', {
|
|
234
1442
|
content: choice.logprobs?.content,
|
|
235
1443
|
snapshot: choiceSnapshot.logprobs?.content ?? [],
|
|
236
1444
|
});
|
|
237
1445
|
}
|
|
238
1446
|
|
|
239
|
-
if (choice.logprobs?.refusal != null &&
|
|
1447
|
+
if (choice.logprobs?.refusal != null && messageSnapshot.role === 'assistant') {
|
|
240
1448
|
this._emit('logprobs.refusal.delta', {
|
|
241
1449
|
refusal: choice.logprobs?.refusal,
|
|
242
1450
|
snapshot: choiceSnapshot.logprobs?.refusal ?? [],
|
|
@@ -253,7 +1461,7 @@ export class ChatCompletionStream<ParsedT = null>
|
|
|
253
1461
|
}
|
|
254
1462
|
}
|
|
255
1463
|
|
|
256
|
-
for (const toolCall of
|
|
1464
|
+
for (const toolCall of capturedToolCalls) {
|
|
257
1465
|
if (state.current_tool_call_index !== toolCall.index) {
|
|
258
1466
|
this.#emitContentDoneEvents(choiceSnapshot);
|
|
259
1467
|
|
|
@@ -266,22 +1474,33 @@ export class ChatCompletionStream<ParsedT = null>
|
|
|
266
1474
|
state.current_tool_call_index = toolCall.index;
|
|
267
1475
|
}
|
|
268
1476
|
|
|
269
|
-
for (const toolCallDelta of
|
|
270
|
-
const toolCallSnapshot =
|
|
1477
|
+
for (const toolCallDelta of capturedToolCalls) {
|
|
1478
|
+
const toolCallSnapshot = messageSnapshot.tool_calls?.[toolCallDelta.index];
|
|
271
1479
|
if (!toolCallSnapshot?.type) {
|
|
272
1480
|
continue;
|
|
273
1481
|
}
|
|
274
1482
|
|
|
275
|
-
if (toolCallSnapshot
|
|
1483
|
+
if (toolCallSnapshot.type === 'function') {
|
|
1484
|
+
const boundIdentity = state.tool_call_identities.get(toolCallDelta.index);
|
|
1485
|
+
let argumentsSnapshot: string;
|
|
1486
|
+
if (boundIdentity?.parseable) {
|
|
1487
|
+
const capturedArguments = captureStructuredJSONSnapshot(toolCallSnapshot.function, 'arguments');
|
|
1488
|
+
if (typeof capturedArguments !== 'string') {
|
|
1489
|
+
throw new OpenAIError('Chat completion stream contains an unsafe structured JSON snapshot');
|
|
1490
|
+
}
|
|
1491
|
+
argumentsSnapshot = capturedArguments;
|
|
1492
|
+
} else {
|
|
1493
|
+
argumentsSnapshot = toolCallSnapshot.function.arguments;
|
|
1494
|
+
}
|
|
276
1495
|
this._emit('tool_calls.function.arguments.delta', {
|
|
277
|
-
name: toolCallSnapshot.function
|
|
1496
|
+
name: toolCallSnapshot.function.name,
|
|
278
1497
|
index: toolCallDelta.index,
|
|
279
|
-
arguments:
|
|
1498
|
+
arguments: argumentsSnapshot,
|
|
280
1499
|
parsed_arguments: toolCallSnapshot.function.parsed_arguments,
|
|
281
|
-
arguments_delta: toolCallDelta.
|
|
1500
|
+
arguments_delta: toolCallDelta.arguments_delta,
|
|
282
1501
|
});
|
|
283
|
-
} else {
|
|
284
|
-
assertNever(toolCallSnapshot
|
|
1502
|
+
} else if (toolCallSnapshot.type !== 'custom') {
|
|
1503
|
+
assertNever(toolCallSnapshot);
|
|
285
1504
|
}
|
|
286
1505
|
}
|
|
287
1506
|
}
|
|
@@ -294,51 +1513,108 @@ export class ChatCompletionStream<ParsedT = null>
|
|
|
294
1513
|
return;
|
|
295
1514
|
}
|
|
296
1515
|
|
|
297
|
-
const
|
|
1516
|
+
const messageSnapshot = this.#hasAutoParseableTool
|
|
1517
|
+
? captureStructuredMessageSnapshot(choiceSnapshot)
|
|
1518
|
+
: choiceSnapshot.message;
|
|
1519
|
+
const toolCallSnapshot = messageSnapshot.tool_calls?.[toolCallIndex];
|
|
298
1520
|
if (!toolCallSnapshot) {
|
|
299
1521
|
throw new Error('no tool call snapshot');
|
|
300
1522
|
}
|
|
1523
|
+
const boundIdentity = state.tool_call_identities.get(toolCallIndex);
|
|
1524
|
+
if (boundIdentity) {
|
|
1525
|
+
assertBoundToolCallIdentity(toolCallSnapshot, boundIdentity);
|
|
1526
|
+
}
|
|
1527
|
+
|
|
301
1528
|
if (!toolCallSnapshot.type) {
|
|
302
1529
|
throw new Error('tool call snapshot missing `type`');
|
|
303
1530
|
}
|
|
304
1531
|
|
|
305
1532
|
if (toolCallSnapshot.type === 'function') {
|
|
1533
|
+
// SAFETY: The find predicate verifies the function-tool discriminator before matching its name; the cast carries that refinement through find.
|
|
306
1534
|
const inputTool = this.#params?.tools?.find(
|
|
307
1535
|
(tool) => isChatCompletionFunctionTool(tool) && tool.function.name === toolCallSnapshot.function.name,
|
|
308
1536
|
) as ChatCompletionFunctionTool | undefined; // TS doesn't narrow based on isChatCompletionTool
|
|
309
1537
|
|
|
1538
|
+
// oxlint-disable-next-line anti-slop/no-known-value-widening -- The initial null is replaced with an arbitrary tool-parser result after snapshot validation.
|
|
1539
|
+
let parsedArguments: unknown = null;
|
|
1540
|
+
const parseable = isAutoParsableTool(inputTool) || inputTool?.function.strict === true;
|
|
1541
|
+
let argumentsSnapshot: string;
|
|
1542
|
+
if (parseable) {
|
|
1543
|
+
if (this.#currentChatCompletionSnapshot) {
|
|
1544
|
+
this.#validateStructuredSnapshots(this.#currentChatCompletionSnapshot);
|
|
1545
|
+
}
|
|
1546
|
+
const capturedArguments = captureStructuredJSONSnapshot(toolCallSnapshot.function, 'arguments');
|
|
1547
|
+
if (typeof capturedArguments !== 'string') {
|
|
1548
|
+
throw new OpenAIError('Chat completion stream contains an unsafe structured JSON snapshot');
|
|
1549
|
+
}
|
|
1550
|
+
argumentsSnapshot = capturedArguments;
|
|
1551
|
+
} else {
|
|
1552
|
+
argumentsSnapshot = toolCallSnapshot.function.arguments;
|
|
1553
|
+
}
|
|
1554
|
+
if (isAutoParsableTool(inputTool)) {
|
|
1555
|
+
parsedArguments = inputTool.$parseRaw(validateStructuredJSONSnapshot(argumentsSnapshot));
|
|
1556
|
+
} else if (inputTool?.function.strict) {
|
|
1557
|
+
parsedArguments = parseResponseFormatContent(
|
|
1558
|
+
{ type: 'json_schema', $parseRaw: undefined },
|
|
1559
|
+
validateStructuredJSONSnapshot(argumentsSnapshot),
|
|
1560
|
+
);
|
|
1561
|
+
}
|
|
1562
|
+
|
|
1563
|
+
if (choiceSnapshot.finish_reason) {
|
|
1564
|
+
state.done_tool_calls.add(toolCallIndex);
|
|
1565
|
+
}
|
|
310
1566
|
this._emit('tool_calls.function.arguments.done', {
|
|
311
1567
|
name: toolCallSnapshot.function.name,
|
|
312
1568
|
index: toolCallIndex,
|
|
313
|
-
arguments:
|
|
314
|
-
parsed_arguments:
|
|
315
|
-
isAutoParsableTool(inputTool) ? inputTool.$parseRaw(toolCallSnapshot.function.arguments)
|
|
316
|
-
: inputTool?.function.strict ? JSON.parse(toolCallSnapshot.function.arguments)
|
|
317
|
-
: null,
|
|
1569
|
+
arguments: argumentsSnapshot,
|
|
1570
|
+
parsed_arguments: parsedArguments,
|
|
318
1571
|
});
|
|
319
|
-
} else {
|
|
320
|
-
assertNever(toolCallSnapshot
|
|
1572
|
+
} else if (toolCallSnapshot.type !== 'custom') {
|
|
1573
|
+
assertNever(toolCallSnapshot);
|
|
321
1574
|
}
|
|
322
1575
|
}
|
|
323
1576
|
|
|
324
1577
|
#emitContentDoneEvents(choiceSnapshot: ChatCompletionSnapshot.Choice) {
|
|
325
1578
|
const state = this.#getChoiceEventState(choiceSnapshot);
|
|
326
|
-
|
|
327
|
-
|
|
1579
|
+
const structuredResponse = isParseableResponseFormat(this.#params?.response_format);
|
|
1580
|
+
const boundedSnapshot = structuredResponse || this.#hasAutoParseableTool;
|
|
1581
|
+
const messageSnapshot = boundedSnapshot
|
|
1582
|
+
? captureStructuredMessageSnapshot(choiceSnapshot)
|
|
1583
|
+
: choiceSnapshot.message;
|
|
1584
|
+
const refusal = boundedSnapshot
|
|
1585
|
+
? captureStructuredJSONSnapshot(messageSnapshot, 'refusal')
|
|
1586
|
+
: messageSnapshot.refusal;
|
|
1587
|
+
const parseableContent = !refusal && structuredResponse;
|
|
1588
|
+
const content = parseableContent
|
|
1589
|
+
? captureStructuredJSONSnapshot(messageSnapshot, 'content')
|
|
1590
|
+
: messageSnapshot.content;
|
|
1591
|
+
|
|
1592
|
+
if (
|
|
1593
|
+
content != null &&
|
|
1594
|
+
(content !== '' ||
|
|
1595
|
+
(!refusal && !messageSnapshot.tool_calls?.length && !messageSnapshot.function_call)) &&
|
|
1596
|
+
!state.content_done
|
|
1597
|
+
) {
|
|
1598
|
+
if (parseableContent && this.#currentChatCompletionSnapshot) {
|
|
1599
|
+
this.#validateStructuredSnapshots(this.#currentChatCompletionSnapshot);
|
|
1600
|
+
}
|
|
328
1601
|
state.content_done = true;
|
|
329
1602
|
|
|
330
|
-
const responseFormat = this.#getAutoParseableResponseFormat();
|
|
331
|
-
|
|
332
1603
|
this._emit('content.done', {
|
|
333
|
-
content
|
|
334
|
-
parsed:
|
|
1604
|
+
content,
|
|
1605
|
+
parsed: refusal
|
|
1606
|
+
? null
|
|
1607
|
+
: parseResponseFormatContent<ParsedT>(
|
|
1608
|
+
this.#params?.response_format,
|
|
1609
|
+
parseableContent ? validateStructuredJSONSnapshot(content) : content,
|
|
1610
|
+
),
|
|
335
1611
|
});
|
|
336
1612
|
}
|
|
337
1613
|
|
|
338
|
-
if (
|
|
1614
|
+
if (refusal && !state.refusal_done) {
|
|
339
1615
|
state.refusal_done = true;
|
|
340
1616
|
|
|
341
|
-
this._emit('refusal.done', { refusal
|
|
1617
|
+
this._emit('refusal.done', { refusal });
|
|
342
1618
|
}
|
|
343
1619
|
|
|
344
1620
|
if (choiceSnapshot.logprobs?.content && !state.logprobs_content_done) {
|
|
@@ -354,6 +1630,120 @@ export class ChatCompletionStream<ParsedT = null>
|
|
|
354
1630
|
}
|
|
355
1631
|
}
|
|
356
1632
|
|
|
1633
|
+
#validateStructuredSnapshots(
|
|
1634
|
+
snapshot: ChatCompletionSnapshot,
|
|
1635
|
+
): WeakMap<ChatCompletionSnapshot.Choice, ValidatedChoiceSnapshot> {
|
|
1636
|
+
const finalJSONBudget: PartialJSONParseBudget = { bytes: 0, fragments: 0, work: 0 };
|
|
1637
|
+
const parseableContent = isParseableResponseFormat(this.#params?.response_format);
|
|
1638
|
+
const validatedMessages = new WeakMap<ChatCompletionSnapshot.Choice, ValidatedChoiceSnapshot>();
|
|
1639
|
+
const choices = captureSnapshotArray<ChatCompletionSnapshot.Choice>(
|
|
1640
|
+
snapshot,
|
|
1641
|
+
'choices',
|
|
1642
|
+
MAX_STREAM_CHOICES,
|
|
1643
|
+
'choice',
|
|
1644
|
+
);
|
|
1645
|
+
if (!choices) {
|
|
1646
|
+
throw new OpenAIError('Chat completion stream contains an unsafe snapshot choice collection');
|
|
1647
|
+
}
|
|
1648
|
+
for (let choiceIndex = 0; choiceIndex < choices.length; choiceIndex += 1) {
|
|
1649
|
+
const choice = captureSnapshotArrayItem(choices, choiceIndex);
|
|
1650
|
+
if (!choice) {
|
|
1651
|
+
continue;
|
|
1652
|
+
}
|
|
1653
|
+
const message = captureStructuredMessageSnapshot(choice);
|
|
1654
|
+
const refusal = captureStructuredJSONSnapshot(message, 'refusal');
|
|
1655
|
+
const content = captureStructuredJSONSnapshot(message, 'content');
|
|
1656
|
+
const validatedTools = new Map<number, ValidatedToolCallSnapshot>();
|
|
1657
|
+
const toolCalls = captureSnapshotArray<ChatCompletionSnapshot.Choice.Message.ToolCall>(
|
|
1658
|
+
message,
|
|
1659
|
+
'tool_calls',
|
|
1660
|
+
MAX_STREAM_TOOL_CALLS,
|
|
1661
|
+
'tool-call',
|
|
1662
|
+
);
|
|
1663
|
+
validatedMessages.set(
|
|
1664
|
+
choice,
|
|
1665
|
+
Object.freeze({
|
|
1666
|
+
message,
|
|
1667
|
+
content,
|
|
1668
|
+
refusal,
|
|
1669
|
+
toolCallCollection: toolCalls,
|
|
1670
|
+
toolCalls: validatedTools,
|
|
1671
|
+
}),
|
|
1672
|
+
);
|
|
1673
|
+
const state = this.#choiceEventStates[choice.index];
|
|
1674
|
+
if (parseableContent && !refusal && typeof content === 'string') {
|
|
1675
|
+
validateStructuredJSONSnapshot(content, finalJSONBudget, this.#partialJSONParseBudget);
|
|
1676
|
+
}
|
|
1677
|
+
for (const [index, identity] of state?.tool_call_identities ?? []) {
|
|
1678
|
+
const toolCall = toolCalls && captureSnapshotArrayItem(toolCalls, index);
|
|
1679
|
+
if (!toolCall) {
|
|
1680
|
+
throw new OpenAIError('Chat completion stream contains a changed tool call identity');
|
|
1681
|
+
}
|
|
1682
|
+
assertBoundToolCallIdentity(toolCall, identity);
|
|
1683
|
+
}
|
|
1684
|
+
if (!this.#hasAutoParseableTool) {
|
|
1685
|
+
continue;
|
|
1686
|
+
}
|
|
1687
|
+
for (let toolCallIndex = 0; toolCallIndex < (toolCalls?.length ?? 0); toolCallIndex += 1) {
|
|
1688
|
+
const toolCall = captureSnapshotArrayItem(toolCalls!, toolCallIndex);
|
|
1689
|
+
if (!toolCall) {
|
|
1690
|
+
continue;
|
|
1691
|
+
}
|
|
1692
|
+
const identity = ownFunctionToolIdentity(toolCall);
|
|
1693
|
+
if (!identity) {
|
|
1694
|
+
const type = Object.getOwnPropertyDescriptor(toolCall, 'type');
|
|
1695
|
+
if (type && !('value' in type)) {
|
|
1696
|
+
throw new OpenAIError('Chat completion stream contains an unsafe structured JSON snapshot');
|
|
1697
|
+
}
|
|
1698
|
+
if (type?.value !== 'function') {
|
|
1699
|
+
continue;
|
|
1700
|
+
}
|
|
1701
|
+
const fn = Object.getOwnPropertyDescriptor(toolCall, 'function');
|
|
1702
|
+
if (fn && !('value' in fn)) {
|
|
1703
|
+
throw new OpenAIError('Chat completion stream contains an unsafe structured JSON snapshot');
|
|
1704
|
+
}
|
|
1705
|
+
if (fn && typeof fn.value === 'object' && fn.value !== null) {
|
|
1706
|
+
const name = Object.getOwnPropertyDescriptor(fn.value, 'name');
|
|
1707
|
+
if (name && !('value' in name)) {
|
|
1708
|
+
throw new OpenAIError('Chat completion stream contains an unsafe structured JSON snapshot');
|
|
1709
|
+
}
|
|
1710
|
+
}
|
|
1711
|
+
continue;
|
|
1712
|
+
}
|
|
1713
|
+
if (
|
|
1714
|
+
!shouldParseToolCall(this.#params, {
|
|
1715
|
+
type: identity.type,
|
|
1716
|
+
function: { name: identity.name },
|
|
1717
|
+
})
|
|
1718
|
+
) {
|
|
1719
|
+
continue;
|
|
1720
|
+
}
|
|
1721
|
+
const descriptor = Object.getOwnPropertyDescriptor(toolCall, 'function');
|
|
1722
|
+
if (!descriptor || !('value' in descriptor)) {
|
|
1723
|
+
throw new OpenAIError('Chat completion stream contains an unsafe structured JSON snapshot');
|
|
1724
|
+
}
|
|
1725
|
+
// SAFETY: The tool-call function is captured through an own data descriptor; its fields are subsequently checked by the structured snapshot reader.
|
|
1726
|
+
const fn = descriptor.value as ChatCompletionSnapshot.Choice.Message.ToolCall.Function;
|
|
1727
|
+
const argumentsSnapshot = captureStructuredJSONSnapshot(fn, 'arguments');
|
|
1728
|
+
if (typeof argumentsSnapshot !== 'string') {
|
|
1729
|
+
throw new OpenAIError('Chat completion stream contains an unsafe structured JSON snapshot');
|
|
1730
|
+
}
|
|
1731
|
+
validateStructuredJSONSnapshot(argumentsSnapshot, finalJSONBudget, this.#partialJSONParseBudget);
|
|
1732
|
+
validatedTools.set(
|
|
1733
|
+
toolCallIndex,
|
|
1734
|
+
Object.freeze({
|
|
1735
|
+
tool: toolCall,
|
|
1736
|
+
function: fn,
|
|
1737
|
+
type: identity.type,
|
|
1738
|
+
name: identity.name,
|
|
1739
|
+
arguments: argumentsSnapshot,
|
|
1740
|
+
}),
|
|
1741
|
+
);
|
|
1742
|
+
}
|
|
1743
|
+
}
|
|
1744
|
+
return validatedMessages;
|
|
1745
|
+
}
|
|
1746
|
+
|
|
357
1747
|
#endRequest(): ParsedChatCompletion<ParsedT> {
|
|
358
1748
|
if (this.ended) {
|
|
359
1749
|
throw new OpenAIError(`stream has ended, this shouldn't happen`);
|
|
@@ -362,9 +1752,27 @@ export class ChatCompletionStream<ParsedT = null>
|
|
|
362
1752
|
if (!snapshot) {
|
|
363
1753
|
throw new OpenAIError(`request ended without sending any chunks`);
|
|
364
1754
|
}
|
|
1755
|
+
const validatedMessages = this.#validateStructuredSnapshots(snapshot);
|
|
1756
|
+
const audioDoneChoiceIndexes = this.#audioDoneChoiceIndexes;
|
|
1757
|
+
this.#audioDoneChoiceIndexes = new Set();
|
|
365
1758
|
this.#currentChatCompletionSnapshot = undefined;
|
|
366
1759
|
this.#choiceEventStates = [];
|
|
367
|
-
return finalizeChatCompletion(snapshot, this.#params);
|
|
1760
|
+
return finalizeChatCompletion(snapshot, this.#params, audioDoneChoiceIndexes, validatedMessages);
|
|
1761
|
+
}
|
|
1762
|
+
|
|
1763
|
+
/** Rejects unfinished turns before tool callbacks while preserving ordinary stream and replay behavior. */
|
|
1764
|
+
protected override _runTools<FunctionsArgs extends BaseFunctionsArgs, ToolContext>(
|
|
1765
|
+
client: OpenAI,
|
|
1766
|
+
params:
|
|
1767
|
+
| ChatCompletionToolRunnerParamsWithContext<FunctionsArgs, ToolContext>
|
|
1768
|
+
| ChatCompletionToolRunnerParamsWithoutContext<FunctionsArgs>
|
|
1769
|
+
| ChatCompletionStreamingToolRunnerParamsWithContext<FunctionsArgs, ToolContext>
|
|
1770
|
+
| ChatCompletionStreamingToolRunnerParamsWithoutContext<FunctionsArgs>,
|
|
1771
|
+
runner: ChatCompletionRunner<any> | ChatCompletionStreamingRunner<any>,
|
|
1772
|
+
options?: RunnerOptions,
|
|
1773
|
+
): Promise<void> {
|
|
1774
|
+
this.#rejectsUnfinishedTurns = true;
|
|
1775
|
+
return super._runTools(client, params, runner, options);
|
|
368
1776
|
}
|
|
369
1777
|
|
|
370
1778
|
protected override async _createChatCompletion(
|
|
@@ -372,24 +1780,42 @@ export class ChatCompletionStream<ParsedT = null>
|
|
|
372
1780
|
params: ChatCompletionCreateParams,
|
|
373
1781
|
options?: RequestOptions,
|
|
374
1782
|
): Promise<ParsedChatCompletion<ParsedT>> {
|
|
375
|
-
|
|
376
|
-
const
|
|
377
|
-
|
|
378
|
-
if (signal.aborted) this.controller.abort();
|
|
379
|
-
signal.addEventListener('abort', () => this.controller.abort());
|
|
380
|
-
}
|
|
1783
|
+
this._listenForAbort(options?.signal);
|
|
1784
|
+
const requestParams = { ...params, stream: true as const };
|
|
1785
|
+
this.#params = requestParams;
|
|
381
1786
|
this.#beginRequest();
|
|
382
1787
|
|
|
383
|
-
const
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
1788
|
+
const parserParams = snapshotChatCompletionParserParams(requestParams);
|
|
1789
|
+
this.#params = parserParams;
|
|
1790
|
+
this.#hasAutoParseableTool =
|
|
1791
|
+
parserParams.tools?.some(
|
|
1792
|
+
(tool) =>
|
|
1793
|
+
isChatCompletionFunctionTool(tool) && (isAutoParsableTool(tool) || tool.function.strict === true),
|
|
1794
|
+
) ?? false;
|
|
1795
|
+
const stopObserving =
|
|
1796
|
+
requestParams.tools || requestParams.response_format
|
|
1797
|
+
? observeSerializedChatCompletionParserParams(requestParams, parserParams, (serialized) => {
|
|
1798
|
+
this.#params = serialized;
|
|
1799
|
+
this.#hasAutoParseableTool =
|
|
1800
|
+
serialized.tools?.some(
|
|
1801
|
+
(tool) =>
|
|
1802
|
+
isChatCompletionFunctionTool(tool) &&
|
|
1803
|
+
(isAutoParsableTool(tool) || tool.function.strict === true),
|
|
1804
|
+
) ?? false;
|
|
1805
|
+
})
|
|
1806
|
+
: undefined;
|
|
1807
|
+
const stream = await client.chat.completions
|
|
1808
|
+
.create(requestParams, {
|
|
1809
|
+
...options,
|
|
1810
|
+
signal: this.controller.signal,
|
|
1811
|
+
})
|
|
1812
|
+
.finally(stopObserving);
|
|
387
1813
|
this._connected();
|
|
388
1814
|
for await (const chunk of stream) {
|
|
389
1815
|
this.#addChunk(chunk);
|
|
390
1816
|
}
|
|
391
1817
|
if (stream.controller.signal?.aborted) {
|
|
392
|
-
throw
|
|
1818
|
+
throw this._userAbortError();
|
|
393
1819
|
}
|
|
394
1820
|
return this._addChatCompletion(this.#endRequest());
|
|
395
1821
|
}
|
|
@@ -398,64 +1824,110 @@ export class ChatCompletionStream<ParsedT = null>
|
|
|
398
1824
|
readableStream: ReadableStream,
|
|
399
1825
|
options?: RequestOptions,
|
|
400
1826
|
): Promise<ChatCompletion> {
|
|
401
|
-
|
|
402
|
-
if (signal) {
|
|
403
|
-
if (signal.aborted) this.controller.abort();
|
|
404
|
-
signal.addEventListener('abort', () => this.controller.abort());
|
|
405
|
-
}
|
|
1827
|
+
this._listenForAbort(options?.signal);
|
|
406
1828
|
this.#beginRequest();
|
|
407
1829
|
this._connected();
|
|
408
|
-
const stream = Stream.fromReadableStream<
|
|
1830
|
+
const stream = Stream.fromReadableStream<ChatCompletionReadableStreamItem>(
|
|
1831
|
+
readableStream,
|
|
1832
|
+
this.controller,
|
|
1833
|
+
);
|
|
409
1834
|
let chatId;
|
|
410
|
-
for await (const
|
|
411
|
-
if (
|
|
1835
|
+
for await (const item of stream) {
|
|
1836
|
+
if ('error' in item && hasOwn(item, 'error') && typeof item.error === 'object' && item.error !== null) {
|
|
1837
|
+
throw new APIError(undefined, item.error, undefined, undefined);
|
|
1838
|
+
}
|
|
1839
|
+
|
|
1840
|
+
if (isChatCompletionReadableStreamMessage(item)) {
|
|
1841
|
+
const message = getChatCompletionReadableStreamMessage(item);
|
|
1842
|
+
if (this.#currentChatCompletionSnapshot) {
|
|
1843
|
+
const toolCalls = this.#currentChatCompletionSnapshot.choices[0]?.message.tool_calls;
|
|
1844
|
+
for (const [index, id] of message.tool_call_ids?.entries() ?? []) {
|
|
1845
|
+
const toolCall = toolCalls?.[index];
|
|
1846
|
+
if (toolCall && id) {
|
|
1847
|
+
toolCall.id = id;
|
|
1848
|
+
}
|
|
1849
|
+
}
|
|
1850
|
+
|
|
1851
|
+
this._addChatCompletion(this.#endRequest());
|
|
1852
|
+
chatId = undefined;
|
|
1853
|
+
}
|
|
1854
|
+
this._addMessage(message.message);
|
|
1855
|
+
continue;
|
|
1856
|
+
}
|
|
1857
|
+
|
|
1858
|
+
const chunk = item;
|
|
1859
|
+
|
|
1860
|
+
if (chatId && chunk.id && chatId !== chunk.id) {
|
|
412
1861
|
// A new request has been made.
|
|
413
1862
|
this._addChatCompletion(this.#endRequest());
|
|
414
1863
|
}
|
|
415
1864
|
|
|
416
1865
|
this.#addChunk(chunk);
|
|
417
|
-
|
|
1866
|
+
if (chunk.id) {
|
|
1867
|
+
chatId = chunk.id;
|
|
1868
|
+
}
|
|
418
1869
|
}
|
|
419
1870
|
if (stream.controller.signal?.aborted) {
|
|
420
|
-
throw
|
|
1871
|
+
throw this._userAbortError();
|
|
421
1872
|
}
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
#getAutoParseableResponseFormat(): AutoParseableResponseFormat<ParsedT> | null {
|
|
426
|
-
const responseFormat = this.#params?.response_format;
|
|
427
|
-
if (isAutoParsableResponseFormat<ParsedT>(responseFormat)) {
|
|
428
|
-
return responseFormat;
|
|
1873
|
+
if (this.#currentChatCompletionSnapshot) {
|
|
1874
|
+
return this._addChatCompletion(this.#endRequest());
|
|
429
1875
|
}
|
|
430
|
-
|
|
431
|
-
|
|
1876
|
+
const lastChatCompletion = this._chatCompletions[this._chatCompletions.length - 1];
|
|
1877
|
+
if (lastChatCompletion) {
|
|
1878
|
+
return lastChatCompletion;
|
|
1879
|
+
}
|
|
1880
|
+
throw new OpenAIError(`request ended without sending any chunks`);
|
|
432
1881
|
}
|
|
433
1882
|
|
|
434
|
-
#accumulateChatCompletion(
|
|
1883
|
+
#accumulateChatCompletion(
|
|
1884
|
+
chunk: ChatCompletionChunk,
|
|
1885
|
+
capturedChoiceFrames: WeakMap<object, CapturedChoiceToolCallFrames>,
|
|
1886
|
+
): ChatCompletionSnapshot {
|
|
435
1887
|
let snapshot = this.#currentChatCompletionSnapshot;
|
|
436
|
-
const { choices, ...rest } = chunk;
|
|
1888
|
+
const { choices, obfuscation: _obfuscation, ...rest } = chunk;
|
|
437
1889
|
if (!snapshot) {
|
|
438
|
-
|
|
1890
|
+
const newSnapshot: ChatCompletionSnapshot = {
|
|
439
1891
|
...rest,
|
|
440
1892
|
choices: [],
|
|
441
1893
|
};
|
|
442
|
-
|
|
443
|
-
|
|
1894
|
+
this.#currentChatCompletionSnapshot = newSnapshot;
|
|
1895
|
+
snapshot = newSnapshot;
|
|
1896
|
+
} else if (chunk.id) {
|
|
1897
|
+
assignOwnProperties(snapshot, rest);
|
|
444
1898
|
}
|
|
445
1899
|
|
|
446
|
-
|
|
1900
|
+
const requestedChoiceCount = this.#params?.n;
|
|
1901
|
+
const maxChoices =
|
|
1902
|
+
typeof requestedChoiceCount === 'number' &&
|
|
1903
|
+
Number.isSafeInteger(requestedChoiceCount) &&
|
|
1904
|
+
requestedChoiceCount > 0
|
|
1905
|
+
? Math.min(requestedChoiceCount, MAX_STREAM_CHOICES)
|
|
1906
|
+
: MAX_STREAM_CHOICES;
|
|
1907
|
+
|
|
1908
|
+
for (const chunkChoice of chunk.choices) {
|
|
1909
|
+
const { delta, finish_reason, index, logprobs = null, ...other } = chunkChoice;
|
|
1910
|
+
const capturedToolCalls: CapturedToolCallDeltaFrame[] = [];
|
|
1911
|
+
capturedChoiceFrames.set(chunkChoice, Object.freeze({ index, tool_calls: capturedToolCalls }));
|
|
1912
|
+
if (!Number.isSafeInteger(index) || index < 0 || index >= maxChoices) {
|
|
1913
|
+
throw new OpenAIError(`Chat completion stream contains an invalid choice index: ${index}`);
|
|
1914
|
+
}
|
|
1915
|
+
|
|
447
1916
|
let choice = snapshot.choices[index];
|
|
448
1917
|
if (!choice) {
|
|
449
|
-
|
|
1918
|
+
const newChoice = { finish_reason, index, message: {}, logprobs: null, ...other };
|
|
1919
|
+
snapshot.choices[index] = newChoice;
|
|
1920
|
+
choice = newChoice;
|
|
1921
|
+
}
|
|
1922
|
+
if (isParseableResponseFormat(this.#params?.response_format) || this.#hasAutoParseableTool) {
|
|
1923
|
+
captureStructuredJSONSnapshot(captureStructuredMessageSnapshot(choice), 'refusal');
|
|
450
1924
|
}
|
|
451
1925
|
|
|
452
1926
|
if (logprobs) {
|
|
453
|
-
if (
|
|
454
|
-
choice.logprobs = Object.assign({}, logprobs);
|
|
455
|
-
} else {
|
|
1927
|
+
if (choice.logprobs) {
|
|
456
1928
|
const { content, refusal, ...rest } = logprobs;
|
|
457
1929
|
assertIsEmpty(rest);
|
|
458
|
-
|
|
1930
|
+
assignOwnProperties(choice.logprobs, rest);
|
|
459
1931
|
|
|
460
1932
|
if (content) {
|
|
461
1933
|
choice.logprobs.content ??= [];
|
|
@@ -466,13 +1938,23 @@ export class ChatCompletionStream<ParsedT = null>
|
|
|
466
1938
|
choice.logprobs.refusal ??= [];
|
|
467
1939
|
choice.logprobs.refusal.push(...refusal);
|
|
468
1940
|
}
|
|
1941
|
+
} else {
|
|
1942
|
+
choice.logprobs = { ...logprobs };
|
|
1943
|
+
|
|
1944
|
+
if (logprobs.content) {
|
|
1945
|
+
choice.logprobs.content = [...logprobs.content];
|
|
1946
|
+
}
|
|
1947
|
+
|
|
1948
|
+
if (logprobs.refusal) {
|
|
1949
|
+
choice.logprobs.refusal = [...logprobs.refusal];
|
|
1950
|
+
}
|
|
469
1951
|
}
|
|
470
1952
|
}
|
|
471
1953
|
|
|
472
1954
|
if (finish_reason) {
|
|
473
1955
|
choice.finish_reason = finish_reason;
|
|
474
1956
|
|
|
475
|
-
if (this.#params && hasAutoParseableInput(this.#params)) {
|
|
1957
|
+
if (this.#params && (this.#rejectsUnfinishedTurns || hasAutoParseableInput(this.#params))) {
|
|
476
1958
|
if (finish_reason === 'length') {
|
|
477
1959
|
throw new LengthFinishReasonError();
|
|
478
1960
|
}
|
|
@@ -483,123 +1965,233 @@ export class ChatCompletionStream<ParsedT = null>
|
|
|
483
1965
|
}
|
|
484
1966
|
}
|
|
485
1967
|
|
|
486
|
-
|
|
1968
|
+
assignOwnProperties(choice, other);
|
|
487
1969
|
|
|
488
|
-
if (!delta)
|
|
1970
|
+
if (!delta) {
|
|
1971
|
+
Object.freeze(capturedToolCalls);
|
|
1972
|
+
continue;
|
|
1973
|
+
} // Shouldn't happen; just in case.
|
|
489
1974
|
|
|
490
|
-
|
|
1975
|
+
this.#audioDoneChoiceIndexes.delete(index);
|
|
1976
|
+
// SAFETY: Streaming audio fields arrive incrementally even though the generated delta type omits them; each present field is merged below.
|
|
1977
|
+
const { audio, content, refusal, function_call, role, ...capturedDeltaFields } =
|
|
1978
|
+
delta as typeof delta & {
|
|
1979
|
+
audio?: Partial<ChatCompletionAudio> | null;
|
|
1980
|
+
};
|
|
1981
|
+
const { tool_calls: capturedToolCallDelta, ...rest } = capturedDeltaFields;
|
|
1982
|
+
const tool_calls = hasOwn(capturedDeltaFields, 'tool_calls') ? capturedToolCallDelta : delta.tool_calls;
|
|
491
1983
|
assertIsEmpty(rest);
|
|
492
|
-
|
|
1984
|
+
assignOwnProperties(choice.message, rest);
|
|
1985
|
+
if (
|
|
1986
|
+
audio?.expires_at != null &&
|
|
1987
|
+
audio.id == null &&
|
|
1988
|
+
audio.data == null &&
|
|
1989
|
+
audio.transcript == null &&
|
|
1990
|
+
content == null &&
|
|
1991
|
+
refusal == null &&
|
|
1992
|
+
function_call == null &&
|
|
1993
|
+
role == null &&
|
|
1994
|
+
tool_calls == null &&
|
|
1995
|
+
Object.keys(rest).length === 0
|
|
1996
|
+
) {
|
|
1997
|
+
this.#audioDoneChoiceIndexes.add(index);
|
|
1998
|
+
}
|
|
493
1999
|
|
|
494
2000
|
if (refusal) {
|
|
495
2001
|
choice.message.refusal = (choice.message.refusal || '') + refusal;
|
|
496
2002
|
}
|
|
497
2003
|
|
|
498
|
-
if (role)
|
|
2004
|
+
if (role) {
|
|
2005
|
+
choice.message.role = role;
|
|
2006
|
+
}
|
|
2007
|
+
if (audio) {
|
|
2008
|
+
const audioSnapshot = (choice.message.audio ??= {});
|
|
2009
|
+
if (audio.id != null) {
|
|
2010
|
+
audioSnapshot.id = audio.id;
|
|
2011
|
+
}
|
|
2012
|
+
if (audio.data != null) {
|
|
2013
|
+
audioSnapshot.data = (audioSnapshot.data ?? '') + audio.data;
|
|
2014
|
+
}
|
|
2015
|
+
if (audio.transcript != null) {
|
|
2016
|
+
audioSnapshot.transcript = (audioSnapshot.transcript ?? '') + audio.transcript;
|
|
2017
|
+
}
|
|
2018
|
+
if (audio.expires_at != null) {
|
|
2019
|
+
audioSnapshot.expires_at = audio.expires_at;
|
|
2020
|
+
}
|
|
2021
|
+
}
|
|
499
2022
|
if (function_call) {
|
|
500
|
-
if (
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
2023
|
+
if (choice.message.function_call) {
|
|
2024
|
+
if (function_call.name) {
|
|
2025
|
+
choice.message.function_call.name = function_call.name;
|
|
2026
|
+
}
|
|
504
2027
|
if (function_call.arguments) {
|
|
505
2028
|
choice.message.function_call.arguments ??= '';
|
|
506
2029
|
choice.message.function_call.arguments += function_call.arguments;
|
|
507
2030
|
}
|
|
2031
|
+
} else {
|
|
2032
|
+
choice.message.function_call = function_call;
|
|
508
2033
|
}
|
|
509
2034
|
}
|
|
510
|
-
if (content) {
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
2035
|
+
if (content != null) {
|
|
2036
|
+
if (!choice.message.refusal && isParseableResponseFormat(this.#params?.response_format)) {
|
|
2037
|
+
const eventState = this.#getChoiceEventState(choice);
|
|
2038
|
+
const parseState = (eventState.content_parse_state ??= createPartialJSONParseState());
|
|
2039
|
+
const shouldParse = recordPartialJSONFragment(parseState, this.#partialJSONParseBudget, content);
|
|
2040
|
+
choice.message.content = (captureStructuredJSONSnapshot(choice.message, 'content') || '') + content;
|
|
2041
|
+
|
|
2042
|
+
// The partial parser does not accept whitespace-only input. Once output
|
|
2043
|
+
// grows, coalesce prefix reparses while preserving every raw snapshot.
|
|
2044
|
+
if (!parseState.has_non_whitespace) {
|
|
2045
|
+
choice.message.parsed = null;
|
|
2046
|
+
} else if (shouldParse && reservePartialJSONParse(parseState, this.#partialJSONParseBudget)) {
|
|
2047
|
+
this.#validateStructuredSnapshots(snapshot);
|
|
2048
|
+
choice.message.parsed = parseStructuredStreamingJSON(
|
|
2049
|
+
validateStructuredJSONSnapshot(choice.message.content),
|
|
2050
|
+
);
|
|
2051
|
+
} else if (content.length > 0) {
|
|
2052
|
+
choice.message.parsed = null;
|
|
2053
|
+
}
|
|
2054
|
+
} else {
|
|
2055
|
+
choice.message.content = (choice.message.content || '') + content;
|
|
515
2056
|
}
|
|
516
2057
|
}
|
|
517
2058
|
|
|
518
2059
|
if (tool_calls) {
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
if (
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
2060
|
+
// Tool calls are built up across chunks, so while the stream is in progress the
|
|
2061
|
+
// entries are only partially filled in; they match `ChatCompletionSnapshot.Choice.Message.ToolCall`
|
|
2062
|
+
// once every delta for them has been accumulated.
|
|
2063
|
+
// SAFETY: This SDK-owned collection holds partial tool calls during accumulation; finalization checks required fields before exposing completed calls.
|
|
2064
|
+
const toolCallSnapshots = (choice.message.tool_calls ??= []) as PartialToolCallSnapshot[];
|
|
2065
|
+
|
|
2066
|
+
for (const toolCallDelta of tool_calls) {
|
|
2067
|
+
const { index, id, type, function: fn, custom, ...rest } = toolCallDelta;
|
|
2068
|
+
if (!Number.isSafeInteger(index) || index < 0 || index >= MAX_STREAM_TOOL_CALLS) {
|
|
2069
|
+
throw new OpenAIError(`Chat completion stream contains an invalid tool call index: ${index}`);
|
|
2070
|
+
}
|
|
2071
|
+
let argumentsDelta = '';
|
|
2072
|
+
|
|
2073
|
+
const tool_call = (toolCallSnapshots[index] ??= {});
|
|
2074
|
+
const functionName = fn?.name;
|
|
2075
|
+
const eventState = this.#hasAutoParseableTool ? this.#getChoiceEventState(choice) : undefined;
|
|
2076
|
+
let boundIdentity = eventState?.tool_call_identities.get(index);
|
|
2077
|
+
if (boundIdentity) {
|
|
2078
|
+
assertBoundToolCallIdentity(tool_call, boundIdentity);
|
|
2079
|
+
if (
|
|
2080
|
+
(type !== undefined && type !== boundIdentity.type) ||
|
|
2081
|
+
(functionName !== undefined && functionName !== boundIdentity.name)
|
|
2082
|
+
) {
|
|
2083
|
+
throw new OpenAIError('Chat completion stream contains a changed tool call identity');
|
|
534
2084
|
}
|
|
535
2085
|
}
|
|
2086
|
+
|
|
2087
|
+
assignOwnProperties(tool_call, rest);
|
|
2088
|
+
if (id) {
|
|
2089
|
+
tool_call.id = id;
|
|
2090
|
+
}
|
|
2091
|
+
if (type) {
|
|
2092
|
+
tool_call.type = type;
|
|
2093
|
+
}
|
|
2094
|
+
if (custom) {
|
|
2095
|
+
const customSnapshot = (tool_call.custom ??= { name: custom.name ?? '', input: '' });
|
|
2096
|
+
if (custom.name) {
|
|
2097
|
+
customSnapshot.name = custom.name;
|
|
2098
|
+
}
|
|
2099
|
+
if (custom.input) {
|
|
2100
|
+
customSnapshot.input += custom.input;
|
|
2101
|
+
}
|
|
2102
|
+
}
|
|
2103
|
+
if (fn) {
|
|
2104
|
+
const functionSnapshot = (tool_call.function ??= { name: functionName ?? '', arguments: '' });
|
|
2105
|
+
if (functionName) {
|
|
2106
|
+
functionSnapshot.name = functionName;
|
|
2107
|
+
}
|
|
2108
|
+
if (eventState && !boundIdentity) {
|
|
2109
|
+
const identity = ownFunctionToolIdentity(tool_call);
|
|
2110
|
+
const configuredTool =
|
|
2111
|
+
identity &&
|
|
2112
|
+
this.#params?.tools?.find(
|
|
2113
|
+
(tool) => isChatCompletionFunctionTool(tool) && tool.function.name === identity.name,
|
|
2114
|
+
);
|
|
2115
|
+
if (identity) {
|
|
2116
|
+
boundIdentity = {
|
|
2117
|
+
...identity,
|
|
2118
|
+
parseable:
|
|
2119
|
+
configuredTool !== undefined &&
|
|
2120
|
+
shouldParseToolCall(this.#params, {
|
|
2121
|
+
type: identity.type,
|
|
2122
|
+
function: { name: identity.name },
|
|
2123
|
+
}),
|
|
2124
|
+
};
|
|
2125
|
+
eventState.tool_call_identities.set(index, boundIdentity);
|
|
2126
|
+
if (!boundIdentity.parseable) {
|
|
2127
|
+
const provisionalState = eventState.tool_call_parse_states.get(index);
|
|
2128
|
+
if (provisionalState) {
|
|
2129
|
+
this.#partialJSONParseBudget.bytes -= provisionalState.bytes;
|
|
2130
|
+
this.#partialJSONParseBudget.fragments -= provisionalState.fragments;
|
|
2131
|
+
this.#partialJSONParseBudget.work -= provisionalState.work;
|
|
2132
|
+
eventState.tool_call_parse_states.delete(index);
|
|
2133
|
+
}
|
|
2134
|
+
}
|
|
2135
|
+
}
|
|
2136
|
+
}
|
|
2137
|
+
const argumentFragment = fn.arguments;
|
|
2138
|
+
if (argumentFragment != null) {
|
|
2139
|
+
argumentsDelta = argumentFragment;
|
|
2140
|
+
if (eventState && boundIdentity?.parseable !== false) {
|
|
2141
|
+
let parseState = eventState.tool_call_parse_states.get(index);
|
|
2142
|
+
if (!parseState) {
|
|
2143
|
+
parseState = createPartialJSONParseState();
|
|
2144
|
+
eventState.tool_call_parse_states.set(index, parseState);
|
|
2145
|
+
}
|
|
2146
|
+
|
|
2147
|
+
const shouldParse = recordPartialJSONFragment(
|
|
2148
|
+
parseState,
|
|
2149
|
+
this.#partialJSONParseBudget,
|
|
2150
|
+
argumentFragment,
|
|
2151
|
+
);
|
|
2152
|
+
const previousArguments = captureStructuredJSONSnapshot(functionSnapshot, 'arguments');
|
|
2153
|
+
if (typeof previousArguments !== 'string') {
|
|
2154
|
+
throw new OpenAIError('Chat completion stream contains an unsafe structured JSON snapshot');
|
|
2155
|
+
}
|
|
2156
|
+
functionSnapshot.arguments = previousArguments + argumentFragment;
|
|
2157
|
+
if (
|
|
2158
|
+
shouldParse &&
|
|
2159
|
+
boundIdentity?.parseable === true &&
|
|
2160
|
+
reservePartialJSONParse(parseState, this.#partialJSONParseBudget)
|
|
2161
|
+
) {
|
|
2162
|
+
this.#validateStructuredSnapshots(snapshot);
|
|
2163
|
+
functionSnapshot.parsed_arguments = parseStructuredStreamingJSON(
|
|
2164
|
+
validateStructuredJSONSnapshot(functionSnapshot.arguments),
|
|
2165
|
+
);
|
|
2166
|
+
} else if (argumentFragment.length > 0 && hasOwn(functionSnapshot, 'parsed_arguments')) {
|
|
2167
|
+
functionSnapshot.parsed_arguments = undefined;
|
|
2168
|
+
}
|
|
2169
|
+
} else {
|
|
2170
|
+
functionSnapshot.arguments += argumentFragment;
|
|
2171
|
+
}
|
|
2172
|
+
}
|
|
2173
|
+
}
|
|
2174
|
+
capturedToolCalls.push(Object.freeze({ index, arguments_delta: argumentsDelta }));
|
|
536
2175
|
}
|
|
537
2176
|
}
|
|
2177
|
+
Object.freeze(capturedToolCalls);
|
|
538
2178
|
}
|
|
539
2179
|
return snapshot;
|
|
540
2180
|
}
|
|
541
2181
|
|
|
2182
|
+
/** Iterates over raw API chunks; stopping iteration early aborts the underlying request. */
|
|
542
2183
|
[Symbol.asyncIterator](this: ChatCompletionStream<ParsedT>): AsyncIterator<ChatCompletionChunk> {
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
let done = false;
|
|
549
|
-
|
|
550
|
-
this.on('chunk', (chunk) => {
|
|
551
|
-
const reader = readQueue.shift();
|
|
552
|
-
if (reader) {
|
|
553
|
-
reader.resolve(chunk);
|
|
554
|
-
} else {
|
|
555
|
-
pushQueue.push(chunk);
|
|
556
|
-
}
|
|
557
|
-
});
|
|
558
|
-
|
|
559
|
-
this.on('end', () => {
|
|
560
|
-
done = true;
|
|
561
|
-
for (const reader of readQueue) {
|
|
562
|
-
reader.resolve(undefined);
|
|
563
|
-
}
|
|
564
|
-
readQueue.length = 0;
|
|
565
|
-
});
|
|
566
|
-
|
|
567
|
-
this.on('abort', (err) => {
|
|
568
|
-
done = true;
|
|
569
|
-
for (const reader of readQueue) {
|
|
570
|
-
reader.reject(err);
|
|
571
|
-
}
|
|
572
|
-
readQueue.length = 0;
|
|
573
|
-
});
|
|
574
|
-
|
|
575
|
-
this.on('error', (err) => {
|
|
576
|
-
done = true;
|
|
577
|
-
for (const reader of readQueue) {
|
|
578
|
-
reader.reject(err);
|
|
579
|
-
}
|
|
580
|
-
readQueue.length = 0;
|
|
581
|
-
});
|
|
582
|
-
|
|
583
|
-
return {
|
|
584
|
-
next: async (): Promise<IteratorResult<ChatCompletionChunk>> => {
|
|
585
|
-
if (!pushQueue.length) {
|
|
586
|
-
if (done) {
|
|
587
|
-
return { value: undefined, done: true };
|
|
588
|
-
}
|
|
589
|
-
return new Promise<ChatCompletionChunk | undefined>((resolve, reject) =>
|
|
590
|
-
readQueue.push({ resolve, reject }),
|
|
591
|
-
).then((chunk) => (chunk ? { value: chunk, done: false } : { value: undefined, done: true }));
|
|
592
|
-
}
|
|
593
|
-
const chunk = pushQueue.shift()!;
|
|
594
|
-
return { value: chunk, done: false };
|
|
595
|
-
},
|
|
596
|
-
return: async () => {
|
|
597
|
-
this.abort();
|
|
598
|
-
return { value: undefined, done: true };
|
|
2184
|
+
return this._createIterator<ChatCompletionChunk>(
|
|
2185
|
+
(push) => {
|
|
2186
|
+
const onChunk = (chunk: ChatCompletionChunk) => push(chunk);
|
|
2187
|
+
this.on('chunk', onChunk);
|
|
2188
|
+
return () => this.off('chunk', onChunk);
|
|
599
2189
|
},
|
|
600
|
-
|
|
2190
|
+
{ onReturn: () => this.abort() },
|
|
2191
|
+
);
|
|
601
2192
|
}
|
|
602
2193
|
|
|
2194
|
+
/** Serializes raw completion chunks into a readable stream for transfer to another runtime. */
|
|
603
2195
|
toReadableStream(): ReadableStream {
|
|
604
2196
|
const stream = new Stream(this[Symbol.asyncIterator].bind(this), this.controller);
|
|
605
2197
|
return stream.toReadableStream();
|
|
@@ -609,18 +2201,56 @@ export class ChatCompletionStream<ParsedT = null>
|
|
|
609
2201
|
function finalizeChatCompletion<ParsedT>(
|
|
610
2202
|
snapshot: ChatCompletionSnapshot,
|
|
611
2203
|
params: ChatCompletionCreateParams | null,
|
|
2204
|
+
audioDoneChoiceIndexes: ReadonlySet<number>,
|
|
2205
|
+
validatedMessages: WeakMap<ChatCompletionSnapshot.Choice, ValidatedChoiceSnapshot>,
|
|
612
2206
|
): ParsedChatCompletion<ParsedT> {
|
|
613
2207
|
const { id, choices, created, model, system_fingerprint, ...rest } = snapshot;
|
|
614
2208
|
const completion: ChatCompletion = {
|
|
615
2209
|
...rest,
|
|
616
2210
|
id,
|
|
617
|
-
choices:
|
|
618
|
-
|
|
619
|
-
|
|
2211
|
+
choices: mapCapturedSnapshotArray(
|
|
2212
|
+
choices,
|
|
2213
|
+
MAX_STREAM_CHOICES,
|
|
2214
|
+
'choice',
|
|
2215
|
+
(choice): ChatCompletion.Choice => {
|
|
2216
|
+
const validated = validatedMessages.get(choice);
|
|
2217
|
+
if (!validated) {
|
|
2218
|
+
throw new OpenAIError('Chat completion stream contains an unsafe structured JSON snapshot');
|
|
2219
|
+
}
|
|
2220
|
+
const stableChoice = new Proxy(choice, {
|
|
2221
|
+
get(target, property, receiver) {
|
|
2222
|
+
// oxlint-disable-next-line anti-slop/no-reflect-get -- Proxy forwarding must preserve arbitrary keys and the original accessor receiver.
|
|
2223
|
+
return property === 'message' ? validated.message : Reflect.get(target, property, receiver);
|
|
2224
|
+
},
|
|
2225
|
+
});
|
|
2226
|
+
const { message: sourceMessage, finish_reason, index, logprobs, ...choiceRest } = stableChoice;
|
|
2227
|
+
const message = new Proxy(sourceMessage, {
|
|
2228
|
+
get(target, property, receiver) {
|
|
2229
|
+
if (property === 'content') {
|
|
2230
|
+
return validated.content;
|
|
2231
|
+
}
|
|
2232
|
+
if (property === 'refusal') {
|
|
2233
|
+
return validated.refusal;
|
|
2234
|
+
}
|
|
2235
|
+
if (property === 'tool_calls') {
|
|
2236
|
+
return validated.toolCallCollection;
|
|
2237
|
+
}
|
|
2238
|
+
// oxlint-disable-next-line anti-slop/no-reflect-get -- Proxy forwarding must preserve arbitrary keys and the original accessor receiver.
|
|
2239
|
+
return Reflect.get(target, property, receiver);
|
|
2240
|
+
},
|
|
2241
|
+
});
|
|
2242
|
+
const { content = null, function_call, tool_calls, audio, ...messageRest } = message;
|
|
2243
|
+
// Audio streams can end with an expires_at-only chunk after the
|
|
2244
|
+
// generated audio, without a separate finish_reason.
|
|
2245
|
+
const finishReason =
|
|
2246
|
+
finish_reason ?? (audioDoneChoiceIndexes.has(index) && isCompleteAudio(audio) ? 'stop' : null);
|
|
2247
|
+
if (!finishReason) {
|
|
620
2248
|
throw new OpenAIError(`missing finish_reason for choice ${index}`);
|
|
621
2249
|
}
|
|
622
2250
|
|
|
623
|
-
|
|
2251
|
+
// SAFETY: The completed API response contract supplies the audio fields; this preserves the existing pass-through behavior at finalization.
|
|
2252
|
+
const audioResponse = audio ? { audio: audio as ChatCompletionAudio } : {};
|
|
2253
|
+
// SAFETY: The API completion contract uses assistant role; retaining the wire role preserves existing behavior without adding runtime rejection.
|
|
624
2254
|
const role = message.role as 'assistant'; // this is what we expect; in theory it could be different which would make our types a slight lie but would be fine.
|
|
625
2255
|
if (!role) {
|
|
626
2256
|
throw new OpenAIError(`missing role for choice ${index}`);
|
|
@@ -639,12 +2269,13 @@ function finalizeChatCompletion<ParsedT>(
|
|
|
639
2269
|
return {
|
|
640
2270
|
...choiceRest,
|
|
641
2271
|
message: {
|
|
2272
|
+
...audioResponse,
|
|
642
2273
|
content,
|
|
643
2274
|
function_call: { arguments: args, name },
|
|
644
2275
|
role,
|
|
645
2276
|
refusal: message.refusal ?? null,
|
|
646
2277
|
},
|
|
647
|
-
finish_reason,
|
|
2278
|
+
finish_reason: finishReason,
|
|
648
2279
|
index,
|
|
649
2280
|
logprobs,
|
|
650
2281
|
};
|
|
@@ -654,42 +2285,108 @@ function finalizeChatCompletion<ParsedT>(
|
|
|
654
2285
|
return {
|
|
655
2286
|
...choiceRest,
|
|
656
2287
|
index,
|
|
657
|
-
finish_reason,
|
|
2288
|
+
finish_reason: finishReason,
|
|
658
2289
|
logprobs,
|
|
659
2290
|
message: {
|
|
660
2291
|
...messageRest,
|
|
2292
|
+
...audioResponse,
|
|
661
2293
|
role,
|
|
662
2294
|
content,
|
|
663
2295
|
refusal: message.refusal ?? null,
|
|
664
|
-
tool_calls:
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
2296
|
+
tool_calls: mapCapturedSnapshotArray(
|
|
2297
|
+
tool_calls,
|
|
2298
|
+
MAX_STREAM_TOOL_CALLS,
|
|
2299
|
+
'tool-call',
|
|
2300
|
+
(tool_call, i): ChatCompletionMessageToolCall => {
|
|
2301
|
+
const captured = validated.toolCalls.get(i);
|
|
2302
|
+
if (!captured) {
|
|
2303
|
+
const identity = ownFunctionToolIdentity(tool_call);
|
|
2304
|
+
if (
|
|
2305
|
+
identity &&
|
|
2306
|
+
shouldParseToolCall(params, {
|
|
2307
|
+
type: identity.type,
|
|
2308
|
+
function: { name: identity.name },
|
|
2309
|
+
})
|
|
2310
|
+
) {
|
|
2311
|
+
throw new OpenAIError(
|
|
2312
|
+
'Chat completion stream contains an unsafe structured JSON snapshot',
|
|
2313
|
+
);
|
|
2314
|
+
}
|
|
2315
|
+
}
|
|
2316
|
+
if (captured && captured.tool !== tool_call) {
|
|
2317
|
+
throw new OpenAIError('Chat completion stream contains a changed tool call identity');
|
|
2318
|
+
}
|
|
2319
|
+
const stableFunction =
|
|
2320
|
+
captured &&
|
|
2321
|
+
new Proxy(captured.function, {
|
|
2322
|
+
get(target, property, receiver) {
|
|
2323
|
+
if (property === 'arguments') {
|
|
2324
|
+
return captured.arguments;
|
|
2325
|
+
}
|
|
2326
|
+
if (property === 'name') {
|
|
2327
|
+
return captured.name;
|
|
2328
|
+
}
|
|
2329
|
+
// oxlint-disable-next-line anti-slop/no-reflect-get -- Proxy forwarding must preserve arbitrary keys and the original accessor receiver.
|
|
2330
|
+
return Reflect.get(target, property, receiver);
|
|
2331
|
+
},
|
|
2332
|
+
});
|
|
2333
|
+
const stableTool =
|
|
2334
|
+
captured && stableFunction
|
|
2335
|
+
? new Proxy(tool_call, {
|
|
2336
|
+
get(target, property, receiver) {
|
|
2337
|
+
if (property === 'type') {
|
|
2338
|
+
return captured.type;
|
|
2339
|
+
}
|
|
2340
|
+
if (property === 'function') {
|
|
2341
|
+
return stableFunction;
|
|
2342
|
+
}
|
|
2343
|
+
// oxlint-disable-next-line anti-slop/no-reflect-get -- Proxy forwarding must preserve arbitrary keys and the original accessor receiver.
|
|
2344
|
+
return Reflect.get(target, property, receiver);
|
|
2345
|
+
},
|
|
2346
|
+
})
|
|
2347
|
+
: tool_call;
|
|
2348
|
+
if (stableTool.type == null) {
|
|
2349
|
+
throw new OpenAIError(`missing choices[${index}].tool_calls[${i}].type`);
|
|
2350
|
+
}
|
|
2351
|
+
|
|
2352
|
+
if (stableTool.type === 'custom') {
|
|
2353
|
+
const { custom, type, id, ...toolRest } = stableTool;
|
|
2354
|
+
const { input = '', name, ...customRest } = custom || {};
|
|
2355
|
+
if (name == null) {
|
|
2356
|
+
throw new OpenAIError(`missing choices[${index}].tool_calls[${i}].custom.name`);
|
|
2357
|
+
}
|
|
2358
|
+
return {
|
|
2359
|
+
...toolRest,
|
|
2360
|
+
id: id || `call_${uuid4()}`,
|
|
2361
|
+
type,
|
|
2362
|
+
custom: { ...customRest, name, input },
|
|
2363
|
+
};
|
|
2364
|
+
}
|
|
2365
|
+
|
|
2366
|
+
const { function: fn, type, id, ...toolRest } = stableTool;
|
|
2367
|
+
const { arguments: args, name, ...fnRest } = fn || {};
|
|
2368
|
+
if (name == null) {
|
|
2369
|
+
throw new OpenAIError(`missing choices[${index}].tool_calls[${i}].function.name`);
|
|
2370
|
+
}
|
|
2371
|
+
if (args == null) {
|
|
2372
|
+
throw new OpenAIError(`missing choices[${index}].tool_calls[${i}].function.arguments`);
|
|
2373
|
+
}
|
|
2374
|
+
|
|
2375
|
+
return {
|
|
2376
|
+
...toolRest,
|
|
2377
|
+
id: id || `call_${uuid4()}`,
|
|
2378
|
+
type,
|
|
2379
|
+
function: { ...fnRest, name, arguments: args },
|
|
2380
|
+
};
|
|
2381
|
+
},
|
|
2382
|
+
),
|
|
686
2383
|
},
|
|
687
2384
|
};
|
|
688
2385
|
}
|
|
689
2386
|
return {
|
|
690
2387
|
...choiceRest,
|
|
691
|
-
message: { ...messageRest, content, role, refusal: message.refusal ?? null },
|
|
692
|
-
finish_reason,
|
|
2388
|
+
message: { ...messageRest, ...audioResponse, content, role, refusal: message.refusal ?? null },
|
|
2389
|
+
finish_reason: finishReason,
|
|
693
2390
|
index,
|
|
694
2391
|
logprobs,
|
|
695
2392
|
};
|
|
@@ -698,19 +2395,22 @@ function finalizeChatCompletion<ParsedT>(
|
|
|
698
2395
|
created,
|
|
699
2396
|
model,
|
|
700
2397
|
object: 'chat.completion',
|
|
2398
|
+
// Spread creates an own data property without invoking inherited setters or changing the object prototype.
|
|
701
2399
|
...(system_fingerprint ? { system_fingerprint } : {}),
|
|
702
2400
|
};
|
|
703
2401
|
|
|
704
2402
|
return maybeParseChatCompletion(completion, params);
|
|
705
2403
|
}
|
|
706
2404
|
|
|
707
|
-
function
|
|
708
|
-
|
|
2405
|
+
function isCompleteAudio(
|
|
2406
|
+
audio: Partial<ChatCompletionAudio> | null | undefined,
|
|
2407
|
+
): audio is ChatCompletionAudio {
|
|
2408
|
+
return audio?.id != null && audio.data != null && audio.transcript != null && audio.expires_at != null;
|
|
709
2409
|
}
|
|
710
2410
|
|
|
711
2411
|
/**
|
|
712
|
-
*
|
|
713
|
-
*
|
|
2412
|
+
* The chat completion accumulated from every streamed chunk received so far.
|
|
2413
|
+
* Fields within each choice can remain incomplete until generation finishes.
|
|
714
2414
|
*/
|
|
715
2415
|
export interface ChatCompletionSnapshot {
|
|
716
2416
|
/**
|
|
@@ -722,7 +2422,7 @@ export interface ChatCompletionSnapshot {
|
|
|
722
2422
|
* A list of chat completion choices. Can be more than one if `n` is greater
|
|
723
2423
|
* than 1.
|
|
724
2424
|
*/
|
|
725
|
-
choices:
|
|
2425
|
+
choices: ChatCompletionSnapshot.Choice[];
|
|
726
2426
|
|
|
727
2427
|
/**
|
|
728
2428
|
* The Unix timestamp (in seconds) of when the chat completion was created.
|
|
@@ -730,7 +2430,7 @@ export interface ChatCompletionSnapshot {
|
|
|
730
2430
|
created: number;
|
|
731
2431
|
|
|
732
2432
|
/**
|
|
733
|
-
* The model
|
|
2433
|
+
* The model generating the completion.
|
|
734
2434
|
*/
|
|
735
2435
|
model: string;
|
|
736
2436
|
|
|
@@ -747,10 +2447,12 @@ export interface ChatCompletionSnapshot {
|
|
|
747
2447
|
system_fingerprint?: string;
|
|
748
2448
|
}
|
|
749
2449
|
|
|
2450
|
+
/** Nested shapes used by an in-progress chat completion snapshot. */
|
|
750
2451
|
export namespace ChatCompletionSnapshot {
|
|
2452
|
+
/** One in-progress assistant choice and the metadata accumulated for it. */
|
|
751
2453
|
export interface Choice {
|
|
752
2454
|
/**
|
|
753
|
-
*
|
|
2455
|
+
* The assistant message accumulated from streamed model response deltas.
|
|
754
2456
|
*/
|
|
755
2457
|
message: Choice.Message;
|
|
756
2458
|
|
|
@@ -758,8 +2460,8 @@ export namespace ChatCompletionSnapshot {
|
|
|
758
2460
|
* The reason the model stopped generating tokens. This will be `stop` if the model
|
|
759
2461
|
* hit a natural stop point or a provided stop sequence, `length` if the maximum
|
|
760
2462
|
* number of tokens specified in the request was reached, `content_filter` if
|
|
761
|
-
* content was omitted due to a flag from our content filters,
|
|
762
|
-
*
|
|
2463
|
+
* content was omitted due to a flag from our content filters, `tool_calls` if
|
|
2464
|
+
* the model called a tool, or the deprecated `function_call` value.
|
|
763
2465
|
*/
|
|
764
2466
|
finish_reason: ChatCompletion.Choice['finish_reason'] | null;
|
|
765
2467
|
|
|
@@ -774,18 +2476,24 @@ export namespace ChatCompletionSnapshot {
|
|
|
774
2476
|
index: number;
|
|
775
2477
|
}
|
|
776
2478
|
|
|
2479
|
+
/** Nested message shapes belonging to an in-progress completion choice. */
|
|
777
2480
|
export namespace Choice {
|
|
778
2481
|
/**
|
|
779
|
-
*
|
|
2482
|
+
* The assistant message accumulated from streamed response deltas.
|
|
780
2483
|
*/
|
|
781
2484
|
export interface Message {
|
|
782
2485
|
/**
|
|
783
|
-
* The
|
|
2486
|
+
* The assistant text accumulated for this message so far.
|
|
784
2487
|
*/
|
|
785
2488
|
content?: string | null;
|
|
786
2489
|
|
|
2490
|
+
/** Audio fields received so far; individual fields can remain absent until generation finishes. */
|
|
2491
|
+
audio?: Partial<ChatCompletionAudio> | null;
|
|
2492
|
+
|
|
2493
|
+
/** The model's refusal text accumulated so far, when the request is refused. */
|
|
787
2494
|
refusal?: string | null;
|
|
788
2495
|
|
|
2496
|
+
/** A best-effort partial parse of structured assistant content. */
|
|
789
2497
|
parsed?: unknown | null;
|
|
790
2498
|
|
|
791
2499
|
/**
|
|
@@ -794,7 +2502,8 @@ export namespace ChatCompletionSnapshot {
|
|
|
794
2502
|
*/
|
|
795
2503
|
function_call?: Message.FunctionCall;
|
|
796
2504
|
|
|
797
|
-
|
|
2505
|
+
/** Function and custom tool calls accumulated so far; inputs may still be incomplete. */
|
|
2506
|
+
tool_calls?: Message.ToolCall[];
|
|
798
2507
|
|
|
799
2508
|
/**
|
|
800
2509
|
* The role of the author of this message.
|
|
@@ -802,22 +2511,30 @@ export namespace ChatCompletionSnapshot {
|
|
|
802
2511
|
role?: ChatCompletionRole;
|
|
803
2512
|
}
|
|
804
2513
|
|
|
2514
|
+
/** Nested tool-call shapes belonging to an in-progress assistant message. */
|
|
805
2515
|
export namespace Message {
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
* The ID of the tool call.
|
|
809
|
-
*/
|
|
810
|
-
id: string;
|
|
2516
|
+
/** A function or custom tool call accumulated incrementally from streamed chunks. */
|
|
2517
|
+
export type ToolCall = ToolCall.FunctionToolCall | ToolCall.CustomToolCall;
|
|
811
2518
|
|
|
812
|
-
|
|
2519
|
+
/** Function and custom details nested under an in-progress tool call. */
|
|
2520
|
+
export namespace ToolCall {
|
|
2521
|
+
/** A function-tool call whose name, identifier, and arguments are streamed incrementally. */
|
|
2522
|
+
export interface FunctionToolCall {
|
|
2523
|
+
/**
|
|
2524
|
+
* The ID of the tool call.
|
|
2525
|
+
*/
|
|
2526
|
+
id: string;
|
|
813
2527
|
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
*/
|
|
817
|
-
type: 'function';
|
|
818
|
-
}
|
|
2528
|
+
/** The function name and the complete or partial JSON arguments received so far. */
|
|
2529
|
+
function: ToolCall.Function;
|
|
819
2530
|
|
|
820
|
-
|
|
2531
|
+
/**
|
|
2532
|
+
* The type of the tool.
|
|
2533
|
+
*/
|
|
2534
|
+
type: 'function';
|
|
2535
|
+
}
|
|
2536
|
+
|
|
2537
|
+
/** The name and incrementally accumulated arguments of a function-tool call. */
|
|
821
2538
|
export interface Function {
|
|
822
2539
|
/**
|
|
823
2540
|
* The arguments to call the function with, as generated by the model in JSON
|
|
@@ -827,6 +2544,7 @@ export namespace ChatCompletionSnapshot {
|
|
|
827
2544
|
*/
|
|
828
2545
|
arguments: string;
|
|
829
2546
|
|
|
2547
|
+
/** A best-effort partial parse of `arguments` for strict or auto-parseable tools. */
|
|
830
2548
|
parsed_arguments?: unknown;
|
|
831
2549
|
|
|
832
2550
|
/**
|
|
@@ -834,6 +2552,34 @@ export namespace ChatCompletionSnapshot {
|
|
|
834
2552
|
*/
|
|
835
2553
|
name: string;
|
|
836
2554
|
}
|
|
2555
|
+
|
|
2556
|
+
/** A custom-tool call whose name, identifier, and input are streamed incrementally. */
|
|
2557
|
+
export interface CustomToolCall {
|
|
2558
|
+
/**
|
|
2559
|
+
* The ID of the tool call.
|
|
2560
|
+
*/
|
|
2561
|
+
id: string;
|
|
2562
|
+
|
|
2563
|
+
/** The custom-tool name and complete or partial input received so far. */
|
|
2564
|
+
custom: CustomToolCall.Custom;
|
|
2565
|
+
|
|
2566
|
+
/**
|
|
2567
|
+
* The type of the tool.
|
|
2568
|
+
*/
|
|
2569
|
+
type: 'custom';
|
|
2570
|
+
}
|
|
2571
|
+
|
|
2572
|
+
/** Custom-tool details nested under an in-progress tool call. */
|
|
2573
|
+
export namespace CustomToolCall {
|
|
2574
|
+
/** The name and incrementally accumulated input of a custom-tool call. */
|
|
2575
|
+
export interface Custom {
|
|
2576
|
+
/** The name of the custom tool to call. */
|
|
2577
|
+
name: string;
|
|
2578
|
+
|
|
2579
|
+
/** The custom tool's complete or partial free-form input. */
|
|
2580
|
+
input: string;
|
|
2581
|
+
}
|
|
2582
|
+
}
|
|
837
2583
|
}
|
|
838
2584
|
|
|
839
2585
|
/**
|
|
@@ -858,15 +2604,17 @@ export namespace ChatCompletionSnapshot {
|
|
|
858
2604
|
}
|
|
859
2605
|
}
|
|
860
2606
|
|
|
861
|
-
type AssertIsEmpty<T extends
|
|
2607
|
+
type AssertIsEmpty<T extends object> = keyof T extends never ? T : never;
|
|
862
2608
|
|
|
863
2609
|
/**
|
|
864
2610
|
* Ensures the given argument is an empty object, useful for
|
|
865
2611
|
* asserting that all known properties on an object have been
|
|
866
2612
|
* destructured.
|
|
867
2613
|
*/
|
|
868
|
-
function assertIsEmpty<T extends
|
|
869
|
-
|
|
2614
|
+
function assertIsEmpty<T extends object>(obj: AssertIsEmpty<T>): asserts obj is AssertIsEmpty<T> {
|
|
2615
|
+
void obj;
|
|
870
2616
|
}
|
|
871
2617
|
|
|
872
|
-
function assertNever(_x: never) {
|
|
2618
|
+
function assertNever(_x: never) {
|
|
2619
|
+
return _x;
|
|
2620
|
+
}
|