@meistrari/sandbox-sdk 0.1.4
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/README.md +373 -0
- package/dist/index.cjs +381 -0
- package/dist/index.d.cts +195 -0
- package/dist/index.d.mts +195 -0
- package/dist/index.d.ts +195 -0
- package/dist/index.mjs +374 -0
- package/package.json +29 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const eventsourceParser = require('eventsource-parser');
|
|
4
|
+
|
|
5
|
+
class FetchError extends Error {
|
|
6
|
+
constructor(message, url, method, status, response) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.url = url;
|
|
9
|
+
this.method = method;
|
|
10
|
+
this.status = status;
|
|
11
|
+
this.response = response;
|
|
12
|
+
this.name = "FetchError";
|
|
13
|
+
}
|
|
14
|
+
static async from(url, method, res) {
|
|
15
|
+
let body;
|
|
16
|
+
try {
|
|
17
|
+
body = await res.json();
|
|
18
|
+
} catch {
|
|
19
|
+
body = await res.text();
|
|
20
|
+
}
|
|
21
|
+
const message = typeof body === "object" && body && "error" in body ? body.error : `Request failed with status ${res.status}`;
|
|
22
|
+
return new FetchError(message, url, method, res.status, body);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async function* parseSSEStream(response) {
|
|
27
|
+
const reader = response.body?.getReader();
|
|
28
|
+
if (!reader)
|
|
29
|
+
throw new Error("No response body");
|
|
30
|
+
const decoder = new TextDecoder();
|
|
31
|
+
const events = [];
|
|
32
|
+
const parser = eventsourceParser.createParser({
|
|
33
|
+
onEvent: (event) => {
|
|
34
|
+
if (event.data) {
|
|
35
|
+
try {
|
|
36
|
+
events.push(JSON.parse(event.data));
|
|
37
|
+
} catch {
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
while (true) {
|
|
43
|
+
const { done, value } = await reader.read();
|
|
44
|
+
if (done)
|
|
45
|
+
break;
|
|
46
|
+
parser.feed(decoder.decode(value, { stream: true }));
|
|
47
|
+
while (events.length > 0) {
|
|
48
|
+
yield events.shift();
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const TERMINAL_STATUSES = ["completed", "failed", "timeout", "cancelled"];
|
|
54
|
+
async function pollUntilComplete(fetcher, options = {}) {
|
|
55
|
+
const { intervalMs = 500, maxAttempts = 600, onStatus } = options;
|
|
56
|
+
let attempts = 0;
|
|
57
|
+
while (attempts < maxAttempts) {
|
|
58
|
+
const result = await fetcher();
|
|
59
|
+
onStatus?.(result.status);
|
|
60
|
+
if (TERMINAL_STATUSES.includes(result.status)) {
|
|
61
|
+
return result;
|
|
62
|
+
}
|
|
63
|
+
await new Promise((resolve) => setTimeout(resolve, intervalMs));
|
|
64
|
+
attempts++;
|
|
65
|
+
}
|
|
66
|
+
throw new Error(`Polling timeout after ${maxAttempts} attempts`);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
var __defProp$2 = Object.defineProperty;
|
|
70
|
+
var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
71
|
+
var __publicField$2 = (obj, key, value) => {
|
|
72
|
+
__defNormalProp$2(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
73
|
+
return value;
|
|
74
|
+
};
|
|
75
|
+
class Sandbox {
|
|
76
|
+
constructor(client, sandboxId, auth) {
|
|
77
|
+
this.client = client;
|
|
78
|
+
this.auth = auth;
|
|
79
|
+
__publicField$2(this, "sandboxId");
|
|
80
|
+
__publicField$2(this, "files");
|
|
81
|
+
__publicField$2(this, "commands");
|
|
82
|
+
__publicField$2(this, "timeoutMs", 3e5);
|
|
83
|
+
this.sandboxId = sandboxId;
|
|
84
|
+
const readFile = (path, options) => {
|
|
85
|
+
return this.client.readFile(sandboxId, path, options);
|
|
86
|
+
};
|
|
87
|
+
this.files = {
|
|
88
|
+
write: (path, content) => this.client.writeFile(sandboxId, path, content),
|
|
89
|
+
read: readFile,
|
|
90
|
+
exists: (path) => this.client.fileExists(sandboxId, path),
|
|
91
|
+
remove: (path) => this.client.deleteFile(sandboxId, path),
|
|
92
|
+
list: (directory) => this.client.listFiles(sandboxId, directory)
|
|
93
|
+
};
|
|
94
|
+
this.commands = {
|
|
95
|
+
run: async (command) => {
|
|
96
|
+
const result = await this.runCode(command, { language: "bash" });
|
|
97
|
+
return {
|
|
98
|
+
stdout: result.stdout,
|
|
99
|
+
stderr: result.stderr,
|
|
100
|
+
exitCode: result.exitCode
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
// Factory methods (static)
|
|
106
|
+
static async create(client, options = {}) {
|
|
107
|
+
return client.createSandbox(options);
|
|
108
|
+
}
|
|
109
|
+
static async connect(client, sandboxId) {
|
|
110
|
+
return client.connectToSandbox(sandboxId);
|
|
111
|
+
}
|
|
112
|
+
// Normalize language aliases ('ts' -> 'typescript', 'js' -> 'javascript')
|
|
113
|
+
normalizeLanguage(lang) {
|
|
114
|
+
if (lang === "ts")
|
|
115
|
+
return "typescript";
|
|
116
|
+
if (lang === "js")
|
|
117
|
+
return "javascript";
|
|
118
|
+
return lang ?? "typescript";
|
|
119
|
+
}
|
|
120
|
+
// Main execution method (compatible with E2B and agent-api)
|
|
121
|
+
async runCode(script, options = {}) {
|
|
122
|
+
let stdout = "";
|
|
123
|
+
let stderr = "";
|
|
124
|
+
let exitCode;
|
|
125
|
+
const stream = this.client.streamRun({
|
|
126
|
+
code: script,
|
|
127
|
+
sandboxId: this.sandboxId,
|
|
128
|
+
language: this.normalizeLanguage(options.language),
|
|
129
|
+
env: options.envs,
|
|
130
|
+
timeoutMs: options.timeoutMs ?? this.timeoutMs,
|
|
131
|
+
libraries: options.libraries,
|
|
132
|
+
files: options.files
|
|
133
|
+
});
|
|
134
|
+
for await (const event of stream) {
|
|
135
|
+
if (event.type === "stdout" && event.content) {
|
|
136
|
+
stdout += event.content + "\n";
|
|
137
|
+
await options.onStdout?.({ line: event.content });
|
|
138
|
+
} else if (event.type === "stderr" && event.content) {
|
|
139
|
+
stderr += event.content + "\n";
|
|
140
|
+
await options.onStderr?.({ line: event.content });
|
|
141
|
+
} else if (event.type === "status" && event.metadata?.exitCode !== void 0) {
|
|
142
|
+
exitCode = event.metadata.exitCode;
|
|
143
|
+
} else if (event.type === "error") {
|
|
144
|
+
const error = new Error(event.content ?? "Execution error");
|
|
145
|
+
options.onError?.(error);
|
|
146
|
+
return { stdout, stderr, exitCode, error: event.content };
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return { stdout: stdout.trim(), stderr: stderr.trim(), exitCode };
|
|
150
|
+
}
|
|
151
|
+
// Lifecycle methods
|
|
152
|
+
async kill() {
|
|
153
|
+
await this.client.deleteSandbox(this.sandboxId);
|
|
154
|
+
}
|
|
155
|
+
async betaPause() {
|
|
156
|
+
}
|
|
157
|
+
setTimeout(ms) {
|
|
158
|
+
this.timeoutMs = ms;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
var __defProp$1 = Object.defineProperty;
|
|
163
|
+
var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
164
|
+
var __publicField$1 = (obj, key, value) => {
|
|
165
|
+
__defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
166
|
+
return value;
|
|
167
|
+
};
|
|
168
|
+
class SandboxRun {
|
|
169
|
+
constructor(client, data) {
|
|
170
|
+
this.client = client;
|
|
171
|
+
__publicField$1(this, "id");
|
|
172
|
+
__publicField$1(this, "sandboxId");
|
|
173
|
+
__publicField$1(this, "status");
|
|
174
|
+
__publicField$1(this, "stdout");
|
|
175
|
+
__publicField$1(this, "stderr");
|
|
176
|
+
__publicField$1(this, "exitCode");
|
|
177
|
+
__publicField$1(this, "errorMessage");
|
|
178
|
+
__publicField$1(this, "durationMs");
|
|
179
|
+
this.id = data.id;
|
|
180
|
+
this.sandboxId = data.sandboxId;
|
|
181
|
+
this.status = data.status;
|
|
182
|
+
this.stdout = data.stdout;
|
|
183
|
+
this.stderr = data.stderr;
|
|
184
|
+
this.exitCode = data.exitCode;
|
|
185
|
+
this.errorMessage = data.errorMessage;
|
|
186
|
+
this.durationMs = data.durationMs;
|
|
187
|
+
}
|
|
188
|
+
async refresh() {
|
|
189
|
+
const run = await this.client.getRun(this.id);
|
|
190
|
+
this.status = run.status;
|
|
191
|
+
this.stdout = run.stdout;
|
|
192
|
+
this.stderr = run.stderr;
|
|
193
|
+
this.exitCode = run.exitCode;
|
|
194
|
+
this.errorMessage = run.errorMessage;
|
|
195
|
+
this.durationMs = run.durationMs;
|
|
196
|
+
return this;
|
|
197
|
+
}
|
|
198
|
+
async waitForCompletion(options) {
|
|
199
|
+
await pollUntilComplete(
|
|
200
|
+
async () => {
|
|
201
|
+
await this.refresh();
|
|
202
|
+
return this;
|
|
203
|
+
},
|
|
204
|
+
options
|
|
205
|
+
);
|
|
206
|
+
return this;
|
|
207
|
+
}
|
|
208
|
+
get isComplete() {
|
|
209
|
+
return ["completed", "failed", "timeout", "cancelled"].includes(this.status);
|
|
210
|
+
}
|
|
211
|
+
get isSuccess() {
|
|
212
|
+
return this.status === "completed" && this.exitCode === 0;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
var __defProp = Object.defineProperty;
|
|
217
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
218
|
+
var __publicField = (obj, key, value) => {
|
|
219
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
220
|
+
return value;
|
|
221
|
+
};
|
|
222
|
+
class SandboxClient {
|
|
223
|
+
constructor(config) {
|
|
224
|
+
__publicField(this, "apiUrl");
|
|
225
|
+
__publicField(this, "auth");
|
|
226
|
+
__publicField(this, "timeout");
|
|
227
|
+
this.apiUrl = config.apiUrl.replace(/\/$/, "");
|
|
228
|
+
this.auth = config.auth;
|
|
229
|
+
this.timeout = config.timeout ?? 3e5;
|
|
230
|
+
}
|
|
231
|
+
async fetch(path, options = {}) {
|
|
232
|
+
const url = `${this.apiUrl}${path}`;
|
|
233
|
+
const headers = new Headers(options.headers);
|
|
234
|
+
const authHeaders = this.auth.getHeaders();
|
|
235
|
+
authHeaders.forEach((value, key) => {
|
|
236
|
+
headers.set(key, value);
|
|
237
|
+
});
|
|
238
|
+
headers.set("Content-Type", "application/json");
|
|
239
|
+
const res = await fetch(url, { ...options, headers });
|
|
240
|
+
if (!res.ok) {
|
|
241
|
+
throw await FetchError.from(url, options.method ?? "GET", res);
|
|
242
|
+
}
|
|
243
|
+
return res.json();
|
|
244
|
+
}
|
|
245
|
+
// === Runs ===
|
|
246
|
+
async createRun(request) {
|
|
247
|
+
const response = await this.fetch("/api/runs", {
|
|
248
|
+
method: "POST",
|
|
249
|
+
body: JSON.stringify(request)
|
|
250
|
+
});
|
|
251
|
+
return new SandboxRun(this, response.data);
|
|
252
|
+
}
|
|
253
|
+
async createRunAndWait(request) {
|
|
254
|
+
const run = await this.createRun(request);
|
|
255
|
+
return run.waitForCompletion();
|
|
256
|
+
}
|
|
257
|
+
async *streamRun(request) {
|
|
258
|
+
const url = `${this.apiUrl}/api/runs/stream`;
|
|
259
|
+
const headers = new Headers();
|
|
260
|
+
const authHeaders = this.auth.getHeaders();
|
|
261
|
+
authHeaders.forEach((value, key) => {
|
|
262
|
+
headers.set(key, value);
|
|
263
|
+
});
|
|
264
|
+
headers.set("Content-Type", "application/json");
|
|
265
|
+
const res = await fetch(url, {
|
|
266
|
+
method: "POST",
|
|
267
|
+
headers,
|
|
268
|
+
body: JSON.stringify(request)
|
|
269
|
+
});
|
|
270
|
+
if (!res.ok) {
|
|
271
|
+
throw await FetchError.from(url, "POST", res);
|
|
272
|
+
}
|
|
273
|
+
yield* parseSSEStream(res);
|
|
274
|
+
}
|
|
275
|
+
async getRun(runId) {
|
|
276
|
+
const response = await this.fetch(`/api/runs/${runId}`);
|
|
277
|
+
return new SandboxRun(this, response.data);
|
|
278
|
+
}
|
|
279
|
+
// === Sandboxes ===
|
|
280
|
+
async createSandbox(request = {}) {
|
|
281
|
+
const response = await this.fetch("/api/sandboxes", {
|
|
282
|
+
method: "POST",
|
|
283
|
+
body: JSON.stringify(request)
|
|
284
|
+
});
|
|
285
|
+
await pollUntilComplete(
|
|
286
|
+
() => this.getSandboxStatus(response.data.sandboxId),
|
|
287
|
+
{ intervalMs: 200, maxAttempts: 150 }
|
|
288
|
+
// 30s max
|
|
289
|
+
);
|
|
290
|
+
return new Sandbox(this, response.data.sandboxId, this.auth);
|
|
291
|
+
}
|
|
292
|
+
async getSandboxStatus(sandboxId) {
|
|
293
|
+
const response = await this.fetch(`/api/sandboxes/${sandboxId}`);
|
|
294
|
+
return response.data;
|
|
295
|
+
}
|
|
296
|
+
async connectToSandbox(sandboxId) {
|
|
297
|
+
await this.getSandboxStatus(sandboxId);
|
|
298
|
+
return new Sandbox(this, sandboxId, this.auth);
|
|
299
|
+
}
|
|
300
|
+
async deleteSandbox(sandboxId) {
|
|
301
|
+
await this.fetch(`/api/sandboxes/${sandboxId}`, { method: "DELETE" });
|
|
302
|
+
}
|
|
303
|
+
// === File Operations ===
|
|
304
|
+
async writeFile(sandboxId, path, content) {
|
|
305
|
+
const isBuffer = content instanceof Uint8Array;
|
|
306
|
+
await this.fetch(`/api/sandboxes/${sandboxId}/files`, {
|
|
307
|
+
method: "POST",
|
|
308
|
+
body: JSON.stringify({
|
|
309
|
+
name: path.split("/").pop(),
|
|
310
|
+
path,
|
|
311
|
+
content: isBuffer ? Buffer.from(content).toString("base64") : content,
|
|
312
|
+
encoding: isBuffer ? "base64" : "utf8"
|
|
313
|
+
})
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
async readFile(sandboxId, path, options) {
|
|
317
|
+
const response = await this.fetch(
|
|
318
|
+
`/api/sandboxes/${sandboxId}/files/${encodeURIComponent(path)}`
|
|
319
|
+
);
|
|
320
|
+
const { file } = response;
|
|
321
|
+
const format = options?.format ?? "text";
|
|
322
|
+
if (format === "blob") {
|
|
323
|
+
if (file.encoding === "base64") {
|
|
324
|
+
const bytes2 = Uint8Array.from(Buffer.from(file.content, "base64"));
|
|
325
|
+
return new Blob([bytes2]);
|
|
326
|
+
}
|
|
327
|
+
const bytes = Uint8Array.from(Buffer.from(file.content, "utf8"));
|
|
328
|
+
return new Blob([bytes]);
|
|
329
|
+
}
|
|
330
|
+
if (file.encoding === "base64") {
|
|
331
|
+
return Buffer.from(file.content, "base64").toString("utf8");
|
|
332
|
+
}
|
|
333
|
+
return file.content;
|
|
334
|
+
}
|
|
335
|
+
async fileExists(sandboxId, path) {
|
|
336
|
+
try {
|
|
337
|
+
await this.fetch(`/api/sandboxes/${sandboxId}/files/${encodeURIComponent(path)}`);
|
|
338
|
+
return true;
|
|
339
|
+
} catch (error) {
|
|
340
|
+
if (error instanceof FetchError && error.status === 404) {
|
|
341
|
+
return false;
|
|
342
|
+
}
|
|
343
|
+
throw error;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
async listFiles(sandboxId, directory) {
|
|
347
|
+
const query = directory ? `?directory=${encodeURIComponent(directory)}` : "";
|
|
348
|
+
const response = await this.fetch(
|
|
349
|
+
`/api/sandboxes/${sandboxId}/files${query}`
|
|
350
|
+
);
|
|
351
|
+
return response.files;
|
|
352
|
+
}
|
|
353
|
+
async deleteFile(sandboxId, path) {
|
|
354
|
+
await this.fetch(`/api/sandboxes/${sandboxId}/files`, {
|
|
355
|
+
method: "DELETE",
|
|
356
|
+
body: JSON.stringify({ filePaths: [path] })
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
class DataTokenAuthStrategy {
|
|
362
|
+
constructor(dataToken) {
|
|
363
|
+
this.dataToken = dataToken;
|
|
364
|
+
}
|
|
365
|
+
getHeaders() {
|
|
366
|
+
return new Headers({
|
|
367
|
+
"x-data-token": this.dataToken
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
function sandbox(config) {
|
|
373
|
+
return new SandboxClient(config);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
exports.DataTokenAuthStrategy = DataTokenAuthStrategy;
|
|
377
|
+
exports.FetchError = FetchError;
|
|
378
|
+
exports.Sandbox = Sandbox;
|
|
379
|
+
exports.SandboxClient = SandboxClient;
|
|
380
|
+
exports.SandboxRun = SandboxRun;
|
|
381
|
+
exports.sandbox = sandbox;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
interface SandboxConfig {
|
|
2
|
+
apiUrl: string;
|
|
3
|
+
auth: AuthStrategy;
|
|
4
|
+
timeout?: number;
|
|
5
|
+
}
|
|
6
|
+
interface AuthStrategy {
|
|
7
|
+
getHeaders(): Headers;
|
|
8
|
+
}
|
|
9
|
+
interface CreateRunRequest {
|
|
10
|
+
code: string;
|
|
11
|
+
language?: 'javascript' | 'typescript' | 'python' | 'bash' | 'ts' | 'js';
|
|
12
|
+
libraries?: string[];
|
|
13
|
+
env?: Record<string, string>;
|
|
14
|
+
files?: FileInput[];
|
|
15
|
+
memSize?: number;
|
|
16
|
+
cpuSize?: number;
|
|
17
|
+
timeoutMs?: number;
|
|
18
|
+
sandboxId?: string;
|
|
19
|
+
persistent?: boolean;
|
|
20
|
+
sandboxTtlSeconds?: number;
|
|
21
|
+
}
|
|
22
|
+
interface CreateSandboxRequest {
|
|
23
|
+
cpuSize?: number;
|
|
24
|
+
memSize?: number;
|
|
25
|
+
sandboxTtlSeconds?: number;
|
|
26
|
+
files?: FileInput[];
|
|
27
|
+
}
|
|
28
|
+
interface FileInput {
|
|
29
|
+
name: string;
|
|
30
|
+
content: string;
|
|
31
|
+
encoding: 'base64' | 'utf8';
|
|
32
|
+
path?: string;
|
|
33
|
+
}
|
|
34
|
+
type Status = 'pending' | 'running' | 'completed' | 'failed' | 'timeout' | 'cancelled';
|
|
35
|
+
interface RunResponse {
|
|
36
|
+
id: string;
|
|
37
|
+
workspaceId: string;
|
|
38
|
+
status: Status;
|
|
39
|
+
sandboxId?: string;
|
|
40
|
+
exitCode?: number;
|
|
41
|
+
stdout?: string;
|
|
42
|
+
stderr?: string;
|
|
43
|
+
durationMs?: number;
|
|
44
|
+
errorMessage?: string;
|
|
45
|
+
createdAt: Date;
|
|
46
|
+
completedAt?: Date;
|
|
47
|
+
}
|
|
48
|
+
interface StreamEvent {
|
|
49
|
+
type: 'run_created' | 'sandbox_created' | 'stdout' | 'stderr' | 'status' | 'done' | 'error';
|
|
50
|
+
content?: string;
|
|
51
|
+
runId?: string;
|
|
52
|
+
sandboxId?: string;
|
|
53
|
+
metadata?: Record<string, unknown>;
|
|
54
|
+
}
|
|
55
|
+
interface StreamCallbacks {
|
|
56
|
+
onStdout?: (line: string) => void | Promise<void>;
|
|
57
|
+
onStderr?: (line: string) => void | Promise<void>;
|
|
58
|
+
onStatus?: (status: Status) => void | Promise<void>;
|
|
59
|
+
onError?: (error: Error) => void | Promise<void>;
|
|
60
|
+
}
|
|
61
|
+
interface ExecutionResult {
|
|
62
|
+
stdout: string;
|
|
63
|
+
stderr: string;
|
|
64
|
+
exitCode?: number;
|
|
65
|
+
error?: string;
|
|
66
|
+
}
|
|
67
|
+
interface RunCodeOptions {
|
|
68
|
+
envs?: Record<string, string>;
|
|
69
|
+
language?: 'typescript' | 'javascript' | 'python' | 'bash' | 'ts' | 'js';
|
|
70
|
+
timeoutMs?: number;
|
|
71
|
+
libraries?: string[];
|
|
72
|
+
files?: FileInput[];
|
|
73
|
+
onStdout?: (data: {
|
|
74
|
+
line: string;
|
|
75
|
+
}) => void | Promise<void>;
|
|
76
|
+
onStderr?: (data: {
|
|
77
|
+
line: string;
|
|
78
|
+
}) => void | Promise<void>;
|
|
79
|
+
onError?: (error: Error) => void;
|
|
80
|
+
}
|
|
81
|
+
interface FileReadOptions {
|
|
82
|
+
format?: 'text' | 'blob';
|
|
83
|
+
}
|
|
84
|
+
interface SandboxFiles {
|
|
85
|
+
write: (path: string, content: string | Uint8Array) => Promise<void>;
|
|
86
|
+
read: {
|
|
87
|
+
(path: string): Promise<string>;
|
|
88
|
+
(path: string, options: {
|
|
89
|
+
format: 'text';
|
|
90
|
+
}): Promise<string>;
|
|
91
|
+
(path: string, options: {
|
|
92
|
+
format: 'blob';
|
|
93
|
+
}): Promise<Blob>;
|
|
94
|
+
};
|
|
95
|
+
exists: (path: string) => Promise<boolean>;
|
|
96
|
+
remove: (path: string) => Promise<void>;
|
|
97
|
+
list: (directory?: string) => Promise<string[]>;
|
|
98
|
+
}
|
|
99
|
+
interface SandboxCommands {
|
|
100
|
+
run: (command: string) => Promise<{
|
|
101
|
+
stdout: string;
|
|
102
|
+
stderr: string;
|
|
103
|
+
exitCode?: number;
|
|
104
|
+
}>;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
declare class Sandbox {
|
|
108
|
+
private client;
|
|
109
|
+
private auth;
|
|
110
|
+
readonly sandboxId: string;
|
|
111
|
+
readonly files: SandboxFiles;
|
|
112
|
+
readonly commands: SandboxCommands;
|
|
113
|
+
private timeoutMs;
|
|
114
|
+
constructor(client: SandboxClient, sandboxId: string, auth: AuthStrategy);
|
|
115
|
+
static create(client: SandboxClient, options?: {
|
|
116
|
+
memSize?: number;
|
|
117
|
+
cpuSize?: number;
|
|
118
|
+
files?: Array<{
|
|
119
|
+
name: string;
|
|
120
|
+
content: string;
|
|
121
|
+
encoding: 'utf8' | 'base64';
|
|
122
|
+
path?: string;
|
|
123
|
+
}>;
|
|
124
|
+
}): Promise<Sandbox>;
|
|
125
|
+
static connect(client: SandboxClient, sandboxId: string): Promise<Sandbox>;
|
|
126
|
+
private normalizeLanguage;
|
|
127
|
+
runCode(script: string, options?: RunCodeOptions): Promise<ExecutionResult>;
|
|
128
|
+
kill(): Promise<void>;
|
|
129
|
+
betaPause(): Promise<void>;
|
|
130
|
+
setTimeout(ms: number): void;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
declare class SandboxRun {
|
|
134
|
+
private client;
|
|
135
|
+
readonly id: string;
|
|
136
|
+
readonly sandboxId?: string;
|
|
137
|
+
status: Status;
|
|
138
|
+
stdout?: string;
|
|
139
|
+
stderr?: string;
|
|
140
|
+
exitCode?: number;
|
|
141
|
+
errorMessage?: string;
|
|
142
|
+
durationMs?: number;
|
|
143
|
+
constructor(client: SandboxClient, data: RunResponse);
|
|
144
|
+
refresh(): Promise<this>;
|
|
145
|
+
waitForCompletion(options?: {
|
|
146
|
+
intervalMs?: number;
|
|
147
|
+
maxAttempts?: number;
|
|
148
|
+
}): Promise<this>;
|
|
149
|
+
get isComplete(): boolean;
|
|
150
|
+
get isSuccess(): boolean;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
declare class SandboxClient {
|
|
154
|
+
private apiUrl;
|
|
155
|
+
private auth;
|
|
156
|
+
private timeout;
|
|
157
|
+
constructor(config: SandboxConfig);
|
|
158
|
+
private fetch;
|
|
159
|
+
createRun(request: CreateRunRequest): Promise<SandboxRun>;
|
|
160
|
+
createRunAndWait(request: CreateRunRequest): Promise<SandboxRun>;
|
|
161
|
+
streamRun(request: CreateRunRequest): AsyncGenerator<StreamEvent>;
|
|
162
|
+
getRun(runId: string): Promise<SandboxRun>;
|
|
163
|
+
createSandbox(request?: CreateSandboxRequest): Promise<Sandbox>;
|
|
164
|
+
getSandboxStatus(sandboxId: string): Promise<{
|
|
165
|
+
sandboxId: string;
|
|
166
|
+
status: Status;
|
|
167
|
+
}>;
|
|
168
|
+
connectToSandbox(sandboxId: string): Promise<Sandbox>;
|
|
169
|
+
deleteSandbox(sandboxId: string): Promise<void>;
|
|
170
|
+
writeFile(sandboxId: string, path: string, content: string | Uint8Array): Promise<void>;
|
|
171
|
+
readFile(sandboxId: string, path: string, options?: FileReadOptions): Promise<string | Blob>;
|
|
172
|
+
fileExists(sandboxId: string, path: string): Promise<boolean>;
|
|
173
|
+
listFiles(sandboxId: string, directory?: string): Promise<string[]>;
|
|
174
|
+
deleteFile(sandboxId: string, path: string): Promise<void>;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
declare class DataTokenAuthStrategy implements AuthStrategy {
|
|
178
|
+
private dataToken;
|
|
179
|
+
constructor(dataToken: string);
|
|
180
|
+
getHeaders(): Headers;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
declare class FetchError extends Error {
|
|
184
|
+
readonly url: string;
|
|
185
|
+
readonly method: string;
|
|
186
|
+
readonly status: number;
|
|
187
|
+
readonly response?: unknown | undefined;
|
|
188
|
+
constructor(message: string, url: string, method: string, status: number, response?: unknown | undefined);
|
|
189
|
+
static from(url: string, method: string, res: Response): Promise<FetchError>;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
declare function sandbox(config: SandboxConfig): SandboxClient;
|
|
193
|
+
|
|
194
|
+
export { DataTokenAuthStrategy, FetchError, Sandbox, SandboxClient, SandboxRun, sandbox };
|
|
195
|
+
export type { AuthStrategy, CreateRunRequest, CreateSandboxRequest, ExecutionResult, FileInput, RunCodeOptions, RunResponse, SandboxCommands, SandboxConfig, SandboxFiles, Status, StreamCallbacks, StreamEvent };
|