Documentation
¶
Overview ¶
Package daemon is the one process per data directory that runs kairo (ADR 0008), and the client every other kairocode process uses to reach it.
The daemon holds daemon.lock for as long as it runs, so a second one does not start. It serves the HTTP API (internal/server) on a Unix socket and writes where it is to server.json. Everything that happens between live processes is then an event inside the daemon: an approval is a Signal there, and the turn that waits for it wakes at once.
Index ¶
- Constants
- Variables
- func FileVersion(path string) (string, error)
- func InstalledAt(path string) func() string
- func Serve(ctx context.Context, o Options) error
- func Stop(ctx context.Context, dir string) (bool, error)
- type Client
- func (c *Client) Answer(ctx context.Context, key, option string) error
- func (c *Client) Approve(ctx context.Context, key string, ok bool, reason string) error
- func (c *Client) Cancel(ctx context.Context, session string) (int, error)
- func (c *Client) Close()
- func (c *Client) CloseSession(ctx context.Context, session string) error
- func (c *Client) Commands(ctx context.Context, session string) ([]opencode.Command, error)
- func (c *Client) Compact(ctx context.Context, session string) (int, error)
- func (c *Client) CreateSession(ctx context.Context, in workflows.SessionInput) (string, error)
- func (c *Client) Efforts(ctx context.Context, id string) ([]string, error)
- func (c *Client) Events(ctx context.Context, session string) (<-chan api.Event, error)
- func (c *Client) FlowRun(ctx context.Context, id string) (api.FlowRun, error)
- func (c *Client) FlowRuns(ctx context.Context, dir, flow, only string, n int) ([]api.FlowRun, error)
- func (c *Client) FlowStats(ctx context.Context, dir string) ([]api.FlowStat, error)
- func (c *Client) Flows(ctx context.Context, session string) ([]api.FlowInfo, error)
- func (c *Client) Fork(ctx context.Context, session string, n int, files bool) (string, int, error)
- func (c *Client) History(ctx context.Context, session string) ([]msg.Message, error)
- func (c *Client) ImageData(ctx context.Context, ref string) ([]byte, error)
- func (c *Client) InstallProvider(ctx context.Context, id, version string) (api.ProviderInfo, error)
- func (c *Client) JobDone(ctx context.Context, id string) error
- func (c *Client) LastFlow(ctx context.Context, session string) (*flowchart.View, error)
- func (c *Client) LastTurn(ctx context.Context, session string) (int, error)
- func (c *Client) Log(ctx context.Context, session string) (api.SessionLog, error)
- func (c *Client) Login(ctx context.Context, id, method string, done bool) error
- func (c *Client) LoginMethods(ctx context.Context, id string) ([]api.AuthMethod, error)
- func (c *Client) MCP(ctx context.Context, session string) ([]api.MCPInfo, error)
- func (c *Client) Models(ctx context.Context, id string) ([]string, error)
- func (c *Client) NewFlow(ctx context.Context, session, request string) (int, error)
- func (c *Client) PID() int
- func (c *Client) Pending(ctx context.Context, session string) ([]api.Pending, error)
- func (c *Client) PluginConnected(ctx context.Context, name string) error
- func (c *Client) Plugins(ctx context.Context, session string) ([]api.PluginInfo, error)
- func (c *Client) PluginsIn(ctx context.Context, dir string) ([]api.PluginInfo, error)
- func (c *Client) Providers(ctx context.Context) ([]api.ProviderInfo, error)
- func (c *Client) Redo(ctx context.Context, session string) (int, error)
- func (c *Client) RunCommand(ctx context.Context, session, name, args string) (int, error)
- func (c *Client) RunFlow(ctx context.Context, session, name, args string) (int, error)
- func (c *Client) Send(ctx context.Context, session, text string) (int, error)
- func (c *Client) SendImages(ctx context.Context, session, text string, images []api.Image) (int, error)
- func (c *Client) Sessions(ctx context.Context, dir string, limit int) ([]api.SessionInfo, error)
- func (c *Client) SetMode(ctx context.Context, session, mode string) (api.Settings, error)
- func (c *Client) Settings(ctx context.Context, session string) (api.Settings, error)
- func (c *Client) Skills(ctx context.Context, session string) (api.SkillsInfo, error)
- func (c *Client) Status(ctx context.Context) (Status, error)
- func (c *Client) Switch(ctx context.Context, session, provider, model, effort string) (api.Settings, error)
- func (c *Client) TryFlow(ctx context.Context, key, args string) error
- func (c *Client) Undo(ctx context.Context, session string) (int, error)
- func (c *Client) Usage(ctx context.Context, session string) (api.SessionUsage, error)
- func (c *Client) WaitTurn(ctx context.Context, session string, n int) (Turn, error)
- type Info
- type Options
- type Start
- type Status
- type Turn
- type Update
Constants ¶
const ( // Owner is the daemon's name in kairo's leases. Only one daemon runs per // data directory, so a daemon started after one was killed takes over // what it left at once, without waiting for its leases to run out. // // Safe only while nothing else opens the database under this name: // kairo expires every lease of the name at Open, and a second process // with it would take steps that still run (and run them twice). The // lock keeps it to one process per SQLite file; a shared database // (PostgreSQL, several hosts) needs a name per host. Owner = "kairocode-daemon" // DefaultIdle is how long a daemon with nothing to run and no client // stays up. DefaultIdle = 10 * time.Minute // DefaultDrain is how long a daemon that stops waits for the steps under // way to finish. DefaultDrain = 30 * time.Second )
Variables ¶
var ErrNoTurn = errors.New("the session has no turn")
ErrNoTurn: the session has no turn (or is not there).
var ErrRunning = errors.New("a daemon already runs for this data directory")
ErrRunning: another daemon runs for the data directory.
Functions ¶
func FileVersion ¶
FileVersion names the build in the executable at path (a link is followed): the hash of its content.
func InstalledAt ¶
InstalledAt is Options.Installed for the executable at path: its version, measured again only when the file changed (its size, time or identity), so that looking each time no step runs costs a stat.
func Serve ¶
Serve runs the daemon until ctx ends, it has been idle for Idle, or a client stops it. It returns ErrRunning if another daemon holds the data directory.
It stops between steps: no new step starts, and the steps under way finish with their outcomes recorded (kairo's Drain), for up to DrainTimeout. A step still running then is cut, as in a crash, and a real one waits in the next daemon for a person to say whether it took effect (invariant 4).
Types ¶
type Client ¶
type Client struct {
// Stale is what to tell the user when the daemon cannot be of the
// client's build: the client is older than the build installed, or
// (an older daemon) others use it.
Stale string
// contains filtered or unexported fields
}
Client talks to a data directory's daemon. When it cannot reach it (the daemon handed over to a new build, or stopped), it connects again, as Connect does (ADR 0019).
func Connect ¶
Connect reaches the daemon of data directory dir, starting it with start if none runs. Clients that find none at once start one: the first takes start.lock, and the others wait on the lock and find the daemon it started.
func (*Client) Answer ¶
Answer answers a flow's question with one of its options. Only the first answer goes through: a later one returns api.ErrAnswered.
func (*Client) Approve ¶
Approve answers one of them. Only the first answer goes through: a later one returns api.ErrAnswered.
func (*Client) Cancel ¶
Undo starts a turn that puts the working files back as they were before the session's last turn in effect; Redo puts back what an undo took away. They return the turn's number. Cancel stops the session's last turn (ADR 0011). It returns the turn's number, or api.ErrNoTurnToCancel when it has ended.
func (*Client) CloseSession ¶
CloseSession ends the session after the turns it has been sent.
func (*Client) Compact ¶
Compact starts a turn that only compacts the session's conversation (ADR 0020), and returns its number.
func (*Client) CreateSession ¶
CreateSession starts a session and returns its id. in.Dir must be absolute: the daemon runs elsewhere.
func (*Client) Events ¶
Events follows the session's events (GET /sessions/{id}/events): first what waits for a person, then what happens. The channel closes when ctx ends or the stream does (the daemon stopped, or this client fell behind); call it again to go on.
func (*Client) FlowRuns ¶
func (c *Client) FlowRuns(ctx context.Context, dir, flow, only string, n int) ([]api.FlowRun, error)
FlowRuns are the flows' runs of dir and flow ("" any), the latest first, at most n (0: all), only "tries" or "runs" or both ("") (ADR 0029).
func (*Client) FlowStats ¶
FlowStats are the flows the sessions in dir ran, and the project's flows that none ran.
func (*Client) Fork ¶
Fork starts a session from turn n of this one (n < 0: its last finished turn), with files put back as they were after it if asked (ADR 0021). It returns the new session and the turn it was forked from; ErrNoForkPoint when there is no such turn.
func (*Client) History ¶
History is the session's conversation as of its last finished turn, then what the turns under way were asked: as a client shows it again.
func (*Client) InstallProvider ¶
InstallProvider installs agent id of ACP's registry (ADR 0015).
func (*Client) JobDone ¶
JobDone tells the daemon that a command in the background ended (its runner, ADR 0028).
func (*Client) Login ¶
Login signs in to provider id with its agent's method; done instead tells the daemon the user ran a terminal method.
func (*Client) LoginMethods ¶
LoginMethods are the ways to sign in to provider id.
func (*Client) NewFlow ¶
NewFlow has the model write a flow from request (/flow new) and returns the turn's number.
func (*Client) PluginConnected ¶
PluginConnected tells the daemon a plugin's credentials changed.
func (*Client) RunCommand ¶
RunCommand runs one with args and returns the turn's number.
func (*Client) RunFlow ¶
RunFlow runs the flow name with args (/flow) and returns the turn's number.
func (*Client) SendImages ¶
func (c *Client) SendImages(ctx context.Context, session, text string, images []api.Image) (int, error)
SendImages is Send with images (ADR 0026).
func (*Client) Sessions ¶
Sessions lists the sessions of dir ("" all), the latest used first, at most limit (0: all).
func (*Client) Switch ¶
func (c *Client) Switch(ctx context.Context, session, provider, model, effort string) (api.Settings, error)
Switch switches the session's provider or model ("": keep it) from its next turn on, and returns the settings it then has.
type Info ¶
type Info struct {
Socket string `json:"socket"`
Token string `json:"token"`
PID int `json:"pid"`
Version string `json:"version,omitempty"`
}
Info is server.json: how a client reaches the daemon.
type Options ¶
type Options struct {
// Dir is the data directory: the database, the lock, server.json and the
// socket live there.
Dir string
// Open opens the runtime with cfg (DB, Owner and Observe set by the
// daemon); the caller adds the engine and the model.
Open func(ctx context.Context, cfg runtime.Config) (*runtime.Runtime, error)
// Idle is how long the daemon stays up with no step running and no
// client connected (DefaultIdle when zero; negative: forever).
Idle time.Duration
// Ready, if set, is told "ready" once clients can connect.
Ready io.Writer
// WaitLock is how long to wait for a daemon that holds the data
// directory to end (zero: not at all). A client starts a daemon only
// when it found none answering: one that holds the lock then is one on
// its way out (idle, or killed and not yet gone).
WaitLock time.Duration
// Version names the build.
Version string
// Installed is the build installed now: the daemon's executable as it
// is on disk (InstalledAt). One other than Version retires the daemon
// once no step runs, and Successor starts it for the clients connected
// (ADR 0019). Nil, or "": the daemon does not retire.
Installed func() string
Successor func() Start
// DrainTimeout is how long a daemon that stops lets the steps under way
// finish (DefaultDrain when zero). A step still running then is cut, as
// in a crash: a real one waits in the next daemon for review.
DrainTimeout time.Duration
// Cut, when closed, ends the drain at once: the steps still running are
// cut (a second Ctrl-C).
Cut <-chan struct{}
}
type Start ¶
type Start struct {
Argv []string
Env []string
// Version is the client's build. A daemon of another build is stopped
// and started again from Argv, if no other client uses it.
Version string
}
Start says how a client starts the daemon when none runs: the command (argv) and its environment. The command must run Serve with Ready set to its standard output.
type Status ¶
type Status struct {
Version string `json:"version"`
PID int `json:"pid"`
// Steps is the number of steps running; Clients the number of
// connections, the asking one included.
Steps int `json:"steps"`
Clients int `json:"clients"`
}
Status is what GET /daemon/status says.
type Turn ¶
type Turn struct {
Reply string `json:"reply"`
Steps int `json:"steps"`
Stopped string `json:"stopped"`
Usage msg.Usage `json:"usage"`
}
Turn is a finished turn.
type Update ¶
type Update struct {
Installed string `json:"installed,omitempty"`
Replaced bool `json:"replaced,omitempty"`
Later bool `json:"later,omitempty"`
}
Update is what POST /daemon/update says (ADR 0019): the build installed now; Replaced, the daemon handed over to it (connect again); Later, it will once no step runs.