您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import { useTranslation } from 'react-i18next'
  2. import { AddDocumentsStep } from './types'
  3. import type { DataSourceOption, Datasource } from '@/app/components/rag-pipeline/components/panel/test-run/types'
  4. import { useCallback, useMemo, useRef, useState } from 'react'
  5. import { BlockEnum, type Node } from '@/app/components/workflow/types'
  6. import type { DataSourceNodeType } from '@/app/components/workflow/nodes/data-source/types'
  7. import type { DatasourceType } from '@/models/pipeline'
  8. import type { CrawlResult, CrawlResultItem, DocumentItem, FileItem } from '@/models/datasets'
  9. import { CrawlStep } from '@/models/datasets'
  10. import produce from 'immer'
  11. import type { NotionPage } from '@/models/common'
  12. export const useAddDocumentsSteps = () => {
  13. const { t } = useTranslation()
  14. const [currentStep, setCurrentStep] = useState(1)
  15. const handleNextStep = useCallback(() => {
  16. setCurrentStep(preStep => preStep + 1)
  17. }, [])
  18. const handleBackStep = useCallback(() => {
  19. setCurrentStep(preStep => preStep - 1)
  20. }, [])
  21. const steps = [
  22. {
  23. label: t('datasetPipeline.addDocuments.steps.chooseDatasource'),
  24. value: AddDocumentsStep.dataSource,
  25. },
  26. {
  27. label: t('datasetPipeline.addDocuments.steps.processDocuments'),
  28. value: AddDocumentsStep.processDocuments,
  29. },
  30. {
  31. label: t('datasetPipeline.addDocuments.steps.processingDocuments'),
  32. value: AddDocumentsStep.processingDocuments,
  33. },
  34. ]
  35. return {
  36. steps,
  37. currentStep,
  38. handleNextStep,
  39. handleBackStep,
  40. }
  41. }
  42. export const useDatasourceOptions = (pipelineNodes: Node<DataSourceNodeType>[]) => {
  43. const datasourceNodes = pipelineNodes.filter(node => node.data.type === BlockEnum.DataSource)
  44. const datasources: Datasource[] = useMemo(() => {
  45. return datasourceNodes.map((node) => {
  46. return {
  47. nodeId: node.id,
  48. type: node.data.provider_type as DatasourceType,
  49. description: node.data.datasource_label,
  50. docTitle: 'How to use?',
  51. docLink: '',
  52. fileExtensions: node.data.fileExtensions || [],
  53. }
  54. })
  55. }, [datasourceNodes])
  56. const options = useMemo(() => {
  57. const options: DataSourceOption[] = []
  58. datasourceNodes.forEach((node) => {
  59. const label = node.data.title
  60. options.push({
  61. label,
  62. value: node.id,
  63. data: node.data,
  64. })
  65. })
  66. return options
  67. }, [datasourceNodes])
  68. return { datasources, options }
  69. }
  70. export const useLocalFile = () => {
  71. const [fileList, setFileList] = useState<FileItem[]>([])
  72. const [currentFile, setCurrentFile] = useState<File | undefined>()
  73. const previewFile = useRef<DocumentItem>()
  74. const allFileLoaded = useMemo(() => (fileList.length > 0 && fileList.every(file => file.file.id)), [fileList])
  75. const updateFile = (fileItem: FileItem, progress: number, list: FileItem[]) => {
  76. const newList = produce(list, (draft) => {
  77. const targetIndex = draft.findIndex(file => file.fileID === fileItem.fileID)
  78. draft[targetIndex] = {
  79. ...draft[targetIndex],
  80. progress,
  81. }
  82. })
  83. setFileList(newList)
  84. previewFile.current = newList[0].file as DocumentItem
  85. }
  86. const updateFileList = useCallback((preparedFiles: FileItem[]) => {
  87. setFileList(preparedFiles)
  88. }, [])
  89. const updateCurrentFile = useCallback((file: File) => {
  90. setCurrentFile(file)
  91. }, [])
  92. const hideFilePreview = useCallback(() => {
  93. setCurrentFile(undefined)
  94. }, [])
  95. return {
  96. fileList,
  97. previewFile,
  98. allFileLoaded,
  99. updateFile,
  100. updateFileList,
  101. currentFile,
  102. updateCurrentFile,
  103. hideFilePreview,
  104. }
  105. }
  106. export const useOnlineDocuments = () => {
  107. const [onlineDocuments, setOnlineDocuments] = useState<NotionPage[]>([])
  108. const [currentDocuments, setCurrentDocuments] = useState<NotionPage | undefined>()
  109. const previewOnlineDocument = useRef<NotionPage>(onlineDocuments[0])
  110. const updateOnlineDocuments = (value: NotionPage[]) => {
  111. setOnlineDocuments(value)
  112. }
  113. const updateCurrentPage = useCallback((page: NotionPage) => {
  114. setCurrentDocuments(page)
  115. }, [])
  116. const hideOnlineDocumentPreview = useCallback(() => {
  117. setCurrentDocuments(undefined)
  118. }, [])
  119. return {
  120. onlineDocuments,
  121. previewOnlineDocument,
  122. updateOnlineDocuments,
  123. currentDocuments,
  124. updateCurrentPage,
  125. hideOnlineDocumentPreview,
  126. }
  127. }
  128. export const useWebsiteCrawl = () => {
  129. const [websitePages, setWebsitePages] = useState<CrawlResultItem[]>([])
  130. const [currentWebsite, setCurrentWebsite] = useState<CrawlResultItem | undefined>()
  131. const [crawlResult, setCrawlResult] = useState<CrawlResult | undefined>()
  132. const [step, setStep] = useState<CrawlStep>(CrawlStep.init)
  133. const [previewIndex, setPreviewIndex] = useState<number>(-1)
  134. const previewWebsitePage = useRef<CrawlResultItem>(websitePages[0])
  135. const updateCurrentWebsite = useCallback((website: CrawlResultItem, index: number) => {
  136. setCurrentWebsite(website)
  137. setPreviewIndex(index)
  138. }, [])
  139. const hideWebsitePreview = useCallback(() => {
  140. setCurrentWebsite(undefined)
  141. setPreviewIndex(-1)
  142. }, [])
  143. const updataCheckedCrawlResultChange = useCallback((checkedCrawlResult: CrawlResultItem[]) => {
  144. setWebsitePages(checkedCrawlResult)
  145. previewWebsitePage.current = checkedCrawlResult[0]
  146. }, [])
  147. return {
  148. websitePages,
  149. crawlResult,
  150. setCrawlResult,
  151. step,
  152. setStep,
  153. previewWebsitePage,
  154. updataCheckedCrawlResultChange,
  155. currentWebsite,
  156. updateCurrentWebsite,
  157. previewIndex,
  158. hideWebsitePreview,
  159. }
  160. }