DuplicatesPanel.tsx
4.42 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { ArrowRight, Merge, RefreshCw } from "lucide-react";
import { useEffect, useState } from "react";
import { api } from "@/lib/api";
import { toast } from "@/store/useAppStore";
import { Badge, Empty, ErrorNote, Skeleton } from "@/components/ui";
import type { DuplicatePair } from "@/lib/types";
/**
* 实体消歧工作台 —— LightRAG 没有这一环。
*
* 自动消歧再准也会有边界情况,交付客户时必须给人工确认的入口。
* 这里列出疑似重复对,标明判定依据,一键合并(度数高的那个作为保留者)。
*/
export function DuplicatesPanel({ onMerged }: { onMerged: () => void }) {
const [pairs, setPairs] = useState<DuplicatePair[] | null>(null);
const [error, setError] = useState<string | null>(null);
const [busy, setBusy] = useState<string | null>(null);
const [ignored, setIgnored] = useState<Set<string>>(new Set());
const load = async () => {
setError(null);
setPairs(null);
try {
setPairs(await api.duplicates(200));
} catch (e) {
setError(e instanceof Error ? e.message : "加载失败");
}
};
useEffect(() => { void load(); }, []);
const merge = async (p: DuplicatePair) => {
const key = `${p.a}|${p.b}`;
const [keep, drop] = p.a_degree >= p.b_degree ? [p.a, p.b] : [p.b, p.a];
setBusy(key);
try {
await api.mergeNodes(keep, [drop]);
toast("ok", `已合并:${drop} → ${keep}`);
setPairs((cur) => (cur ?? []).filter((x) => `${x.a}|${x.b}` !== key));
onMerged();
} catch (e) {
toast("error", "合并失败", e instanceof Error ? e.message : undefined);
} finally {
setBusy(null);
}
};
if (error) return <div className="p-3"><ErrorNote onRetry={load}>{error}</ErrorNote></div>;
if (!pairs) {
return (
<div className="space-y-2 p-3">
{Array.from({ length: 5 }).map((_, i) => <Skeleton key={i} className="h-14 w-full" />)}
</div>
);
}
const visible = pairs.filter((p) => !ignored.has(`${p.a}|${p.b}`));
if (!visible.length) {
return (
<Empty icon={<Merge className="h-8 w-8" />} title="没有发现疑似重复实体"
desc="抽取时已经做过自动消歧。新增文档后可以再来复查一次。"
action={<button className="btn btn-sm" onClick={load}>
<RefreshCw className="h-3.5 w-3.5" />重新检测</button>} />
);
}
return (
<div className="flex h-full flex-col">
<div className="flex items-center justify-between border-b border-line px-3 py-2">
<p className="text-xs text-muted">
共 <b className="tabular-nums text-ink">{visible.length}</b> 组疑似重复。合并时保留度数更高的一侧,
另一侧会成为它的别名。
</p>
<button className="btn btn-ghost btn-sm" onClick={load} title="重新检测">
<RefreshCw className="h-3.5 w-3.5" />
</button>
</div>
<div className="flex-1 space-y-1.5 overflow-auto p-3">
{visible.map((p) => {
const key = `${p.a}|${p.b}`;
const [keep, drop] = p.a_degree >= p.b_degree ? [p.a, p.b] : [p.b, p.a];
return (
<div key={key} className="rounded-lg border border-line bg-elevated p-2.5">
<div className="flex flex-wrap items-center gap-1.5 text-[13px]">
<span className="rounded-md bg-ok/10 px-2 py-0.5 font-medium text-ok">{keep}</span>
<ArrowRight className="h-3.5 w-3.5 shrink-0 text-muted" />
<span className="rounded-md bg-ink/5 px-2 py-0.5 text-muted line-through">{drop}</span>
</div>
<div className="mt-1.5 flex flex-wrap items-center gap-1.5">
<Badge tone={p.auto ? "bg-ok/10 text-ok" : "bg-warn/10 text-warn"}>{p.reason}</Badge>
<Badge>度数 {Math.max(p.a_degree, p.b_degree)} / {Math.min(p.a_degree, p.b_degree)}</Badge>
<div className="ml-auto flex gap-1.5">
<button className="btn btn-sm"
onClick={() => setIgnored(new Set([...ignored, key]))}>忽略</button>
<button className="btn btn-primary btn-sm" disabled={busy === key}
onClick={() => merge(p)}>
<Merge className="h-3.5 w-3.5" />{busy === key ? "合并中…" : "合并"}
</button>
</div>
</div>
</div>
);
})}
</div>
</div>
);
}