fix: prevent duplicate execution of WebSocket tools

- Add specific handling for WebSocket tools in _check_tool_repeated_usage
- Add test cases for WebSocket tool execution
- Fix issue #2209

Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
Devin AI
2025-02-24 13:15:17 +00:00
parent b50772a38b
commit c41bb4b8c7
2 changed files with 87 additions and 4 deletions

View File

@@ -283,13 +283,22 @@ class ToolUsage:
def _check_tool_repeated_usage(
self, calling: Union[ToolCalling, InstructorToolCalling]
) -> None:
) -> bool:
if not self.tools_handler:
return False # type: ignore # No return value expected
return False
if last_tool_usage := self.tools_handler.last_used_tool:
return (calling.tool_name == last_tool_usage.tool_name) and ( # type: ignore # No return value expected
calling.arguments == last_tool_usage.arguments
# For WebSocket tools, we need to check if the question is the same
if "question" in calling.arguments and "question" in last_tool_usage.arguments:
return (
calling.tool_name == last_tool_usage.tool_name
and calling.arguments["question"] == last_tool_usage.arguments["question"]
)
# For other tools, check all arguments
return (
calling.tool_name == last_tool_usage.tool_name
and calling.arguments == last_tool_usage.arguments
)
return False
def _select_tool(self, tool_name: str) -> Any:
order_tools = sorted(