File size: 1,156 Bytes
e67043b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from tool import Tool
from typing import Dict, Callable, Any, List

ToolBuilder = Callable[[Any], Tool]
FuncToolBuilder = Callable[[], ToolBuilder]


class ToolsRegistry:
    def __init__(self) -> None:
        self.tools: Dict[str, FuncToolBuilder] = {}

    def register(self, tool_name: str, tool: FuncToolBuilder):
        print(f"will register {tool_name}")
        self.tools[tool_name] = tool

    def build(self, tool_name, config) -> Tool:
        ret = self.tools[tool_name]()(config)
        if isinstance(ret, Tool):
            return ret
        raise ValueError(
            "Tool builder {} did not return a Tool instance".format(tool_name)
        )

    def list_tools(self) -> List[str]:
        return list(self.tools.keys())


tools_registry = ToolsRegistry()


def register(tool_name):
    def decorator(tool: FuncToolBuilder):
        tools_registry.register(tool_name, tool)
        return tool

    return decorator


def build_tool(tool_name: str, config: Any) -> Tool:
    print(f"will build {tool_name}")
    return tools_registry.build(tool_name, config)


def list_tools() -> List[str]:
    return tools_registry.list_tools()