node.tsx 7.8 KB

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