refactor(kb): 文库列表/正文分离 + [[双链]]索引表(可扛大文件)

不再一次拉回整库正文、不再前端扫全文 —— 列表只读元数据,正文按需取,链接走索引。

- store: SaveDoc 维护 size+preview(前 500 字);ListVault 仅 Select 元数据(name/size/preview,
  不含 content);GetDoc 取单篇全文;DocLink 表 + ReplaceDocLinks(入库/编辑时按 from 重建出链)
  + ListLinks。
- gateway: 入库/笔记保存时正则抽 [[链接]]→ReplaceDocLinks 维护索引;
  /kb/vault 改返元数据+预览;新增 /kb/doc(单篇全文) 与 /kb/links(全库双链)。
- 前端:listVault 返元数据,新增 getDoc/listLinks;VaultPanel 列表只展示名/字数,
  选中后 getDoc 按需载正文(带加载态),反链/笔记关系图改用服务端 links 索引(不扫全文)。

验证:curl /kb/vault 仅 name/size/preview;/kb/doc 取单篇;/kb/links 返 3 条双链。
Preview:文库点「架构总览」按需载正文(平台分五层)、反向链接(1)=Dispatcher(来自索引)。tsc+vite+gateway build 通过。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Blizzard
2026-06-13 16:53:23 +08:00
parent 4fd44380aa
commit 69967ea534
5 changed files with 174 additions and 46 deletions
+23 -2
View File
@@ -123,7 +123,12 @@ export async function createKb(id: Identity, name: string, kind: string): Promis
export interface VaultDoc {
name: string;
content: string;
size?: number;
preview?: string;
}
export interface DocLink {
from: string;
to: string;
}
// ---- 我的 Agent 编排(服务端保存,owner 隔离)----
@@ -169,7 +174,7 @@ export async function listChatModels(): Promise<string[]> {
}
}
// listVault: GET /api/v1/kb/vault —— 某知识库的原始文档(Obsidian 式文库浏览)。
// listVault: GET /api/v1/kb/vault —— 文库列表(仅元数据 + 预览,不拉全文)。
export async function listVault(id: Identity, kb: string): Promise<VaultDoc[]> {
const res = await fetch(`${GATEWAY}/api/v1/kb/vault?kb=${encodeURIComponent(kb)}`, { headers: idHeaders(id) });
const data = (await res.json()) as { docs?: VaultDoc[]; error?: string };
@@ -177,6 +182,22 @@ export async function listVault(id: Identity, kb: string): Promise<VaultDoc[]> {
return data.docs ?? [];
}
// getDoc: GET /api/v1/kb/doc —— 取单篇文档全文(按需加载,避免列表拉全量)。
export async function getDoc(id: Identity, kb: string, name: string): Promise<string> {
const res = await fetch(`${GATEWAY}/api/v1/kb/doc?kb=${encodeURIComponent(kb)}&name=${encodeURIComponent(name)}`, { headers: idHeaders(id) });
const data = (await res.json()) as { content?: string; error?: string };
if (!res.ok) throw new Error(data.error ?? `doc failed: ${res.status}`);
return data.content ?? "";
}
// listLinks: GET /api/v1/kb/links —— 某库全部 [[双链]](反链/笔记关系图,数据小)。
export async function listLinks(id: Identity, kb: string): Promise<DocLink[]> {
const res = await fetch(`${GATEWAY}/api/v1/kb/links?kb=${encodeURIComponent(kb)}`, { headers: idHeaders(id) });
const data = (await res.json()) as { links?: DocLink[]; error?: string };
if (!res.ok) throw new Error(data.error ?? `links failed: ${res.status}`);
return data.links ?? [];
}
// saveNote: POST /api/v1/kb/note —— 新建/编辑笔记(落库 + 按 doc 重入库替换旧块)。
export async function saveNote(id: Identity, kb: string, name: string, content: string): Promise<void> {
const res = await fetch(`${GATEWAY}/api/v1/kb/note`, {
+40 -34
View File
@@ -34,12 +34,15 @@ import {
listKb,
createKb,
listVault,
getDoc,
listLinks,
saveNote,
type IngestEvent,
type KbHit,
type Triple,
type KbInfo,
type VaultDoc,
type DocLink,
type Identity,
} from "../lib/api";
import { GraphView } from "../components/GraphView";
@@ -431,24 +434,15 @@ export function KbView({ identity }: { identity: Identity }) {
);
}
function escapeReg(s: string): string {
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
// wikiLinks 从内容中抽出所有 [[名称]](忽略别名部分)。
function wikiLinks(content: string): string[] {
const out: string[] = [];
const re = /\[\[([^\]|]+)(\|[^\]]*)?\]\]/g;
let m: RegExpExecArray | null;
while ((m = re.exec(content)) !== null) out.push(m[1].trim());
return out;
}
// VaultPanelObsidian 式文库 —— 文档列表 / Markdown 阅读+编辑([[双链]]可点)/ 反向链接 / 笔记关系图。
// VaultPanelObsidian 式文库 —— 列表(仅元数据) / 正文按需加载 / [[双链]]可点 / 反链 / 笔记关系图。
// 列表与正文分离 + 链接走服务端索引,不再一次拉回整库正文,可扛十几万字大文件。
function VaultPanel({ identity, kb }: { identity: Identity; kb: string }) {
const toast = useToast();
const [docs, setDocs] = useState<VaultDoc[]>([]);
const [links, setLinks] = useState<DocLink[]>([]);
const [sel, setSel] = useState<string | null>(null);
const [content, setContent] = useState("");
const [loadingDoc, setLoadingDoc] = useState(false);
const [loading, setLoading] = useState(false);
const [mode, setMode] = useState<"read" | "graph">("read");
const [editing, setEditing] = useState(false);
@@ -460,11 +454,13 @@ function VaultPanel({ identity, kb }: { identity: Identity; kb: string }) {
const load = useCallback(async () => {
setLoading(true);
try {
const d = await listVault(identity, kb);
const [d, lk] = await Promise.all([listVault(identity, kb), listLinks(identity, kb)]);
setDocs(d);
setLinks(lk);
setSel((s) => (d.some((x) => x.name === s) ? s : d[0]?.name ?? null));
} catch {
setDocs([]);
setLinks([]);
} finally {
setLoading(false);
}
@@ -475,6 +471,20 @@ function VaultPanel({ identity, kb }: { identity: Identity; kb: string }) {
setMode("read");
}, [load]);
// 选中文档时按需取全文(不随列表一起拉)。
useEffect(() => {
if (!sel || editing) return;
let alive = true;
setLoadingDoc(true);
getDoc(identity, kb, sel)
.then((c) => alive && setContent(c))
.catch(() => alive && setContent(""))
.finally(() => alive && setLoadingDoc(false));
return () => {
alive = false;
};
}, [sel, kb, identity, editing]);
const names = new Set(docs.map((d) => d.name));
const current = docs.find((d) => d.name === sel);
const open = (name: string) => {
@@ -484,17 +494,9 @@ function VaultPanel({ identity, kb }: { identity: Identity; kb: string }) {
setEditing(false);
}
};
const backlinks = current
? docs.filter((d) => d.name !== current.name && new RegExp(`\\[\\[\\s*${escapeReg(current.name)}(\\|[^\\]]*)?\\s*\\]\\]`).test(d.content))
: [];
// 笔记关系图:文档间 [[双链]] → 三元组(仅保留指向已存在笔记的边)。
const noteTriples: Triple[] = [];
for (const d of docs) {
for (const link of wikiLinks(d.content)) {
if (names.has(link) && link !== d.name) noteTriples.push({ s: d.name, p: "链接", o: link });
}
}
// 反链/笔记关系图来自服务端 [[双链]]索引,不扫全文。
const backlinks = sel ? [...new Set(links.filter((l) => l.to === sel).map((l) => l.from))] : [];
const noteTriples: Triple[] = links.filter((l) => names.has(l.from) && names.has(l.to)).map((l) => ({ s: l.from, p: "链接", o: l.to }));
const startNew = () => {
setCreatingNew(true);
@@ -507,7 +509,7 @@ function VaultPanel({ identity, kb }: { identity: Identity; kb: string }) {
setCreatingNew(false);
setEditing(true);
setDraftName(current.name);
setDraft(current.content);
setDraft(content);
};
const onSave = async () => {
const name = (creatingNew ? draftName : current?.name ?? "").trim();
@@ -521,10 +523,9 @@ function VaultPanel({ identity, kb }: { identity: Identity; kb: string }) {
toast.push("success", `已保存「${name}」(正在重建索引)`);
setEditing(false);
setCreatingNew(false);
// 乐观更新本地 + 选中,再后台刷新。
setDocs((ds) => [...ds.filter((x) => x.name !== name), { name, content: draft }]);
setContent(draft);
setSel(name);
setTimeout(() => void load(), 300);
setTimeout(() => void load(), 400);
} catch (e) {
toast.push("error", (e as Error).message);
} finally {
@@ -609,8 +610,13 @@ function VaultPanel({ identity, kb }: { identity: Identity; kb: string }) {
<h2 className="mb-3 flex items-center gap-2 text-base font-semibold text-slate-100">
<FileText className="h-4 w-4 text-brand-400" />
{current.name}
{current.size != null && <span className="text-[11px] font-normal text-slate-600">{current.size} </span>}
</h2>
<Markdown text={current.content} className="text-sm" onLink={open} />
{loadingDoc ? (
<div className="flex items-center gap-1.5 text-xs text-slate-500"><Loader2 className="h-3.5 w-3.5 animate-spin" /> </div>
) : (
<Markdown text={content} className="text-sm" onLink={open} />
)}
<div className="mt-6 border-t border-line pt-3">
<div className="mb-2 flex items-center gap-1.5 text-xs font-medium text-slate-400">
<Link2 className="h-3.5 w-3.5" /> {backlinks.length}
@@ -620,9 +626,9 @@ function VaultPanel({ identity, kb }: { identity: Identity; kb: string }) {
) : (
<ul className="space-y-1">
{backlinks.map((b) => (
<li key={b.name}>
<button onClick={() => open(b.name)} className="text-xs text-brand-400 hover:underline">
{b.name}
<li key={b}>
<button onClick={() => open(b)} className="text-xs text-brand-400 hover:underline">
{b}
</button>
</li>
))}