agent_tool.go

Overview

The agent_tool.go file defines the Agent Tool, a specialized tool implementation that enables an AI agent to invoke another agent as a sub-agent within a compositional workflow. This facilitates hierarchical agent delegation, allowing complex AI workflows by composing multiple agents with distinct capabilities.

The core function of this tool is to wrap a sub-agent and expose it as a callable tool with a well-defined interface, including input/output schemas. When invoked, it creates a new session context for the sub-agent, runs it via an internal runner, and returns the sub-agent's output. This file is part of the broader Agent Tools and Tooling System for modular tool-based agent extensibility.


Types and Structures

agentTool struct

type agentTool struct {
	agent             agent.Agent
	skipSummarization bool
}

Config struct

type Config struct {
	SkipSummarization bool
}

Functions and Methods

New

func New(agent agent.Agent, cfg *Config) tool.Tool

Name

func (t *agentTool) Name() string

Description

func (t *agentTool) Description() string

IsLongRunning

func (t *agentTool) IsLongRunning() bool

Declaration

func (t *agentTool) Declaration() *genai.FunctionDeclaration

Run

func (t *agentTool) Run(toolCtx tool.Context, args any) (map[string]any, error)

ProcessRequest

func (t *agentTool) ProcessRequest(ctx tool.Context, req *model.LLMRequest) error

Important Implementation Details and Algorithms


Interactions with Other System Components


Usage Example

// Create a sub-agent tool wrapping an existing agent instance.
subAgentTool := agenttool.New(mySubAgent, &agenttool.Config{
    SkipSummarization: true,
})

// Add to LLM request before generation.
err := subAgentTool.ProcessRequest(toolCtx, llmRequest)
if err != nil {
    // handle error
}

// Run the sub-agent tool with input arguments.
output, err := subAgentTool.Run(toolCtx, map[string]any{
    "request": "Analyze this data.",
})
if err != nil {
    // handle error
}
fmt.Println("Sub-agent output:", output)

Visual Diagram

classDiagram
class agentTool {
-agent: agent.Agent
-skipSummarization: bool
+Name(): string
+Description(): string
+IsLongRunning(): bool
+Declaration(): genai.FunctionDeclaration
+Run(tool.Context, any) map[string]any
+ProcessRequest(tool.Context, *model.LLMRequest) error
}
agentTool ..> agent.Agent : wraps >
agentTool ..> tool.Tool : implements >
agentTool ..> session.Service : uses >
agentTool ..> runner.Runner : uses >
agentTool ..> genai.FunctionDeclaration : produces >

References