workflow-preview.tsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import {
  2. memo,
  3. useEffect,
  4. // useRef,
  5. useState,
  6. } from 'react'
  7. import cn from 'classnames'
  8. import { useTranslation } from 'react-i18next'
  9. import copy from 'copy-to-clipboard'
  10. import ResultText from '../run/result-text'
  11. import ResultPanel from '../run/result-panel'
  12. import TracingPanel from '../run/tracing-panel'
  13. import {
  14. useWorkflowInteractions,
  15. } from '../hooks'
  16. import { useStore } from '../store'
  17. import {
  18. WorkflowRunningStatus,
  19. } from '../types'
  20. import { SimpleBtn } from '../../app/text-generate/item'
  21. import Toast from '../../base/toast'
  22. import InputsPanel from './inputs-panel'
  23. import Loading from '@/app/components/base/loading'
  24. import { XClose } from '@/app/components/base/icons/src/vender/line/general'
  25. import { Clipboard } from '@/app/components/base/icons/src/vender/line/files'
  26. const WorkflowPreview = () => {
  27. const { t } = useTranslation()
  28. const { handleCancelDebugAndPreviewPanel } = useWorkflowInteractions()
  29. const workflowRunningData = useStore(s => s.workflowRunningData)
  30. const showInputsPanel = useStore(s => s.showInputsPanel)
  31. const showDebugAndPreviewPanel = useStore(s => s.showDebugAndPreviewPanel)
  32. const [currentTab, setCurrentTab] = useState<string>(showInputsPanel ? 'INPUT' : 'TRACING')
  33. const switchTab = async (tab: string) => {
  34. setCurrentTab(tab)
  35. }
  36. useEffect(() => {
  37. if (showDebugAndPreviewPanel && showInputsPanel)
  38. setCurrentTab('INPUT')
  39. }, [showDebugAndPreviewPanel, showInputsPanel])
  40. useEffect(() => {
  41. if ((workflowRunningData?.result.status === WorkflowRunningStatus.Succeeded || workflowRunningData?.result.status === WorkflowRunningStatus.Failed) && !workflowRunningData.resultText)
  42. switchTab('DETAIL')
  43. }, [workflowRunningData])
  44. return (
  45. <div className={`
  46. flex flex-col w-[420px] h-full rounded-l-2xl border-[0.5px] border-gray-200 shadow-xl bg-white
  47. `}>
  48. <div className='flex items-center justify-between p-4 pb-1 text-base font-semibold text-gray-900'>
  49. {`Test Run${!workflowRunningData?.result.sequence_number ? '' : `#${workflowRunningData?.result.sequence_number}`}`}
  50. <div className='p-1 cursor-pointer' onClick={() => handleCancelDebugAndPreviewPanel()}>
  51. <XClose className='w-4 h-4 text-gray-500' />
  52. </div>
  53. </div>
  54. <div className='grow relative flex flex-col'>
  55. <div className='shrink-0 flex items-center px-4 border-b-[0.5px] border-[rgba(0,0,0,0.05)]'>
  56. {showInputsPanel && (
  57. <div
  58. className={cn(
  59. 'mr-6 py-3 border-b-2 border-transparent text-[13px] font-semibold leading-[18px] text-gray-400 cursor-pointer',
  60. currentTab === 'INPUT' && '!border-[rgb(21,94,239)] text-gray-700',
  61. )}
  62. onClick={() => switchTab('INPUT')}
  63. >{t('runLog.input')}</div>
  64. )}
  65. <div
  66. className={cn(
  67. 'mr-6 py-3 border-b-2 border-transparent text-[13px] font-semibold leading-[18px] text-gray-400 cursor-pointer',
  68. currentTab === 'RESULT' && '!border-[rgb(21,94,239)] text-gray-700',
  69. !workflowRunningData && 'opacity-30 !cursor-not-allowed',
  70. )}
  71. onClick={() => {
  72. if (!workflowRunningData)
  73. return
  74. switchTab('RESULT')
  75. }}
  76. >{t('runLog.result')}</div>
  77. <div
  78. className={cn(
  79. 'mr-6 py-3 border-b-2 border-transparent text-[13px] font-semibold leading-[18px] text-gray-400 cursor-pointer',
  80. currentTab === 'DETAIL' && '!border-[rgb(21,94,239)] text-gray-700',
  81. !workflowRunningData && 'opacity-30 !cursor-not-allowed',
  82. )}
  83. onClick={() => {
  84. if (!workflowRunningData)
  85. return
  86. switchTab('DETAIL')
  87. }}
  88. >{t('runLog.detail')}</div>
  89. <div
  90. className={cn(
  91. 'mr-6 py-3 border-b-2 border-transparent text-[13px] font-semibold leading-[18px] text-gray-400 cursor-pointer',
  92. currentTab === 'TRACING' && '!border-[rgb(21,94,239)] text-gray-700',
  93. !workflowRunningData && 'opacity-30 !cursor-not-allowed',
  94. )}
  95. onClick={() => {
  96. if (!workflowRunningData)
  97. return
  98. switchTab('TRACING')
  99. }}
  100. >{t('runLog.tracing')}</div>
  101. </div>
  102. <div className={cn(
  103. 'grow bg-white h-0 overflow-y-auto rounded-b-2xl',
  104. (currentTab === 'RESULT' || currentTab === 'TRACING') && '!bg-gray-50',
  105. )}>
  106. {currentTab === 'INPUT' && showInputsPanel && (
  107. <InputsPanel onRun={() => switchTab('RESULT')} />
  108. )}
  109. {currentTab === 'RESULT' && (
  110. <>
  111. <ResultText
  112. isRunning={workflowRunningData?.result?.status === WorkflowRunningStatus.Running || !workflowRunningData?.result}
  113. outputs={workflowRunningData?.resultText}
  114. error={workflowRunningData?.result?.error}
  115. onClick={() => switchTab('DETAIL')}
  116. />
  117. <SimpleBtn
  118. isDisabled={workflowRunningData?.result.status !== WorkflowRunningStatus.Succeeded}
  119. className={cn('ml-4 mb-4 inline-flex space-x-1')}
  120. onClick={() => {
  121. const content = workflowRunningData?.resultText
  122. if (typeof content === 'string')
  123. copy(content)
  124. else
  125. copy(JSON.stringify(content))
  126. Toast.notify({ type: 'success', message: t('common.actionMsg.copySuccessfully') })
  127. }}>
  128. <Clipboard className='w-3.5 h-3.5' />
  129. <div>{t('common.operation.copy')}</div>
  130. </SimpleBtn>
  131. </>
  132. )}
  133. {currentTab === 'DETAIL' && (
  134. <ResultPanel
  135. inputs={workflowRunningData?.result?.inputs}
  136. outputs={workflowRunningData?.result?.outputs}
  137. status={workflowRunningData?.result?.status || ''}
  138. error={workflowRunningData?.result?.error}
  139. elapsed_time={workflowRunningData?.result?.elapsed_time}
  140. total_tokens={workflowRunningData?.result?.total_tokens}
  141. created_at={workflowRunningData?.result?.created_at}
  142. created_by={(workflowRunningData?.result?.created_by as any)?.name}
  143. steps={workflowRunningData?.result?.total_steps}
  144. />
  145. )}
  146. {currentTab === 'DETAIL' && !workflowRunningData?.result && (
  147. <div className='flex h-full items-center justify-center bg-white'>
  148. <Loading />
  149. </div>
  150. )}
  151. {currentTab === 'TRACING' && (
  152. <TracingPanel
  153. list={workflowRunningData?.tracing || []}
  154. />
  155. )}
  156. {currentTab === 'TRACING' && !workflowRunningData?.tracing?.length && (
  157. <div className='flex h-full items-center justify-center bg-gray-50'>
  158. <Loading />
  159. </div>
  160. )}
  161. </div>
  162. </div>
  163. </div>
  164. )
  165. }
  166. export default memo(WorkflowPreview)