@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/src/errors.ts ADDED
@@ -0,0 +1,219 @@
1
+ /**
2
+ * Standard error response from the sandbox API
3
+ */
4
+ export interface SandboxErrorResponse {
5
+ error?: string;
6
+ status?: string;
7
+ progress?: number;
8
+ }
9
+
10
+ /**
11
+ * Base error class for all Sandbox-related errors
12
+ */
13
+ export class SandboxError extends Error {
14
+ constructor(message: string) {
15
+ super(message);
16
+ this.name = this.constructor.name;
17
+
18
+ // Maintains proper stack trace for where our error was thrown (only available on V8)
19
+ if (Error.captureStackTrace) {
20
+ Error.captureStackTrace(this, this.constructor);
21
+ }
22
+ }
23
+ }
24
+
25
+ /**
26
+ * Error thrown when interpreter functionality is requested but the service is still initializing.
27
+ *
28
+ * Note: With the current implementation, requests wait for interpreter to be ready.
29
+ * This error is only thrown when:
30
+ * 1. The request times out waiting for interpreter (default: 30 seconds)
31
+ * 2. interpreter initialization actually fails
32
+ *
33
+ * Most requests will succeed after a delay, not throw this error.
34
+ */
35
+ export class InterpreterNotReadyError extends SandboxError {
36
+ public readonly code = "INTERPRETER_NOT_READY";
37
+ public readonly retryAfter: number;
38
+ public readonly progress?: number;
39
+
40
+ constructor(
41
+ message?: string,
42
+ options?: { retryAfter?: number; progress?: number }
43
+ ) {
44
+ super(
45
+ message ||
46
+ "Interpreter is still initializing. Please retry in a few seconds."
47
+ );
48
+ this.retryAfter = options?.retryAfter || 5;
49
+ this.progress = options?.progress;
50
+ }
51
+ }
52
+
53
+ /**
54
+ * Error thrown when a context is not found
55
+ */
56
+ export class ContextNotFoundError extends SandboxError {
57
+ public readonly code = "CONTEXT_NOT_FOUND";
58
+ public readonly contextId: string;
59
+
60
+ constructor(contextId: string) {
61
+ super(`Context ${contextId} not found`);
62
+ this.contextId = contextId;
63
+ }
64
+ }
65
+
66
+ /**
67
+ * Error thrown when code execution fails
68
+ */
69
+ export class CodeExecutionError extends SandboxError {
70
+ public readonly code = "CODE_EXECUTION_ERROR";
71
+ public readonly executionError?: {
72
+ ename?: string;
73
+ evalue?: string;
74
+ traceback?: string[];
75
+ };
76
+
77
+ constructor(message: string, executionError?: any) {
78
+ super(message);
79
+ this.executionError = executionError;
80
+ }
81
+ }
82
+
83
+ /**
84
+ * Error thrown when the sandbox container is not ready
85
+ */
86
+ export class ContainerNotReadyError extends SandboxError {
87
+ public readonly code = "CONTAINER_NOT_READY";
88
+
89
+ constructor(message?: string) {
90
+ super(
91
+ message ||
92
+ "Container is not ready. Please wait for initialization to complete."
93
+ );
94
+ }
95
+ }
96
+
97
+ /**
98
+ * Error thrown when a network request to the sandbox fails
99
+ */
100
+ export class SandboxNetworkError extends SandboxError {
101
+ public readonly code = "NETWORK_ERROR";
102
+ public readonly statusCode?: number;
103
+ public readonly statusText?: string;
104
+
105
+ constructor(message: string, statusCode?: number, statusText?: string) {
106
+ super(message);
107
+ this.statusCode = statusCode;
108
+ this.statusText = statusText;
109
+ }
110
+ }
111
+
112
+ /**
113
+ * Error thrown when service is temporarily unavailable (e.g., circuit breaker open)
114
+ */
115
+ export class ServiceUnavailableError extends SandboxError {
116
+ public readonly code = "SERVICE_UNAVAILABLE";
117
+ public readonly retryAfter?: number;
118
+
119
+ constructor(message?: string, retryAfter?: number) {
120
+ // Simple, user-friendly message without implementation details
121
+ super(message || "Service temporarily unavailable");
122
+ this.retryAfter = retryAfter;
123
+ }
124
+ }
125
+
126
+ /**
127
+ * Type guard to check if an error is a InterpreterNotReadyError
128
+ */
129
+ export function isInterpreterNotReadyError(
130
+ error: unknown
131
+ ): error is InterpreterNotReadyError {
132
+ return error instanceof InterpreterNotReadyError;
133
+ }
134
+
135
+ /**
136
+ * Type guard to check if an error is any SandboxError
137
+ */
138
+ export function isSandboxError(error: unknown): error is SandboxError {
139
+ return error instanceof SandboxError;
140
+ }
141
+
142
+ /**
143
+ * Helper to determine if an error is retryable
144
+ */
145
+ export function isRetryableError(error: unknown): boolean {
146
+ if (
147
+ error instanceof InterpreterNotReadyError ||
148
+ error instanceof ContainerNotReadyError ||
149
+ error instanceof ServiceUnavailableError
150
+ ) {
151
+ return true;
152
+ }
153
+
154
+ if (error instanceof SandboxNetworkError) {
155
+ // Retry on 502, 503, 504 (gateway/service unavailable errors)
156
+ return error.statusCode
157
+ ? [502, 503, 504].includes(error.statusCode)
158
+ : false;
159
+ }
160
+
161
+ return false;
162
+ }
163
+
164
+ /**
165
+ * Parse error response from the sandbox API and return appropriate error instance
166
+ */
167
+ export async function parseErrorResponse(
168
+ response: Response
169
+ ): Promise<SandboxError> {
170
+ let data: SandboxErrorResponse;
171
+
172
+ try {
173
+ data = (await response.json()) as SandboxErrorResponse;
174
+ } catch {
175
+ // If JSON parsing fails, return a generic network error
176
+ return new SandboxNetworkError(
177
+ `Request failed with status ${response.status}`,
178
+ response.status,
179
+ response.statusText
180
+ );
181
+ }
182
+
183
+ // Check for specific error types based on response
184
+ if (response.status === 503) {
185
+ // Circuit breaker error
186
+ if (data.status === "circuit_open") {
187
+ return new ServiceUnavailableError(
188
+ "Service temporarily unavailable",
189
+ parseInt(response.headers.get("Retry-After") || "30")
190
+ );
191
+ }
192
+
193
+ // Interpreter initialization error
194
+ if (data.status === "initializing") {
195
+ return new InterpreterNotReadyError(data.error, {
196
+ retryAfter: parseInt(response.headers.get("Retry-After") || "5"),
197
+ progress: data.progress,
198
+ });
199
+ }
200
+ }
201
+
202
+ // Check for context not found
203
+ if (
204
+ response.status === 404 &&
205
+ data.error?.includes("Context") &&
206
+ data.error?.includes("not found")
207
+ ) {
208
+ const contextId =
209
+ data.error.match(/Context (\S+) not found/)?.[1] || "unknown";
210
+ return new ContextNotFoundError(contextId);
211
+ }
212
+
213
+ // Default network error
214
+ return new SandboxNetworkError(
215
+ data.error || `Request failed with status ${response.status}`,
216
+ response.status,
217
+ response.statusText
218
+ );
219
+ }
package/src/index.ts CHANGED
@@ -1,20 +1,75 @@
1
- // Export types from client
2
- export type {
3
- DeleteFileResponse, ExecuteResponse,
4
- GitCheckoutResponse,
5
- MkdirResponse, MoveFileResponse,
6
- ReadFileResponse, RenameFileResponse, WriteFileResponse
7
- } from "./client";
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";
8
6
 
9
- // Re-export request handler utilities
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
10
14
  export {
11
- proxyToSandbox, type RouteInfo, type SandboxEnv
12
- } from './request-handler';
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";
13
28
 
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";
14
48
  export { getSandbox, Sandbox } from "./sandbox";
15
-
16
49
  // Export SSE parser for converting ReadableStream to AsyncIterable
17
- export { asyncIterableToSSEStream, parseSSEStream, responseToAsyncIterable } from "./sse-parser";
18
-
19
- // Export event types for streaming
20
- export type { ExecEvent, LogEvent } from "./types";
50
+ export {
51
+ asyncIterableToSSEStream,
52
+ parseSSEStream,
53
+ responseToAsyncIterable,
54
+ } from "./sse-parser";
55
+ export type {
56
+ DeleteFileResponse,
57
+ ExecEvent,
58
+ ExecOptions,
59
+ ExecResult,
60
+ ExecuteResponse,
61
+ ExecutionSession,
62
+ GitCheckoutResponse,
63
+ ISandbox,
64
+ ListFilesResponse,
65
+ LogEvent,
66
+ MkdirResponse,
67
+ MoveFileResponse,
68
+ Process,
69
+ ProcessOptions,
70
+ ProcessStatus,
71
+ ReadFileResponse,
72
+ RenameFileResponse,
73
+ StreamOptions,
74
+ WriteFileResponse,
75
+ } from "./types";
@@ -0,0 +1,352 @@
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 InterpreterClient 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(
243
+ "[InterpreterClient] Error parsing execution result:",
244
+ error
245
+ );
246
+ }
247
+ }
248
+
249
+ async listCodeContexts(): Promise<CodeContext[]> {
250
+ return this.executeWithRetry(async () => {
251
+ const response = await this.doFetch("/api/contexts", {
252
+ method: "GET",
253
+ headers: { "Content-Type": "application/json" },
254
+ });
255
+
256
+ if (!response.ok) {
257
+ throw await parseErrorResponse(response);
258
+ }
259
+
260
+ const data = (await response.json()) as ContextListResponse;
261
+ return data.contexts.map((ctx) => ({
262
+ id: ctx.id,
263
+ language: ctx.language,
264
+ cwd: ctx.cwd,
265
+ createdAt: new Date(ctx.createdAt),
266
+ lastUsed: new Date(ctx.lastUsed),
267
+ }));
268
+ });
269
+ }
270
+
271
+ async deleteCodeContext(contextId: string): Promise<void> {
272
+ return this.executeWithRetry(async () => {
273
+ const response = await this.doFetch(`/api/contexts/${contextId}`, {
274
+ method: "DELETE",
275
+ headers: { "Content-Type": "application/json" },
276
+ });
277
+
278
+ if (!response.ok) {
279
+ throw await parseErrorResponse(response);
280
+ }
281
+ });
282
+ }
283
+
284
+ // Override parent doFetch to be public for this class
285
+ public async doFetch(path: string, options?: RequestInit): Promise<Response> {
286
+ return super.doFetch(path, options);
287
+ }
288
+
289
+ /**
290
+ * Execute an operation with automatic retry for transient errors
291
+ */
292
+ private async executeWithRetry<T>(operation: () => Promise<T>): Promise<T> {
293
+ let lastError: Error | undefined;
294
+
295
+ for (let attempt = 0; attempt < this.maxRetries; attempt++) {
296
+ try {
297
+ return await operation();
298
+ } catch (error) {
299
+ lastError = error as Error;
300
+
301
+ // Check if it's a retryable error (circuit breaker or interpreter not ready)
302
+ if (this.isRetryableError(error)) {
303
+ // Don't retry on the last attempt
304
+ if (attempt < this.maxRetries - 1) {
305
+ // Exponential backoff with jitter
306
+ const delay =
307
+ this.retryDelayMs * 2 ** attempt + Math.random() * 1000;
308
+ await new Promise((resolve) => setTimeout(resolve, delay));
309
+ continue;
310
+ }
311
+ }
312
+
313
+ // Non-retryable error or last attempt - throw immediately
314
+ throw error;
315
+ }
316
+ }
317
+
318
+ // All retries exhausted - throw a clean error without implementation details
319
+ if (lastError?.message.includes("Code execution")) {
320
+ // If the error already has a clean message about code execution, use it
321
+ throw lastError;
322
+ }
323
+
324
+ // Otherwise, throw a generic but user-friendly error
325
+ throw new Error("Unable to execute code at this time");
326
+ }
327
+
328
+ /**
329
+ * Check if an error is retryable
330
+ */
331
+ private isRetryableError(error: unknown): boolean {
332
+ // Use the SDK's built-in retryable check
333
+ if (isRetryableError(error)) {
334
+ return true;
335
+ }
336
+
337
+ // Also check for circuit breaker specific errors
338
+ if (error instanceof Error) {
339
+ // Circuit breaker errors (from the container's response)
340
+ if (error.message.includes("Circuit breaker is open")) {
341
+ return true;
342
+ }
343
+
344
+ // Check if error has a status property
345
+ if ("status" in error && error.status === "circuit_open") {
346
+ return true;
347
+ }
348
+ }
349
+
350
+ return false;
351
+ }
352
+ }