Documentation
¶
Overview ¶
Event Bus — the shared output spine.
Historically every part of the agent wrote straight to stdout with fmt.Println. That works for a REPL but makes alternate front-ends (a TUI, a web dashboard) impossible without duplicating output plumbing. The Bus inverts it: code EMITS typed events, and any number of SUBSCRIBERS render them however they like. stdout is just the first subscriber; the TUI and dashboard become additional ones, reading the same stream.
Migration is incremental and safe. This step introduces the Bus and makes stdout a subscriber, but does NOT rewrite the 250+ existing fmt.Println sites — those keep printing directly for now. New structured emits (tokens, tool calls, stats) go through the Bus; the plain-text sites get migrated file-by-file in later steps. During the transition the terminal output is identical because the stdout subscriber mirrors what the direct prints already do. Nothing breaks; the Bus simply becomes available.
Index ¶
- Constants
- Variables
- func ConfinePath(base, p string) (string, error)
- func Dedup(ss []string) []string
- func DrainBrowserSubmission() string
- func EmitAssistant(text string)
- func EmitBusy(working bool)
- func EmitDiff(text string)
- func EmitError(text string)
- func EmitLine(text string)
- func EmitLineC(color, text string)
- func EmitStats(text string, meta map[string]string)
- func EmitStatus(text string)
- func EmitThinking(text string)
- func EmitToken(text string)
- func EmitToolCall(tool, argsPreview string)
- func EmitToolCallInline(tool, argsPreview string)
- func EmitToolDone(tool, summary string)
- func EmitUser(text string)
- func NewBus() *eventBus
- func ParseTestFailures(out string) string
- func RelPaths(root string, abs []string) []string
- func TermWidth() int
- func Tint(code, s string) string
- func VetCommand(cmdStr string) error
- type Event
- type EventKind
- type Message
- type Subscriber
- type SubscriberFunc
- type ToolCall
Constants ¶
const ( ColorDim = "2" ColorRed = "31" ColorGreen = "32" ColorYellow = "33" ColorCyan = "36" )
ANSI SGR codes used across the UI.
const VaultAgentDir = "agent"
VaultAgentDir is the subdirectory under the vault where the agent writes.
Variables ¶
var BrowserSubmissions = make(chan string, 16)
BrowserSubmissions carries prompts submitted from the web dashboard into the agent loop. It lives in core so the web package (producer) and the engine / tui (consumers) can share it without importing each other — the same neutral role the event bus plays.
var Bus = &eventBus{}
Bus is the process-wide event Bus.
var EmbedModel string
EmbedModel is the embedding model id (config "embed_model"); "" = disabled. Shared: set by the engine from config, read by the vault/embed tools.
var UseColor = func() bool { if os.Getenv("NO_COLOR") != "" { return false } fi, err := os.Stdout.Stat() return err == nil && fi.Mode()&os.ModeCharDevice != 0 }()
UseColor reports whether ANSI styling should be emitted: only when stdout is a real terminal and NO_COLOR is unset. Computed once at startup.
var VaultPath string
VaultPath is the configured Obsidian vault directory ("" = unconfigured). Shared config primitive: the vault tool feature (tools_ext) and the journal writer (agent) both read it.
Functions ¶
func ConfinePath ¶
ConfinePath resolves p (relative to base if not absolute) and verifies the result stays within base. It returns the cleaned absolute path, or an error if the path escapes base via "..", symlink-style tricks, or an absolute path pointing elsewhere. Use this before any file operation whose path derives from user- or model-supplied input.
func DrainBrowserSubmission ¶
func DrainBrowserSubmission() string
DrainBrowserSubmission returns a queued browser prompt if one is waiting, or "" immediately if none. Non-blocking.
func EmitAssistant ¶
func EmitAssistant(text string)
func EmitDiff ¶
func EmitDiff(text string)
EmitDiff emits pre-formatted, already-styled output (diffs, edit traces) that must NOT be re-rendered as markdown or word-wrapped. The TUI prints it verbatim in-viewport instead of letting raw fmt.Print corrupt the bubbletea alt-screen.
func EmitStatus ¶
func EmitStatus(text string)
func EmitThinking ¶
func EmitThinking(text string)
func EmitToolCall ¶
func EmitToolCall(tool, argsPreview string)
func EmitToolCallInline ¶
func EmitToolCallInline(tool, argsPreview string)
func EmitToolDone ¶
func EmitToolDone(tool, summary string)
func NewBus ¶
func NewBus() *eventBus
NewBus returns a fresh, empty event bus. Tests use it to exercise subscribe/emit in isolation from the process-global Bus.
func ParseTestFailures ¶
parseTestFailures distills `go test` (or build) output to the actionable parts: which tests failed and the file:line + message of each assertion. Returns "" when nothing failed (caller keeps the raw output for context).
func TermWidth ¶
func TermWidth() int
TermWidth returns the terminal's column width, trying stdout/stderr/stdin, then $COLUMNS, then a conservative default of 80.
func Tint ¶
Tint wraps s in the given ANSI SGR code (e.g. ColorRed), or returns s unchanged when color is disabled.
func VetCommand ¶
VetCommand returns a non-nil error if cmdStr matches a catastrophic, irreversible-damage pattern that must be blocked regardless of approval. It intentionally does NOT try to be a general command sanitizer — running arbitrary shell commands is the tool's purpose, and the approval gate is the primary control. This is only a last-resort guard against a handful of system-destroying mistakes.
Types ¶
type Event ¶
type Event struct {
Kind EventKind
Text string
// Optional structured payload for richer subscribers (TUI/dashboard).
// The stdout subscriber mostly uses Text.
Tool string // tool name (EvToolCall/EvToolDone)
Meta map[string]string // arbitrary extras (stats fields, args preview)
Color string // suggested color hint for the line (maps to term colors)
Time time.Time
}
Event is one thing that happened, timestamped. Fields beyond Kind/Text are optional and kind-specific; subscribers read what they need.
type EventKind ¶
type EventKind string
EventKind classifies a Bus event so subscribers can render selectively.
const ( EvLine EventKind = "line" // a plain status/trace line (the fmt.Println replacement) EvToken EventKind = "token" // a streamed assistant content token EvThinking EventKind = "thinking" // reasoning progress (token count / label) EvToolCall EventKind = "tool_call" // a tool invocation (name + short args) EvToolDone EventKind = "tool_done" // a tool result summary EvUser EventKind = "user" // the user's submitted message EvAssistant EventKind = "assistant" // a completed assistant message EvStats EventKind = "stats" // a stats/budget snapshot update EvStatus EventKind = "status" // connection/mode status (mcp connected, model, etc.) EvError EventKind = "error" // an error line EvBusy EventKind = "busy" // turn lifecycle: Text is "1" (working) or "0" (idle) EvApproval EventKind = "approval" // a tool/command awaits user approval (web mode) )
type Message ¶
type Message struct {
Role string `json:"role"` // system | user | assistant | tool
Content string `json:"content"`
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
ToolCallID string `json:"tool_call_id,omitempty"` // set on role=tool replies
}
Message is one turn in an OpenAI-compatible chat conversation. It is the central data type passed between the model client, session store, and UI.
type Subscriber ¶
type Subscriber interface {
OnEvent(Event)
}
Subscriber receives every event. Implementations must be non-blocking or buffer internally — the emit path holds a lock briefly and must not stall.
type SubscriberFunc ¶
type SubscriberFunc func(Event)
SubscriberFunc adapts a plain function to Subscriber.
func (SubscriberFunc) OnEvent ¶
func (f SubscriberFunc) OnEvent(e Event)
type ToolCall ¶
type ToolCall struct {
ID string `json:"id"`
Type string `json:"type"`
Function struct {
Name string `json:"name"`
Arguments string `json:"arguments"`
} `json:"function"`
}
ToolCall is a single tool invocation requested by the model. Arguments is a JSON *string* (OpenAI-compatible), unlike Ollama's pre-parsed map.