ai 6.0.321 → 6.0.323

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.
@@ -1,5 +1,7 @@
1
1
  import { AISDKError, type LanguageModelV3DataContent } from '@ai-sdk/provider';
2
2
  import {
3
+ isUint8Array,
4
+ isArrayBuffer,
3
5
  convertBase64ToUint8Array,
4
6
  convertUint8ArrayToBase64,
5
7
  type DataContent,
@@ -13,8 +15,8 @@ import { splitDataUrl } from './split-data-url';
13
15
  */
14
16
  export const dataContentSchema: z.ZodType<DataContent> = z.union([
15
17
  z.string(),
16
- z.instanceof(Uint8Array),
17
- z.instanceof(ArrayBuffer),
18
+ z.custom<Uint8Array>(isUint8Array, { message: 'Must be a Uint8Array' }),
19
+ z.custom<ArrayBuffer>(isArrayBuffer, { message: 'Must be an ArrayBuffer' }),
18
20
  z.custom<Buffer>(
19
21
  // Buffer might not be available in some environments such as CloudFlare:
20
22
  (value: unknown): value is Buffer =>
@@ -33,12 +35,15 @@ export function convertToLanguageModelV3DataContent(
33
35
  let originalUrl: string | undefined;
34
36
 
35
37
  // Buffer & Uint8Array:
36
- if (content instanceof Uint8Array) {
37
- return { data: content, mediaType: undefined };
38
+ if (isUint8Array(content)) {
39
+ return {
40
+ data: convertDataContentToUint8Array(content),
41
+ mediaType: undefined,
42
+ };
38
43
  }
39
44
 
40
45
  // ArrayBuffer needs conversion to Uint8Array (lightweight):
41
- if (content instanceof ArrayBuffer) {
46
+ if (isArrayBuffer(content)) {
42
47
  return { data: new Uint8Array(content), mediaType: undefined };
43
48
  }
44
49
 
@@ -88,11 +93,7 @@ export function convertDataContentToBase64String(content: DataContent): string {
88
93
  return content;
89
94
  }
90
95
 
91
- if (content instanceof ArrayBuffer) {
92
- return convertUint8ArrayToBase64(new Uint8Array(content));
93
- }
94
-
95
- return convertUint8ArrayToBase64(content);
96
+ return convertUint8ArrayToBase64(convertDataContentToUint8Array(content));
96
97
  }
97
98
 
98
99
  /**
@@ -104,8 +105,12 @@ export function convertDataContentToBase64String(content: DataContent): string {
104
105
  export function convertDataContentToUint8Array(
105
106
  content: DataContent,
106
107
  ): Uint8Array {
107
- if (content instanceof Uint8Array) {
108
- return content;
108
+ if (isUint8Array(content)) {
109
+ if (content instanceof Uint8Array) {
110
+ return content;
111
+ }
112
+ const { buffer, byteOffset, byteLength } = content;
113
+ return new Uint8Array(buffer, byteOffset, byteLength);
109
114
  }
110
115
 
111
116
  if (typeof content === 'string') {
@@ -121,7 +126,7 @@ export function convertDataContentToUint8Array(
121
126
  }
122
127
  }
123
128
 
124
- if (content instanceof ArrayBuffer) {
129
+ if (isArrayBuffer(content)) {
125
130
  return new Uint8Array(content);
126
131
  }
127
132