From 179ca5217fa379c7cc101dceac52d0cb339fc2f9 Mon Sep 17 00:00:00 2001 From: User1234608 Date: Sun, 24 Aug 2025 14:11:18 +0200 Subject: [PATCH] Create registry.py --- tools/registry.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 tools/registry.py diff --git a/tools/registry.py b/tools/registry.py new file mode 100644 index 0000000..0ab02a8 --- /dev/null +++ b/tools/registry.py @@ -0,0 +1,51 @@ + +from typing import Callable, Dict, Any + +class ToolRegistry: + """Registry for managing custom tools that agents can call.""" + + def __init__(self): + self._tools: Dict[str, Callable[..., Any]] = {} + + def register_tool(self, name: str, func: Callable[..., Any]): + if name in self._tools: + raise ValueError(f"Tool '{name}' already exists.") + self._tools[name] = func + + def unregister_tool(self, name: str): + if name not in self._tools: + raise KeyError(f"Tool '{name}' not found.") + del self._tools[name] + + def get_tool(self, name: str) -> Callable[..., Any]: + if name not in self._tools: + raise KeyError(f"Tool '{name}' not found.") + return self._tools[name] + + def list_tools(self): + return list(self._tools.keys()) + + +# global instance +registry = ToolRegistry() + + +def tool(name: str): + """Decorator for easy tool registration.""" + def decorator(func: Callable[..., Any]): + registry.register_tool(name, func) + return func + return decorator + + +# Example: custom tool added by user +@tool("hello_world") +def hello_tool(name: str = "developer") -> str: + return f"Hello, {name}! This is a custom tool hook." + + +if __name__ == "__main__": + # Demonstration + print("Registered tools:", registry.list_tools()) + result = registry.get_tool("hello_world")("DeepCode") + print(result)