Fix issue #2434: Allow tools to specify if they permit repeated usage

Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
Devin AI
2025-03-21 12:35:52 +00:00
parent 03f1d57463
commit 245399bca0
5 changed files with 190 additions and 0 deletions

View File

@@ -0,0 +1,48 @@
import pytest
from unittest.mock import MagicMock
from crewai.tools import BaseTool
from crewai.tools.tool_usage import ToolUsage
from crewai.tools.tool_calling import ToolCalling
def test_tool_repeated_usage_allowed():
"""Test that a tool with allow_repeated_usage=True can be used repeatedly with same args."""
class RepeatedUsageTool(BaseTool):
name: str = "Repeated Usage Tool"
description: str = "A tool that can be used repeatedly with the same arguments"
allow_repeated_usage: bool = True
def _run(self, test_arg: str) -> str:
return f"Used with arg: {test_arg}"
# Setup tool usage
tool = RepeatedUsageTool()
tools_handler = MagicMock()
tools_handler.last_used_tool = ToolCalling(
tool_name="Repeated Usage Tool",
arguments={"test_arg": "test"}
)
tool_usage = ToolUsage(
tools_handler=tools_handler,
tools=[tool],
original_tools=[tool],
tools_description="Test tools",
tools_names="Repeated Usage Tool",
agent=MagicMock(),
task=MagicMock(),
function_calling_llm=MagicMock(),
action=MagicMock(),
)
# Create a new tool calling with the same arguments
calling = ToolCalling(
tool_name="Repeated Usage Tool",
arguments={"test_arg": "test"}
)
# This should return False since the tool allows repeated usage
result = tool_usage._check_tool_repeated_usage(calling=calling)
assert result is False