index.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import type { NodeTracing } from '@/types/workflow'
  2. import { addChildrenToIterationNode } from './iteration'
  3. import { addChildrenToLoopNode } from './loop'
  4. import formatParallelNode from './parallel'
  5. import formatRetryNode from './retry'
  6. import formatAgentNode from './agent'
  7. import { cloneDeep } from 'lodash-es'
  8. import { BlockEnum } from '../../../types'
  9. const formatIterationAndLoopNode = (list: NodeTracing[], t: any) => {
  10. const clonedList = cloneDeep(list)
  11. // Identify all loop and iteration nodes
  12. const loopNodeIds = clonedList
  13. .filter(item => item.node_type === BlockEnum.Loop)
  14. .map(item => item.node_id)
  15. const iterationNodeIds = clonedList
  16. .filter(item => item.node_type === BlockEnum.Iteration)
  17. .map(item => item.node_id)
  18. // Identify all child nodes for both loop and iteration
  19. const loopChildrenNodeIds = clonedList
  20. .filter(item => item.execution_metadata?.loop_id && loopNodeIds.includes(item.execution_metadata.loop_id))
  21. .map(item => item.node_id)
  22. const iterationChildrenNodeIds = clonedList
  23. .filter(item => item.execution_metadata?.iteration_id && iterationNodeIds.includes(item.execution_metadata.iteration_id))
  24. .map(item => item.node_id)
  25. // Filter out child nodes as they will be included in their parent nodes
  26. const result = clonedList
  27. .filter(item => !loopChildrenNodeIds.includes(item.node_id) && !iterationChildrenNodeIds.includes(item.node_id))
  28. .map((item) => {
  29. // Process Loop nodes
  30. if (item.node_type === BlockEnum.Loop) {
  31. const childrenNodes = clonedList.filter(child => child.execution_metadata?.loop_id === item.node_id)
  32. const error = childrenNodes.find(child => child.status === 'failed')
  33. if (error) {
  34. item.status = 'failed'
  35. item.error = error.error
  36. }
  37. const addedChildrenList = addChildrenToLoopNode(item, childrenNodes)
  38. // Handle parallel nodes in loop node
  39. if (addedChildrenList.details && addedChildrenList.details.length > 0) {
  40. addedChildrenList.details = addedChildrenList.details.map((row) => {
  41. return formatParallelNode(row, t)
  42. })
  43. }
  44. return addedChildrenList
  45. }
  46. // Process Iteration nodes
  47. if (item.node_type === BlockEnum.Iteration) {
  48. const childrenNodes = clonedList.filter(child => child.execution_metadata?.iteration_id === item.node_id)
  49. const error = childrenNodes.find(child => child.status === 'failed')
  50. if (error) {
  51. item.status = 'failed'
  52. item.error = error.error
  53. }
  54. const addedChildrenList = addChildrenToIterationNode(item, childrenNodes)
  55. // Handle parallel nodes in iteration node
  56. if (addedChildrenList.details && addedChildrenList.details.length > 0) {
  57. addedChildrenList.details = addedChildrenList.details.map((row) => {
  58. return formatParallelNode(row, t)
  59. })
  60. }
  61. return addedChildrenList
  62. }
  63. return item
  64. })
  65. return result
  66. }
  67. const formatToTracingNodeList = (list: NodeTracing[], t: any) => {
  68. const allItems = cloneDeep([...list]).sort((a, b) => a.index - b.index)
  69. /*
  70. * First handle not change list structure node
  71. * Because Handle struct node will put the node in different
  72. */
  73. const formattedAgentList = formatAgentNode(allItems)
  74. const formattedRetryList = formatRetryNode(formattedAgentList) // retry one node
  75. // would change the structure of the list. Iteration and parallel can include each other.
  76. const formattedLoopAndIterationList = formatIterationAndLoopNode(formattedRetryList, t)
  77. const formattedParallelList = formatParallelNode(formattedLoopAndIterationList, t)
  78. const result = formattedParallelList
  79. // console.log(allItems)
  80. // console.log(result)
  81. return result
  82. }
  83. export default formatToTracingNodeList