File size: 934 Bytes
df07284
 
1870dee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from .state import AgentState, tools_config
from .tools import TOOL_REGISTRY

def execute_tool(state: AgentState):
    tool_name = state["selected_tool"]
    args = state["tool_args"]

    # Make sure the tool exists
    if tool_name not in TOOL_REGISTRY:
        return {
            "tool_result": {
                "status": "error",
                "message": "Unknown tool"
            }
        }

    tool_config = next(
        t for t in tools_config
        if t["name"] == tool_name
    )

    allowed = {
        p["name"] for p in tool_config["parameters"]
    }

    if not set(args).issubset(allowed):
        return {
            "tool_result": {
                "status": "error",
                "message": "Unexpected tool arguments"
            }
        }

    # Execute the selected function
    function = TOOL_REGISTRY[tool_name]
    result = function(**args)

    return {
        "tool_result": result
    }