node.tsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. 'use client'
  2. import { useTranslation } from 'react-i18next'
  3. import type { FC } from 'react'
  4. import { useCallback, useEffect, useState } from 'react'
  5. import cn from 'classnames'
  6. import BlockIcon from '../block-icon'
  7. import { BlockEnum } from '../types'
  8. import Split from '../nodes/_base/components/split'
  9. import CodeEditor from '@/app/components/workflow/nodes/_base/components/editor/code-editor'
  10. import { CodeLanguage } from '@/app/components/workflow/nodes/code/types'
  11. import { AlertCircle, AlertTriangle } from '@/app/components/base/icons/src/vender/line/alertsAndFeedback'
  12. import { CheckCircle, Loading02 } from '@/app/components/base/icons/src/vender/line/general'
  13. import { ArrowNarrowRight, ChevronRight } from '@/app/components/base/icons/src/vender/line/arrows'
  14. import type { NodeTracing } from '@/types/workflow'
  15. type Props = {
  16. className?: string
  17. nodeInfo: NodeTracing
  18. hideInfo?: boolean
  19. hideProcessDetail?: boolean
  20. onShowIterationDetail?: (detail: NodeTracing[][]) => void
  21. notShowIterationNav?: boolean
  22. justShowIterationNavArrow?: boolean
  23. }
  24. const NodePanel: FC<Props> = ({
  25. className,
  26. nodeInfo,
  27. hideInfo = false,
  28. hideProcessDetail,
  29. onShowIterationDetail,
  30. notShowIterationNav,
  31. justShowIterationNavArrow,
  32. }) => {
  33. const [collapseState, doSetCollapseState] = useState<boolean>(true)
  34. const setCollapseState = useCallback((state: boolean) => {
  35. if (hideProcessDetail)
  36. return
  37. doSetCollapseState(state)
  38. }, [hideProcessDetail])
  39. const { t } = useTranslation()
  40. const getTime = (time: number) => {
  41. if (time < 1)
  42. return `${(time * 1000).toFixed(3)} ms`
  43. if (time > 60)
  44. return `${parseInt(Math.round(time / 60).toString())} m ${(time % 60).toFixed(3)} s`
  45. return `${time.toFixed(3)} s`
  46. }
  47. const getTokenCount = (tokens: number) => {
  48. if (tokens < 1000)
  49. return tokens
  50. if (tokens >= 1000 && tokens < 1000000)
  51. return `${parseFloat((tokens / 1000).toFixed(3))}K`
  52. if (tokens >= 1000000)
  53. return `${parseFloat((tokens / 1000000).toFixed(3))}M`
  54. }
  55. useEffect(() => {
  56. setCollapseState(!nodeInfo.expand)
  57. }, [nodeInfo.expand, setCollapseState])
  58. const isIterationNode = nodeInfo.node_type === BlockEnum.Iteration
  59. const handleOnShowIterationDetail = (e: React.MouseEvent<HTMLDivElement>) => {
  60. e.stopPropagation()
  61. e.nativeEvent.stopImmediatePropagation()
  62. onShowIterationDetail?.(nodeInfo.details || [])
  63. }
  64. return (
  65. <div className={cn('px-4 py-1', className, hideInfo && '!p-0')}>
  66. <div className={cn('group transition-all bg-white border border-gray-100 rounded-2xl shadow-xs hover:shadow-md', hideInfo && '!rounded-lg')}>
  67. <div
  68. className={cn(
  69. 'flex items-center pl-[6px] pr-3 cursor-pointer',
  70. hideInfo ? 'py-2' : 'py-3',
  71. !collapseState && (hideInfo ? '!pb-1' : '!pb-2'),
  72. )}
  73. onClick={() => setCollapseState(!collapseState)}
  74. >
  75. {!hideProcessDetail && (
  76. <ChevronRight
  77. className={cn(
  78. 'shrink-0 w-3 h-3 mr-1 text-gray-400 transition-all group-hover:text-gray-500',
  79. !collapseState && 'rotate-90',
  80. )}
  81. />
  82. )}
  83. <BlockIcon size={hideInfo ? 'xs' : 'sm'} className={cn('shrink-0 mr-2', hideInfo && '!mr-1')} type={nodeInfo.node_type} toolIcon={nodeInfo.extras?.icon || nodeInfo.extras} />
  84. <div className={cn(
  85. 'grow text-gray-700 text-[13px] leading-[16px] font-semibold truncate',
  86. hideInfo && '!text-xs',
  87. )} title={nodeInfo.title}>{nodeInfo.title}</div>
  88. {nodeInfo.status !== 'running' && !hideInfo && (
  89. <div className='shrink-0 text-gray-500 text-xs leading-[18px]'>{`${getTime(nodeInfo.elapsed_time || 0)} · ${getTokenCount(nodeInfo.execution_metadata?.total_tokens || 0)} tokens`}</div>
  90. )}
  91. {nodeInfo.status === 'succeeded' && (
  92. <CheckCircle className='shrink-0 ml-2 w-3.5 h-3.5 text-[#12B76A]' />
  93. )}
  94. {nodeInfo.status === 'failed' && (
  95. <AlertCircle className='shrink-0 ml-2 w-3.5 h-3.5 text-[#F04438]' />
  96. )}
  97. {nodeInfo.status === 'stopped' && (
  98. <AlertTriangle className='shrink-0 ml-2 w-3.5 h-3.5 text-[#F79009]' />
  99. )}
  100. {nodeInfo.status === 'running' && (
  101. <div className='shrink-0 flex items-center text-primary-600 text-[13px] leading-[16px] font-medium'>
  102. <span className='mr-2 text-xs font-normal'>Running</span>
  103. <Loading02 className='w-3.5 h-3.5 animate-spin' />
  104. </div>
  105. )}
  106. </div>
  107. {!collapseState && !hideProcessDetail && (
  108. <div className='pb-2'>
  109. {/* The nav to the iteration detail */}
  110. {isIterationNode && !notShowIterationNav && (
  111. <div className='mt-2 mb-1 !px-2'>
  112. <div
  113. className='flex items-center h-[34px] justify-between px-3 bg-gray-100 border-[0.5px] border-gray-200 rounded-lg cursor-pointer'
  114. onClick={handleOnShowIterationDetail}>
  115. <div className='leading-[18px] text-[13px] font-medium text-gray-700'>{t('workflow.nodes.iteration.iteration', { count: nodeInfo.metadata?.iterator_length || (nodeInfo.execution_metadata?.steps_boundary?.length - 1) })}</div>
  116. {justShowIterationNavArrow
  117. ? (
  118. <ArrowNarrowRight className='w-3.5 h-3.5 text-gray-500' />
  119. )
  120. : (
  121. <div className='flex items-center space-x-1 text-[#155EEF]'>
  122. <div className='text-[13px] font-normal '>{t('workflow.common.viewDetailInTracingPanel')}</div>
  123. <ArrowNarrowRight className='w-3.5 h-3.5' />
  124. </div>
  125. )}
  126. </div>
  127. <Split className='mt-2' />
  128. </div>
  129. )}
  130. <div className={cn('px-[10px] py-1', hideInfo && '!px-2 !py-0.5')}>
  131. {nodeInfo.status === 'stopped' && (
  132. <div className='px-3 py-[10px] bg-[#fffaeb] rounded-lg border-[0.5px] border-[rbga(0,0,0,0.05)] text-xs leading-[18px] text-[#dc6803] shadow-xs'>{t('workflow.tracing.stopBy', { user: nodeInfo.created_by ? nodeInfo.created_by.name : 'N/A' })}</div>
  133. )}
  134. {nodeInfo.status === 'failed' && (
  135. <div className='px-3 py-[10px] bg-[#fef3f2] rounded-lg border-[0.5px] border-[rbga(0,0,0,0.05)] text-xs leading-[18px] text-[#d92d20] shadow-xs'>{nodeInfo.error}</div>
  136. )}
  137. </div>
  138. {nodeInfo.inputs && (
  139. <div className={cn('px-[10px] py-1', hideInfo && '!px-2 !py-0.5')}>
  140. <CodeEditor
  141. readOnly
  142. title={<div>{t('workflow.common.input').toLocaleUpperCase()}</div>}
  143. language={CodeLanguage.json}
  144. value={nodeInfo.inputs}
  145. isJSONStringifyBeauty
  146. />
  147. </div>
  148. )}
  149. {nodeInfo.process_data && (
  150. <div className={cn('px-[10px] py-1', hideInfo && '!px-2 !py-0.5')}>
  151. <CodeEditor
  152. readOnly
  153. title={<div>{t('workflow.common.processData').toLocaleUpperCase()}</div>}
  154. language={CodeLanguage.json}
  155. value={nodeInfo.process_data}
  156. isJSONStringifyBeauty
  157. />
  158. </div>
  159. )}
  160. {nodeInfo.outputs && (
  161. <div className={cn('px-[10px] py-1', hideInfo && '!px-2 !py-0.5')}>
  162. <CodeEditor
  163. readOnly
  164. title={<div>{t('workflow.common.output').toLocaleUpperCase()}</div>}
  165. language={CodeLanguage.json}
  166. value={nodeInfo.outputs}
  167. isJSONStringifyBeauty
  168. />
  169. </div>
  170. )}
  171. </div>
  172. )}
  173. </div>
  174. </div>
  175. )
  176. }
  177. export default NodePanel