Spaces:
Sleeping
Sleeping
File size: 5,690 Bytes
75bea1c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | from __future__ import annotations
"""Base classes for tool definitions and registry."""
from abc import ABC, abstractmethod
from dataclasses import dataclass, field
from typing import Any
from src.utils.exceptions import ToolExecutionError, ToolNotFoundError
from src.utils.logging import get_logger
logger = get_logger(__name__)
@dataclass
class ToolResult:
"""Result from a tool execution."""
success: bool
data: Any
error: str | None = None
@classmethod
def ok(cls, data: Any) -> "ToolResult":
"""Create a successful result."""
return cls(success=True, data=data, error=None)
@classmethod
def fail(cls, error: str) -> "ToolResult":
"""Create a failed result."""
return cls(success=False, data=None, error=error)
@dataclass
class ToolParameter:
"""Definition of a tool parameter."""
name: str
type: str
description: str
required: bool = True
default: Any = None
enum: list[str] | None = None
@dataclass
class Tool(ABC):
"""Abstract base class for tools."""
name: str
description: str
parameters: list[ToolParameter] = field(default_factory=list)
@abstractmethod
async def execute(self, **kwargs: Any) -> ToolResult:
"""Execute the tool with the given parameters.
Args:
**kwargs: Tool parameters
Returns:
ToolResult with success/failure and data
"""
pass
def to_openai_schema(self) -> dict[str, Any]:
"""Convert tool to OpenAI function calling schema.
Returns:
OpenAI-compatible tool definition
"""
properties = {}
required = []
for param in self.parameters:
prop: dict[str, Any] = {
"type": param.type,
"description": param.description,
}
if param.enum:
prop["enum"] = param.enum
properties[param.name] = prop
if param.required:
required.append(param.name)
return {
"type": "function",
"function": {
"name": self.name,
"description": self.description,
"parameters": {
"type": "object",
"properties": properties,
"required": required,
},
},
}
def validate_parameters(self, **kwargs: Any) -> tuple[bool, str | None]:
"""Validate that required parameters are provided.
Args:
**kwargs: Parameters to validate
Returns:
Tuple of (is_valid, error_message)
"""
for param in self.parameters:
if param.required and param.name not in kwargs:
return False, f"Missing required parameter: {param.name}"
return True, None
class ToolRegistry:
"""Registry for managing available tools."""
def __init__(self) -> None:
"""Initialize empty tool registry."""
self._tools: dict[str, Tool] = {}
def register(self, tool: Tool) -> None:
"""Register a tool.
Args:
tool: Tool instance to register
"""
self._tools[tool.name] = tool
logger.info(f"Registered tool: {tool.name}")
def get(self, name: str) -> Tool:
"""Get a tool by name.
Args:
name: Tool name
Returns:
Tool instance
Raises:
ToolNotFoundError: If tool not found
"""
if name not in self._tools:
raise ToolNotFoundError(f"Tool not found: {name}")
return self._tools[name]
def list_tools(self) -> list[Tool]:
"""List all registered tools.
Returns:
List of all tools
"""
return list(self._tools.values())
def get_schemas(self) -> list[dict[str, Any]]:
"""Get OpenAI schemas for all tools.
Returns:
List of tool schemas
"""
return [tool.to_openai_schema() for tool in self._tools.values()]
async def execute(self, tool_name: str, **kwargs: Any) -> ToolResult:
"""Execute a tool by name.
Args:
tool_name: Name of the tool to execute
**kwargs: Tool parameters
Returns:
ToolResult from execution
"""
tool = self.get(tool_name)
# Validate parameters
is_valid, error = tool.validate_parameters(**kwargs)
if not is_valid:
return ToolResult.fail(error or "Invalid parameters")
try:
logger.info(f"Executing tool: {tool_name} with params: {kwargs}")
result = await tool.execute(**kwargs)
logger.info(f"Tool {tool_name} completed: success={result.success}")
return result
except Exception as e:
logger.error(f"Tool {tool_name} failed: {e}")
return ToolResult.fail(str(e))
def create_default_registry() -> ToolRegistry:
"""Create a registry with all default tools.
Returns:
ToolRegistry with default tools registered
"""
from src.tools.web_search import WebSearchTool
from src.tools.web_scraper import WebScraperTool
from src.tools.calculator import CalculatorTool
from src.tools.datetime_tool import DateTimeTool
registry = ToolRegistry()
registry.register(WebSearchTool())
registry.register(WebScraperTool())
registry.register(CalculatorTool())
registry.register(DateTimeTool())
return registry
|