← Back to PRs

#16099: feat: add opencode-cli as CLI backend provider

by imwxc open 2026-02-14 08:29 View on GitHub →
docs gateway agents stale size: S
## Summary Add OpenCode CLI as a built-in CLI backend provider, enabling users to use OpenCode CLI models directly in OpenClaw. OpenCode CLI is an open-source AI coding assistant that supports multiple LLM providers (Anthropic, OpenAI, Google, DeepSeek, etc.) with MCP support. ## Changes ### 1. Core Implementation (`src/agents/cli-backends.ts`) - Added `OPENCODE_MODEL_ALIASES` for common model shortcuts - Added `DEFAULT_OPENCODE_BACKEND` configuration: - Command: `opencode run --format json` - Session support: `--session` flag for continuity - Model selection: `--model` flag - Image support: `--file` flag - Output format: JSONL (streaming events) - Clears API keys from environment for security - Updated `resolveCliBackendIds()` to include `opencode-cli` - Updated `resolveCliBackendConfig()` to handle `opencode-cli` provider ### 2. Model Selection (`src/agents/model-selection.ts`) - Added `opencode-cli` to `isCliProvider()` function ### 3. Documentation (`docs/gateway/cli-backends.md`) - Added quick start example for OpenCode CLI - Added default configuration documentation - Updated read_when section ## Usage ```bash # Quick start - no config needed openclaw agent --message "hi" --model opencode-cli/anthropic/claude-sonnet-4-5 # Different providers openclaw agent --message "hi" --model opencode-cli/openai/gpt-4o openclaw agent --message "hi" --model opencode-cli/gemini/gemini-2.5-pro # As fallback { agents: { defaults: { model: { primary: "anthropic/claude-opus-4-6", fallbacks: ["opencode-cli/anthropic/claude-sonnet-4-5"], }, }, }, } ``` ## Technical Details OpenCode CLI JSON output format (verified): ```json {"type":"step_start","sessionID":"ses_xxx",...} {"type":"text","part":{"text":"response text",...} {"type":"step_finish",...} ``` Session ID is extracted from `step_start.sessionID` field. ## Testing - Verified OpenCode CLI JSON output format with `opencode run --format json` - Configuration follows existing patterns from claude-cli and codex-cli backends --- **Note**: This PR was created with AI assistance. <!-- greptile_comment --> <h3>Greptile Summary</h3> Adds OpenCode CLI as a third built-in CLI backend provider (alongside `claude-cli` and `codex-cli`). The implementation follows existing patterns closely: a new `DEFAULT_OPENCODE_BACKEND` config in `cli-backends.ts`, registration in `resolveCliBackendIds()` and `resolveCliBackendConfig()`, provider recognition in `isCliProvider()`, and JSONL parser support for OpenCode's `part.text` response format. - Model aliases map short names (e.g. `claude-sonnet-4-5`) to OpenCode's `provider/model` format (e.g. `anthropic/claude-sonnet-4-5`), and full paths like `opencode-cli/anthropic/claude-sonnet-4-5` pass through correctly - Session flow is sound: first call has no session (due to `sessionMode: "existing"`), session ID is extracted from `step_start.sessionID` in the response, subsequent calls use `resumeArgs` with `{sessionId}` substitution - The `parseCliJsonl` extension correctly handles the previous thread's concern about `part.text` extraction - `clearEnv` covers a broad set of API keys to prevent credential leakage to the subprocess - Documentation additions are consistent with existing style <h3>Confidence Score: 4/5</h3> - This PR is safe to merge — it follows established patterns and the changes are additive with no modifications to existing behavior. - The implementation closely follows the existing patterns for claude-cli and codex-cli backends. All four changed files are straightforward additions. The JSONL parser extension for `part.text` is the only shared-code change and it is additive (won't affect existing codex-cli parsing). No tests are broken since no unit tests exist for these modules. The session flow and arg-building logic were verified against the cli-runner code. Score is 4 rather than 5 because this is a new integration that would benefit from runtime verification with actual OpenCode CLI output. - No files require special attention. The `src/agents/cli-runner/helpers.ts` change is the most impactful since it modifies shared JSONL parsing, but the addition is purely additive. <sub>Last reviewed commit: 0fa1a34</sub> <!-- greptile_other_comments_section --> <!-- /greptile_comment -->

Most Similar PRs