Skip to content

Conversation

@ghostwriternr
Copy link
Member

@ghostwriternr ghostwriternr commented Dec 8, 2025

Summary

Adds support for running OpenCode (AI coding agent) inside Sandbox containers with three functions:

  • createOpencodeServer() for server lifecycle management
  • createOpencode() for programmatic SDK access (server + typed client)
  • proxyToOpencode() for web UI request proxying

Usage

Web UI proxy:

import { createOpencodeServer, proxyToOpencode } from '@cloudflare/sandbox/opencode'

export default {
  async fetch(request, env) {
    const sandbox = getSandbox(env.Sandbox, 'opencode')
    const server = await createOpencodeServer(sandbox, {
      directory: '/home/user/project',
      config: { provider: { anthropic: { options: { apiKey: env.ANTHROPIC_API_KEY } } } }
    })
    return proxyToOpencode(request, sandbox, server)
  }
}

Programmatic SDK access:

import { createOpencode } from '@cloudflare/sandbox/opencode'

const { client, server } = await createOpencode<OpencodeClient>(sandbox, {
  directory: '/home/user/project',
  config: { provider: { anthropic: { options: { apiKey: env.ANTHROPIC_API_KEY } } } }
})

const session = await client.session.create({ body: { title: 'Task' } })
await client.session.prompt({ path: { id: session.data.id }, body: { ... } })

Implementation details

  • Custom fetch adapter routes SDK requests through sandbox.containerFetch()
  • Detects and reuses existing OpenCode server process on the same port
  • Extracts API keys from config and sets as env vars (OpenCode expects ANTHROPIC_API_KEY etc.)
  • Handles localhost redirect quirk for proper API routing in web UI
  • Peer dependency on @opencode-ai/sdk (optional install)
  • Example container pre-clones sample project for immediate use

Adds support for running OpenCode (AI coding agent) inside Sandbox
containers with two usage patterns:

1. Programmatic SDK access via createOpencode():
   - Returns typed client from @opencode-ai/sdk
   - Custom fetch adapter routes requests through container
   - Automatic server lifecycle management

2. Web UI proxy via proxyToOpencode():
   - Exposes OpenCode's web interface through Workers
   - Handles localhost redirect for proper API routing

Features:
- Process reuse: detects existing OpenCode server on port
- Provider auth: extracts API keys from config to env vars
- Peer dependency on @opencode-ai/sdk (optional install)

Includes example worker demonstrating both patterns.
@changeset-bot
Copy link

changeset-bot bot commented Dec 8, 2025

🦋 Changeset detected

Latest commit: 1a9d1fe

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@cloudflare/sandbox Patch

Not sure what this means? Click here to learn what changesets are.

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

@pkg-pr-new
Copy link

pkg-pr-new bot commented Dec 8, 2025

Open in StackBlitz

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

commit: 1a9d1fe

@github-actions
Copy link
Contributor

github-actions bot commented Dec 8, 2025

🐳 Docker Images Published

Default (no Python):

FROM cloudflare/sandbox:0.0.0-pr-282-9de5893

With Python:

FROM cloudflare/sandbox:0.0.0-pr-282-9de5893-python

With OpenCode:

FROM cloudflare/sandbox:0.0.0-pr-282-9de5893-opencode

Version: 0.0.0-pr-282-9de5893

Use the -python variant for Python code execution, or -opencode for the OpenCode AI coding agent.

agents-git-bot bot pushed a commit to cloudflare/cloudflare-docs that referenced this pull request Dec 8, 2025
Adds comprehensive documentation for the new OpenCode integration:
- How-to guide covering both web UI and programmatic SDK usage
- Complete API reference for createOpencode() and proxyToOpencode()
- Configuration examples and error handling patterns
- Production deployment considerations

Related to cloudflare/sandbox-sdk#282

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
claude[bot]

This comment was marked as outdated.

- Use `unknown` instead of `any` for dynamic SDK import
- Add typed mock interfaces for Process and Sandbox in tests
- Add tests for process reuse logic and API key extraction
claude[bot]

This comment was marked as outdated.

agents-git-bot bot pushed a commit to cloudflare/cloudflare-docs that referenced this pull request Dec 8, 2025
Documents two usage patterns for running OpenCode AI coding agent
inside Sandbox containers:

- Web UI proxy with proxyToOpencode() for browser-based experience
- Programmatic SDK access with createOpencode() for automation

Includes configuration, error handling, and combined usage examples.

Relates to cloudflare/sandbox-sdk#282
claude[bot]

This comment was marked as outdated.

- Add -opencode Docker image variant with OpenCode CLI pre-installed
- Handle concurrent startup attempts gracefully with retry logic
- Add error handling in proxyToOpencode with proper error responses
- Add OPENCODE_STARTUP_FAILED error code to shared error codes
- Add logging throughout OpenCode server lifecycle
- Use exact command matching for process detection
- Add E2E tests for OpenCode CLI availability and server lifecycle
- Update CI workflows to build -opencode image variant
@ghostwriternr ghostwriternr force-pushed the feature/opencode-integration branch from c79d553 to 3c8d0e2 Compare December 8, 2025 12:42
claude[bot]

This comment was marked as outdated.

- Use @opencode-ai/sdk Config type instead of Record<string, unknown>
- Fix API key extraction path (options.apiKey)
- Add OpencodeStartupContext for structured error context
- Remove Logger from public options (internal concern)
- Add internal logging for debugging without exposing in API
- Fix Sandbox<any> to Sandbox<unknown>
@ghostwriternr ghostwriternr force-pushed the feature/opencode-integration branch from 1b3340f to b4eee16 Compare December 8, 2025 14:56
@ghostwriternr ghostwriternr enabled auto-merge (squash) December 8, 2025 14:58
claude[bot]

This comment was marked as outdated.

@ghostwriternr ghostwriternr force-pushed the feature/opencode-integration branch from b4eee16 to 3b748af Compare December 8, 2025 15:33
claude[bot]

This comment was marked as outdated.

@ghostwriternr ghostwriternr force-pushed the feature/opencode-integration branch from 704dc49 to dac1cc1 Compare December 9, 2025 19:56
claude[bot]

This comment was marked as outdated.

Refactors the integration to match OpenCode's own SDK structure with
createOpencodeServer for server lifecycle and proxyToOpencode for web
UI requests. Adds directory option and pre-clones sample project.
@ghostwriternr ghostwriternr force-pushed the feature/opencode-integration branch from dac1cc1 to c42a9e7 Compare December 10, 2025 17:37
claude[bot]

This comment was marked as outdated.

agents-git-bot bot pushed a commit to cloudflare/cloudflare-docs that referenced this pull request Dec 10, 2025
Adds comprehensive documentation for the new OpenCode integration feature:

- API reference for createOpencodeServer(), createOpencode(), and proxyToOpencode()
- How-to guide for both web UI and programmatic SDK access
- Updated Dockerfile reference to document image variants (-opencode, -python)

🤖 Generated with Claude Code
Synced from: cloudflare/sandbox-sdk#282

Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
Validate that config.provider is an object before iterating, preventing
crashes when users pass malformed config like strings or arrays. Add
tests for proxyToOpencode redirect logic and malformed config handling.
claude[bot]

This comment was marked as outdated.

claude[bot]

This comment was marked as resolved.

@ghostwriternr ghostwriternr merged commit d3997a8 into main Dec 10, 2025
14 of 15 checks passed
@ghostwriternr ghostwriternr deleted the feature/opencode-integration branch December 10, 2025 20:09
@github-actions github-actions bot mentioned this pull request Dec 10, 2025
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