|
| 1 | +import copy |
| 2 | +from typing import Any, Optional |
| 3 | + |
| 4 | + |
| 5 | +_EDITOR_FLOW_NODE_TYPE = 'flowNode' |
| 6 | +_EDITOR_NOTE_NODE_TYPE = 'noteNode' |
| 7 | +_CONDITION_NODE_TYPE = 'condition' |
| 8 | +_CONDITION_PARAM_KEY = 'condition' |
| 9 | +_EDITOR_CONDITION_RIGHT_VALUE_TYPES = {'input', 'ref'} |
| 10 | + |
| 11 | + |
| 12 | +def _normalize_editor_condition_cases(condition_cases: Any) -> Any: |
| 13 | + if not isinstance(condition_cases, list): |
| 14 | + return condition_cases |
| 15 | + |
| 16 | + normalized_cases = [] |
| 17 | + for raw_case in condition_cases: |
| 18 | + if not isinstance(raw_case, dict): |
| 19 | + normalized_cases.append(raw_case) |
| 20 | + continue |
| 21 | + |
| 22 | + case = copy.deepcopy(raw_case) |
| 23 | + conditions = case.get('conditions') |
| 24 | + if isinstance(conditions, list): |
| 25 | + normalized_conditions = [] |
| 26 | + for raw_condition in conditions: |
| 27 | + if not isinstance(raw_condition, dict): |
| 28 | + normalized_conditions.append(raw_condition) |
| 29 | + continue |
| 30 | + |
| 31 | + condition = copy.deepcopy(raw_condition) |
| 32 | + condition.setdefault('left_label', '') |
| 33 | + condition.setdefault('right_label', '') |
| 34 | + if condition.get('left_var') is None: |
| 35 | + condition['left_var'] = '' |
| 36 | + if condition.get('right_value') is None: |
| 37 | + condition['right_value'] = '' |
| 38 | + if condition.get('comparison_operation') is None: |
| 39 | + condition['comparison_operation'] = '' |
| 40 | + |
| 41 | + right_value_type = condition.get('right_value_type') |
| 42 | + if right_value_type not in _EDITOR_CONDITION_RIGHT_VALUE_TYPES: |
| 43 | + condition['right_value_type'] = 'ref' if right_value_type == 'ref' else 'input' |
| 44 | + |
| 45 | + normalized_conditions.append(condition) |
| 46 | + case['conditions'] = normalized_conditions |
| 47 | + |
| 48 | + normalized_cases.append(case) |
| 49 | + |
| 50 | + return normalized_cases |
| 51 | + |
| 52 | + |
| 53 | +def normalize_workflow_editor_graph(graph_data: Optional[dict], *, in_place: bool = False) -> Optional[dict]: |
| 54 | + if not isinstance(graph_data, dict): |
| 55 | + return graph_data |
| 56 | + |
| 57 | + nodes = graph_data.get('nodes') |
| 58 | + if not isinstance(nodes, list): |
| 59 | + return graph_data |
| 60 | + |
| 61 | + normalized_graph = graph_data if in_place else copy.deepcopy(graph_data) |
| 62 | + for node in normalized_graph.get('nodes', []): |
| 63 | + if not isinstance(node, dict): |
| 64 | + continue |
| 65 | + |
| 66 | + node_data = node.get('data') |
| 67 | + if not isinstance(node_data, dict): |
| 68 | + continue |
| 69 | + |
| 70 | + node_type = node_data.get('type') |
| 71 | + if node_type: |
| 72 | + if node.get('type') not in {_EDITOR_FLOW_NODE_TYPE, _EDITOR_NOTE_NODE_TYPE}: |
| 73 | + node['type'] = _EDITOR_NOTE_NODE_TYPE if node_type == 'note' else _EDITOR_FLOW_NODE_TYPE |
| 74 | + |
| 75 | + if node_type == _CONDITION_NODE_TYPE: |
| 76 | + for group in node_data.get('group_params', []): |
| 77 | + if not isinstance(group, dict): |
| 78 | + continue |
| 79 | + for param in group.get('params', []): |
| 80 | + if not isinstance(param, dict): |
| 81 | + continue |
| 82 | + if param.get('key') == _CONDITION_PARAM_KEY: |
| 83 | + param['value'] = _normalize_editor_condition_cases(param.get('value') or []) |
| 84 | + |
| 85 | + return normalized_graph |
0 commit comments