@cloudflare/sandbox 0.0.0-d55b0f4 → 0.0.0-d81d2a5

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.
@@ -0,0 +1,150 @@
1
+ import {
2
+ type CodeContext,
3
+ type CreateContextOptions,
4
+ Execution,
5
+ ResultImpl,
6
+ type RunCodeOptions,
7
+ } from "./interpreter-types.js";
8
+ import type { JupyterClient } from "./jupyter-client.js";
9
+ import type { Sandbox } from "./sandbox.js";
10
+
11
+ export class CodeInterpreter {
12
+ private jupyterClient: JupyterClient;
13
+ private contexts = new Map<string, CodeContext>();
14
+
15
+ constructor(sandbox: Sandbox) {
16
+ this.jupyterClient = sandbox.client as JupyterClient;
17
+ }
18
+
19
+ /**
20
+ * Create a new code execution context
21
+ */
22
+ async createCodeContext(
23
+ options: CreateContextOptions = {}
24
+ ): Promise<CodeContext> {
25
+ const context = await this.jupyterClient.createCodeContext(options);
26
+ this.contexts.set(context.id, context);
27
+ return context;
28
+ }
29
+
30
+ /**
31
+ * Run code with optional context
32
+ */
33
+ async runCode(
34
+ code: string,
35
+ options: RunCodeOptions = {}
36
+ ): Promise<Execution> {
37
+ // Get or create context
38
+ let context = options.context;
39
+ if (!context) {
40
+ // Try to find or create a default context for the language
41
+ const language = options.language || "python";
42
+ context = await this.getOrCreateDefaultContext(language);
43
+ }
44
+
45
+ // Create execution object to collect results
46
+ const execution = new Execution(code, context);
47
+
48
+ // Stream execution
49
+ await this.jupyterClient.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
+ });
67
+
68
+ return execution;
69
+ }
70
+
71
+ /**
72
+ * Run code and return a streaming response
73
+ */
74
+ async runCodeStream(
75
+ code: string,
76
+ options: RunCodeOptions = {}
77
+ ): Promise<ReadableStream> {
78
+ // Get or create context
79
+ let context = options.context;
80
+ if (!context) {
81
+ const language = options.language || "python";
82
+ context = await this.getOrCreateDefaultContext(language);
83
+ }
84
+
85
+ // Create streaming response
86
+ const response = await this.jupyterClient.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
+ });
98
+
99
+ if (!response.ok) {
100
+ const errorData = (await response
101
+ .json()
102
+ .catch(() => ({ error: "Unknown error" }))) as { error?: string };
103
+ throw new Error(
104
+ errorData.error || `Failed to execute code: ${response.status}`
105
+ );
106
+ }
107
+
108
+ if (!response.body) {
109
+ throw new Error("No response body for streaming execution");
110
+ }
111
+
112
+ return response.body;
113
+ }
114
+
115
+ /**
116
+ * List all code contexts
117
+ */
118
+ async listCodeContexts(): Promise<CodeContext[]> {
119
+ const contexts = await this.jupyterClient.listCodeContexts();
120
+
121
+ // Update local cache
122
+ for (const context of contexts) {
123
+ this.contexts.set(context.id, context);
124
+ }
125
+
126
+ return contexts;
127
+ }
128
+
129
+ /**
130
+ * Delete a code context
131
+ */
132
+ async deleteCodeContext(contextId: string): Promise<void> {
133
+ await this.jupyterClient.deleteCodeContext(contextId);
134
+ this.contexts.delete(contextId);
135
+ }
136
+
137
+ private async getOrCreateDefaultContext(
138
+ language: "python" | "javascript" | "typescript"
139
+ ): Promise<CodeContext> {
140
+ // Check if we have a cached context for this language
141
+ for (const context of this.contexts.values()) {
142
+ if (context.language === language) {
143
+ return context;
144
+ }
145
+ }
146
+
147
+ // Create new default context
148
+ return this.createCodeContext({ language });
149
+ }
150
+ }
@@ -0,0 +1,266 @@
1
+ import { HttpClient } from './client.js';
2
+ import type {
3
+ CodeContext,
4
+ CreateContextOptions,
5
+ ExecutionError,
6
+ OutputMessage,
7
+ Result
8
+ } from './interpreter-types.js';
9
+
10
+ // API Response types
11
+ interface ContextResponse {
12
+ id: string;
13
+ language: string;
14
+ cwd: string;
15
+ createdAt: string; // ISO date string from JSON
16
+ lastUsed: string; // ISO date string from JSON
17
+ }
18
+
19
+ interface ContextListResponse {
20
+ contexts: ContextResponse[];
21
+ }
22
+
23
+ interface ErrorResponse {
24
+ error: string;
25
+ }
26
+
27
+ // Streaming execution data from the server
28
+ interface StreamingExecutionData {
29
+ type: 'result' | 'stdout' | 'stderr' | 'error' | 'execution_complete';
30
+ text?: string;
31
+ html?: string;
32
+ png?: string; // base64
33
+ jpeg?: string; // base64
34
+ svg?: string;
35
+ latex?: string;
36
+ markdown?: string;
37
+ javascript?: string;
38
+ json?: any;
39
+ chart?: any;
40
+ data?: any;
41
+ metadata?: any;
42
+ execution_count?: number;
43
+ ename?: string;
44
+ evalue?: string;
45
+ traceback?: string[];
46
+ lineNumber?: number;
47
+ timestamp?: number;
48
+ }
49
+
50
+ export interface ExecutionCallbacks {
51
+ onStdout?: (output: OutputMessage) => void | Promise<void>;
52
+ onStderr?: (output: OutputMessage) => void | Promise<void>;
53
+ onResult?: (result: Result) => void | Promise<void>;
54
+ onError?: (error: ExecutionError) => void | Promise<void>;
55
+ }
56
+
57
+ export class JupyterClient extends HttpClient {
58
+ async createCodeContext(options: CreateContextOptions = {}): Promise<CodeContext> {
59
+ const response = await this.doFetch('/api/contexts', {
60
+ method: 'POST',
61
+ headers: { 'Content-Type': 'application/json' },
62
+ body: JSON.stringify({
63
+ language: options.language || 'python',
64
+ cwd: options.cwd || '/workspace',
65
+ env_vars: options.envVars
66
+ }),
67
+ });
68
+
69
+ if (!response.ok) {
70
+ const errorData = await response.json().catch(() => ({ error: 'Unknown error' })) as ErrorResponse;
71
+ throw new Error(errorData.error || `Failed to create context: ${response.status}`);
72
+ }
73
+
74
+ const data = await response.json() as ContextResponse;
75
+ return {
76
+ id: data.id,
77
+ language: data.language,
78
+ cwd: data.cwd,
79
+ createdAt: new Date(data.createdAt),
80
+ lastUsed: new Date(data.lastUsed)
81
+ };
82
+ }
83
+
84
+ async runCodeStream(
85
+ contextId: string | undefined,
86
+ code: string,
87
+ language: string | undefined,
88
+ callbacks: ExecutionCallbacks
89
+ ): Promise<void> {
90
+ const response = await this.doFetch('/api/execute/code', {
91
+ method: 'POST',
92
+ headers: {
93
+ 'Content-Type': 'application/json',
94
+ 'Accept': 'text/event-stream'
95
+ },
96
+ body: JSON.stringify({
97
+ context_id: contextId,
98
+ code,
99
+ language
100
+ }),
101
+ });
102
+
103
+ if (!response.ok) {
104
+ const errorData = await response.json().catch(() => ({ error: 'Unknown error' })) as ErrorResponse;
105
+ throw new Error(errorData.error || `Failed to execute code: ${response.status}`);
106
+ }
107
+
108
+ if (!response.body) {
109
+ throw new Error('No response body for streaming execution');
110
+ }
111
+
112
+ // Process streaming response
113
+ for await (const chunk of this.readLines(response.body)) {
114
+ await this.parseExecutionResult(chunk, callbacks);
115
+ }
116
+ }
117
+
118
+ private async *readLines(stream: ReadableStream<Uint8Array>): AsyncGenerator<string> {
119
+ const reader = stream.getReader();
120
+ let buffer = '';
121
+
122
+ try {
123
+ while (true) {
124
+ const { done, value } = await reader.read();
125
+ if (value) {
126
+ buffer += new TextDecoder().decode(value);
127
+ }
128
+ if (done) break;
129
+
130
+ let newlineIdx = buffer.indexOf('\n');
131
+ while (newlineIdx !== -1) {
132
+ yield buffer.slice(0, newlineIdx);
133
+ buffer = buffer.slice(newlineIdx + 1);
134
+ newlineIdx = buffer.indexOf('\n');
135
+ }
136
+ }
137
+
138
+ // Yield any remaining data
139
+ if (buffer.length > 0) {
140
+ yield buffer;
141
+ }
142
+ } finally {
143
+ reader.releaseLock();
144
+ }
145
+ }
146
+
147
+ private async parseExecutionResult(line: string, callbacks: ExecutionCallbacks) {
148
+ if (!line.trim()) return;
149
+
150
+ try {
151
+ const data = JSON.parse(line) as StreamingExecutionData;
152
+
153
+ switch (data.type) {
154
+ case 'stdout':
155
+ if (callbacks.onStdout && data.text) {
156
+ await callbacks.onStdout({
157
+ text: data.text,
158
+ timestamp: data.timestamp || Date.now()
159
+ });
160
+ }
161
+ break;
162
+
163
+ case 'stderr':
164
+ if (callbacks.onStderr && data.text) {
165
+ await callbacks.onStderr({
166
+ text: data.text,
167
+ timestamp: data.timestamp || Date.now()
168
+ });
169
+ }
170
+ break;
171
+
172
+ case 'result':
173
+ if (callbacks.onResult) {
174
+ // Convert raw result to Result interface
175
+ const result: Result = {
176
+ text: data.text,
177
+ html: data.html,
178
+ png: data.png,
179
+ jpeg: data.jpeg,
180
+ svg: data.svg,
181
+ latex: data.latex,
182
+ markdown: data.markdown,
183
+ javascript: data.javascript,
184
+ json: data.json,
185
+ chart: data.chart,
186
+ data: data.data,
187
+ formats: () => {
188
+ const formats: string[] = [];
189
+ if (data.text) formats.push('text');
190
+ if (data.html) formats.push('html');
191
+ if (data.png) formats.push('png');
192
+ if (data.jpeg) formats.push('jpeg');
193
+ if (data.svg) formats.push('svg');
194
+ if (data.latex) formats.push('latex');
195
+ if (data.markdown) formats.push('markdown');
196
+ if (data.javascript) formats.push('javascript');
197
+ if (data.json) formats.push('json');
198
+ if (data.chart) formats.push('chart');
199
+ return formats;
200
+ }
201
+ };
202
+ await callbacks.onResult(result);
203
+ }
204
+ break;
205
+
206
+ case 'error':
207
+ if (callbacks.onError) {
208
+ await callbacks.onError({
209
+ name: data.ename || 'Error',
210
+ value: data.evalue || data.text || 'Unknown error',
211
+ traceback: data.traceback || [],
212
+ lineNumber: data.lineNumber
213
+ });
214
+ }
215
+ break;
216
+
217
+ case 'execution_complete':
218
+ // Execution completed successfully
219
+ break;
220
+ }
221
+ } catch (error) {
222
+ console.error('[JupyterClient] Error parsing execution result:', error);
223
+ }
224
+ }
225
+
226
+ async listCodeContexts(): Promise<CodeContext[]> {
227
+ const response = await this.doFetch('/api/contexts', {
228
+ method: 'GET',
229
+ headers: { 'Content-Type': 'application/json' }
230
+ });
231
+
232
+ if (!response.ok) {
233
+ const errorData = await response.json().catch(() => ({ error: 'Unknown error' })) as ErrorResponse;
234
+ throw new Error(errorData.error || `Failed to list contexts: ${response.status}`);
235
+ }
236
+
237
+ const data = await response.json() as ContextListResponse;
238
+ return data.contexts.map((ctx) => ({
239
+ id: ctx.id,
240
+ language: ctx.language,
241
+ cwd: ctx.cwd,
242
+ createdAt: new Date(ctx.createdAt),
243
+ lastUsed: new Date(ctx.lastUsed)
244
+ }));
245
+ }
246
+
247
+ async deleteCodeContext(contextId: string): Promise<void> {
248
+ const response = await this.doFetch(`/api/contexts/${contextId}`, {
249
+ method: 'DELETE',
250
+ headers: { 'Content-Type': 'application/json' }
251
+ });
252
+
253
+ if (!response.ok) {
254
+ const errorData = await response.json().catch(() => ({ error: 'Unknown error' })) as ErrorResponse;
255
+ throw new Error(errorData.error || `Failed to delete context: ${response.status}`);
256
+ }
257
+ }
258
+
259
+ // Override parent doFetch to be public for this class
260
+ public async doFetch(
261
+ path: string,
262
+ options?: RequestInit
263
+ ): Promise<Response> {
264
+ return super.doFetch(path, options);
265
+ }
266
+ }