node.tsx 9.9 KB

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