File size: 1,519 Bytes
63d1774
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import hashlib
import json

from langchain_core.messages import ToolMessage
from modules.nodes.state import ChatState

def dedup_tool_call(state: ChatState):
    # ensure seen_tool_calls exists
    if state.get("seen_tool_calls") is None:
        state["seen_tool_calls"] = set()
    state["skip_tool"] = False  # reset every time

    if not state.get("messages"):
        return state

    last_msg = state["messages"][-1]

    # only process messages that have tool_calls
    if hasattr(last_msg, "tool_calls") and last_msg.tool_calls:
        call = last_msg.tool_calls[0]
        tool_name = call["name"]
        raw_args = call.get("arguments") or {}
        tool_args = json.dumps(raw_args, sort_keys=True)
        sig = (tool_name, hashlib.md5(tool_args.encode()).hexdigest())

        if sig in state["seen_tool_calls"]:
            # Duplicate detected → append a proper ToolMessage instead of a system message
            state["messages"].append(
                ToolMessage(
                    content=f"Duplicate tool call skipped: {tool_name}({tool_args})",
                    tool_call_id=call["id"],
                    name=tool_name,
                    additional_kwargs={},
                )
            )
            state["skip_tool"] = True
            # remove the tool_calls from the last assistant message to prevent validation error
            last_msg.tool_calls = []
        else:
            state["seen_tool_calls"].add(sig)
            state["skip_tool"] = False

    return state