Documentation
¶
Overview ¶
Turn loop that streams the model, gates and runs its tool calls and repeats until it stops
Context management hooks that summarize old turns and drop stale thinking before a request
Index ¶
- Constants
- Variables
- func NextSeq() int64
- func Observe(ctx context.Context, ev Event)
- func WithLineage(ctx context.Context, parent string, depth int) context.Context
- func WithObserver(ctx context.Context, observe func(Event)) context.Context
- type Advice
- type Agent
- type Approval
- type Child
- type Dedup
- type Event
- type EventType
- type Factory
- type Hook
- type HookBlocked
- type HookCommand
- type HookConfig
- type HookEvent
- type HookExecution
- type HookGroup
- type HookRunner
- func (r HookRunner) AfterTool(ctx context.Context, call protocol.ToolCall, res protocol.ToolResult) (protocol.ToolResult, error)
- func (HookRunner) AfterTurn(context.Context, protocol.Response) error
- func (r HookRunner) BeforeRun(ctx context.Context, input []protocol.Part) ([]protocol.Part, error)
- func (r HookRunner) BeforeTool(ctx context.Context, call protocol.ToolCall) (Advice, error)
- func (HookRunner) BeforeTurn(context.Context, *protocol.Request) error
- func (r HookRunner) Fire(ctx context.Context, event HookEvent, fields map[string]any) (string, error)
- func (r HookRunner) OnStop(ctx context.Context, resp protocol.Response, again bool) ([]protocol.Part, error)
- type Hooks
- func (h Hooks) AfterTool(ctx context.Context, call protocol.ToolCall, result protocol.ToolResult) (protocol.ToolResult, error)
- func (h Hooks) AfterTurn(ctx context.Context, resp protocol.Response) error
- func (h Hooks) BeforeRun(ctx context.Context, input []protocol.Part) ([]protocol.Part, error)
- func (h Hooks) BeforeTool(ctx context.Context, call protocol.ToolCall) (Advice, error)
- func (h Hooks) BeforeTurn(ctx context.Context, req *protocol.Request) error
- func (h Hooks) OnStop(ctx context.Context, resp protocol.Response, again bool) ([]protocol.Part, error)
- type Kind
- type Lineage
- type Model
- type Pruner
- type StripThinking
- type Subagents
- func (t *Subagents) Annotations() tool.Annotations
- func (t *Subagents) Cancel(name string) error
- func (t *Subagents) Child(name string) (Child, bool)
- func (t *Subagents) Children() []Child
- func (t *Subagents) Definition() protocol.Tool
- func (t *Subagents) Run(ctx context.Context, args json.RawMessage) (tool.Result, error)
- func (t *Subagents) Steer(name string, parts ...protocol.Part) error
- type Summarizer
Constants ¶
const Omitted = "(reasoning omitted)"
Text left where an assistant turn held nothing but thinking
const SubagentTool = "Agent"
Name the parent model calls to delegate
Variables ¶
var ErrInterrupted = errors.New("agent: interrupted")
Error a run ends with after Interrupt was called
var ErrRounds = errors.New("rounds reached")
Error of a run that used every round it was allowed
var HookEvents = []HookEvent{UserPromptSubmit, PreToolUse, PostToolUse, Stop, PreCompact, SessionStart, SessionEnd, SubagentStop, Notification}
Every event a configuration may name
Functions ¶
func WithLineage ¶
Carries the parent and depth a child agent runs under
Types ¶
type Advice ¶
type Advice struct {
Call protocol.ToolCall
Decision policy.Decision
// Text appended to the tool result for the model
Context string
}
What a hook asks for a tool call before it runs
type Agent ¶
type Agent struct {
Name string
Model Model
ModelID string
System string
Tools *tool.Registry
Gate policy.Gate
Session *session.Session
// Request options copied onto every turn, the output budget left to the model when zero
MaxTokens int
Temperature *float64
Extra map[string]json.RawMessage
// Tool rounds one Run may take before the model must answer, unlimited when zero
MaxRounds int
Hooks []Hook
// contains filtered or unexported fields
}
Model, tools, policy and transcript wired into one loop
func (*Agent) Interrupt ¶
func (a *Agent) Interrupt()
Asks the run to stop after the current turn or tool round finishes
type Child ¶
type Child struct {
Name string
Description string
Parent string
Depth int
CallID string
Agent *Agent
Started time.Time
Finished time.Time
Running bool
Err error
Answer string
// contains filtered or unexported fields
}
One child the tool spawned, live or finished
type Dedup ¶
Hook that answers a repeated read only call whose result did not change by pointing at the earlier result
type Event ¶
type Event struct {
// Position across every agent in the process
Seq int64
Time time.Time
Type EventType
// Name of the agent that produced the event
Agent string
// Name of the agent that spawned the producer, empty at the root
Parent string
Depth int
// Model turn the event belongs to, counted from one within a run
Round int
Model protocol.Event
Call protocol.ToolCall
Result protocol.ToolResult
Data any
Duration time.Duration
// Why the call was refused before it ran, or why the run ended
Err error
Approval *Approval
Parts []protocol.Part
Text string
// Completed turn on turn end and the final response on run end
Response *protocol.Response
// Turn usage on turn end and run total on run end
Usage protocol.Usage
}
Progress of a run, stamped with its order, time and the agent lineage that produced it
type EventType ¶
type EventType string
const ( EventRunStart EventType = "run_start" EventRunEnd EventType = "run_end" EventTurnStart EventType = "turn_start" EventTurnEnd EventType = "turn_end" EventModel EventType = "model" EventToolStart EventType = "tool_start" EventToolEnd EventType = "tool_end" EventApproval EventType = "approval" EventSteer EventType = "steer" EventNote EventType = "note" EventCompactStart EventType = "compact_start" EventCompactEnd EventType = "compact_end" EventHook EventType = "hook" EventSubagentStart EventType = "subagent_start" EventSubagentEnd EventType = "subagent_end" EventInterrupt EventType = "interrupt" )
type Factory ¶
Builds a fresh child agent for one task, of the named kind or the default one when the name is empty
type Hook ¶
type Hook interface {
// Runs before the first turn and may rewrite the user input
BeforeRun(ctx context.Context, input []protocol.Part) ([]protocol.Part, error)
// Runs before every model turn and may rewrite the request
BeforeTurn(ctx context.Context, req *protocol.Request) error
// Runs after the assistant turn was appended to the session
AfterTurn(ctx context.Context, resp protocol.Response) error
// Runs before a tool call and may rewrite it, settle its permission, add context or refuse it with an error
BeforeTool(ctx context.Context, call protocol.ToolCall) (Advice, error)
// Runs after a tool call and may rewrite its result
AfterTool(ctx context.Context, call protocol.ToolCall, result protocol.ToolResult) (protocol.ToolResult, error)
// Runs when the model stops and may hand back parts that continue the run, again reporting an earlier continuation
OnStop(ctx context.Context, resp protocol.Response, again bool) ([]protocol.Part, error)
}
Steps of a run that outside code may observe and rewrite
type HookBlocked ¶
Refusal raised by a hook
func (*HookBlocked) Error ¶
func (b *HookBlocked) Error() string
type HookCommand ¶
type HookCommand struct {
Type string `json:"type,omitempty"`
Command string `json:"command"`
Timeout int `json:"timeout,omitempty"`
}
One command to run
type HookConfig ¶
func LoadHooks ¶
func LoadHooks(r io.Reader) (HookConfig, error)
Reads a hooks block from JSON, nested under hooks or at the top level, and checks every entry
func LoadHooksFile ¶
func LoadHooksFile(path string) (HookConfig, error)
func (HookConfig) Validate ¶
func (c HookConfig) Validate() error
Checks event names, command kinds and matcher patterns, compiling the patterns
type HookEvent ¶
type HookEvent string
Moment in a run a hook can attach to
const ( UserPromptSubmit HookEvent = "UserPromptSubmit" PreToolUse HookEvent = "PreToolUse" PostToolUse HookEvent = "PostToolUse" Stop HookEvent = "Stop" PreCompact HookEvent = "PreCompact" SessionStart HookEvent = "SessionStart" SessionEnd HookEvent = "SessionEnd" SubagentStop HookEvent = "SubagentStop" Notification HookEvent = "Notification" )
type HookExecution ¶
type HookExecution struct {
HookEvent HookEvent
Command string
Tool string
ExitCode int
Duration time.Duration
Stdout string
Stderr string
Blocked bool
}
What one hook execution produced, published as the data of a hook event
type HookGroup ¶
type HookGroup struct {
Matcher string `json:"matcher,omitempty"`
Hooks []HookCommand `json:"hooks"`
// contains filtered or unexported fields
}
Commands run for tools whose name matches the pattern, every tool when empty
type HookRunner ¶
type HookRunner struct {
Config HookConfig
// Working directory of hook commands, the process directory when empty
Dir string
// KEY=VALUE pairs layered over the process environment
Env []string
// Passed to hooks as session_id and ICEBATH_SESSION_ID
SessionID string
// Path of the stored session log, passed to hooks as transcript_path
Transcript string
// Receives hook standard error and any standard hookOutput that is not JSON, discarded when nil
Log io.Writer
}
Runs configured hooks as an agent hook
func (HookRunner) AfterTool ¶
func (r HookRunner) AfterTool(ctx context.Context, call protocol.ToolCall, res protocol.ToolResult) (protocol.ToolResult, error)
Runs PostToolUse hooks, an updated response replacing the result and a block marking it as failed with the reason
func (HookRunner) BeforeTool ¶
Runs PreToolUse hooks, carrying their rewritten input, settled permission and context back as advice
func (HookRunner) BeforeTurn ¶
type Hooks ¶
type Hooks struct {
BeforeRunFn func(ctx context.Context, input []protocol.Part) ([]protocol.Part, error)
BeforeTurnFn func(ctx context.Context, req *protocol.Request) error
AfterTurnFn func(ctx context.Context, resp protocol.Response) error
BeforeToolFn func(ctx context.Context, call protocol.ToolCall) (Advice, error)
AfterToolFn func(ctx context.Context, call protocol.ToolCall, result protocol.ToolResult) (protocol.ToolResult, error)
OnStopFn func(ctx context.Context, resp protocol.Response, again bool) ([]protocol.Part, error)
}
Hook assembled from optional functions, unset ones passing through
func (Hooks) AfterTool ¶
func (h Hooks) AfterTool(ctx context.Context, call protocol.ToolCall, result protocol.ToolResult) (protocol.ToolResult, error)
func (Hooks) BeforeTool ¶
func (Hooks) BeforeTurn ¶
type Lineage ¶
Who spawned an agent and how deep it sits
func LineageFrom ¶
Lineage carried by the context, the root lineage when none was set
type Model ¶
type Model interface {
Stream(ctx context.Context, req protocol.Request) iter.Seq2[protocol.Event, error]
}
Streams one turn, satisfied by a provider
type Pruner ¶
Hook that swaps tool results older than After assistant turns for one line stubs in the request
type StripThinking ¶
type StripThinking struct {
Hooks
// contains filtered or unexported fields
}
Hook that keeps thinking only on the newest turn of the run and only while the request before it is unchanged
func (*StripThinking) BeforeTurn ¶
type Subagents ¶
type Subagents struct {
Factory Factory
// Levels of children allowed below the root, unlimited when zero
MaxDepth int
// Budget for one child run, unlimited when zero
Timeout time.Duration
// Lets several children of one round run at the same time
Parallel bool
// Kinds the model may name, none when nil
Kinds func() []Kind
// contains filtered or unexported fields
}
Tool that runs a child agent to completion and returns its final text
func (*Subagents) Annotations ¶
func (t *Subagents) Annotations() tool.Annotations
Destructive and open world always, and read only when children may run side by side
func (*Subagents) Definition ¶
type Summarizer ¶
type Summarizer struct {
Hooks
Model Model
ModelID string
Session *session.Session
// Context tokens, as measured by the last response plus what was added since, at which a summary is made
Threshold int
// Messages kept verbatim after the summary, none when zero
Keep int
// Extra guidance appended to the summary instructions
Instructions string
// Runs before a summary is made, such as external PreCompact hooks
Before func(ctx context.Context) error
}
Hook that replaces the oldest turns with a model written summary once the context grows past a threshold
func (*Summarizer) BeforeTurn ¶
Summarizes and compacts the session when the pending request would grow past the threshold