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

feat: chat now shows cited wiki pages instead of raw source files

nash_su29e2b4e0e1a6
diff --git a/src/components/chat/chat-message.tsx b/src/components/chat/chat-message.tsx
index 32500c7b..898fe1f4 100644
--- a/src/components/chat/chat-message.tsx
+++ b/src/components/chat/chat-message.tsx
@@ -1,9 +1,10 @@
import { useCallback, useEffect, useRef, useState, useMemo } from "react"
import ReactMarkdown from "react-markdown"
import remarkGfm from "remark-gfm"
-import { Bot, User, FileText, Paperclip, BookmarkPlus } from "lucide-react"
+import { Bot, User, FileText, BookmarkPlus } from "lucide-react"
import { useWikiStore } from "@/stores/wiki-store"
import { readFile, writeFile, listDirectory } from "@/commands/fs"
+import { lastQueryPages } from "@/components/chat/chat-panel"
import type { DisplayMessage } from "@/stores/chat-store"
import type { FileNode } from "@/types/wiki"
@@ -186,65 +187,72 @@ function SaveToWikiButton({ content, visible }: { content: string; visible: bool
)
}
+interface CitedPage {
+ title: string
+ path: string
+}
+
function SourceFilesBar({ content }: { content: string }) {
- // Ensure source files are loaded
const project = useWikiStore((s) => s.project)
- const fileTree = useWikiStore((s) => s.fileTree)
-
- useEffect(() => {
- if (!project) return
- listDirectory(`${project.path}/raw/sources`)
- .then((tree) => { cachedSourceFiles = flattenNames(tree) })
- .catch(() => {})
- }, [project, fileTree])
+ const setSelectedFile = useWikiStore((s) => s.setSelectedFile)
- const cited = useMemo(() => extractCitedSources(content), [content, fileTree])
+ const citedPages = useMemo(() => extractCitedPages(content), [content])
- if (cited.length === 0) return null
+ if (citedPages.length === 0) return null
return (
<div className="flex flex-wrap items-center gap-1 px-1">
<span className="text-[10px] text-muted-foreground">Sources:</span>
- {cited.map((fileName) => (
- <SourceRef key={fileName} fileName={fileName} />
+ {citedPages.map((page) => (
+ <button
+ key={page.path}
+ type="button"
+ onClick={() => {
+ if (project) {
+ const fullPath = page.path.startsWith("/") ? page.path : `${project.path}/${page.path}`
+ setSelectedFile(fullPath)
+ }
+ }}
+ className="inline-flex items-center gap-0.5 rounded bg-accent/50 px-1.5 py-0.5 text-[11px] font-medium text-primary hover:bg-accent transition-colors"
+ title={page.path}
+ >
+ <FileText className="h-3 w-3" />
+ {page.title}
+ </button>
))}
</div>
)
}
/**
- * Extract cited source files from the hidden comment at the end of the response.
- * Format: <!-- sources: file1.pdf, file2.md -->
- * Only shows files explicitly cited by the LLM. Returns empty if none cited.
+ * Extract cited wiki pages from the hidden <!-- cited: 1, 3, 5 --> comment.
+ * Maps page numbers back to the pages that were sent to the LLM.
*/
-function extractCitedSources(text: string): string[] {
- // Try to parse the hidden comment
- const commentMatch = text.match(/<!--\s*sources:\s*(.+?)\s*-->/)
- if (commentMatch) {
- // LLM cited specific files — validate they exist
- const cited = commentMatch[1]
+function extractCitedPages(text: string): CitedPage[] {
+ const citedMatch = text.match(/<!--\s*cited:\s*(.+?)\s*-->/)
+ if (citedMatch && lastQueryPages.length > 0) {
+ const numbers = citedMatch[1]
.split(",")
- .map((s) => s.trim())
- .filter((s) => s.length > 0 && cachedSourceFiles.includes(s))
- if (cited.length > 0) return cited
-
- // LLM cited files but names don't match exactly — try fuzzy match
- const rawCited = commentMatch[1].split(",").map((s) => s.trim()).filter((s) => s.length > 0)
- const fuzzy = rawCited.flatMap((cited) => {
- const lower = cited.toLowerCase()
- return cachedSourceFiles.filter((f) =>
- f.toLowerCase().includes(lower) || lower.includes(f.toLowerCase().replace(/\.[^.]+$/, ""))
- )
- })
- const unique = [...new Set(fuzzy)]
- if (unique.length > 0) return unique
+ .map((s) => parseInt(s.trim(), 10))
+ .filter((n) => !isNaN(n) && n >= 1 && n <= lastQueryPages.length)
+
+ const pages = numbers.map((n) => lastQueryPages[n - 1])
+ if (pages.length > 0) return pages
}
- // Fallback: check which known source filenames appear in the text
- const mentioned = cachedSourceFiles.filter((name) => text.includes(name))
- if (mentioned.length > 0) return mentioned
+ // Fallback: if LLM used [1], [2] notation in text, try to match those
+ if (lastQueryPages.length > 0) {
+ const numberRefs = text.match(/\[(\d+)\]/g)
+ if (numberRefs) {
+ const numbers = [...new Set(numberRefs.map((r) => parseInt(r.slice(1, -1), 10)))]
+ .filter((n) => n >= 1 && n <= lastQueryPages.length)
+ if (numbers.length > 0) {
+ return numbers.map((n) => lastQueryPages[n - 1])
+ }
+ }
+ }
- // No sources identified — return empty (don't show all)
+ // No citations found
return []
}
diff --git a/src/components/chat/chat-panel.tsx b/src/components/chat/chat-panel.tsx
index c59a7487..c31bd187 100644
--- a/src/components/chat/chat-panel.tsx
+++ b/src/components/chat/chat-panel.tsx
@@ -12,6 +12,9 @@ import { searchWiki } from "@/lib/search"
import { useReviewStore } from "@/stores/review-store"
import type { FileNode } from "@/types/wiki"
+// Store the page mapping from the last query so SourceFilesBar can show which pages were cited
+export let lastQueryPages: { title: string; path: string }[] = []
+
export function ChatPanel() {
useSourceFiles() // Keep source file cache warm
const messages = useChatStore((s) => s.messages)
@@ -81,35 +84,47 @@ export function ChatPanel() {
}
}
- // Build the wiki context with actual page content
+ // Build numbered wiki context so AI can cite by number
const pagesContext = relevantPages.length > 0
- ? relevantPages.map((p) =>
- `### ${p.title} (${p.path})\n\n${p.content}`
+ ? relevantPages.map((p, i) =>
+ `### [${i + 1}] ${p.title}\nPath: ${p.path}\n\n${p.content}`
).join("\n\n---\n\n")
: "(No wiki pages found)"
+ // Build page list for reference
+ const pageList = relevantPages.map((p, i) =>
+ `[${i + 1}] ${p.title} (${p.path})`
+ ).join("\n")
+
systemMessages.push({
role: "system",
content: [
"You are a knowledgeable wiki assistant. Answer questions based on the wiki content provided below.",
"",
"## Rules",
- "- Answer based ONLY on the wiki page content provided below.",
+ "- Answer based ONLY on the numbered wiki pages provided below.",
"- If the provided pages don't contain enough information, say so honestly.",
- "- Use [[wikilink]] syntax to reference wiki pages you cite.",
+ "- Use [[wikilink]] syntax to reference wiki pages.",
+ "- When citing information, use the page number in brackets, e.g. [1], [2].",
+ "- At the VERY END of your response, add a hidden comment listing which page numbers you used:",
+ " <!-- cited: 1, 3, 5 -->",
"",
"## Save Judgment",
- "- If your answer contains a valuable synthesis, comparison, analysis, or new insight worth preserving, add this at the VERY END:",
+ "- If your answer contains a valuable synthesis, comparison, analysis, or new insight worth preserving, add BEFORE the cited comment:",
" <!-- save-worthy: yes | Brief reason -->",
- "- Only for genuinely valuable answers (cross-source comparisons, synthesis, novel connections). NOT for simple lookups.",
+ "- Only for genuinely valuable answers. NOT for simple lookups.",
"",
"Use markdown formatting for clarity.",
"",
purpose ? `## Wiki Purpose\n${purpose}` : "",
index ? `## Wiki Index\n${index}` : "",
- `## Relevant Wiki Pages (use these to answer)\n\n${pagesContext}`,
+ relevantPages.length > 0 ? `## Page List\n${pageList}` : "",
+ `## Wiki Pages\n\n${pagesContext}`,
].filter(Boolean).join("\n"),
})
+
+ // Store page mapping for SourceFilesBar to use
+ lastQueryPages = relevantPages.map((p) => ({ title: p.title, path: p.path }))
}
// Only include user and assistant messages in conversation history (not internal system messages)