openai 7.10.0 → 7.12.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +196 -0
- package/README.md +7 -5
- package/_vendor/partial-json-parser/parser.d.mts.map +1 -1
- package/_vendor/partial-json-parser/parser.d.ts.map +1 -1
- package/_vendor/partial-json-parser/parser.js +4 -3
- package/_vendor/partial-json-parser/parser.js.map +1 -1
- package/_vendor/partial-json-parser/parser.mjs +4 -3
- package/_vendor/partial-json-parser/parser.mjs.map +1 -1
- package/_vendor/zod-to-json-schema/parsers/string.js +1 -1
- package/_vendor/zod-to-json-schema/parsers/string.js.map +1 -1
- package/_vendor/zod-to-json-schema/parsers/string.mjs +1 -1
- package/_vendor/zod-to-json-schema/parsers/string.mjs.map +1 -1
- package/_vendor/zod-to-json-schema/parsers/tuple.d.mts.map +1 -1
- package/_vendor/zod-to-json-schema/parsers/tuple.d.ts.map +1 -1
- package/_vendor/zod-to-json-schema/parsers/tuple.js +19 -12
- package/_vendor/zod-to-json-schema/parsers/tuple.js.map +1 -1
- package/_vendor/zod-to-json-schema/parsers/tuple.mjs +19 -12
- package/_vendor/zod-to-json-schema/parsers/tuple.mjs.map +1 -1
- package/_vendor/zod-to-json-schema/zodToJsonSchema.d.mts.map +1 -1
- package/_vendor/zod-to-json-schema/zodToJsonSchema.d.ts.map +1 -1
- package/_vendor/zod-to-json-schema/zodToJsonSchema.js +6 -2
- package/_vendor/zod-to-json-schema/zodToJsonSchema.js.map +1 -1
- package/_vendor/zod-to-json-schema/zodToJsonSchema.mjs +6 -2
- package/_vendor/zod-to-json-schema/zodToJsonSchema.mjs.map +1 -1
- package/auth/subject-token-providers.d.mts.map +1 -1
- package/auth/subject-token-providers.d.ts.map +1 -1
- package/auth/subject-token-providers.js +10 -5
- package/auth/subject-token-providers.js.map +1 -1
- package/auth/subject-token-providers.mjs +10 -5
- package/auth/subject-token-providers.mjs.map +1 -1
- package/auth/workload-identity-auth.d.mts.map +1 -1
- package/auth/workload-identity-auth.d.ts.map +1 -1
- package/auth/workload-identity-auth.js +21 -11
- package/auth/workload-identity-auth.js.map +1 -1
- package/auth/workload-identity-auth.mjs +21 -11
- package/auth/workload-identity-auth.mjs.map +1 -1
- package/azure.d.mts.map +1 -1
- package/azure.d.ts.map +1 -1
- package/azure.js +31 -2
- package/azure.js.map +1 -1
- package/azure.mjs +32 -3
- package/azure.mjs.map +1 -1
- package/beta/realtime/internal-base.d.mts.map +1 -1
- package/beta/realtime/internal-base.d.ts.map +1 -1
- package/beta/realtime/internal-base.js +3 -2
- package/beta/realtime/internal-base.js.map +1 -1
- package/beta/realtime/internal-base.mjs +3 -2
- package/beta/realtime/internal-base.mjs.map +1 -1
- package/beta/realtime/websocket.d.mts +2 -0
- package/beta/realtime/websocket.d.mts.map +1 -1
- package/beta/realtime/websocket.d.ts +2 -0
- package/beta/realtime/websocket.d.ts.map +1 -1
- package/beta/realtime/websocket.js +12 -7
- package/beta/realtime/websocket.js.map +1 -1
- package/beta/realtime/websocket.mjs +12 -7
- package/beta/realtime/websocket.mjs.map +1 -1
- package/beta/realtime/ws.d.mts +2 -0
- package/beta/realtime/ws.d.mts.map +1 -1
- package/beta/realtime/ws.d.ts +2 -0
- package/beta/realtime/ws.d.ts.map +1 -1
- package/beta/realtime/ws.js +7 -5
- package/beta/realtime/ws.js.map +1 -1
- package/beta/realtime/ws.mjs +7 -5
- package/beta/realtime/ws.mjs.map +1 -1
- package/client.d.mts +9 -1
- package/client.d.mts.map +1 -1
- package/client.d.ts +9 -1
- package/client.d.ts.map +1 -1
- package/client.js +17 -4
- package/client.js.map +1 -1
- package/client.mjs +17 -4
- package/client.mjs.map +1 -1
- package/core/EventEmitter.d.mts +2 -1
- package/core/EventEmitter.d.mts.map +1 -1
- package/core/EventEmitter.d.ts +2 -1
- package/core/EventEmitter.d.ts.map +1 -1
- package/core/EventEmitter.js +2 -1
- package/core/EventEmitter.js.map +1 -1
- package/core/EventEmitter.mjs +2 -1
- package/core/EventEmitter.mjs.map +1 -1
- package/core/pagination.js +1 -1
- package/core/pagination.js.map +1 -1
- package/core/pagination.mjs +1 -1
- package/core/pagination.mjs.map +1 -1
- package/core/streaming.d.mts +8 -0
- package/core/streaming.d.mts.map +1 -1
- package/core/streaming.d.ts +8 -0
- package/core/streaming.d.ts.map +1 -1
- package/core/streaming.js +68 -15
- package/core/streaming.js.map +1 -1
- package/core/streaming.mjs +68 -15
- package/core/streaming.mjs.map +1 -1
- package/helpers/audio.d.mts +1 -1
- package/helpers/audio.d.mts.map +1 -1
- package/helpers/audio.d.ts +1 -1
- package/helpers/audio.d.ts.map +1 -1
- package/helpers/audio.js +25 -5
- package/helpers/audio.js.map +1 -1
- package/helpers/audio.mjs +25 -5
- package/helpers/audio.mjs.map +1 -1
- package/helpers/zod.d.mts +28 -15
- package/helpers/zod.d.mts.map +1 -1
- package/helpers/zod.d.ts +28 -15
- package/helpers/zod.d.ts.map +1 -1
- package/helpers/zod.js +30 -23
- package/helpers/zod.js.map +1 -1
- package/helpers/zod.mjs +30 -23
- package/helpers/zod.mjs.map +1 -1
- package/index.js +4 -5
- package/internal/bedrock.d.mts +11 -0
- package/internal/bedrock.d.mts.map +1 -1
- package/internal/bedrock.d.ts +11 -0
- package/internal/bedrock.d.ts.map +1 -1
- package/internal/bedrock.js +71 -55
- package/internal/bedrock.js.map +1 -1
- package/internal/bedrock.mjs +70 -55
- package/internal/bedrock.mjs.map +1 -1
- package/internal/detect-platform.js +2 -2
- package/internal/detect-platform.js.map +1 -1
- package/internal/detect-platform.mjs +2 -2
- package/internal/detect-platform.mjs.map +1 -1
- package/internal/errors.d.mts.map +1 -1
- package/internal/errors.d.ts.map +1 -1
- package/internal/errors.js +3 -2
- package/internal/errors.js.map +1 -1
- package/internal/errors.mjs +3 -2
- package/internal/errors.mjs.map +1 -1
- package/internal/headers.d.mts.map +1 -1
- package/internal/headers.d.ts.map +1 -1
- package/internal/headers.js +4 -5
- package/internal/headers.js.map +1 -1
- package/internal/headers.mjs +4 -5
- package/internal/headers.mjs.map +1 -1
- package/internal/parse.js +1 -1
- package/internal/parse.js.map +1 -1
- package/internal/parse.mjs +1 -1
- package/internal/parse.mjs.map +1 -1
- package/internal/realtime-credentials.d.mts +15 -0
- package/internal/realtime-credentials.d.mts.map +1 -0
- package/internal/realtime-credentials.d.ts +15 -0
- package/internal/realtime-credentials.d.ts.map +1 -0
- package/internal/realtime-credentials.js +23 -0
- package/internal/realtime-credentials.js.map +1 -0
- package/internal/realtime-credentials.mjs +19 -0
- package/internal/realtime-credentials.mjs.map +1 -0
- package/internal/responses/response-accumulator.d.mts.map +1 -1
- package/internal/responses/response-accumulator.d.ts.map +1 -1
- package/internal/responses/response-accumulator.js +2 -1
- package/internal/responses/response-accumulator.js.map +1 -1
- package/internal/responses/response-accumulator.mjs +2 -1
- package/internal/responses/response-accumulator.mjs.map +1 -1
- package/internal/to-file.d.mts +2 -1
- package/internal/to-file.d.mts.map +1 -1
- package/internal/to-file.d.ts +2 -1
- package/internal/to-file.d.ts.map +1 -1
- package/internal/to-file.js +5 -4
- package/internal/to-file.js.map +1 -1
- package/internal/to-file.mjs +6 -5
- package/internal/to-file.mjs.map +1 -1
- package/internal/types.d.mts +3 -3
- package/internal/types.d.mts.map +1 -1
- package/internal/types.d.ts +3 -3
- package/internal/types.d.ts.map +1 -1
- package/internal/uploads.d.mts +2 -0
- package/internal/uploads.d.mts.map +1 -1
- package/internal/uploads.d.ts +2 -0
- package/internal/uploads.d.ts.map +1 -1
- package/internal/uploads.js +12 -1
- package/internal/uploads.js.map +1 -1
- package/internal/uploads.mjs +11 -1
- package/internal/uploads.mjs.map +1 -1
- package/internal/utils/base64.d.mts.map +1 -1
- package/internal/utils/base64.d.ts.map +1 -1
- package/internal/utils/base64.js +3 -0
- package/internal/utils/base64.js.map +1 -1
- package/internal/utils/base64.mjs +3 -0
- package/internal/utils/base64.mjs.map +1 -1
- package/lib/AbstractChatCompletionRunner.d.mts +1 -1
- package/lib/AbstractChatCompletionRunner.d.mts.map +1 -1
- package/lib/AbstractChatCompletionRunner.d.ts +1 -1
- package/lib/AbstractChatCompletionRunner.d.ts.map +1 -1
- package/lib/AbstractChatCompletionRunner.js +31 -9
- package/lib/AbstractChatCompletionRunner.js.map +1 -1
- package/lib/AbstractChatCompletionRunner.mjs +31 -9
- package/lib/AbstractChatCompletionRunner.mjs.map +1 -1
- package/lib/AssistantStream.d.mts +4 -1
- package/lib/AssistantStream.d.mts.map +1 -1
- package/lib/AssistantStream.d.ts +4 -1
- package/lib/AssistantStream.d.ts.map +1 -1
- package/lib/AssistantStream.js +23 -8
- package/lib/AssistantStream.js.map +1 -1
- package/lib/AssistantStream.mjs +24 -9
- package/lib/AssistantStream.mjs.map +1 -1
- package/lib/ChatCompletionRunner.d.mts +6 -3
- package/lib/ChatCompletionRunner.d.mts.map +1 -1
- package/lib/ChatCompletionRunner.d.ts +6 -3
- package/lib/ChatCompletionRunner.d.ts.map +1 -1
- package/lib/ChatCompletionRunner.js +7 -4
- package/lib/ChatCompletionRunner.js.map +1 -1
- package/lib/ChatCompletionRunner.mjs +7 -4
- package/lib/ChatCompletionRunner.mjs.map +1 -1
- package/lib/ChatCompletionStream.d.mts.map +1 -1
- package/lib/ChatCompletionStream.d.ts.map +1 -1
- package/lib/ChatCompletionStream.js +6 -0
- package/lib/ChatCompletionStream.js.map +1 -1
- package/lib/ChatCompletionStream.mjs +7 -1
- package/lib/ChatCompletionStream.mjs.map +1 -1
- package/lib/ChatCompletionStreamingRunner.d.mts.map +1 -1
- package/lib/ChatCompletionStreamingRunner.d.ts.map +1 -1
- package/lib/ChatCompletionStreamingRunner.js +1 -0
- package/lib/ChatCompletionStreamingRunner.js.map +1 -1
- package/lib/ChatCompletionStreamingRunner.mjs +1 -0
- package/lib/ChatCompletionStreamingRunner.mjs.map +1 -1
- package/lib/EventEmitter.d.mts +3 -1
- package/lib/EventEmitter.d.mts.map +1 -1
- package/lib/EventEmitter.d.ts +3 -1
- package/lib/EventEmitter.d.ts.map +1 -1
- package/lib/EventEmitter.js +2 -1
- package/lib/EventEmitter.js.map +1 -1
- package/lib/EventEmitter.mjs +2 -1
- package/lib/EventEmitter.mjs.map +1 -1
- package/lib/EventStream.d.mts +2 -1
- package/lib/EventStream.d.mts.map +1 -1
- package/lib/EventStream.d.ts +2 -1
- package/lib/EventStream.d.ts.map +1 -1
- package/lib/EventStream.js +99 -46
- package/lib/EventStream.js.map +1 -1
- package/lib/EventStream.mjs +99 -46
- package/lib/EventStream.mjs.map +1 -1
- package/lib/ResponsesParser.d.mts +5 -4
- package/lib/ResponsesParser.d.mts.map +1 -1
- package/lib/ResponsesParser.d.ts +5 -4
- package/lib/ResponsesParser.d.ts.map +1 -1
- package/lib/ResponsesParser.js +21 -7
- package/lib/ResponsesParser.js.map +1 -1
- package/lib/ResponsesParser.mjs +21 -7
- package/lib/ResponsesParser.mjs.map +1 -1
- package/lib/RunnableFunction.d.mts +2 -2
- package/lib/RunnableFunction.d.mts.map +1 -1
- package/lib/RunnableFunction.d.ts +2 -2
- package/lib/RunnableFunction.d.ts.map +1 -1
- package/lib/embeddings.d.mts.map +1 -1
- package/lib/embeddings.d.ts.map +1 -1
- package/lib/embeddings.js +18 -12
- package/lib/embeddings.js.map +1 -1
- package/lib/embeddings.mjs +18 -12
- package/lib/embeddings.mjs.map +1 -1
- package/lib/file-processing.d.mts +3 -2
- package/lib/file-processing.d.mts.map +1 -1
- package/lib/file-processing.d.ts +3 -2
- package/lib/file-processing.d.ts.map +1 -1
- package/lib/file-processing.js +5 -4
- package/lib/file-processing.js.map +1 -1
- package/lib/file-processing.mjs +5 -4
- package/lib/file-processing.mjs.map +1 -1
- package/lib/parser.d.mts +16 -4
- package/lib/parser.d.mts.map +1 -1
- package/lib/parser.d.ts +16 -4
- package/lib/parser.d.ts.map +1 -1
- package/lib/parser.js +1 -1
- package/lib/parser.js.map +1 -1
- package/lib/parser.mjs +1 -1
- package/lib/parser.mjs.map +1 -1
- package/lib/polling.d.mts +2 -2
- package/lib/polling.d.mts.map +1 -1
- package/lib/polling.d.ts +2 -2
- package/lib/polling.d.ts.map +1 -1
- package/lib/polling.js +5 -4
- package/lib/polling.js.map +1 -1
- package/lib/polling.mjs +5 -4
- package/lib/polling.mjs.map +1 -1
- package/lib/responses/EventTypes.d.mts +2 -2
- package/lib/responses/EventTypes.d.mts.map +1 -1
- package/lib/responses/EventTypes.d.ts +2 -2
- package/lib/responses/EventTypes.d.ts.map +1 -1
- package/lib/responses/ResponseInputItems.d.mts +3 -2
- package/lib/responses/ResponseInputItems.d.mts.map +1 -1
- package/lib/responses/ResponseInputItems.d.ts +3 -2
- package/lib/responses/ResponseInputItems.d.ts.map +1 -1
- package/lib/responses/ResponseInputItems.js +27 -4
- package/lib/responses/ResponseInputItems.js.map +1 -1
- package/lib/responses/ResponseInputItems.mjs +27 -4
- package/lib/responses/ResponseInputItems.mjs.map +1 -1
- package/lib/transform.d.mts.map +1 -1
- package/lib/transform.d.ts.map +1 -1
- package/lib/transform.js +1 -0
- package/lib/transform.js.map +1 -1
- package/lib/transform.mjs +1 -0
- package/lib/transform.mjs.map +1 -1
- package/lib/vector-store-polling.d.mts +2 -2
- package/lib/vector-store-polling.d.ts +2 -2
- package/lib/vector-store-polling.js +3 -3
- package/lib/vector-store-polling.js.map +1 -1
- package/lib/vector-store-polling.mjs +3 -3
- package/lib/vector-store-polling.mjs.map +1 -1
- package/lib/vector-store-upload.d.mts +2 -1
- package/lib/vector-store-upload.d.mts.map +1 -1
- package/lib/vector-store-upload.d.ts +2 -1
- package/lib/vector-store-upload.d.ts.map +1 -1
- package/lib/vector-store-upload.js +5 -1
- package/lib/vector-store-upload.js.map +1 -1
- package/lib/vector-store-upload.mjs +5 -1
- package/lib/vector-store-upload.mjs.map +1 -1
- package/package.json +1 -1
- package/providers/bedrock/aws.d.mts.map +1 -1
- package/providers/bedrock/aws.d.ts.map +1 -1
- package/providers/bedrock/aws.js +14 -16
- package/providers/bedrock/aws.js.map +1 -1
- package/providers/bedrock/aws.mjs +15 -17
- package/providers/bedrock/aws.mjs.map +1 -1
- package/realtime/internal-base.d.mts.map +1 -1
- package/realtime/internal-base.d.ts.map +1 -1
- package/realtime/internal-base.js +3 -5
- package/realtime/internal-base.js.map +1 -1
- package/realtime/internal-base.mjs +3 -5
- package/realtime/internal-base.mjs.map +1 -1
- package/realtime/websocket.d.mts +2 -0
- package/realtime/websocket.d.mts.map +1 -1
- package/realtime/websocket.d.ts +2 -0
- package/realtime/websocket.d.ts.map +1 -1
- package/realtime/websocket.js +12 -6
- package/realtime/websocket.js.map +1 -1
- package/realtime/websocket.mjs +12 -6
- package/realtime/websocket.mjs.map +1 -1
- package/realtime/ws.d.mts +2 -0
- package/realtime/ws.d.mts.map +1 -1
- package/realtime/ws.d.ts +2 -0
- package/realtime/ws.d.ts.map +1 -1
- package/realtime/ws.js +7 -4
- package/realtime/ws.js.map +1 -1
- package/realtime/ws.mjs +7 -4
- package/realtime/ws.mjs.map +1 -1
- package/resources/admin/organization/projects/service-accounts/api-keys.d.mts +9 -0
- package/resources/admin/organization/projects/service-accounts/api-keys.d.mts.map +1 -1
- package/resources/admin/organization/projects/service-accounts/api-keys.d.ts +9 -0
- package/resources/admin/organization/projects/service-accounts/api-keys.d.ts.map +1 -1
- package/resources/beta/responses/responses.d.mts +112 -33
- package/resources/beta/responses/responses.d.mts.map +1 -1
- package/resources/beta/responses/responses.d.ts +112 -33
- package/resources/beta/responses/responses.d.ts.map +1 -1
- package/resources/beta/responses/responses.js.map +1 -1
- package/resources/beta/responses/responses.mjs.map +1 -1
- package/resources/conversations/items.d.mts +9 -0
- package/resources/conversations/items.d.mts.map +1 -1
- package/resources/conversations/items.d.ts +9 -0
- package/resources/conversations/items.d.ts.map +1 -1
- package/resources/images.d.mts +73 -58
- package/resources/images.d.mts.map +1 -1
- package/resources/images.d.ts +73 -58
- package/resources/images.d.ts.map +1 -1
- package/resources/images.js.map +1 -1
- package/resources/images.mjs.map +1 -1
- package/resources/responses/responses.d.mts +112 -32
- package/resources/responses/responses.d.mts.map +1 -1
- package/resources/responses/responses.d.ts +112 -32
- package/resources/responses/responses.d.ts.map +1 -1
- package/resources/responses/responses.js.map +1 -1
- package/resources/responses/responses.mjs.map +1 -1
- package/resources/webhooks/webhooks.d.mts +6 -4
- package/resources/webhooks/webhooks.d.mts.map +1 -1
- package/resources/webhooks/webhooks.d.ts +6 -4
- package/resources/webhooks/webhooks.d.ts.map +1 -1
- package/src/_vendor/partial-json-parser/parser.ts +4 -3
- package/src/_vendor/zod-to-json-schema/parsers/string.ts +1 -1
- package/src/_vendor/zod-to-json-schema/parsers/tuple.ts +23 -16
- package/src/_vendor/zod-to-json-schema/zodToJsonSchema.ts +11 -6
- package/src/auth/subject-token-providers.ts +12 -7
- package/src/auth/workload-identity-auth.ts +27 -12
- package/src/azure.ts +34 -3
- package/src/beta/realtime/internal-base.ts +3 -2
- package/src/beta/realtime/websocket.ts +17 -7
- package/src/beta/realtime/ws.ts +9 -5
- package/src/client.ts +19 -4
- package/src/core/EventEmitter.ts +5 -8
- package/src/core/pagination.ts +1 -1
- package/src/core/streaming.ts +69 -13
- package/src/helpers/audio.ts +27 -6
- package/src/helpers/zod.ts +78 -29
- package/src/internal/bedrock.ts +84 -58
- package/src/internal/detect-platform.ts +2 -2
- package/src/internal/errors.ts +3 -2
- package/src/internal/headers.ts +7 -5
- package/src/internal/parse.ts +1 -1
- package/src/internal/realtime-credentials.ts +27 -0
- package/src/internal/responses/response-accumulator.ts +1 -0
- package/src/internal/to-file.ts +6 -5
- package/src/internal/uploads.ts +12 -1
- package/src/internal/utils/base64.ts +3 -0
- package/src/lib/AbstractChatCompletionRunner.ts +31 -6
- package/src/lib/AssistantStream.ts +25 -11
- package/src/lib/ChatCompletionRunner.ts +8 -4
- package/src/lib/ChatCompletionStream.ts +8 -0
- package/src/lib/ChatCompletionStreamingRunner.ts +1 -0
- package/src/lib/EventEmitter.ts +17 -8
- package/src/lib/EventStream.ts +105 -52
- package/src/lib/ResponsesParser.ts +33 -12
- package/src/lib/RunnableFunction.ts +2 -2
- package/src/lib/embeddings.ts +18 -12
- package/src/lib/file-processing.ts +5 -4
- package/src/lib/parser.ts +35 -11
- package/src/lib/polling.ts +5 -4
- package/src/lib/responses/EventTypes.ts +3 -69
- package/src/lib/responses/ResponseInputItems.ts +29 -4
- package/src/lib/transform.ts +1 -0
- package/src/lib/vector-store-polling.ts +3 -3
- package/src/lib/vector-store-upload.ts +5 -1
- package/src/providers/bedrock/aws.ts +23 -24
- package/src/realtime/internal-base.ts +3 -5
- package/src/realtime/websocket.ts +14 -6
- package/src/realtime/ws.ts +9 -4
- package/src/resources/admin/organization/projects/service-accounts/api-keys.ts +11 -0
- package/src/resources/beta/responses/responses.ts +143 -31
- package/src/resources/conversations/items.ts +11 -0
- package/src/resources/images.ts +76 -57
- package/src/resources/responses/responses.ts +143 -30
- package/src/resources/webhooks/webhooks.ts +6 -4
- package/src/version.ts +1 -1
- package/version.d.mts +1 -1
- package/version.d.ts +1 -1
- package/version.js +1 -1
- package/version.mjs +1 -1
package/src/core/streaming.ts
CHANGED
|
@@ -9,22 +9,34 @@ import type { OpenAI } from '../client';
|
|
|
9
9
|
|
|
10
10
|
type Bytes = string | ArrayBuffer | Uint8Array | null | undefined;
|
|
11
11
|
|
|
12
|
+
function isTransportAbortError(error: unknown): boolean {
|
|
13
|
+
return !(error instanceof APIError) && isAbortError(error);
|
|
14
|
+
}
|
|
15
|
+
|
|
12
16
|
type StreamTeeQueue<Item> = {
|
|
13
17
|
readonly length: number;
|
|
18
|
+
readonly canceled: boolean;
|
|
14
19
|
enqueue: (value: Promise<IteratorResult<Item>>) => void;
|
|
15
20
|
dequeue: () => Promise<IteratorResult<Item>> | undefined;
|
|
21
|
+
cancel: () => void;
|
|
16
22
|
};
|
|
17
23
|
|
|
18
24
|
function createStreamTeeQueue<Item>(): StreamTeeQueue<Item> {
|
|
19
25
|
let entries: (Promise<IteratorResult<Item>> | undefined)[] = [];
|
|
20
26
|
let head = 0;
|
|
27
|
+
let canceled = false;
|
|
21
28
|
|
|
22
29
|
return {
|
|
23
30
|
get length() {
|
|
24
31
|
return entries.length - head;
|
|
25
32
|
},
|
|
33
|
+
get canceled() {
|
|
34
|
+
return canceled;
|
|
35
|
+
},
|
|
26
36
|
enqueue(value) {
|
|
27
|
-
|
|
37
|
+
if (!canceled) {
|
|
38
|
+
entries.push(value);
|
|
39
|
+
}
|
|
28
40
|
},
|
|
29
41
|
dequeue() {
|
|
30
42
|
if (head === entries.length) {
|
|
@@ -45,6 +57,11 @@ function createStreamTeeQueue<Item>(): StreamTeeQueue<Item> {
|
|
|
45
57
|
|
|
46
58
|
return value;
|
|
47
59
|
},
|
|
60
|
+
cancel() {
|
|
61
|
+
canceled = true;
|
|
62
|
+
entries.length = 0;
|
|
63
|
+
head = 0;
|
|
64
|
+
},
|
|
48
65
|
};
|
|
49
66
|
}
|
|
50
67
|
|
|
@@ -69,6 +86,7 @@ export class Stream<Item> implements AsyncIterable<Item> {
|
|
|
69
86
|
/** Abort controller for the underlying request and all branches created with `tee()`. */
|
|
70
87
|
controller: AbortController;
|
|
71
88
|
#client: OpenAI | undefined;
|
|
89
|
+
#isTeeBranch = false;
|
|
72
90
|
private iterator: () => AsyncIterator<Item>;
|
|
73
91
|
|
|
74
92
|
/** Wraps an asynchronous event iterator and the controller that owns its request. */
|
|
@@ -101,8 +119,17 @@ export class Stream<Item> implements AsyncIterable<Item> {
|
|
|
101
119
|
consumed = true;
|
|
102
120
|
let done = false;
|
|
103
121
|
let receivedCompletionSentinel = false;
|
|
122
|
+
const messages = _iterSSEMessages(response, controller);
|
|
123
|
+
const closeMessages = messages.return.bind(messages);
|
|
124
|
+
messages.return = (value) => {
|
|
125
|
+
// Abort before nested iterator cleanup can wait on the response body's cancellation.
|
|
126
|
+
if (!receivedCompletionSentinel) {
|
|
127
|
+
controller.abort();
|
|
128
|
+
}
|
|
129
|
+
return closeMessages(value);
|
|
130
|
+
};
|
|
104
131
|
try {
|
|
105
|
-
for await (const sse of
|
|
132
|
+
for await (const sse of messages) {
|
|
106
133
|
if (sse.data === '[DONE]') {
|
|
107
134
|
receivedCompletionSentinel = true;
|
|
108
135
|
break;
|
|
@@ -145,7 +172,7 @@ export class Stream<Item> implements AsyncIterable<Item> {
|
|
|
145
172
|
// Abort errors and cleanup failures after the completion sentinel are non-fatal.
|
|
146
173
|
if (
|
|
147
174
|
receivedCompletionSentinel ||
|
|
148
|
-
|
|
175
|
+
isTransportAbortError(e) ||
|
|
149
176
|
(controller.signal.aborted && e === controller.signal.reason)
|
|
150
177
|
) {
|
|
151
178
|
return;
|
|
@@ -234,9 +261,6 @@ export class Stream<Item> implements AsyncIterable<Item> {
|
|
|
234
261
|
let done = false;
|
|
235
262
|
try {
|
|
236
263
|
for await (const line of iterLines()) {
|
|
237
|
-
if (done) {
|
|
238
|
-
continue;
|
|
239
|
-
}
|
|
240
264
|
if (line) {
|
|
241
265
|
let data: Item;
|
|
242
266
|
try {
|
|
@@ -278,14 +302,24 @@ export class Stream<Item> implements AsyncIterable<Item> {
|
|
|
278
302
|
/**
|
|
279
303
|
* Splits the stream into two streams which can be
|
|
280
304
|
* independently read from at different speeds.
|
|
305
|
+
* Closing a branch discards its buffered events without stopping its sibling.
|
|
306
|
+
* Future reads on that branch finish immediately; previously issued `next()`
|
|
307
|
+
* promises remain shared with its sibling and may still resolve with events.
|
|
308
|
+
* Closing both branches invokes the source iterator's `return()` when available.
|
|
309
|
+
* For {@link Stream.fromReadableStream}, closing both branches before iteration
|
|
310
|
+
* starts does not cancel the supplied readable; cancel that readable directly.
|
|
281
311
|
*/
|
|
282
312
|
tee(): [Stream<Item>, Stream<Item>] {
|
|
313
|
+
const { controller } = this;
|
|
283
314
|
const left = createStreamTeeQueue<Item>();
|
|
284
315
|
const right = createStreamTeeQueue<Item>();
|
|
285
316
|
const iterator = this.iterator();
|
|
286
317
|
|
|
287
318
|
const teeIterator = (queue: StreamTeeQueue<Item>): AsyncIterator<Item> => ({
|
|
288
319
|
next: () => {
|
|
320
|
+
if (queue.canceled) {
|
|
321
|
+
return Promise.resolve({ value: undefined, done: true });
|
|
322
|
+
}
|
|
289
323
|
if (queue.length === 0) {
|
|
290
324
|
const result = iterator.next();
|
|
291
325
|
left.enqueue(result);
|
|
@@ -293,20 +327,34 @@ export class Stream<Item> implements AsyncIterable<Item> {
|
|
|
293
327
|
}
|
|
294
328
|
return queue.dequeue()!;
|
|
295
329
|
},
|
|
330
|
+
return: async () => {
|
|
331
|
+
if (!queue.canceled) {
|
|
332
|
+
queue.cancel();
|
|
333
|
+
if (left.canceled && right.canceled) {
|
|
334
|
+
await this.#cancelIterator(iterator, controller);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
return { value: undefined, done: true };
|
|
338
|
+
},
|
|
296
339
|
});
|
|
297
340
|
|
|
298
|
-
|
|
299
|
-
new Stream(() => teeIterator(
|
|
300
|
-
|
|
301
|
-
|
|
341
|
+
const branch = (queue: StreamTeeQueue<Item>) => {
|
|
342
|
+
const stream = new Stream(() => teeIterator(queue), controller, this.#client);
|
|
343
|
+
stream.#isTeeBranch = true;
|
|
344
|
+
return stream;
|
|
345
|
+
};
|
|
346
|
+
return [branch(left), branch(right)];
|
|
302
347
|
}
|
|
303
348
|
|
|
304
349
|
/**
|
|
305
350
|
* Converts this stream to a newline-separated ReadableStream of
|
|
306
351
|
* JSON stringified values in the stream
|
|
307
352
|
* which can be turned back into a Stream with `Stream.fromReadableStream()`.
|
|
353
|
+
* Canceling a response-backed readable aborts its request. Canceling a tee
|
|
354
|
+
* branch discards its buffered events and leaves sibling consumers running.
|
|
308
355
|
*/
|
|
309
356
|
toReadableStream(): ReadableStream {
|
|
357
|
+
const { controller } = this;
|
|
310
358
|
let iter: AsyncIterator<Item>;
|
|
311
359
|
|
|
312
360
|
return makeReadableStream({
|
|
@@ -327,11 +375,19 @@ export class Stream<Item> implements AsyncIterable<Item> {
|
|
|
327
375
|
ctrl.error(err);
|
|
328
376
|
}
|
|
329
377
|
},
|
|
330
|
-
|
|
331
|
-
await iter.return?.();
|
|
332
|
-
},
|
|
378
|
+
cancel: () => this.#cancelIterator(iter, controller),
|
|
333
379
|
});
|
|
334
380
|
}
|
|
381
|
+
|
|
382
|
+
async #cancelIterator(iterator: AsyncIterator<Item>, controller: AbortController): Promise<void> {
|
|
383
|
+
const returnMethod = iterator.return;
|
|
384
|
+
if (returnMethod) {
|
|
385
|
+
if (!this.#isTeeBranch) {
|
|
386
|
+
controller.abort();
|
|
387
|
+
}
|
|
388
|
+
await Reflect.apply(returnMethod, iterator, []);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
335
391
|
}
|
|
336
392
|
|
|
337
393
|
function createAbortableSSESource(body: NonNullable<Response['body']>, signal: AbortSignal) {
|
package/src/helpers/audio.ts
CHANGED
|
@@ -24,7 +24,17 @@ const recordingProviders: Record<NodeJS.Platform, string> = {
|
|
|
24
24
|
};
|
|
25
25
|
|
|
26
26
|
function isResponse(stream: NodeJS.ReadableStream | Response | File): stream is Response {
|
|
27
|
-
|
|
27
|
+
// Readables have event and flow-control methods, even after they have ended.
|
|
28
|
+
const nodeReadable =
|
|
29
|
+
'pipe' in stream &&
|
|
30
|
+
typeof stream.pipe === 'function' &&
|
|
31
|
+
'on' in stream &&
|
|
32
|
+
typeof stream.on === 'function' &&
|
|
33
|
+
'pause' in stream &&
|
|
34
|
+
typeof stream.pause === 'function' &&
|
|
35
|
+
'resume' in stream &&
|
|
36
|
+
typeof stream.resume === 'function';
|
|
37
|
+
return !nodeReadable && 'body' in stream && stream.body !== undefined;
|
|
28
38
|
}
|
|
29
39
|
|
|
30
40
|
function isFile(stream: NodeJS.ReadableStream | Response | File): stream is File {
|
|
@@ -57,10 +67,18 @@ async function nodejsPlayAudio(stream: NodeJS.ReadableStream | Response | File):
|
|
|
57
67
|
ffplay.stderr?.resume();
|
|
58
68
|
ffplay.on('error', reject);
|
|
59
69
|
|
|
70
|
+
// The player can exit before the input stream finishes its cleanup.
|
|
71
|
+
let inputFinished = false;
|
|
72
|
+
let processClosed = false;
|
|
60
73
|
pipeline(source, ffplay.stdin, (error) => {
|
|
61
74
|
if (error) {
|
|
62
75
|
ffplay.kill();
|
|
63
76
|
reject(error);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
inputFinished = true;
|
|
80
|
+
if (processClosed) {
|
|
81
|
+
resolve();
|
|
64
82
|
}
|
|
65
83
|
});
|
|
66
84
|
|
|
@@ -69,7 +87,10 @@ async function nodejsPlayAudio(stream: NodeJS.ReadableStream | Response | File):
|
|
|
69
87
|
reject(new Error(`ffplay process exited with code ${code}`));
|
|
70
88
|
return;
|
|
71
89
|
}
|
|
72
|
-
|
|
90
|
+
processClosed = true;
|
|
91
|
+
if (inputFinished) {
|
|
92
|
+
resolve();
|
|
93
|
+
}
|
|
73
94
|
});
|
|
74
95
|
} catch (error) {
|
|
75
96
|
reject(error);
|
|
@@ -103,7 +124,7 @@ type RecordAudioOptions = {
|
|
|
103
124
|
/** Stops recording when aborted; successful termination returns the captured audio. */
|
|
104
125
|
signal?: AbortSignal;
|
|
105
126
|
|
|
106
|
-
/** Zero-based audio-input
|
|
127
|
+
/** Zero-based audio-input index (ALSA card number on Linux); defaults to `0`. */
|
|
107
128
|
device?: number;
|
|
108
129
|
|
|
109
130
|
/** Positive recording duration in milliseconds; nonpositive values disable the timeout. */
|
|
@@ -137,7 +158,7 @@ function nodejsRecordAudio({ signal, device, timeout }: RecordAudioOptions = {})
|
|
|
137
158
|
}
|
|
138
159
|
};
|
|
139
160
|
const stopRecording = () => {
|
|
140
|
-
if (!settled && ffmpeg) {
|
|
161
|
+
if (!settled && ffmpeg?.pid) {
|
|
141
162
|
try {
|
|
142
163
|
wasStopped ||= ffmpeg.kill('SIGTERM');
|
|
143
164
|
} catch (error) {
|
|
@@ -223,7 +244,7 @@ function nodejsRecordAudio({ signal, device, timeout }: RecordAudioOptions = {})
|
|
|
223
244
|
'-f',
|
|
224
245
|
provider,
|
|
225
246
|
'-i',
|
|
226
|
-
|
|
247
|
+
provider === 'alsa' ? `hw:${device ?? 0}` : `:${device ?? 0}`,
|
|
227
248
|
'-ar',
|
|
228
249
|
DEFAULT_SAMPLE_RATE.toString(),
|
|
229
250
|
'-ac',
|
|
@@ -270,7 +291,7 @@ function nodejsRecordAudio({ signal, device, timeout }: RecordAudioOptions = {})
|
|
|
270
291
|
}
|
|
271
292
|
|
|
272
293
|
try {
|
|
273
|
-
if (!wasStopped) {
|
|
294
|
+
if (!wasStopped && ffmpeg?.pid) {
|
|
274
295
|
ffmpeg?.kill('SIGTERM');
|
|
275
296
|
}
|
|
276
297
|
} catch {
|
package/src/helpers/zod.ts
CHANGED
|
@@ -82,11 +82,11 @@ function validateSchemaDefinitions(schemaDefinitions: ZodSchemaDefinitions | und
|
|
|
82
82
|
|
|
83
83
|
function escapeSchemaDefinitionRefs<T extends object>(
|
|
84
84
|
schema: T,
|
|
85
|
-
|
|
85
|
+
definitionNames: ReadonlyMap<string, string>,
|
|
86
86
|
): T {
|
|
87
87
|
const refReplacements = new Map(
|
|
88
|
-
|
|
89
|
-
`#/definitions/${
|
|
88
|
+
[...definitionNames].map(([id, name]) => [
|
|
89
|
+
`#/definitions/${id}`,
|
|
90
90
|
`#/definitions/${encodeSchemaDefinitionRefToken(name)}`,
|
|
91
91
|
]),
|
|
92
92
|
);
|
|
@@ -147,7 +147,10 @@ function zodV3ToJsonSchema(
|
|
|
147
147
|
: undefined),
|
|
148
148
|
});
|
|
149
149
|
|
|
150
|
-
const escapedSchema = escapeSchemaDefinitionRefs(
|
|
150
|
+
const escapedSchema = escapeSchemaDefinitionRefs(
|
|
151
|
+
jsonSchema,
|
|
152
|
+
new Map(Object.keys(options.schemaDefinitions ?? {}).map((name) => [name, name])),
|
|
153
|
+
);
|
|
151
154
|
assertJSONSerializableSchema(escapedSchema);
|
|
152
155
|
return escapedSchema;
|
|
153
156
|
}
|
|
@@ -157,16 +160,29 @@ function zodV4ToJsonSchema(
|
|
|
157
160
|
options: { schemaDefinitions?: ZodSchemaDefinitions | undefined } = {},
|
|
158
161
|
): Record<string, unknown> {
|
|
159
162
|
const metadata = options.schemaDefinitions ? z4.registry<Record<string, unknown>>() : undefined;
|
|
163
|
+
const definitionNames = new Map<string, string>();
|
|
160
164
|
for (const [name, definition] of Object.entries(options.schemaDefinitions ?? {})) {
|
|
161
|
-
|
|
165
|
+
// Avoid `/` and `~` so Zod versions that escape JSON Pointer tokens emit the same IDs.
|
|
166
|
+
const id = encodeURIComponent(name).replace(/~/g, '%7E');
|
|
167
|
+
definitionNames.set(id, name);
|
|
168
|
+
metadata?.add(definition as unknown as z4.ZodType, { id });
|
|
162
169
|
}
|
|
163
170
|
|
|
164
171
|
const jsonSchema = z4.toJSONSchema(schema, {
|
|
165
172
|
target: 'draft-7',
|
|
166
173
|
...(metadata ? { metadata } : undefined),
|
|
167
|
-
override: ({ zodSchema, jsonSchema }) => {
|
|
174
|
+
override: ({ zodSchema, jsonSchema, path }) => {
|
|
168
175
|
const def = zodSchema._zod.def;
|
|
169
176
|
|
|
177
|
+
if (def.type === 'object' && hasOwn(def.shape, '__proto__')) {
|
|
178
|
+
const propertyPath = [...path, 'properties', '__proto__']
|
|
179
|
+
.map((part) => encodeSchemaDefinitionRefToken(String(part)))
|
|
180
|
+
.join('/');
|
|
181
|
+
throw new Error(
|
|
182
|
+
`Zod field at \`#/${propertyPath}\` uses unsupported property name \`__proto__\`, which Zod omits from parsed output.`,
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
|
|
170
186
|
if (def.type === 'union' && 'discriminator' in def && Array.isArray(jsonSchema.oneOf)) {
|
|
171
187
|
if (jsonSchema.anyOf !== undefined) {
|
|
172
188
|
throw new Error(
|
|
@@ -182,7 +198,15 @@ function zodV4ToJsonSchema(
|
|
|
182
198
|
},
|
|
183
199
|
}) as JSONSchema;
|
|
184
200
|
|
|
185
|
-
const escapedSchema = escapeSchemaDefinitionRefs(jsonSchema,
|
|
201
|
+
const escapedSchema = escapeSchemaDefinitionRefs(jsonSchema, definitionNames);
|
|
202
|
+
if (escapedSchema.definitions) {
|
|
203
|
+
escapedSchema.definitions = Object.fromEntries(
|
|
204
|
+
Object.entries(escapedSchema.definitions).map(([id, definition]) => [
|
|
205
|
+
definitionNames.get(id) ?? id,
|
|
206
|
+
definition,
|
|
207
|
+
]),
|
|
208
|
+
);
|
|
209
|
+
}
|
|
186
210
|
|
|
187
211
|
return toStrictJsonSchema(escapedSchema) as Record<string, unknown>;
|
|
188
212
|
}
|
|
@@ -333,18 +357,8 @@ export function zodTextFormat<ZodInput extends ZodTypeLike>(
|
|
|
333
357
|
);
|
|
334
358
|
}
|
|
335
359
|
|
|
336
|
-
/**
|
|
337
|
-
|
|
338
|
-
* automatically by the chat completion `.runTools()` method or automatically
|
|
339
|
-
* parsed by `.parse()` / `.stream()`.
|
|
340
|
-
*
|
|
341
|
-
* Arguments are converted to strict JSON Schema and validated with the supplied
|
|
342
|
-
* Zod schema before the optional callback receives them.
|
|
343
|
-
*
|
|
344
|
-
* @param options Model-visible function name, Zod parameter schema, description,
|
|
345
|
-
* and optional callback used by `chat.completions.runTools()`.
|
|
346
|
-
*/
|
|
347
|
-
export function zodFunction<Parameters extends ZodTypeLike>(options: {
|
|
360
|
+
/** Model-facing settings and an optional execution callback for a Zod function tool. */
|
|
361
|
+
interface ZodFunctionOptions<Parameters extends ZodTypeLike> {
|
|
348
362
|
/** Model-visible function name used to identify matching tool calls. */
|
|
349
363
|
name: string;
|
|
350
364
|
|
|
@@ -356,19 +370,53 @@ export function zodFunction<Parameters extends ZodTypeLike>(options: {
|
|
|
356
370
|
|
|
357
371
|
/** Optional model-visible explanation of when and how the function should be used. */
|
|
358
372
|
description?: string | undefined;
|
|
359
|
-
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/** A Zod function tool retaining its inferred argument and callback types. */
|
|
376
|
+
type ZodFunctionTool<
|
|
377
|
+
Parameters extends ZodTypeLike,
|
|
378
|
+
Callback extends ZodFunctionOptions<Parameters>['function'],
|
|
379
|
+
> = AutoParseableTool<{
|
|
360
380
|
/** Inferred argument type produced by the Zod parameter schema. */
|
|
361
381
|
arguments: InferZodType<Parameters>;
|
|
362
|
-
|
|
363
382
|
/** Model-visible name used to match generated function calls. */
|
|
364
383
|
name: string;
|
|
384
|
+
/** Callback availability determines whether the tool can be executed. */
|
|
385
|
+
function: Callback;
|
|
386
|
+
}>;
|
|
365
387
|
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
388
|
+
/**
|
|
389
|
+
* Creates a chat completion `function` tool that can be invoked automatically
|
|
390
|
+
* by `.runTools()` or parsed by `.parse()` / `.stream()`.
|
|
391
|
+
*
|
|
392
|
+
* Arguments are converted to strict JSON Schema and validated with the supplied
|
|
393
|
+
* Zod schema before the callback receives them.
|
|
394
|
+
*
|
|
395
|
+
* @param options Model-visible function name, Zod parameter schema, description,
|
|
396
|
+
* and callback used by `chat.completions.runTools()`.
|
|
397
|
+
*/
|
|
398
|
+
export function zodFunction<Parameters extends ZodTypeLike>(
|
|
399
|
+
options: ZodFunctionOptions<Parameters> & {
|
|
400
|
+
/** Callback invoked with validated arguments by chat `runTools()`. */
|
|
401
|
+
function: NonNullable<ZodFunctionOptions<Parameters>['function']>;
|
|
402
|
+
},
|
|
403
|
+
): ZodFunctionTool<Parameters, NonNullable<ZodFunctionOptions<Parameters>['function']>>;
|
|
404
|
+
|
|
405
|
+
/**
|
|
406
|
+
* Creates a strict chat completion function tool for `.parse()` / `.stream()`.
|
|
407
|
+
* Without a guaranteed callback, the tool cannot be executed by `.runTools()`.
|
|
408
|
+
*
|
|
409
|
+
* @param options Model-visible function details and an optional callback.
|
|
410
|
+
*/
|
|
411
|
+
export function zodFunction<Parameters extends ZodTypeLike>(
|
|
412
|
+
options: ZodFunctionOptions<Parameters>,
|
|
413
|
+
): ZodFunctionTool<Parameters, ZodFunctionOptions<Parameters>['function']>;
|
|
414
|
+
|
|
415
|
+
/** Builds a strict Chat Completions function tool from the supplied Zod schema. */
|
|
416
|
+
export function zodFunction<Parameters extends ZodTypeLike>(options: ZodFunctionOptions<Parameters>) {
|
|
417
|
+
const parameters = options.parameters;
|
|
418
|
+
const zodSchema = parameters as unknown as ZodSchema;
|
|
370
419
|
|
|
371
|
-
// @ts-expect-error TODO
|
|
372
420
|
return makeParseableTool<any>(
|
|
373
421
|
{
|
|
374
422
|
type: 'function',
|
|
@@ -383,7 +431,7 @@ export function zodFunction<Parameters extends ZodTypeLike>(options: {
|
|
|
383
431
|
},
|
|
384
432
|
{
|
|
385
433
|
callback: options.function,
|
|
386
|
-
parser: (args) => parseZodObject(
|
|
434
|
+
parser: (args) => parseZodObject(parameters, args),
|
|
387
435
|
},
|
|
388
436
|
);
|
|
389
437
|
}
|
|
@@ -421,7 +469,8 @@ export function zodResponsesFunction<Parameters extends ZodTypeLike>(options: {
|
|
|
421
469
|
/** Callback signature associated with validated function-call arguments. */
|
|
422
470
|
function: (args: InferZodType<Parameters>) => unknown;
|
|
423
471
|
}> {
|
|
424
|
-
const
|
|
472
|
+
const parameters = options.parameters;
|
|
473
|
+
const zodSchema = parameters as unknown as ZodSchema;
|
|
425
474
|
|
|
426
475
|
return makeParseableResponseTool<any>(
|
|
427
476
|
{
|
|
@@ -435,7 +484,7 @@ export function zodResponsesFunction<Parameters extends ZodTypeLike>(options: {
|
|
|
435
484
|
},
|
|
436
485
|
{
|
|
437
486
|
callback: options.function,
|
|
438
|
-
parser: (args) => parseZodObject(
|
|
487
|
+
parser: (args) => parseZodObject(parameters, args),
|
|
439
488
|
},
|
|
440
489
|
);
|
|
441
490
|
}
|
package/src/internal/bedrock.ts
CHANGED
|
@@ -290,7 +290,7 @@ export function assertValidBedrockBearerCredential(credential: string): void {
|
|
|
290
290
|
}
|
|
291
291
|
}
|
|
292
292
|
|
|
293
|
-
interface
|
|
293
|
+
interface BedrockAuthSignalFailure {
|
|
294
294
|
error?: { value: unknown };
|
|
295
295
|
removeListeners?: () => void;
|
|
296
296
|
}
|
|
@@ -313,13 +313,13 @@ function removeBedrockAbortListener(signal: AbortSignal, listener: () => void):
|
|
|
313
313
|
}
|
|
314
314
|
}
|
|
315
315
|
|
|
316
|
-
function
|
|
317
|
-
|
|
316
|
+
function resolveAbortableBedrockAuth<T>(
|
|
317
|
+
operation: () => Promise<T>,
|
|
318
318
|
signals: readonly AbortSignal[],
|
|
319
|
-
failure:
|
|
320
|
-
): Promise<
|
|
319
|
+
failure: BedrockAuthSignalFailure,
|
|
320
|
+
): Promise<T> {
|
|
321
321
|
// oxlint-disable-next-line promise/avoid-new -- AbortSignal events require a Promise callback bridge.
|
|
322
|
-
return new Promise<
|
|
322
|
+
return new Promise<T>((resolve, reject) => {
|
|
323
323
|
let settled = false;
|
|
324
324
|
const listeners: { signal: AbortSignal; listener: () => void }[] = [];
|
|
325
325
|
|
|
@@ -333,13 +333,13 @@ function resolveAbortableBedrockBearerToken(
|
|
|
333
333
|
};
|
|
334
334
|
failure.removeListeners = removeListeners;
|
|
335
335
|
|
|
336
|
-
const settle = (result: {
|
|
336
|
+
const settle = (result: { value: T } | { error: unknown }) => {
|
|
337
337
|
if (settled) {
|
|
338
338
|
return;
|
|
339
339
|
}
|
|
340
340
|
settled = true;
|
|
341
|
-
if ('
|
|
342
|
-
resolve(result.
|
|
341
|
+
if ('value' in result) {
|
|
342
|
+
resolve(result.value);
|
|
343
343
|
} else {
|
|
344
344
|
removeListeners();
|
|
345
345
|
reject(result.error);
|
|
@@ -399,26 +399,75 @@ function resolveAbortableBedrockBearerToken(
|
|
|
399
399
|
}
|
|
400
400
|
}
|
|
401
401
|
|
|
402
|
-
let
|
|
402
|
+
let pending: Promise<T>;
|
|
403
403
|
try {
|
|
404
|
-
|
|
404
|
+
pending = operation();
|
|
405
405
|
} catch (error) {
|
|
406
406
|
settle({ error });
|
|
407
407
|
return;
|
|
408
408
|
}
|
|
409
409
|
|
|
410
|
-
const
|
|
410
|
+
const observeResult = async () => {
|
|
411
411
|
try {
|
|
412
|
-
settle({
|
|
412
|
+
settle({ value: await pending });
|
|
413
413
|
} catch (error) {
|
|
414
414
|
settle({ error });
|
|
415
415
|
}
|
|
416
416
|
};
|
|
417
417
|
// Observe the result even if the provider synchronously triggered cancellation.
|
|
418
|
-
void
|
|
418
|
+
void observeResult();
|
|
419
419
|
});
|
|
420
420
|
}
|
|
421
421
|
|
|
422
|
+
/**
|
|
423
|
+
* Resolves Bedrock authentication work with caller cancellation, then applies
|
|
424
|
+
* its result synchronously after the final cancellation checks.
|
|
425
|
+
*
|
|
426
|
+
* @internal
|
|
427
|
+
*/
|
|
428
|
+
export async function prepareBedrockAuth<T>(
|
|
429
|
+
request: FinalizedRequestInit,
|
|
430
|
+
context: ProviderRequestContext,
|
|
431
|
+
operation: {
|
|
432
|
+
resolve: () => Promise<T>;
|
|
433
|
+
failureMessage: string;
|
|
434
|
+
apply: (value: T) => void;
|
|
435
|
+
},
|
|
436
|
+
): Promise<void> {
|
|
437
|
+
const signals: AbortSignal[] = [];
|
|
438
|
+
for (const signal of [context.options.signal, request.signal]) {
|
|
439
|
+
if (signal != null && !signals.includes(signal)) {
|
|
440
|
+
signals.push(signal);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
const signalFailure: BedrockAuthSignalFailure = {};
|
|
444
|
+
let value: T;
|
|
445
|
+
try {
|
|
446
|
+
try {
|
|
447
|
+
value =
|
|
448
|
+
signals.length > 0
|
|
449
|
+
? await resolveAbortableBedrockAuth(operation.resolve, signals, signalFailure)
|
|
450
|
+
: await operation.resolve();
|
|
451
|
+
} catch (cause) {
|
|
452
|
+
if (signalFailure.error && Object.is(cause, signalFailure.error.value)) {
|
|
453
|
+
throw cause;
|
|
454
|
+
}
|
|
455
|
+
throw errorWithCause(operation.failureMessage, cause);
|
|
456
|
+
}
|
|
457
|
+
if (signalFailure.error) {
|
|
458
|
+
throw signalFailure.error.value;
|
|
459
|
+
}
|
|
460
|
+
for (const signal of signals) {
|
|
461
|
+
if (signal.aborted) {
|
|
462
|
+
throw createBedrockUserAbortError(signal);
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
} finally {
|
|
466
|
+
signalFailure.removeListeners?.();
|
|
467
|
+
}
|
|
468
|
+
operation.apply(value);
|
|
469
|
+
}
|
|
470
|
+
|
|
422
471
|
class BedrockBearerAuth implements BedrockRequestAuth {
|
|
423
472
|
private readonly tokenProvider: ApiKeySetter;
|
|
424
473
|
|
|
@@ -430,52 +479,29 @@ class BedrockBearerAuth implements BedrockRequestAuth {
|
|
|
430
479
|
const headers = new Headers(request.headers);
|
|
431
480
|
assertProviderOwnsAuthorization(headers);
|
|
432
481
|
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
try {
|
|
442
|
-
try {
|
|
443
|
-
token =
|
|
444
|
-
signals.length > 0
|
|
445
|
-
? await resolveAbortableBedrockBearerToken(() => this.tokenProvider(), signals, signalFailure)
|
|
446
|
-
: await this.tokenProvider();
|
|
447
|
-
} catch (cause) {
|
|
448
|
-
if (signalFailure.error && Object.is(cause, signalFailure.error.value)) {
|
|
449
|
-
throw cause;
|
|
482
|
+
await prepareBedrockAuth(request, context, {
|
|
483
|
+
resolve: () => this.tokenProvider(),
|
|
484
|
+
failureMessage: 'Failed to resolve a bearer credential for Bedrock.',
|
|
485
|
+
apply: (token) => {
|
|
486
|
+
if (typeof token !== 'string' || !token.trim()) {
|
|
487
|
+
throw new Errors.OpenAIError(
|
|
488
|
+
'The Bedrock bearer credential provider must return a non-empty string.',
|
|
489
|
+
);
|
|
450
490
|
}
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
491
|
+
assertValidBedrockBearerCredential(token);
|
|
492
|
+
try {
|
|
493
|
+
headers.set('authorization', `Bearer ${token}`);
|
|
494
|
+
} catch (error) {
|
|
495
|
+
if (error instanceof TypeError) {
|
|
496
|
+
// oxlint-disable-next-line eslint/preserve-caught-error -- The original error contains the bearer credential.
|
|
497
|
+
throw new TypeError('Bedrock bearer credential contains an invalid HTTP header value.');
|
|
498
|
+
}
|
|
499
|
+
throw error;
|
|
459
500
|
}
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
}
|
|
464
|
-
if (typeof token !== 'string' || !token.trim()) {
|
|
465
|
-
throw new Errors.OpenAIError('The Bedrock bearer credential provider must return a non-empty string.');
|
|
466
|
-
}
|
|
467
|
-
assertValidBedrockBearerCredential(token);
|
|
468
|
-
try {
|
|
469
|
-
headers.set('authorization', `Bearer ${token}`);
|
|
470
|
-
} catch (error) {
|
|
471
|
-
if (error instanceof TypeError) {
|
|
472
|
-
// oxlint-disable-next-line eslint/preserve-caught-error -- The original error contains the bearer credential.
|
|
473
|
-
throw new TypeError('Bedrock bearer credential contains an invalid HTTP header value.');
|
|
474
|
-
}
|
|
475
|
-
throw error;
|
|
476
|
-
}
|
|
477
|
-
request.redirect = 'manual';
|
|
478
|
-
request.headers = headers;
|
|
501
|
+
request.redirect = 'manual';
|
|
502
|
+
request.headers = headers;
|
|
503
|
+
},
|
|
504
|
+
});
|
|
479
505
|
}
|
|
480
506
|
}
|
|
481
507
|
|
|
@@ -75,7 +75,7 @@ const getPlatformProperties = (): PlatformProperties => {
|
|
|
75
75
|
'X-Stainless-OS': 'Unknown',
|
|
76
76
|
'X-Stainless-Arch': `other:${EdgeRuntime}`,
|
|
77
77
|
'X-Stainless-Runtime': 'edge',
|
|
78
|
-
'X-Stainless-Runtime-Version': (globalThis as any).process
|
|
78
|
+
'X-Stainless-Runtime-Version': (globalThis as any).process?.version ?? 'unknown',
|
|
79
79
|
};
|
|
80
80
|
}
|
|
81
81
|
// Check if Node.js
|
|
@@ -128,7 +128,7 @@ function getBrowserInfo(): BrowserInfo | null {
|
|
|
128
128
|
|
|
129
129
|
// NOTE: The order matters here!
|
|
130
130
|
const browserPatterns = [
|
|
131
|
-
{ key: 'edge' as const, pattern:
|
|
131
|
+
{ key: 'edge' as const, pattern: /\bEdg(?:e|A|iOS)?\b(?:\W+(\d+)\.(\d+)(?:\.(\d+))?)?/ },
|
|
132
132
|
{ key: 'ie' as const, pattern: /MSIE(?:\W+(\d+)\.(\d+)(?:\.(\d+))?)?/ },
|
|
133
133
|
{ key: 'ie' as const, pattern: /Trident(?:.*rv\:(\d+)\.(\d+)(?:\.(\d+))?)?/ },
|
|
134
134
|
{ key: 'chrome' as const, pattern: /Chrome(?:\W+(\d+)\.(\d+)(?:\.(\d+))?)?/ },
|
package/src/internal/errors.ts
CHANGED
|
@@ -14,11 +14,12 @@ export const castToError = (err: any): Error => {
|
|
|
14
14
|
if (typeof err === 'object' && err !== null) {
|
|
15
15
|
try {
|
|
16
16
|
if (Object.prototype.toString.call(err) === '[object Error]') {
|
|
17
|
+
const hasCause = 'cause' in err;
|
|
17
18
|
// @ts-ignore - not all envs have native support for cause yet
|
|
18
|
-
const error = new Error(err.message,
|
|
19
|
+
const error = new Error(err.message, hasCause ? { cause: err.cause } : {});
|
|
19
20
|
if (err.stack) error.stack = err.stack;
|
|
20
21
|
// @ts-ignore - not all envs have native support for cause yet
|
|
21
|
-
if (
|
|
22
|
+
if (hasCause && !Object.prototype.hasOwnProperty.call(error, 'cause')) error.cause = err.cause;
|
|
22
23
|
if (err.name) error.name = err.name;
|
|
23
24
|
return error;
|
|
24
25
|
}
|