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: filter knowledge by source

nash_su92f3cbaa360e
diff --git a/src/components/layout/knowledge-tree.tsx b/src/components/layout/knowledge-tree.tsx
index 6b79f7d3..dadaec49 100644
--- a/src/components/layout/knowledge-tree.tsx
+++ b/src/components/layout/knowledge-tree.tsx
@@ -1,4 +1,4 @@
-import { useState, useEffect, useCallback } from "react"
+import { useState, useEffect, useCallback, useMemo } from "react"
import {
FileText, Users, Lightbulb, BookOpen, HelpCircle, GitMerge, BarChart3, TrendingUp, Target, ChevronRight, ChevronDown, Layout, Globe, Trash2,
} from "lucide-react"
@@ -14,6 +14,8 @@ import { inferWikiTypeFromPath, wikiTypeLabel } from "@/lib/wiki-page-types"
import { filterRawSourceTree } from "@/lib/source-filter"
import { useTranslation } from "react-i18next"
import { useAppDialog } from "@/stores/app-dialog-store"
+import { parseSources } from "@/lib/sources-merge"
+import { filterPagesBySource, listPageSourceIdentities } from "@/lib/knowledge-source-filter"
interface WikiPageInfo {
path: string
@@ -21,6 +23,7 @@ interface WikiPageInfo {
type: string
tags: string[]
origin?: string
+ sources: string[]
}
const TYPE_CONFIG: Record<string, { icon: typeof FileText; label: string; color: string; order: number }> = {
@@ -49,6 +52,7 @@ export function KnowledgeTree() {
const openPathInPreview = useWikiStore((s) => s.openPathInPreview)
const dataVersion = useWikiStore((s) => s.dataVersion)
const [pages, setPages] = useState<WikiPageInfo[]>([])
+ const [selectedSource, setSelectedSource] = useState<string | null>(null)
const [expandedTypes, setExpandedTypes] = useState<Set<string>>(new Set(["overview", "entity", "concept", "source"]))
// Two-stage delete: first click arms the row, second click executes.
// Only one row armed at a time (clicking another row replaces).
@@ -76,6 +80,7 @@ export function KnowledgeTree() {
title: file.name.replace(".md", "").replace(/-/g, " "),
type: "other",
tags: [],
+ sources: [],
})
}
}
@@ -93,6 +98,20 @@ export function KnowledgeTree() {
loadPages()
}, [loadPages, dataVersion])
+ useEffect(() => {
+ setSelectedSource(null)
+ }, [project?.id])
+
+ const sourceOptions = useMemo(() => listPageSourceIdentities(pages), [pages])
+ const visiblePages = useMemo(
+ () => filterPagesBySource(pages, selectedSource),
+ [pages, selectedSource],
+ )
+
+ useEffect(() => {
+ if (selectedSource && !sourceOptions.includes(selectedSource)) setSelectedSource(null)
+ }, [selectedSource, sourceOptions])
+
const handleDeleteClick = useCallback(
async (pagePath: string) => {
if (!project) return
@@ -137,7 +156,7 @@ export function KnowledgeTree() {
// Group pages by type
const grouped = new Map<string, WikiPageInfo[]>()
- for (const page of pages) {
+ for (const page of visiblePages) {
const list = grouped.get(page.type) ?? []
list.push(page)
grouped.set(page.type, list)
@@ -167,6 +186,25 @@ export function KnowledgeTree() {
{project.name}
</div>
+ {sourceOptions.length > 1 && (
+ <div className="mb-2 px-2">
+ <label className="sr-only" htmlFor="knowledge-source-filter">
+ {t("sidebar.filterBySource")}
+ </label>
+ <select
+ id="knowledge-source-filter"
+ value={selectedSource ?? ""}
+ onChange={(event) => setSelectedSource(event.target.value || null)}
+ className="h-8 w-full rounded-md border border-input bg-background px-2 text-xs text-foreground outline-none focus:ring-2 focus:ring-ring"
+ >
+ <option value="">{t("sidebar.allSources")}</option>
+ {sourceOptions.map((source) => (
+ <option key={source} value={source}>{source}</option>
+ ))}
+ </select>
+ </div>
+ )}
+
{sortedGroups.length === 0 && (
<div className="px-2 py-4 text-center text-xs text-muted-foreground">
{t("sidebar.noWikiPages")}
@@ -344,7 +382,7 @@ function parsePageInfo(path: string, fileName: string, content: string): WikiPag
type = inferWikiTypeFromPath(path, fileName) ?? "other"
}
- return { path, title, type, tags, origin }
+ return { path, title, type, tags, origin, sources: parseSources(content) }
}
/**
diff --git a/src/i18n/en.json b/src/i18n/en.json
index f3a5ed19..b39112f8 100644
--- a/src/i18n/en.json
+++ b/src/i18n/en.json
@@ -45,6 +45,8 @@
"knowledge": "Knowledge",
"files": "Files",
"rawSources": "Raw Sources",
+ "filterBySource": "Filter knowledge by source",
+ "allSources": "All sources",
"noWikiPages": "No wiki pages yet. Import sources to get started.",
"typeLabels": {
"overview": "Overview", "entity": "Entities", "concept": "Concepts",
diff --git a/src/i18n/it.json b/src/i18n/it.json
index a51be18b..78781e1b 100644
--- a/src/i18n/it.json
+++ b/src/i18n/it.json
@@ -45,6 +45,8 @@
"knowledge": "Conoscenza",
"files": "File",
"rawSources": "Fonti grezze",
+ "filterBySource": "Filtra la conoscenza per fonte",
+ "allSources": "Tutte le fonti",
"noWikiPages": "Ancora nessuna pagina wiki. Importa delle fonti per iniziare.",
"typeLabels": {
"overview": "Panoramica", "entity": "Entità", "concept": "Concetti",
diff --git a/src/i18n/ru.json b/src/i18n/ru.json
index b5980dac..b6c00c88 100644
--- a/src/i18n/ru.json
+++ b/src/i18n/ru.json
@@ -45,6 +45,8 @@
"knowledge": "Знания",
"files": "Файлы",
"rawSources": "Исходные источники",
+ "filterBySource": "Фильтр знаний по источнику",
+ "allSources": "Все источники",
"noWikiPages": "Пока нет страниц вики. Импортируйте источники, чтобы начать.",
"typeLabels": {
"overview": "Обзор", "entity": "Сущности", "concept": "Понятия",
diff --git a/src/i18n/zh.json b/src/i18n/zh.json
index 1f3d5333..296b5cfa 100644
--- a/src/i18n/zh.json
+++ b/src/i18n/zh.json
@@ -45,6 +45,8 @@
"knowledge": "知识库",
"files": "文件",
"rawSources": "原始资料",
+ "filterBySource": "按原始资料筛选知识",
+ "allSources": "全部资料",
"noWikiPages": "暂无 Wiki 页面,请先导入原始资料。",
"typeLabels": {
"overview": "概览", "entity": "实体", "concept": "概念",
diff --git a/src/lib/knowledge-source-filter.test.ts b/src/lib/knowledge-source-filter.test.ts
new file mode 100644
index 00000000..837bd3f0
--- /dev/null
+++ b/src/lib/knowledge-source-filter.test.ts
@@ -0,0 +1,35 @@
+import { describe, expect, it } from "vitest"
+import {
+ filterPagesBySource,
+ listPageSourceIdentities,
+ normalizeSourceIdentity,
+} from "./knowledge-source-filter"
+
+const pages = [
+ { title: "A", sources: ["BookA.pdf"] },
+ { title: "Shared", sources: ["BookA.pdf", "folder/BookB.pdf"] },
+ { title: "B", sources: ["raw/sources/folder/BookB.pdf"] },
+ { title: "Manual", sources: [] },
+]
+
+describe("knowledge source filtering", () => {
+ it("normalizes stored and project-relative source identities", () => {
+ expect(normalizeSourceIdentity("raw\\sources\\Folder\\Book.pdf")).toBe("folder/book.pdf")
+ })
+
+ it("lists unique source identities in natural order", () => {
+ expect(listPageSourceIdentities([
+ ...pages,
+ { title: "Duplicate", sources: ["booka.PDF"] },
+ { title: "Later", sources: ["Book10.pdf", "Book2.pdf"] },
+ ])).toEqual(["Book2.pdf", "Book10.pdf", "BookA.pdf", "folder/BookB.pdf"])
+ })
+
+ it("shows only pages linked to the selected source", () => {
+ expect(filterPagesBySource(pages, "folder/bookb.PDF").map((page) => page.title)).toEqual([
+ "Shared",
+ "B",
+ ])
+ expect(filterPagesBySource(pages, null)).toEqual(pages)
+ })
+})
diff --git a/src/lib/knowledge-source-filter.ts b/src/lib/knowledge-source-filter.ts
new file mode 100644
index 00000000..63dc1228
--- /dev/null
+++ b/src/lib/knowledge-source-filter.ts
@@ -0,0 +1,38 @@
+import { normalizePath } from "@/lib/path-utils"
+
+export interface SourceLinkedPage {
+ sources: readonly string[]
+}
+
+export function normalizeSourceIdentity(source: string): string {
+ return normalizePath(source)
+ .replace(/^\.\//, "")
+ .replace(/^raw\/sources\//i, "")
+ .toLocaleLowerCase()
+}
+
+export function listPageSourceIdentities<T extends SourceLinkedPage>(pages: readonly T[]): string[] {
+ const identities = new Map<string, string>()
+ for (const page of pages) {
+ for (const source of page.sources) {
+ const trimmed = source.trim()
+ const key = normalizeSourceIdentity(trimmed)
+ if (key && !identities.has(key)) identities.set(key, trimmed)
+ }
+ }
+ return [...identities.values()].sort((a, b) => a.localeCompare(b, undefined, {
+ numeric: true,
+ sensitivity: "base",
+ }))
+}
+
+export function filterPagesBySource<T extends SourceLinkedPage>(
+ pages: readonly T[],
+ selectedSource: string | null,
+): T[] {
+ if (!selectedSource) return [...pages]
+ const selectedIdentity = normalizeSourceIdentity(selectedSource)
+ return pages.filter((page) =>
+ page.sources.some((source) => normalizeSourceIdentity(source) === selectedIdentity),
+ )
+}