您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

workflow-init.ts 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. import {
  2. getConnectedEdges,
  3. } from 'reactflow'
  4. import {
  5. cloneDeep,
  6. } from 'lodash-es'
  7. import type {
  8. Edge,
  9. Node,
  10. } from '../types'
  11. import {
  12. BlockEnum,
  13. ErrorHandleMode,
  14. } from '../types'
  15. import {
  16. CUSTOM_NODE,
  17. DEFAULT_RETRY_INTERVAL,
  18. DEFAULT_RETRY_MAX,
  19. ITERATION_CHILDREN_Z_INDEX,
  20. LOOP_CHILDREN_Z_INDEX,
  21. NODE_WIDTH_X_OFFSET,
  22. START_INITIAL_POSITION,
  23. } from '../constants'
  24. import { CUSTOM_ITERATION_START_NODE } from '../nodes/iteration-start/constants'
  25. import { CUSTOM_LOOP_START_NODE } from '../nodes/loop-start/constants'
  26. import type { QuestionClassifierNodeType } from '../nodes/question-classifier/types'
  27. import type { IfElseNodeType } from '../nodes/if-else/types'
  28. import { branchNameCorrect } from '../nodes/if-else/utils'
  29. import type { IterationNodeType } from '../nodes/iteration/types'
  30. import type { LoopNodeType } from '../nodes/loop/types'
  31. import {
  32. getIterationStartNode,
  33. getLoopStartNode,
  34. } from '.'
  35. import { correctModelProvider } from '@/utils'
  36. const WHITE = 'WHITE'
  37. const GRAY = 'GRAY'
  38. const BLACK = 'BLACK'
  39. const isCyclicUtil = (nodeId: string, color: Record<string, string>, adjList: Record<string, string[]>, stack: string[]) => {
  40. color[nodeId] = GRAY
  41. stack.push(nodeId)
  42. for (let i = 0; i < adjList[nodeId].length; ++i) {
  43. const childId = adjList[nodeId][i]
  44. if (color[childId] === GRAY) {
  45. stack.push(childId)
  46. return true
  47. }
  48. if (color[childId] === WHITE && isCyclicUtil(childId, color, adjList, stack))
  49. return true
  50. }
  51. color[nodeId] = BLACK
  52. if (stack.length > 0 && stack[stack.length - 1] === nodeId)
  53. stack.pop()
  54. return false
  55. }
  56. const getCycleEdges = (nodes: Node[], edges: Edge[]) => {
  57. const adjList: Record<string, string[]> = {}
  58. const color: Record<string, string> = {}
  59. const stack: string[] = []
  60. for (const node of nodes) {
  61. color[node.id] = WHITE
  62. adjList[node.id] = []
  63. }
  64. for (const edge of edges)
  65. adjList[edge.source]?.push(edge.target)
  66. for (let i = 0; i < nodes.length; i++) {
  67. if (color[nodes[i].id] === WHITE)
  68. isCyclicUtil(nodes[i].id, color, adjList, stack)
  69. }
  70. const cycleEdges = []
  71. if (stack.length > 0) {
  72. const cycleNodes = new Set(stack)
  73. for (const edge of edges) {
  74. if (cycleNodes.has(edge.source) && cycleNodes.has(edge.target))
  75. cycleEdges.push(edge)
  76. }
  77. }
  78. return cycleEdges
  79. }
  80. export const preprocessNodesAndEdges = (nodes: Node[], edges: Edge[]) => {
  81. const hasIterationNode = nodes.some(node => node.data.type === BlockEnum.Iteration)
  82. const hasLoopNode = nodes.some(node => node.data.type === BlockEnum.Loop)
  83. if (!hasIterationNode && !hasLoopNode) {
  84. return {
  85. nodes,
  86. edges,
  87. }
  88. }
  89. const nodesMap = nodes.reduce((prev, next) => {
  90. prev[next.id] = next
  91. return prev
  92. }, {} as Record<string, Node>)
  93. const iterationNodesWithStartNode = []
  94. const iterationNodesWithoutStartNode = []
  95. const loopNodesWithStartNode = []
  96. const loopNodesWithoutStartNode = []
  97. for (let i = 0; i < nodes.length; i++) {
  98. const currentNode = nodes[i] as Node<IterationNodeType | LoopNodeType>
  99. if (currentNode.data.type === BlockEnum.Iteration) {
  100. if (currentNode.data.start_node_id) {
  101. if (nodesMap[currentNode.data.start_node_id]?.type !== CUSTOM_ITERATION_START_NODE)
  102. iterationNodesWithStartNode.push(currentNode)
  103. }
  104. else {
  105. iterationNodesWithoutStartNode.push(currentNode)
  106. }
  107. }
  108. if (currentNode.data.type === BlockEnum.Loop) {
  109. if (currentNode.data.start_node_id) {
  110. if (nodesMap[currentNode.data.start_node_id]?.type !== CUSTOM_LOOP_START_NODE)
  111. loopNodesWithStartNode.push(currentNode)
  112. }
  113. else {
  114. loopNodesWithoutStartNode.push(currentNode)
  115. }
  116. }
  117. }
  118. const newIterationStartNodesMap = {} as Record<string, Node>
  119. const newIterationStartNodes = [...iterationNodesWithStartNode, ...iterationNodesWithoutStartNode].map((iterationNode, index) => {
  120. const newNode = getIterationStartNode(iterationNode.id)
  121. newNode.id = newNode.id + index
  122. newIterationStartNodesMap[iterationNode.id] = newNode
  123. return newNode
  124. })
  125. const newLoopStartNodesMap = {} as Record<string, Node>
  126. const newLoopStartNodes = [...loopNodesWithStartNode, ...loopNodesWithoutStartNode].map((loopNode, index) => {
  127. const newNode = getLoopStartNode(loopNode.id)
  128. newNode.id = newNode.id + index
  129. newLoopStartNodesMap[loopNode.id] = newNode
  130. return newNode
  131. })
  132. const newEdges = [...iterationNodesWithStartNode, ...loopNodesWithStartNode].map((nodeItem) => {
  133. const isIteration = nodeItem.data.type === BlockEnum.Iteration
  134. const newNode = (isIteration ? newIterationStartNodesMap : newLoopStartNodesMap)[nodeItem.id]
  135. const startNode = nodesMap[nodeItem.data.start_node_id]
  136. const source = newNode.id
  137. const sourceHandle = 'source'
  138. const target = startNode.id
  139. const targetHandle = 'target'
  140. const parentNode = nodes.find(node => node.id === startNode.parentId) || null
  141. const isInIteration = !!parentNode && parentNode.data.type === BlockEnum.Iteration
  142. const isInLoop = !!parentNode && parentNode.data.type === BlockEnum.Loop
  143. return {
  144. id: `${source}-${sourceHandle}-${target}-${targetHandle}`,
  145. type: 'custom',
  146. source,
  147. sourceHandle,
  148. target,
  149. targetHandle,
  150. data: {
  151. sourceType: newNode.data.type,
  152. targetType: startNode.data.type,
  153. isInIteration,
  154. iteration_id: isInIteration ? startNode.parentId : undefined,
  155. isInLoop,
  156. loop_id: isInLoop ? startNode.parentId : undefined,
  157. _connectedNodeIsSelected: true,
  158. },
  159. zIndex: isIteration ? ITERATION_CHILDREN_Z_INDEX : LOOP_CHILDREN_Z_INDEX,
  160. }
  161. })
  162. nodes.forEach((node) => {
  163. if (node.data.type === BlockEnum.Iteration && newIterationStartNodesMap[node.id])
  164. (node.data as IterationNodeType).start_node_id = newIterationStartNodesMap[node.id].id
  165. if (node.data.type === BlockEnum.Loop && newLoopStartNodesMap[node.id])
  166. (node.data as LoopNodeType).start_node_id = newLoopStartNodesMap[node.id].id
  167. })
  168. return {
  169. nodes: [...nodes, ...newIterationStartNodes, ...newLoopStartNodes],
  170. edges: [...edges, ...newEdges],
  171. }
  172. }
  173. export const initialNodes = (originNodes: Node[], originEdges: Edge[]) => {
  174. const { nodes, edges } = preprocessNodesAndEdges(cloneDeep(originNodes), cloneDeep(originEdges))
  175. const firstNode = nodes[0]
  176. if (!firstNode?.position) {
  177. nodes.forEach((node, index) => {
  178. node.position = {
  179. x: START_INITIAL_POSITION.x + index * NODE_WIDTH_X_OFFSET,
  180. y: START_INITIAL_POSITION.y,
  181. }
  182. })
  183. }
  184. const iterationOrLoopNodeMap = nodes.reduce((acc, node) => {
  185. if (node.parentId) {
  186. if (acc[node.parentId])
  187. acc[node.parentId].push({ nodeId: node.id, nodeType: node.data.type })
  188. else
  189. acc[node.parentId] = [{ nodeId: node.id, nodeType: node.data.type }]
  190. }
  191. return acc
  192. }, {} as Record<string, { nodeId: string; nodeType: BlockEnum }[]>)
  193. return nodes.map((node) => {
  194. if (!node.type)
  195. node.type = CUSTOM_NODE
  196. const connectedEdges = getConnectedEdges([node], edges)
  197. node.data._connectedSourceHandleIds = connectedEdges.filter(edge => edge.source === node.id).map(edge => edge.sourceHandle || 'source')
  198. node.data._connectedTargetHandleIds = connectedEdges.filter(edge => edge.target === node.id).map(edge => edge.targetHandle || 'target')
  199. if (node.data.type === BlockEnum.IfElse) {
  200. const nodeData = node.data as IfElseNodeType
  201. if (!nodeData.cases && nodeData.logical_operator && nodeData.conditions) {
  202. (node.data as IfElseNodeType).cases = [
  203. {
  204. case_id: 'true',
  205. logical_operator: nodeData.logical_operator,
  206. conditions: nodeData.conditions,
  207. },
  208. ]
  209. }
  210. node.data._targetBranches = branchNameCorrect([
  211. ...(node.data as IfElseNodeType).cases.map(item => ({ id: item.case_id, name: '' })),
  212. { id: 'false', name: '' },
  213. ])
  214. }
  215. if (node.data.type === BlockEnum.QuestionClassifier) {
  216. node.data._targetBranches = (node.data as QuestionClassifierNodeType).classes.map((topic) => {
  217. return topic
  218. })
  219. }
  220. if (node.data.type === BlockEnum.Iteration) {
  221. const iterationNodeData = node.data as IterationNodeType
  222. iterationNodeData._children = iterationOrLoopNodeMap[node.id] || []
  223. iterationNodeData.is_parallel = iterationNodeData.is_parallel || false
  224. iterationNodeData.parallel_nums = iterationNodeData.parallel_nums || 10
  225. iterationNodeData.error_handle_mode = iterationNodeData.error_handle_mode || ErrorHandleMode.Terminated
  226. }
  227. // TODO: loop error handle mode
  228. if (node.data.type === BlockEnum.Loop) {
  229. const loopNodeData = node.data as LoopNodeType
  230. loopNodeData._children = iterationOrLoopNodeMap[node.id] || []
  231. loopNodeData.error_handle_mode = loopNodeData.error_handle_mode || ErrorHandleMode.Terminated
  232. }
  233. // legacy provider handle
  234. if (node.data.type === BlockEnum.LLM)
  235. (node as any).data.model.provider = correctModelProvider((node as any).data.model.provider)
  236. if (node.data.type === BlockEnum.KnowledgeRetrieval && (node as any).data.multiple_retrieval_config?.reranking_model)
  237. (node as any).data.multiple_retrieval_config.reranking_model.provider = correctModelProvider((node as any).data.multiple_retrieval_config?.reranking_model.provider)
  238. if (node.data.type === BlockEnum.QuestionClassifier)
  239. (node as any).data.model.provider = correctModelProvider((node as any).data.model.provider)
  240. if (node.data.type === BlockEnum.ParameterExtractor)
  241. (node as any).data.model.provider = correctModelProvider((node as any).data.model.provider)
  242. if (node.data.type === BlockEnum.HttpRequest && !node.data.retry_config) {
  243. node.data.retry_config = {
  244. retry_enabled: true,
  245. max_retries: DEFAULT_RETRY_MAX,
  246. retry_interval: DEFAULT_RETRY_INTERVAL,
  247. }
  248. }
  249. return node
  250. })
  251. }
  252. export const initialEdges = (originEdges: Edge[], originNodes: Node[]) => {
  253. const { nodes, edges } = preprocessNodesAndEdges(cloneDeep(originNodes), cloneDeep(originEdges))
  254. let selectedNode: Node | null = null
  255. const nodesMap = nodes.reduce((acc, node) => {
  256. acc[node.id] = node
  257. if (node.data?.selected)
  258. selectedNode = node
  259. return acc
  260. }, {} as Record<string, Node>)
  261. const cycleEdges = getCycleEdges(nodes, edges)
  262. return edges.filter((edge) => {
  263. return !cycleEdges.find(cycEdge => cycEdge.source === edge.source && cycEdge.target === edge.target)
  264. }).map((edge) => {
  265. edge.type = 'custom'
  266. if (!edge.sourceHandle)
  267. edge.sourceHandle = 'source'
  268. if (!edge.targetHandle)
  269. edge.targetHandle = 'target'
  270. if (!edge.data?.sourceType && edge.source && nodesMap[edge.source]) {
  271. edge.data = {
  272. ...edge.data,
  273. sourceType: nodesMap[edge.source].data.type!,
  274. } as any
  275. }
  276. if (!edge.data?.targetType && edge.target && nodesMap[edge.target]) {
  277. edge.data = {
  278. ...edge.data,
  279. targetType: nodesMap[edge.target].data.type!,
  280. } as any
  281. }
  282. if (selectedNode) {
  283. edge.data = {
  284. ...edge.data,
  285. _connectedNodeIsSelected: edge.source === selectedNode.id || edge.target === selectedNode.id,
  286. } as any
  287. }
  288. return edge
  289. })
  290. }