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.

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