tracing-panel.tsx 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. 'use client'
  2. import type { FC } from 'react'
  3. import
  4. React,
  5. {
  6. useCallback,
  7. useState,
  8. } from 'react'
  9. import cn from 'classnames'
  10. import {
  11. RiArrowDownSLine,
  12. RiMenu4Line,
  13. } from '@remixicon/react'
  14. import { useTranslation } from 'react-i18next'
  15. import NodePanel from './node'
  16. import {
  17. BlockEnum,
  18. } from '@/app/components/workflow/types'
  19. import type { IterationDurationMap, NodeTracing } from '@/types/workflow'
  20. type TracingPanelProps = {
  21. list: NodeTracing[]
  22. onShowIterationDetail?: (detail: NodeTracing[][], iterDurationMap: IterationDurationMap) => void
  23. className?: string
  24. hideNodeInfo?: boolean
  25. hideNodeProcessDetail?: boolean
  26. }
  27. type TracingNodeProps = {
  28. id: string
  29. uniqueId: string
  30. isParallel: boolean
  31. data: NodeTracing | null
  32. children: TracingNodeProps[]
  33. parallelTitle?: string
  34. branchTitle?: string
  35. hideNodeInfo?: boolean
  36. hideNodeProcessDetail?: boolean
  37. }
  38. function buildLogTree(nodes: NodeTracing[], t: (key: string) => string): TracingNodeProps[] {
  39. const rootNodes: TracingNodeProps[] = []
  40. const parallelStacks: { [key: string]: TracingNodeProps } = {}
  41. const levelCounts: { [key: string]: number } = {}
  42. const parallelChildCounts: { [key: string]: Set<string> } = {}
  43. let uniqueIdCounter = 0
  44. const getUniqueId = () => {
  45. uniqueIdCounter++
  46. return `unique-${uniqueIdCounter}`
  47. }
  48. const getParallelTitle = (parentId: string | null): string => {
  49. const levelKey = parentId || 'root'
  50. if (!levelCounts[levelKey])
  51. levelCounts[levelKey] = 0
  52. levelCounts[levelKey]++
  53. const parentTitle = parentId ? parallelStacks[parentId]?.parallelTitle : ''
  54. const levelNumber = parentTitle ? parseInt(parentTitle.split('-')[1]) + 1 : 1
  55. const letter = parallelChildCounts[levelKey]?.size > 1 ? String.fromCharCode(64 + levelCounts[levelKey]) : ''
  56. return `${t('workflow.common.parallel')}-${levelNumber}${letter}`
  57. }
  58. const getBranchTitle = (parentId: string | null, branchNum: number): string => {
  59. const levelKey = parentId || 'root'
  60. const parentTitle = parentId ? parallelStacks[parentId]?.parallelTitle : ''
  61. const levelNumber = parentTitle ? parseInt(parentTitle.split('-')[1]) + 1 : 1
  62. const letter = parallelChildCounts[levelKey]?.size > 1 ? String.fromCharCode(64 + levelCounts[levelKey]) : ''
  63. const branchLetter = String.fromCharCode(64 + branchNum)
  64. return `${t('workflow.common.branch')}-${levelNumber}${letter}-${branchLetter}`
  65. }
  66. // Count parallel children (for figuring out if we need to use letters)
  67. for (const node of nodes) {
  68. const parent_parallel_id = node.parent_parallel_id ?? node.execution_metadata?.parent_parallel_id ?? null
  69. const parallel_id = node.parallel_id ?? node.execution_metadata?.parallel_id ?? null
  70. if (parallel_id) {
  71. const parentKey = parent_parallel_id || 'root'
  72. if (!parallelChildCounts[parentKey])
  73. parallelChildCounts[parentKey] = new Set()
  74. parallelChildCounts[parentKey].add(parallel_id)
  75. }
  76. }
  77. for (const node of nodes) {
  78. const parallel_id = node.parallel_id ?? node.execution_metadata?.parallel_id ?? null
  79. const parent_parallel_id = node.parent_parallel_id ?? node.execution_metadata?.parent_parallel_id ?? null
  80. const parallel_start_node_id = node.parallel_start_node_id ?? node.execution_metadata?.parallel_start_node_id ?? null
  81. const parent_parallel_start_node_id = node.parent_parallel_start_node_id ?? node.execution_metadata?.parent_parallel_start_node_id ?? null
  82. if (!parallel_id || node.node_type === BlockEnum.End) {
  83. rootNodes.push({
  84. id: node.id,
  85. uniqueId: getUniqueId(),
  86. isParallel: false,
  87. data: node,
  88. children: [],
  89. })
  90. }
  91. else {
  92. if (!parallelStacks[parallel_id]) {
  93. const newParallelGroup: TracingNodeProps = {
  94. id: parallel_id,
  95. uniqueId: getUniqueId(),
  96. isParallel: true,
  97. data: null,
  98. children: [],
  99. parallelTitle: '',
  100. }
  101. parallelStacks[parallel_id] = newParallelGroup
  102. if (parent_parallel_id && parallelStacks[parent_parallel_id]) {
  103. const sameBranchIndex = parallelStacks[parent_parallel_id].children.findLastIndex(c =>
  104. c.data?.execution_metadata?.parallel_start_node_id === parent_parallel_start_node_id || c.data?.parallel_start_node_id === parent_parallel_start_node_id,
  105. )
  106. parallelStacks[parent_parallel_id].children.splice(sameBranchIndex + 1, 0, newParallelGroup)
  107. newParallelGroup.parallelTitle = getParallelTitle(parent_parallel_id)
  108. }
  109. else {
  110. newParallelGroup.parallelTitle = getParallelTitle(parent_parallel_id)
  111. rootNodes.push(newParallelGroup)
  112. }
  113. }
  114. const branchTitle = parallel_start_node_id === node.node_id ? getBranchTitle(parent_parallel_id, parallelStacks[parallel_id].children.length + 1) : ''
  115. if (branchTitle) {
  116. parallelStacks[parallel_id].children.push({
  117. id: node.id,
  118. uniqueId: getUniqueId(),
  119. isParallel: false,
  120. data: node,
  121. children: [],
  122. branchTitle,
  123. })
  124. }
  125. else {
  126. let sameBranchIndex = parallelStacks[parallel_id].children.findLastIndex(c =>
  127. c.data?.execution_metadata?.parallel_start_node_id === parallel_start_node_id || c.data?.parallel_start_node_id === parallel_start_node_id,
  128. )
  129. if (parallelStacks[parallel_id].children[sameBranchIndex + 1]?.isParallel)
  130. sameBranchIndex++
  131. parallelStacks[parallel_id].children.splice(sameBranchIndex + 1, 0, {
  132. id: node.id,
  133. uniqueId: getUniqueId(),
  134. isParallel: false,
  135. data: node,
  136. children: [],
  137. branchTitle,
  138. })
  139. }
  140. }
  141. }
  142. return rootNodes
  143. }
  144. const TracingPanel: FC<TracingPanelProps> = ({
  145. list,
  146. onShowIterationDetail,
  147. className,
  148. hideNodeInfo = false,
  149. hideNodeProcessDetail = false,
  150. }) => {
  151. const { t } = useTranslation()
  152. const treeNodes = buildLogTree(list, t)
  153. const [collapsedNodes, setCollapsedNodes] = useState<Set<string>>(new Set())
  154. const [hoveredParallel, setHoveredParallel] = useState<string | null>(null)
  155. const toggleCollapse = (id: string) => {
  156. setCollapsedNodes((prev) => {
  157. const newSet = new Set(prev)
  158. if (newSet.has(id))
  159. newSet.delete(id)
  160. else
  161. newSet.add(id)
  162. return newSet
  163. })
  164. }
  165. const handleParallelMouseEnter = useCallback((id: string) => {
  166. setHoveredParallel(id)
  167. }, [])
  168. const handleParallelMouseLeave = useCallback((e: React.MouseEvent) => {
  169. const relatedTarget = e.relatedTarget as Element | null
  170. if (relatedTarget && 'closest' in relatedTarget) {
  171. const closestParallel = relatedTarget.closest('[data-parallel-id]')
  172. if (closestParallel)
  173. setHoveredParallel(closestParallel.getAttribute('data-parallel-id'))
  174. else
  175. setHoveredParallel(null)
  176. }
  177. else {
  178. setHoveredParallel(null)
  179. }
  180. }, [])
  181. const renderNode = (node: TracingNodeProps) => {
  182. if (node.isParallel) {
  183. const isCollapsed = collapsedNodes.has(node.id)
  184. const isHovered = hoveredParallel === node.id
  185. return (
  186. <div
  187. key={node.uniqueId}
  188. className="ml-4 mb-2 relative"
  189. data-parallel-id={node.id}
  190. onMouseEnter={() => handleParallelMouseEnter(node.id)}
  191. onMouseLeave={handleParallelMouseLeave}
  192. >
  193. <div className="flex items-center mb-1">
  194. <button
  195. onClick={() => toggleCollapse(node.id)}
  196. className={cn(
  197. 'mr-2 transition-colors',
  198. isHovered ? 'rounded border-components-button-primary-border bg-components-button-primary-bg text-text-primary-on-surface' : 'text-text-secondary hover:text-text-primary',
  199. )}
  200. >
  201. {isHovered ? <RiArrowDownSLine className="w-3 h-3" /> : <RiMenu4Line className="w-3 h-3 text-text-tertiary" />}
  202. </button>
  203. <div className="system-xs-semibold-uppercase text-text-secondary flex items-center">
  204. <span>{node.parallelTitle}</span>
  205. </div>
  206. <div
  207. className="mx-2 flex-grow h-px bg-divider-subtle"
  208. style={{ background: 'linear-gradient(to right, rgba(16, 24, 40, 0.08), rgba(255, 255, 255, 0)' }}
  209. ></div>
  210. </div>
  211. <div className={`pl-2 relative ${isCollapsed ? 'hidden' : ''}`}>
  212. <div className={cn(
  213. 'absolute top-0 bottom-0 left-[5px] w-[2px]',
  214. isHovered ? 'bg-text-accent-secondary' : 'bg-divider-subtle',
  215. )}></div>
  216. {node.children.map(renderNode)}
  217. </div>
  218. </div>
  219. )
  220. }
  221. else {
  222. const isHovered = hoveredParallel === node.id
  223. return (
  224. <div key={node.uniqueId}>
  225. <div className={cn('pl-4 -mb-1.5 system-2xs-medium-uppercase', isHovered ? 'text-text-tertiary' : 'text-text-quaternary')}>
  226. {node.branchTitle}
  227. </div>
  228. <NodePanel
  229. nodeInfo={node.data!}
  230. onShowIterationDetail={onShowIterationDetail}
  231. justShowIterationNavArrow={true}
  232. hideInfo={hideNodeInfo}
  233. hideProcessDetail={hideNodeProcessDetail}
  234. />
  235. </div>
  236. )
  237. }
  238. }
  239. return (
  240. <div className={cn(className || 'bg-components-panel-bg', 'py-2')}>
  241. {treeNodes.map(renderNode)}
  242. </div>
  243. )
  244. }
  245. export default TracingPanel