Skip to content

Commit 0c5d016

Browse files
committed
Fix condition node editor compatibility
1 parent deeaccc commit 0c5d016

2 files changed

Lines changed: 70 additions & 11 deletions

File tree

src/frontend/platform/src/pages/BuildPage/flow/FlowNode/component/ConditionItem.tsx

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,27 @@ interface Item {
2424
del: boolean
2525
}
2626

27+
const normalizeConditionRule = (item?: Partial<Item>) => ({
28+
id: item?.id || generateUUID(8),
29+
left_var: typeof item?.left_var === 'string' ? item.left_var : '',
30+
left_label: typeof item?.left_label === 'string' ? item.left_label : '',
31+
comparison_operation: typeof item?.comparison_operation === 'string' ? item.comparison_operation : '',
32+
right_value_type: item?.right_value_type === 'ref' ? 'ref' : 'input',
33+
right_value: typeof item?.right_value === 'string' ? item.right_value : '',
34+
right_label: typeof item?.right_label === 'string' ? item.right_label : '',
35+
});
36+
37+
const normalizeConditionBranches = (value) => {
38+
if (!Array.isArray(value)) return [];
39+
return value.map((branch) => ({
40+
id: branch?.id || generateUUID(8),
41+
operator: branch?.operator === 'or' ? 'or' : 'and',
42+
conditions: Array.isArray(branch?.conditions)
43+
? branch.conditions.map((item) => normalizeConditionRule(item))
44+
: [],
45+
}));
46+
};
47+
2748

2849
const Item = ({ nodeId, item, index, del, required, varErrors, onUpdateItem, onDeleteItem }) => {
2950
const { t } = useTranslation('flow');
@@ -159,6 +180,11 @@ export default function ConditionItem({ nodeId, node, data: paramItem, onChange,
159180
const { t } = useTranslation('flow'); // 获取翻译函数
160181
const [value, setValue] = useState([]);
161182
const [required, setRequired] = useState(false);
183+
const normalizedParamValue = useMemo(() => normalizeConditionBranches(paramItem?.value), [paramItem?.value]);
184+
const needsParamNormalization = useMemo(
185+
() => JSON.stringify(normalizedParamValue) !== JSON.stringify(paramItem?.value ?? []),
186+
[normalizedParamValue, paramItem?.value]
187+
);
162188

163189
const handleAddCondition = () => {
164190
setRequired(false);
@@ -170,12 +196,20 @@ export default function ConditionItem({ nodeId, node, data: paramItem, onChange,
170196
};
171197

172198
useEffect(() => {
173-
if (paramItem.value && paramItem.value.length) {
174-
setValue(paramItem.value);
175-
} else {
176-
handleAddCondition();
199+
if (normalizedParamValue.length) {
200+
setValue(normalizedParamValue);
201+
if (needsParamNormalization) {
202+
onChange(normalizedParamValue);
203+
}
204+
return;
177205
}
178-
}, []);
206+
setValue((current) => {
207+
if (current.length) return current;
208+
const initialValue = [{ id: generateUUID(8), operator: 'and', conditions: [] }];
209+
onChange(initialValue);
210+
return initialValue;
211+
});
212+
}, [needsParamNormalization, normalizedParamValue, onChange, paramItem?.value]);
179213

180214
const deleteCondition = (id) => {
181215
setValue((val) => {
@@ -219,8 +253,8 @@ export default function ConditionItem({ nodeId, node, data: paramItem, onChange,
219253
setTimeout(() => {
220254
setRequired(true);
221255
}, 100);
222-
if (paramItem.value.length === 0) return t('conditionBranchCannotBeEmpty'); // 条件分支不可为空
223-
const res = paramItem.value.some((item) => {
256+
if (value.length === 0) return t('conditionBranchCannotBeEmpty'); // 条件分支不可为空
257+
const res = value.some((item) => {
224258
if (!item.conditions.length) return true;
225259
return item.conditions.some((cds) => {
226260
if (!cds.left_label) return true;
@@ -237,7 +271,7 @@ export default function ConditionItem({ nodeId, node, data: paramItem, onChange,
237271
});
238272

239273
return () => onValidate(() => { });
240-
}, [paramItem.value]);
274+
}, [onValidate, t, value]);
241275

242276
// 校验变量是否可用
243277
const { flow } = useFlowStore();
@@ -265,7 +299,7 @@ export default function ConditionItem({ nodeId, node, data: paramItem, onChange,
265299
useEffect(() => {
266300
onVarEvent && onVarEvent(validateVarAvailble);
267301
return () => onVarEvent && onVarEvent(() => { });
268-
}, [paramItem, value]);
302+
}, [onVarEvent, paramItem, value]);
269303

270304
// Update Preset Questions
271305
// const [_, forceUpdate] = useState(false)
@@ -361,4 +395,4 @@ export default function ConditionItem({ nodeId, node, data: paramItem, onChange,
361395
</Button>
362396
</div>
363397
);
364-
}
398+
}

src/frontend/platform/src/util/flowCompatible.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,35 @@ export const flowVersionCompatible = (flow) => {
1212
case 'llm': comptibleLLM(node.data); break;
1313
case 'rag': comptibleRag(node.data); break;
1414
case 'knowledge_retriever': comptibleKnowledgeRetriever(node.data); break;
15+
case 'condition': comptibleCondition(node.data); break;
1516
}
1617
})
1718
return flow
1819
}
20+
21+
const normalizeConditionRule = (rule) => ({
22+
id: rule?.id || generateUUID(8),
23+
left_var: typeof rule?.left_var === 'string' ? rule.left_var : '',
24+
left_label: typeof rule?.left_label === 'string' ? rule.left_label : '',
25+
comparison_operation: typeof rule?.comparison_operation === 'string' ? rule.comparison_operation : '',
26+
right_value_type: rule?.right_value_type === 'ref' ? 'ref' : 'input',
27+
right_value: typeof rule?.right_value === 'string' ? rule.right_value : '',
28+
right_label: typeof rule?.right_label === 'string' ? rule.right_label : '',
29+
});
30+
31+
const comptibleCondition = (node) => {
32+
if (!Array.isArray(node?.group_params) || !node.group_params[0]?.params?.length) return;
33+
34+
const conditionParam = node.group_params[0].params.find((param) => param.key === 'condition');
35+
if (!conditionParam) return;
36+
37+
const rawBranches = Array.isArray(conditionParam.value) ? conditionParam.value : [];
38+
conditionParam.value = rawBranches.map((branch) => ({
39+
id: branch?.id || generateUUID(8),
40+
operator: branch?.operator === 'or' ? 'or' : 'and',
41+
conditions: Array.isArray(branch?.conditions) ? branch.conditions.map(normalizeConditionRule) : [],
42+
}));
43+
}
1944
const comptibleRag = (node) => {
2045
if (!node.v) {
2146
node.v = 1
@@ -391,4 +416,4 @@ const comptibleLLM = (node) => {
391416

392417
node.v = 2
393418
}
394-
}
419+
}

0 commit comments

Comments
 (0)