tool.go

Overview

The tool.go file defines the foundational interfaces and types for the modular tooling system within the agent framework. It specifies abstractions for tools—discrete functional components callable by an agent—and their associated invocation context and grouping constructs.

Tools encapsulate specific tasks or operations that an agent can invoke during execution, enabling extensible integration of functionalities such as external API calls, sub-agent invocation, artifact loading, or other domain-specific capabilities.

This file provides the core contracts that all tools and tool collections must implement to participate in the agent's tooling ecosystem, establishing the interaction patterns between tools, the agent's runtime context, and session state.

Detailed Explanations

Interfaces

Tool

type Tool interface {
	Name() string
	Description() string
	IsLongRunning() bool
}

Context

type Context interface {
	agent.CallbackContext
	FunctionCallID() string
	Actions() *session.EventActions
	SearchMemory(context.Context, string) (*memory.SearchResponse, error)
}

Toolset

type Toolset interface {
	Name() string
	Tools(ctx agent.ReadonlyContext) ([]Tool, error)
}

Predicate and StringPredicate

type Predicate func(ctx agent.ReadonlyContext, tool Tool) bool
func StringPredicate(allowedTools []string) Predicate

Important Implementation Details

Interaction with Other System Components

Visual Diagram: Tooling System Structure in tool.go

classDiagram
class Tool {
+Name()
+Description()
+IsLongRunning()
}
class Context {
+FunctionCallID()
+Actions()
+SearchMemory()
}
class Toolset {
+Name()
+Tools()
}
class Predicate
class StringPredicate
Tool <|.. Toolset : provides >
Context <|.. Tool : passed to >
Predicate <.. StringPredicate : creates >

This diagram illustrates the core interfaces and their relationships:


Usage Examples and Scenarios

Implementing a Custom Tool

To implement a custom tool, define a struct satisfying the Tool interface:

type MyTool struct {}

func (t *MyTool) Name() string {
    return "MyCustomTool"
}

func (t *MyTool) Description() string {
    return "Performs a specialized operation."
}

func (t *MyTool) IsLongRunning() bool {
    return false
}

This tool can then be included in a Toolset or registered directly with an agent.

Filtering Tools with StringPredicate

When providing tools dynamically, a Predicate can be used to restrict exposure:

allowed := []string{"MyCustomTool", "GoogleSearch"}
filter := StringPredicate(allowed)

tools, err := toolset.Tools(ctx)
filtered := []Tool{}
for _, tool := range tools {
    if filter(ctx, tool) {
        filtered = append(filtered, tool)
    }
}

Accessing Invocation Context in a Tool

Within long-running or stateful tools, the Context interface allows:


References to Related Topics