feat(workflow): support multi-variable assignment in assigner node

This commit is contained in:
mengyonghao
2025-12-23 17:45:37 +08:00
parent ac1afeffaf
commit d423e80ddb
2 changed files with 61 additions and 50 deletions

View File

@@ -1,21 +1,32 @@
from pydantic import Field from pydantic import Field, BaseModel
from app.core.workflow.nodes.base_config import BaseNodeConfig from app.core.workflow.nodes.base_config import BaseNodeConfig
from app.core.workflow.nodes.enums import AssignmentOperator from app.core.workflow.nodes.enums import AssignmentOperator
class AssignerNodeConfig(BaseNodeConfig): class AssignmentItem(BaseModel):
"""
Single assignment definition.
"""
variable_selector: str | list[str] = Field( variable_selector: str | list[str] = Field(
..., ...,
description="Variables to be assigned", description="Target variable name(s) to assign",
) )
operation: AssignmentOperator = Field( operation: AssignmentOperator = Field(
..., ...,
description="Operator to assign", description="Assignment operator",
) )
value: str | list[str] = Field( value: str | list[str] = Field(
..., ...,
description="Values to assign", description="Value(s) to assign to the variable(s)",
)
class AssignerNodeConfig(BaseNodeConfig):
assignments: list[AssignmentItem] = Field(
...,
description="List of variable assignment definitions",
) )

View File

@@ -29,9 +29,9 @@ class AssignerNode(BaseNode):
""" """
# Initialize a variable pool for accessing conversation, node, and system variables # Initialize a variable pool for accessing conversation, node, and system variables
pool = VariablePool(state) pool = VariablePool(state)
for assignment in self.typed_config.assignments:
# Get the target variable selector (e.g., "conv.test") # Get the target variable selector (e.g., "conv.test")
variable_selector = self.typed_config.variable_selector variable_selector = assignment.variable_selector
if isinstance(variable_selector, str): if isinstance(variable_selector, str):
# Support dot-separated string paths, e.g., "conv.test" -> ["conv", "test"] # Support dot-separated string paths, e.g., "conv.test" -> ["conv", "test"]
variable_selector = variable_selector.split('.') variable_selector = variable_selector.split('.')
@@ -41,7 +41,7 @@ class AssignerNode(BaseNode):
raise ValueError("Only conversation variables can be assigned.") raise ValueError("Only conversation variables can be assigned.")
# Get the value or expression to assign # Get the value or expression to assign
value = self.typed_config.value value = assignment.value
if isinstance(value, list): if isinstance(value, list):
value = '.'.join(value) value = '.'.join(value)
value = ExpressionEvaluator.evaluate( value = ExpressionEvaluator.evaluate(
@@ -57,7 +57,7 @@ class AssignerNode(BaseNode):
) )
# Execute the configured assignment operation # Execute the configured assignment operation
match self.typed_config.operation: match assignment.operation:
case AssignmentOperator.ASSIGN: case AssignmentOperator.ASSIGN:
operator.assign() operator.assign()
case AssignmentOperator.CLEAR: case AssignmentOperator.CLEAR:
@@ -77,4 +77,4 @@ class AssignerNode(BaseNode):
case AssignmentOperator.REMOVE_LAST: case AssignmentOperator.REMOVE_LAST:
operator.remove_last() operator.remove_last()
case _: case _:
raise ValueError(f"Invalid Operator: {self.typed_config.operation}") raise ValueError(f"Invalid Operator: {assignment.operation}")