node.tsx 11 KB

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