@ai-sdk/provider-utils 3.0.10 → 3.0.11

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 ? "3.0.10" : "0.0.0-test";
288
+ var VERSION = true ? "3.0.11" : "0.0.0-test";
288
289
 
289
290
  // src/get-from-api.ts
290
291
  var getOriginalFetch = () => globalThis.fetch;
@@ -570,8 +571,17 @@ 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
586
  function standardSchemaValidator(standardSchema) {
577
587
  return validator(async (value) => {
@@ -1072,8 +1082,7 @@ var z4 = __toESM(require("zod/v4"));
1072
1082
  var getRelativePath = (pathA, pathB) => {
1073
1083
  let i = 0;
1074
1084
  for (; i < pathA.length && i < pathB.length; i++) {
1075
- if (pathA[i] !== pathB[i])
1076
- break;
1085
+ if (pathA[i] !== pathB[i]) break;
1077
1086
  }
1078
1087
  return [(pathA.length - i).toString(), ...pathB.slice(i)].join("/");
1079
1088
  };
@@ -1151,8 +1160,7 @@ function parseBigintDef(def) {
1151
1160
  type: "integer",
1152
1161
  format: "int64"
1153
1162
  };
1154
- if (!def.checks)
1155
- return res;
1163
+ if (!def.checks) return res;
1156
1164
  for (const check of def.checks) {
1157
1165
  switch (check.kind) {
1158
1166
  case "min":
@@ -1257,8 +1265,7 @@ function parseEnumDef(def) {
1257
1265
 
1258
1266
  // src/zod-to-json-schema/parsers/intersection.ts
1259
1267
  var isJsonSchema7AllOfType = (type) => {
1260
- if ("type" in type && type.type === "string")
1261
- return false;
1268
+ if ("type" in type && type.type === "string") return false;
1262
1269
  return "allOf" in type;
1263
1270
  };
1264
1271
  function parseIntersectionDef(def, refs) {
@@ -1762,8 +1769,7 @@ function parseUnionDef(def, refs) {
1762
1769
  case "bigint":
1763
1770
  return [...acc, "integer"];
1764
1771
  case "object":
1765
- if (x._def.value === null)
1766
- return [...acc, "null"];
1772
+ if (x._def.value === null) return [...acc, "null"];
1767
1773
  case "symbol":
1768
1774
  case "undefined":
1769
1775
  case "function":
@@ -1835,8 +1841,7 @@ function parseNumberDef(def) {
1835
1841
  const res = {
1836
1842
  type: "number"
1837
1843
  };
1838
- if (!def.checks)
1839
- return res;
1844
+ if (!def.checks) return res;
1840
1845
  for (const check of def.checks) {
1841
1846
  switch (check.kind) {
1842
1847
  case "int":
@@ -2259,7 +2264,8 @@ function zod3Schema(zodSchema2, options) {
2259
2264
  var _a;
2260
2265
  const useReferences = (_a = options == null ? void 0 : options.useReferences) != null ? _a : false;
2261
2266
  return jsonSchema(
2262
- zod_to_json_schema_default(zodSchema2, {
2267
+ // defer json schema creation to avoid unnecessary computation when only validation is needed
2268
+ () => zod_to_json_schema_default(zodSchema2, {
2263
2269
  $refStrategy: useReferences ? "root" : "none"
2264
2270
  }),
2265
2271
  {
@@ -2273,17 +2279,20 @@ function zod3Schema(zodSchema2, options) {
2273
2279
  function zod4Schema(zodSchema2, options) {
2274
2280
  var _a;
2275
2281
  const useReferences = (_a = options == null ? void 0 : options.useReferences) != null ? _a : false;
2276
- const z4JSONSchema = z4.toJSONSchema(zodSchema2, {
2277
- target: "draft-7",
2278
- io: "output",
2279
- reused: useReferences ? "ref" : "inline"
2280
- });
2281
- return jsonSchema(z4JSONSchema, {
2282
- validate: async (value) => {
2283
- const result = await z4.safeParseAsync(zodSchema2, value);
2284
- return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
2282
+ return jsonSchema(
2283
+ // defer json schema creation to avoid unnecessary computation when only validation is needed
2284
+ () => z4.toJSONSchema(zodSchema2, {
2285
+ target: "draft-7",
2286
+ io: "output",
2287
+ reused: useReferences ? "ref" : "inline"
2288
+ }),
2289
+ {
2290
+ validate: async (value) => {
2291
+ const result = await z4.safeParseAsync(zodSchema2, value);
2292
+ return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
2293
+ }
2285
2294
  }
2286
- });
2295
+ );
2287
2296
  }
2288
2297
  function isZod4Schema(zodSchema2) {
2289
2298
  return "_zod" in zodSchema2;
@@ -2298,6 +2307,15 @@ function zodSchema(zodSchema2, options) {
2298
2307
 
2299
2308
  // src/schema.ts
2300
2309
  var schemaSymbol = Symbol.for("vercel.ai.schema");
2310
+ function lazySchema(createSchema) {
2311
+ let schema;
2312
+ return () => {
2313
+ if (schema == null) {
2314
+ schema = createSchema();
2315
+ }
2316
+ return schema;
2317
+ };
2318
+ }
2301
2319
  function jsonSchema(jsonSchema2, {
2302
2320
  validate
2303
2321
  } = {}) {
@@ -2306,7 +2324,12 @@ function jsonSchema(jsonSchema2, {
2306
2324
  _type: void 0,
2307
2325
  // should never be used directly
2308
2326
  [validatorSymbol]: true,
2309
- jsonSchema: jsonSchema2,
2327
+ get jsonSchema() {
2328
+ if (typeof jsonSchema2 === "function") {
2329
+ jsonSchema2 = jsonSchema2();
2330
+ }
2331
+ return jsonSchema2;
2332
+ },
2310
2333
  validate
2311
2334
  };
2312
2335
  }
@@ -2317,7 +2340,7 @@ function asSchema(schema) {
2317
2340
  return schema == null ? jsonSchema({
2318
2341
  properties: {},
2319
2342
  additionalProperties: false
2320
- }) : isSchema(schema) ? schema : zodSchema(schema);
2343
+ }) : isSchema(schema) ? schema : typeof schema === "function" ? schema() : zodSchema(schema);
2321
2344
  }
2322
2345
 
2323
2346
  // src/uint8-utils.ts
@@ -2404,6 +2427,8 @@ var import_stream2 = require("eventsource-parser/stream");
2404
2427
  isUrlSupported,
2405
2428
  isValidator,
2406
2429
  jsonSchema,
2430
+ lazySchema,
2431
+ lazyValidator,
2407
2432
  loadApiKey,
2408
2433
  loadOptionalSetting,
2409
2434
  loadSetting,
@@ -2422,7 +2447,6 @@ var import_stream2 = require("eventsource-parser/stream");
2422
2447
  tool,
2423
2448
  validateTypes,
2424
2449
  validator,
2425
- validatorSymbol,
2426
2450
  withUserAgentSuffix,
2427
2451
  withoutTrailingSlash,
2428
2452
  zodSchema,