tools

package
v0.0.0-...-6acc9d1 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ErrNoWorkDir           toolError = "working directory not set"
	ErrPathOutsideWorkDir  toolError = "path is outside working directory"
	ErrPermissionDenied    toolError = "permission denied"
	ErrBashNotAllowed      toolError = "bash execution not allowed"
	ErrFileReadNotAllowed  toolError = "file read not allowed"
	ErrFileWriteNotAllowed toolError = "file write not allowed"
	ErrGitNotAllowed       toolError = "git operations not allowed"
	ErrGitHubNotAllowed    toolError = "github operations not allowed"
	ErrNetworkNotAllowed   toolError = "network operations not allowed"
)

Variables

View Source
var DefaultRegistry = NewRegistry()

DefaultRegistry is the global default registry.

Functions

func MustRegister

func MustRegister(tool Tool)

MustRegister adds a tool to the default registry and panics on error.

func Register

func Register(tool Tool) error

Register adds a tool to the default registry.

Types

type Permissions

type Permissions struct {
	// AllowBash allows executing bash commands.
	AllowBash bool

	// AllowFileRead allows reading files.
	AllowFileRead bool

	// AllowFileWrite allows writing files.
	AllowFileWrite bool

	// AllowGit allows git operations.
	AllowGit bool

	// AllowGitHub allows GitHub API operations.
	AllowGitHub bool

	// AllowNetwork allows network operations.
	AllowNetwork bool
}

Permissions defines what operations a tool is allowed to perform.

func DefaultPermissions

func DefaultPermissions() Permissions

DefaultPermissions returns permissions with all operations allowed.

func RestrictedPermissions

func RestrictedPermissions() Permissions

RestrictedPermissions returns permissions with only read operations allowed.

type Registry

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

Registry manages tool registration and lookup.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates a new tool registry.

func (*Registry) Clear

func (r *Registry) Clear()

Clear removes all tools from the registry.

func (*Registry) Count

func (r *Registry) Count() int

Count returns the number of registered tools.

func (*Registry) Get

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

Get retrieves a tool by name. Returns nil if the tool is not found.

func (*Registry) Has

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

Has checks if a tool exists in the registry.

func (*Registry) List

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

List returns all registered tools.

func (*Registry) MustRegister

func (r *Registry) MustRegister(tool Tool)

MustRegister adds a tool to the registry and panics on error.

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) error

Register adds a tool to the registry. Returns an error if a tool with the same name already exists.

type Tool

type Tool interface {
	// Name returns the unique name of the tool.
	Name() string

	// Description returns a human-readable description of what the tool does.
	Description() string

	// InputSchema returns the JSON Schema for the tool's input parameters.
	InputSchema() map[string]any

	// Execute runs the tool with the given input and returns the result.
	Execute(ctx context.Context, toolCtx *ToolContext, input map[string]any) (ToolResult, error)
}

Tool defines the interface for all tools available to the agent.

func Get

func Get(name string) Tool

Get retrieves a tool from the default registry by name.

func List

func List() []Tool

List returns all tools from the default registry.

type ToolContext

type ToolContext struct {
	// WorkDir is the working directory for tool execution.
	// File operations should be restricted to this directory.
	WorkDir string

	// Permissions defines what operations are allowed.
	Permissions Permissions

	// GitHubToken is the token for GitHub API operations.
	GitHubToken string

	// RepoOwner is the owner of the repository.
	RepoOwner string

	// RepoName is the name of the repository.
	RepoName string

	// Env contains environment variables available to tools.
	Env map[string]string

	// BashTimeout is the timeout for bash command execution in seconds.
	BashTimeout int
}

ToolContext provides execution context for tools.

func NewToolContext

func NewToolContext(workDir string) *ToolContext

NewToolContext creates a new tool context with the given working directory.

func (*ToolContext) CheckBash

func (c *ToolContext) CheckBash() error

CheckBash checks if bash execution is allowed.

func (*ToolContext) CheckFileRead

func (c *ToolContext) CheckFileRead() error

CheckFileRead checks if file read operations are allowed.

func (*ToolContext) CheckFileWrite

func (c *ToolContext) CheckFileWrite() error

CheckFileWrite checks if file write operations are allowed.

func (*ToolContext) CheckGit

func (c *ToolContext) CheckGit() error

CheckGit checks if git operations are allowed.

func (*ToolContext) CheckGitHub

func (c *ToolContext) CheckGitHub() error

CheckGitHub checks if GitHub operations are allowed.

func (*ToolContext) CheckNetwork

func (c *ToolContext) CheckNetwork() error

CheckNetwork checks if network operations are allowed.

func (*ToolContext) FileExists

func (c *ToolContext) FileExists(path string) bool

FileExists checks if a file exists at the given path.

func (*ToolContext) IsDir

func (c *ToolContext) IsDir(path string) bool

IsDir checks if the path is a directory.

func (*ToolContext) ResolvePath

func (c *ToolContext) ResolvePath(path string) string

ResolvePath resolves a path relative to the working directory. Unlike ValidatePath, this does not check if the path exists.

func (*ToolContext) ValidatePath

func (c *ToolContext) ValidatePath(path string) (string, error)

ValidatePath checks if the given path is within the working directory. Returns the cleaned absolute path if valid, or an error if the path is outside the working directory.

func (*ToolContext) WithBashTimeout

func (c *ToolContext) WithBashTimeout(seconds int) *ToolContext

WithBashTimeout sets the bash timeout and returns the context for chaining.

func (*ToolContext) WithEnv

func (c *ToolContext) WithEnv(key, value string) *ToolContext

WithEnv sets an environment variable and returns the context for chaining.

func (*ToolContext) WithGitHub

func (c *ToolContext) WithGitHub(token, owner, repo string) *ToolContext

WithGitHub sets GitHub credentials and returns the context for chaining.

func (*ToolContext) WithPermissions

func (c *ToolContext) WithPermissions(p Permissions) *ToolContext

WithPermissions sets the permissions and returns the context for chaining.

type ToolResult

type ToolResult struct {
	// Content is the output of the tool execution.
	Content string

	// IsError indicates if the execution resulted in an error.
	IsError bool

	// Metadata contains additional information about the execution.
	Metadata map[string]any
}

ToolResult represents the result of a tool execution.

func NewErrorResult

func NewErrorResult(err error) ToolResult

NewErrorResult creates an error tool result.

func NewErrorResultf

func NewErrorResultf(format string, args ...any) ToolResult

NewErrorResultf creates an error tool result with a formatted message.

func NewToolResult

func NewToolResult(content string) ToolResult

NewToolResult creates a successful tool result.

func (ToolResult) WithMetadata

func (r ToolResult) WithMetadata(key string, value any) ToolResult

WithMetadata adds metadata to a tool result.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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