@cloudflare/sandbox 0.0.0-dc66e8e → 0.0.0-e943505
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 +161 -0
- package/Dockerfile +46 -24
- package/README.md +846 -0
- package/container_src/bun.lock +76 -0
- package/container_src/circuit-breaker.ts +121 -0
- package/container_src/control-process.ts +784 -0
- package/container_src/handler/exec.ts +99 -251
- package/container_src/handler/file.ts +204 -642
- package/container_src/handler/git.ts +28 -80
- package/container_src/handler/process.ts +443 -515
- package/container_src/handler/session.ts +92 -0
- package/container_src/index.ts +354 -123
- package/container_src/interpreter-service.ts +276 -0
- package/container_src/isolation.ts +1038 -0
- package/container_src/mime-processor.ts +255 -0
- package/container_src/package.json +9 -0
- package/container_src/runtime/executors/javascript/node_executor.ts +123 -0
- package/container_src/runtime/executors/python/ipython_executor.py +338 -0
- package/container_src/runtime/executors/typescript/ts_executor.ts +138 -0
- package/container_src/runtime/process-pool.ts +464 -0
- package/container_src/shell-escape.ts +42 -0
- package/container_src/startup.sh +11 -0
- package/container_src/types.ts +42 -14
- package/package.json +5 -4
- package/src/client.ts +206 -235
- package/src/errors.ts +219 -0
- package/src/index.ts +70 -15
- package/src/interpreter-client.ts +352 -0
- package/src/interpreter-types.ts +390 -0
- package/src/interpreter.ts +150 -0
- package/src/sandbox.ts +502 -400
- package/src/types.ts +140 -24
- package/tsconfig.json +1 -1
package/src/sandbox.ts
CHANGED
|
@@ -1,25 +1,33 @@
|
|
|
1
1
|
import { Container, getContainer } from "@cloudflare/containers";
|
|
2
|
-
import {
|
|
2
|
+
import { CodeInterpreter } from "./interpreter";
|
|
3
|
+
import { InterpreterClient } from "./interpreter-client";
|
|
4
|
+
import type {
|
|
5
|
+
CodeContext,
|
|
6
|
+
CreateContextOptions,
|
|
7
|
+
ExecutionResult,
|
|
8
|
+
RunCodeOptions,
|
|
9
|
+
} from "./interpreter-types";
|
|
3
10
|
import { isLocalhostPattern } from "./request-handler";
|
|
4
11
|
import {
|
|
5
12
|
logSecurityEvent,
|
|
6
13
|
SecurityError,
|
|
7
14
|
sanitizeSandboxId,
|
|
8
|
-
validatePort
|
|
15
|
+
validatePort,
|
|
9
16
|
} from "./security";
|
|
17
|
+
import { parseSSEStream } from "./sse-parser";
|
|
10
18
|
import type {
|
|
19
|
+
ExecEvent,
|
|
11
20
|
ExecOptions,
|
|
12
21
|
ExecResult,
|
|
22
|
+
ExecuteResponse,
|
|
23
|
+
ExecutionSession,
|
|
13
24
|
ISandbox,
|
|
14
25
|
Process,
|
|
15
26
|
ProcessOptions,
|
|
16
27
|
ProcessStatus,
|
|
17
|
-
StreamOptions
|
|
18
|
-
} from "./types";
|
|
19
|
-
import {
|
|
20
|
-
ProcessNotFoundError,
|
|
21
|
-
SandboxError
|
|
28
|
+
StreamOptions,
|
|
22
29
|
} from "./types";
|
|
30
|
+
import { ProcessNotFoundError, SandboxError } from "./types";
|
|
23
31
|
|
|
24
32
|
export function getSandbox(ns: DurableObjectNamespace<Sandbox>, id: string) {
|
|
25
33
|
const stub = getContainer(ns, id);
|
|
@@ -31,22 +39,23 @@ export function getSandbox(ns: DurableObjectNamespace<Sandbox>, id: string) {
|
|
|
31
39
|
}
|
|
32
40
|
|
|
33
41
|
export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
|
|
34
|
-
|
|
35
|
-
|
|
42
|
+
defaultPort = 3000; // Default port for the container's Bun server
|
|
43
|
+
sleepAfter = "20m"; // Keep container warm for 20 minutes to avoid cold starts
|
|
44
|
+
client: InterpreterClient;
|
|
36
45
|
private sandboxName: string | null = null;
|
|
46
|
+
private codeInterpreter: CodeInterpreter;
|
|
47
|
+
private defaultSession: ExecutionSession | null = null;
|
|
37
48
|
|
|
38
49
|
constructor(ctx: DurableObjectState, env: Env) {
|
|
39
50
|
super(ctx, env);
|
|
40
|
-
this.client = new
|
|
51
|
+
this.client = new InterpreterClient({
|
|
41
52
|
onCommandComplete: (success, exitCode, _stdout, _stderr, command) => {
|
|
42
53
|
console.log(
|
|
43
54
|
`[Container] Command completed: ${command}, Success: ${success}, Exit code: ${exitCode}`
|
|
44
55
|
);
|
|
45
56
|
},
|
|
46
57
|
onCommandStart: (command) => {
|
|
47
|
-
console.log(
|
|
48
|
-
`[Container] Command started: ${command}`
|
|
49
|
-
);
|
|
58
|
+
console.log(`[Container] Command started: ${command}`);
|
|
50
59
|
},
|
|
51
60
|
onError: (error, _command) => {
|
|
52
61
|
console.error(`[Container] Command error: ${error}`);
|
|
@@ -58,9 +67,13 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
|
|
|
58
67
|
stub: this,
|
|
59
68
|
});
|
|
60
69
|
|
|
70
|
+
// Initialize code interpreter
|
|
71
|
+
this.codeInterpreter = new CodeInterpreter(this);
|
|
72
|
+
|
|
61
73
|
// Load the sandbox name from storage on initialization
|
|
62
74
|
this.ctx.blockConcurrencyWhile(async () => {
|
|
63
|
-
this.sandboxName =
|
|
75
|
+
this.sandboxName =
|
|
76
|
+
(await this.ctx.storage.get<string>("sandboxName")) || null;
|
|
64
77
|
});
|
|
65
78
|
}
|
|
66
79
|
|
|
@@ -68,7 +81,7 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
|
|
|
68
81
|
async setSandboxName(name: string): Promise<void> {
|
|
69
82
|
if (!this.sandboxName) {
|
|
70
83
|
this.sandboxName = name;
|
|
71
|
-
await this.ctx.storage.put(
|
|
84
|
+
await this.ctx.storage.put("sandboxName", name);
|
|
72
85
|
console.log(`[Sandbox] Stored sandbox name via RPC: ${name}`);
|
|
73
86
|
}
|
|
74
87
|
}
|
|
@@ -77,6 +90,11 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
|
|
|
77
90
|
async setEnvVars(envVars: Record<string, string>): Promise<void> {
|
|
78
91
|
this.envVars = { ...this.envVars, ...envVars };
|
|
79
92
|
console.log(`[Sandbox] Updated environment variables`);
|
|
93
|
+
|
|
94
|
+
// If we have a default session, update its environment too
|
|
95
|
+
if (this.defaultSession) {
|
|
96
|
+
await this.defaultSession.setEnvVars(envVars);
|
|
97
|
+
}
|
|
80
98
|
}
|
|
81
99
|
|
|
82
100
|
override onStart() {
|
|
@@ -85,9 +103,6 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
|
|
|
85
103
|
|
|
86
104
|
override onStop() {
|
|
87
105
|
console.log("Sandbox successfully shut down");
|
|
88
|
-
if (this.client) {
|
|
89
|
-
this.client.clearSession();
|
|
90
|
-
}
|
|
91
106
|
}
|
|
92
107
|
|
|
93
108
|
override onError(error: unknown) {
|
|
@@ -99,10 +114,10 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
|
|
|
99
114
|
const url = new URL(request.url);
|
|
100
115
|
|
|
101
116
|
// Capture and store the sandbox name from the header if present
|
|
102
|
-
if (!this.sandboxName && request.headers.has(
|
|
103
|
-
const name = request.headers.get(
|
|
117
|
+
if (!this.sandboxName && request.headers.has("X-Sandbox-Name")) {
|
|
118
|
+
const name = request.headers.get("X-Sandbox-Name")!;
|
|
104
119
|
this.sandboxName = name;
|
|
105
|
-
await this.ctx.storage.put(
|
|
120
|
+
await this.ctx.storage.put("sandboxName", name);
|
|
106
121
|
console.log(`[Sandbox] Stored sandbox name: ${this.sandboxName}`);
|
|
107
122
|
}
|
|
108
123
|
|
|
@@ -120,354 +135,104 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
|
|
|
120
135
|
return parseInt(proxyMatch[1]);
|
|
121
136
|
}
|
|
122
137
|
|
|
138
|
+
if (url.port) {
|
|
139
|
+
return parseInt(url.port);
|
|
140
|
+
}
|
|
141
|
+
|
|
123
142
|
// All other requests go to control plane on port 3000
|
|
124
143
|
// This includes /api/* endpoints and any other control requests
|
|
125
144
|
return 3000;
|
|
126
145
|
}
|
|
127
146
|
|
|
128
|
-
//
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
if (options?.signal?.aborted) {
|
|
140
|
-
throw new Error('Operation was aborted');
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
let result: ExecResult;
|
|
144
|
-
|
|
145
|
-
if (options?.stream && options?.onOutput) {
|
|
146
|
-
// Streaming with callbacks - we need to collect the final result
|
|
147
|
-
result = await this.executeWithStreaming(command, options, startTime, timestamp);
|
|
148
|
-
} else {
|
|
149
|
-
// Regular execution
|
|
150
|
-
const response = await this.client.execute(
|
|
151
|
-
command,
|
|
152
|
-
options?.sessionId
|
|
153
|
-
);
|
|
154
|
-
|
|
155
|
-
const duration = Date.now() - startTime;
|
|
156
|
-
result = this.mapExecuteResponseToExecResult(response, duration, options?.sessionId);
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
// Call completion callback if provided
|
|
160
|
-
if (options?.onComplete) {
|
|
161
|
-
options.onComplete(result);
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
return result;
|
|
165
|
-
} catch (error) {
|
|
166
|
-
if (options?.onError && error instanceof Error) {
|
|
167
|
-
options.onError(error);
|
|
168
|
-
}
|
|
169
|
-
throw error;
|
|
170
|
-
} finally {
|
|
171
|
-
if (timeoutId) {
|
|
172
|
-
clearTimeout(timeoutId);
|
|
173
|
-
}
|
|
147
|
+
// Helper to ensure default session is initialized
|
|
148
|
+
private async ensureDefaultSession(): Promise<ExecutionSession> {
|
|
149
|
+
if (!this.defaultSession) {
|
|
150
|
+
const sessionId = `sandbox-${this.sandboxName || 'default'}`;
|
|
151
|
+
this.defaultSession = await this.createSession({
|
|
152
|
+
id: sessionId,
|
|
153
|
+
env: this.envVars || {},
|
|
154
|
+
cwd: '/workspace',
|
|
155
|
+
isolation: true
|
|
156
|
+
});
|
|
157
|
+
console.log(`[Sandbox] Default session initialized: ${sessionId}`);
|
|
174
158
|
}
|
|
159
|
+
return this.defaultSession;
|
|
175
160
|
}
|
|
176
161
|
|
|
177
|
-
private async executeWithStreaming(
|
|
178
|
-
command: string,
|
|
179
|
-
options: ExecOptions,
|
|
180
|
-
startTime: number,
|
|
181
|
-
timestamp: string
|
|
182
|
-
): Promise<ExecResult> {
|
|
183
|
-
let stdout = '';
|
|
184
|
-
let stderr = '';
|
|
185
|
-
|
|
186
|
-
try {
|
|
187
|
-
const stream = await this.client.executeCommandStream(command, options.sessionId);
|
|
188
|
-
const { parseSSEStream } = await import('./sse-parser');
|
|
189
|
-
|
|
190
|
-
for await (const event of parseSSEStream<import('./types').ExecEvent>(stream)) {
|
|
191
|
-
// Check for cancellation
|
|
192
|
-
if (options.signal?.aborted) {
|
|
193
|
-
throw new Error('Operation was aborted');
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
switch (event.type) {
|
|
197
|
-
case 'stdout':
|
|
198
|
-
case 'stderr':
|
|
199
|
-
if (event.data) {
|
|
200
|
-
// Update accumulated output
|
|
201
|
-
if (event.type === 'stdout') stdout += event.data;
|
|
202
|
-
if (event.type === 'stderr') stderr += event.data;
|
|
203
|
-
|
|
204
|
-
// Call user's callback
|
|
205
|
-
if (options.onOutput) {
|
|
206
|
-
options.onOutput(event.type, event.data);
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
break;
|
|
210
|
-
|
|
211
|
-
case 'complete': {
|
|
212
|
-
// Use result from complete event if available
|
|
213
|
-
const duration = Date.now() - startTime;
|
|
214
|
-
return event.result || {
|
|
215
|
-
success: event.exitCode === 0,
|
|
216
|
-
exitCode: event.exitCode || 0,
|
|
217
|
-
stdout,
|
|
218
|
-
stderr,
|
|
219
|
-
command,
|
|
220
|
-
duration,
|
|
221
|
-
timestamp,
|
|
222
|
-
sessionId: options.sessionId
|
|
223
|
-
};
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
case 'error':
|
|
227
|
-
throw new Error(event.error || 'Command execution failed');
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
// If we get here without a complete event, something went wrong
|
|
232
|
-
throw new Error('Stream ended without completion event');
|
|
233
162
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
}
|
|
238
|
-
throw error;
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
private mapExecuteResponseToExecResult(
|
|
243
|
-
response: import('./client').ExecuteResponse,
|
|
244
|
-
duration: number,
|
|
245
|
-
sessionId?: string
|
|
246
|
-
): ExecResult {
|
|
247
|
-
return {
|
|
248
|
-
success: response.success,
|
|
249
|
-
exitCode: response.exitCode,
|
|
250
|
-
stdout: response.stdout,
|
|
251
|
-
stderr: response.stderr,
|
|
252
|
-
command: response.command,
|
|
253
|
-
duration,
|
|
254
|
-
timestamp: response.timestamp,
|
|
255
|
-
sessionId
|
|
256
|
-
};
|
|
163
|
+
async exec(command: string, options?: ExecOptions): Promise<ExecResult> {
|
|
164
|
+
const session = await this.ensureDefaultSession();
|
|
165
|
+
return session.exec(command, options);
|
|
257
166
|
}
|
|
258
167
|
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
processId: options?.processId,
|
|
266
|
-
sessionId: options?.sessionId,
|
|
267
|
-
timeout: options?.timeout,
|
|
268
|
-
env: options?.env,
|
|
269
|
-
cwd: options?.cwd,
|
|
270
|
-
encoding: options?.encoding,
|
|
271
|
-
autoCleanup: options?.autoCleanup
|
|
272
|
-
});
|
|
273
|
-
|
|
274
|
-
const process = response.process;
|
|
275
|
-
const processObj: Process = {
|
|
276
|
-
id: process.id,
|
|
277
|
-
pid: process.pid,
|
|
278
|
-
command: process.command,
|
|
279
|
-
status: process.status as ProcessStatus,
|
|
280
|
-
startTime: new Date(process.startTime),
|
|
281
|
-
endTime: undefined,
|
|
282
|
-
exitCode: undefined,
|
|
283
|
-
sessionId: process.sessionId,
|
|
284
|
-
|
|
285
|
-
async kill(): Promise<void> {
|
|
286
|
-
throw new Error('Method will be replaced');
|
|
287
|
-
},
|
|
288
|
-
async getStatus(): Promise<ProcessStatus> {
|
|
289
|
-
throw new Error('Method will be replaced');
|
|
290
|
-
},
|
|
291
|
-
async getLogs(): Promise<{ stdout: string; stderr: string }> {
|
|
292
|
-
throw new Error('Method will be replaced');
|
|
293
|
-
}
|
|
294
|
-
};
|
|
295
|
-
|
|
296
|
-
// Bind context properly
|
|
297
|
-
processObj.kill = async (signal?: string) => {
|
|
298
|
-
await this.killProcess(process.id, signal);
|
|
299
|
-
};
|
|
300
|
-
|
|
301
|
-
processObj.getStatus = async () => {
|
|
302
|
-
const current = await this.getProcess(process.id);
|
|
303
|
-
return current?.status || 'error';
|
|
304
|
-
};
|
|
305
|
-
|
|
306
|
-
processObj.getLogs = async () => {
|
|
307
|
-
const logs = await this.getProcessLogs(process.id);
|
|
308
|
-
return { stdout: logs.stdout, stderr: logs.stderr };
|
|
309
|
-
};
|
|
310
|
-
|
|
311
|
-
// Call onStart callback if provided
|
|
312
|
-
if (options?.onStart) {
|
|
313
|
-
options.onStart(processObj);
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
return processObj;
|
|
317
|
-
|
|
318
|
-
} catch (error) {
|
|
319
|
-
if (options?.onError && error instanceof Error) {
|
|
320
|
-
options.onError(error);
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
throw error;
|
|
324
|
-
}
|
|
168
|
+
async startProcess(
|
|
169
|
+
command: string,
|
|
170
|
+
options?: ProcessOptions
|
|
171
|
+
): Promise<Process> {
|
|
172
|
+
const session = await this.ensureDefaultSession();
|
|
173
|
+
return session.startProcess(command, options);
|
|
325
174
|
}
|
|
326
175
|
|
|
327
176
|
async listProcesses(): Promise<Process[]> {
|
|
328
|
-
const
|
|
329
|
-
|
|
330
|
-
return response.processes.map(processData => ({
|
|
331
|
-
id: processData.id,
|
|
332
|
-
pid: processData.pid,
|
|
333
|
-
command: processData.command,
|
|
334
|
-
status: processData.status,
|
|
335
|
-
startTime: new Date(processData.startTime),
|
|
336
|
-
endTime: processData.endTime ? new Date(processData.endTime) : undefined,
|
|
337
|
-
exitCode: processData.exitCode,
|
|
338
|
-
sessionId: processData.sessionId,
|
|
339
|
-
|
|
340
|
-
kill: async (signal?: string) => {
|
|
341
|
-
await this.killProcess(processData.id, signal);
|
|
342
|
-
},
|
|
343
|
-
|
|
344
|
-
getStatus: async () => {
|
|
345
|
-
const current = await this.getProcess(processData.id);
|
|
346
|
-
return current?.status || 'error';
|
|
347
|
-
},
|
|
348
|
-
|
|
349
|
-
getLogs: async () => {
|
|
350
|
-
const logs = await this.getProcessLogs(processData.id);
|
|
351
|
-
return { stdout: logs.stdout, stderr: logs.stderr };
|
|
352
|
-
}
|
|
353
|
-
}));
|
|
177
|
+
const session = await this.ensureDefaultSession();
|
|
178
|
+
return session.listProcesses();
|
|
354
179
|
}
|
|
355
180
|
|
|
356
181
|
async getProcess(id: string): Promise<Process | null> {
|
|
357
|
-
const
|
|
358
|
-
|
|
359
|
-
return null;
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
const processData = response.process;
|
|
363
|
-
return {
|
|
364
|
-
id: processData.id,
|
|
365
|
-
pid: processData.pid,
|
|
366
|
-
command: processData.command,
|
|
367
|
-
status: processData.status,
|
|
368
|
-
startTime: new Date(processData.startTime),
|
|
369
|
-
endTime: processData.endTime ? new Date(processData.endTime) : undefined,
|
|
370
|
-
exitCode: processData.exitCode,
|
|
371
|
-
sessionId: processData.sessionId,
|
|
372
|
-
|
|
373
|
-
kill: async (signal?: string) => {
|
|
374
|
-
await this.killProcess(processData.id, signal);
|
|
375
|
-
},
|
|
376
|
-
|
|
377
|
-
getStatus: async () => {
|
|
378
|
-
const current = await this.getProcess(processData.id);
|
|
379
|
-
return current?.status || 'error';
|
|
380
|
-
},
|
|
381
|
-
|
|
382
|
-
getLogs: async () => {
|
|
383
|
-
const logs = await this.getProcessLogs(processData.id);
|
|
384
|
-
return { stdout: logs.stdout, stderr: logs.stderr };
|
|
385
|
-
}
|
|
386
|
-
};
|
|
182
|
+
const session = await this.ensureDefaultSession();
|
|
183
|
+
return session.getProcess(id);
|
|
387
184
|
}
|
|
388
185
|
|
|
389
|
-
async killProcess(id: string,
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
await this.client.killProcess(id);
|
|
393
|
-
} catch (error) {
|
|
394
|
-
if (error instanceof Error && error.message.includes('Process not found')) {
|
|
395
|
-
throw new ProcessNotFoundError(id);
|
|
396
|
-
}
|
|
397
|
-
throw new SandboxError(
|
|
398
|
-
`Failed to kill process ${id}: ${error instanceof Error ? error.message : 'Unknown error'}`,
|
|
399
|
-
'KILL_PROCESS_FAILED'
|
|
400
|
-
);
|
|
401
|
-
}
|
|
186
|
+
async killProcess(id: string, signal?: string): Promise<void> {
|
|
187
|
+
const session = await this.ensureDefaultSession();
|
|
188
|
+
return session.killProcess(id, signal);
|
|
402
189
|
}
|
|
403
190
|
|
|
404
191
|
async killAllProcesses(): Promise<number> {
|
|
405
|
-
const
|
|
406
|
-
return
|
|
192
|
+
const session = await this.ensureDefaultSession();
|
|
193
|
+
return session.killAllProcesses();
|
|
407
194
|
}
|
|
408
195
|
|
|
409
196
|
async cleanupCompletedProcesses(): Promise<number> {
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
// We'll return 0 as a placeholder until the container endpoint is added
|
|
413
|
-
return 0;
|
|
197
|
+
const session = await this.ensureDefaultSession();
|
|
198
|
+
return session.cleanupCompletedProcesses();
|
|
414
199
|
}
|
|
415
200
|
|
|
416
|
-
async getProcessLogs(
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
stderr: response.stderr
|
|
422
|
-
};
|
|
423
|
-
} catch (error) {
|
|
424
|
-
if (error instanceof Error && error.message.includes('Process not found')) {
|
|
425
|
-
throw new ProcessNotFoundError(id);
|
|
426
|
-
}
|
|
427
|
-
throw error;
|
|
428
|
-
}
|
|
201
|
+
async getProcessLogs(
|
|
202
|
+
id: string
|
|
203
|
+
): Promise<{ stdout: string; stderr: string }> {
|
|
204
|
+
const session = await this.ensureDefaultSession();
|
|
205
|
+
return session.getProcessLogs(id);
|
|
429
206
|
}
|
|
430
207
|
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
// Get the stream from HttpClient (need to add this method)
|
|
440
|
-
const stream = await this.client.executeCommandStream(command, options?.sessionId);
|
|
441
|
-
|
|
442
|
-
// Return the ReadableStream directly - can be converted to AsyncIterable by consumers
|
|
443
|
-
return stream;
|
|
208
|
+
// Streaming methods - delegates to default session
|
|
209
|
+
async execStream(
|
|
210
|
+
command: string,
|
|
211
|
+
options?: StreamOptions
|
|
212
|
+
): Promise<ReadableStream<Uint8Array>> {
|
|
213
|
+
const session = await this.ensureDefaultSession();
|
|
214
|
+
return session.execStream(command, options);
|
|
444
215
|
}
|
|
445
216
|
|
|
446
|
-
async streamProcessLogs(
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
// Get the stream from HttpClient
|
|
453
|
-
const stream = await this.client.streamProcessLogs(processId);
|
|
454
|
-
|
|
455
|
-
// Return the ReadableStream directly - can be converted to AsyncIterable by consumers
|
|
456
|
-
return stream;
|
|
217
|
+
async streamProcessLogs(
|
|
218
|
+
processId: string,
|
|
219
|
+
options?: { signal?: AbortSignal }
|
|
220
|
+
): Promise<ReadableStream<Uint8Array>> {
|
|
221
|
+
const session = await this.ensureDefaultSession();
|
|
222
|
+
return session.streamProcessLogs(processId, options);
|
|
457
223
|
}
|
|
458
224
|
|
|
459
225
|
async gitCheckout(
|
|
460
226
|
repoUrl: string,
|
|
461
227
|
options: { branch?: string; targetDir?: string }
|
|
462
228
|
) {
|
|
463
|
-
|
|
229
|
+
const session = await this.ensureDefaultSession();
|
|
230
|
+
return session.gitCheckout(repoUrl, options);
|
|
464
231
|
}
|
|
465
232
|
|
|
466
|
-
async mkdir(
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
) {
|
|
470
|
-
return this.client.mkdir(path, options.recursive);
|
|
233
|
+
async mkdir(path: string, options: { recursive?: boolean } = {}) {
|
|
234
|
+
const session = await this.ensureDefaultSession();
|
|
235
|
+
return session.mkdir(path, options);
|
|
471
236
|
}
|
|
472
237
|
|
|
473
238
|
async writeFile(
|
|
@@ -475,32 +240,39 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
|
|
|
475
240
|
content: string,
|
|
476
241
|
options: { encoding?: string } = {}
|
|
477
242
|
) {
|
|
478
|
-
|
|
243
|
+
const session = await this.ensureDefaultSession();
|
|
244
|
+
return session.writeFile(path, content, options);
|
|
479
245
|
}
|
|
480
246
|
|
|
481
247
|
async deleteFile(path: string) {
|
|
482
|
-
|
|
248
|
+
const session = await this.ensureDefaultSession();
|
|
249
|
+
return session.deleteFile(path);
|
|
483
250
|
}
|
|
484
251
|
|
|
485
|
-
async renameFile(
|
|
486
|
-
|
|
487
|
-
newPath
|
|
488
|
-
) {
|
|
489
|
-
return this.client.renameFile(oldPath, newPath);
|
|
252
|
+
async renameFile(oldPath: string, newPath: string) {
|
|
253
|
+
const session = await this.ensureDefaultSession();
|
|
254
|
+
return session.renameFile(oldPath, newPath);
|
|
490
255
|
}
|
|
491
256
|
|
|
492
|
-
async moveFile(
|
|
493
|
-
|
|
494
|
-
destinationPath
|
|
495
|
-
|
|
496
|
-
|
|
257
|
+
async moveFile(sourcePath: string, destinationPath: string) {
|
|
258
|
+
const session = await this.ensureDefaultSession();
|
|
259
|
+
return session.moveFile(sourcePath, destinationPath);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
async readFile(path: string, options: { encoding?: string } = {}) {
|
|
263
|
+
const session = await this.ensureDefaultSession();
|
|
264
|
+
return session.readFile(path, options);
|
|
497
265
|
}
|
|
498
266
|
|
|
499
|
-
async
|
|
267
|
+
async listFiles(
|
|
500
268
|
path: string,
|
|
501
|
-
options: {
|
|
269
|
+
options: {
|
|
270
|
+
recursive?: boolean;
|
|
271
|
+
includeHidden?: boolean;
|
|
272
|
+
} = {}
|
|
502
273
|
) {
|
|
503
|
-
|
|
274
|
+
const session = await this.ensureDefaultSession();
|
|
275
|
+
return session.listFiles(path, options);
|
|
504
276
|
}
|
|
505
277
|
|
|
506
278
|
async exposePort(port: number, options: { name?: string; hostname: string }) {
|
|
@@ -508,10 +280,16 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
|
|
|
508
280
|
|
|
509
281
|
// We need the sandbox name to construct preview URLs
|
|
510
282
|
if (!this.sandboxName) {
|
|
511
|
-
throw new Error(
|
|
283
|
+
throw new Error(
|
|
284
|
+
"Sandbox name not available. Ensure sandbox is accessed through getSandbox()"
|
|
285
|
+
);
|
|
512
286
|
}
|
|
513
287
|
|
|
514
|
-
const url = this.constructPreviewUrl(
|
|
288
|
+
const url = this.constructPreviewUrl(
|
|
289
|
+
port,
|
|
290
|
+
this.sandboxName,
|
|
291
|
+
options.hostname
|
|
292
|
+
);
|
|
515
293
|
|
|
516
294
|
return {
|
|
517
295
|
url,
|
|
@@ -522,17 +300,27 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
|
|
|
522
300
|
|
|
523
301
|
async unexposePort(port: number) {
|
|
524
302
|
if (!validatePort(port)) {
|
|
525
|
-
logSecurityEvent(
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
303
|
+
logSecurityEvent(
|
|
304
|
+
"INVALID_PORT_UNEXPOSE",
|
|
305
|
+
{
|
|
306
|
+
port,
|
|
307
|
+
},
|
|
308
|
+
"high"
|
|
309
|
+
);
|
|
310
|
+
throw new SecurityError(
|
|
311
|
+
`Invalid port number: ${port}. Must be between 1024-65535 and not reserved.`
|
|
312
|
+
);
|
|
529
313
|
}
|
|
530
314
|
|
|
531
315
|
await this.client.unexposePort(port);
|
|
532
316
|
|
|
533
|
-
logSecurityEvent(
|
|
534
|
-
|
|
535
|
-
|
|
317
|
+
logSecurityEvent(
|
|
318
|
+
"PORT_UNEXPOSED",
|
|
319
|
+
{
|
|
320
|
+
port,
|
|
321
|
+
},
|
|
322
|
+
"low"
|
|
323
|
+
);
|
|
536
324
|
}
|
|
537
325
|
|
|
538
326
|
async getExposedPorts(hostname: string) {
|
|
@@ -540,10 +328,12 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
|
|
|
540
328
|
|
|
541
329
|
// We need the sandbox name to construct preview URLs
|
|
542
330
|
if (!this.sandboxName) {
|
|
543
|
-
throw new Error(
|
|
331
|
+
throw new Error(
|
|
332
|
+
"Sandbox name not available. Ensure sandbox is accessed through getSandbox()"
|
|
333
|
+
);
|
|
544
334
|
}
|
|
545
335
|
|
|
546
|
-
return response.ports.map(port => ({
|
|
336
|
+
return response.ports.map((port) => ({
|
|
547
337
|
url: this.constructPreviewUrl(port.port, this.sandboxName!, hostname),
|
|
548
338
|
port: port.port,
|
|
549
339
|
name: port.name,
|
|
@@ -551,27 +341,40 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
|
|
|
551
341
|
}));
|
|
552
342
|
}
|
|
553
343
|
|
|
554
|
-
|
|
555
|
-
|
|
344
|
+
private constructPreviewUrl(
|
|
345
|
+
port: number,
|
|
346
|
+
sandboxId: string,
|
|
347
|
+
hostname: string
|
|
348
|
+
): string {
|
|
556
349
|
if (!validatePort(port)) {
|
|
557
|
-
logSecurityEvent(
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
350
|
+
logSecurityEvent(
|
|
351
|
+
"INVALID_PORT_REJECTED",
|
|
352
|
+
{
|
|
353
|
+
port,
|
|
354
|
+
sandboxId,
|
|
355
|
+
hostname,
|
|
356
|
+
},
|
|
357
|
+
"high"
|
|
358
|
+
);
|
|
359
|
+
throw new SecurityError(
|
|
360
|
+
`Invalid port number: ${port}. Must be between 1024-65535 and not reserved.`
|
|
361
|
+
);
|
|
563
362
|
}
|
|
564
363
|
|
|
565
364
|
let sanitizedSandboxId: string;
|
|
566
365
|
try {
|
|
567
366
|
sanitizedSandboxId = sanitizeSandboxId(sandboxId);
|
|
568
367
|
} catch (error) {
|
|
569
|
-
logSecurityEvent(
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
368
|
+
logSecurityEvent(
|
|
369
|
+
"INVALID_SANDBOX_ID_REJECTED",
|
|
370
|
+
{
|
|
371
|
+
sandboxId,
|
|
372
|
+
port,
|
|
373
|
+
hostname,
|
|
374
|
+
error: error instanceof Error ? error.message : "Unknown error",
|
|
375
|
+
},
|
|
376
|
+
"high"
|
|
377
|
+
);
|
|
575
378
|
throw error;
|
|
576
379
|
}
|
|
577
380
|
|
|
@@ -579,8 +382,8 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
|
|
|
579
382
|
|
|
580
383
|
if (isLocalhost) {
|
|
581
384
|
// Unified subdomain approach for localhost (RFC 6761)
|
|
582
|
-
const [host, portStr] = hostname.split(
|
|
583
|
-
const mainPort = portStr ||
|
|
385
|
+
const [host, portStr] = hostname.split(":");
|
|
386
|
+
const mainPort = portStr || "80";
|
|
584
387
|
|
|
585
388
|
// Use URL constructor for safe URL building
|
|
586
389
|
try {
|
|
@@ -591,23 +394,35 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
|
|
|
591
394
|
|
|
592
395
|
const finalUrl = baseUrl.toString();
|
|
593
396
|
|
|
594
|
-
logSecurityEvent(
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
397
|
+
logSecurityEvent(
|
|
398
|
+
"PREVIEW_URL_CONSTRUCTED",
|
|
399
|
+
{
|
|
400
|
+
port,
|
|
401
|
+
sandboxId: sanitizedSandboxId,
|
|
402
|
+
hostname,
|
|
403
|
+
resultUrl: finalUrl,
|
|
404
|
+
environment: "localhost",
|
|
405
|
+
},
|
|
406
|
+
"low"
|
|
407
|
+
);
|
|
601
408
|
|
|
602
409
|
return finalUrl;
|
|
603
410
|
} catch (error) {
|
|
604
|
-
logSecurityEvent(
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
411
|
+
logSecurityEvent(
|
|
412
|
+
"URL_CONSTRUCTION_FAILED",
|
|
413
|
+
{
|
|
414
|
+
port,
|
|
415
|
+
sandboxId: sanitizedSandboxId,
|
|
416
|
+
hostname,
|
|
417
|
+
error: error instanceof Error ? error.message : "Unknown error",
|
|
418
|
+
},
|
|
419
|
+
"high"
|
|
420
|
+
);
|
|
421
|
+
throw new SecurityError(
|
|
422
|
+
`Failed to construct preview URL: ${
|
|
423
|
+
error instanceof Error ? error.message : "Unknown error"
|
|
424
|
+
}`
|
|
425
|
+
);
|
|
611
426
|
}
|
|
612
427
|
}
|
|
613
428
|
|
|
@@ -623,23 +438,310 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
|
|
|
623
438
|
|
|
624
439
|
const finalUrl = baseUrl.toString();
|
|
625
440
|
|
|
626
|
-
logSecurityEvent(
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
441
|
+
logSecurityEvent(
|
|
442
|
+
"PREVIEW_URL_CONSTRUCTED",
|
|
443
|
+
{
|
|
444
|
+
port,
|
|
445
|
+
sandboxId: sanitizedSandboxId,
|
|
446
|
+
hostname,
|
|
447
|
+
resultUrl: finalUrl,
|
|
448
|
+
environment: "production",
|
|
449
|
+
},
|
|
450
|
+
"low"
|
|
451
|
+
);
|
|
633
452
|
|
|
634
453
|
return finalUrl;
|
|
635
454
|
} catch (error) {
|
|
636
|
-
logSecurityEvent(
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
455
|
+
logSecurityEvent(
|
|
456
|
+
"URL_CONSTRUCTION_FAILED",
|
|
457
|
+
{
|
|
458
|
+
port,
|
|
459
|
+
sandboxId: sanitizedSandboxId,
|
|
460
|
+
hostname,
|
|
461
|
+
error: error instanceof Error ? error.message : "Unknown error",
|
|
462
|
+
},
|
|
463
|
+
"high"
|
|
464
|
+
);
|
|
465
|
+
throw new SecurityError(
|
|
466
|
+
`Failed to construct preview URL: ${
|
|
467
|
+
error instanceof Error ? error.message : "Unknown error"
|
|
468
|
+
}`
|
|
469
|
+
);
|
|
643
470
|
}
|
|
644
471
|
}
|
|
472
|
+
|
|
473
|
+
// Code Interpreter Methods
|
|
474
|
+
|
|
475
|
+
/**
|
|
476
|
+
* Create a new code execution context
|
|
477
|
+
*/
|
|
478
|
+
async createCodeContext(
|
|
479
|
+
options?: CreateContextOptions
|
|
480
|
+
): Promise<CodeContext> {
|
|
481
|
+
return this.codeInterpreter.createCodeContext(options);
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
/**
|
|
485
|
+
* Run code with streaming callbacks
|
|
486
|
+
*/
|
|
487
|
+
async runCode(
|
|
488
|
+
code: string,
|
|
489
|
+
options?: RunCodeOptions
|
|
490
|
+
): Promise<ExecutionResult> {
|
|
491
|
+
const execution = await this.codeInterpreter.runCode(code, options);
|
|
492
|
+
// Convert to plain object for RPC serialization
|
|
493
|
+
return execution.toJSON();
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
/**
|
|
497
|
+
* Run code and return a streaming response
|
|
498
|
+
*/
|
|
499
|
+
async runCodeStream(
|
|
500
|
+
code: string,
|
|
501
|
+
options?: RunCodeOptions
|
|
502
|
+
): Promise<ReadableStream> {
|
|
503
|
+
return this.codeInterpreter.runCodeStream(code, options);
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
/**
|
|
507
|
+
* List all code contexts
|
|
508
|
+
*/
|
|
509
|
+
async listCodeContexts(): Promise<CodeContext[]> {
|
|
510
|
+
return this.codeInterpreter.listCodeContexts();
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
/**
|
|
514
|
+
* Delete a code context
|
|
515
|
+
*/
|
|
516
|
+
async deleteCodeContext(contextId: string): Promise<void> {
|
|
517
|
+
return this.codeInterpreter.deleteCodeContext(contextId);
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
// ============================================================================
|
|
521
|
+
// Session Management (Simple Isolation)
|
|
522
|
+
// ============================================================================
|
|
523
|
+
|
|
524
|
+
/**
|
|
525
|
+
* Create a new execution session with isolation
|
|
526
|
+
* Returns a session object with exec() method
|
|
527
|
+
*/
|
|
528
|
+
|
|
529
|
+
async createSession(options: {
|
|
530
|
+
id?: string;
|
|
531
|
+
env?: Record<string, string>;
|
|
532
|
+
cwd?: string;
|
|
533
|
+
isolation?: boolean;
|
|
534
|
+
}): Promise<ExecutionSession> {
|
|
535
|
+
const sessionId = options.id || `session-${Date.now()}`;
|
|
536
|
+
|
|
537
|
+
await this.client.createSession({
|
|
538
|
+
id: sessionId,
|
|
539
|
+
env: options.env,
|
|
540
|
+
cwd: options.cwd,
|
|
541
|
+
isolation: options.isolation
|
|
542
|
+
});
|
|
543
|
+
// Return comprehensive ExecutionSession object that implements all ISandbox methods
|
|
544
|
+
return {
|
|
545
|
+
id: sessionId,
|
|
546
|
+
|
|
547
|
+
// Command execution - clean method names
|
|
548
|
+
exec: async (command: string, options?: ExecOptions) => {
|
|
549
|
+
const result = await this.client.exec(sessionId, command);
|
|
550
|
+
return {
|
|
551
|
+
...result,
|
|
552
|
+
command,
|
|
553
|
+
duration: 0,
|
|
554
|
+
timestamp: new Date().toISOString()
|
|
555
|
+
};
|
|
556
|
+
},
|
|
557
|
+
|
|
558
|
+
execStream: async (command: string, options?: StreamOptions) => {
|
|
559
|
+
return await this.client.execStream(sessionId, command);
|
|
560
|
+
},
|
|
561
|
+
|
|
562
|
+
// Process management - route to session-aware methods
|
|
563
|
+
startProcess: async (command: string, options?: ProcessOptions) => {
|
|
564
|
+
// Use session-specific process management
|
|
565
|
+
const response = await this.client.startProcess(command, sessionId, {
|
|
566
|
+
processId: options?.processId,
|
|
567
|
+
timeout: options?.timeout,
|
|
568
|
+
env: options?.env,
|
|
569
|
+
cwd: options?.cwd,
|
|
570
|
+
encoding: options?.encoding,
|
|
571
|
+
autoCleanup: options?.autoCleanup,
|
|
572
|
+
});
|
|
573
|
+
|
|
574
|
+
// Convert response to Process object with bound methods
|
|
575
|
+
const process = response.process;
|
|
576
|
+
return {
|
|
577
|
+
id: process.id,
|
|
578
|
+
pid: process.pid,
|
|
579
|
+
command: process.command,
|
|
580
|
+
status: process.status as ProcessStatus,
|
|
581
|
+
startTime: new Date(process.startTime),
|
|
582
|
+
endTime: process.endTime ? new Date(process.endTime) : undefined,
|
|
583
|
+
exitCode: process.exitCode ?? undefined,
|
|
584
|
+
kill: async (signal?: string) => {
|
|
585
|
+
await this.client.killProcess(process.id);
|
|
586
|
+
},
|
|
587
|
+
getStatus: async () => {
|
|
588
|
+
const resp = await this.client.getProcess(process.id);
|
|
589
|
+
return resp.process?.status as ProcessStatus || "error";
|
|
590
|
+
},
|
|
591
|
+
getLogs: async () => {
|
|
592
|
+
return await this.client.getProcessLogs(process.id);
|
|
593
|
+
},
|
|
594
|
+
};
|
|
595
|
+
},
|
|
596
|
+
|
|
597
|
+
listProcesses: async () => {
|
|
598
|
+
// Get processes for this specific session
|
|
599
|
+
const response = await this.client.listProcesses(sessionId);
|
|
600
|
+
|
|
601
|
+
// Convert to Process objects with bound methods
|
|
602
|
+
return response.processes.map(p => ({
|
|
603
|
+
id: p.id,
|
|
604
|
+
pid: p.pid,
|
|
605
|
+
command: p.command,
|
|
606
|
+
status: p.status as ProcessStatus,
|
|
607
|
+
startTime: new Date(p.startTime),
|
|
608
|
+
endTime: p.endTime ? new Date(p.endTime) : undefined,
|
|
609
|
+
exitCode: p.exitCode ?? undefined,
|
|
610
|
+
kill: async (signal?: string) => {
|
|
611
|
+
await this.client.killProcess(p.id);
|
|
612
|
+
},
|
|
613
|
+
getStatus: async () => {
|
|
614
|
+
const processResp = await this.client.getProcess(p.id);
|
|
615
|
+
return processResp.process?.status as ProcessStatus || "error";
|
|
616
|
+
},
|
|
617
|
+
getLogs: async () => {
|
|
618
|
+
return this.client.getProcessLogs(p.id);
|
|
619
|
+
},
|
|
620
|
+
}));
|
|
621
|
+
},
|
|
622
|
+
|
|
623
|
+
getProcess: async (id: string) => {
|
|
624
|
+
const response = await this.client.getProcess(id);
|
|
625
|
+
if (!response.process) return null;
|
|
626
|
+
|
|
627
|
+
const p = response.process;
|
|
628
|
+
return {
|
|
629
|
+
id: p.id,
|
|
630
|
+
pid: p.pid,
|
|
631
|
+
command: p.command,
|
|
632
|
+
status: p.status as ProcessStatus,
|
|
633
|
+
startTime: new Date(p.startTime),
|
|
634
|
+
endTime: p.endTime ? new Date(p.endTime) : undefined,
|
|
635
|
+
exitCode: p.exitCode ?? undefined,
|
|
636
|
+
kill: async (signal?: string) => {
|
|
637
|
+
await this.client.killProcess(p.id);
|
|
638
|
+
},
|
|
639
|
+
getStatus: async () => {
|
|
640
|
+
const processResp = await this.client.getProcess(p.id);
|
|
641
|
+
return processResp.process?.status as ProcessStatus || "error";
|
|
642
|
+
},
|
|
643
|
+
getLogs: async () => {
|
|
644
|
+
return this.client.getProcessLogs(p.id);
|
|
645
|
+
},
|
|
646
|
+
};
|
|
647
|
+
},
|
|
648
|
+
|
|
649
|
+
killProcess: async (id: string, signal?: string) => {
|
|
650
|
+
await this.client.killProcess(id);
|
|
651
|
+
},
|
|
652
|
+
|
|
653
|
+
killAllProcesses: async () => {
|
|
654
|
+
// Kill all processes for this specific session
|
|
655
|
+
const response = await this.client.killAllProcesses(sessionId);
|
|
656
|
+
return response.killedCount;
|
|
657
|
+
},
|
|
658
|
+
|
|
659
|
+
streamProcessLogs: async (processId: string, options?: { signal?: AbortSignal }) => {
|
|
660
|
+
return await this.client.streamProcessLogs(processId, options);
|
|
661
|
+
},
|
|
662
|
+
|
|
663
|
+
getProcessLogs: async (id: string) => {
|
|
664
|
+
return await this.client.getProcessLogs(id);
|
|
665
|
+
},
|
|
666
|
+
|
|
667
|
+
cleanupCompletedProcesses: async () => {
|
|
668
|
+
// This would need a new endpoint to cleanup processes for a specific session
|
|
669
|
+
// For now, return 0 as no cleanup is performed
|
|
670
|
+
return 0;
|
|
671
|
+
},
|
|
672
|
+
|
|
673
|
+
// File operations - clean method names (no "InSession" suffix)
|
|
674
|
+
writeFile: async (path: string, content: string, options?: { encoding?: string }) => {
|
|
675
|
+
return await this.client.writeFile(path, content, options?.encoding, sessionId);
|
|
676
|
+
},
|
|
677
|
+
|
|
678
|
+
readFile: async (path: string, options?: { encoding?: string }) => {
|
|
679
|
+
return await this.client.readFile(path, options?.encoding, sessionId);
|
|
680
|
+
},
|
|
681
|
+
|
|
682
|
+
mkdir: async (path: string, options?: { recursive?: boolean }) => {
|
|
683
|
+
return await this.client.mkdir(path, options?.recursive, sessionId);
|
|
684
|
+
},
|
|
685
|
+
|
|
686
|
+
deleteFile: async (path: string) => {
|
|
687
|
+
return await this.client.deleteFile(path, sessionId);
|
|
688
|
+
},
|
|
689
|
+
|
|
690
|
+
renameFile: async (oldPath: string, newPath: string) => {
|
|
691
|
+
return await this.client.renameFile(oldPath, newPath, sessionId);
|
|
692
|
+
},
|
|
693
|
+
|
|
694
|
+
moveFile: async (sourcePath: string, destinationPath: string) => {
|
|
695
|
+
return await this.client.moveFile(sourcePath, destinationPath, sessionId);
|
|
696
|
+
},
|
|
697
|
+
|
|
698
|
+
listFiles: async (path: string, options?: { recursive?: boolean; includeHidden?: boolean }) => {
|
|
699
|
+
return await this.client.listFiles(path, sessionId, options);
|
|
700
|
+
},
|
|
701
|
+
|
|
702
|
+
gitCheckout: async (repoUrl: string, options?: { branch?: string; targetDir?: string }) => {
|
|
703
|
+
return await this.client.gitCheckout(repoUrl, sessionId, options?.branch, options?.targetDir);
|
|
704
|
+
},
|
|
705
|
+
|
|
706
|
+
// Port management
|
|
707
|
+
exposePort: async (port: number, options: { name?: string; hostname: string }) => {
|
|
708
|
+
return await this.exposePort(port, options);
|
|
709
|
+
},
|
|
710
|
+
|
|
711
|
+
unexposePort: async (port: number) => {
|
|
712
|
+
return await this.unexposePort(port);
|
|
713
|
+
},
|
|
714
|
+
|
|
715
|
+
getExposedPorts: async (hostname: string) => {
|
|
716
|
+
return await this.getExposedPorts(hostname);
|
|
717
|
+
},
|
|
718
|
+
|
|
719
|
+
// Environment management
|
|
720
|
+
setEnvVars: async (envVars: Record<string, string>) => {
|
|
721
|
+
// TODO: Implement session-specific environment updates
|
|
722
|
+
console.log(`[Session ${sessionId}] Environment variables update not yet implemented`);
|
|
723
|
+
},
|
|
724
|
+
|
|
725
|
+
// Code Interpreter API
|
|
726
|
+
createCodeContext: async (options?: any) => {
|
|
727
|
+
return await this.createCodeContext(options);
|
|
728
|
+
},
|
|
729
|
+
|
|
730
|
+
runCode: async (code: string, options?: any) => {
|
|
731
|
+
return await this.runCode(code, options);
|
|
732
|
+
},
|
|
733
|
+
|
|
734
|
+
runCodeStream: async (code: string, options?: any) => {
|
|
735
|
+
return await this.runCodeStream(code, options);
|
|
736
|
+
},
|
|
737
|
+
|
|
738
|
+
listCodeContexts: async () => {
|
|
739
|
+
return await this.listCodeContexts();
|
|
740
|
+
},
|
|
741
|
+
|
|
742
|
+
deleteCodeContext: async (contextId: string) => {
|
|
743
|
+
return await this.deleteCodeContext(contextId);
|
|
744
|
+
}
|
|
745
|
+
};
|
|
746
|
+
}
|
|
645
747
|
}
|