openai 6.46.0 → 6.48.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +47 -0
- package/README.md +9 -2
- package/client.d.mts +1 -0
- package/client.d.mts.map +1 -1
- package/client.d.ts +1 -0
- package/client.d.ts.map +1 -1
- package/client.js +11 -5
- package/client.js.map +1 -1
- package/client.mjs +11 -5
- package/client.mjs.map +1 -1
- package/core/streaming.d.mts.map +1 -1
- package/core/streaming.d.ts.map +1 -1
- package/core/streaming.js +38 -6
- package/core/streaming.js.map +1 -1
- package/core/streaming.mjs +38 -6
- package/core/streaming.mjs.map +1 -1
- package/core/uploads.d.mts +1 -1
- package/core/uploads.d.mts.map +1 -1
- package/core/uploads.d.ts +1 -1
- package/core/uploads.d.ts.map +1 -1
- package/core/uploads.js +3 -1
- package/core/uploads.js.map +1 -1
- package/core/uploads.mjs +1 -0
- package/core/uploads.mjs.map +1 -1
- package/helpers/zod.d.mts +20 -7
- package/helpers/zod.d.mts.map +1 -1
- package/helpers/zod.d.ts +20 -7
- package/helpers/zod.d.ts.map +1 -1
- package/helpers/zod.js +24 -12
- package/helpers/zod.js.map +1 -1
- package/helpers/zod.mjs +24 -12
- package/helpers/zod.mjs.map +1 -1
- package/index.d.mts +1 -1
- package/index.d.mts.map +1 -1
- package/index.d.ts +1 -1
- package/index.d.ts.map +1 -1
- package/index.js +2 -1
- package/index.js.map +1 -1
- package/index.mjs +1 -1
- package/index.mjs.map +1 -1
- package/internal/headers.d.mts.map +1 -1
- package/internal/headers.d.ts.map +1 -1
- package/internal/headers.js +7 -3
- package/internal/headers.js.map +1 -1
- package/internal/headers.mjs +7 -3
- package/internal/headers.mjs.map +1 -1
- package/internal/uploads.d.mts +26 -1
- package/internal/uploads.d.mts.map +1 -1
- package/internal/uploads.d.ts +26 -1
- package/internal/uploads.d.ts.map +1 -1
- package/internal/uploads.js +169 -1
- package/internal/uploads.js.map +1 -1
- package/internal/uploads.mjs +169 -2
- package/internal/uploads.mjs.map +1 -1
- package/lib/AbstractChatCompletionRunner.d.mts +3 -3
- package/lib/AbstractChatCompletionRunner.d.mts.map +1 -1
- package/lib/AbstractChatCompletionRunner.d.ts +3 -3
- package/lib/AbstractChatCompletionRunner.d.ts.map +1 -1
- package/lib/AbstractChatCompletionRunner.js +4 -3
- package/lib/AbstractChatCompletionRunner.js.map +1 -1
- package/lib/AbstractChatCompletionRunner.mjs +4 -3
- package/lib/AbstractChatCompletionRunner.mjs.map +1 -1
- package/lib/AssistantStream.d.mts.map +1 -1
- package/lib/AssistantStream.d.ts.map +1 -1
- package/lib/AssistantStream.js +3 -2
- package/lib/AssistantStream.js.map +1 -1
- package/lib/AssistantStream.mjs +3 -2
- package/lib/AssistantStream.mjs.map +1 -1
- package/lib/ChatCompletionRunner.d.mts +26 -2
- package/lib/ChatCompletionRunner.d.mts.map +1 -1
- package/lib/ChatCompletionRunner.d.ts +26 -2
- package/lib/ChatCompletionRunner.d.ts.map +1 -1
- package/lib/ChatCompletionRunner.js.map +1 -1
- package/lib/ChatCompletionRunner.mjs.map +1 -1
- package/lib/ChatCompletionStream.d.mts +14 -1
- package/lib/ChatCompletionStream.d.mts.map +1 -1
- package/lib/ChatCompletionStream.d.ts +14 -1
- package/lib/ChatCompletionStream.d.ts.map +1 -1
- package/lib/ChatCompletionStream.js +68 -12
- package/lib/ChatCompletionStream.js.map +1 -1
- package/lib/ChatCompletionStream.mjs +67 -12
- package/lib/ChatCompletionStream.mjs.map +1 -1
- package/lib/ChatCompletionStreamingRunner.d.mts +25 -2
- package/lib/ChatCompletionStreamingRunner.d.mts.map +1 -1
- package/lib/ChatCompletionStreamingRunner.d.ts +25 -2
- package/lib/ChatCompletionStreamingRunner.d.ts.map +1 -1
- package/lib/ChatCompletionStreamingRunner.js +77 -0
- package/lib/ChatCompletionStreamingRunner.js.map +1 -1
- package/lib/ChatCompletionStreamingRunner.mjs +78 -1
- package/lib/ChatCompletionStreamingRunner.mjs.map +1 -1
- package/lib/EventStream.d.mts +13 -0
- package/lib/EventStream.d.mts.map +1 -1
- package/lib/EventStream.d.ts +13 -0
- package/lib/EventStream.d.ts.map +1 -1
- package/lib/EventStream.js +107 -2
- package/lib/EventStream.js.map +1 -1
- package/lib/EventStream.mjs +107 -2
- package/lib/EventStream.mjs.map +1 -1
- package/lib/RunnableFunction.d.mts +38 -21
- package/lib/RunnableFunction.d.mts.map +1 -1
- package/lib/RunnableFunction.d.ts +38 -21
- package/lib/RunnableFunction.d.ts.map +1 -1
- package/lib/RunnableFunction.js.map +1 -1
- package/lib/RunnableFunction.mjs.map +1 -1
- package/lib/parser.d.mts +2 -2
- package/lib/parser.d.mts.map +1 -1
- package/lib/parser.d.ts +2 -2
- package/lib/parser.d.ts.map +1 -1
- package/lib/parser.js.map +1 -1
- package/lib/parser.mjs.map +1 -1
- package/lib/responses/ResponseStream.d.mts +6 -1
- package/lib/responses/ResponseStream.d.mts.map +1 -1
- package/lib/responses/ResponseStream.d.ts +6 -1
- package/lib/responses/ResponseStream.d.ts.map +1 -1
- package/lib/responses/ResponseStream.js +21 -0
- package/lib/responses/ResponseStream.js.map +1 -1
- package/lib/responses/ResponseStream.mjs +21 -0
- package/lib/responses/ResponseStream.mjs.map +1 -1
- package/package.json +1 -1
- package/resources/admin/organization/audit-logs.d.mts +2 -2
- package/resources/admin/organization/audit-logs.d.mts.map +1 -1
- package/resources/admin/organization/audit-logs.d.ts +2 -2
- package/resources/admin/organization/audit-logs.d.ts.map +1 -1
- package/resources/admin/organization/projects/api-keys.d.mts +12 -0
- package/resources/admin/organization/projects/api-keys.d.mts.map +1 -1
- package/resources/admin/organization/projects/api-keys.d.ts +12 -0
- package/resources/admin/organization/projects/api-keys.d.ts.map +1 -1
- package/resources/admin/organization/projects/index.d.mts +1 -1
- package/resources/admin/organization/projects/index.d.mts.map +1 -1
- package/resources/admin/organization/projects/index.d.ts +1 -1
- package/resources/admin/organization/projects/index.d.ts.map +1 -1
- package/resources/admin/organization/projects/index.js +4 -4
- package/resources/admin/organization/projects/index.js.map +1 -1
- package/resources/admin/organization/projects/index.mjs +1 -1
- package/resources/admin/organization/projects/index.mjs.map +1 -1
- package/resources/admin/organization/projects/projects.d.mts +2 -2
- package/resources/admin/organization/projects/projects.d.mts.map +1 -1
- package/resources/admin/organization/projects/projects.d.ts +2 -2
- package/resources/admin/organization/projects/projects.d.ts.map +1 -1
- package/resources/admin/organization/projects/projects.js +2 -2
- package/resources/admin/organization/projects/projects.js.map +1 -1
- package/resources/admin/organization/projects/projects.mjs +2 -2
- package/resources/admin/organization/projects/projects.mjs.map +1 -1
- package/resources/admin/organization/projects/service-accounts/api-keys.d.mts +58 -0
- package/resources/admin/organization/projects/service-accounts/api-keys.d.mts.map +1 -0
- package/resources/admin/organization/projects/service-accounts/api-keys.d.ts +58 -0
- package/resources/admin/organization/projects/service-accounts/api-keys.d.ts.map +1 -0
- package/resources/admin/organization/projects/service-accounts/api-keys.js +26 -0
- package/resources/admin/organization/projects/service-accounts/api-keys.js.map +1 -0
- package/resources/admin/organization/projects/service-accounts/api-keys.mjs +22 -0
- package/resources/admin/organization/projects/service-accounts/api-keys.mjs.map +1 -0
- package/resources/admin/organization/projects/service-accounts/index.d.mts +3 -0
- package/resources/admin/organization/projects/service-accounts/index.d.mts.map +1 -0
- package/resources/admin/organization/projects/service-accounts/index.d.ts +3 -0
- package/resources/admin/organization/projects/service-accounts/index.d.ts.map +1 -0
- package/resources/admin/organization/projects/service-accounts/index.js +9 -0
- package/resources/admin/organization/projects/service-accounts/index.js.map +1 -0
- package/resources/admin/organization/projects/service-accounts/index.mjs +4 -0
- package/resources/admin/organization/projects/service-accounts/index.mjs.map +1 -0
- package/resources/admin/organization/projects/service-accounts/service-accounts.d.mts +177 -0
- package/resources/admin/organization/projects/service-accounts/service-accounts.d.mts.map +1 -0
- package/resources/admin/organization/projects/service-accounts/service-accounts.d.ts +177 -0
- package/resources/admin/organization/projects/service-accounts/service-accounts.d.ts.map +1 -0
- package/resources/admin/organization/projects/service-accounts/service-accounts.js +109 -0
- package/resources/admin/organization/projects/service-accounts/service-accounts.js.map +1 -0
- package/resources/admin/organization/projects/service-accounts/service-accounts.mjs +104 -0
- package/resources/admin/organization/projects/service-accounts/service-accounts.mjs.map +1 -0
- package/resources/admin/organization/projects/service-accounts.d.mts +1 -167
- package/resources/admin/organization/projects/service-accounts.d.mts.map +1 -1
- package/resources/admin/organization/projects/service-accounts.d.ts +1 -167
- package/resources/admin/organization/projects/service-accounts.d.ts.map +1 -1
- package/resources/admin/organization/projects/service-accounts.js +2 -97
- package/resources/admin/organization/projects/service-accounts.js.map +1 -1
- package/resources/admin/organization/projects/service-accounts.mjs +1 -95
- package/resources/admin/organization/projects/service-accounts.mjs.map +1 -1
- package/resources/admin/organization/usage.d.mts +517 -88
- package/resources/admin/organization/usage.d.mts.map +1 -1
- package/resources/admin/organization/usage.d.ts +517 -88
- package/resources/admin/organization/usage.d.ts.map +1 -1
- package/resources/beta/responses/responses.d.mts +7 -6
- package/resources/beta/responses/responses.d.mts.map +1 -1
- package/resources/beta/responses/responses.d.ts +7 -6
- 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/chat/completions/completions.d.mts +15 -6
- package/resources/chat/completions/completions.d.mts.map +1 -1
- package/resources/chat/completions/completions.d.ts +15 -6
- package/resources/chat/completions/completions.d.ts.map +1 -1
- package/resources/chat/completions/completions.js.map +1 -1
- package/resources/chat/completions/completions.mjs.map +1 -1
- package/resources/responses/responses.d.mts +3 -2
- package/resources/responses/responses.d.mts.map +1 -1
- package/resources/responses/responses.d.ts +3 -2
- 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 +2 -54
- package/resources/webhooks/webhooks.d.mts.map +1 -1
- package/resources/webhooks/webhooks.d.ts +2 -54
- package/resources/webhooks/webhooks.d.ts.map +1 -1
- package/src/client.ts +13 -5
- package/src/core/streaming.ts +36 -8
- package/src/core/uploads.ts +6 -1
- package/src/helpers/zod.ts +62 -23
- package/src/index.ts +1 -1
- package/src/internal/headers.ts +7 -3
- package/src/internal/uploads.ts +214 -4
- package/src/lib/AbstractChatCompletionRunner.ts +17 -9
- package/src/lib/AssistantStream.ts +3 -2
- package/src/lib/ChatCompletionRunner.ts +46 -6
- package/src/lib/ChatCompletionStream.ts +108 -13
- package/src/lib/ChatCompletionStreamingRunner.ts +144 -7
- package/src/lib/EventStream.ts +114 -4
- package/src/lib/RunnableFunction.ts +60 -31
- package/src/lib/parser.ts +4 -0
- package/src/lib/responses/ResponseStream.ts +29 -1
- package/src/resources/admin/organization/audit-logs.ts +169 -1
- package/src/resources/admin/organization/projects/api-keys.ts +15 -1
- package/src/resources/admin/organization/projects/index.ts +1 -1
- package/src/resources/admin/organization/projects/projects.ts +13 -13
- package/src/resources/admin/organization/projects/service-accounts/api-keys.ts +80 -0
- package/src/resources/admin/organization/projects/service-accounts/index.ts +15 -0
- package/src/resources/admin/organization/projects/service-accounts/service-accounts.ts +284 -0
- package/src/resources/admin/organization/projects/service-accounts.ts +1 -264
- package/src/resources/admin/organization/usage.ts +616 -88
- package/src/resources/beta/responses/responses.ts +7 -6
- package/src/resources/chat/completions/completions.ts +66 -9
- package/src/resources/responses/responses.ts +3 -2
- package/src/resources/webhooks/api.md +0 -1
- package/src/resources/webhooks/webhooks.ts +1 -65
- package/src/version.ts +1 -1
- package/version.d.mts +1 -1
- package/version.d.ts +1 -1
- package/version.js +1 -1
- package/version.mjs +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"webhooks.d.ts","sourceRoot":"","sources":["../../src/resources/webhooks/webhooks.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,WAAW,EAAE,+BAA4B;AAClD,OAAO,EAAgB,WAAW,EAAE,kCAA+B;AAEnE,qBAAa,QAAS,SAAQ,WAA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C7D,KAAK,0BAA0B,IAAI,0BAA0B,EAC7D,KAAK,wBAAwB,IAAI,wBAAwB,EACzD,KAAK,uBAAuB,IAAI,uBAAuB,EACvD,KAAK,2BAA2B,IAAI,2BAA2B,EAC/D,KAAK,yBAAyB,IAAI,yBAAyB,EAC3D,KAAK,4BAA4B,IAAI,4BAA4B,EACjE,KAAK,kCAAkC,IAAI,kCAAkC,EAC7E,KAAK,+BAA+B,IAAI,+BAA+B,EACvE,KAAK,kCAAkC,IAAI,kCAAkC,EAC7E,KAAK,gCAAgC,IAAI,gCAAgC,EACzE,KAAK,6BAA6B,IAAI,6BAA6B,EACnE,KAAK,6BAA6B,IAAI,6BAA6B,EACnE,KAAK,0BAA0B,IAAI,0BAA0B,EAC7D,KAAK,8BAA8B,IAAI,8BAA8B,EACrE,KAAK,kBAAkB,IAAI,kBAAkB,GAC9C,CAAC;CACH"}
|
package/src/client.ts
CHANGED
|
@@ -765,6 +765,7 @@ export class OpenAI {
|
|
|
765
765
|
const { req, url, timeout } = await this.buildRequest(options, {
|
|
766
766
|
retryCount: maxRetries - retriesRemaining,
|
|
767
767
|
});
|
|
768
|
+
const hasStreamingBody = options.__metadata?.['hasStreamingBody'] === true;
|
|
768
769
|
|
|
769
770
|
await this.prepareRequest(req, { url, options });
|
|
770
771
|
await this._provider?.prepareRequest?.(req, { url, options });
|
|
@@ -806,7 +807,7 @@ export class OpenAI {
|
|
|
806
807
|
const isTimeout =
|
|
807
808
|
isAbortError(response) ||
|
|
808
809
|
/timed? ?out/i.test(String(response) + ('cause' in response ? String(response.cause) : ''));
|
|
809
|
-
if (retriesRemaining) {
|
|
810
|
+
if (retriesRemaining && !hasStreamingBody) {
|
|
810
811
|
loggerFor(this).info(
|
|
811
812
|
`[${requestLogID}] connection ${isTimeout ? 'timed out' : 'failed'} - ${retryMessage}`,
|
|
812
813
|
);
|
|
@@ -821,11 +822,13 @@ export class OpenAI {
|
|
|
821
822
|
);
|
|
822
823
|
return this.retryRequest(options, retriesRemaining, retryOfRequestLogID ?? requestLogID);
|
|
823
824
|
}
|
|
825
|
+
const terminalMessage =
|
|
826
|
+
hasStreamingBody ? 'error; streaming body cannot be retried' : 'error; no more retries left';
|
|
824
827
|
loggerFor(this).info(
|
|
825
|
-
`[${requestLogID}] connection ${isTimeout ? 'timed out' : 'failed'} -
|
|
828
|
+
`[${requestLogID}] connection ${isTimeout ? 'timed out' : 'failed'} - ${terminalMessage}`,
|
|
826
829
|
);
|
|
827
830
|
loggerFor(this).debug(
|
|
828
|
-
`[${requestLogID}] connection ${isTimeout ? 'timed out' : 'failed'} (
|
|
831
|
+
`[${requestLogID}] connection ${isTimeout ? 'timed out' : 'failed'} (${terminalMessage})`,
|
|
829
832
|
formatRequestDetails({
|
|
830
833
|
retryOfRequestLogID,
|
|
831
834
|
url,
|
|
@@ -878,7 +881,7 @@ export class OpenAI {
|
|
|
878
881
|
}
|
|
879
882
|
|
|
880
883
|
const shouldRetry = await this.shouldRetry(response);
|
|
881
|
-
if (retriesRemaining && shouldRetry) {
|
|
884
|
+
if (retriesRemaining && shouldRetry && !hasStreamingBody) {
|
|
882
885
|
const retryMessage = `retrying, ${retriesRemaining} attempts remaining`;
|
|
883
886
|
|
|
884
887
|
// We don't need the body of this response.
|
|
@@ -902,7 +905,11 @@ export class OpenAI {
|
|
|
902
905
|
);
|
|
903
906
|
}
|
|
904
907
|
|
|
905
|
-
const retryMessage =
|
|
908
|
+
const retryMessage =
|
|
909
|
+
shouldRetry ?
|
|
910
|
+
hasStreamingBody ? `error; streaming body cannot be retried`
|
|
911
|
+
: `error; no more retries left`
|
|
912
|
+
: `error; not retryable`;
|
|
906
913
|
|
|
907
914
|
loggerFor(this).info(`${responseInfo} - ${retryMessage}`);
|
|
908
915
|
|
|
@@ -1278,6 +1285,7 @@ export class OpenAI {
|
|
|
1278
1285
|
static InvalidWebhookSignatureError = Errors.InvalidWebhookSignatureError;
|
|
1279
1286
|
|
|
1280
1287
|
static toFile = Uploads.toFile;
|
|
1288
|
+
static toStreamingFile = Uploads.toStreamingFile;
|
|
1281
1289
|
|
|
1282
1290
|
/**
|
|
1283
1291
|
* Given a prompt, the model will return one or more predicted completions, and can also return the probabilities of alternative tokens at each position.
|
package/src/core/streaming.ts
CHANGED
|
@@ -114,16 +114,44 @@ export class Stream<Item> implements AsyncIterable<Item> {
|
|
|
114
114
|
|
|
115
115
|
async function* iterLines(): AsyncGenerator<string, void, unknown> {
|
|
116
116
|
const lineDecoder = new LineDecoder();
|
|
117
|
+
const reader = readableStream.getReader();
|
|
118
|
+
let closed = false;
|
|
119
|
+
let cancelPromise: Promise<void> | undefined;
|
|
120
|
+
const cancel = () => {
|
|
121
|
+
cancelPromise ??= reader.cancel();
|
|
122
|
+
cancelPromise.catch(() => {});
|
|
123
|
+
};
|
|
117
124
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
125
|
+
controller.signal.addEventListener('abort', cancel, { once: true });
|
|
126
|
+
try {
|
|
127
|
+
if (controller.signal.aborted) {
|
|
128
|
+
cancel();
|
|
129
|
+
return;
|
|
122
130
|
}
|
|
123
|
-
}
|
|
124
131
|
|
|
125
|
-
|
|
126
|
-
|
|
132
|
+
while (true) {
|
|
133
|
+
const { value: chunk, done } = await reader.read();
|
|
134
|
+
if (done) {
|
|
135
|
+
closed = true;
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
if (controller.signal.aborted) return;
|
|
139
|
+
|
|
140
|
+
for (const line of lineDecoder.decode(chunk)) {
|
|
141
|
+
if (controller.signal.aborted) return;
|
|
142
|
+
yield line;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (controller.signal.aborted) return;
|
|
147
|
+
for (const line of lineDecoder.flush()) {
|
|
148
|
+
if (controller.signal.aborted) return;
|
|
149
|
+
yield line;
|
|
150
|
+
}
|
|
151
|
+
} finally {
|
|
152
|
+
controller.signal.removeEventListener('abort', cancel);
|
|
153
|
+
if (!closed) cancel();
|
|
154
|
+
reader.releaseLock();
|
|
127
155
|
}
|
|
128
156
|
}
|
|
129
157
|
|
|
@@ -141,7 +169,7 @@ export class Stream<Item> implements AsyncIterable<Item> {
|
|
|
141
169
|
done = true;
|
|
142
170
|
} catch (e) {
|
|
143
171
|
// If the user calls `stream.controller.abort()`, we should exit without throwing.
|
|
144
|
-
if (isAbortError(e)) return;
|
|
172
|
+
if (controller.signal.aborted || isAbortError(e)) return;
|
|
145
173
|
throw e;
|
|
146
174
|
} finally {
|
|
147
175
|
// If the user `break`s, abort the ongoing request.
|
package/src/core/uploads.ts
CHANGED
package/src/helpers/zod.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { ResponseFormatJSONSchema } from '../resources/index';
|
|
2
2
|
import * as z3 from 'zod/v3';
|
|
3
3
|
import * as z4 from 'zod/v4';
|
|
4
|
+
import type * as z4Mini from 'zod/v4-mini';
|
|
4
5
|
import {
|
|
5
6
|
AutoParseableResponseFormat,
|
|
6
7
|
AutoParseableTextFormat,
|
|
@@ -15,9 +16,25 @@ import { type ResponseFormatTextJSONSchemaConfig } from '../resources/responses/
|
|
|
15
16
|
import { toStrictJsonSchema } from '../lib/transform';
|
|
16
17
|
import { JSONSchema } from '../lib/jsonschema';
|
|
17
18
|
|
|
18
|
-
type
|
|
19
|
-
|
|
20
|
-
|
|
19
|
+
type ZodV4Schema = z4.ZodType | z4Mini.ZodMiniType;
|
|
20
|
+
type ZodSchema = z3.ZodType | ZodV4Schema;
|
|
21
|
+
|
|
22
|
+
// The public helpers only need Zod's output type and, when available, parser. Using these small
|
|
23
|
+
// structural shapes avoids expanding Zod's full v3/v4 type graphs in Deno.
|
|
24
|
+
type ZodTypeLike = (
|
|
25
|
+
| { _output: unknown }
|
|
26
|
+
| {
|
|
27
|
+
_zod: {
|
|
28
|
+
output: unknown;
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
) & {
|
|
32
|
+
parse?: (data: unknown) => unknown;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
type InferZodType<T extends ZodTypeLike> =
|
|
36
|
+
T extends { _output: infer Output } ? Output
|
|
37
|
+
: T extends { _zod: { output: infer Output } } ? Output
|
|
21
38
|
: never;
|
|
22
39
|
|
|
23
40
|
function zodV3ToJsonSchema(schema: z3.ZodType, options: { name: string }): Record<string, unknown> {
|
|
@@ -30,7 +47,7 @@ function zodV3ToJsonSchema(schema: z3.ZodType, options: { name: string }): Recor
|
|
|
30
47
|
});
|
|
31
48
|
}
|
|
32
49
|
|
|
33
|
-
function zodV4ToJsonSchema(schema:
|
|
50
|
+
function zodV4ToJsonSchema(schema: ZodV4Schema): Record<string, unknown> {
|
|
34
51
|
return toStrictJsonSchema(
|
|
35
52
|
z4.toJSONSchema(schema, {
|
|
36
53
|
target: 'draft-7',
|
|
@@ -54,10 +71,24 @@ function zodV4ToJsonSchema(schema: z4.ZodType): Record<string, unknown> {
|
|
|
54
71
|
) as Record<string, unknown>;
|
|
55
72
|
}
|
|
56
73
|
|
|
57
|
-
function isZodV4(zodObject:
|
|
74
|
+
function isZodV4(zodObject: ZodSchema): zodObject is ZodV4Schema {
|
|
58
75
|
return '_zod' in zodObject;
|
|
59
76
|
}
|
|
60
77
|
|
|
78
|
+
function parseZodObject<ZodInput extends ZodTypeLike>(
|
|
79
|
+
zodObject: ZodInput,
|
|
80
|
+
content: string,
|
|
81
|
+
): InferZodType<ZodInput> {
|
|
82
|
+
const parsed = JSON.parse(content);
|
|
83
|
+
const parser = (zodObject as { parse?: (data: unknown) => unknown }).parse;
|
|
84
|
+
|
|
85
|
+
if (typeof parser === 'function') {
|
|
86
|
+
return parser.call(zodObject, parsed) as InferZodType<ZodInput>;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return z4.parse(zodObject as unknown as ZodV4Schema, parsed) as InferZodType<ZodInput>;
|
|
90
|
+
}
|
|
91
|
+
|
|
61
92
|
/**
|
|
62
93
|
* Creates a chat completion `JSONSchema` response format object from
|
|
63
94
|
* the given Zod schema.
|
|
@@ -95,39 +126,43 @@ function isZodV4(zodObject: z3.ZodType | z4.ZodType): zodObject is z4.ZodType {
|
|
|
95
126
|
* This can be passed directly to the `.create()` method but will not
|
|
96
127
|
* result in any automatic parsing, you'll have to parse the response yourself.
|
|
97
128
|
*/
|
|
98
|
-
export function zodResponseFormat<ZodInput extends
|
|
129
|
+
export function zodResponseFormat<ZodInput extends ZodTypeLike>(
|
|
99
130
|
zodObject: ZodInput,
|
|
100
131
|
name: string,
|
|
101
132
|
props?: Omit<ResponseFormatJSONSchema.JSONSchema, 'schema' | 'strict' | 'name'>,
|
|
102
133
|
): AutoParseableResponseFormat<InferZodType<ZodInput>> {
|
|
103
|
-
|
|
134
|
+
const zodSchema = zodObject as unknown as ZodSchema;
|
|
135
|
+
|
|
136
|
+
return makeParseableResponseFormat<InferZodType<ZodInput>>(
|
|
104
137
|
{
|
|
105
138
|
type: 'json_schema',
|
|
106
139
|
json_schema: {
|
|
107
140
|
...props,
|
|
108
141
|
name,
|
|
109
142
|
strict: true,
|
|
110
|
-
schema: isZodV4(
|
|
143
|
+
schema: isZodV4(zodSchema) ? zodV4ToJsonSchema(zodSchema) : zodV3ToJsonSchema(zodSchema, { name }),
|
|
111
144
|
},
|
|
112
145
|
},
|
|
113
|
-
(content) => zodObject
|
|
146
|
+
(content) => parseZodObject(zodObject, content),
|
|
114
147
|
);
|
|
115
148
|
}
|
|
116
149
|
|
|
117
|
-
export function zodTextFormat<ZodInput extends
|
|
150
|
+
export function zodTextFormat<ZodInput extends ZodTypeLike>(
|
|
118
151
|
zodObject: ZodInput,
|
|
119
152
|
name: string,
|
|
120
153
|
props?: Omit<ResponseFormatTextJSONSchemaConfig, 'schema' | 'type' | 'strict' | 'name'>,
|
|
121
154
|
): AutoParseableTextFormat<InferZodType<ZodInput>> {
|
|
122
|
-
|
|
155
|
+
const zodSchema = zodObject as unknown as ZodSchema;
|
|
156
|
+
|
|
157
|
+
return makeParseableTextFormat<InferZodType<ZodInput>>(
|
|
123
158
|
{
|
|
124
159
|
type: 'json_schema',
|
|
125
160
|
...props,
|
|
126
161
|
name,
|
|
127
162
|
strict: true,
|
|
128
|
-
schema: isZodV4(
|
|
163
|
+
schema: isZodV4(zodSchema) ? zodV4ToJsonSchema(zodSchema) : zodV3ToJsonSchema(zodSchema, { name }),
|
|
129
164
|
},
|
|
130
|
-
(content) => zodObject
|
|
165
|
+
(content) => parseZodObject(zodObject, content),
|
|
131
166
|
);
|
|
132
167
|
}
|
|
133
168
|
|
|
@@ -136,7 +171,7 @@ export function zodTextFormat<ZodInput extends z3.ZodType | z4.ZodType>(
|
|
|
136
171
|
* automatically by the chat completion `.runTools()` method or automatically
|
|
137
172
|
* parsed by `.parse()` / `.stream()`.
|
|
138
173
|
*/
|
|
139
|
-
export function zodFunction<Parameters extends
|
|
174
|
+
export function zodFunction<Parameters extends ZodTypeLike>(options: {
|
|
140
175
|
name: string;
|
|
141
176
|
parameters: Parameters;
|
|
142
177
|
function?: ((args: InferZodType<Parameters>) => unknown | Promise<unknown>) | undefined;
|
|
@@ -146,6 +181,8 @@ export function zodFunction<Parameters extends z3.ZodType | z4.ZodType>(options:
|
|
|
146
181
|
name: string;
|
|
147
182
|
function: (args: InferZodType<Parameters>) => unknown;
|
|
148
183
|
}> {
|
|
184
|
+
const zodSchema = options.parameters as unknown as ZodSchema;
|
|
185
|
+
|
|
149
186
|
// @ts-expect-error TODO
|
|
150
187
|
return makeParseableTool<any>(
|
|
151
188
|
{
|
|
@@ -153,21 +190,21 @@ export function zodFunction<Parameters extends z3.ZodType | z4.ZodType>(options:
|
|
|
153
190
|
function: {
|
|
154
191
|
name: options.name,
|
|
155
192
|
parameters:
|
|
156
|
-
isZodV4(
|
|
157
|
-
zodV4ToJsonSchema(
|
|
158
|
-
: zodV3ToJsonSchema(
|
|
193
|
+
isZodV4(zodSchema) ?
|
|
194
|
+
zodV4ToJsonSchema(zodSchema)
|
|
195
|
+
: zodV3ToJsonSchema(zodSchema, { name: options.name }),
|
|
159
196
|
strict: true,
|
|
160
197
|
...(options.description ? { description: options.description } : undefined),
|
|
161
198
|
},
|
|
162
199
|
},
|
|
163
200
|
{
|
|
164
201
|
callback: options.function,
|
|
165
|
-
parser: (args) => options.parameters
|
|
202
|
+
parser: (args) => parseZodObject(options.parameters, args),
|
|
166
203
|
},
|
|
167
204
|
);
|
|
168
205
|
}
|
|
169
206
|
|
|
170
|
-
export function zodResponsesFunction<Parameters extends
|
|
207
|
+
export function zodResponsesFunction<Parameters extends ZodTypeLike>(options: {
|
|
171
208
|
name: string;
|
|
172
209
|
parameters: Parameters;
|
|
173
210
|
function?: ((args: InferZodType<Parameters>) => unknown | Promise<unknown>) | undefined;
|
|
@@ -177,20 +214,22 @@ export function zodResponsesFunction<Parameters extends z3.ZodType | z4.ZodType>
|
|
|
177
214
|
name: string;
|
|
178
215
|
function: (args: InferZodType<Parameters>) => unknown;
|
|
179
216
|
}> {
|
|
217
|
+
const zodSchema = options.parameters as unknown as ZodSchema;
|
|
218
|
+
|
|
180
219
|
return makeParseableResponseTool<any>(
|
|
181
220
|
{
|
|
182
221
|
type: 'function',
|
|
183
222
|
name: options.name,
|
|
184
223
|
parameters:
|
|
185
|
-
isZodV4(
|
|
186
|
-
zodV4ToJsonSchema(
|
|
187
|
-
: zodV3ToJsonSchema(
|
|
224
|
+
isZodV4(zodSchema) ?
|
|
225
|
+
zodV4ToJsonSchema(zodSchema)
|
|
226
|
+
: zodV3ToJsonSchema(zodSchema, { name: options.name }),
|
|
188
227
|
strict: true,
|
|
189
228
|
...(options.description ? { description: options.description } : undefined),
|
|
190
229
|
},
|
|
191
230
|
{
|
|
192
231
|
callback: options.function,
|
|
193
|
-
parser: (args) => options.parameters
|
|
232
|
+
parser: (args) => parseZodObject(options.parameters, args),
|
|
194
233
|
},
|
|
195
234
|
);
|
|
196
235
|
}
|
package/src/index.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
export { OpenAI as default } from './client';
|
|
4
4
|
|
|
5
|
-
export { type Uploadable, toFile } from './core/uploads';
|
|
5
|
+
export { type Uploadable, toFile, toStreamingFile } from './core/uploads';
|
|
6
6
|
export { APIPromise } from './core/api-promise';
|
|
7
7
|
export { OpenAI, type ClientOptions } from './client';
|
|
8
8
|
export { PagePromise } from './core/pagination';
|
package/src/internal/headers.ts
CHANGED
|
@@ -12,6 +12,7 @@ export type HeadersLike =
|
|
|
12
12
|
| NullableHeaders;
|
|
13
13
|
|
|
14
14
|
const brand_privateNullableHeaders = /* @__PURE__ */ Symbol('brand.privateNullableHeaders');
|
|
15
|
+
const httpTokenHeaderName = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/;
|
|
15
16
|
|
|
16
17
|
/**
|
|
17
18
|
* @internal
|
|
@@ -74,16 +75,19 @@ export const buildHeaders = (newHeaders: HeadersLike[]): NullableHeaders => {
|
|
|
74
75
|
for (const headers of newHeaders) {
|
|
75
76
|
const seenHeaders = new Set<string>();
|
|
76
77
|
for (const [name, value] of iterateHeaders(headers)) {
|
|
78
|
+
if (!httpTokenHeaderName.test(name)) {
|
|
79
|
+
throw new TypeError(`Header name must be a valid HTTP token ["${name}"]`);
|
|
80
|
+
}
|
|
77
81
|
const lowerName = name.toLowerCase();
|
|
78
82
|
if (!seenHeaders.has(lowerName)) {
|
|
79
|
-
targetHeaders.delete(
|
|
83
|
+
targetHeaders.delete(lowerName);
|
|
80
84
|
seenHeaders.add(lowerName);
|
|
81
85
|
}
|
|
82
86
|
if (value === null) {
|
|
83
|
-
targetHeaders.delete(
|
|
87
|
+
targetHeaders.delete(lowerName);
|
|
84
88
|
nullHeaders.add(lowerName);
|
|
85
89
|
} else {
|
|
86
|
-
targetHeaders.append(
|
|
90
|
+
targetHeaders.append(lowerName, value);
|
|
87
91
|
nullHeaders.delete(lowerName);
|
|
88
92
|
}
|
|
89
93
|
}
|
package/src/internal/uploads.ts
CHANGED
|
@@ -1,16 +1,61 @@
|
|
|
1
1
|
import { type RequestOptions } from './request-options';
|
|
2
2
|
import type { FilePropertyBag, Fetch } from './builtin-types';
|
|
3
3
|
import type { OpenAI } from '../client';
|
|
4
|
-
import {
|
|
4
|
+
import { buildHeaders } from './headers';
|
|
5
|
+
import { ReadableStreamFrom, ReadableStreamToAsyncIterable } from './shims';
|
|
6
|
+
import type { ReadableStream } from './shim-types';
|
|
7
|
+
import { encodeUTF8 } from './utils/bytes';
|
|
5
8
|
|
|
6
9
|
export type BlobPart = string | ArrayBuffer | ArrayBufferView | Blob | DataView;
|
|
7
10
|
type FsReadStream = AsyncIterable<Uint8Array> & { path: string | { toString(): string } };
|
|
8
11
|
|
|
12
|
+
export type StreamingFileInput = AsyncIterable<BlobPart> | ReadableStream<BlobPart>;
|
|
13
|
+
|
|
14
|
+
const brand_privateStreamingFile = /* @__PURE__ */ Symbol('brand.privateStreamingFile');
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* A file whose contents are read lazily while the multipart request is sent.
|
|
18
|
+
* Create one with {@link toStreamingFile} when buffering an upload into a `File` is undesirable.
|
|
19
|
+
*/
|
|
20
|
+
export interface StreamingFile {
|
|
21
|
+
/** Brand check, prevent users from creating a StreamingFile without a filename. */
|
|
22
|
+
readonly [brand_privateStreamingFile]: true;
|
|
23
|
+
readonly data: StreamingFileInput;
|
|
24
|
+
readonly name: string;
|
|
25
|
+
readonly type?: string | undefined;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Wrap a stream as an uploadable file without reading it into memory.
|
|
30
|
+
*
|
|
31
|
+
* Unlike {@link toFile}, this helper does not create a web `File`, because the `File` constructor
|
|
32
|
+
* must consume all of its contents up front. The stream is instead encoded lazily as multipart
|
|
33
|
+
* form data when the request is sent.
|
|
34
|
+
*/
|
|
35
|
+
export function toStreamingFile(
|
|
36
|
+
data: StreamingFileInput,
|
|
37
|
+
name: string,
|
|
38
|
+
options?: Pick<FilePropertyBag, 'type'>,
|
|
39
|
+
): StreamingFile {
|
|
40
|
+
if (!name) {
|
|
41
|
+
throw new TypeError('toStreamingFile requires a non-empty file name');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return {
|
|
45
|
+
[brand_privateStreamingFile]: true,
|
|
46
|
+
data,
|
|
47
|
+
name,
|
|
48
|
+
...(options?.type ? { type: options.type } : {}),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
9
52
|
// https://github.com/oven-sh/bun/issues/5980
|
|
10
53
|
interface BunFile extends Blob {
|
|
11
54
|
readonly name?: string | undefined;
|
|
12
55
|
}
|
|
13
56
|
|
|
57
|
+
type NamedBlob = Blob & { readonly name?: string | undefined };
|
|
58
|
+
|
|
14
59
|
export const checkFileSupport = () => {
|
|
15
60
|
if (typeof File === 'undefined') {
|
|
16
61
|
const { process } = globalThis as any;
|
|
@@ -34,7 +79,15 @@ export const checkFileSupport = () => {
|
|
|
34
79
|
* For convenience, you can also pass a fetch Response, or in Node,
|
|
35
80
|
* the result of fs.createReadStream().
|
|
36
81
|
*/
|
|
37
|
-
export type Uploadable =
|
|
82
|
+
export type Uploadable =
|
|
83
|
+
| File
|
|
84
|
+
| Response
|
|
85
|
+
| FsReadStream
|
|
86
|
+
| BunFile
|
|
87
|
+
| NamedBlob
|
|
88
|
+
| AsyncIterable<BlobPart>
|
|
89
|
+
| ReadableStream<BlobPart>
|
|
90
|
+
| StreamingFile;
|
|
38
91
|
|
|
39
92
|
/**
|
|
40
93
|
* Construct a `File` instance. This is used to ensure a helpful error is thrown
|
|
@@ -78,6 +131,10 @@ export const maybeMultipartFormRequestOptions = async (
|
|
|
78
131
|
): Promise<RequestOptions> => {
|
|
79
132
|
if (!hasUploadableValue(opts.body)) return opts;
|
|
80
133
|
|
|
134
|
+
if (hasStreamingUploadableValue(opts.body)) {
|
|
135
|
+
return createStreamingFormRequestOptions(opts);
|
|
136
|
+
}
|
|
137
|
+
|
|
81
138
|
return { ...opts, body: await createForm(opts.body, fetch) };
|
|
82
139
|
};
|
|
83
140
|
|
|
@@ -87,6 +144,10 @@ export const multipartFormRequestOptions = async (
|
|
|
87
144
|
opts: MultipartFormRequestOptions,
|
|
88
145
|
fetch: OpenAI | Fetch,
|
|
89
146
|
): Promise<RequestOptions> => {
|
|
147
|
+
if (hasStreamingUploadableValue(opts.body)) {
|
|
148
|
+
return createStreamingFormRequestOptions(opts);
|
|
149
|
+
}
|
|
150
|
+
|
|
90
151
|
return { ...opts, body: await createForm(opts.body, fetch) };
|
|
91
152
|
};
|
|
92
153
|
|
|
@@ -138,12 +199,36 @@ export const createForm = async <T = Record<string, unknown>>(
|
|
|
138
199
|
|
|
139
200
|
// We check for Blob not File because Bun.File doesn't inherit from File,
|
|
140
201
|
// but they both inherit from Blob and have a `name` property at runtime.
|
|
141
|
-
const isNamedBlob = (value: unknown) => value instanceof Blob && 'name' in value;
|
|
202
|
+
const isNamedBlob = (value: unknown): value is NamedBlob => value instanceof Blob && 'name' in value;
|
|
203
|
+
|
|
204
|
+
const isReadableStream = (value: unknown): value is ReadableStream<BlobPart> =>
|
|
205
|
+
typeof value === 'object' &&
|
|
206
|
+
value !== null &&
|
|
207
|
+
'getReader' in value &&
|
|
208
|
+
typeof value.getReader === 'function';
|
|
209
|
+
|
|
210
|
+
const isStreamingFile = (value: unknown): value is StreamingFile =>
|
|
211
|
+
typeof value === 'object' && value !== null && brand_privateStreamingFile in value;
|
|
142
212
|
|
|
143
213
|
const isUploadable = (value: unknown) =>
|
|
144
214
|
typeof value === 'object' &&
|
|
145
215
|
value !== null &&
|
|
146
|
-
(value instanceof Response ||
|
|
216
|
+
(value instanceof Response ||
|
|
217
|
+
isAsyncIterable(value) ||
|
|
218
|
+
isReadableStream(value) ||
|
|
219
|
+
isStreamingFile(value) ||
|
|
220
|
+
isNamedBlob(value));
|
|
221
|
+
|
|
222
|
+
const hasStreamingUploadableValue = (value: unknown): boolean => {
|
|
223
|
+
if (isStreamingFile(value) || isAsyncIterable(value) || isReadableStream(value)) return true;
|
|
224
|
+
if (Array.isArray(value)) return value.some(hasStreamingUploadableValue);
|
|
225
|
+
if (value && typeof value === 'object' && !isNamedBlob(value) && !(value instanceof Response)) {
|
|
226
|
+
for (const k in value) {
|
|
227
|
+
if (hasStreamingUploadableValue((value as Record<string, unknown>)[k])) return true;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
return false;
|
|
231
|
+
};
|
|
147
232
|
|
|
148
233
|
const hasUploadableValue = (value: unknown): boolean => {
|
|
149
234
|
if (isUploadable(value)) return true;
|
|
@@ -156,6 +241,131 @@ const hasUploadableValue = (value: unknown): boolean => {
|
|
|
156
241
|
return false;
|
|
157
242
|
};
|
|
158
243
|
|
|
244
|
+
type FormEntry = { key: string; value: unknown };
|
|
245
|
+
|
|
246
|
+
const createStreamingFormRequestOptions = (opts: RequestOptions): RequestOptions => {
|
|
247
|
+
const boundary = `openai-${Math.random().toString(36).slice(2)}`;
|
|
248
|
+
const body = ReadableStreamFrom(iterateMultipartBody(opts.body, boundary));
|
|
249
|
+
|
|
250
|
+
return {
|
|
251
|
+
...opts,
|
|
252
|
+
body,
|
|
253
|
+
headers: buildHeaders([{ 'content-type': `multipart/form-data; boundary=${boundary}` }, opts.headers]),
|
|
254
|
+
};
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
async function* iterateMultipartBody(body: unknown, boundary: string): AsyncGenerator<Uint8Array> {
|
|
258
|
+
for await (const { key, value } of iterateFormEntries(body)) {
|
|
259
|
+
yield encodeUTF8(`--${boundary}\r\n`);
|
|
260
|
+
if (isUploadable(value)) {
|
|
261
|
+
const filename = getStreamingFileName(value);
|
|
262
|
+
const type = getStreamingFileType(value);
|
|
263
|
+
yield encodeUTF8(
|
|
264
|
+
`Content-Disposition: form-data; name="${escapeHeaderValue(key)}"; filename="${escapeHeaderValue(
|
|
265
|
+
filename,
|
|
266
|
+
)}"\r\n` + `Content-Type: ${type}\r\n\r\n`,
|
|
267
|
+
);
|
|
268
|
+
yield* iterateBytes(getStreamingFileData(value));
|
|
269
|
+
} else {
|
|
270
|
+
yield encodeUTF8(
|
|
271
|
+
`Content-Disposition: form-data; name="${escapeHeaderValue(key)}"\r\n\r\n${String(value)}`,
|
|
272
|
+
);
|
|
273
|
+
}
|
|
274
|
+
yield encodeUTF8('\r\n');
|
|
275
|
+
}
|
|
276
|
+
yield encodeUTF8(`--${boundary}--\r\n`);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
async function* iterateFormEntries(body: unknown): AsyncGenerator<FormEntry> {
|
|
280
|
+
if (!body || typeof body !== 'object') return;
|
|
281
|
+
|
|
282
|
+
for (const [key, value] of Object.entries(body)) {
|
|
283
|
+
yield* iterateFormValue(key, value);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
async function* iterateFormValue(key: string, value: unknown): AsyncGenerator<FormEntry> {
|
|
288
|
+
if (value === undefined) return;
|
|
289
|
+
if (value == null) {
|
|
290
|
+
throw new TypeError(
|
|
291
|
+
`Received null for "${key}"; to pass null in FormData, you must use the string 'null'`,
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
if (
|
|
296
|
+
typeof value === 'string' ||
|
|
297
|
+
typeof value === 'number' ||
|
|
298
|
+
typeof value === 'boolean' ||
|
|
299
|
+
isUploadable(value)
|
|
300
|
+
) {
|
|
301
|
+
yield { key, value };
|
|
302
|
+
} else if (Array.isArray(value)) {
|
|
303
|
+
for (const entry of value) {
|
|
304
|
+
yield* iterateFormValue(key + '[]', entry);
|
|
305
|
+
}
|
|
306
|
+
} else if (typeof value === 'object') {
|
|
307
|
+
for (const [name, prop] of Object.entries(value)) {
|
|
308
|
+
yield* iterateFormValue(`${key}[${name}]`, prop);
|
|
309
|
+
}
|
|
310
|
+
} else {
|
|
311
|
+
throw new TypeError(
|
|
312
|
+
`Invalid value given to form, expected a string, number, boolean, object, Array, File or Blob but got ${value} instead`,
|
|
313
|
+
);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
function getStreamingFileName(value: Uploadable): string {
|
|
318
|
+
return isStreamingFile(value) ? value.name : getName(value) ?? 'unknown_file';
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
function getStreamingFileType(value: Uploadable): string {
|
|
322
|
+
if (isStreamingFile(value)) return value.type || 'application/octet-stream';
|
|
323
|
+
if (isNamedBlob(value) && value.type) return value.type;
|
|
324
|
+
if (value instanceof Response) return value.headers.get('content-type') || 'application/octet-stream';
|
|
325
|
+
return 'application/octet-stream';
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
function getStreamingFileData(value: Uploadable): unknown {
|
|
329
|
+
if (isStreamingFile(value)) return value.data;
|
|
330
|
+
return value;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
async function* iterateBytes(value: unknown): AsyncGenerator<Uint8Array> {
|
|
334
|
+
if (typeof value === 'string') {
|
|
335
|
+
yield encodeUTF8(value);
|
|
336
|
+
} else if (ArrayBuffer.isView(value)) {
|
|
337
|
+
yield new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
|
|
338
|
+
} else if (value instanceof ArrayBuffer) {
|
|
339
|
+
yield new Uint8Array(value);
|
|
340
|
+
} else if (value instanceof Response) {
|
|
341
|
+
if (value.body) {
|
|
342
|
+
yield* iterateBytes(value.body);
|
|
343
|
+
} else {
|
|
344
|
+
yield* iterateBytes(await value.blob());
|
|
345
|
+
}
|
|
346
|
+
} else if (value instanceof Blob) {
|
|
347
|
+
if (typeof value.stream === 'function') {
|
|
348
|
+
yield* iterateBytes(value.stream());
|
|
349
|
+
} else {
|
|
350
|
+
yield new Uint8Array(await value.arrayBuffer());
|
|
351
|
+
}
|
|
352
|
+
} else if (isReadableStream(value)) {
|
|
353
|
+
for await (const chunk of ReadableStreamToAsyncIterable<unknown>(value)) {
|
|
354
|
+
yield* iterateBytes(chunk);
|
|
355
|
+
}
|
|
356
|
+
} else if (isAsyncIterable(value)) {
|
|
357
|
+
for await (const chunk of value) {
|
|
358
|
+
yield* iterateBytes(chunk);
|
|
359
|
+
}
|
|
360
|
+
} else {
|
|
361
|
+
throw new TypeError(`Invalid streaming file chunk: ${String(value)}`);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
function escapeHeaderValue(value: string): string {
|
|
366
|
+
return value.replace(/["\\\r\n]/g, (character) => encodeURIComponent(character));
|
|
367
|
+
}
|
|
368
|
+
|
|
159
369
|
const addFormValue = async (form: FormData, key: string, value: unknown): Promise<void> => {
|
|
160
370
|
if (value === undefined) return;
|
|
161
371
|
if (value == null) {
|