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

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