@ai-sdk/provider-utils 3.0.19 → 3.0.21

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
@@ -100,17 +100,17 @@ var DelayedPromise = class {
100
100
  return this._promise;
101
101
  }
102
102
  resolve(value) {
103
- var _a;
103
+ var _a2;
104
104
  this.status = { type: "resolved", value };
105
105
  if (this._promise) {
106
- (_a = this._resolve) == null ? void 0 : _a.call(this, value);
106
+ (_a2 = this._resolve) == null ? void 0 : _a2.call(this, value);
107
107
  }
108
108
  }
109
109
  reject(error) {
110
- var _a;
110
+ var _a2;
111
111
  this.status = { type: "rejected", error };
112
112
  if (this._promise) {
113
- (_a = this._reject) == null ? void 0 : _a.call(this, error);
113
+ (_a2 = this._reject) == null ? void 0 : _a2.call(this, error);
114
114
  }
115
115
  }
116
116
  isResolved() {
@@ -129,6 +129,86 @@ function extractResponseHeaders(response) {
129
129
  return Object.fromEntries([...response.headers]);
130
130
  }
131
131
 
132
+ // src/download-error.ts
133
+ import { AISDKError } from "@ai-sdk/provider";
134
+ var name = "AI_DownloadError";
135
+ var marker = `vercel.ai.error.${name}`;
136
+ var symbol = Symbol.for(marker);
137
+ var _a, _b;
138
+ var DownloadError = class extends (_b = AISDKError, _a = symbol, _b) {
139
+ constructor({
140
+ url,
141
+ statusCode,
142
+ statusText,
143
+ cause,
144
+ message = cause == null ? `Failed to download ${url}: ${statusCode} ${statusText}` : `Failed to download ${url}: ${cause}`
145
+ }) {
146
+ super({ name, message, cause });
147
+ this[_a] = true;
148
+ this.url = url;
149
+ this.statusCode = statusCode;
150
+ this.statusText = statusText;
151
+ }
152
+ static isInstance(error) {
153
+ return AISDKError.hasMarker(error, marker);
154
+ }
155
+ };
156
+
157
+ // src/read-response-with-size-limit.ts
158
+ var DEFAULT_MAX_DOWNLOAD_SIZE = 2 * 1024 * 1024 * 1024;
159
+ async function readResponseWithSizeLimit({
160
+ response,
161
+ url,
162
+ maxBytes = DEFAULT_MAX_DOWNLOAD_SIZE
163
+ }) {
164
+ const contentLength = response.headers.get("content-length");
165
+ if (contentLength != null) {
166
+ const length = parseInt(contentLength, 10);
167
+ if (!isNaN(length) && length > maxBytes) {
168
+ throw new DownloadError({
169
+ url,
170
+ message: `Download of ${url} exceeded maximum size of ${maxBytes} bytes (Content-Length: ${length}).`
171
+ });
172
+ }
173
+ }
174
+ const body = response.body;
175
+ if (body == null) {
176
+ return new Uint8Array(0);
177
+ }
178
+ const reader = body.getReader();
179
+ const chunks = [];
180
+ let totalBytes = 0;
181
+ try {
182
+ while (true) {
183
+ const { done, value } = await reader.read();
184
+ if (done) {
185
+ break;
186
+ }
187
+ totalBytes += value.length;
188
+ if (totalBytes > maxBytes) {
189
+ throw new DownloadError({
190
+ url,
191
+ message: `Download of ${url} exceeded maximum size of ${maxBytes} bytes.`
192
+ });
193
+ }
194
+ chunks.push(value);
195
+ }
196
+ } finally {
197
+ try {
198
+ await reader.cancel();
199
+ } finally {
200
+ reader.releaseLock();
201
+ }
202
+ }
203
+ const result = new Uint8Array(totalBytes);
204
+ let offset = 0;
205
+ for (const chunk of chunks) {
206
+ result.set(chunk, offset);
207
+ offset += chunk.length;
208
+ }
209
+ return result;
210
+ }
211
+
132
212
  // src/generate-id.ts
133
213
  import { InvalidArgumentError } from "@ai-sdk/provider";
134
214
  var createIdGenerator = ({
@@ -212,14 +292,14 @@ function handleFetchError({
212
292
 
213
293
  // src/get-runtime-environment-user-agent.ts
214
294
  function getRuntimeEnvironmentUserAgent(globalThisAny = globalThis) {
215
- var _a, _b, _c;
295
+ var _a2, _b2, _c;
216
296
  if (globalThisAny.window) {
217
297
  return `runtime/browser`;
218
298
  }
219
- if ((_a = globalThisAny.navigator) == null ? void 0 : _a.userAgent) {
299
+ if ((_a2 = globalThisAny.navigator) == null ? void 0 : _a2.userAgent) {
220
300
  return `runtime/${globalThisAny.navigator.userAgent.toLowerCase()}`;
221
301
  }
222
- if ((_c = (_b = globalThisAny.process) == null ? void 0 : _b.versions) == null ? void 0 : _c.node) {
302
+ if ((_c = (_b2 = globalThisAny.process) == null ? void 0 : _b2.versions) == null ? void 0 : _c.node) {
223
303
  return `runtime/node.js/${globalThisAny.process.version.substring(0)}`;
224
304
  }
225
305
  if (globalThisAny.EdgeRuntime) {
@@ -263,7 +343,7 @@ function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
263
343
  }
264
344
 
265
345
  // src/version.ts
266
- var VERSION = true ? "3.0.19" : "0.0.0-test";
346
+ var VERSION = true ? "3.0.21" : "0.0.0-test";
267
347
 
268
348
  // src/get-from-api.ts
269
349
  var getOriginalFetch = () => globalThis.fetch;
@@ -360,8 +440,8 @@ function injectJsonInstructionIntoMessages({
360
440
  schemaPrefix,
361
441
  schemaSuffix
362
442
  }) {
363
- var _a, _b;
364
- const systemMessage = ((_a = messages[0]) == null ? void 0 : _a.role) === "system" ? { ...messages[0] } : { role: "system", content: "" };
443
+ var _a2, _b2;
444
+ const systemMessage = ((_a2 = messages[0]) == null ? void 0 : _a2.role) === "system" ? { ...messages[0] } : { role: "system", content: "" };
365
445
  systemMessage.content = injectJsonInstruction({
366
446
  prompt: systemMessage.content,
367
447
  schema,
@@ -370,7 +450,7 @@ function injectJsonInstructionIntoMessages({
370
450
  });
371
451
  return [
372
452
  systemMessage,
373
- ...((_b = messages[0]) == null ? void 0 : _b.role) === "system" ? messages.slice(1) : messages
453
+ ...((_b2 = messages[0]) == null ? void 0 : _b2.role) === "system" ? messages.slice(1) : messages
374
454
  ];
375
455
  }
376
456
 
@@ -478,15 +558,15 @@ function loadSetting({
478
558
 
479
559
  // src/media-type-to-extension.ts
480
560
  function mediaTypeToExtension(mediaType) {
481
- var _a;
561
+ var _a2;
482
562
  const [_type, subtype = ""] = mediaType.toLowerCase().split("/");
483
- return (_a = {
563
+ return (_a2 = {
484
564
  mpeg: "mp3",
485
565
  "x-wav": "wav",
486
566
  opus: "ogg",
487
567
  mp4: "m4a",
488
568
  "x-m4a": "m4a"
489
- }[subtype]) != null ? _a : subtype;
569
+ }[subtype]) != null ? _a2 : subtype;
490
570
  }
491
571
 
492
572
  // src/parse-json.ts
@@ -835,7 +915,7 @@ function dynamicTool(tool2) {
835
915
  // src/provider-defined-tool-factory.ts
836
916
  function createProviderDefinedToolFactory({
837
917
  id,
838
- name,
918
+ name: name2,
839
919
  inputSchema
840
920
  }) {
841
921
  return ({
@@ -849,7 +929,7 @@ function createProviderDefinedToolFactory({
849
929
  }) => tool({
850
930
  type: "provider-defined",
851
931
  id,
852
- name,
932
+ name: name2,
853
933
  args,
854
934
  inputSchema,
855
935
  outputSchema,
@@ -862,7 +942,7 @@ function createProviderDefinedToolFactory({
862
942
  }
863
943
  function createProviderDefinedToolFactoryWithOutputSchema({
864
944
  id,
865
- name,
945
+ name: name2,
866
946
  inputSchema,
867
947
  outputSchema
868
948
  }) {
@@ -876,7 +956,7 @@ function createProviderDefinedToolFactoryWithOutputSchema({
876
956
  }) => tool({
877
957
  type: "provider-defined",
878
958
  id,
879
- name,
959
+ name: name2,
880
960
  args,
881
961
  inputSchema,
882
962
  outputSchema,
@@ -1152,11 +1232,11 @@ function parseAnyDef() {
1152
1232
  // src/zod-to-json-schema/parsers/array.ts
1153
1233
  import { ZodFirstPartyTypeKind } from "zod/v3";
1154
1234
  function parseArrayDef(def, refs) {
1155
- var _a, _b, _c;
1235
+ var _a2, _b2, _c;
1156
1236
  const res = {
1157
1237
  type: "array"
1158
1238
  };
1159
- if (((_a = def.type) == null ? void 0 : _a._def) && ((_c = (_b = def.type) == null ? void 0 : _b._def) == null ? void 0 : _c.typeName) !== ZodFirstPartyTypeKind.ZodAny) {
1239
+ if (((_a2 = def.type) == null ? void 0 : _a2._def) && ((_c = (_b2 = def.type) == null ? void 0 : _b2._def) == null ? void 0 : _c.typeName) !== ZodFirstPartyTypeKind.ZodAny) {
1160
1240
  res.items = parseDef(def.type._def, {
1161
1241
  ...refs,
1162
1242
  currentPath: [...refs.currentPath, "items"]
@@ -1549,8 +1629,8 @@ function escapeNonAlphaNumeric(source) {
1549
1629
  return result;
1550
1630
  }
1551
1631
  function addFormat(schema, value, message, refs) {
1552
- var _a;
1553
- if (schema.format || ((_a = schema.anyOf) == null ? void 0 : _a.some((x) => x.format))) {
1632
+ var _a2;
1633
+ if (schema.format || ((_a2 = schema.anyOf) == null ? void 0 : _a2.some((x) => x.format))) {
1554
1634
  if (!schema.anyOf) {
1555
1635
  schema.anyOf = [];
1556
1636
  }
@@ -1569,8 +1649,8 @@ function addFormat(schema, value, message, refs) {
1569
1649
  }
1570
1650
  }
1571
1651
  function addPattern(schema, regex, message, refs) {
1572
- var _a;
1573
- if (schema.pattern || ((_a = schema.allOf) == null ? void 0 : _a.some((x) => x.pattern))) {
1652
+ var _a2;
1653
+ if (schema.pattern || ((_a2 = schema.allOf) == null ? void 0 : _a2.some((x) => x.pattern))) {
1574
1654
  if (!schema.allOf) {
1575
1655
  schema.allOf = [];
1576
1656
  }
@@ -1589,7 +1669,7 @@ function addPattern(schema, regex, message, refs) {
1589
1669
  }
1590
1670
  }
1591
1671
  function stringifyRegExpWithFlags(regex, refs) {
1592
- var _a;
1672
+ var _a2;
1593
1673
  if (!refs.applyRegexFlags || !regex.flags) {
1594
1674
  return regex.source;
1595
1675
  }
@@ -1619,7 +1699,7 @@ function stringifyRegExpWithFlags(regex, refs) {
1619
1699
  pattern += source[i];
1620
1700
  pattern += `${source[i - 2]}-${source[i]}`.toUpperCase();
1621
1701
  inCharRange = false;
1622
- } else if (source[i + 1] === "-" && ((_a = source[i + 2]) == null ? void 0 : _a.match(/[a-z]/))) {
1702
+ } else if (source[i + 1] === "-" && ((_a2 = source[i + 2]) == null ? void 0 : _a2.match(/[a-z]/))) {
1623
1703
  pattern += source[i];
1624
1704
  inCharRange = true;
1625
1705
  } else {
@@ -1673,15 +1753,15 @@ function stringifyRegExpWithFlags(regex, refs) {
1673
1753
 
1674
1754
  // src/zod-to-json-schema/parsers/record.ts
1675
1755
  function parseRecordDef(def, refs) {
1676
- var _a, _b, _c, _d, _e, _f;
1756
+ var _a2, _b2, _c, _d, _e, _f;
1677
1757
  const schema = {
1678
1758
  type: "object",
1679
- additionalProperties: (_a = parseDef(def.valueType._def, {
1759
+ additionalProperties: (_a2 = parseDef(def.valueType._def, {
1680
1760
  ...refs,
1681
1761
  currentPath: [...refs.currentPath, "additionalProperties"]
1682
- })) != null ? _a : refs.allowedAdditionalProperties
1762
+ })) != null ? _a2 : refs.allowedAdditionalProperties
1683
1763
  };
1684
- if (((_b = def.keyType) == null ? void 0 : _b._def.typeName) === ZodFirstPartyTypeKind2.ZodString && ((_c = def.keyType._def.checks) == null ? void 0 : _c.length)) {
1764
+ if (((_b2 = def.keyType) == null ? void 0 : _b2._def.typeName) === ZodFirstPartyTypeKind2.ZodString && ((_c = def.keyType._def.checks) == null ? void 0 : _c.length)) {
1685
1765
  const { type, ...keyType } = parseStringDef(def.keyType._def, refs);
1686
1766
  return {
1687
1767
  ...schema,
@@ -1954,8 +2034,8 @@ function safeIsOptional(schema) {
1954
2034
 
1955
2035
  // src/zod-to-json-schema/parsers/optional.ts
1956
2036
  var parseOptionalDef = (def, refs) => {
1957
- var _a;
1958
- if (refs.currentPath.toString() === ((_a = refs.propertyPath) == null ? void 0 : _a.toString())) {
2037
+ var _a2;
2038
+ if (refs.currentPath.toString() === ((_a2 = refs.propertyPath) == null ? void 0 : _a2.toString())) {
1959
2039
  return parseDef(def.innerType._def, refs);
1960
2040
  }
1961
2041
  const innerSchema = parseDef(def.innerType._def, {
@@ -2143,10 +2223,10 @@ var selectParser = (def, typeName, refs) => {
2143
2223
 
2144
2224
  // src/zod-to-json-schema/parse-def.ts
2145
2225
  function parseDef(def, refs, forceResolution = false) {
2146
- var _a;
2226
+ var _a2;
2147
2227
  const seenItem = refs.seen.get(def);
2148
2228
  if (refs.override) {
2149
- const overrideResult = (_a = refs.override) == null ? void 0 : _a.call(
2229
+ const overrideResult = (_a2 = refs.override) == null ? void 0 : _a2.call(
2150
2230
  refs,
2151
2231
  def,
2152
2232
  refs,
@@ -2214,11 +2294,11 @@ var getRefs = (options) => {
2214
2294
  currentPath,
2215
2295
  propertyPath: void 0,
2216
2296
  seen: new Map(
2217
- Object.entries(_options.definitions).map(([name, def]) => [
2297
+ Object.entries(_options.definitions).map(([name2, def]) => [
2218
2298
  def._def,
2219
2299
  {
2220
2300
  def: def._def,
2221
- path: [..._options.basePath, _options.definitionPath, name],
2301
+ path: [..._options.basePath, _options.definitionPath, name2],
2222
2302
  // Resolution of references will be forced even though seen, so it's ok that the schema is undefined here for now.
2223
2303
  jsonSchema: void 0
2224
2304
  }
@@ -2229,50 +2309,50 @@ var getRefs = (options) => {
2229
2309
 
2230
2310
  // src/zod-to-json-schema/zod-to-json-schema.ts
2231
2311
  var zodToJsonSchema = (schema, options) => {
2232
- var _a;
2312
+ var _a2;
2233
2313
  const refs = getRefs(options);
2234
2314
  let definitions = typeof options === "object" && options.definitions ? Object.entries(options.definitions).reduce(
2235
- (acc, [name2, schema2]) => {
2236
- var _a2;
2315
+ (acc, [name3, schema2]) => {
2316
+ var _a3;
2237
2317
  return {
2238
2318
  ...acc,
2239
- [name2]: (_a2 = parseDef(
2319
+ [name3]: (_a3 = parseDef(
2240
2320
  schema2._def,
2241
2321
  {
2242
2322
  ...refs,
2243
- currentPath: [...refs.basePath, refs.definitionPath, name2]
2323
+ currentPath: [...refs.basePath, refs.definitionPath, name3]
2244
2324
  },
2245
2325
  true
2246
- )) != null ? _a2 : parseAnyDef()
2326
+ )) != null ? _a3 : parseAnyDef()
2247
2327
  };
2248
2328
  },
2249
2329
  {}
2250
2330
  ) : void 0;
2251
- const name = typeof options === "string" ? options : (options == null ? void 0 : options.nameStrategy) === "title" ? void 0 : options == null ? void 0 : options.name;
2252
- const main = (_a = parseDef(
2331
+ const name2 = typeof options === "string" ? options : (options == null ? void 0 : options.nameStrategy) === "title" ? void 0 : options == null ? void 0 : options.name;
2332
+ const main = (_a2 = parseDef(
2253
2333
  schema._def,
2254
- name === void 0 ? refs : {
2334
+ name2 === void 0 ? refs : {
2255
2335
  ...refs,
2256
- currentPath: [...refs.basePath, refs.definitionPath, name]
2336
+ currentPath: [...refs.basePath, refs.definitionPath, name2]
2257
2337
  },
2258
2338
  false
2259
- )) != null ? _a : parseAnyDef();
2339
+ )) != null ? _a2 : parseAnyDef();
2260
2340
  const title = typeof options === "object" && options.name !== void 0 && options.nameStrategy === "title" ? options.name : void 0;
2261
2341
  if (title !== void 0) {
2262
2342
  main.title = title;
2263
2343
  }
2264
- const combined = name === void 0 ? definitions ? {
2344
+ const combined = name2 === void 0 ? definitions ? {
2265
2345
  ...main,
2266
2346
  [refs.definitionPath]: definitions
2267
2347
  } : main : {
2268
2348
  $ref: [
2269
2349
  ...refs.$refStrategy === "relative" ? [] : refs.basePath,
2270
2350
  refs.definitionPath,
2271
- name
2351
+ name2
2272
2352
  ].join("/"),
2273
2353
  [refs.definitionPath]: {
2274
2354
  ...definitions,
2275
- [name]: main
2355
+ [name2]: main
2276
2356
  }
2277
2357
  };
2278
2358
  combined.$schema = "http://json-schema.org/draft-07/schema#";
@@ -2284,8 +2364,8 @@ var zod_to_json_schema_default = zodToJsonSchema;
2284
2364
 
2285
2365
  // src/zod-schema.ts
2286
2366
  function zod3Schema(zodSchema2, options) {
2287
- var _a;
2288
- const useReferences = (_a = options == null ? void 0 : options.useReferences) != null ? _a : false;
2367
+ var _a2;
2368
+ const useReferences = (_a2 = options == null ? void 0 : options.useReferences) != null ? _a2 : false;
2289
2369
  return jsonSchema(
2290
2370
  // defer json schema creation to avoid unnecessary computation when only validation is needed
2291
2371
  () => zod_to_json_schema_default(zodSchema2, {
@@ -2300,8 +2380,8 @@ function zod3Schema(zodSchema2, options) {
2300
2380
  );
2301
2381
  }
2302
2382
  function zod4Schema(zodSchema2, options) {
2303
- var _a;
2304
- const useReferences = (_a = options == null ? void 0 : options.useReferences) != null ? _a : false;
2383
+ var _a2;
2384
+ const useReferences = (_a2 = options == null ? void 0 : options.useReferences) != null ? _a2 : false;
2305
2385
  return jsonSchema(
2306
2386
  // defer json schema creation to avoid unnecessary computation when only validation is needed
2307
2387
  () => addAdditionalPropertiesToJsonSchema(
@@ -2421,7 +2501,9 @@ import {
2421
2501
  EventSourceParserStream as EventSourceParserStream2
2422
2502
  } from "eventsource-parser/stream";
2423
2503
  export {
2504
+ DEFAULT_MAX_DOWNLOAD_SIZE,
2424
2505
  DelayedPromise,
2506
+ DownloadError,
2425
2507
  EventSourceParserStream2 as EventSourceParserStream,
2426
2508
  VERSION,
2427
2509
  asSchema,
@@ -2467,6 +2549,7 @@ export {
2467
2549
  postFormDataToApi,
2468
2550
  postJsonToApi,
2469
2551
  postToApi,
2552
+ readResponseWithSizeLimit,
2470
2553
  removeUndefinedEntries,
2471
2554
  resolve,
2472
2555
  safeParseJSON,