@ai-sdk/provider-utils 2.2.8 → 3.0.0-alpha.10
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 +237 -14
- package/dist/index.d.mts +156 -72
- package/dist/index.d.ts +156 -72
- package/dist/index.js +243 -81
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +215 -61
- package/dist/index.mjs.map +1 -1
- package/dist/test/index.d.mts +87 -0
- package/dist/test/index.d.ts +87 -0
- package/{test/dist → dist/test}/index.js +66 -358
- package/dist/test/index.js.map +1 -0
- package/{test/dist → dist/test}/index.mjs +64 -353
- package/dist/test/index.mjs.map +1 -0
- package/package.json +19 -17
- package/test.d.ts +1 -0
- package/test/dist/index.d.mts +0 -162
- package/test/dist/index.d.ts +0 -162
- package/test/dist/index.js.map +0 -1
- package/test/dist/index.mjs.map +0 -1
- /package/{test/dist → dist/test}/chunk-D6YTI3O5.mjs +0 -0
- /package/{test/dist → dist/test}/chunk-D6YTI3O5.mjs.map +0 -0
- /package/{test/dist → dist/test}/graphql-6JDEV3ML.mjs +0 -0
- /package/{test/dist → dist/test}/graphql-6JDEV3ML.mjs.map +0 -0
package/dist/index.mjs
CHANGED
@@ -137,23 +137,25 @@ function splitLines(buffer, chunk) {
|
|
137
137
|
|
138
138
|
// src/extract-response-headers.ts
|
139
139
|
function extractResponseHeaders(response) {
|
140
|
-
|
141
|
-
response.headers.forEach((value, key) => {
|
142
|
-
headers[key] = value;
|
143
|
-
});
|
144
|
-
return headers;
|
140
|
+
return Object.fromEntries([...response.headers]);
|
145
141
|
}
|
146
142
|
|
147
143
|
// src/generate-id.ts
|
148
144
|
import { InvalidArgumentError } from "@ai-sdk/provider";
|
149
|
-
import { customAlphabet } from "nanoid/non-secure";
|
150
145
|
var createIdGenerator = ({
|
151
146
|
prefix,
|
152
|
-
size
|
147
|
+
size = 16,
|
153
148
|
alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
154
149
|
separator = "-"
|
155
150
|
} = {}) => {
|
156
|
-
const generator =
|
151
|
+
const generator = () => {
|
152
|
+
const alphabetLength = alphabet.length;
|
153
|
+
const chars = new Array(size);
|
154
|
+
for (let i = 0; i < size; i++) {
|
155
|
+
chars[i] = alphabet[Math.random() * alphabetLength | 0];
|
156
|
+
}
|
157
|
+
return chars.join("");
|
158
|
+
};
|
157
159
|
if (prefix == null) {
|
158
160
|
return generator;
|
159
161
|
}
|
@@ -163,7 +165,7 @@ var createIdGenerator = ({
|
|
163
165
|
message: `The separator "${separator}" must not be part of the alphabet "${alphabet}".`
|
164
166
|
});
|
165
167
|
}
|
166
|
-
return (
|
168
|
+
return () => `${prefix}${separator}${generator()}`;
|
167
169
|
};
|
168
170
|
var generateId = createIdGenerator();
|
169
171
|
|
@@ -277,6 +279,20 @@ var getFromApi = async ({
|
|
277
279
|
}
|
278
280
|
};
|
279
281
|
|
282
|
+
// src/is-url-supported.ts
|
283
|
+
function isUrlSupported({
|
284
|
+
mediaType,
|
285
|
+
url,
|
286
|
+
supportedUrls
|
287
|
+
}) {
|
288
|
+
url = url.toLowerCase();
|
289
|
+
mediaType = mediaType.toLowerCase();
|
290
|
+
return Object.entries(supportedUrls).map(([key, value]) => {
|
291
|
+
const mediaType2 = key.toLowerCase();
|
292
|
+
return mediaType2 === "*" || mediaType2 === "*/*" ? { mediaTypePrefix: "", regexes: value } : { mediaTypePrefix: mediaType2.replace(/\*/, ""), regexes: value };
|
293
|
+
}).filter(({ mediaTypePrefix }) => mediaType.startsWith(mediaTypePrefix)).flatMap(({ regexes }) => regexes).some((pattern) => pattern.test(url));
|
294
|
+
}
|
295
|
+
|
280
296
|
// src/load-api-key.ts
|
281
297
|
import { LoadAPIKeyError } from "@ai-sdk/provider";
|
282
298
|
function loadApiKey({
|
@@ -368,14 +384,59 @@ function loadSetting({
|
|
368
384
|
// src/parse-json.ts
|
369
385
|
import {
|
370
386
|
JSONParseError,
|
371
|
-
TypeValidationError as
|
387
|
+
TypeValidationError as TypeValidationError3
|
372
388
|
} from "@ai-sdk/provider";
|
373
|
-
|
389
|
+
|
390
|
+
// src/secure-json-parse.ts
|
391
|
+
var suspectProtoRx = /"__proto__"\s*:/;
|
392
|
+
var suspectConstructorRx = /"constructor"\s*:/;
|
393
|
+
function _parse(text) {
|
394
|
+
const obj = JSON.parse(text);
|
395
|
+
if (obj === null || typeof obj !== "object") {
|
396
|
+
return obj;
|
397
|
+
}
|
398
|
+
if (suspectProtoRx.test(text) === false && suspectConstructorRx.test(text) === false) {
|
399
|
+
return obj;
|
400
|
+
}
|
401
|
+
return filter(obj);
|
402
|
+
}
|
403
|
+
function filter(obj) {
|
404
|
+
let next = [obj];
|
405
|
+
while (next.length) {
|
406
|
+
const nodes = next;
|
407
|
+
next = [];
|
408
|
+
for (const node of nodes) {
|
409
|
+
if (Object.prototype.hasOwnProperty.call(node, "__proto__")) {
|
410
|
+
throw new SyntaxError("Object contains forbidden prototype property");
|
411
|
+
}
|
412
|
+
if (Object.prototype.hasOwnProperty.call(node, "constructor") && Object.prototype.hasOwnProperty.call(node.constructor, "prototype")) {
|
413
|
+
throw new SyntaxError("Object contains forbidden prototype property");
|
414
|
+
}
|
415
|
+
for (const key in node) {
|
416
|
+
const value = node[key];
|
417
|
+
if (value && typeof value === "object") {
|
418
|
+
next.push(value);
|
419
|
+
}
|
420
|
+
}
|
421
|
+
}
|
422
|
+
}
|
423
|
+
return obj;
|
424
|
+
}
|
425
|
+
function secureJsonParse(text) {
|
426
|
+
const { stackTraceLimit } = Error;
|
427
|
+
Error.stackTraceLimit = 0;
|
428
|
+
try {
|
429
|
+
return _parse(text);
|
430
|
+
} finally {
|
431
|
+
Error.stackTraceLimit = stackTraceLimit;
|
432
|
+
}
|
433
|
+
}
|
374
434
|
|
375
435
|
// src/validate-types.ts
|
376
|
-
import { TypeValidationError } from "@ai-sdk/provider";
|
436
|
+
import { TypeValidationError as TypeValidationError2 } from "@ai-sdk/provider";
|
377
437
|
|
378
438
|
// src/validator.ts
|
439
|
+
import { TypeValidationError } from "@ai-sdk/provider";
|
379
440
|
var validatorSymbol = Symbol.for("vercel.ai.validator");
|
380
441
|
function validator(validate) {
|
381
442
|
return { [validatorSymbol]: true, validate };
|
@@ -384,99 +445,121 @@ function isValidator(value) {
|
|
384
445
|
return typeof value === "object" && value !== null && validatorSymbol in value && value[validatorSymbol] === true && "validate" in value;
|
385
446
|
}
|
386
447
|
function asValidator(value) {
|
387
|
-
return isValidator(value) ? value :
|
448
|
+
return isValidator(value) ? value : standardSchemaValidator(value);
|
388
449
|
}
|
389
|
-
function
|
390
|
-
return validator((value) => {
|
391
|
-
const result =
|
392
|
-
return result.
|
450
|
+
function standardSchemaValidator(standardSchema) {
|
451
|
+
return validator(async (value) => {
|
452
|
+
const result = await standardSchema["~standard"].validate(value);
|
453
|
+
return result.issues == null ? { success: true, value: result.value } : {
|
454
|
+
success: false,
|
455
|
+
error: new TypeValidationError({
|
456
|
+
value,
|
457
|
+
cause: result.issues
|
458
|
+
})
|
459
|
+
};
|
393
460
|
});
|
394
461
|
}
|
395
462
|
|
396
463
|
// src/validate-types.ts
|
397
|
-
function validateTypes({
|
464
|
+
async function validateTypes({
|
398
465
|
value,
|
399
|
-
schema
|
466
|
+
schema
|
400
467
|
}) {
|
401
|
-
const result = safeValidateTypes({ value, schema
|
468
|
+
const result = await safeValidateTypes({ value, schema });
|
402
469
|
if (!result.success) {
|
403
|
-
throw
|
470
|
+
throw TypeValidationError2.wrap({ value, cause: result.error });
|
404
471
|
}
|
405
472
|
return result.value;
|
406
473
|
}
|
407
|
-
function safeValidateTypes({
|
474
|
+
async function safeValidateTypes({
|
408
475
|
value,
|
409
476
|
schema
|
410
477
|
}) {
|
411
478
|
const validator2 = asValidator(schema);
|
412
479
|
try {
|
413
480
|
if (validator2.validate == null) {
|
414
|
-
return { success: true, value };
|
481
|
+
return { success: true, value, rawValue: value };
|
415
482
|
}
|
416
|
-
const result = validator2.validate(value);
|
483
|
+
const result = await validator2.validate(value);
|
417
484
|
if (result.success) {
|
418
|
-
return result;
|
485
|
+
return { success: true, value: result.value, rawValue: value };
|
419
486
|
}
|
420
487
|
return {
|
421
488
|
success: false,
|
422
|
-
error:
|
489
|
+
error: TypeValidationError2.wrap({ value, cause: result.error }),
|
490
|
+
rawValue: value
|
423
491
|
};
|
424
492
|
} catch (error) {
|
425
493
|
return {
|
426
494
|
success: false,
|
427
|
-
error:
|
495
|
+
error: TypeValidationError2.wrap({ value, cause: error }),
|
496
|
+
rawValue: value
|
428
497
|
};
|
429
498
|
}
|
430
499
|
}
|
431
500
|
|
432
501
|
// src/parse-json.ts
|
433
|
-
function parseJSON({
|
434
|
-
text,
|
435
|
-
schema
|
436
|
-
}) {
|
502
|
+
async function parseJSON({ text, schema }) {
|
437
503
|
try {
|
438
|
-
const value =
|
504
|
+
const value = secureJsonParse(text);
|
439
505
|
if (schema == null) {
|
440
506
|
return value;
|
441
507
|
}
|
442
508
|
return validateTypes({ value, schema });
|
443
509
|
} catch (error) {
|
444
|
-
if (JSONParseError.isInstance(error) ||
|
510
|
+
if (JSONParseError.isInstance(error) || TypeValidationError3.isInstance(error)) {
|
445
511
|
throw error;
|
446
512
|
}
|
447
513
|
throw new JSONParseError({ text, cause: error });
|
448
514
|
}
|
449
515
|
}
|
450
|
-
function safeParseJSON({
|
516
|
+
async function safeParseJSON({
|
451
517
|
text,
|
452
518
|
schema
|
453
519
|
}) {
|
454
520
|
try {
|
455
|
-
const value =
|
521
|
+
const value = secureJsonParse(text);
|
456
522
|
if (schema == null) {
|
457
523
|
return { success: true, value, rawValue: value };
|
458
524
|
}
|
459
|
-
|
460
|
-
return validationResult.success ? { ...validationResult, rawValue: value } : validationResult;
|
525
|
+
return await safeValidateTypes({ value, schema });
|
461
526
|
} catch (error) {
|
462
527
|
return {
|
463
528
|
success: false,
|
464
|
-
error: JSONParseError.isInstance(error) ? error : new JSONParseError({ text, cause: error })
|
529
|
+
error: JSONParseError.isInstance(error) ? error : new JSONParseError({ text, cause: error }),
|
530
|
+
rawValue: void 0
|
465
531
|
};
|
466
532
|
}
|
467
533
|
}
|
468
534
|
function isParsableJson(input) {
|
469
535
|
try {
|
470
|
-
|
536
|
+
secureJsonParse(input);
|
471
537
|
return true;
|
472
538
|
} catch (e) {
|
473
539
|
return false;
|
474
540
|
}
|
475
541
|
}
|
476
542
|
|
543
|
+
// src/parse-json-event-stream.ts
|
544
|
+
function parseJsonEventStream({
|
545
|
+
stream,
|
546
|
+
schema
|
547
|
+
}) {
|
548
|
+
return stream.pipeThrough(new TextDecoderStream()).pipeThrough(createEventSourceParserStream()).pipeThrough(
|
549
|
+
new TransformStream({
|
550
|
+
async transform({ data }, controller) {
|
551
|
+
if (data === "[DONE]") {
|
552
|
+
return;
|
553
|
+
}
|
554
|
+
controller.enqueue(await safeParseJSON({ text: data, schema }));
|
555
|
+
}
|
556
|
+
})
|
557
|
+
);
|
558
|
+
}
|
559
|
+
|
477
560
|
// src/parse-provider-options.ts
|
478
561
|
import { InvalidArgumentError as InvalidArgumentError2 } from "@ai-sdk/provider";
|
479
|
-
function parseProviderOptions({
|
562
|
+
async function parseProviderOptions({
|
480
563
|
provider,
|
481
564
|
providerOptions,
|
482
565
|
schema
|
@@ -484,7 +567,7 @@ function parseProviderOptions({
|
|
484
567
|
if ((providerOptions == null ? void 0 : providerOptions[provider]) == null) {
|
485
568
|
return void 0;
|
486
569
|
}
|
487
|
-
const parsedProviderOptions = safeValidateTypes({
|
570
|
+
const parsedProviderOptions = await safeValidateTypes({
|
488
571
|
value: providerOptions[provider],
|
489
572
|
schema
|
490
573
|
});
|
@@ -658,7 +741,7 @@ var createJsonErrorResponseHandler = ({
|
|
658
741
|
};
|
659
742
|
}
|
660
743
|
try {
|
661
|
-
const parsedError = parseJSON({
|
744
|
+
const parsedError = await parseJSON({
|
662
745
|
text: responseBody,
|
663
746
|
schema: errorSchema
|
664
747
|
});
|
@@ -697,21 +780,10 @@ var createEventSourceResponseHandler = (chunkSchema) => async ({ response }) =>
|
|
697
780
|
}
|
698
781
|
return {
|
699
782
|
responseHeaders,
|
700
|
-
value:
|
701
|
-
|
702
|
-
|
703
|
-
|
704
|
-
return;
|
705
|
-
}
|
706
|
-
controller.enqueue(
|
707
|
-
safeParseJSON({
|
708
|
-
text: data,
|
709
|
-
schema: chunkSchema
|
710
|
-
})
|
711
|
-
);
|
712
|
-
}
|
713
|
-
})
|
714
|
-
)
|
783
|
+
value: parseJsonEventStream({
|
784
|
+
stream: response.body,
|
785
|
+
schema: chunkSchema
|
786
|
+
})
|
715
787
|
};
|
716
788
|
};
|
717
789
|
var createJsonStreamResponseHandler = (chunkSchema) => async ({ response }) => {
|
@@ -724,10 +796,10 @@ var createJsonStreamResponseHandler = (chunkSchema) => async ({ response }) => {
|
|
724
796
|
responseHeaders,
|
725
797
|
value: response.body.pipeThrough(new TextDecoderStream()).pipeThrough(
|
726
798
|
new TransformStream({
|
727
|
-
transform(chunkText, controller) {
|
799
|
+
async transform(chunkText, controller) {
|
728
800
|
if (chunkText.endsWith("\n")) {
|
729
801
|
controller.enqueue(
|
730
|
-
safeParseJSON({
|
802
|
+
await safeParseJSON({
|
731
803
|
text: buffer + chunkText,
|
732
804
|
schema: chunkSchema
|
733
805
|
})
|
@@ -743,7 +815,7 @@ var createJsonStreamResponseHandler = (chunkSchema) => async ({ response }) => {
|
|
743
815
|
};
|
744
816
|
var createJsonResponseHandler = (responseSchema) => async ({ response, url, requestBodyValues }) => {
|
745
817
|
const responseBody = await response.text();
|
746
|
-
const parsedResult = safeParseJSON({
|
818
|
+
const parsedResult = await safeParseJSON({
|
747
819
|
text: responseBody,
|
748
820
|
schema: responseSchema
|
749
821
|
});
|
@@ -811,6 +883,76 @@ var createStatusCodeErrorResponseHandler = () => async ({ response, url, request
|
|
811
883
|
};
|
812
884
|
};
|
813
885
|
|
886
|
+
// src/zod-schema.ts
|
887
|
+
import * as z4 from "zod/v4/core";
|
888
|
+
import zodToJsonSchema from "zod-to-json-schema";
|
889
|
+
function zod3Schema(zodSchema2, options) {
|
890
|
+
var _a;
|
891
|
+
const useReferences = (_a = options == null ? void 0 : options.useReferences) != null ? _a : false;
|
892
|
+
return jsonSchema(
|
893
|
+
zodToJsonSchema(zodSchema2, {
|
894
|
+
$refStrategy: useReferences ? "root" : "none",
|
895
|
+
target: "jsonSchema7"
|
896
|
+
// note: openai mode breaks various gemini conversions
|
897
|
+
}),
|
898
|
+
{
|
899
|
+
validate: (value) => {
|
900
|
+
const result = zodSchema2.safeParse(value);
|
901
|
+
return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
|
902
|
+
}
|
903
|
+
}
|
904
|
+
);
|
905
|
+
}
|
906
|
+
function zod4Schema(zodSchema2, options) {
|
907
|
+
var _a;
|
908
|
+
const useReferences = (_a = options == null ? void 0 : options.useReferences) != null ? _a : false;
|
909
|
+
const z4JSONSchema = z4.toJSONSchema(zodSchema2, {
|
910
|
+
target: "draft-7",
|
911
|
+
io: "output",
|
912
|
+
reused: useReferences ? "ref" : "inline"
|
913
|
+
});
|
914
|
+
return jsonSchema(z4JSONSchema, {
|
915
|
+
validate: (value) => {
|
916
|
+
const result = z4.safeParse(zodSchema2, value);
|
917
|
+
return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
|
918
|
+
}
|
919
|
+
});
|
920
|
+
}
|
921
|
+
function isZod4Schema(zodSchema2) {
|
922
|
+
return "_zod" in zodSchema2;
|
923
|
+
}
|
924
|
+
function zodSchema(zodSchema2, options) {
|
925
|
+
if (isZod4Schema(zodSchema2)) {
|
926
|
+
return zod4Schema(zodSchema2, options);
|
927
|
+
} else {
|
928
|
+
return zod3Schema(zodSchema2, options);
|
929
|
+
}
|
930
|
+
}
|
931
|
+
|
932
|
+
// src/schema.ts
|
933
|
+
var schemaSymbol = Symbol.for("vercel.ai.schema");
|
934
|
+
function jsonSchema(jsonSchema2, {
|
935
|
+
validate
|
936
|
+
} = {}) {
|
937
|
+
return {
|
938
|
+
[schemaSymbol]: true,
|
939
|
+
_type: void 0,
|
940
|
+
// should never be used directly
|
941
|
+
[validatorSymbol]: true,
|
942
|
+
jsonSchema: jsonSchema2,
|
943
|
+
validate
|
944
|
+
};
|
945
|
+
}
|
946
|
+
function isSchema(value) {
|
947
|
+
return typeof value === "object" && value !== null && schemaSymbol in value && value[schemaSymbol] === true && "jsonSchema" in value && "validate" in value;
|
948
|
+
}
|
949
|
+
function asSchema(schema) {
|
950
|
+
return schema == null ? jsonSchema({
|
951
|
+
properties: {},
|
952
|
+
additionalProperties: false
|
953
|
+
}) : isSchema(schema) ? schema : zodSchema(schema);
|
954
|
+
}
|
955
|
+
|
814
956
|
// src/uint8-utils.ts
|
815
957
|
var { btoa, atob } = globalThis;
|
816
958
|
function convertBase64ToUint8Array(base64String) {
|
@@ -825,16 +967,24 @@ function convertUint8ArrayToBase64(array) {
|
|
825
967
|
}
|
826
968
|
return btoa(latin1string);
|
827
969
|
}
|
970
|
+
function convertToBase64(value) {
|
971
|
+
return value instanceof Uint8Array ? convertUint8ArrayToBase64(value) : value;
|
972
|
+
}
|
828
973
|
|
829
974
|
// src/without-trailing-slash.ts
|
830
975
|
function withoutTrailingSlash(url) {
|
831
976
|
return url == null ? void 0 : url.replace(/\/$/, "");
|
832
977
|
}
|
978
|
+
|
979
|
+
// src/index.ts
|
980
|
+
export * from "@standard-schema/spec";
|
833
981
|
export {
|
982
|
+
asSchema,
|
834
983
|
asValidator,
|
835
984
|
combineHeaders,
|
836
985
|
convertAsyncIteratorToReadableStream,
|
837
986
|
convertBase64ToUint8Array,
|
987
|
+
convertToBase64,
|
838
988
|
convertUint8ArrayToBase64,
|
839
989
|
createBinaryResponseHandler,
|
840
990
|
createEventSourceParserStream,
|
@@ -851,11 +1001,14 @@ export {
|
|
851
1001
|
getFromApi,
|
852
1002
|
isAbortError,
|
853
1003
|
isParsableJson,
|
1004
|
+
isUrlSupported,
|
854
1005
|
isValidator,
|
1006
|
+
jsonSchema,
|
855
1007
|
loadApiKey,
|
856
1008
|
loadOptionalSetting,
|
857
1009
|
loadSetting,
|
858
1010
|
parseJSON,
|
1011
|
+
parseJsonEventStream,
|
859
1012
|
parseProviderOptions,
|
860
1013
|
postFormDataToApi,
|
861
1014
|
postJsonToApi,
|
@@ -864,10 +1017,11 @@ export {
|
|
864
1017
|
resolve,
|
865
1018
|
safeParseJSON,
|
866
1019
|
safeValidateTypes,
|
1020
|
+
standardSchemaValidator,
|
867
1021
|
validateTypes,
|
868
1022
|
validator,
|
869
1023
|
validatorSymbol,
|
870
1024
|
withoutTrailingSlash,
|
871
|
-
|
1025
|
+
zodSchema
|
872
1026
|
};
|
873
1027
|
//# sourceMappingURL=index.mjs.map
|