| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- 'use client'
- import { useCallback, useMemo, useRef, useState } from 'react'
- import DataSourceOptions from './data-source-options'
- import type { CrawlResultItem, DocumentItem, CustomFile as File, FileIndexingEstimateResponse } from '@/models/datasets'
- import LocalFile from '@/app/components/rag-pipeline/components/panel/test-run/data-source/local-file'
- import { useProviderContextSelector } from '@/context/provider-context'
- import type { NotionPage } from '@/models/common'
- import OnlineDocuments from '@/app/components/rag-pipeline/components/panel/test-run/data-source/online-documents'
- import VectorSpaceFull from '@/app/components/billing/vector-space-full'
- import WebsiteCrawl from '@/app/components/rag-pipeline/components/panel/test-run/data-source/website-crawl'
- import Actions from './data-source/actions'
- import { useTranslation } from 'react-i18next'
- import type { Datasource } from '@/app/components/rag-pipeline/components/panel/test-run/types'
- import LeftHeader from './left-header'
- import { usePublishedPipelineInfo, useRunPublishedPipeline } from '@/service/use-pipeline'
- import { useDatasetDetailContextWithSelector } from '@/context/dataset-detail'
- import Loading from '@/app/components/base/loading'
- import type { Node } from '@/app/components/workflow/types'
- import type { DataSourceNodeType } from '@/app/components/workflow/nodes/data-source/types'
- import FilePreview from './preview/file-preview'
- import NotionPagePreview from './preview/notion-page-preview'
- import WebsitePreview from './preview/web-preview'
- import ProcessDocuments from './process-documents'
- import ChunkPreview from './preview/chunk-preview'
- import Processing from './processing'
- import type { InitialDocumentDetail, PublishedPipelineRunPreviewResponse, PublishedPipelineRunResponse } from '@/models/pipeline'
- import { DatasourceType } from '@/models/pipeline'
- import { TransferMethod } from '@/types/app'
- import { useAddDocumentsSteps, useLocalFile, useOnlineDocuments, useWebsiteCrawl } from './hooks'
-
- const CreateFormPipeline = () => {
- const { t } = useTranslation()
- const plan = useProviderContextSelector(state => state.plan)
- const enableBilling = useProviderContextSelector(state => state.enableBilling)
- const pipelineId = useDatasetDetailContextWithSelector(s => s.dataset?.pipeline_id)
- const [datasource, setDatasource] = useState<Datasource>()
- const [estimateData, setEstimateData] = useState<FileIndexingEstimateResponse | undefined>(undefined)
- const [batchId, setBatchId] = useState('')
- const [documents, setDocuments] = useState<InitialDocumentDetail[]>([])
-
- const isPreview = useRef(false)
- const formRef = useRef<any>(null)
-
- const { data: pipelineInfo, isFetching: isFetchingPipelineInfo } = usePublishedPipelineInfo(pipelineId || '')
-
- const {
- steps,
- currentStep,
- handleNextStep,
- handleBackStep,
- } = useAddDocumentsSteps()
- const {
- fileList,
- previewFile,
- allFileLoaded,
- updateFile,
- updateFileList,
- currentFile,
- updateCurrentFile,
- hideFilePreview,
- } = useLocalFile()
- const {
- onlineDocuments,
- previewOnlineDocument,
- updateOnlineDocuments,
- currentDocuments,
- updateCurrentPage,
- hideOnlineDocumentPreview,
- } = useOnlineDocuments()
- const {
- websitePages,
- previewWebsitePage,
- updataCheckedCrawlResultChange,
- currentWebsite,
- updateCurrentWebsite,
- hideWebsitePreview,
- } = useWebsiteCrawl()
-
- const isVectorSpaceFull = plan.usage.vectorSpace >= plan.total.vectorSpace
- const isShowVectorSpaceFull = allFileLoaded && isVectorSpaceFull && enableBilling
- const notSupportBatchUpload = enableBilling && plan.type === 'sandbox'
-
- const nextBtnDisabled = useMemo(() => {
- if (!datasource) return true
- if (datasource.type === DatasourceType.localFile)
- return isShowVectorSpaceFull || !fileList.length || fileList.some(file => !file.file.id)
- if (datasource.type === DatasourceType.onlineDocument)
- return isShowVectorSpaceFull || !onlineDocuments.length
- if (datasource.type === DatasourceType.websiteCrawl)
- return isShowVectorSpaceFull || !websitePages.length
- return false
- }, [datasource, isShowVectorSpaceFull, fileList, onlineDocuments.length, websitePages.length])
-
- const { mutateAsync: runPublishedPipeline, isIdle, isPending } = useRunPublishedPipeline()
-
- const handlePreviewChunks = useCallback(async (data: Record<string, any>) => {
- if (!datasource)
- return
- const datasourceInfoList: Record<string, any>[] = []
- if (datasource.type === DatasourceType.localFile) {
- const { id, name, type, size, extension, mime_type } = previewFile.current as File
- const documentInfo = {
- related_id: id,
- name,
- type,
- size,
- extension,
- mime_type,
- url: '',
- transfer_method: TransferMethod.local_file,
- }
- datasourceInfoList.push(documentInfo)
- }
- if (datasource.type === DatasourceType.onlineDocument) {
- const { workspace_id, ...rest } = previewOnlineDocument.current
- const documentInfo = {
- workspace_id,
- page: rest,
- }
- datasourceInfoList.push(documentInfo)
- }
- if (datasource.type === DatasourceType.websiteCrawl)
- datasourceInfoList.push(previewWebsitePage.current)
- await runPublishedPipeline({
- pipeline_id: pipelineId!,
- inputs: data,
- start_node_id: datasource.nodeId,
- datasource_type: datasource.type,
- datasource_info_list: datasourceInfoList,
- is_preview: true,
- }, {
- onSuccess: (res) => {
- setEstimateData((res as PublishedPipelineRunPreviewResponse).data.outputs)
- },
- })
- }, [datasource, pipelineId, previewFile, previewOnlineDocument, previewWebsitePage, runPublishedPipeline])
-
- const handleProcess = useCallback(async (data: Record<string, any>) => {
- if (!datasource)
- return
- const datasourceInfoList: Record<string, any>[] = []
- if (datasource.type === DatasourceType.localFile) {
- fileList.forEach((file) => {
- const { id, name, type, size, extension, mime_type } = file.file
- const documentInfo = {
- related_id: id,
- name,
- type,
- size,
- extension,
- mime_type,
- url: '',
- transfer_method: TransferMethod.local_file,
- }
- datasourceInfoList.push(documentInfo)
- })
- }
- if (datasource.type === DatasourceType.onlineDocument) {
- onlineDocuments.forEach((page) => {
- const { workspace_id, ...rest } = page
- const documentInfo = {
- workspace_id,
- page: rest,
- }
- datasourceInfoList.push(documentInfo)
- })
- }
- if (datasource.type === DatasourceType.websiteCrawl) {
- websitePages.forEach((websitePage) => {
- datasourceInfoList.push(websitePage)
- })
- }
- await runPublishedPipeline({
- pipeline_id: pipelineId!,
- inputs: data,
- start_node_id: datasource.nodeId,
- datasource_type: datasource.type,
- datasource_info_list: datasourceInfoList,
- is_preview: false,
- }, {
- onSuccess: (res) => {
- setBatchId((res as PublishedPipelineRunResponse).batch || '')
- setDocuments((res as PublishedPipelineRunResponse).documents || [])
- handleNextStep()
- },
- })
- }, [datasource, fileList, handleNextStep, onlineDocuments, pipelineId, runPublishedPipeline, websitePages])
-
- const onClickProcess = useCallback(() => {
- isPreview.current = false
- formRef.current?.submit()
- }, [])
-
- const onClickPreview = useCallback(() => {
- isPreview.current = true
- formRef.current?.submit()
- }, [])
-
- const handleSubmit = useCallback((data: Record<string, any>) => {
- isPreview.current ? handlePreviewChunks(data) : handleProcess(data)
- }, [handlePreviewChunks, handleProcess])
-
- const handlePreviewFileChange = useCallback((file: DocumentItem) => {
- previewFile.current = file
- onClickPreview()
- }, [onClickPreview, previewFile])
-
- const handlePreviewOnlineDocumentChange = useCallback((page: NotionPage) => {
- previewOnlineDocument.current = page
- onClickPreview()
- }, [onClickPreview, previewOnlineDocument])
-
- const handlePreviewWebsiteChange = useCallback((website: CrawlResultItem) => {
- previewWebsitePage.current = website
- onClickPreview()
- }, [onClickPreview, previewWebsitePage])
-
- if (isFetchingPipelineInfo) {
- return (
- <Loading type='app' />
- )
- }
-
- return (
- <div
- className='relative flex h-[calc(100vh-56px)] overflow-x-auto rounded-t-2xl border-t border-effects-highlight bg-background-default-subtle'
- >
- <div className='flex h-full min-w-[760px] flex-1 flex-col px-14'>
- <LeftHeader
- steps={steps}
- title={t('datasetPipeline.addDocuments.title')}
- currentStep={currentStep}
- />
- <div className='grow overflow-y-auto'>
- {
- currentStep === 1 && (
- <div className='flex flex-col gap-y-5 pt-4'>
- <DataSourceOptions
- datasourceNodeId={datasource?.nodeId || ''}
- onSelect={setDatasource}
- pipelineNodes={(pipelineInfo?.graph.nodes || []) as Node<DataSourceNodeType>[]}
- />
- {datasource?.type === DatasourceType.localFile && (
- <LocalFile
- files={fileList}
- allowedExtensions={datasource?.fileExtensions || []}
- updateFile={updateFile}
- updateFileList={updateFileList}
- onPreview={updateCurrentFile}
- notSupportBatchUpload={notSupportBatchUpload}
- />
- )}
- {datasource?.type === DatasourceType.onlineDocument && (
- <OnlineDocuments
- nodeId={datasource?.nodeId || ''}
- headerInfo={{
- title: datasource.description,
- docTitle: datasource.docTitle || '',
- docLink: datasource.docLink || '',
- }}
- onlineDocuments={onlineDocuments}
- updateOnlineDocuments={updateOnlineDocuments}
- canPreview
- onPreview={updateCurrentPage}
- />
- )}
- {datasource?.type === DatasourceType.websiteCrawl && (
- <WebsiteCrawl
- nodeId={datasource?.nodeId || ''}
- headerInfo={{
- title: datasource.description,
- docTitle: datasource.docTitle || '',
- docLink: datasource.docLink || '',
- }}
- checkedCrawlResult={websitePages}
- onCheckedCrawlResultChange={updataCheckedCrawlResultChange}
- onPreview={updateCurrentWebsite}
- />
- )}
- {isShowVectorSpaceFull && (
- <VectorSpaceFull />
- )}
- <Actions disabled={nextBtnDisabled} handleNextStep={handleNextStep} />
- </div>
- )
- }
- {
- currentStep === 2 && (
- <ProcessDocuments
- ref={formRef}
- dataSourceNodeId={datasource?.nodeId || ''}
- onProcess={onClickProcess}
- onPreview={onClickPreview}
- onSubmit={handleSubmit}
- onBack={handleBackStep}
- />
- )
- }
- {
- currentStep === 3 && (
- <Processing
- batchId={batchId}
- documents={documents}
- />
- )
- }
- </div>
- </div>
- {/* Preview */}
- {
- currentStep === 1 && (
- <div className='flex h-full w-[752px] shrink-0 pl-2 pt-2'>
- {currentFile && <FilePreview file={currentFile} hidePreview={hideFilePreview} />}
- {currentDocuments && <NotionPagePreview currentPage={currentDocuments} hidePreview={hideOnlineDocumentPreview} />}
- {currentWebsite && <WebsitePreview payload={currentWebsite} hidePreview={hideWebsitePreview} />}
- </div>
- )
- }
- {
- currentStep === 2 && (
- <div className='flex h-full w-[752px] shrink-0 pl-2 pt-2'>
- <ChunkPreview
- dataSourceType={datasource!.type}
- files={fileList.map(file => file.file)}
- onlineDocuments={onlineDocuments}
- websitePages={websitePages}
- isIdle={isIdle}
- isPending={isPending && isPreview.current}
- estimateData={estimateData}
- onPreview={onClickPreview}
- handlePreviewFileChange={handlePreviewFileChange}
- handlePreviewOnlineDocumentChange={handlePreviewOnlineDocumentChange}
- handlePreviewWebsitePageChange={handlePreviewWebsiteChange}
- />
- </div>
- )
- }
- </div>
- )
- }
-
- export default CreateFormPipeline
|