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

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