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: normalize review ids across api and persistence
nash_sudbf0e72fc966
diff --git a/src-tauri/src/api_server.rs b/src-tauri/src/api_server.rsindex cf0cdc39..ee6935ba 100644--- a/src-tauri/src/api_server.rs+++ b/src-tauri/src/api_server.rs@@ -1017,6 +1017,55 @@ fn parse_review_query(query: &str) -> Result<ReviewQuery, String> {})}+fn normalize_review_title(title: &str) -> String {+ let trimmed = title.trim_start();+ let lower = trimmed.to_lowercase();+ let mut rest = trimmed;+ for prefix in [+ "missing page",+ "missing-page",+ "missingpage",+ "duplicate page",+ "duplicate-page",+ "duplicatepage",+ "possible duplicate",+ "possible-duplicate",+ "possibleduplicate",+ "缺失页面",+ "缺少页面",+ "重复页面",+ "疑似重复",+ ] {+ if lower.starts_with(prefix) {+ rest = &trimmed[prefix.len()..];+ rest = rest.trim_start_matches(|ch: char| ch == ':' || ch == ':' || ch.is_whitespace());+ break;+ }+ }+ rest.split_whitespace().collect::<Vec<_>>().join(" ").to_lowercase()+}++fn review_id_for_parts(item_type: &str, title: &str) -> String {+ let key = format!("{item_type}::{}", normalize_review_title(title));+ let mut h: u32 = 0x811c9dc5;+ for unit in key.encode_utf16() {+ h ^= u32::from(unit);+ h = h.wrapping_mul(0x01000193);+ }+ format!("review-{h:08x}")+}++fn stable_review_id(item: &Value) -> Option<String> {+ let item_type = item.get("type").and_then(Value::as_str)?;+ let title = item.get("title").and_then(Value::as_str)?;+ Some(review_id_for_parts(item_type, title))+}++fn review_id_matches(item: &Value, requested_id: &str) -> bool {+ item.get("id").and_then(Value::as_str) == Some(requested_id)+ || stable_review_id(item).as_deref() == Some(requested_id)+}+fn load_review_items(project_path: &str, query: &ReviewQuery) -> Result<Vec<Value>, String> {let path = Path::new(project_path).join(".llm-wiki/review.json");let raw = match fs::read_to_string(&path) {@@ -1030,7 +1079,8 @@ fn load_review_items(project_path: &str, query: &ReviewQuery) -> Result<Vec<Valu.as_array().ok_or_else(|| "Invalid review state JSON: expected an array".to_string())?;- let mut reviews = Vec::new();+ let mut reviews: Vec<Value> = Vec::new();+ let mut index_by_id: BTreeMap<String, usize> = BTreeMap::new();for item in items {let resolved = item.get("resolved")@@ -1044,8 +1094,21 @@ fn load_review_items(project_path: &str, query: &ReviewQuery) -> Result<Vec<Valucontinue;}}- reviews.push(sanitize_review_item(item));- if reviews.len() >= query.limit {+ let sanitized = sanitize_review_item(item);+ let id = sanitized.get("id").and_then(Value::as_str).map(str::to_string);+ if let Some(id) = id {+ if let Some(existing_idx) = index_by_id.get(&id).copied() {+ merge_sanitized_review(&mut reviews[existing_idx], &sanitized);+ continue;+ }+ if reviews.len() >= query.limit {+ break;+ }+ index_by_id.insert(id, reviews.len());+ reviews.push(sanitized);+ } else if reviews.len() < query.limit {+ reviews.push(sanitized);+ } else {break;}}@@ -1055,7 +1118,11 @@ fn load_review_items(project_path: &str, query: &ReviewQuery) -> Result<Vec<Valufn sanitize_review_item(item: &Value) -> Value {let mut out = Map::new();- copy_string_field(item, &mut out, "id");+ if let Some(id) = stable_review_id(item) {+ out.insert("id".to_string(), Value::String(id));+ } else {+ copy_string_field(item, &mut out, "id");+ }copy_string_field(item, &mut out, "type");copy_string_field(item, &mut out, "title");copy_string_field(item, &mut out, "description");@@ -1069,6 +1136,114 @@ fn sanitize_review_item(item: &Value) -> Value {Value::Object(out)}+fn merge_sanitized_review(existing: &mut Value, incoming: &Value) {+ let Some(existing) = existing.as_object_mut() else {+ return;+ };+ let Some(incoming) = incoming.as_object() else {+ return;+ };++ let resolved = existing+ .get("resolved")+ .and_then(Value::as_bool)+ .unwrap_or(false)+ || incoming+ .get("resolved")+ .and_then(Value::as_bool)+ .unwrap_or(false);+ existing.insert("resolved".to_string(), Value::Bool(resolved));++ if resolved && !existing.contains_key("resolvedAction") {+ if let Some(action) = incoming.get("resolvedAction").and_then(Value::as_str) {+ existing.insert("resolvedAction".to_string(), Value::String(action.to_string()));+ }+ }++ for key in ["description", "sourcePath"] {+ let existing_empty = existing+ .get(key)+ .and_then(Value::as_str)+ .map(str::is_empty)+ .unwrap_or(true);+ if existing_empty {+ if let Some(value) = incoming.get(key).and_then(Value::as_str) {+ existing.insert(key.to_string(), Value::String(value.to_string()));+ }+ }+ }++ merge_string_array_field(existing, incoming, "affectedPages");+ merge_string_array_field(existing, incoming, "searchQueries");+ merge_review_options_field(existing, incoming);++ if let Some(incoming_created) = incoming.get("createdAt").and_then(Value::as_f64) {+ let existing_created = existing+ .get("createdAt")+ .and_then(Value::as_f64)+ .unwrap_or(incoming_created);+ existing.insert("createdAt".to_string(), json!(existing_created.min(incoming_created)));+ }+}++fn merge_string_array_field(existing: &mut Map<String, Value>, incoming: &Map<String, Value>, key: &str) {+ let mut values = existing+ .get(key)+ .and_then(Value::as_array)+ .map(|values| {+ values+ .iter()+ .filter_map(Value::as_str)+ .map(str::to_string)+ .collect::<Vec<_>>()+ })+ .unwrap_or_default();+ for value in incoming+ .get(key)+ .and_then(Value::as_array)+ .into_iter()+ .flatten()+ .filter_map(Value::as_str)+ {+ if !values.iter().any(|existing| existing == value) {+ values.push(value.to_string());+ }+ }+ if !values.is_empty() {+ existing.insert(+ key.to_string(),+ Value::Array(values.into_iter().map(Value::String).collect()),+ );+ }+}++fn merge_review_options_field(existing: &mut Map<String, Value>, incoming: &Map<String, Value>) {+ let mut options = existing+ .get("options")+ .and_then(Value::as_array)+ .cloned()+ .unwrap_or_default();+ for option in incoming+ .get("options")+ .and_then(Value::as_array)+ .into_iter()+ .flatten()+ {+ let action = option.get("action").and_then(Value::as_str);+ let already_present = action.is_some_and(|action| {+ options+ .iter()+ .any(|existing| existing.get("action").and_then(Value::as_str) == Some(action))+ });+ if !already_present {+ options.push(option.clone());+ }+ }+ if !options.is_empty() {+ existing.insert("options".to_string(), Value::Array(options));+ }+}+fn copy_string_field(item: &Value, out: &mut Map<String, Value>, key: &str) {if let Some(value) = item.get(key).and_then(Value::as_str) {out.insert(key.to_string(), Value::String(value.to_string()));@@ -1258,12 +1433,16 @@ fn patch_review_item(let mut found = false;for item in items.iter_mut() {- if item.get("id").and_then(Value::as_str) != Some(review_id) {+ if !review_id_matches(item, review_id) {continue;}apply_resolution(item, resolved, action);+ if let Some(stable_id) = stable_review_id(item) {+ if let Some(obj) = item.as_object_mut() {+ obj.insert("id".to_string(), Value::String(stable_id));+ }+ }found = true;- break;}if !found {@@ -1293,15 +1472,23 @@ fn resolve_review_items(.as_array_mut().ok_or_else(|| "Invalid review state JSON: expected an array".to_string())?;- let requested: BTreeSet<&str> = ids.iter().map(String::as_str).collect();let mut found: BTreeSet<String> = BTreeSet::new();for item in items.iter_mut() {- let id = match item.get("id").and_then(Value::as_str) {- Some(id) if requested.contains(id) => id.to_string(),- _ => continue,+ let raw_id = item.get("id").and_then(Value::as_str);+ let stable_id = stable_review_id(item);+ let matched_request = ids.iter().find(|id| {+ raw_id == Some(id.as_str()) || stable_id.as_deref() == Some(id.as_str())+ });+ let Some(requested_id) = matched_request else {+ continue;};apply_resolution(item, true, action);- found.insert(id);+ if let Some(stable_id) = stable_id {+ if let Some(obj) = item.as_object_mut() {+ obj.insert("id".to_string(), Value::String(stable_id));+ }+ }+ found.insert(requested_id.clone());}if !found.is_empty() {@@ -1723,7 +1910,10 @@ mod tests {assert_eq!(query.status.as_str(), "unresolved");assert_eq!(reviews.len(), 1);- assert_eq!(reviews[0].get("id").and_then(Value::as_str), Some("r1"));+ assert_eq!(+ reviews[0].get("id").and_then(Value::as_str),+ Some(review_id_for_parts("missing-page", "Missing page: Attention").as_str())+ );assert!(reviews[0].get("internalSecret").is_none());let _ = fs::remove_dir_all(root);}@@ -1736,8 +1926,8 @@ mod tests {fs::write(state_dir.join("review.json"),json!([- { "id": "r1", "type": "missing-page", "resolved": false, "createdAt": 1 },- { "id": "r2", "type": "missing-page", "resolved": false, "createdAt": 2 },+ { "id": "r1", "type": "missing-page", "title": "r1", "resolved": false, "createdAt": 1 },+ { "id": "r2", "type": "missing-page", "title": "r2", "resolved": false, "createdAt": 2 },{ "id": "r3", "type": "duplicate", "resolved": false, "createdAt": 3 },{ "id": "r4", "type": "missing-page", "resolved": true, "createdAt": 4 }])@@ -1753,12 +1943,72 @@ mod tests {reviews.iter().filter_map(|r| r.get("id").and_then(Value::as_str))+ .map(str::to_string).collect::<Vec<_>>(),- vec!["r1", "r2"]+ vec![+ review_id_for_parts("missing-page", "r1"),+ review_id_for_parts("missing-page", "r2"),+ ]);let _ = fs::remove_dir_all(root);}+ #[test]+ fn review_query_collapses_legacy_duplicate_ids_to_stable_id() {+ let root = test_project_dir();+ let state_dir = root.join(".llm-wiki");+ fs::create_dir_all(&state_dir).unwrap();+ fs::write(+ state_dir.join("review.json"),+ json!([+ {+ "id": "review-1",+ "type": "missing-page",+ "title": "Attention",+ "description": "",+ "affectedPages": ["a.md"],+ "resolved": false,+ "createdAt": 5+ },+ {+ "id": "review-2",+ "type": "missing-page",+ "title": "Missing page: Attention",+ "description": "resolved copy",+ "affectedPages": ["b.md"],+ "resolved": true,+ "resolvedAction": "user-resolved",+ "createdAt": 2+ }+ ])+ .to_string(),+ )+ .unwrap();++ let query = parse_review_query("status=all").unwrap();+ let reviews = load_review_items(root.to_str().unwrap(), &query).unwrap();++ assert_eq!(reviews.len(), 1);+ assert_eq!(+ reviews[0].get("id").and_then(Value::as_str),+ Some(review_id_for_parts("missing-page", "Attention").as_str())+ );+ assert_eq!(reviews[0].get("resolved").and_then(Value::as_bool), Some(true));+ assert_eq!(+ reviews[0].get("resolvedAction").and_then(Value::as_str),+ Some("user-resolved")+ );+ assert_eq!(+ reviews[0]+ .get("affectedPages")+ .and_then(Value::as_array)+ .map(|pages| pages.iter().filter_map(Value::as_str).collect::<Vec<_>>()),+ Some(vec!["a.md", "b.md"])+ );+ assert_eq!(reviews[0].get("createdAt").and_then(Value::as_f64), Some(2.0));+ let _ = fs::remove_dir_all(root);+ }+fn write_reviews(root: &Path, value: Value) {let state_dir = root.join(".llm-wiki");fs::create_dir_all(&state_dir).unwrap();@@ -1813,6 +2063,34 @@ mod tests {let _ = fs::remove_dir_all(root);}+ #[test]+ fn patch_review_item_accepts_stable_id_for_legacy_counter_item() {+ let root = test_project_dir();+ write_reviews(+ &root,+ json!([+ {+ "id": "review-1",+ "type": "missing-page",+ "title": "Missing page: Attention",+ "resolved": false+ }+ ]),+ );++ let stable_id = review_id_for_parts("missing-page", "Attention");+ let found =+ patch_review_item(root.to_str().unwrap(), &stable_id, true, Some("API")).unwrap();+ assert!(found);++ let parsed = read_reviews(&root);+ let item = &parsed.as_array().unwrap()[0];+ assert_eq!(item.get("id").and_then(Value::as_str), Some(stable_id.as_str()));+ assert_eq!(item.get("resolved").and_then(Value::as_bool), Some(true));+ assert_eq!(item.get("resolvedAction").and_then(Value::as_str), Some("API"));+ let _ = fs::remove_dir_all(root);+ }+#[test]fn patch_review_item_can_reopen_with_resolved_false() {let root = test_project_dir();@@ -1890,6 +2168,45 @@ mod tests {let _ = fs::remove_dir_all(root);}+ #[test]+ fn resolve_review_items_accepts_stable_ids_for_legacy_counter_items() {+ let root = test_project_dir();+ write_reviews(+ &root,+ json!([+ {+ "id": "review-1",+ "type": "missing-page",+ "title": "Missing page: Attention",+ "resolved": false+ },+ {+ "id": "review-2",+ "type": "duplicate",+ "title": "Duplicate page: Transformer",+ "resolved": false+ }+ ]),+ );++ let ids = vec![+ review_id_for_parts("missing-page", "Attention"),+ "missing".to_string(),+ ];+ let (resolved, not_found) =+ resolve_review_items(root.to_str().unwrap(), &ids, Some("Bulk")).unwrap();++ assert_eq!(resolved, vec![ids[0].clone()]);+ assert_eq!(not_found, vec!["missing".to_string()]);+ let parsed = read_reviews(&root);+ let items = parsed.as_array().unwrap();+ assert_eq!(items[0].get("id").and_then(Value::as_str), Some(ids[0].as_str()));+ assert_eq!(items[0].get("resolved").and_then(Value::as_bool), Some(true));+ assert_eq!(items[1].get("id").and_then(Value::as_str), Some("review-2"));+ assert_eq!(items[1].get("resolved").and_then(Value::as_bool), Some(false));+ let _ = fs::remove_dir_all(root);+ }+#[test]fn resolve_review_items_missing_file_reports_all_not_found() {let root = test_project_dir();diff --git a/src/lib/persist.integration.test.ts b/src/lib/persist.integration.test.tsindex 1f7a29ba..b24fd11e 100644--- a/src/lib/persist.integration.test.ts+++ b/src/lib/persist.integration.test.ts@@ -9,7 +9,7 @@*/import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"import { realFs, createTempProject, readFileRaw, writeFileRaw, fileExists } from "@/test-helpers/fs-temp"-import type { ReviewItem } from "@/stores/review-store"+import { reviewIdFor, type ReviewItem } from "@/stores/review-store"import type { LintItem } from "@/stores/lint-store"import type { Conversation, DisplayMessage } from "@/stores/chat-store"@@ -62,14 +62,17 @@ afterEach(async () => {})describe("review persistence — round-trip", () => {- it("save then load returns identical items", async () => {+ it("save then load returns normalized review items", async () => {const items: ReviewItem[] = [makeReview({ id: "r-1", title: "Alpha" }),makeReview({ id: "r-2", title: "Beta", type: "duplicate" }),]await saveReviewItems(tmp.path, items)const loaded = await loadReviewItems(tmp.path)- expect(loaded).toEqual(items)+ expect(loaded).toEqual([+ { ...items[0], id: reviewIdFor(items[0]) },+ { ...items[1], id: reviewIdFor(items[1]) },+ ])})it("creates the .llm-wiki directory on first save", async () => {@@ -97,10 +100,31 @@ describe("review persistence — round-trip", () => {]await saveReviewItems(tmp.path, items)const loaded = await loadReviewItems(tmp.path)- expect(loaded).toEqual(items)+ expect(loaded.map((item) => item.title)).toEqual(items.map((item) => item.title))+ expect(loaded.map((item) => item.id)).toEqual(items.map((item) => reviewIdFor(item)))expect(loaded[0].title).toBe("注意力机制")})+ it("migrates old counter review ids and collapses duplicate content on load", async () => {+ await writeFileRaw(`${tmp.path}/.llm-wiki/review.json`, JSON.stringify([+ makeReview({ id: "review-1", title: "Attention", resolved: false, affectedPages: ["a.md"] }),+ makeReview({+ id: "review-2",+ title: "Missing page: Attention",+ resolved: true,+ resolvedAction: "user-resolved",+ affectedPages: ["b.md"],+ }),+ ]))++ const loaded = await loadReviewItems(tmp.path)+ expect(loaded).toHaveLength(1)+ expect(loaded[0].id).toBe(reviewIdFor(loaded[0]))+ expect(loaded[0].resolved).toBe(true)+ expect(loaded[0].resolvedAction).toBe("user-resolved")+ expect(loaded[0].affectedPages).toEqual(expect.arrayContaining(["a.md", "b.md"]))+ })+it("overwrites existing file on subsequent saves", async () => {await saveReviewItems(tmp.path, [makeReview({ id: "r-1", title: "Old" })])await saveReviewItems(tmp.path, [makeReview({ id: "r-2", title: "New" })])diff --git a/src/lib/persist.ts b/src/lib/persist.tsindex 7e4b9891..b607f486 100644--- a/src/lib/persist.ts+++ b/src/lib/persist.ts@@ -1,5 +1,5 @@import { writeFile, readFile, createDirectory } from "@/commands/fs"-import type { ReviewItem } from "@/stores/review-store"+import { normalizeReviewItems, type ReviewItem } from "@/stores/review-store"import type { LintItem } from "@/stores/lint-store"import type { DisplayMessage, Conversation } from "@/stores/chat-store"import type { ChatAgentMode } from "@/lib/chat-agent"@@ -20,7 +20,7 @@ export async function loadReviewItems(projectPath: string): Promise<ReviewItem[]const pp = normalizePath(projectPath)try {const content = await readFile(`${pp}/.llm-wiki/review.json`)- return JSON.parse(content) as ReviewItem[]+ return normalizeReviewItems(JSON.parse(content) as ReviewItem[])} catch {return []}diff --git a/src/stores/review-store.test.ts b/src/stores/review-store.test.tsindex 327710a6..478b0109 100644--- a/src/stores/review-store.test.ts+++ b/src/stores/review-store.test.ts@@ -1,5 +1,5 @@import { describe, it, expect, beforeEach } from "vitest"-import { useReviewStore, reviewIdFor, type ReviewItem } from "./review-store"+import { normalizeReviewItems, useReviewStore, reviewIdFor, type ReviewItem } from "./review-store"// Minimal builder so each test only specifies what it cares about.function makeInput(overrides: Partial<Omit<ReviewItem, "id" | "resolved" | "createdAt">> = {}) {@@ -232,6 +232,57 @@ describe("review-store setItems — migrate-on-load", () => {expect(items[0].createdAt).toBe(2) // earliest})+ it("preserves a later duplicate resolvedAction when the first resolved item lacks one", () => {+ const normalized = normalizeReviewItems([+ {+ ...makeInput({ title: "Attention" }),+ id: "review-1",+ resolved: true,+ createdAt: 1,+ },+ {+ ...makeInput({ title: "Missing page: Attention" }),+ id: "review-2",+ resolved: true,+ resolvedAction: "user-resolved",+ createdAt: 2,+ },+ ])++ expect(normalized).toHaveLength(1)+ expect(normalized[0].resolved).toBe(true)+ expect(normalized[0].resolvedAction).toBe("user-resolved")+ })++ it("merges options when duplicate legacy items collapse", () => {+ const normalized = normalizeReviewItems([+ {+ ...makeInput({+ title: "Attention",+ options: [{ label: "Create", action: "create" }],+ }),+ id: "review-1",+ resolved: false,+ createdAt: 1,+ },+ {+ ...makeInput({+ title: "Missing page: Attention",+ options: [{ label: "Skip", action: "skip" }],+ }),+ id: "review-2",+ resolved: false,+ createdAt: 2,+ },+ ])++ expect(normalized).toHaveLength(1)+ expect(normalized[0].options).toEqual([+ { label: "Create", action: "create" },+ { label: "Skip", action: "skip" },+ ])+ })+it("is idempotent — loading already-stable ids changes nothing", () => {const stable = reviewIdFor({ type: "missing-page", title: "Attention" })useReviewStore.getState().setItems([diff --git a/src/stores/review-store.ts b/src/stores/review-store.tsindex 916a1251..b28f9a00 100644--- a/src/stores/review-store.ts+++ b/src/stores/review-store.ts@@ -63,24 +63,45 @@ function unionField(a?: string[], b?: string[]): string[] | undefined {return merged.length > 0 ? merged : undefined}+function mergeOptions(a: ReviewOption[], b: ReviewOption[]): ReviewOption[] {+ const byAction = new Map<string, ReviewOption>()+ for (const option of [...a, ...b]) {+ byAction.set(option.action, option)+ }+ return [...byAction.values()]+}+/*** Collapse two items that resolved to the same stable id. resolved* wins (if either was resolved, the survivor is), union the array* fields, keep the earliest createdAt, prefer a non-empty description.*/function mergeReviewItems(a: ReviewItem, b: ReviewItem): ReviewItem {+ const resolved = a.resolved || b.resolved+ const resolvedAction = resolved ? a.resolvedAction ?? b.resolvedAction : undefinedreturn {...a, // a.id is kept; both share it by construction- resolved: a.resolved || b.resolved,- resolvedAction: a.resolved ? a.resolvedAction : b.resolved ? b.resolvedAction : a.resolvedAction,+ resolved,+ resolvedAction,description: a.description || b.description,sourcePath: a.sourcePath ?? b.sourcePath,affectedPages: unionField(a.affectedPages, b.affectedPages),searchQueries: unionField(a.searchQueries, b.searchQueries),+ options: mergeOptions(a.options, b.options),createdAt: Math.min(a.createdAt, b.createdAt),}}+export function normalizeReviewItems(items: ReviewItem[]): ReviewItem[] {+ const byId = new Map<string, ReviewItem>()+ for (const raw of items) {+ const remapped: ReviewItem = { ...raw, id: reviewIdFor(raw) }+ const existing = byId.get(remapped.id)+ byId.set(remapped.id, existing ? mergeReviewItems(existing, remapped) : remapped)+ }+ return [...byId.values()]+}+export const useReviewStore = create<ReviewState>((set) => ({items: [],@@ -139,13 +160,7 @@ export const useReviewStore = create<ReviewState>((set) => ({// resolved review keeps its resolution across the id-scheme change.// Computing the id from content (not the old id) makes this// idempotent — no migration-version flag needed.- const byId = new Map<string, ReviewItem>()- for (const raw of items) {- const remapped: ReviewItem = { ...raw, id: reviewIdFor(raw) }- const existing = byId.get(remapped.id)- byId.set(remapped.id, existing ? mergeReviewItems(existing, remapped) : remapped)- }- set({ items: [...byId.values()] })+ set({ items: normalizeReviewItems(items) })},resolveItem: (id, action) =>