|
| 1 | +# OpenCode on Tenki (headless server on a public URL) |
| 2 | + |
| 3 | +Boot a disposable [Tenki Sandbox](https://tenki.cloud/products/sandbox) microVM with [OpenCode](https://opencode.ai) and a Git checkout already inside it, start the agent's headless server, and expose it on a public HTTPS URL — a coding agent, and its web UI, running on a machine you can throw away. No install step: `enableOpenCode` bakes the CLI into the guest. |
| 4 | + |
| 5 | +## The code (`run.mjs`) |
| 6 | + |
| 7 | +```js |
| 8 | +import { TenkiSandbox } from "@tenkicloud/sandbox"; |
| 9 | +import { randomBytes } from "node:crypto"; |
| 10 | + |
| 11 | +const REPO = "https://github.com/sindresorhus/yocto-queue"; |
| 12 | +const PORT = 4096; |
| 13 | +const PASSWORD = randomBytes(24).toString("hex"); // this URL is public; see the note below |
| 14 | + |
| 15 | +const tenki = new TenkiSandbox({ authToken: process.env.TENKI_AUTH_TOKEN }); |
| 16 | + |
| 17 | +// enableOpenCode bakes the CLI into the guest — nothing to npm install. |
| 18 | +// openCodeProvider.apiKey lands in the guest as OPENCODE_API_KEY. |
| 19 | +await using sandbox = await tenki.createAndWait({ |
| 20 | + cpuCores: 2, |
| 21 | + memoryMb: 4096, |
| 22 | + enableOpenCode: true, |
| 23 | + openCodeProvider: { apiKey: process.env.OPENAI_API_KEY }, |
| 24 | + allowInbound: true, |
| 25 | + cloneRepoUrl: REPO, |
| 26 | + workspaceId: process.env.TENKI_WORKSPACE_ID, |
| 27 | +}); |
| 28 | + |
| 29 | +// --hostname 0.0.0.0 is load-bearing: the default 127.0.0.1 is unreachable from the gateway. |
| 30 | +// setsid + redirected stdio keeps the server running after this exec() returns. |
| 31 | +await sandbox.exec("sh", { |
| 32 | + args: ["-c", `cd repo && setsid opencode serve --port ${PORT} --hostname 0.0.0.0 >/tmp/opencode.log 2>&1 </dev/null & sleep 5`], |
| 33 | + env: { OPENCODE_SERVER_PASSWORD: PASSWORD }, |
| 34 | +}); |
| 35 | + |
| 36 | +const { previewUrl } = await sandbox.exposePort(PORT); |
| 37 | +// Auth is HTTP Basic — any username, the password above. A Bearer token is rejected. |
| 38 | +const auth = { Authorization: "Basic " + Buffer.from(`opencode:${PASSWORD}`).toString("base64") }; |
| 39 | + |
| 40 | +const project = await (await fetch(`${previewUrl}/project/current`, { headers: auth })).json(); |
| 41 | +console.log(`${project.worktree} (${project.vcs}) is live at ${previewUrl}/app`); |
| 42 | +``` |
| 43 | + |
| 44 | +Open the printed `/app` URL in a browser and you are looking at OpenCode's own UI, driving a repo inside the microVM. The same URL serves the JSON API — `/session`, `/agent`, `/project/current` — so a script can drive it just as easily. |
| 45 | + |
| 46 | +## Run it |
| 47 | + |
| 48 | +```bash |
| 49 | +npm install |
| 50 | +export TENKI_AUTH_TOKEN=... # from `tenki login` (~/.config/tenki/config.yaml) |
| 51 | +export TENKI_WORKSPACE_ID=... |
| 52 | +export OPENAI_API_KEY=sk-... # or any provider OpenCode supports, via openCodeProvider |
| 53 | +node run.mjs # -> /home/tenki/repo (git) is live at https://....sb.tenki.sh/app |
| 54 | +``` |
| 55 | + |
| 56 | +`run.mjs` uses top-level `await using`, which needs Node 24+. |
| 57 | + |
| 58 | +Verify the Tenki half without a model key — this is what CI runs: |
| 59 | + |
| 60 | +```bash |
| 61 | +node verify.mjs # boot → serve → exposePort → assert 401 unauthed, then 200 + the checkout |
| 62 | +``` |
| 63 | + |
| 64 | +## Notes |
| 65 | + |
| 66 | +- **Set `OPENCODE_SERVER_PASSWORD`, always.** Without it the server logs `OPENCODE_SERVER_PASSWORD is not set; server is unsecured` and answers every caller — and `exposePort` has just put it on the public internet, so that is an open coding agent with a shell. With it set, unauthenticated requests get `401`. Auth is HTTP **Basic** (any username, that password); a `Bearer` token is rejected. `verify.mjs` asserts the `401` precisely so this cannot regress unnoticed. |
| 67 | +- **Port 7681 is spoken for.** Tenki's own `ttyd` console listens there, and `exposePort(7681)` fails with `[invalid_argument] port 7681 cannot be exposed as a preview`. Pick any other port for your server. |
| 68 | +- **`--hostname 0.0.0.0` is required.** `opencode serve` defaults to `127.0.0.1`, which the gateway cannot reach, so the preview URL would just hang. |
| 69 | +- `enableOpenCode: true` puts `opencode` (1.17.20 at the time of writing) at `/usr/local/bin/opencode` before the sandbox reports ready — no `npm install`, no custom image. `openCodeProvider.apiKey` and `.baseUrl` arrive in the guest as `OPENCODE_API_KEY` and `OPENCODE_PROVIDER_BASE_URL`. |
| 70 | +- `cloneRepoUrl` checks out to `./repo`, so the server is started with `cd repo` — that is what makes `/project/current` report `worktree: /home/tenki/repo` with `vcs: git` instead of an empty directory. |
| 71 | +- For the same agent driven as a one-shot CLI instead of a server, `opencode run "<task>"` works too — the shape used by [claude-code-sandbox](../claude-code-sandbox/) and [codex-sandbox](../codex-sandbox/). |
0 commit comments