@cloudflare/sandbox 0.0.0-bb855ca → 0.0.0-c1f057e
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 +159 -0
- package/Dockerfile +107 -66
- package/README.md +88 -710
- package/dist/chunk-BFVUNTP4.js +104 -0
- package/dist/chunk-BFVUNTP4.js.map +1 -0
- package/dist/chunk-EKSWCBCA.js +86 -0
- package/dist/chunk-EKSWCBCA.js.map +1 -0
- package/dist/chunk-JXZMAU2C.js +559 -0
- package/dist/chunk-JXZMAU2C.js.map +1 -0
- package/dist/chunk-KWOEDJUN.js +7 -0
- package/dist/chunk-KWOEDJUN.js.map +1 -0
- package/dist/chunk-Y52QYTSM.js +2420 -0
- package/dist/chunk-Y52QYTSM.js.map +1 -0
- package/dist/chunk-Z532A7QC.js +78 -0
- package/dist/chunk-Z532A7QC.js.map +1 -0
- package/dist/file-stream.d.ts +43 -0
- package/dist/file-stream.js +9 -0
- package/dist/file-stream.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +67 -0
- package/dist/index.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/request-handler.d.ts +18 -0
- package/dist/request-handler.js +13 -0
- package/dist/request-handler.js.map +1 -0
- package/dist/sandbox-DMlNr93l.d.ts +596 -0
- package/dist/sandbox.d.ts +4 -0
- package/dist/sandbox.js +13 -0
- package/dist/sandbox.js.map +1 -0
- package/dist/security.d.ts +31 -0
- package/dist/security.js +13 -0
- package/dist/security.js.map +1 -0
- package/dist/sse-parser.d.ts +28 -0
- package/dist/sse-parser.js +11 -0
- package/dist/sse-parser.js.map +1 -0
- package/dist/version.d.ts +8 -0
- package/dist/version.js +7 -0
- package/dist/version.js.map +1 -0
- package/package.json +13 -5
- package/src/clients/base-client.ts +280 -0
- package/src/clients/command-client.ts +115 -0
- package/src/clients/file-client.ts +269 -0
- package/src/clients/git-client.ts +92 -0
- package/src/clients/index.ts +64 -0
- package/src/clients/interpreter-client.ts +329 -0
- package/src/clients/port-client.ts +105 -0
- package/src/clients/process-client.ts +177 -0
- package/src/clients/sandbox-client.ts +41 -0
- package/src/clients/types.ts +84 -0
- package/src/clients/utility-client.ts +119 -0
- package/src/errors/adapter.ts +180 -0
- package/src/errors/classes.ts +469 -0
- package/src/errors/index.ts +105 -0
- package/src/file-stream.ts +164 -0
- package/src/index.ts +85 -21
- package/src/interpreter.ts +22 -13
- package/src/request-handler.ts +69 -43
- package/src/sandbox.ts +663 -444
- package/src/security.ts +14 -23
- package/src/sse-parser.ts +4 -8
- package/src/version.ts +6 -0
- package/startup.sh +3 -0
- package/tests/base-client.test.ts +328 -0
- package/tests/command-client.test.ts +407 -0
- package/tests/file-client.test.ts +643 -0
- package/tests/file-stream.test.ts +306 -0
- package/tests/get-sandbox.test.ts +110 -0
- package/tests/git-client.test.ts +328 -0
- package/tests/port-client.test.ts +301 -0
- package/tests/process-client.test.ts +658 -0
- package/tests/sandbox.test.ts +465 -0
- package/tests/sse-parser.test.ts +290 -0
- package/tests/utility-client.test.ts +332 -0
- package/tests/version.test.ts +16 -0
- package/tests/wrangler.jsonc +35 -0
- package/tsconfig.json +9 -1
- package/vitest.config.ts +31 -0
- package/container_src/bun.lock +0 -122
- package/container_src/handler/exec.ts +0 -340
- package/container_src/handler/file.ts +0 -844
- package/container_src/handler/git.ts +0 -182
- package/container_src/handler/ports.ts +0 -314
- package/container_src/handler/process.ts +0 -640
- package/container_src/index.ts +0 -531
- package/container_src/jupyter-server.ts +0 -336
- package/container_src/mime-processor.ts +0 -255
- package/container_src/package.json +0 -18
- package/container_src/startup.sh +0 -52
- package/container_src/types.ts +0 -108
- package/src/client.ts +0 -1021
- package/src/interpreter-types.ts +0 -383
- package/src/jupyter-client.ts +0 -266
- package/src/types.ts +0 -401
package/src/interpreter-types.ts
DELETED
|
@@ -1,383 +0,0 @@
|
|
|
1
|
-
// Context Management
|
|
2
|
-
export interface CreateContextOptions {
|
|
3
|
-
/**
|
|
4
|
-
* Programming language for the context
|
|
5
|
-
* @default 'python'
|
|
6
|
-
*/
|
|
7
|
-
language?: 'python' | 'javascript' | 'typescript';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Working directory for the context
|
|
11
|
-
* @default '/workspace'
|
|
12
|
-
*/
|
|
13
|
-
cwd?: string;
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Environment variables for the context
|
|
17
|
-
*/
|
|
18
|
-
envVars?: Record<string, string>;
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Request timeout in milliseconds
|
|
22
|
-
* @default 30000
|
|
23
|
-
*/
|
|
24
|
-
timeout?: number;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export interface CodeContext {
|
|
28
|
-
/**
|
|
29
|
-
* Unique identifier for the context
|
|
30
|
-
*/
|
|
31
|
-
readonly id: string;
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Programming language of the context
|
|
35
|
-
*/
|
|
36
|
-
readonly language: string;
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Current working directory
|
|
40
|
-
*/
|
|
41
|
-
readonly cwd: string;
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* When the context was created
|
|
45
|
-
*/
|
|
46
|
-
readonly createdAt: Date;
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* When the context was last used
|
|
50
|
-
*/
|
|
51
|
-
readonly lastUsed: Date;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// Execution Options
|
|
55
|
-
export interface RunCodeOptions {
|
|
56
|
-
/**
|
|
57
|
-
* Context to run the code in. If not provided, uses default context for the language
|
|
58
|
-
*/
|
|
59
|
-
context?: CodeContext;
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Language to use if context is not provided
|
|
63
|
-
* @default 'python'
|
|
64
|
-
*/
|
|
65
|
-
language?: 'python' | 'javascript' | 'typescript';
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Environment variables for this execution
|
|
69
|
-
*/
|
|
70
|
-
envVars?: Record<string, string>;
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Execution timeout in milliseconds
|
|
74
|
-
* @default 60000
|
|
75
|
-
*/
|
|
76
|
-
timeout?: number;
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* AbortSignal for cancelling execution
|
|
80
|
-
*/
|
|
81
|
-
signal?: AbortSignal;
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Callback for stdout output
|
|
85
|
-
*/
|
|
86
|
-
onStdout?: (output: OutputMessage) => void | Promise<void>;
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Callback for stderr output
|
|
90
|
-
*/
|
|
91
|
-
onStderr?: (output: OutputMessage) => void | Promise<void>;
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Callback for execution results (charts, tables, etc)
|
|
95
|
-
*/
|
|
96
|
-
onResult?: (result: Result) => void | Promise<void>;
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Callback for execution errors
|
|
100
|
-
*/
|
|
101
|
-
onError?: (error: ExecutionError) => void | Promise<void>;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
// Output Messages
|
|
105
|
-
export interface OutputMessage {
|
|
106
|
-
/**
|
|
107
|
-
* The output text
|
|
108
|
-
*/
|
|
109
|
-
text: string;
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* Timestamp of the output
|
|
113
|
-
*/
|
|
114
|
-
timestamp: number;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
// Execution Results
|
|
118
|
-
export interface Result {
|
|
119
|
-
/**
|
|
120
|
-
* Plain text representation
|
|
121
|
-
*/
|
|
122
|
-
text?: string;
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* HTML representation (tables, formatted output)
|
|
126
|
-
*/
|
|
127
|
-
html?: string;
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* PNG image data (base64 encoded)
|
|
131
|
-
*/
|
|
132
|
-
png?: string;
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* JPEG image data (base64 encoded)
|
|
136
|
-
*/
|
|
137
|
-
jpeg?: string;
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* SVG image data
|
|
141
|
-
*/
|
|
142
|
-
svg?: string;
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* LaTeX representation
|
|
146
|
-
*/
|
|
147
|
-
latex?: string;
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* Markdown representation
|
|
151
|
-
*/
|
|
152
|
-
markdown?: string;
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* JavaScript code to execute
|
|
156
|
-
*/
|
|
157
|
-
javascript?: string;
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* JSON data
|
|
161
|
-
*/
|
|
162
|
-
json?: any;
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* Chart data if the result is a visualization
|
|
166
|
-
*/
|
|
167
|
-
chart?: ChartData;
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* Raw data object
|
|
171
|
-
*/
|
|
172
|
-
data?: any;
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* Available output formats
|
|
176
|
-
*/
|
|
177
|
-
formats(): string[];
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
// Chart Data
|
|
181
|
-
export interface ChartData {
|
|
182
|
-
/**
|
|
183
|
-
* Type of chart
|
|
184
|
-
*/
|
|
185
|
-
type: 'line' | 'bar' | 'scatter' | 'pie' | 'histogram' | 'heatmap' | 'unknown';
|
|
186
|
-
|
|
187
|
-
/**
|
|
188
|
-
* Chart title
|
|
189
|
-
*/
|
|
190
|
-
title?: string;
|
|
191
|
-
|
|
192
|
-
/**
|
|
193
|
-
* Chart data (format depends on library)
|
|
194
|
-
*/
|
|
195
|
-
data: any;
|
|
196
|
-
|
|
197
|
-
/**
|
|
198
|
-
* Chart layout/configuration
|
|
199
|
-
*/
|
|
200
|
-
layout?: any;
|
|
201
|
-
|
|
202
|
-
/**
|
|
203
|
-
* Additional configuration
|
|
204
|
-
*/
|
|
205
|
-
config?: any;
|
|
206
|
-
|
|
207
|
-
/**
|
|
208
|
-
* Library that generated the chart
|
|
209
|
-
*/
|
|
210
|
-
library?: 'matplotlib' | 'plotly' | 'altair' | 'seaborn' | 'unknown';
|
|
211
|
-
|
|
212
|
-
/**
|
|
213
|
-
* Base64 encoded image if available
|
|
214
|
-
*/
|
|
215
|
-
image?: string;
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
// Execution Error
|
|
219
|
-
export interface ExecutionError {
|
|
220
|
-
/**
|
|
221
|
-
* Error name/type (e.g., 'NameError', 'SyntaxError')
|
|
222
|
-
*/
|
|
223
|
-
name: string;
|
|
224
|
-
|
|
225
|
-
/**
|
|
226
|
-
* Error message
|
|
227
|
-
*/
|
|
228
|
-
value: string;
|
|
229
|
-
|
|
230
|
-
/**
|
|
231
|
-
* Stack trace
|
|
232
|
-
*/
|
|
233
|
-
traceback: string[];
|
|
234
|
-
|
|
235
|
-
/**
|
|
236
|
-
* Line number where error occurred
|
|
237
|
-
*/
|
|
238
|
-
lineNumber?: number;
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
// Serializable execution result
|
|
242
|
-
export interface ExecutionResult {
|
|
243
|
-
code: string;
|
|
244
|
-
logs: {
|
|
245
|
-
stdout: string[];
|
|
246
|
-
stderr: string[];
|
|
247
|
-
};
|
|
248
|
-
error?: ExecutionError;
|
|
249
|
-
executionCount?: number;
|
|
250
|
-
results: Array<{
|
|
251
|
-
text?: string;
|
|
252
|
-
html?: string;
|
|
253
|
-
png?: string;
|
|
254
|
-
jpeg?: string;
|
|
255
|
-
svg?: string;
|
|
256
|
-
latex?: string;
|
|
257
|
-
markdown?: string;
|
|
258
|
-
javascript?: string;
|
|
259
|
-
json?: any;
|
|
260
|
-
chart?: ChartData;
|
|
261
|
-
data?: any;
|
|
262
|
-
}>;
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
// Execution Result Container
|
|
266
|
-
export class Execution {
|
|
267
|
-
/**
|
|
268
|
-
* All results from the execution
|
|
269
|
-
*/
|
|
270
|
-
public results: Result[] = [];
|
|
271
|
-
|
|
272
|
-
/**
|
|
273
|
-
* Accumulated stdout and stderr
|
|
274
|
-
*/
|
|
275
|
-
public logs = {
|
|
276
|
-
stdout: [] as string[],
|
|
277
|
-
stderr: [] as string[]
|
|
278
|
-
};
|
|
279
|
-
|
|
280
|
-
/**
|
|
281
|
-
* Execution error if any
|
|
282
|
-
*/
|
|
283
|
-
public error?: ExecutionError;
|
|
284
|
-
|
|
285
|
-
/**
|
|
286
|
-
* Execution count (for Jupyter)
|
|
287
|
-
*/
|
|
288
|
-
public executionCount?: number;
|
|
289
|
-
|
|
290
|
-
constructor(
|
|
291
|
-
public readonly code: string,
|
|
292
|
-
public readonly context: CodeContext
|
|
293
|
-
) {}
|
|
294
|
-
|
|
295
|
-
/**
|
|
296
|
-
* Convert to a plain object for serialization
|
|
297
|
-
*/
|
|
298
|
-
toJSON(): ExecutionResult {
|
|
299
|
-
return {
|
|
300
|
-
code: this.code,
|
|
301
|
-
logs: this.logs,
|
|
302
|
-
error: this.error,
|
|
303
|
-
executionCount: this.executionCount,
|
|
304
|
-
results: this.results.map(result => ({
|
|
305
|
-
text: result.text,
|
|
306
|
-
html: result.html,
|
|
307
|
-
png: result.png,
|
|
308
|
-
jpeg: result.jpeg,
|
|
309
|
-
svg: result.svg,
|
|
310
|
-
latex: result.latex,
|
|
311
|
-
markdown: result.markdown,
|
|
312
|
-
javascript: result.javascript,
|
|
313
|
-
json: result.json,
|
|
314
|
-
chart: result.chart,
|
|
315
|
-
data: result.data
|
|
316
|
-
}))
|
|
317
|
-
};
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
// Implementation of Result
|
|
322
|
-
export class ResultImpl implements Result {
|
|
323
|
-
constructor(private raw: any) {}
|
|
324
|
-
|
|
325
|
-
get text(): string | undefined {
|
|
326
|
-
return this.raw.text || this.raw.data?.['text/plain'];
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
get html(): string | undefined {
|
|
330
|
-
return this.raw.html || this.raw.data?.['text/html'];
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
get png(): string | undefined {
|
|
334
|
-
return this.raw.png || this.raw.data?.['image/png'];
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
get jpeg(): string | undefined {
|
|
338
|
-
return this.raw.jpeg || this.raw.data?.['image/jpeg'];
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
get svg(): string | undefined {
|
|
342
|
-
return this.raw.svg || this.raw.data?.['image/svg+xml'];
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
get latex(): string | undefined {
|
|
346
|
-
return this.raw.latex || this.raw.data?.['text/latex'];
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
get markdown(): string | undefined {
|
|
350
|
-
return this.raw.markdown || this.raw.data?.['text/markdown'];
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
get javascript(): string | undefined {
|
|
354
|
-
return this.raw.javascript || this.raw.data?.['application/javascript'];
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
get json(): any {
|
|
358
|
-
return this.raw.json || this.raw.data?.['application/json'];
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
get chart(): ChartData | undefined {
|
|
362
|
-
return this.raw.chart;
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
get data(): any {
|
|
366
|
-
return this.raw.data;
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
formats(): string[] {
|
|
370
|
-
const formats: string[] = [];
|
|
371
|
-
if (this.text) formats.push('text');
|
|
372
|
-
if (this.html) formats.push('html');
|
|
373
|
-
if (this.png) formats.push('png');
|
|
374
|
-
if (this.jpeg) formats.push('jpeg');
|
|
375
|
-
if (this.svg) formats.push('svg');
|
|
376
|
-
if (this.latex) formats.push('latex');
|
|
377
|
-
if (this.markdown) formats.push('markdown');
|
|
378
|
-
if (this.javascript) formats.push('javascript');
|
|
379
|
-
if (this.json) formats.push('json');
|
|
380
|
-
if (this.chart) formats.push('chart');
|
|
381
|
-
return formats;
|
|
382
|
-
}
|
|
383
|
-
}
|
package/src/jupyter-client.ts
DELETED
|
@@ -1,266 +0,0 @@
|
|
|
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
|
-
}
|