tools

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package tools provides tool interfaces, registry, and argument parsing for AI tool calling.

Index

Constants

This section is empty.

Variables

View Source
var ErrDuplicateTool = errors.New("tool already registered")

ErrDuplicateTool is returned when attempting to register a tool with a name that is already registered.

Functions

func ParseArgs

func ParseArgs[T any](call core.ToolCall) (*T, error)

ParseArgs parses tool call arguments into a typed struct. It unmarshals the JSON arguments from the ToolCall into the target type T.

Example:

type WeatherArgs struct {
    Location string `json:"location"`
    Unit     string `json:"unit"`
}

args, err := tools.ParseArgs[WeatherArgs](toolCall)
if err != nil {
    return nil, err
}
// Use args.Location, args.Unit

Types

type Registry

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

Registry manages a collection of tools indexed by name. Registry is safe for concurrent use.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates a new empty tool registry.

func (*Registry) Get

func (r *Registry) Get(name string) (Tool, bool)

Get retrieves a tool by name. Returns the tool and true if found, or nil and false if not found.

func (*Registry) List

func (r *Registry) List() []Tool

List returns all registered tools. The returned slice is a copy and safe to modify.

func (*Registry) Register

func (r *Registry) Register(t Tool) error

Register adds a tool to the registry. Returns ErrDuplicateTool if a tool with the same name is already registered.

type Tool

type Tool interface {
	// Name returns the unique identifier for this tool.
	Name() string

	// Description returns a human-readable description of what this tool does.
	// This is provided to the AI model to help it decide when to use the tool.
	Description() string

	// Schema returns the JSON Schema that describes the tool's parameters.
	Schema() ToolSchema

	// Call executes the tool with the given arguments.
	// The args parameter contains the raw JSON arguments from the model.
	// Returns the tool's result or an error if execution fails.
	Call(ctx context.Context, args json.RawMessage) (any, error)
}

Tool defines the interface for AI-callable tools. Tools provide a schema for argument validation and a Call method for execution.

Any type implementing Tool also satisfies core.Tool (which requires only Name and Description), allowing tools to be used with ChatRequest.Tools.

type ToolSchema

type ToolSchema struct {
	// JSONSchema is a valid JSON Schema object describing the tool's parameters.
	// Example: {"type": "object", "properties": {"location": {"type": "string"}}}
	JSONSchema json.RawMessage `json:"json_schema"`
}

ToolSchema describes the parameters a tool accepts. JSONSchema must be a valid JSON Schema object.

Jump to

Keyboard shortcuts

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