Skip to content

Add new OpenCode x Sandboxes demo with remote tool calls#534

Draft
aron-cf wants to merge 3 commits intomainfrom
opencode-demo
Draft

Add new OpenCode x Sandboxes demo with remote tool calls#534
aron-cf wants to merge 3 commits intomainfrom
opencode-demo

Conversation

@aron-cf
Copy link
Copy Markdown
Contributor

@aron-cf aron-cf commented Mar 27, 2026

Adds an experimental example showing OpenCode running inside a Cloudflare Sandbox, with all tool calls (bash, read, write, edit, grep, glob, list) routed through an isolated CodeSandbox rather than the container's own filesystem.

The demo aims to demonstrate how workers can provide a capnproto RPC API very easily to services (either internally or externally) that allow easy interaction with those primatives.

class SandboxRpcApi extends RpcTarget {
  #env: Env;

  constructor(env: Env) {
    super();
    this.#env = env;
  }

  async sandbox(sessionId: string) {
    return getSandbox(this.#env.SANDBOX, `session-${sessionId}`);
  }
}

Then consumed in the OpenCode plugin:

const api = await getSandbox();
    const result = await api
      .sandbox(context.sessionID)
      .exec(args.command, { timeout: args.timeout });

Also includes a small addition to @cloudflare/sandbox's OpenCode integration: exposes process on OpencodeServer and adds an onStart callback to OpencodeOptions, invoked only when the server is freshly started (not reused). The example uses this to stream OpenCode logs to the Worker's console.

Architecture

Two containers back the Worker:

  • OpenCodeSandbox — runs opencode serve with a custom plugin that opens a Cap'n Web WebSocket server on port 3001
  • CodeSandbox — one instance per OpenCode session; all tool calls are routed here via RPC

The Durable Object connects to the plugin over Cap'n Web and exposes a SandboxRpcApi that maps each tool call to the correct per-session CodeSandbox.

Testing

cd examples/experimental/opencode
npm run dev
opencode attach http://localhost:8787

Agent added 3 commits March 27, 2026 11:50
Add process field to OpencodeServer so callers can inspect or interact
with the underlying process. Add onStart callback to OpencodeOptions,
invoked only when the server is freshly created rather than reused.
Prototype showing OpenCode running inside a Cloudflare Sandbox, with
a custom plugin that routes tool calls (bash, read, write, edit, glob,
grep, list) through the sandbox API rather than the local filesystem.
@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Mar 27, 2026

⚠️ No Changeset found

Latest commit: c73bb6f

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Mar 27, 2026

Open in StackBlitz

npm i https://pkg.pr.new/cloudflare/sandbox-sdk/@cloudflare/sandbox@534

commit: c73bb6f

@github-actions
Copy link
Copy Markdown
Contributor

🐳 Docker Images Published

Variant Image
Default cloudflare/sandbox:0.0.0-pr-534-c73bb6f
Python cloudflare/sandbox:0.0.0-pr-534-c73bb6f-python
OpenCode cloudflare/sandbox:0.0.0-pr-534-c73bb6f-opencode
Musl cloudflare/sandbox:0.0.0-pr-534-c73bb6f-musl
Desktop cloudflare/sandbox:0.0.0-pr-534-c73bb6f-desktop

Usage:

FROM cloudflare/sandbox:0.0.0-pr-534-c73bb6f

Version: 0.0.0-pr-534-c73bb6f


📦 Standalone Binary

For arbitrary Dockerfiles:

COPY --from=cloudflare/sandbox:0.0.0-pr-534-c73bb6f /container-server/sandbox /sandbox
ENTRYPOINT ["/sandbox"]

Download via GitHub CLI:

gh run download 23646265198 -n sandbox-binary

Extract from Docker:

docker run --rm cloudflare/sandbox:0.0.0-pr-534-c73bb6f cat /container-server/sandbox > sandbox && chmod +x sandbox

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant