claude-code-templates 1.24.0 → 1.24.2
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/components/sandbox/README.md +92 -10
- package/components/sandbox/cloudflare/.dev.vars.example +13 -0
- package/components/sandbox/cloudflare/IMPLEMENTATION_SUMMARY.md +375 -0
- package/components/sandbox/cloudflare/QUICKSTART.md +267 -0
- package/components/sandbox/cloudflare/README.md +301 -0
- package/components/sandbox/cloudflare/SANDBOX_DEBUGGING.md +442 -0
- package/components/sandbox/cloudflare/claude-code-sandbox.md +314 -0
- package/components/sandbox/cloudflare/launcher.ts +472 -0
- package/components/sandbox/cloudflare/monitor.ts +388 -0
- package/components/sandbox/cloudflare/package.json +54 -0
- package/components/sandbox/cloudflare/src/index.ts +240 -0
- package/components/sandbox/cloudflare/tsconfig.json +31 -0
- package/components/sandbox/cloudflare/wrangler.toml +50 -0
- package/package.json +2 -1
- package/src/index.js +312 -127
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
# Cloudflare Claude Code Sandbox
|
|
2
|
+
|
|
3
|
+
Execute Claude Code in an isolated Cloudflare Workers sandbox environment with AI-powered code execution.
|
|
4
|
+
|
|
5
|
+
## Description
|
|
6
|
+
|
|
7
|
+
This component sets up Cloudflare Sandbox SDK integration to run Claude Code in a secure, isolated cloud environment. Built on Cloudflare's container-based sandboxes with Durable Objects for persistent execution.
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- **Isolated Execution**: Run Claude Code in secure Cloudflare Workers sandboxes
|
|
12
|
+
- **AI Code Executor**: Turn natural language into executable Python/Node.js code
|
|
13
|
+
- **Real-time Streaming**: Stream execution output as it happens
|
|
14
|
+
- **Persistent Storage**: Use Durable Objects for stateful sandbox sessions
|
|
15
|
+
- **Global Distribution**: Leverage Cloudflare's edge network for low latency
|
|
16
|
+
- **Component Installation**: Automatically install agents and commands in sandbox
|
|
17
|
+
|
|
18
|
+
## Requirements
|
|
19
|
+
|
|
20
|
+
- Cloudflare Account (Workers Paid plan for Durable Objects)
|
|
21
|
+
- Anthropic API Key
|
|
22
|
+
- Node.js 16.17.0+
|
|
23
|
+
- Docker (for local development)
|
|
24
|
+
- Wrangler CLI
|
|
25
|
+
|
|
26
|
+
## Architecture
|
|
27
|
+
|
|
28
|
+
```
|
|
29
|
+
┌─────────────────────────────────────────────────────┐
|
|
30
|
+
│ User Request (Natural Language) │
|
|
31
|
+
└──────────────────┬──────────────────────────────────┘
|
|
32
|
+
│
|
|
33
|
+
▼
|
|
34
|
+
┌─────────────────────────────────────────────────────┐
|
|
35
|
+
│ Cloudflare Worker (API Endpoint) │
|
|
36
|
+
│ • POST /execute │
|
|
37
|
+
│ • Receives question/prompt │
|
|
38
|
+
└──────────────────┬──────────────────────────────────┘
|
|
39
|
+
│
|
|
40
|
+
▼
|
|
41
|
+
┌─────────────────────────────────────────────────────┐
|
|
42
|
+
│ Claude AI (via Anthropic SDK) │
|
|
43
|
+
│ • Generates Python/TypeScript code │
|
|
44
|
+
│ • Returns executable implementation │
|
|
45
|
+
└──────────────────┬──────────────────────────────────┘
|
|
46
|
+
│
|
|
47
|
+
▼
|
|
48
|
+
┌─────────────────────────────────────────────────────┐
|
|
49
|
+
│ Cloudflare Sandbox (Durable Object) │
|
|
50
|
+
│ • Isolated container execution │
|
|
51
|
+
│ • Python/Node.js runtime │
|
|
52
|
+
│ • File system access │
|
|
53
|
+
│ • Real-time streaming output │
|
|
54
|
+
└──────────────────┬──────────────────────────────────┘
|
|
55
|
+
│
|
|
56
|
+
▼
|
|
57
|
+
┌─────────────────────────────────────────────────────┐
|
|
58
|
+
│ Results │
|
|
59
|
+
│ • Generated code │
|
|
60
|
+
│ • Execution output │
|
|
61
|
+
│ • Error messages (if any) │
|
|
62
|
+
└─────────────────────────────────────────────────────┘
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Usage
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
# Execute a prompt in Cloudflare sandbox
|
|
69
|
+
npx claude-code-templates@latest --sandbox cloudflare --prompt "Calculate the 10th Fibonacci number"
|
|
70
|
+
|
|
71
|
+
# Pass API keys directly
|
|
72
|
+
npx claude-code-templates@latest --sandbox cloudflare \
|
|
73
|
+
--anthropic-api-key your_anthropic_key \
|
|
74
|
+
--prompt "Create a web scraper"
|
|
75
|
+
|
|
76
|
+
# Install components and execute
|
|
77
|
+
npx claude-code-templates@latest --sandbox cloudflare \
|
|
78
|
+
--agent frontend-developer \
|
|
79
|
+
--command setup-react \
|
|
80
|
+
--anthropic-api-key your_anthropic_key \
|
|
81
|
+
--prompt "Create a modern todo app"
|
|
82
|
+
|
|
83
|
+
# Deploy your own Cloudflare Worker sandbox
|
|
84
|
+
cd .claude/sandbox/cloudflare
|
|
85
|
+
npm install
|
|
86
|
+
npx wrangler secret put ANTHROPIC_API_KEY
|
|
87
|
+
npx wrangler deploy
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Environment Setup
|
|
91
|
+
|
|
92
|
+
The component creates:
|
|
93
|
+
- `.claude/sandbox/cloudflare/src/index.ts` - Worker with sandbox logic
|
|
94
|
+
- `.claude/sandbox/cloudflare/wrangler.toml` - Cloudflare configuration
|
|
95
|
+
- `.claude/sandbox/cloudflare/package.json` - Node.js dependencies
|
|
96
|
+
- `.claude/sandbox/cloudflare/launcher.ts` - TypeScript launcher script
|
|
97
|
+
- `.claude/sandbox/cloudflare/monitor.ts` - Real-time monitoring tool
|
|
98
|
+
|
|
99
|
+
## API Key Configuration
|
|
100
|
+
|
|
101
|
+
### Option 1: CLI Parameters (Recommended)
|
|
102
|
+
```bash
|
|
103
|
+
npx claude-code-templates@latest --sandbox cloudflare \
|
|
104
|
+
--anthropic-api-key your_anthropic_api_key \
|
|
105
|
+
--prompt "Your prompt here"
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### Option 2: Wrangler Secrets
|
|
109
|
+
```bash
|
|
110
|
+
cd .claude/sandbox/cloudflare
|
|
111
|
+
npx wrangler secret put ANTHROPIC_API_KEY
|
|
112
|
+
# Paste your API key when prompted
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Option 3: Environment Variables
|
|
116
|
+
```bash
|
|
117
|
+
export ANTHROPIC_API_KEY=your_anthropic_api_key_here
|
|
118
|
+
|
|
119
|
+
# Or create .dev.vars file:
|
|
120
|
+
ANTHROPIC_API_KEY=your_anthropic_api_key_here
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
**Note**: Wrangler secrets are required for production deployment. CLI parameters work for local execution only.
|
|
124
|
+
|
|
125
|
+
## How it Works
|
|
126
|
+
|
|
127
|
+
1. User sends natural language request (e.g., "What's the factorial of 5?")
|
|
128
|
+
2. Cloudflare Worker receives request via POST /execute
|
|
129
|
+
3. Claude generates executable Python/TypeScript code via Anthropic API
|
|
130
|
+
4. Code is written to sandbox file system
|
|
131
|
+
5. Sandbox executes code in isolated container
|
|
132
|
+
6. Results stream back in real-time
|
|
133
|
+
7. Worker returns both code and execution output
|
|
134
|
+
|
|
135
|
+
## Deployment
|
|
136
|
+
|
|
137
|
+
### Local Development
|
|
138
|
+
```bash
|
|
139
|
+
cd .claude/sandbox/cloudflare
|
|
140
|
+
npm install
|
|
141
|
+
npm run dev
|
|
142
|
+
|
|
143
|
+
# Test locally
|
|
144
|
+
curl -X POST http://localhost:8787/execute \
|
|
145
|
+
-H "Content-Type: application/json" \
|
|
146
|
+
-d '{"question": "What is 2^10?"}'
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
### Production Deployment
|
|
150
|
+
```bash
|
|
151
|
+
# Set API key secret
|
|
152
|
+
npx wrangler secret put ANTHROPIC_API_KEY
|
|
153
|
+
|
|
154
|
+
# Deploy to Cloudflare Workers
|
|
155
|
+
npx wrangler deploy
|
|
156
|
+
|
|
157
|
+
# Wait 2-3 minutes for container provisioning
|
|
158
|
+
npx wrangler containers list
|
|
159
|
+
|
|
160
|
+
# Test deployment
|
|
161
|
+
curl -X POST https://your-worker.your-subdomain.workers.dev/execute \
|
|
162
|
+
-H "Content-Type: application/json" \
|
|
163
|
+
-d '{"question": "Calculate factorial of 5"}'
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
## Security Benefits
|
|
167
|
+
|
|
168
|
+
- **Container Isolation**: Each execution runs in isolated Cloudflare container
|
|
169
|
+
- **No Local Access**: Sandboxes have no access to your local system
|
|
170
|
+
- **Resource Limits**: Automatic CPU time and memory constraints
|
|
171
|
+
- **Temporary Execution**: Containers destroyed after execution
|
|
172
|
+
- **Edge Security**: Cloudflare's security infrastructure built-in
|
|
173
|
+
|
|
174
|
+
## Advanced Features
|
|
175
|
+
|
|
176
|
+
### Code Interpreter API
|
|
177
|
+
```typescript
|
|
178
|
+
// Use built-in code interpreter instead of exec
|
|
179
|
+
import { getCodeInterpreter } from '@cloudflare/sandbox';
|
|
180
|
+
|
|
181
|
+
const interpreter = getCodeInterpreter(env.Sandbox, 'user-id');
|
|
182
|
+
const result = await interpreter.notebook.execCell('print(2**10)');
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
### Streaming Output
|
|
186
|
+
```typescript
|
|
187
|
+
// Stream execution results in real-time
|
|
188
|
+
return new Response(
|
|
189
|
+
new ReadableStream({
|
|
190
|
+
async start(controller) {
|
|
191
|
+
const result = await sandbox.exec('python script.py', {
|
|
192
|
+
onStdout: (data) => controller.enqueue(data),
|
|
193
|
+
onStderr: (data) => controller.enqueue(data)
|
|
194
|
+
});
|
|
195
|
+
controller.close();
|
|
196
|
+
}
|
|
197
|
+
})
|
|
198
|
+
);
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
### Persistent Sessions
|
|
202
|
+
```typescript
|
|
203
|
+
// Maintain sandbox state across requests
|
|
204
|
+
const sandbox = getSandbox(env.Sandbox, userId);
|
|
205
|
+
await sandbox.writeFile('/data/state.json', JSON.stringify(state));
|
|
206
|
+
// Later...
|
|
207
|
+
const state = await sandbox.readFile('/data/state.json');
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
## Examples
|
|
211
|
+
|
|
212
|
+
```bash
|
|
213
|
+
# Mathematical computation
|
|
214
|
+
npx claude-code-templates@latest --sandbox cloudflare \
|
|
215
|
+
--prompt "Calculate the 100th Fibonacci number"
|
|
216
|
+
|
|
217
|
+
# Data analysis
|
|
218
|
+
npx claude-code-templates@latest --sandbox cloudflare \
|
|
219
|
+
--prompt "What is the mean of [10, 20, 30, 40, 50]?"
|
|
220
|
+
|
|
221
|
+
# String manipulation
|
|
222
|
+
npx claude-code-templates@latest --sandbox cloudflare \
|
|
223
|
+
--prompt "Reverse the string 'Hello World'"
|
|
224
|
+
|
|
225
|
+
# Web development
|
|
226
|
+
npx claude-code-templates@latest --sandbox cloudflare \
|
|
227
|
+
--agent frontend-developer \
|
|
228
|
+
--prompt "Create a responsive navigation bar"
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
## Comparison with E2B
|
|
232
|
+
|
|
233
|
+
| Feature | Cloudflare Sandbox | E2B Sandbox |
|
|
234
|
+
|---------|-------------------|-------------|
|
|
235
|
+
| **Provider** | Cloudflare Workers | E2B.dev |
|
|
236
|
+
| **Infrastructure** | Cloudflare Edge Network | Cloud VMs |
|
|
237
|
+
| **Pricing** | $5/month (Workers Paid) | Usage-based |
|
|
238
|
+
| **Cold Start** | ~100ms | ~2-3 seconds |
|
|
239
|
+
| **Max Duration** | 30 seconds (Workers) | Up to hours |
|
|
240
|
+
| **Languages** | Python, Node.js | Full Linux environment |
|
|
241
|
+
| **Global** | Yes (edge network) | Single region |
|
|
242
|
+
| **Best For** | Fast, lightweight tasks | Long-running operations |
|
|
243
|
+
|
|
244
|
+
## Troubleshooting
|
|
245
|
+
|
|
246
|
+
### Container Not Ready
|
|
247
|
+
```bash
|
|
248
|
+
# After first deployment, wait 2-3 minutes
|
|
249
|
+
npx wrangler containers list
|
|
250
|
+
|
|
251
|
+
# Check container status
|
|
252
|
+
npx wrangler tail
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
### API Key Issues
|
|
256
|
+
```bash
|
|
257
|
+
# Verify secret is set
|
|
258
|
+
npx wrangler secret list
|
|
259
|
+
|
|
260
|
+
# Update secret
|
|
261
|
+
npx wrangler secret put ANTHROPIC_API_KEY
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
### Local Development Issues
|
|
265
|
+
```bash
|
|
266
|
+
# Ensure Docker is running
|
|
267
|
+
docker ps
|
|
268
|
+
|
|
269
|
+
# Clear wrangler cache
|
|
270
|
+
rm -rf .wrangler
|
|
271
|
+
|
|
272
|
+
# Reinstall dependencies
|
|
273
|
+
rm -rf node_modules package-lock.json
|
|
274
|
+
npm install
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
## Performance Tips
|
|
278
|
+
|
|
279
|
+
1. **Use Code Interpreter API** for better Python performance
|
|
280
|
+
2. **Implement caching** for frequently used code patterns
|
|
281
|
+
3. **Stream output** for long-running operations
|
|
282
|
+
4. **Use Durable Objects** for session persistence
|
|
283
|
+
5. **Deploy to multiple regions** (automatic with Workers)
|
|
284
|
+
|
|
285
|
+
## Template Information
|
|
286
|
+
|
|
287
|
+
- **Provider**: Cloudflare Workers + Sandbox SDK
|
|
288
|
+
- **Runtime**: V8 isolates with container sandboxes
|
|
289
|
+
- **Languages**: Python 3.x, Node.js
|
|
290
|
+
- **Timeout**: 30 seconds (Workers), configurable for Durable Objects
|
|
291
|
+
- **Memory**: 128MB default
|
|
292
|
+
- **Storage**: Ephemeral (use Durable Objects for persistence)
|
|
293
|
+
|
|
294
|
+
## Resources
|
|
295
|
+
|
|
296
|
+
- [Cloudflare Sandbox SDK Docs](https://developers.cloudflare.com/sandbox/)
|
|
297
|
+
- [Workers Documentation](https://developers.cloudflare.com/workers/)
|
|
298
|
+
- [Durable Objects Guide](https://developers.cloudflare.com/durable-objects/)
|
|
299
|
+
- [Wrangler CLI Reference](https://developers.cloudflare.com/workers/wrangler/)
|
|
300
|
+
- [Anthropic API Documentation](https://docs.anthropic.com/)
|
|
301
|
+
|
|
302
|
+
## Next Steps
|
|
303
|
+
|
|
304
|
+
After installation:
|
|
305
|
+
1. Set up Cloudflare account and get API credentials
|
|
306
|
+
2. Install Wrangler CLI: `npm install -g wrangler`
|
|
307
|
+
3. Configure secrets: `npx wrangler secret put ANTHROPIC_API_KEY`
|
|
308
|
+
4. Deploy your worker: `npx wrangler deploy`
|
|
309
|
+
5. Test with example requests
|
|
310
|
+
6. Customize sandbox configuration for your use case
|
|
311
|
+
|
|
312
|
+
## License
|
|
313
|
+
|
|
314
|
+
Uses Cloudflare Sandbox SDK (open source) and requires Cloudflare Workers Paid plan ($5/month) for Durable Objects support.
|