serve

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2026 License: GPL-3.0 Imports: 14 Imported by: 0

Documentation

Overview

Package serve manages a short-lived `opencode serve` subprocess and exposes the base URL of its HTTP API.

`opencode serve` is a distinct transport from `opencode acp`: it runs an HTTP/OpenAPI 3.1 server instead of stdio JSON-RPC. The SDK's primary transport is ACP, but a few pieces of metadata — notably per-model capability flags (reasoning, tool_call, attachment, …) — are only exposed over the HTTP surface. This package lets the SDK fetch that metadata on demand without altering the ACP lifecycle.

Index

Constants

This section is empty.

Variables

View Source
var ErrServerNotReady = errors.New("opencode serve did not become ready")

ErrServerNotReady is returned when the serve subprocess exits or fails to announce a listening URL within the configured timeout.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Path is the resolved path to the opencode binary.
	Path string
	// Env overlays the inherited environment. nil means inherit
	// os.Environ() unchanged.
	Env map[string]string
	// Cwd is the working directory for the subprocess. Empty defers to
	// the caller's cwd.
	Cwd string
	// Logger receives diagnostics. Must not be nil.
	Logger *slog.Logger
	// ReadyTimeout caps how long Spawn waits for the server to announce
	// its listening URL. Zero defaults to 10s.
	ReadyTimeout time.Duration
}

Config configures the opencode serve subprocess.

type Process

type Process struct {
	// contains filtered or unexported fields
}

Process is a running `opencode serve` subprocess with a reachable HTTP endpoint.

func Spawn

func Spawn(ctx context.Context, cfg Config) (*Process, error)

Spawn launches `opencode serve` on an ephemeral loopback port, waits for the "listening on …" banner, and returns a Process that owns the subprocess lifetime. Callers MUST call Close.

Port allocation: Spawn picks a free 127.0.0.1 port by briefly binding a net.Listener on :0, then hands the port to opencode via --port. The window between our Close and opencode's bind is racy in principle; in practice it is microseconds and the fallback is a clear "address in use" error from opencode that surfaces as ErrServerNotReady.

func (*Process) BaseURL

func (p *Process) BaseURL() string

BaseURL returns the HTTP base URL the subprocess is listening on, e.g. "http://127.0.0.1:41023". Safe to call after Spawn returns nil.

func (*Process) Close

func (p *Process) Close() error

Close terminates the subprocess. Idempotent and safe to call after partial Spawn failures.

Jump to

Keyboard shortcuts

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