|
| 1 | +# ABOUTME: Tool pool system using existing AgentTool base class for tool selection and management |
| 2 | +# ABOUTME: Integrates with existing tool infrastructure and @tool decorator pattern |
| 3 | +"""Experimental tool pool system for structured tool management.""" |
| 4 | + |
| 5 | +from typing import Any, Optional, Union, Callable |
| 6 | + |
| 7 | +from ..types.tools import AgentTool |
| 8 | +from ..tools.tools import PythonAgentTool |
| 9 | +from ..tools.decorator import DecoratedFunctionTool |
| 10 | + |
| 11 | + |
| 12 | +class ToolPool: |
| 13 | + """Pool of available tools for agent selection using existing tool infrastructure.""" |
| 14 | + |
| 15 | + def __init__(self, tools: Optional[list[Union[AgentTool, Callable]]] = None): |
| 16 | + """Initialize tool pool. |
| 17 | + |
| 18 | + Args: |
| 19 | + tools: List of AgentTool instances or @tool decorated functions |
| 20 | + """ |
| 21 | + self._tools: dict[str, AgentTool] = {} |
| 22 | + if tools: |
| 23 | + for tool in tools: |
| 24 | + if isinstance(tool, AgentTool): |
| 25 | + self.add_tool(tool) |
| 26 | + elif callable(tool): |
| 27 | + self.add_tool_function(tool) |
| 28 | + else: |
| 29 | + raise ValueError(f"Tool must be AgentTool instance or callable, got {type(tool)}") |
| 30 | + |
| 31 | + def add_tool(self, tool: AgentTool) -> None: |
| 32 | + """Add existing AgentTool instance to the pool. |
| 33 | + |
| 34 | + Args: |
| 35 | + tool: AgentTool instance to add |
| 36 | + """ |
| 37 | + self._tools[tool.tool_name] = tool |
| 38 | + |
| 39 | + def add_tool_function(self, tool_func: Callable) -> None: |
| 40 | + """Add @tool decorated function to the pool. |
| 41 | + |
| 42 | + Args: |
| 43 | + tool_func: Function decorated with @tool |
| 44 | + """ |
| 45 | + if hasattr(tool_func, '_strands_tool_spec'): |
| 46 | + # This is a decorated function tool |
| 47 | + tool_spec = tool_func._strands_tool_spec |
| 48 | + tool_name = tool_spec.get('name', tool_func.__name__) |
| 49 | + decorated_tool = DecoratedFunctionTool( |
| 50 | + tool_name=tool_name, |
| 51 | + tool_spec=tool_spec, |
| 52 | + tool_func=tool_func, |
| 53 | + metadata={} |
| 54 | + ) |
| 55 | + self.add_tool(decorated_tool) |
| 56 | + else: |
| 57 | + raise ValueError(f"Function {tool_func.__name__} is not decorated with @tool") |
| 58 | + |
| 59 | + def add_tools_from_module(self, module: Any) -> None: |
| 60 | + """Add all @tool decorated functions from a Python module. |
| 61 | + |
| 62 | + Args: |
| 63 | + module: Python module containing @tool decorated functions |
| 64 | + """ |
| 65 | + import inspect |
| 66 | + |
| 67 | + for name, obj in inspect.getmembers(module): |
| 68 | + if inspect.isfunction(obj) and hasattr(obj, '_strands_tool_spec'): |
| 69 | + self.add_tool_function(obj) |
| 70 | + |
| 71 | + @classmethod |
| 72 | + def from_module(cls, module: Any) -> "ToolPool": |
| 73 | + """Create ToolPool from all @tool functions in a module. |
| 74 | + |
| 75 | + Args: |
| 76 | + module: Python module containing @tool decorated functions |
| 77 | + |
| 78 | + Returns: |
| 79 | + ToolPool with all tools from the module |
| 80 | + """ |
| 81 | + pool = cls() |
| 82 | + pool.add_tools_from_module(module) |
| 83 | + return pool |
| 84 | + |
| 85 | + def get_tool(self, name: str) -> Optional[AgentTool]: |
| 86 | + """Get tool by name. |
| 87 | + |
| 88 | + Args: |
| 89 | + name: Tool name |
| 90 | + |
| 91 | + Returns: |
| 92 | + AgentTool if found, None otherwise |
| 93 | + """ |
| 94 | + return self._tools.get(name) |
| 95 | + |
| 96 | + def list_tools(self) -> list[str]: |
| 97 | + """List available tool names. |
| 98 | + |
| 99 | + Returns: |
| 100 | + List of tool names |
| 101 | + """ |
| 102 | + return list(self._tools.keys()) |
| 103 | + |
| 104 | + def to_agent_tools(self) -> list[AgentTool]: |
| 105 | + """Convert to format compatible with Agent tools parameter. |
| 106 | + |
| 107 | + Returns: |
| 108 | + List of AgentTool instances |
| 109 | + """ |
| 110 | + return list(self._tools.values()) |
0 commit comments