From 3f417b2fc144547bdffdb7dfda4a9f348931fa22 Mon Sep 17 00:00:00 2001 From: Lorenze Jay Date: Wed, 12 Mar 2025 13:07:37 -0700 Subject: [PATCH] Enhance EventListener with property setters for crew, task, agent, tool, flow, and method branches to streamline state management --- src/crewai/utilities/events/event_listener.py | 41 +++++++++++++++---- .../events/utils/console_formatter.py | 17 ++------ 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/src/crewai/utilities/events/event_listener.py b/src/crewai/utilities/events/event_listener.py index 921594de1..e20c450b0 100644 --- a/src/crewai/utilities/events/event_listener.py +++ b/src/crewai/utilities/events/event_listener.py @@ -72,26 +72,50 @@ class EventListener(BaseEventListener): def current_crew_tree(self) -> Optional[Tree]: return self.formatter.current_crew_tree + @current_crew_tree.setter + def current_crew_tree(self, value: Optional[Tree]): + self.formatter.current_crew_tree = value + @property def current_task_branch(self) -> Optional[Tree]: return self.formatter.current_task_branch + @current_task_branch.setter + def current_task_branch(self, value: Optional[Tree]): + self.formatter.current_task_branch = value + @property def current_agent_branch(self) -> Optional[Tree]: return self.formatter.current_agent_branch + @current_agent_branch.setter + def current_agent_branch(self, value: Optional[Tree]): + self.formatter.current_agent_branch = value + @property def current_tool_branch(self) -> Optional[Tree]: return self.formatter.current_tool_branch + @current_tool_branch.setter + def current_tool_branch(self, value: Optional[Tree]): + self.formatter.current_tool_branch = value + @property def current_flow_tree(self) -> Optional[Tree]: return self.formatter.current_flow_tree + @current_flow_tree.setter + def current_flow_tree(self, value: Optional[Tree]): + self.formatter.current_flow_tree = value + @property def current_method_branch(self) -> Optional[Tree]: return self.formatter.current_method_branch + @current_method_branch.setter + def current_method_branch(self, value: Optional[Tree]): + self.formatter.current_method_branch = value + # ----------- CREW EVENTS ----------- def setup_listeners(self, crewai_event_bus): @@ -200,14 +224,14 @@ class EventListener(BaseEventListener): @crewai_event_bus.on(FlowCreatedEvent) def on_flow_created(source, event: FlowCreatedEvent): self._telemetry.flow_creation_span(event.flow_name) - self.formatter.create_flow_tree(event.flow_name) + self.current_flow_tree = self.formatter.create_flow_tree(event.flow_name) @crewai_event_bus.on(FlowStartedEvent) def on_flow_started(source, event: FlowStartedEvent): self._telemetry.flow_execution_span( event.flow_name, list(source._methods.keys()) ) - self.formatter.start_flow(event.flow_name) + self.current_flow_tree = self.formatter.start_flow(event.flow_name) @crewai_event_bus.on(FlowFinishedEvent) def on_flow_finished(source, event: FlowFinishedEvent): @@ -218,12 +242,13 @@ class EventListener(BaseEventListener): @crewai_event_bus.on(MethodExecutionStartedEvent) def on_method_execution_started(source, event: MethodExecutionStartedEvent): - self.formatter.update_method_status( - self.current_method_branch, - self.current_flow_tree, - event.method_name, - "running", - ) + if self.current_flow_tree: + self.current_method_branch = self.formatter.update_method_status( + self.current_method_branch, + self.current_flow_tree, + event.method_name, + "running", + ) @crewai_event_bus.on(MethodExecutionFinishedEvent) def on_method_execution_finished(source, event: MethodExecutionFinishedEvent): diff --git a/src/crewai/utilities/events/utils/console_formatter.py b/src/crewai/utilities/events/utils/console_formatter.py index a279d3ff6..dbfa35742 100644 --- a/src/crewai/utilities/events/utils/console_formatter.py +++ b/src/crewai/utilities/events/utils/console_formatter.py @@ -68,8 +68,7 @@ class ConsoleFormatter: def print(self, *args, **kwargs) -> None: """Print to console with consistent formatting if verbose is enabled.""" - if self.verbose: - self.console.print(*args, **kwargs) + self.console.print(*args, **kwargs) def print_panel( self, content: Text, title: str, style: str = "blue", is_flow: bool = False @@ -239,14 +238,13 @@ class ConsoleFormatter: self.print(crew_tree) self.print() - def create_flow_tree(self, flow_name: str) -> Optional[Tree]: + def create_flow_tree(self, flow_name: str) -> Tree: """Create and initialize a flow tree.""" - # if not self.verbose: - # return None content = self.create_status_content( "Starting Flow Execution", flow_name, "blue" ) + self.print_panel(content, "Flow Execution", "blue", is_flow=True) # Create initial tree @@ -264,9 +262,6 @@ class ConsoleFormatter: def start_flow(self, flow_name: str) -> Optional[Tree]: """Initialize a flow execution tree.""" - # if not self.verbose: - # return None - flow_tree = Tree("") self.update_tree_label(flow_tree, "🌊 Flow:", flow_name, "blue", "In Progress") self.add_tree_node(flow_tree, "🧠 Initializing...", "yellow") @@ -279,9 +274,6 @@ class ConsoleFormatter: self, flow_tree: Tree, flow_name: str, flow_id: str, status: str = "completed" ) -> None: """Update flow status in the tree.""" - # if not self.verbose: - # return - self.update_tree_label( flow_tree, "✅ Flow Finished:" if status == "completed" else "❌ Flow Failed:", @@ -311,9 +303,6 @@ class ConsoleFormatter: status: str = "running", ) -> Optional[Tree]: """Update method status in the flow tree.""" - # if not flow_tree: - # return None - if status == "running": prefix, style = "🔄 Running:", "yellow" elif status == "completed":