Documentation
¶
Index ¶
- func AnyToolFailed(results []convdomain.ConversationEntry) bool
- func AnyToolRejected(results []convdomain.ConversationEntry) bool
- type AgentContext
- type AgentEvent
- type AgentExecutionState
- type AgentStateMachine
- type AllToolsProcessedEvent
- type ApprovalDecision
- type ApprovalFailedEvent
- type ApprovingToolsState
- type BlockingToolsState
- type CallOrder
- type CancelledState
- type CheckingQueueState
- type CompletingState
- type CompletionRequestedEvent
- type ErrorState
- type EvaluatingToolsState
- type ExecutingToolsState
- type IdleState
- type InputRequiredState
- type MessageReceivedEvent
- type PostStreamState
- type PostToolExecutionState
- type StartStreamingEvent
- type StateAction
- type StateContext
- type StateGuard
- type StateHandler
- func NewApprovingToolsState(ctx *StateContext) StateHandler
- func NewBlockingToolsState(ctx *StateContext) StateHandler
- func NewCancelledState(ctx *StateContext) StateHandler
- func NewCheckingQueueState(ctx *StateContext) StateHandler
- func NewCompletingState(ctx *StateContext) StateHandler
- func NewErrorState(ctx *StateContext) StateHandler
- func NewEvaluatingToolsState(ctx *StateContext) StateHandler
- func NewExecutingToolsState(ctx *StateContext) StateHandler
- func NewIdleState(ctx *StateContext) StateHandler
- func NewInputRequiredState(ctx *StateContext) StateHandler
- func NewPostStreamState(ctx *StateContext) StateHandler
- func NewPostToolExecutionState(ctx *StateContext) StateHandler
- func NewStoppedState(ctx *StateContext) StateHandler
- func NewStreamingLLMState(ctx *StateContext) StateHandler
- type StoppedState
- type StreamCompletedEvent
- type StreamingLLMState
- type ToolsCompletedEvent
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AnyToolFailed ¶
func AnyToolFailed(results []convdomain.ConversationEntry) bool
AnyToolFailed reports whether any entry in a completed tool batch failed (executed with a non-success result). It backs the post_tool `on_failure` reminder trigger. Callers set AgentContext.LastToolFailed from it when a batch completes.
func AnyToolRejected ¶
func AnyToolRejected(results []convdomain.ConversationEntry) bool
AnyToolRejected reports whether any entry in a completed tool batch was rejected by the user. A rejection ends the agent turn instead of feeding the results back for another LLM response.
Types ¶
type AgentContext ¶
type AgentContext struct {
RequestID string
Conversation *[]sdk.Message
MessageQueue convdomain.MessageQueue
ConversationRepo convdomain.ConversationRepository
ToolCalls []*sdk.ChatCompletionMessageToolCall
Turns int
MaxTurns int
StartedAt time.Time
MaxDuration time.Duration
HasToolResults bool
LastToolFailed bool
ApprovalPolicy agentdomain.ApprovalPolicy
Ctx context.Context
IsChatMode bool
MaxTurnsExceeded bool
MaxDurationExceeded bool
}
AgentContext is the execution context of the agent state machine. MaxTurnsExceeded and MaxDurationExceeded are set when the run is forced into Completing because the turn limit or the time budget ran out first.
type AgentEvent ¶
type AgentEvent interface {
EventType() string
}
AgentEvent represents an event in the event-driven agent system
type AgentExecutionState ¶
type AgentExecutionState int
AgentExecutionState represents the state of the agent execution loop This is a more granular state than ChatStatus and is used for the state machine
const ( // StateIdle indicates no active work StateIdle AgentExecutionState = iota // StateCheckingQueue indicates examining message queue StateCheckingQueue // StateStreamingLLM indicates waiting for LLM response StateStreamingLLM // StatePostStream indicates after stream, before tool evaluation StatePostStream // StateEvaluatingTools indicates categorizing tool calls StateEvaluatingTools // StateApprovingTools indicates waiting for user approvals (sequential) StateApprovingTools // StateBlockingTools indicates approval is required but no approver is // reachable (approval_behaviour resolves to block), so the gated tool calls // are rejected with a reason instead of being prompted or executed. StateBlockingTools // StateExecutingTools indicates running tools (parallel) StateExecutingTools // StatePostToolExecution indicates after all tools complete StatePostToolExecution // StateCompleting indicates finalizing loop StateCompleting // StateInputRequired indicates the run is blocked on the user: a tool // approval, a sandbox approval or a question is waiting for an answer StateInputRequired // StateStopped indicates loop terminated StateStopped // StateCancelled indicates user cancelled StateCancelled // StateError indicates error occurred StateError )
func (AgentExecutionState) IsWorking ¶ added in v0.226.0
func (s AgentExecutionState) IsWorking() bool
IsWorking reports whether the agent is spending its own time in this state. At rest, at a terminal state and while blocked on the user it is not.
func (AgentExecutionState) String ¶
func (s AgentExecutionState) String() string
type AgentStateMachine ¶
type AgentStateMachine interface {
// Transition attempts to transition to the target state
Transition(ctx *AgentContext, targetState AgentExecutionState) error
// GetCurrentState returns the current state (thread-safe)
GetCurrentState() AgentExecutionState
// CanTransition checks if a transition is valid without executing it
CanTransition(ctx *AgentContext, targetState AgentExecutionState) bool
// Reset resets the state machine to idle
Reset()
}
AgentStateMachine manages agent execution state transitions
type AllToolsProcessedEvent ¶
type AllToolsProcessedEvent struct{}
AllToolsProcessedEvent is triggered when all tools have been processed
func (AllToolsProcessedEvent) EventType ¶
func (e AllToolsProcessedEvent) EventType() string
type ApprovalDecision ¶ added in v0.234.0
ApprovalDecision is a pre-fetched approval verdict for one tool call. Reason is non-empty only for judge rejections, matching RequestToolApproval.
type ApprovalFailedEvent ¶
type ApprovalFailedEvent struct {
Error error
}
ApprovalFailedEvent is triggered when approval fails
func (ApprovalFailedEvent) EventType ¶
func (e ApprovalFailedEvent) EventType() string
type ApprovingToolsState ¶
type ApprovingToolsState struct {
// contains filtered or unexported fields
}
ApprovingToolsState approves tool calls one at a time, then runs them overlapping. MessageReceivedEvent starts the round, AllToolsProcessedEvent moves to PostToolExecution and ApprovalFailedEvent handles failures. Calls the policy does not gate are cleared without a prompt, like BlockingToolsState.
func (*ApprovingToolsState) Handle ¶
func (s *ApprovingToolsState) Handle(event AgentEvent) error
Handle processes events in ApprovingTools state
func (*ApprovingToolsState) Name ¶
func (s *ApprovingToolsState) Name() AgentExecutionState
Name returns the state this handler manages
type BlockingToolsState ¶
type BlockingToolsState struct {
// contains filtered or unexported fields
}
BlockingToolsState rejects gated tool calls with an actionable reason when their approval cannot be delivered, such as approval_behaviour=block or ipc without a broker. Ungated calls in the same batch still run in order. MessageReceivedEvent processes the batch and AllToolsProcessedEvent moves to PostToolExecution.
func (*BlockingToolsState) Handle ¶
func (s *BlockingToolsState) Handle(event AgentEvent) error
Handle processes events in BlockingTools state
func (*BlockingToolsState) Name ¶
func (s *BlockingToolsState) Name() AgentExecutionState
Name returns the state this handler manages
type CallOrder ¶
type CallOrder struct {
Tools agentdomain.ToolManifestLookup
// contains filtered or unexported fields
}
CallOrder keeps a tool batch's side effects in the order the model issued them while running read-only calls, as Tools reports them, concurrently. Next must be called in batch order from one goroutine at a time. The zero value runs every call in order.
type CancelledState ¶
type CancelledState struct {
// contains filtered or unexported fields
}
CancelledState handles events in the Cancelled state.
The Cancelled state is a terminal state reached when the agent is cancelled by the user. The event loop will exit when this state is reached.
func (*CancelledState) Handle ¶
func (s *CancelledState) Handle(event AgentEvent) error
Handle processes events in Cancelled state This is a terminal state, so no events are expected
func (*CancelledState) Name ¶
func (s *CancelledState) Name() AgentExecutionState
Name returns the state this handler manages
type CheckingQueueState ¶
type CheckingQueueState struct {
// contains filtered or unexported fields
}
CheckingQueueState picks the next step: answer pending tool results, drain queued messages, complete, or keep looping. Chat turns never wait here for background work, since the UI starts fresh turns. Headless runs wait at the completion boundary so they never exit with orphaned background tasks.
func (*CheckingQueueState) Handle ¶
func (s *CheckingQueueState) Handle(event AgentEvent) error
Handle processes events in CheckingQueue state
func (*CheckingQueueState) Name ¶
func (s *CheckingQueueState) Name() AgentExecutionState
Name returns the state this handler manages
type CompletingState ¶
type CompletingState struct {
// contains filtered or unexported fields
}
CompletingState finalizes a run with one last short queue check. Queued messages restart the agent at CheckingQueue, otherwise it publishes the completion event and returns to Idle.
func (*CompletingState) Handle ¶
func (s *CompletingState) Handle(event AgentEvent) error
Handle processes events in Completing state. Completion is driven solely by CompletionRequestedEvent (emitted right after the transition into Completing); any other event is ignored so a stray wake-up cannot finalize the agent.
func (*CompletingState) Name ¶
func (s *CompletingState) Name() AgentExecutionState
Name returns the state this handler manages
type CompletionRequestedEvent ¶
type CompletionRequestedEvent struct{}
CompletionRequestedEvent is triggered when the agent should complete
func (CompletionRequestedEvent) EventType ¶
func (e CompletionRequestedEvent) EventType() string
type ErrorState ¶
type ErrorState struct {
// contains filtered or unexported fields
}
ErrorState handles events in the Error state.
The Error state is a terminal state reached when unrecoverable errors occur. The event loop will exit when this state is reached.
func (*ErrorState) Handle ¶
func (s *ErrorState) Handle(event AgentEvent) error
Handle processes events in Error state This is a terminal state, so no events are expected
func (*ErrorState) Name ¶
func (s *ErrorState) Name() AgentExecutionState
Name returns the state this handler manages
type EvaluatingToolsState ¶
type EvaluatingToolsState struct {
// contains filtered or unexported fields
}
EvaluatingToolsState publishes the chat complete event with its tool calls, then moves to ApprovingTools when any call needs approval and to ExecutingTools otherwise.
func (*EvaluatingToolsState) Handle ¶
func (s *EvaluatingToolsState) Handle(event AgentEvent) error
Handle processes events in EvaluatingTools state
func (*EvaluatingToolsState) Name ¶
func (s *EvaluatingToolsState) Name() AgentExecutionState
Name returns the state this handler manages
type ExecutingToolsState ¶
type ExecutingToolsState struct {
// contains filtered or unexported fields
}
ExecutingToolsState handles events in the ExecutingTools state.
This state processes tool execution completion:
- ToolsCompletedEvent → transitions to PostToolExecution
func (*ExecutingToolsState) Handle ¶
func (s *ExecutingToolsState) Handle(event AgentEvent) error
Handle processes events in ExecutingTools state
func (*ExecutingToolsState) Name ¶
func (s *ExecutingToolsState) Name() AgentExecutionState
Name returns the state this handler manages
type IdleState ¶
type IdleState struct {
// contains filtered or unexported fields
}
IdleState handles events in the Idle state.
The Idle state is the initial and final resting state of the agent. When a MessageReceivedEvent arrives, it transitions to CheckingQueue to begin processing.
func (*IdleState) Handle ¶
func (s *IdleState) Handle(event AgentEvent) error
Handle processes events in Idle state
func (*IdleState) Name ¶
func (s *IdleState) Name() AgentExecutionState
Name returns the state this handler manages
type InputRequiredState ¶ added in v0.226.0
type InputRequiredState struct {
// contains filtered or unexported fields
}
InputRequiredState handles events in the InputRequired state.
The run is blocked on the user and resumes into the state it left once the prompt is answered. No loop event is emitted while a prompt is open.
func (*InputRequiredState) Handle ¶ added in v0.226.0
func (s *InputRequiredState) Handle(event AgentEvent) error
Handle processes events in InputRequired state
func (*InputRequiredState) Name ¶ added in v0.226.0
func (s *InputRequiredState) Name() AgentExecutionState
Name returns the state this handler manages
type MessageReceivedEvent ¶
type MessageReceivedEvent struct{}
MessageReceivedEvent is triggered when a new message arrives
func (MessageReceivedEvent) EventType ¶
func (e MessageReceivedEvent) EventType() string
type PostStreamState ¶
type PostStreamState struct {
// contains filtered or unexported fields
}
PostStreamState stores the assistant message, then moves to EvaluatingTools when it has tool calls, to Completing when it can complete, and to CheckingQueue otherwise. Messages queued during the stream drain after tools.
func (*PostStreamState) Handle ¶
func (s *PostStreamState) Handle(event AgentEvent) error
Handle processes events in PostStream state
func (*PostStreamState) Name ¶
func (s *PostStreamState) Name() AgentExecutionState
Name returns the state this handler manages
type PostToolExecutionState ¶
type PostToolExecutionState struct {
// contains filtered or unexported fields
}
PostToolExecutionState hands the images the tools returned to the model, then presents a requested plan and stops, drains messages queued during execution, completes, or moves to CheckingQueue for the next turn.
func (*PostToolExecutionState) Handle ¶
func (s *PostToolExecutionState) Handle(event AgentEvent) error
Handle processes events in PostToolExecution state
func (*PostToolExecutionState) Name ¶
func (s *PostToolExecutionState) Name() AgentExecutionState
Name returns the state this handler manages
type StartStreamingEvent ¶
type StartStreamingEvent struct{}
StartStreamingEvent is triggered when the agent should start streaming
func (StartStreamingEvent) EventType ¶
func (e StartStreamingEvent) EventType() string
type StateAction ¶
type StateAction func(ctx *AgentContext) error
StateAction is a function executed on state transitions
type StateContext ¶
type StateContext struct {
StateMachine AgentStateMachine
AgentCtx *AgentContext
Events chan AgentEvent
WaitGroup *sync.WaitGroup
Mutex *sync.Mutex
CurrentMessage *sdk.Message
CurrentToolCalls *[]*sdk.ChatCompletionMessageToolCall
CurrentReasoning *string
Tools agentdomain.ToolManifestLookup
ToolsNeedingApproval *[]sdk.ChatCompletionMessageToolCall
CurrentToolIndex *int
ToolResults *[]convdomain.ConversationEntry
Request *agentdomain.AgentRequest
MaxConcurrentTools int
ToolExecutor *func()
StartStreaming func()
GetMetrics func(requestID string) *agentdomain.ChatMetrics
ShouldRequireApproval func(toolCall *sdk.ChatCompletionMessageToolCall, isChatMode bool) bool
ApprovalDelivery func(toolCall *sdk.ChatCompletionMessageToolCall) string
BlockedToolHint func(toolCall *sdk.ChatCompletionMessageToolCall) string
AddMessage func(entry convdomain.ConversationEntry) error
BatchDrainQueue func() int
RequestToolApproval func(toolCall sdk.ChatCompletionMessageToolCall) (bool, string, error)
RequestJudgeApprovals func(calls []sdk.ChatCompletionMessageToolCall) map[string]ApprovalDecision
ExecuteToolInternal func(toolCall sdk.ChatCompletionMessageToolCall, isApproved bool) convdomain.ConversationEntry
ForgetPendingToolCall func(toolCallID string)
GetAgentMode func() agentdomain.AgentMode
PublishChatEvent func(event agentdomain.ChatEvent)
PublishChatComplete func(reasoning string, toolCalls []sdk.ChatCompletionMessageToolCall, metrics *agentdomain.ChatMetrics)
PublishChatCancelled func(metrics *agentdomain.ChatMetrics)
PublishToolResults func(results []convdomain.ConversationEntry)
AttachToolImages func(results []convdomain.ConversationEntry)
PresentPlan func(results []convdomain.ConversationEntry) bool
DispatchHooks func(hook agentdomain.HookPoint)
WaitForBackgroundTasks func()
}
StateContext gives state handlers the agent's shared state and callbacks. MaxConcurrentTools bounds approved tools running while later ones await approval. RequestJudgeApprovals decides a round's judge calls in one request, AttachToolImages hands tool images to the model, PresentPlan reports a presented plan, and WaitForBackgroundTasks runs only for non-chat runs.
type StateGuard ¶
type StateGuard func(ctx *AgentContext) bool
StateGuard is a function that determines if a state transition should occur
type StateHandler ¶
type StateHandler interface {
Handle(event AgentEvent) error
Name() AgentExecutionState
}
StateHandler defines the interface for handling events in a specific state
func NewApprovingToolsState ¶
func NewApprovingToolsState(ctx *StateContext) StateHandler
NewApprovingToolsState creates a new ApprovingTools state handler
func NewBlockingToolsState ¶
func NewBlockingToolsState(ctx *StateContext) StateHandler
NewBlockingToolsState creates a new BlockingTools state handler
func NewCancelledState ¶
func NewCancelledState(ctx *StateContext) StateHandler
NewCancelledState creates a new Cancelled state handler
func NewCheckingQueueState ¶
func NewCheckingQueueState(ctx *StateContext) StateHandler
NewCheckingQueueState creates a new CheckingQueue state handler
func NewCompletingState ¶
func NewCompletingState(ctx *StateContext) StateHandler
NewCompletingState creates a new Completing state handler
func NewErrorState ¶
func NewErrorState(ctx *StateContext) StateHandler
NewErrorState creates a new Error state handler
func NewEvaluatingToolsState ¶
func NewEvaluatingToolsState(ctx *StateContext) StateHandler
NewEvaluatingToolsState creates a new EvaluatingTools state handler
func NewExecutingToolsState ¶
func NewExecutingToolsState(ctx *StateContext) StateHandler
NewExecutingToolsState creates a new ExecutingTools state handler
func NewIdleState ¶
func NewIdleState(ctx *StateContext) StateHandler
NewIdleState creates a new Idle state handler
func NewInputRequiredState ¶ added in v0.226.0
func NewInputRequiredState(ctx *StateContext) StateHandler
NewInputRequiredState creates a new InputRequired state handler
func NewPostStreamState ¶
func NewPostStreamState(ctx *StateContext) StateHandler
NewPostStreamState creates a new PostStream state handler
func NewPostToolExecutionState ¶
func NewPostToolExecutionState(ctx *StateContext) StateHandler
NewPostToolExecutionState creates a new PostToolExecution state handler
func NewStoppedState ¶
func NewStoppedState(ctx *StateContext) StateHandler
NewStoppedState creates a new Stopped state handler
func NewStreamingLLMState ¶
func NewStreamingLLMState(ctx *StateContext) StateHandler
NewStreamingLLMState creates a new StreamingLLM state handler
type StoppedState ¶
type StoppedState struct {
// contains filtered or unexported fields
}
StoppedState handles events in the Stopped state.
The Stopped state is a terminal state reached when the agent stops execution (e.g., due to tool rejection or other stop conditions). The event loop will exit when this state is reached.
func (*StoppedState) Handle ¶
func (s *StoppedState) Handle(event AgentEvent) error
Handle processes events in Stopped state This is a terminal state, so no events are expected
func (*StoppedState) Name ¶
func (s *StoppedState) Name() AgentExecutionState
Name returns the state this handler manages
type StreamCompletedEvent ¶
type StreamCompletedEvent struct {
Message sdk.Message
ToolCalls []*sdk.ChatCompletionMessageToolCall
Reasoning string
}
StreamCompletedEvent is triggered when LLM streaming completes
func (StreamCompletedEvent) EventType ¶
func (e StreamCompletedEvent) EventType() string
type StreamingLLMState ¶
type StreamingLLMState struct {
// contains filtered or unexported fields
}
StreamingLLMState handles events in the StreamingLLM state.
This state manages LLM streaming:
- StartStreamingEvent → launches background streaming goroutine
- StreamCompletedEvent → processes completed stream, stores data, transitions to PostStream
func (*StreamingLLMState) Handle ¶
func (s *StreamingLLMState) Handle(event AgentEvent) error
Handle processes events in StreamingLLM state
func (*StreamingLLMState) Name ¶
func (s *StreamingLLMState) Name() AgentExecutionState
Name returns the state this handler manages
type ToolsCompletedEvent ¶
type ToolsCompletedEvent struct{}
ToolsCompletedEvent is triggered when all tools finish executing.
func (ToolsCompletedEvent) EventType ¶
func (e ToolsCompletedEvent) EventType() string