From b7308dca5ddc8c0a826697012ea62594de401555 Mon Sep 17 00:00:00 2001 From: Mark Backman Date: Fri, 15 Nov 2024 15:44:25 -0500 Subject: [PATCH] Fix issue where actions would execute on terminating nodes --- src/pipecat/flows/manager.py | 8 ++++++-- src/pipecat/flows/state.py | 7 ++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/pipecat/flows/manager.py b/src/pipecat/flows/manager.py index 2a94c99d5..83e1f858e 100644 --- a/src/pipecat/flows/manager.py +++ b/src/pipecat/flows/manager.py @@ -165,12 +165,16 @@ class FlowManager: raise RuntimeError("FlowManager must be initialized before handling transitions") available_functions = self.flow.get_available_function_names() + current_node = self.flow.get_current_node() if function_name in available_functions: new_node = self.flow.transition(function_name) if new_node: + # Only execute actions if we actually changed nodes + is_new_node = new_node != current_node + # Execute pre-actions before updating LLM context - if self.flow.get_current_pre_actions(): + if is_new_node and self.flow.get_current_pre_actions(): logger.debug(f"Executing pre-actions for node {new_node}") await self._execute_actions(self.flow.get_current_pre_actions()) @@ -182,7 +186,7 @@ class FlowManager: ) # Execute post-actions after updating LLM context - if self.flow.get_current_post_actions(): + if is_new_node and self.flow.get_current_post_actions(): logger.debug(f"Executing post-actions for node {new_node}") await self._execute_actions(self.flow.get_current_post_actions()) diff --git a/src/pipecat/flows/state.py b/src/pipecat/flows/state.py index 5c7464b8b..21b9939d4 100644 --- a/src/pipecat/flows/state.py +++ b/src/pipecat/flows/state.py @@ -146,11 +146,16 @@ class FlowState: if function_name in available_functions: if function_name in self.nodes: # Regular transition to a new node + previous_node = self.current_node self.current_node = function_name - logger.info(f"Transitioned to node: {self.current_node}") + logger.info(f"Transitioned from {previous_node} to node: {self.current_node}") return self.current_node else: # Handle terminal function calls (functions that don't lead to new nodes) logger.info(f"Executed terminal function: {function_name}") return self.current_node return None + + def get_current_node(self) -> str: + """Get the current node ID.""" + return self.current_node