You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. import {
  2. getConnectedEdges,
  3. getIncomers,
  4. getOutgoers,
  5. } from 'reactflow'
  6. import { v4 as uuid4 } from 'uuid'
  7. import {
  8. groupBy,
  9. isEqual,
  10. uniqBy,
  11. } from 'lodash-es'
  12. import type {
  13. Edge,
  14. Node,
  15. } from '../types'
  16. import {
  17. BlockEnum,
  18. } from '../types'
  19. import type { IterationNodeType } from '../nodes/iteration/types'
  20. import type { LoopNodeType } from '../nodes/loop/types'
  21. export const canRunBySingle = (nodeType: BlockEnum, isChildNode: boolean) => {
  22. // child node means in iteration or loop. Set value to iteration(or loop) may cause variable not exit problem in backend.
  23. if(isChildNode && nodeType === BlockEnum.Assigner)
  24. return false
  25. return nodeType === BlockEnum.LLM
  26. || nodeType === BlockEnum.KnowledgeRetrieval
  27. || nodeType === BlockEnum.Code
  28. || nodeType === BlockEnum.TemplateTransform
  29. || nodeType === BlockEnum.QuestionClassifier
  30. || nodeType === BlockEnum.HttpRequest
  31. || nodeType === BlockEnum.Tool
  32. || nodeType === BlockEnum.ParameterExtractor
  33. || nodeType === BlockEnum.Iteration
  34. || nodeType === BlockEnum.Agent
  35. || nodeType === BlockEnum.DocExtractor
  36. || nodeType === BlockEnum.Loop
  37. || nodeType === BlockEnum.Start
  38. || nodeType === BlockEnum.IfElse
  39. || nodeType === BlockEnum.VariableAggregator
  40. || nodeType === BlockEnum.Assigner
  41. }
  42. type ConnectedSourceOrTargetNodesChange = {
  43. type: string
  44. edge: Edge
  45. }[]
  46. export const getNodesConnectedSourceOrTargetHandleIdsMap = (changes: ConnectedSourceOrTargetNodesChange, nodes: Node[]) => {
  47. const nodesConnectedSourceOrTargetHandleIdsMap = {} as Record<string, any>
  48. changes.forEach((change) => {
  49. const {
  50. edge,
  51. type,
  52. } = change
  53. const sourceNode = nodes.find(node => node.id === edge.source)!
  54. if (sourceNode) {
  55. nodesConnectedSourceOrTargetHandleIdsMap[sourceNode.id] = nodesConnectedSourceOrTargetHandleIdsMap[sourceNode.id] || {
  56. _connectedSourceHandleIds: [...(sourceNode?.data._connectedSourceHandleIds || [])],
  57. _connectedTargetHandleIds: [...(sourceNode?.data._connectedTargetHandleIds || [])],
  58. }
  59. }
  60. const targetNode = nodes.find(node => node.id === edge.target)!
  61. if (targetNode) {
  62. nodesConnectedSourceOrTargetHandleIdsMap[targetNode.id] = nodesConnectedSourceOrTargetHandleIdsMap[targetNode.id] || {
  63. _connectedSourceHandleIds: [...(targetNode?.data._connectedSourceHandleIds || [])],
  64. _connectedTargetHandleIds: [...(targetNode?.data._connectedTargetHandleIds || [])],
  65. }
  66. }
  67. if (sourceNode) {
  68. if (type === 'remove') {
  69. const index = nodesConnectedSourceOrTargetHandleIdsMap[sourceNode.id]._connectedSourceHandleIds.findIndex((handleId: string) => handleId === edge.sourceHandle)
  70. nodesConnectedSourceOrTargetHandleIdsMap[sourceNode.id]._connectedSourceHandleIds.splice(index, 1)
  71. }
  72. if (type === 'add')
  73. nodesConnectedSourceOrTargetHandleIdsMap[sourceNode.id]._connectedSourceHandleIds.push(edge.sourceHandle || 'source')
  74. }
  75. if (targetNode) {
  76. if (type === 'remove') {
  77. const index = nodesConnectedSourceOrTargetHandleIdsMap[targetNode.id]._connectedTargetHandleIds.findIndex((handleId: string) => handleId === edge.targetHandle)
  78. nodesConnectedSourceOrTargetHandleIdsMap[targetNode.id]._connectedTargetHandleIds.splice(index, 1)
  79. }
  80. if (type === 'add')
  81. nodesConnectedSourceOrTargetHandleIdsMap[targetNode.id]._connectedTargetHandleIds.push(edge.targetHandle || 'target')
  82. }
  83. })
  84. return nodesConnectedSourceOrTargetHandleIdsMap
  85. }
  86. export const getValidTreeNodes = (nodes: Node[], edges: Edge[]) => {
  87. const startNode = nodes.find(node => node.data.type === BlockEnum.Start)
  88. if (!startNode) {
  89. return {
  90. validNodes: [],
  91. maxDepth: 0,
  92. }
  93. }
  94. const list: Node[] = [startNode]
  95. let maxDepth = 1
  96. const traverse = (root: Node, depth: number) => {
  97. if (depth > maxDepth)
  98. maxDepth = depth
  99. const outgoers = getOutgoers(root, nodes, edges)
  100. if (outgoers.length) {
  101. outgoers.forEach((outgoer) => {
  102. list.push(outgoer)
  103. if (outgoer.data.type === BlockEnum.Iteration)
  104. list.push(...nodes.filter(node => node.parentId === outgoer.id))
  105. if (outgoer.data.type === BlockEnum.Loop)
  106. list.push(...nodes.filter(node => node.parentId === outgoer.id))
  107. traverse(outgoer, depth + 1)
  108. })
  109. }
  110. else {
  111. list.push(root)
  112. if (root.data.type === BlockEnum.Iteration)
  113. list.push(...nodes.filter(node => node.parentId === root.id))
  114. if (root.data.type === BlockEnum.Loop)
  115. list.push(...nodes.filter(node => node.parentId === root.id))
  116. }
  117. }
  118. traverse(startNode, maxDepth)
  119. return {
  120. validNodes: uniqBy(list, 'id'),
  121. maxDepth,
  122. }
  123. }
  124. export const changeNodesAndEdgesId = (nodes: Node[], edges: Edge[]) => {
  125. const idMap = nodes.reduce((acc, node) => {
  126. acc[node.id] = uuid4()
  127. return acc
  128. }, {} as Record<string, string>)
  129. const newNodes = nodes.map((node) => {
  130. return {
  131. ...node,
  132. id: idMap[node.id],
  133. }
  134. })
  135. const newEdges = edges.map((edge) => {
  136. return {
  137. ...edge,
  138. source: idMap[edge.source],
  139. target: idMap[edge.target],
  140. }
  141. })
  142. return [newNodes, newEdges] as [Node[], Edge[]]
  143. }
  144. type ParallelInfoItem = {
  145. parallelNodeId: string
  146. depth: number
  147. isBranch?: boolean
  148. }
  149. type NodeParallelInfo = {
  150. parallelNodeId: string
  151. edgeHandleId: string
  152. depth: number
  153. }
  154. type NodeHandle = {
  155. node: Node
  156. handle: string
  157. }
  158. type NodeStreamInfo = {
  159. upstreamNodes: Set<string>
  160. downstreamEdges: Set<string>
  161. }
  162. export const getParallelInfo = (nodes: Node[], edges: Edge[], parentNodeId?: string) => {
  163. let startNode
  164. if (parentNodeId) {
  165. const parentNode = nodes.find(node => node.id === parentNodeId)
  166. if (!parentNode)
  167. throw new Error('Parent node not found')
  168. startNode = nodes.find(node => node.id === (parentNode.data as (IterationNodeType | LoopNodeType)).start_node_id)
  169. }
  170. else {
  171. startNode = nodes.find(node => node.data.type === BlockEnum.Start)
  172. }
  173. if (!startNode)
  174. throw new Error('Start node not found')
  175. const parallelList = [] as ParallelInfoItem[]
  176. const nextNodeHandles = [{ node: startNode, handle: 'source' }]
  177. let hasAbnormalEdges = false
  178. const traverse = (firstNodeHandle: NodeHandle) => {
  179. const nodeEdgesSet = {} as Record<string, Set<string>>
  180. const totalEdgesSet = new Set<string>()
  181. const nextHandles = [firstNodeHandle]
  182. const streamInfo = {} as Record<string, NodeStreamInfo>
  183. const parallelListItem = {
  184. parallelNodeId: '',
  185. depth: 0,
  186. } as ParallelInfoItem
  187. const nodeParallelInfoMap = {} as Record<string, NodeParallelInfo>
  188. nodeParallelInfoMap[firstNodeHandle.node.id] = {
  189. parallelNodeId: '',
  190. edgeHandleId: '',
  191. depth: 0,
  192. }
  193. while (nextHandles.length) {
  194. const currentNodeHandle = nextHandles.shift()!
  195. const { node: currentNode, handle: currentHandle = 'source' } = currentNodeHandle
  196. const currentNodeHandleKey = currentNode.id
  197. const connectedEdges = edges.filter(edge => edge.source === currentNode.id && edge.sourceHandle === currentHandle)
  198. const connectedEdgesLength = connectedEdges.length
  199. const outgoers = nodes.filter(node => connectedEdges.some(edge => edge.target === node.id))
  200. const incomers = getIncomers(currentNode, nodes, edges)
  201. if (!streamInfo[currentNodeHandleKey]) {
  202. streamInfo[currentNodeHandleKey] = {
  203. upstreamNodes: new Set<string>(),
  204. downstreamEdges: new Set<string>(),
  205. }
  206. }
  207. if (nodeEdgesSet[currentNodeHandleKey]?.size > 0 && incomers.length > 1) {
  208. const newSet = new Set<string>()
  209. for (const item of totalEdgesSet) {
  210. if (!streamInfo[currentNodeHandleKey].downstreamEdges.has(item))
  211. newSet.add(item)
  212. }
  213. if (isEqual(nodeEdgesSet[currentNodeHandleKey], newSet)) {
  214. parallelListItem.depth = nodeParallelInfoMap[currentNode.id].depth
  215. nextNodeHandles.push({ node: currentNode, handle: currentHandle })
  216. break
  217. }
  218. }
  219. if (nodeParallelInfoMap[currentNode.id].depth > parallelListItem.depth)
  220. parallelListItem.depth = nodeParallelInfoMap[currentNode.id].depth
  221. outgoers.forEach((outgoer) => {
  222. const outgoerConnectedEdges = getConnectedEdges([outgoer], edges).filter(edge => edge.source === outgoer.id)
  223. const sourceEdgesGroup = groupBy(outgoerConnectedEdges, 'sourceHandle')
  224. const incomers = getIncomers(outgoer, nodes, edges)
  225. if (outgoers.length > 1 && incomers.length > 1)
  226. hasAbnormalEdges = true
  227. Object.keys(sourceEdgesGroup).forEach((sourceHandle) => {
  228. nextHandles.push({ node: outgoer, handle: sourceHandle })
  229. })
  230. if (!outgoerConnectedEdges.length)
  231. nextHandles.push({ node: outgoer, handle: 'source' })
  232. const outgoerKey = outgoer.id
  233. if (!nodeEdgesSet[outgoerKey])
  234. nodeEdgesSet[outgoerKey] = new Set<string>()
  235. if (nodeEdgesSet[currentNodeHandleKey]) {
  236. for (const item of nodeEdgesSet[currentNodeHandleKey])
  237. nodeEdgesSet[outgoerKey].add(item)
  238. }
  239. if (!streamInfo[outgoerKey]) {
  240. streamInfo[outgoerKey] = {
  241. upstreamNodes: new Set<string>(),
  242. downstreamEdges: new Set<string>(),
  243. }
  244. }
  245. if (!nodeParallelInfoMap[outgoer.id]) {
  246. nodeParallelInfoMap[outgoer.id] = {
  247. ...nodeParallelInfoMap[currentNode.id],
  248. }
  249. }
  250. if (connectedEdgesLength > 1) {
  251. const edge = connectedEdges.find(edge => edge.target === outgoer.id)!
  252. nodeEdgesSet[outgoerKey].add(edge.id)
  253. totalEdgesSet.add(edge.id)
  254. streamInfo[currentNodeHandleKey].downstreamEdges.add(edge.id)
  255. streamInfo[outgoerKey].upstreamNodes.add(currentNodeHandleKey)
  256. for (const item of streamInfo[currentNodeHandleKey].upstreamNodes)
  257. streamInfo[item].downstreamEdges.add(edge.id)
  258. if (!parallelListItem.parallelNodeId)
  259. parallelListItem.parallelNodeId = currentNode.id
  260. const prevDepth = nodeParallelInfoMap[currentNode.id].depth + 1
  261. const currentDepth = nodeParallelInfoMap[outgoer.id].depth
  262. nodeParallelInfoMap[outgoer.id].depth = Math.max(prevDepth, currentDepth)
  263. }
  264. else {
  265. for (const item of streamInfo[currentNodeHandleKey].upstreamNodes)
  266. streamInfo[outgoerKey].upstreamNodes.add(item)
  267. nodeParallelInfoMap[outgoer.id].depth = nodeParallelInfoMap[currentNode.id].depth
  268. }
  269. })
  270. }
  271. parallelList.push(parallelListItem)
  272. }
  273. while (nextNodeHandles.length) {
  274. const nodeHandle = nextNodeHandles.shift()!
  275. traverse(nodeHandle)
  276. }
  277. return {
  278. parallelList,
  279. hasAbnormalEdges,
  280. }
  281. }
  282. export const hasErrorHandleNode = (nodeType?: BlockEnum) => {
  283. return nodeType === BlockEnum.LLM || nodeType === BlockEnum.Tool || nodeType === BlockEnum.HttpRequest || nodeType === BlockEnum.Code
  284. }