workflow-preview.tsx 9.1 KB

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