Skip to content

Commit ebac76a

Browse files
committed
final fixes
1 parent e724994 commit ebac76a

File tree

1 file changed

+82
-71
lines changed

1 file changed

+82
-71
lines changed

src/content/changelog/agents/2025-08-05-sandbox-sdk-major-update.mdx

Lines changed: 82 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -17,55 +17,98 @@ This makes it ideal for building AI agents, CI runners, cloud REPLs, data analys
1717

1818
Create persistent code contexts with support for rich visual + structured outputs.
1919

20+
#### createCodeContext(options?)
21+
22+
Creates a new code execution context with persistent state.
23+
24+
```ts
25+
// Create a Python context
26+
const pythonCtx = await sandbox.createCodeContext({ language: "python" });
27+
28+
// Create a JavaScript context
29+
const jsCtx = await sandbox.createCodeContext({ language: "javascript" });
30+
```
31+
32+
Options:
33+
34+
- language: Programming language ('python' | 'javascript' | 'typescript')
35+
- cwd: Working directory (default: /workspace)
36+
- envVars: Environment variables for the context
37+
38+
#### runCode(code, options?)
39+
40+
Executes code with optional streaming callbacks.
41+
2042
```ts
21-
import { getSandbox } from "@cloudflare/sandbox";
22-
23-
export default {
24-
async fetch(request: Request, env: Env) {
25-
const sandbox = getSandbox(env.Sandbox, "my-sandbox");
26-
27-
// Code interpreter with rich output support
28-
const pythonResult = await sandbox.runCode({
29-
context: "python",
30-
code: `
31-
import matplotlib.pyplot as plt
32-
import pandas as pd
33-
34-
# Create sample data
35-
data = {'x': [1, 2, 3, 4, 5], 'y': [2, 4, 6, 8, 10]}
36-
df = pd.DataFrame(data)
37-
38-
# Create visualization
39-
plt.figure(figsize=(8, 6))
40-
plt.plot(df['x'], df['y'], marker='o')
41-
plt.title('Sample Plot')
42-
plt.show()
43-
44-
print(df.describe())
45-
`,
46-
});
47-
48-
// JavaScript/TypeScript execution
49-
const jsResult = await sandbox.runCode({
50-
context: "javascript",
51-
code: `
52-
const data = [1, 2, 3, 4, 5];
53-
const sum = data.reduce((a, b) => a + b, 0);
54-
console.log(\`Sum: \${sum}\`);
55-
return { sum, average: sum / data.length };
56-
`,
57-
});
43+
// Simple execution
44+
const execution = await sandbox.runCode('print("Hello World")', {
45+
context: pythonCtx,
46+
});
47+
48+
// With streaming callbacks
49+
await sandbox.runCode(
50+
`
51+
for i in range(5):
52+
print(f"Step {i}")
53+
time.sleep(1)
54+
`,
55+
{
56+
context: pythonCtx,
57+
onStdout: (output) => console.log("Real-time:", output.text),
58+
onResult: (result) => console.log("Result:", result),
59+
},
60+
);
5861
```
5962

60-
### Real-time streaming output
63+
Options:
64+
65+
- language: Programming language ('python' | 'javascript' | 'typescript')
66+
- cwd: Working directory (default: /workspace)
67+
- envVars: Environment variables for the context
68+
69+
#### Real-time streaming output
6170

6271
Returns a streaming response for real-time processing.
6372

6473
```ts
65-
/const stream = await sandbox.runCodeStream('import time; [print(i) for i in range(10)]');
74+
const stream = await sandbox.runCodeStream(
75+
"import time; [print(i) for i in range(10)]",
76+
);
6677
// Process the stream as needed
6778
```
6879

80+
#### Rich output handling
81+
82+
Interpreter outputs are auto-formatted and returned in multiple formats:
83+
84+
- text
85+
- html (e.g., Pandas tables)
86+
- png, svg (e.g., Matplotlib charts)
87+
- json (structured data)
88+
- chart (parsed visualizations)
89+
90+
```ts
91+
const result = await sandbox.runCode(
92+
`
93+
import seaborn as sns
94+
import matplotlib.pyplot as plt
95+
96+
data = sns.load_dataset("flights")
97+
pivot = data.pivot("month", "year", "passengers")
98+
sns.heatmap(pivot, annot=True, fmt="d")
99+
plt.title("Flight Passengers")
100+
plt.show()
101+
102+
pivot.to_dict()
103+
`,
104+
{ context: pythonCtx },
105+
);
106+
107+
if (result.png) {
108+
console.log("Chart output:", result.png);
109+
}
110+
```
111+
69112
### Preview URLs from Exposed Ports
70113

71114
Start background processes and expose them with live URLs.
@@ -110,36 +153,4 @@ await sandbox.gitCheckout("https://github.com/user/repo", {
110153
});
111154
```
112155

113-
### Rich output handling
114-
115-
Interpreter outputs are auto-formatted and returned in multiple formats:
116-
117-
- text
118-
- html (e.g., Pandas tables)
119-
- png, svg (e.g., Matplotlib charts)
120-
- json (structured data)
121-
- chart (parsed visualizations)
122-
123-
```ts
124-
const result = await sandbox.runCode(
125-
`
126-
import seaborn as sns
127-
import matplotlib.pyplot as plt
128-
129-
data = sns.load_dataset("flights")
130-
pivot = data.pivot("month", "year", "passengers")
131-
sns.heatmap(pivot, annot=True, fmt="d")
132-
plt.title("Flight Passengers")
133-
plt.show()
134-
135-
pivot.to_dict()
136-
`,
137-
{ context: pythonCtx },
138-
);
139-
140-
if (result.png) {
141-
console.log("Chart output:", result.png);
142-
}
143-
```
144-
145156
Sandboxes are still experimental. We're using them to explore how isolated, container-like workloads might scale on Cloudflare — and to help define the developer experience around them.

0 commit comments

Comments
 (0)