Files
dify/api/dify_graph/nodes/loop/loop_node.py
T

435 lines
19 KiB
Python
Raw Normal View History

import contextlib
2025-04-02 13:53:26 +08:00
import json
2025-03-05 17:41:15 +08:00
import logging
+9
2025-09-18 12:49:10 +08:00
from collections.abc import Callable, Generator, Mapping, Sequence
from datetime import datetime
from typing import TYPE_CHECKING, Any, Literal, cast
from dify_graph.entities.graph_config import NodeConfigDictAdapter
from dify_graph.enums import (
BuiltinNodeTypes,
+9
2025-09-18 12:49:10 +08:00
NodeExecutionType,
WorkflowNodeExecutionMetadataKey,
WorkflowNodeExecutionStatus,
2025-04-02 13:53:26 +08:00
)
from dify_graph.graph_events import (
+9
2025-09-18 12:49:10 +08:00
GraphNodeEventBase,
2025-03-05 17:41:15 +08:00
GraphRunFailedEvent,
NodeRunSucceededEvent,
)
from dify_graph.model_runtime.entities.llm_entities import LLMUsage
from dify_graph.node_events import (
+9
2025-09-18 12:49:10 +08:00
LoopFailedEvent,
LoopNextEvent,
LoopStartedEvent,
LoopSucceededEvent,
NodeEventBase,
NodeRunResult,
StreamCompletedEvent,
)
from dify_graph.nodes.base import LLMUsageTrackingMixin
from dify_graph.nodes.base.node import Node
from dify_graph.nodes.loop.entities import LoopCompletedReason, LoopNodeData, LoopVariableData
from dify_graph.utils.condition.processor import ConditionProcessor
from dify_graph.variables import Segment, SegmentType
+9
2025-09-18 12:49:10 +08:00
from factories.variable_factory import TypeMismatchError, build_segment_with_type, segment_to_variable
from libs.datetime_utils import naive_utc_now
2025-03-05 17:41:15 +08:00
2025-04-02 13:53:26 +08:00
if TYPE_CHECKING:
from dify_graph.graph_engine import GraphEngine
2025-04-02 13:53:26 +08:00
2025-03-05 17:41:15 +08:00
logger = logging.getLogger(__name__)
2024-05-27 22:01:11 +08:00
class LoopNode(LLMUsageTrackingMixin, Node[LoopNodeData]):
2024-05-27 22:01:11 +08:00
"""
Loop Node.
"""
node_type = BuiltinNodeTypes.LOOP
+9
2025-09-18 12:49:10 +08:00
execution_type = NodeExecutionType.CONTAINER
@classmethod
def version(cls) -> str:
return "1"
+9
2025-09-18 12:49:10 +08:00
def _run(self) -> Generator:
2025-03-05 17:41:15 +08:00
"""Run the node."""
# Get inputs
loop_count = self.node_data.loop_count
break_conditions = self.node_data.break_conditions
logical_operator = self.node_data.logical_operator
2025-03-05 17:41:15 +08:00
inputs = {"loop_count": loop_count}
if not self.node_data.start_node_id:
+9
2025-09-18 12:49:10 +08:00
raise ValueError(f"field start_node_id in loop {self._node_id} not found")
2025-03-05 17:41:15 +08:00
root_node_id = self.node_data.start_node_id
2025-03-05 17:41:15 +08:00
+9
2025-09-18 12:49:10 +08:00
# Initialize loop variables in the original variable pool
2025-04-02 13:53:26 +08:00
loop_variable_selectors = {}
if self.node_data.loop_variables:
+9
2025-09-18 12:49:10 +08:00
value_processor: dict[Literal["constant", "variable"], Callable[[LoopVariableData], Segment | None]] = {
"constant": lambda var: self._get_segment_for_constant(var.var_type, var.value),
"variable": lambda var: (
self.graph_runtime_state.variable_pool.get(var.value) if isinstance(var.value, list) else None
),
+9
2025-09-18 12:49:10 +08:00
}
for loop_variable in self.node_data.loop_variables:
2025-04-02 13:53:26 +08:00
if loop_variable.value_type not in value_processor:
raise ValueError(
f"Invalid value type '{loop_variable.value_type}' for loop variable {loop_variable.label}"
)
+9
2025-09-18 12:49:10 +08:00
processed_segment = value_processor[loop_variable.value_type](loop_variable)
2025-04-02 13:53:26 +08:00
if not processed_segment:
raise ValueError(f"Invalid value for loop variable {loop_variable.label}")
+9
2025-09-18 12:49:10 +08:00
variable_selector = [self._node_id, loop_variable.label]
variable = segment_to_variable(segment=processed_segment, selector=variable_selector)
self.graph_runtime_state.variable_pool.add(variable_selector, variable.value)
2025-04-02 13:53:26 +08:00
loop_variable_selectors[loop_variable.label] = variable_selector
inputs[loop_variable.label] = processed_segment.value
start_at = naive_utc_now()
2025-03-05 17:41:15 +08:00
condition_processor = ConditionProcessor()
+9
2025-09-18 12:49:10 +08:00
loop_duration_map: dict[str, float] = {}
single_loop_variable_map: dict[str, dict[str, Any]] = {} # single loop variable output
loop_usage = LLMUsage.empty_usage()
loop_node_ids = self._extract_loop_node_ids_from_config(self.graph_config, self._node_id)
+9
2025-09-18 12:49:10 +08:00
2025-03-05 17:41:15 +08:00
# Start Loop event
+9
2025-09-18 12:49:10 +08:00
yield LoopStartedEvent(
2025-03-05 17:41:15 +08:00
start_at=start_at,
inputs=inputs,
metadata={"loop_length": loop_count},
)
try:
+9
2025-09-18 12:49:10 +08:00
reach_break_condition = False
if break_conditions:
with contextlib.suppress(ValueError):
_, _, reach_break_condition = condition_processor.process_conditions(
variable_pool=self.graph_runtime_state.variable_pool,
conditions=break_conditions,
operator=logical_operator,
)
+9
2025-09-18 12:49:10 +08:00
if reach_break_condition:
loop_count = 0
2025-03-05 17:41:15 +08:00
+9
2025-09-18 12:49:10 +08:00
for i in range(loop_count):
# Clear stale variables from previous loop iterations to avoid streaming old values
self._clear_loop_subgraph_variables(loop_node_ids)
+9
2025-09-18 12:49:10 +08:00
graph_engine = self._create_graph_engine(start_at=start_at, root_node_id=root_node_id)
loop_start_time = naive_utc_now()
reach_break_node = yield from self._run_single_loop(graph_engine=graph_engine, current_index=i)
# Track loop duration
loop_duration_map[str(i)] = (naive_utc_now() - loop_start_time).total_seconds()
# Accumulate outputs from the sub-graph's response nodes
for key, value in graph_engine.graph_runtime_state.outputs.items():
if key == "answer":
# Concatenate answer outputs with newline
existing_answer = self.graph_runtime_state.get_output("answer", "")
if existing_answer:
self.graph_runtime_state.set_output("answer", f"{existing_answer}{value}")
else:
self.graph_runtime_state.set_output("answer", value)
else:
# For other outputs, just update
self.graph_runtime_state.set_output(key, value)
# Accumulate usage from the sub-graph execution
loop_usage = self._merge_usage(loop_usage, graph_engine.graph_runtime_state.llm_usage)
+9
2025-09-18 12:49:10 +08:00
# Collect loop variable values after iteration
2025-04-02 13:53:26 +08:00
single_loop_variable = {}
for key, selector in loop_variable_selectors.items():
+9
2025-09-18 12:49:10 +08:00
segment = self.graph_runtime_state.variable_pool.get(selector)
single_loop_variable[key] = segment.value if segment else None
2025-03-05 17:41:15 +08:00
2025-04-02 13:53:26 +08:00
single_loop_variable_map[str(i)] = single_loop_variable
+9
2025-09-18 12:49:10 +08:00
if reach_break_node:
2025-03-05 17:41:15 +08:00
break
+9
2025-09-18 12:49:10 +08:00
if break_conditions:
_, _, reach_break_condition = condition_processor.process_conditions(
variable_pool=self.graph_runtime_state.variable_pool,
conditions=break_conditions,
operator=logical_operator,
)
if reach_break_condition:
break
yield LoopNextEvent(
index=i + 1,
pre_loop_output=self.node_data.outputs,
+9
2025-09-18 12:49:10 +08:00
)
self._accumulate_usage(loop_usage)
2025-03-05 17:41:15 +08:00
# Loop completed successfully
+9
2025-09-18 12:49:10 +08:00
yield LoopSucceededEvent(
2025-03-05 17:41:15 +08:00
start_at=start_at,
inputs=inputs,
outputs=self.node_data.outputs,
2025-03-05 17:41:15 +08:00
steps=loop_count,
metadata={
WorkflowNodeExecutionMetadataKey.TOTAL_TOKENS: loop_usage.total_tokens,
WorkflowNodeExecutionMetadataKey.TOTAL_PRICE: loop_usage.total_price,
WorkflowNodeExecutionMetadataKey.CURRENCY: loop_usage.currency,
WorkflowNodeExecutionMetadataKey.COMPLETED_REASON: (
LoopCompletedReason.LOOP_BREAK
if reach_break_condition
else LoopCompletedReason.LOOP_COMPLETED.value
),
WorkflowNodeExecutionMetadataKey.LOOP_DURATION_MAP: loop_duration_map,
WorkflowNodeExecutionMetadataKey.LOOP_VARIABLE_MAP: single_loop_variable_map,
2025-03-05 17:41:15 +08:00
},
)
+9
2025-09-18 12:49:10 +08:00
yield StreamCompletedEvent(
node_run_result=NodeRunResult(
2025-03-05 17:41:15 +08:00
status=WorkflowNodeExecutionStatus.SUCCEEDED,
2025-04-02 13:53:26 +08:00
metadata={
WorkflowNodeExecutionMetadataKey.TOTAL_TOKENS: loop_usage.total_tokens,
WorkflowNodeExecutionMetadataKey.TOTAL_PRICE: loop_usage.total_price,
WorkflowNodeExecutionMetadataKey.CURRENCY: loop_usage.currency,
WorkflowNodeExecutionMetadataKey.LOOP_DURATION_MAP: loop_duration_map,
WorkflowNodeExecutionMetadataKey.LOOP_VARIABLE_MAP: single_loop_variable_map,
2025-04-02 13:53:26 +08:00
},
outputs=self.node_data.outputs,
2025-04-02 13:53:26 +08:00
inputs=inputs,
llm_usage=loop_usage,
2025-03-05 17:41:15 +08:00
)
)
except Exception as e:
self._accumulate_usage(loop_usage)
+9
2025-09-18 12:49:10 +08:00
yield LoopFailedEvent(
2025-03-05 17:41:15 +08:00
start_at=start_at,
inputs=inputs,
steps=loop_count,
metadata={
WorkflowNodeExecutionMetadataKey.TOTAL_TOKENS: loop_usage.total_tokens,
WorkflowNodeExecutionMetadataKey.TOTAL_PRICE: loop_usage.total_price,
WorkflowNodeExecutionMetadataKey.CURRENCY: loop_usage.currency,
2025-03-05 17:41:15 +08:00
"completed_reason": "error",
WorkflowNodeExecutionMetadataKey.LOOP_DURATION_MAP: loop_duration_map,
WorkflowNodeExecutionMetadataKey.LOOP_VARIABLE_MAP: single_loop_variable_map,
2025-03-05 17:41:15 +08:00
},
error=str(e),
)
+9
2025-09-18 12:49:10 +08:00
yield StreamCompletedEvent(
node_run_result=NodeRunResult(
2025-03-05 17:41:15 +08:00
status=WorkflowNodeExecutionStatus.FAILED,
error=str(e),
2025-04-02 13:53:26 +08:00
metadata={
WorkflowNodeExecutionMetadataKey.TOTAL_TOKENS: loop_usage.total_tokens,
WorkflowNodeExecutionMetadataKey.TOTAL_PRICE: loop_usage.total_price,
WorkflowNodeExecutionMetadataKey.CURRENCY: loop_usage.currency,
WorkflowNodeExecutionMetadataKey.LOOP_DURATION_MAP: loop_duration_map,
WorkflowNodeExecutionMetadataKey.LOOP_VARIABLE_MAP: single_loop_variable_map,
2025-04-02 13:53:26 +08:00
},
llm_usage=loop_usage,
2025-03-05 17:41:15 +08:00
)
)
2025-04-02 13:53:26 +08:00
def _run_single_loop(
self,
*,
graph_engine: "GraphEngine",
current_index: int,
+9
2025-09-18 12:49:10 +08:00
) -> Generator[NodeEventBase | GraphNodeEventBase, None, bool]:
reach_break_node = False
for event in graph_engine.run():
if isinstance(event, GraphNodeEventBase):
self._append_loop_info_to_event(event=event, loop_run_index=current_index)
2025-04-02 13:53:26 +08:00
if isinstance(event, GraphNodeEventBase) and event.node_type == BuiltinNodeTypes.LOOP_START:
2025-04-02 13:53:26 +08:00
continue
+9
2025-09-18 12:49:10 +08:00
if isinstance(event, GraphNodeEventBase):
yield event
if isinstance(event, NodeRunSucceededEvent) and event.node_type == BuiltinNodeTypes.LOOP_END:
+9
2025-09-18 12:49:10 +08:00
reach_break_node = True
if isinstance(event, GraphRunFailedEvent):
raise Exception(event.error)
2025-04-02 13:53:26 +08:00
for loop_var in self.node_data.loop_variables or []:
+9
2025-09-18 12:49:10 +08:00
key, sel = loop_var.label, [self._node_id, loop_var.label]
segment = self.graph_runtime_state.variable_pool.get(sel)
self.node_data.outputs[key] = segment.value if segment else None
self.node_data.outputs["loop_round"] = current_index + 1
2025-04-02 13:53:26 +08:00
+9
2025-09-18 12:49:10 +08:00
return reach_break_node
2025-04-02 13:53:26 +08:00
+9
2025-09-18 12:49:10 +08:00
def _append_loop_info_to_event(
self,
event: GraphNodeEventBase,
loop_run_index: int,
):
event.in_loop_id = self._node_id
loop_metadata = {
WorkflowNodeExecutionMetadataKey.LOOP_ID: self._node_id,
WorkflowNodeExecutionMetadataKey.LOOP_INDEX: loop_run_index,
}
+9
2025-09-18 12:49:10 +08:00
current_metadata = event.node_run_result.metadata
if WorkflowNodeExecutionMetadataKey.LOOP_ID not in current_metadata:
event.node_run_result.metadata = {**current_metadata, **loop_metadata}
2024-05-27 22:01:11 +08:00
def _clear_loop_subgraph_variables(self, loop_node_ids: set[str]) -> None:
"""
Remove variables produced by loop sub-graph nodes from previous iterations.
Keeping stale variables causes a freshly created response coordinator in the
next iteration to fall back to outdated values when no stream chunks exist.
"""
variable_pool = self.graph_runtime_state.variable_pool
for node_id in loop_node_ids:
variable_pool.remove([node_id])
@classmethod
2025-03-05 17:41:15 +08:00
def _extract_variable_selector_to_variable_mapping(
cls,
*,
graph_config: Mapping[str, Any],
node_id: str,
node_data: LoopNodeData,
2025-03-05 17:41:15 +08:00
) -> Mapping[str, Sequence[str]]:
variable_mapping = {}
# Extract loop node IDs statically from graph_config
+9
2025-09-18 12:49:10 +08:00
loop_node_ids = cls._extract_loop_node_ids_from_config(graph_config, node_id)
+9
2025-09-18 12:49:10 +08:00
# Get node configs from graph_config
+9
2025-09-18 12:49:10 +08:00
node_configs = {node["id"]: node for node in graph_config.get("nodes", []) if "id" in node}
for sub_node_id, sub_node_config in node_configs.items():
2025-03-05 17:41:15 +08:00
if sub_node_config.get("data", {}).get("loop_id") != node_id:
continue
# variable selector to variable mapping
try:
typed_sub_node_config = NodeConfigDictAdapter.validate_python(sub_node_config)
node_type = typed_sub_node_config["data"].type
node_mapping = Node.get_node_type_classes_mapping()
if node_type not in node_mapping:
2025-03-05 17:41:15 +08:00
continue
node_version = str(typed_sub_node_config["data"].version)
node_cls = node_mapping[node_type][node_version]
2025-03-05 17:41:15 +08:00
sub_node_variable_mapping = node_cls.extract_variable_selector_to_variable_mapping(
graph_config=graph_config, config=typed_sub_node_config
2025-03-05 17:41:15 +08:00
)
sub_node_variable_mapping = cast(dict[str, Sequence[str]], sub_node_variable_mapping)
except NotImplementedError:
sub_node_variable_mapping = {}
# remove loop variables
sub_node_variable_mapping = {
sub_node_id + "." + key: value
for key, value in sub_node_variable_mapping.items()
if value[0] != node_id
}
variable_mapping.update(sub_node_variable_mapping)
for loop_variable in node_data.loop_variables or []:
if loop_variable.value_type == "variable":
assert loop_variable.value is not None, "Loop variable value must be provided for variable type"
# add loop variable to variable mapping
selector = loop_variable.value
variable_mapping[f"{node_id}.{loop_variable.label}"] = selector
2025-03-05 17:41:15 +08:00
# remove variable out from loop
variable_mapping = {key: value for key, value in variable_mapping.items() if value[0] not in loop_node_ids}
2025-03-05 17:41:15 +08:00
return variable_mapping
2025-04-02 13:53:26 +08:00
@classmethod
def _extract_loop_node_ids_from_config(cls, graph_config: Mapping[str, Any], loop_node_id: str) -> set[str]:
"""
Extract node IDs that belong to a specific loop from graph configuration.
This method statically analyzes the graph configuration to find all nodes
that are part of the specified loop, without creating actual node instances.
:param graph_config: the complete graph configuration
:param loop_node_id: the ID of the loop node
:return: set of node IDs that belong to the loop
"""
loop_node_ids = set()
# Find all nodes that belong to this loop
nodes = graph_config.get("nodes", [])
for node in nodes:
node_data = node.get("data", {})
if node_data.get("loop_id") == loop_node_id:
node_id = node.get("id")
if node_id:
loop_node_ids.add(node_id)
return loop_node_ids
2025-04-02 13:53:26 +08:00
@staticmethod
def _get_segment_for_constant(var_type: SegmentType, original_value: Any) -> Segment:
2025-04-02 13:53:26 +08:00
"""Get the appropriate segment type for a constant value."""
# TODO: Refactor for maintainability:
# 1. Ensure type handling logic stays synchronized with _VALID_VAR_TYPE (entities.py)
# 2. Consider moving this method to LoopVariableData class for better encapsulation
if not var_type.is_array_type() or var_type == SegmentType.ARRAY_BOOLEAN:
value = original_value
elif var_type in [
SegmentType.ARRAY_NUMBER,
SegmentType.ARRAY_OBJECT,
SegmentType.ARRAY_STRING,
]:
if original_value and isinstance(original_value, str):
value = json.loads(original_value)
2025-04-02 13:53:26 +08:00
else:
logger.warning("unexpected value for LoopNode, value_type=%s, value=%s", original_value, var_type)
2025-04-02 13:53:26 +08:00
value = []
else:
raise AssertionError("this statement should be unreachable.")
try:
return build_segment_with_type(var_type, value=value)
except TypeMismatchError as type_exc:
# Attempt to parse the value as a JSON-encoded string, if applicable.
if not isinstance(original_value, str):
raise
try:
value = json.loads(original_value)
except ValueError:
raise type_exc
return build_segment_with_type(var_type, value)
+9
2025-09-18 12:49:10 +08:00
def _create_graph_engine(self, start_at: datetime, root_node_id: str):
from dify_graph.entities import GraphInitParams
from dify_graph.runtime import GraphRuntimeState
+9
2025-09-18 12:49:10 +08:00
# Create GraphInitParams for child graph execution.
+9
2025-09-18 12:49:10 +08:00
graph_init_params = GraphInitParams(
workflow_id=self.workflow_id,
graph_config=self.graph_config,
run_context=self.run_context,
+9
2025-09-18 12:49:10 +08:00
call_depth=self.workflow_call_depth,
)
# Create a new GraphRuntimeState for this iteration
graph_runtime_state_copy = GraphRuntimeState(
variable_pool=self.graph_runtime_state.variable_pool,
start_at=start_at.timestamp(),
)
return self.graph_runtime_state.create_child_engine(
+9
2025-09-18 12:49:10 +08:00
workflow_id=self.workflow_id,
graph_init_params=graph_init_params,
+9
2025-09-18 12:49:10 +08:00
graph_runtime_state=graph_runtime_state_copy,
graph_config=self.graph_config,
root_node_id=root_node_id,
+9
2025-09-18 12:49:10 +08:00
)