wujingjing
2024-12-13 59951a7031d50c8528764c624628886c31a6ea2c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<template>
    <NodeBasicLayout v-model:title="data.title" :type="NodeType.Output" :description="VueFlowHelper.getConfigValue(data, 'description', '可向用户发送消息,并且支持进行更丰富的交互,例如请求用户批准进行某项敏感操作、允许用户在模型输出内容的基础上直接修改并提交。')">
        <Handle :id="targetHandleId" type="target" :position="Position.Left" />
        <FieldLayout level="2" :title="VueFlowHelper.getParams(outputParams, 'output_msg').label">
            <el-input
                v-model="VueFlowHelper.getParams(outputParams, 'output_msg').value.msg"
                type="textarea"
                :rows="4"
                :placeholder="VueFlowHelper.getParams(outputParams, 'output_msg').placeholder"
                autocomplete="off"
            >
            </el-input>
        </FieldLayout>
        <FieldLayout level="2" :title="VueFlowHelper.getParams(outputParams, 'output_result').label">
            <el-radio-group v-model="VueFlowHelper.getParams(outputParams, 'output_result').value.type" @change="interactionTypeChange">
                <el-radio v-for="item in Object.keys(interactionTypeMap)" :key="item" :value="item">{{ interactionTypeMap[item] }}</el-radio>
            </el-radio-group>
            <el-input
                v-model="VueFlowHelper.getParams(outputParams, 'output_result').value.value"
                type="textarea"
                :rows="4"
                v-if="VueFlowHelper.getParams(outputParams, 'output_result').value.type === InteractionType.Input"
            >
            </el-input>
 
            <div
                class="self-start w-full flex-col flex items-start gap-2"
                v-else-if="VueFlowHelper.getParams(outputParams, 'output_result').value.type === InteractionType.Select"
            >
                <div class="flex-column gap-2">
                    <div
                        class="text-gray-400 cursor-not-allowed border border-solid rounded-lg border-gray-300 py-3 pl-3 pr-2 items-center justify-between flex group/option relative mr-6"
                        :key="item.id"
                        v-for="(item, index) in VueFlowHelper.getParams(outputParams, 'output_result').options"
                    >
                        <span>{{ item.label }}</span>
                        <span
                            @click="delOption(index)"
                            class="cursor-pointer group-hover/option:visible invisible ywifont ywicon-shanchu text-red-500"
                        ></span>
                        <Handle class="absolute !-right-4" :id="item.id" type="source" :position="Position.Right" />
                    </div>
                </div>
 
                <div v-if="isEditStatus" class="flex-items-center w-full">
                    <el-input class="flex-auto" v-model="tmpEditValue"></el-input>
                    <el-button class="flex-0 ml-2" type="success" @click="confirmClick">确定 </el-button>
                </div>
                <el-button v-else type="primary" @click="addOptionClick">添加选项</el-button>
            </div>
            <Handle
                v-if="
                    VueFlowHelper.getParams(outputParams, 'output_result').value.type === InteractionType.Input ||
                    VueFlowHelper.getParams(outputParams, 'output_result').value.type === InteractionType.None
                "
                :id="sourceHandleId"
                type="source"
                :position="Position.Right"
            />
        </FieldLayout>
    </NodeBasicLayout>
</template>
 
<script lang="ts" setup>
import { Handle, Position, useNode, useVueFlow } from '@vue-flow/core';
import { ref } from 'vue';
import { NodeType } from '../../vueFlowEnum';
 
import type { NodeProps } from '@vue-flow/core';
import { nextTick } from 'vue';
import { VueFlowHelper } from '../../VueFlowHelper';
import { InteractionType, interactionTypeMap } from '../../vueFlowEnum';
import { LLMNodeData, LLMNodeEvents } from './index';
 
import NodeBasicLayout from './components/NodeBasicLayout.vue';
import FieldLayout from './components/FieldLayout.vue';
defineProps<NodeProps<LLMNodeData, LLMNodeEvents>>();
 
const node = useNode();
const sourceHandleId = ref(VueFlowHelper.getHandleId(node.node, 'source'));
const targetHandleId = ref(VueFlowHelper.getHandleId(node.node, 'target'));
 
const data = ref(node.node.data);
 
const outputParams = ref(VueFlowHelper.getGroupParam(data.value, 0));
 
const { removeEdges } = useVueFlow();
 
const addOptionClick = () => {
    isEditStatus.value = true;
};
 
const confirmClick = () => {
    isEditStatus.value = false;
 
    addOption(tmpEditValue.value);
    nextTick(() => {
        tmpEditValue.value = '';
    });
};
const addOption = (val) => {
    const options = VueFlowHelper.getParams(outputParams.value, 'output_result').options;
    options.push({
        id: VueFlowHelper.genId(),
        label: val,
        value: '',
    });
};
 
const removeRelativeHandleEdge = (handleId: string) => {
    const edges = node.connectedEdges.value.filter((item) => item.sourceHandle === handleId || item.targetHandle === handleId);
    removeEdges(edges);
};
 
const delOption = (index) => {
    const options = VueFlowHelper.getParams(outputParams.value, 'output_result').options;
    const option = options[index];
 
    options.splice(index);
    removeRelativeHandleEdge(option.id);
};
 
let preInteractionType = VueFlowHelper.getParams(outputParams.value, 'output_result').value.type;
const interactionTypeChange = (type) => {
    removeRelativeHandleEdge(sourceHandleId.value);
    if (preInteractionType === InteractionType.Select) {
        const options = VueFlowHelper.getParams(outputParams.value, 'output_result').options;
        for (const item of options) {
            removeRelativeHandleEdge(item.id);
        }
    }
    preInteractionType = type;
};
 
const isEditStatus = ref(false);
const tmpEditValue = ref('');
</script>