Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

index.tsx 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import {
  2. memo,
  3. useCallback,
  4. useEffect,
  5. useRef,
  6. useState,
  7. } from 'react'
  8. import { useKeyPress } from 'ahooks'
  9. import { RiCloseLine, RiEqualizer2Line } from '@remixicon/react'
  10. import { useTranslation } from 'react-i18next'
  11. import { useNodes } from 'reactflow'
  12. import {
  13. useEdgesInteractions,
  14. useNodesInteractions,
  15. useWorkflowInteractions,
  16. } from '../../hooks'
  17. import { BlockEnum } from '../../types'
  18. import type { StartNodeType } from '../../nodes/start/types'
  19. import ChatWrapper from './chat-wrapper'
  20. import cn from '@/utils/classnames'
  21. import { RefreshCcw01 } from '@/app/components/base/icons/src/vender/line/arrows'
  22. import { BubbleX } from '@/app/components/base/icons/src/vender/line/others'
  23. import Tooltip from '@/app/components/base/tooltip'
  24. import ActionButton, { ActionButtonState } from '@/app/components/base/action-button'
  25. import { useStore } from '@/app/components/workflow/store'
  26. export type ChatWrapperRefType = {
  27. handleRestart: () => void
  28. }
  29. const DebugAndPreview = () => {
  30. const { t } = useTranslation()
  31. const chatRef = useRef({ handleRestart: () => { } })
  32. const { handleCancelDebugAndPreviewPanel } = useWorkflowInteractions()
  33. const { handleNodeCancelRunningStatus } = useNodesInteractions()
  34. const { handleEdgeCancelRunningStatus } = useEdgesInteractions()
  35. const varList = useStore(s => s.conversationVariables)
  36. const [expanded, setExpanded] = useState(true)
  37. const nodes = useNodes<StartNodeType>()
  38. const startNode = nodes.find(node => node.data.type === BlockEnum.Start)
  39. const variables = startNode?.data.variables || []
  40. const [showConversationVariableModal, setShowConversationVariableModal] = useState(false)
  41. const handleRestartChat = () => {
  42. handleNodeCancelRunningStatus()
  43. handleEdgeCancelRunningStatus()
  44. chatRef.current.handleRestart()
  45. }
  46. useKeyPress('shift.r', () => {
  47. handleRestartChat()
  48. }, {
  49. exactMatch: true,
  50. })
  51. const [panelWidth, setPanelWidth] = useState(420)
  52. const [isResizing, setIsResizing] = useState(false)
  53. const startResizing = useCallback((e: React.MouseEvent) => {
  54. e.preventDefault()
  55. setIsResizing(true)
  56. }, [])
  57. const stopResizing = useCallback(() => {
  58. setIsResizing(false)
  59. }, [])
  60. const resize = useCallback((e: MouseEvent) => {
  61. if (isResizing) {
  62. const newWidth = window.innerWidth - e.clientX
  63. if (newWidth > 420 && newWidth < 1024)
  64. setPanelWidth(newWidth)
  65. }
  66. }, [isResizing])
  67. useEffect(() => {
  68. window.addEventListener('mousemove', resize)
  69. window.addEventListener('mouseup', stopResizing)
  70. return () => {
  71. window.removeEventListener('mousemove', resize)
  72. window.removeEventListener('mouseup', stopResizing)
  73. }
  74. }, [resize, stopResizing])
  75. return (
  76. <div
  77. className={cn(
  78. 'relative flex h-full flex-col rounded-l-2xl border border-r-0 border-components-panel-border bg-chatbot-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='system-xl-semibold flex shrink-0 items-center justify-between px-4 pb-2 pt-3 text-text-primary'>
  87. <div className='h-8'>{t('workflow.common.debugAndPreview').toLocaleUpperCase()}</div>
  88. <div className='flex items-center gap-1'>
  89. <Tooltip
  90. popupContent={t('common.operation.refresh')}
  91. >
  92. <ActionButton onClick={() => handleRestartChat()}>
  93. <RefreshCcw01 className='h-4 w-4' />
  94. </ActionButton>
  95. </Tooltip>
  96. {varList.length > 0 && (
  97. <Tooltip
  98. popupContent={t('workflow.chatVariable.panelTitle')}
  99. >
  100. <ActionButton onClick={() => setShowConversationVariableModal(true)}>
  101. <BubbleX className='h-4 w-4' />
  102. </ActionButton>
  103. </Tooltip>
  104. )}
  105. {variables.length > 0 && (
  106. <div className='relative'>
  107. <Tooltip
  108. popupContent={t('workflow.panel.userInputField')}
  109. >
  110. <ActionButton state={expanded ? ActionButtonState.Active : undefined} onClick={() => setExpanded(!expanded)}>
  111. <RiEqualizer2Line className='h-4 w-4' />
  112. </ActionButton>
  113. </Tooltip>
  114. {expanded && <div className='absolute bottom-[-17px] right-[5px] z-10 h-3 w-3 rotate-45 border-l-[0.5px] border-t-[0.5px] border-components-panel-border-subtle bg-components-panel-on-panel-item-bg'/>}
  115. </div>
  116. )}
  117. <div className='mx-3 h-3.5 w-[1px] bg-gray-200'></div>
  118. <div
  119. className='flex h-6 w-6 cursor-pointer items-center justify-center'
  120. onClick={handleCancelDebugAndPreviewPanel}
  121. >
  122. <RiCloseLine className='h-4 w-4 text-gray-500' />
  123. </div>
  124. </div>
  125. </div>
  126. <div className='grow overflow-y-auto rounded-b-2xl'>
  127. <ChatWrapper
  128. ref={chatRef}
  129. showConversationVariableModal={showConversationVariableModal}
  130. onConversationModalHide={() => setShowConversationVariableModal(false)}
  131. showInputsFieldsPanel={expanded}
  132. onHide={() => setExpanded(false)}
  133. />
  134. </div>
  135. </div>
  136. )
  137. }
  138. export default memo(DebugAndPreview)