opencode

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Overview

Package opencode implements a genai provider backed by the OpenCode CLI.

Instead of making HTTP requests directly, it launches `opencode acp` as a subprocess and communicates over stdin/stdout using the ACP (Agent Client Protocol) JSON-RPC 2.0 protocol.

See https://opencode.ai for OpenCode documentation and https://agentclientprotocol.com for the ACP specification.

Protocol

The provider performs a JSON-RPC handshake (initialize → session/new) on each call, then sends a session/prompt request and reads session/update notifications until the session/prompt response arrives.

Session / multi-turn

Each GenSync or GenStream call launches a fresh subprocess. The session ID is always returned inside Reply.Opaque["session_id"]. When the message history contains a previous session ID, it is automatically picked up: session/load is used instead of session/new and only the last user message is sent.

Example
package main

import (
	"context"
	"fmt"
	"log"

	"github.com/maruel/genai"
	"github.com/maruel/genai/providers/opencode"
)

func main() {
	c, err := opencode.New(context.Background(), genai.ProviderOptionModel("opencode/big-pickle"))
	if c != nil {
		defer func() {
			if err := c.Close(); err != nil {
				log.Printf("Close: %v", err)
			}
		}()
	}
	if err != nil {
		log.Print(err)
		return
	}
	res, err := c.GenSync(
		context.Background(),
		genai.Messages{genai.NewTextMessage("Say hello")},
		genai.EffortXHigh,
	)
	if err != nil {
		log.Print(err)
		return
	}
	fmt.Println(res.Replies[0].Text)
}

Index

Examples

Constants

View Source
const (
	ConfigOptionEffort = acp.ConfigOptionID("effort")
	ConfigOptionMode   = acp.ConfigOptionID("mode")
)

Session configuration option IDs exposed by OpenCode.

Variables

This section is empty.

Functions

func Scoreboard

func Scoreboard() scoreboard.Score

Scoreboard for the OpenCode CLI provider.

Types

type Client

type Client struct {
	base.NotImplemented
	// contains filtered or unexported fields
}

Client is a genai provider that delegates to the local `opencode` CLI.

func New

func New(_ context.Context, opts ...genai.ProviderOption) (*Client, error)

New creates a Client for the `opencode` CLI.

The binary is located lazily on the first call to GenSync, GenStream, or Ping, so New succeeds even when the CLI is not installed.

Supported ProviderOptions:

  • genai.ProviderOptionModel — model ID (e.g. "opencode/big-pickle"). genai.ModelCheap, genai.ModelGood, and genai.ModelSOTA are rejected.
  • *ProviderOption — process settings.
  • genai.ProviderOptionStarterWrapper — intercepts subprocess creation.

func (*Client) Capabilities

func (c *Client) Capabilities() genai.ProviderCapabilities

Capabilities implements genai.Provider.

func (*Client) Close added in v0.9.0

func (c *Client) Close() error

Close implements io.Closer. It currently does nothing.

func (*Client) GenStream

func (c *Client) GenStream(ctx context.Context, msgs genai.Messages, opts ...genai.GenOption) (iter.Seq[genai.Reply], func() (genai.Result, error))

GenStream implements genai.Provider.

func (*Client) GenSync

func (c *Client) GenSync(ctx context.Context, msgs genai.Messages, opts ...genai.GenOption) (r genai.Result, err error)

GenSync implements genai.Provider.

func (*Client) HTTPClient

func (c *Client) HTTPClient() *http.Client

HTTPClient implements genai.Provider. The CLI provider does not use HTTP.

func (*Client) ListModels

func (c *Client) ListModels(ctx context.Context) ([]genai.Model, error)

ListModels implements genai.Provider by querying the OpenCode ACP server's available models. It launches a subprocess, performs the handshake, and returns the model list from session/new.

func (*Client) ModelID

func (c *Client) ModelID() string

ModelID implements genai.Provider.

func (*Client) Name

func (c *Client) Name() string

Name implements genai.Provider.

func (*Client) OutputModalities

func (c *Client) OutputModalities() genai.Modalities

OutputModalities implements genai.Provider.

func (*Client) Ping

func (c *Client) Ping(ctx context.Context) error

Ping implements genai.ProviderPing by running `opencode --version`.

func (*Client) Scoreboard

func (c *Client) Scoreboard() scoreboard.Score

Scoreboard implements genai.Provider.

type Effort added in v0.7.1

type Effort string

Effort is an OpenCode reasoning-effort value serialized in the ACP effort configuration option.

Availability is provider- and model-specific. The provider validates it against the effort values returned by the ACP session.

const (
	EffortDefault Effort = "default"
	EffortNone    Effort = "none"
	EffortMinimal Effort = "minimal"
	EffortLow     Effort = "low"
	EffortMedium  Effort = "medium"
	EffortHigh    Effort = "high"
	EffortXHigh   Effort = "xhigh"
	EffortMax     Effort = "max"
)

Reasoning effort levels supported by OpenCode model variants.

type GenOption added in v0.7.1

type GenOption struct {
	// Mode selects an OpenCode session mode returned by ACP. An empty value
	// leaves OpenCode's current mode intact.
	Mode Mode
	// contains filtered or unexported fields
}

GenOption configures one prompt of a running opencode session.

func (*GenOption) Validate added in v0.7.1

func (g *GenOption) Validate() error

Validate implements genai.GenOption.

type Mode added in v0.7.1

type Mode string

Mode is an OpenCode session-mode value serialized in the ACP mode configuration option.

type ProviderOption added in v0.9.0

type ProviderOption struct {
	// DangerouslySkipPermissions approves every permission request the agent
	// makes, such as file edits and shell commands.
	DangerouslySkipPermissions bool
	// ExtraArgs are appended to the `opencode acp` command line.
	ExtraArgs []string
	// contains filtered or unexported fields
}

ProviderOption configures the opencode process. It implements genai.ProviderOption.

All fields are opt-in. By default the provider rejects every permission request the agent makes.

func (*ProviderOption) Validate added in v0.9.0

func (p *ProviderOption) Validate() error

Validate implements genai.ProviderOption.

Jump to

Keyboard shortcuts

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