tool

package
v0.2.5 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package tool provides the tool system for LLM function-calling: Tool interface, Registry, concurrent execution, and schema building.

Index

Constants

View Source
const (
	// ScopeAgent marks a tool as available to all agents (default).
	ScopeAgent = "agent"
	// ScopePlatform marks a tool as internal to the CoPilot platform.
	// Platform tools are hidden from tool_list and the frontend ToolSelector,
	// but can still be referenced explicitly in an LLM node's tool_names.
	ScopePlatform = "platform"
)

Tool scope constants control visibility in tool_list and /api/tools. The scope is registry-level metadata and does NOT appear in model.ToolDefinition.

Variables

This section is empty.

Functions

This section is empty.

Types

type PropertyDef

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

PropertyDef describes a single JSON Schema property.

func ArrayProperty

func ArrayProperty(name, description string, items map[string]any) PropertyDef

ArrayProperty creates an array property with item schema.

func EnumProperty

func EnumProperty(name, typ, description string, values ...string) PropertyDef

EnumProperty creates a property restricted to a set of string values.

func ObjectProperty

func ObjectProperty(name, description string, properties map[string]any) PropertyDef

ObjectProperty creates an object property with nested properties schema.

func Property

func Property(name, typ, description string) PropertyDef

Property creates a simple typed property definition.

func PropertyWithDefault added in v0.1.1

func PropertyWithDefault(name, typ, description string, defaultVal any) PropertyDef

PropertyWithDefault creates a typed property with a default value.

type Registry

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

Registry is a thread-safe collection of Tools.

func NewRegistry

func NewRegistry(opts ...RegistryOption) *Registry

NewRegistry creates a new tool registry.

func (*Registry) Definitions

func (r *Registry) Definitions() []model.ToolDefinition

Definitions returns the ToolDefinition for every registered tool (all scopes).

func (*Registry) DefinitionsByScope

func (r *Registry) DefinitionsByScope(scope string) []model.ToolDefinition

DefinitionsByScope returns only the ToolDefinitions matching the given scope.

func (*Registry) Execute

func (r *Registry) Execute(ctx context.Context, call model.ToolCall) model.ToolResult

Execute runs a single tool call with OTel tracing and metrics. All errors (including tool-not-found) are returned as ToolResult with IsError=true, so callers never need to handle a separate error path.

func (*Registry) ExecuteAll

func (r *Registry) ExecuteAll(ctx context.Context, calls []model.ToolCall) []model.ToolResult

ExecuteAll runs multiple tool calls concurrently with a semaphore limiting parallelism. Results are returned in the same order as the input calls.

func (*Registry) Get

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

Get returns a tool by name.

func (*Registry) Len

func (r *Registry) Len() int

Len returns the number of registered tools.

func (*Registry) Names

func (r *Registry) Names() []string

Names returns the names of all registered tools.

func (*Registry) Register

func (r *Registry) Register(tool Tool)

Register adds a tool to the registry with the default scope (ScopeAgent).

func (*Registry) RegisterWithScope

func (r *Registry) RegisterWithScope(tool Tool, scope string)

RegisterWithScope adds a tool to the registry with the specified scope.

func (*Registry) ScopeOf

func (r *Registry) ScopeOf(name string) string

ScopeOf returns the scope of a registered tool, or ScopeAgent if not found.

func (*Registry) Unregister

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

Unregister removes a tool by name. Returns true if the tool existed.

type RegistryOption

type RegistryOption func(*Registry)

RegistryOption configures a Registry.

func WithExecTimeout

func WithExecTimeout(d time.Duration) RegistryOption

WithExecTimeout sets the default timeout for individual tool executions.

func WithMaxConcurrency

func WithMaxConcurrency(n int) RegistryOption

WithMaxConcurrency sets the maximum number of concurrent tool executions.

type SchemaBuilder

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

SchemaBuilder constructs a ToolDefinition using a fluent API.

func DefineSchema

func DefineSchema(name, description string, props ...PropertyDef) *SchemaBuilder

DefineSchema starts building a ToolDefinition with the given properties.

func (*SchemaBuilder) Build

func (b *SchemaBuilder) Build() model.ToolDefinition

Build returns the final ToolDefinition.

func (*SchemaBuilder) Required

func (b *SchemaBuilder) Required(names ...string) *SchemaBuilder

Required marks the given property names as required in the JSON Schema. Duplicate names are silently ignored.

type SelfTimeouter

type SelfTimeouter interface {
	SelfTimeout() bool
}

SelfTimeouter is an optional interface a Tool can implement to signal that it manages its own execution timeout internally (e.g. sandbox tools). When Registry detects this, it skips the default per-tool timeout wrapper so the tool's own timeout takes effect.

type Tool

type Tool interface {
	Definition() model.ToolDefinition
	Execute(ctx context.Context, arguments string) (string, error)
}

Tool is the interface that LLM-callable tools must implement.

func FuncTool

func FuncTool(def model.ToolDefinition, fn func(ctx context.Context, args string) (string, error)) Tool

FuncTool wraps a plain function as a Tool.

Jump to

Keyboard shortcuts

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