Enhance event system type safety and error handling

- Improve type annotations for event bus and event types
- Add null checks for agent and task in event emissions
- Update import paths for base tool and base agent
- Refactor event listener type hints
- Remove unnecessary print statements
- Update test configurations to match new event handling
This commit is contained in:
Lorenze Jay
2025-02-12 15:46:56 -08:00
parent 25453f7cb1
commit 1250388635
9 changed files with 7099 additions and 4757 deletions

View File

@@ -20,7 +20,7 @@ from crewai.agents.cache.cache_handler import CacheHandler
from crewai.agents.tools_handler import ToolsHandler
from crewai.knowledge.knowledge import Knowledge
from crewai.knowledge.source.base_knowledge_source import BaseKnowledgeSource
from crewai.tools import BaseTool
from crewai.tools.base_tool import BaseTool
from crewai.tools.base_tool import Tool
from crewai.utilities import I18N, Logger, RPMController
from crewai.utilities.config import process_config
@@ -112,7 +112,7 @@ class BaseAgent(ABC, BaseModel):
default=False,
description="Enable agent to delegate and ask questions among each other.",
)
tools: Optional[List[Any]] = Field(
tools: Optional[List[BaseTool]] = Field(
default_factory=list, description="Tools at agents' disposal"
)
max_iter: int = Field(

View File

@@ -90,15 +90,16 @@ class CrewAgentExecutor(CrewAgentExecutorMixin):
self.llm.stop = list(set(self.llm.stop + self.stop))
def invoke(self, inputs: Dict[str, str]) -> Dict[str, Any]:
event_bus.emit(
self,
event=AgentExecutionStarted(
agent=self.agent,
task=self.task,
tools=self.tools,
inputs=inputs,
),
)
if self.agent and self.task:
event_bus.emit(
self,
event=AgentExecutionStarted(
agent=self.agent,
tools=self.tools,
inputs=inputs,
task=self.task,
),
)
if "system" in self.prompt:
system_prompt = self._format_prompt(self.prompt.get("system", ""), inputs)
user_prompt = self._format_prompt(self.prompt.get("user", ""), inputs)
@@ -191,12 +192,13 @@ class CrewAgentExecutor(CrewAgentExecutorMixin):
def _handle_unknown_error(self, exception: Exception) -> None:
"""Handle unknown errors by informing the user."""
event_bus.emit(
self,
event=AgentExecutionError(
agent=self.agent, task=self.task, error=str(exception)
),
)
if self.agent:
event_bus.emit(
self,
event=AgentExecutionError(
agent=self.agent, task=self.task, error=str(exception)
),
)
self._printer.print(
content="An unknown error occurred. Please check the details below.",
color="red",