mirror of
https://github.com/langgenius/dify.git
synced 2026-04-28 20:17:29 +08:00
node value init
This commit is contained in:
parent
129a68bb06
commit
e33260d2e2
@ -16,13 +16,15 @@ export const useNodesInitialData = () => {
|
|||||||
return useMemo(() => produce(NODES_INITIAL_DATA, (draft) => {
|
return useMemo(() => produce(NODES_INITIAL_DATA, (draft) => {
|
||||||
Object.keys(draft).forEach((key) => {
|
Object.keys(draft).forEach((key) => {
|
||||||
draft[key as BlockEnum].title = t(`workflow.blocks.${key}`)
|
draft[key as BlockEnum].title = t(`workflow.blocks.${key}`)
|
||||||
draft[key as BlockEnum]._isReady = true
|
|
||||||
if (nodesDefaultConfigs[key as BlockEnum]) {
|
if (nodesDefaultConfigs[key as BlockEnum]) {
|
||||||
draft[key as BlockEnum] = {
|
draft[key as BlockEnum] = {
|
||||||
...draft[key as BlockEnum],
|
...draft[key as BlockEnum],
|
||||||
...nodesDefaultConfigs[key as BlockEnum],
|
...nodesDefaultConfigs[key as BlockEnum],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
draft[key as BlockEnum]._isReady = true
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}), [t, nodesDefaultConfigs])
|
}), [t, nodesDefaultConfigs])
|
||||||
}
|
}
|
||||||
|
|||||||
@ -236,7 +236,7 @@ export const useWorkflowInit = () => {
|
|||||||
workflowStore.setState({
|
workflowStore.setState({
|
||||||
nodesDefaultConfigs: nodesDefaultConfigsData.reduce((acc, block) => {
|
nodesDefaultConfigs: nodesDefaultConfigsData.reduce((acc, block) => {
|
||||||
if (!acc[block.type])
|
if (!acc[block.type])
|
||||||
acc[block.type] = block.config
|
acc[block.type] = { ...block.config, _isReady: true }
|
||||||
return acc
|
return acc
|
||||||
}, {} as Record<string, any>),
|
}, {} as Record<string, any>),
|
||||||
})
|
})
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
import {
|
import {
|
||||||
Position,
|
Position,
|
||||||
getConnectedEdges,
|
getConnectedEdges,
|
||||||
getOutgoers,
|
|
||||||
} from 'reactflow'
|
} from 'reactflow'
|
||||||
import dagre from 'dagre'
|
import dagre from 'dagre'
|
||||||
import { cloneDeep } from 'lodash-es'
|
import { cloneDeep } from 'lodash-es'
|
||||||
@ -12,76 +11,6 @@ import type {
|
|||||||
import { BlockEnum } from './types'
|
import { BlockEnum } from './types'
|
||||||
import type { QuestionClassifierNodeType } from './nodes/question-classifier/types'
|
import type { QuestionClassifierNodeType } from './nodes/question-classifier/types'
|
||||||
|
|
||||||
export const nodesLevelOrderTraverse = (
|
|
||||||
firstNode: Node,
|
|
||||||
nodes: Node[],
|
|
||||||
edges: Edge[],
|
|
||||||
callback: (n: any) => void,
|
|
||||||
) => {
|
|
||||||
const queue = [{
|
|
||||||
node: firstNode,
|
|
||||||
depth: 0,
|
|
||||||
breath: 0,
|
|
||||||
}]
|
|
||||||
|
|
||||||
let currenDepth = 0
|
|
||||||
let currentBreath = 0
|
|
||||||
while (queue.length) {
|
|
||||||
const { node, depth, breath } = queue.shift()!
|
|
||||||
|
|
||||||
if (currenDepth !== depth) {
|
|
||||||
currenDepth = depth
|
|
||||||
currentBreath = 0
|
|
||||||
}
|
|
||||||
|
|
||||||
callback({ node, depth, breath })
|
|
||||||
|
|
||||||
const targetBranches = node.data._targetBranches
|
|
||||||
if (targetBranches?.length) {
|
|
||||||
const targetEdges = getConnectedEdges([node], edges)
|
|
||||||
|
|
||||||
if (targetEdges.length) {
|
|
||||||
const sortedTargetEdges = targetEdges
|
|
||||||
.filter(edge => edge.source === node.id)
|
|
||||||
.sort((a, b) => {
|
|
||||||
const aIndex = targetBranches.findIndex(branch => branch.id === a.sourceHandle)
|
|
||||||
const bIndex = targetBranches.findIndex(branch => branch.id === b.sourceHandle)
|
|
||||||
|
|
||||||
return aIndex - bIndex
|
|
||||||
})
|
|
||||||
|
|
||||||
const outgoers = getOutgoers(node, nodes, sortedTargetEdges)
|
|
||||||
|
|
||||||
queue.push(...outgoers.map((outgoer, index) => {
|
|
||||||
return {
|
|
||||||
node: outgoer,
|
|
||||||
depth: depth + 1,
|
|
||||||
breath: currentBreath + index,
|
|
||||||
}
|
|
||||||
}))
|
|
||||||
|
|
||||||
currentBreath += outgoers.length
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
currentBreath += 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
const outgoers = getOutgoers(node, nodes, edges)
|
|
||||||
|
|
||||||
if (outgoers.length === 1) {
|
|
||||||
queue.push({
|
|
||||||
node: outgoers[0],
|
|
||||||
depth: depth + 1,
|
|
||||||
breath: 0,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
currentBreath += 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export const initialNodes = (nodes: Node[], edges: Edge[]) => {
|
export const initialNodes = (nodes: Node[], edges: Edge[]) => {
|
||||||
return nodes.map((node) => {
|
return nodes.map((node) => {
|
||||||
node.type = 'custom'
|
node.type = 'custom'
|
||||||
@ -121,34 +50,8 @@ export const initialEdges = (edges: Edge[]) => {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export type PositionMap = {
|
|
||||||
index: {
|
|
||||||
x: number
|
|
||||||
y: number
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export const getNodesPositionMap = (nodes: Node[], edges: Edge[]) => {
|
|
||||||
const startNode = nodes.find((node: Node) => node.data.type === BlockEnum.Start)
|
|
||||||
const positionMap: Record<string, PositionMap> = {}
|
|
||||||
|
|
||||||
if (startNode) {
|
|
||||||
nodesLevelOrderTraverse(startNode, nodes, edges, ({ node, depth, breath }) => {
|
|
||||||
positionMap[node.id] = {
|
|
||||||
index: {
|
|
||||||
x: depth,
|
|
||||||
y: breath,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
return positionMap
|
|
||||||
}
|
|
||||||
|
|
||||||
const dagreGraph = new dagre.graphlib.Graph()
|
const dagreGraph = new dagre.graphlib.Graph()
|
||||||
dagreGraph.setDefaultEdgeLabel(() => ({}))
|
dagreGraph.setDefaultEdgeLabel(() => ({}))
|
||||||
|
|
||||||
export const getLayoutByDagre = (originNodes: Node[], originEdges: Edge[]) => {
|
export const getLayoutByDagre = (originNodes: Node[], originEdges: Edge[]) => {
|
||||||
const nodes = cloneDeep(originNodes)
|
const nodes = cloneDeep(originNodes)
|
||||||
const edges = cloneDeep(originEdges)
|
const edges = cloneDeep(originEdges)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user