Documentation
¶
Overview ¶
Package handler defines the AgentEventHandler interface that decouples the agent runner from any specific UI implementation (TUI, ACP, Web, etc.).
All agent-loop events flow through this interface. Concrete implementations adapt the events to the target transport (BubbleTea, HTTP/SSE, ACP JSON-RPC…).
Index ¶
- func MarshalEvent(ev WebEvent) ([]byte, error)
- type ACPHandler
- func (h *ACPHandler) OnAgentDone(err error)
- func (h *ACPHandler) OnAgentText(text string)
- func (h *ACPHandler) OnTodoUpdate()
- func (h *ACPHandler) OnTokenUpdate(info TokenUsage)
- func (h *ACPHandler) OnToolCall(name, args, _ string)
- func (h *ACPHandler) OnToolResult(name, output, _ string, err error)
- func (h *ACPHandler) RequestApproval(ctx context.Context, req ApprovalRequest) (ApprovalResponse, error)
- type AgentEventHandler
- type ApprovalMode
- type ApprovalRequest
- type ApprovalResponse
- type NotifyingHandler
- func (h *NotifyingHandler) AddNotifier(n channel.Notifier)
- func (h *NotifyingHandler) CloseNotifiers()
- func (h *NotifyingHandler) OnAgentDone(err error)
- func (h *NotifyingHandler) OnAgentText(text string)
- func (h *NotifyingHandler) OnTodoUpdate()
- func (h *NotifyingHandler) OnTokenUpdate(info TokenUsage)
- func (h *NotifyingHandler) OnToolCall(name, args, toolCallID string)
- func (h *NotifyingHandler) OnToolResult(name, output, toolCallID string, err error)
- func (h *NotifyingHandler) RequestApproval(ctx context.Context, req ApprovalRequest) (ApprovalResponse, error)
- func (h *NotifyingHandler) SetApprovalNotifier(fn func(toolName, toolArgs string))
- func (h *NotifyingHandler) SetDoneNotifier(fn func(summary string, err error))
- type TUIHandler
- func (h *TUIHandler) OnAgentDone(err error)
- func (h *TUIHandler) OnAgentText(text string)
- func (h *TUIHandler) OnTodoUpdate()
- func (h *TUIHandler) OnTokenUpdate(info TokenUsage)
- func (h *TUIHandler) OnToolCall(name, args, _ string)
- func (h *TUIHandler) OnToolResult(name, output, _ string, err error)
- func (h *TUIHandler) RequestApproval(ctx context.Context, req ApprovalRequest) (ApprovalResponse, error)
- func (h *TUIHandler) SetProgram(p *tea.Program)
- type TokenUsage
- type WebApprovalRequestData
- type WebDoneData
- type WebEvent
- type WebHandler
- func (h *WebHandler) Emit(event string, data any)
- func (h *WebHandler) Events() <-chan WebEvent
- func (h *WebHandler) OnAgentDone(err error)
- func (h *WebHandler) OnAgentText(text string)
- func (h *WebHandler) OnSubagentEvent(name, agentType string, done bool, result string, err error)
- func (h *WebHandler) OnSubagentProgress(agentName, event, toolName, detail string)
- func (h *WebHandler) OnTodoUpdate()
- func (h *WebHandler) OnTokenUpdate(info TokenUsage)
- func (h *WebHandler) OnToolCall(name, args, toolCallID string)
- func (h *WebHandler) OnToolResult(name, output, toolCallID string, err error)
- func (h *WebHandler) RequestApproval(ctx context.Context, req ApprovalRequest) (ApprovalResponse, error)
- func (h *WebHandler) ResolveApproval(id string, approved bool) error
- type WebSubagentData
- type WebSubagentProgressData
- type WebTextData
- type WebTokenData
- type WebToolCallData
- type WebToolResultData
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MarshalEvent ¶
MarshalEvent marshals a WebEvent to JSON bytes.
Types ¶
type ACPHandler ¶
type ACPHandler struct {
// contains filtered or unexported fields
}
ACPHandler implements AgentEventHandler by sending ACP SessionUpdate notifications through an AgentSideConnection to the connected client.
func NewACPHandler ¶
func NewACPHandler(conn *acp.AgentSideConnection, sessionID acp.SessionId) *ACPHandler
NewACPHandler creates a handler bound to an ACP connection and session.
func (*ACPHandler) OnAgentDone ¶
func (h *ACPHandler) OnAgentDone(err error)
func (*ACPHandler) OnAgentText ¶
func (h *ACPHandler) OnAgentText(text string)
func (*ACPHandler) OnTodoUpdate ¶
func (h *ACPHandler) OnTodoUpdate()
func (*ACPHandler) OnTokenUpdate ¶
func (h *ACPHandler) OnTokenUpdate(info TokenUsage)
func (*ACPHandler) OnToolCall ¶
func (h *ACPHandler) OnToolCall(name, args, _ string)
func (*ACPHandler) OnToolResult ¶
func (h *ACPHandler) OnToolResult(name, output, _ string, err error)
func (*ACPHandler) RequestApproval ¶
func (h *ACPHandler) RequestApproval(ctx context.Context, req ApprovalRequest) (ApprovalResponse, error)
type AgentEventHandler ¶
type AgentEventHandler interface {
// OnAgentText is called when the agent emits a text chunk (streaming).
OnAgentText(text string)
// OnToolCall is called at the beginning of a tool invocation.
OnToolCall(name, args, toolCallID string)
// OnToolResult is called when a tool execution completes.
OnToolResult(name, output, toolCallID string, err error)
// OnTodoUpdate is called when the todo store is mutated.
OnTodoUpdate()
// OnAgentDone is called when the agent loop finishes (err may be nil).
OnAgentDone(err error)
// OnTokenUpdate reports cumulative token usage after a run.
OnTokenUpdate(info TokenUsage)
// RequestApproval asks the UI for tool-execution permission.
// It blocks until the user responds or ctx is cancelled.
// Returns (approved, newMode, error).
RequestApproval(ctx context.Context, req ApprovalRequest) (ApprovalResponse, error)
}
AgentEventHandler is the primary abstraction between the agent runner and the presentation layer. It covers three concerns:
- Output events — one-way notifications from agent to UI.
- Approval flow — bidirectional: agent requests permission, UI responds.
- Lifecycle — done signals, token usage, etc.
Implementations must be safe for concurrent use; the runner may call methods from multiple goroutines (e.g. streaming text while a tool result arrives).
type ApprovalMode ¶
type ApprovalMode int
ApprovalMode mirrors tui.ApprovalMode so that handler consumers don't import tui.
const ( ModeManual ApprovalMode = iota ModeAuto )
type ApprovalRequest ¶
type ApprovalRequest struct {
ToolName string
ToolArgs string
ToolCallID string // unique ID of this tool invocation (from the LLM)
IsExternal bool // true when accessing paths outside workpath
WorkerName string // non-empty for teammate agents
WorkerColor string
}
ApprovalRequest describes a tool that needs user permission.
type ApprovalResponse ¶
type ApprovalResponse struct {
Approved bool
Mode ApprovalMode
}
ApprovalResponse is what the UI returns for an approval request.
type NotifyingHandler ¶ added in v0.1.1
type NotifyingHandler struct {
// contains filtered or unexported fields
}
NotifyingHandler wraps another AgentEventHandler and adds delayed notification capabilities for approval requests and agent completion events. It also supports a list of channel.Notifier instances for lightweight one-way status pushes (e.g. BLE IoT devices).
func NewNotifyingHandler ¶ added in v0.1.1
func NewNotifyingHandler(inner AgentEventHandler, delay time.Duration) *NotifyingHandler
NewNotifyingHandler creates a handler that wraps inner and can fire external notifications for approvals (after a delay) and agent completion.
func (*NotifyingHandler) AddNotifier ¶ added in v0.3.1
func (h *NotifyingHandler) AddNotifier(n channel.Notifier)
AddNotifier registers a lightweight notifier (e.g. BLE device). Notifiers receive automatic status pushes for agent lifecycle events.
func (*NotifyingHandler) CloseNotifiers ¶ added in v0.3.1
func (h *NotifyingHandler) CloseNotifiers()
CloseNotifiers closes all registered notifiers.
func (*NotifyingHandler) OnAgentDone ¶ added in v0.1.1
func (h *NotifyingHandler) OnAgentDone(err error)
func (*NotifyingHandler) OnAgentText ¶ added in v0.1.1
func (h *NotifyingHandler) OnAgentText(text string)
func (*NotifyingHandler) OnTodoUpdate ¶ added in v0.1.1
func (h *NotifyingHandler) OnTodoUpdate()
func (*NotifyingHandler) OnTokenUpdate ¶ added in v0.1.1
func (h *NotifyingHandler) OnTokenUpdate(info TokenUsage)
func (*NotifyingHandler) OnToolCall ¶ added in v0.1.1
func (h *NotifyingHandler) OnToolCall(name, args, toolCallID string)
func (*NotifyingHandler) OnToolResult ¶ added in v0.1.1
func (h *NotifyingHandler) OnToolResult(name, output, toolCallID string, err error)
func (*NotifyingHandler) RequestApproval ¶ added in v0.1.1
func (h *NotifyingHandler) RequestApproval(ctx context.Context, req ApprovalRequest) (ApprovalResponse, error)
func (*NotifyingHandler) SetApprovalNotifier ¶ added in v0.1.1
func (h *NotifyingHandler) SetApprovalNotifier(fn func(toolName, toolArgs string))
SetApprovalNotifier sets the callback fired when an approval is not resolved within the delay.
func (*NotifyingHandler) SetDoneNotifier ¶ added in v0.1.1
func (h *NotifyingHandler) SetDoneNotifier(fn func(summary string, err error))
SetDoneNotifier sets the callback fired when the agent finishes.
type TUIHandler ¶
type TUIHandler struct {
// contains filtered or unexported fields
}
TUIHandler adapts AgentEventHandler to a BubbleTea *tea.Program. It translates every interface method into a p.Send(msg) call using the existing TUI message types.
func NewTUIHandler ¶
func NewTUIHandler(p *tea.Program) *TUIHandler
NewTUIHandler creates a handler backed by a BubbleTea program.
func (*TUIHandler) OnAgentDone ¶
func (h *TUIHandler) OnAgentDone(err error)
func (*TUIHandler) OnAgentText ¶
func (h *TUIHandler) OnAgentText(text string)
func (*TUIHandler) OnTodoUpdate ¶
func (h *TUIHandler) OnTodoUpdate()
func (*TUIHandler) OnTokenUpdate ¶
func (h *TUIHandler) OnTokenUpdate(info TokenUsage)
func (*TUIHandler) OnToolCall ¶
func (h *TUIHandler) OnToolCall(name, args, _ string)
func (*TUIHandler) OnToolResult ¶
func (h *TUIHandler) OnToolResult(name, output, _ string, err error)
func (*TUIHandler) RequestApproval ¶
func (h *TUIHandler) RequestApproval(ctx context.Context, req ApprovalRequest) (ApprovalResponse, error)
func (*TUIHandler) SetProgram ¶
func (h *TUIHandler) SetProgram(p *tea.Program)
SetProgram replaces the underlying BubbleTea program (e.g. after the program is created lazily).
type TokenUsage ¶
type TokenUsage struct {
PromptTokens int64
CompletionTokens int64
TotalTokens int64
ModelContextLimit int // 0 if unknown
}
TokenUsage carries cumulative token counters.
type WebApprovalRequestData ¶
type WebApprovalRequestData struct {
ID string `json:"id"`
ToolName string `json:"tool_name"`
ToolArgs string `json:"tool_args"`
IsExternal bool `json:"is_external"`
}
WebApprovalRequestData carries an approval request.
type WebDoneData ¶
type WebDoneData struct {
Error string `json:"error,omitempty"`
}
WebDoneData signals agent completion.
type WebHandler ¶
type WebHandler struct {
// contains filtered or unexported fields
}
WebHandler implements AgentEventHandler by sending events to web clients through a channel-based event broker.
func NewWebHandler ¶
func NewWebHandler() *WebHandler
NewWebHandler creates a handler that sends events to the given channel.
func (*WebHandler) Emit ¶ added in v0.1.1
func (h *WebHandler) Emit(event string, data any)
Emit sends a custom event to all connected web clients.
func (*WebHandler) Events ¶
func (h *WebHandler) Events() <-chan WebEvent
Events returns the read-only event channel.
func (*WebHandler) OnAgentDone ¶
func (h *WebHandler) OnAgentDone(err error)
func (*WebHandler) OnAgentText ¶
func (h *WebHandler) OnAgentText(text string)
func (*WebHandler) OnSubagentEvent ¶
func (h *WebHandler) OnSubagentEvent(name, agentType string, done bool, result string, err error)
func (*WebHandler) OnSubagentProgress ¶
func (h *WebHandler) OnSubagentProgress(agentName, event, toolName, detail string)
func (*WebHandler) OnTodoUpdate ¶
func (h *WebHandler) OnTodoUpdate()
func (*WebHandler) OnTokenUpdate ¶
func (h *WebHandler) OnTokenUpdate(info TokenUsage)
func (*WebHandler) OnToolCall ¶
func (h *WebHandler) OnToolCall(name, args, toolCallID string)
func (*WebHandler) OnToolResult ¶
func (h *WebHandler) OnToolResult(name, output, toolCallID string, err error)
func (*WebHandler) RequestApproval ¶
func (h *WebHandler) RequestApproval(ctx context.Context, req ApprovalRequest) (ApprovalResponse, error)
func (*WebHandler) ResolveApproval ¶
func (h *WebHandler) ResolveApproval(id string, approved bool) error
ResolveApproval resolves a pending approval request. Called by API handler.
type WebSubagentData ¶
type WebSubagentData struct {
Name string `json:"name"`
AgentType string `json:"agent_type"`
Done bool `json:"done"`
Result string `json:"result,omitempty"`
Error string `json:"error,omitempty"`
}
WebSubagentData carries subagent lifecycle events.
type WebSubagentProgressData ¶
type WebSubagentProgressData struct {
AgentName string `json:"agent_name"`
Event string `json:"event"` // "tool_call" or "tool_result"
ToolName string `json:"tool_name"`
Detail string `json:"detail"`
}
WebSubagentProgressData carries intermediate subagent tool call/result events.
type WebTextData ¶
type WebTextData struct {
Text string `json:"text"`
}
WebTextData carries a streaming text chunk.
type WebTokenData ¶
type WebTokenData struct {
PromptTokens int64 `json:"prompt_tokens"`
CompletionTokens int64 `json:"completion_tokens"`
TotalTokens int64 `json:"total_tokens"`
ModelContextLimit int `json:"model_context_limit"`
}
WebTokenData carries token usage.
type WebToolCallData ¶
type WebToolCallData struct {
Name string `json:"name"`
Args string `json:"args"`
ToolCallID string `json:"tool_call_id,omitempty"`
}
WebToolCallData carries tool invocation info.