Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 48 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
E2B sandboxes for OpenCode. One package supports both OpenCode releases:

- **OpenCode 1** (`opencode-ai`): the plugin registers an `e2b` type for OpenCode's experimental workspace API. A workspace runs OpenCode itself in an E2B sandbox. The sections from [Install](#install) on describe this mode.
- **OpenCode 2** (`@opencode/cli`): OpenCode 2 has no workspace API for plugins. The plugin runs every shell command of your local OpenCode in an E2B sandbox instead, and keeps your files on your machine. See [OpenCode 2](#opencode-2).
- **OpenCode 2** (`@opencode/cli`): OpenCode 2 has no workspace API for plugins. The plugin runs every shell command of your local OpenCode in an E2B sandbox instead, and keeps your files on your machine. For long tasks, a box runs OpenCode itself in a sandbox. See [OpenCode 2](#opencode-2).

The package's default export carries both entry points: OpenCode 1 calls `server`, OpenCode 2 calls `setup`, and each loads only its own code.

Expand Down Expand Up @@ -39,6 +39,18 @@ E2B_API_KEY=... OPENCODE_EXPERIMENTAL_WORKSPACES=true opencode

## OpenCode 2

Two ways to use E2B with OpenCode 2:

- **Shell mode** (the plugin): OpenCode stays on your machine, and its shell commands run in a sandbox.
- **Boxes** (`opencode-e2b box`): OpenCode itself runs in a sandbox, for a task that runs on without your machine. You attach to it with your TUI and pull the work home as a branch.

### Shell mode

<picture>
<source media="(prefers-color-scheme: dark)" srcset="assets/shell-dark.png">
<img alt="Shell mode: OpenCode on your machine sends each shell command to the project sandbox, with your changes first, and gets the output and changed files back" src="assets/shell.png">
</picture>

OpenCode runs on your machine. Every shell command that it runs (the agent's and your `!` commands) runs in an E2B sandbox, at the same path as on your machine. Reading and editing files stays local, so diffs, undo and the file picker work as usual.

```jsonc
Expand Down Expand Up @@ -66,6 +78,7 @@ Git: a plain git command (`git add .`, `git commit -m "..."`, `git checkout -b x
| `domain` | `E2B_DOMAIN` or `e2b.app` | E2B region: `e2b.app` (US), `e2b-juliett.dev` (EU), `e2b-tango.dev` (APAC). The API key must belong to that region. |
| `template` | `opencode` | Template for new sandboxes |
| `idleMinutes` | `15` | Idle time before the sandbox pauses |
| `boxHours` | `1` | How long a box from `/e2b-box up` runs before it pauses. The E2B plan caps this. |
| `env` | `[]` | Names of environment variables to pass to commands. Nothing else from your environment goes to the sandbox. |
| `git` | `"auto"` | Send `.git` to the sandbox: `"auto"` up to 200 MB, `true` always, `false` never |
| `network` | none | `{ "allowOut": [...], "denyOut": [...] }` |
Expand All @@ -77,6 +90,38 @@ Commands: `/e2b-status`, `/e2b-pull <path>` (copy sandbox-only files such as `di

Limits: the project must be a git repository; submodule contents and a linked worktree's history are not synced; gitignored build output stays in the sandbox (use `e2b_read` or `/e2b-pull`).

### Boxes

<picture>
<source media="(prefers-color-scheme: dark)" srcset="assets/box-dark.png">
<img alt="Box mode: box up sends the project with its git history to a box running the OpenCode server; your TUI attaches to it; the box reaches the model API with a key that E2B adds; box pull brings the work home as branch e2b/name" src="assets/box.png">
</picture>

A box is a sandbox with your project and an OpenCode 2 server in it. The agent keeps working when your laptop sleeps. Each box is one task.

```bash
opencode-e2b secrets set anthropic # once: stores ANTHROPIC_API_KEY (or stdin) as an E2B secret
opencode-e2b box up --attach # start a box named after the branch, open the TUI on it
opencode-e2b box pull # the box's work, uncommitted files too, as branch e2b/<name>
opencode-e2b box kill
```

| Command | What it does |
| --- | --- |
| `box up [name]` | Uploads the project (tracked and uncommitted files, and `.git`) to the same path in a new sandbox, and starts `opencode2 serve` there. Options: `--template`, `--hours` (default 1), `--attach`. |
| `box attach [name]` | Wakes the box if it is paused, then runs your local `opencode2 --server <box>`. |
| `box pull [name]` | Commits the box's work in progress on a scratch ref (the agent's branch and index stay as they are) and fetches it as a git bundle into `e2b/<name>`. No credentials go to the box. |
| `box list`, `box stop`, `box kill` | List the boxes this machine started, pause one, delete one. |
| `secrets set\|list\|remove <provider>` | Model keys for boxes: `anthropic`, `openai`, `openrouter`. |

The name defaults to the current branch, or to the project's only box. In OpenCode, `/e2b-box up|pull|list|stop|kill` and `/e2b-secrets` do the same. `/e2b-secrets set` takes the key from OpenCode's environment, never from the prompt.

Model keys: a box gets a placeholder, and E2B's egress proxy puts the stored secret into requests to the provider's API. No process in the box holds the key. The box offers only the providers with a stored key, and commits with your git `user.name` and `user.email`.

Each box has a random server password. It is stored with the box record in `~/.local/state/opencode-e2b/boxes/` (readable only by you), and never sent to E2B as metadata.

A box pauses after `--hours`; attach wakes it. E2B plans cap the run time (1 hour on the lower plans).

## Development

```bash
Expand All @@ -86,7 +131,7 @@ bun run format:check # oxfmt
bun run test
```

`bun run check` runs all four. Lint rules are errors in `src/v2` and `src/index.ts`, and warnings in older code until each rule is cleaned up there. The anti-slop rules are vendored in `tools/oxlint/anti-slop` (see its `VENDOR.md`).
`bun run check` runs all four. The diagrams are built from `assets/diagrams/*.html` with `assets/diagrams/build.sh`. Lint rules are errors in `src/v2` and `src/index.ts`, and warnings in older code until each rule is cleaned up there. The anti-slop rules are vendored in `tools/oxlint/anti-slop` (see its `VENDOR.md`).

The plugin is loaded by OpenCode's Bun runtime and uses Bun as its development runtime. `bun run test` builds `dist/` first, then runs the Bun test runner against `test/*.test.ts`.

Expand All @@ -96,7 +141,7 @@ Layout:
| -------------- | ----------------------------------------------------------------------------------------- | ------------------------------- |
| `src/core/` | Shared E2B code: SDK setup and attribution, sandboxes, templates, cleanup, upload, config | No OpenCode packages |
| `src/v1/` | OpenCode 1: workspace adapter, bootstrap, compatibility checks | `@opencode-ai/plugin` |
| `src/v2/` | OpenCode 2: shell hook, sync, sandbox per project, tools and commands | `@opencode/plugin` (types only) |
| `src/v2/` | OpenCode 2: shell hook, sync, sandbox per project, tools and commands; boxes and the CLI | `@opencode/plugin` (types only) |
| `src/index.ts` | The default export with both entry points, each loaded on first use | type imports only |

## Runtime Behavior
Expand Down
Binary file added assets/box-dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/box.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading