mirror of
https://github.com/langgenius/dify.git
synced 2026-03-27 01:00:13 +08:00
Signed-off-by: -LAN- <laipz8200@outlook.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: WH-2099 <wh2099@pm.me>
58 lines
2.1 KiB
Python
58 lines
2.1 KiB
Python
from typing import Any
|
|
|
|
from jsonschema import Draft7Validator, ValidationError
|
|
|
|
from dify_graph.enums import BuiltinNodeTypes, NodeExecutionType, WorkflowNodeExecutionStatus
|
|
from dify_graph.node_events import NodeRunResult
|
|
from dify_graph.nodes.base.node import Node
|
|
from dify_graph.nodes.start.entities import StartNodeData
|
|
from dify_graph.variables.input_entities import VariableEntityType
|
|
|
|
|
|
class StartNode(Node[StartNodeData]):
|
|
node_type = BuiltinNodeTypes.START
|
|
execution_type = NodeExecutionType.ROOT
|
|
|
|
@classmethod
|
|
def version(cls) -> str:
|
|
return "1"
|
|
|
|
def _run(self) -> NodeRunResult:
|
|
node_inputs = dict(self.graph_runtime_state.variable_pool.get_by_prefix(self.id))
|
|
self._validate_and_normalize_json_object_inputs(node_inputs)
|
|
outputs = dict(self.graph_runtime_state.variable_pool.flatten(unprefixed_node_id=self.id))
|
|
outputs.update(node_inputs)
|
|
|
|
return NodeRunResult(status=WorkflowNodeExecutionStatus.SUCCEEDED, inputs=node_inputs, outputs=outputs)
|
|
|
|
def _validate_and_normalize_json_object_inputs(self, node_inputs: dict[str, Any]) -> None:
|
|
for variable in self.node_data.variables:
|
|
if variable.type != VariableEntityType.JSON_OBJECT:
|
|
continue
|
|
|
|
key = variable.variable
|
|
value = node_inputs.get(key)
|
|
|
|
if value is None and variable.required:
|
|
raise ValueError(f"{key} is required in input form")
|
|
|
|
# If no value provided, skip further processing for this key
|
|
if not value:
|
|
continue
|
|
|
|
if not isinstance(value, dict):
|
|
raise ValueError(f"JSON object for '{key}' must be an object")
|
|
|
|
# Overwrite with normalized dict to ensure downstream consistency
|
|
node_inputs[key] = value
|
|
|
|
# If schema exists, then validate against it
|
|
schema = variable.json_schema
|
|
if not schema:
|
|
continue
|
|
|
|
try:
|
|
Draft7Validator(schema).validate(value)
|
|
except ValidationError as e:
|
|
raise ValueError(f"JSON object for '{key}' does not match schema: {e.message}")
|