yangyin
2024-12-13 c6cf46f3e11e60f692e0b8d12188458ad1b6120e
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import { HandleType, useNode, useVueFlow } from '@vue-flow/core';
import { v4 as uuid } from 'uuid';
import { VueFlowConstant } from './VueFlowConstant';
import { CompareOperation, ConditionOperator, NodeType, VarType, nodeTypeMap } from './vueFlowEnum';
import { get } from 'lodash';
 
export class VueFlowHelper {
    static genId() {
        return uuid().slice(0, 8);
    }
 
    static genGeometryId() {
        return uuid().slice(0, 12);
    }
 
    static getDefaultData = (type: NodeType) => {
        let data: any = {
            title: nodeTypeMap[type],
        };
        switch (type) {
            case NodeType.Start:
                data = {
                    ...data,
                    description: '工作流运行的起始节点。',
                };
                data[VueFlowConstant.GROUP_PARAMS_KEY] = [
                    {
                        [VueFlowConstant.PARAMS_KEY]: [
                            {
                                key: 'var_list',
                                label: '',
                                type: 'var_list',
                                value: [],
                            },
                        ],
                    },
                ];
                break;
            case NodeType.End:
                data = {
                    ...data,
                    description: '工作流运行到此结束。',
                };
                break;
            case NodeType.Condition:
                data = {
                    ...data,
                    description: '根据条件表达式执行不同的分支。',
                };
                data[VueFlowConstant.GROUP_PARAMS_KEY] = [
                    {
                        [VueFlowConstant.PARAMS_KEY]: [
                            {
                                key: 'condition',
                                label: '',
                                type: 'condition',
                                value: [ConditionHelper.getDefaultConditionGroup(), ConditionHelper.getDefaultConditionGroup(true)],
                            },
                        ],
                    },
                ];
                break;
            case NodeType.LLM:
                data = {
                    ...data,
                    description: '调用大模型回答用户问题或者处理任务。',
                };
                data[VueFlowConstant.GROUP_PARAMS_KEY] = [
                    {
                        name: '模型设置',
 
                        [VueFlowConstant.PARAMS_KEY]: [
                            {
                                key: 'llm_model',
                                label: '模型',
                                type: 'llm_model',
                                value: '',
                                required: true,
                                placeholder: '请选择模型',
                            },
                            { key: 'temperature', label: '温度', type: 'slide', scope: [0, 2], step: 0.1, value: 0.6 },
                        ],
                    },
                    {
                        name: '提示词',
                        [VueFlowConstant.PARAMS_KEY]: [{ key: 'prompt', label: '', type: 'textarea', value: '' }],
                    },
                ];
                break;
 
            case NodeType.Agent:
                data[VueFlowConstant.GROUP_PARAMS_KEY] = [
                    {
                        name: '代理名称',
                        [VueFlowConstant.PARAMS_KEY]: [
                            {
                                key: 'agent',
                                label: '',
                                type: 'agent_select',
                                value: '',
                                // value_label:'',
                                required: true,
                                placeholder: '代理名称',
                            },
                        ],
                    },
                ];
                break;
 
            case NodeType.Func:
                data[VueFlowConstant.GROUP_PARAMS_KEY] = [
                    {
                        name: '函数名称',
                        [VueFlowConstant.PARAMS_KEY]: [
                            {
                                key: 'func_name',
                                label: '',
                                type: 'func_name_select',
                                value: '',
                                // value_label:'',
                                required: true,
                                placeholder: '函数名称',
                            },
                        ],
                    },
                ];
                break;
            case NodeType.Output:
                data = {
                    ...data,
                    description:
                        '可向用户发送消息,并且支持进行更丰富的交互,例如请求用户批准进行某项敏感操作、允许用户在模型输出内容的基础上直接修改并提交。',
                };
                data[VueFlowConstant.GROUP_PARAMS_KEY] = [
                    {
                        [VueFlowConstant.PARAMS_KEY]: [
                            {
                                key: 'output_msg',
                                label: '消息内容',
                                type: 'var_textarea_file',
                                required: true,
                                placeholder:
                                    '输入需要发送给用户的消息,例如“接下来我将执行 XX 操作,请您确认”,“以下是我的初版草稿,您可以在其基础上进行修改”',
                                value: { msg: '', files: [] },
                            },
                            {
                                key: 'output_result',
                                label: '交互类型',
                                global: 'value.type=input',
                                type: 'output_form',
                                required: true,
                                value: { type: 'none', value: '' },
                                options: [],
                            },
                        ],
                    },
                ];
 
            default:
                break;
        }
        return data;
    };
 
    /**
     * 强制写入到配置信息中,用于 patch 过去版本中没有的字段,或字段名称已经修改
     *
     * 版本稳定后可删除此方法
     * @param obj
     * @param key
     * @param val
     */
    static getConfigValue = (obj: any, key: string, val: string) => {
        const value = obj[key];
        if (!value || value !== val) {
            obj[key] = val;
        }
        return val;
    };
 
    static getHandleId = (node: any, handleType: HandleType, order?: number) => {
        const orderSuffix = order == undefined ? '' : `__${order + ''}`;
        return `${node.id}__handle-${handleType}${orderSuffix}`;
    };
 
    static getFieldValue = (data, key, index = 0) => {
        let varList = [];
        const group = data?.[VueFlowConstant.GROUP_PARAMS_KEY];
        if (group && group.length > 0) {
            if (index !== null) {
                const val = group?.[index]?.[VueFlowConstant.PARAMS_KEY]?.find((item) => item.key === key)?.value;
                if (val) {
                    varList.push(val);
                }
            } else {
                for (const item of group) {
                    if (item[VueFlowConstant.PARAMS_KEY].key === key) {
                        varList.push(item[VueFlowConstant.PARAMS_KEY].value);
                    }
                }
            }
        }
        if (varList.length === 0) {
            return null;
        } else if (varList.length === 1) {
            return varList[0];
        } else {
            return varList;
        }
    };
 
    static getGroupParam = (data, index = 0) => {
        const group = data?.[VueFlowConstant.GROUP_PARAMS_KEY]?.[index];
        return group;
    };
 
    static getParams = (group, key) => {
        return group?.[VueFlowConstant.PARAMS_KEY]?.find((item) => item.key === key);
    };
}
 
export class StartNodeHelper {
    // static getDefaultData = () => {
    //     return {
    //         title: nodeTypeMap[NodeType.Start],
    //     };
    // };
 
    static getVarList = (data) => {
        const varList = data[VueFlowConstant.GROUP_PARAMS_KEY][0][VueFlowConstant.PARAMS_KEY].find(
            (item) => item.key === 'condition'
        ).value;
        return varList;
    };
}
 
export class ConditionHelper {
    static getConditionItem = (
        left?: {
            var: string;
            label: string;
            value: string;
        },
        right?: {
            type: VarType;
            value: string;
            label: string;
        },
        operation?: CompareOperation
    ) => {
        return {
            id: VueFlowHelper.genId(),
            left_var: left?.var ?? '',
            left_label: left?.label ?? '',
            left_value: left?.var ?? '',
            comparison_operation: operation ?? '',
            // right_value_type: right?.type ?? VarType.Input,
            // 固定选择 input
            right_value_type: VarType.Input,
 
            right_value: right?.value ?? '',
            right_label: right?.label ?? '',
        };
    };
 
    static getDefaultConditionGroup = (isElse = false) => {
        return isElse
            ? {
                    id: VueFlowHelper.genId(),
              }
            : {
                    id: VueFlowHelper.genId(),
                    operator: ConditionOperator.And,
                    conditions: [ConditionHelper.getConditionItem()],
              };
    };
}