node.tsx 8.7 KB

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