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: pre-process files at import time for instant preview

nash_su583e03a4b9b6
diff --git a/src-tauri/src/commands/fs.rs b/src-tauri/src/commands/fs.rs
index cb34b11a..c461169d 100644
--- a/src-tauri/src/commands/fs.rs
+++ b/src-tauri/src/commands/fs.rs
@@ -13,12 +13,67 @@ pub fn read_file(path: String) -> Result<String, String> {
.to_lowercase();
match ext.as_str() {
- "pdf" => extract_pdf_text(&path),
+ "pdf" => {
+ // Check for pre-processed cache first
+ if let Some(cached) = read_cache(p) {
+ return Ok(cached);
+ }
+ extract_pdf_text(&path)
+ }
_ => fs::read_to_string(&path)
.map_err(|e| format!("Failed to read file '{}': {}", path, e)),
}
}
+/// Pre-process a file and cache the extracted text.
+/// Called at import time so previews are instant later.
+#[tauri::command]
+pub fn preprocess_file(path: String) -> Result<String, String> {
+ let p = Path::new(&path);
+ let ext = p
+ .extension()
+ .and_then(|e| e.to_str())
+ .unwrap_or("")
+ .to_lowercase();
+
+ let text = match ext.as_str() {
+ "pdf" => extract_pdf_text(&path)?,
+ _ => return Ok("no preprocessing needed".to_string()),
+ };
+
+ // Write cache
+ let cache_path = cache_path_for(p);
+ if let Some(parent) = cache_path.parent() {
+ fs::create_dir_all(parent).ok();
+ }
+ fs::write(&cache_path, &text)
+ .map_err(|e| format!("Failed to write cache: {}", e))?;
+
+ Ok(text)
+}
+
+fn cache_path_for(original: &Path) -> std::path::PathBuf {
+ let parent = original.parent().unwrap_or(Path::new("."));
+ let cache_dir = parent.join(".cache");
+ let file_name = original
+ .file_name()
+ .unwrap_or_default()
+ .to_string_lossy();
+ cache_dir.join(format!("{}.txt", file_name))
+}
+
+fn read_cache(original: &Path) -> Option<String> {
+ let cache_path = cache_path_for(original);
+ // Only use cache if it's newer than the original
+ let original_modified = fs::metadata(original).ok()?.modified().ok()?;
+ let cache_modified = fs::metadata(&cache_path).ok()?.modified().ok()?;
+ if cache_modified >= original_modified {
+ fs::read_to_string(&cache_path).ok()
+ } else {
+ None
+ }
+}
+
fn extract_pdf_text(path: &str) -> Result<String, String> {
let bytes =
fs::read(path).map_err(|e| format!("Failed to read PDF '{}': {}", path, e))?;
diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs
index 9e3d59a3..a8d8eb53 100644
--- a/src-tauri/src/lib.rs
+++ b/src-tauri/src/lib.rs
@@ -12,6 +12,7 @@ pub fn run() {
commands::fs::write_file,
commands::fs::list_directory,
commands::fs::copy_file,
+ commands::fs::preprocess_file,
commands::fs::delete_file,
commands::fs::find_related_wiki_pages,
commands::fs::create_directory,
diff --git a/src/commands/fs.ts b/src/commands/fs.ts
index 79227956..311b0f58 100644
--- a/src/commands/fs.ts
+++ b/src/commands/fs.ts
@@ -20,6 +20,10 @@ export async function copyFile(
return invoke("copy_file", { source, destination })
}
+export async function preprocessFile(path: string): Promise<string> {
+ return invoke<string>("preprocess_file", { path })
+}
+
export async function deleteFile(path: string): Promise<void> {
return invoke("delete_file", { path })
}
diff --git a/src/components/sources/sources-view.tsx b/src/components/sources/sources-view.tsx
index 688a8ec8..9adf877b 100644
--- a/src/components/sources/sources-view.tsx
+++ b/src/components/sources/sources-view.tsx
@@ -4,7 +4,7 @@ import { Plus, FileText, RefreshCw, BookOpen, Trash2 } from "lucide-react"
import { Button } from "@/components/ui/button"
import { ScrollArea } from "@/components/ui/scroll-area"
import { useWikiStore } from "@/stores/wiki-store"
-import { copyFile, listDirectory, readFile, writeFile, deleteFile, findRelatedWikiPages } from "@/commands/fs"
+import { copyFile, listDirectory, readFile, writeFile, deleteFile, findRelatedWikiPages, preprocessFile } from "@/commands/fs"
import type { FileNode } from "@/types/wiki"
import { startIngest, autoIngest } from "@/lib/ingest"
@@ -80,11 +80,15 @@ export function SourcesView() {
setImporting(true)
const paths = Array.isArray(selected) ? selected : [selected]
+ const importedPaths: string[] = []
for (const sourcePath of paths) {
const fileName = sourcePath.split("/").pop() || sourcePath.split("\\").pop() || "unknown"
const destPath = `${project.path}/raw/sources/${fileName}`
try {
await copyFile(sourcePath, destPath)
+ importedPaths.push(destPath)
+ // Pre-process file (extract text from PDF, etc.) for instant preview later
+ preprocessFile(destPath).catch(() => {})
} catch (err) {
console.error(`Failed to import ${fileName}:`, err)
}
@@ -95,11 +99,10 @@ export function SourcesView() {
// Auto-ingest each imported file (runs in background, progress shown in activity panel)
if (llmConfig.apiKey || llmConfig.provider === "ollama") {
- for (const sourcePath of paths) {
- const fileName = sourcePath.split("/").pop() || sourcePath.split("\\").pop() || "unknown"
- const destPath = `${project.path}/raw/sources/${fileName}`
+ for (const destPath of importedPaths) {
+ const name = destPath.split("/").pop() ?? destPath
autoIngest(project.path, destPath, llmConfig).catch((err) =>
- console.error(`Failed to auto-ingest ${fileName}:`, err)
+ console.error(`Failed to auto-ingest ${name}:`, err)
)
}
}