workflow-preview.tsx 9.0 KB

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