@ai-sdk/provider-utils 4.0.62 → 4.0.64

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/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # @ai-sdk/provider-utils
2
2
 
3
+ ## 4.0.64
4
+
5
+ ### Patch Changes
6
+
7
+ - d900e58: fix(provider-utils): support cross-realm Uint8Array and ArrayBuffer in binary type guards and base64 conversion
8
+ - d900e58: fix(provider-utils): decode base64 into a preallocated Uint8Array to avoid ~25x transient memory and long event-loop blocking on large files
9
+ - d900e58: Add `stringifyWithBigIntAndCycles` for provider tool serialization that converts BigInt values to decimal strings and replaces ancestor cycles while preserving shared references and `toJSON` behavior.
10
+
11
+ ## 4.0.63
12
+
13
+ ### Patch Changes
14
+
15
+ - 28252da: use native Buffer base64 encoding when available. encoding a `Uint8Array` backed by a detached `ArrayBuffer` now throws a `TypeError` in runtimes with `Buffer` instead of returning an empty string
16
+
3
17
  ## 4.0.62
4
18
 
5
19
  ### Patch Changes
package/dist/index.d.mts CHANGED
@@ -1599,6 +1599,8 @@ declare function stripFileExtension(filename: string): string;
1599
1599
 
1600
1600
  declare function convertBase64ToUint8Array(base64String: string): Uint8Array<ArrayBuffer>;
1601
1601
  declare function convertUint8ArrayToBase64(array: Uint8Array): string;
1602
+ declare function isUint8Array(value: unknown): value is Uint8Array;
1603
+ declare function isArrayBuffer(value: unknown): value is ArrayBuffer;
1602
1604
  declare function convertToBase64(value: string | Uint8Array): string;
1603
1605
 
1604
1606
  /**
@@ -1744,4 +1746,11 @@ interface ToolResult<NAME extends string, INPUT, OUTPUT> {
1744
1746
  */
1745
1747
  type ToolCallOptions = ToolExecutionOptions;
1746
1748
 
1747
- export { type AssistantContent, type AssistantModelMessage, DEFAULT_MAX_DOWNLOAD_SIZE, type DataContent, DelayedPromise, DownloadError, EMBEDDING_MODEL_MAX_INPUT_BYTES_PER_CALL as EXPERIMENTAL_EMBEDDING_MODEL_MAX_INPUT_BYTES_PER_CALL, EMBEDDING_MODEL_PROVIDER_OPTIONS_TRANSFORMER as EXPERIMENTAL_EMBEDDING_MODEL_PROVIDER_OPTIONS_TRANSFORMER, type EmbeddingModelProviderOptionsTransformer, type FetchFunction, type FilePart, type FlexibleSchema, type IdGenerator, type ImagePart, type InferSchema, type InferToolInput, type InferToolOutput, type LazySchema, type MaybePromiseLike, type ModelMessage, type ParseResult, type ProviderOptions, type ProviderToolFactory, type ProviderToolFactoryWithOutputSchema, type ReasoningPart, type Resolvable, type ResponseHandler, type RetryDelayProvider, type RetryErrorFactory, type RetryErrorReason, type RetryFunction, type Schema, type ShouldRetryFunction, type StreamingToolCallDelta, StreamingToolCallTracker, type StreamingToolCallTrackerOptions, type SystemModelMessage, type TextPart, type Tool, type ToolApprovalRequest, type ToolApprovalResponse, type ToolCall, type ToolCallOptions, type ToolCallPart, type ToolContent, type ToolExecuteFunction, type ToolExecutionOptions, type ToolModelMessage, type ToolNameMapping, type ToolNeedsApprovalFunction, type ToolResult, type ToolResultOutput, type ToolResultPart, type UserContent, type UserModelMessage, VERSION, type ValidationResult, asSchema, cancelResponseBody, combineHeaders, convertAsyncIteratorToReadableStream, convertBase64ToUint8Array, convertImageModelFileToDataUri, convertToBase64, convertToFormData, convertUint8ArrayToBase64, createBinaryResponseHandler, createEventSourceResponseHandler, createIdGenerator, createJsonErrorResponseHandler, createJsonResponseHandler, createProviderToolFactory, createProviderToolFactoryWithOutputSchema, createStatusCodeErrorResponseHandler, createToolNameMapping, delay, downloadBlob, dynamicTool, encodePathSegment, executeTool, extractResponseHeaders, fetchWithValidatedEndpoint, fetchWithValidatedRedirects, generateId, getErrorMessage, getFromApi, getRuntimeEnvironmentUserAgent, injectJsonInstructionIntoMessages, isAbortError, isBrowserRuntime, isNonNullable, isParsableJson, isSameOrigin, isUrlSupported, jsonSchema, lazySchema, loadApiKey, loadOptionalSetting, loadSetting, mediaTypeToExtension, normalizeHeaders, parseJSON, parseJsonEventStream, parseProviderOptions, postFormDataToApi, postJsonToApi, postToApi, readResponseWithSizeLimit, removeUndefinedEntries, resolve, retryWithExponentialBackoff, safeParseJSON, safeValidateTypes, secureJsonParse, stripFileExtension, tool, validateDownloadUrl, validateTypes, withUserAgentSuffix, withoutTrailingSlash, zodSchema };
1749
+ /**
1750
+ * Serializes a value as JSON, representing BigInt values as decimal strings and
1751
+ * ancestor cycles as "[Circular]". Reused objects, toJSON methods, and other JSON
1752
+ * serialization behavior are preserved. Getters and toJSON methods may throw.
1753
+ */
1754
+ declare function stringifyWithBigIntAndCycles(value: unknown): string;
1755
+
1756
+ export { type AssistantContent, type AssistantModelMessage, DEFAULT_MAX_DOWNLOAD_SIZE, type DataContent, DelayedPromise, DownloadError, EMBEDDING_MODEL_MAX_INPUT_BYTES_PER_CALL as EXPERIMENTAL_EMBEDDING_MODEL_MAX_INPUT_BYTES_PER_CALL, EMBEDDING_MODEL_PROVIDER_OPTIONS_TRANSFORMER as EXPERIMENTAL_EMBEDDING_MODEL_PROVIDER_OPTIONS_TRANSFORMER, type EmbeddingModelProviderOptionsTransformer, type FetchFunction, type FilePart, type FlexibleSchema, type IdGenerator, type ImagePart, type InferSchema, type InferToolInput, type InferToolOutput, type LazySchema, type MaybePromiseLike, type ModelMessage, type ParseResult, type ProviderOptions, type ProviderToolFactory, type ProviderToolFactoryWithOutputSchema, type ReasoningPart, type Resolvable, type ResponseHandler, type RetryDelayProvider, type RetryErrorFactory, type RetryErrorReason, type RetryFunction, type Schema, type ShouldRetryFunction, type StreamingToolCallDelta, StreamingToolCallTracker, type StreamingToolCallTrackerOptions, type SystemModelMessage, type TextPart, type Tool, type ToolApprovalRequest, type ToolApprovalResponse, type ToolCall, type ToolCallOptions, type ToolCallPart, type ToolContent, type ToolExecuteFunction, type ToolExecutionOptions, type ToolModelMessage, type ToolNameMapping, type ToolNeedsApprovalFunction, type ToolResult, type ToolResultOutput, type ToolResultPart, type UserContent, type UserModelMessage, VERSION, type ValidationResult, asSchema, cancelResponseBody, combineHeaders, convertAsyncIteratorToReadableStream, convertBase64ToUint8Array, convertImageModelFileToDataUri, convertToBase64, convertToFormData, convertUint8ArrayToBase64, createBinaryResponseHandler, createEventSourceResponseHandler, createIdGenerator, createJsonErrorResponseHandler, createJsonResponseHandler, createProviderToolFactory, createProviderToolFactoryWithOutputSchema, createStatusCodeErrorResponseHandler, createToolNameMapping, delay, downloadBlob, dynamicTool, encodePathSegment, executeTool, extractResponseHeaders, fetchWithValidatedEndpoint, fetchWithValidatedRedirects, generateId, getErrorMessage, getFromApi, getRuntimeEnvironmentUserAgent, injectJsonInstructionIntoMessages, isAbortError, isArrayBuffer, isBrowserRuntime, isNonNullable, isParsableJson, isSameOrigin, isUint8Array, isUrlSupported, jsonSchema, lazySchema, loadApiKey, loadOptionalSetting, loadSetting, mediaTypeToExtension, normalizeHeaders, parseJSON, parseJsonEventStream, parseProviderOptions, postFormDataToApi, postJsonToApi, postToApi, readResponseWithSizeLimit, removeUndefinedEntries, resolve, retryWithExponentialBackoff, safeParseJSON, safeValidateTypes, secureJsonParse, stringifyWithBigIntAndCycles, stripFileExtension, tool, validateDownloadUrl, validateTypes, withUserAgentSuffix, withoutTrailingSlash, zodSchema };
package/dist/index.d.ts CHANGED
@@ -1599,6 +1599,8 @@ declare function stripFileExtension(filename: string): string;
1599
1599
 
1600
1600
  declare function convertBase64ToUint8Array(base64String: string): Uint8Array<ArrayBuffer>;
1601
1601
  declare function convertUint8ArrayToBase64(array: Uint8Array): string;
1602
+ declare function isUint8Array(value: unknown): value is Uint8Array;
1603
+ declare function isArrayBuffer(value: unknown): value is ArrayBuffer;
1602
1604
  declare function convertToBase64(value: string | Uint8Array): string;
1603
1605
 
1604
1606
  /**
@@ -1744,4 +1746,11 @@ interface ToolResult<NAME extends string, INPUT, OUTPUT> {
1744
1746
  */
1745
1747
  type ToolCallOptions = ToolExecutionOptions;
1746
1748
 
1747
- export { type AssistantContent, type AssistantModelMessage, DEFAULT_MAX_DOWNLOAD_SIZE, type DataContent, DelayedPromise, DownloadError, EMBEDDING_MODEL_MAX_INPUT_BYTES_PER_CALL as EXPERIMENTAL_EMBEDDING_MODEL_MAX_INPUT_BYTES_PER_CALL, EMBEDDING_MODEL_PROVIDER_OPTIONS_TRANSFORMER as EXPERIMENTAL_EMBEDDING_MODEL_PROVIDER_OPTIONS_TRANSFORMER, type EmbeddingModelProviderOptionsTransformer, type FetchFunction, type FilePart, type FlexibleSchema, type IdGenerator, type ImagePart, type InferSchema, type InferToolInput, type InferToolOutput, type LazySchema, type MaybePromiseLike, type ModelMessage, type ParseResult, type ProviderOptions, type ProviderToolFactory, type ProviderToolFactoryWithOutputSchema, type ReasoningPart, type Resolvable, type ResponseHandler, type RetryDelayProvider, type RetryErrorFactory, type RetryErrorReason, type RetryFunction, type Schema, type ShouldRetryFunction, type StreamingToolCallDelta, StreamingToolCallTracker, type StreamingToolCallTrackerOptions, type SystemModelMessage, type TextPart, type Tool, type ToolApprovalRequest, type ToolApprovalResponse, type ToolCall, type ToolCallOptions, type ToolCallPart, type ToolContent, type ToolExecuteFunction, type ToolExecutionOptions, type ToolModelMessage, type ToolNameMapping, type ToolNeedsApprovalFunction, type ToolResult, type ToolResultOutput, type ToolResultPart, type UserContent, type UserModelMessage, VERSION, type ValidationResult, asSchema, cancelResponseBody, combineHeaders, convertAsyncIteratorToReadableStream, convertBase64ToUint8Array, convertImageModelFileToDataUri, convertToBase64, convertToFormData, convertUint8ArrayToBase64, createBinaryResponseHandler, createEventSourceResponseHandler, createIdGenerator, createJsonErrorResponseHandler, createJsonResponseHandler, createProviderToolFactory, createProviderToolFactoryWithOutputSchema, createStatusCodeErrorResponseHandler, createToolNameMapping, delay, downloadBlob, dynamicTool, encodePathSegment, executeTool, extractResponseHeaders, fetchWithValidatedEndpoint, fetchWithValidatedRedirects, generateId, getErrorMessage, getFromApi, getRuntimeEnvironmentUserAgent, injectJsonInstructionIntoMessages, isAbortError, isBrowserRuntime, isNonNullable, isParsableJson, isSameOrigin, isUrlSupported, jsonSchema, lazySchema, loadApiKey, loadOptionalSetting, loadSetting, mediaTypeToExtension, normalizeHeaders, parseJSON, parseJsonEventStream, parseProviderOptions, postFormDataToApi, postJsonToApi, postToApi, readResponseWithSizeLimit, removeUndefinedEntries, resolve, retryWithExponentialBackoff, safeParseJSON, safeValidateTypes, secureJsonParse, stripFileExtension, tool, validateDownloadUrl, validateTypes, withUserAgentSuffix, withoutTrailingSlash, zodSchema };
1749
+ /**
1750
+ * Serializes a value as JSON, representing BigInt values as decimal strings and
1751
+ * ancestor cycles as "[Circular]". Reused objects, toJSON methods, and other JSON
1752
+ * serialization behavior are preserved. Getters and toJSON methods may throw.
1753
+ */
1754
+ declare function stringifyWithBigIntAndCycles(value: unknown): string;
1755
+
1756
+ export { type AssistantContent, type AssistantModelMessage, DEFAULT_MAX_DOWNLOAD_SIZE, type DataContent, DelayedPromise, DownloadError, EMBEDDING_MODEL_MAX_INPUT_BYTES_PER_CALL as EXPERIMENTAL_EMBEDDING_MODEL_MAX_INPUT_BYTES_PER_CALL, EMBEDDING_MODEL_PROVIDER_OPTIONS_TRANSFORMER as EXPERIMENTAL_EMBEDDING_MODEL_PROVIDER_OPTIONS_TRANSFORMER, type EmbeddingModelProviderOptionsTransformer, type FetchFunction, type FilePart, type FlexibleSchema, type IdGenerator, type ImagePart, type InferSchema, type InferToolInput, type InferToolOutput, type LazySchema, type MaybePromiseLike, type ModelMessage, type ParseResult, type ProviderOptions, type ProviderToolFactory, type ProviderToolFactoryWithOutputSchema, type ReasoningPart, type Resolvable, type ResponseHandler, type RetryDelayProvider, type RetryErrorFactory, type RetryErrorReason, type RetryFunction, type Schema, type ShouldRetryFunction, type StreamingToolCallDelta, StreamingToolCallTracker, type StreamingToolCallTrackerOptions, type SystemModelMessage, type TextPart, type Tool, type ToolApprovalRequest, type ToolApprovalResponse, type ToolCall, type ToolCallOptions, type ToolCallPart, type ToolContent, type ToolExecuteFunction, type ToolExecutionOptions, type ToolModelMessage, type ToolNameMapping, type ToolNeedsApprovalFunction, type ToolResult, type ToolResultOutput, type ToolResultPart, type UserContent, type UserModelMessage, VERSION, type ValidationResult, asSchema, cancelResponseBody, combineHeaders, convertAsyncIteratorToReadableStream, convertBase64ToUint8Array, convertImageModelFileToDataUri, convertToBase64, convertToFormData, convertUint8ArrayToBase64, createBinaryResponseHandler, createEventSourceResponseHandler, createIdGenerator, createJsonErrorResponseHandler, createJsonResponseHandler, createProviderToolFactory, createProviderToolFactoryWithOutputSchema, createStatusCodeErrorResponseHandler, createToolNameMapping, delay, downloadBlob, dynamicTool, encodePathSegment, executeTool, extractResponseHeaders, fetchWithValidatedEndpoint, fetchWithValidatedRedirects, generateId, getErrorMessage, getFromApi, getRuntimeEnvironmentUserAgent, injectJsonInstructionIntoMessages, isAbortError, isArrayBuffer, isBrowserRuntime, isNonNullable, isParsableJson, isSameOrigin, isUint8Array, isUrlSupported, jsonSchema, lazySchema, loadApiKey, loadOptionalSetting, loadSetting, mediaTypeToExtension, normalizeHeaders, parseJSON, parseJsonEventStream, parseProviderOptions, postFormDataToApi, postJsonToApi, postToApi, readResponseWithSizeLimit, removeUndefinedEntries, resolve, retryWithExponentialBackoff, safeParseJSON, safeValidateTypes, secureJsonParse, stringifyWithBigIntAndCycles, stripFileExtension, tool, validateDownloadUrl, validateTypes, withUserAgentSuffix, withoutTrailingSlash, zodSchema };
package/dist/index.js CHANGED
@@ -70,10 +70,12 @@ __export(index_exports, {
70
70
  getRuntimeEnvironmentUserAgent: () => getRuntimeEnvironmentUserAgent,
71
71
  injectJsonInstructionIntoMessages: () => injectJsonInstructionIntoMessages,
72
72
  isAbortError: () => isAbortError,
73
+ isArrayBuffer: () => isArrayBuffer,
73
74
  isBrowserRuntime: () => isBrowserRuntime,
74
75
  isNonNullable: () => isNonNullable,
75
76
  isParsableJson: () => isParsableJson,
76
77
  isSameOrigin: () => isSameOrigin,
78
+ isUint8Array: () => isUint8Array,
77
79
  isUrlSupported: () => isUrlSupported,
78
80
  jsonSchema: () => jsonSchema,
79
81
  lazySchema: () => lazySchema,
@@ -95,6 +97,7 @@ __export(index_exports, {
95
97
  safeParseJSON: () => safeParseJSON,
96
98
  safeValidateTypes: () => safeValidateTypes,
97
99
  secureJsonParse: () => secureJsonParse,
100
+ stringifyWithBigIntAndCycles: () => stringifyWithBigIntAndCycles,
98
101
  stripFileExtension: () => stripFileExtension,
99
102
  tool: () => tool,
100
103
  validateDownloadUrl: () => validateDownloadUrl,
@@ -272,9 +275,21 @@ var { btoa, atob } = globalThis;
272
275
  function convertBase64ToUint8Array(base64String) {
273
276
  const base64Url = base64String.replace(/-/g, "+").replace(/_/g, "/");
274
277
  const latin1string = atob(base64Url);
275
- return Uint8Array.from(latin1string, (byte) => byte.codePointAt(0));
278
+ const bytes = new Uint8Array(latin1string.length);
279
+ for (let i = 0; i < latin1string.length; i++) {
280
+ bytes[i] = latin1string.charCodeAt(i);
281
+ }
282
+ return bytes;
276
283
  }
277
284
  function convertUint8ArrayToBase64(array) {
285
+ var _a2;
286
+ if (typeof ((_a2 = globalThis.Buffer) == null ? void 0 : _a2.from) === "function") {
287
+ return globalThis.Buffer.from(
288
+ array.buffer,
289
+ array.byteOffset,
290
+ array.byteLength
291
+ ).toString("base64");
292
+ }
278
293
  const chunks = [];
279
294
  const chunkSize = 4096;
280
295
  for (let i = 0; i < array.length; i += chunkSize) {
@@ -282,8 +297,30 @@ function convertUint8ArrayToBase64(array) {
282
297
  }
283
298
  return btoa(chunks.join(""));
284
299
  }
300
+ var typedArrayTag = Object.getOwnPropertyDescriptor(
301
+ Object.getPrototypeOf(Uint8Array.prototype),
302
+ Symbol.toStringTag
303
+ ).get;
304
+ var arrayBufferByteLength = Object.getOwnPropertyDescriptor(
305
+ ArrayBuffer.prototype,
306
+ "byteLength"
307
+ ).get;
308
+ function isUint8Array(value) {
309
+ return ArrayBuffer.isView(value) && typedArrayTag.call(value) === "Uint8Array";
310
+ }
311
+ function isArrayBuffer(value) {
312
+ if (typeof value !== "object" || value === null || !("byteLength" in value)) {
313
+ return false;
314
+ }
315
+ try {
316
+ arrayBufferByteLength.call(value);
317
+ return true;
318
+ } catch (e) {
319
+ return false;
320
+ }
321
+ }
285
322
  function convertToBase64(value) {
286
- return value instanceof Uint8Array ? convertUint8ArrayToBase64(value) : value;
323
+ return isUint8Array(value) ? convertUint8ArrayToBase64(value) : value;
287
324
  }
288
325
 
289
326
  // src/convert-image-model-file-to-data-uri.ts
@@ -1006,7 +1043,7 @@ function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
1006
1043
  }
1007
1044
 
1008
1045
  // src/version.ts
1009
- var VERSION = true ? "4.0.62" : "0.0.0-test";
1046
+ var VERSION = true ? "4.0.64" : "0.0.0-test";
1010
1047
 
1011
1048
  // src/get-from-api.ts
1012
1049
  var getOriginalFetch = () => globalThis.fetch;
@@ -3651,6 +3688,27 @@ async function* executeTool({
3651
3688
 
3652
3689
  // src/index.ts
3653
3690
  var import_stream2 = require("eventsource-parser/stream");
3691
+
3692
+ // src/stringify-with-bigint-and-cycles.ts
3693
+ function stringifyWithBigIntAndCycles(value) {
3694
+ const ancestors = [];
3695
+ return JSON.stringify(value, function(_key, value2) {
3696
+ if (typeof value2 === "bigint") {
3697
+ return value2.toString();
3698
+ }
3699
+ if (typeof value2 !== "object" || value2 === null) {
3700
+ return value2;
3701
+ }
3702
+ while (ancestors.length > 0 && ancestors.at(-1) !== this) {
3703
+ ancestors.pop();
3704
+ }
3705
+ if (ancestors.includes(value2)) {
3706
+ return "[Circular]";
3707
+ }
3708
+ ancestors.push(value2);
3709
+ return value2;
3710
+ });
3711
+ }
3654
3712
  // Annotate the CommonJS export names for ESM import in node:
3655
3713
  0 && (module.exports = {
3656
3714
  DEFAULT_MAX_DOWNLOAD_SIZE,
@@ -3693,10 +3751,12 @@ var import_stream2 = require("eventsource-parser/stream");
3693
3751
  getRuntimeEnvironmentUserAgent,
3694
3752
  injectJsonInstructionIntoMessages,
3695
3753
  isAbortError,
3754
+ isArrayBuffer,
3696
3755
  isBrowserRuntime,
3697
3756
  isNonNullable,
3698
3757
  isParsableJson,
3699
3758
  isSameOrigin,
3759
+ isUint8Array,
3700
3760
  isUrlSupported,
3701
3761
  jsonSchema,
3702
3762
  lazySchema,
@@ -3718,6 +3778,7 @@ var import_stream2 = require("eventsource-parser/stream");
3718
3778
  safeParseJSON,
3719
3779
  safeValidateTypes,
3720
3780
  secureJsonParse,
3781
+ stringifyWithBigIntAndCycles,
3721
3782
  stripFileExtension,
3722
3783
  tool,
3723
3784
  validateDownloadUrl,