Documentation
¶
Overview ¶
Package app provides the main application structure.
Index ¶
- Variables
- func AggregateTokens(results map[string]*SubagentResult) int
- func FilterResults(results map[string]*SubagentResult, successOnly bool) map[string]*SubagentResult
- func MergeResults(results map[string]*SubagentResult) []any
- type Action
- type AgentLoop
- type App
- type Feedback
- type LoopConfig
- type LoopContext
- type LoopRunner
- type LoopState
- type Message
- type Option
- func WithClient(c client.Client) Option
- func WithModel(model string) Option
- func WithOutputFormat(formatter output.Formatter) Option
- func WithProvider(provider string) Option
- func WithRunFunc(fn RunFunc) Option
- func WithSDKOption(opt claude.ClientOption) Option
- func WithSystemPrompt(prompt string) Option
- func WithTool(tool *tools.Tool) Option
- type Result
- type RunFunc
- type ShutdownHook
- type SimpleLoop
- func (l *SimpleLoop) DecideAction(ctx context.Context, state *LoopState) (*Action, error)
- func (l *SimpleLoop) GatherContext(ctx context.Context, state *LoopState) (*LoopContext, error)
- func (l *SimpleLoop) ShouldContinue(state *LoopState) bool
- func (l *SimpleLoop) TakeAction(ctx context.Context, action *Action) (*Result, error)
- func (l *SimpleLoop) Verify(ctx context.Context, state *LoopState) (*Feedback, error)
- type SimpleLoopOption
- func WithActionFunc(fn func(ctx context.Context, action *Action) (*Result, error)) SimpleLoopOption
- func WithContinueFunc(fn func(state *LoopState) bool) SimpleLoopOption
- func WithDecideFunc(fn func(ctx context.Context, state *LoopState) (*Action, error)) SimpleLoopOption
- func WithGatherFunc(fn func(ctx context.Context, state *LoopState) (*LoopContext, error)) SimpleLoopOption
- func WithVerifyFunc(fn func(ctx context.Context, state *LoopState) (*Feedback, error)) SimpleLoopOption
- type Subagent
- type SubagentConfig
- type SubagentContext
- type SubagentExecutor
- type SubagentExecutorFunc
- type SubagentManager
- func (m *SubagentManager) Clear()
- func (m *SubagentManager) Get(id string) *Subagent
- func (m *SubagentManager) List() []*Subagent
- func (m *SubagentManager) Run(ctx context.Context, agent *Subagent) (*SubagentResult, error)
- func (m *SubagentManager) RunAgents(ctx context.Context, agents ...*Subagent) (map[string]*SubagentResult, error)
- func (m *SubagentManager) RunAll(ctx context.Context) (map[string]*SubagentResult, error)
- func (m *SubagentManager) Spawn(name, task string, opts ...SubagentOption) (*Subagent, error)
- type SubagentOption
- type SubagentResult
- type ToolInfo
Constants ¶
This section is empty.
Variables ¶
var ErrAllAgentsFailed = errors.New("all subagents failed")
ErrAllAgentsFailed is returned when all subagents fail execution.
var ErrMaxSubagentsReached = errors.New("maximum subagents reached")
ErrMaxSubagentsReached is returned when spawning would exceed the limit.
Functions ¶
func AggregateTokens ¶
func AggregateTokens(results map[string]*SubagentResult) int
AggregateTokens sums token usage across results.
func FilterResults ¶
func FilterResults(results map[string]*SubagentResult, successOnly bool) map[string]*SubagentResult
FilterResults filters subagent results by success status.
func MergeResults ¶
func MergeResults(results map[string]*SubagentResult) []any
MergeResults combines outputs from multiple subagent results.
Types ¶
type Action ¶
type Action struct {
Type string // "tool_call", "response", "delegate"
ToolName string // For tool calls
ToolInput map[string]any // Tool arguments
Response string // For direct responses
Subagent string // For delegation
SubagentTask string
}
Action represents an action to take.
type AgentLoop ¶
type AgentLoop interface {
// GatherContext collects relevant context for the current iteration.
GatherContext(ctx context.Context, state *LoopState) (*LoopContext, error)
// DecideAction determines what action to take based on context.
DecideAction(ctx context.Context, state *LoopState) (*Action, error)
// TakeAction executes the decided action.
TakeAction(ctx context.Context, action *Action) (*Result, error)
// Verify validates the result and provides feedback.
Verify(ctx context.Context, state *LoopState) (*Feedback, error)
// ShouldContinue determines if the loop should continue.
ShouldContinue(state *LoopState) bool
}
AgentLoop defines the core agent loop interface. Pattern: gather context → take action → verify → repeat
type App ¶
type App struct {
// contains filtered or unexported fields
}
App represents the main application.
func (*App) Output ¶
func (a *App) Output() *output.Dispatcher
Output returns the output dispatcher.
type LoopConfig ¶
type LoopConfig struct {
// MaxIterations limits loop cycles (safety). 0 = unlimited.
MaxIterations int
// MaxTokens limits total tokens before compaction.
MaxTokens int
// Timeout for the entire loop.
Timeout time.Duration
// ShutdownTimeout is the maximum time to wait for shutdown hooks.
// Default: 30 seconds.
ShutdownTimeout time.Duration
// StopOnError halts the loop on first error.
StopOnError bool
// MinScore minimum verification score to continue.
MinScore float64
// Hooks for extensibility
OnIterationStart func(state *LoopState)
OnIterationEnd func(state *LoopState)
OnError func(err error, state *LoopState)
}
LoopConfig configures the agent loop behavior.
func DefaultLoopConfig ¶
func DefaultLoopConfig() *LoopConfig
DefaultLoopConfig returns sensible defaults.
type LoopContext ¶
LoopContext holds gathered context for an iteration.
type LoopRunner ¶
type LoopRunner struct {
// contains filtered or unexported fields
}
LoopRunner executes an agent loop.
func NewLoopRunner ¶
func NewLoopRunner(loop AgentLoop, config *LoopConfig) *LoopRunner
NewLoopRunner creates a new loop runner.
func (*LoopRunner) OnShutdown ¶
func (r *LoopRunner) OnShutdown(hook ShutdownHook)
OnShutdown registers a hook to be called during graceful shutdown. Hooks are called in reverse registration order (LIFO). The hook receives a context with the shutdown timeout applied.
type LoopState ¶
type LoopState struct {
Iteration int
Context *LoopContext
LastAction *Action
LastResult *Result
LastVerify *Feedback
StartedAt time.Time
CompletedAt time.Time
}
LoopState represents the current state of an agent loop iteration.
type Option ¶
type Option func(*App)
Option is a functional option for configuring App.
func WithClient ¶
WithClient sets a custom client (useful for testing).
func WithOutputFormat ¶
WithOutputFormat registers a custom output formatter.
func WithSDKOption ¶
func WithSDKOption(opt claude.ClientOption) Option
WithSDKOption adds a Claude SDK client option.
func WithSystemPrompt ¶
WithSystemPrompt sets the system prompt.
type ShutdownHook ¶
ShutdownHook is a function called during graceful shutdown. The context has the shutdown timeout applied.
type SimpleLoop ¶
type SimpleLoop struct {
// contains filtered or unexported fields
}
SimpleLoop provides a basic AgentLoop implementation.
func NewSimpleLoop ¶
func NewSimpleLoop(opts ...SimpleLoopOption) *SimpleLoop
NewSimpleLoop creates a configurable simple loop.
func (*SimpleLoop) DecideAction ¶
DecideAction implements AgentLoop.
func (*SimpleLoop) GatherContext ¶
func (l *SimpleLoop) GatherContext(ctx context.Context, state *LoopState) (*LoopContext, error)
GatherContext implements AgentLoop.
func (*SimpleLoop) ShouldContinue ¶
func (l *SimpleLoop) ShouldContinue(state *LoopState) bool
ShouldContinue implements AgentLoop.
func (*SimpleLoop) TakeAction ¶
TakeAction implements AgentLoop.
type SimpleLoopOption ¶
type SimpleLoopOption func(*SimpleLoop)
SimpleLoopOption configures a SimpleLoop.
func WithActionFunc ¶
WithActionFunc sets the action execution function.
func WithContinueFunc ¶
func WithContinueFunc(fn func(state *LoopState) bool) SimpleLoopOption
WithContinueFunc sets the continuation check function.
func WithDecideFunc ¶
func WithDecideFunc(fn func(ctx context.Context, state *LoopState) (*Action, error)) SimpleLoopOption
WithDecideFunc sets the action decision function.
func WithGatherFunc ¶
func WithGatherFunc(fn func(ctx context.Context, state *LoopState) (*LoopContext, error)) SimpleLoopOption
WithGatherFunc sets the context gathering function.
func WithVerifyFunc ¶
func WithVerifyFunc(fn func(ctx context.Context, state *LoopState) (*Feedback, error)) SimpleLoopOption
WithVerifyFunc sets the verification function.
type Subagent ¶
type Subagent struct {
ID string
Name string
Task string
Context *SubagentContext
Result *SubagentResult
// contains filtered or unexported fields
}
Subagent represents an isolated child agent with its own context.
type SubagentConfig ¶
type SubagentConfig struct {
// MaxConcurrent limits parallel subagent execution.
MaxConcurrent int
// MaxSubagents limits total subagents that can be spawned (0 = unlimited).
MaxSubagents int
// IsolateContext creates fresh context per subagent.
IsolateContext bool
ShareTools bool
// PropagateCancel cancels children when parent cancels.
PropagateCancel bool
}
SubagentConfig configures subagent behavior.
func DefaultSubagentConfig ¶
func DefaultSubagentConfig() *SubagentConfig
DefaultSubagentConfig returns sensible defaults.
type SubagentContext ¶
type SubagentContext struct {
Messages []Message
Tools []ToolInfo
SystemPrompt string
State map[string]any
MaxTokens int
}
SubagentContext holds isolated context for a subagent.
type SubagentExecutor ¶
type SubagentExecutor interface {
Execute(ctx context.Context, agent *Subagent) (*SubagentResult, error)
}
SubagentExecutor defines how to run a subagent.
type SubagentExecutorFunc ¶
type SubagentExecutorFunc func(ctx context.Context, agent *Subagent) (*SubagentResult, error)
SubagentExecutorFunc is a function adapter for SubagentExecutor.
func (SubagentExecutorFunc) Execute ¶
func (f SubagentExecutorFunc) Execute(ctx context.Context, agent *Subagent) (*SubagentResult, error)
type SubagentManager ¶
type SubagentManager struct {
// contains filtered or unexported fields
}
SubagentManager coordinates multiple subagents.
func NewSubagentManager ¶
func NewSubagentManager(config *SubagentConfig, executor SubagentExecutor) *SubagentManager
NewSubagentManager creates a new subagent manager.
func (*SubagentManager) Get ¶
func (m *SubagentManager) Get(id string) *Subagent
Get retrieves a subagent by ID.
func (*SubagentManager) List ¶
func (m *SubagentManager) List() []*Subagent
List returns all subagents.
func (*SubagentManager) Run ¶
func (m *SubagentManager) Run(ctx context.Context, agent *Subagent) (*SubagentResult, error)
Run executes a single subagent.
func (*SubagentManager) RunAgents ¶
func (m *SubagentManager) RunAgents(ctx context.Context, agents ...*Subagent) (map[string]*SubagentResult, error)
RunAgents executes specific subagents concurrently.
func (*SubagentManager) RunAll ¶
func (m *SubagentManager) RunAll(ctx context.Context) (map[string]*SubagentResult, error)
RunAll executes all spawned subagents concurrently.
func (*SubagentManager) Spawn ¶
func (m *SubagentManager) Spawn(name, task string, opts ...SubagentOption) (*Subagent, error)
Spawn creates a new subagent with isolated context. Returns nil and ErrMaxSubagentsReached if the limit is exceeded.
type SubagentOption ¶
type SubagentOption func(*Subagent)
SubagentOption configures a subagent.
func WithSubagentMaxTokens ¶
func WithSubagentMaxTokens(max int) SubagentOption
WithSubagentMaxTokens sets the token limit.
func WithSubagentMessages ¶
func WithSubagentMessages(messages []Message) SubagentOption
WithSubagentMessages sets initial messages.
func WithSubagentPrompt ¶
func WithSubagentPrompt(prompt string) SubagentOption
WithSubagentPrompt sets the subagent's system prompt.
func WithSubagentState ¶
func WithSubagentState(state map[string]any) SubagentOption
WithSubagentState sets initial state.
func WithSubagentTools ¶
func WithSubagentTools(tools []ToolInfo) SubagentOption
WithSubagentTools sets available tools.
type SubagentResult ¶
SubagentResult contains the outcome of a subagent's work.