tool

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package tool defines the Tool abstraction and a Registry. Built-in tools live in tool/builtin and self-register via init(); plugin-provided tools are added to a runtime Registry alongside the enabled built-ins. The agent sees only a *Registry, never the global built-in set directly.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterBuiltin

func RegisterBuiltin(t Tool)

RegisterBuiltin registers a compile-time built-in tool. Intended for init(). A duplicate name is logged to stderr and skipped — a duplicate is a compile-time wiring mistake, but crashing the binary is worse for the user.

Types

type Previewer

type Previewer interface {
	Preview(args json.RawMessage) (diff.Change, error)
}

Previewer is an optional capability a writer Tool may implement: given the same raw JSON args Execute would receive, compute the file change the call *would* make — without touching disk. A front-end uses it to show an approval card or a changed-files panel before the call runs (the permission gate, not Preview, decides whether it may proceed). Type-assert a Tool to Previewer to discover support; the file-writing built-ins implement it, most tools do not.

type Registry

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

Registry is a per-run set of tools: enabled built-ins plus plugin tools. Safe for concurrent use: reads (Get, Names, Schemas, Len) take RLock; writes (Add, RemovePrefix) take Lock. Supports hierarchical tool groups: when groups are empty (default), all tools are visible. When groups are set via ActivateGroups, only tools in those groups are returned by Schemas/Names/Get — cutting schema token costs.

Group switches are deferred: ActivateGroups sets pendingGroups, which is applied at the start of the next turn via ApplyPendingGroups. This keeps tool schemas byte-stable within a turn so DeepSeek's prefix cache stays warm.

func NewRegistry

func NewRegistry() *Registry

NewRegistry returns an empty registry.

func (*Registry) ActivateGroups

func (r *Registry) ActivateGroups(groups ...string)

ActivateGroups remembers the desired groups but does NOT apply them immediately. The switch takes effect at the start of the next turn (via ApplyPendingGroups) so tool schemas stay byte-stable within the current turn and DeepSeek's prefix cache stays warm across API calls.

func (*Registry) ActiveGroupNames

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

ActiveGroupNames returns the currently active group names.

func (*Registry) Add

func (r *Registry) Add(t Tool)

Add inserts (or replaces) a tool, preserving first-seen order. Determines the group from ToolGrouper if implemented, defaulting to "core".

func (*Registry) AddToGroup

func (r *Registry) AddToGroup(t Tool, group string)

AddToGroup inserts a tool with an explicit group override.

func (*Registry) AllNames

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

AllNames returns every registered tool name regardless of group visibility.

func (*Registry) ApplyPendingGroups

func (r *Registry) ApplyPendingGroups()

ApplyPendingGroups applies any group switch deferred by ActivateGroups. Called at the start of each turn (before the first stream call). Idempotent — a no-op when no pending switch exists.

func (*Registry) DeactivateGroups

func (r *Registry) DeactivateGroups(groups ...string)

DeactivateGroups hides groups entirely. After this only "core" tools remain.

func (*Registry) Get

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

Get looks up a tool by name. Returns false if the tool is not in the current active group set (or doesn't exist).

func (*Registry) GetAny

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

GetAny looks up a tool by name regardless of group visibility. Used internally for tool execution — the agent can still call a tool that was used in a previous turn even if its group is now inactive.

func (*Registry) Len

func (r *Registry) Len() int

Len returns the number of visible registered tools.

func (*Registry) Names

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

Names returns the visible registered tool names in insertion order.

func (*Registry) RemovePrefix

func (r *Registry) RemovePrefix(prefix string) int

RemovePrefix unregisters every tool whose name starts with prefix — used to drop an MCP server's "mcp__<server>__" namespace when it's disconnected — and returns the count removed.

func (*Registry) Schemas

func (r *Registry) Schemas() []provider.ToolSchema

Schemas exports tool definitions in insertion order for the provider, returning only tools in the currently active groups. Results are cached until the registry is mutated (Add/Remove/group change).

func (*Registry) SetGroup

func (r *Registry) SetGroup(name, group string)

SetGroup changes the group of an already-registered tool by name. Used by boot to apply hierarchical group assignments after init(). No-op for unknown names.

type RegistrySetGroups

type RegistrySetGroups interface {
	ToolRegistry
	ActivateGroups(groups ...string)
	ApplyPendingGroups()
	AllNames() []string
	ActiveGroupNames() []string
}

RegistrySetGroups is the interface for switching tool groups at runtime. Separated from ToolRegistry so only the boot wiring exposes it.

type Tool

type Tool interface {
	Name() string
	Description() string
	// Schema returns the JSON Schema for the tool's parameters.
	Schema() json.RawMessage
	// Execute parses the model-generated raw JSON args and returns result text
	// to feed back to the model.
	Execute(ctx context.Context, args json.RawMessage) (string, error)
	// ReadOnly reports whether the tool has no observable side effects on the
	// host. The agent parallelises a batch of tool calls only when every call
	// in the batch is ReadOnly; mixed batches stay sequential so write/read
	// ordering is preserved. bash and plugin tools must return false because
	// their effects can't be inferred statically from args.
	ReadOnly() bool
}

Tool is a capability the model can invoke.

func Builtins

func Builtins() []Tool

Builtins returns all registered built-in tools, sorted by name.

func LookupBuiltin

func LookupBuiltin(name string) (Tool, bool)

LookupBuiltin returns a registered built-in by name.

type ToolGrouper

type ToolGrouper interface {
	Group() string
}

ToolGrouper is an optional interface a Tool may implement to declare its group membership for hierarchical tool registration. Groups let the agent expose only a subset of tools to the model based on task complexity, saving ~50% schema tokens per turn.

Built-in groups (case-insensitive):

core      — always visible (read/write files, search, task, etc.)
advanced  — git, database, debug, deploy, desktop, etc.
knowledge — rag, semantic-search, code analysis
admin     — repo, make-tool, go-profile, etc.

Tools that don't implement ToolGrouper default to "core".

type ToolRegistry

type ToolRegistry interface {
	Get(name string) (Tool, bool)
	GetAny(name string) (Tool, bool) // lookup regardless of group visibility
	Names() []string
	Schemas() []provider.ToolSchema
	Len() int
}

ToolRegistry is the interface the agent (and anyone who needs to look up or enumerate tools) depends on. A *Registry implements it; tests can satisfy it with a fake rather than importing the concrete implementation.

Directories

Path Synopsis
Package builtin provides the built-in tools for the OK agent.
Package builtin provides the built-in tools for the OK agent.

Jump to

Keyboard shortcuts

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