Documentation
¶
Overview ¶
Package acp is KairoCode as an ACP agent (Agent Client Protocol v1, ADR 0002): an editor starts `kairocode acp` and talks JSON-RPC over its stdin and stdout. The agent is a client of the daemon (ADR 0008): it maps the daemon's sessions, turns, events and approvals to ACP, and holds nothing that must outlive it.
Index ¶
- Constants
- func Serve(ctx context.Context, b Backend, r io.Reader, w io.Writer, o Options) error
- type Backend
- type Client
- func (cl *Client) Cancel(session string) error
- func (cl *Client) Done() <-chan struct{}
- func (cl *Client) FlowRun(ctx context.Context, id string) (api.FlowRun, error)
- func (cl *Client) FlowRuns(ctx context.Context, dir, flow, only string, n int) ([]api.FlowRun, error)
- func (cl *Client) GoOn(ctx context.Context, session string) (string, int, error)
- func (cl *Client) Initialize(ctx context.Context) (string, error)
- func (cl *Client) InstallProvider(ctx context.Context, id, version string) error
- func (cl *Client) ListSessions(ctx context.Context, cwd string) ([]SessionInfo, error)
- func (cl *Client) LoadSession(ctx context.Context, session, cwd string) error
- func (cl *Client) Login(ctx context.Context, provider, method string, done bool) error
- func (cl *Client) LoginMethods(ctx context.Context, provider string) ([]LoginMethod, error)
- func (cl *Client) Models(ctx context.Context, provider string) ([]string, error)
- func (cl *Client) NewSession(ctx context.Context, cwd string) (string, error)
- func (cl *Client) PluginChange(ctx context.Context, args ...string) (string, error)
- func (cl *Client) Plugins(ctx context.Context, session string) (PluginsView, error)
- func (cl *Client) Prompt(ctx context.Context, session, text string, files ...string) (string, error)
- func (cl *Client) SetConfigOption(ctx context.Context, session, id, value string) error
- func (cl *Client) SetMode(ctx context.Context, session, mode string) error
- type ClientHandler
- type Command
- type CommandMeta
- type CommandOption
- type ConfigOption
- type ConfigValue
- type InstallInfo
- type LoginMethod
- type Options
- type PermissionAnswer
- type PermissionOption
- type PermissionRequest
- type PluginsView
- type Section
- type SessionInfo
- type SessionMode
- type Update
Constants ¶
const ( // SectionMeta, on a text block of an approval's content, names the // section of what the call would do it is (its text after the // section's heading): a client may show them apart (the TUI's review // of a flow). SectionMeta = "kairocode/section" // TryOption is the option of a flow's approval that tries it first // (ADR 0017); the reason (ReasonMeta) is the words to run it with. TryOption = "try" // RefsMeta, with SectionMeta: for each line of the section, the // script's lines it is ([first, last], [0, 0] for none). RefsMeta = "kairocode/refs" // DetailsMeta: for each line, more of it to show ("" none); ChartMeta: // the section's flow as a chart (flowchart.Chart), to draw. DetailsMeta = "kairocode/details" ChartMeta = "kairocode/chart" InstallMeta = "kairocode/install" InstallMethod = "_kairocode/install_provider" // ModelsMethod gives a provider's models: {"provider": id} → // {"models": [...]} (the client's /model, ADR 0014). ModelsMethod = "_kairocode/models" // AuthMeta, in a provider's value's _meta, is its sign-in state // (signed-in, needed, unknown). LoginMethodsRPC gives the ways to // sign in ({"provider"} → {"methods"}); LoginRPC signs in with the // agent's own ({"provider", "method"}), or notes that the user ran a // terminal one ({"provider", "done": true}). AuthMeta = "kairocode/auth" LoginMethodsRPC = "_kairocode/login_methods" LoginRPC = "_kairocode/login" // PluginsRPC gives the session's plugins, the marketplaces' and the // marketplaces ({"sessionId"} → PluginsView), for the terminal's // /plugin; PluginRPC does /plugin's install, update, remove or // marketplace ({"args": [...]} → {"text"}). ADR 0027. PluginsRPC = "_kairocode/plugins" PluginRPC = "_kairocode/plugin" // FlowRunsRPC lists the flows' runs of a folder ({"dir", "flow", "n"} // → {"runs"}); FlowRunRPC gives one, with its chart and steps // ({"id"} → api.FlowRun). ADR 0029. FlowRunsRPC = "_kairocode/flow_runs" FlowRunRPC = "_kairocode/flow_run" // GoOnRPC goes on from a session that failed, in a new session forked // from its last finished turn, the working files as they are // ({"sessionId"} → {"sessionId", "turn"}). ADR 0021. GoOnRPC = "_kairocode/go_on" // CodeSessionFailed answers a prompt to a session that failed and // takes no more (ADR 0021). CodeSessionFailed = -32010 )
InstallMeta, in a provider's value's _meta, says it is installed when chosen (ADR 0015): {"source", "version"}. InstallMethod installs it: {"provider": id, "version": the version shown}.
const FlowMeta = "kairocode/flow"
FlowMeta is the key of a plan update's _meta that holds the flow's chart and states (flowchart.View), for a client that draws it (kairo's TUI).
const OptionsMeta = "kairocode/options"
OptionsMeta is the key, in a command's _meta, of the words it takes after its name.
const ProtocolVersion = 1
ProtocolVersion is the ACP version this agent speaks (the schema it is tested against is in testdata).
const ReasonMeta = "kairocode/reason"
ReasonMeta is the key of a permission response's _meta that holds what the user said to do instead (KairoCode's agent gives it to the model).
const SettledMeta = "kairocode/settled"
SettledMeta marks, in a tool_call_update's _meta, the call a request made settled with its answer: for a client that marks the call by the option chosen. kairo's terminal shows the answer its own way.
const TextMeta = "kairocode/text"
TextMeta, with SectionMeta: the section's text as it is, the block's being Markdown.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Backend ¶
type Backend interface {
CreateSession(ctx context.Context, in workflows.SessionInput) (string, error)
Send(ctx context.Context, session, text string) (int, error)
// SendImages and ImageData: a message with images, and an image's
// bytes for a conversation shown again (ADR 0026).
SendImages(ctx context.Context, session, text string, images []api.Image) (int, error)
ImageData(ctx context.Context, ref string) ([]byte, error)
WaitTurn(ctx context.Context, session string, n int) (daemon.Turn, error)
Approve(ctx context.Context, key string, ok bool, reason string) error
// TryFlow tries the flow an approval would write first (ADR 0017).
TryFlow(ctx context.Context, key, args string) error
Events(ctx context.Context, session string) (<-chan api.Event, error)
History(ctx context.Context, session string) ([]msg.Message, error)
Undo(ctx context.Context, session string) (int, error)
Redo(ctx context.Context, session string) (int, error)
Compact(ctx context.Context, session string) (int, error)
Fork(ctx context.Context, session string, n int, files bool) (string, int, error)
Commands(ctx context.Context, session string) ([]opencode.Command, error)
// Skills are the session's skills, and the SKILL.md files not taken.
Skills(ctx context.Context, session string) (api.SkillsInfo, error)
// MCP lists the session's MCP servers and custom tools (ADR 0022).
MCP(ctx context.Context, session string) ([]api.MCPInfo, error)
Plugins(ctx context.Context, session string) ([]api.PluginInfo, error)
Sessions(ctx context.Context, dir string, limit int) ([]api.SessionInfo, error)
// Log is the session's record (ADR 0023).
Log(ctx context.Context, session string) (api.SessionLog, error)
RunCommand(ctx context.Context, session, name, args string) (int, error)
Cancel(ctx context.Context, session string) (int, error)
Flows(ctx context.Context, session string) ([]api.FlowInfo, error)
// FlowRuns and FlowRun are the flows' runs, kept once they end (ADR
// 0029).
FlowRuns(ctx context.Context, dir, flow, only string, n int) ([]api.FlowRun, error)
FlowRun(ctx context.Context, id string) (api.FlowRun, error)
Answer(ctx context.Context, key, option string) error
RunFlow(ctx context.Context, session, name, args string) (int, error)
NewFlow(ctx context.Context, session, request string) (int, error)
Providers(ctx context.Context) ([]api.ProviderInfo, error)
InstallProvider(ctx context.Context, id, version string) (api.ProviderInfo, error)
LoginMethods(ctx context.Context, id string) ([]api.AuthMethod, error)
Login(ctx context.Context, id, method string, done bool) error
Models(ctx context.Context, provider string) ([]string, error)
Settings(ctx context.Context, session string) (api.Settings, error)
Switch(ctx context.Context, session, provider, model, effort string) (api.Settings, error)
Efforts(ctx context.Context, provider string) ([]string, error)
SetMode(ctx context.Context, session, mode string) (api.Settings, error)
}
Backend is what the agent needs of the daemon (daemon.Client).
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is the client side of ACP: what an editor does, for KairoCode's own terminal chat (ADR 0002). It talks to an agent over r (from it) and w (to it).
func Dial ¶
Dial begins talking to an agent. The client stops when r ends or ctx does; Done is closed then.
func (*Client) Cancel ¶
Cancel asks the agent to stop the session's turn under way: its Prompt then answers "cancelled".
func (*Client) Done ¶
func (cl *Client) Done() <-chan struct{}
Done is closed once the connection has ended.
func (*Client) FlowRuns ¶
func (cl *Client) FlowRuns(ctx context.Context, dir, flow, only string, n int) ([]api.FlowRun, error)
FlowRuns are the flows' runs of dir ("" any), of flow ("" any), the latest first, at most n (only KairoCode's agent has this; ADR 0029).
func (*Client) GoOn ¶
GoOn goes on from a session that failed, in a new session forked from its last finished turn (ADR 0021): the new session and that turn.
func (*Client) Initialize ¶
Initialize begins the protocol, and returns the agent's name.
func (*Client) InstallProvider ¶
InstallProvider installs a provider of ACP's registry, the user having chosen to (ADR 0015). Only KairoCode's agent has it.
func (*Client) ListSessions ¶
ListSessions lists the agent's sessions of cwd, every page of them.
func (*Client) LoadSession ¶
LoadSession takes up a session: its conversation comes again as updates, and what waits for a person is asked again.
func (*Client) Login ¶
Login signs in to provider with the agent's own method, or, done, tells the agent the user ran a terminal method.
func (*Client) LoginMethods ¶
LoginMethods are the ways to sign in to provider (KairoCode's agent).
func (*Client) NewSession ¶
NewSession starts a session working in cwd (an absolute path).
func (*Client) PluginChange ¶
PluginChange does /plugin's install, update, remove or marketplace, and says what came of it.
func (*Client) Plugins ¶
Plugins are the session's plugins, the marketplaces' and the marketplaces (only KairoCode's agent has this; ADR 0027).
func (*Client) Prompt ¶
func (cl *Client) Prompt(ctx context.Context, session, text string, files ...string) (string, error)
Prompt runs a turn, and returns why it stopped (ACP's stopReason). The turn's updates have all been given to the handler when it returns.
func (*Client) SetConfigOption ¶
SetConfigOption sets one of the session's settings (ADR 0014: provider, model).
type ClientHandler ¶
type ClientHandler struct {
Update func(Update)
Permission func(ctx context.Context, p PermissionRequest) PermissionAnswer
}
ClientHandler is what a client does with what the agent sends. Updates come one at a time, in the order they were sent. Permission is asked in its own goroutine; ctx ends when the agent withdraws the request (it was answered elsewhere). It returns the chosen option's ID, or "" to cancel.
type Command ¶
type Command struct {
Name string `json:"name"`
Description string `json:"description"`
// Meta: what KairoCode's agent tells of its own commands.
Meta *CommandMeta `json:"_meta,omitempty"`
}
Command is a slash command the agent offers.
type CommandMeta ¶
type CommandMeta struct {
Options []CommandOption `json:"kairocode/options,omitempty"`
}
CommandMeta is a command's _meta as KairoCode's agent sends it.
type CommandOption ¶
type CommandOption struct {
Name string `json:"name"`
Hint string `json:"hint,omitempty"`
Description string `json:"description"`
}
CommandOption is what a command takes after its name: a word (or a few, as "marketplace add"), what follows it, and what it does.
type ConfigOption ¶
type ConfigOption struct {
ID string `json:"id"`
Name string `json:"name"`
Category string `json:"category"`
CurrentValue string `json:"currentValue"`
Options []ConfigValue `json:"options"`
}
ConfigOption is one of a session's settings: a selector, its current value and the values there are.
type ConfigValue ¶
type ConfigValue struct {
Value string `json:"value"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
// Install, for a provider KairoCode installs when chosen (ADR 0015):
// what it fetches.
Install *InstallInfo `json:"-"`
Meta struct {
Install *InstallInfo `json:"kairocode/install,omitempty"`
Auth string `json:"kairocode/auth,omitempty"`
} `json:"_meta"`
}
ConfigValue is a value a setting can take.
type InstallInfo ¶
InstallInfo is what installing a provider fetches.
type LoginMethod ¶
type LoginMethod struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
Command []string `json:"command,omitempty"`
Agent bool `json:"agent,omitempty"`
}
LoginMethod is a way to sign in to a provider: a command to run in the terminal, or one the agent carries out (Agent).
type Options ¶
type Options struct {
// Version is KairoCode's, told to the client.
Version string
// Engine and Model are given to the sessions it starts (empty: the
// daemon's defaults).
Engine, Model string
Logger *slog.Logger
}
Options are the agent's settings.
type PermissionAnswer ¶
PermissionAnswer is the user's answer: the option chosen ("" to cancel), and, with a refusal, what to do instead, for the model (sent in _meta, ReasonMeta).
type PermissionOption ¶
type PermissionOption struct {
ID string `json:"optionId"`
Name string `json:"name"`
// Kind is allow_once, allow_always, reject_once or reject_always.
Kind string `json:"kind"`
}
PermissionOption is one answer offered.
type PermissionRequest ¶
type PermissionRequest struct {
Session string
// ToolCallID is the tool call it is about.
ToolCallID string
Title string
// ToolKind is ACP's kind of the tool call (read, edit, execute, ...).
ToolKind string
Options []PermissionOption
// Content is what the call would do, as text, if the agent says (a
// flow's shape); Sections, the same in its named parts, if the agent
// gives them (SectionMeta).
Content string
Sections []Section
}
PermissionRequest is a session/request_permission.
type PluginsView ¶
type PluginsView struct {
Installed []api.PluginInfo `json:"installed"`
Catalog []api.CatalogPlugin `json:"catalog"`
Marketplaces []api.MarketInfo `json:"marketplaces"`
Errors []string `json:"errors,omitempty"`
}
PluginsView is what the terminal's /plugin shows.
type Section ¶
type Section struct {
Name string `json:"name"`
Text string `json:"text"`
// Refs: for each line of Text, the script's lines it is ({0, 0} for
// none); Details: more of each line to show ("" none). Chart: the
// flow as a chart (flowchart.Chart), to draw in place of Text.
Refs [][2]int `json:"refs,omitempty"`
Details []string `json:"details,omitempty"`
Chart json.RawMessage `json:"chart,omitempty"`
}
Section is a named part of what a call would do (Summary, Tree, Changes, Script).
type SessionInfo ¶
type SessionInfo struct {
ID string `json:"sessionId"`
Cwd string `json:"cwd"`
Title string `json:"title"`
Updated string `json:"updatedAt"`
Meta struct {
KairoCode struct {
Turns int `json:"turns"`
Closed bool `json:"closed"`
Waiting int `json:"waiting"` // asked of a person, in its turns under way
Jobs int `json:"jobs"` // its commands in the background running
} `json:"kairocode"`
} `json:"_meta"`
}
SessionInfo is a session as session/list gives it.
type SessionMode ¶
type SessionMode struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
}
SessionMode is one of a session's modes.
type Update ¶
type Update struct {
Session string
// Kind is ACP's sessionUpdate: agent_message_chunk, user_message_chunk,
// tool_call, tool_call_update, available_commands_update, ...
Kind string
// Text: a message chunk's text, or a tool call's output.
Text string
// A tool call's.
ToolCallID, Title, ToolKind, Status string
// Commands: available_commands_update's.
Commands []Command
// Meta is the update's _meta; Flow, a plan's flow (FlowMeta), when the
// agent is KairoCode's.
Meta json.RawMessage
Flow *flowchart.View
// Mode: current_mode_update's (and a made or loaded session's) mode;
// Modes, a made or loaded session's modes (ADR 0016).
Mode string
Modes []SessionMode
// Config: config_option_update's, the session's settings (ADR 0014).
// The client gives one too when a session is made or loaded, and when a
// setting is set.
Config []ConfigOption
}
Update is one session/update, as far as a client shows it.