@cloudflare/sandbox 0.0.0-0ac3cfa → 0.0.0-12bbd12

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,255 @@
1
+ export interface ExecutionResult {
2
+ type: 'result' | 'stdout' | 'stderr' | 'error' | 'execution_complete';
3
+ text?: string;
4
+ html?: string;
5
+ png?: string; // base64
6
+ jpeg?: string; // base64
7
+ svg?: string;
8
+ latex?: string;
9
+ markdown?: string;
10
+ javascript?: string;
11
+ json?: any;
12
+ chart?: ChartData;
13
+ data?: any;
14
+ metadata?: any;
15
+ execution_count?: number;
16
+ ename?: string;
17
+ evalue?: string;
18
+ traceback?: string[];
19
+ timestamp: number;
20
+ }
21
+
22
+ export interface ChartData {
23
+ type: 'line' | 'bar' | 'scatter' | 'pie' | 'histogram' | 'heatmap' | 'unknown';
24
+ title?: string;
25
+ data: any;
26
+ layout?: any;
27
+ config?: any;
28
+ library?: 'matplotlib' | 'plotly' | 'altair' | 'seaborn' | 'unknown';
29
+ }
30
+
31
+ export function processJupyterMessage(msg: any): ExecutionResult | null {
32
+ const msgType = msg.header?.msg_type || msg.msg_type;
33
+
34
+ switch (msgType) {
35
+ case 'execute_result':
36
+ case 'display_data':
37
+ return processDisplayData(msg.content.data, msg.content.metadata);
38
+
39
+ case 'stream':
40
+ return {
41
+ type: msg.content.name === 'stdout' ? 'stdout' : 'stderr',
42
+ text: msg.content.text,
43
+ timestamp: Date.now()
44
+ };
45
+
46
+ case 'error':
47
+ return {
48
+ type: 'error',
49
+ ename: msg.content.ename,
50
+ evalue: msg.content.evalue,
51
+ traceback: msg.content.traceback,
52
+ timestamp: Date.now()
53
+ };
54
+
55
+ default:
56
+ return null;
57
+ }
58
+ }
59
+
60
+ function processDisplayData(data: any, metadata?: any): ExecutionResult {
61
+ const result: ExecutionResult = {
62
+ type: 'result',
63
+ timestamp: Date.now(),
64
+ metadata
65
+ };
66
+
67
+ // Process different MIME types in order of preference
68
+
69
+ // Interactive/Rich formats
70
+ if (data['application/vnd.plotly.v1+json']) {
71
+ result.chart = extractPlotlyChart(data['application/vnd.plotly.v1+json']);
72
+ result.json = data['application/vnd.plotly.v1+json'];
73
+ }
74
+
75
+ if (data['application/vnd.vega.v5+json']) {
76
+ result.chart = extractVegaChart(data['application/vnd.vega.v5+json'], 'vega');
77
+ result.json = data['application/vnd.vega.v5+json'];
78
+ }
79
+
80
+ if (data['application/vnd.vegalite.v4+json'] || data['application/vnd.vegalite.v5+json']) {
81
+ const vegaData = data['application/vnd.vegalite.v4+json'] || data['application/vnd.vegalite.v5+json'];
82
+ result.chart = extractVegaChart(vegaData, 'vega-lite');
83
+ result.json = vegaData;
84
+ }
85
+
86
+ // HTML content (tables, formatted output)
87
+ if (data['text/html']) {
88
+ result.html = data['text/html'];
89
+
90
+ // Check if it's a pandas DataFrame
91
+ if (isPandasDataFrame(data['text/html'])) {
92
+ result.data = { type: 'dataframe', html: data['text/html'] };
93
+ }
94
+ }
95
+
96
+ // Images
97
+ if (data['image/png']) {
98
+ result.png = data['image/png'];
99
+
100
+ // Try to detect if it's a chart
101
+ if (isLikelyChart(data, metadata)) {
102
+ result.chart = {
103
+ type: 'unknown',
104
+ library: 'matplotlib',
105
+ data: { image: data['image/png'] }
106
+ };
107
+ }
108
+ }
109
+
110
+ if (data['image/jpeg']) {
111
+ result.jpeg = data['image/jpeg'];
112
+ }
113
+
114
+ if (data['image/svg+xml']) {
115
+ result.svg = data['image/svg+xml'];
116
+ }
117
+
118
+ // Mathematical content
119
+ if (data['text/latex']) {
120
+ result.latex = data['text/latex'];
121
+ }
122
+
123
+ // Code
124
+ if (data['application/javascript']) {
125
+ result.javascript = data['application/javascript'];
126
+ }
127
+
128
+ // Structured data
129
+ if (data['application/json']) {
130
+ result.json = data['application/json'];
131
+ }
132
+
133
+ // Markdown
134
+ if (data['text/markdown']) {
135
+ result.markdown = data['text/markdown'];
136
+ }
137
+
138
+ // Plain text (fallback)
139
+ if (data['text/plain']) {
140
+ result.text = data['text/plain'];
141
+ }
142
+
143
+ return result;
144
+ }
145
+
146
+ function extractPlotlyChart(plotlyData: any): ChartData {
147
+ const data = plotlyData.data || plotlyData;
148
+ const layout = plotlyData.layout || {};
149
+
150
+ // Try to detect chart type from traces
151
+ let chartType: ChartData['type'] = 'unknown';
152
+ if (data && data.length > 0) {
153
+ const firstTrace = data[0];
154
+ if (firstTrace.type === 'scatter') {
155
+ chartType = firstTrace.mode?.includes('lines') ? 'line' : 'scatter';
156
+ } else if (firstTrace.type === 'bar') {
157
+ chartType = 'bar';
158
+ } else if (firstTrace.type === 'pie') {
159
+ chartType = 'pie';
160
+ } else if (firstTrace.type === 'histogram') {
161
+ chartType = 'histogram';
162
+ } else if (firstTrace.type === 'heatmap') {
163
+ chartType = 'heatmap';
164
+ }
165
+ }
166
+
167
+ return {
168
+ type: chartType,
169
+ title: layout.title?.text || layout.title,
170
+ data: data,
171
+ layout: layout,
172
+ config: plotlyData.config,
173
+ library: 'plotly'
174
+ };
175
+ }
176
+
177
+ function extractVegaChart(vegaData: any, format: 'vega' | 'vega-lite'): ChartData {
178
+ // Try to detect chart type from mark or encoding
179
+ let chartType: ChartData['type'] = 'unknown';
180
+
181
+ if (format === 'vega-lite' && vegaData.mark) {
182
+ const mark = typeof vegaData.mark === 'string' ? vegaData.mark : vegaData.mark.type;
183
+ switch (mark) {
184
+ case 'line':
185
+ chartType = 'line';
186
+ break;
187
+ case 'bar':
188
+ chartType = 'bar';
189
+ break;
190
+ case 'point':
191
+ case 'circle':
192
+ chartType = 'scatter';
193
+ break;
194
+ case 'arc':
195
+ chartType = 'pie';
196
+ break;
197
+ case 'rect':
198
+ if (vegaData.encoding?.color) {
199
+ chartType = 'heatmap';
200
+ }
201
+ break;
202
+ }
203
+ }
204
+
205
+ return {
206
+ type: chartType,
207
+ title: vegaData.title,
208
+ data: vegaData,
209
+ library: 'altair' // Altair outputs Vega-Lite
210
+ };
211
+ }
212
+
213
+ function isPandasDataFrame(html: string): boolean {
214
+ // Simple heuristic to detect pandas DataFrame HTML
215
+ return html.includes('dataframe') ||
216
+ (html.includes('<table') && html.includes('<thead') && html.includes('<tbody'));
217
+ }
218
+
219
+ function isLikelyChart(data: any, metadata?: any): boolean {
220
+ // Check metadata for hints
221
+ if (metadata?.needs?.includes('matplotlib')) {
222
+ return true;
223
+ }
224
+
225
+ // Check if other chart formats are present
226
+ if (data['application/vnd.plotly.v1+json'] ||
227
+ data['application/vnd.vega.v5+json'] ||
228
+ data['application/vnd.vegalite.v4+json']) {
229
+ return true;
230
+ }
231
+
232
+ // If only image output without text, likely a chart
233
+ if ((data['image/png'] || data['image/svg+xml']) && !data['text/plain']) {
234
+ return true;
235
+ }
236
+
237
+ return false;
238
+ }
239
+
240
+ export function extractFormats(result: ExecutionResult): string[] {
241
+ const formats: string[] = [];
242
+
243
+ if (result.text) formats.push('text');
244
+ if (result.html) formats.push('html');
245
+ if (result.png) formats.push('png');
246
+ if (result.jpeg) formats.push('jpeg');
247
+ if (result.svg) formats.push('svg');
248
+ if (result.latex) formats.push('latex');
249
+ if (result.markdown) formats.push('markdown');
250
+ if (result.javascript) formats.push('javascript');
251
+ if (result.json) formats.push('json');
252
+ if (result.chart) formats.push('chart');
253
+
254
+ return formats;
255
+ }
@@ -5,5 +5,14 @@
5
5
  "main": "index.ts",
6
6
  "scripts": {
7
7
  "start": "bun run index.ts"
8
+ },
9
+ "dependencies": {
10
+ "@jupyterlab/services": "^7.0.0",
11
+ "ws": "^8.16.0",
12
+ "uuid": "^9.0.1"
13
+ },
14
+ "devDependencies": {
15
+ "@types/ws": "^8.5.10",
16
+ "@types/uuid": "^9.0.7"
8
17
  }
9
18
  }
@@ -0,0 +1,84 @@
1
+ #!/bin/bash
2
+
3
+ # Function to check if Jupyter is ready
4
+ check_jupyter_ready() {
5
+ # Check if API is responsive and kernelspecs are available
6
+ curl -s http://localhost:8888/api/kernelspecs > /dev/null 2>&1
7
+ }
8
+
9
+ # Function to notify Bun server that Jupyter is ready
10
+ notify_jupyter_ready() {
11
+ # Create a marker file that the Bun server can check
12
+ touch /tmp/jupyter-ready
13
+ echo "[Startup] Jupyter is ready, notified Bun server"
14
+ }
15
+
16
+ # Start Jupyter server in background
17
+ echo "[Startup] Starting Jupyter server..."
18
+ jupyter server \
19
+ --config=/container-server/jupyter_config.py \
20
+ > /tmp/jupyter.log 2>&1 &
21
+
22
+ JUPYTER_PID=$!
23
+
24
+ # Start Bun server immediately (parallel startup)
25
+ echo "[Startup] Starting Bun server..."
26
+ bun index.ts &
27
+ BUN_PID=$!
28
+
29
+ # Monitor Jupyter readiness in background
30
+ (
31
+ echo "[Startup] Monitoring Jupyter readiness in background..."
32
+ MAX_ATTEMPTS=60
33
+ ATTEMPT=0
34
+
35
+ # Track start time for reporting
36
+ START_TIME=$(date +%s.%N)
37
+
38
+ while [ $ATTEMPT -lt $MAX_ATTEMPTS ]; do
39
+ if check_jupyter_ready; then
40
+ notify_jupyter_ready
41
+ END_TIME=$(date +%s.%N)
42
+ ELAPSED=$(awk "BEGIN {printf \"%.2f\", $END_TIME - $START_TIME}")
43
+ echo "[Startup] Jupyter server is ready after $ELAPSED seconds ($ATTEMPT attempts)"
44
+ break
45
+ fi
46
+
47
+ # Check if Jupyter process is still running
48
+ if ! kill -0 $JUPYTER_PID 2>/dev/null; then
49
+ echo "[Startup] WARNING: Jupyter process died. Check /tmp/jupyter.log for details"
50
+ cat /tmp/jupyter.log
51
+ # Don't exit - let Bun server continue running in degraded mode
52
+ break
53
+ fi
54
+
55
+ ATTEMPT=$((ATTEMPT + 1))
56
+
57
+ # Start with faster checks
58
+ if [ $ATTEMPT -eq 1 ]; then
59
+ DELAY=0.5 # Start at 0.5s
60
+ else
61
+ # Exponential backoff with 1.3x multiplier (less aggressive than 1.5x)
62
+ DELAY=$(awk "BEGIN {printf \"%.2f\", $DELAY * 1.3}")
63
+ # Cap at 2s max (instead of 5s)
64
+ if [ $(awk "BEGIN {print ($DELAY > 2)}") -eq 1 ]; then
65
+ DELAY=2
66
+ fi
67
+ fi
68
+
69
+ # Log with current delay for transparency
70
+ echo "[Startup] Jupyter not ready yet (attempt $ATTEMPT/$MAX_ATTEMPTS, next check in ${DELAY}s)"
71
+
72
+ sleep $DELAY
73
+ done
74
+
75
+ if [ $ATTEMPT -eq $MAX_ATTEMPTS ]; then
76
+ echo "[Startup] WARNING: Jupyter failed to become ready within attempts"
77
+ echo "[Startup] Jupyter logs:"
78
+ cat /tmp/jupyter.log
79
+ # Don't exit - let Bun server continue in degraded mode
80
+ fi
81
+ ) &
82
+
83
+ # Wait for Bun server (main process)
84
+ wait $BUN_PID
@@ -0,0 +1,117 @@
1
+ import type { ChildProcess } from "node:child_process";
2
+
3
+ // Process management types
4
+ export type ProcessStatus =
5
+ | 'starting'
6
+ | 'running'
7
+ | 'completed'
8
+ | 'failed'
9
+ | 'killed'
10
+ | 'error';
11
+
12
+ export interface ProcessRecord {
13
+ id: string;
14
+ pid?: number;
15
+ command: string;
16
+ status: ProcessStatus;
17
+ startTime: Date;
18
+ endTime?: Date;
19
+ exitCode?: number;
20
+ sessionId?: string;
21
+ childProcess?: ChildProcess;
22
+ stdout: string;
23
+ stderr: string;
24
+ outputListeners: Set<(stream: 'stdout' | 'stderr', data: string) => void>;
25
+ statusListeners: Set<(status: ProcessStatus) => void>;
26
+ }
27
+
28
+ export interface StartProcessRequest {
29
+ command: string;
30
+ options?: {
31
+ processId?: string;
32
+ sessionId?: string;
33
+ timeout?: number;
34
+ env?: Record<string, string>;
35
+ cwd?: string;
36
+ encoding?: string;
37
+ autoCleanup?: boolean;
38
+ };
39
+ }
40
+
41
+ export interface ExecuteOptions {
42
+ sessionId?: string | null;
43
+ background?: boolean;
44
+ cwd?: string | URL;
45
+ env?: Record<string, string>;
46
+ }
47
+
48
+ export interface ExecuteRequest extends ExecuteOptions {
49
+ command: string;
50
+ }
51
+
52
+ export interface GitCheckoutRequest {
53
+ repoUrl: string;
54
+ branch?: string;
55
+ targetDir?: string;
56
+ sessionId?: string;
57
+ }
58
+
59
+ export interface MkdirRequest {
60
+ path: string;
61
+ recursive?: boolean;
62
+ sessionId?: string;
63
+ }
64
+
65
+ export interface WriteFileRequest {
66
+ path: string;
67
+ content: string;
68
+ encoding?: string;
69
+ sessionId?: string;
70
+ }
71
+
72
+ export interface ReadFileRequest {
73
+ path: string;
74
+ encoding?: string;
75
+ sessionId?: string;
76
+ }
77
+
78
+ export interface DeleteFileRequest {
79
+ path: string;
80
+ sessionId?: string;
81
+ }
82
+
83
+ export interface RenameFileRequest {
84
+ oldPath: string;
85
+ newPath: string;
86
+ sessionId?: string;
87
+ }
88
+
89
+ export interface MoveFileRequest {
90
+ sourcePath: string;
91
+ destinationPath: string;
92
+ sessionId?: string;
93
+ }
94
+
95
+ export interface ListFilesRequest {
96
+ path: string;
97
+ options?: {
98
+ recursive?: boolean;
99
+ includeHidden?: boolean;
100
+ };
101
+ sessionId?: string;
102
+ }
103
+
104
+ export interface ExposePortRequest {
105
+ port: number;
106
+ name?: string;
107
+ }
108
+
109
+ export interface UnexposePortRequest {
110
+ port: number;
111
+ }
112
+
113
+ export interface SessionData {
114
+ sessionId: string;
115
+ activeProcess: ChildProcess | null;
116
+ createdAt: Date;
117
+ }
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@cloudflare/sandbox",
3
- "version": "0.0.0-0ac3cfa",
3
+ "version": "0.0.0-12bbd12",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/cloudflare/sandbox-sdk"
7
7
  },
8
8
  "description": "A sandboxed environment for running commands",
9
9
  "dependencies": {
10
- "@cloudflare/containers": "^0.0.13"
10
+ "@cloudflare/containers": "^0.0.25"
11
11
  },
12
12
  "tags": [
13
13
  "sandbox",
@@ -18,19 +18,15 @@
18
18
  ],
19
19
  "scripts": {
20
20
  "build": "rm -rf dist && tsup src/*.ts --outDir dist --dts --sourcemap --format esm",
21
- "docker:build": "docker build -t ghostwriternr/cloudflare-sandbox:$npm_package_version .",
22
- "docker:publish": "docker push docker.io/ghostwriternr/cloudflare-sandbox:$npm_package_version"
21
+ "docker:local": "docker build . -t cloudflare/sandbox-test:$npm_package_version",
22
+ "docker:publish": "docker buildx build --platform linux/amd64,linux/arm64 -t cloudflare/sandbox:$npm_package_version --push .",
23
+ "docker:publish:beta": "docker buildx build --platform linux/amd64,linux/arm64 -t cloudflare/sandbox:$npm_package_version-beta --push ."
23
24
  },
24
25
  "exports": {
25
26
  ".": {
26
27
  "types": "./dist/index.d.ts",
27
28
  "import": "./dist/index.js",
28
29
  "require": "./dist/index.js"
29
- },
30
- "./client": {
31
- "types": "./dist/client.d.ts",
32
- "import": "./dist/client.js",
33
- "require": "./dist/client.js"
34
30
  }
35
31
  },
36
32
  "keywords": [],