From 358befe2c13d817a1eb6bf14c7aa632333630d18 Mon Sep 17 00:00:00 2001 From: Brandon Hancock Date: Thu, 13 Mar 2025 15:45:11 -0400 Subject: [PATCH] wip --- src/crewai/agent.py | 6 ++++-- src/crewai/tools/base_tool.py | 18 ++++++++++++++---- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/crewai/agent.py b/src/crewai/agent.py index d37cecc27..6a62313c1 100644 --- a/src/crewai/agent.py +++ b/src/crewai/agent.py @@ -345,9 +345,11 @@ class Agent(BaseAgent): def get_delegation_tools(self, agents: Sequence[BaseAgent]) -> Sequence[BaseTool]: # If delegate_to is specified, use those agents instead of all agents if self.delegate_to is not None: - agents_to_use = cast(List[BaseAgent], list(self.delegate_to)) + agents_to_use: Sequence[BaseAgent] = cast( + List[BaseAgent], list(self.delegate_to) + ) else: - agents_to_use = list(agents) # Convert to list to match expected type + agents_to_use: Sequence[BaseAgent] = cast(List[BaseAgent], list(agents)) agent_tools = AgentTools(agents=agents_to_use) delegation_tools = agent_tools.tools() diff --git a/src/crewai/tools/base_tool.py b/src/crewai/tools/base_tool.py index b3c0f997c..f4825fb1d 100644 --- a/src/crewai/tools/base_tool.py +++ b/src/crewai/tools/base_tool.py @@ -248,13 +248,18 @@ def to_langchain( def tool(*args): """ Decorator to create a tool from a function. + Ensures the decorated function is always wrapped as a BaseTool. """ - def _make_with_name(tool_name: str) -> Callable: + def _make_with_name(tool_name: str) -> Callable[[Callable], BaseTool]: def _make_tool(f: Callable) -> BaseTool: + # If f is already a BaseTool, return it + if isinstance(f, BaseTool): + return f + if f.__doc__ is None: raise ValueError("Function must have a docstring") - if f.__annotations__ is None: + if not f.__annotations__: raise ValueError("Function must have type annotations") class_name = "".join(tool_name.split()).title() @@ -278,7 +283,12 @@ def tool(*args): return _make_tool if len(args) == 1 and callable(args[0]): + # Direct function decoration + if isinstance(args[0], BaseTool): + return args[0] # Already a BaseTool, return as-is return _make_with_name(args[0].__name__)(args[0]) - if len(args) == 1 and isinstance(args[0], str): + elif len(args) == 1 and isinstance(args[0], str): + # Name provided, return a decorator return _make_with_name(args[0]) - raise ValueError("Invalid arguments") + else: + raise ValueError("Invalid arguments")