Skip to content

Commit 57086ff

Browse files
authored
chore(provider-utils): export types for Experimental_SandboxSession's method inputs (#24097)
## Background Follow-up to #24092: Sandbox implementations still redeclare method input shapes, even though `spawn` already has exported options types. ## Summary - Export experimental options types for `run` and all six file methods. - Reuse these types in the package adapters and example implementations. - Add type coverage for the exports and unchanged input shapes. No runtime behavior changes. ## Checklist - [x] All commits are signed (PRs with unsigned commits cannot be merged) - [x] Tests have been added / updated (for bug fixes / features) - [ ] Documentation has been added / updated (for bug fixes / features) - [x] A _patch_ changeset for relevant packages has been added (for bug fixes / features - run `pnpm changeset` in the project root) - [x] I have reviewed this pull request (self-review)
1 parent cb6eaad commit 57086ff

10 files changed

Lines changed: 243 additions & 274 deletions

File tree

‎.changeset/beige-peaches-fail.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@ai-sdk/sandbox-just-bash": patch
3+
"@ai-sdk/provider-utils": patch
4+
"@ai-sdk/sandbox-vercel": patch
5+
---
6+
7+
chore(provider-utils): export types for `Experimental_SandboxSession`'s method inputs

‎examples/ai-e2e-next/sandbox/vercel-sandbox.ts‎

Lines changed: 14 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ import { isAbsolute, join } from 'node:path';
22
import { Readable } from 'node:stream';
33
import {
44
extractLines,
5+
type Experimental_SandboxRunOptions as RunOptions,
6+
type Experimental_SandboxReadFileOptions as ReadFileOptions,
7+
type Experimental_SandboxReadBinaryFileOptions as ReadBinaryFileOptions,
8+
type Experimental_SandboxReadTextFileOptions as ReadTextFileOptions,
9+
type Experimental_SandboxWriteFileOptions as WriteFileOptions,
10+
type Experimental_SandboxWriteBinaryFileOptions as WriteBinaryFileOptions,
11+
type Experimental_SandboxWriteTextFileOptions as WriteTextFileOptions,
512
type Experimental_SandboxProcess,
613
type Experimental_SandboxSpawnOptions as SpawnOptions,
714
type Experimental_SandboxSpawnOptionsWithStdin as SpawnOptionsWithStdin,
@@ -37,17 +44,7 @@ export class VercelSandboxSession implements SandboxSession {
3744
return isAbsolute(path) ? path : join(rootDirectory, path);
3845
}
3946

40-
async run({
41-
command,
42-
workingDirectory,
43-
env,
44-
abortSignal,
45-
}: {
46-
command: string;
47-
workingDirectory?: string;
48-
env?: Record<string, string>;
49-
abortSignal?: AbortSignal;
50-
}) {
47+
async run({ command, workingDirectory, env, abortSignal }: RunOptions) {
5148
const proc = await this.spawn({
5249
command,
5350
workingDirectory,
@@ -95,10 +92,7 @@ export class VercelSandboxSession implements SandboxSession {
9592
async readFile({
9693
path,
9794
abortSignal,
98-
}: {
99-
path: string;
100-
abortSignal?: AbortSignal;
101-
}): Promise<ReadableStream<Uint8Array> | null> {
95+
}: ReadFileOptions): Promise<ReadableStream<Uint8Array> | null> {
10296
abortSignal?.throwIfAborted();
10397
const result = await this.sandbox.readFile({
10498
path: this.resolvePath(path),
@@ -113,11 +107,7 @@ export class VercelSandboxSession implements SandboxSession {
113107
path,
114108
content,
115109
abortSignal,
116-
}: {
117-
path: string;
118-
content: ReadableStream<Uint8Array>;
119-
abortSignal?: AbortSignal;
120-
}): Promise<void> {
110+
}: WriteFileOptions): Promise<void> {
121111
abortSignal?.throwIfAborted();
122112
const bytes = await collectStream(content);
123113
abortSignal?.throwIfAborted();
@@ -129,10 +119,7 @@ export class VercelSandboxSession implements SandboxSession {
129119
async readBinaryFile({
130120
path,
131121
abortSignal,
132-
}: {
133-
path: string;
134-
abortSignal?: AbortSignal;
135-
}): Promise<Uint8Array | null> {
122+
}: ReadBinaryFileOptions): Promise<Uint8Array | null> {
136123
const stream = await this.readFile({ path, abortSignal });
137124
if (stream == null) return null;
138125
return collectStream(stream);
@@ -142,11 +129,7 @@ export class VercelSandboxSession implements SandboxSession {
142129
path,
143130
content,
144131
abortSignal,
145-
}: {
146-
path: string;
147-
content: Uint8Array;
148-
abortSignal?: AbortSignal;
149-
}): Promise<void> {
132+
}: WriteBinaryFileOptions): Promise<void> {
150133
await this.writeFile({
151134
path,
152135
content: bytesToStream(content),
@@ -160,13 +143,7 @@ export class VercelSandboxSession implements SandboxSession {
160143
startLine,
161144
endLine,
162145
abortSignal,
163-
}: {
164-
path: string;
165-
encoding?: string;
166-
startLine?: number;
167-
endLine?: number;
168-
abortSignal?: AbortSignal;
169-
}): Promise<string | null> {
146+
}: ReadTextFileOptions): Promise<string | null> {
170147
const bytes = await this.readBinaryFile({ path, abortSignal });
171148
if (bytes == null) return null;
172149
const text = Buffer.from(bytes).toString(encoding as BufferEncoding);
@@ -178,12 +155,7 @@ export class VercelSandboxSession implements SandboxSession {
178155
content,
179156
encoding = 'utf-8',
180157
abortSignal,
181-
}: {
182-
path: string;
183-
content: string;
184-
encoding?: string;
185-
abortSignal?: AbortSignal;
186-
}): Promise<void> {
158+
}: WriteTextFileOptions): Promise<void> {
187159
const bytes = Buffer.from(content, encoding as BufferEncoding);
188160
await this.writeBinaryFile({
189161
path,

‎examples/ai-functions/src/sandbox/just-bash-sandbox.ts‎

Lines changed: 14 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import { isAbsolute, join } from 'node:path';
22
import {
33
extractLines,
4+
type Experimental_SandboxRunOptions as RunOptions,
5+
type Experimental_SandboxReadFileOptions as ReadFileOptions,
6+
type Experimental_SandboxReadBinaryFileOptions as ReadBinaryFileOptions,
7+
type Experimental_SandboxReadTextFileOptions as ReadTextFileOptions,
8+
type Experimental_SandboxWriteFileOptions as WriteFileOptions,
9+
type Experimental_SandboxWriteBinaryFileOptions as WriteBinaryFileOptions,
10+
type Experimental_SandboxWriteTextFileOptions as WriteTextFileOptions,
411
type Experimental_SandboxProcess,
512
type Experimental_SandboxSpawnOptions as SpawnOptions,
613
type Experimental_SandboxSpawnOptionsWithStdin as SpawnOptionsWithStdin,
@@ -26,17 +33,7 @@ export class JustBashSandboxSession implements SandboxSession {
2633
: join(this.sandbox.bashEnvInstance.getCwd(), path);
2734
}
2835

29-
async run({
30-
command,
31-
workingDirectory,
32-
env,
33-
abortSignal,
34-
}: {
35-
command: string;
36-
workingDirectory?: string;
37-
env?: Record<string, string>;
38-
abortSignal?: AbortSignal;
39-
}) {
36+
async run({ command, workingDirectory, env, abortSignal }: RunOptions) {
4037
const proc = await this.spawn({
4138
command,
4239
workingDirectory,
@@ -85,10 +82,7 @@ export class JustBashSandboxSession implements SandboxSession {
8582
async readFile({
8683
path,
8784
abortSignal,
88-
}: {
89-
path: string;
90-
abortSignal?: AbortSignal;
91-
}): Promise<ReadableStream<Uint8Array> | null> {
85+
}: ReadFileOptions): Promise<ReadableStream<Uint8Array> | null> {
9286
abortSignal?.throwIfAborted();
9387
const resolved = this.resolvePath(path);
9488
try {
@@ -110,11 +104,7 @@ export class JustBashSandboxSession implements SandboxSession {
110104
path,
111105
content,
112106
abortSignal,
113-
}: {
114-
path: string;
115-
content: ReadableStream<Uint8Array>;
116-
abortSignal?: AbortSignal;
117-
}): Promise<void> {
107+
}: WriteFileOptions): Promise<void> {
118108
abortSignal?.throwIfAborted();
119109
const bytes = await collectStream(content);
120110
abortSignal?.throwIfAborted();
@@ -127,10 +117,7 @@ export class JustBashSandboxSession implements SandboxSession {
127117
async readBinaryFile({
128118
path,
129119
abortSignal,
130-
}: {
131-
path: string;
132-
abortSignal?: AbortSignal;
133-
}): Promise<Uint8Array | null> {
120+
}: ReadBinaryFileOptions): Promise<Uint8Array | null> {
134121
const stream = await this.readFile({ path, abortSignal });
135122
if (stream == null) return null;
136123
return collectStream(stream);
@@ -140,11 +127,7 @@ export class JustBashSandboxSession implements SandboxSession {
140127
path,
141128
content,
142129
abortSignal,
143-
}: {
144-
path: string;
145-
content: Uint8Array;
146-
abortSignal?: AbortSignal;
147-
}): Promise<void> {
130+
}: WriteBinaryFileOptions): Promise<void> {
148131
await this.writeFile({
149132
path,
150133
content: bytesToStream(content),
@@ -158,13 +141,7 @@ export class JustBashSandboxSession implements SandboxSession {
158141
startLine,
159142
endLine,
160143
abortSignal,
161-
}: {
162-
path: string;
163-
encoding?: string;
164-
startLine?: number;
165-
endLine?: number;
166-
abortSignal?: AbortSignal;
167-
}): Promise<string | null> {
144+
}: ReadTextFileOptions): Promise<string | null> {
168145
const bytes = await this.readBinaryFile({ path, abortSignal });
169146
if (bytes == null) return null;
170147
const text = Buffer.from(bytes).toString(encoding as BufferEncoding);
@@ -176,12 +153,7 @@ export class JustBashSandboxSession implements SandboxSession {
176153
content,
177154
encoding = 'utf-8',
178155
abortSignal,
179-
}: {
180-
path: string;
181-
content: string;
182-
encoding?: string;
183-
abortSignal?: AbortSignal;
184-
}): Promise<void> {
156+
}: WriteTextFileOptions): Promise<void> {
185157
const bytes = Buffer.from(content, encoding as BufferEncoding);
186158
await this.writeBinaryFile({
187159
path,

‎examples/ai-functions/src/sandbox/local-sandbox.ts‎

Lines changed: 14 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ import { Readable, type Writable } from 'node:stream';
66
import { pipeline } from 'node:stream/promises';
77
import {
88
extractLines,
9+
type Experimental_SandboxRunOptions as RunOptions,
10+
type Experimental_SandboxReadFileOptions as ReadFileOptions,
11+
type Experimental_SandboxReadBinaryFileOptions as ReadBinaryFileOptions,
12+
type Experimental_SandboxReadTextFileOptions as ReadTextFileOptions,
13+
type Experimental_SandboxWriteFileOptions as WriteFileOptions,
14+
type Experimental_SandboxWriteBinaryFileOptions as WriteBinaryFileOptions,
15+
type Experimental_SandboxWriteTextFileOptions as WriteTextFileOptions,
916
type Experimental_SandboxProcess,
1017
type Experimental_SandboxSpawnOptions as SpawnOptions,
1118
type Experimental_SandboxSpawnOptionsWithStdin as SpawnOptionsWithStdin,
@@ -43,17 +50,7 @@ export class LocalSandboxSession implements SandboxSession {
4350
return isAbsolute(path) ? path : join(this.rootDirectory, path);
4451
}
4552

46-
async run({
47-
command,
48-
workingDirectory,
49-
env,
50-
abortSignal,
51-
}: {
52-
command: string;
53-
workingDirectory?: string;
54-
env?: Record<string, string>;
55-
abortSignal?: AbortSignal;
56-
}) {
53+
async run({ command, workingDirectory, env, abortSignal }: RunOptions) {
5754
const proc = await this.spawn({
5855
command,
5956
workingDirectory,
@@ -149,10 +146,7 @@ export class LocalSandboxSession implements SandboxSession {
149146
async readFile({
150147
path,
151148
abortSignal,
152-
}: {
153-
path: string;
154-
abortSignal?: AbortSignal;
155-
}): Promise<ReadableStream<Uint8Array> | null> {
149+
}: ReadFileOptions): Promise<ReadableStream<Uint8Array> | null> {
156150
const resolved = this.resolvePath(path);
157151
try {
158152
await stat(resolved);
@@ -168,11 +162,7 @@ export class LocalSandboxSession implements SandboxSession {
168162
path,
169163
content,
170164
abortSignal,
171-
}: {
172-
path: string;
173-
content: ReadableStream<Uint8Array>;
174-
abortSignal?: AbortSignal;
175-
}): Promise<void> {
165+
}: WriteFileOptions): Promise<void> {
176166
const resolved = this.resolvePath(path);
177167
await mkdir(dirname(resolved), { recursive: true });
178168
const source = Readable.fromWeb(content as any);
@@ -185,10 +175,7 @@ export class LocalSandboxSession implements SandboxSession {
185175
async readBinaryFile({
186176
path,
187177
abortSignal,
188-
}: {
189-
path: string;
190-
abortSignal?: AbortSignal;
191-
}): Promise<Uint8Array | null> {
178+
}: ReadBinaryFileOptions): Promise<Uint8Array | null> {
192179
const stream = await this.readFile({ path, abortSignal });
193180
if (stream == null) return null;
194181
return collectStream(stream);
@@ -198,11 +185,7 @@ export class LocalSandboxSession implements SandboxSession {
198185
path,
199186
content,
200187
abortSignal,
201-
}: {
202-
path: string;
203-
content: Uint8Array;
204-
abortSignal?: AbortSignal;
205-
}): Promise<void> {
188+
}: WriteBinaryFileOptions): Promise<void> {
206189
await this.writeFile({
207190
path,
208191
content: bytesToStream(content),
@@ -216,13 +199,7 @@ export class LocalSandboxSession implements SandboxSession {
216199
startLine,
217200
endLine,
218201
abortSignal,
219-
}: {
220-
path: string;
221-
encoding?: string;
222-
startLine?: number;
223-
endLine?: number;
224-
abortSignal?: AbortSignal;
225-
}): Promise<string | null> {
202+
}: ReadTextFileOptions): Promise<string | null> {
226203
const bytes = await this.readBinaryFile({ path, abortSignal });
227204
if (bytes == null) return null;
228205
const text = Buffer.from(bytes).toString(encoding as BufferEncoding);
@@ -234,12 +211,7 @@ export class LocalSandboxSession implements SandboxSession {
234211
content,
235212
encoding = 'utf-8',
236213
abortSignal,
237-
}: {
238-
path: string;
239-
content: string;
240-
encoding?: string;
241-
abortSignal?: AbortSignal;
242-
}): Promise<void> {
214+
}: WriteTextFileOptions): Promise<void> {
243215
const bytes = Buffer.from(content, encoding as BufferEncoding);
244216
await this.writeBinaryFile({
245217
path,

0 commit comments

Comments
 (0)