feat: implement hierarchical agent delegation with allowed_agents parameter

- Add allowed_agents parameter to BaseAgent
- Add validation for allowed_agents list
- Update delegation tools to respect restrictions
- Add error messages for unauthorized delegation
- Add tests for hierarchical delegation

Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
Devin AI
2025-02-09 20:10:23 +00:00
parent 409892d65f
commit 9cf4c092ba
4 changed files with 190 additions and 3 deletions

View File

@@ -1,6 +1,5 @@
from typing import Optional, Union
from pydantic import Field
from pydantic import UUID4, Field
from crewai.agents.agent_builder.base_agent import BaseAgent
from crewai.task import Task
@@ -12,6 +11,7 @@ class BaseAgentTool(BaseTool):
"""Base class for agent-related tools"""
agents: list[BaseAgent] = Field(description="List of available agents")
agent_id: UUID4 = Field(description="ID of the agent using this tool")
i18n: I18N = Field(
default_factory=I18N, description="Internationalization settings"
)
@@ -58,6 +58,15 @@ class BaseAgentTool(BaseTool):
)
)
# Check if delegation is allowed based on allowed_agents list
delegating_agent = [a for a in self.agents if a.id == self.agent_id][0]
if (delegating_agent.allowed_agents is not None and
agent[0].role not in delegating_agent.allowed_agents):
return self.i18n.errors("agent_tool_unauthorized_delegation").format(
coworker=agent[0].role,
allowed_agents="\n".join([f"- {role}" for role in delegating_agent.allowed_agents])
)
agent = agent[0]
task_with_assigned_agent = Task( # type: ignore # Incompatible types in assignment (expression has type "Task", variable has type "str")
description=task,