import type { Fetcher } from 'swr' import qs from 'qs' import { del, get, patch, post, put } from './base' import type { CreateDocumentReq, DataSet, DataSetListResponse, DocumentDetailResponse, DocumentListResponse, FileIndexingEstimateResponse, HitTestingRecordsResponse, HitTestingResponse, IndexingEstimateResponse, IndexingStatusBatchResponse, IndexingStatusResponse, ProcessRuleResponse, RelatedAppResponse, SegmentDetailModel, SegmentUpdator, SegmentsQuery, SegmentsResponse, createDocumentResponse, } from '@/models/datasets' import type { CommonResponse, DataSourceNotionWorkspace } from '@/models/common' // apis for documents in a dataset type CommonDocReq = { datasetId: string documentId: string } type BatchReq = { datasetId: string batchId: string } export type SortType = 'created_at' | 'hit_count' | '-created_at' | '-hit_count' export type MetadataType = 'all' | 'only' | 'without' export const fetchDataDetail: Fetcher = (datasetId: string) => { return get(`/datasets/${datasetId}`) as Promise } export const updateDatasetSetting: Fetcher> }> = ({ datasetId, body }) => { return patch(`/datasets/${datasetId}`, { body }) as Promise } export const fetchDatasetRelatedApps: Fetcher = (datasetId: string) => { return get(`/datasets/${datasetId}/related-apps`) as Promise } export const fetchDatasets: Fetcher = ({ url, params }) => { const urlParams = qs.stringify(params, { indices: false }) return get(`${url}?${urlParams}`) as Promise } export const createEmptyDataset: Fetcher = ({ name }) => { return post('/datasets', { body: { name } }) as Promise } export const deleteDataset: Fetcher = (datasetID) => { return del(`/datasets/${datasetID}`) as Promise } export const fetchDefaultProcessRule: Fetcher = ({ url }) => { return get(url) as Promise } export const fetchProcessRule: Fetcher = ({ params: { documentId } }) => { return get('/datasets/process-rule', { params: { document_id: documentId } }) as Promise } export const fetchDocuments: Fetcher = ({ datasetId, params }) => { return get(`/datasets/${datasetId}/documents`, { params }) as Promise } export const createFirstDocument: Fetcher = ({ body }) => { return post('/datasets/init', { body }) as Promise } export const createDocument: Fetcher = ({ datasetId, body }) => { return post(`/datasets/${datasetId}/documents`, { body }) as Promise } export const fetchIndexingEstimate: Fetcher = ({ datasetId, documentId }) => { return get(`/datasets/${datasetId}/documents/${documentId}/indexing-estimate`, {}) as Promise } export const fetchIndexingEstimateBatch: Fetcher = ({ datasetId, batchId }) => { return get(`/datasets/${datasetId}/batch/${batchId}/indexing-estimate`, {}) as Promise } export const fetchIndexingStatus: Fetcher = ({ datasetId, documentId }) => { return get(`/datasets/${datasetId}/documents/${documentId}/indexing-status`, {}) as Promise } export const fetchIndexingStatusBatch: Fetcher = ({ datasetId, batchId }) => { return get(`/datasets/${datasetId}/batch/${batchId}/indexing-status`, {}) as Promise } export const fetchDocumentDetail: Fetcher = ({ datasetId, documentId, params }) => { return get(`/datasets/${datasetId}/documents/${documentId}`, { params }) as Promise } export const pauseDocIndexing: Fetcher = ({ datasetId, documentId }) => { return patch(`/datasets/${datasetId}/documents/${documentId}/processing/pause`) as Promise } export const resumeDocIndexing: Fetcher = ({ datasetId, documentId }) => { return patch(`/datasets/${datasetId}/documents/${documentId}/processing/resume`) as Promise } export const deleteDocument: Fetcher = ({ datasetId, documentId }) => { return del(`/datasets/${datasetId}/documents/${documentId}`) as Promise } export const archiveDocument: Fetcher = ({ datasetId, documentId }) => { return patch(`/datasets/${datasetId}/documents/${documentId}/status/archive`) as Promise } export const enableDocument: Fetcher = ({ datasetId, documentId }) => { return patch(`/datasets/${datasetId}/documents/${documentId}/status/enable`) as Promise } export const disableDocument: Fetcher = ({ datasetId, documentId }) => { return patch(`/datasets/${datasetId}/documents/${documentId}/status/disable`) as Promise } export const syncDocument: Fetcher = ({ datasetId, documentId }) => { return get(`/datasets/${datasetId}/documents/${documentId}/notion/sync`) as Promise } export const preImportNotionPages: Fetcher<{ notion_info: DataSourceNotionWorkspace[] }, { url: string; datasetId?: string }> = ({ url, datasetId }) => { return get(url, { params: { dataset_id: datasetId } }) as Promise<{ notion_info: DataSourceNotionWorkspace[] }> } export const modifyDocMetadata: Fetcher } }> = ({ datasetId, documentId, body }) => { return put(`/datasets/${datasetId}/documents/${documentId}/metadata`, { body }) as Promise } export const getDatasetIndexingStatus: Fetcher<{ data: IndexingStatusResponse[] }, string> = (datasetId) => { return get(`/datasets/${datasetId}/indexing-status`) as Promise<{ data: IndexingStatusResponse[] }> } // apis for segments in a document export const fetchSegments: Fetcher = ({ datasetId, documentId, params }) => { return get(`/datasets/${datasetId}/documents/${documentId}/segments`, { params }) as Promise } export const enableSegment: Fetcher = ({ datasetId, segmentId }) => { return patch(`/datasets/${datasetId}/segments/${segmentId}/enable`) as Promise } export const disableSegment: Fetcher = ({ datasetId, segmentId }) => { return patch(`/datasets/${datasetId}/segments/${segmentId}/disable`) as Promise } export const updateSegment: Fetcher<{ data: SegmentDetailModel; doc_form: string }, { datasetId: string; documentId: string; segmentId: string; body: SegmentUpdator }> = ({ datasetId, documentId, segmentId, body }) => { return patch(`/datasets/${datasetId}/documents/${documentId}/segments/${segmentId}`, { body }) as Promise<{ data: SegmentDetailModel; doc_form: string }> } export const addSegment: Fetcher<{ data: SegmentDetailModel; doc_form: string }, { datasetId: string; documentId: string; body: SegmentUpdator }> = ({ datasetId, documentId, body }) => { return post(`/datasets/${datasetId}/documents/${documentId}/segment`, { body }) as Promise<{ data: SegmentDetailModel; doc_form: string }> } // hit testing export const hitTesting: Fetcher = ({ datasetId, queryText }) => { return post(`/datasets/${datasetId}/hit-testing`, { body: { query: queryText } }) as Promise } export const fetchTestingRecords: Fetcher = ({ datasetId, params }) => { return get(`/datasets/${datasetId}/queries`, { params }) as Promise } export const fetchFileIndexingEstimate: Fetcher = (body: any) => { return post('/datasets/indexing-estimate', { body }) as Promise } export const fetchNotionPagePreview: Fetcher<{ content: string }, { workspaceID: string; pageID: string; pageType: string }> = ({ workspaceID, pageID, pageType }) => { return get(`notion/workspaces/${workspaceID}/pages/${pageID}/${pageType}/preview`) as Promise<{ content: string }> }