tool_registry.py 939 B

123456789101112131415161718192021222324252627
  1. from typing import Type, Dict
  2. from pqai_agent.toolkit.function_tool import FunctionTool
  3. class ToolRegistry:
  4. tool_map: Dict[str, FunctionTool] = {}
  5. @classmethod
  6. def register_tools(cls, toolkit_class: Type):
  7. """
  8. Register tools from a toolkit class into the global tool_map.
  9. Args:
  10. toolkit_class (Type): A class that implements a `get_tools` method.
  11. """
  12. instance = toolkit_class()
  13. if not hasattr(instance, 'get_tools') or not callable(instance.get_tools):
  14. raise ValueError(f"{toolkit_class.__name__} must implement a callable `get_tools` method.")
  15. tools = instance.get_tools()
  16. for tool in tools:
  17. if not hasattr(tool, 'name'):
  18. raise ValueError(f"Tool {tool} must have a `name` attribute.")
  19. cls.tool_map[tool.name] = tool
  20. def register_toolkit(cls):
  21. ToolRegistry.register_tools(cls)
  22. return cls