feat:完成dify知识库文档基础CRUD模块

This commit is contained in:
PingChuan
2025-11-30 21:28:49 +08:00
parent d85010bada
commit 754ec2c7b5
21 changed files with 1142 additions and 706 deletions
@@ -0,0 +1,74 @@
import { type LoaderFunctionArgs } from '@remix-run/node';
import { API_BASE_URL } from '~/config/api-config';
/**
* GET /api/dataset/datasets/:datasetId/documents/:batch/indexing-status
* 获取文档嵌入状态(处理进度)
*
* Dify API: GET /datasets/{dataset_id}/documents/{batch}/indexing-status
*
* 返回示例:
* {
* "data": [{
* "id": "",
* "indexing_status": "indexing",
* "processing_started_at": 1681623462.0,
* "parsing_completed_at": 1681623462.0,
* "cleaning_completed_at": 1681623462.0,
* "splitting_completed_at": 1681623462.0,
* "completed_at": null,
* "paused_at": null,
* "error": null,
* "stopped_at": null,
* "completed_segments": 24,
* "total_segments": 100
* }]
* }
*/
export async function loader({ request, params }: LoaderFunctionArgs) {
try {
const { getUserSession } = await import("~/api/login/auth.server");
const { frontendJWT } = await getUserSession(request);
if (!frontendJWT) {
return new Response(
JSON.stringify({ error: 'JWT认证失败,请重新登录' }),
{ status: 401, headers: { 'Content-Type': 'application/json' } }
);
}
const { datasetId, batch } = params;
if (!datasetId || !batch) {
return new Response(
JSON.stringify({ error: '缺少必要参数 (datasetId, batch)' }),
{ status: 400, headers: { 'Content-Type': 'application/json' } }
);
}
console.log('[API] Indexing Status:', { datasetId, batch });
// 转发请求到 FastAPI -> Dify API
const apiUrl = `${API_BASE_URL}/dify_dataset/datasets/${datasetId}/documents/${batch}/indexing-status`;
const response = await fetch(apiUrl, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${frontendJWT}`,
},
});
const data = await response.json();
return new Response(JSON.stringify(data), {
status: response.status,
headers: { 'Content-Type': 'application/json' },
});
} catch (error: any) {
console.error('[API] Indexing Status - Error:', error.message);
return new Response(
JSON.stringify({ error: error.message || 'Failed to get indexing status' }),
{ status: 500, headers: { 'Content-Type': 'application/json' } }
);
}
}
@@ -0,0 +1,143 @@
import { type LoaderFunctionArgs, type ActionFunctionArgs } from '@remix-run/node';
import { API_BASE_URL } from '~/config/api-config';
/**
* GET /api/dataset/datasets/:datasetId/documents/:documentId/segments/:segmentId - 获取分段详情
* Dify API: GET /datasets/{dataset_id}/documents/{document_id}/segments/{segment_id}
*/
export async function loader({ request, params }: LoaderFunctionArgs) {
try {
const { getUserSession } = await import("~/api/login/auth.server");
const { frontendJWT } = await getUserSession(request);
if (!frontendJWT) {
return new Response(
JSON.stringify({ error: 'JWT认证失败,请重新登录' }),
{ status: 401, headers: { 'Content-Type': 'application/json' } }
);
}
const { datasetId, documentId, segmentId } = params;
if (!datasetId || !documentId || !segmentId) {
return new Response(
JSON.stringify({ error: '缺少必要参数' }),
{ status: 400, headers: { 'Content-Type': 'application/json' } }
);
}
console.log('[API] Segment Detail:', { datasetId, documentId, segmentId });
// 转发请求到 FastAPI -> Dify API
const apiUrl = `${API_BASE_URL}/dify_dataset/datasets/${datasetId}/documents/${documentId}/segments/${segmentId}`;
const response = await fetch(apiUrl, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${frontendJWT}`,
},
});
const data = await response.json();
return new Response(JSON.stringify(data), {
status: response.status,
headers: { 'Content-Type': 'application/json' },
});
} catch (error: any) {
console.error('[API] Segment Detail - Error:', error.message);
return new Response(
JSON.stringify({ error: error.message || 'Failed to get segment' }),
{ status: 500, headers: { 'Content-Type': 'application/json' } }
);
}
}
/**
* DELETE /api/dataset/datasets/:datasetId/documents/:documentId/segments/:segmentId - 删除分段
* POST - 更新分段 (Dify用POST更新分段,可传enabled参数切换状态)
* Dify API: DELETE/POST /datasets/{dataset_id}/documents/{document_id}/segments/{segment_id}
*/
export async function action({ request, params }: ActionFunctionArgs) {
try {
const { getUserSession } = await import("~/api/login/auth.server");
const { frontendJWT } = await getUserSession(request);
if (!frontendJWT) {
return new Response(
JSON.stringify({ error: 'JWT认证失败,请重新登录' }),
{ status: 401, headers: { 'Content-Type': 'application/json' } }
);
}
const { datasetId, documentId, segmentId } = params;
if (!datasetId || !documentId || !segmentId) {
return new Response(
JSON.stringify({ error: '缺少必要参数' }),
{ status: 400, headers: { 'Content-Type': 'application/json' } }
);
}
const method = request.method;
const apiUrl = `${API_BASE_URL}/dify_dataset/datasets/${datasetId}/documents/${documentId}/segments/${segmentId}`;
if (method === 'DELETE') {
console.log('[API] Delete Segment:', { datasetId, documentId, segmentId });
const response = await fetch(apiUrl, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${frontendJWT}`,
},
});
// Dify删除分段返回 204 No Content
if (response.status === 204) {
return new Response(
JSON.stringify({ result: 'success' }),
{ status: 200, headers: { 'Content-Type': 'application/json' } }
);
}
const data = await response.json();
return new Response(JSON.stringify(data), {
status: response.status,
headers: { 'Content-Type': 'application/json' },
});
}
if (method === 'POST') {
// POST 用于更新分段(包括启用/禁用)
const body = await request.json();
console.log('[API] Update Segment:', { datasetId, documentId, segmentId, body });
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${frontendJWT}`,
},
body: JSON.stringify(body),
});
const data = await response.json();
return new Response(JSON.stringify(data), {
status: response.status,
headers: { 'Content-Type': 'application/json' },
});
}
return new Response(
JSON.stringify({ error: 'Method not allowed' }),
{ status: 405, headers: { 'Content-Type': 'application/json' } }
);
} catch (error: any) {
console.error('[API] Segment Action - Error:', error.message);
return new Response(
JSON.stringify({ error: error.message || 'Failed to process segment request' }),
{ status: 500, headers: { 'Content-Type': 'application/json' } }
);
}
}
@@ -0,0 +1,66 @@
import { type LoaderFunctionArgs } from '@remix-run/node';
import { API_BASE_URL } from '~/config/api-config';
/**
* GET /api/dataset/datasets/:datasetId/documents/:documentId/segments - 获取分段列表
* Dify API: GET /datasets/{dataset_id}/documents/{document_id}/segments
*/
export async function loader({ request, params }: LoaderFunctionArgs) {
try {
const { getUserSession } = await import("~/api/login/auth.server");
const { frontendJWT } = await getUserSession(request);
if (!frontendJWT) {
return new Response(
JSON.stringify({ error: 'JWT认证失败,请重新登录' }),
{ status: 401, headers: { 'Content-Type': 'application/json' } }
);
}
const { datasetId, documentId } = params;
if (!datasetId || !documentId) {
return new Response(
JSON.stringify({ error: '缺少必要参数' }),
{ status: 400, headers: { 'Content-Type': 'application/json' } }
);
}
// 获取查询参数
const url = new URL(request.url);
const page = url.searchParams.get('page') || '1';
const limit = url.searchParams.get('limit') || '20';
const keyword = url.searchParams.get('keyword') || '';
const status = url.searchParams.get('status') || '';
console.log('[API] Segments List:', { datasetId, documentId, page, limit, keyword, status });
// 构建查询参数
const queryParams = new URLSearchParams({ page, limit });
if (keyword) queryParams.append('keyword', keyword);
if (status) queryParams.append('status', status);
// 转发请求到 FastAPI -> Dify API
const apiUrl = `${API_BASE_URL}/dify_dataset/datasets/${datasetId}/documents/${documentId}/segments?${queryParams}`;
const response = await fetch(apiUrl, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${frontendJWT}`,
},
});
const data = await response.json();
return new Response(JSON.stringify(data), {
status: response.status,
headers: { 'Content-Type': 'application/json' },
});
} catch (error: any) {
console.error('[API] Segments List - Error:', error.message);
return new Response(
JSON.stringify({ error: error.message || 'Failed to get segments' }),
{ status: 500, headers: { 'Content-Type': 'application/json' } }
);
}
}
@@ -26,7 +26,7 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
console.log('[API] Document Detail:', { datasetId, documentId });
const apiUrl = `${API_BASE_URL}/dify-dataset/datasets/${datasetId}/documents/${documentId}`;
const apiUrl = `${API_BASE_URL}/dify_dataset/datasets/${datasetId}/documents/${documentId}`;
const response = await fetch(apiUrl, {
method: 'GET',
headers: {
@@ -79,7 +79,7 @@ export async function action({ request, params }: ActionFunctionArgs) {
if (method === 'DELETE') {
console.log('[API] Delete Document:', { datasetId, documentId });
const apiUrl = `${API_BASE_URL}/dify-dataset/datasets/${datasetId}/documents/${documentId}`;
const apiUrl = `${API_BASE_URL}/dify_dataset/datasets/${datasetId}/documents/${documentId}`;
const response = await fetch(apiUrl, {
method: 'DELETE',
headers: {
@@ -1,10 +1,26 @@
import { type ActionFunctionArgs } from '@remix-run/node';
import { type LoaderFunctionArgs } from '@remix-run/node';
import { API_BASE_URL } from '~/config/api-config';
/**
* PATCH /api/dataset/datasets/:datasetId/documents/:documentId/status -
* GET /api/dataset/datasets/:datasetId/documents/:documentId/upload-file
*
*
* Dify API: GET /datasets/{dataset_id}/documents/{document_id}/upload-file
*
* :
* {
* "id": "file_id",
* "name": "file_name",
* "size": 1024,
* "extension": "txt",
* "url": "preview_url",
* "download_url": "download_url",
* "mime_type": "text/plain",
* "created_by": "user_id",
* "created_at": 1728734540
* }
*/
export async function action({ request, params }: ActionFunctionArgs) {
export async function loader({ request, params }: LoaderFunctionArgs) {
try {
const { getUserSession } = await import("~/api/login/auth.server");
const { frontendJWT } = await getUserSession(request);
@@ -19,24 +35,21 @@ export async function action({ request, params }: ActionFunctionArgs) {
const { datasetId, documentId } = params;
if (!datasetId || !documentId) {
return new Response(
JSON.stringify({ error: '缺少必要参数' }),
JSON.stringify({ error: '缺少必要参数 (datasetId, documentId)' }),
{ status: 400, headers: { 'Content-Type': 'application/json' } }
);
}
const body = await request.json();
const { enabled } = body;
console.log('[API] Upload File Info:', { datasetId, documentId });
console.log('[API] Toggle Document Status:', { datasetId, documentId, enabled });
const apiUrl = `${API_BASE_URL}/dify-dataset/datasets/${datasetId}/documents/${documentId}/status`;
// 转发请求到 FastAPI -> Dify API
const apiUrl = `${API_BASE_URL}/dify_dataset/datasets/${datasetId}/documents/${documentId}/upload-file`;
const response = await fetch(apiUrl, {
method: 'PATCH',
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${frontendJWT}`,
},
body: JSON.stringify({ enabled }),
});
const data = await response.json();
@@ -47,9 +60,9 @@ export async function action({ request, params }: ActionFunctionArgs) {
});
} catch (error: any) {
console.error('[API] Toggle Document Status - Error:', error.message);
console.error('[API] Upload File Info - Error:', error.message);
return new Response(
JSON.stringify({ error: error.message || 'Failed to toggle status' }),
JSON.stringify({ error: error.message || 'Failed to get upload file info' }),
{ status: 500, headers: { 'Content-Type': 'application/json' } }
);
}
@@ -0,0 +1,75 @@
import { type ActionFunctionArgs } from '@remix-run/node';
import { API_BASE_URL } from '~/config/api-config';
/**
* PATCH /api/dataset/datasets/:datasetId/documents/status/:action - 批量更新文档状态
* Dify API: PATCH /datasets/{dataset_id}/documents/status/{action}
* action: enable / disable / archive / un_archive
*/
export async function action({ request, params }: ActionFunctionArgs) {
try {
const { getUserSession } = await import("~/api/login/auth.server");
const { frontendJWT } = await getUserSession(request);
if (!frontendJWT) {
return new Response(
JSON.stringify({ error: 'JWT认证失败,请重新登录' }),
{ status: 401, headers: { 'Content-Type': 'application/json' } }
);
}
const { datasetId, action: statusAction } = params;
if (!datasetId || !statusAction) {
return new Response(
JSON.stringify({ error: '缺少必要参数' }),
{ status: 400, headers: { 'Content-Type': 'application/json' } }
);
}
// 验证action参数
const validActions = ['enable', 'disable', 'archive', 'un_archive'];
if (!validActions.includes(statusAction)) {
return new Response(
JSON.stringify({ error: `无效的action参数,有效值: ${validActions.join(', ')}` }),
{ status: 400, headers: { 'Content-Type': 'application/json' } }
);
}
const body = await request.json();
const { document_ids } = body;
if (!document_ids || !Array.isArray(document_ids) || document_ids.length === 0) {
return new Response(
JSON.stringify({ error: '缺少 document_ids 参数' }),
{ status: 400, headers: { 'Content-Type': 'application/json' } }
);
}
console.log('[API] Update Documents Status:', { datasetId, action: statusAction, document_ids });
// 转发请求到 FastAPI -> Dify API
const apiUrl = `${API_BASE_URL}/dify_dataset/datasets/${datasetId}/documents/status/${statusAction}`;
const response = await fetch(apiUrl, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${frontendJWT}`,
},
body: JSON.stringify({ document_ids }),
});
const data = await response.json();
return new Response(JSON.stringify(data), {
status: response.status,
headers: { 'Content-Type': 'application/json' },
});
} catch (error: any) {
console.error('[API] Update Documents Status - Error:', error.message);
return new Response(
JSON.stringify({ error: error.message || 'Failed to update documents status' }),
{ status: 500, headers: { 'Content-Type': 'application/json' } }
);
}
}
@@ -38,7 +38,7 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
if (keyword) queryParams.append('keyword', keyword);
// 转发请求到 FastAPI
const apiUrl = `${API_BASE_URL}/dify-dataset/datasets/${datasetId}/documents?${queryParams}`;
const apiUrl = `${API_BASE_URL}/dify_dataset/datasets/${datasetId}/documents?${queryParams}`;
const response = await fetch(apiUrl, {
method: 'GET',
headers: {
@@ -92,8 +92,8 @@ export async function action({ request, params }: ActionFunctionArgs) {
console.log('[API] Upload Document:', { datasetId });
// 转发请求到 FastAPI
const apiUrl = `${API_BASE_URL}/dify-dataset/datasets/${datasetId}/documents/create-by-file`;
// 转发请求到 FastAPI (注意:Dify API是 /document/create-by-filedocument是单数)
const apiUrl = `${API_BASE_URL}/dify_dataset/datasets/${datasetId}/document/create-by-file`;
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
@@ -0,0 +1,135 @@
import { type LoaderFunctionArgs, type ActionFunctionArgs } from '@remix-run/node';
import { API_BASE_URL } from '~/config/api-config';
/**
* GET /api/dataset/datasets/:datasetId - 获取知识库详情
* Dify API: GET /datasets/{dataset_id}
*/
export async function loader({ request, params }: LoaderFunctionArgs) {
try {
const { getUserSession } = await import("~/api/login/auth.server");
const { frontendJWT } = await getUserSession(request);
if (!frontendJWT) {
return new Response(
JSON.stringify({ error: 'JWT认证失败,请重新登录' }),
{ status: 401, headers: { 'Content-Type': 'application/json' } }
);
}
const { datasetId } = params;
if (!datasetId) {
return new Response(
JSON.stringify({ error: '缺少 datasetId 参数' }),
{ status: 400, headers: { 'Content-Type': 'application/json' } }
);
}
console.log('[API] Dataset Detail:', { datasetId });
// 转发请求到 FastAPI -> Dify API
const apiUrl = `${API_BASE_URL}/dify_dataset/datasets/${datasetId}`;
const response = await fetch(apiUrl, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${frontendJWT}`,
},
});
const data = await response.json();
return new Response(JSON.stringify(data), {
status: response.status,
headers: { 'Content-Type': 'application/json' },
});
} catch (error: any) {
console.error('[API] Dataset Detail - Error:', error.message);
return new Response(
JSON.stringify({ error: error.message || 'Failed to get dataset' }),
{ status: 500, headers: { 'Content-Type': 'application/json' } }
);
}
}
/**
* PATCH /api/dataset/datasets/:datasetId - 修改知识库详情
*
* Dify API: PATCH /datasets/{dataset_id}
*
* 请求体示例:
* {
* "name": "知识库名称",
* "description": "描述",
* "indexing_technique": "high_quality",
* "permission": "only_me",
* "embedding_model_provider": "zhipuai",
* "embedding_model": "embedding-3",
* "retrieval_model": {
* "search_method": "semantic_search",
* "reranking_enable": false,
* "top_k": 2,
* "score_threshold_enabled": false
* }
* }
*
* 注意:删除知识库功能不对外开放
*/
export async function action({ request, params }: ActionFunctionArgs) {
try {
const { getUserSession } = await import("~/api/login/auth.server");
const { frontendJWT } = await getUserSession(request);
if (!frontendJWT) {
return new Response(
JSON.stringify({ error: 'JWT认证失败,请重新登录' }),
{ status: 401, headers: { 'Content-Type': 'application/json' } }
);
}
const { datasetId } = params;
if (!datasetId) {
return new Response(
JSON.stringify({ error: '缺少 datasetId 参数' }),
{ status: 400, headers: { 'Content-Type': 'application/json' } }
);
}
const method = request.method;
if (method === 'PATCH') {
// 修改知识库详情
const body = await request.json();
console.log('[API] Update Dataset:', { datasetId, body });
const apiUrl = `${API_BASE_URL}/dify_dataset/datasets/${datasetId}`;
const response = await fetch(apiUrl, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${frontendJWT}`,
},
body: JSON.stringify(body),
});
const data = await response.json();
return new Response(JSON.stringify(data), {
status: response.status,
headers: { 'Content-Type': 'application/json' },
});
}
return new Response(
JSON.stringify({ error: 'Method not allowed' }),
{ status: 405, headers: { 'Content-Type': 'application/json' } }
);
} catch (error: any) {
console.error('[API] Dataset Action - Error:', error.message);
return new Response(
JSON.stringify({ error: error.message || 'Failed to process dataset request' }),
{ status: 500, headers: { 'Content-Type': 'application/json' } }
);
}
}
+1 -1
View File
@@ -30,7 +30,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
console.log('[API] Dataset List - 获取知识库列表:', { page, limit });
// 转发请求到 FastAPI
const apiUrl = `${API_BASE_URL}/dify-dataset/datasets?page=${page}&limit=${limit}`;
const apiUrl = `${API_BASE_URL}/dify_dataset/datasets?page=${page}&limit=${limit}`;
const response = await fetch(apiUrl, {
method: 'GET',
headers: {