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

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