SettingsPage.tsx
10.4 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import { AlertTriangle, Check, Eraser, RotateCcw, Save, Trash2 } from "lucide-react";
import { useEffect, useState } from "react";
import { api } from "@/lib/api";
import { cn } from "@/lib/utils";
import { toast } from "@/store/useAppStore";
import { Badge, Card, CardHeader, Confirm, ErrorNote, Skeleton } from "@/components/ui";
import type { ConfigField, ConfigResponse, OntologyResponse } from "@/lib/types";
const GROUP_ORDER = ["模型", "分块", "抽取", "消歧", "社区", "检索"];
export function SettingsPage({ onChanged }: { onChanged?: () => void }) {
const [cfg, setCfg] = useState<ConfigResponse | null>(null);
const [onto, setOnto] = useState<OntologyResponse | null>(null);
const [draft, setDraft] = useState<Record<string, string>>({});
const [error, setError] = useState<string | null>(null);
const [saving, setSaving] = useState(false);
const [confirmClear, setConfirmClear] = useState(false);
const load = () => {
setError(null);
Promise.all([api.config(), api.ontology()])
.then(([c, o]) => { setCfg(c); setOnto(o); setDraft({}); })
.catch((e) => setError(e instanceof Error ? e.message : "加载失败"));
};
useEffect(load, []);
const dirty = Object.keys(draft).length > 0;
const save = async () => {
if (!dirty) return;
setSaving(true);
try {
const r = await api.saveConfig(draft);
toast("ok", `已保存 ${r.saved.length} 项参数`, "下次抽取 / 检索即时生效");
load();
onChanged?.();
} catch (e) {
toast("error", "保存失败", e instanceof Error ? e.message : undefined);
} finally {
setSaving(false);
}
};
if (error) return <ErrorNote onRetry={load}>{error}</ErrorNote>;
if (!cfg || !onto) {
return <div className="space-y-3">
{Array.from({ length: 4 }).map((_, i) => <Skeleton key={i} className="h-44 w-full rounded-xl" />)}
</div>;
}
const groups = GROUP_ORDER.filter((g) => cfg.groups[g]?.length);
return (
<div className="space-y-3 pb-16">
{groups.map((g) => (
<Card key={g}>
<CardHeader title={g} desc={GROUP_DESC[g]} />
<div className="grid gap-4 p-4 sm:grid-cols-2 xl:grid-cols-3">
{cfg.groups[g].map((f) => (
<Field key={f.key} field={f}
value={draft[f.key] ?? f.value}
changed={draft[f.key] != null && draft[f.key] !== f.value}
onChange={(v) => setDraft((d) => {
const next = { ...d };
if (v === f.value) delete next[f.key]; else next[f.key] = v;
return next;
})} />
))}
</div>
</Card>
))}
<OntologyCard onto={onto} onSaved={load} />
<Card className="border-bad/30">
<CardHeader title="危险操作"
desc="这些操作不可撤销,请确认后再执行"
right={<AlertTriangle className="h-4 w-4 text-bad" />} />
<div className="flex flex-wrap items-center gap-2 p-4">
<button className="btn" onClick={async () => {
try { await api.clearCache(); toast("ok", "已清空 LLM 与向量缓存"); }
catch (e) { toast("error", "清空失败", e instanceof Error ? e.message : undefined); }
}}>
<Eraser className="h-3.5 w-3.5" />清空模型缓存
</button>
<span className="text-xs text-muted">缓存只影响成本与速度,不影响结果正确性。</span>
<button className="btn btn-danger ml-auto" onClick={() => setConfirmClear(true)}>
<Trash2 className="h-3.5 w-3.5" />清空整个知识库
</button>
</div>
</Card>
{/* 固定保存条 */}
{dirty && (
<div className="fixed bottom-4 left-1/2 z-40 flex -translate-x-1/2 items-center gap-3
rounded-xl border border-line bg-surface px-4 py-2.5 shadow-lg">
<span className="text-[13px]">
有 <b className="tabular-nums">{Object.keys(draft).length}</b> 项修改未保存
</span>
<button className="btn btn-sm" onClick={() => setDraft({})}>
<RotateCcw className="h-3.5 w-3.5" />放弃
</button>
<button className="btn btn-primary btn-sm" onClick={save} disabled={saving}>
<Save className="h-3.5 w-3.5" />{saving ? "保存中…" : "保存"}
</button>
</div>
)}
<Confirm open={confirmClear} onClose={() => setConfirmClear(false)} danger confirmText="清空"
title="清空整个知识库?"
body="会删除全部实体、关系、社区与索引,文档记录会被重置为「待抽取」。原始文件保留,可以重新抽取。"
onConfirm={async () => {
try {
await api.clearGraph();
toast("ok", "知识库已清空");
onChanged?.();
} catch (e) {
toast("error", "清空失败", e instanceof Error ? e.message : undefined);
}
}} />
</div>
);
}
const GROUP_DESC: Record<string, string> = {
模型: "对话 / 向量 / 重排模型与缓存开关",
分块: "决定每次喂给大模型的文本粒度,也直接影响抽取成本",
抽取: "并发、补抽轮数与噪声过滤强度",
消歧: "把「主机厂」和「主机厂侧」这类同义实体合并的判定阈值",
社区: "Louvain 分区与主题摘要,支撑概览类问题",
检索: "召回数量、图谱扩展策略与上下文预算",
};
function Field({ field, value, changed, onChange }: {
field: ConfigField; value: string; changed: boolean; onChange: (v: string) => void;
}) {
const id = `cfg-${field.key}`;
return (
<div className={cn("rounded-lg p-2 -m-2 transition-colors", changed && "bg-brand/5")}>
<label htmlFor={id} className="mb-1 flex items-center gap-1.5 text-xs font-medium">
{field.label}
{changed && <Badge tone="bg-brand/10 text-brand">已修改</Badge>}
</label>
{field.type === "bool" ? (
<button id={id} role="switch" aria-checked={value === "1"}
onClick={() => onChange(value === "1" ? "0" : "1")}
className={cn("relative h-5 w-9 rounded-full transition-colors",
value === "1" ? "bg-brand" : "bg-ink/15")}>
<span className={cn("absolute top-0.5 h-4 w-4 rounded-full bg-white transition-transform",
value === "1" ? "translate-x-[1.125rem]" : "translate-x-0.5")} />
</button>
) : field.type === "enum" ? (
<select id={id} className="input" value={value} onChange={(e) => onChange(e.target.value)}>
{(field.options ?? []).map((o) => <option key={o} value={o}>{o}</option>)}
</select>
) : field.type === "int" || field.type === "float" ? (
<div className="flex items-center gap-2">
<input id={id} type="number" className="input" value={value}
min={field.min} max={field.max} step={field.type === "float" ? 0.01 : 1}
onChange={(e) => onChange(e.target.value)} />
{field.min != null && field.max != null && (
<span className="shrink-0 text-[10.5px] tabular-nums text-muted">{field.min}~{field.max}</span>
)}
</div>
) : (
<input id={id} className="input" value={value} onChange={(e) => onChange(e.target.value)} />
)}
{field.help && <p className="mt-1 text-[11px] leading-relaxed text-muted">{field.help}</p>}
<code className="mt-1 block font-mono text-[10px] text-muted/60">{field.key}</code>
</div>
);
}
function OntologyCard({ onto, onSaved }: { onto: OntologyResponse; onSaved: () => void }) {
const [ent, setEnt] = useState<string[]>(onto.entity_types);
const [rel, setRel] = useState<string[]>(onto.relation_types);
const [saving, setSaving] = useState(false);
useEffect(() => { setEnt(onto.entity_types); setRel(onto.relation_types); }, [onto]);
const toggle = (list: string[], set: (v: string[]) => void, name: string) =>
set(list.includes(name) ? list.filter((x) => x !== name) : [...list, name]);
const save = async () => {
setSaving(true);
try {
await api.saveOntology(ent, rel);
toast("ok", "本体已保存", "下次抽取生效");
onSaved();
} catch (e) {
toast("error", "保存失败", e instanceof Error ? e.message : undefined);
} finally {
setSaving(false);
}
};
const dirty = ent.join() !== onto.entity_types.join() || rel.join() !== onto.relation_types.join();
return (
<Card>
<CardHeader
title="本体类型"
desc="抽取时只允许使用这里勾选的类型。闭集是有意为之 —— 放开自由扩展会让类型数量失控,图谱着色和类型过滤都会失效。"
right={<button className="btn btn-primary btn-sm" onClick={save} disabled={!dirty || saving}>
<Check className="h-3.5 w-3.5" />{saving ? "保存中…" : "保存本体"}</button>}
/>
<div className="space-y-4 p-4">
<div>
<p className="label">实体类型(已选 {ent.length} / {onto.available_entity_types.length})</p>
<div className="flex flex-wrap gap-1.5">
{onto.available_entity_types.map((t) => (
<button key={t.name}
title={[t.desc, t.example && `如:${t.example}`].filter(Boolean).join("\n")}
className={cn("chip", !ent.includes(t.name) && "chip-off",
t.name === "其他" && "cursor-not-allowed")}
onClick={() => t.name !== "其他" && toggle(ent, setEnt, t.name)}>
{t.name}
</button>
))}
</div>
<p className="mt-1.5 text-[11px] text-muted">「其他」是兜底类目,不可取消 —— 否则模型会自造类型。</p>
</div>
<div>
<p className="label">关系类型(已选 {rel.length} / {onto.available_relation_types.length})</p>
<div className="flex flex-wrap gap-1.5">
{onto.available_relation_types.map((t) => (
<button key={t.name} title={t.desc}
className={cn("chip", !rel.includes(t.name) && "chip-off",
t.name === "关联" && "cursor-not-allowed")}
onClick={() => t.name !== "关联" && toggle(rel, setRel, t.name)}>
{t.name}
</button>
))}
</div>
</div>
</div>
</Card>
);
}