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.

new-segment.tsx 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import { memo, useMemo, useRef, useState } from 'react'
  2. import type { FC } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import { useContext } from 'use-context-selector'
  5. import { useParams } from 'next/navigation'
  6. import { RiCloseLine, RiExpandDiagonalLine } from '@remixicon/react'
  7. import { useShallow } from 'zustand/react/shallow'
  8. import { useSegmentListContext } from './completed'
  9. import { SegmentIndexTag } from './completed/common/segment-index-tag'
  10. import ActionButtons from './completed/common/action-buttons'
  11. import Keywords from './completed/common/keywords'
  12. import ChunkContent from './completed/common/chunk-content'
  13. import AddAnother from './completed/common/add-another'
  14. import Dot from './completed/common/dot'
  15. import { useStore as useAppStore } from '@/app/components/app/store'
  16. import { ToastContext } from '@/app/components/base/toast'
  17. import { ChunkingMode, type SegmentUpdater } from '@/models/datasets'
  18. import classNames from '@/utils/classnames'
  19. import { formatNumber } from '@/utils/format'
  20. import Divider from '@/app/components/base/divider'
  21. import { useAddSegment } from '@/service/knowledge/use-segment'
  22. import { useDatasetDetailContextWithSelector } from '@/context/dataset-detail'
  23. import { IndexingType } from '../../create/step-two'
  24. type NewSegmentModalProps = {
  25. onCancel: () => void
  26. docForm: ChunkingMode
  27. onSave: () => void
  28. viewNewlyAddedChunk: () => void
  29. }
  30. const NewSegmentModal: FC<NewSegmentModalProps> = ({
  31. onCancel,
  32. docForm,
  33. onSave,
  34. viewNewlyAddedChunk,
  35. }) => {
  36. const { t } = useTranslation()
  37. const { notify } = useContext(ToastContext)
  38. const [question, setQuestion] = useState('')
  39. const [answer, setAnswer] = useState('')
  40. const { datasetId, documentId } = useParams<{ datasetId: string; documentId: string }>()
  41. const [keywords, setKeywords] = useState<string[]>([])
  42. const [loading, setLoading] = useState(false)
  43. const [addAnother, setAddAnother] = useState(true)
  44. const fullScreen = useSegmentListContext(s => s.fullScreen)
  45. const toggleFullScreen = useSegmentListContext(s => s.toggleFullScreen)
  46. const indexingTechnique = useDatasetDetailContextWithSelector(s => s.dataset?.indexing_technique)
  47. const { appSidebarExpand } = useAppStore(useShallow(state => ({
  48. appSidebarExpand: state.appSidebarExpand,
  49. })))
  50. const refreshTimer = useRef<any>(null)
  51. const CustomButton = <>
  52. <Divider type='vertical' className='mx-1 h-3 bg-divider-regular' />
  53. <button
  54. type='button'
  55. className='system-xs-semibold text-text-accent'
  56. onClick={() => {
  57. clearTimeout(refreshTimer.current)
  58. viewNewlyAddedChunk()
  59. }}>
  60. {t('common.operation.view')}
  61. </button>
  62. </>
  63. const isQAModel = useMemo(() => {
  64. return docForm === ChunkingMode.qa
  65. }, [docForm])
  66. const handleCancel = (actionType: 'esc' | 'add' = 'esc') => {
  67. if (actionType === 'esc' || !addAnother)
  68. onCancel()
  69. }
  70. const { mutateAsync: addSegment } = useAddSegment()
  71. const handleSave = async () => {
  72. const params: SegmentUpdater = { content: '' }
  73. if (isQAModel) {
  74. if (!question.trim()) {
  75. return notify({
  76. type: 'error',
  77. message: t('datasetDocuments.segment.questionEmpty'),
  78. })
  79. }
  80. if (!answer.trim()) {
  81. return notify({
  82. type: 'error',
  83. message: t('datasetDocuments.segment.answerEmpty'),
  84. })
  85. }
  86. params.content = question
  87. params.answer = answer
  88. }
  89. else {
  90. if (!question.trim()) {
  91. return notify({
  92. type: 'error',
  93. message: t('datasetDocuments.segment.contentEmpty'),
  94. })
  95. }
  96. params.content = question
  97. }
  98. if (keywords?.length)
  99. params.keywords = keywords
  100. setLoading(true)
  101. await addSegment({ datasetId, documentId, body: params }, {
  102. onSuccess() {
  103. notify({
  104. type: 'success',
  105. message: t('datasetDocuments.segment.chunkAdded'),
  106. className: `!w-[296px] !bottom-0 ${appSidebarExpand === 'expand' ? '!left-[216px]' : '!left-14'}
  107. !top-auto !right-auto !mb-[52px] !ml-11`,
  108. customComponent: CustomButton,
  109. })
  110. handleCancel('add')
  111. setQuestion('')
  112. setAnswer('')
  113. setKeywords([])
  114. refreshTimer.current = setTimeout(() => {
  115. onSave()
  116. }, 3000)
  117. },
  118. onSettled() {
  119. setLoading(false)
  120. },
  121. })
  122. }
  123. const wordCountText = useMemo(() => {
  124. const count = isQAModel ? (question.length + answer.length) : question.length
  125. return `${formatNumber(count)} ${t('datasetDocuments.segment.characters', { count })}`
  126. // eslint-disable-next-line react-hooks/exhaustive-deps
  127. }, [question.length, answer.length, isQAModel])
  128. const isECOIndexing = indexingTechnique === IndexingType.ECONOMICAL
  129. return (
  130. <div className={'flex h-full flex-col'}>
  131. <div className={classNames('flex items-center justify-between', fullScreen ? 'py-3 pr-4 pl-6 border border-divider-subtle' : 'pt-3 pr-3 pl-4')}>
  132. <div className='flex flex-col'>
  133. <div className='system-xl-semibold text-text-primary'>{
  134. t('datasetDocuments.segment.addChunk')
  135. }</div>
  136. <div className='flex items-center gap-x-2'>
  137. <SegmentIndexTag label={t('datasetDocuments.segment.newChunk')!} />
  138. <Dot />
  139. <span className='system-xs-medium text-text-tertiary'>{wordCountText}</span>
  140. </div>
  141. </div>
  142. <div className='flex items-center'>
  143. {fullScreen && (
  144. <>
  145. <AddAnother className='mr-3' isChecked={addAnother} onCheck={() => setAddAnother(!addAnother)} />
  146. <ActionButtons
  147. handleCancel={handleCancel.bind(null, 'esc')}
  148. handleSave={handleSave}
  149. loading={loading}
  150. actionType='add'
  151. />
  152. <Divider type='vertical' className='ml-4 mr-2 h-3.5 bg-divider-regular' />
  153. </>
  154. )}
  155. <div className='mr-1 flex h-8 w-8 cursor-pointer items-center justify-center p-1.5' onClick={toggleFullScreen}>
  156. <RiExpandDiagonalLine className='h-4 w-4 text-text-tertiary' />
  157. </div>
  158. <div className='flex h-8 w-8 cursor-pointer items-center justify-center p-1.5' onClick={handleCancel.bind(null, 'esc')}>
  159. <RiCloseLine className='h-4 w-4 text-text-tertiary' />
  160. </div>
  161. </div>
  162. </div>
  163. <div className={classNames('flex grow', fullScreen ? 'w-full flex-row justify-center px-6 pt-6 gap-x-8' : 'flex-col gap-y-1 py-3 px-4')}>
  164. <div className={classNames('break-all overflow-hidden whitespace-pre-line', fullScreen ? 'w-1/2' : 'grow')}>
  165. <ChunkContent
  166. docForm={docForm}
  167. question={question}
  168. answer={answer}
  169. onQuestionChange={question => setQuestion(question)}
  170. onAnswerChange={answer => setAnswer(answer)}
  171. isEditMode={true}
  172. />
  173. </div>
  174. {isECOIndexing && <Keywords
  175. className={fullScreen ? 'w-1/5' : ''}
  176. actionType='add'
  177. keywords={keywords}
  178. isEditMode={true}
  179. onKeywordsChange={keywords => setKeywords(keywords)}
  180. />}
  181. </div>
  182. {!fullScreen && (
  183. <div className='flex items-center justify-between border-t-[1px] border-t-divider-subtle p-4 pt-3'>
  184. <AddAnother isChecked={addAnother} onCheck={() => setAddAnother(!addAnother)} />
  185. <ActionButtons
  186. handleCancel={handleCancel.bind(null, 'esc')}
  187. handleSave={handleSave}
  188. loading={loading}
  189. actionType='add'
  190. />
  191. </div>
  192. )}
  193. </div>
  194. )
  195. }
  196. export default memo(NewSegmentModal)