@ai-sdk/provider-utils 4.0.0-beta.10 → 4.0.0-beta.12

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/dist/index.js CHANGED
@@ -63,6 +63,8 @@ __export(src_exports, {
63
63
  isUrlSupported: () => isUrlSupported,
64
64
  isValidator: () => isValidator,
65
65
  jsonSchema: () => jsonSchema,
66
+ lazySchema: () => lazySchema,
67
+ lazyValidator: () => lazyValidator,
66
68
  loadApiKey: () => loadApiKey,
67
69
  loadOptionalSetting: () => loadOptionalSetting,
68
70
  loadSetting: () => loadSetting,
@@ -81,7 +83,6 @@ __export(src_exports, {
81
83
  tool: () => tool,
82
84
  validateTypes: () => validateTypes,
83
85
  validator: () => validator,
84
- validatorSymbol: () => validatorSymbol,
85
86
  withUserAgentSuffix: () => withUserAgentSuffix,
86
87
  withoutTrailingSlash: () => withoutTrailingSlash,
87
88
  zodSchema: () => zodSchema
@@ -163,45 +164,6 @@ function extractResponseHeaders(response) {
163
164
  return Object.fromEntries([...response.headers]);
164
165
  }
165
166
 
166
- // src/get-runtime-environment-user-agent.ts
167
- function getRuntimeEnvironmentUserAgent(globalThisAny = globalThis) {
168
- var _a, _b, _c;
169
- if (globalThisAny.window) {
170
- return `runtime/browser`;
171
- }
172
- if ((_a = globalThisAny.navigator) == null ? void 0 : _a.userAgent) {
173
- return `runtime/${globalThisAny.navigator.userAgent.toLowerCase()}`;
174
- }
175
- if ((_c = (_b = globalThisAny.process) == null ? void 0 : _b.versions) == null ? void 0 : _c.node) {
176
- return `runtime/node.js/${globalThisAny.process.version.substring(0)}`;
177
- }
178
- if (globalThisAny.EdgeRuntime) {
179
- return `runtime/vercel-edge`;
180
- }
181
- return "runtime/unknown";
182
- }
183
-
184
- // src/remove-undefined-entries.ts
185
- function removeUndefinedEntries(record) {
186
- return Object.fromEntries(
187
- Object.entries(record).filter(([_key, value]) => value != null)
188
- );
189
- }
190
-
191
- // src/with-user-agent-suffix.ts
192
- function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
193
- const cleanedHeaders = removeUndefinedEntries(
194
- headers != null ? headers : {}
195
- );
196
- const normalizedHeaders = new Headers(cleanedHeaders);
197
- const currentUserAgentHeader = normalizedHeaders.get("user-agent") || "";
198
- normalizedHeaders.set(
199
- "user-agent",
200
- [currentUserAgentHeader, ...userAgentSuffixParts].filter(Boolean).join(" ")
201
- );
202
- return Object.fromEntries(normalizedHeaders);
203
- }
204
-
205
167
  // src/generate-id.ts
206
168
  var import_provider = require("@ai-sdk/provider");
207
169
  var createIdGenerator = ({
@@ -283,8 +245,47 @@ function handleFetchError({
283
245
  return error;
284
246
  }
285
247
 
248
+ // src/get-runtime-environment-user-agent.ts
249
+ function getRuntimeEnvironmentUserAgent(globalThisAny = globalThis) {
250
+ var _a, _b, _c;
251
+ if (globalThisAny.window) {
252
+ return `runtime/browser`;
253
+ }
254
+ if ((_a = globalThisAny.navigator) == null ? void 0 : _a.userAgent) {
255
+ return `runtime/${globalThisAny.navigator.userAgent.toLowerCase()}`;
256
+ }
257
+ if ((_c = (_b = globalThisAny.process) == null ? void 0 : _b.versions) == null ? void 0 : _c.node) {
258
+ return `runtime/node.js/${globalThisAny.process.version.substring(0)}`;
259
+ }
260
+ if (globalThisAny.EdgeRuntime) {
261
+ return `runtime/vercel-edge`;
262
+ }
263
+ return "runtime/unknown";
264
+ }
265
+
266
+ // src/remove-undefined-entries.ts
267
+ function removeUndefinedEntries(record) {
268
+ return Object.fromEntries(
269
+ Object.entries(record).filter(([_key, value]) => value != null)
270
+ );
271
+ }
272
+
273
+ // src/with-user-agent-suffix.ts
274
+ function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
275
+ const cleanedHeaders = removeUndefinedEntries(
276
+ headers != null ? headers : {}
277
+ );
278
+ const normalizedHeaders = new Headers(cleanedHeaders);
279
+ const currentUserAgentHeader = normalizedHeaders.get("user-agent") || "";
280
+ normalizedHeaders.set(
281
+ "user-agent",
282
+ [currentUserAgentHeader, ...userAgentSuffixParts].filter(Boolean).join(" ")
283
+ );
284
+ return Object.fromEntries(normalizedHeaders);
285
+ }
286
+
286
287
  // src/version.ts
287
- var VERSION = true ? "4.0.0-beta.10" : "0.0.0-test";
288
+ var VERSION = true ? "4.0.0-beta.12" : "0.0.0-test";
288
289
 
289
290
  // src/get-from-api.ts
290
291
  var getOriginalFetch = () => globalThis.fetch;
@@ -570,12 +571,21 @@ function validator(validate) {
570
571
  function isValidator(value) {
571
572
  return typeof value === "object" && value !== null && validatorSymbol in value && value[validatorSymbol] === true && "validate" in value;
572
573
  }
574
+ function lazyValidator(createValidator) {
575
+ let validator2;
576
+ return () => {
577
+ if (validator2 == null) {
578
+ validator2 = createValidator();
579
+ }
580
+ return validator2;
581
+ };
582
+ }
573
583
  function asValidator(value) {
574
- return isValidator(value) ? value : standardSchemaValidator(value);
584
+ return isValidator(value) ? value : typeof value === "function" ? value() : standardSchemaValidator(value);
575
585
  }
576
- function standardSchemaValidator(standardSchema) {
586
+ function standardSchemaValidator(standardSchema2) {
577
587
  return validator(async (value) => {
578
- const result = await standardSchema["~standard"].validate(value);
588
+ const result = await standardSchema2["~standard"].validate(value);
579
589
  return result.issues == null ? { success: true, value: result.value } : {
580
590
  success: false,
581
591
  error: new import_provider6.TypeValidationError({
@@ -1069,9 +1079,20 @@ var createStatusCodeErrorResponseHandler = () => async ({ response, url, request
1069
1079
  };
1070
1080
  };
1071
1081
 
1072
- // src/zod-schema.ts
1082
+ // src/schema.ts
1083
+ var import_provider12 = require("@ai-sdk/provider");
1073
1084
  var z4 = __toESM(require("zod/v4"));
1074
1085
 
1086
+ // src/valibot-to-json-schema/valibot-to-json-schema.ts
1087
+ var valibotToJsonSchema = async (schema) => {
1088
+ try {
1089
+ const { toJsonSchema } = await import("@valibot/to-json-schema");
1090
+ return toJsonSchema(schema);
1091
+ } catch (e) {
1092
+ throw new Error(`Failed to import @valibot/to-json-schema`);
1093
+ }
1094
+ };
1095
+
1075
1096
  // src/zod-to-json-schema/get-relative-path.ts
1076
1097
  var getRelativePath = (pathA, pathB) => {
1077
1098
  let i = 0;
@@ -2253,12 +2274,84 @@ var zodToJsonSchema = (schema, options) => {
2253
2274
  // src/zod-to-json-schema/index.ts
2254
2275
  var zod_to_json_schema_default = zodToJsonSchema;
2255
2276
 
2256
- // src/zod-schema.ts
2277
+ // src/schema.ts
2278
+ var schemaSymbol = Symbol.for("vercel.ai.schema");
2279
+ function lazySchema(createSchema) {
2280
+ let schema;
2281
+ return () => {
2282
+ if (schema == null) {
2283
+ schema = createSchema();
2284
+ }
2285
+ return schema;
2286
+ };
2287
+ }
2288
+ function jsonSchema(jsonSchema2, {
2289
+ validate
2290
+ } = {}) {
2291
+ return {
2292
+ [schemaSymbol]: true,
2293
+ _type: void 0,
2294
+ // should never be used directly
2295
+ [validatorSymbol]: true,
2296
+ get jsonSchema() {
2297
+ if (typeof jsonSchema2 === "function") {
2298
+ jsonSchema2 = jsonSchema2();
2299
+ }
2300
+ return jsonSchema2;
2301
+ },
2302
+ validate
2303
+ };
2304
+ }
2305
+ function isSchema(value) {
2306
+ return typeof value === "object" && value !== null && schemaSymbol in value && value[schemaSymbol] === true && "jsonSchema" in value && "validate" in value;
2307
+ }
2308
+ function asSchema(schema) {
2309
+ return schema == null ? jsonSchema({
2310
+ properties: {},
2311
+ additionalProperties: false
2312
+ }) : isSchema(schema) ? schema : typeof schema === "function" ? schema() : standardSchema(schema);
2313
+ }
2314
+ function standardSchema(standardSchema2) {
2315
+ const vendor = standardSchema2["~standard"].vendor;
2316
+ switch (vendor) {
2317
+ case "zod": {
2318
+ return zodSchema(
2319
+ standardSchema2
2320
+ );
2321
+ }
2322
+ case "valibot": {
2323
+ return standardSchemaWithJsonSchemaResolver(
2324
+ standardSchema2,
2325
+ valibotToJsonSchema
2326
+ );
2327
+ }
2328
+ default: {
2329
+ return standardSchemaWithJsonSchemaResolver(standardSchema2, () => {
2330
+ throw new Error(`Unsupported standard schema vendor: ${vendor}`);
2331
+ });
2332
+ }
2333
+ }
2334
+ }
2335
+ function standardSchemaWithJsonSchemaResolver(standardSchema2, jsonSchemaResolver) {
2336
+ return jsonSchema(jsonSchemaResolver(standardSchema2), {
2337
+ validate: async (value) => {
2338
+ const result = await standardSchema2["~standard"].validate(value);
2339
+ return "value" in result ? { success: true, value: result.value } : {
2340
+ success: false,
2341
+ error: new import_provider12.TypeValidationError({
2342
+ value,
2343
+ cause: result.issues
2344
+ })
2345
+ };
2346
+ }
2347
+ });
2348
+ }
2257
2349
  function zod3Schema(zodSchema2, options) {
2258
2350
  var _a;
2259
2351
  const useReferences = (_a = options == null ? void 0 : options.useReferences) != null ? _a : false;
2260
2352
  return jsonSchema(
2261
- zod_to_json_schema_default(zodSchema2, {
2353
+ // defer json schema creation to avoid unnecessary computation when only validation is needed
2354
+ () => zod_to_json_schema_default(zodSchema2, {
2262
2355
  $refStrategy: useReferences ? "root" : "none"
2263
2356
  }),
2264
2357
  {
@@ -2272,17 +2365,20 @@ function zod3Schema(zodSchema2, options) {
2272
2365
  function zod4Schema(zodSchema2, options) {
2273
2366
  var _a;
2274
2367
  const useReferences = (_a = options == null ? void 0 : options.useReferences) != null ? _a : false;
2275
- const z4JSONSchema = z4.toJSONSchema(zodSchema2, {
2276
- target: "draft-7",
2277
- io: "output",
2278
- reused: useReferences ? "ref" : "inline"
2279
- });
2280
- return jsonSchema(z4JSONSchema, {
2281
- validate: async (value) => {
2282
- const result = await z4.safeParseAsync(zodSchema2, value);
2283
- return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
2368
+ return jsonSchema(
2369
+ // defer json schema creation to avoid unnecessary computation when only validation is needed
2370
+ () => z4.toJSONSchema(zodSchema2, {
2371
+ target: "draft-7",
2372
+ io: "output",
2373
+ reused: useReferences ? "ref" : "inline"
2374
+ }),
2375
+ {
2376
+ validate: async (value) => {
2377
+ const result = await z4.safeParseAsync(zodSchema2, value);
2378
+ return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
2379
+ }
2284
2380
  }
2285
- });
2381
+ );
2286
2382
  }
2287
2383
  function isZod4Schema(zodSchema2) {
2288
2384
  return "_zod" in zodSchema2;
@@ -2295,30 +2391,6 @@ function zodSchema(zodSchema2, options) {
2295
2391
  }
2296
2392
  }
2297
2393
 
2298
- // src/schema.ts
2299
- var schemaSymbol = Symbol.for("vercel.ai.schema");
2300
- function jsonSchema(jsonSchema2, {
2301
- validate
2302
- } = {}) {
2303
- return {
2304
- [schemaSymbol]: true,
2305
- _type: void 0,
2306
- // should never be used directly
2307
- [validatorSymbol]: true,
2308
- jsonSchema: jsonSchema2,
2309
- validate
2310
- };
2311
- }
2312
- function isSchema(value) {
2313
- return typeof value === "object" && value !== null && schemaSymbol in value && value[schemaSymbol] === true && "jsonSchema" in value && "validate" in value;
2314
- }
2315
- function asSchema(schema) {
2316
- return schema == null ? jsonSchema({
2317
- properties: {},
2318
- additionalProperties: false
2319
- }) : isSchema(schema) ? schema : zodSchema(schema);
2320
- }
2321
-
2322
2394
  // src/uint8-utils.ts
2323
2395
  var { btoa, atob } = globalThis;
2324
2396
  function convertBase64ToUint8Array(base64String) {
@@ -2403,6 +2475,8 @@ var import_stream2 = require("eventsource-parser/stream");
2403
2475
  isUrlSupported,
2404
2476
  isValidator,
2405
2477
  jsonSchema,
2478
+ lazySchema,
2479
+ lazyValidator,
2406
2480
  loadApiKey,
2407
2481
  loadOptionalSetting,
2408
2482
  loadSetting,
@@ -2421,7 +2495,6 @@ var import_stream2 = require("eventsource-parser/stream");
2421
2495
  tool,
2422
2496
  validateTypes,
2423
2497
  validator,
2424
- validatorSymbol,
2425
2498
  withUserAgentSuffix,
2426
2499
  withoutTrailingSlash,
2427
2500
  zodSchema,