tool

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FilterTools

func FilterTools(defs []schema.ToolDef, names []string) []schema.ToolDef

FilterTools returns only the tools whose names are in the whitelist. If names is empty, all tools are returned.

func ToAIModelTools

func ToAIModelTools(defs []schema.ToolDef) []aimodel.Tool

ToAIModelTools converts tool definitions to the aimodel.Tool format for ChatRequest.

Types

type ExternalToolCaller

type ExternalToolCaller interface {
	CallTool(ctx context.Context, name, args string) (schema.ToolResult, error)
}

ExternalToolCaller calls tools that are not handled locally.

type Registry

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

Registry is a thread-safe in-memory tool registry.

func NewRegistry

func NewRegistry(opts ...RegistryOption) *Registry

NewRegistry creates an empty Registry.

func (*Registry) Execute

func (r *Registry) Execute(ctx context.Context, name, args string) (schema.ToolResult, error)

func (*Registry) Get

func (r *Registry) Get(name string) (schema.ToolDef, bool)

func (*Registry) List

func (r *Registry) List() []schema.ToolDef

List returns all registered tool definitions in a deterministic (name-sorted) order. Stable ordering keeps the Anthropic prompt-cache prefix (tools block) byte-identical across independent invocations, which is a prerequisite for cache hits — map iteration order would otherwise shuffle the prefix on every call.

func (*Registry) Merge

func (r *Registry) Merge(defs []schema.ToolDef)

func (*Registry) Register

func (r *Registry) Register(def schema.ToolDef, handler ToolHandler) error

func (*Registry) RegisterIfAbsent

func (r *Registry) RegisterIfAbsent(def schema.ToolDef, handler ToolHandler) error

RegisterIfAbsent atomically checks for duplicates and registers under a single write lock to avoid a TOCTOU race between a separate read-lock check and Register().

func (*Registry) SetExternalCaller

func (r *Registry) SetExternalCaller(c ExternalToolCaller)

SetExternalCaller sets the caller used for tools with no local handler. Prefer WithExternalCaller at construction time when possible.

func (*Registry) Unregister

func (r *Registry) Unregister(name string) error

type RegistryOption

type RegistryOption func(*Registry)

RegistryOption configures a Registry during construction.

func WithExternalCaller

func WithExternalCaller(c ExternalToolCaller) RegistryOption

WithExternalCaller sets the caller used for tools with no local handler.

type ToolExecutor

type ToolExecutor interface {
	Execute(ctx context.Context, name, args string) (schema.ToolResult, error)
}

ToolExecutor executes a named tool with the given arguments.

type ToolHandler

type ToolHandler func(ctx context.Context, name, args string) (schema.ToolResult, error)

ToolHandler is a function that handles a tool invocation. name is the tool name, args is the raw JSON arguments string.

type ToolRegistry

type ToolRegistry interface {
	ToolExecutor
	Register(def schema.ToolDef, handler ToolHandler) error
	Unregister(name string) error
	Get(name string) (schema.ToolDef, bool)
	List() []schema.ToolDef
	Merge(defs []schema.ToolDef) // merge external tool definitions (for MCP)
}

ToolRegistry manages tool definitions and their handlers.

type TruncatingToolRegistry added in v0.4.0

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

TruncatingToolRegistry wraps a ToolRegistry and truncates tool results that exceed maxTokens.

TruncatingToolRegistry is safe for concurrent use if the underlying ToolRegistry is safe for concurrent use.

func NewTruncatingToolRegistry added in v0.4.0

func NewTruncatingToolRegistry(inner ToolRegistry, maxTokens int) *TruncatingToolRegistry

NewTruncatingToolRegistry creates a truncating wrapper. If maxTokens <= 0, no truncation is applied.

func (*TruncatingToolRegistry) Execute added in v0.4.0

func (t *TruncatingToolRegistry) Execute(ctx context.Context, name, args string) (schema.ToolResult, error)

Execute delegates to the inner registry, then truncates text content parts exceeding maxTokens.

func (*TruncatingToolRegistry) Get added in v0.4.0

Get delegates to the inner registry.

func (*TruncatingToolRegistry) List added in v0.4.0

List delegates to the inner registry.

func (*TruncatingToolRegistry) Merge added in v0.4.0

func (t *TruncatingToolRegistry) Merge(defs []schema.ToolDef)

Merge delegates to the inner registry.

func (*TruncatingToolRegistry) Register added in v0.4.0

func (t *TruncatingToolRegistry) Register(def schema.ToolDef, handler ToolHandler) error

Register delegates to the inner registry.

func (*TruncatingToolRegistry) Unregister added in v0.4.0

func (t *TruncatingToolRegistry) Unregister(name string) error

Unregister delegates to the inner registry.

Directories

Path Synopsis
Package todo implements the todo_write built-in tool: a session-scoped, in-memory task tracker that the LLM agent maintains during a multi-step task.
Package todo implements the todo_write built-in tool: a session-scoped, in-memory task tracker that the LLM agent maintains during a multi-step task.
Package toolkit provides shared utilities for file tools, including path validation, atomic file writes, file locking, and edit snippet generation.
Package toolkit provides shared utilities for file tools, including path validation, atomic file writes, file locking, and edit snippet generation.

Jump to

Keyboard shortcuts

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