mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-30 02:28:13 +00:00
enhance: improve validator flexibility and role matching robustness
- Broaden allowed_agents validator to accept any Sequence type (not just list) - Add .strip() to role string comparisons for whitespace handling - Improve type hints and documentation based on code review feedback Co-Authored-By: João <joao@crewai.com>
This commit is contained in:
@@ -2,7 +2,9 @@ import uuid
|
|||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
from copy import copy as shallow_copy
|
from copy import copy as shallow_copy
|
||||||
from hashlib import md5
|
from hashlib import md5
|
||||||
|
from __future__ import annotations
|
||||||
from typing import Any, Callable, Dict, List, Optional, TypeVar, Union
|
from typing import Any, Callable, Dict, List, Optional, TypeVar, Union
|
||||||
|
from collections.abc import Sequence
|
||||||
|
|
||||||
from pydantic import (
|
from pydantic import (
|
||||||
UUID4,
|
UUID4,
|
||||||
@@ -107,9 +109,9 @@ class BaseAgent(ABC, BaseModel):
|
|||||||
default=False,
|
default=False,
|
||||||
description="Enable agent to delegate and ask questions among each other.",
|
description="Enable agent to delegate and ask questions among each other.",
|
||||||
)
|
)
|
||||||
allowed_agents: Optional[List[Union[str, "BaseAgent"]]] = Field(
|
allowed_agents: Optional[List[Union[str, BaseAgent]]] = Field(
|
||||||
default=None,
|
default=None,
|
||||||
description="List of agent roles or agent instances that this agent can delegate to. If None, can delegate to all agents when allow_delegation=True.",
|
description="List of agent roles or agent instances that this agent can delegate to. If None, can delegate to all agents when allow_delegation=True. Empty list prevents all delegation.",
|
||||||
)
|
)
|
||||||
tools: Optional[List[BaseTool]] = Field(
|
tools: Optional[List[BaseTool]] = Field(
|
||||||
default_factory=list, description="Tools at agents' disposal"
|
default_factory=list, description="Tools at agents' disposal"
|
||||||
@@ -200,19 +202,19 @@ class BaseAgent(ABC, BaseModel):
|
|||||||
|
|
||||||
@field_validator("allowed_agents", mode="before")
|
@field_validator("allowed_agents", mode="before")
|
||||||
@classmethod
|
@classmethod
|
||||||
def validate_allowed_agents(cls, allowed_agents: Optional[List[Union[str, "BaseAgent"]]]) -> Optional[List[Union[str, "BaseAgent"]]]:
|
def validate_allowed_agents(cls, allowed_agents: Optional[Sequence[Union[str, BaseAgent]]]) -> Optional[List[Union[str, BaseAgent]]]:
|
||||||
"""Validate the allowed_agents list."""
|
"""Validate the allowed_agents list."""
|
||||||
if allowed_agents is None:
|
if allowed_agents is None:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
if not isinstance(allowed_agents, list):
|
if not isinstance(allowed_agents, Sequence) or isinstance(allowed_agents, str):
|
||||||
raise ValueError("allowed_agents must be a list of agent roles (strings) or agent instances")
|
raise ValueError("allowed_agents must be a list or tuple of agent roles (strings) or agent instances")
|
||||||
|
|
||||||
for agent in allowed_agents:
|
for agent in allowed_agents:
|
||||||
if not isinstance(agent, (str, BaseAgent)):
|
if not isinstance(agent, (str, BaseAgent)):
|
||||||
raise ValueError("Each item in allowed_agents must be either a string (agent role) or a BaseAgent instance")
|
raise ValueError("Each item in allowed_agents must be either a string (agent role) or a BaseAgent instance")
|
||||||
|
|
||||||
return allowed_agents
|
return list(allowed_agents)
|
||||||
|
|
||||||
@model_validator(mode="after")
|
@model_validator(mode="after")
|
||||||
def validate_and_set_attributes(self):
|
def validate_and_set_attributes(self):
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ class AgentTools:
|
|||||||
for agent in self.agents:
|
for agent in self.agents:
|
||||||
for allowed in delegating_agent.allowed_agents:
|
for allowed in delegating_agent.allowed_agents:
|
||||||
if isinstance(allowed, str):
|
if isinstance(allowed, str):
|
||||||
if agent.role.lower() == allowed.lower():
|
if agent.role.strip().lower() == allowed.strip().lower():
|
||||||
filtered_agents.append(agent)
|
filtered_agents.append(agent)
|
||||||
break
|
break
|
||||||
elif isinstance(allowed, BaseAgent):
|
elif isinstance(allowed, BaseAgent):
|
||||||
|
|||||||
Reference in New Issue
Block a user