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

fix: prevent hallucinated wiki citations, validate wikilinks exist

nash_su8155d7ce4acb
diff --git a/src/components/chat/chat-message.tsx b/src/components/chat/chat-message.tsx
index 299d69da..e8ecf2e9 100644
--- a/src/components/chat/chat-message.tsx
+++ b/src/components/chat/chat-message.tsx
@@ -1,4 +1,4 @@
-import { useCallback } from "react"
+import { useCallback, useEffect, useRef, useState } from "react"
import ReactMarkdown from "react-markdown"
import { Bot, User, FileText } from "lucide-react"
import { useWikiStore } from "@/stores/wiki-store"
@@ -109,10 +109,12 @@ function WikiLink({ pageName, children }: { pageName: string; children: React.Re
const setSelectedFile = useWikiStore((s) => s.setSelectedFile)
const setFileContent = useWikiStore((s) => s.setFileContent)
const setActiveView = useWikiStore((s) => s.setActiveView)
+ const [exists, setExists] = useState<boolean | null>(null)
+ const resolvedPath = useRef<string | null>(null)
- const handleClick = useCallback(async () => {
+ // Check if the page exists on mount
+ useEffect(() => {
if (!project) return
- // Try to find the file in common wiki directories
const candidates = [
`${project.path}/wiki/entities/${pageName}.md`,
`${project.path}/wiki/concepts/${pageName}.md`,
@@ -123,20 +125,46 @@ function WikiLink({ pageName, children }: { pageName: string; children: React.Re
`${project.path}/wiki/${pageName}.md`,
]
- for (const path of candidates) {
- try {
- const content = await readFile(path)
- setSelectedFile(path)
- setFileContent(content)
- setActiveView("wiki")
- return
- } catch {
- // try next candidate
+ let cancelled = false
+ async function check() {
+ for (const path of candidates) {
+ try {
+ await readFile(path)
+ if (!cancelled) {
+ resolvedPath.current = path
+ setExists(true)
+ }
+ return
+ } catch {
+ // try next
+ }
}
+ if (!cancelled) setExists(false)
}
- // If not found, just set the name
- console.warn(`Wiki page not found: ${pageName}`)
- }, [project, pageName, setSelectedFile, setFileContent, setActiveView])
+ check()
+ return () => { cancelled = true }
+ }, [project, pageName])
+
+ const handleClick = useCallback(async () => {
+ if (!resolvedPath.current) return
+ try {
+ const content = await readFile(resolvedPath.current)
+ setSelectedFile(resolvedPath.current)
+ setFileContent(content)
+ setActiveView("wiki")
+ } catch {
+ // ignore
+ }
+ }, [setSelectedFile, setFileContent, setActiveView])
+
+ // Page doesn't exist — render as plain text (not clickable)
+ if (exists === false) {
+ return (
+ <span className="inline text-muted-foreground" title={`Page not found: ${pageName}`}>
+ {children}
+ </span>
+ )
+ }
return (
<button
diff --git a/src/components/chat/chat-panel.tsx b/src/components/chat/chat-panel.tsx
index a2798d7d..70481aee 100644
--- a/src/components/chat/chat-panel.tsx
+++ b/src/components/chat/chat-panel.tsx
@@ -51,14 +51,18 @@ export function ChatPanel() {
systemMessages.push({
role: "system",
content: [
- "You are a knowledgeable wiki assistant. Answer questions using the wiki's content.",
- "When you reference wiki pages, use [[wikilink]] syntax (e.g., [[entity-name]], [[concept-name]]).",
- "Always cite which wiki pages your answer draws from.",
+ "You are a knowledgeable wiki assistant. Answer questions based ONLY on the wiki's actual content.",
+ "",
+ "## Citation Rules (CRITICAL)",
+ "- Use [[wikilink]] syntax ONLY for pages that actually exist in the Wiki Index below.",
+ "- NEVER invent or hallucinate page names. If a page doesn't exist in the index, don't link to it.",
+ "- If the wiki doesn't have enough information to answer, say so honestly.",
+ "- At the end of your answer, list the actual wiki pages you referenced under '**Sources:**'",
+ "",
"Use markdown formatting for clarity.",
"",
purpose ? `## Wiki Purpose\n${purpose}` : "",
- schema ? `## Wiki Schema\n${schema}` : "",
- index ? `## Wiki Index\n${index}` : "",
+ index ? `## Wiki Index (ONLY reference pages listed here)\n${index}` : "## Wiki Index\n(No pages yet)",
].filter(Boolean).join("\n"),
})
}