feat(integrations): add OpenCode SDK integration - #2770
Conversation
|
|
This PR is from an external contributor and must be approved by a stagehand team member with write access before CI can run. |
There was a problem hiding this comment.
All reported issues were addressed across 13 files
Architecture diagram
sequenceDiagram
participant CLI as CLI Entry (src/agent.ts)
participant Runtime as OpenCode Runtime (runOpenCode)
participant SDK as OpenCode SDK (createOpencodeServer/Client)
participant Config as OpenCode Config
participant MCP as Stagehand Facade (stdio-server)
participant Browser as Browser Session
Note over CLI,Browser: OpenCode SDK integration - Runtime Flow
CLI->>Runtime: runOpenCode(instruction)
Runtime->>Runtime: createRuntimeDirectory()
Runtime->>Config: buildOpenCodeConfig(facadeServerPath, env)
Config->>Config: Build allowlisted env (STAGEHAND_*/BROWSERBASE_*)
Config->>Config: Enable only stagehand_run/snapshot/screenshot tools
Runtime->>SDK: startOpenCodeRuntime({config, directory, configRoot})
SDK->>SDK: Create temp config dirs + empty config file
SDK->>SDK: Set env overrides (XDG_CONFIG_HOME, OPENCODE_CONFIG)
SDK->>SDK: createOpencodeServer()
SDK-->>Runtime: Server URL
Runtime->>SDK: session.create({title})
SDK-->>Runtime: session ID
Runtime->>SDK: session.prompt({tools, system, parts})
SDK->>MCP: Spawn with allowlisted env
MCP->>Browser: Persistent session across calls
loop Tool calls (run/snapshot/screenshot)
MCP->>MCP: Execute model-authored JS
MCP-->>SDK: Tool results (incl. native MCP images)
end
SDK-->>Runtime: Assistant text parts
Runtime->>Runtime: extractAssistantText()
alt Successful run
Runtime-->>CLI: Return assistant text
else Prompt/creation failure
Runtime->>Runtime: Throw API error
end
Runtime->>SDK: session.delete()
Runtime->>SDK: server.close()
Runtime->>Runtime: Remove runtime directory
Note over CLI,SDK: Signal handling (SIGINT/SIGTERM)
CLI->>Runtime: Termination signal
Runtime->>Runtime: Forward to AbortController
Runtime->>SDK: Abort active request
Runtime->>SDK: Cleanup (delete session, close, remove dir)
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
There was a problem hiding this comment.
All reported issues were addressed across 3 files (changes from recent commits).
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
There was a problem hiding this comment.
All reported issues were addressed across 3 files (changes from recent commits).
Tip: Review your code locally with the cubic CLI to iterate faster.
Re-trigger cubic
There was a problem hiding this comment.
All reported issues were addressed across 2 files (changes from recent commits).
Tip: Review your code locally with the cubic CLI to iterate faster.
Re-trigger cubic
There was a problem hiding this comment.
All reported issues were addressed across 2 files (changes from recent commits).
Tip: Review your code locally with the cubic CLI to iterate faster.
Re-trigger cubic
why
Add an OpenCode SDK example alongside the existing Claude Code and Codex integrations, giving OpenCode sessions persistent Stagehand browser tools over MCP/stdio.
what changed
@opencode-ai/sdkintegration.run,snapshot, andscreenshot.STAGEHAND_*andBROWSERBASE_*variables to the MCP child.Summary by cubic
Adds an OpenCode SDK integration that mounts the Stagehand facade as an MCP/stdio server so OpenCode sessions share one persistent browser across
run,snapshot, andscreenshot. Previously OpenCode was unsupported; now a local SDK example and optional CLI config provide these tools with strict isolation.Example at
packages/integrations/opencodeuses@opencode-ai/sdkv2 to start a local server, create one session, prompt with onlystagehand_*tools, and clean up on completion or SIGINT/SIGTERM; the MCP child receives onlySTAGEHAND_*andBROWSERBASE_*env (provider credentials stay in OpenCode).Enables exactly
stagehand_run,stagehand_snapshot, andstagehand_screenshot; all other tools are disabled and denied.OPENCODE_MODELis optional for selecting the OpenCode model; Stagehand JavaScript helpers still useSTAGEHAND_MODEL_*credentials (docs clarify provider tokens are not reused). The includedopencode.jsonmounts the facade for theopencodeCLI; note the CLI forwards its shell env to the MCP child, unlike the SDK example’s allowlist.Tests cover env allowlist, tool permissions, session lifecycle, interruption, and cleanup. Docs add
v4/integrations/opencode, update the overview, and include an OpenCode icon. Workspace adds@opencode-ai/sdkandopencode-ai, enablesopencode-aibuilds, updatesturbo.json, and pins lockfile entries.No migration required. Requires Node 24+ and pnpm 11.10.0. Build
@browserbasehq/stagehand-integrations, authenticate OpenCode or export a provider key, and setSTAGEHAND_BROWSER/BROWSERBASE_*for Browserbase.Written for commit 9597ff8. Summary will update on new commits.