import type { Fetcher } from 'swr' import qs from 'qs' import { del, get, patch, post, put } from './base' import type { CreateDocumentReq, DataSet, DataSetListResponse, DocumentDetailResponse, DocumentListResponse, ErrorDocsResponse, FileIndexingEstimateResponse, HitTestingRecordsResponse, HitTestingResponse, IndexingEstimateParams, IndexingEstimateResponse, IndexingStatusBatchResponse, IndexingStatusResponse, ProcessRuleResponse, RelatedAppResponse, SegmentDetailModel, SegmentUpdator, SegmentsQuery, SegmentsResponse, createDocumentResponse, } from '@/models/datasets' import type { CommonResponse, DataSourceNotionWorkspace } from '@/models/common' import type { ApikeysListResponse, CreateApiKeyResponse, } from '@/models/app' import type { RetrievalConfig } from '@/types/app' // 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 fetchDatasetDetail: Fetcher = (datasetId: string) => { return get(`/datasets/${datasetId}`) } export const updateDatasetSetting: Fetcher> }> = ({ datasetId, body }) => { return patch(`/datasets/${datasetId}`, { body }) } export const fetchDatasetRelatedApps: Fetcher = (datasetId: string) => { return get(`/datasets/${datasetId}/related-apps`) } export const fetchDatasets: Fetcher = ({ url, params }) => { const urlParams = qs.stringify(params, { indices: false }) return get(`${url}?${urlParams}`) } export const createEmptyDataset: Fetcher = ({ name }) => { return post('/datasets', { body: { name } }) } export const deleteDataset: Fetcher = (datasetID) => { return del(`/datasets/${datasetID}`) } export const fetchDefaultProcessRule: Fetcher = ({ url }) => { return get(url) } export const fetchProcessRule: Fetcher = ({ params: { documentId } }) => { return get('/datasets/process-rule', { params: { document_id: documentId } }) } export const fetchDocuments: Fetcher = ({ datasetId, params }) => { return get(`/datasets/${datasetId}/documents`, { params }) } export const createFirstDocument: Fetcher = ({ body }) => { return post('/datasets/init', { body }) } export const createDocument: Fetcher = ({ datasetId, body }) => { return post(`/datasets/${datasetId}/documents`, { body }) } export const fetchIndexingEstimate: Fetcher = ({ datasetId, documentId }) => { return get(`/datasets/${datasetId}/documents/${documentId}/indexing-estimate`, {}) } export const fetchIndexingEstimateBatch: Fetcher = ({ datasetId, batchId }) => { return get(`/datasets/${datasetId}/batch/${batchId}/indexing-estimate`, {}) } export const fetchIndexingStatus: Fetcher = ({ datasetId, documentId }) => { return get(`/datasets/${datasetId}/documents/${documentId}/indexing-status`, {}) } export const fetchIndexingStatusBatch: Fetcher = ({ datasetId, batchId }) => { return get(`/datasets/${datasetId}/batch/${batchId}/indexing-status`, {}) } export const fetchDocumentDetail: Fetcher = ({ datasetId, documentId, params }) => { return get(`/datasets/${datasetId}/documents/${documentId}`, { params }) } export const renameDocumentName: Fetcher = ({ datasetId, documentId, name }) => { return post(`/datasets/${datasetId}/documents/${documentId}/rename`, { body: { name }, }) } export const pauseDocIndexing: Fetcher = ({ datasetId, documentId }) => { return patch(`/datasets/${datasetId}/documents/${documentId}/processing/pause`) } export const resumeDocIndexing: Fetcher = ({ datasetId, documentId }) => { return patch(`/datasets/${datasetId}/documents/${documentId}/processing/resume`) } export const deleteDocument: Fetcher = ({ datasetId, documentId }) => { return del(`/datasets/${datasetId}/documents/${documentId}`) } export const archiveDocument: Fetcher = ({ datasetId, documentId }) => { return patch(`/datasets/${datasetId}/documents/${documentId}/status/archive`) } export const unArchiveDocument: Fetcher = ({ datasetId, documentId }) => { return patch(`/datasets/${datasetId}/documents/${documentId}/status/un_archive`) } export const enableDocument: Fetcher = ({ datasetId, documentId }) => { return patch(`/datasets/${datasetId}/documents/${documentId}/status/enable`) } export const disableDocument: Fetcher = ({ datasetId, documentId }) => { return patch(`/datasets/${datasetId}/documents/${documentId}/status/disable`) } export const syncDocument: Fetcher = ({ datasetId, documentId }) => { return get(`/datasets/${datasetId}/documents/${documentId}/notion/sync`) } export const syncWebsite: Fetcher = ({ datasetId, documentId }) => { return get(`/datasets/${datasetId}/documents/${documentId}/website-sync`) } export const preImportNotionPages: Fetcher<{ notion_info: DataSourceNotionWorkspace[] }, { url: string; datasetId?: string }> = ({ url, datasetId }) => { return get<{ notion_info: DataSourceNotionWorkspace[] }>(url, { params: { dataset_id: datasetId } }) } export const modifyDocMetadata: Fetcher } }> = ({ datasetId, documentId, body }) => { return put(`/datasets/${datasetId}/documents/${documentId}/metadata`, { body }) } // apis for segments in a document export const fetchSegments: Fetcher = ({ datasetId, documentId, params }) => { return get(`/datasets/${datasetId}/documents/${documentId}/segments`, { params }) } export const enableSegment: Fetcher = ({ datasetId, segmentId }) => { return patch(`/datasets/${datasetId}/segments/${segmentId}/enable`) } export const disableSegment: Fetcher = ({ datasetId, segmentId }) => { return patch(`/datasets/${datasetId}/segments/${segmentId}/disable`) } export const updateSegment: Fetcher<{ data: SegmentDetailModel; doc_form: string }, { datasetId: string; documentId: string; segmentId: string; body: SegmentUpdator }> = ({ datasetId, documentId, segmentId, body }) => { return patch<{ data: SegmentDetailModel; doc_form: string }>(`/datasets/${datasetId}/documents/${documentId}/segments/${segmentId}`, { body }) } export const addSegment: Fetcher<{ data: SegmentDetailModel; doc_form: string }, { datasetId: string; documentId: string; body: SegmentUpdator }> = ({ datasetId, documentId, body }) => { return post<{ data: SegmentDetailModel; doc_form: string }>(`/datasets/${datasetId}/documents/${documentId}/segment`, { body }) } export const deleteSegment: Fetcher = ({ datasetId, documentId, segmentId }) => { return del(`/datasets/${datasetId}/documents/${documentId}/segments/${segmentId}`) } export const segmentBatchImport: Fetcher<{ job_id: string; job_status: string }, { url: string; body: FormData }> = ({ url, body }) => { return post<{ job_id: string; job_status: string }>(url, { body }, { bodyStringify: false, deleteContentType: true }) } export const checkSegmentBatchImportProgress: Fetcher<{ job_id: string; job_status: string }, { jobID: string }> = ({ jobID }) => { return get<{ job_id: string; job_status: string }>(`/datasets/batch_import_status/${jobID}`) } // hit testing export const hitTesting: Fetcher = ({ datasetId, queryText, retrieval_model }) => { return post(`/datasets/${datasetId}/hit-testing`, { body: { query: queryText, retrieval_model } }) } export const fetchTestingRecords: Fetcher = ({ datasetId, params }) => { return get(`/datasets/${datasetId}/queries`, { params }) } export const fetchFileIndexingEstimate: Fetcher = (body: IndexingEstimateParams) => { return post('/datasets/indexing-estimate', { body }) } export const fetchNotionPagePreview: Fetcher<{ content: string }, { workspaceID: string; pageID: string; pageType: string }> = ({ workspaceID, pageID, pageType }) => { return get<{ content: string }>(`notion/workspaces/${workspaceID}/pages/${pageID}/${pageType}/preview`) } export const fetchApiKeysList: Fetcher }> = ({ url, params }) => { return get(url, params) } export const delApikey: Fetcher }> = ({ url, params }) => { return del(url, params) } export const createApikey: Fetcher }> = ({ url, body }) => { return post(url, body) } export const fetchDatasetApiBaseUrl: Fetcher<{ api_base_url: string }, string> = (url) => { return get<{ api_base_url: string }>(url) } export const fetchDataSources = () => { return get('api-key-auth/data-source') } export const createDataSourceApiKeyBinding: Fetcher> = (body) => { return post('api-key-auth/data-source/binding', { body }) } export const removeDataSourceApiKeyBinding: Fetcher = (id: string) => { return del(`api-key-auth/data-source/${id}`) } export const createFirecrawlTask: Fetcher> = (body) => { return post('website/crawl', { body: { ...body, provider: 'firecrawl', }, }) } export const checkFirecrawlTaskStatus: Fetcher = (jobId: string) => { return get(`website/crawl/status/${jobId}`, { params: { provider: 'firecrawl', }, }, { silent: true, }) } type FileTypesRes = { allowed_extensions: string[] } export const fetchSupportFileTypes: Fetcher = ({ url }) => { return get(url) } export const getErrorDocs: Fetcher = ({ datasetId }) => { return get(`/datasets/${datasetId}/error-docs`) } export const retryErrorDocs: Fetcher = ({ datasetId, document_ids }) => { return post(`/datasets/${datasetId}/retry`, { body: { document_ids } }) }