node.tsx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. RiAlertFill,
  7. RiArrowRightSLine,
  8. RiCheckboxCircleFill,
  9. RiErrorWarningLine,
  10. RiLoader2Line,
  11. } from '@remixicon/react'
  12. import BlockIcon from '../block-icon'
  13. import { BlockEnum } from '../types'
  14. import Split from '../nodes/_base/components/split'
  15. import { Iteration } from '@/app/components/base/icons/src/vender/workflow'
  16. import cn from '@/utils/classnames'
  17. import StatusContainer from '@/app/components/workflow/run/status-container'
  18. import CodeEditor from '@/app/components/workflow/nodes/_base/components/editor/code-editor'
  19. import Button from '@/app/components/base/button'
  20. import { CodeLanguage } from '@/app/components/workflow/nodes/code/types'
  21. import type { IterationDurationMap, NodeTracing } from '@/types/workflow'
  22. type Props = {
  23. className?: string
  24. nodeInfo: NodeTracing
  25. inMessage?: boolean
  26. hideInfo?: boolean
  27. hideProcessDetail?: boolean
  28. onShowIterationDetail?: (detail: NodeTracing[][], iterDurationMap: IterationDurationMap) => void
  29. notShowIterationNav?: boolean
  30. justShowIterationNavArrow?: boolean
  31. }
  32. const NodePanel: FC<Props> = ({
  33. className,
  34. nodeInfo,
  35. inMessage = false,
  36. hideInfo = false,
  37. hideProcessDetail,
  38. onShowIterationDetail,
  39. notShowIterationNav,
  40. justShowIterationNavArrow,
  41. }) => {
  42. const [collapseState, doSetCollapseState] = useState<boolean>(true)
  43. const setCollapseState = useCallback((state: boolean) => {
  44. if (hideProcessDetail)
  45. return
  46. doSetCollapseState(state)
  47. }, [hideProcessDetail])
  48. const { t } = useTranslation()
  49. const getTime = (time: number) => {
  50. if (time < 1)
  51. return `${(time * 1000).toFixed(3)} ms`
  52. if (time > 60)
  53. return `${parseInt(Math.round(time / 60).toString())} m ${(time % 60).toFixed(3)} s`
  54. return `${time.toFixed(3)} s`
  55. }
  56. const getTokenCount = (tokens: number) => {
  57. if (tokens < 1000)
  58. return tokens
  59. if (tokens >= 1000 && tokens < 1000000)
  60. return `${parseFloat((tokens / 1000).toFixed(3))}K`
  61. if (tokens >= 1000000)
  62. return `${parseFloat((tokens / 1000000).toFixed(3))}M`
  63. }
  64. const getCount = (iteration_curr_length: number | undefined, iteration_length: number) => {
  65. if ((iteration_curr_length && iteration_curr_length < iteration_length) || !iteration_length)
  66. return iteration_curr_length
  67. return iteration_length
  68. }
  69. const getErrorCount = (details: NodeTracing[][] | undefined) => {
  70. if (!details || details.length === 0)
  71. return 0
  72. return details.reduce((acc, iteration) => {
  73. if (iteration.some(item => item.status === 'failed'))
  74. acc++
  75. return acc
  76. }, 0)
  77. }
  78. useEffect(() => {
  79. setCollapseState(!nodeInfo.expand)
  80. }, [nodeInfo.expand, setCollapseState])
  81. const isIterationNode = nodeInfo.node_type === BlockEnum.Iteration
  82. const handleOnShowIterationDetail = (e: React.MouseEvent<HTMLButtonElement>) => {
  83. e.stopPropagation()
  84. e.nativeEvent.stopImmediatePropagation()
  85. onShowIterationDetail?.(nodeInfo.details || [], nodeInfo?.iterDurationMap || nodeInfo.execution_metadata?.iteration_duration_map || {})
  86. }
  87. return (
  88. <div className={cn('px-2 py-1', className)}>
  89. <div className='group transition-all bg-background-default border border-components-panel-border rounded-[10px] shadow-xs hover:shadow-md'>
  90. <div
  91. className={cn(
  92. 'flex items-center pl-1 pr-3 cursor-pointer',
  93. hideInfo ? 'py-2' : 'py-1.5',
  94. !collapseState && (hideInfo ? '!pb-1' : '!pb-1.5'),
  95. )}
  96. onClick={() => setCollapseState(!collapseState)}
  97. >
  98. {!hideProcessDetail && (
  99. <RiArrowRightSLine
  100. className={cn(
  101. 'shrink-0 w-4 h-4 mr-1 text-text-quaternary transition-all group-hover:text-text-tertiary',
  102. !collapseState && 'rotate-90',
  103. )}
  104. />
  105. )}
  106. <BlockIcon size={inMessage ? 'xs' : 'sm'} className={cn('shrink-0 mr-2', inMessage && '!mr-1')} type={nodeInfo.node_type} toolIcon={nodeInfo.extras?.icon || nodeInfo.extras} />
  107. <div className={cn(
  108. 'grow text-text-secondary system-xs-semibold-uppercase truncate',
  109. hideInfo && '!text-xs',
  110. )} title={nodeInfo.title}>{nodeInfo.title}</div>
  111. {nodeInfo.status !== 'running' && !hideInfo && (
  112. <div className='shrink-0 text-text-tertiary system-xs-regular'>{nodeInfo.execution_metadata?.total_tokens ? `${getTokenCount(nodeInfo.execution_metadata?.total_tokens || 0)} tokens · ` : ''}{`${getTime(nodeInfo.elapsed_time || 0)}`}</div>
  113. )}
  114. {nodeInfo.status === 'succeeded' && (
  115. <RiCheckboxCircleFill className='shrink-0 ml-2 w-3.5 h-3.5 text-text-success' />
  116. )}
  117. {nodeInfo.status === 'failed' && (
  118. <RiErrorWarningLine className='shrink-0 ml-2 w-3.5 h-3.5 text-text-warning' />
  119. )}
  120. {nodeInfo.status === 'stopped' && (
  121. <RiAlertFill className={cn('shrink-0 ml-2 w-4 h-4 text-text-warning-secondary', inMessage && 'w-3.5 h-3.5')} />
  122. )}
  123. {nodeInfo.status === 'running' && (
  124. <div className='shrink-0 flex items-center text-text-accent text-[13px] leading-[16px] font-medium'>
  125. <span className='mr-2 text-xs font-normal'>Running</span>
  126. <RiLoader2Line className='w-3.5 h-3.5 animate-spin' />
  127. </div>
  128. )}
  129. </div>
  130. {!collapseState && !hideProcessDetail && (
  131. <div className='px-1 pb-1'>
  132. {/* The nav to the iteration detail */}
  133. {isIterationNode && !notShowIterationNav && (
  134. <div className='mt-2 mb-1 !px-2'>
  135. <Button
  136. className='flex items-center w-full self-stretch gap-2 px-3 py-2 bg-components-button-tertiary-bg-hover hover:bg-components-button-tertiary-bg-hover rounded-lg cursor-pointer border-none'
  137. onClick={handleOnShowIterationDetail}
  138. >
  139. <Iteration className='w-4 h-4 text-components-button-tertiary-text flex-shrink-0' />
  140. <div className='flex-1 text-left system-sm-medium text-components-button-tertiary-text'>{t('workflow.nodes.iteration.iteration', { count: getCount(nodeInfo.details?.length, nodeInfo.metadata?.iterator_length) })}{getErrorCount(nodeInfo.details) > 0 && (
  141. <>
  142. {t('workflow.nodes.iteration.comma')}
  143. {t('workflow.nodes.iteration.error', { count: getErrorCount(nodeInfo.details) })}
  144. </>
  145. )}</div>
  146. {justShowIterationNavArrow
  147. ? (
  148. <RiArrowRightSLine className='w-4 h-4 text-components-button-tertiary-text flex-shrink-0' />
  149. )
  150. : (
  151. <div className='flex items-center space-x-1 text-[#155EEF]'>
  152. <div className='text-[13px] font-normal '>{t('workflow.common.viewDetailInTracingPanel')}</div>
  153. <RiArrowRightSLine className='w-4 h-4 text-components-button-tertiary-text flex-shrink-0' />
  154. </div>
  155. )}
  156. </Button>
  157. <Split className='mt-2' />
  158. </div>
  159. )}
  160. <div className={cn('px-[10px]', hideInfo && '!px-2 !py-0.5')}>
  161. {nodeInfo.status === 'stopped' && (
  162. <StatusContainer status='stopped'>
  163. {t('workflow.tracing.stopBy', { user: nodeInfo.created_by ? nodeInfo.created_by.name : 'N/A' })}
  164. </StatusContainer>
  165. )}
  166. {nodeInfo.status === 'failed' && (
  167. <StatusContainer status='failed'>
  168. {nodeInfo.error}
  169. </StatusContainer>
  170. )}
  171. </div>
  172. {nodeInfo.inputs && (
  173. <div className={cn('mb-1')}>
  174. <CodeEditor
  175. readOnly
  176. title={<div>{t('workflow.common.input').toLocaleUpperCase()}</div>}
  177. language={CodeLanguage.json}
  178. value={nodeInfo.inputs}
  179. isJSONStringifyBeauty
  180. />
  181. </div>
  182. )}
  183. {nodeInfo.process_data && (
  184. <div className={cn('mb-1')}>
  185. <CodeEditor
  186. readOnly
  187. title={<div>{t('workflow.common.processData').toLocaleUpperCase()}</div>}
  188. language={CodeLanguage.json}
  189. value={nodeInfo.process_data}
  190. isJSONStringifyBeauty
  191. />
  192. </div>
  193. )}
  194. {nodeInfo.outputs && (
  195. <div>
  196. <CodeEditor
  197. readOnly
  198. title={<div>{t('workflow.common.output').toLocaleUpperCase()}</div>}
  199. language={CodeLanguage.json}
  200. value={nodeInfo.outputs}
  201. isJSONStringifyBeauty
  202. />
  203. </div>
  204. )}
  205. </div>
  206. )}
  207. </div>
  208. </div>
  209. )
  210. }
  211. export default NodePanel