feat:完成dify知识库文档基础CRUD模块
This commit is contained in:
@@ -9,11 +9,15 @@
|
||||
|
||||
import axios from 'axios';
|
||||
import type {
|
||||
Dataset,
|
||||
DatasetsResponse,
|
||||
DocumentsResponse,
|
||||
SegmentsResponse,
|
||||
Document,
|
||||
OperationResult,
|
||||
IndexingStatusResponse,
|
||||
UploadFileInfo,
|
||||
UpdateDatasetRequest,
|
||||
} from './types';
|
||||
|
||||
// ============================================================================
|
||||
@@ -59,14 +63,38 @@ export async function fetchDatasets(
|
||||
* @param datasetId - 知识库 ID
|
||||
* @returns 知识库详情
|
||||
*/
|
||||
export async function fetchDataset(datasetId: string): Promise<any> {
|
||||
const response = await axios.get(
|
||||
export async function fetchDataset(datasetId: string): Promise<Dataset> {
|
||||
const response = await axios.get<Dataset>(
|
||||
`${API_URL}/datasets/${datasetId}`,
|
||||
{ withCredentials: true }
|
||||
);
|
||||
return response.data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新知识库详情
|
||||
*
|
||||
* @param datasetId - 知识库 ID
|
||||
* @param data - 更新数据
|
||||
* @returns 更新后的知识库详情
|
||||
*/
|
||||
export async function updateDataset(
|
||||
datasetId: string,
|
||||
data: UpdateDatasetRequest
|
||||
): Promise<Dataset> {
|
||||
console.log('[Dataset Client] 更新知识库:', { datasetId, data });
|
||||
|
||||
const response = await axios.patch<Dataset>(
|
||||
`${API_URL}/datasets/${datasetId}`,
|
||||
data,
|
||||
{
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
withCredentials: true,
|
||||
}
|
||||
);
|
||||
return response.data;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// 文档 API
|
||||
// ============================================================================
|
||||
@@ -144,6 +172,8 @@ export async function deleteDocument(
|
||||
|
||||
/**
|
||||
* 启用/禁用文档
|
||||
* Dify API: PATCH /datasets/{dataset_id}/documents/status/{action}
|
||||
* action: enable / disable / archive / un_archive
|
||||
*
|
||||
* @param datasetId - 知识库 ID
|
||||
* @param documentId - 文档 ID
|
||||
@@ -155,11 +185,12 @@ export async function toggleDocumentStatus(
|
||||
documentId: string,
|
||||
enabled: boolean
|
||||
): Promise<OperationResult> {
|
||||
console.log('[Dataset Client] 切换文档状态:', { datasetId, documentId, enabled });
|
||||
const action = enabled ? 'enable' : 'disable';
|
||||
console.log('[Dataset Client] 切换文档状态:', { datasetId, documentId, action });
|
||||
|
||||
const response = await axios.patch<OperationResult>(
|
||||
`${API_URL}/datasets/${datasetId}/documents/${documentId}/status`,
|
||||
{ enabled },
|
||||
`${API_URL}/datasets/${datasetId}/documents/status/${action}`,
|
||||
{ document_ids: [documentId] },
|
||||
{
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
withCredentials: true,
|
||||
@@ -231,6 +262,8 @@ export async function deleteSegment(
|
||||
|
||||
/**
|
||||
* 启用/禁用分段
|
||||
* Dify API: POST /datasets/{dataset_id}/documents/{document_id}/segments/{segment_id}
|
||||
* 通过更新分段的方式来切换状态
|
||||
*
|
||||
* @param datasetId - 知识库 ID
|
||||
* @param documentId - 文档 ID
|
||||
@@ -244,9 +277,11 @@ export async function toggleSegmentStatus(
|
||||
segmentId: string,
|
||||
enabled: boolean
|
||||
): Promise<OperationResult> {
|
||||
const response = await axios.patch<OperationResult>(
|
||||
`${API_URL}/datasets/${datasetId}/documents/${documentId}/segments/${segmentId}/status`,
|
||||
{ enabled },
|
||||
console.log('[Dataset Client] 切换分段状态:', { datasetId, documentId, segmentId, enabled });
|
||||
|
||||
const response = await axios.post<OperationResult>(
|
||||
`${API_URL}/datasets/${datasetId}/documents/${documentId}/segments/${segmentId}`,
|
||||
{ segment: { enabled } },
|
||||
{
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
withCredentials: true,
|
||||
@@ -284,7 +319,7 @@ export async function uploadDocument(
|
||||
console.log('[Dataset Client] 上传文档:', { datasetId, fileName: file.name });
|
||||
|
||||
const response = await axios.post(
|
||||
`${API_URL}/datasets/${datasetId}/documents/create-by-file`,
|
||||
`${API_URL}/datasets/${datasetId}/documents`,
|
||||
formData,
|
||||
{
|
||||
withCredentials: true,
|
||||
@@ -298,3 +333,43 @@ export async function uploadDocument(
|
||||
);
|
||||
return response.data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文档嵌入状态(索引进度)
|
||||
*
|
||||
* @param datasetId - 知识库 ID
|
||||
* @param batch - 上传文档的批次号
|
||||
* @returns 索引状态列表
|
||||
*/
|
||||
export async function fetchIndexingStatus(
|
||||
datasetId: string,
|
||||
batch: string
|
||||
): Promise<IndexingStatusResponse> {
|
||||
console.log('[Dataset Client] 获取索引状态:', { datasetId, batch });
|
||||
|
||||
const response = await axios.get<IndexingStatusResponse>(
|
||||
`${API_URL}/datasets/${datasetId}/documents/${batch}/indexing-status`,
|
||||
{ withCredentials: true }
|
||||
);
|
||||
return response.data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文档上传文件信息
|
||||
*
|
||||
* @param datasetId - 知识库 ID
|
||||
* @param documentId - 文档 ID
|
||||
* @returns 上传文件信息
|
||||
*/
|
||||
export async function fetchUploadFileInfo(
|
||||
datasetId: string,
|
||||
documentId: string
|
||||
): Promise<UploadFileInfo> {
|
||||
console.log('[Dataset Client] 获取上传文件信息:', { datasetId, documentId });
|
||||
|
||||
const response = await axios.get<UploadFileInfo>(
|
||||
`${API_URL}/datasets/${datasetId}/documents/${documentId}/upload-file`,
|
||||
{ withCredentials: true }
|
||||
);
|
||||
return response.data;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user