opencode

package
v0.8.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 28, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package opencode implements the agent.Agent interface by shelling out to the OpenCode CLI. It reuses the shared prompt templates and helpers from internal/agent/prompt so its planning and review prompts are identical to the claude provider.

Key differences from the claude provider:

  • OpenCode's `run` subcommand reads the prompt from stdin when stdin is not a TTY (and concatenates a positional message + stdin when both are present). Execute and Review pipe the prompt via stdin to avoid the OS ARG_MAX limit for large prompts (e.g. PR diffs in Review).
  • There is no headless --system-prompt flag, so the review system prompt is prepended to the user message before being piped to stdin.
  • The TUI (`opencode --prompt`) used by Plan and Discuss cannot accept a piped prompt (its stdin is reserved for interactive input), so the prompt is passed via the --prompt flag. To prevent an opaque "argument list too long" failure, both Plan and Discuss guard against prompts larger than maxArgvPromptBytes with a clear error.
  • The model flag is --model with provider/model form (e.g. anthropic/claude-sonnet-4).
  • Permissions are auto-approved with --dangerously-skip-permissions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type OpenCodeAgent

type OpenCodeAgent struct {
	BinaryPath string // Path to `opencode` CLI (default: "opencode")
	Model      string // Model override in provider/model form (optional)
}

OpenCodeAgent implements agent.Agent using the OpenCode CLI.

func New

func New(binaryPath, model string) *OpenCodeAgent

New creates a new OpenCodeAgent with the given binary path and model. If binaryPath is empty, defaults to "opencode".

func (*OpenCodeAgent) Discuss

func (o *OpenCodeAgent) Discuss(ctx context.Context, opts agent.DiscussOptions) error

Discuss launches OpenCode in interactive TUI mode with a caller-supplied system prompt. Because OpenCode has no --system-prompt flag, the system prompt and the optional initial prompt are folded together and passed via --prompt. Returns an error only if the agent process fails to start or exits non-zero.

OpenCode's TUI cannot accept a piped stdin (its stdin is reserved for interactive input), so the combined prompt is passed in argv. To prevent an opaque "argument list too long" exec failure, Discuss rejects combined prompts larger than maxArgvPromptBytes with a clear error.

func (*OpenCodeAgent) Execute

Execute runs the OpenCode CLI in headless mode to complete a task. The task body (or opts.SystemPrompt, if set) is piped to the child process's stdin so that arbitrarily large prompts do not trip the OS ARG_MAX limit. The agent commits as it works in the repo.

If the agent returns suspicious output (empty, "Execution error", or very short), Execute retries once after prompt.RetryDelay before returning an error.

Note: OpenCode has no --max-turns flag; opts.MaxTurns is ignored for this provider.

func (*OpenCodeAgent) Plan

func (o *OpenCodeAgent) Plan(ctx context.Context, initialPrompt string, opts agent.PlanOptions) (*agent.Plan, error)

Plan launches OpenCode in interactive TUI mode for a planning session. Because OpenCode has no --system-prompt flag, the rendered planning system prompt and the optional initialPrompt are folded together and passed via --prompt. After the agent exits, the plan JSON is read from opts.OutputPath.

OpenCode's TUI cannot accept a piped stdin (its stdin is reserved for interactive input), so the combined prompt is passed in argv. To prevent an opaque "argument list too long" exec failure, Plan rejects combined prompts larger than maxArgvPromptBytes with a clear error.

func (*OpenCodeAgent) Review

Review runs the OpenCode CLI in headless mode to review a diff. Because OpenCode has no --system-prompt flag, the review system prompt is prepended to the rendered review message; the combined message is piped via stdin so large diffs do not trip the OS ARG_MAX limit.

Returns a structured review result parsed from the agent's JSON output; unparseable output yields ReviewResult{Approved:false, IsUnparseable:true} with a Summary beginning "Failed to parse".

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL