mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-05-01 07:13:00 +00:00
fix: Add error handling and consolidate tool assignment logic
Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
@@ -137,7 +137,16 @@ class Agent(BaseAgent):
|
|||||||
|
|
||||||
# If this is a manager agent, ensure it only has delegation tools
|
# If this is a manager agent, ensure it only has delegation tools
|
||||||
if self.role == "Manager" and self.allow_delegation and self.crew:
|
if self.role == "Manager" and self.allow_delegation and self.crew:
|
||||||
self.tools = self.get_delegation_tools(self.crew.agents)
|
try:
|
||||||
|
self.tools = self.get_delegation_tools(self.crew.agents)
|
||||||
|
self._logger.log(
|
||||||
|
"info",
|
||||||
|
f"Manager agent has delegation tools: {[tool.name for tool in self.tools]}",
|
||||||
|
color="blue",
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
self._logger.log("error", f"Failed to set delegation tools: {str(e)}", color="red")
|
||||||
|
raise ValueError(f"Failed to set delegation tools: {str(e)}")
|
||||||
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
|||||||
@@ -694,14 +694,18 @@ class Crew(BaseModel):
|
|||||||
if self.manager_agent is not None:
|
if self.manager_agent is not None:
|
||||||
manager = self.manager_agent
|
manager = self.manager_agent
|
||||||
manager.allow_delegation = True
|
manager.allow_delegation = True
|
||||||
# Only use delegation tools for manager agent
|
manager.crew = self
|
||||||
delegation_tools = AgentTools(agents=self.agents).tools()
|
try:
|
||||||
manager.tools = delegation_tools
|
delegation_tools = AgentTools(agents=self.agents).tools()
|
||||||
self._logger.log(
|
manager.tools = delegation_tools
|
||||||
"info",
|
self._logger.log(
|
||||||
f"Manager agent has delegation tools: {[tool.name for tool in manager.tools]}",
|
"info",
|
||||||
color="blue",
|
f"Manager agent has delegation tools: {[tool.name for tool in manager.tools]}",
|
||||||
)
|
color="blue",
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
self._logger.log("error", f"Failed to set manager tools: {str(e)}", color="red")
|
||||||
|
raise ValueError(f"Failed to set manager tools: {str(e)}")
|
||||||
else:
|
else:
|
||||||
self.manager_llm = create_llm(self.manager_llm)
|
self.manager_llm = create_llm(self.manager_llm)
|
||||||
manager = Agent(
|
manager = Agent(
|
||||||
@@ -714,9 +718,19 @@ class Crew(BaseModel):
|
|||||||
verbose=self.verbose,
|
verbose=self.verbose,
|
||||||
)
|
)
|
||||||
self.manager_agent = manager
|
self.manager_agent = manager
|
||||||
# Set delegation tools after initialization
|
manager.crew = self
|
||||||
manager.tools = AgentTools(agents=self.agents).tools()
|
try:
|
||||||
manager.crew = self
|
delegation_tools = AgentTools(agents=self.agents).tools()
|
||||||
|
manager.tools = delegation_tools
|
||||||
|
self._logger.log(
|
||||||
|
"info",
|
||||||
|
f"Manager agent has delegation tools: {[tool.name for tool in manager.tools]}",
|
||||||
|
color="blue",
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
self._logger.log("error", f"Failed to set manager tools: {str(e)}", color="red")
|
||||||
|
raise ValueError(f"Failed to set manager tools: {str(e)}")
|
||||||
|
|
||||||
|
|
||||||
def _execute_tasks(
|
def _execute_tasks(
|
||||||
self,
|
self,
|
||||||
|
|||||||
@@ -358,6 +358,10 @@ class Task(BaseModel):
|
|||||||
self.start_time = datetime.datetime.now()
|
self.start_time = datetime.datetime.now()
|
||||||
self._execution_span = self._telemetry.task_started(crew=agent.crew, task=self)
|
self._execution_span = self._telemetry.task_started(crew=agent.crew, task=self)
|
||||||
|
|
||||||
|
# Track delegation if this task is being executed by a different agent
|
||||||
|
if self.agent and agent.role != self.agent.role:
|
||||||
|
self.increment_delegations(agent.role)
|
||||||
|
|
||||||
self.prompt_context = context
|
self.prompt_context = context
|
||||||
tools = tools or self.tools or []
|
tools = tools or self.tools or []
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,11 @@ from .ask_question_tool import AskQuestionTool
|
|||||||
from .delegate_work_tool import DelegateWorkTool
|
from .delegate_work_tool import DelegateWorkTool
|
||||||
|
|
||||||
|
|
||||||
|
# Tool name constants
|
||||||
|
DELEGATE_WORK_TOOL = "Delegate Work"
|
||||||
|
ASK_QUESTION_TOOL = "Ask Question"
|
||||||
|
|
||||||
|
|
||||||
class AgentTools:
|
class AgentTools:
|
||||||
"""Manager class for agent-related tools"""
|
"""Manager class for agent-related tools"""
|
||||||
|
|
||||||
@@ -21,14 +26,14 @@ class AgentTools:
|
|||||||
agents=self.agents,
|
agents=self.agents,
|
||||||
i18n=self.i18n,
|
i18n=self.i18n,
|
||||||
description=self.i18n.tools("delegate_work").format(coworkers=coworkers), # type: ignore
|
description=self.i18n.tools("delegate_work").format(coworkers=coworkers), # type: ignore
|
||||||
name="Delegate Work"
|
name=DELEGATE_WORK_TOOL # Using constant for consistency
|
||||||
)
|
)
|
||||||
|
|
||||||
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=self.i18n.tools("ask_question").format(coworkers=coworkers), # type: ignore
|
||||||
name="Ask Question"
|
name=ASK_QUESTION_TOOL # Using constant for consistency
|
||||||
)
|
)
|
||||||
|
|
||||||
return [delegate_tool, ask_tool]
|
return [delegate_tool, ask_tool]
|
||||||
|
|||||||
Reference in New Issue
Block a user