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:
@@ -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 +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 (
|
||||
|
||||
@@ -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('');
|
||||
|
||||
|
||||
@@ -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" />
|
||||
|
||||
@@ -5,6 +5,7 @@ export interface User {
|
||||
email: string;
|
||||
full_name?: string;
|
||||
is_active: boolean;
|
||||
is_superuser?: boolean;
|
||||
created_at: string;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user