adk

package
v0.5.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 16, 2025 License: Apache-2.0 Imports: 23 Imported by: 26

Documentation

Index

Constants

View Source
const (
	TransferToAgentToolName = "transfer_to_agent"
	TransferToAgentToolDesc = "Transfer the question to another agent."
)
View Source
const (
	TransferToAgentInstruction = `` /* 268-byte string literal not displayed */

)

Variables

View Source
var ErrExceedMaxIterations = errors.New("exceeds max iterations")
View Source
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

func AddSessionValue(ctx context.Context, key string, value any)

func AddSessionValues

func AddSessionValues(ctx context.Context, kvs map[string]any)

func ClearRunCtx

func ClearRunCtx(ctx context.Context) context.Context

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 GenTransferMessages

func GenTransferMessages(_ context.Context, destAgentName string) (Message, Message)

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)

func GetSessionValue

func GetSessionValue(ctx context.Context, key string) (any, bool)

func GetSessionValues

func GetSessionValues(ctx context.Context) map[string]any

func NewAgentTool

func NewAgentTool(_ context.Context, agent Agent, options ...AgentToolOption) tool.BaseTool

func NewAsyncIteratorPair

func NewAsyncIteratorPair[T any]() (*AsyncIterator[T], *AsyncGenerator[T])

func SendToolGenAction

func SendToolGenAction(ctx context.Context, toolName string, action *AgentAction) error

Types

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

func AgentWithOptions

func AgentWithOptions(ctx context.Context, agent Agent, opts ...AgentOption) Agent

func NewLoopAgent

func NewLoopAgent(ctx context.Context, config *LoopAgentConfig) (Agent, error)

func NewParallelAgent

func NewParallelAgent(ctx context.Context, config *ParallelAgentConfig) (Agent, error)

func NewSequentialAgent

func NewSequentialAgent(ctx context.Context, config *SequentialAgentConfig) (Agent, error)

func SetSubAgents

func SetSubAgents(ctx context.Context, agent Agent, subAgents []Agent) (Agent, error)

type AgentAction

type AgentAction struct {
	Exit bool

	Interrupted *InterruptInfo

	TransferToAgent *TransferToAgentAction

	CustomizedAction any
}

func NewExitAction

func NewExitAction() *AgentAction

func NewTransferToAgentAction

func NewTransferToAgentAction(destAgentName string) *AgentAction

type AgentEvent

type AgentEvent struct {
	AgentName string

	RunPath []RunStep

	Output *AgentOutput

	Action *AgentAction

	Err error
}

func EventFromMessage

func EventFromMessage(msg Message, msgStream MessageStream,
	role schema.RoleType, toolName string) *AgentEvent

type AgentInput

type AgentInput struct {
	Messages        []Message
	EnableStreaming bool
}

type AgentOption

type AgentOption func(options *flowAgent)

func WithDisallowTransferToParent

func WithDisallowTransferToParent() AgentOption

func WithHistoryRewriter

func WithHistoryRewriter(h HistoryRewriter) AgentOption

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

func WithChatModelOptions

func WithChatModelOptions(opts []model.Option) AgentRunOption

func WithCheckPointID

func WithCheckPointID(id string) AgentRunOption

func WithHistoryModifier

func WithHistoryModifier(f func(context.Context, []Message) []Message) AgentRunOption

func WithSessionValues

func WithSessionValues(v map[string]any) AgentRunOption

func WithSkipTransferMessages

func WithSkipTransferMessages() AgentRunOption

func WithToolOptions

func WithToolOptions(opts []tool.Option) AgentRunOption

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

func WithFullChatHistoryAsInput

func WithFullChatHistoryAsInput() AgentToolOption

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 ChatModelAgent

type ChatModelAgent struct {
	// contains filtered or unexported fields
}

func NewChatModelAgent

func NewChatModelAgent(_ context.Context, config *ChatModelAgentConfig) (*ChatModelAgent, error)

func (*ChatModelAgent) Description

func (a *ChatModelAgent) Description(_ context.Context) string

func (*ChatModelAgent) Name

func (a *ChatModelAgent) Name(_ context.Context) 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 (*ChatModelAgent) Run

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 model.ToolCallingChatModel

	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
}

type ChatModelAgentInterruptInfo

type ChatModelAgentInterruptInfo struct {
	Info *compose.InterruptInfo
	Data []byte
}

type DeterministicTransferConfig

type DeterministicTransferConfig struct {
	Agent        Agent
	ToAgentNames []string
}

type ExitTool

type ExitTool struct{}

func (ExitTool) Info

func (et ExitTool) Info(_ context.Context) (*schema.ToolInfo, error)

func (ExitTool) InvokableRun

func (et ExitTool) InvokableRun(ctx context.Context, argumentsInJSON string, _ ...tool.Option) (string, error)

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 HistoryEntry

type HistoryEntry struct {
	IsUserInput bool
	AgentName   string
	Message     Message
}

type HistoryRewriter

type HistoryRewriter func(ctx context.Context, entries []*HistoryEntry) ([]Message, error)

type InterruptInfo

type InterruptInfo struct {
	Data any
}

type LoopAgentConfig

type LoopAgentConfig struct {
	Name        string
	Description string
	SubAgents   []Agent

	MaxIterations int
}

type Message

type Message = *schema.Message

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 OnSubAgents

type OnSubAgents interface {
	OnSetSubAgents(ctx context.Context, subAgents []Agent) error
	OnSetAsSubAgent(ctx context.Context, parent Agent) error

	OnDisallowTransferToParent(ctx context.Context) error
}

type ParallelAgentConfig

type ParallelAgentConfig struct {
	Name        string
	Description string
	SubAgents   []Agent
}

type ResumableAgent

type ResumableAgent interface {
	Agent

	Resume(ctx context.Context, info *ResumeInfo, opts ...AgentRunOption) *AsyncIterator[*AgentEvent]
}

type ResumeInfo

type ResumeInfo struct {
	EnableStreaming bool
	*InterruptInfo
}

type RunStep

type RunStep struct {
	// contains filtered or unexported fields
}

func (*RunStep) Equals

func (r *RunStep) Equals(r1 RunStep) bool

func (*RunStep) GobDecode

func (r *RunStep) GobDecode(b []byte) error

func (*RunStep) GobEncode

func (r *RunStep) GobEncode() ([]byte, error)

func (*RunStep) String

func (r *RunStep) String() string

type Runner

type Runner struct {
	// contains filtered or unexported fields
}

func NewRunner

func NewRunner(_ context.Context, conf RunnerConfig) *Runner

func (*Runner) Query

func (r *Runner) Query(ctx context.Context,
	query string, opts ...AgentRunOption) *AsyncIterator[*AgentEvent]

func (*Runner) Resume

func (r *Runner) Resume(ctx context.Context, checkPointID string, opts ...AgentRunOption) (*AsyncIterator[*AgentEvent], error)

func (*Runner) Run

func (r *Runner) Run(ctx context.Context, messages []Message,
	opts ...AgentRunOption) *AsyncIterator[*AgentEvent]

type RunnerConfig

type RunnerConfig struct {
	Agent           Agent
	EnableStreaming bool

	CheckPointStore compose.CheckPointStore
}

type SequentialAgentConfig

type SequentialAgentConfig struct {
	Name        string
	Description string
	SubAgents   []Agent
}

type State

type State struct {
	Messages []Message

	ReturnDirectlyToolCallID string

	ToolGenActions map[string]*AgentAction

	AgentName string

	AgentToolInterruptData map[string]*agentToolInterruptInfo

	RemainingIterations int
}

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
}

type TransferToAgentAction

type TransferToAgentAction struct {
	DestAgentName string
}

type WorkflowInterruptInfo

type WorkflowInterruptInfo struct {
	OrigInput *AgentInput

	SequentialInterruptIndex int
	SequentialInterruptInfo  *InterruptInfo

	LoopIterations int

	ParallelInterruptInfo map[int]*InterruptInfo
}

Directories

Path Synopsis
prebuilt

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL