feat: system knowledge base management with admin/user role separation

- Auto-create system KBs from data/knowledge_base/ subdirectories on startup
- Upgrade existing user KBs to system KBs when matching directory found
- Admin can upload to system KBs (saved to knowledge_base dir)
- Regular users see only their own KBs on knowledge page
- Fix API permission checks for system KBs (list, detail, documents, update, delete)
- Add is_superuser to UserResponse and frontend User type
- Fix sidebar session menu visibility for long titles

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-05-27 16:42:28 +08:00
parent 1ce689ad1e
commit 38865b0f7d
10 changed files with 344 additions and 129 deletions
+150 -70
View File
@@ -30,7 +30,7 @@ import { KnowledgeBase } from "@/types";
export default function KnowledgePage() {
const router = useRouter();
const { isAuthenticated, isLoading: authLoading } = useAuthStore();
const { isAuthenticated, isLoading: authLoading, user } = useAuthStore();
const [knowledgeBases, setKnowledgeBases] = useState<KnowledgeBase[]>([]);
const [isLoading, setIsLoading] = useState(true);
const [searchQuery, setSearchQuery] = useState("");
@@ -58,9 +58,7 @@ export default function KnowledgePage() {
setIsLoading(true);
setError(null);
const bases = await knowledgeBaseAPI.getKnowledgeBases();
// 过滤掉系统知识库,只显示用户创建的知识库
const userBases = bases.filter(kb => !kb.is_system);
setKnowledgeBases(userBases);
setKnowledgeBases(bases);
} catch (err) {
console.error("加载知识库失败:", err);
setError("加载知识库失败");
@@ -114,6 +112,10 @@ export default function KnowledgePage() {
(kb.description && kb.description.toLowerCase().includes(searchQuery.toLowerCase()))
);
const isAdmin = user?.is_superuser === true;
const systemKBs = isAdmin ? filteredKnowledgeBases.filter(kb => kb.is_system) : [];
const userKBs = filteredKnowledgeBases.filter(kb => !kb.is_system);
if (authLoading || isLoading) {
return (
<div className="min-h-screen flex items-center justify-center">
@@ -213,13 +215,80 @@ export default function KnowledgePage() {
/>
</div>
<div className="text-sm text-muted-foreground">
{filteredKnowledgeBases.length}
{userKBs.length} {systemKBs.length > 0 ? `${systemKBs.length} 个系统知识库` : ""}
</div>
</div>
</div>
{/* 知识库列表 */}
{filteredKnowledgeBases.length === 0 ? (
{/* 系统知识库 */}
{systemKBs.length > 0 && (
<div className="mb-8">
<div className="flex items-center space-x-2 mb-4">
<FolderOpen className="w-5 h-5 text-amber-600" />
<h2 className="text-lg font-semibold"></h2>
<span className="text-sm text-muted-foreground">({systemKBs.length})</span>
</div>
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
{systemKBs.map((kb) => (
<Card key={kb.id} className="hover:shadow-lg transition-shadow backdrop-blur-sm bg-card/80 border-amber-500/30">
<CardHeader>
<div className="flex items-start justify-between">
<div className="flex items-center space-x-2">
<FolderOpen className="w-5 h-5 text-amber-600" />
<div>
<CardTitle className="text-lg">{kb.name}</CardTitle>
<CardDescription className="text-sm">
{kb.description || "系统知识库"}
</CardDescription>
</div>
</div>
<div className="flex items-center space-x-1">
{kb.document_count > 0 ? (
<CheckCircle className="w-4 h-4 text-green-600" />
) : (
<Clock className="w-4 h-4 text-gray-400" />
)}
</div>
</div>
</CardHeader>
<CardContent>
<div className="space-y-2">
<div className="flex justify-between text-sm text-muted-foreground">
<span></span>
<span>{kb.document_count} </span>
</div>
<div className="flex justify-between text-sm text-muted-foreground">
<span></span>
<span>{formatDate(kb.created_at)}</span>
</div>
<div className="flex justify-between text-sm">
<span></span>
<span className={kb.document_count > 0 ? "text-green-600" : "text-gray-500"}>
{kb.document_count > 0 ? "已就绪" : "空知识库"}
</span>
</div>
</div>
<div className="flex space-x-2 mt-4">
<Button
variant="outline"
size="sm"
className="flex-1"
onClick={() => router.push(`/knowledge/${kb.id}`)}
>
<Settings className="w-4 h-4 mr-1" />
</Button>
</div>
</CardContent>
</Card>
))}
</div>
</div>
)}
{/* 用户知识库 */}
{userKBs.length === 0 && systemKBs.length === 0 ? (
<Card className="backdrop-blur-sm bg-card/80 border-border/50 shadow-xl">
<CardContent className="text-center py-12">
<BookOpen className="w-12 h-12 text-muted-foreground mx-auto mb-4" />
@@ -238,70 +307,81 @@ export default function KnowledgePage() {
</CardContent>
</Card>
) : (
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
{filteredKnowledgeBases.map((kb) => (
<Card key={kb.id} className="hover:shadow-lg transition-shadow backdrop-blur-sm bg-card/80 border-border/50">
<CardHeader>
<div className="flex items-start justify-between">
<div className="flex items-center space-x-2">
<BookOpen className="w-5 h-5 text-blue-600" />
<div>
<CardTitle className="text-lg">{kb.name}</CardTitle>
<CardDescription className="text-sm">
{kb.description || "暂无描述"}
</CardDescription>
userKBs.length > 0 && (
<div>
{systemKBs.length > 0 && (
<div className="flex items-center space-x-2 mb-4">
<BookOpen className="w-5 h-5 text-blue-600" />
<h2 className="text-lg font-semibold"></h2>
<span className="text-sm text-muted-foreground">({userKBs.length})</span>
</div>
)}
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
{userKBs.map((kb) => (
<Card key={kb.id} className="hover:shadow-lg transition-shadow backdrop-blur-sm bg-card/80 border-border/50">
<CardHeader>
<div className="flex items-start justify-between">
<div className="flex items-center space-x-2">
<BookOpen className="w-5 h-5 text-blue-600" />
<div>
<CardTitle className="text-lg">{kb.name}</CardTitle>
<CardDescription className="text-sm">
{kb.description || "暂无描述"}
</CardDescription>
</div>
</div>
<div className="flex items-center space-x-1">
{kb.document_count > 0 ? (
<CheckCircle className="w-4 h-4 text-green-600" />
) : (
<Clock className="w-4 h-4 text-gray-400" />
)}
</div>
</div>
</div>
<div className="flex items-center space-x-1">
{kb.document_count > 0 ? (
<CheckCircle className="w-4 h-4 text-green-600" />
) : (
<Clock className="w-4 h-4 text-gray-400" />
)}
</div>
</div>
</CardHeader>
<CardContent>
<div className="space-y-2">
<div className="flex justify-between text-sm text-muted-foreground">
<span></span>
<span>{kb.document_count} </span>
</div>
<div className="flex justify-between text-sm text-muted-foreground">
<span></span>
<span>{formatDate(kb.created_at)}</span>
</div>
<div className="flex justify-between text-sm">
<span></span>
<span className={kb.document_count > 0 ? "text-green-600" : "text-gray-500"}>
{kb.document_count > 0 ? "已就绪" : "空知识库"}
</span>
</div>
</div>
<div className="flex space-x-2 mt-4">
<Button
variant="outline"
size="sm"
className="flex-1"
onClick={() => router.push(`/knowledge/${kb.id}`)}
>
<Settings className="w-4 h-4 mr-1" />
</Button>
<Button
variant="outline"
size="sm"
onClick={() => handleDeleteKnowledgeBase(kb.id)}
className="text-red-600 hover:text-red-700"
>
<Trash2 className="w-4 h-4" />
</Button>
</div>
</CardContent>
</Card>
))}
</div>
</CardHeader>
<CardContent>
<div className="space-y-2">
<div className="flex justify-between text-sm text-muted-foreground">
<span></span>
<span>{kb.document_count} </span>
</div>
<div className="flex justify-between text-sm text-muted-foreground">
<span></span>
<span>{formatDate(kb.created_at)}</span>
</div>
<div className="flex justify-between text-sm">
<span></span>
<span className={kb.document_count > 0 ? "text-green-600" : "text-gray-500"}>
{kb.document_count > 0 ? "已就绪" : "空知识库"}
</span>
</div>
</div>
<div className="flex space-x-2 mt-4">
<Button
variant="outline"
size="sm"
className="flex-1"
onClick={() => router.push(`/knowledge/${kb.id}`)}
>
<Settings className="w-4 h-4 mr-1" />
</Button>
<Button
variant="outline"
size="sm"
onClick={() => handleDeleteKnowledgeBase(kb.id)}
className="text-red-600 hover:text-red-700"
>
<Trash2 className="w-4 h-4" />
</Button>
</div>
</CardContent>
</Card>
))}
</div>
</div>
)
)}
</div>
+1 -7
View File
@@ -1,7 +1,7 @@
"use client";
import { useState } from "react";
import { Download, FileText, FileJson, File } from "lucide-react";
import { Download, FileText, FileJson } from "lucide-react";
import { Button } from "@/components/ui/button";
import {
Dialog,
@@ -61,12 +61,6 @@ export default function ExportDialog({ sessionId, sessionTitle, children, onClos
description: "可读性好的文本格式,适合分享",
icon: FileText,
},
{
value: "pdf",
label: "PDF 格式",
description: "适合打印和正式文档",
icon: File,
},
];
return (
+7 -1
View File
@@ -11,7 +11,7 @@ import { format } from "date-fns";
import { Button } from "@/components/ui/button";
import { useChatStore } from "@/store/chat";
import SourceReferences from "./source-references";
import { useState } from "react";
import { useState, useEffect } from "react";
import { toast } from "sonner";
interface MessageItemProps {
@@ -21,6 +21,7 @@ interface MessageItemProps {
const ThinkingProcess = ({ thinking, isStreaming }: { thinking: ThinkingStep[]; isStreaming?: boolean }) => {
const [expanded, setExpanded] = useState(false);
if (!thinking || thinking.length === 0) return null;
// 只提取有实际内容的步骤:推理内容和检索文档
@@ -32,6 +33,11 @@ const ThinkingProcess = ({ thinking, isStreaming }: { thinking: ThinkingStep[];
// 没有推理内容也没有文档详情时不显示
if (!hasReasoning && !hasDocs && !isStreaming) return null;
// 流式生成时自动展开,完成后自动收起
useEffect(() => {
setExpanded(!!isStreaming);
}, [isStreaming]);
// 合并推理文本
const reasoningText = reasoningSteps.map(s => s.message).join('');
+8 -3
View File
@@ -13,6 +13,7 @@ import {
Download,
Search,
MoreVertical,
PanelLeftClose,
} from "lucide-react";
import {
DropdownMenu,
@@ -121,9 +122,13 @@ export default function Sidebar({ onClose }: SidebarProps) {
<div className="px-3 pt-4 pb-3 space-y-3 border-b border-border/30">
<div className="flex items-center justify-between px-1">
<span className="text-sm font-semibold text-foreground"></span>
<Button onClick={handleNewChat} size="sm" variant="ghost" className="h-7 w-7 p-0 hover:bg-primary/10 hover:text-primary">
<Plus className="w-4 h-4" />
</Button>
<button
onClick={onClose}
className="h-7 w-7 flex items-center justify-center rounded-md text-muted-foreground hover:text-foreground hover:bg-muted/50 transition-colors"
title="收起侧边栏"
>
<PanelLeftClose className="w-4 h-4" />
</button>
</div>
<div className="relative">
<Search className="absolute left-2.5 top-1/2 -translate-y-1/2 w-3.5 h-3.5 text-muted-foreground" />
+1
View File
@@ -5,6 +5,7 @@ export interface User {
email: string;
full_name?: string;
is_active: boolean;
is_superuser?: boolean;
created_at: string;
}