You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

workflow-preview.tsx 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import {
  2. memo,
  3. useCallback,
  4. useEffect,
  5. useState,
  6. } from 'react'
  7. import {
  8. RiClipboardLine,
  9. RiCloseLine,
  10. } from '@remixicon/react'
  11. import { useTranslation } from 'react-i18next'
  12. import copy from 'copy-to-clipboard'
  13. import ResultText from '../run/result-text'
  14. import ResultPanel from '../run/result-panel'
  15. import TracingPanel from '../run/tracing-panel'
  16. import {
  17. useWorkflowInteractions,
  18. } from '../hooks'
  19. import { useStore } from '../store'
  20. import {
  21. WorkflowRunningStatus,
  22. } from '../types'
  23. import { formatWorkflowRunIdentifier } from '../utils'
  24. import Toast from '../../base/toast'
  25. import InputsPanel from './inputs-panel'
  26. import cn from '@/utils/classnames'
  27. import Loading from '@/app/components/base/loading'
  28. import Button from '@/app/components/base/button'
  29. const WorkflowPreview = () => {
  30. const { t } = useTranslation()
  31. const { handleCancelDebugAndPreviewPanel } = useWorkflowInteractions()
  32. const workflowRunningData = useStore(s => s.workflowRunningData)
  33. const showInputsPanel = useStore(s => s.showInputsPanel)
  34. const workflowCanvasWidth = useStore(s => s.workflowCanvasWidth)
  35. const panelWidth = useStore(s => s.previewPanelWidth)
  36. const setPreviewPanelWidth = useStore(s => s.setPreviewPanelWidth)
  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 && !workflowRunningData.result.files?.length)
  48. switchTab('DETAIL')
  49. }, [workflowRunningData])
  50. const [isResizing, setIsResizing] = useState(false)
  51. const startResizing = useCallback((e: React.MouseEvent) => {
  52. e.preventDefault()
  53. setIsResizing(true)
  54. }, [])
  55. const stopResizing = useCallback(() => {
  56. setIsResizing(false)
  57. }, [])
  58. const resize = useCallback((e: MouseEvent) => {
  59. if (isResizing) {
  60. const newWidth = window.innerWidth - e.clientX
  61. // width constraints: 400 <= width <= maxAllowed (canvas - reserved 400)
  62. const reservedCanvasWidth = 400
  63. const maxAllowed = workflowCanvasWidth ? (workflowCanvasWidth - reservedCanvasWidth) : 1024
  64. if (newWidth >= 400 && newWidth <= maxAllowed)
  65. setPreviewPanelWidth(newWidth)
  66. }
  67. }, [isResizing, workflowCanvasWidth, setPreviewPanelWidth])
  68. useEffect(() => {
  69. window.addEventListener('mousemove', resize)
  70. window.addEventListener('mouseup', stopResizing)
  71. return () => {
  72. window.removeEventListener('mousemove', resize)
  73. window.removeEventListener('mouseup', stopResizing)
  74. }
  75. }, [resize, stopResizing])
  76. return (
  77. <div className={
  78. 'relative flex h-full flex-col rounded-l-2xl border-[0.5px] border-components-panel-border bg-components-panel-bg shadow-xl'
  79. }
  80. style={{ width: `${panelWidth}px` }}
  81. >
  82. <div
  83. className="absolute bottom-0 left-[3px] top-1/2 z-50 h-6 w-[3px] cursor-col-resize rounded bg-gray-300"
  84. onMouseDown={startResizing}
  85. />
  86. <div className='flex items-center justify-between p-4 pb-1 text-base font-semibold text-text-primary'>
  87. {`Test Run${formatWorkflowRunIdentifier(workflowRunningData?.result.finished_at)}`}
  88. <div className='cursor-pointer p-1' onClick={() => handleCancelDebugAndPreviewPanel()}>
  89. <RiCloseLine className='h-4 w-4 text-text-tertiary' />
  90. </div>
  91. </div>
  92. <div className='relative flex grow flex-col'>
  93. <div className='flex shrink-0 items-center border-b-[0.5px] border-divider-subtle px-4'>
  94. {showInputsPanel && (
  95. <div
  96. className={cn(
  97. 'mr-6 cursor-pointer border-b-2 border-transparent py-3 text-[13px] font-semibold leading-[18px] text-text-tertiary',
  98. currentTab === 'INPUT' && '!border-[rgb(21,94,239)] text-text-secondary',
  99. )}
  100. onClick={() => switchTab('INPUT')}
  101. >{t('runLog.input')}</div>
  102. )}
  103. <div
  104. className={cn(
  105. 'mr-6 cursor-pointer border-b-2 border-transparent py-3 text-[13px] font-semibold leading-[18px] text-text-tertiary',
  106. currentTab === 'RESULT' && '!border-[rgb(21,94,239)] text-text-secondary',
  107. !workflowRunningData && '!cursor-not-allowed opacity-30',
  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 cursor-pointer border-b-2 border-transparent py-3 text-[13px] font-semibold leading-[18px] text-text-tertiary',
  118. currentTab === 'DETAIL' && '!border-[rgb(21,94,239)] text-text-secondary',
  119. !workflowRunningData && '!cursor-not-allowed opacity-30',
  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 cursor-pointer border-b-2 border-transparent py-3 text-[13px] font-semibold leading-[18px] text-text-tertiary',
  130. currentTab === 'TRACING' && '!border-[rgb(21,94,239)] text-text-secondary',
  131. !workflowRunningData && '!cursor-not-allowed opacity-30',
  132. )}
  133. onClick={() => {
  134. if (!workflowRunningData)
  135. return
  136. switchTab('TRACING')
  137. }}
  138. >{t('runLog.tracing')}</div>
  139. </div>
  140. <div className={cn(
  141. 'h-0 grow overflow-y-auto rounded-b-2xl bg-components-panel-bg',
  142. (currentTab === 'RESULT' || currentTab === 'TRACING') && '!bg-background-section-burn',
  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. allFiles={workflowRunningData?.result?.files}
  153. error={workflowRunningData?.result?.error}
  154. onClick={() => switchTab('DETAIL')}
  155. />
  156. {(workflowRunningData?.result.status === WorkflowRunningStatus.Succeeded && workflowRunningData?.resultText && typeof workflowRunningData?.resultText === 'string') && (
  157. <Button
  158. className={cn('mb-4 ml-4 space-x-1')}
  159. onClick={() => {
  160. const content = workflowRunningData?.resultText
  161. if (typeof content === 'string')
  162. copy(content)
  163. else
  164. copy(JSON.stringify(content))
  165. Toast.notify({ type: 'success', message: t('common.actionMsg.copySuccessfully') })
  166. }}>
  167. <RiClipboardLine className='h-3.5 w-3.5' />
  168. <div>{t('common.operation.copy')}</div>
  169. </Button>
  170. )}
  171. </>
  172. )}
  173. {currentTab === 'DETAIL' && (
  174. <ResultPanel
  175. inputs={workflowRunningData?.result?.inputs}
  176. outputs={workflowRunningData?.result?.outputs}
  177. status={workflowRunningData?.result?.status || ''}
  178. error={workflowRunningData?.result?.error}
  179. elapsed_time={workflowRunningData?.result?.elapsed_time}
  180. total_tokens={workflowRunningData?.result?.total_tokens}
  181. created_at={workflowRunningData?.result?.created_at}
  182. created_by={(workflowRunningData?.result?.created_by as any)?.name}
  183. steps={workflowRunningData?.result?.total_steps}
  184. exceptionCounts={workflowRunningData?.result?.exceptions_count}
  185. />
  186. )}
  187. {currentTab === 'DETAIL' && !workflowRunningData?.result && (
  188. <div className='flex h-full items-center justify-center bg-components-panel-bg'>
  189. <Loading />
  190. </div>
  191. )}
  192. {currentTab === 'TRACING' && (
  193. <TracingPanel
  194. className='bg-background-section-burn'
  195. list={workflowRunningData?.tracing || []}
  196. />
  197. )}
  198. {currentTab === 'TRACING' && !workflowRunningData?.tracing?.length && (
  199. <div className='flex h-full items-center justify-center !bg-background-section-burn'>
  200. <Loading />
  201. </div>
  202. )}
  203. </div>
  204. </div>
  205. </div>
  206. )
  207. }
  208. export default memo(WorkflowPreview)