<template>
|
<NodeBasicLayout v-model:title="data.title" :type="NodeType.Condition" :description="VueFlowHelper.getConfigValue(data, 'description', '根据条件表达式执行不同的分支。')">
|
<Handle :id="leftId" type="target" :position="Position.Left" />
|
<FieldLayout
|
v-for="(item, index) in conditionGroupList"
|
:key="item.id"
|
class="relative group/conditionGroup"
|
:title="item.conditions ? '如果' : '否则'"
|
>
|
<template #right v-if="item.conditions">
|
<el-select v-model="item.operator" class="w-[130px]">
|
<el-option
|
v-for="item in Object.keys(conditionOperatorMap)"
|
:key="item"
|
:value="item"
|
:label="conditionOperatorMap[item]"
|
></el-option>
|
</el-select>
|
<!-- v-if="conditionGroupList.length > 1" -->
|
|
<span
|
class="ywifont ywicon-shanchu text-red-400 invisible group-hover/conditionGroup:visible cursor-pointer"
|
@click="delConditionBranch(index)"
|
></span>
|
</template>
|
|
<div class="flex flex-col gap-y-2" v-if="item.conditions">
|
<div v-for="(subItem, subIndex) in item.conditions" class="ml-5 flex-items-center gap-x-2 group/conditionItem pr-8">
|
<el-input filterable class="w-[120px] flex-0" v-model="subItem.left_value" placeholder="输入变量"> </el-input>
|
<el-select filterable v-model="subItem.comparison_operation" class="flex-0 w-[120px]" placeholder="选择条件">
|
<el-option
|
v-for="item in Object.keys(compareOperationMap)"
|
:key="item"
|
:value="item"
|
:label="compareOperationMap[item]"
|
></el-option>
|
</el-select>
|
|
<!-- <el-select v-model="subItem.right_value_type" class="flex-0 w-[90px]" placeholder="请选择">
|
<el-option v-for="item in Object.keys(varTypeMap)" :key="item" :value="item" :label="varTypeMap[item]"></el-option>
|
</el-select> -->
|
<template v-if="isShowRight(subItem)">
|
<el-input
|
v-if="subItem.right_value_type === VarType.Input"
|
v-model="subItem.right_value"
|
class="w-[180px]"
|
placeholder="输入值"
|
>
|
</el-input>
|
|
<el-tree-select
|
v-else
|
filterable
|
class="w-[120px] flex-0"
|
v-model="subItem.right_value"
|
:data="treeReferOptions"
|
node-key="id"
|
:clearable="true"
|
:accordion="false"
|
:expandNode="false"
|
:check-strictly="false"
|
placeholder="选择变量"
|
>
|
</el-tree-select>
|
</template>
|
|
<span
|
class="ywifont ywicon-shanchu text-red-400 invisible group-hover/conditionItem:visible cursor-pointer"
|
@click="delConditionItem(item, subIndex)"
|
></span>
|
</div>
|
<el-button class="w-fit mt-3" type="primary" @click="addConditionItem(item)">添加条件</el-button>
|
</div>
|
<el-button v-else @click="addConditionBranch" class="w-fit mt-3" type="success">添加分支</el-button>
|
<Handle class="!right-0 !-translate-y-4" :id="item.id" type="source" :position="Position.Right" />
|
</FieldLayout>
|
</NodeBasicLayout>
|
</template>
|
|
<script lang="ts" setup>
|
import type { NodeProps } from '@vue-flow/core';
|
import { Handle, Position, useNode, useVueFlow } from '@vue-flow/core';
|
import { computed, ref, watchEffect } from 'vue';
|
import { ConditionHelper, VueFlowHelper } from '../../VueFlowHelper';
|
import { CompareOperation, NodeType, VarType, compareOperationMap, conditionOperatorMap } from '../../vueFlowEnum';
|
import { LLMNodeData, LLMNodeEvents } from './index';
|
|
import FieldLayout from './components/FieldLayout.vue';
|
import NodeBasicLayout from './components/NodeBasicLayout.vue';
|
defineProps<NodeProps<LLMNodeData, LLMNodeEvents>>();
|
const node = useNode();
|
const leftId = VueFlowHelper.getHandleId(node, 'target');
|
const { findNode } = useVueFlow();
|
const referenceOptions = ref([]);
|
|
const isShowRight = (item) => {
|
const yes = ![CompareOperation.empty, CompareOperation.notEmpty].includes(item.comparison_operation);
|
return yes;
|
};
|
|
const treeReferOptions = computed(() => {
|
const result = [];
|
for (const item of referenceOptions.value) {
|
result.push({
|
id: item.id,
|
label: item.groupName,
|
children: item.options.map((subItem, index) => {
|
return {
|
id: `${item.id}_${index}`,
|
label: subItem.label,
|
value: subItem.value,
|
};
|
}),
|
});
|
}
|
return result;
|
});
|
|
const travelConnectNode = (node, cb: Function) => {
|
if (node.connectedEdges && node.connectedEdges.value.length > 0) {
|
const filteredEdges = node.connectedEdges.value.filter((item) => item.target === node.id);
|
filteredEdges.map((edge) => {
|
const node = findNode(edge.source);
|
if (node) {
|
travelConnectNode(node, cb);
|
cb(node);
|
}
|
});
|
}
|
};
|
watchEffect(() => {
|
const result = [];
|
travelConnectNode(node, (item) => {
|
const currentItem = {
|
id: `option-${item.id}`,
|
groupName: item?.data.title ?? item?.label,
|
options: [],
|
};
|
const varList = VueFlowHelper.getFieldValue(item.data, 'var_list');
|
if (varList) {
|
varList
|
.filter((item: any) => Boolean(item.name))
|
.forEach((option: any) => {
|
currentItem.options.push({
|
label: option.name,
|
value: option.name,
|
});
|
});
|
} else {
|
currentItem.options = [];
|
}
|
result.push(currentItem);
|
});
|
referenceOptions.value = result;
|
});
|
|
const data = ref(node.node.data);
|
|
const conditionGroupList = ref(VueFlowHelper.getFieldValue(data.value, 'condition'));
|
|
const { removeEdges } = useVueFlow();
|
|
const removeRelativeHandleEdge = (handleId: string) => {
|
const edges = node.connectedEdges.value.filter((item) => item.sourceHandle === handleId || item.targetHandle === handleId);
|
removeEdges(edges);
|
};
|
const addConditionBranch = () => {
|
const conditionGroup = ConditionHelper.getDefaultConditionGroup();
|
// 不能删除其他分支
|
conditionGroupList.value.splice(-1, 0, conditionGroup);
|
};
|
|
const delConditionBranch = (index) => {
|
const group = conditionGroupList.value[index];
|
|
conditionGroupList.value.splice(index, 1);
|
removeRelativeHandleEdge(group.id);
|
if (conditionGroupList.value.length === 0) {
|
addConditionBranch();
|
}
|
};
|
|
const addConditionItem = (group) => {
|
const conditionGroup = ConditionHelper.getConditionItem();
|
group.conditions.push(conditionGroup);
|
};
|
|
const delConditionItem = (group, index) => {
|
group.conditions.splice(index, 1);
|
};
|
</script>
|