opencode

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2026 License: Apache-2.0 Imports: 18 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(genai.ProviderOptionModel("opencode/big-pickle"))
	if err != nil {
		log.Fatal(err)
	}
	res, err := c.GenSync(context.Background(), genai.Messages{genai.NewTextMessage("Say hello")})
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(res.Replies[0].Text)
}

Index

Examples

Constants

This section is empty.

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(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"). Use genai.ModelCheap, genai.ModelGood, or genai.ModelSOTA for automatic selection.

func (*Client) Capabilities

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

Capabilities implements genai.Provider.

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.

Jump to

Keyboard shortcuts

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