node.tsx 8.5 KB

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