Documentation
¶
Overview ¶
Package tools provides tool interfaces, registry, and argument parsing for AI tool calling.
Index ¶
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 (*Registry) Get ¶
Get retrieves a tool by name. Returns the tool and true if found, or nil and false if not found.
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.