runtime

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 4, 2026 License: MIT Imports: 58 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const (
	AuthSignedIn = api.AuthSignedIn
	AuthNeeded   = api.AuthNeeded
	AuthUnknown  = api.AuthUnknown
)
View Source
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
)
View Source
const (
	FlowRunOK        = api.FlowRunOK
	FlowRunFailed    = api.FlowRunFailed
	FlowRunBroken    = api.FlowRunBroken
	FlowRunCancelled = api.FlowRunCancelled
	FlowRunRunning   = api.FlowRunRunning
)
View Source
const (
	FromCommand = api.FromCommand
	FromHook    = api.FromHook
	FromAgent   = api.FromAgent
)
View Source
const (
	FlowRunning = api.FlowRunning
	FlowDone    = api.FlowDone
	FlowFailed  = api.FlowFailed
	FlowWaiting = api.FlowWaiting
)
View Source
const ConfigFile = config.File

ConfigFile is the name of KairoCode's settings file (config.File).

View Source
const FailedNote = api.FailedNote
View Source
const FlowsDir = flowchart.Dir

FlowsDir is where a project's flows are (ADR 0003): <name>.star.

View Source
const KindQuestion = api.KindQuestion
View Source
const MCPServerName = "kairocode"

MCPServerName is how Claude Code names KairoCode's tools: mcp__kairocode__<tool>.

View Source
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).

View Source
const PluginManifest = plugins.ManifestFile

PluginManifest is the file KairoCode reads in a plugin's folder.

View Source
const ProviderAnthropic = "anthropic"

ProviderAnthropic is the API engine's name as a provider (ADR 0014).

View Source
const StoppedCancelled = api.StoppedCancelled

Variables

View Source
var (
	ErrNotAFlow = api.ErrNotAFlow
	ErrTrying   = api.ErrTrying
)
View Source
var (
	ErrNoForkPoint      = api.ErrNoForkPoint
	ErrNoSnapshot       = api.ErrNoSnapshot
	ErrForkWhileRunning = api.ErrForkWhileRunning
)
View Source
var (
	ErrNoProvider   = api.ErrNoProvider
	ErrNotInstalled = api.ErrNotInstalled
)
View Source
var CheckImages = api.CheckImages
View Source
var ErrAnswered = api.ErrAnswered
View Source
var ErrBadImage = api.ErrBadImage
View Source
var ErrClosing = api.ErrClosing
View Source
var ErrNoCommand = api.ErrNoCommand
View Source
var ErrNoFlow = api.ErrNoFlow
View Source
var ErrNoFlowName = api.ErrNoFlowName
View Source
var ErrNoFlowRequest = api.ErrNoFlowRequest
View Source
var ErrNoFlowRun = api.ErrNoFlowRun
View Source
var ErrNoTurnToCancel = api.ErrNoTurnToCancel
View Source
var ErrNoUndo = api.ErrNoUndo
View Source
var ErrQuestion = api.ErrQuestion
View Source
var ErrSessionFailed = api.ErrSessionFailed
View Source
var ErrUnavailable = api.ErrUnavailable
View Source
var FlowHelp = flowfile.FlowHelp
View Source
var Modes = api.Modes
View Source
var PluginsText = plugins.PluginsText

Functions

func DBIn

func DBIn(dir string) string

DBIn is the database in data directory dir.

func DefaultDB

func DefaultDB() string

DefaultDB is the database in the default data directory.

func DefaultDataDir

func DefaultDataDir() string

DefaultDataDir is where a user's sessions live, and where their daemon runs (ADR 0008).

func FlowFrom

func FlowFrom(from map[string]int) string

FlowFrom puts how a flow ran in words: "command 2, hook 1".

func FlowName

func FlowName(name string) bool

FlowName reports whether name can be a flow's.

func PluginOAuth

func PluginOAuth(ctx context.Context, dir, name string, open func(url string) error) error

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

func PluginSecretKey(p opencode.Plugin, name string) string

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

func RunJob(folder string) error

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.

func Secrets

func Secrets() secrets.Store

Secrets is this machine's store of plugins' credentials.

func SessionID

func SessionID(id string) string

SessionID is the kairo id of session id (AGENTS.md, invariant 8).

Types

type AuthInfo

type AuthInfo = api.AuthInfo

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 Event

type Event = api.Event

type FlowInfo

type FlowInfo = api.FlowInfo

type FlowRun

type FlowRun = api.FlowRun

type FlowStat

type FlowStat = api.FlowStat

type Image

type Image = api.Image

type LogEntry

type LogEntry = api.LogEntry

type MCPInfo

type MCPInfo = api.MCPInfo

type MCPToolInfo

type MCPToolInfo = api.MCPToolInfo

type Pending

type Pending = api.Pending

type PluginInfo

type PluginInfo = api.PluginInfo

type PreviewSection

type PreviewSection = api.PreviewSection

type ProviderInfo

type ProviderInfo = api.ProviderInfo

type Runtime

type Runtime struct {
	K *kairo.Kairo

	Store *store.Store
	// contains filtered or unexported fields
}

func Open

func Open(ctx context.Context, cfg Config) (*Runtime, error)

func (*Runtime) Answer

func (r *Runtime) Answer(ctx context.Context, key, option string) error

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

func (r *Runtime) Approve(ctx context.Context, key string, ok bool, reason string) error

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

func (r *Runtime) Auth(ctx context.Context, id string) AuthInfo

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

func (r *Runtime) Awaiting(ctx context.Context) bool

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

func (r *Runtime) Cancel(ctx context.Context, session string) (int, error)

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) Close

func (r *Runtime) Close() error

Close closes the runtime; closing it again does nothing.

func (*Runtime) CloseSession

func (r *Runtime) CloseSession(ctx context.Context, session string) error

CloseSession ends a session after the turns it has been sent.

func (*Runtime) Commands

func (r *Runtime) Commands(ctx context.Context, session string) ([]opencode.Command, error)

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

func (r *Runtime) Compact(ctx context.Context, session string) (int, error)

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

func (r *Runtime) Drain(ctx context.Context) error

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

func (r *Runtime) Efforts(ctx context.Context, id string) ([]string, error)

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

func (r *Runtime) Events(ctx context.Context, session string) (<-chan Event, error)

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

func (r *Runtime) FlowRunByID(ctx context.Context, id string) (FlowRun, error)

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

func (r *Runtime) FlowStats(ctx context.Context, dir string) ([]FlowStat, error)

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

func (r *Runtime) Flows(ctx context.Context, session string) ([]FlowInfo, error)

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

func (r *Runtime) History(ctx context.Context, session string) ([]msg.Message, error)

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

func (r *Runtime) HistoryLive(ctx context.Context, session string) ([]msg.Message, error)

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

func (r *Runtime) ImageData(ctx context.Context, ref string) ([]byte, error)

ImageData is the bytes of the image stored as ref, for a client that shows a conversation again (ADR 0026).

func (*Runtime) InstallProvider

func (r *Runtime) InstallProvider(ctx context.Context, id, version string) (ProviderInfo, error)

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

func (r *Runtime) JobDone(id string) error

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

func (r *Runtime) LastFlow(ctx context.Context, session string) (*flowchart.View, error)

LastFlow is the session's latest flow, running or ended, as far as it has gone.

func (*Runtime) LastTurn

func (r *Runtime) LastTurn(ctx context.Context, session string) (int, bool, error)

LastTurn is the number of the session's latest turn; false if it has none.

func (*Runtime) Log

func (r *Runtime) Log(ctx context.Context, session string) (SessionLog, error)

Log is session's record.

func (*Runtime) LoggedIn

func (r *Runtime) LoggedIn(id string)

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

func (r *Runtime) Login(ctx context.Context, id, method string) error

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

func (r *Runtime) LoginMethods(ctx context.Context, id string) ([]AuthMethod, error)

LoginMethods are the ways to sign in to provider id. For an agent not run yet, it is started to ask.

func (*Runtime) MCP

func (r *Runtime) MCP(ctx context.Context, session string) ([]MCPInfo, error)

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

func (r *Runtime) Models(ctx context.Context, id string) ([]string, error)

Models are the models of provider id to choose from; any other name may be given too.

func (*Runtime) NewFlow

func (r *Runtime) NewFlow(ctx context.Context, session, request string) (int, error)

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) Pending

func (r *Runtime) Pending(ctx context.Context, session string) ([]Pending, error)

Pending lists the calls of a session that wait for approval.

func (*Runtime) PluginConnected

func (r *Runtime) PluginConnected(plugin string)

PluginConnected is told that a plugin's credentials changed: its servers start again with them, and it is no longer "expired".

func (*Runtime) Plugins

func (r *Runtime) Plugins(ctx context.Context, session string) ([]PluginInfo, error)

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) PluginsIn

func (r *Runtime) PluginsIn(ctx context.Context, dir string) ([]PluginInfo, error)

PluginsIn lists dir's plugins and their state (kairocode plugin).

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) Redo

func (r *Runtime) Redo(ctx context.Context, session string) (int, error)

func (*Runtime) Reply

func (r *Runtime) Reply(ctx context.Context, hash string) (string, error)

Reply is the text of an assistant message.

func (*Runtime) RunCommand

func (r *Runtime) RunCommand(ctx context.Context, session, name, args string) (int, error)

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

func (r *Runtime) RunFlow(ctx context.Context, session, name, args string) (int, error)

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

func (r *Runtime) Send(ctx context.Context, session, text string) (int, error)

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

func (r *Runtime) Sessions(ctx context.Context, dir string, limit int) ([]SessionInfo, error)

Sessions lists the sessions of dir ("" any), the latest used first, at most limit (0: all).

func (*Runtime) SetMode

func (r *Runtime) SetMode(ctx context.Context, session, mode string) (Settings, error)

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

func (r *Runtime) Settings(ctx context.Context, session string) (Settings, error)

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

func (r *Runtime) Skills(ctx context.Context, session string) (SkillsInfo, error)

Skills are the skills of the session's working directory, and what of them is not taken.

func (*Runtime) Switch

func (r *Runtime) Switch(ctx context.Context, session, id, model, effort string) (Settings, error)

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

func (r *Runtime) TryFlow(ctx context.Context, key, args string) error

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

func (r *Runtime) TurnRun(ctx context.Context, session string, n int) (kairo.RunInfo, bool, error)

TurnRun is the kairo run of turn n of a session, or "" if the session has not started it yet.

func (*Runtime) Undo

func (r *Runtime) Undo(ctx context.Context, session string) (int, error)

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

func (r *Runtime) Usage(ctx context.Context, session string) (SessionUsage, error)

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 Settings

type Settings = api.Settings

type SkillsInfo

type SkillsInfo = api.SkillsInfo

type TryTarget

type TryTarget = api.TryTarget

type TurnUsage

type TurnUsage = api.TurnUsage

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL