node.tsx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. 'use client'
  2. import { useTranslation } from 'react-i18next'
  3. import type { FC } from 'react'
  4. import { useCallback, useEffect, 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 Split from '../nodes/_base/components/split'
  15. import { Iteration } from '@/app/components/base/icons/src/vender/workflow'
  16. import cn from '@/utils/classnames'
  17. import StatusContainer from '@/app/components/workflow/run/status-container'
  18. import CodeEditor from '@/app/components/workflow/nodes/_base/components/editor/code-editor'
  19. import Button from '@/app/components/base/button'
  20. import { CodeLanguage } from '@/app/components/workflow/nodes/code/types'
  21. import type { NodeTracing } from '@/types/workflow'
  22. type Props = {
  23. className?: string
  24. nodeInfo: NodeTracing
  25. inMessage?: boolean
  26. hideInfo?: boolean
  27. hideProcessDetail?: boolean
  28. onShowIterationDetail?: (detail: NodeTracing[][]) => void
  29. notShowIterationNav?: boolean
  30. justShowIterationNavArrow?: boolean
  31. }
  32. const NodePanel: FC<Props> = ({
  33. className,
  34. nodeInfo,
  35. inMessage = false,
  36. hideInfo = false,
  37. hideProcessDetail,
  38. onShowIterationDetail,
  39. notShowIterationNav,
  40. justShowIterationNavArrow,
  41. }) => {
  42. const [collapseState, doSetCollapseState] = useState<boolean>(true)
  43. const setCollapseState = useCallback((state: boolean) => {
  44. if (hideProcessDetail)
  45. return
  46. doSetCollapseState(state)
  47. }, [hideProcessDetail])
  48. const { t } = useTranslation()
  49. const getTime = (time: number) => {
  50. if (time < 1)
  51. return `${(time * 1000).toFixed(3)} ms`
  52. if (time > 60)
  53. return `${parseInt(Math.round(time / 60).toString())} m ${(time % 60).toFixed(3)} s`
  54. return `${time.toFixed(3)} s`
  55. }
  56. const getTokenCount = (tokens: number) => {
  57. if (tokens < 1000)
  58. return tokens
  59. if (tokens >= 1000 && tokens < 1000000)
  60. return `${parseFloat((tokens / 1000).toFixed(3))}K`
  61. if (tokens >= 1000000)
  62. return `${parseFloat((tokens / 1000000).toFixed(3))}M`
  63. }
  64. const getCount = (iteration_curr_length: number | undefined, iteration_length: number) => {
  65. if ((iteration_curr_length && iteration_curr_length < iteration_length) || !iteration_length)
  66. return iteration_curr_length
  67. return iteration_length
  68. }
  69. useEffect(() => {
  70. setCollapseState(!nodeInfo.expand)
  71. }, [nodeInfo.expand, setCollapseState])
  72. const isIterationNode = nodeInfo.node_type === BlockEnum.Iteration
  73. const handleOnShowIterationDetail = (e: React.MouseEvent<HTMLButtonElement>) => {
  74. e.stopPropagation()
  75. e.nativeEvent.stopImmediatePropagation()
  76. onShowIterationDetail?.(nodeInfo.details || [])
  77. }
  78. return (
  79. <div className={cn('px-2 py-1', className)}>
  80. <div className='group transition-all bg-background-default border border-components-panel-border rounded-[10px] shadow-xs hover:shadow-md'>
  81. <div
  82. className={cn(
  83. 'flex items-center pl-1 pr-3 cursor-pointer',
  84. hideInfo ? 'py-2' : 'py-1.5',
  85. !collapseState && (hideInfo ? '!pb-1' : '!pb-1.5'),
  86. )}
  87. onClick={() => setCollapseState(!collapseState)}
  88. >
  89. {!hideProcessDetail && (
  90. <RiArrowRightSLine
  91. className={cn(
  92. 'shrink-0 w-4 h-4 mr-1 text-text-quaternary transition-all group-hover:text-text-tertiary',
  93. !collapseState && 'rotate-90',
  94. )}
  95. />
  96. )}
  97. <BlockIcon size={inMessage ? 'xs' : 'sm'} className={cn('shrink-0 mr-2', inMessage && '!mr-1')} type={nodeInfo.node_type} toolIcon={nodeInfo.extras?.icon || nodeInfo.extras} />
  98. <div className={cn(
  99. 'grow text-text-secondary system-xs-semibold-uppercase truncate',
  100. hideInfo && '!text-xs',
  101. )} title={nodeInfo.title}>{nodeInfo.title}</div>
  102. {nodeInfo.status !== 'running' && !hideInfo && (
  103. <div className='shrink-0 text-text-tertiary system-xs-regular'>{nodeInfo.execution_metadata?.total_tokens ? `${getTokenCount(nodeInfo.execution_metadata?.total_tokens || 0)} tokens · ` : ''}{`${getTime(nodeInfo.elapsed_time || 0)}`}</div>
  104. )}
  105. {nodeInfo.status === 'succeeded' && (
  106. <RiCheckboxCircleFill className='shrink-0 ml-2 w-3.5 h-3.5 text-text-success' />
  107. )}
  108. {nodeInfo.status === 'failed' && (
  109. <RiErrorWarningLine className='shrink-0 ml-2 w-3.5 h-3.5 text-text-warning' />
  110. )}
  111. {nodeInfo.status === 'stopped' && (
  112. <RiAlertFill className={cn('shrink-0 ml-2 w-4 h-4 text-text-warning-secondary', inMessage && 'w-3.5 h-3.5')} />
  113. )}
  114. {nodeInfo.status === 'running' && (
  115. <div className='shrink-0 flex items-center text-text-accent text-[13px] leading-[16px] font-medium'>
  116. <span className='mr-2 text-xs font-normal'>Running</span>
  117. <RiLoader2Line className='w-3.5 h-3.5 animate-spin' />
  118. </div>
  119. )}
  120. </div>
  121. {!collapseState && !hideProcessDetail && (
  122. <div className='px-1 pb-1'>
  123. {/* The nav to the iteration detail */}
  124. {isIterationNode && !notShowIterationNav && (
  125. <div className='mt-2 mb-1 !px-2'>
  126. <Button
  127. className='flex items-center w-full self-stretch gap-2 px-3 py-2 bg-components-button-tertiary-bg-hover hover:bg-components-button-tertiary-bg-hover rounded-lg cursor-pointer border-none'
  128. onClick={handleOnShowIterationDetail}
  129. >
  130. <Iteration className='w-4 h-4 text-components-button-tertiary-text flex-shrink-0' />
  131. <div className='flex-1 text-left system-sm-medium text-components-button-tertiary-text'>{t('workflow.nodes.iteration.iteration', { count: getCount(nodeInfo.details?.length, nodeInfo.metadata?.iterator_length) })}</div>
  132. {justShowIterationNavArrow
  133. ? (
  134. <RiArrowRightSLine className='w-4 h-4 text-components-button-tertiary-text flex-shrink-0' />
  135. )
  136. : (
  137. <div className='flex items-center space-x-1 text-[#155EEF]'>
  138. <div className='text-[13px] font-normal '>{t('workflow.common.viewDetailInTracingPanel')}</div>
  139. <RiArrowRightSLine className='w-4 h-4 text-components-button-tertiary-text flex-shrink-0' />
  140. </div>
  141. )}
  142. </Button>
  143. <Split className='mt-2' />
  144. </div>
  145. )}
  146. <div className={cn('px-[10px]', hideInfo && '!px-2 !py-0.5')}>
  147. {nodeInfo.status === 'stopped' && (
  148. <StatusContainer status='stopped'>
  149. {t('workflow.tracing.stopBy', { user: nodeInfo.created_by ? nodeInfo.created_by.name : 'N/A' })}
  150. </StatusContainer>
  151. )}
  152. {nodeInfo.status === 'failed' && (
  153. <StatusContainer status='failed'>
  154. {nodeInfo.error}
  155. </StatusContainer>
  156. )}
  157. </div>
  158. {nodeInfo.inputs && (
  159. <div className={cn('mb-1')}>
  160. <CodeEditor
  161. readOnly
  162. title={<div>{t('workflow.common.input').toLocaleUpperCase()}</div>}
  163. language={CodeLanguage.json}
  164. value={nodeInfo.inputs}
  165. isJSONStringifyBeauty
  166. />
  167. </div>
  168. )}
  169. {nodeInfo.process_data && (
  170. <div className={cn('mb-1')}>
  171. <CodeEditor
  172. readOnly
  173. title={<div>{t('workflow.common.processData').toLocaleUpperCase()}</div>}
  174. language={CodeLanguage.json}
  175. value={nodeInfo.process_data}
  176. isJSONStringifyBeauty
  177. />
  178. </div>
  179. )}
  180. {nodeInfo.outputs && (
  181. <div>
  182. <CodeEditor
  183. readOnly
  184. title={<div>{t('workflow.common.output').toLocaleUpperCase()}</div>}
  185. language={CodeLanguage.json}
  186. value={nodeInfo.outputs}
  187. isJSONStringifyBeauty
  188. />
  189. </div>
  190. )}
  191. </div>
  192. )}
  193. </div>
  194. </div>
  195. )
  196. }
  197. export default NodePanel