node.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. 'use client'
  2. import { useTranslation } from 'react-i18next'
  3. import type { FC } from 'react'
  4. import { useCallback, useEffect, useState } from 'react'
  5. import cn from 'classnames'
  6. import BlockIcon from '../block-icon'
  7. import CodeEditor from '@/app/components/workflow/nodes/_base/components/editor/code-editor'
  8. import { CodeLanguage } from '@/app/components/workflow/nodes/code/types'
  9. import { AlertCircle, AlertTriangle } from '@/app/components/base/icons/src/vender/line/alertsAndFeedback'
  10. import { CheckCircle, Loading02 } from '@/app/components/base/icons/src/vender/line/general'
  11. import { ChevronRight } from '@/app/components/base/icons/src/vender/line/arrows'
  12. import type { NodeTracing } from '@/types/workflow'
  13. type Props = {
  14. nodeInfo: NodeTracing
  15. hideInfo?: boolean
  16. hideProcessDetail?: boolean
  17. }
  18. const NodePanel: FC<Props> = ({
  19. nodeInfo,
  20. hideInfo = false,
  21. hideProcessDetail,
  22. }) => {
  23. const [collapseState, doSetCollapseState] = useState<boolean>(true)
  24. const setCollapseState = useCallback((state: boolean) => {
  25. if (hideProcessDetail)
  26. return
  27. doSetCollapseState(state)
  28. }, [hideProcessDetail])
  29. const { t } = useTranslation()
  30. const getTime = (time: number) => {
  31. if (time < 1)
  32. return `${(time * 1000).toFixed(3)} ms`
  33. if (time > 60)
  34. return `${parseInt(Math.round(time / 60).toString())} m ${(time % 60).toFixed(3)} s`
  35. return `${time.toFixed(3)} s`
  36. }
  37. const getTokenCount = (tokens: number) => {
  38. if (tokens < 1000)
  39. return tokens
  40. if (tokens >= 1000 && tokens < 1000000)
  41. return `${parseFloat((tokens / 1000).toFixed(3))}K`
  42. if (tokens >= 1000000)
  43. return `${parseFloat((tokens / 1000000).toFixed(3))}M`
  44. }
  45. useEffect(() => {
  46. setCollapseState(!nodeInfo.expand)
  47. }, [nodeInfo.expand, setCollapseState])
  48. return (
  49. <div className={cn('px-4 py-1', hideInfo && '!p-0')}>
  50. <div className={cn('group transition-all bg-white border border-gray-100 rounded-2xl shadow-xs hover:shadow-md', hideInfo && '!rounded-lg')}>
  51. <div
  52. className={cn(
  53. 'flex items-center pl-[6px] pr-3 cursor-pointer',
  54. hideInfo ? 'py-2' : 'py-3',
  55. !collapseState && (hideInfo ? '!pb-1' : '!pb-2'),
  56. )}
  57. onClick={() => setCollapseState(!collapseState)}
  58. >
  59. {!hideProcessDetail && (
  60. <ChevronRight
  61. className={cn(
  62. 'shrink-0 w-3 h-3 mr-1 text-gray-400 transition-all group-hover:text-gray-500',
  63. !collapseState && 'rotate-90',
  64. )}
  65. />
  66. )}
  67. <BlockIcon size={hideInfo ? 'xs' : 'sm'} className={cn('shrink-0 mr-2', hideInfo && '!mr-1')} type={nodeInfo.node_type} toolIcon={nodeInfo.extras?.icon || nodeInfo.extras} />
  68. <div className={cn(
  69. 'grow text-gray-700 text-[13px] leading-[16px] font-semibold truncate',
  70. hideInfo && '!text-xs',
  71. )} title={nodeInfo.title}>{nodeInfo.title}</div>
  72. {nodeInfo.status !== 'running' && !hideInfo && (
  73. <div className='shrink-0 text-gray-500 text-xs leading-[18px]'>{`${getTime(nodeInfo.elapsed_time || 0)} · ${getTokenCount(nodeInfo.execution_metadata?.total_tokens || 0)} tokens`}</div>
  74. )}
  75. {nodeInfo.status === 'succeeded' && (
  76. <CheckCircle className='shrink-0 ml-2 w-3.5 h-3.5 text-[#12B76A]' />
  77. )}
  78. {nodeInfo.status === 'failed' && (
  79. <AlertCircle className='shrink-0 ml-2 w-3.5 h-3.5 text-[#F04438]' />
  80. )}
  81. {nodeInfo.status === 'stopped' && (
  82. <AlertTriangle className='shrink-0 ml-2 w-3.5 h-3.5 text-[#F79009]' />
  83. )}
  84. {nodeInfo.status === 'running' && (
  85. <div className='shrink-0 flex items-center text-primary-600 text-[13px] leading-[16px] font-medium'>
  86. <span className='mr-2 text-xs font-normal'>Running</span>
  87. <Loading02 className='w-3.5 h-3.5 animate-spin' />
  88. </div>
  89. )}
  90. </div>
  91. {!collapseState && !hideProcessDetail && (
  92. <div className='pb-2'>
  93. <div className={cn('px-[10px] py-1', hideInfo && '!px-2 !py-0.5')}>
  94. {nodeInfo.status === 'stopped' && (
  95. <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>
  96. )}
  97. {nodeInfo.status === 'failed' && (
  98. <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>
  99. )}
  100. </div>
  101. {nodeInfo.inputs && (
  102. <div className={cn('px-[10px] py-1', hideInfo && '!px-2 !py-0.5')}>
  103. <CodeEditor
  104. readOnly
  105. title={<div>{t('workflow.common.input').toLocaleUpperCase()}</div>}
  106. language={CodeLanguage.json}
  107. value={nodeInfo.inputs}
  108. isJSONStringifyBeauty
  109. />
  110. </div>
  111. )}
  112. {nodeInfo.process_data && (
  113. <div className={cn('px-[10px] py-1', hideInfo && '!px-2 !py-0.5')}>
  114. <CodeEditor
  115. readOnly
  116. title={<div>{t('workflow.common.processData').toLocaleUpperCase()}</div>}
  117. language={CodeLanguage.json}
  118. value={nodeInfo.process_data}
  119. isJSONStringifyBeauty
  120. />
  121. </div>
  122. )}
  123. {nodeInfo.outputs && (
  124. <div className={cn('px-[10px] py-1', hideInfo && '!px-2 !py-0.5')}>
  125. <CodeEditor
  126. readOnly
  127. title={<div>{t('workflow.common.output').toLocaleUpperCase()}</div>}
  128. language={CodeLanguage.json}
  129. value={nodeInfo.outputs}
  130. isJSONStringifyBeauty
  131. />
  132. </div>
  133. )}
  134. </div>
  135. )}
  136. </div>
  137. </div>
  138. )
  139. }
  140. export default NodePanel