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 10.0KB

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