toolmanager

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2025 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package toolmanager provides functionality for managing tools within the application. It handles loading tool definitions from files and managing their lifecycle.

Tools are defined using YAML configuration files and must implement the tool.Tool interface from the tool package. The toolmanager loads these definitions and creates the appropriate tool instances.

The toolmanager is responsible for:

  • Loading tool definitions from YAML files
  • Creating and managing tool instances
  • Providing access to tools by name
  • Maintaining the tool registry
  • Managing the exec tool as a special built-in tool

Example usage:

agent := agent.New(
	agent.WithLogger(logger),
	agent.WithConfig(cfg),
)

tm := toolmanager.New(
	toolmanager.WithLogger(logger),
	toolmanager.WithConfig(cfg),
	toolmanager.WithAgent(agent),
)

if err := tm.LoadTools(); err != nil {
	// Handle error
}

tools := tm.GetTools()

Tool definitions are loaded from YAML files and include:

  • Display name for UI presentation
  • Description of the tool's functionality
  • System prompt for AI interaction
  • Input parameters with validation schemas
  • Optional executable path for command-line tools

Tool Validation:

Each tool definition is validated to ensure:

  • Display name is provided and non-empty
  • Description is provided and non-empty
  • Input parameters have valid types and descriptions
  • System prompt is valid if provided
  • Executable path exists and is executable if specified

Exec Tool:

The exec tool is a special built-in tool that:

  • Is always loaded regardless of configuration
  • Provides direct command execution capabilities
  • Uses the shell specified in configuration
  • Has its own timeout configuration

Error Handling:

The package uses the following error constants:

  • ErrLoadingTools: Returned when tools cannot be loaded from directory
  • ErrLoadingTool: Returned when a specific tool fails to load
  • ErrParsingTool: Returned when tool YAML parsing fails
  • ErrToolNotFound: Returned when requested tool doesn't exist
  • ErrInvalidToolDefinition: Returned when tool definition is invalid

Thread Safety:

The toolmanager is safe for concurrent access:

  • Tool loading is synchronized
  • Tool access methods are safe for concurrent use
  • Tool instances are immutable after creation
  • The exec tool maintains its own thread safety

The toolmanager requires an agent to be provided for tool execution. The agent is responsible for running tool operations and managing their lifecycle.

The package uses JSON Schema for input validation and provides error handling for common failure scenarios such as missing tools or invalid configurations.

Index

Constants

View Source
const (
	// ErrLoadingTools is the error message for failed to load tools.
	ErrLoadingTools = "failed to load tools"
	// ErrLoadingTool is the error message for failed to load a specific tool.
	ErrLoadingTool = "failed to load tool"
	// ErrParsingTool is the error message for failed to parse a tool.
	ErrParsingTool = "failed to parse tool"
	// ErrToolNotFound is the error message for a tool not found.
	ErrToolNotFound = "tool not found"
	// ErrInvalidToolDefinition is the error message for an invalid tool definition.
	ErrInvalidToolDefinition = "invalid tool definition"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Manager

type Manager interface {
	// LoadTools loads the tools from the tool manager.
	LoadTools() error
	// GetTools returns all tools.
	GetTools() map[string]tool.Tool
	// GetTool returns a tool by name.
	GetTool(name string) (tool.Tool, error)
}

Manager is the interface for the tool manager.

type Option

type Option func(*ToolManager)

Option is a function that modifies the tool manager.

func WithAgent

func WithAgent(agent *agent.Agent) Option

WithAgent sets the agent for the tool manager.

func WithConfig

func WithConfig(cfg config.Configuration) Option

WithConfig sets the configuration for the tool manager.

func WithContext

func WithContext(ctx context.Context) Option

WithContext sets the context for the tool manager.

func WithDirectory

func WithDirectory(dir string) Option

WithDirectory sets the directory for the tool manager.

func WithLogger

func WithLogger(logger *slog.Logger) Option

WithLogger sets the logger for the tool manager.

type ToolManager

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

ToolManager is the tool manager.

func New

func New(opts ...Option) *ToolManager

New creates a new tool manager.

func (*ToolManager) GetTool

func (tm *ToolManager) GetTool(name string) (tool.Tool, error)

GetTool returns a tool by name.

func (*ToolManager) GetTools

func (tm *ToolManager) GetTools() map[string]tool.Tool

GetTools returns all tools.

func (*ToolManager) LoadTools

func (tm *ToolManager) LoadTools() error

LoadTools loads the tools from the tool manager.

Jump to

Keyboard shortcuts

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