@ai-sdk/provider-utils 5.0.71 → 5.0.73

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/provider-utils",
3
- "version": "5.0.71",
3
+ "version": "5.0.73",
4
4
  "type": "module",
5
5
  "license": "Apache-2.0",
6
6
  "sideEffects": false,
@@ -42,7 +42,7 @@
42
42
  }
43
43
  },
44
44
  "dependencies": {
45
- "@ai-sdk/provider": "4.0.34",
45
+ "@ai-sdk/provider": "4.0.35",
46
46
  "@standard-schema/spec": "^1.1.0",
47
47
  "@workflow/serde": "4.1.0",
48
48
  "eventsource-parser": "^3.0.8",
@@ -1,6 +1,10 @@
1
1
  import { UnsupportedFunctionalityError } from '@ai-sdk/provider';
2
2
  import type { FilePart } from './types/content-part';
3
- import { convertBase64ToUint8Array } from './uint8-utils';
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 instanceof ArrayBuffer) {
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
@@ -165,3 +165,5 @@ export {
165
165
  EventSourceParserStream,
166
166
  type EventSourceMessage,
167
167
  } from 'eventsource-parser/stream';
168
+
169
+ export { stringifyWithBigIntAndCycles } from './stringify-with-bigint-and-cycles';
@@ -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 instanceof Uint8Array) &&
16
+ !isUint8Array(data) &&
16
17
  !(data instanceof URL) &&
17
- !(data instanceof ArrayBuffer) &&
18
+ !isArrayBuffer(data) &&
18
19
  !isBuffer(data) &&
19
20
  !('type' in data)
20
21
  );
package/src/resolve.ts CHANGED
@@ -30,3 +30,28 @@ export async function resolve<T>(value: Resolvable<T>): Promise<T> {
30
30
  // Otherwise just resolve whatever we got (value or promise)
31
31
  return value as T;
32
32
  }
33
+
34
+ /**
35
+ * Resolves and transforms a value, preserving synchronous results. This allows
36
+ * synchronous credentials to remain usable by workflow serialization while
37
+ * asynchronous credentials are awaited by request handlers.
38
+ */
39
+ export function resolveWith<T, U>(
40
+ value: Resolvable<T>,
41
+ transform: (value: T) => U,
42
+ ): U | Promise<Awaited<U>> {
43
+ const resolved =
44
+ typeof value === 'function'
45
+ ? (value as () => MaybePromiseLike<T>)()
46
+ : value;
47
+
48
+ if (
49
+ resolved != null &&
50
+ (typeof resolved === 'object' || typeof resolved === 'function') &&
51
+ typeof (resolved as PromiseLike<T>).then === 'function'
52
+ ) {
53
+ return Promise.resolve(resolved).then(transform) as Promise<Awaited<U>>;
54
+ }
55
+
56
+ return transform(resolved as T);
57
+ }
@@ -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
+ }
@@ -45,6 +45,11 @@ export type {
45
45
  SandboxWriteBinaryFileOptions as Experimental_SandboxWriteBinaryFileOptions,
46
46
  SandboxWriteTextFileOptions as Experimental_SandboxWriteTextFileOptions,
47
47
  WritableSandboxProcess as Experimental_WritableSandboxProcess,
48
+ SandboxStatOptions as Experimental_SandboxStatOptions,
49
+ SandboxListDirectoryOptions as Experimental_SandboxListDirectoryOptions,
50
+ SandboxDeleteFileOptions as Experimental_SandboxDeleteFileOptions,
51
+ SandboxFileStat as Experimental_SandboxFileStat,
52
+ SandboxDirectoryEntry as Experimental_SandboxDirectoryEntry,
48
53
  } from './sandbox';
49
54
  export type { SystemModelMessage } from './system-model-message';
50
55
  export {
@@ -42,23 +42,25 @@ export type SandboxSpawnOptionsWithoutStdin = SandboxProcessOptions & {
42
42
  };
43
43
 
44
44
  /**
45
- * Options for reading a file from the sandbox.
45
+ * Options for reading, inspecting, or deleting one path in the sandbox.
46
46
  */
47
- export type SandboxReadFileOptions = {
47
+ type PathOptions = {
48
48
  /**
49
- * Path of the file to read.
49
+ * Path to operate on.
50
50
  */
51
51
  path: string;
52
52
 
53
53
  /**
54
- * Signal that can be used to abort the read.
54
+ * Signal that can be used to abort the operation.
55
55
  */
56
56
  abortSignal?: AbortSignal;
57
57
  };
58
58
 
59
- export type SandboxReadBinaryFileOptions = SandboxReadFileOptions;
59
+ export type SandboxReadFileOptions = PathOptions;
60
60
 
61
- export type SandboxReadTextFileOptions = SandboxReadFileOptions & {
61
+ export type SandboxReadBinaryFileOptions = PathOptions;
62
+
63
+ export type SandboxReadTextFileOptions = PathOptions & {
62
64
  /**
63
65
  * Text encoding used to decode the file bytes. Defaults to `"utf-8"`.
64
66
  */
@@ -76,6 +78,44 @@ export type SandboxReadTextFileOptions = SandboxReadFileOptions & {
76
78
  endLine?: number;
77
79
  };
78
80
 
81
+ export type SandboxStatOptions = PathOptions;
82
+
83
+ export type SandboxListDirectoryOptions = PathOptions;
84
+
85
+ export type SandboxDeleteFileOptions = PathOptions;
86
+
87
+ /**
88
+ * Information about a file or directory in the sandbox, returned by
89
+ * `SandboxSession.stat`. Symbolic links are followed.
90
+ */
91
+ export type SandboxFileStat = {
92
+ /**
93
+ * Type of the path. `other` covers devices, sockets, and named pipes.
94
+ */
95
+ type: 'file' | 'directory' | 'other';
96
+
97
+ /**
98
+ * Size in bytes. The size of a directory is implementation-defined.
99
+ */
100
+ size: number;
101
+ };
102
+
103
+ /**
104
+ * Entry in a directory listing, returned by `SandboxSession.listDirectory`.
105
+ */
106
+ export type SandboxDirectoryEntry = {
107
+ /**
108
+ * Name of the entry, without the directory path.
109
+ */
110
+ name: string;
111
+
112
+ /**
113
+ * Type of the entry. Symbolic links are reported as `symlink` and not
114
+ * followed. `other` covers devices, sockets, and named pipes.
115
+ */
116
+ type: 'file' | 'directory' | 'symlink' | 'other';
117
+ };
118
+
79
119
  /**
80
120
  * Options for writing a file to the sandbox. `CONTENT` is the payload written
81
121
  * to the file: a byte stream, raw bytes, or a string.
@@ -112,6 +152,13 @@ export type SandboxWriteTextFileOptions = WriteFileOptions<string> & {
112
152
 
113
153
  /**
114
154
  * Sandbox session that can execute commands and read/write files.
155
+ *
156
+ * Relative paths in every method, including the `workingDirectory` option of
157
+ * `run` and `spawn`, resolve against the same base directory: the directory
158
+ * that `run` and `spawn` start in when no `workingDirectory` is given. The
159
+ * base directory does not change for the lifetime of the session; changing
160
+ * the directory inside a command, e.g. with `cd`, only affects that command.
161
+ * Absolute paths are used as is.
115
162
  */
116
163
  export type SandboxSession = {
117
164
  /**
@@ -125,9 +172,8 @@ export type SandboxSession = {
125
172
  * Read one file from the sandbox as a stream of bytes. Resolves to `null`
126
173
  * when the file does not exist.
127
174
  *
128
- * Relative path handling is implementation-defined. This is the lowest-level
129
- * read primitive; prefer `readBinaryFile` or `readTextFile` unless you need
130
- * to stream bytes.
175
+ * This is the lowest-level read primitive; prefer `readBinaryFile` or
176
+ * `readTextFile` unless you need to stream bytes.
131
177
  */
132
178
  readonly readFile: (
133
179
  options: SandboxReadFileOptions,
@@ -154,7 +200,8 @@ export type SandboxSession = {
154
200
 
155
201
  /**
156
202
  * Write one file to the sandbox from a stream of bytes. Creates parent
157
- * directories recursively and overwrites any existing file.
203
+ * directories recursively and overwrites any existing file. Rejects when the
204
+ * path is a directory.
158
205
  *
159
206
  * This is the lowest-level write primitive; prefer `writeBinaryFile` or
160
207
  * `writeTextFile` when the full content is already materialized in memory.
@@ -163,7 +210,8 @@ export type SandboxSession = {
163
210
 
164
211
  /**
165
212
  * Write one file to the sandbox from raw bytes. Creates parent directories
166
- * recursively and overwrites any existing file.
213
+ * recursively and overwrites any existing file. Rejects when the path is a
214
+ * directory.
167
215
  */
168
216
  readonly writeBinaryFile: (
169
217
  options: SandboxWriteBinaryFileOptions,
@@ -172,12 +220,39 @@ export type SandboxSession = {
172
220
  /**
173
221
  * Write one file to the sandbox from a string, encoded using the requested
174
222
  * encoding. Creates parent directories recursively and overwrites any
175
- * existing file.
223
+ * existing file. Rejects when the path is a directory.
176
224
  */
177
225
  readonly writeTextFile: (
178
226
  options: SandboxWriteTextFileOptions,
179
227
  ) => PromiseLike<void>;
180
228
 
229
+ /**
230
+ * Get information about a file or directory. Symbolic links are followed.
231
+ * Resolves to `null` when the path does not exist.
232
+ */
233
+ readonly stat: (
234
+ options: SandboxStatOptions,
235
+ ) => PromiseLike<SandboxFileStat | null>;
236
+
237
+ /**
238
+ * List the entries of one directory, without descending into
239
+ * subdirectories. The order of entries is unspecified. Resolves to `null`
240
+ * when the directory does not exist and rejects when the path is not a
241
+ * directory.
242
+ */
243
+ readonly listDirectory: (
244
+ options: SandboxListDirectoryOptions,
245
+ ) => PromiseLike<Array<SandboxDirectoryEntry> | null>;
246
+
247
+ /**
248
+ * Delete one file. A symbolic link is deleted itself, not its target.
249
+ * Resolves to `true` when the file was deleted and `false` when it does not
250
+ * exist. Rejects when the path is a directory.
251
+ */
252
+ readonly deleteFile: (
253
+ options: SandboxDeleteFileOptions,
254
+ ) => PromiseLike<boolean>;
255
+
181
256
  /**
182
257
  * Spawn a long-running process in the sandbox. Returns immediately with a
183
258
  * handle that streams stdout/stderr, can be waited on, and can be killed.
@@ -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
- return Uint8Array.from(latin1string, byte => byte.codePointAt(0)!);
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 instanceof Uint8Array ? convertUint8ArrayToBase64(value) : value;
71
+ return isUint8Array(value) ? convertUint8ArrayToBase64(value) : value;
35
72
  }