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

refactor: unify vector search in search.ts, remove duplicate from chat-panel

nash_subbe67de985d6
diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock
index 962c6562..e761bb8c 100644
--- a/src-tauri/Cargo.lock
+++ b/src-tauri/Cargo.lock
@@ -2344,7 +2344,7 @@ checksum = "92daf443525c4cce67b150400bc2316076100ce0b3686209eb8cf3c31612e6f0"
[[package]]
name = "llm-wiki"
-version = "0.1.0"
+version = "0.2.0"
dependencies = [
"calamine",
"chrono",
diff --git a/src/components/chat/chat-panel.tsx b/src/components/chat/chat-panel.tsx
index 02855425..cb098037 100644
--- a/src/components/chat/chat-panel.tsx
+++ b/src/components/chat/chat-panel.tsx
@@ -213,60 +213,9 @@ export function ChatPanel() {
}
}
- // ── Phase 1.5: Vector search (if embedding enabled) ─────
- const embCfg = useWikiStore.getState().embeddingConfig
- if (embCfg.enabled && embCfg.model) {
- try {
- const { searchByEmbedding } = await import("@/lib/embedding")
- const llmCfg = useWikiStore.getState().llmConfig
- const vectorResults = await searchByEmbedding(pp, text, llmCfg, embCfg, 10)
- // Merge vector results into search results (avoid duplicates)
- const existingPaths = new Set(topSearchResults.map((r) => r.path))
- for (const vr of vectorResults) {
- const pagePath = `${pp}/wiki`
- // Find matching file in search results by page ID
- const matchingSearch = searchResults.find((r) => {
- const fileName = getFileName(r.path).replace(/\.md$/, "")
- return fileName === vr.id
- })
- if (matchingSearch && !existingPaths.has(matchingSearch.path)) {
- topSearchResults.push(matchingSearch)
- existingPaths.add(matchingSearch.path)
- } else if (!matchingSearch) {
- // Try to find the file path by scanning wiki directories
- const possiblePaths = [
- `${pp}/wiki/entities/${vr.id}.md`,
- `${pp}/wiki/concepts/${vr.id}.md`,
- `${pp}/wiki/sources/${vr.id}.md`,
- `${pp}/wiki/synthesis/${vr.id}.md`,
- `${pp}/wiki/comparison/${vr.id}.md`,
- `${pp}/wiki/queries/${vr.id}.md`,
- ]
- for (const tryPath of possiblePaths) {
- if (!existingPaths.has(tryPath)) {
- try {
- await readFile(tryPath)
- topSearchResults.push({
- path: tryPath,
- title: vr.id.replace(/-/g, " "),
- score: vr.score * 10,
- titleMatch: false,
- })
- existingPaths.add(tryPath)
- break
- } catch {
- // file doesn't exist at this path
- }
- }
- }
- }
- }
- } catch {
- // Vector search failed, continue without it
- }
- }
-
// ── Phase 2: Graph 1-level expansion ───────────────────
+ // Note: Vector search (if enabled) is already merged into searchResults
+ // by searchWiki() in search.ts — no duplicate code needed here.
const graph = await buildRetrievalGraph(pp, dataVersion)
const expandedIds = new Set<string>()
const searchHitPaths = new Set(topSearchResults.map((r) => r.path))
diff --git a/src/lib/search.ts b/src/lib/search.ts
index 8fb6981f..8d35cf87 100644
--- a/src/lib/search.ts
+++ b/src/lib/search.ts
@@ -139,6 +139,56 @@ export async function searchWiki(
// no raw sources
}
+ // Vector search: merge semantic results if embedding enabled
+ try {
+ const { useWikiStore } = await import("@/stores/wiki-store")
+ const embCfg = useWikiStore.getState().embeddingConfig
+ if (embCfg.enabled && embCfg.model) {
+ const { searchByEmbedding } = await import("@/lib/embedding")
+ const llmCfg = useWikiStore.getState().llmConfig
+ const vectorResults = await searchByEmbedding(pp, query, llmCfg, embCfg, 10)
+
+ const existingPaths = new Set(results.map((r) => r.path))
+
+ for (const vr of vectorResults) {
+ // Check if already in results
+ const existing = results.find((r) => {
+ const fileName = r.path.split("/").pop()?.replace(/\.md$/, "") ?? ""
+ return fileName === vr.id
+ })
+
+ if (existing) {
+ // Boost score of existing result
+ existing.score += vr.score * 5
+ } else {
+ // Try to find the file and add it
+ const dirs = ["entities", "concepts", "sources", "synthesis", "comparison", "queries"]
+ for (const dir of dirs) {
+ const tryPath = `${pp}/wiki/${dir}/${vr.id}.md`
+ if (existingPaths.has(tryPath)) break
+ try {
+ const content = await readFile(tryPath)
+ const title = extractTitle(content, `${vr.id}.md`)
+ results.push({
+ path: tryPath,
+ title,
+ snippet: buildSnippet(content, query),
+ titleMatch: false,
+ score: vr.score * 5,
+ })
+ existingPaths.add(tryPath)
+ break
+ } catch {
+ // not in this directory
+ }
+ }
+ }
+ }
+ }
+ } catch {
+ // Vector search failed or not available, continue without it
+ }
+
// Sort by score descending
results.sort((a, b) => b.score - a.score)
return results.slice(0, MAX_RESULTS)