Documentation
¶
Overview ¶
Package cli provides a CLI subprocess transport adapter for agentrun engines.
A Backend implements Spawner and Parser to define how subprocesses are launched and how their stdout is parsed into agentrun.Message values. Optional capabilities (Resumer, Streamer, InputFormatter) are discovered via type assertion at runtime.
NewEngine wraps a Backend into an agentrun.Engine. The returned Engine manages subprocess lifecycle, message pumping, graceful shutdown (SIGTERM then SIGKILL), and the Resumer subprocess-replacement pattern for multi-turn sessions.
Platform Support ¶
The Engine and process types use Unix signals (SIGTERM, SIGKILL) for subprocess lifecycle management and are not available on Windows. The interface types (Backend, Spawner, Parser, Resumer, Streamer, InputFormatter) and option types are available on all platforms.
Consumer Obligations ¶
Callers must either drain the agentrun.Process.Output channel to completion or call agentrun.Process.Stop to release subprocess resources. Failing to do so may leave the subprocess running and leak goroutines.
Concrete backends (claude, codex, opencode) implement the Backend interface.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrSkipLine = errors.New("cli: skip line")
ErrSkipLine is returned by Parser.ParseLine when a line should be consumed but produces no Message (blank lines, heartbeats, comments). The CLIEngine checks errors.Is(err, ErrSkipLine) and continues iteration.
Functions ¶
This section is empty.
Types ¶
type Backend ¶
Backend is the minimum interface a CLI backend must implement. Optional capabilities (Resumer, Streamer, InputFormatter) are discovered via type assertion at runtime.
Backends must implement at least one send path for Engine.Start to succeed: either Streamer+InputFormatter or Resumer. Start returns agentrun.ErrSendNotSupported if neither is available.
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine is a CLI subprocess engine that adapts a Backend into an agentrun.Engine. It orchestrates subprocess lifecycle, message pumping, and graceful shutdown.
func NewEngine ¶
func NewEngine(backend Backend, opts ...EngineOption) *Engine
NewEngine creates a CLI engine backed by the given Backend. Use EngineOption functions to customize buffer sizes and grace period.
func (*Engine) Start ¶
func (e *Engine) Start(_ context.Context, session agentrun.Session, opts ...agentrun.Option) (agentrun.Process, error)
Start initializes a subprocess session and returns a Process handle. Returns agentrun.ErrSendNotSupported if the backend lacks a send path (neither Streamer+InputFormatter nor Resumer). The context parameter is reserved for future use (e.g., start timeout); subprocess lifetime is controlled via agentrun.Process.Stop.
type EngineOption ¶
type EngineOption func(*EngineOptions)
EngineOption configures an Engine at construction time.
func WithGracePeriod ¶
func WithGracePeriod(d time.Duration) EngineOption
WithGracePeriod sets the duration to wait after SIGTERM before sending SIGKILL. Values <= 0 are ignored.
func WithOutputBuffer ¶
func WithOutputBuffer(size int) EngineOption
WithOutputBuffer sets the channel buffer size for process output messages. Values <= 0 are ignored.
func WithScannerBuffer ¶
func WithScannerBuffer(size int) EngineOption
WithScannerBuffer sets the maximum line size in bytes for the stdout scanner. Values <= 0 are ignored.
type EngineOptions ¶
type EngineOptions struct {
// OutputBuffer is the channel buffer size for process output messages.
OutputBuffer int
// ScannerBuffer is the maximum line size in bytes for the stdout scanner.
ScannerBuffer int
// GracePeriod is the duration to wait after SIGTERM before sending SIGKILL.
GracePeriod time.Duration
}
EngineOptions holds resolved construction-time configuration for a CLI engine. Use NewEngine with EngineOption functions to customize these values.
type InputFormatter ¶
InputFormatter encodes user messages for delivery to a subprocess stdin pipe. InputFormatter is optional — the CLIEngine discovers it via type assertion independently of Streamer:
if f, ok := backend.(InputFormatter); ok {
data, err := f.FormatInput("user message")
}
A backend can implement Streamer, InputFormatter, both, or neither. message must be valid UTF-8. Implementations must return an error for messages containing null bytes.
type Parser ¶
Parser converts a raw stdout line into a structured Message. Every CLI backend must implement Parser.
ParseLine returns ErrSkipLine when a line should be consumed but produces no Message. Any other non-nil error indicates a parse failure. A returned Message with zero Timestamp will have its Timestamp set by the CLIEngine.
type Resumer ¶
type Resumer interface {
ResumeArgs(session agentrun.Session, initialPrompt string) (binary string, args []string, err error)
}
Resumer builds the exec.Cmd arguments to resume an existing session. Resumer is optional — the CLIEngine discovers it via type assertion:
if r, ok := backend.(Resumer); ok {
binary, args, err := r.ResumeArgs(session, "continue from here")
}
The initialPrompt parameter is baked into the subprocess args because some CLI tools (e.g., Claude Code's --resume flag) require the first message at spawn time (single-shot resume pattern).
type Spawner ¶
Spawner builds the exec.Cmd arguments to start a new session. Every CLI backend must implement Spawner.
SpawnArgs is a pure argument builder — it must not fail. Backends that need to validate session state should do so in their constructor or in Engine.Validate. Implementations MUST return arguments suitable for exec.Cmd (pre-split argv) and MUST NOT pass arguments through a shell interpreter.
type Streamer ¶
Streamer builds a long-lived streaming command that attaches to a subprocess for continuous output. When a backend implements Streamer, the initial prompt is NOT included in the command args — the caller must send it explicitly via Process.Send after Start returns. Streamer is optional — the CLIEngine discovers it via type assertion:
if s, ok := backend.(Streamer); ok {
binary, args := s.StreamArgs(session)
}
Directories
¶
| Path | Synopsis |
|---|---|
|
Package claude provides a Claude Code CLI backend for agentrun.
|
Package claude provides a Claude Code CLI backend for agentrun. |
|
Package codex provides a Codex CLI backend for agentrun.
|
Package codex provides a Codex CLI backend for agentrun. |
|
internal
|
|
|
jsonutil
Package jsonutil provides safe JSON extraction helpers for CLI backend parsers.
|
Package jsonutil provides safe JSON extraction helpers for CLI backend parsers. |
|
optutil
Package optutil provides shared option resolution helpers for CLI backends.
|
Package optutil provides shared option resolution helpers for CLI backends. |
|
Package opencode provides an OpenCode CLI backend for agentrun.
|
Package opencode provides an OpenCode CLI backend for agentrun. |