223 lines
8.9 KiB
TypeScript
223 lines
8.9 KiB
TypeScript
import { userFacingKnowledgeMessage } from "@/lib/knowledge-message";
|
|
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
import {
|
|
Archive,
|
|
ArrowLeft,
|
|
Download,
|
|
HardDrive,
|
|
Loader2,
|
|
PackagePlus,
|
|
RefreshCw,
|
|
} from "lucide-react";
|
|
import { Navigate, useNavigate, useParams } from "react-router-dom";
|
|
import { toast } from "sonner";
|
|
|
|
import { Badge } from "@/components/ui/badge";
|
|
import { Button } from "@/components/ui/button";
|
|
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
|
|
import { KnowledgeProductNav } from "@/components/workspace/knowledge-product-nav";
|
|
import { getStoredAuth } from "@/core/auth";
|
|
import {
|
|
createLlmWikiExportPackage,
|
|
listLlmWikiExportPackages,
|
|
} from "@/strategy-components/api/assistant-knowledge";
|
|
import { useLlmWikiKnowledgeBase, useLlmWikiRuntime } from "@/strategy-components/api/llmwiki";
|
|
import { WikiPackageTransfers } from "./WikiPackageTransfers";
|
|
|
|
const RUNNING = new Set(["queued", "running"]);
|
|
const STATUS_LABEL: Record<string, string> = {
|
|
queued: "排队中",
|
|
running: "整理中",
|
|
completed: "已完成",
|
|
failed: "失败",
|
|
};
|
|
|
|
function formatBytes(value?: number | null) {
|
|
let current = Number(value || 0);
|
|
if (!current) return "—";
|
|
const units = ["B", "KB", "MB", "GB", "TB"];
|
|
let unit = 0;
|
|
while (current >= 1024 && unit < units.length - 1) {
|
|
current /= 1024;
|
|
unit += 1;
|
|
}
|
|
return `${current >= 10 || unit === 0 ? current.toFixed(0) : current.toFixed(1)} ${units[unit]}`;
|
|
}
|
|
|
|
export default function WeKnoraPackageTransfersPage() {
|
|
const { noteId = "" } = useParams();
|
|
const navigate = useNavigate();
|
|
const queryClient = useQueryClient();
|
|
const isAdmin = getStoredAuth()?.system_role === "admin";
|
|
const runtime = useLlmWikiRuntime();
|
|
const knowledge = useLlmWikiKnowledgeBase(noteId || null, !!noteId);
|
|
const jobsKey = ["llmwiki-export-packages", noteId];
|
|
const jobs = useQuery({
|
|
queryKey: jobsKey,
|
|
queryFn: () => listLlmWikiExportPackages(noteId),
|
|
enabled: isAdmin && !!noteId && runtime.data?.provider === "weknora",
|
|
refetchInterval: (query) =>
|
|
query.state.data?.some((job) => RUNNING.has(job.status)) ? 3000 : false,
|
|
});
|
|
const createExport = useMutation({
|
|
mutationFn: () => createLlmWikiExportPackage(noteId),
|
|
onSuccess: () => {
|
|
void queryClient.invalidateQueries({ queryKey: jobsKey });
|
|
toast.success("已创建后台整理任务,完成后可在本页下载");
|
|
},
|
|
onError: (error) => toast.error(error.message),
|
|
});
|
|
if (!isAdmin) return <Navigate to="/page/workspace/knowledge" replace />;
|
|
|
|
return (
|
|
<div className="flex size-full min-h-0 flex-col bg-muted/20">
|
|
<KnowledgeProductNav />
|
|
<main className="min-h-0 flex-1 overflow-auto px-5 py-6 lg:px-8">
|
|
<div className="mx-auto max-w-6xl space-y-6">
|
|
<header className="flex flex-wrap items-center justify-between gap-4">
|
|
<div className="flex min-w-0 items-center gap-3">
|
|
<Button
|
|
variant="ghost"
|
|
size="icon"
|
|
onClick={() =>
|
|
navigate(
|
|
`/page/workspace/knowledge/${encodeURIComponent(noteId)}`,
|
|
)
|
|
}
|
|
>
|
|
<ArrowLeft className="size-5" />
|
|
</Button>
|
|
<div className="flex size-11 shrink-0 items-center justify-center rounded-xl bg-primary/10 text-primary">
|
|
<Archive className="size-5" />
|
|
</div>
|
|
<div className="min-w-0">
|
|
<h1 className="truncate text-xl font-semibold">
|
|
知识库数据包任务
|
|
</h1>
|
|
<p className="truncate text-sm text-muted-foreground">
|
|
{knowledge.data?.name || "正在读取知识库"} ·
|
|
大文件在后台整理,不占用详情页面
|
|
</p>
|
|
</div>
|
|
</div>
|
|
<Button
|
|
onClick={() => createExport.mutate()}
|
|
disabled={createExport.isPending || !knowledge.data}
|
|
>
|
|
{createExport.isPending ? (
|
|
<Loader2 className="mr-2 size-4 animate-spin" />
|
|
) : (
|
|
<PackagePlus className="mr-2 size-4" />
|
|
)}
|
|
新建导出任务
|
|
</Button>
|
|
</header>
|
|
|
|
<Card>
|
|
<CardHeader className="flex-row items-center justify-between space-y-0 border-b">
|
|
<div>
|
|
<CardTitle className="text-base">
|
|
向量化数据包导出记录
|
|
</CardTitle>
|
|
<p className="mt-1 text-sm text-muted-foreground">
|
|
每次生成独立记录;即使文件达到数
|
|
GB,也只在后台落盘完成后提供下载。
|
|
</p>
|
|
</div>
|
|
<Button
|
|
variant="ghost"
|
|
size="sm"
|
|
onClick={() => void jobs.refetch()}
|
|
disabled={jobs.isFetching}
|
|
>
|
|
<RefreshCw
|
|
className={`mr-2 size-4 ${jobs.isFetching ? "animate-spin" : ""}`}
|
|
/>
|
|
刷新
|
|
</Button>
|
|
</CardHeader>
|
|
<CardContent className="p-0">
|
|
{jobs.data?.map((job) => {
|
|
const counts = job.counts || {};
|
|
const ready =
|
|
job.status === "completed" &&
|
|
job.file_exists &&
|
|
job.package_download_url;
|
|
return (
|
|
<div
|
|
key={job.id}
|
|
className="grid gap-4 border-b p-5 last:border-b-0 md:grid-cols-[auto_1fr_auto] md:items-center"
|
|
>
|
|
<div
|
|
className={`flex size-10 items-center justify-center rounded-full ${job.status === "completed" ? "bg-emerald-100 text-emerald-700" : job.status === "failed" ? "bg-destructive/10 text-destructive" : "bg-amber-100 text-amber-700"}`}
|
|
>
|
|
{RUNNING.has(job.status) ? (
|
|
<Loader2 className="size-4 animate-spin" />
|
|
) : (
|
|
<HardDrive className="size-4" />
|
|
)}
|
|
</div>
|
|
<div className="min-w-0">
|
|
<div className="flex flex-wrap items-center gap-2">
|
|
<span className="font-medium">
|
|
任务 {job.id.slice(0, 8)}
|
|
</span>
|
|
<Badge variant="outline">
|
|
{STATUS_LABEL[job.status] || job.status}
|
|
</Badge>
|
|
<span className="text-xs text-muted-foreground">
|
|
{job.created_at
|
|
? new Date(job.created_at).toLocaleString()
|
|
: "—"}
|
|
</span>
|
|
</div>
|
|
<div className="mt-2 flex flex-wrap gap-x-5 gap-y-1 text-sm text-muted-foreground">
|
|
<span>
|
|
Wiki {counts.wiki_page ?? counts.wiki_pages ?? 0}
|
|
</span>
|
|
<span>向量 {counts.vector ?? counts.vectors ?? 0}</span>
|
|
<span>文件 {formatBytes(job.file_size_bytes)}</span>
|
|
<span>阶段 {job.phase}</span>
|
|
</div>
|
|
{job.error && (
|
|
<p className="mt-2 text-sm text-destructive">
|
|
{userFacingKnowledgeMessage(job.error)}
|
|
</p>
|
|
)}
|
|
</div>
|
|
{ready ? (
|
|
<Button asChild variant="outline">
|
|
<a href={job.package_download_url}>
|
|
<Download className="mr-2 size-4" />
|
|
下载数据包
|
|
</a>
|
|
</Button>
|
|
) : (
|
|
<Button variant="outline" disabled>
|
|
{job.status === "failed" ? "整理失败" : "等待完成"}
|
|
</Button>
|
|
)}
|
|
</div>
|
|
);
|
|
})}
|
|
{!jobs.isLoading && !jobs.data?.length && (
|
|
<div className="px-6 py-14 text-center text-sm text-muted-foreground">
|
|
暂无导出记录。点击右上角“新建导出任务”开始整理。
|
|
</div>
|
|
)}
|
|
{jobs.isLoading && (
|
|
<div className="flex items-center justify-center px-6 py-14 text-sm text-muted-foreground">
|
|
<Loader2 className="mr-2 size-4 animate-spin" />
|
|
加载导出记录
|
|
</div>
|
|
)}
|
|
</CardContent>
|
|
</Card>
|
|
|
|
<WikiPackageTransfers id={noteId} />
|
|
</div>
|
|
</main>
|
|
</div>
|
|
);
|
|
}
|