@ai-sdk/provider-utils 4.0.0-beta.10 → 4.0.0-beta.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 ? "4.0.0-beta.10" : "0.0.0-test";
197
+ var VERSION = true ? "4.0.0-beta.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) => {
@@ -2175,7 +2184,8 @@ function zod3Schema(zodSchema2, options) {
2175
2184
  var _a;
2176
2185
  const useReferences = (_a = options == null ? void 0 : options.useReferences) != null ? _a : false;
2177
2186
  return jsonSchema(
2178
- zod_to_json_schema_default(zodSchema2, {
2187
+ // defer json schema creation to avoid unnecessary computation when only validation is needed
2188
+ () => zod_to_json_schema_default(zodSchema2, {
2179
2189
  $refStrategy: useReferences ? "root" : "none"
2180
2190
  }),
2181
2191
  {
@@ -2189,17 +2199,20 @@ function zod3Schema(zodSchema2, options) {
2189
2199
  function zod4Schema(zodSchema2, options) {
2190
2200
  var _a;
2191
2201
  const useReferences = (_a = options == null ? void 0 : options.useReferences) != null ? _a : false;
2192
- const z4JSONSchema = z4.toJSONSchema(zodSchema2, {
2193
- target: "draft-7",
2194
- io: "output",
2195
- reused: useReferences ? "ref" : "inline"
2196
- });
2197
- return jsonSchema(z4JSONSchema, {
2198
- validate: async (value) => {
2199
- const result = await z4.safeParseAsync(zodSchema2, value);
2200
- return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
2202
+ return jsonSchema(
2203
+ // defer json schema creation to avoid unnecessary computation when only validation is needed
2204
+ () => z4.toJSONSchema(zodSchema2, {
2205
+ target: "draft-7",
2206
+ io: "output",
2207
+ reused: useReferences ? "ref" : "inline"
2208
+ }),
2209
+ {
2210
+ validate: async (value) => {
2211
+ const result = await z4.safeParseAsync(zodSchema2, value);
2212
+ return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
2213
+ }
2201
2214
  }
2202
- });
2215
+ );
2203
2216
  }
2204
2217
  function isZod4Schema(zodSchema2) {
2205
2218
  return "_zod" in zodSchema2;
@@ -2214,6 +2227,15 @@ function zodSchema(zodSchema2, options) {
2214
2227
 
2215
2228
  // src/schema.ts
2216
2229
  var schemaSymbol = Symbol.for("vercel.ai.schema");
2230
+ function lazySchema(createSchema) {
2231
+ let schema;
2232
+ return () => {
2233
+ if (schema == null) {
2234
+ schema = createSchema();
2235
+ }
2236
+ return schema;
2237
+ };
2238
+ }
2217
2239
  function jsonSchema(jsonSchema2, {
2218
2240
  validate
2219
2241
  } = {}) {
@@ -2222,7 +2244,12 @@ function jsonSchema(jsonSchema2, {
2222
2244
  _type: void 0,
2223
2245
  // should never be used directly
2224
2246
  [validatorSymbol]: true,
2225
- jsonSchema: jsonSchema2,
2247
+ get jsonSchema() {
2248
+ if (typeof jsonSchema2 === "function") {
2249
+ jsonSchema2 = jsonSchema2();
2250
+ }
2251
+ return jsonSchema2;
2252
+ },
2226
2253
  validate
2227
2254
  };
2228
2255
  }
@@ -2233,7 +2260,7 @@ function asSchema(schema) {
2233
2260
  return schema == null ? jsonSchema({
2234
2261
  properties: {},
2235
2262
  additionalProperties: false
2236
- }) : isSchema(schema) ? schema : zodSchema(schema);
2263
+ }) : isSchema(schema) ? schema : typeof schema === "function" ? schema() : zodSchema(schema);
2237
2264
  }
2238
2265
 
2239
2266
  // src/uint8-utils.ts
@@ -2321,6 +2348,8 @@ export {
2321
2348
  isUrlSupported,
2322
2349
  isValidator,
2323
2350
  jsonSchema,
2351
+ lazySchema,
2352
+ lazyValidator,
2324
2353
  loadApiKey,
2325
2354
  loadOptionalSetting,
2326
2355
  loadSetting,
@@ -2339,7 +2368,6 @@ export {
2339
2368
  tool,
2340
2369
  validateTypes,
2341
2370
  validator,
2342
- validatorSymbol,
2343
2371
  withUserAgentSuffix,
2344
2372
  withoutTrailingSlash,
2345
2373
  zodSchema