@cloudflare/sandbox 0.2.0 → 0.2.2
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 +12 -0
- package/Dockerfile +31 -7
- package/README.md +226 -2
- package/container_src/bun.lock +122 -0
- package/container_src/circuit-breaker.ts +121 -0
- package/container_src/index.ts +305 -10
- package/container_src/jupyter-server.ts +579 -0
- package/container_src/jupyter-service.ts +448 -0
- package/container_src/mime-processor.ts +255 -0
- package/container_src/package.json +9 -0
- package/container_src/startup.sh +83 -0
- package/dist/{chunk-YVZ3K26G.js → chunk-CUHYLCMT.js} +9 -21
- package/dist/chunk-CUHYLCMT.js.map +1 -0
- package/dist/chunk-EGC5IYXA.js +108 -0
- package/dist/chunk-EGC5IYXA.js.map +1 -0
- package/dist/chunk-FKBV7CZS.js +113 -0
- package/dist/chunk-FKBV7CZS.js.map +1 -0
- package/dist/chunk-LALY4SFU.js +129 -0
- package/dist/chunk-LALY4SFU.js.map +1 -0
- package/dist/{chunk-6THNBO4S.js → chunk-S5FFBU4Y.js} +1 -1
- package/dist/{chunk-6THNBO4S.js.map → chunk-S5FFBU4Y.js.map} +1 -1
- package/dist/chunk-VTKZL632.js +237 -0
- package/dist/chunk-VTKZL632.js.map +1 -0
- package/dist/{chunk-ZJN2PQOS.js → chunk-ZMPO44U4.js} +171 -72
- package/dist/chunk-ZMPO44U4.js.map +1 -0
- package/dist/{client-BXYlxy-j.d.ts → client-bzEV222a.d.ts} +52 -4
- package/dist/client.d.ts +2 -1
- package/dist/client.js +1 -1
- package/dist/errors.d.ts +95 -0
- package/dist/errors.js +27 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.js +33 -3
- package/dist/interpreter-types.d.ts +259 -0
- package/dist/interpreter-types.js +9 -0
- package/dist/interpreter-types.js.map +1 -0
- package/dist/interpreter.d.ts +33 -0
- package/dist/interpreter.js +8 -0
- package/dist/interpreter.js.map +1 -0
- package/dist/jupyter-client.d.ts +4 -0
- package/dist/jupyter-client.js +9 -0
- package/dist/jupyter-client.js.map +1 -0
- package/dist/request-handler.d.ts +2 -1
- package/dist/request-handler.js +8 -3
- package/dist/sandbox.d.ts +2 -1
- package/dist/sandbox.js +8 -3
- package/dist/types.d.ts +8 -0
- package/dist/types.js +1 -1
- package/package.json +1 -1
- package/src/client.ts +37 -54
- package/src/errors.ts +218 -0
- package/src/index.ts +44 -10
- package/src/interpreter-types.ts +383 -0
- package/src/interpreter.ts +150 -0
- package/src/jupyter-client.ts +349 -0
- package/src/sandbox.ts +281 -153
- package/src/types.ts +15 -0
- package/dist/chunk-YVZ3K26G.js.map +0 -1
- package/dist/chunk-ZJN2PQOS.js.map +0 -1
|
@@ -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
|
+
}
|