mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-11 00:58:30 +00:00
Fix: Pass task context through delegation chain for proper coworker recognition
Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
@@ -406,8 +406,17 @@ class Agent(BaseAgent):
|
|||||||
callbacks=[TokenCalcHandler(self._token_process)],
|
callbacks=[TokenCalcHandler(self._token_process)],
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_delegation_tools(self, agents: List[BaseAgent]):
|
def get_delegation_tools(self, agents: List[BaseAgent], task: Optional[Task] = None) -> List[BaseTool]:
|
||||||
agent_tools = AgentTools(agents=agents)
|
"""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()
|
tools = agent_tools.tools()
|
||||||
return tools
|
return tools
|
||||||
|
|
||||||
|
|||||||
@@ -9,24 +9,36 @@ from .delegate_work_tool import DelegateWorkTool
|
|||||||
class AgentTools:
|
class AgentTools:
|
||||||
"""Manager class for agent-related tools"""
|
"""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.agents = agents
|
||||||
self.i18n = i18n
|
self.i18n = i18n
|
||||||
|
self.task = task
|
||||||
|
|
||||||
def tools(self) -> list[BaseTool]:
|
def tools(self) -> list[BaseTool]:
|
||||||
"""Get all available agent tools"""
|
"""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(
|
delegate_tool = DelegateWorkTool(
|
||||||
agents=self.agents,
|
agents=self.agents,
|
||||||
i18n=self.i18n,
|
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(
|
ask_tool = AskQuestionTool(
|
||||||
agents=self.agents,
|
agents=self.agents,
|
||||||
i18n=self.i18n,
|
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]
|
return [delegate_tool, ask_tool]
|
||||||
|
|||||||
Reference in New Issue
Block a user