temp:临时备份,完成一半知识库管理模块

This commit is contained in:
PingChuan
2025-12-01 12:33:53 +08:00
parent 754ec2c7b5
commit 0c1b81cfb2
25 changed files with 3564 additions and 560 deletions
@@ -1,4 +1,4 @@
import { type LoaderFunctionArgs } from '@remix-run/node';
import { type LoaderFunctionArgs, type ActionFunctionArgs } from '@remix-run/node';
import { API_BASE_URL } from '~/config/api-config';
/**
@@ -64,3 +64,65 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
);
}
}
/**
* POST /api/dataset/datasets/:datasetId/documents/:documentId/segments - 新增分段(批量)
* Dify API: POST /datasets/{dataset_id}/documents/{document_id}/segments
* 请求体: { segments: [{ content, answer?, keywords? }] }
*/
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 } = params;
if (!datasetId || !documentId) {
return new Response(
JSON.stringify({ error: '缺少必要参数' }),
{ status: 400, headers: { 'Content-Type': 'application/json' } }
);
}
if (request.method !== 'POST') {
return new Response(
JSON.stringify({ error: 'Method not allowed' }),
{ status: 405, headers: { 'Content-Type': 'application/json' } }
);
}
const body = await request.json();
console.log('[API] Create Segments:', { datasetId, documentId, segmentsCount: body.segments?.length });
// 转发请求到 FastAPI -> Dify API
const apiUrl = `${API_BASE_URL}/dify_dataset/datasets/${datasetId}/documents/${documentId}/segments`;
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' },
});
} catch (error: any) {
console.error('[API] Create Segments - Error:', error.message);
return new Response(
JSON.stringify({ error: error.message || 'Failed to create segments' }),
{ status: 500, headers: { 'Content-Type': 'application/json' } }
);
}
}