tool

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultFileHistory = NewFileHistory(100)

DefaultFileHistory is the global history instance used by write tools.

Functions

func ExtractFilePath added in v0.2.0

func ExtractFilePath(input json.RawMessage) string

ExtractFilePath is a helper to extract file_path from common tool inputs.

func ListUndoHistory added in v0.3.0

func ListUndoHistory() string

ListUndoHistory returns a formatted string of all available undo operations.

Types

type BashTool

type BashTool struct {
	WorkDir string
}

BashTool executes shell commands.

func (*BashTool) Description

func (t *BashTool) Description() string

func (*BashTool) Execute

func (t *BashTool) Execute(ctx context.Context, input json.RawMessage) (Result, error)

func (*BashTool) InputSchema

func (t *BashTool) InputSchema() anthropic.ToolInputSchemaParam

func (*BashTool) Name

func (t *BashTool) Name() string

func (*BashTool) NormalizeInput added in v0.3.0

func (t *BashTool) NormalizeInput(input json.RawMessage) json.RawMessage

NormalizeInput strips redundant "cd <workdir> &&" prefixes from bash commands. This ensures permission checks and execution see the actual command being run.

type DiffTool added in v0.3.0

type DiffTool struct{}

DiffTool compares two files or a file against provided content. It implements ParallelSafeTool since it only reads data.

func (*DiffTool) Description added in v0.3.0

func (t *DiffTool) Description() string

func (*DiffTool) Execute added in v0.3.0

func (t *DiffTool) Execute(_ context.Context, input json.RawMessage) (Result, error)

func (*DiffTool) InputSchema added in v0.3.0

func (t *DiffTool) InputSchema() anthropic.ToolInputSchemaParam

func (*DiffTool) IsParallelSafe added in v0.3.0

func (t *DiffTool) IsParallelSafe() bool

IsParallelSafe returns true since diff operations don't modify state.

func (*DiffTool) Name added in v0.3.0

func (t *DiffTool) Name() string

type EditTool

type EditTool struct {
	// History is the file history to record changes to. If nil, uses DefaultFileHistory.
	History *FileHistory
}

EditTool performs string-replacement edits on files. It implements FileAccessor to enable conflict detection.

func (*EditTool) Description

func (t *EditTool) Description() string

func (*EditTool) Execute

func (t *EditTool) Execute(_ context.Context, input json.RawMessage) (Result, error)

func (*EditTool) GetFilePath added in v0.2.0

func (t *EditTool) GetFilePath(input json.RawMessage) string

GetFilePath extracts the target file path from the input.

func (*EditTool) InputSchema

func (t *EditTool) InputSchema() anthropic.ToolInputSchemaParam

func (*EditTool) IsWriteOperation added in v0.2.0

func (t *EditTool) IsWriteOperation() bool

IsWriteOperation returns true since this tool modifies files.

func (*EditTool) Name

func (t *EditTool) Name() string

type FileAccessor added in v0.2.0

type FileAccessor interface {
	// GetFilePath extracts the target file path from the tool input.
	// Returns empty string if the tool doesn't target a specific file.
	GetFilePath(input json.RawMessage) string

	// IsWriteOperation returns true if this tool modifies the file system.
	IsWriteOperation() bool
}

FileAccessor is implemented by tools that access files, enabling conflict detection for parallel execution. Write operations to the same file must be serialized.

type FileChange added in v0.3.0

type FileChange struct {
	FilePath    string
	OldContent  []byte // nil means file didn't exist before
	Existed     bool   // whether the file existed before the change
	Timestamp   time.Time
	ToolName    string // which tool made the change
	Description string // human-readable description of what changed
}

FileChange represents a single file modification that can be undone.

type FileHistory added in v0.3.0

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

FileHistory tracks file changes for undo operations. It maintains a stack of changes per file, allowing multiple undos.

func NewFileHistory added in v0.3.0

func NewFileHistory(maxSize int) *FileHistory

NewFileHistory creates a new FileHistory with the given max size.

func (*FileHistory) Clear added in v0.3.0

func (h *FileHistory) Clear()

Clear removes all recorded changes.

func (*FileHistory) GetAllChanges added in v0.3.0

func (h *FileHistory) GetAllChanges() []FileChange

GetAllChanges returns all recorded changes, most recent first.

func (*FileHistory) GetChanges added in v0.3.0

func (h *FileHistory) GetChanges(filePath string) []FileChange

GetChanges returns the change history for a specific file, most recent first.

func (*FileHistory) Len added in v0.3.0

func (h *FileHistory) Len() int

Len returns the number of recorded changes.

func (*FileHistory) PopChange added in v0.3.0

func (h *FileHistory) PopChange(filePath string) *FileChange

PopChange removes and returns the most recent change for a file. Returns nil if no changes exist for the file.

func (*FileHistory) PopMostRecent added in v0.3.0

func (h *FileHistory) PopMostRecent() *FileChange

PopMostRecent removes and returns the most recent change across all files. Returns nil if no changes exist.

func (*FileHistory) RecordChange added in v0.3.0

func (h *FileHistory) RecordChange(filePath, toolName, description string) error

RecordChange saves the current state of a file before it is modified. If the file doesn't exist, it records that fact so undo can delete it.

type GitStatus added in v0.4.2

type GitStatus struct {
	Branch         string            `json:"branch"`
	Ahead          int               `json:"ahead,omitempty"`
	Behind         int               `json:"behind,omitempty"`
	Staged         []string          `json:"staged,omitempty"`
	Modified       []string          `json:"modified,omitempty"`
	Untracked      []string          `json:"untracked,omitempty"`
	Deleted        []string          `json:"deleted,omitempty"`
	Renamed        map[string]string `json:"renamed,omitempty"`
	HasConflicts   bool              `json:"has_conflicts"`
	IsClean        bool              `json:"is_clean"`
	RemoteTracking string            `json:"remote_tracking,omitempty"`
}

GitStatus represents the structured output of git status.

type GitTool added in v0.4.2

type GitTool struct {
	WorkDir string
}

GitTool provides git version control operations with enhanced safety and structure.

func (*GitTool) Description added in v0.4.2

func (t *GitTool) Description() string

func (*GitTool) Execute added in v0.4.2

func (t *GitTool) Execute(ctx context.Context, input json.RawMessage) (Result, error)

func (*GitTool) InputSchema added in v0.4.2

func (t *GitTool) InputSchema() anthropic.ToolInputSchemaParam

func (*GitTool) Name added in v0.4.2

func (t *GitTool) Name() string

type GlobTool

type GlobTool struct {
	WorkDir string
}

GlobTool searches for files matching a glob pattern. It implements ParallelSafeTool since it only reads directory listings.

func (*GlobTool) Description

func (t *GlobTool) Description() string

func (*GlobTool) Execute

func (t *GlobTool) Execute(_ context.Context, input json.RawMessage) (Result, error)

func (*GlobTool) InputSchema

func (t *GlobTool) InputSchema() anthropic.ToolInputSchemaParam

func (*GlobTool) IsParallelSafe added in v0.2.0

func (t *GlobTool) IsParallelSafe() bool

IsParallelSafe returns true since glob operations don't modify state.

func (*GlobTool) Name

func (t *GlobTool) Name() string

type GrepTool

type GrepTool struct {
	WorkDir string
}

GrepTool searches file contents for a regular expression. It implements ParallelSafeTool since it only reads file contents.

func (*GrepTool) Description

func (t *GrepTool) Description() string

func (*GrepTool) Execute

func (t *GrepTool) Execute(_ context.Context, input json.RawMessage) (Result, error)

func (*GrepTool) InputSchema

func (t *GrepTool) InputSchema() anthropic.ToolInputSchemaParam

func (*GrepTool) IsParallelSafe added in v0.2.0

func (t *GrepTool) IsParallelSafe() bool

IsParallelSafe returns true since grep operations don't modify state.

func (*GrepTool) Name

func (t *GrepTool) Name() string

type InputNormalizer added in v0.3.0

type InputNormalizer interface {
	NormalizeInput(input json.RawMessage) json.RawMessage
}

InputNormalizer is an optional interface for tools that need to normalize their input before permission checking and execution. For example, the bash tool strips redundant "cd <workdir> &&" prefixes since commands already run in the working directory.

type LSPTool added in v0.3.0

type LSPTool struct {
	WorkDir string
	Manager *lsp.Manager
}

LSPTool provides code navigation using language servers.

func (*LSPTool) Close added in v0.3.0

func (t *LSPTool) Close() error

Close cleans up the LSP manager.

func (*LSPTool) Description added in v0.3.0

func (t *LSPTool) Description() string

Description returns a description of the tool.

func (*LSPTool) Execute added in v0.3.0

func (t *LSPTool) Execute(ctx context.Context, input json.RawMessage) (Result, error)

Execute runs the LSP operation.

func (*LSPTool) InputSchema added in v0.3.0

func (t *LSPTool) InputSchema() anthropic.ToolInputSchemaParam

InputSchema returns the JSON schema for the tool input.

func (*LSPTool) IsParallelSafe added in v0.3.0

func (t *LSPTool) IsParallelSafe() bool

IsParallelSafe returns true because LSP operations are read-only.

func (*LSPTool) Name added in v0.3.0

func (t *LSPTool) Name() string

Name returns the tool name.

type ListDirTool added in v0.3.0

type ListDirTool struct {
	WorkDir string
}

ListDirTool lists directory contents. It implements ParallelSafeTool since it only reads data.

func (*ListDirTool) Description added in v0.3.0

func (t *ListDirTool) Description() string

func (*ListDirTool) Execute added in v0.3.0

func (t *ListDirTool) Execute(_ context.Context, input json.RawMessage) (Result, error)

func (*ListDirTool) InputSchema added in v0.3.0

func (t *ListDirTool) InputSchema() anthropic.ToolInputSchemaParam

func (*ListDirTool) IsParallelSafe added in v0.3.0

func (t *ListDirTool) IsParallelSafe() bool

IsParallelSafe returns true since list operations don't modify state.

func (*ListDirTool) Name added in v0.3.0

func (t *ListDirTool) Name() string

type MoveTool added in v0.2.0

type MoveTool struct{}

MoveTool moves or renames files and directories.

func (*MoveTool) Description added in v0.2.0

func (t *MoveTool) Description() string

func (*MoveTool) Execute added in v0.2.0

func (t *MoveTool) Execute(_ context.Context, input json.RawMessage) (Result, error)

func (*MoveTool) InputSchema added in v0.2.0

func (t *MoveTool) InputSchema() anthropic.ToolInputSchemaParam

func (*MoveTool) Name added in v0.2.0

func (t *MoveTool) Name() string

type MultiReadTool added in v0.3.0

type MultiReadTool struct{}

MultiReadTool reads multiple files in a single call. It implements ParallelSafeTool since it only reads data.

func (*MultiReadTool) Description added in v0.3.0

func (t *MultiReadTool) Description() string

func (*MultiReadTool) Execute added in v0.3.0

func (t *MultiReadTool) Execute(_ context.Context, input json.RawMessage) (Result, error)

func (*MultiReadTool) InputSchema added in v0.3.0

func (t *MultiReadTool) InputSchema() anthropic.ToolInputSchemaParam

func (*MultiReadTool) IsParallelSafe added in v0.3.0

func (t *MultiReadTool) IsParallelSafe() bool

IsParallelSafe returns true since read operations don't modify state.

func (*MultiReadTool) Name added in v0.3.0

func (t *MultiReadTool) Name() string

type ParallelSafeTool added in v0.2.0

type ParallelSafeTool interface {
	Tool
	IsParallelSafe() bool
}

ParallelSafeTool indicates a tool can safely run concurrently with other parallel-safe tools. Tools that only read data (e.g., read, glob, grep) should implement this interface.

type ProgressUpdate added in v0.2.0

type ProgressUpdate struct {
	TotalTasks     int
	CompletedTasks int
	InProgress     []string // Tool names currently executing
}

ProgressUpdate reports progress during parallel tool execution.

type ReadTool

type ReadTool struct{}

ReadTool reads file contents with optional offset and limit. It implements ParallelSafeTool since it only reads data.

func (*ReadTool) Description

func (t *ReadTool) Description() string

func (*ReadTool) Execute

func (t *ReadTool) Execute(_ context.Context, input json.RawMessage) (Result, error)

func (*ReadTool) InputSchema

func (t *ReadTool) InputSchema() anthropic.ToolInputSchemaParam

func (*ReadTool) IsParallelSafe added in v0.2.0

func (t *ReadTool) IsParallelSafe() bool

IsParallelSafe returns true since read operations don't modify state.

func (*ReadTool) Name

func (t *ReadTool) Name() string

type Registry

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

Registry manages the set of tools available to the agent.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates a new empty tool registry.

func (*Registry) List

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

List returns all registered tools in registration order.

func (*Registry) Lookup

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

Lookup returns the tool with the given name, or nil if not found.

func (*Registry) Register

func (r *Registry) Register(t Tool) error

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

func (*Registry) ToolParams

func (r *Registry) ToolParams() []anthropic.ToolUnionParam

ToolParams returns the Anthropic API tool parameter definitions for all registered tools.

type Result

type Result struct {
	Output  string
	IsError bool
}

Result holds the output from a tool execution.

type TaskResult added in v0.2.0

type TaskResult struct {
	ID     string
	Name   string
	Result Result
	Err    error
}

TaskResult holds the outcome of a tool execution.

type TodoTool added in v0.3.0

type TodoTool struct {
	Store *todo.Store
}

TodoTool manages the task list for tracking progress.

func (*TodoTool) Description added in v0.3.0

func (t *TodoTool) Description() string

func (*TodoTool) Execute added in v0.3.0

func (t *TodoTool) Execute(_ context.Context, input json.RawMessage) (Result, error)

func (*TodoTool) InputSchema added in v0.3.0

func (t *TodoTool) InputSchema() anthropic.ToolInputSchemaParam

func (*TodoTool) IsParallelSafe added in v0.3.0

func (t *TodoTool) IsParallelSafe() bool

IsParallelSafe returns false since todo updates should be sequential.

func (*TodoTool) Name added in v0.3.0

func (t *TodoTool) Name() string

type Tool

type Tool interface {
	Name() string
	Description() string
	InputSchema() anthropic.ToolInputSchemaParam
	Execute(ctx context.Context, input json.RawMessage) (Result, error)
}

Tool defines the interface that all agent tools must implement.

type ToolCall added in v0.2.0

type ToolCall struct {
	ID    string
	Name  string
	Input json.RawMessage
}

ToolCall represents a single tool invocation request.

type ToolExecutor added in v0.2.0

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

ToolExecutor orchestrates tool execution, running parallel-safe tools concurrently while serializing conflicting operations.

func NewToolExecutor added in v0.2.0

func NewToolExecutor(registry *Registry, workers int) *ToolExecutor

NewToolExecutor creates a new executor with the specified number of workers.

func (*ToolExecutor) CanParallelize added in v0.2.0

func (e *ToolExecutor) CanParallelize(t Tool) bool

CanParallelize checks if a tool is safe for parallel execution.

func (*ToolExecutor) ExecuteTools added in v0.2.0

func (e *ToolExecutor) ExecuteTools(ctx context.Context, calls []ToolCall, progressCh chan<- ProgressUpdate) ([]TaskResult, error)

ExecuteTools runs the given tool calls, parallelizing where safe. Tools are grouped by conflict detection: parallel-safe read operations run concurrently, while write operations to the same file are serialized. Results are returned in the same order as the input calls.

type ToolTask added in v0.2.0

type ToolTask struct {
	Call     ToolCall
	Tool     Tool
	Ctx      context.Context
	ResultCh chan<- TaskResult
}

ToolTask is an internal structure for the worker pool.

type TreeTool added in v0.3.0

type TreeTool struct {
	WorkDir string
}

TreeTool displays directory structure as a tree. It implements ParallelSafeTool since it only reads data.

func (*TreeTool) Description added in v0.3.0

func (t *TreeTool) Description() string

func (*TreeTool) Execute added in v0.3.0

func (t *TreeTool) Execute(_ context.Context, input json.RawMessage) (Result, error)

func (*TreeTool) InputSchema added in v0.3.0

func (t *TreeTool) InputSchema() anthropic.ToolInputSchemaParam

func (*TreeTool) IsParallelSafe added in v0.3.0

func (t *TreeTool) IsParallelSafe() bool

IsParallelSafe returns true since tree operations don't modify state.

func (*TreeTool) Name added in v0.3.0

func (t *TreeTool) Name() string

type UndoTool added in v0.3.0

type UndoTool struct {
	// History is the file history to undo from. If nil, uses DefaultFileHistory.
	History *FileHistory
}

UndoTool reverts recent file changes made by write and edit tools.

func (*UndoTool) Description added in v0.3.0

func (t *UndoTool) Description() string

func (*UndoTool) Execute added in v0.3.0

func (t *UndoTool) Execute(_ context.Context, input json.RawMessage) (Result, error)

func (*UndoTool) InputSchema added in v0.3.0

func (t *UndoTool) InputSchema() anthropic.ToolInputSchemaParam

func (*UndoTool) Name added in v0.3.0

func (t *UndoTool) Name() string

type WebFetchTool added in v0.3.0

type WebFetchTool struct{}

WebFetchTool fetches content from a URL. It implements ParallelSafeTool since it only reads external data.

func (*WebFetchTool) Description added in v0.3.0

func (t *WebFetchTool) Description() string

func (*WebFetchTool) Execute added in v0.3.0

func (t *WebFetchTool) Execute(ctx context.Context, input json.RawMessage) (Result, error)

func (*WebFetchTool) InputSchema added in v0.3.0

func (t *WebFetchTool) InputSchema() anthropic.ToolInputSchemaParam

func (*WebFetchTool) IsParallelSafe added in v0.3.0

func (t *WebFetchTool) IsParallelSafe() bool

IsParallelSafe returns true since fetch operations don't modify local state.

func (*WebFetchTool) Name added in v0.3.0

func (t *WebFetchTool) Name() string

type WebSearchTool added in v0.3.0

type WebSearchTool struct{}

WebSearchTool searches the web using DuckDuckGo. It implements ParallelSafeTool since it only reads external data.

func (*WebSearchTool) Description added in v0.3.0

func (t *WebSearchTool) Description() string

func (*WebSearchTool) Execute added in v0.3.0

func (t *WebSearchTool) Execute(ctx context.Context, input json.RawMessage) (Result, error)

func (*WebSearchTool) InputSchema added in v0.3.0

func (t *WebSearchTool) InputSchema() anthropic.ToolInputSchemaParam

func (*WebSearchTool) IsParallelSafe added in v0.3.0

func (t *WebSearchTool) IsParallelSafe() bool

IsParallelSafe returns true since search operations don't modify local state.

func (*WebSearchTool) Name added in v0.3.0

func (t *WebSearchTool) Name() string

type WorkerPool added in v0.2.0

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

WorkerPool manages a pool of workers for concurrent tool execution.

func NewWorkerPool added in v0.2.0

func NewWorkerPool(workers int) *WorkerPool

NewWorkerPool creates a new worker pool with the specified number of workers.

func (*WorkerPool) ExecuteBatch added in v0.2.0

func (p *WorkerPool) ExecuteBatch(ctx context.Context, tasks []ToolTask, progressCh chan<- ProgressUpdate) []TaskResult

ExecuteBatch runs multiple tool tasks concurrently, respecting the worker limit. Results are returned in the same order as the input tasks. The progressCh receives updates as tasks complete (can be nil to disable).

type WriteTool

type WriteTool struct {
	// History is the file history to record changes to. If nil, uses DefaultFileHistory.
	History *FileHistory
}

WriteTool writes content to a file, creating parent directories as needed. It implements FileAccessor to enable conflict detection.

func (*WriteTool) Description

func (t *WriteTool) Description() string

func (*WriteTool) Execute

func (t *WriteTool) Execute(_ context.Context, input json.RawMessage) (Result, error)

func (*WriteTool) GetFilePath added in v0.2.0

func (t *WriteTool) GetFilePath(input json.RawMessage) string

GetFilePath extracts the target file path from the input.

func (*WriteTool) InputSchema

func (t *WriteTool) InputSchema() anthropic.ToolInputSchemaParam

func (*WriteTool) IsWriteOperation added in v0.2.0

func (t *WriteTool) IsWriteOperation() bool

IsWriteOperation returns true since this tool modifies files.

func (*WriteTool) Name

func (t *WriteTool) Name() string

Jump to

Keyboard shortcuts

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