@ai-sdk/provider-utils 4.0.14 → 4.0.16

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
@@ -50,25 +50,30 @@ function convertAsyncIteratorToReadableStream(iterator) {
50
50
  // src/create-tool-name-mapping.ts
51
51
  function createToolNameMapping({
52
52
  tools = [],
53
- providerToolNames
53
+ providerToolNames,
54
+ resolveProviderToolName
54
55
  }) {
56
+ var _a2;
55
57
  const customToolNameToProviderToolName = {};
56
58
  const providerToolNameToCustomToolName = {};
57
59
  for (const tool2 of tools) {
58
- if (tool2.type === "provider" && tool2.id in providerToolNames) {
59
- const providerToolName = providerToolNames[tool2.id];
60
+ if (tool2.type === "provider") {
61
+ const providerToolName = (_a2 = resolveProviderToolName == null ? void 0 : resolveProviderToolName(tool2)) != null ? _a2 : tool2.id in providerToolNames ? providerToolNames[tool2.id] : void 0;
62
+ if (providerToolName == null) {
63
+ continue;
64
+ }
60
65
  customToolNameToProviderToolName[tool2.name] = providerToolName;
61
66
  providerToolNameToCustomToolName[providerToolName] = tool2.name;
62
67
  }
63
68
  }
64
69
  return {
65
70
  toProviderToolName: (customToolName) => {
66
- var _a2;
67
- return (_a2 = customToolNameToProviderToolName[customToolName]) != null ? _a2 : customToolName;
71
+ var _a3;
72
+ return (_a3 = customToolNameToProviderToolName[customToolName]) != null ? _a3 : customToolName;
68
73
  },
69
74
  toCustomToolName: (providerToolName) => {
70
- var _a2;
71
- return (_a2 = providerToolNameToCustomToolName[providerToolName]) != null ? _a2 : providerToolName;
75
+ var _a3;
76
+ return (_a3 = providerToolNameToCustomToolName[providerToolName]) != null ? _a3 : providerToolName;
72
77
  }
73
78
  };
74
79
  }
@@ -228,10 +233,68 @@ var DownloadError = class extends (_b = AISDKError, _a = symbol, _b) {
228
233
  }
229
234
  };
230
235
 
236
+ // src/read-response-with-size-limit.ts
237
+ var DEFAULT_MAX_DOWNLOAD_SIZE = 2 * 1024 * 1024 * 1024;
238
+ async function readResponseWithSizeLimit({
239
+ response,
240
+ url,
241
+ maxBytes = DEFAULT_MAX_DOWNLOAD_SIZE
242
+ }) {
243
+ const contentLength = response.headers.get("content-length");
244
+ if (contentLength != null) {
245
+ const length = parseInt(contentLength, 10);
246
+ if (!isNaN(length) && length > maxBytes) {
247
+ throw new DownloadError({
248
+ url,
249
+ message: `Download of ${url} exceeded maximum size of ${maxBytes} bytes (Content-Length: ${length}).`
250
+ });
251
+ }
252
+ }
253
+ const body = response.body;
254
+ if (body == null) {
255
+ return new Uint8Array(0);
256
+ }
257
+ const reader = body.getReader();
258
+ const chunks = [];
259
+ let totalBytes = 0;
260
+ try {
261
+ while (true) {
262
+ const { done, value } = await reader.read();
263
+ if (done) {
264
+ break;
265
+ }
266
+ totalBytes += value.length;
267
+ if (totalBytes > maxBytes) {
268
+ throw new DownloadError({
269
+ url,
270
+ message: `Download of ${url} exceeded maximum size of ${maxBytes} bytes.`
271
+ });
272
+ }
273
+ chunks.push(value);
274
+ }
275
+ } finally {
276
+ try {
277
+ await reader.cancel();
278
+ } finally {
279
+ reader.releaseLock();
280
+ }
281
+ }
282
+ const result = new Uint8Array(totalBytes);
283
+ let offset = 0;
284
+ for (const chunk of chunks) {
285
+ result.set(chunk, offset);
286
+ offset += chunk.length;
287
+ }
288
+ return result;
289
+ }
290
+
231
291
  // src/download-blob.ts
232
- async function downloadBlob(url) {
292
+ async function downloadBlob(url, options) {
293
+ var _a2, _b2;
233
294
  try {
234
- const response = await fetch(url);
295
+ const response = await fetch(url, {
296
+ signal: options == null ? void 0 : options.abortSignal
297
+ });
235
298
  if (!response.ok) {
236
299
  throw new DownloadError({
237
300
  url,
@@ -239,7 +302,13 @@ async function downloadBlob(url) {
239
302
  statusText: response.statusText
240
303
  });
241
304
  }
242
- return await response.blob();
305
+ const data = await readResponseWithSizeLimit({
306
+ response,
307
+ url,
308
+ maxBytes: (_a2 = options == null ? void 0 : options.maxBytes) != null ? _a2 : DEFAULT_MAX_DOWNLOAD_SIZE
309
+ });
310
+ const contentType = (_b2 = response.headers.get("content-type")) != null ? _b2 : void 0;
311
+ return new Blob([data], contentType ? { type: contentType } : void 0);
243
312
  } catch (error) {
244
313
  if (DownloadError.isInstance(error)) {
245
314
  throw error;
@@ -410,7 +479,7 @@ function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
410
479
  }
411
480
 
412
481
  // src/version.ts
413
- var VERSION = true ? "4.0.14" : "0.0.0-test";
482
+ var VERSION = true ? "4.0.16" : "0.0.0-test";
414
483
 
415
484
  // src/get-from-api.ts
416
485
  var getOriginalFetch = () => globalThis.fetch;
@@ -2519,6 +2588,7 @@ import {
2519
2588
  EventSourceParserStream as EventSourceParserStream2
2520
2589
  } from "eventsource-parser/stream";
2521
2590
  export {
2591
+ DEFAULT_MAX_DOWNLOAD_SIZE,
2522
2592
  DelayedPromise,
2523
2593
  DownloadError,
2524
2594
  EventSourceParserStream2 as EventSourceParserStream,
@@ -2567,6 +2637,7 @@ export {
2567
2637
  postFormDataToApi,
2568
2638
  postJsonToApi,
2569
2639
  postToApi,
2640
+ readResponseWithSizeLimit,
2570
2641
  removeUndefinedEntries,
2571
2642
  resolve,
2572
2643
  safeParseJSON,