Documentation
¶
Overview ¶
Package adk provides core agent development kit utilities and types.
Index ¶
- Constants
- Variables
- func AddSessionValue(ctx context.Context, key string, value any)
- func AddSessionValues(ctx context.Context, kvs map[string]any)
- func AppendAddressSegment(ctx context.Context, segType AddressSegmentType, segID string) context.Context
- func ClearRunCtx(ctx context.Context) context.Context
- func DeleteRunLocalValue(ctx context.Context, key string) error
- func GenTransferMessages(_ context.Context, destAgentName string) (Message, Message)
- func GetImplSpecificOptions[T any](base *T, opts ...AgentRunOption) *T
- func GetMessage(e *AgentEvent) (Message, *AgentEvent, error)
- func GetRunLocalValue(ctx context.Context, key string) (any, bool, error)
- func GetSessionValue(ctx context.Context, key string) (any, bool)
- func GetSessionValues(ctx context.Context) map[string]any
- func NewAgentTool(_ context.Context, agent Agent, options ...AgentToolOption) tool.BaseTool
- func NewAsyncIteratorPair[T any]() (*AsyncIterator[T], *AsyncGenerator[T])
- func SendEvent(ctx context.Context, event *AgentEvent) error
- func SendToolGenAction(ctx context.Context, toolName string, action *AgentAction) error
- func SetLanguage(lang Language) error
- func SetRunLocalValue(ctx context.Context, key string, value any) error
- func WithCancel() (AgentRunOption, AgentCancelFunc)
- type Address
- type AddressSegment
- type AddressSegmentType
- type Agent
- type AgentAction
- type AgentCallbackInput
- type AgentCallbackOutput
- type AgentCancelFunc
- type AgentCancelInfo
- type AgentCancelOption
- type AgentEvent
- func CompositeInterrupt(ctx context.Context, info any, state any, ...) *AgentEvent
- func EventFromMessage(msg Message, msgStream MessageStream, role schema.RoleType, toolName string) *AgentEvent
- func Interrupt(ctx context.Context, info any) *AgentEvent
- func StatefulInterrupt(ctx context.Context, info any, state any) *AgentEvent
- type AgentInput
- type AgentMiddleware
- type AgentOption
- type AgentOutput
- type AgentRunOption
- func WithAgentToolRunOptions(opts map[string][]AgentRunOption) AgentRunOption
- func WithCallbacks(handlers ...callbacks.Handler) AgentRunOption
- func WithChatModelOptions(opts []model.Option) AgentRunOption
- func WithCheckPointID(id string) AgentRunOption
- func WithHistoryModifier(f func(context.Context, []Message) []Message) AgentRunOption
- func WithSessionValues(v map[string]any) AgentRunOption
- func WithSkipTransferMessages() AgentRunOption
- func WithToolOptions(opts []tool.Option) AgentRunOption
- func WrapImplSpecificOptFn[T any](optFn func(*T)) AgentRunOption
- type AgentToolOption
- type AgentToolOptions
- type AsyncGenerator
- type AsyncIterator
- type BaseChatModelAgentMiddleware
- func (b *BaseChatModelAgentMiddleware) AfterModelRewriteState(ctx context.Context, state *ChatModelAgentState, mc *ModelContext) (context.Context, *ChatModelAgentState, error)
- func (b *BaseChatModelAgentMiddleware) AfterToolCallsRewriteState(ctx context.Context, state *ChatModelAgentState, tc *ToolCallsContext) (context.Context, *ChatModelAgentState, error)
- func (b *BaseChatModelAgentMiddleware) BeforeAgent(ctx context.Context, runCtx *ChatModelAgentContext) (context.Context, *ChatModelAgentContext, error)
- func (b *BaseChatModelAgentMiddleware) BeforeModelRewriteState(ctx context.Context, state *ChatModelAgentState, mc *ModelContext) (context.Context, *ChatModelAgentState, error)
- func (b *BaseChatModelAgentMiddleware) WrapEnhancedInvokableToolCall(_ context.Context, endpoint EnhancedInvokableToolCallEndpoint, _ *ToolContext) (EnhancedInvokableToolCallEndpoint, error)
- func (b *BaseChatModelAgentMiddleware) WrapEnhancedStreamableToolCall(_ context.Context, endpoint EnhancedStreamableToolCallEndpoint, _ *ToolContext) (EnhancedStreamableToolCallEndpoint, error)
- func (b *BaseChatModelAgentMiddleware) WrapInvokableToolCall(_ context.Context, endpoint InvokableToolCallEndpoint, _ *ToolContext) (InvokableToolCallEndpoint, error)
- func (b *BaseChatModelAgentMiddleware) WrapModel(_ context.Context, m model.BaseChatModel, _ *ModelContext) (model.BaseChatModel, error)
- func (b *BaseChatModelAgentMiddleware) WrapStreamableToolCall(_ context.Context, endpoint StreamableToolCallEndpoint, _ *ToolContext) (StreamableToolCallEndpoint, error)
- type BreakLoopAction
- type CancelError
- type CancelHandle
- type CancelMode
- type ChatModelAgent
- func (a *ChatModelAgent) Description(_ context.Context) string
- func (a *ChatModelAgent) GetType() string
- func (a *ChatModelAgent) Name(_ context.Context) string
- func (a *ChatModelAgent) OnDisallowTransferToParent(_ context.Context) error
- func (a *ChatModelAgent) OnSetAsSubAgent(_ context.Context, parent Agent) error
- func (a *ChatModelAgent) OnSetSubAgents(_ context.Context, subAgents []Agent) error
- func (a *ChatModelAgent) Resume(ctx context.Context, info *ResumeInfo, opts ...AgentRunOption) *AsyncIterator[*AgentEvent]
- func (a *ChatModelAgent) Run(ctx context.Context, input *AgentInput, opts ...AgentRunOption) *AsyncIterator[*AgentEvent]
- type ChatModelAgentConfig
- type ChatModelAgentContext
- type ChatModelAgentInterruptInfo
- type ChatModelAgentMiddleware
- type ChatModelAgentResumeData
- type ChatModelAgentState
- type CheckPointDeleter
- type CheckPointStore
- type DeterministicTransferConfig
- type EnhancedInvokableToolCallEndpoint
- type EnhancedStreamableToolCallEndpoint
- type ExitTool
- type GenInputResult
- type GenModelInput
- type GenResumeResult
- type HistoryEntry
- type HistoryRewriter
- type InterruptCtx
- type InterruptInfo
- type InterruptSignal
- type InvokableToolCallEndpoint
- type Language
- type LoopAgentConfig
- type Message
- type MessageStream
- type MessageVariant
- type ModelContext
- type ModelRetryConfig
- type OnSubAgents
- type ParallelAgentConfig
- type PushOption
- type ResumableAgent
- func NewLoopAgent(ctx context.Context, config *LoopAgentConfig) (ResumableAgent, error)
- func NewParallelAgent(ctx context.Context, config *ParallelAgentConfig) (ResumableAgent, error)
- func NewSequentialAgent(ctx context.Context, config *SequentialAgentConfig) (ResumableAgent, error)
- func SetSubAgents(ctx context.Context, agent Agent, subAgents []Agent) (ResumableAgent, error)
- type ResumeInfo
- type ResumeParams
- type RetryExhaustedError
- type RunStep
- type Runner
- func (r *Runner) Query(ctx context.Context, query string, opts ...AgentRunOption) *AsyncIterator[*AgentEvent]
- func (r *Runner) Resume(ctx context.Context, checkPointID string, opts ...AgentRunOption) (*AsyncIterator[*AgentEvent], error)
- func (r *Runner) ResumeWithParams(ctx context.Context, checkPointID string, params *ResumeParams, ...) (*AsyncIterator[*AgentEvent], error)
- func (r *Runner) Run(ctx context.Context, messages []Message, opts ...AgentRunOption) *AsyncIterator[*AgentEvent]
- type RunnerConfig
- type SequentialAgentConfig
- type Statedeprecated
- type StopOption
- type StreamCanceledError
- type StreamableToolCallEndpoint
- type ToolCallsContext
- type ToolContext
- type ToolsConfig
- type TransferToAgentAction
- type TurnContext
- type TurnLoop
- type TurnLoopConfig
- type TurnLoopExitState
- type WillRetryError
- type WorkflowInterruptInfo
Constants ¶
const ( TransferToAgentToolName = "transfer_to_agent" TransferToAgentToolDesc = "Transfer the question to another agent." TransferToAgentToolDescChinese = "将问题移交给其他 Agent。" )
const ( TransferToAgentInstruction = `` /* 268-byte string literal not displayed */ TransferToAgentInstructionChinese = `` /* 303-byte string literal not displayed */ )
const ComponentOfAgent components.Component = "Agent"
ComponentOfAgent is the component type identifier for ADK agents in callbacks. Use this to filter callback events to only agent-related events.
Variables ¶
var ( // ErrCancelTimeout is returned by CancelHandle.Wait when the cancel operation timed out. ErrCancelTimeout = errors.New("cancel timed out") // ErrExecutionCompleted is returned by CancelHandle.Wait when the agent finished // before the cancel took effect. "Finished" means the event stream was fully // drained without any interrupt — normal completion or a fatal error. // // Note: business interrupts that occur while cancel is active are absorbed // into CancelError (see CancelError doc), so they result in nil (cancel // succeeded), NOT ErrExecutionCompleted. Only execution that completes with // no interrupt at all produces this error. ErrExecutionCompleted = errors.New("execution already completed") // ErrStreamCanceled is the error sent through the stream when CancelImmediate aborts it. // It is a *StreamCanceledError so it can be gob-serialized during checkpoint save // (when stored as agentEventWrapper.StreamErr). ErrStreamCanceled error = &StreamCanceledError{} )
Sentinel errors for cancel outcomes.
var ErrCheckpointStoreNil = errors.New("checkpoint store is nil")
ErrCheckpointStoreNil is returned when a checkpoint operation requires a Store but none was configured in TurnLoopConfig.
var ErrExceedMaxIterations = errors.New("exceeds max iterations")
ErrExceedMaxIterations indicates the agent reached the maximum iterations limit.
var ( // ErrExceedMaxRetries is returned when the maximum number of retries has been exceeded. // Use errors.Is to check if an error is due to max retries being exceeded: // // if errors.Is(err, adk.ErrExceedMaxRetries) { // // handle max retries exceeded // } // // Use errors.As to extract the underlying RetryExhaustedError for the last error details: // // var retryErr *adk.RetryExhaustedError // if errors.As(err, &retryErr) { // fmt.Printf("last error was: %v\n", retryErr.LastErr) // } ErrExceedMaxRetries = errors.New("exceeds max retries") )
var ( ToolInfoExit = &schema.ToolInfo{ Name: "exit", Desc: "Exit the agent process and return the final result.", ParamsOneOf: schema.NewParamsOneOfByParams(map[string]*schema.ParameterInfo{ "final_result": { Desc: "the final result to return", Required: true, Type: schema.String, }, }), } )
Functions ¶
func AddSessionValue ¶
AddSessionValue sets a single session key-value pair for the current run.
func AddSessionValues ¶
AddSessionValues sets multiple session key-value pairs for the current run.
func AppendAddressSegment ¶ added in v0.7.0
func AppendAddressSegment(ctx context.Context, segType AddressSegmentType, segID string) context.Context
AppendAddressSegment adds an address segment for the current execution context.
func ClearRunCtx ¶
ClearRunCtx clears the run context of the multi-agents. This is particularly useful when a customized agent with a multi-agents inside it is set as a subagent of another multi-agents. In such cases, it's not expected to pass the outside run context to the inside multi-agents, so this function helps isolate the contexts properly.
func DeleteRunLocalValue ¶ added in v0.8.0
DeleteRunLocalValue removes a value that was set during the current agent Run() invocation.
This function can only be called from within a ChatModelAgentMiddleware during agent execution. Returns an error if called outside of an agent execution context.
func GenTransferMessages ¶
GenTransferMessages generates assistant and tool messages to instruct a transfer-to-agent tool call targeting the destination agent.
func GetImplSpecificOptions ¶
func GetImplSpecificOptions[T any](base *T, opts ...AgentRunOption) *T
GetImplSpecificOptions extract the implementation specific options from AgentRunOption list, optionally providing a base options with default values. e.g.
myOption := &MyOption{
Field1: "default_value",
}
myOption := model.GetImplSpecificOptions(myOption, opts...)
func GetMessage ¶
func GetMessage(e *AgentEvent) (Message, *AgentEvent, error)
GetMessage extracts the Message from an AgentEvent. For streaming output, it duplicates the stream and concatenates it into a single Message.
func GetRunLocalValue ¶ added in v0.8.0
GetRunLocalValue retrieves a value that was set during the current agent Run() invocation. The value is scoped to this specific execution and is not shared across different Run() calls or agent instances.
Values stored via SetRunLocalValue are compatible with interrupt/resume cycles - they will be serialized and restored when the agent is resumed. For custom types, you must register them using schema.RegisterName[T]() in an init() function to ensure proper serialization.
This function can only be called from within a ChatModelAgentMiddleware during agent execution. Returns the value and true if found, or nil and false if not found or if called outside of an agent execution context.
func GetSessionValue ¶
GetSessionValue retrieves a session value by key and reports whether it exists.
func GetSessionValues ¶
GetSessionValues returns all session key-value pairs for the current run.
func NewAgentTool ¶
NewAgentTool creates a tool that wraps an agent for invocation.
Event Streaming: When EmitInternalEvents is enabled in ToolsConfig, the agent tool will emit AgentEvent from the inner agent to the parent agent's AsyncGenerator, allowing real-time streaming of the inner agent's output to the end-user via Runner.
Note that these forwarded events are NOT recorded in the parent agent's runSession. They are only emitted to the end-user and have no effect on the parent agent's state or checkpoint. The only exception is Interrupted action, which is propagated via CompositeInterrupt to enable proper interrupt/resume across agent boundaries.
Action Scoping: Actions emitted by the inner agent are scoped to the agent tool boundary:
- Interrupted: Propagated via CompositeInterrupt to allow proper interrupt/resume across boundaries
- Exit, TransferToAgent, BreakLoop: Ignored outside the agent tool; these actions only affect the inner agent's execution and do not propagate to the parent agent
This scoping ensures that nested agents cannot unexpectedly terminate or transfer control of their parent agent's execution flow.
func NewAsyncIteratorPair ¶
func NewAsyncIteratorPair[T any]() (*AsyncIterator[T], *AsyncGenerator[T])
NewAsyncIteratorPair returns a paired async iterator and generator that share the same underlying channel.
func SendEvent ¶ added in v0.7.34
func SendEvent(ctx context.Context, event *AgentEvent) error
SendEvent sends a custom AgentEvent to the event stream during agent execution. This allows ChatModelAgentMiddleware implementations to emit custom events that will be received by the caller iterating over the agent's event stream.
This function can only be called from within a ChatModelAgentMiddleware during agent execution. Returns an error if called outside of an agent execution context.
func SendToolGenAction ¶
func SendToolGenAction(ctx context.Context, toolName string, action *AgentAction) error
SendToolGenAction attaches an AgentAction to the next tool event emitted for the current tool execution.
Where/when to use:
- Invoke within a tool's Run (Invokable/Streamable) implementation to include an action alongside that tool's output event.
- The action is scoped by the current tool call context: if a ToolCallID is available, it is used as the key to support concurrent calls of the same tool with different parameters; otherwise, the provided toolName is used.
- The stored action is ephemeral and will be popped and attached to the tool event when the tool finishes (including streaming completion).
Limitation:
- This function is intended for use within ChatModelAgent runs only. It relies on ChatModelAgent's internal State to store and pop actions, which is not available in other agent types.
func SetLanguage ¶ added in v0.8.0
SetLanguage sets the language for the ADK built-in prompts. The default language is English if not explicitly set.
func SetRunLocalValue ¶ added in v0.8.0
SetRunLocalValue sets a key-value pair that persists for the duration of the current agent Run() invocation. The value is scoped to this specific execution and is not shared across different Run() calls or agent instances.
Values stored here are compatible with interrupt/resume cycles - they will be serialized and restored when the agent is resumed. For custom types, you must register them using schema.RegisterName[T]() in an init() function to ensure proper serialization.
This function can only be called from within a ChatModelAgentMiddleware during agent execution. Returns an error if called outside of an agent execution context.
func WithCancel ¶
func WithCancel() (AgentRunOption, AgentCancelFunc)
WithCancel creates an AgentRunOption that enables cancellation for an agent run. It returns the option to pass to Run/Resume and a cancel function. Cancel options (mode, timeout) are passed to the returned AgentCancelFunc at call time.
Types ¶
type Address ¶ added in v0.7.0
Address represents the unique, hierarchical address of a component within an execution. It is a slice of AddressSegments, where each segment represents one level of nesting. This is a type alias for core.Address. See the core package for more details.
type AddressSegment ¶ added in v0.7.0
type AddressSegment = core.AddressSegment
type AddressSegmentType ¶ added in v0.7.0
type AddressSegmentType = core.AddressSegmentType
const ( AddressSegmentAgent AddressSegmentType = "agent" AddressSegmentTool AddressSegmentType = "tool" )
type Agent ¶
type Agent interface {
Name(ctx context.Context) string
Description(ctx context.Context) string
// Run runs the agent.
// The returned AgentEvent within the AsyncIterator must be safe to modify.
// If the returned AgentEvent within the AsyncIterator contains MessageStream,
// the MessageStream MUST be exclusive and safe to be received directly.
// NOTE: it's recommended to use SetAutomaticClose() on the MessageStream of AgentEvents emitted by AsyncIterator,
// so that even the events are not processed, the MessageStream can still be closed.
Run(ctx context.Context, input *AgentInput, options ...AgentRunOption) *AsyncIterator[*AgentEvent]
}
func AgentWithDeterministicTransferTo ¶
func AgentWithDeterministicTransferTo(_ context.Context, config *DeterministicTransferConfig) Agent
AgentWithDeterministicTransferTo wraps an agent to transfer to given agents deterministically.
func AgentWithOptions ¶
func AgentWithOptions(ctx context.Context, agent Agent, opts ...AgentOption) Agent
AgentWithOptions wraps an agent with flow-specific options and returns it.
type AgentAction ¶
type AgentAction struct {
Exit bool
Interrupted *InterruptInfo
TransferToAgent *TransferToAgentAction
BreakLoop *BreakLoopAction
CustomizedAction any
// contains filtered or unexported fields
}
AgentAction represents actions that an agent can emit during execution.
Action Scoping in Agent Tools: When an agent is wrapped as an agent tool (via NewAgentTool), actions emitted by the inner agent are scoped to the tool boundary:
- Interrupted: Propagated via CompositeInterrupt to allow proper interrupt/resume across boundaries
- Exit, TransferToAgent, BreakLoop: Ignored outside the agent tool; these actions only affect the inner agent's execution and do not propagate to the parent agent
This scoping ensures that nested agents cannot unexpectedly terminate or transfer control of their parent agent's execution flow.
func NewBreakLoopAction ¶ added in v0.5.8
func NewBreakLoopAction(agentName string) *AgentAction
NewBreakLoopAction creates a new BreakLoopAction, signaling a request to terminate the current loop.
func NewExitAction ¶
func NewExitAction() *AgentAction
NewExitAction creates an action that signals the agent to exit.
func NewTransferToAgentAction ¶
func NewTransferToAgentAction(destAgentName string) *AgentAction
NewTransferToAgentAction creates an action to transfer to the specified agent.
type AgentCallbackInput ¶ added in v0.8.0
type AgentCallbackInput struct {
// Input contains the agent input for a new run. Nil when resuming.
Input *AgentInput
// ResumeInfo contains resume information when resuming from an interrupt. Nil for new runs.
ResumeInfo *ResumeInfo
}
AgentCallbackInput represents the input passed to agent callbacks during OnStart. Use ConvAgentCallbackInput to safely convert from callbacks.CallbackInput.
func ConvAgentCallbackInput ¶ added in v0.8.0
func ConvAgentCallbackInput(input callbacks.CallbackInput) *AgentCallbackInput
ConvAgentCallbackInput converts a generic CallbackInput to AgentCallbackInput. Returns nil if the input is not an AgentCallbackInput.
type AgentCallbackOutput ¶ added in v0.8.0
type AgentCallbackOutput struct {
// Events provides a stream of agent events. Each handler receives its own copy.
Events *AsyncIterator[*AgentEvent]
}
AgentCallbackOutput represents the output passed to agent callbacks during OnEnd. Use ConvAgentCallbackOutput to safely convert from callbacks.CallbackOutput.
Important: The Events iterator should be consumed asynchronously to avoid blocking the agent execution. Each callback handler receives an independent copy of the iterator.
func ConvAgentCallbackOutput ¶ added in v0.8.0
func ConvAgentCallbackOutput(output callbacks.CallbackOutput) *AgentCallbackOutput
ConvAgentCallbackOutput converts a generic CallbackOutput to AgentCallbackOutput. Returns nil if the output is not an AgentCallbackOutput.
type AgentCancelFunc ¶
type AgentCancelFunc func(...AgentCancelOption) (*CancelHandle, bool)
AgentCancelFunc is called to request cancellation of a running agent. It returns after the cancel request is committed; use the returned handle's Wait to block for completion and outcome.
The returned bool reports whether this call contributed to the CancelError for the current execution. "Contributed" means this call's cancel options were included before cancellation was finalized. It is false when cancellation was already finalized (handled or execution completed).
type AgentCancelInfo ¶
type AgentCancelInfo struct {
Mode CancelMode
Escalated bool
Timeout bool
}
AgentCancelInfo contains information about a cancel operation.
type AgentCancelOption ¶
type AgentCancelOption func(*agentCancelConfig)
AgentCancelOption configures cancel behavior.
func WithAgentCancelMode ¶
func WithAgentCancelMode(mode CancelMode) AgentCancelOption
WithAgentCancelMode sets the cancel mode for the agent cancel operation.
func WithAgentCancelTimeout ¶
func WithAgentCancelTimeout(timeout time.Duration) AgentCancelOption
WithAgentCancelTimeout sets a timeout for the cancel operation. This only applies to safe-point modes (CancelAfterChatModel, CancelAfterToolCalls): if the safe-point hasn't fired within this duration, the cancel escalates to an immediate graph interrupt. For CancelImmediate this timeout is ignored — the graph interrupt fires immediately with timeout=0.
type AgentEvent ¶
type AgentEvent struct {
AgentName string
// RunPath represents the execution path from root agent to the current event source.
// This field is managed entirely by the eino framework and cannot be set by end-users
// because RunStep's fields are unexported. The framework sets RunPath exactly once:
// - flowAgent sets it when the event has no RunPath (len == 0)
// - agentTool prepends parent RunPath when forwarding events from nested agents
RunPath []RunStep
Output *AgentOutput
Action *AgentAction
Err error
}
AgentEvent CheckpointSchema: persisted via serialization.RunCtx (gob).
func CompositeInterrupt ¶ added in v0.7.0
func CompositeInterrupt(ctx context.Context, info any, state any, subInterruptSignals ...*InterruptSignal) *AgentEvent
CompositeInterrupt creates an interrupt action for a workflow agent. It combines the interrupts from one or more of its sub-agents into a single, cohesive interrupt. This is used by workflow agents (like Sequential, Parallel, or Loop) to propagate interrupts from their children. The `info` parameter is user-facing data describing the workflow's own reason for interrupting. The `state` parameter is the workflow agent's own state (e.g., the index of the sub-agent that was interrupted). The `subInterruptSignals` is a variadic list of the InterruptSignal objects from the interrupted sub-agents.
func EventFromMessage ¶
func EventFromMessage(msg Message, msgStream MessageStream, role schema.RoleType, toolName string) *AgentEvent
EventFromMessage wraps a message or stream into an AgentEvent with role metadata.
func Interrupt ¶ added in v0.7.0
func Interrupt(ctx context.Context, info any) *AgentEvent
Interrupt creates a basic interrupt action. This is used when an agent needs to pause its execution to request external input or intervention, but does not need to save any internal state to be restored upon resumption. The `info` parameter is user-facing data that describes the reason for the interrupt.
func StatefulInterrupt ¶ added in v0.7.0
func StatefulInterrupt(ctx context.Context, info any, state any) *AgentEvent
StatefulInterrupt creates an interrupt action that also saves the agent's internal state. This is used when an agent has internal state that must be restored for it to continue correctly. The `info` parameter is user-facing data describing the interrupt. The `state` parameter is the agent's internal state object, which will be serialized and stored.
type AgentInput ¶
type AgentMiddleware ¶ added in v0.5.14
type AgentMiddleware struct {
// AdditionalInstruction adds supplementary text to the agent's system instruction.
// This instruction is concatenated with the base instruction before each chat model call.
AdditionalInstruction string
// AdditionalTools adds supplementary tools to the agent's available toolset.
// These tools are combined with the tools configured for the agent.
AdditionalTools []tool.BaseTool
// BeforeChatModel is called before each ChatModel invocation, allowing modification of the agent state.
BeforeChatModel func(context.Context, *ChatModelAgentState) error
// AfterChatModel is called after each ChatModel invocation, allowing modification of the agent state.
AfterChatModel func(context.Context, *ChatModelAgentState) error
// WrapToolCall wraps tool calls with custom middleware logic.
// Each middleware contains Invokable and/or Streamable functions for tool calls.
WrapToolCall compose.ToolMiddleware
}
AgentMiddleware provides hooks to customize agent behavior at various stages of execution.
Limitations of AgentMiddleware (struct-based):
- Struct types are closed: users cannot add new methods
- Callbacks only return error, cannot return modified context
- Configuration is scattered across closures when using factory functions
For new code requiring extensibility, consider using ChatModelAgentMiddleware (interface-based) instead. AgentMiddleware is kept for backward compatibility and remains suitable for simple, static additions like extra instruction or tools.
See ChatModelAgentMiddleware documentation for detailed comparison.
type AgentOption ¶
type AgentOption func(options *flowAgent)
func WithDisallowTransferToParent ¶
func WithDisallowTransferToParent() AgentOption
WithDisallowTransferToParent prevents a sub-agent from transferring to its parent.
func WithHistoryRewriter ¶
func WithHistoryRewriter(h HistoryRewriter) AgentOption
WithHistoryRewriter sets a rewriter to transform conversation history.
type AgentOutput ¶
type AgentOutput struct {
MessageOutput *MessageVariant
CustomizedOutput any
}
type AgentRunOption ¶
type AgentRunOption struct {
// contains filtered or unexported fields
}
AgentRunOption is the call option for adk Agent.
func WithAgentToolRunOptions ¶
func WithAgentToolRunOptions(opts map[string][]AgentRunOption) AgentRunOption
WithAgentToolRunOptions specifies per-tool run options for the agent.
func WithCallbacks ¶ added in v0.8.0
func WithCallbacks(handlers ...callbacks.Handler) AgentRunOption
WithCallbacks adds callback handlers to receive agent lifecycle events. Handlers receive OnStart with AgentCallbackInput and OnEnd with AgentCallbackOutput. Multiple handlers can be added; each receives an independent copy of the event stream.
func WithChatModelOptions ¶
func WithChatModelOptions(opts []model.Option) AgentRunOption
WithChatModelOptions sets options for the underlying chat model.
func WithCheckPointID ¶
func WithCheckPointID(id string) AgentRunOption
WithCheckPointID sets the checkpoint ID used for interruption persistence.
func WithHistoryModifier ¶
func WithHistoryModifier(f func(context.Context, []Message) []Message) AgentRunOption
WithHistoryModifier sets a function to modify history during resume. Deprecated: use ResumeWithData and ChatModelAgentResumeData instead.
func WithSessionValues ¶
func WithSessionValues(v map[string]any) AgentRunOption
WithSessionValues sets session-scoped values for the agent run.
func WithSkipTransferMessages ¶
func WithSkipTransferMessages() AgentRunOption
WithSkipTransferMessages disables forwarding transfer messages during execution.
func WithToolOptions ¶
func WithToolOptions(opts []tool.Option) AgentRunOption
WithToolOptions sets options for tools used by the chat model agent.
func WrapImplSpecificOptFn ¶
func WrapImplSpecificOptFn[T any](optFn func(*T)) AgentRunOption
WrapImplSpecificOptFn is the option to wrap the implementation specific option function.
func (AgentRunOption) DesignateAgent ¶
func (o AgentRunOption) DesignateAgent(name ...string) AgentRunOption
type AgentToolOption ¶
type AgentToolOption func(*AgentToolOptions)
func WithAgentInputSchema ¶ added in v0.5.4
func WithAgentInputSchema(schema *schema.ParamsOneOf) AgentToolOption
WithAgentInputSchema sets a custom input schema for the agent tool.
func WithFullChatHistoryAsInput ¶
func WithFullChatHistoryAsInput() AgentToolOption
WithFullChatHistoryAsInput enables using the full chat history as input.
type AgentToolOptions ¶
type AgentToolOptions struct {
// contains filtered or unexported fields
}
type AsyncGenerator ¶
type AsyncGenerator[T any] struct { // contains filtered or unexported fields }
func (*AsyncGenerator[T]) Close ¶
func (ag *AsyncGenerator[T]) Close()
func (*AsyncGenerator[T]) Send ¶
func (ag *AsyncGenerator[T]) Send(v T)
type AsyncIterator ¶
type AsyncIterator[T any] struct { // contains filtered or unexported fields }
func (*AsyncIterator[T]) Next ¶
func (ai *AsyncIterator[T]) Next() (T, bool)
type BaseChatModelAgentMiddleware ¶ added in v0.8.0
type BaseChatModelAgentMiddleware struct{}
BaseChatModelAgentMiddleware provides default no-op implementations for ChatModelAgentMiddleware. Embed *BaseChatModelAgentMiddleware in custom handlers to only override the methods you need.
Example:
type MyHandler struct {
*adk.BaseChatModelAgentMiddleware
// custom fields
}
func (h *MyHandler) BeforeModelRewriteState(ctx context.Context, state *adk.ChatModelAgentState, mc *adk.ModelContext) (context.Context, *adk.ChatModelAgentState, error) {
// custom logic
return ctx, state, nil
}
func (*BaseChatModelAgentMiddleware) AfterModelRewriteState ¶ added in v0.8.0
func (b *BaseChatModelAgentMiddleware) AfterModelRewriteState(ctx context.Context, state *ChatModelAgentState, mc *ModelContext) (context.Context, *ChatModelAgentState, error)
func (*BaseChatModelAgentMiddleware) AfterToolCallsRewriteState ¶
func (b *BaseChatModelAgentMiddleware) AfterToolCallsRewriteState(ctx context.Context, state *ChatModelAgentState, tc *ToolCallsContext) (context.Context, *ChatModelAgentState, error)
func (*BaseChatModelAgentMiddleware) BeforeAgent ¶ added in v0.8.0
func (b *BaseChatModelAgentMiddleware) BeforeAgent(ctx context.Context, runCtx *ChatModelAgentContext) (context.Context, *ChatModelAgentContext, error)
func (*BaseChatModelAgentMiddleware) BeforeModelRewriteState ¶ added in v0.8.0
func (b *BaseChatModelAgentMiddleware) BeforeModelRewriteState(ctx context.Context, state *ChatModelAgentState, mc *ModelContext) (context.Context, *ChatModelAgentState, error)
func (*BaseChatModelAgentMiddleware) WrapEnhancedInvokableToolCall ¶ added in v0.8.0
func (b *BaseChatModelAgentMiddleware) WrapEnhancedInvokableToolCall(_ context.Context, endpoint EnhancedInvokableToolCallEndpoint, _ *ToolContext) (EnhancedInvokableToolCallEndpoint, error)
func (*BaseChatModelAgentMiddleware) WrapEnhancedStreamableToolCall ¶ added in v0.8.0
func (b *BaseChatModelAgentMiddleware) WrapEnhancedStreamableToolCall(_ context.Context, endpoint EnhancedStreamableToolCallEndpoint, _ *ToolContext) (EnhancedStreamableToolCallEndpoint, error)
func (*BaseChatModelAgentMiddleware) WrapInvokableToolCall ¶ added in v0.8.0
func (b *BaseChatModelAgentMiddleware) WrapInvokableToolCall(_ context.Context, endpoint InvokableToolCallEndpoint, _ *ToolContext) (InvokableToolCallEndpoint, error)
func (*BaseChatModelAgentMiddleware) WrapModel ¶ added in v0.8.0
func (b *BaseChatModelAgentMiddleware) WrapModel(_ context.Context, m model.BaseChatModel, _ *ModelContext) (model.BaseChatModel, error)
func (*BaseChatModelAgentMiddleware) WrapStreamableToolCall ¶ added in v0.8.0
func (b *BaseChatModelAgentMiddleware) WrapStreamableToolCall(_ context.Context, endpoint StreamableToolCallEndpoint, _ *ToolContext) (StreamableToolCallEndpoint, error)
type BreakLoopAction ¶ added in v0.5.8
type BreakLoopAction struct {
// From records the name of the agent that initiated the break loop action.
From string
// Done is a state flag that can be used by the framework to mark when the
// action has been handled.
Done bool
// CurrentIterations is populated by the framework to record at which
// iteration the loop was broken.
CurrentIterations int
}
BreakLoopAction is a programmatic-only agent action used to prematurely terminate the execution of a loop workflow agent. When a loop workflow agent receives this action from a sub-agent, it will stop its current iteration and will not proceed to the next one. It will mark the BreakLoopAction as Done, signalling to any 'upper level' loop agent that this action has been processed and should be ignored further up. This action is not intended to be used by LLMs.
type CancelError ¶
type CancelError struct {
Info *AgentCancelInfo
// CheckPointID is the checkpoint ID associated with this cancel operation.
// When non-empty, the cancelled agent's state has been persisted under this ID
// and can be resumed via Runner.Resume or GenInputResult.ResumeFromCheckpointID.
CheckPointID string
// InterruptContexts provides the interrupt contexts needed for targeted
// resumption via Runner.ResumeWithParams. Each context represents a step
// in the agent hierarchy that was interrupted. This is a slice because
// composite agents (e.g. parallel workflows) may interrupt at multiple
// points simultaneously, matching the shape of AgentAction.Interrupted.InterruptContexts.
// Use each InterruptCtx.ID as a key in ResumeParams.Targets.
InterruptContexts []*InterruptCtx
// contains filtered or unexported fields
}
CancelError is sent via AgentEvent.Err when an agent is canceled. Use errors.As to match and extract *CancelError from event errors.
Interrupt absorption: when a cancel is active (shouldCancel() == true), ANY interrupt — whether from a cancel safe-point node or from business logic (e.g. compose.Interrupt in a tool) — is converted to a CancelError. The cancel "absorbs" the business interrupt. This is intentional:
- In concurrent execution (parallel workflows, concurrent tool calls), cancel-induced and business interrupts can arrive as a single composite signal that cannot be split apart.
- Even in sequential execution, treating business interrupts as CancelError during active cancel gives consistent semantics.
- The business interrupt is NOT lost — the checkpoint preserves the full interrupt hierarchy. On resume (Runner.Resume), the agent re-executes the interrupting code path and the business interrupt re-fires naturally.
func (*CancelError) Error ¶
func (e *CancelError) Error() string
type CancelHandle ¶
type CancelHandle struct {
// contains filtered or unexported fields
}
CancelHandle represents a cancel operation that can be waited on.
func (*CancelHandle) Wait ¶
func (h *CancelHandle) Wait() error
Wait blocks until the cancel request reaches a terminal outcome.
It reports the result of the cancel operation itself, not the agent's final business error:
- nil: cancellation succeeded, including the case where a business interrupt was absorbed into CancelError while cancellation was active
- ErrCancelTimeout: the requested safe-point cancellation timed out and was escalated to immediate cancellation
- ErrExecutionCompleted: the execution finished before cancellation took effect, meaning the stream drained to completion without any interrupt
type CancelMode ¶
type CancelMode int
CancelMode specifies when an agent should be canceled. Modes can be combined with bitwise OR to cancel at multiple safe-points. For example, CancelAfterChatModel | CancelAfterToolCalls cancels the agent after whichever safe-point is reached first.
const ( // CancelImmediate cancels the agent as soon as the signal is received, // without waiting for a ChatModel or ToolCalls safe-point. Propagates // to all descendant agents via the cancel context hierarchy, including // agents nested inside AgentTools and workflow sub-agents. CancelImmediate CancelMode = 0 // CancelAfterChatModel cancels after the first chat model call that completes // anywhere in the agent hierarchy, including nested sub-agents, agent tools, // and workflow branches. The cancel mode propagates to all descendant agents; // whichever ChatModel finishes first triggers the cancel. The interrupting // agent emits an interrupt that bubbles up through the agent tree — parent // agents do not need to reach their own ChatModel safe-point. CancelAfterChatModel CancelMode = 1 << iota // CancelAfterToolCalls cancels after the first set of concurrent tool calls // that completes anywhere in the agent hierarchy. Like CancelAfterChatModel, // this mode propagates to all descendants and fires at whichever level // reaches the safe-point first. CancelAfterToolCalls )
type ChatModelAgent ¶
type ChatModelAgent struct {
// contains filtered or unexported fields
}
func NewChatModelAgent ¶
func NewChatModelAgent(ctx context.Context, config *ChatModelAgentConfig) (*ChatModelAgent, error)
NewChatModelAgent constructs a chat model-backed agent with the provided config.
func (*ChatModelAgent) Description ¶
func (a *ChatModelAgent) Description(_ context.Context) string
func (*ChatModelAgent) GetType ¶ added in v0.8.0
func (a *ChatModelAgent) GetType() string
func (*ChatModelAgent) OnDisallowTransferToParent ¶
func (a *ChatModelAgent) OnDisallowTransferToParent(_ context.Context) error
func (*ChatModelAgent) OnSetAsSubAgent ¶
func (a *ChatModelAgent) OnSetAsSubAgent(_ context.Context, parent Agent) error
func (*ChatModelAgent) OnSetSubAgents ¶
func (a *ChatModelAgent) OnSetSubAgents(_ context.Context, subAgents []Agent) error
func (*ChatModelAgent) Resume ¶
func (a *ChatModelAgent) Resume(ctx context.Context, info *ResumeInfo, opts ...AgentRunOption) *AsyncIterator[*AgentEvent]
func (*ChatModelAgent) Run ¶
func (a *ChatModelAgent) Run(ctx context.Context, input *AgentInput, opts ...AgentRunOption) *AsyncIterator[*AgentEvent]
type ChatModelAgentConfig ¶
type ChatModelAgentConfig struct {
// Name of the agent. Better be unique across all agents.
Name string
// Description of the agent's capabilities.
// Helps other agents determine whether to transfer tasks to this agent.
Description string
// Instruction used as the system prompt for this agent.
// Optional. If empty, no system prompt will be used.
// Supports f-string placeholders for session values in default GenModelInput, for example:
// "You are a helpful assistant. The current time is {Time}. The current user is {User}."
// These placeholders will be replaced with session values for "Time" and "User".
Instruction string
// Model is the chat model used by the agent.
// If your ChatModelAgent uses any tools, this model must support the model.WithTools
// call option, as that's how ChatModelAgent configures the model with tool information.
Model model.BaseChatModel
ToolsConfig ToolsConfig
// GenModelInput transforms instructions and input messages into the model's input format.
// Optional. Defaults to defaultGenModelInput which combines instruction and messages.
GenModelInput GenModelInput
// Exit defines the tool used to terminate the agent process.
// Optional. If nil, no Exit Action will be generated.
// You can use the provided 'ExitTool' implementation directly.
Exit tool.BaseTool
// OutputKey stores the agent's response in the session.
// Optional. When set, stores output via AddSessionValue(ctx, outputKey, msg.Content).
OutputKey string
// MaxIterations defines the upper limit of ChatModel generation cycles.
// The agent will terminate with an error if this limit is exceeded.
// Optional. Defaults to 20.
MaxIterations int
// Middlewares configures agent middleware for extending functionality.
// Use for simple, static additions like extra instruction or tools.
// Kept for backward compatibility; for new code, consider using Handlers instead.
Middlewares []AgentMiddleware
// Handlers configures interface-based handlers for extending agent behavior.
// Unlike Middlewares (struct-based), Handlers allow users to:
// - Add custom methods to their handler implementations
// - Return modified context from handler methods
// - Centralize configuration in struct fields instead of closures
//
// Handlers are processed after Middlewares, in registration order.
// See ChatModelAgentMiddleware documentation for when to use Handlers vs Middlewares.
//
// Execution Order (relative to AgentMiddleware and ToolsConfig):
//
// Model call lifecycle (outermost to innermost wrapper chain):
// 1. AgentMiddleware.BeforeChatModel (hook, runs before model call)
// 2. ChatModelAgentMiddleware.BeforeModelRewriteState (hook, can modify state before model call)
// 3. retryModelWrapper (internal - retries on failure, if configured)
// 4. eventSenderModelWrapper (internal - sends model response events)
// 5. ChatModelAgentMiddleware.WrapModel (wrapper, first registered is outermost)
// 6. callbackInjectionModelWrapper (internal - injects callbacks if not enabled)
// 7. Model.Generate/Stream
// 8. ChatModelAgentMiddleware.AfterModelRewriteState (hook, can modify state after model call)
// 9. AgentMiddleware.AfterChatModel (hook, runs after model call)
//
// Custom Event Sender Position:
// By default, events are sent after all user middlewares (WrapModel) have processed the output,
// containing the modified messages. To send events with original (unmodified) output, pass
// NewEventSenderModelWrapper as a Handler after the modifying middleware:
//
// agent, _ := adk.NewChatModelAgent(ctx, &adk.ChatModelAgentConfig{
// Handlers: []adk.ChatModelAgentMiddleware{
// myCustomHandler, // First registered = outermost wrapper
// adk.NewEventSenderModelWrapper(), // Last registered = innermost, events sent with original output
// },
// })
//
// Handler order: first registered is outermost. So [A, B, C] becomes A(B(C(model))).
// EventSenderModelWrapper sends events in post-processing, so placing it innermost
// means it receives the original model output before outer handlers modify it.
//
// When EventSenderModelWrapper is detected in Handlers, the framework skips
// the default event sender to avoid duplicate events.
//
// Tool call lifecycle (outermost to innermost):
// 1. eventSenderToolWrapper (internal ToolMiddleware - sends tool result events after all processing)
// 2. ToolsConfig.ToolCallMiddlewares (ToolMiddleware)
// 3. AgentMiddleware.WrapToolCall (ToolMiddleware)
// 4. ChatModelAgentMiddleware.WrapToolCall (wrapper, first registered is outermost)
// 5. callbackInjectedToolCall (internal - injects callbacks if tool doesn't handle them)
// 6. Tool.InvokableRun/StreamableRun
//
// Custom Tool Event Sender Position:
// By default, tool result events are emitted by an internal event sender placed before
// all user middlewares (outermost), so events reflect the fully processed tool output.
// To control exactly where in the handler chain tool events are emitted, pass
// NewEventSenderToolWrapper() as one of the Handlers. Its position determines which
// middlewares' effects are visible in the emitted event:
//
// agent, _ := adk.NewChatModelAgent(ctx, &adk.ChatModelAgentConfig{
// Handlers: []adk.ChatModelAgentMiddleware{
// loggingHandler, // Outermost: sees event-sender output
// adk.NewEventSenderToolWrapper(), // Events reflect output from handlers below
// sanitizationHandler, // Innermost: runs first, modifies tool output
// },
// })
//
// Handler order: first registered is outermost. So [A, B, C] wraps as A(B(C(tool))).
// The event sender captures tool output in post-processing, so its position controls
// which handlers' modifications are included in the emitted events.
//
// When NewEventSenderToolWrapper is detected in Handlers, the framework skips
// the default event sender to avoid duplicate events.
//
// Tool List Modification:
//
// There are two ways to modify the tool list:
//
// 1. In BeforeAgent: Modify ChatModelAgentContext.Tools ([]tool.BaseTool) directly. This affects
// both the tool info list passed to ChatModel AND the actual tools available for
// execution. Changes persist for the entire agent run.
//
// 2. In WrapModel: Create a model wrapper that modifies the tool info list per model
// request using model.WithTools(toolInfos). This ONLY affects the tool info list
// passed to ChatModel, NOT the actual tools available for execution. Use this for
// dynamic tool filtering/selection based on conversation context. The modification
// is scoped to this model request only.
Handlers []ChatModelAgentMiddleware
// ModelRetryConfig configures retry behavior for the ChatModel.
// When set, the agent will automatically retry failed ChatModel calls
// based on the configured policy.
// Optional. If nil, no retry will be performed.
ModelRetryConfig *ModelRetryConfig
}
type ChatModelAgentContext ¶ added in v0.8.0
type ChatModelAgentContext struct {
// Instruction is the current instruction for the Agent execution.
// It includes the instruction configured for the agent, additional instructions appended by framework
// and AgentMiddleware, and modifications applied by previous BeforeAgent handlers.
// The finalized instruction after all BeforeAgent handlers are then passed to GenModelInput,
// to be (optionally) formatted with SessionValues and converted to system message.
Instruction string
// Tools are the raw tools (without any wrapper or tool middleware) currently configured for the Agent execution.
// They includes tools passed in AgentConfig, implicit tools added by framework such as transfer / exit tools,
// and other tools already added by middlewares.
Tools []tool.BaseTool
// ReturnDirectly is the set of tool names currently configured to cause the Agent to return directly.
// This is based on the return directly map configured for the agent, plus any modifications
// by previous BeforeAgent handlers.
ReturnDirectly map[string]bool
}
ChatModelAgentContext contains runtime information passed to handlers before each ChatModelAgent run. Handlers can modify Instruction, Tools, and ReturnDirectly to customize agent behavior.
This type is specific to ChatModelAgent. Other agent types may define their own context types.
type ChatModelAgentInterruptInfo ¶
type ChatModelAgentInterruptInfo struct {
Info *compose.InterruptInfo
Data []byte
}
type ChatModelAgentMiddleware ¶ added in v0.8.0
type ChatModelAgentMiddleware interface {
// BeforeAgent is called before each agent run, allowing modification of
// the agent's instruction and tools configuration.
BeforeAgent(ctx context.Context, runCtx *ChatModelAgentContext) (context.Context, *ChatModelAgentContext, error)
// BeforeModelRewriteState is called before each model invocation.
// The returned state is persisted to the agent's internal state and passed to the model.
// The returned context is propagated to the model call and subsequent handlers.
//
// The ChatModelAgentState struct provides access to:
// - Messages: the conversation history
//
// The ModelContext struct provides read-only access to:
// - Tools: the current tool list that will be sent to the model
BeforeModelRewriteState(ctx context.Context, state *ChatModelAgentState, mc *ModelContext) (context.Context, *ChatModelAgentState, error)
// AfterModelRewriteState is called after each model invocation.
// The input state includes the model's response as the last message.
// The returned state is persisted to the agent's internal state.
//
// The ChatModelAgentState struct provides access to:
// - Messages: the conversation history including the model's response
//
// The ModelContext struct provides read-only access to:
// - Tools: the current tool list that was sent to the model
AfterModelRewriteState(ctx context.Context, state *ChatModelAgentState, mc *ModelContext) (context.Context, *ChatModelAgentState, error)
// AfterToolCallsRewriteState is called after all concurrent tool calls in an iteration complete.
// The input state includes all messages up to and including the tool call results.
// The returned state is persisted to the agent's internal state.
//
// The ToolCallsContext provides metadata about the tool calls that just completed,
// derived from the assistant message's ToolCalls field.
AfterToolCallsRewriteState(ctx context.Context, state *ChatModelAgentState, tc *ToolCallsContext) (context.Context, *ChatModelAgentState, error)
// WrapInvokableToolCall wraps a tool's synchronous execution with custom behavior.
// Return the input endpoint unchanged and nil error if no wrapping is needed.
//
// This method is only called for tools that implement InvokableTool.
// If a tool only implements StreamableTool, this method will not be called for that tool.
//
// This method is called at request time when the tool is about to be executed.
// The tCtx parameter provides metadata about the tool:
// - Name: The name of the tool being wrapped
// - CallID: The unique identifier for this specific tool call
WrapInvokableToolCall(ctx context.Context, endpoint InvokableToolCallEndpoint, tCtx *ToolContext) (InvokableToolCallEndpoint, error)
// WrapStreamableToolCall wraps a tool's streaming execution with custom behavior.
// Return the input endpoint unchanged and nil error if no wrapping is needed.
//
// This method is only called for tools that implement StreamableTool.
// If a tool only implements InvokableTool, this method will not be called for that tool.
//
// This method is called at request time when the tool is about to be executed.
// The tCtx parameter provides metadata about the tool:
// - Name: The name of the tool being wrapped
// - CallID: The unique identifier for this specific tool call
WrapStreamableToolCall(ctx context.Context, endpoint StreamableToolCallEndpoint, tCtx *ToolContext) (StreamableToolCallEndpoint, error)
// WrapEnhancedInvokableToolCall wraps an enhanced tool's synchronous execution with custom behavior.
// Return the input endpoint unchanged and nil error if no wrapping is needed.
//
// This method is only called for tools that implement EnhancedInvokableTool.
// If a tool only implements EnhancedStreamableTool, this method will not be called for that tool.
//
// This method is called at request time when the tool is about to be executed.
// The tCtx parameter provides metadata about the tool:
// - Name: The name of the tool being wrapped
// - CallID: The unique identifier for this specific tool call
WrapEnhancedInvokableToolCall(ctx context.Context, endpoint EnhancedInvokableToolCallEndpoint, tCtx *ToolContext) (EnhancedInvokableToolCallEndpoint, error)
// WrapEnhancedStreamableToolCall wraps an enhanced tool's streaming execution with custom behavior.
// Return the input endpoint unchanged and nil error if no wrapping is needed.
//
// This method is only called for tools that implement EnhancedStreamableTool.
// If a tool only implements EnhancedInvokableTool, this method will not be called for that tool.
//
// This method is called at request time when the tool is about to be executed.
// The tCtx parameter provides metadata about the tool:
// - Name: The name of the tool being wrapped
// - CallID: The unique identifier for this specific tool call
WrapEnhancedStreamableToolCall(ctx context.Context, endpoint EnhancedStreamableToolCallEndpoint, tCtx *ToolContext) (EnhancedStreamableToolCallEndpoint, error)
// WrapModel wraps a chat model with custom behavior.
// Return the input model unchanged and nil error if no wrapping is needed.
//
// This method is called at request time when the model is about to be invoked.
// Note: The parameter is BaseChatModel (not ToolCallingChatModel) because wrappers
// only need to intercept Generate/Stream calls. Tool binding (WithTools) is handled
// separately by the framework and does not flow through user wrappers.
//
// The mc parameter contains the current tool configuration:
// - Tools: The tool infos that will be sent to the model
WrapModel(ctx context.Context, m model.BaseChatModel, mc *ModelContext) (model.BaseChatModel, error)
}
ChatModelAgentMiddleware defines the interface for customizing ChatModelAgent behavior.
IMPORTANT: This interface is specifically designed for ChatModelAgent and agents built on top of it (e.g., DeepAgent).
Why ChatModelAgentMiddleware instead of AgentMiddleware?
AgentMiddleware is a struct type, which has inherent limitations:
- Struct types are closed: users cannot add new methods to extend functionality
- The framework only recognizes AgentMiddleware's fixed fields, so even if users embed AgentMiddleware in a custom struct and add methods, the framework cannot call those methods (config.Middlewares is []AgentMiddleware, not a user type)
- Callbacks in AgentMiddleware only return error, cannot return modified context
ChatModelAgentMiddleware is an interface type, which is open for extension:
- Users can implement custom handlers with arbitrary internal state and methods
- Hook methods return (context.Context, ..., error) for direct context propagation
- Wrapper methods (WrapToolCall, WrapModel) enable context propagation through the wrapped endpoint chain: wrappers can pass modified context to the next wrapper
- Configuration is centralized in struct fields rather than scattered in closures
ChatModelAgentMiddleware vs AgentMiddleware:
- Use AgentMiddleware for simple, static additions (extra instruction/tools)
- Use ChatModelAgentMiddleware for dynamic behavior, context modification, or call wrapping
- AgentMiddleware is kept for backward compatibility with existing users
- Both can be used together; see AgentMiddleware documentation for execution order
Use *BaseChatModelAgentMiddleware as an embedded struct to provide default no-op implementations for all methods.
func NewEventSenderModelWrapper ¶ added in v0.8.0
func NewEventSenderModelWrapper() ChatModelAgentMiddleware
NewEventSenderModelWrapper returns a ChatModelAgentMiddleware that sends model response events. By default, the framework applies this wrapper after all user middlewares, so events contain modified messages. To send events with original (unmodified) output, pass this as a Handler after the modifying middleware (placing it innermost in the wrapper chain). When detected in Handlers, the framework skips the default event sender to avoid duplicates.
func NewEventSenderToolWrapper ¶
func NewEventSenderToolWrapper() ChatModelAgentMiddleware
NewEventSenderToolWrapper returns a ChatModelAgentMiddleware that sends tool result events. By default, the framework places this before all user middlewares (outermost), so events reflect the fully processed tool output. To control exactly where events are emitted, include this in ChatModelAgentConfig.Handlers at the desired position. When detected in Handlers, the framework skips the default event sender to avoid duplicates.
type ChatModelAgentResumeData ¶ added in v0.7.0
type ChatModelAgentResumeData struct {
// HistoryModifier is a function that can transform the agent's message history before it is sent to the model.
// This allows for adding new information or context upon resumption.
HistoryModifier func(ctx context.Context, history []Message) []Message
}
ChatModelAgentResumeData holds data that can be provided to a ChatModelAgent during a resume operation to modify its behavior. It is provided via the adk.ResumeWithData function.
type ChatModelAgentState ¶ added in v0.5.14
type ChatModelAgentState struct {
// Messages contains all messages in the current conversation session.
Messages []Message
}
ChatModelAgentState represents the state of a chat model agent during conversation. This is the primary state type for both ChatModelAgentMiddleware and AgentMiddleware callbacks.
type CheckPointDeleter ¶
type CheckPointDeleter = core.CheckPointDeleter
type CheckPointStore ¶ added in v0.7.22
type CheckPointStore = core.CheckPointStore
type EnhancedInvokableToolCallEndpoint ¶ added in v0.8.0
type EnhancedInvokableToolCallEndpoint func(ctx context.Context, toolArgument *schema.ToolArgument, opts ...tool.Option) (*schema.ToolResult, error)
type EnhancedStreamableToolCallEndpoint ¶ added in v0.8.0
type EnhancedStreamableToolCallEndpoint func(ctx context.Context, toolArgument *schema.ToolArgument, opts ...tool.Option) (*schema.StreamReader[*schema.ToolResult], error)
type GenInputResult ¶
type GenInputResult[T any] struct { // RunCtx, if non-nil, overrides the context for this turn's execution // (PrepareAgent, agent run, OnAgentEvents). // // Must be derived from the ctx passed to GenInput to preserve the // TurnLoop's cancellation semantics and inherited values. For example: // // runCtx := context.WithValue(ctx, traceKey{}, extractTraceID(items)) // return &GenInputResult[T]{RunCtx: runCtx, ...}, nil // // If nil, the TurnLoop's context is used unchanged. RunCtx context.Context // Input is the agent input to execute Input *AgentInput // RunOpts are the options for this agent run RunOpts []AgentRunOption // Consumed are the items selected for this turn. // They are removed from the buffer and passed to PrepareAgent. Consumed []T // Remaining are the items to keep in the buffer for a future turn. // TurnLoop pushes Remaining back into the buffer before running the agent. // // Items from the GenInput input slice that are in neither Consumed nor Remaining // are dropped by the loop. Remaining []T }
GenInputResult contains the result of GenInput processing.
type GenModelInput ¶
type GenModelInput func(ctx context.Context, instruction string, input *AgentInput) ([]Message, error)
GenModelInput transforms agent instructions and input into a format suitable for the model.
type GenResumeResult ¶
type GenResumeResult[T any] struct { // RunCtx, if non-nil, overrides the context for this resumed turn's execution // (PrepareAgent, agent resume, OnAgentEvents). RunCtx context.Context // RunOpts are the options for this agent resume run. RunOpts []AgentRunOption // ResumeParams are optional parameters for resuming an interrupted agent. ResumeParams *ResumeParams // Consumed are the items selected for this resumed turn. // They are removed from the buffer and passed to PrepareAgent. Consumed []T // Remaining are the items to keep in the buffer for a future turn. // TurnLoop pushes Remaining back into the buffer before resuming the agent. // // Items from (canceledItems, unhandledItems, newItems) that are in neither Consumed // nor Remaining are dropped by the loop. Remaining []T }
GenResumeResult contains the result of GenResume processing.
type HistoryEntry ¶
type HistoryRewriter ¶
type HistoryRewriter func(ctx context.Context, entries []*HistoryEntry) ([]Message, error)
type InterruptCtx ¶ added in v0.7.0
type InterruptCtx = core.InterruptCtx
InterruptCtx provides a structured, user-facing view of a single point of interruption. It contains the ID and Address of the interrupted component, as well as user-defined info. This is a type alias for core.InterruptCtx. See the core package for more details.
type InterruptInfo ¶
type InterruptInfo struct {
Data any
// InterruptContexts provides a structured, user-facing view of the interrupt chain.
// Each context represents a step in the agent hierarchy that was interrupted.
InterruptContexts []*InterruptCtx
}
InterruptInfo contains all the information about an interruption event. It is created by the framework when an agent returns an interrupt action.
type InterruptSignal ¶ added in v0.7.0
type InterruptSignal = core.InterruptSignal
func FromInterruptContexts ¶ added in v0.7.0
func FromInterruptContexts(contexts []*InterruptCtx) *InterruptSignal
FromInterruptContexts converts user-facing interrupt contexts to an interrupt signal.
type InvokableToolCallEndpoint ¶ added in v0.8.0
type InvokableToolCallEndpoint func(ctx context.Context, argumentsInJSON string, opts ...tool.Option) (string, error)
InvokableToolCallEndpoint is the function signature for invoking a tool synchronously. Middleware authors implement wrappers around this endpoint to add custom behavior.
type Language ¶ added in v0.8.0
Language represents the language setting for the ADK built-in prompts.
const ( // LanguageEnglish represents English language. LanguageEnglish Language = internal.LanguageEnglish // LanguageChinese represents Chinese language. LanguageChinese Language = internal.LanguageChinese )
type LoopAgentConfig ¶
type MessageStream ¶
type MessageStream = *schema.StreamReader[Message]
type MessageVariant ¶
type MessageVariant struct {
IsStreaming bool
Message Message
MessageStream MessageStream
// message role: Assistant or Tool
Role schema.RoleType
// only used when Role is Tool
ToolName string
}
func (*MessageVariant) GetMessage ¶
func (mv *MessageVariant) GetMessage() (Message, error)
func (*MessageVariant) GobDecode ¶
func (mv *MessageVariant) GobDecode(b []byte) error
func (*MessageVariant) GobEncode ¶
func (mv *MessageVariant) GobEncode() ([]byte, error)
type ModelContext ¶ added in v0.8.0
type ModelContext struct {
// Tools contains the current tool list configured for the agent.
// This is populated at request time with the tools that will be sent to the model.
Tools []*schema.ToolInfo
// ModelRetryConfig contains the retry configuration for the model.
// This is populated at request time from the agent's ModelRetryConfig.
// Used by EventSenderModelWrapper to wrap stream errors appropriately.
ModelRetryConfig *ModelRetryConfig
// contains filtered or unexported fields
}
ModelContext contains context information passed to WrapModel.
type ModelRetryConfig ¶ added in v0.7.14
type ModelRetryConfig struct {
// MaxRetries specifies the maximum number of retry attempts.
// A value of 0 means no retries will be attempted.
// A value of 3 means up to 3 retry attempts (4 total calls including the initial attempt).
MaxRetries int
// IsRetryAble is a function that determines whether an error should trigger a retry.
// If nil, all errors are considered retry-able.
// Return true if the error is transient and the operation should be retried.
// Return false if the error is permanent and should be propagated immediately.
IsRetryAble func(ctx context.Context, err error) bool
// BackoffFunc calculates the delay before the next retry attempt.
// The attempt parameter starts at 1 for the first retry.
// If nil, a default exponential backoff with jitter is used:
// base delay 100ms, exponentially increasing up to 10s max,
// with random jitter (0-50% of delay) to prevent thundering herd.
BackoffFunc func(ctx context.Context, attempt int) time.Duration
}
ModelRetryConfig configures retry behavior for the ChatModel node. It defines how the agent should handle transient failures when calling the ChatModel.
type OnSubAgents ¶
type ParallelAgentConfig ¶
type PushOption ¶
type PushOption[T any] func(*pushConfig[T])
PushOption is an option for Push().
func WithPreempt ¶
func WithPreempt[T any](agentCancelOpts ...AgentCancelOption) PushOption[T]
WithPreempt signals that the current agent should be canceled after pushing. This enables atomic "push + preempt" to avoid race conditions between pushing an urgent item and triggering preemption. The loop will cancel the current agent turn and continue with the next turn, where GenInput will see all buffered items including the newly pushed one.
func WithPreemptDelay ¶
func WithPreemptDelay[T any](delay time.Duration) PushOption[T]
WithPreemptDelay sets a delay duration before preemption takes effect. When used with WithPreempt, the push will succeed immediately, but the preemption signal will be delayed by the specified duration. This allows the current agent to continue processing for a grace period before being preempted.
func WithPushStrategy ¶
func WithPushStrategy[T any](fn func(ctx context.Context, tc *TurnContext[T]) []PushOption[T]) PushOption[T]
WithPushStrategy provides dynamic push option resolution based on the current turn state. The callback receives the current turn's context and TurnContext (nil if no turn is active) and returns the actual PushOptions to apply. When WithPushStrategy is used, all other PushOptions passed to the same Push call are ignored.
The returned options must not contain another WithPushStrategy; any nested strategy is silently stripped.
Example: preempt only if the current turn is processing low-priority items:
loop.Push(urgentItem, WithPushStrategy(func(ctx context.Context, tc *TurnContext[MyItem]) []PushOption[MyItem] {
if tc == nil {
return nil // between turns, plain push
}
if isLowPriority(tc.Consumed) {
return []PushOption[MyItem]{WithPreempt[MyItem]()}
}
return nil // don't preempt high-priority work
}))
type ResumableAgent ¶
type ResumableAgent interface {
Agent
Resume(ctx context.Context, info *ResumeInfo, opts ...AgentRunOption) *AsyncIterator[*AgentEvent]
}
func NewLoopAgent ¶
func NewLoopAgent(ctx context.Context, config *LoopAgentConfig) (ResumableAgent, error)
NewLoopAgent creates an agent that loops over sub-agents with a max iteration limit.
func NewParallelAgent ¶
func NewParallelAgent(ctx context.Context, config *ParallelAgentConfig) (ResumableAgent, error)
NewParallelAgent creates an agent that runs sub-agents in parallel.
func NewSequentialAgent ¶
func NewSequentialAgent(ctx context.Context, config *SequentialAgentConfig) (ResumableAgent, error)
NewSequentialAgent creates an agent that runs sub-agents sequentially.
func SetSubAgents ¶
SetSubAgents sets sub-agents for the given agent and returns the updated agent.
type ResumeInfo ¶
type ResumeInfo struct {
// EnableStreaming indicates whether the original execution was in streaming mode.
EnableStreaming bool
// Deprecated: use InterruptContexts from the embedded InterruptInfo for user-facing details,
// and GetInterruptState for internal state retrieval.
*InterruptInfo
WasInterrupted bool
InterruptState any
IsResumeTarget bool
ResumeData any
}
ResumeInfo holds all the information necessary to resume an interrupted agent execution. It is created by the framework and passed to an agent's Resume method.
type ResumeParams ¶ added in v0.7.0
type ResumeParams struct {
// Targets contains the addresses of components to be resumed as keys,
// with their corresponding resume data as values
Targets map[string]any
}
ResumeParams contains all parameters needed to resume an execution. This struct provides an extensible way to pass resume parameters without requiring breaking changes to method signatures.
type RetryExhaustedError ¶ added in v0.7.14
RetryExhaustedError is returned when all retry attempts have been exhausted. It wraps the last error that occurred during retry attempts.
func (*RetryExhaustedError) Error ¶ added in v0.7.14
func (e *RetryExhaustedError) Error() string
func (*RetryExhaustedError) Unwrap ¶ added in v0.7.14
func (e *RetryExhaustedError) Unwrap() error
type RunStep ¶
type RunStep struct {
// contains filtered or unexported fields
}
RunStep CheckpointSchema: persisted via serialization.RunCtx (gob).
type Runner ¶
type Runner struct {
// contains filtered or unexported fields
}
Runner is the primary entry point for executing an Agent. It manages the agent's lifecycle, including starting, resuming, and checkpointing.
func NewRunner ¶
func NewRunner(_ context.Context, conf RunnerConfig) *Runner
NewRunner creates a Runner that executes an Agent with optional streaming and checkpoint persistence.
func (*Runner) Query ¶
func (r *Runner) Query(ctx context.Context, query string, opts ...AgentRunOption) *AsyncIterator[*AgentEvent]
Query is a convenience method that starts a new execution with a single user query string.
func (*Runner) Resume ¶
func (r *Runner) Resume(ctx context.Context, checkPointID string, opts ...AgentRunOption) ( *AsyncIterator[*AgentEvent], error)
Resume continues an interrupted execution from a checkpoint, using an "Implicit Resume All" strategy. This method is best for simpler use cases where the act of resuming implies that all previously interrupted points should proceed without specific data.
When using this method, all interrupted agents will receive `isResumeFlow = false` when they call `GetResumeContext`, as no specific agent was targeted. This is suitable for the "Simple Confirmation" pattern where an agent only needs to know `wasInterrupted` is true to continue.
func (*Runner) ResumeWithParams ¶ added in v0.7.0
func (r *Runner) ResumeWithParams(ctx context.Context, checkPointID string, params *ResumeParams, opts ...AgentRunOption) (*AsyncIterator[*AgentEvent], error)
ResumeWithParams continues an interrupted execution from a checkpoint with specific parameters. This is the most common and powerful way to resume, allowing you to target specific interrupt points (identified by their address/ID) and provide them with data.
The params.Targets map should contain the addresses of the components to be resumed as keys. These addresses can point to any interruptible component in the entire execution graph, including ADK agents, compose graph nodes, or tools. The value can be the resume data for that component, or `nil` if no data is needed.
When using this method:
- Components whose addresses are in the params.Targets map will receive `isResumeFlow = true` when they call `GetResumeContext`.
- Interrupted components whose addresses are NOT in the params.Targets map must decide how to proceed: -- "Leaf" components (the actual root causes of the original interrupt) MUST re-interrupt themselves to preserve their state. -- "Composite" agents (like SequentialAgent or ChatModelAgent) should generally proceed with their execution. They act as conduits, allowing the resume signal to flow to their children. They will naturally re-interrupt if one of their interrupted children re-interrupts, as they receive the new `CompositeInterrupt` signal from them.
func (*Runner) Run ¶
func (r *Runner) Run(ctx context.Context, messages []Message, opts ...AgentRunOption) *AsyncIterator[*AgentEvent]
Run starts a new execution of the agent with a given set of messages. It returns an iterator that yields agent events as they occur. If the Runner was configured with a CheckPointStore, it will automatically save the agent's state upon interruption.
type RunnerConfig ¶
type RunnerConfig struct {
Agent Agent
EnableStreaming bool
CheckPointStore CheckPointStore
}
type SequentialAgentConfig ¶
type State
deprecated
type State struct {
Messages []Message
Extra map[string]any
// Internal fields below - do not access directly.
// Kept exported for backward compatibility with existing checkpoints.
HasReturnDirectly bool
ReturnDirectlyToolCallID string
ToolGenActions map[string]*AgentAction
AgentName string
RemainingIterations int
ReturnDirectlyEvent *AgentEvent
RetryAttempt int
}
State holds agent runtime state including messages and user-extensible storage.
Deprecated: This type will be unexported in v1.0.0. Use ChatModelAgentState in HandlerMiddleware and AgentMiddleware callbacks instead. Direct use of compose.ProcessState[*State] is discouraged and will stop working in v1.0.0; use the handler APIs instead.
type StopOption ¶
type StopOption func(*stopConfig)
StopOption is an option for Stop().
func WithAgentCancel ¶
func WithAgentCancel(opts ...AgentCancelOption) StopOption
WithAgentCancel sets the agent cancel options to use when stopping the loop. These options control how the currently running agent is cancelled.
func WithSkipCheckpoint ¶
func WithSkipCheckpoint() StopOption
WithSkipCheckpoint tells the TurnLoop not to persist a checkpoint for this Stop call. Use this when the caller does not intend to resume in the future. The flag is sticky: once any Stop() call sets it, subsequent calls cannot undo it.
func WithStopCause ¶
func WithStopCause(cause string) StopOption
WithStopCause attaches a business-supplied reason string to this Stop call. The cause is surfaced in TurnLoopExitState.StopCause and, after the Stopped channel closes, via TurnContext.StopCause(). If multiple Stop() calls provide a cause, the first non-empty value wins.
type StreamCanceledError ¶
type StreamCanceledError struct{}
StreamCanceledError is the concrete error type for ErrStreamCanceled. It is exported so that gob can serialize it during checkpoint save when the error is stored in agentEventWrapper.StreamErr.
func (*StreamCanceledError) Error ¶
func (e *StreamCanceledError) Error() string
type StreamableToolCallEndpoint ¶ added in v0.8.0
type StreamableToolCallEndpoint func(ctx context.Context, argumentsInJSON string, opts ...tool.Option) (*schema.StreamReader[string], error)
StreamableToolCallEndpoint is the function signature for invoking a tool with streaming output. Middleware authors implement wrappers around this endpoint to add custom behavior.
type ToolCallsContext ¶
type ToolCallsContext struct {
// ToolCalls contains the tool call metadata from the model's response.
ToolCalls []ToolContext
}
ToolCallsContext contains metadata about the tool calls that just completed.
type ToolContext ¶ added in v0.8.0
ToolContext provides metadata about the tool being wrapped.
type ToolsConfig ¶
type ToolsConfig struct {
compose.ToolsNodeConfig
// ReturnDirectly specifies tools that cause the agent to return immediately when called.
// If multiple listed tools are called simultaneously, only the first one triggers the return.
// The map keys are tool names indicate whether the tool should trigger immediate return.
ReturnDirectly map[string]bool
// EmitInternalEvents indicates whether internal events from agentTool should be emitted
// to the parent agent's AsyncGenerator, allowing real-time streaming of nested agent output
// to the end-user via Runner.
//
// Note that these forwarded events are NOT recorded in the parent agent's runSession.
// They are only emitted to the end-user and have no effect on the parent agent's state
// or checkpoint.
//
// Action Scoping:
// Actions emitted by the inner agent are scoped to the agent tool boundary:
// - Interrupted: Propagated via CompositeInterrupt to allow proper interrupt/resume
// - Exit, TransferToAgent, BreakLoop: Ignored outside the agent tool
EmitInternalEvents bool
}
type TransferToAgentAction ¶
type TransferToAgentAction struct {
DestAgentName string
}
type TurnContext ¶
type TurnContext[T any] struct { // Loop is the TurnLoop instance, allowing Push() or Stop() calls. Loop *TurnLoop[T] // Consumed contains items that triggered this agent execution. Consumed []T // Preempted is closed when a preempt signal fires for the current turn // (via Push with WithPreempt) and at least one preemptive Push contributed // to the CancelError for the current turn. // "Contributed" means the preempt's cancel options were included in the // CancelError before it was finalized. Remains open if no preempt contributed. // Use in a select to detect preemption while processing events. Preempted <-chan struct{} // Stopped is closed when a Stop() call contributed to the CancelError for the // current turn. // "Contributed" means Stop's cancel options were included in the CancelError // before it was finalized. Remains open if Stop did not contribute. // Use in a select to detect stop while processing events. Stopped <-chan struct{} // StopCause returns the business-supplied reason from WithStopCause. // This value is only meaningful after the Stopped channel is closed. // Before that, it returns an empty string. StopCause func() string }
TurnContext provides per-turn context to the OnAgentEvents callback.
type TurnLoop ¶
type TurnLoop[T any] struct { // contains filtered or unexported fields }
TurnLoop is a push-based event loop for agent execution. Users push items via Push() and the loop processes them through the agent.
Create with NewTurnLoop, then start with Run:
loop := NewTurnLoop(cfg) // pass loop to other components, push initial items, etc. loop.Run(ctx)
Permissive API ¶
All methods are valid on a not-yet-running loop:
- Push: items are buffered and will be processed once Run is called.
- Stop: sets the stopped flag; a subsequent Run will exit immediately.
- Wait: blocks until Run is called AND the loop exits. If Run is never called, Wait blocks forever (this is a programming error, analogous to reading from a channel that nobody writes to).
func NewTurnLoop ¶
func NewTurnLoop[T any](cfg TurnLoopConfig[T]) *TurnLoop[T]
NewTurnLoop creates a new TurnLoop without starting it. The returned loop accepts Push and Stop calls immediately; pushed items are buffered until Run is called. Call Run to start the processing goroutine.
NewTurnLoop panics if GenInput or PrepareAgent is nil.
func (*TurnLoop[T]) Push ¶
func (l *TurnLoop[T]) Push(item T, opts ...PushOption[T]) (bool, <-chan struct{})
Push adds an item to the loop's buffer for processing. This method is non-blocking and thread-safe. Returns false if the loop has stopped, true otherwise. If a preemptive push succeeds, the second return value is a channel that is closed when the loop has acknowledged the preempt signal (by either initiating cancellation of the current agent run or reaching a point where no cancellation is needed). If the loop has not been started yet (Run not called), items are buffered and will be processed once Run is called. After Wait() returns, failed pushes can be recovered via TurnLoopExitState.TakeLateItems(). Once TakeLateItems() has been called, any subsequent push that would become a late item will panic instead of being silently dropped.
Use WithPreempt() to atomically push an item and signal preemption of the current agent. This is useful for urgent items that should interrupt the current processing. The returned channel may be waited on if the caller needs to ensure the preempt signal has been observed.
Use WithPreemptDelay() together with WithPreempt() to delay the preemption signal. Push returns immediately after the item is buffered, and a goroutine is spawned to signal preemption after the delay.
func (*TurnLoop[T]) Run ¶
Run starts the loop's processing goroutine. It is non-blocking: the loop runs in the background and results are obtained via Wait.
If CheckpointID is configured in TurnLoopConfig and a matching checkpoint exists in Store, the loop automatically resumes from that checkpoint. Otherwise it starts fresh with whatever items were Push()-ed.
Calling Run more than once is a no-op: only the first call starts the loop.
func (*TurnLoop[T]) Stop ¶
func (l *TurnLoop[T]) Stop(opts ...StopOption)
Stop signals the loop to stop and returns immediately (non-blocking). The loop will finish the current turn (or cancel it via WithAgentCancel options), then exit without starting a new turn. Use WithAgentCancel to control how the currently running agent is cancelled. This method is idempotent - multiple calls update cancel options. Call Wait() to block until the loop has fully exited and get the result.
Stop may be called before Run. In that case, the stopped flag is set and a subsequent Run will exit the loop immediately.
If the running agent does not support the WithCancel AgentRunOption, Stop degrades to "exit the loop on entering the next iteration" — the current agent turn runs to completion before the loop exits.
func (*TurnLoop[T]) Wait ¶
func (l *TurnLoop[T]) Wait() *TurnLoopExitState[T]
Wait blocks until the loop exits and returns the result. This method is safe to call from multiple goroutines. All callers will receive the same result.
Wait blocks until Run is called AND the loop exits. If Run is ever called, Wait blocks forever.
type TurnLoopConfig ¶
type TurnLoopConfig[T any] struct { // GenInput receives the TurnLoop instance and all buffered items, and decides what to process. // It returns which items to consume now vs keep for later turns. // The loop parameter allows calling Push() or Stop() directly from within the callback. // Required. GenInput func(ctx context.Context, loop *TurnLoop[T], items []T) (*GenInputResult[T], error) // GenResume is called exactly once when the TurnLoop detects a mid-turn // checkpoint on startup (i.e. CheckpointID is configured and the stored // checkpoint has runner state from an interrupted agent execution). // It receives: // - canceledItems: the items being processed when the prior run was canceled // - unhandledItems: items buffered but not processed when the prior run exited // - newItems: items that were Push()-ed before Run() was called // // It returns a GenResumeResult describing how to resume the interrupted agent // turn (optional ResumeParams) and how to manipulate the buffer // (Consumed/Remaining) before continuing. GenResume func(ctx context.Context, loop *TurnLoop[T], canceledItems, unhandledItems, newItems []T) (*GenResumeResult[T], error) // PrepareAgent returns an Agent configured to handle the consumed items. // This callback should set up the agent with appropriate system prompt, // tools, and middlewares based on what items are being processed. // Called once per turn with the items that GenInput decided to consume. // The loop parameter allows calling Push() or Stop() directly from within the callback. // Required. PrepareAgent func(ctx context.Context, loop *TurnLoop[T], consumed []T) (Agent, error) // OnAgentEvents is called to handle events emitted by the agent. // The TurnContext provides per-turn info and control: // - tc.Consumed: items that triggered this agent execution // - tc.Loop: allows calling Push() or Stop() directly from within the callback // - tc.Preempted / tc.Stopped: signals while processing events // Optional. If not provided, events are drained and errors (except CancelError // from Stop-triggered cancellation) are returned as ExitReason. OnAgentEvents func(ctx context.Context, tc *TurnContext[T], events *AsyncIterator[*AgentEvent]) error // Store is the checkpoint store for persistence and resume. Optional. // When set together with CheckpointID, enables automatic checkpoint-based resume. // The TurnLoop always persists both runner checkpoint bytes and item bookkeeping // (CanceledItems, UnhandledItems) via gob encoding, so T must be gob-encodable // when Store is used. Store CheckPointStore // CheckpointID, when set together with Store, enables automatic // checkpoint-based resume. On Run(), the TurnLoop queries Store for this ID: // - If a checkpoint exists with runner state (mid-turn interrupt), // GenResume is called to plan the resume turn. // - If a checkpoint exists without runner state (between-turns), // the stored unhandled items are buffered and the loop proceeds // normally via GenInput. // - If no checkpoint exists, the loop starts fresh. // // On exit, if the TurnLoop saved a new checkpoint, it is saved under this // same CheckpointID. On clean exit (no checkpoint saved), the existing // checkpoint under CheckpointID is deleted to prevent stale resumption. CheckpointID string }
TurnLoopConfig is the configuration for creating a TurnLoop.
type TurnLoopExitState ¶
type TurnLoopExitState[T any] struct { // ExitReason indicates why the loop exited. // nil means clean exit (Stop() was called and completed normally). // Non-nil values include context errors, callback errors, *CancelError, etc. // When Stop() cancels a running agent, ExitReason will be a *CancelError. // This never contains checkpoint errors — see CheckpointErr for those. ExitReason error // UnhandledItems contains items that were buffered but not processed. // These are items for which Push returned true but were never consumed by a turn. // This is always valid regardless of ExitReason. UnhandledItems []T // CanceledItems contains the items whose turn was canceled by Stop(). // This is set when Stop() is called during a running turn, even if it // did not contribute to the final CancelError. // It can be used to reconstruct GenInput/PrepareAgent inputs when resuming. CanceledItems []T // StopCause is the business-supplied reason passed via WithStopCause. // Empty if Stop was not called or no cause was provided. StopCause string // Checkpointed indicates whether a checkpoint save was attempted during cleanup. // True only when Store is configured, CheckpointID is set, Stop() was called, // and the loop was not idle at exit time. Checkpointed bool // CheckpointErr is the error from checkpoint save, if any. // nil when Checkpointed is false (no attempt was made) or when the save succeeded. CheckpointErr error // TakeLateItems returns items that were pushed after the loop stopped // (i.e., Push returned false for these items). These items are NOT included // in the checkpoint. // // This function is idempotent: the first call computes and caches the result; // subsequent calls return the same slice. // // After TakeLateItems is called, any subsequent Push() will panic. This // seals the late buffer and prevents items from being silently lost. // // It is safe to call TakeLateItems from any goroutine after Wait() returns. // If TakeLateItems is never called, late items are simply garbage collected. TakeLateItems func() []T }
TurnLoopExitState is returned when TurnLoop exits, containing the exit reason and any items that were not processed.
type WillRetryError ¶ added in v0.7.14
type WillRetryError struct {
ErrStr string
RetryAttempt int
// contains filtered or unexported fields
}
WillRetryError is emitted when a retryable error occurs and a retry will be attempted. It allows end-users to observe retry events in real-time via AgentEvent.
Field design rationale:
- ErrStr (exported): Stores the error message string for Gob serialization during checkpointing. This ensures the error message is preserved after checkpoint restore.
- err (unexported): Stores the original error for Unwrap() support at runtime. This field is intentionally unexported because Gob serialization would fail for unregistered concrete error types. Since end-users only need the original error when the AgentEvent first occurs (not after restoring from checkpoint), skipping serialization is acceptable. After checkpoint restore, err will be nil and Unwrap() returns nil.
func (*WillRetryError) Error ¶ added in v0.7.14
func (e *WillRetryError) Error() string
func (*WillRetryError) Unwrap ¶ added in v0.7.24
func (e *WillRetryError) Unwrap() error
type WorkflowInterruptInfo ¶
type WorkflowInterruptInfo struct {
OrigInput *AgentInput
SequentialInterruptIndex int
SequentialInterruptInfo *InterruptInfo
LoopIterations int
ParallelInterruptInfo map[int]*InterruptInfo
}
WorkflowInterruptInfo CheckpointSchema: persisted via InterruptInfo.Data (gob).
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package filesystem provides file system operations.
|
Package filesystem provides file system operations. |
|
Package internal provides adk internal utils.
|
Package internal provides adk internal utils. |
|
middlewares
|
|
|
agentsmd
Package agentsmd provides a middleware that automatically injects Agents.md file contents into model input messages.
|
Package agentsmd provides a middleware that automatically injects Agents.md file contents into model input messages. |
|
dynamictool/toolsearch
Package toolsearch provides tool search middleware.
|
Package toolsearch provides tool search middleware. |
|
filesystem
Package filesystem provides middlewares.
|
Package filesystem provides middlewares. |
|
patchtoolcalls
Package patchtoolcalls provides a middleware that patches dangling tool calls in the message history.
|
Package patchtoolcalls provides a middleware that patches dangling tool calls in the message history. |
|
reduction
Package reduction provides middlewares to trim context and clear tool results.
|
Package reduction provides middlewares to trim context and clear tool results. |
|
reduction/internal
Package internal provides middlewares to trim context and clear tool results.
|
Package internal provides middlewares to trim context and clear tool results. |
|
skill
Package skill provides a Skill middleware, types, and a local filesystem backend.
|
Package skill provides a Skill middleware, types, and a local filesystem backend. |
|
summarization
Package summarization provides a middleware that automatically summarizes conversation history when token count exceeds the configured threshold.
|
Package summarization provides a middleware that automatically summarizes conversation history when token count exceeds the configured threshold. |
|
prebuilt
|
|
|
deep
Package deep provides a prebuilt agent with deep task orchestration.
|
Package deep provides a prebuilt agent with deep task orchestration. |
|
planexecute
Package planexecute implements a plan–execute–replan style agent.
|
Package planexecute implements a plan–execute–replan style agent. |
|
supervisor
Package supervisor implements the supervisor pattern for multi-agent systems, where a central agent coordinates a set of sub-agents.
|
Package supervisor implements the supervisor pattern for multi-agent systems, where a central agent coordinates a set of sub-agents. |