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 ¶
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 ¶
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 ¶
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.