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
Resolve overview.mdx conflicts to include both OpenCode and fx entries.
|
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
|
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 a local MCP/stdio server so one OpenCode session keeps a persistent browser across run, snapshot, and screenshot. Previously OpenCode was unsupported; now an SDK example and optional CLI config enable these tools with explicit environment isolation.
packages/integrations/opencodeuses@opencode-ai/sdkv2 to start a local server, create one session, prompt with onlystagehand_*tools, and clean up; the MCP child receives onlySTAGEHAND_*andBROWSERBASE_*env (provider creds stay in OpenCode).stagehand_run,stagehand_snapshot, andstagehand_screenshot; all other tools are disabled and denied.OPENCODE_MODELis optional; Stagehand JS helpers still use separateSTAGEHAND_MODEL_*credentials.opencode.jsonfor theopencodeCLI; note the CLI forwards its shell env to the MCP child (unlike the SDK example's allowlist).buildAllowlistedEnvto accept an optional env source so the OpenCode example reuses it. Tests cover env allowlist, tool permissions, session lifecycle, interruption, and cleanup.v4/integrations/opencode, the icon, and update the overview to include both OpenCode and fx; the integrations README lists OpenCode. Workspace adds@opencode-ai/sdkandopencode-ai, enablesopencode-aibuilds, updatesturbo.json, and pins lockfile entries.@browserbasehq/stagehand-integrations, authenticate OpenCode or export a provider key, and setSTAGEHAND_BROWSER/BROWSERBASE_*for Browserbase.Written for commit 16174c2. Summary will update on new commits.