You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

node.tsx 9.8KB

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