mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-10 00:28:31 +00:00
Enhance EventListener with property setters for crew, task, agent, tool, flow, and method branches to streamline state management
This commit is contained in:
@@ -72,26 +72,50 @@ class EventListener(BaseEventListener):
|
|||||||
def current_crew_tree(self) -> Optional[Tree]:
|
def current_crew_tree(self) -> Optional[Tree]:
|
||||||
return self.formatter.current_crew_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
|
@property
|
||||||
def current_task_branch(self) -> Optional[Tree]:
|
def current_task_branch(self) -> Optional[Tree]:
|
||||||
return self.formatter.current_task_branch
|
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
|
@property
|
||||||
def current_agent_branch(self) -> Optional[Tree]:
|
def current_agent_branch(self) -> Optional[Tree]:
|
||||||
return self.formatter.current_agent_branch
|
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
|
@property
|
||||||
def current_tool_branch(self) -> Optional[Tree]:
|
def current_tool_branch(self) -> Optional[Tree]:
|
||||||
return self.formatter.current_tool_branch
|
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
|
@property
|
||||||
def current_flow_tree(self) -> Optional[Tree]:
|
def current_flow_tree(self) -> Optional[Tree]:
|
||||||
return self.formatter.current_flow_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
|
@property
|
||||||
def current_method_branch(self) -> Optional[Tree]:
|
def current_method_branch(self) -> Optional[Tree]:
|
||||||
return self.formatter.current_method_branch
|
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 -----------
|
# ----------- CREW EVENTS -----------
|
||||||
|
|
||||||
def setup_listeners(self, crewai_event_bus):
|
def setup_listeners(self, crewai_event_bus):
|
||||||
@@ -200,14 +224,14 @@ class EventListener(BaseEventListener):
|
|||||||
@crewai_event_bus.on(FlowCreatedEvent)
|
@crewai_event_bus.on(FlowCreatedEvent)
|
||||||
def on_flow_created(source, event: FlowCreatedEvent):
|
def on_flow_created(source, event: FlowCreatedEvent):
|
||||||
self._telemetry.flow_creation_span(event.flow_name)
|
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)
|
@crewai_event_bus.on(FlowStartedEvent)
|
||||||
def on_flow_started(source, event: FlowStartedEvent):
|
def on_flow_started(source, event: FlowStartedEvent):
|
||||||
self._telemetry.flow_execution_span(
|
self._telemetry.flow_execution_span(
|
||||||
event.flow_name, list(source._methods.keys())
|
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)
|
@crewai_event_bus.on(FlowFinishedEvent)
|
||||||
def on_flow_finished(source, event: FlowFinishedEvent):
|
def on_flow_finished(source, event: FlowFinishedEvent):
|
||||||
@@ -218,7 +242,8 @@ class EventListener(BaseEventListener):
|
|||||||
|
|
||||||
@crewai_event_bus.on(MethodExecutionStartedEvent)
|
@crewai_event_bus.on(MethodExecutionStartedEvent)
|
||||||
def on_method_execution_started(source, event: MethodExecutionStartedEvent):
|
def on_method_execution_started(source, event: MethodExecutionStartedEvent):
|
||||||
self.formatter.update_method_status(
|
if self.current_flow_tree:
|
||||||
|
self.current_method_branch = self.formatter.update_method_status(
|
||||||
self.current_method_branch,
|
self.current_method_branch,
|
||||||
self.current_flow_tree,
|
self.current_flow_tree,
|
||||||
event.method_name,
|
event.method_name,
|
||||||
|
|||||||
@@ -68,7 +68,6 @@ class ConsoleFormatter:
|
|||||||
|
|
||||||
def print(self, *args, **kwargs) -> None:
|
def print(self, *args, **kwargs) -> None:
|
||||||
"""Print to console with consistent formatting if verbose is enabled."""
|
"""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(
|
def print_panel(
|
||||||
@@ -239,14 +238,13 @@ class ConsoleFormatter:
|
|||||||
self.print(crew_tree)
|
self.print(crew_tree)
|
||||||
self.print()
|
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."""
|
"""Create and initialize a flow tree."""
|
||||||
# if not self.verbose:
|
|
||||||
# return None
|
|
||||||
|
|
||||||
content = self.create_status_content(
|
content = self.create_status_content(
|
||||||
"Starting Flow Execution", flow_name, "blue"
|
"Starting Flow Execution", flow_name, "blue"
|
||||||
)
|
)
|
||||||
|
|
||||||
self.print_panel(content, "Flow Execution", "blue", is_flow=True)
|
self.print_panel(content, "Flow Execution", "blue", is_flow=True)
|
||||||
|
|
||||||
# Create initial tree
|
# Create initial tree
|
||||||
@@ -264,9 +262,6 @@ class ConsoleFormatter:
|
|||||||
|
|
||||||
def start_flow(self, flow_name: str) -> Optional[Tree]:
|
def start_flow(self, flow_name: str) -> Optional[Tree]:
|
||||||
"""Initialize a flow execution tree."""
|
"""Initialize a flow execution tree."""
|
||||||
# if not self.verbose:
|
|
||||||
# return None
|
|
||||||
|
|
||||||
flow_tree = Tree("")
|
flow_tree = Tree("")
|
||||||
self.update_tree_label(flow_tree, "🌊 Flow:", flow_name, "blue", "In Progress")
|
self.update_tree_label(flow_tree, "🌊 Flow:", flow_name, "blue", "In Progress")
|
||||||
self.add_tree_node(flow_tree, "🧠 Initializing...", "yellow")
|
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"
|
self, flow_tree: Tree, flow_name: str, flow_id: str, status: str = "completed"
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Update flow status in the tree."""
|
"""Update flow status in the tree."""
|
||||||
# if not self.verbose:
|
|
||||||
# return
|
|
||||||
|
|
||||||
self.update_tree_label(
|
self.update_tree_label(
|
||||||
flow_tree,
|
flow_tree,
|
||||||
"✅ Flow Finished:" if status == "completed" else "❌ Flow Failed:",
|
"✅ Flow Finished:" if status == "completed" else "❌ Flow Failed:",
|
||||||
@@ -311,9 +303,6 @@ class ConsoleFormatter:
|
|||||||
status: str = "running",
|
status: str = "running",
|
||||||
) -> Optional[Tree]:
|
) -> Optional[Tree]:
|
||||||
"""Update method status in the flow tree."""
|
"""Update method status in the flow tree."""
|
||||||
# if not flow_tree:
|
|
||||||
# return None
|
|
||||||
|
|
||||||
if status == "running":
|
if status == "running":
|
||||||
prefix, style = "🔄 Running:", "yellow"
|
prefix, style = "🔄 Running:", "yellow"
|
||||||
elif status == "completed":
|
elif status == "completed":
|
||||||
|
|||||||
Reference in New Issue
Block a user