@cloudflare/sandbox 0.0.0-dc66e8e → 0.0.0-e1fa354

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,349 @@
1
+ import { HttpClient } from "./client.js";
2
+ import { isRetryableError, parseErrorResponse } from "./errors.js";
3
+ import type {
4
+ CodeContext,
5
+ CreateContextOptions,
6
+ ExecutionError,
7
+ OutputMessage,
8
+ Result,
9
+ } from "./interpreter-types.js";
10
+
11
+ // API Response types
12
+ interface ContextResponse {
13
+ id: string;
14
+ language: string;
15
+ cwd: string;
16
+ createdAt: string; // ISO date string from JSON
17
+ lastUsed: string; // ISO date string from JSON
18
+ }
19
+
20
+ interface ContextListResponse {
21
+ contexts: ContextResponse[];
22
+ }
23
+
24
+ // Streaming execution data from the server
25
+ interface StreamingExecutionData {
26
+ type: "result" | "stdout" | "stderr" | "error" | "execution_complete";
27
+ text?: string;
28
+ html?: string;
29
+ png?: string; // base64
30
+ jpeg?: string; // base64
31
+ svg?: string;
32
+ latex?: string;
33
+ markdown?: string;
34
+ javascript?: string;
35
+ json?: unknown;
36
+ chart?: {
37
+ type:
38
+ | "line"
39
+ | "bar"
40
+ | "scatter"
41
+ | "pie"
42
+ | "histogram"
43
+ | "heatmap"
44
+ | "unknown";
45
+ data: unknown;
46
+ options?: unknown;
47
+ };
48
+ data?: unknown;
49
+ metadata?: Record<string, unknown>;
50
+ execution_count?: number;
51
+ ename?: string;
52
+ evalue?: string;
53
+ traceback?: string[];
54
+ lineNumber?: number;
55
+ timestamp?: number;
56
+ }
57
+
58
+ export interface ExecutionCallbacks {
59
+ onStdout?: (output: OutputMessage) => void | Promise<void>;
60
+ onStderr?: (output: OutputMessage) => void | Promise<void>;
61
+ onResult?: (result: Result) => void | Promise<void>;
62
+ onError?: (error: ExecutionError) => void | Promise<void>;
63
+ }
64
+
65
+ export class JupyterClient extends HttpClient {
66
+ private readonly maxRetries = 3;
67
+ private readonly retryDelayMs = 1000;
68
+
69
+ async createCodeContext(
70
+ options: CreateContextOptions = {}
71
+ ): Promise<CodeContext> {
72
+ return this.executeWithRetry(async () => {
73
+ const response = await this.doFetch("/api/contexts", {
74
+ method: "POST",
75
+ headers: { "Content-Type": "application/json" },
76
+ body: JSON.stringify({
77
+ language: options.language || "python",
78
+ cwd: options.cwd || "/workspace",
79
+ env_vars: options.envVars,
80
+ }),
81
+ });
82
+
83
+ if (!response.ok) {
84
+ throw await parseErrorResponse(response);
85
+ }
86
+
87
+ const data = (await response.json()) as ContextResponse;
88
+ return {
89
+ id: data.id,
90
+ language: data.language,
91
+ cwd: data.cwd,
92
+ createdAt: new Date(data.createdAt),
93
+ lastUsed: new Date(data.lastUsed),
94
+ };
95
+ });
96
+ }
97
+
98
+ async runCodeStream(
99
+ contextId: string | undefined,
100
+ code: string,
101
+ language: string | undefined,
102
+ callbacks: ExecutionCallbacks
103
+ ): Promise<void> {
104
+ return this.executeWithRetry(async () => {
105
+ const response = await this.doFetch("/api/execute/code", {
106
+ method: "POST",
107
+ headers: {
108
+ "Content-Type": "application/json",
109
+ Accept: "text/event-stream",
110
+ },
111
+ body: JSON.stringify({
112
+ context_id: contextId,
113
+ code,
114
+ language,
115
+ }),
116
+ });
117
+
118
+ if (!response.ok) {
119
+ throw await parseErrorResponse(response);
120
+ }
121
+
122
+ if (!response.body) {
123
+ throw new Error("No response body for streaming execution");
124
+ }
125
+
126
+ // Process streaming response
127
+ for await (const chunk of this.readLines(response.body)) {
128
+ await this.parseExecutionResult(chunk, callbacks);
129
+ }
130
+ });
131
+ }
132
+
133
+ private async *readLines(
134
+ stream: ReadableStream<Uint8Array>
135
+ ): AsyncGenerator<string> {
136
+ const reader = stream.getReader();
137
+ let buffer = "";
138
+
139
+ try {
140
+ while (true) {
141
+ const { done, value } = await reader.read();
142
+ if (value) {
143
+ buffer += new TextDecoder().decode(value);
144
+ }
145
+ if (done) break;
146
+
147
+ let newlineIdx = buffer.indexOf("\n");
148
+ while (newlineIdx !== -1) {
149
+ yield buffer.slice(0, newlineIdx);
150
+ buffer = buffer.slice(newlineIdx + 1);
151
+ newlineIdx = buffer.indexOf("\n");
152
+ }
153
+ }
154
+
155
+ // Yield any remaining data
156
+ if (buffer.length > 0) {
157
+ yield buffer;
158
+ }
159
+ } finally {
160
+ reader.releaseLock();
161
+ }
162
+ }
163
+
164
+ private async parseExecutionResult(
165
+ line: string,
166
+ callbacks: ExecutionCallbacks
167
+ ) {
168
+ if (!line.trim()) return;
169
+
170
+ try {
171
+ const data = JSON.parse(line) as StreamingExecutionData;
172
+
173
+ switch (data.type) {
174
+ case "stdout":
175
+ if (callbacks.onStdout && data.text) {
176
+ await callbacks.onStdout({
177
+ text: data.text,
178
+ timestamp: data.timestamp || Date.now(),
179
+ });
180
+ }
181
+ break;
182
+
183
+ case "stderr":
184
+ if (callbacks.onStderr && data.text) {
185
+ await callbacks.onStderr({
186
+ text: data.text,
187
+ timestamp: data.timestamp || Date.now(),
188
+ });
189
+ }
190
+ break;
191
+
192
+ case "result":
193
+ if (callbacks.onResult) {
194
+ // Convert raw result to Result interface
195
+ const result: Result = {
196
+ text: data.text,
197
+ html: data.html,
198
+ png: data.png,
199
+ jpeg: data.jpeg,
200
+ svg: data.svg,
201
+ latex: data.latex,
202
+ markdown: data.markdown,
203
+ javascript: data.javascript,
204
+ json: data.json,
205
+ chart: data.chart,
206
+ data: data.data,
207
+ formats: () => {
208
+ const formats: string[] = [];
209
+ if (data.text) formats.push("text");
210
+ if (data.html) formats.push("html");
211
+ if (data.png) formats.push("png");
212
+ if (data.jpeg) formats.push("jpeg");
213
+ if (data.svg) formats.push("svg");
214
+ if (data.latex) formats.push("latex");
215
+ if (data.markdown) formats.push("markdown");
216
+ if (data.javascript) formats.push("javascript");
217
+ if (data.json) formats.push("json");
218
+ if (data.chart) formats.push("chart");
219
+ return formats;
220
+ },
221
+ };
222
+ await callbacks.onResult(result);
223
+ }
224
+ break;
225
+
226
+ case "error":
227
+ if (callbacks.onError) {
228
+ await callbacks.onError({
229
+ name: data.ename || "Error",
230
+ value: data.evalue || data.text || "Unknown error",
231
+ traceback: data.traceback || [],
232
+ lineNumber: data.lineNumber,
233
+ });
234
+ }
235
+ break;
236
+
237
+ case "execution_complete":
238
+ // Execution completed successfully
239
+ break;
240
+ }
241
+ } catch (error) {
242
+ console.error("[JupyterClient] Error parsing execution result:", error);
243
+ }
244
+ }
245
+
246
+ async listCodeContexts(): Promise<CodeContext[]> {
247
+ return this.executeWithRetry(async () => {
248
+ const response = await this.doFetch("/api/contexts", {
249
+ method: "GET",
250
+ headers: { "Content-Type": "application/json" },
251
+ });
252
+
253
+ if (!response.ok) {
254
+ throw await parseErrorResponse(response);
255
+ }
256
+
257
+ const data = (await response.json()) as ContextListResponse;
258
+ return data.contexts.map((ctx) => ({
259
+ id: ctx.id,
260
+ language: ctx.language,
261
+ cwd: ctx.cwd,
262
+ createdAt: new Date(ctx.createdAt),
263
+ lastUsed: new Date(ctx.lastUsed),
264
+ }));
265
+ });
266
+ }
267
+
268
+ async deleteCodeContext(contextId: string): Promise<void> {
269
+ return this.executeWithRetry(async () => {
270
+ const response = await this.doFetch(`/api/contexts/${contextId}`, {
271
+ method: "DELETE",
272
+ headers: { "Content-Type": "application/json" },
273
+ });
274
+
275
+ if (!response.ok) {
276
+ throw await parseErrorResponse(response);
277
+ }
278
+ });
279
+ }
280
+
281
+ // Override parent doFetch to be public for this class
282
+ public async doFetch(path: string, options?: RequestInit): Promise<Response> {
283
+ return super.doFetch(path, options);
284
+ }
285
+
286
+ /**
287
+ * Execute an operation with automatic retry for transient errors
288
+ */
289
+ private async executeWithRetry<T>(operation: () => Promise<T>): Promise<T> {
290
+ let lastError: Error | undefined;
291
+
292
+ for (let attempt = 0; attempt < this.maxRetries; attempt++) {
293
+ try {
294
+ return await operation();
295
+ } catch (error) {
296
+ lastError = error as Error;
297
+
298
+ // Check if it's a retryable error (circuit breaker or Jupyter not ready)
299
+ if (this.isRetryableError(error)) {
300
+ // Don't retry on the last attempt
301
+ if (attempt < this.maxRetries - 1) {
302
+ // Exponential backoff with jitter
303
+ const delay =
304
+ this.retryDelayMs * 2 ** attempt + Math.random() * 1000;
305
+ await new Promise((resolve) => setTimeout(resolve, delay));
306
+ continue;
307
+ }
308
+ }
309
+
310
+ // Non-retryable error or last attempt - throw immediately
311
+ throw error;
312
+ }
313
+ }
314
+
315
+ // All retries exhausted - throw a clean error without implementation details
316
+ if (lastError?.message.includes("Code execution")) {
317
+ // If the error already has a clean message about code execution, use it
318
+ throw lastError;
319
+ }
320
+
321
+ // Otherwise, throw a generic but user-friendly error
322
+ throw new Error("Unable to execute code at this time");
323
+ }
324
+
325
+ /**
326
+ * Check if an error is retryable
327
+ */
328
+ private isRetryableError(error: unknown): boolean {
329
+ // Use the SDK's built-in retryable check
330
+ if (isRetryableError(error)) {
331
+ return true;
332
+ }
333
+
334
+ // Also check for circuit breaker specific errors
335
+ if (error instanceof Error) {
336
+ // Circuit breaker errors (from the container's response)
337
+ if (error.message.includes("Circuit breaker is open")) {
338
+ return true;
339
+ }
340
+
341
+ // Check if error has a status property
342
+ if ("status" in error && error.status === "circuit_open") {
343
+ return true;
344
+ }
345
+ }
346
+
347
+ return false;
348
+ }
349
+ }