File size: 1,744 Bytes
ec946c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

class CustomToolNode:
    """Tool node personalizzato che può accedere allo stato completo"""
    
    def __init__(self, tools_dict):
        self.tools_dict = tools_dict
    
    def __call__(self, state):
        messages = state["messages"]
        last_message = messages[-1]
        
        # Estrai i tool calls dall'ultimo messaggio
        if hasattr(last_message, 'tool_calls') and last_message.tool_calls:
            results = []
            for tool_call in last_message.tool_calls:
                tool_name = tool_call["name"]
                tool_args = tool_call["args"]
                
                # Aggiungi task_id agli argomenti del tool
                tool_args_with_state = {
                    **tool_args,
                    "task_id": state["task_id"],
                    "state": state  # Opzionale: passa tutto lo stato
                }
                
                if tool_name in self.tools_dict:
                    try:
                        result = self.tools_dict[tool_name].invoke(tool_args_with_state)
                        results.append({
                            "type": "tool",
                            "name": tool_name,
                            "tool_call_id": tool_call["id"],
                            "content": str(result)
                        })
                    except Exception as e:
                        results.append({
                            "type": "tool",
                            "name": tool_name,
                            "tool_call_id": tool_call["id"],
                            "content": f"Error: {str(e)}"
                        })
            
            return {"messages": results}
        
        return {"messages": []}