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: MD5-based file tracking for scheduled import
skfan1359b9c7a3d291d
diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.tomlindex f25552e5..1b3a2b32 100644--- a/src-tauri/Cargo.toml+++ b/src-tauri/Cargo.toml@@ -56,6 +56,7 @@ uuid = { version = "1", features = ["v4"] }image = { version = "0.25", default-features = false, features = ["png"] }base64 = "0.22"sha2 = "0.10"+md5 = "0.7"[dev-dependencies]tokio = { version = "1", features = ["macros", "rt", "rt-multi-thread"] }diff --git a/src-tauri/src/commands/fs.rs b/src-tauri/src/commands/fs.rsindex 877634dc..74dfc9df 100644--- a/src-tauri/src/commands/fs.rs+++ b/src-tauri/src/commands/fs.rs@@ -1322,6 +1322,21 @@ pub async fn get_file_modified_time(path: String) -> Result<u64, String> {.map_err(|e| format!("get_file_modified_time blocking task join error: {e}"))?}+/// Compute MD5 hash of a file. Returns the hex-encoded hash string.+#[tauri::command]+pub async fn get_file_md5(path: String) -> Result<String, String> {+ tauri::async_runtime::spawn_blocking(move || {+ run_guarded("get_file_md5", || {+ let bytes = fs::read(&path)+ .map_err(|e| format!("Failed to read file '{}': {}", path, e))?;+ let digest = md5::compute(&bytes);+ Ok(format!("{:x}", digest))+ })+ })+ .await+ .map_err(|e| format!("get_file_md5 blocking task join error: {e}"))?+}+#[cfg(test)]mod tests {use super::*;diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rsindex d772ad4d..f51d1179 100644--- a/src-tauri/src/lib.rs+++ b/src-tauri/src/lib.rs@@ -87,6 +87,7 @@ pub fn run() {commands::fs::create_directory,commands::fs::file_exists,commands::fs::get_file_modified_time,+ commands::fs::get_file_md5,commands::fs::read_file_as_base64,commands::project::create_project,commands::project::open_project,diff --git a/src/commands/fs.ts b/src/commands/fs.tsindex 2b2bb8de..0615bbc8 100644--- a/src/commands/fs.ts+++ b/src/commands/fs.ts@@ -54,6 +54,10 @@ export async function getFileModifiedTime(path: string): Promise<number> {return invoke<number>("get_file_modified_time", { path })}+export async function getFileMd5(path: string): Promise<string> {+ return invoke<string>("get_file_md5", { path })+}+/** Mirror of `commands::fs::FileBase64` (Rust side). */export interface FileBase64 {base64: stringdiff --git a/src/lib/scheduled-import.ts b/src/lib/scheduled-import.tsindex 72144925..f2ee3343 100644--- a/src/lib/scheduled-import.ts+++ b/src/lib/scheduled-import.ts@@ -1,4 +1,4 @@-import { listDirectory, copyFile, preprocessFile, getFileModifiedTime } from "@/commands/fs"+import { listDirectory, copyFile, preprocessFile, getFileMd5, createDirectory, fileExists, readFile, writeFile } from "@/commands/fs"import { enqueueIngest } from "@/lib/ingest-queue"import { normalizePath, getFileName } from "@/lib/path-utils"import { hasUsableLlm } from "@/lib/has-usable-llm"@@ -16,13 +16,21 @@ const IMPORTABLE_EXTENSIONS = new Set(["c", "cpp", "h", "rb", "php", "swift", "sql", "sh",])+// Hidden config folder name in the monitored directory+const IMPORT_DB_FOLDER = ".llm-wiki-imported"+const IMPORT_DB_FILE = "db.json"+let scanTimer: ReturnType<typeof setInterval> | null = nulllet scanning = false-interface FileChange {- type: "new" | "modified"- sourcePath: string // absolute path in monitored directory- fileName: string+interface ImportDbEntry {+ md5: string+ importedAt: number+}++interface ImportDb {+ files: Record<string, ImportDbEntry>+ lastScan: number}/**@@ -49,91 +57,66 @@ function isImportableFile(fileName: string): boolean {}/**- * Build a map of existing sources: fileName -> { path, modifiedTime }.- * Uses base filename as key to match files across different directories.+ * Get the path to the import db folder within the monitored directory.*/-async function buildExistingSourcesMap(projectPath: string): Promise<Map<string, { path: string; modified: number }>> {- const pp = normalizePath(projectPath)- const map = new Map<string, { path: string; modified: number }>()+function getDbFolderPath(importPath: string): string {+ return `${importPath}/${IMPORT_DB_FOLDER}`+}+/**+ * Get the path to the import db.json file.+ */+function getDbFilePath(importPath: string): string {+ return `${importPath}/${IMPORT_DB_FOLDER}/${IMPORT_DB_FILE}`+}++/**+ * Ensure the .llm-wiki-imported/ directory exists in the monitored directory.+ */+async function ensureImportDb(importPath: string): Promise<void> {+ const dbFolder = getDbFolderPath(importPath)try {- const tree = await listDirectory(`${pp}/raw/sources`)- const files = collectFiles(tree)+ await createDirectory(dbFolder)+ } catch {+ // Directory may already exist+ }+}- for (const file of files) {- try {- const modified = await getFileModifiedTime(file.path)- const baseName = getFileName(file.path) || file.name- map.set(baseName, { path: file.path, modified })- } catch {- // Can't get modified time, skip- }+/**+ * Load the import database from .llm-wiki-imported/db.json.+ * Returns an empty database if the file doesn't exist.+ */+async function loadImportDb(importPath: string): Promise<ImportDb> {+ const dbFilePath = getDbFilePath(importPath)+ try {+ const exists = await fileExists(dbFilePath)+ if (!exists) return { files: {}, lastScan: 0 }+ const content = await readFile(dbFilePath)+ const parsed = JSON.parse(content)+ return {+ files: parsed.files || {},+ lastScan: parsed.lastScan || 0,}} catch {- // sources directory may not exist yet+ return { files: {}, lastScan: 0 }}-- return map}/**- * Detect changes between monitored directory and existing sources.- * Compares by filename to match files across different directories.+ * Save the import database to .llm-wiki-imported/db.json.*/-async function detectChanges(- monitoredPath: string,- existingSources: Map<string, { path: string; modified: number }>,-): Promise<FileChange[]> {- const changes: FileChange[] = []-+async function saveImportDb(importPath: string, db: ImportDb): Promise<void> {+ const dbFilePath = getDbFilePath(importPath)try {- const tree = await listDirectory(monitoredPath)- const files = collectFiles(tree)-- for (const file of files) {- if (!isImportableFile(file.name)) continue-- const baseName = getFileName(file.path) || file.name- const existing = existingSources.get(baseName)-- if (!existing) {- // New file - not in sources yet- changes.push({- type: "new",- sourcePath: file.path,- fileName: baseName,- })- } else {- // File exists in sources - check if modified- const normalizedSource = normalizePath(file.path)- const normalizedExisting = normalizePath(existing.path)-- // Skip if it's the exact same file (monitored dir IS raw/sources)- if (normalizedSource === normalizedExisting) continue-- try {- const sourceModified = await getFileModifiedTime(file.path)- if (sourceModified > existing.modified) {- changes.push({- type: "modified",- sourcePath: file.path,- fileName: baseName,- })- }- } catch {- // Can't compare, skip- }- }- }+ await writeFile(dbFilePath, JSON.stringify(db, null, 2))} catch (err) {- console.error("[Scheduled Import] Failed to scan monitored directory:", err)+ console.error("[Scheduled Import] Failed to save import db:", err)}-- return changes}/**- * Execute a scan: detect changes and import modified/new files.+ * Scan the monitored directory and import new or modified files.+ * Uses MD5 hashing stored in .llm-wiki-imported/db.json to detect changes.*/export async function scanAndImport(projectPath: string, importPath: string): Promise<void> {if (scanning) {@@ -148,28 +131,65 @@ export async function scanAndImport(projectPath: string, importPath: string): Prconsole.log(`[Scheduled Import] Starting scan of ${ip}`)try {- const existingSources = await buildExistingSourcesMap(pp)- const changes = await detectChanges(ip, existingSources)+ // Ensure the hidden config folder exists+ await ensureImportDb(ip)++ // Load existing import database+ const db = await loadImportDb(ip)++ // List all files in the monitored directory+ let monitoredFiles: Array<{ name: string; path: string }> = []+ try {+ const tree = await listDirectory(ip)+ monitoredFiles = collectFiles(tree)+ } catch (err) {+ console.error("[Scheduled Import] Failed to list monitored directory:", err)+ return+ }- if (changes.length === 0) {- console.log("[Scheduled Import] No changes detected")+ // Filter to importable files only+ const importableFiles = monitoredFiles.filter(f => {+ // Skip files inside the hidden config folder+ if (f.path.includes(`/${IMPORT_DB_FOLDER}/`) || f.path.includes(`\\${IMPORT_DB_FOLDER}\\`)) {+ return false+ }+ return isImportableFile(f.name)+ })++ if (importableFiles.length === 0) {+ console.log("[Scheduled Import] No importable files found")return}- console.log(`[Scheduled Import] Found ${changes.length} changes`)+ console.log(`[Scheduled Import] Found ${importableFiles.length} importable files`)const llmConfig = useWikiStore.getState().llmConfigconst hasLlm = hasUsableLlm(llmConfig)+ let importedCount = 0+ let skippedCount = 0- for (const change of changes) {+ for (const file of importableFiles) {try {- const destPath = `${pp}/raw/sources/${change.fileName}`- const normalizedSource = normalizePath(change.sourcePath)- const normalizedDest = normalizePath(destPath)+ // Use relative path (from monitored dir) as the key+ const relativePath = file.name+ const currentMd5 = await getFileMd5(file.path)+ const existing = db.files[relativePath]++ // Check if file has changed+ if (existing && existing.md5 === currentMd5) {+ skippedCount+++ continue+ }++ // File is new or modified - import it+ const baseName = getFileName(file.path) || file.name+ const destPath = `${pp}/raw/sources/${baseName}`+ // Copy file to sources directory (skip if source IS the destination)+ const normalizedSource = normalizePath(file.path)+ const normalizedDest = normalizePath(destPath)if (normalizedSource !== normalizedDest) {- // Source is outside raw/sources - copy it- await copyFile(change.sourcePath, destPath)+ await copyFile(file.path, destPath)}// Preprocess the file@@ -183,17 +203,27 @@ export async function scanAndImport(projectPath: string, importPath: string): Pr}}- console.log(`[Scheduled Import] ${change.type === "new" ? "Imported" : "Updated"}: ${change.fileName}`)+ // Update the database with the new MD5+ db.files[relativePath] = {+ md5: currentMd5,+ importedAt: Date.now(),+ }++ importedCount+++ console.log(`[Scheduled Import] ${existing ? "Updated" : "Imported"}: ${baseName}`)} catch (err) {- console.error(`[Scheduled Import] Failed to process ${change.fileName}:`, err)+ console.error(`[Scheduled Import] Failed to process ${file.name}:`, err)}}- // Update last scan time+ // Save the updated database+ db.lastScan = Date.now()+ await saveImportDb(ip, db)++ // Update last scan time in the storeconst config = useWikiStore.getState().scheduledImportConfigconst updatedConfig = { ...config, lastScan: Date.now() }useWikiStore.getState().setScheduledImportConfig(updatedConfig)- // Persist per-project configconst { saveScheduledImportConfig } = await import("@/lib/project-store")await saveScheduledImportConfig(projectPath, updatedConfig)@@ -203,6 +233,7 @@ export async function scanAndImport(projectPath: string, importPath: string): PruseWikiStore.getState().setFileTree(tree)useWikiStore.getState().bumpDataVersion()+ console.log(`[Scheduled Import] Scan complete: ${importedCount} imported, ${skippedCount} skipped`)} catch (err) {console.error("[Scheduled Import] Scan failed:", err)} finally {@@ -217,13 +248,10 @@ export async function scanAndImport(projectPath: string, importPath: string): Pr*/export function resolveImportPath(projectPath: string, configPath: string): string {const pp = normalizePath(projectPath)- // Default to "raw/sources" if path is emptyconst path = configPath || "raw/sources"- // If path is already absolute, use it as-isif (path.startsWith("/") || path.match(/^[a-zA-Z]:[/\\]/)) {return normalizePath(path)}- // Otherwise, treat as relative to project pathreturn `${pp}/${path}`}