import type { Fetcher } from 'swr' import { del, get, post, put, patch } from './base' import qs from 'qs' import type { RelatedAppResponse, DataSet, HitTestingResponse, HitTestingRecordsResponse, DataSetListResponse, CreateDocumentReq, InitialDocumentDetail, DocumentDetailResponse, DocumentListResponse, IndexingEstimateResponse, FileIndexingEstimateResponse, IndexingStatusResponse, ProcessRuleResponse, SegmentsQuery, SegmentsResponse, createDocumentResponse } from '@/models/datasets' import type { CommonResponse } from '@/models/common' // apis for documents in a dataset type CommonDocReq = { datasetId: string documentId: 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 fetchIndexingStatus: Fetcher = ({ datasetId, documentId }) => { return get(`/datasets/${datasetId}/documents/${documentId}/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 modifyDocMetadata: Fetcher } }> = ({ datasetId, documentId, body }) => { return put(`/datasets/${datasetId}/documents/${documentId}/metadata`, { body }) as Promise } // 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 } // 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/file-indexing-estimate`, { body }) as Promise }