@ai-sdk/provider-utils 5.0.72 → 5.0.74
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 +14 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +41 -6
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/convert-inline-file-data-to-uint8-array.ts +13 -2
- package/src/index.ts +2 -0
- package/src/is-provider-reference.ts +3 -2
- package/src/stringify-with-bigint-and-cycles.ts +26 -0
- package/src/uint8-utils.ts +39 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-sdk/provider-utils",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.74",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
"@standard-schema/spec": "^1.1.0",
|
|
47
47
|
"@workflow/serde": "4.1.0",
|
|
48
48
|
"eventsource-parser": "^3.0.8",
|
|
49
|
-
"undici": "^7.29.
|
|
49
|
+
"undici": "^7.29.1"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
52
|
"@types/node": "22.19.19",
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import { UnsupportedFunctionalityError } from '@ai-sdk/provider';
|
|
2
2
|
import type { FilePart } from './types/content-part';
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
convertBase64ToUint8Array,
|
|
5
|
+
isArrayBuffer,
|
|
6
|
+
isUint8Array,
|
|
7
|
+
} from './uint8-utils';
|
|
4
8
|
|
|
5
9
|
type InlineFileData = Extract<
|
|
6
10
|
FilePart['data'],
|
|
@@ -35,7 +39,14 @@ export function convertInlineFileDataToUint8Array(
|
|
|
35
39
|
if (data.data instanceof Uint8Array) {
|
|
36
40
|
return data.data;
|
|
37
41
|
}
|
|
38
|
-
if (data.data
|
|
42
|
+
if (isUint8Array(data.data)) {
|
|
43
|
+
return new Uint8Array(
|
|
44
|
+
data.data.buffer,
|
|
45
|
+
data.data.byteOffset,
|
|
46
|
+
data.data.byteLength,
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
if (isArrayBuffer(data.data)) {
|
|
39
50
|
return new Uint8Array(data.data);
|
|
40
51
|
}
|
|
41
52
|
return convertBase64ToUint8Array(data.data);
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { SharedV4ProviderReference } from '@ai-sdk/provider';
|
|
2
2
|
import { isBuffer } from './is-buffer';
|
|
3
|
+
import { isArrayBuffer, isUint8Array } from './uint8-utils';
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* Checks whether a value is a provider reference (a mapping of provider names
|
|
@@ -12,9 +13,9 @@ export function isProviderReference(
|
|
|
12
13
|
return (
|
|
13
14
|
typeof data === 'object' &&
|
|
14
15
|
data !== null &&
|
|
15
|
-
!(data
|
|
16
|
+
!isUint8Array(data) &&
|
|
16
17
|
!(data instanceof URL) &&
|
|
17
|
-
!(data
|
|
18
|
+
!isArrayBuffer(data) &&
|
|
18
19
|
!isBuffer(data) &&
|
|
19
20
|
!('type' in data)
|
|
20
21
|
);
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Serializes a value as JSON, representing BigInt values as decimal strings and
|
|
3
|
+
* ancestor cycles as "[Circular]". Reused objects, toJSON methods, and other JSON
|
|
4
|
+
* serialization behavior are preserved. Getters and toJSON methods may throw.
|
|
5
|
+
*/
|
|
6
|
+
export function stringifyWithBigIntAndCycles(value: unknown): string {
|
|
7
|
+
const ancestors: object[] = [];
|
|
8
|
+
return JSON.stringify(value, function (_key, value) {
|
|
9
|
+
if (typeof value === 'bigint') {
|
|
10
|
+
return value.toString();
|
|
11
|
+
}
|
|
12
|
+
if (typeof value !== 'object' || value === null) {
|
|
13
|
+
return value;
|
|
14
|
+
}
|
|
15
|
+
// JSON.stringify visits siblings after their previous subtree. Only
|
|
16
|
+
// objects on the current ancestor path are circular references.
|
|
17
|
+
while (ancestors.length > 0 && ancestors.at(-1) !== this) {
|
|
18
|
+
ancestors.pop();
|
|
19
|
+
}
|
|
20
|
+
if (ancestors.includes(value)) {
|
|
21
|
+
return '[Circular]';
|
|
22
|
+
}
|
|
23
|
+
ancestors.push(value);
|
|
24
|
+
return value;
|
|
25
|
+
});
|
|
26
|
+
}
|
package/src/uint8-utils.ts
CHANGED
|
@@ -6,7 +6,15 @@ const { btoa, atob } = globalThis;
|
|
|
6
6
|
export function convertBase64ToUint8Array(base64String: string) {
|
|
7
7
|
const base64Url = base64String.replace(/-/g, '+').replace(/_/g, '/');
|
|
8
8
|
const latin1string = atob(base64Url);
|
|
9
|
-
|
|
9
|
+
|
|
10
|
+
// Preallocate and fill: `Uint8Array.from(string, mapFn)` iterates per
|
|
11
|
+
// character and collects the mapped values before allocating the result,
|
|
12
|
+
// which peaks at ~25x the decoded size and blocks for ~1s on 20 MB inputs.
|
|
13
|
+
const bytes = new Uint8Array(latin1string.length);
|
|
14
|
+
for (let i = 0; i < latin1string.length; i++) {
|
|
15
|
+
bytes[i] = latin1string.charCodeAt(i);
|
|
16
|
+
}
|
|
17
|
+
return bytes;
|
|
10
18
|
}
|
|
11
19
|
|
|
12
20
|
export function convertUint8ArrayToBase64(array: Uint8Array): string {
|
|
@@ -30,6 +38,35 @@ export function convertUint8ArrayToBase64(array: Uint8Array): string {
|
|
|
30
38
|
return btoa(chunks.join(''));
|
|
31
39
|
}
|
|
32
40
|
|
|
41
|
+
// Intrinsic getters check the built-in slots across realms without relying on
|
|
42
|
+
// constructor names or user-defined Symbol.toStringTag properties.
|
|
43
|
+
const typedArrayTag = Object.getOwnPropertyDescriptor(
|
|
44
|
+
Object.getPrototypeOf(Uint8Array.prototype),
|
|
45
|
+
Symbol.toStringTag,
|
|
46
|
+
)!.get!;
|
|
47
|
+
const arrayBufferByteLength = Object.getOwnPropertyDescriptor(
|
|
48
|
+
ArrayBuffer.prototype,
|
|
49
|
+
'byteLength',
|
|
50
|
+
)!.get!;
|
|
51
|
+
|
|
52
|
+
export function isUint8Array(value: unknown): value is Uint8Array {
|
|
53
|
+
return (
|
|
54
|
+
ArrayBuffer.isView(value) && typedArrayTag.call(value) === 'Uint8Array'
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function isArrayBuffer(value: unknown): value is ArrayBuffer {
|
|
59
|
+
if (typeof value !== 'object' || value === null || !('byteLength' in value)) {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
try {
|
|
63
|
+
arrayBufferByteLength.call(value);
|
|
64
|
+
return true;
|
|
65
|
+
} catch {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
33
70
|
export function convertToBase64(value: string | Uint8Array): string {
|
|
34
|
-
return value
|
|
71
|
+
return isUint8Array(value) ? convertUint8ArrayToBase64(value) : value;
|
|
35
72
|
}
|