Documentation
¶
Overview ¶
Package cliagent runs agent turns through a coding-agent CLI (Claude Code or Codex) instead of a model API. The CLI owns the agent loop; Aviary exposes its tools to the CLI over MCP and receives the CLI's JSON event stream as backend-neutral events.
Index ¶
Constants ¶
const MCPServerName = "aviary"
MCPServerName is the name Aviary's MCP server is registered under in the CLI's MCP configuration. Tool calls against it are reported by Aviary's MCP boundary rather than mapped from the CLI's event stream.
const MCPToolTimeout = 30 * time.Minute
MCPToolTimeout is how long a CLI waits on one call to Aviary's MCP server. The CLIs give up on a tool call after 60 seconds by default, while tools such as project_issue_create run a whole gh-create (issue #710), so it covers the longest tool rather than the CLI's default.
const ModelPrefix = "cli/"
ModelPrefix is the provider prefix that selects a CLI backend in a model string such as "cli/claude/opus". A backend's own name selects it too, as in "claude/opus": each CLI is a provider in its own right.
Variables ¶
var Backends = []Backend{Claude, Codex}
Backends lists every supported backend.
Functions ¶
func IsCLIModel ¶
IsCLIModel reports whether model selects a CLI backend, either as "cli/<backend>/<model>" or as "<backend>/<model>".
func RunCommandHook ¶
RunCommandHook answers one Claude Code PreToolUse hook call read from in, with getenv supplying the hook's configuration. It writes a deny decision naming the responsible rule when the agent's Allowed Exec Commands refuse the command, as Aviary's own exec tool would, and nothing otherwise, which leaves the call to Claude's own permission check. A call it cannot check is refused rather than left to run.
Types ¶
type Backend ¶
type Backend string
Backend identifies a supported agent CLI.
func ParseModel ¶
ParseModel splits "cli/<backend>/<model>" or "<backend>/<model>" into its backend and the model name passed to that CLI.
type CommandHook ¶
type CommandHook struct {
// Executable is the aviary binary that answers the hook.
Executable string
// Agent names the agent whose rules these are, for the refusal.
Agent string
// Rules are the agent's ordered Allowed Exec Commands, as written.
Rules []string
}
CommandHook has Claude Code check every call to its shell tools against an agent's Allowed Exec Commands before running it, through a PreToolUse hook that runs Executable's "cli-hook" command. Claude's dontAsk mode still runs commands it classifies as read-only, such as "echo hi", with no allow rule covering them, so its own allow rules alone cannot narrow the shell to the agent's rules (#595).
type EventType ¶
type EventType string
EventType identifies a backend-neutral CLI event.
const ( // EventText carries a chunk of assistant text. EventText EventType = "text" // EventToolStart reports that the CLI started one of its own tools. EventToolStart EventType = "tool_start" // EventToolResult reports the result of one of the CLI's own tools. EventToolResult EventType = "tool_result" // EventSession reports the CLI-native session ID. EventSession EventType = "session" // EventUsage reports token usage and cost for the turn. EventUsage EventType = "usage" )
EventType values.
type Options ¶
type Options struct {
Backend Backend
Model string
SystemPrompt string
WorkDir string
// MCPURL and MCPToken point the CLI at Aviary's scoped MCP endpoint.
// When MCPURL is empty the CLI gets no MCP servers at all.
MCPURL string
MCPToken string
// BuiltinTools enables the CLI's own shell, file-edit and web tools.
// When false they are turned off and only Aviary's MCP tools remain.
BuiltinTools bool
// Approval are the CLI's native permission flags for the surface's
// approval mode, as its CLI provider definition translates it. They are
// always passed, so the user's own CLI settings never pick the mode.
Approval []string
// ExtraDir are the CLI's native arguments that add the agent's own
// directory to the session's workspace scope beside WorkDir, as its CLI
// provider definition declares them. They widen which directories are in
// scope, never what the approval mode allows there.
ExtraDir []string
// Permissions are the CLI's native allow and deny arguments carrying the
// agent's exec and filesystem rules, as its CLI provider definition
// translates them for the approval mode.
Permissions []string
// RefusalNote, when set, is added to a CLI tool's error when the CLI's
// own permission check refused the call, naming that layer.
RefusalNote string
// CommandHook, when set with BuiltinTools, has the CLI check each of its
// shell tool calls against the agent's Allowed Exec Commands. Only
// claude honors it.
CommandHook *CommandHook
// ResumeID is the CLI-native session ID to resume. Empty starts a new
// session; the new ID is reported with an EventSession event.
ResumeID string
// NewSessionID, when set, is used as the ID of a new Claude Code session.
NewSessionID string
// Ephemeral runs without persisting the CLI session, so it cannot be
// resumed later.
Ephemeral bool
// Binary overrides the executable path (tests).
Binary string
// Env is appended to the CLI's environment.
Env []string
}
Options configures a CLI session. Options are fixed for the life of a session; changing any of them requires a new session.
type Result ¶
type Result struct {
// Text is the final assistant message.
Text string
}
Result is the outcome of a completed turn.
type Session ¶
type Session interface {
// Send runs one turn. onEvent is called synchronously for every event.
// Send returns once the CLI finishes the turn. Turns must not overlap.
Send(ctx context.Context, turn Turn, onEvent func(Event)) (Result, error)
// Close stops any process the session holds.
Close() error
}
Session runs turns against one CLI conversation.
func NewSession ¶
NewSession creates a session for opts.Backend. No process is started until the first Send.