Skip to content

LLM Wiki is a cross-platform desktop application that turns your documents into an organized, interlinked knowledge base — automatically. Instead of traditional RAG (retrieve-and-answer from scratch every time), the LLM incrementally builds and maintains a persistent wiki from your sources。

TypeScript

perf: index retrieval graph link aliases

nash_succ81bce6e28b
diff --git a/src/lib/graph-relevance.test.ts b/src/lib/graph-relevance.test.ts
index 389fcb9e..cdf1254c 100644
--- a/src/lib/graph-relevance.test.ts
+++ b/src/lib/graph-relevance.test.ts
@@ -71,4 +71,96 @@ describe("buildRetrievalGraph", () => {
expect(graph.nodes.get("page-0")?.outLinks).toContain("page-1")
expect(graph.nodes.get("page-1")?.inLinks).toContain("page-0")
})
+
+ it("resolves case and whitespace aliases through the prebuilt index", async () => {
+ listDirectory.mockResolvedValue([
+ { name: "source.md", path: "/project/wiki/source.md", is_dir: false },
+ { name: "page-00042.md", path: "/project/wiki/page-00042.md", is_dir: false },
+ ])
+ readFile.mockImplementation(async (path: string) => path.endsWith("source.md")
+ ? "# Source\n\n[[Page 00042]]"
+ : "# Target")
+
+ const graph = await buildRetrievalGraph("/project", 3)
+
+ expect(graph.nodes.get("source")?.outLinks).toEqual(new Set(["page-00042"]))
+ expect(graph.nodes.get("page-00042")?.inLinks).toEqual(new Set(["source"]))
+ })
+
+ it("keeps exact IDs ahead of normalized aliases", async () => {
+ listDirectory.mockResolvedValue([
+ { name: "source.md", path: "/project/wiki/source.md", is_dir: false },
+ { name: "Page 1.md", path: "/project/wiki/Page 1.md", is_dir: false },
+ { name: "page-1.md", path: "/project/wiki/page-1.md", is_dir: false },
+ ])
+ readFile.mockImplementation(async (path: string) => path.endsWith("source.md")
+ ? "# Source\n\n[[page-1]]"
+ : "# Target")
+
+ const graph = await buildRetrievalGraph("/project", 4)
+
+ expect(graph.nodes.get("source")?.outLinks).toEqual(new Set(["page-1"]))
+ })
+
+ it.each([
+ [["foo-bar.md", "foo bar.md"], "foo-bar"],
+ [["foo bar.md", "foo-bar.md"], "foo bar"],
+ ] as const)("keeps the first listed node for ambiguous aliases in order %j", async (names, expected) => {
+ listDirectory.mockResolvedValue([
+ { name: "source.md", path: "/project/wiki/source.md", is_dir: false },
+ ...names.map((name) => ({ name, path: `/project/wiki/${name}`, is_dir: false })),
+ ])
+ readFile.mockImplementation(async (path: string) => path.endsWith("source.md")
+ ? "# Source\n\n[[FOO BAR]]"
+ : "# Target")
+
+ const graph = await buildRetrievalGraph("/project", 5)
+
+ expect(graph.nodes.get("source")?.outLinks).toEqual(new Set([expected]))
+ })
+
+ it("collapses repeated whitespace and filters self-links resolved through aliases", async () => {
+ listDirectory.mockResolvedValue([
+ { name: "source-page.md", path: "/project/wiki/source-page.md", is_dir: false },
+ { name: "target-page.md", path: "/project/wiki/target-page.md", is_dir: false },
+ ])
+ readFile.mockImplementation(async (path: string) => path.endsWith("source-page.md")
+ ? "# Source\n\n[[SOURCE PAGE]] [[Target Page]]"
+ : "# Target")
+
+ const graph = await buildRetrievalGraph("/project", 6)
+
+ expect(graph.nodes.get("source-page")?.outLinks).toEqual(new Set(["target-page"]))
+ expect(graph.nodes.get("source-page")?.inLinks.size).toBe(0)
+ })
+
+ it("resolves frontmatter related aliases and non-ASCII whitespace", async () => {
+ listDirectory.mockResolvedValue([
+ { name: "source-page.md", path: "/project/wiki/source-page.md", is_dir: false },
+ { name: "target-page.md", path: "/project/wiki/target-page.md", is_dir: false },
+ ])
+ readFile.mockImplementation(async (path: string) => path.endsWith("source-page.md")
+ ? "---\nrelated: [Target\u00a0Page]\n---\n# Source"
+ : "# Target")
+
+ const graph = await buildRetrievalGraph("/project", 7)
+
+ expect(graph.nodes.get("source-page")?.outLinks).toEqual(new Set(["target-page"]))
+ expect(graph.nodes.get("target-page")?.inLinks).toEqual(new Set(["source-page"]))
+ })
+
+ it("does not create edges for many missing aliases", async () => {
+ listDirectory.mockResolvedValue([
+ { name: "source.md", path: "/project/wiki/source.md", is_dir: false },
+ ...files("/project", 1_000),
+ ])
+ readFile.mockImplementation(async (path: string) => path.endsWith("source.md")
+ ? `# Source\n\n${Array.from({ length: 1_000 }, (_, i) => `[[missing ${i}]]`).join(" ")}`
+ : "# Target")
+
+ const graph = await buildRetrievalGraph("/project", 8)
+
+ expect(graph.nodes.get("source")?.outLinks.size).toBe(0)
+ expect([...graph.nodes.values()].every((node) => node.inLinks.size === 0)).toBe(true)
+ })
})
diff --git a/src/lib/graph-relevance.ts b/src/lib/graph-relevance.ts
index 9bf6c5e1..5fca8712 100644
--- a/src/lib/graph-relevance.ts
+++ b/src/lib/graph-relevance.ts
@@ -160,20 +160,28 @@ function extractWikilinks(content: string): string[] {
return links
}
-function resolveTarget(
- raw: string,
- nodeIds: ReadonlySet<string>,
-): string | null {
- if (nodeIds.has(raw)) return raw
+interface TargetResolver {
+ readonly nodeIds: ReadonlySet<string>
+ readonly aliases: ReadonlyMap<string, string>
+}
+
+function normalizeLinkKey(value: string): string {
+ return value.toLowerCase().replace(/\s+/g, "-")
+}
- const normalized = raw.toLowerCase().replace(/\s+/g, "-")
+function buildTargetResolver(nodeIds: ReadonlySet<string>): TargetResolver {
+ const aliases = new Map<string, string>()
for (const id of nodeIds) {
- const idLower = id.toLowerCase()
- if (idLower === normalized) return id
- if (idLower === raw.toLowerCase()) return id
- if (idLower.replace(/\s+/g, "-") === normalized) return id
+ const normalized = normalizeLinkKey(id)
+ // Preserve the original directory-listing order for alias collisions.
+ if (!aliases.has(normalized)) aliases.set(normalized, id)
}
- return null
+ return { nodeIds, aliases }
+}
+
+function resolveTarget(raw: string, resolver: TargetResolver): string | null {
+ if (resolver.nodeIds.has(raw)) return raw
+ return resolver.aliases.get(normalizeLinkKey(raw)) ?? null
}
function getNeighbors(node: RetrievalNode): ReadonlySet<string> {
@@ -249,6 +257,7 @@ export async function buildRetrievalGraph(
const rawNodes = parsedFiles.filter((node): node is RawNode => node !== null)
const nodeIds = new Set(rawNodes.map((n) => n.id))
+ const targetResolver = buildTargetResolver(nodeIds)
// Second pass: resolve links and build graph nodes
const outLinksMap = new Map<string, Set<string>>()
@@ -261,7 +270,7 @@ export async function buildRetrievalGraph(
for (const raw of rawNodes) {
for (const linkTarget of raw.rawLinks) {
- const resolvedId = resolveTarget(linkTarget, nodeIds)
+ const resolvedId = resolveTarget(linkTarget, targetResolver)
if (resolvedId === null || resolvedId === raw.id) continue
outLinksMap.get(raw.id)!.add(resolvedId)
inLinksMap.get(resolvedId)!.add(raw.id)