Fix: Pass task context through delegation chain for proper coworker recognition

Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
Devin AI
2024-12-30 20:26:43 +00:00
parent c08b4e5d9c
commit c59c4afc16
2 changed files with 27 additions and 6 deletions

View File

@@ -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

View File

@@ -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]