File size: 6,223 Bytes
7b2787b |
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 208 209 210 211 212 213 214 215 216 217 218 219 |
"""
Tool Registry for Workflow Engine.
The tool registry maintains a collection of callable tools that
workflow nodes can use. Tools are simple Python functions that
perform specific operations.
"""
from typing import Any, Callable, Dict, List, Optional
from dataclasses import dataclass, field
import functools
import inspect
import logging
logger = logging.getLogger(__name__)
@dataclass
class Tool:
"""
A registered tool.
Attributes:
name: Unique identifier for the tool
func: The callable function
description: Human-readable description
parameters: Parameter descriptions
"""
name: str
func: Callable
description: str = ""
parameters: Dict[str, str] = field(default_factory=dict)
def __call__(self, *args, **kwargs) -> Any:
"""Call the tool function."""
return self.func(*args, **kwargs)
def to_dict(self) -> Dict[str, Any]:
"""Serialize tool metadata."""
return {
"name": self.name,
"description": self.description,
"parameters": self.parameters,
}
class ToolRegistry:
"""
Registry for workflow tools.
Tools are simple Python functions that nodes can call to perform
specific operations. The registry allows dynamic registration
and lookup of tools.
Usage:
registry = ToolRegistry()
@registry.register("my_tool")
def my_tool(data: str) -> dict:
return {"result": data.upper()}
# Later
tool = registry.get("my_tool")
result = tool("hello")
"""
def __init__(self):
self._tools: Dict[str, Tool] = {}
def register(
self,
name: Optional[str] = None,
description: str = "",
parameters: Optional[Dict[str, str]] = None
) -> Callable:
"""
Decorator to register a function as a tool.
Args:
name: Tool name (defaults to function name)
description: Tool description (defaults to docstring)
parameters: Parameter descriptions
Returns:
Decorator function
"""
def decorator(func: Callable) -> Callable:
tool_name = name or func.__name__
tool_desc = description or func.__doc__ or ""
# Extract parameters from signature if not provided
params = parameters or {}
if not params:
sig = inspect.signature(func)
for param_name, param in sig.parameters.items():
if param_name not in ("self", "cls"):
params[param_name] = str(param.annotation) if param.annotation != inspect.Parameter.empty else "Any"
# Create and store tool
tool = Tool(
name=tool_name,
func=func,
description=tool_desc.strip(),
parameters=params,
)
self._tools[tool_name] = tool
logger.debug(f"Registered tool: {tool_name}")
@functools.wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
return wrapper
return decorator
def add(
self,
func: Callable,
name: Optional[str] = None,
description: str = "",
parameters: Optional[Dict[str, str]] = None
) -> None:
"""
Directly add a function as a tool (non-decorator version).
Args:
func: The function to register
name: Tool name (defaults to function name)
description: Tool description
parameters: Parameter descriptions
"""
tool_name = name or func.__name__
tool_desc = description or func.__doc__ or ""
tool = Tool(
name=tool_name,
func=func,
description=tool_desc.strip(),
parameters=parameters or {},
)
self._tools[tool_name] = tool
logger.debug(f"Added tool: {tool_name}")
def get(self, name: str) -> Optional[Tool]:
"""Get a tool by name."""
return self._tools.get(name)
def call(self, name: str, *args, **kwargs) -> Any:
"""
Call a tool by name.
Args:
name: Tool name
*args: Positional arguments
**kwargs: Keyword arguments
Returns:
Tool result
Raises:
KeyError: If tool not found
"""
tool = self.get(name)
if not tool:
raise KeyError(f"Tool '{name}' not found in registry")
return tool(*args, **kwargs)
def remove(self, name: str) -> bool:
"""Remove a tool from the registry."""
if name in self._tools:
del self._tools[name]
return True
return False
def list_tools(self) -> List[Dict[str, Any]]:
"""List all registered tools with their metadata."""
return [tool.to_dict() for tool in self._tools.values()]
def has(self, name: str) -> bool:
"""Check if a tool is registered."""
return name in self._tools
def __contains__(self, name: str) -> bool:
return self.has(name)
def __len__(self) -> int:
return len(self._tools)
def __iter__(self):
return iter(self._tools.values())
# Global tool registry instance
tool_registry = ToolRegistry()
def register_tool(
name: Optional[str] = None,
description: str = "",
parameters: Optional[Dict[str, str]] = None
) -> Callable:
"""
Convenience decorator to register a tool in the global registry.
Usage:
@register_tool("my_tool", description="Does something cool")
def my_tool(data: str) -> dict:
return {"result": data}
"""
return tool_registry.register(name, description, parameters)
def get_tool(name: str) -> Optional[Tool]:
"""Get a tool from the global registry."""
return tool_registry.get(name)
|