File size: 1,569 Bytes
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
42
43
44
45
46
47
48
49
50
51
52

from typing import TypedDict, Annotated, Any
from langchain_core.messages import AnyMessage
from langgraph.graph.message import add_messages

tools_config = [
    {
        "name": "book_table",
        "description": "Book a table at a restaurant",
        "parameters": [
            {"name": "restaurant", "type": "string", "required": True},
            {"name": "people", "type": "integer", "required": True},
            {"name": "date", "type": "string", "required": True},
            {"name": "time", "type": "string", "required": True}
        ]
    },
    {
        "name": "check_menu",
        "description": "Check if a dish is on the restaurant menu",
        "parameters": [
            {"name": "restaurant", "type": "string", "required": True},
            {"name": "dish", "type": "string", "required": True}
        ]
    }
]

def build_parameter_catalog(tools_config):
    catalog = {}

    for tool in tools_config:
        for param in tool["parameters"]:
            name = param["name"]

            if name not in catalog:
                catalog[name] = {
                    "type": param["type"]
                }

    return catalog

parameter_catalog = build_parameter_catalog(tools_config)

def merge_dicts(old: dict, new: dict) -> dict:
    return {**old, **new}

class AgentState(TypedDict):
    messages: Annotated[list[AnyMessage], add_messages]
    global_params: Annotated[dict[str, Any], merge_dicts]
    selected_tool: str | None
    missing_params: list[str]
    tool_args: dict[str, Any]
    tool_result: dict[str, Any] | None