|
| 1 | +# OpenSandbox Guide |
| 2 | + |
| 3 | +KODE SDK supports OpenSandbox as a sandbox backend for isolated command execution and file operations. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## Overview |
| 8 | + |
| 9 | +| Feature | Description | |
| 10 | +|---------|-------------| |
| 11 | +| **Deployment** | Self-hosted OpenSandbox server | |
| 12 | +| **Runtime** | Container-based isolated execution environment | |
| 13 | +| **Lifecycle** | Create/connect/dispose by `sandboxId` | |
| 14 | +| **Compatibility** | Works with existing `bash_*` and `fs_*` tools | |
| 15 | + |
| 16 | +### When to Use OpenSandbox vs E2B vs Local |
| 17 | + |
| 18 | +| Scenario | Recommended | |
| 19 | +|----------|-------------| |
| 20 | +| You need self-hosted control in your own infra | OpenSandbox | |
| 21 | +| You want fully managed cloud sandbox | E2B | |
| 22 | +| Local development and offline debugging | Local Sandbox | |
| 23 | + |
| 24 | +--- |
| 25 | + |
| 26 | +## Prerequisites |
| 27 | + |
| 28 | +1. Docker daemon is running and can pull required images. |
| 29 | +2. OpenSandbox server is running (for example on `http://127.0.0.1:8080`). |
| 30 | +3. If your server enables auth, prepare an API key. |
| 31 | + |
| 32 | +Optional environment variables: |
| 33 | + |
| 34 | +```bash |
| 35 | +export OPEN_SANDBOX_API_KEY=... # optional, only when auth is enabled |
| 36 | +export OPEN_SANDBOX_ENDPOINT=http://127.0.0.1:8080 # optional |
| 37 | +export OPEN_SANDBOX_IMAGE=ubuntu # optional |
| 38 | +``` |
| 39 | + |
| 40 | +--- |
| 41 | + |
| 42 | +## Quick Start |
| 43 | + |
| 44 | +### Create and Use a Sandbox |
| 45 | + |
| 46 | +```typescript |
| 47 | +import { OpenSandbox } from '@shareai-lab/kode-sdk'; |
| 48 | + |
| 49 | +const sandbox = new OpenSandbox({ |
| 50 | + kind: 'opensandbox', |
| 51 | + apiKey: process.env.OPEN_SANDBOX_API_KEY, |
| 52 | + endpoint: process.env.OPEN_SANDBOX_ENDPOINT, |
| 53 | + image: process.env.OPEN_SANDBOX_IMAGE || 'ubuntu', |
| 54 | + timeoutMs: 600_000, |
| 55 | + execTimeoutMs: 120_000, |
| 56 | + useServerProxy: false, |
| 57 | + watch: { mode: 'polling', pollIntervalMs: 1000 }, |
| 58 | + lifecycle: { disposeAction: 'kill' }, |
| 59 | +}); |
| 60 | + |
| 61 | +await sandbox.init(); |
| 62 | +console.log('sandboxId:', sandbox.getSandboxId()); |
| 63 | + |
| 64 | +const result = await sandbox.exec('echo "hello opensandbox"'); |
| 65 | +console.log(result.code, result.stdout.trim()); |
| 66 | + |
| 67 | +await sandbox.fs.write('demo.txt', 'hello from opensandbox'); |
| 68 | +const content = await sandbox.fs.read('demo.txt'); |
| 69 | +console.log(content.trim()); |
| 70 | + |
| 71 | +await sandbox.dispose(); |
| 72 | +``` |
| 73 | + |
| 74 | +--- |
| 75 | + |
| 76 | +## Configuration |
| 77 | + |
| 78 | +### OpenSandboxOptions |
| 79 | + |
| 80 | +```typescript |
| 81 | +interface OpenSandboxOptions { |
| 82 | + kind: 'opensandbox'; |
| 83 | + apiKey?: string; |
| 84 | + endpoint?: string; |
| 85 | + domain?: string; |
| 86 | + protocol?: 'http' | 'https'; |
| 87 | + sandboxId?: string; |
| 88 | + image?: string; |
| 89 | + template?: string; // alias of image in current implementation |
| 90 | + workDir?: string; // default '/workspace' |
| 91 | + timeoutMs?: number; |
| 92 | + execTimeoutMs?: number; |
| 93 | + requestTimeoutSeconds?: number; |
| 94 | + useServerProxy?: boolean; // default false |
| 95 | + env?: Record<string, string>; |
| 96 | + metadata?: Record<string, string>; |
| 97 | + resource?: Record<string, string>; |
| 98 | + networkPolicy?: Record<string, any>; |
| 99 | + skipHealthCheck?: boolean; |
| 100 | + readyTimeoutSeconds?: number; |
| 101 | + healthCheckPollingInterval?: number; |
| 102 | + watch?: { |
| 103 | + mode?: 'native' | 'polling' | 'off'; // default 'polling' |
| 104 | + pollIntervalMs?: number; // default 1000 |
| 105 | + }; |
| 106 | + lifecycle?: { |
| 107 | + disposeAction?: 'close' | 'kill'; // default 'kill' |
| 108 | + }; |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +### Environment Variables |
| 113 | + |
| 114 | +| Variable | Description | |
| 115 | +|----------|-------------| |
| 116 | +| `OPEN_SANDBOX_API_KEY` | Optional API key (required only when server auth is enabled) | |
| 117 | +| `OPEN_SANDBOX_ENDPOINT` | OpenSandbox server endpoint | |
| 118 | +| `OPEN_SANDBOX_IMAGE` | Default image when creating a new sandbox | |
| 119 | + |
| 120 | +--- |
| 121 | + |
| 122 | +## Agent Integration |
| 123 | + |
| 124 | +### Use Sandbox Config (recommended) |
| 125 | + |
| 126 | +```typescript |
| 127 | +const agent = await Agent.create({ |
| 128 | + templateId: 'coder', |
| 129 | + sandbox: { |
| 130 | + kind: 'opensandbox', |
| 131 | + endpoint: process.env.OPEN_SANDBOX_ENDPOINT, |
| 132 | + apiKey: process.env.OPEN_SANDBOX_API_KEY, |
| 133 | + image: 'debian:latest', |
| 134 | + lifecycle: { disposeAction: 'kill' }, |
| 135 | + }, |
| 136 | +}, deps); |
| 137 | +``` |
| 138 | + |
| 139 | +When you pass sandbox config, `SandboxFactory.createAsync()` initializes OpenSandbox automatically. |
| 140 | + |
| 141 | +### Use Sandbox Instance |
| 142 | + |
| 143 | +```typescript |
| 144 | +const sandbox = new OpenSandbox({ kind: 'opensandbox', endpoint: 'http://127.0.0.1:8080' }); |
| 145 | +await sandbox.init(); |
| 146 | + |
| 147 | +const agent = await Agent.create({ templateId: 'coder', sandbox }, deps); |
| 148 | +``` |
| 149 | + |
| 150 | +When you pass sandbox instance directly, call `sandbox.init()` yourself before `Agent.create()`. |
| 151 | + |
| 152 | +### Resume with `sandboxId` |
| 153 | + |
| 154 | +```typescript |
| 155 | +const sandbox = new OpenSandbox({ kind: 'opensandbox', endpoint: 'http://127.0.0.1:8080' }); |
| 156 | +await sandbox.init(); |
| 157 | +const id = sandbox.getSandboxId(); |
| 158 | + |
| 159 | +const restored = new OpenSandbox({ |
| 160 | + kind: 'opensandbox', |
| 161 | + endpoint: 'http://127.0.0.1:8080', |
| 162 | + sandboxId: id, |
| 163 | +}); |
| 164 | +await restored.init(); |
| 165 | +``` |
| 166 | + |
| 167 | +--- |
| 168 | + |
| 169 | +## Watch and Lifecycle Semantics |
| 170 | + |
| 171 | +1. `watch.mode='native'` uses `inotifywait` in the sandbox container. |
| 172 | +2. If `inotifywait` is unavailable or native stream exits unexpectedly, the SDK auto-falls back to polling mode. |
| 173 | +3. `watch.mode='off'` disables file watch registration. |
| 174 | +4. `disposeAction='kill'` performs `kill()` first, then `close()`. |
| 175 | +5. `disposeAction='close'` only closes the connection. |
| 176 | +6. Polling watch is level-triggered by mtime delta and may coalesce multiple writes within one polling interval. |
| 177 | +7. Polling watch does not guarantee one callback per write operation; treat events as "file changed" hints. |
| 178 | + |
| 179 | +--- |
| 180 | + |
| 181 | +## Troubleshooting |
| 182 | + |
| 183 | +1. `DOCKER::SANDBOX_IMAGE_PULL_FAILED` or `DOCKER::SANDBOX_EXECD_START_FAILED`: Docker cannot pull required images (`image` and `opensandbox/execd`). |
| 184 | +2. Verify OpenSandbox server endpoint is reachable from the SDK process. |
| 185 | +3. If you use a proxy, verify Docker daemon proxy and OpenSandbox server network settings separately. |
0 commit comments