feat: 添加知识库配置管理功能

新增地区-知识库绑定管理功能,支持增删改查操作
- 添加 V3 API 路由层:area-datasets 相关接口
- 添加 API 客户端:area-datasets.ts
- 添加自定义 Hook:use-area-dataset-config.ts
- 添加管理组件:area-dataset-config.tsx
- 修复路由冲突问题,删除重复的 .ts 路由文件
- 更新 dataset-manager 页面,添加 Tabs 导航

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-12-07 23:12:21 +08:00
parent 21bcb1310b
commit 27aff59152
13 changed files with 3740 additions and 21 deletions
@@ -0,0 +1,76 @@
/**
* PUT /api/v3/dify/area-datasets/{id} - 更新知识库绑定
* DELETE /api/v3/dify/area-datasets/{id} - 删除知识库绑定
*/
import { type LoaderFunctionArgs, json } from '@remix-run/node';
import { API_BASE_URL } from '~/config/api-config';
import { getUserSession } from '~/api/login/auth.server';
import { request as backendRequest } from '~/api/axios-client';
export async function loader({ request, params }: LoaderFunctionArgs) {
return json({ error: 'Method not allowed' }, { status: 405 });
}
/**
* 更新知识库绑定
*/
export async function action({ request, params }: LoaderFunctionArgs) {
try {
const { frontendJWT } = await getUserSession(request);
if (!frontendJWT) {
return new Response(
JSON.stringify({ error: 'JWT认证失败,请重新登录' }),
{ status: 401, headers: { 'Content-Type': 'application/json' } }
);
}
const { id } = params;
const method = request.method;
if (method === 'PUT') {
// 更新知识库绑定
const body = await request.json();
console.log(`[API V3] Update Area Dataset: ${id}`, body);
const apiUrl = `${API_BASE_URL}/v3/dify/area-datasets/${id}`;
const response = await backendRequest(apiUrl, {
method: 'PUT',
headers: {
Authorization: `Bearer ${frontendJWT}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
});
const data = await response.json();
return json(data, { status: response.status });
} else if (method === 'DELETE') {
// 删除知识库绑定
console.log(`[API V3] Delete Area Dataset: ${id}`);
const apiUrl = `${API_BASE_URL}/v3/dify/area-datasets/${id}`;
const response = await backendRequest(apiUrl, {
method: 'DELETE',
headers: {
Authorization: `Bearer ${frontendJWT}`,
},
});
const data = await response.json();
return json(data, { status: response.status });
} else {
return json({ error: 'Method not allowed' }, { status: 405 });
}
} catch (error: any) {
console.error('[API V3] Area Dataset Action - Error:', error.message);
return json(
{ error: error.message || 'Operation failed' },
{ status: 500 }
);
}
}
@@ -0,0 +1,42 @@
/**
* GET /api/v3/dify/area-datasets/areas - 获取可用地区列表(管理员)
*/
import { type LoaderFunctionArgs, json } from '@remix-run/node';
import { API_BASE_URL } from '~/config/api-config';
import { getUserSession } from '~/api/login/auth.server';
import { request as backendRequest } from '~/api/axios-client';
export async function loader({ request }: LoaderFunctionArgs) {
try {
const { frontendJWT } = await getUserSession(request);
if (!frontendJWT) {
return new Response(
JSON.stringify({ error: 'JWT认证失败,请重新登录' }),
{ status: 401, headers: { 'Content-Type': 'application/json' } }
);
}
console.log('[API V3] Get Available Areas');
// 转发请求到后端
const apiUrl = `${API_BASE_URL}/v3/dify/area-datasets/areas`;
const response = await backendRequest(apiUrl, {
method: 'GET',
headers: {
Authorization: `Bearer ${frontendJWT}`,
},
});
const data = await response.json();
return json(data, { status: response.status });
} catch (error: any) {
console.error('[API V3] Get Available Areas - Error:', error.message);
return json(
{ error: error.message || 'Failed to get available areas' },
{ status: 500 }
);
}
}
@@ -0,0 +1,42 @@
/**
* GET /api/v3/dify/area-datasets/my - 获取当前用户可访问的知识库列表
*/
import { type LoaderFunctionArgs, json } from '@remix-run/node';
import { API_BASE_URL } from '~/config/api-config';
import { getUserSession } from '~/api/login/auth.server';
import { request as backendRequest } from '~/api/axios-client';
export async function loader({ request }: LoaderFunctionArgs) {
try {
const { frontendJWT } = await getUserSession(request);
if (!frontendJWT) {
return new Response(
JSON.stringify({ error: 'JWT认证失败,请重新登录' }),
{ status: 401, headers: { 'Content-Type': 'application/json' } }
);
}
console.log('[API V3] Get My Area Datasets');
// 转发请求到后端
const apiUrl = `${API_BASE_URL}/v3/dify/area-datasets/my`;
const response = await backendRequest(apiUrl, {
method: 'GET',
headers: {
Authorization: `Bearer ${frontendJWT}`,
},
});
const data = await response.json();
return json(data, { status: response.status });
} catch (error: any) {
console.error('[API V3] Get My Area Datasets - Error:', error.message);
return json(
{ error: error.message || 'Failed to get area datasets' },
{ status: 500 }
);
}
}
+100
View File
@@ -0,0 +1,100 @@
/**
* GET /api/v3/dify/area-datasets - 获取所有知识库绑定列表(管理员)
* POST /api/v3/dify/area-datasets - 创建知识库绑定
*/
import { type LoaderFunctionArgs, json } from '@remix-run/node';
import { API_BASE_URL } from '~/config/api-config';
import { getUserSession } from '~/api/login/auth.server';
import { request as backendRequest } from '~/api/axios-client';
/**
* GET - 获取所有知识库绑定列表
*/
export async function loader({ request }: LoaderFunctionArgs) {
try {
const { frontendJWT } = await getUserSession(request);
if (!frontendJWT) {
return new Response(
JSON.stringify({ error: 'JWT认证失败,请重新登录' }),
{ status: 401, headers: { 'Content-Type': 'application/json' } }
);
}
// 获取查询参数
const url = new URL(request.url);
const area = url.searchParams.get('area');
const only_enabled = url.searchParams.get('only_enabled');
const page = url.searchParams.get('page') || '1';
const page_size = url.searchParams.get('page_size') || '20';
// 构建查询参数
const params = new URLSearchParams();
if (area) params.append('area', area);
if (only_enabled !== null) params.append('only_enabled', only_enabled);
params.append('page', page);
params.append('page_size', page_size);
console.log('[API V3] Get All Area Datasets', { area, only_enabled, page, page_size });
// 转发请求到后端
const apiUrl = `${API_BASE_URL}/v3/dify/area-datasets?${params}`;
const response = await backendRequest(apiUrl, {
method: 'GET',
headers: {
Authorization: `Bearer ${frontendJWT}`,
},
});
const data = await response.json();
return json(data, { status: response.status });
} catch (error: any) {
console.error('[API V3] Get All Area Datasets - Error:', error.message);
return json(
{ error: error.message || 'Failed to get all area datasets' },
{ status: 500 }
);
}
}
/**
* POST - 创建知识库绑定
*/
export async function action({ request }: LoaderFunctionArgs) {
try {
const { frontendJWT } = await getUserSession(request);
if (!frontendJWT) {
return new Response(
JSON.stringify({ error: 'JWT认证失败,请重新登录' }),
{ status: 401, headers: { 'Content-Type': 'application/json' } }
);
}
const body = await request.json();
console.log('[API V3] Create Area Dataset', body);
// 转发创建请求到后端
const apiUrl = `${API_BASE_URL}/v3/dify/area-datasets`;
const response = await backendRequest(apiUrl, {
method: 'POST',
headers: {
Authorization: `Bearer ${frontendJWT}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
});
const data = await response.json();
return json(data, { status: response.status });
} catch (error: any) {
console.error('[API V3] Create Area Dataset - Error:', error.message);
return json(
{ error: error.message || 'Failed to create area dataset' },
{ status: 500 }
);
}
}
+27 -17
View File
@@ -1,26 +1,36 @@
import { Tabs } from 'antd';
import DatasetManager from "~/components/dify-dataset-manager";
import datasetManagerStyles from "~/styles/components/dify-dataset-manager/index.css?url";
// import sidebarStyles from "~/styles/components/dify-dataset-manager/sidebar.css?url";
// import documentListStyles from "~/styles/components/dify-dataset-manager/document-list.css?url";
import AreaDatasetConfig from "~/components/dify-dataset-manager/area-dataset-config";
/**
* 注册样式
*/
export function links() {
return [
{ rel: "stylesheet", href: datasetManagerStyles },
// { rel: "stylesheet", href: sidebarStyles },
// { rel: "stylesheet", href: documentListStyles },
];
}
/**
* 知识库管理首页
* 知识库管理首页 - 带标签页导航
* 标签1: 知识库列表 - 进入单个知识库管理
* 标签2: 知识库配置管理 - 地区-知识库绑定管理
*/
export default function DatasetManagerIndex() {
const items = [
{
key: 'dataset-list',
label: '知识库列表',
children: <DatasetManager />,
},
{
key: 'area-config',
label: '知识库配置管理',
children: <AreaDatasetConfig />,
},
];
// 使用Tabs作为顶层导航,默认选中第一个
const defaultActiveTab = 'dataset-list';
return (
<div className="dataset-manager-page" style={{ height: '90vh', padding: '16px' }}>
<DatasetManager />
<div className="dataset-manager-container">
<Tabs
defaultActiveKey={defaultActiveTab}
items={items}
className="dataset-manager-tabs"
/>
</div>
);
}