node.tsx 9.3 KB

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