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: LLM auto-detects valuable answers, prompts save via Review

nash_su0c5df1a9c718
diff --git a/src/components/chat/chat-panel.tsx b/src/components/chat/chat-panel.tsx
index 00ce63ad..d40e4ad1 100644
--- a/src/components/chat/chat-panel.tsx
+++ b/src/components/chat/chat-panel.tsx
@@ -7,8 +7,9 @@ import { useChatStore, chatMessagesToLLM } from "@/stores/chat-store"
import { useWikiStore } from "@/stores/wiki-store"
import { streamChat, type ChatMessage as LLMMessage } from "@/lib/llm-client"
import { executeIngestWrites } from "@/lib/ingest"
-import { listDirectory, readFile } from "@/commands/fs"
+import { listDirectory, readFile, writeFile } from "@/commands/fs"
import { searchWiki } from "@/lib/search"
+import { useReviewStore } from "@/stores/review-store"
import type { FileNode } from "@/types/wiki"
export function ChatPanel() {
@@ -186,10 +187,16 @@ export function ChatPanel() {
"- Pages marked [Source] are the user's original uploaded documents — they contain the full detail.",
"- If the provided pages don't contain enough information, say so honestly.",
"- Use [[wikilink]] syntax to reference wiki pages you cite.",
- "- At the VERY END of your response, add a hidden comment listing source files used:",
+ "- At the VERY END of your response, add hidden comments:",
" <!-- sources: file1.pdf, file2.md -->",
"- Use EXACT file names from the Source Files list below.",
"",
+ "## Save Judgment",
+ "- If your answer contains a valuable synthesis, comparison, analysis, new insight, or connection worth preserving in the wiki, add this hidden comment BEFORE the sources comment:",
+ " <!-- save-worthy: yes | Brief reason why this is worth saving -->",
+ "- Only mark as save-worthy if the answer genuinely adds knowledge value. Simple factual lookups or short answers are NOT save-worthy.",
+ "- Examples of save-worthy: cross-source comparisons, multi-concept synthesis, discovered contradictions, novel connections between topics.",
+ "",
"Use markdown formatting for clarity.",
"",
purpose ? `## Wiki Purpose\n${purpose}` : "",
@@ -221,6 +228,8 @@ export function ChatPanel() {
onDone: () => {
finalizeStream(accumulated)
abortRef.current = null
+ // Check if LLM marked this answer as save-worthy
+ checkSaveWorthy(accumulated, text)
},
onError: (err) => {
finalizeStream(`Error: ${err.message}`)
@@ -299,6 +308,38 @@ export function ChatPanel() {
)
}
+/**
+ * Check if the LLM marked its response as save-worthy.
+ * If so, add a review item prompting the user to save it.
+ */
+function checkSaveWorthy(response: string, question: string) {
+ const match = response.match(/<!--\s*save-worthy:\s*yes\s*\|\s*(.+?)\s*-->/)
+ if (!match) return
+
+ const reason = match[1]
+ const firstLine = response.split("\n").find((l) => l.trim() && !l.startsWith("<!--"))?.replace(/^#+\s*/, "").trim() ?? "Chat answer"
+ const title = firstLine.slice(0, 60)
+
+ // Store the content reference so the review action can save it
+ const contentToSave = response
+ const questionText = question
+
+ useReviewStore.getState().addItem({
+ type: "suggestion",
+ title: `Save to Wiki: ${title}`,
+ description: `${reason}\n\nQuestion: "${questionText.slice(0, 100)}${questionText.length > 100 ? "..." : ""}"`,
+ options: [
+ { label: "Save to Wiki", action: `save:${encodeContent(contentToSave)}` },
+ { label: "Skip", action: "Skip" },
+ ],
+ })
+}
+
+function encodeContent(text: string): string {
+ // Use base64-like encoding to safely store content in action string
+ return btoa(encodeURIComponent(text))
+}
+
function flattenFileNames(nodes: FileNode[]): string[] {
const names: string[] = []
for (const node of nodes) {
diff --git a/src/components/review/review-view.tsx b/src/components/review/review-view.tsx
index 347f12d0..23b583a9 100644
--- a/src/components/review/review-view.tsx
+++ b/src/components/review/review-view.tsx
@@ -1,3 +1,4 @@
+import { useCallback } from "react"
import {
AlertTriangle,
Copy,
@@ -12,6 +13,8 @@ import {
import { Button } from "@/components/ui/button"
import { ScrollArea } from "@/components/ui/scroll-area"
import { useReviewStore, type ReviewItem } from "@/stores/review-store"
+import { useWikiStore } from "@/stores/wiki-store"
+import { writeFile, readFile, listDirectory } from "@/commands/fs"
const typeConfig: Record<ReviewItem["type"], { icon: typeof AlertTriangle; label: string; color: string }> = {
contradiction: { icon: AlertTriangle, label: "Contradiction", color: "text-amber-500" },
@@ -26,6 +29,64 @@ export function ReviewView() {
const resolveItem = useReviewStore((s) => s.resolveItem)
const dismissItem = useReviewStore((s) => s.dismissItem)
const clearResolved = useReviewStore((s) => s.clearResolved)
+ const project = useWikiStore((s) => s.project)
+ const setFileTree = useWikiStore((s) => s.setFileTree)
+
+ const handleResolve = useCallback(async (id: string, action: string) => {
+ if (action.startsWith("save:") && project) {
+ // Decode and save the content to wiki
+ try {
+ const encoded = action.slice(5)
+ const content = decodeURIComponent(atob(encoded))
+
+ // Strip hidden comments
+ const cleanContent = content
+ .replace(/<!--\s*save-worthy:.*?-->/g, "")
+ .replace(/<!--\s*sources:.*?-->/g, "")
+ .trimEnd()
+
+ // Generate filename
+ const firstLine = cleanContent.split("\n").find((l) => l.trim() && !l.startsWith("<!--"))?.replace(/^#+\s*/, "").trim() ?? "Saved Query"
+ const title = firstLine.slice(0, 60)
+ const slug = title.toLowerCase().replace(/[^a-z0-9\s-]/g, "").trim().replace(/\s+/g, "-").slice(0, 50)
+ const date = new Date().toISOString().slice(0, 10)
+ const fileName = `${slug}-${date}.md`
+ const filePath = `${project.path}/wiki/queries/${fileName}`
+
+ const frontmatter = `---\ntype: query\ntitle: "${title.replace(/"/g, '\\"')}"\ncreated: ${date}\ntags: []\n---\n\n`
+ await writeFile(filePath, frontmatter + cleanContent)
+
+ // Update index
+ const indexPath = `${project.path}/wiki/index.md`
+ let indexContent = ""
+ try { indexContent = await readFile(indexPath) } catch { indexContent = "# Wiki Index\n" }
+ const entry = `- [[queries/${slug}-${date}|${title}]]`
+ if (indexContent.includes("## Queries")) {
+ indexContent = indexContent.replace(/(## Queries\n)/, `$1${entry}\n`)
+ } else {
+ indexContent = indexContent.trimEnd() + "\n\n## Queries\n" + entry + "\n"
+ }
+ await writeFile(indexPath, indexContent)
+
+ // Append log
+ const logPath = `${project.path}/wiki/log.md`
+ let logContent = ""
+ try { logContent = await readFile(logPath) } catch { logContent = "# Wiki Log\n" }
+ await writeFile(logPath, logContent.trimEnd() + `\n- ${date}: Saved query page \`${fileName}\`\n`)
+
+ // Refresh tree
+ const tree = await listDirectory(project.path)
+ setFileTree(tree)
+
+ resolveItem(id, "Saved to Wiki")
+ } catch (err) {
+ console.error("Failed to save to wiki from review:", err)
+ resolveItem(id, "Save failed")
+ }
+ } else {
+ resolveItem(id, action)
+ }
+ }, [project, resolveItem, setFileTree])
const pending = items.filter((i) => !i.resolved)
const resolved = items.filter((i) => i.resolved)
@@ -61,7 +122,7 @@ export function ReviewView() {
<ReviewCard
key={item.id}
item={item}
- onResolve={resolveItem}
+ onResolve={handleResolve}
onDismiss={dismissItem}
/>
))}
@@ -74,7 +135,7 @@ export function ReviewView() {
<ReviewCard
key={item.id}
item={item}
- onResolve={resolveItem}
+ onResolve={handleResolve}
onDismiss={dismissItem}
/>
))}