@ai-sdk/provider-utils 3.0.9 → 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.mjs CHANGED
@@ -73,45 +73,6 @@ function extractResponseHeaders(response) {
73
73
  return Object.fromEntries([...response.headers]);
74
74
  }
75
75
 
76
- // src/get-runtime-environment-user-agent.ts
77
- function getRuntimeEnvironmentUserAgent(globalThisAny = globalThis) {
78
- var _a, _b, _c;
79
- if (globalThisAny.window) {
80
- return `runtime/browser`;
81
- }
82
- if ((_a = globalThisAny.navigator) == null ? void 0 : _a.userAgent) {
83
- return `runtime/${globalThisAny.navigator.userAgent.toLowerCase()}`;
84
- }
85
- if ((_c = (_b = globalThisAny.process) == null ? void 0 : _b.versions) == null ? void 0 : _c.node) {
86
- return `runtime/node.js/${globalThisAny.process.version.substring(0)}`;
87
- }
88
- if (globalThisAny.EdgeRuntime) {
89
- return `runtime/vercel-edge`;
90
- }
91
- return "runtime/unknown";
92
- }
93
-
94
- // src/remove-undefined-entries.ts
95
- function removeUndefinedEntries(record) {
96
- return Object.fromEntries(
97
- Object.entries(record).filter(([_key, value]) => value != null)
98
- );
99
- }
100
-
101
- // src/with-user-agent-suffix.ts
102
- function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
103
- const cleanedHeaders = removeUndefinedEntries(
104
- headers != null ? headers : {}
105
- );
106
- const normalizedHeaders = new Headers(cleanedHeaders);
107
- const currentUserAgentHeader = normalizedHeaders.get("user-agent") || "";
108
- normalizedHeaders.set(
109
- "user-agent",
110
- [currentUserAgentHeader, ...userAgentSuffixParts].filter(Boolean).join(" ")
111
- );
112
- return Object.fromEntries(normalizedHeaders);
113
- }
114
-
115
76
  // src/generate-id.ts
116
77
  import { InvalidArgumentError } from "@ai-sdk/provider";
117
78
  var createIdGenerator = ({
@@ -193,8 +154,47 @@ function handleFetchError({
193
154
  return error;
194
155
  }
195
156
 
157
+ // src/get-runtime-environment-user-agent.ts
158
+ function getRuntimeEnvironmentUserAgent(globalThisAny = globalThis) {
159
+ var _a, _b, _c;
160
+ if (globalThisAny.window) {
161
+ return `runtime/browser`;
162
+ }
163
+ if ((_a = globalThisAny.navigator) == null ? void 0 : _a.userAgent) {
164
+ return `runtime/${globalThisAny.navigator.userAgent.toLowerCase()}`;
165
+ }
166
+ if ((_c = (_b = globalThisAny.process) == null ? void 0 : _b.versions) == null ? void 0 : _c.node) {
167
+ return `runtime/node.js/${globalThisAny.process.version.substring(0)}`;
168
+ }
169
+ if (globalThisAny.EdgeRuntime) {
170
+ return `runtime/vercel-edge`;
171
+ }
172
+ return "runtime/unknown";
173
+ }
174
+
175
+ // src/remove-undefined-entries.ts
176
+ function removeUndefinedEntries(record) {
177
+ return Object.fromEntries(
178
+ Object.entries(record).filter(([_key, value]) => value != null)
179
+ );
180
+ }
181
+
182
+ // src/with-user-agent-suffix.ts
183
+ function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
184
+ const cleanedHeaders = removeUndefinedEntries(
185
+ headers != null ? headers : {}
186
+ );
187
+ const normalizedHeaders = new Headers(cleanedHeaders);
188
+ const currentUserAgentHeader = normalizedHeaders.get("user-agent") || "";
189
+ normalizedHeaders.set(
190
+ "user-agent",
191
+ [currentUserAgentHeader, ...userAgentSuffixParts].filter(Boolean).join(" ")
192
+ );
193
+ return Object.fromEntries(normalizedHeaders);
194
+ }
195
+
196
196
  // src/version.ts
197
- var VERSION = true ? "3.0.9" : "0.0.0-test";
197
+ var VERSION = true ? "3.0.11" : "0.0.0-test";
198
198
 
199
199
  // src/get-from-api.ts
200
200
  var getOriginalFetch = () => globalThis.fetch;
@@ -483,8 +483,17 @@ function validator(validate) {
483
483
  function isValidator(value) {
484
484
  return typeof value === "object" && value !== null && validatorSymbol in value && value[validatorSymbol] === true && "validate" in value;
485
485
  }
486
+ function lazyValidator(createValidator) {
487
+ let validator2;
488
+ return () => {
489
+ if (validator2 == null) {
490
+ validator2 = createValidator();
491
+ }
492
+ return validator2;
493
+ };
494
+ }
486
495
  function asValidator(value) {
487
- return isValidator(value) ? value : standardSchemaValidator(value);
496
+ return isValidator(value) ? value : typeof value === "function" ? value() : standardSchemaValidator(value);
488
497
  }
489
498
  function standardSchemaValidator(standardSchema) {
490
499
  return validator(async (value) => {
@@ -987,8 +996,7 @@ import * as z4 from "zod/v4";
987
996
  var getRelativePath = (pathA, pathB) => {
988
997
  let i = 0;
989
998
  for (; i < pathA.length && i < pathB.length; i++) {
990
- if (pathA[i] !== pathB[i])
991
- break;
999
+ if (pathA[i] !== pathB[i]) break;
992
1000
  }
993
1001
  return [(pathA.length - i).toString(), ...pathB.slice(i)].join("/");
994
1002
  };
@@ -1066,8 +1074,7 @@ function parseBigintDef(def) {
1066
1074
  type: "integer",
1067
1075
  format: "int64"
1068
1076
  };
1069
- if (!def.checks)
1070
- return res;
1077
+ if (!def.checks) return res;
1071
1078
  for (const check of def.checks) {
1072
1079
  switch (check.kind) {
1073
1080
  case "min":
@@ -1172,8 +1179,7 @@ function parseEnumDef(def) {
1172
1179
 
1173
1180
  // src/zod-to-json-schema/parsers/intersection.ts
1174
1181
  var isJsonSchema7AllOfType = (type) => {
1175
- if ("type" in type && type.type === "string")
1176
- return false;
1182
+ if ("type" in type && type.type === "string") return false;
1177
1183
  return "allOf" in type;
1178
1184
  };
1179
1185
  function parseIntersectionDef(def, refs) {
@@ -1679,8 +1685,7 @@ function parseUnionDef(def, refs) {
1679
1685
  case "bigint":
1680
1686
  return [...acc, "integer"];
1681
1687
  case "object":
1682
- if (x._def.value === null)
1683
- return [...acc, "null"];
1688
+ if (x._def.value === null) return [...acc, "null"];
1684
1689
  case "symbol":
1685
1690
  case "undefined":
1686
1691
  case "function":
@@ -1752,8 +1757,7 @@ function parseNumberDef(def) {
1752
1757
  const res = {
1753
1758
  type: "number"
1754
1759
  };
1755
- if (!def.checks)
1756
- return res;
1760
+ if (!def.checks) return res;
1757
1761
  for (const check of def.checks) {
1758
1762
  switch (check.kind) {
1759
1763
  case "int":
@@ -2176,7 +2180,8 @@ function zod3Schema(zodSchema2, options) {
2176
2180
  var _a;
2177
2181
  const useReferences = (_a = options == null ? void 0 : options.useReferences) != null ? _a : false;
2178
2182
  return jsonSchema(
2179
- zod_to_json_schema_default(zodSchema2, {
2183
+ // defer json schema creation to avoid unnecessary computation when only validation is needed
2184
+ () => zod_to_json_schema_default(zodSchema2, {
2180
2185
  $refStrategy: useReferences ? "root" : "none"
2181
2186
  }),
2182
2187
  {
@@ -2190,17 +2195,20 @@ function zod3Schema(zodSchema2, options) {
2190
2195
  function zod4Schema(zodSchema2, options) {
2191
2196
  var _a;
2192
2197
  const useReferences = (_a = options == null ? void 0 : options.useReferences) != null ? _a : false;
2193
- const z4JSONSchema = z4.toJSONSchema(zodSchema2, {
2194
- target: "draft-7",
2195
- io: "output",
2196
- reused: useReferences ? "ref" : "inline"
2197
- });
2198
- return jsonSchema(z4JSONSchema, {
2199
- validate: async (value) => {
2200
- const result = await z4.safeParseAsync(zodSchema2, value);
2201
- return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
2198
+ return jsonSchema(
2199
+ // defer json schema creation to avoid unnecessary computation when only validation is needed
2200
+ () => z4.toJSONSchema(zodSchema2, {
2201
+ target: "draft-7",
2202
+ io: "output",
2203
+ reused: useReferences ? "ref" : "inline"
2204
+ }),
2205
+ {
2206
+ validate: async (value) => {
2207
+ const result = await z4.safeParseAsync(zodSchema2, value);
2208
+ return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
2209
+ }
2202
2210
  }
2203
- });
2211
+ );
2204
2212
  }
2205
2213
  function isZod4Schema(zodSchema2) {
2206
2214
  return "_zod" in zodSchema2;
@@ -2215,6 +2223,15 @@ function zodSchema(zodSchema2, options) {
2215
2223
 
2216
2224
  // src/schema.ts
2217
2225
  var schemaSymbol = Symbol.for("vercel.ai.schema");
2226
+ function lazySchema(createSchema) {
2227
+ let schema;
2228
+ return () => {
2229
+ if (schema == null) {
2230
+ schema = createSchema();
2231
+ }
2232
+ return schema;
2233
+ };
2234
+ }
2218
2235
  function jsonSchema(jsonSchema2, {
2219
2236
  validate
2220
2237
  } = {}) {
@@ -2223,7 +2240,12 @@ function jsonSchema(jsonSchema2, {
2223
2240
  _type: void 0,
2224
2241
  // should never be used directly
2225
2242
  [validatorSymbol]: true,
2226
- jsonSchema: jsonSchema2,
2243
+ get jsonSchema() {
2244
+ if (typeof jsonSchema2 === "function") {
2245
+ jsonSchema2 = jsonSchema2();
2246
+ }
2247
+ return jsonSchema2;
2248
+ },
2227
2249
  validate
2228
2250
  };
2229
2251
  }
@@ -2234,7 +2256,7 @@ function asSchema(schema) {
2234
2256
  return schema == null ? jsonSchema({
2235
2257
  properties: {},
2236
2258
  additionalProperties: false
2237
- }) : isSchema(schema) ? schema : zodSchema(schema);
2259
+ }) : isSchema(schema) ? schema : typeof schema === "function" ? schema() : zodSchema(schema);
2238
2260
  }
2239
2261
 
2240
2262
  // src/uint8-utils.ts
@@ -2322,6 +2344,8 @@ export {
2322
2344
  isUrlSupported,
2323
2345
  isValidator,
2324
2346
  jsonSchema,
2347
+ lazySchema,
2348
+ lazyValidator,
2325
2349
  loadApiKey,
2326
2350
  loadOptionalSetting,
2327
2351
  loadSetting,
@@ -2340,7 +2364,6 @@ export {
2340
2364
  tool,
2341
2365
  validateTypes,
2342
2366
  validator,
2343
- validatorSymbol,
2344
2367
  withUserAgentSuffix,
2345
2368
  withoutTrailingSlash,
2346
2369
  zodSchema