Documentation
¶
Overview ¶
Package runtime opens kairo, declares KairoCode's actions and workflows on it, and gives the server and the CLI a small API over sessions.
Index ¶
- Constants
- Variables
- func DBIn(dir string) string
- func DefaultDB() string
- func DefaultDataDir() string
- func FlowFrom(from map[string]int) string
- func FlowName(name string) bool
- func PluginOAuth(ctx context.Context, dir, name string, open func(url string) error) error
- func PluginSecretKey(p opencode.Plugin, name string) string
- func RunJob(folder string) error
- func Secrets() secrets.Store
- func SessionID(id string) string
- type AuthInfo
- type AuthMethod
- type Config
- type Event
- type FlowInfo
- type FlowRun
- type FlowStat
- type Image
- type LogEntry
- type MCPInfo
- type MCPToolInfo
- type Pending
- type PluginInfo
- type PreviewSection
- type ProviderInfo
- type Runtime
- func (r *Runtime) Answer(ctx context.Context, key, option string) error
- func (r *Runtime) Approve(ctx context.Context, key string, ok bool, reason string) error
- func (r *Runtime) Auth(ctx context.Context, id string) AuthInfo
- func (r *Runtime) Awaiting(ctx context.Context) bool
- func (r *Runtime) Cancel(ctx context.Context, session string) (int, error)
- func (r *Runtime) Close() error
- func (r *Runtime) CloseSession(ctx context.Context, session string) error
- func (r *Runtime) Commands(ctx context.Context, session string) ([]opencode.Command, error)
- func (r *Runtime) Compact(ctx context.Context, session string) (int, error)
- func (r *Runtime) CreateSession(ctx context.Context, id string, in workflows.SessionInput) (string, error)
- func (r *Runtime) Drain(ctx context.Context) error
- func (r *Runtime) Efforts(ctx context.Context, id string) ([]string, error)
- func (r *Runtime) Events(ctx context.Context, session string) (<-chan Event, error)
- func (r *Runtime) FlowRunByID(ctx context.Context, id string) (FlowRun, error)
- func (r *Runtime) FlowRuns(ctx context.Context, dir, name, only string, limit int) ([]FlowRun, error)
- func (r *Runtime) FlowStats(ctx context.Context, dir string) ([]FlowStat, error)
- func (r *Runtime) FlowView(ctx context.Context, flow kairo.RunInfo, chart *flowchart.Chart) (*flowchart.View, error)
- func (r *Runtime) Flows(ctx context.Context, session string) ([]FlowInfo, error)
- func (r *Runtime) Fork(ctx context.Context, id, from string, n int, files bool) (string, int, error)
- func (r *Runtime) History(ctx context.Context, session string) ([]msg.Message, error)
- func (r *Runtime) HistoryLive(ctx context.Context, session string) ([]msg.Message, error)
- func (r *Runtime) ImageData(ctx context.Context, ref string) ([]byte, error)
- func (r *Runtime) InstallProvider(ctx context.Context, id, version string) (ProviderInfo, error)
- func (r *Runtime) JobDone(id string) error
- func (r *Runtime) LastFlow(ctx context.Context, session string) (*flowchart.View, error)
- func (r *Runtime) LastTurn(ctx context.Context, session string) (int, bool, error)
- func (r *Runtime) Log(ctx context.Context, session string) (SessionLog, error)
- func (r *Runtime) LoggedIn(id string)
- func (r *Runtime) Login(ctx context.Context, id, method string) error
- func (r *Runtime) LoginMethods(ctx context.Context, id string) ([]AuthMethod, error)
- func (r *Runtime) MCP(ctx context.Context, session string) ([]MCPInfo, error)
- func (r *Runtime) Models(ctx context.Context, id string) ([]string, error)
- func (r *Runtime) NewFlow(ctx context.Context, session, request string) (int, error)
- func (r *Runtime) Pending(ctx context.Context, session string) ([]Pending, error)
- func (r *Runtime) PluginConnected(plugin string)
- func (r *Runtime) Plugins(ctx context.Context, session string) ([]PluginInfo, error)
- func (r *Runtime) PluginsIn(ctx context.Context, dir string) ([]PluginInfo, error)
- func (r *Runtime) Providers(ctx context.Context) []ProviderInfo
- func (r *Runtime) Redo(ctx context.Context, session string) (int, error)
- func (r *Runtime) Reply(ctx context.Context, hash string) (string, error)
- func (r *Runtime) RunCommand(ctx context.Context, session, name, args string) (int, error)
- func (r *Runtime) RunFlow(ctx context.Context, session, name, args string) (int, error)
- func (r *Runtime) Send(ctx context.Context, session, text string) (int, error)
- func (r *Runtime) SendImages(ctx context.Context, session, text string, images []Image) (int, error)
- func (r *Runtime) Sessions(ctx context.Context, dir string, limit int) ([]SessionInfo, error)
- func (r *Runtime) SetMode(ctx context.Context, session, mode string) (Settings, error)
- func (r *Runtime) Settings(ctx context.Context, session string) (Settings, error)
- func (r *Runtime) Skills(ctx context.Context, session string) (SkillsInfo, error)
- func (r *Runtime) Switch(ctx context.Context, session, id, model, effort string) (Settings, error)
- func (r *Runtime) TryFlow(ctx context.Context, key, args string) error
- func (r *Runtime) TurnRun(ctx context.Context, session string, n int) (kairo.RunInfo, bool, error)
- func (r *Runtime) Undo(ctx context.Context, session string) (int, error)
- func (r *Runtime) Usage(ctx context.Context, session string) (SessionUsage, error)
- func (r *Runtime) WaitTurn(ctx context.Context, session string, n int) (workflows.TurnOutput, error)
- type SessionInfo
- type SessionLog
- type SessionUsage
- type Settings
- type SkillsInfo
- type TryTarget
- type TurnUsage
Constants ¶
const ( AuthSignedIn = api.AuthSignedIn AuthNeeded = api.AuthNeeded AuthUnknown = api.AuthUnknown )
const ( EventToken = api.EventToken EventTurnStart = api.EventTurnStart EventTurnEnd = api.EventTurnEnd EventToolStart = api.EventToolStart EventToolEnd = api.EventToolEnd EventApprovalRequested = api.EventApprovalRequested EventApprovalResolved = api.EventApprovalResolved EventReviewRequested = api.EventReviewRequested EventReviewResolved = api.EventReviewResolved EventQuestionRequested = api.EventQuestionRequested EventQuestionResolved = api.EventQuestionResolved EventFlow = api.EventFlow )
const ( FlowRunOK = api.FlowRunOK FlowRunFailed = api.FlowRunFailed FlowRunBroken = api.FlowRunBroken FlowRunCancelled = api.FlowRunCancelled FlowRunRunning = api.FlowRunRunning )
const ( FromCommand = api.FromCommand FromHook = api.FromHook FromAgent = api.FromAgent )
const ( FlowRunning = api.FlowRunning FlowDone = api.FlowDone FlowFailed = api.FlowFailed FlowWaiting = api.FlowWaiting )
const ConfigFile = config.File
ConfigFile is the name of KairoCode's settings file (config.File).
const FailedNote = api.FailedNote
const FlowsDir = flowchart.Dir
FlowsDir is where a project's flows are (ADR 0003): <name>.star.
const KindQuestion = api.KindQuestion
const MCPServerName = "kairocode"
MCPServerName is how Claude Code names KairoCode's tools: mcp__kairocode__<tool>.
const NoCLIs = "KAIROCODE_NO_CLIS"
NoCLIs is the environment variable that, set to "1", keeps the runtime from finding the user's claude, codex and copilot by name (tests: they never run the real ones, invariant 9).
const PluginManifest = plugins.ManifestFile
PluginManifest is the file KairoCode reads in a plugin's folder.
const ProviderAnthropic = "anthropic"
ProviderAnthropic is the API engine's name as a provider (ADR 0014).
const StoppedCancelled = api.StoppedCancelled
Variables ¶
var ( ErrNotAFlow = api.ErrNotAFlow ErrTrying = api.ErrTrying )
var ( ErrNoForkPoint = api.ErrNoForkPoint ErrNoSnapshot = api.ErrNoSnapshot ErrForkWhileRunning = api.ErrForkWhileRunning )
var ( ErrNoProvider = api.ErrNoProvider ErrNotInstalled = api.ErrNotInstalled )
var CheckImages = api.CheckImages
var ErrAnswered = api.ErrAnswered
var ErrBadImage = api.ErrBadImage
var ErrClosing = api.ErrClosing
var ErrNoCommand = api.ErrNoCommand
var ErrNoFlow = api.ErrNoFlow
var ErrNoFlowName = api.ErrNoFlowName
var ErrNoFlowRequest = api.ErrNoFlowRequest
var ErrNoFlowRun = api.ErrNoFlowRun
var ErrNoTurnToCancel = api.ErrNoTurnToCancel
var ErrNoUndo = api.ErrNoUndo
var ErrQuestion = api.ErrQuestion
var ErrSessionFailed = api.ErrSessionFailed
var FlowHelp = flowfile.FlowHelp
var Modes = api.Modes
var PluginsText = plugins.PluginsText
Functions ¶
func DefaultDataDir ¶
func DefaultDataDir() string
DefaultDataDir is where a user's sessions live, and where their daemon runs (ADR 0008).
func PluginOAuth ¶
PluginOAuth connects dir's plugin name by OAuth: open shows the user the authorization page (a browser), which comes back here. The grant is kept in the key store.
func PluginSecretKey ¶
PluginSecretKey is where a plugin's credential is kept. A project's plugin's are its own, by its folder: one that takes a plugin of the user's name gets none of the user's (invariant 6).
func RunJob ¶
RunJob is a command's runner (kairocode jobs run <folder>): it runs the command in its sandbox, its output to the folder, until it ends, is stopped (SIGTERM) or runs out of time, and writes how it ended.
Types ¶
type AuthMethod ¶
type AuthMethod = api.AuthMethod
type Config ¶
type Config struct {
// DB is the SQLite file kairo and the message store share.
DB string
// Engine runs the model loop of a session that names none:
// workflows.EngineAPI (the default) or workflows.EngineClaudeCode (ADR
// 0007). A session may name the other: the API engine then needs
// Provider.
Engine string
// Provider is the model provider of the API engine.
Provider provider.Provider
// ClaudePath is the claude command of the Claude Code engine;
// CodexPath and CopilotPath, the codex and copilot commands of theirs
// (ADR 0014).
ClaudePath string
CodexPath string // the ACP agent: codex-acp
CopilotPath string // copilot, run with --acp
// CopilotHome is copilot's settings directory, where KairoCode turns
// copilot's own hooks off (ADR 0018; default: $COPILOT_HOME, else
// ~/.copilot; with NoCLIs, none: nothing is written).
CopilotHome string
// AgentsDir keeps the ACP agents installed from the registry (ADR
// 0015; default: agents beside DB); AgentRegistry is the registry's
// URL (agents.RegistryURL for ACP's; "" reads none).
AgentsDir string
AgentRegistry string
// ExtraTools are offered besides the built-in ones (tests).
ExtraTools []tools.Def
// JobRunner runs a command in the background, given its folder
// (ADR 0028; default: this binary's kairocode jobs run).
JobRunner []string
// Model is the model an agent uses unless it names one.
Model string
MaxSteps int
// Owner names this process in kairo's leases. A process that opens with
// the name its earlier self had takes over what that self left running.
Owner string
// Lease is how long kairo leases a step to this process (kairo's default
// when zero).
Lease time.Duration
Logger *slog.Logger
// Observe, if set, is also given what kairo observes here (the daemon
// tells from it whether a step runs, ADR 0008).
Observe func(kairo.Observation)
// Now is kairo's clock (tests; the real clock when nil).
Now func() time.Time
// contains filtered or unexported fields
}
type MCPToolInfo ¶
type MCPToolInfo = api.MCPToolInfo
type PluginInfo ¶
type PluginInfo = api.PluginInfo
type PreviewSection ¶
type PreviewSection = api.PreviewSection
type ProviderInfo ¶
type ProviderInfo = api.ProviderInfo
type Runtime ¶
func (*Runtime) Answer ¶
Answer answers a flow's question (Pending of Kind "question") with one of its options. As with Approve, only the first answer goes through.
func (*Runtime) Approve ¶
Approve answers the request key names (see Pending), any time after the request. Only the first answer goes through: one given while the request no longer waits, or after another answer to it, returns ErrAnswered. Those who watch the session are told the answer (Event.OK).
For an approval, ok lets the call run. For a review, ok says the call took effect: either way the call is settled with a result that tells the model what the user said, and the turn goes on; the call is not run again.
func (*Runtime) Auth ¶
Auth is provider id's sign-in as far as it is known: a CLI's own word for Claude Code and Codex (asked at most once a minute), what running it showed, else unknown.
func (*Runtime) Awaiting ¶
Awaiting says something here needs the process to stay up though nothing runs (ADR 0028): a wait with a deadline or a sleep (kairo's timers fire only in a process). A command in the background does not: its runner starts a daemon to tell of its end.
func (*Runtime) Cancel ¶
Cancel stops the session's turn under way (ADR 0011): kairo cancels it, with what it waits for and the steps under way. It returns the turn's number. Turns run one at a time: the one under way is the one not ended, whatever was sent after it. With none under way, the last turn sent is waited for if it has not begun (a stop pressed just after a send).
func (*Runtime) CloseSession ¶
CloseSession ends a session after the turns it has been sent.
func (*Runtime) Commands ¶
Commands are the commands a session can run: opencode's commands/*.md for its working directory (ADR 0002), and its skills as /name where no command has the name, by name.
func (*Runtime) Compact ¶
Redo asks the session to put back what its last undo took away. Compact starts a turn that only compacts the session's conversation (/compact, ADR 0020), and returns its number.
func (*Runtime) CreateSession ¶
func (r *Runtime) CreateSession(ctx context.Context, id string, in workflows.SessionInput) (string, error)
CreateSession starts session id in dir. Creating it again returns the same session.
func (*Runtime) Drain ¶
Drain stops the runtime at the edges of its steps (kairo's Drain): no new step starts, and the steps under way finish with their outcomes recorded, so a real one needs no review in the next process. It returns once they have, or when ctx ends; draining has begun either way. Close after it.
func (*Runtime) Efforts ¶
Efforts are the efforts of provider id to choose from; none if it has no effort to set (or has not said).
func (*Runtime) Events ¶
Events delivers the events of session until ctx ends, when the channel is closed. It is closed earlier if the watcher falls too far behind, or the runtime closes: watch again to be told what waits.
func (*Runtime) FlowRunByID ¶
FlowRunByID is a flow's run with its chart and steps: as it goes, if it runs; as it was kept, if it ended.
func (*Runtime) FlowRuns ¶
func (r *Runtime) FlowRuns(ctx context.Context, dir, name, only string, limit int) ([]FlowRun, error)
FlowRuns are the flows' runs of dir ("" any), of flow name ("" any), the latest first, at most limit (0: all): those running, and those kept; only "tries" or "runs" of them, or both (""). Each without its chart and steps (FlowRunByID gives them).
func (*Runtime) FlowStats ¶
FlowStats are the flows the sessions in dir ran, and the project's flows that none ran, by name. Only what the records keep is counted: the runs of trees that ended longer ago than KeepFinished are gone.
func (*Runtime) FlowView ¶
func (r *Runtime) FlowView(ctx context.Context, flow kairo.RunInfo, chart *flowchart.Chart) (*flowchart.View, error)
FlowView is how far the flow run has gone: its chart, from the script it recorded, and each call's state (ADR 0003). Each run of the flow says which call of the script it is (At, flow version 2 on); a run of version 1 has a chart with no states. chart, if known, is the run's chart: it is not built again.
func (*Runtime) Flows ¶
Flows are the flows of the session's project, by name, with their DESCRIPTION; then their drafts.
func (*Runtime) Fork ¶
func (r *Runtime) Fork(ctx context.Context, id, from string, n int, files bool) (string, int, error)
Fork starts session id from turn n of session from (n < 0: its last finished turn), with that turn's conversation and settings; from is not changed (ADR 0021). With files, the new session's first turn puts the working files back as they were after turn n. It returns the session and the turn it was forked from.
func (*Runtime) History ¶
History is the session's conversation as of its last finished turn: what a client that comes back shows (ACP's session/load).
func (*Runtime) HistoryLive ¶
HistoryLive is the session's conversation as a client shows it again: History, then what the turns under way were asked (a turn waiting for an approval is not in the history yet; shown without it, the approval would come with nothing before it).
func (*Runtime) ImageData ¶
ImageData is the bytes of the image stored as ref, for a client that shows a conversation again (ADR 0026).
func (*Runtime) InstallProvider ¶
InstallProvider installs agent id of ACP's registry at version (the one the user was shown; "" for the registry's), the user having chosen to (ADR 0015).
func (*Runtime) JobDone ¶
JobDone is told by a command's runner that it ended (the daemon's /jobs/<id>/done): for a runner the runtime that started it is no longer here to wait on.
func (*Runtime) LastFlow ¶
LastFlow is the session's latest flow, running or ended, as far as it has gone.
func (*Runtime) LastTurn ¶
LastTurn is the number of the session's latest turn; false if it has none.
func (*Runtime) LoggedIn ¶
LoggedIn notes that the user signed in to provider id by a command of their own (a terminal method): it is known again when it next runs.
func (*Runtime) Login ¶
Login signs in to provider id with an agent's own method (ACP's authenticate; it may open a browser and wait). A terminal method is the client's to run.
func (*Runtime) LoginMethods ¶
LoginMethods are the ways to sign in to provider id. For an agent not run yet, it is started to ask.
func (*Runtime) MCP ¶
MCP lists the session's MCP servers and custom tools, and codex's own servers: how each runs, and the tools a server has (it is started for that, if it may run). A project's that the user has not said how to run is not started.
func (*Runtime) Models ¶
Models are the models of provider id to choose from; any other name may be given too.
func (*Runtime) NewFlow ¶
NewFlow starts a turn in which the model writes a flow from the user's words (/flow new). The conversation keeps the request as it is asked of the model, so the turns after it can change the flow too.
func (*Runtime) PluginConnected ¶
PluginConnected is told that a plugin's credentials changed: its servers start again with them, and it is no longer "expired".
func (*Runtime) Plugins ¶
Plugins lists the session's plugins and their state: whether each is connected, needs connecting, has lost its authentication, does not start, or waits for the user to say how it runs (a project's). A plugin may run only as the user said: one not said about is not started for the list.
func (*Runtime) Providers ¶
func (r *Runtime) Providers(ctx context.Context) []ProviderInfo
Providers are the providers: the API, Claude Code, and the agents of ACP's registry, with whether each runs here or can be installed. If the registry cannot be read, the agents installed are still there. Each that runs here has its sign-in.
func (*Runtime) RunCommand ¶
RunCommand runs command name with args in session: its template is expanded as opencode expands it (its !`commands` run in the sandbox, read-only and offline) and sent as the user's message, to the command's agent and model if it names them. It returns the turn's number. What was expanded is recorded as the message: a resumed turn does not expand it again.
func (*Runtime) RunFlow ¶
RunFlow starts a turn that runs the flow name with args (/flow name args). The message the conversation keeps is what was typed.
func (*Runtime) Send ¶
Send stores text as a user message and gives it to the session. It returns the number of the turn that will answer it.
func (*Runtime) SendImages ¶
func (r *Runtime) SendImages(ctx context.Context, session, text string, images []Image) (int, error)
SendImages is Send with images: one user message, the text and then the images, each stored once by its hash.
func (*Runtime) Sessions ¶
Sessions lists the sessions of dir ("" any), the latest used first, at most limit (0: all).
func (*Runtime) SetMode ¶
SetMode sets the session's mode from its next turn on (ADR 0016), recorded with its next message as a switch is.
func (*Runtime) Settings ¶
Settings are what the session's next turn runs with: its latest turn's, or its own, with a switch not yet sent applied.
func (*Runtime) Skills ¶
Skills are the skills of the session's working directory, and what of them is not taken.
func (*Runtime) Switch ¶
Switch sets the provider (id, "" to keep it), the model and the effort ("" to keep them) of the session's turns from its next one on (ADR 0014). A provider switched to without a model or an effort gets those last chosen for it, in this session or another, else its defaults; what is chosen is kept for the next time. The switch goes to the session with its next message, and is recorded there.
func (*Runtime) TryFlow ¶
TryFlow tries the flow an approval would write (ADR 0017), with args as the words after /flow <name>: the call keeps waiting, and the try runs below it; tools that do more than read are not called. The approval is asked again when the try ends, with what it did.
func (*Runtime) TurnRun ¶
TurnRun is the kairo run of turn n of a session, or "" if the session has not started it yet.
func (*Runtime) Undo ¶
Undo asks the session to put the working files back as they were before its last turn in effect (design 7). It returns the number of the turn that does it.
func (*Runtime) Usage ¶
Usage adds up the usage the session's model calls recorded (the API's model calls, and Claude Code's runs), turn by turn: a turn under way counts what it has used so far.
func (*Runtime) WaitTurn ¶
func (r *Runtime) WaitTurn(ctx context.Context, session string, n int) (workflows.TurnOutput, error)
WaitTurn waits for turn n of a session to finish and returns its result. It waits on runs starting and settling in this process, not on a timer (kairo-gap #15: a turn another process starts is noticed only once found).
type SessionInfo ¶
type SessionInfo = api.SessionInfo
type SessionLog ¶
type SessionLog = api.SessionLog
type SessionUsage ¶
type SessionUsage = api.SessionUsage
type SkillsInfo ¶
type SkillsInfo = api.SkillsInfo
Source Files
¶
- acpengine.go
- actions.go
- agent.go
- agenthooks.go
- approvals.go
- auth.go
- autojudge.go
- claude.go
- cliengines.go
- commandhooks.go
- commands.go
- compact.go
- decide.go
- declare.go
- engines.go
- events.go
- external.go
- extservers.go
- flow.go
- flownew.go
- flowruns.go
- flowstats.go
- flowtry.go
- flowview.go
- fork.go
- history.go
- hooks.go
- ids.go
- images.go
- jobs.go
- log.go
- mcpstatus.go
- netconfig.go
- oauth.go
- permission.go
- plugins.go
- preview.go
- proc_linux.go
- providers.go
- records.go
- redact.go
- runtime.go
- send.go
- sessions.go
- snapshot.go
- toolargs.go
- toolserver.go
- turns.go
- usage.go