feat(workflow): add variable validation for code nodes
- Introduce useNodesAvailableVarList hook to track available variables - Enhance code node validation to check variable references - Add debug logging for validation processpull/21932/head
parent
a79f37b686
commit
f50773b1e9
@ -0,0 +1,74 @@
|
|||||||
|
import {
|
||||||
|
useIsChatMode,
|
||||||
|
useWorkflow,
|
||||||
|
useWorkflowVariables,
|
||||||
|
} from '@/app/components/workflow/hooks'
|
||||||
|
import type { Node, NodeOutPutVar, ValueSelector, Var } from '@/app/components/workflow/types'
|
||||||
|
type Params = {
|
||||||
|
onlyLeafNodeVar?: boolean
|
||||||
|
hideEnv?: boolean
|
||||||
|
hideChatVar?: boolean
|
||||||
|
filterVar: (payload: Var, selector: ValueSelector) => boolean
|
||||||
|
passedInAvailableNodes?: Node[]
|
||||||
|
}
|
||||||
|
|
||||||
|
const getNodeInfo = (nodeId: string, nodes: Node[]) => {
|
||||||
|
const allNodes = nodes
|
||||||
|
const node = allNodes.find(n => n.id === nodeId)
|
||||||
|
const isInIteration = !!node?.data.isInIteration
|
||||||
|
const isInLoop = !!node?.data.isInLoop
|
||||||
|
const parentNodeId = node?.parentId
|
||||||
|
const parentNode = allNodes.find(n => n.id === parentNodeId)
|
||||||
|
return {
|
||||||
|
node,
|
||||||
|
isInIteration,
|
||||||
|
isInLoop,
|
||||||
|
parentNode,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: loop type?
|
||||||
|
const useNodesAvailableVarList = (nodes: Node[], {
|
||||||
|
onlyLeafNodeVar,
|
||||||
|
filterVar,
|
||||||
|
hideEnv,
|
||||||
|
hideChatVar,
|
||||||
|
passedInAvailableNodes,
|
||||||
|
}: Params = {
|
||||||
|
onlyLeafNodeVar: false,
|
||||||
|
filterVar: () => true,
|
||||||
|
}) => {
|
||||||
|
const { getTreeLeafNodes, getBeforeNodesInSameBranchIncludeParent } = useWorkflow()
|
||||||
|
const { getNodeAvailableVars } = useWorkflowVariables()
|
||||||
|
const isChatMode = useIsChatMode()
|
||||||
|
|
||||||
|
const map: { [key: string ]: { availableVars: NodeOutPutVar[], availableNodes: Node[] } } = {}
|
||||||
|
|
||||||
|
nodes.forEach((node) => {
|
||||||
|
const nodeId = node.id
|
||||||
|
const availableNodes = passedInAvailableNodes || (onlyLeafNodeVar ? getTreeLeafNodes(nodeId) : getBeforeNodesInSameBranchIncludeParent(nodeId))
|
||||||
|
|
||||||
|
const {
|
||||||
|
parentNode: iterationNode,
|
||||||
|
} = getNodeInfo(nodeId, nodes)
|
||||||
|
|
||||||
|
const availableVars = getNodeAvailableVars({
|
||||||
|
parentNode: iterationNode,
|
||||||
|
beforeNodes: availableNodes,
|
||||||
|
isChatMode,
|
||||||
|
filterVar,
|
||||||
|
hideEnv,
|
||||||
|
hideChatVar,
|
||||||
|
})
|
||||||
|
|
||||||
|
const result = {
|
||||||
|
node,
|
||||||
|
availableVars,
|
||||||
|
availableNodes,
|
||||||
|
}
|
||||||
|
map[nodeId] = result
|
||||||
|
})
|
||||||
|
return map
|
||||||
|
}
|
||||||
|
|
||||||
|
export default useNodesAvailableVarList
|
||||||
Loading…
Reference in New Issue