@cloudflare/sandbox 0.1.4 → 0.2.1

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 (53) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/Dockerfile +37 -11
  3. package/README.md +229 -5
  4. package/container_src/bun.lock +122 -0
  5. package/container_src/handler/exec.ts +4 -2
  6. package/container_src/handler/process.ts +1 -1
  7. package/container_src/index.ts +171 -1
  8. package/container_src/jupyter-server.ts +336 -0
  9. package/container_src/mime-processor.ts +255 -0
  10. package/container_src/package.json +9 -0
  11. package/container_src/startup.sh +52 -0
  12. package/dist/{chunk-YVZ3K26G.js → chunk-CUHYLCMT.js} +9 -21
  13. package/dist/chunk-CUHYLCMT.js.map +1 -0
  14. package/dist/chunk-EGC5IYXA.js +108 -0
  15. package/dist/chunk-EGC5IYXA.js.map +1 -0
  16. package/dist/chunk-FKBV7CZS.js +113 -0
  17. package/dist/chunk-FKBV7CZS.js.map +1 -0
  18. package/dist/{chunk-ZJN2PQOS.js → chunk-IATLC32Y.js} +173 -74
  19. package/dist/chunk-IATLC32Y.js.map +1 -0
  20. package/dist/{chunk-6THNBO4S.js → chunk-S5FFBU4Y.js} +1 -1
  21. package/dist/{chunk-6THNBO4S.js.map → chunk-S5FFBU4Y.js.map} +1 -1
  22. package/dist/chunk-SYMWNYWA.js +185 -0
  23. package/dist/chunk-SYMWNYWA.js.map +1 -0
  24. package/dist/{client-BXYlxy-j.d.ts → client-C7rKCYBD.d.ts} +42 -4
  25. package/dist/client.d.ts +2 -1
  26. package/dist/client.js +1 -1
  27. package/dist/index.d.ts +2 -1
  28. package/dist/index.js +10 -4
  29. package/dist/interpreter-types.d.ts +259 -0
  30. package/dist/interpreter-types.js +9 -0
  31. package/dist/interpreter-types.js.map +1 -0
  32. package/dist/interpreter.d.ts +33 -0
  33. package/dist/interpreter.js +8 -0
  34. package/dist/interpreter.js.map +1 -0
  35. package/dist/jupyter-client.d.ts +4 -0
  36. package/dist/jupyter-client.js +8 -0
  37. package/dist/jupyter-client.js.map +1 -0
  38. package/dist/request-handler.d.ts +2 -1
  39. package/dist/request-handler.js +7 -3
  40. package/dist/sandbox.d.ts +2 -1
  41. package/dist/sandbox.js +7 -3
  42. package/dist/types.d.ts +8 -0
  43. package/dist/types.js +1 -1
  44. package/package.json +1 -1
  45. package/src/client.ts +37 -54
  46. package/src/index.ts +13 -4
  47. package/src/interpreter-types.ts +383 -0
  48. package/src/interpreter.ts +150 -0
  49. package/src/jupyter-client.ts +266 -0
  50. package/src/sandbox.ts +281 -153
  51. package/src/types.ts +15 -0
  52. package/dist/chunk-YVZ3K26G.js.map +0 -1
  53. package/dist/chunk-ZJN2PQOS.js.map +0 -1
@@ -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
+ }