feat(web): 增加表单数据校验
This commit is contained in:
@@ -1,13 +1,39 @@
|
||||
import {has, isEmpty} from 'licia'
|
||||
import {getAllIncomerNodeOutputVariables} from './Helper.tsx'
|
||||
import CodeNode from './node/CodeNode.tsx'
|
||||
import KnowledgeNode from './node/KnowledgeNode.tsx'
|
||||
import LlmNode from './node/LlmNode.tsx'
|
||||
import OutputNode from './node/OutputNode.tsx'
|
||||
import SwitchNode from './node/SwitchNode.tsx'
|
||||
import type {NodeChecker} from './types.ts'
|
||||
|
||||
const inputVariableChecker: NodeChecker = (id, nodes, edges, data) => {
|
||||
let nodeData = data[id] ?? {}
|
||||
if (has(nodeData, 'inputs')) {
|
||||
let inputs = nodeData?.inputs ?? {}
|
||||
if (!isEmpty(inputs)) {
|
||||
let outputVariables = new Set(
|
||||
getAllIncomerNodeOutputVariables(id, nodes, edges, data).map(i => `${i.id}.${i.variable}`),
|
||||
)
|
||||
for (const key of Object.keys(inputs)) {
|
||||
let variable = inputs[key]?.variable ?? ''
|
||||
if (!outputVariables.has(variable)) {
|
||||
return {
|
||||
error: true,
|
||||
message: `节点 ${id} 存在错误:变量 ${variable} 不存在`,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return {error: false}
|
||||
}
|
||||
|
||||
type NodeDefine = {
|
||||
name: string,
|
||||
description: string,
|
||||
component: any,
|
||||
checkers: NodeChecker[],
|
||||
}
|
||||
|
||||
const NodeRegistry: Record<string, NodeDefine> = {
|
||||
@@ -15,26 +41,31 @@ const NodeRegistry: Record<string, NodeDefine> = {
|
||||
name: '输出',
|
||||
description: '定义输出变量',
|
||||
component: OutputNode,
|
||||
checkers: [inputVariableChecker],
|
||||
},
|
||||
'llm-node': {
|
||||
name: '大模型',
|
||||
description: '使用大模型对话',
|
||||
component: LlmNode,
|
||||
checkers: [inputVariableChecker],
|
||||
},
|
||||
'knowledge-node': {
|
||||
name: '知识库',
|
||||
description: '',
|
||||
component: KnowledgeNode,
|
||||
checkers: [inputVariableChecker],
|
||||
},
|
||||
'code-node': {
|
||||
name: '代码执行',
|
||||
description: '执行自定义的处理代码',
|
||||
component: CodeNode,
|
||||
checkers: [inputVariableChecker],
|
||||
},
|
||||
'switch-node': {
|
||||
name: '分支节点',
|
||||
description: '根据不同的情况前往不同的分支',
|
||||
component: SwitchNode,
|
||||
checkers: [],
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user