@cloudflare/sandbox 0.0.0-1be7d53 → 0.0.0-1bf3576

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.
Files changed (74) hide show
  1. package/CHANGELOG.md +91 -6
  2. package/Dockerfile +91 -51
  3. package/README.md +87 -825
  4. package/dist/index.d.ts +1907 -0
  5. package/dist/index.d.ts.map +1 -0
  6. package/dist/index.js +3159 -0
  7. package/dist/index.js.map +1 -0
  8. package/package.json +16 -8
  9. package/src/clients/base-client.ts +295 -0
  10. package/src/clients/command-client.ts +115 -0
  11. package/src/clients/file-client.ts +300 -0
  12. package/src/clients/git-client.ts +91 -0
  13. package/src/clients/index.ts +60 -0
  14. package/src/clients/interpreter-client.ts +333 -0
  15. package/src/clients/port-client.ts +105 -0
  16. package/src/clients/process-client.ts +180 -0
  17. package/src/clients/sandbox-client.ts +39 -0
  18. package/src/clients/types.ts +88 -0
  19. package/src/clients/utility-client.ts +123 -0
  20. package/src/errors/adapter.ts +238 -0
  21. package/src/errors/classes.ts +594 -0
  22. package/src/errors/index.ts +109 -0
  23. package/src/file-stream.ts +123 -116
  24. package/src/index.ts +85 -66
  25. package/src/interpreter.ts +58 -40
  26. package/src/request-handler.ts +94 -55
  27. package/src/sandbox.ts +989 -498
  28. package/src/security.ts +34 -28
  29. package/src/sse-parser.ts +8 -11
  30. package/src/version.ts +6 -0
  31. package/startup.sh +3 -0
  32. package/tests/base-client.test.ts +364 -0
  33. package/tests/command-client.test.ts +444 -0
  34. package/tests/file-client.test.ts +831 -0
  35. package/tests/file-stream.test.ts +310 -0
  36. package/tests/get-sandbox.test.ts +149 -0
  37. package/tests/git-client.test.ts +415 -0
  38. package/tests/port-client.test.ts +293 -0
  39. package/tests/process-client.test.ts +683 -0
  40. package/tests/request-handler.test.ts +292 -0
  41. package/tests/sandbox.test.ts +702 -0
  42. package/tests/sse-parser.test.ts +291 -0
  43. package/tests/utility-client.test.ts +339 -0
  44. package/tests/version.test.ts +16 -0
  45. package/tests/wrangler.jsonc +35 -0
  46. package/tsconfig.json +9 -1
  47. package/tsdown.config.ts +12 -0
  48. package/vitest.config.ts +31 -0
  49. package/container_src/bun.lock +0 -76
  50. package/container_src/circuit-breaker.ts +0 -121
  51. package/container_src/control-process.ts +0 -784
  52. package/container_src/handler/exec.ts +0 -185
  53. package/container_src/handler/file.ts +0 -457
  54. package/container_src/handler/git.ts +0 -130
  55. package/container_src/handler/ports.ts +0 -314
  56. package/container_src/handler/process.ts +0 -568
  57. package/container_src/handler/session.ts +0 -92
  58. package/container_src/index.ts +0 -601
  59. package/container_src/interpreter-service.ts +0 -276
  60. package/container_src/isolation.ts +0 -1213
  61. package/container_src/mime-processor.ts +0 -255
  62. package/container_src/package.json +0 -18
  63. package/container_src/runtime/executors/javascript/node_executor.ts +0 -123
  64. package/container_src/runtime/executors/python/ipython_executor.py +0 -338
  65. package/container_src/runtime/executors/typescript/ts_executor.ts +0 -138
  66. package/container_src/runtime/process-pool.ts +0 -464
  67. package/container_src/shell-escape.ts +0 -42
  68. package/container_src/startup.sh +0 -11
  69. package/container_src/types.ts +0 -131
  70. package/src/client.ts +0 -1048
  71. package/src/errors.ts +0 -219
  72. package/src/interpreter-client.ts +0 -352
  73. package/src/interpreter-types.ts +0 -390
  74. package/src/types.ts +0 -571
@@ -1,162 +1,169 @@
1
+ import type { FileChunk, FileMetadata, FileStreamEvent } from '@repo/shared';
2
+
1
3
  /**
2
- * File streaming utilities for reading binary and text files
3
- * Provides simple AsyncIterable API over SSE stream with automatic base64 decoding
4
+ * Parse SSE (Server-Sent Events) lines from a stream
4
5
  */
6
+ async function* parseSSE(
7
+ stream: ReadableStream<Uint8Array>
8
+ ): AsyncGenerator<FileStreamEvent> {
9
+ const reader = stream.getReader();
10
+ const decoder = new TextDecoder();
11
+ let buffer = '';
12
+
13
+ try {
14
+ while (true) {
15
+ const { done, value } = await reader.read();
16
+
17
+ if (done) {
18
+ break;
19
+ }
20
+
21
+ buffer += decoder.decode(value, { stream: true });
22
+ const lines = buffer.split('\n');
5
23
 
6
- import { parseSSEStream } from './sse-parser';
7
- import type { FileChunk, FileMetadata, FileStreamEvent } from './types';
24
+ // Keep the last incomplete line in the buffer
25
+ buffer = lines.pop() || '';
26
+
27
+ for (const line of lines) {
28
+ if (line.startsWith('data: ')) {
29
+ const data = line.slice(6); // Remove 'data: ' prefix
30
+ try {
31
+ const event = JSON.parse(data) as FileStreamEvent;
32
+ yield event;
33
+ } catch {
34
+ // Skip invalid JSON events and continue processing
35
+ }
36
+ }
37
+ }
38
+ }
39
+ } finally {
40
+ reader.releaseLock();
41
+ }
42
+ }
8
43
 
9
44
  /**
10
- * Convert ReadableStream of SSE file events to AsyncIterable of file chunks
11
- * Automatically decodes base64 for binary files and provides metadata
45
+ * Stream a file from the sandbox with automatic base64 decoding for binary files
12
46
  *
13
- * @param stream - The SSE ReadableStream from readFileStream()
14
- * @param signal - Optional AbortSignal for cancellation
15
- * @returns AsyncIterable that yields file chunks (string for text, Uint8Array for binary)
47
+ * @param stream - The ReadableStream from readFileStream()
48
+ * @returns AsyncGenerator that yields FileChunk (string for text, Uint8Array for binary)
16
49
  *
17
50
  * @example
18
- * ```typescript
51
+ * ```ts
19
52
  * const stream = await sandbox.readFileStream('/path/to/file.png');
20
- *
21
53
  * for await (const chunk of streamFile(stream)) {
22
54
  * if (chunk instanceof Uint8Array) {
23
- * // Binary chunk - already decoded from base64
24
- * console.log('Binary chunk:', chunk.byteLength, 'bytes');
55
+ * // Binary chunk
56
+ * console.log('Binary chunk:', chunk.length, 'bytes');
25
57
  * } else {
26
58
  * // Text chunk
27
59
  * console.log('Text chunk:', chunk);
28
60
  * }
29
61
  * }
30
- *
31
- * // Access metadata
32
- * const iter = streamFile(stream);
33
- * for await (const chunk of iter) {
34
- * console.log('MIME type:', iter.metadata?.mimeType);
35
- * // process chunk...
36
- * }
37
62
  * ```
38
63
  */
39
64
  export async function* streamFile(
40
- stream: ReadableStream<Uint8Array>,
41
- signal?: AbortSignal
42
- ): AsyncGenerator<FileChunk, void, undefined> {
43
- let metadata: FileMetadata | undefined;
65
+ stream: ReadableStream<Uint8Array>
66
+ ): AsyncGenerator<FileChunk, FileMetadata> {
67
+ let metadata: FileMetadata | null = null;
44
68
 
45
- try {
46
- for await (const event of parseSSEStream<FileStreamEvent>(stream, signal)) {
47
- switch (event.type) {
48
- case 'metadata':
49
- // Store metadata for access via iterator
50
- metadata = {
51
- mimeType: event.mimeType,
52
- size: event.size,
53
- isBinary: event.isBinary,
54
- encoding: event.encoding,
55
- };
56
- // Store on generator function for external access
57
- (streamFile as any).metadata = metadata;
58
- break;
59
-
60
- case 'chunk':
61
- // Auto-decode base64 for binary files
62
- if (metadata?.isBinary && metadata?.encoding === 'base64') {
63
- // Decode base64 to Uint8Array
64
- const binaryString = atob(event.data);
65
- const bytes = new Uint8Array(binaryString.length);
66
- for (let i = 0; i < binaryString.length; i++) {
67
- bytes[i] = binaryString.charCodeAt(i);
68
- }
69
- yield bytes;
70
- } else {
71
- // Text file - yield as-is
72
- yield event.data;
69
+ for await (const event of parseSSE(stream)) {
70
+ switch (event.type) {
71
+ case 'metadata':
72
+ metadata = {
73
+ mimeType: event.mimeType,
74
+ size: event.size,
75
+ isBinary: event.isBinary,
76
+ encoding: event.encoding
77
+ };
78
+ break;
79
+
80
+ case 'chunk':
81
+ if (!metadata) {
82
+ throw new Error('Received chunk before metadata');
83
+ }
84
+
85
+ if (metadata.isBinary && metadata.encoding === 'base64') {
86
+ // Decode base64 to Uint8Array for binary files
87
+ const binaryString = atob(event.data);
88
+ const bytes = new Uint8Array(binaryString.length);
89
+ for (let i = 0; i < binaryString.length; i++) {
90
+ bytes[i] = binaryString.charCodeAt(i);
73
91
  }
74
- break;
92
+ yield bytes;
93
+ } else {
94
+ // Text files - yield as-is
95
+ yield event.data;
96
+ }
97
+ break;
75
98
 
76
- case 'complete':
77
- // Stream completed successfully
78
- console.log(`[streamFile] File streaming complete: ${event.bytesRead} bytes read`);
79
- return;
99
+ case 'complete':
100
+ if (!metadata) {
101
+ throw new Error('Stream completed without metadata');
102
+ }
103
+ return metadata;
80
104
 
81
- case 'error':
82
- // Stream error
83
- throw new Error(`File streaming error: ${event.error}`);
84
- }
105
+ case 'error':
106
+ throw new Error(`File streaming error: ${event.error}`);
85
107
  }
86
- } catch (error) {
87
- console.error('[streamFile] Error streaming file:', error);
88
- throw error;
89
108
  }
109
+
110
+ throw new Error('Stream ended unexpectedly');
90
111
  }
91
112
 
92
113
  /**
93
- * Helper to collect entire file from stream into memory
94
- * Useful for smaller files where you want the complete content at once
114
+ * Collect an entire file into memory from a stream
95
115
  *
96
- * @param stream - The SSE ReadableStream from readFileStream()
97
- * @param signal - Optional AbortSignal for cancellation
98
- * @returns Object with content (string or Uint8Array) and metadata
116
+ * @param stream - The ReadableStream from readFileStream()
117
+ * @returns Object containing the file content and metadata
99
118
  *
100
119
  * @example
101
- * ```typescript
102
- * const stream = await sandbox.readFileStream('/path/to/image.png');
120
+ * ```ts
121
+ * const stream = await sandbox.readFileStream('/path/to/file.txt');
103
122
  * const { content, metadata } = await collectFile(stream);
104
- *
105
- * if (content instanceof Uint8Array) {
106
- * console.log('Binary file:', metadata.mimeType, content.byteLength, 'bytes');
107
- * } else {
108
- * console.log('Text file:', metadata.mimeType, content.length, 'chars');
109
- * }
123
+ * console.log('Content:', content);
124
+ * console.log('MIME type:', metadata.mimeType);
110
125
  * ```
111
126
  */
112
- export async function collectFile(
113
- stream: ReadableStream<Uint8Array>,
114
- signal?: AbortSignal
115
- ): Promise<{ content: string | Uint8Array; metadata: FileMetadata }> {
116
- let metadata: FileMetadata | undefined;
117
- const chunks: FileChunk[] = [];
118
-
119
- for await (const chunk of streamFile(stream, signal)) {
120
- chunks.push(chunk);
121
- // Capture metadata from first iteration
122
- if (!metadata && (streamFile as any).metadata) {
123
- metadata = (streamFile as any).metadata;
124
- }
127
+ export async function collectFile(stream: ReadableStream<Uint8Array>): Promise<{
128
+ content: string | Uint8Array;
129
+ metadata: FileMetadata;
130
+ }> {
131
+ const chunks: Array<string | Uint8Array> = [];
132
+
133
+ // Iterate through the generator and get the return value (metadata)
134
+ const generator = streamFile(stream);
135
+ let result = await generator.next();
136
+
137
+ while (!result.done) {
138
+ chunks.push(result.value);
139
+ result = await generator.next();
125
140
  }
126
141
 
142
+ const metadata = result.value;
143
+
127
144
  if (!metadata) {
128
- throw new Error('No metadata received from file stream');
145
+ throw new Error('Failed to get file metadata');
129
146
  }
130
147
 
131
148
  // Combine chunks based on type
132
- if (chunks.length === 0) {
133
- // Empty file
134
- return {
135
- content: metadata.isBinary ? new Uint8Array(0) : '',
136
- metadata,
137
- };
138
- }
139
-
140
- // Check if binary or text based on first chunk
141
- if (chunks[0] instanceof Uint8Array) {
142
- // Binary file - concatenate Uint8Arrays
143
- const totalLength = chunks.reduce((sum, chunk) => {
144
- return sum + (chunk as Uint8Array).byteLength;
145
- }, 0);
146
-
147
- const result = new Uint8Array(totalLength);
149
+ if (metadata.isBinary) {
150
+ // Binary file - combine Uint8Arrays
151
+ const totalLength = chunks.reduce(
152
+ (sum, chunk) => sum + (chunk instanceof Uint8Array ? chunk.length : 0),
153
+ 0
154
+ );
155
+ const combined = new Uint8Array(totalLength);
148
156
  let offset = 0;
149
157
  for (const chunk of chunks) {
150
- result.set(chunk as Uint8Array, offset);
151
- offset += (chunk as Uint8Array).byteLength;
158
+ if (chunk instanceof Uint8Array) {
159
+ combined.set(chunk, offset);
160
+ offset += chunk.length;
161
+ }
152
162
  }
153
-
154
- return { content: result, metadata };
163
+ return { content: combined, metadata };
155
164
  } else {
156
- // Text file - concatenate strings
157
- return {
158
- content: chunks.join(''),
159
- metadata,
160
- };
165
+ // Text file - combine strings
166
+ const combined = chunks.filter((c) => typeof c === 'string').join('');
167
+ return { content: combined, metadata };
161
168
  }
162
169
  }
package/src/index.ts CHANGED
@@ -1,81 +1,100 @@
1
- // biome-ignore-start assist/source/organizeImports: Need separate exports for deprecation warnings to work properly
2
- /**
3
- * @deprecated Use `InterpreterNotReadyError` instead. Will be removed in a future version.
4
- */
5
- export { InterpreterNotReadyError as JupyterNotReadyError } from "./errors";
1
+ // Export the main Sandbox class and utilities
6
2
 
7
- /**
8
- * @deprecated Use `isInterpreterNotReadyError` instead. Will be removed in a future version.
9
- */
10
- export { isInterpreterNotReadyError as isJupyterNotReadyError } from "./errors";
11
- // biome-ignore-end assist/source/organizeImports: Need separate exports for deprecation warnings to work properly
12
-
13
- // Export API response types
3
+ // Export the new client architecture
14
4
  export {
15
- CodeExecutionError,
16
- ContainerNotReadyError,
17
- ContextNotFoundError,
18
- InterpreterNotReadyError,
19
- isInterpreterNotReadyError,
20
- isRetryableError,
21
- isSandboxError,
22
- parseErrorResponse,
23
- SandboxError,
24
- type SandboxErrorResponse,
25
- SandboxNetworkError,
26
- ServiceUnavailableError,
27
- } from "./errors";
5
+ CommandClient,
6
+ FileClient,
7
+ GitClient,
8
+ PortClient,
9
+ ProcessClient,
10
+ SandboxClient,
11
+ UtilityClient
12
+ } from './clients';
13
+ export { connect, getSandbox, Sandbox } from './sandbox';
28
14
 
29
- // Export code interpreter types
30
- export type {
31
- ChartData,
32
- CodeContext,
33
- CreateContextOptions,
34
- Execution,
35
- ExecutionError,
36
- OutputMessage,
37
- Result,
38
- RunCodeOptions,
39
- } from "./interpreter-types";
40
- // Export the implementations
41
- export { ResultImpl } from "./interpreter-types";
42
- // Re-export request handler utilities
43
- export {
44
- proxyToSandbox,
45
- type RouteInfo,
46
- type SandboxEnv,
47
- } from "./request-handler";
48
- export { getSandbox, Sandbox } from "./sandbox";
49
- // Export SSE parser for converting ReadableStream to AsyncIterable
50
- export {
51
- asyncIterableToSSEStream,
52
- parseSSEStream,
53
- responseToAsyncIterable,
54
- } from "./sse-parser";
55
- // Export file streaming utilities
56
- export { streamFile, collectFile } from "./file-stream";
15
+ // Legacy types are now imported from the new client architecture
16
+
17
+ // Export core SDK types for consumers
57
18
  export type {
58
- DeleteFileResponse,
19
+ BaseExecOptions,
59
20
  ExecEvent,
60
21
  ExecOptions,
61
22
  ExecResult,
62
- ExecuteResponse,
63
- ExecutionSession,
64
23
  FileChunk,
65
24
  FileMetadata,
66
- FileStream,
67
25
  FileStreamEvent,
68
- GitCheckoutResponse,
69
26
  ISandbox,
70
- ListFilesResponse,
71
27
  LogEvent,
72
- MkdirResponse,
73
- MoveFileResponse,
74
28
  Process,
75
29
  ProcessOptions,
76
30
  ProcessStatus,
77
- ReadFileResponse,
78
- RenameFileResponse,
79
- StreamOptions,
80
- WriteFileResponse,
81
- } from "./types";
31
+ StreamOptions
32
+ } from '@repo/shared';
33
+ export * from '@repo/shared';
34
+ // Export type guards for runtime validation
35
+ export { isExecResult, isProcess, isProcessStatus } from '@repo/shared';
36
+ // Export all client types from new architecture
37
+ export type {
38
+ BaseApiResponse,
39
+ CommandsResponse,
40
+ ContainerStub,
41
+ ErrorResponse,
42
+
43
+ // Command client types
44
+ ExecuteRequest,
45
+ ExecuteResponse as CommandExecuteResponse,
46
+
47
+ // Port client types
48
+ ExposePortRequest,
49
+ FileOperationRequest,
50
+
51
+ // Git client types
52
+ GitCheckoutRequest,
53
+ GitCheckoutResult,
54
+ // Base client types
55
+ HttpClientOptions as SandboxClientOptions,
56
+
57
+ // File client types
58
+ MkdirRequest,
59
+
60
+ // Utility client types
61
+ PingResponse,
62
+ PortCloseResult,
63
+ PortExposeResult,
64
+ PortListResult,
65
+ ProcessCleanupResult,
66
+ ProcessInfoResult,
67
+ ProcessKillResult,
68
+ ProcessListResult,
69
+ ProcessLogsResult,
70
+ ProcessStartResult,
71
+ ReadFileRequest,
72
+ RequestConfig,
73
+ ResponseHandler,
74
+ SessionRequest,
75
+
76
+ // Process client types
77
+ StartProcessRequest,
78
+ UnexposePortRequest,
79
+ WriteFileRequest
80
+ } from './clients';
81
+ export type {
82
+ ExecutionCallbacks,
83
+ InterpreterClient
84
+ } from './clients/interpreter-client.js';
85
+ // Export file streaming utilities for binary file support
86
+ export { collectFile, streamFile } from './file-stream';
87
+ // Export interpreter functionality
88
+ export { CodeInterpreter } from './interpreter.js';
89
+ // Re-export request handler utilities
90
+ export {
91
+ proxyToSandbox,
92
+ type RouteInfo,
93
+ type SandboxEnv
94
+ } from './request-handler';
95
+ // Export SSE parser for converting ReadableStream to AsyncIterable
96
+ export {
97
+ asyncIterableToSSEStream,
98
+ parseSSEStream,
99
+ responseToAsyncIterable
100
+ } from './sse-parser';
@@ -1,19 +1,25 @@
1
- import type { InterpreterClient } from "./interpreter-client.js";
2
1
  import {
3
2
  type CodeContext,
4
3
  type CreateContextOptions,
5
4
  Execution,
5
+ type ExecutionError,
6
+ type OutputMessage,
7
+ type Result,
6
8
  ResultImpl,
7
- type RunCodeOptions,
8
- } from "./interpreter-types.js";
9
- import type { Sandbox } from "./sandbox.js";
9
+ type RunCodeOptions
10
+ } from '@repo/shared';
11
+ import type { InterpreterClient } from './clients/interpreter-client.js';
12
+ import type { Sandbox } from './sandbox.js';
13
+ import { validateLanguage } from './security.js';
10
14
 
11
15
  export class CodeInterpreter {
12
16
  private interpreterClient: InterpreterClient;
13
17
  private contexts = new Map<string, CodeContext>();
14
18
 
15
19
  constructor(sandbox: Sandbox) {
16
- this.interpreterClient = sandbox.client as InterpreterClient;
20
+ // In init-testing architecture, client is a SandboxClient with an interpreter property
21
+ this.interpreterClient = (sandbox.client as any)
22
+ .interpreter as InterpreterClient;
17
23
  }
18
24
 
19
25
  /**
@@ -22,6 +28,9 @@ export class CodeInterpreter {
22
28
  async createCodeContext(
23
29
  options: CreateContextOptions = {}
24
30
  ): Promise<CodeContext> {
31
+ // Validate language before sending to container
32
+ validateLanguage(options.language);
33
+
25
34
  const context = await this.interpreterClient.createCodeContext(options);
26
35
  this.contexts.set(context.id, context);
27
36
  return context;
@@ -38,7 +47,7 @@ export class CodeInterpreter {
38
47
  let context = options.context;
39
48
  if (!context) {
40
49
  // Try to find or create a default context for the language
41
- const language = options.language || "python";
50
+ const language = options.language || 'python';
42
51
  context = await this.getOrCreateDefaultContext(language);
43
52
  }
44
53
 
@@ -46,24 +55,29 @@ export class CodeInterpreter {
46
55
  const execution = new Execution(code, context);
47
56
 
48
57
  // Stream execution
49
- await this.interpreterClient.runCodeStream(context.id, code, options.language, {
50
- onStdout: (output) => {
51
- execution.logs.stdout.push(output.text);
52
- if (options.onStdout) return options.onStdout(output);
53
- },
54
- onStderr: (output) => {
55
- execution.logs.stderr.push(output.text);
56
- if (options.onStderr) return options.onStderr(output);
57
- },
58
- onResult: async (result) => {
59
- execution.results.push(new ResultImpl(result) as any);
60
- if (options.onResult) return options.onResult(result);
61
- },
62
- onError: (error) => {
63
- execution.error = error;
64
- if (options.onError) return options.onError(error);
65
- },
66
- });
58
+ await this.interpreterClient.runCodeStream(
59
+ context.id,
60
+ code,
61
+ options.language,
62
+ {
63
+ onStdout: (output: OutputMessage) => {
64
+ execution.logs.stdout.push(output.text);
65
+ if (options.onStdout) return options.onStdout(output);
66
+ },
67
+ onStderr: (output: OutputMessage) => {
68
+ execution.logs.stderr.push(output.text);
69
+ if (options.onStderr) return options.onStderr(output);
70
+ },
71
+ onResult: async (result: Result) => {
72
+ execution.results.push(new ResultImpl(result) as any);
73
+ if (options.onResult) return options.onResult(result);
74
+ },
75
+ onError: (error: ExecutionError) => {
76
+ execution.error = error;
77
+ if (options.onError) return options.onError(error);
78
+ }
79
+ }
80
+ );
67
81
 
68
82
  return execution;
69
83
  }
@@ -78,35 +92,39 @@ export class CodeInterpreter {
78
92
  // Get or create context
79
93
  let context = options.context;
80
94
  if (!context) {
81
- const language = options.language || "python";
95
+ const language = options.language || 'python';
82
96
  context = await this.getOrCreateDefaultContext(language);
83
97
  }
84
98
 
85
99
  // Create streaming response
86
- const response = await this.interpreterClient.doFetch("/api/execute/code", {
87
- method: "POST",
88
- headers: {
89
- "Content-Type": "application/json",
90
- Accept: "text/event-stream",
91
- },
92
- body: JSON.stringify({
93
- context_id: context.id,
94
- code,
95
- language: options.language,
96
- }),
97
- });
100
+ // Note: doFetch is protected but we need direct access for raw stream response
101
+ const response = await (this.interpreterClient as any).doFetch(
102
+ '/api/execute/code',
103
+ {
104
+ method: 'POST',
105
+ headers: {
106
+ 'Content-Type': 'application/json',
107
+ Accept: 'text/event-stream'
108
+ },
109
+ body: JSON.stringify({
110
+ context_id: context.id,
111
+ code,
112
+ language: options.language
113
+ })
114
+ }
115
+ );
98
116
 
99
117
  if (!response.ok) {
100
118
  const errorData = (await response
101
119
  .json()
102
- .catch(() => ({ error: "Unknown error" }))) as { error?: string };
120
+ .catch(() => ({ error: 'Unknown error' }))) as { error?: string };
103
121
  throw new Error(
104
122
  errorData.error || `Failed to execute code: ${response.status}`
105
123
  );
106
124
  }
107
125
 
108
126
  if (!response.body) {
109
- throw new Error("No response body for streaming execution");
127
+ throw new Error('No response body for streaming execution');
110
128
  }
111
129
 
112
130
  return response.body;
@@ -135,7 +153,7 @@ export class CodeInterpreter {
135
153
  }
136
154
 
137
155
  private async getOrCreateDefaultContext(
138
- language: "python" | "javascript" | "typescript"
156
+ language: 'python' | 'javascript' | 'typescript'
139
157
  ): Promise<CodeContext> {
140
158
  // Check if we have a cached context for this language
141
159
  for (const context of this.contexts.values()) {