From c59c4afc16238163c0cd7b0fb0319cc6e2fcec0a Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Mon, 30 Dec 2024 20:26:43 +0000 Subject: [PATCH] Fix: Pass task context through delegation chain for proper coworker recognition Co-Authored-By: Joe Moura --- src/crewai/agent.py | 13 +++++++++++-- src/crewai/tools/agent_tools/agent_tools.py | 20 ++++++++++++++++---- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/crewai/agent.py b/src/crewai/agent.py index 999d1d800..6b6b7bbc3 100644 --- a/src/crewai/agent.py +++ b/src/crewai/agent.py @@ -406,8 +406,17 @@ class Agent(BaseAgent): callbacks=[TokenCalcHandler(self._token_process)], ) - def get_delegation_tools(self, agents: List[BaseAgent]): - agent_tools = AgentTools(agents=agents) + def get_delegation_tools(self, agents: List[BaseAgent], task: Optional[Task] = None) -> List[BaseTool]: + """Get the delegation tools for this agent. + + Args: + agents: List of agents that can be delegated to + task: Optional task context for delegation + + Returns: + List of delegation tools + """ + agent_tools = AgentTools(agents=agents, task=task, i18n=self.i18n) tools = agent_tools.tools() return tools diff --git a/src/crewai/tools/agent_tools/agent_tools.py b/src/crewai/tools/agent_tools/agent_tools.py index 77d3c2d89..fbd42e658 100644 --- a/src/crewai/tools/agent_tools/agent_tools.py +++ b/src/crewai/tools/agent_tools/agent_tools.py @@ -9,24 +9,36 @@ from .delegate_work_tool import DelegateWorkTool class AgentTools: """Manager class for agent-related tools""" - def __init__(self, agents: list[BaseAgent], i18n: I18N = I18N()): + def __init__(self, agents: list[BaseAgent], i18n: I18N = I18N(), task=None): self.agents = agents self.i18n = i18n + self.task = task def tools(self) -> list[BaseTool]: """Get all available agent tools""" - coworkers = ", ".join([f"{agent.role}" for agent in self.agents]) + # Format coworkers list based on agents and task context + if len(self.agents) == 1: + coworkers = self.agents[0].role + elif self.task and hasattr(self.task, 'async_execution') and self.task.async_execution and hasattr(self.task, 'agent') and self.task.agent: + # For async tasks with a specific agent, only show that agent + coworkers = self.task.agent.role + else: + # Show all agents for non-async tasks or when no specific agent is assigned + coworkers = ", ".join([agent.role for agent in self.agents]) + + # Ensure coworkers list doesn't have extra spaces or newlines + coworkers = coworkers.strip() delegate_tool = DelegateWorkTool( agents=self.agents, i18n=self.i18n, - description=self.i18n.tools("delegate_work").format(coworkers=coworkers), # type: ignore + description=f"Delegate a specific task to one of the following coworkers: {coworkers}\n", ) ask_tool = AskQuestionTool( agents=self.agents, i18n=self.i18n, - description=self.i18n.tools("ask_question").format(coworkers=coworkers), # type: ignore + description=f"Ask a specific question to one of the following coworkers: {coworkers}\n", ) return [delegate_tool, ask_tool]