utils.ts
3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { clsx, type ClassValue } from "clsx";
export const cn = (...inputs: ClassValue[]) => clsx(inputs);
export function formatBytes(n: number): string {
if (!n) return "0 B";
const units = ["B", "KB", "MB", "GB"];
const i = Math.min(units.length - 1, Math.floor(Math.log(n) / Math.log(1024)));
return `${(n / 1024 ** i).toFixed(i === 0 ? 0 : 1)} ${units[i]}`;
}
export function formatDuration(ms: number): string {
if (ms < 1000) return `${ms} ms`;
if (ms < 60_000) return `${(ms / 1000).toFixed(1)} 秒`;
const m = Math.floor(ms / 60_000);
return `${m} 分 ${Math.round((ms % 60_000) / 1000)} 秒`;
}
export function formatTime(iso?: string | null): string {
if (!iso) return "—";
const d = new Date(iso);
if (Number.isNaN(d.getTime())) return "—";
const now = Date.now();
const diff = now - d.getTime();
if (diff < 60_000) return "刚刚";
if (diff < 3_600_000) return `${Math.floor(diff / 60_000)} 分钟前`;
if (diff < 86_400_000) return `${Math.floor(diff / 3_600_000)} 小时前`;
return d.toLocaleString("zh-CN", { month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit" });
}
export function truncate(s: string, n: number): string {
return s.length > n ? `${s.slice(0, n)}…` : s;
}
/** 章节路径 "A > B > C" 只显示最后两级,避免把工具条撑爆 */
export function shortHeading(h?: string, keep = 2): string {
if (!h) return "正文";
const parts = h.split(" > ").filter(Boolean);
return parts.length <= keep ? parts.join(" / ") : `… / ${parts.slice(-keep).join(" / ")}`;
}
export function downloadText(filename: string, text: string, mime = "application/json") {
const url = URL.createObjectURL(new Blob([text], { type: mime }));
const a = document.createElement("a");
a.href = url;
a.download = filename;
a.click();
setTimeout(() => URL.revokeObjectURL(url), 1000);
}
export const STATUS_TEXT: Record<string, string> = {
uploaded: "待抽取", queued: "排队中", parsing: "解析中", extracting: "抽取中",
done: "已完成", failed: "失败", running: "进行中", cancelled: "已取消",
};
export const STATUS_TONE: Record<string, string> = {
uploaded: "bg-brand/10 text-brand",
queued: "bg-warn/10 text-warn",
parsing: "bg-warn/10 text-warn",
extracting: "bg-warn/10 text-warn",
running: "bg-warn/10 text-warn",
done: "bg-ok/10 text-ok",
failed: "bg-bad/10 text-bad",
cancelled: "bg-muted/10 text-muted",
};
export const MODE_INFO: Record<string, { name: string; desc: string }> = {
agentic: { name: "透明深挖", desc: "多轮 agentic 深挖,每一步都可见可溯源(本系统差异化)" },
mix: { name: "混合全量", desc: "实体 + 关系 + 原文全都召回,覆盖最广" },
hybrid: { name: "实体+关系", desc: "LightRAG hybrid:低层实体与高层关系两路召回" },
local: { name: "实体中心", desc: "LightRAG local:从具体实体出发看它的邻域" },
global: { name: "关系中心", desc: "LightRAG global:从关系向量库出发,适合主题类问题" },
naive: { name: "朴素检索", desc: "不走图谱,纯原文向量 + BM25,作为对照基线" },
};