node.tsx 5.7 KB

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