mirror of
https://github.com/langgenius/dify.git
synced 2026-05-06 18:27:19 +08:00
Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: jyong <718720800@qq.com> Co-authored-by: Yansong Zhang <916125788@qq.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: hj24 <mambahj24@gmail.com> Co-authored-by: hj24 <huangjian@dify.ai> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: Stephen Zhou <38493346+hyoban@users.noreply.github.com> Co-authored-by: CodingOnStar <hanxujiang@dify.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: 非法操作 <hjlarry@163.com> Co-authored-by: Ayush Baluni <73417844+aayushbaluni@users.noreply.github.com> Co-authored-by: yyh <92089059+lyzno1@users.noreply.github.com> Co-authored-by: jimcody1995 <jjimcody@gmail.com> Co-authored-by: James <63717587+jamesrayammons@users.noreply.github.com> Co-authored-by: Yunlu Wen <yunlu.wen@dify.ai> Co-authored-by: Stephen Zhou <hi@hyoban.cc> Co-authored-by: Coding On Star <447357187@qq.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: jerryzai <jerryzh8710@protonmail.com> Co-authored-by: NVIDIAN <speedy.hpc@hotmail.com> Co-authored-by: ai-hpc <ai-hpc@users.noreply.github.com> Co-authored-by: Asuka Minato <i@asukaminato.eu.org> Co-authored-by: Junghwan <70629228+shaun0927@users.noreply.github.com> Co-authored-by: HeYinKazune <70251095+HeYin-OS@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: yyh <yuanyouhuilyz@gmail.com> Co-authored-by: Jingyi <jingyi.qi@dify.ai> Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com> Co-authored-by: sxxtony <166789813+sxxtony@users.noreply.github.com>
153 lines
3.9 KiB
TypeScript
153 lines
3.9 KiB
TypeScript
import type { IterationNodeType } from '../nodes/iteration/types'
|
|
import type { LoopNodeType } from '../nodes/loop/types'
|
|
import type {
|
|
Node,
|
|
} from '../types'
|
|
import {
|
|
Position,
|
|
} from 'reactflow'
|
|
import { CUSTOM_SIMPLE_NODE } from '@/app/components/workflow/simple-node/constants'
|
|
import {
|
|
CUSTOM_NODE,
|
|
ITERATION_CHILDREN_Z_INDEX,
|
|
ITERATION_NODE_Z_INDEX,
|
|
LOOP_CHILDREN_Z_INDEX,
|
|
LOOP_NODE_Z_INDEX,
|
|
} from '../constants'
|
|
import { CUSTOM_ITERATION_START_NODE } from '../nodes/iteration-start/constants'
|
|
import { CUSTOM_LOOP_START_NODE } from '../nodes/loop-start/constants'
|
|
import {
|
|
BlockEnum,
|
|
} from '../types'
|
|
|
|
export function generateNewNode({ data, position, id, zIndex, type, ...rest }: Omit<Node, 'id'> & { id?: string }): {
|
|
newNode: Node
|
|
newIterationStartNode?: Node
|
|
newLoopStartNode?: Node
|
|
} {
|
|
const newNode = {
|
|
id: id || `${Date.now()}`,
|
|
type: type || CUSTOM_NODE,
|
|
data,
|
|
position,
|
|
targetPosition: Position.Left,
|
|
sourcePosition: Position.Right,
|
|
zIndex: data.type === BlockEnum.Iteration ? ITERATION_NODE_Z_INDEX : (data.type === BlockEnum.Loop ? LOOP_NODE_Z_INDEX : zIndex),
|
|
...rest,
|
|
} as Node
|
|
|
|
if (data.type === BlockEnum.Iteration) {
|
|
const newIterationStartNode = getIterationStartNode(newNode.id);
|
|
(newNode.data as IterationNodeType).start_node_id = newIterationStartNode.id;
|
|
(newNode.data as IterationNodeType)._children = [{ nodeId: newIterationStartNode.id, nodeType: BlockEnum.IterationStart }]
|
|
return {
|
|
newNode,
|
|
newIterationStartNode,
|
|
}
|
|
}
|
|
|
|
if (data.type === BlockEnum.Loop) {
|
|
const newLoopStartNode = getLoopStartNode(newNode.id);
|
|
(newNode.data as LoopNodeType).start_node_id = newLoopStartNode.id;
|
|
(newNode.data as LoopNodeType)._children = [{ nodeId: newLoopStartNode.id, nodeType: BlockEnum.LoopStart }]
|
|
return {
|
|
newNode,
|
|
newLoopStartNode,
|
|
}
|
|
}
|
|
|
|
return {
|
|
newNode,
|
|
}
|
|
}
|
|
|
|
export function getIterationStartNode(iterationId: string): Node {
|
|
return generateNewNode({
|
|
id: `${iterationId}start`,
|
|
type: CUSTOM_ITERATION_START_NODE,
|
|
data: {
|
|
title: '',
|
|
desc: '',
|
|
type: BlockEnum.IterationStart,
|
|
isInIteration: true,
|
|
},
|
|
position: {
|
|
x: 24,
|
|
y: 68,
|
|
},
|
|
zIndex: ITERATION_CHILDREN_Z_INDEX,
|
|
parentId: iterationId,
|
|
selectable: false,
|
|
draggable: false,
|
|
}).newNode
|
|
}
|
|
|
|
export function getLoopStartNode(loopId: string): Node {
|
|
return generateNewNode({
|
|
id: `${loopId}start`,
|
|
type: CUSTOM_LOOP_START_NODE,
|
|
data: {
|
|
title: '',
|
|
desc: '',
|
|
type: BlockEnum.LoopStart,
|
|
isInLoop: true,
|
|
},
|
|
position: {
|
|
x: 24,
|
|
y: 68,
|
|
},
|
|
zIndex: LOOP_CHILDREN_Z_INDEX,
|
|
parentId: loopId,
|
|
selectable: false,
|
|
draggable: false,
|
|
}).newNode
|
|
}
|
|
|
|
export const genNewNodeTitleFromOld = (oldTitle: string) => {
|
|
const regex = /^(.+?)\s*\((\d+)\)\s*$/
|
|
const match = regex.exec(oldTitle)
|
|
|
|
if (match) {
|
|
const title = match[1]
|
|
const num = Number.parseInt(match[2]!, 10)
|
|
return `${title} (${num + 1})`
|
|
}
|
|
else {
|
|
return `${oldTitle} (1)`
|
|
}
|
|
}
|
|
|
|
export const getTopLeftNodePosition = (nodes: Node[]) => {
|
|
let minX = Infinity
|
|
let minY = Infinity
|
|
|
|
nodes.forEach((node) => {
|
|
if (node.position.x < minX)
|
|
minX = node.position.x
|
|
|
|
if (node.position.y < minY)
|
|
minY = node.position.y
|
|
})
|
|
|
|
return {
|
|
x: minX,
|
|
y: minY,
|
|
}
|
|
}
|
|
|
|
export const getNestedNodePosition = (node: Node, parentNode: Node) => {
|
|
return {
|
|
x: node.position.x - parentNode.position.x,
|
|
y: node.position.y - parentNode.position.y,
|
|
}
|
|
}
|
|
|
|
export const hasRetryNode = (nodeType?: BlockEnum) => {
|
|
return nodeType === BlockEnum.LLM || nodeType === BlockEnum.Tool || nodeType === BlockEnum.HttpRequest || nodeType === BlockEnum.Code
|
|
}
|
|
|
|
export const getNodeCustomTypeByNodeDataType = (nodeType: BlockEnum) => {
|
|
if (nodeType === BlockEnum.LoopEnd)
|
|
return CUSTOM_SIMPLE_NODE
|
|
}
|