Skip to content

Commit 9466c98

Browse files
authored
Replace OpenCode Web UI with SDK client pattern (#120)
* Replace OpenCode Web UI section with SDK client pattern Switch from browser-based `opencode web` to programmatic `opencode serve` + SDK client approach, which better fits developers building agent workflows. * Fix OpenCode SDK usage and add server health check - Use JS/TS SDK only, Python tab uses plain HTTP requests - Add health check polling loop to wait for server startup - Fix session.create/prompt calls to match SDK docs * Remove auto-pause warning from OpenCode SDK section
1 parent c4cc2bd commit 9466c98

1 file changed

Lines changed: 64 additions & 29 deletions

File tree

docs/agents/opencode.mdx

Lines changed: 64 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -111,55 +111,90 @@ sandbox.kill()
111111
```
112112
</CodeGroup>
113113

114-
## Launch the web UI
114+
## Connect with the OpenCode SDK
115115

116-
OpenCode has a [built-in web interface](https://opencode.ai/docs/web/). Start it inside a sandbox and connect from your browser.
117-
118-
<Warning>
119-
This sandbox is created with a 10-minute timeout and auto-pause enabled — after 10 minutes of inactivity it pauses and can be resumed later. See [Sandbox Persistence](/docs/sandbox/persistence) and [Sandbox Lifecycle](/docs/sandbox) for more details.
120-
</Warning>
116+
OpenCode includes a [headless HTTP server](https://opencode.ai/docs/server/) that you can control programmatically using the [`@opencode-ai/sdk`](https://opencode.ai/docs/sdk/) client. Start the server inside a sandbox, get the public URL with `sandbox.getHost()`, and connect from your application.
121117

122118
<CodeGroup>
123119
```typescript JavaScript & TypeScript
124120
import { Sandbox } from 'e2b'
121+
import { createOpencodeClient } from '@opencode-ai/sdk'
125122

126123
const sandbox = await Sandbox.betaCreate('opencode', {
127-
envs: {
128-
ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY,
129-
OPENCODE_SERVER_PASSWORD: 'your-password',
130-
},
124+
envs: { ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY },
131125
autoPause: true,
132126
timeoutMs: 10 * 60 * 1000,
133127
})
134128

135-
// Start the web server
136-
sandbox.commands.run('opencode web --hostname 0.0.0.0 --port 4096')
129+
// Start the OpenCode server
130+
sandbox.commands.run('opencode serve --hostname 0.0.0.0 --port 4096', {
131+
background: true,
132+
})
137133

134+
// Wait for the server to be ready
138135
const host = sandbox.getHost(4096)
139-
const url = `https://${host}`
140-
console.log(`OpenCode Web UI: ${url}`)
141-
console.log(`Username: opencode`)
142-
console.log(`Password: your-password`)
143-
console.log(`Sandbox ID: ${sandbox.sandboxId}`)
136+
const baseUrl = `https://${host}`
137+
while (true) {
138+
try {
139+
await fetch(`${baseUrl}/global/health`)
140+
break
141+
} catch {
142+
await new Promise((r) => setTimeout(r, 500))
143+
}
144+
}
145+
146+
// Connect to the server
147+
const client = createOpencodeClient({
148+
baseUrl,
149+
})
150+
151+
// Create a session and send a prompt
152+
const { data: session } = await client.session.create({
153+
body: { title: 'E2B Session' },
154+
})
155+
const { data: result } = await client.session.prompt({
156+
path: { id: session.id },
157+
body: {
158+
parts: [{ type: 'text', text: 'Create a hello world HTTP server in Go' }],
159+
},
160+
})
161+
console.log(result)
144162
```
145163
```python Python
146164
import os
165+
import time
166+
import requests
147167
from e2b import Sandbox
148168

149169
sandbox = Sandbox.beta_create("opencode", envs={
150170
"ANTHROPIC_API_KEY": os.environ["ANTHROPIC_API_KEY"],
151-
"OPENCODE_SERVER_PASSWORD": "your-password",
152171
}, auto_pause=True, timeout=10 * 60)
153172

154-
# Start the web server
155-
sandbox.commands.run("opencode web --hostname 0.0.0.0 --port 4096")
173+
# Start the OpenCode server
174+
sandbox.commands.run(
175+
"opencode serve --hostname 0.0.0.0 --port 4096",
176+
background=True,
177+
)
156178

179+
# Wait for the server to be ready
157180
host = sandbox.get_host(4096)
158-
url = f"https://{host}"
159-
print(f"OpenCode Web UI: {url}")
160-
print(f"Username: opencode")
161-
print(f"Password: your-password")
162-
print(f"Sandbox ID: {sandbox.sandbox_id}")
181+
base_url = f"https://{host}"
182+
while True:
183+
try:
184+
requests.get(f"{base_url}/global/health")
185+
break
186+
except requests.ConnectionError:
187+
time.sleep(0.5)
188+
189+
# Create a session and send a prompt via the HTTP API
190+
session = requests.post(f"{base_url}/session").json()
191+
result = requests.post(
192+
f"{base_url}/session/{session['id']}/message",
193+
json={
194+
"parts": [{"type": "text", "text": "Create a hello world HTTP server in Go"}],
195+
},
196+
).json()
197+
print(result)
163198
```
164199
</CodeGroup>
165200

@@ -177,9 +212,9 @@ export const template = Template()
177212
.setEnvs({
178213
OPENCODE_SERVER_PASSWORD: 'your-password',
179214
})
180-
// Optional - start the OpenCode web server on sandbox start
215+
// Optional - start the OpenCode server on sandbox start
181216
.setStartCmd(
182-
'opencode web --hostname 0.0.0.0 --port 4096',
217+
'opencode serve --hostname 0.0.0.0 --port 4096',
183218
waitForPort(4096)
184219
)
185220
```
@@ -193,9 +228,9 @@ template = (
193228
.set_envs({
194229
"OPENCODE_SERVER_PASSWORD": "your-password",
195230
})
196-
# Optional - start the OpenCode web server on sandbox start
231+
# Optional - start the OpenCode server on sandbox start
197232
.set_start_cmd(
198-
"opencode web --hostname 0.0.0.0 --port 4096",
233+
"opencode serve --hostname 0.0.0.0 --port 4096",
199234
wait_for_port(4096)
200235
)
201236
)

0 commit comments

Comments
 (0)