feat(end_stream_processor): support streaming output of all configured variables in End node

- Iterate all variable selectors for all end nodes and stream if matched
- Add detailed English comments explaining each step of the logic
- This enables streaming output for multiple variables (e.g., text, text1, text2...) instead of only the first one
pull/21981/head
YiQiu Jia 11 months ago
parent 8f723697ef
commit 151fe3d02e

@ -42,23 +42,26 @@ class EndStreamProcessor(StreamProcessor):
yield event yield event
continue continue
if event.route_node_state.node_id in self.current_stream_chunk_generating_node_ids: # Iterate all variable selectors for all end nodes; stream if matched
stream_out_end_node_ids = self.current_stream_chunk_generating_node_ids[ matched = False # Flag to indicate if a match is found
event.route_node_state.node_id for end_node_id, value_selectors_list in self.end_stream_param.end_stream_variable_selector_mapping.items():
] # Loop through all variable selectors configured for the current end node
else: for value_selector in value_selectors_list:
stream_out_end_node_ids = self._get_stream_out_end_node_ids(event) # Check if the current event's from_variable_selector matches the configured selector
self.current_stream_chunk_generating_node_ids[event.route_node_state.node_id] = ( if event.from_variable_selector == value_selector:
stream_out_end_node_ids # If there was previous output and this node hasn't output yet, prepend a newline
) if self.has_output and event.node_id not in self.output_node_ids:
event.chunk_content = "\n" + event.chunk_content
if stream_out_end_node_ids: # Mark this node as having output to avoid duplicate newlines
if self.has_output and event.node_id not in self.output_node_ids: self.output_node_ids.add(event.node_id)
event.chunk_content = "\n" + event.chunk_content # Set the flag indicating output has occurred
self.has_output = True
self.output_node_ids.add(event.node_id) # Stream the current event
self.has_output = True yield event
yield event matched = True # Set matched flag to break out of loops
break # Exit inner loop after match
if matched:
break # Exit outer loop after match
elif isinstance(event, NodeRunSucceededEvent): elif isinstance(event, NodeRunSucceededEvent):
yield event yield event
if event.route_node_state.node_id in self.current_stream_chunk_generating_node_ids: if event.route_node_state.node_id in self.current_stream_chunk_generating_node_ids:

Loading…
Cancel
Save