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

Add graph search

nash_su19867a8814cf
diff --git a/src/components/graph/graph-view.tsx b/src/components/graph/graph-view.tsx
index 61469c49..b0947f63 100644
--- a/src/components/graph/graph-view.tsx
+++ b/src/components/graph/graph-view.tsx
@@ -16,6 +16,7 @@ import { queueResearch } from "@/lib/deep-research"
import { optimizeResearchTopic } from "@/lib/optimize-research-topic"
import { normalizePath } from "@/lib/path-utils"
import { applyGraphFilters, DEFAULT_GRAPH_FILTERS, hasActiveGraphFilters, type GraphFilterState } from "@/lib/graph-filters"
+import { applyGraphSearch } from "@/lib/graph-search"
import { useTranslation } from "react-i18next"
const NODE_TYPE_COLORS: Record<string, string> = {
@@ -333,6 +334,8 @@ export function GraphView() {
const [isResizing, setIsResizing] = useState(false)
const [legendCollapsed, setLegendCollapsed] = useState(false)
const [showFilters, setShowFilters] = useState(false)
+ const [graphSearchOpen, setGraphSearchOpen] = useState(false)
+ const [graphSearch, setGraphSearch] = useState("")
const [filters, setFilters] = useState<GraphFilterState>(() => ({
...DEFAULT_GRAPH_FILTERS,
hiddenTypes: new Set(),
@@ -342,6 +345,7 @@ export function GraphView() {
const graphContainerRef = useRef<HTMLDivElement>(null)
// i18n node type labels (populated after mount to support language switching)
const [nodeTypeLabels, setNodeTypeLabels] = useState<Record<string, string>>({})
+ const graphSearchInputRef = useRef<HTMLInputElement>(null)
// Research confirmation dialog
const [researchDialog, setResearchDialog] = useState<{
@@ -391,6 +395,12 @@ export function GraphView() {
}
}, [loadGraph, dataVersion])
+ useEffect(() => {
+ if (!graphSearchOpen) return
+ const id = window.requestAnimationFrame(() => graphSearchInputRef.current?.focus())
+ return () => window.cancelAnimationFrame(id)
+ }, [graphSearchOpen])
+
const handleNodeClick = useCallback(
async (nodeId: string) => {
const node = nodes.find((n) => n.id === nodeId)
@@ -523,6 +533,11 @@ export function GraphView() {
() => applyGraphFilters(nodes, edges, filters),
[nodes, edges, filters],
)
+ const searchedGraph = useMemo(
+ () => applyGraphSearch(filteredGraph.nodes, filteredGraph.edges, graphSearch),
+ [filteredGraph.nodes, filteredGraph.edges, graphSearch],
+ )
+ const searchActive = graphSearch.trim().length > 0
const hiddenCount = nodes.length - filteredGraph.nodes.length
const filtersActive = hasActiveGraphFilters(filters)
const contextNode = nodeMenu ? nodes.find((node) => node.id === nodeMenu.nodeId) : null
@@ -575,8 +590,8 @@ export function GraphView() {
<span className="text-sm font-medium">{t("graph.knowledgeGraph")}</span>
</div>
<div className="flex items-center gap-1.5 text-xs text-muted-foreground">
- <span className="rounded bg-muted px-1.5 py-0.5">{filteredGraph.nodes.length}/{nodes.length} {t("graph.pages", { count: nodes.length })}</span>
- <span className="rounded bg-muted px-1.5 py-0.5">{filteredGraph.edges.length}/{edges.length} {t("graph.links", { count: edges.length })}</span>
+ <span className="rounded bg-muted px-1.5 py-0.5">{searchedGraph.nodes.length}/{nodes.length} {t("graph.pages", { count: nodes.length })}</span>
+ <span className="rounded bg-muted px-1.5 py-0.5">{searchedGraph.edges.length}/{edges.length} {t("graph.links", { count: edges.length })}</span>
{hiddenCount > 0 && (
<span className="rounded bg-amber-500/10 px-1.5 py-0.5 text-amber-700 dark:text-amber-300">
{hiddenCount} {t("graph.hidden")}
@@ -585,6 +600,51 @@ export function GraphView() {
</div>
</div>
<div className="flex items-center gap-1">
+ {graphSearchOpen || searchActive ? (
+ <div className="relative mr-1 w-52">
+ <Search className="pointer-events-none absolute left-2 top-1/2 h-3.5 w-3.5 -translate-y-1/2 text-muted-foreground" />
+ <input
+ ref={graphSearchInputRef}
+ value={graphSearch}
+ onChange={(e) => setGraphSearch(e.target.value)}
+ onKeyDown={(e) => {
+ if (e.key === "Escape") {
+ setGraphSearch("")
+ setGraphSearchOpen(false)
+ }
+ }}
+ className="h-7 w-full rounded-md border bg-background pl-7 pr-7 text-xs outline-none placeholder:text-muted-foreground focus:border-ring"
+ placeholder={t("graph.searchPlaceholder")}
+ aria-label={t("graph.searchLabel")}
+ />
+ <button
+ type="button"
+ className="absolute right-1.5 top-1/2 -translate-y-1/2 rounded p-0.5 text-muted-foreground hover:bg-accent hover:text-foreground"
+ onClick={() => {
+ if (searchActive) {
+ setGraphSearch("")
+ return
+ }
+ setGraphSearchOpen(false)
+ }}
+ aria-label={searchActive ? t("graph.clearSearch") : t("graph.closeSearch")}
+ title={searchActive ? t("graph.clearSearch") : t("graph.closeSearch")}
+ >
+ <X className="h-3.5 w-3.5" />
+ </button>
+ </div>
+ ) : (
+ <Button
+ variant="ghost"
+ size="sm"
+ onClick={() => setGraphSearchOpen(true)}
+ className="text-xs gap-1 h-7"
+ aria-label={t("graph.searchLabel")}
+ title={t("graph.searchLabel")}
+ >
+ <Search className="h-3.5 w-3.5" />
+ </Button>
+ )}
<Button
variant={showFilters ? "secondary" : "ghost"}
size="sm"
@@ -662,6 +722,16 @@ export function GraphView() {
<div className="flex h-full items-center justify-center text-xs text-muted-foreground">
{t("graph.resizing")}
</div>
+ ) : searchedGraph.nodes.length === 0 ? (
+ <div className="flex h-full flex-col items-center justify-center gap-2 text-muted-foreground">
+ <Search className="h-8 w-8 opacity-40" />
+ <p className="text-sm">{searchActive ? t("graph.noSearchResults") : t("graph.noVisibleNodes")}</p>
+ {searchActive && (
+ <Button variant="outline" size="sm" onClick={() => setGraphSearch("")}>
+ {t("graph.clearSearch")}
+ </Button>
+ )}
+ </div>
) : (
<ErrorBoundary>
<SigmaContainer
@@ -713,9 +783,9 @@ export function GraphView() {
},
}}
>
- <GraphLoader nodes={filteredGraph.nodes} edges={filteredGraph.edges} colorMode={colorMode} />
+ <GraphLoader nodes={searchedGraph.nodes} edges={searchedGraph.edges} colorMode={colorMode} />
<EventHandler onNodeClick={handleNodeClick} onNodeContextMenu={handleNodeContextMenu} />
- <HighlightManager highlightedNodes={highlightedNodes} />
+ <HighlightManager highlightedNodes={searchActive ? searchedGraph.matchedNodeIds : highlightedNodes} />
<ZoomControls />
</SigmaContainer>
</ErrorBoundary>
diff --git a/src/i18n/en.json b/src/i18n/en.json
index 27c7d3b0..2e9cd409 100644
--- a/src/i18n/en.json
+++ b/src/i18n/en.json
@@ -498,6 +498,12 @@
"hidden": "hidden",
"pages": "pages",
"links": "links",
+ "searchLabel": "Search graph",
+ "searchPlaceholder": "Search nodes...",
+ "clearSearch": "Clear graph search",
+ "closeSearch": "Close graph search",
+ "noSearchResults": "No matching nodes",
+ "noVisibleNodes": "No visible nodes",
"filter": "Filter",
"reset": "Reset",
"type": "Type",
diff --git a/src/i18n/zh.json b/src/i18n/zh.json
index 20fbff83..a07343e1 100644
--- a/src/i18n/zh.json
+++ b/src/i18n/zh.json
@@ -498,6 +498,12 @@
"hidden": "已隐藏",
"pages": "页面",
"links": "链接",
+ "searchLabel": "搜索关系图",
+ "searchPlaceholder": "搜索节点...",
+ "clearSearch": "清除关系图搜索",
+ "closeSearch": "关闭关系图搜索",
+ "noSearchResults": "没有匹配的节点",
+ "noVisibleNodes": "没有可见节点",
"filter": "过滤器",
"reset": "重置",
"type": "类型",
diff --git a/src/lib/graph-search.test.ts b/src/lib/graph-search.test.ts
new file mode 100644
index 00000000..db8e13f1
--- /dev/null
+++ b/src/lib/graph-search.test.ts
@@ -0,0 +1,39 @@
+import { describe, expect, it } from "vitest"
+import type { GraphEdge, GraphNode } from "@/lib/wiki-graph"
+import { applyGraphSearch } from "./graph-search"
+
+const nodes: GraphNode[] = [
+ { id: "vector-db", label: "Vector Database", type: "concept", path: "wiki/concepts/vector-db.md", linkCount: 2, community: 0 },
+ { id: "hinton", label: "Geoffrey Hinton", type: "entity", path: "wiki/entities/hinton.md", linkCount: 1, community: 0 },
+ { id: "raw-paper", label: "Source Paper", type: "source", path: "wiki/sources/source-paper.md", linkCount: 1, community: 1 },
+]
+
+const edges: GraphEdge[] = [
+ { source: "vector-db", target: "hinton", weight: 1 },
+ { source: "vector-db", target: "raw-paper", weight: 1 },
+]
+
+describe("applyGraphSearch", () => {
+ it("returns the original graph for an empty query", () => {
+ const out = applyGraphSearch(nodes, edges, " ")
+ expect(out.nodes).toHaveLength(3)
+ expect(out.edges).toHaveLength(2)
+ expect(out.matchedNodeIds.size).toBe(0)
+ })
+
+ it("matches by label, id, type, and path", () => {
+ expect(applyGraphSearch(nodes, edges, "vector").nodes.map((n) => n.id)).toEqual(["vector-db"])
+ expect(applyGraphSearch(nodes, edges, "entity").nodes.map((n) => n.id)).toEqual(["hinton"])
+ expect(applyGraphSearch(nodes, edges, "sources").nodes.map((n) => n.id)).toEqual(["raw-paper"])
+ })
+
+ it("keeps only edges between matched nodes", () => {
+ const out = applyGraphSearch(nodes, edges, "wiki")
+ expect(out.nodes).toHaveLength(3)
+ expect(out.edges).toHaveLength(2)
+
+ const sourceOnly = applyGraphSearch(nodes, edges, "paper")
+ expect(sourceOnly.nodes.map((n) => n.id)).toEqual(["raw-paper"])
+ expect(sourceOnly.edges).toHaveLength(0)
+ })
+})
diff --git a/src/lib/graph-search.ts b/src/lib/graph-search.ts
new file mode 100644
index 00000000..61cd27ff
--- /dev/null
+++ b/src/lib/graph-search.ts
@@ -0,0 +1,43 @@
+import type { GraphEdge, GraphNode } from "@/lib/wiki-graph"
+
+export interface GraphSearchResult {
+ nodes: GraphNode[]
+ edges: GraphEdge[]
+ matchedNodeIds: Set<string>
+}
+
+export function applyGraphSearch(
+ nodes: readonly GraphNode[],
+ edges: readonly GraphEdge[],
+ query: string,
+): GraphSearchResult {
+ const tokens = query
+ .toLowerCase()
+ .trim()
+ .split(/\s+/)
+ .filter(Boolean)
+
+ if (tokens.length === 0) {
+ return { nodes: [...nodes], edges: [...edges], matchedNodeIds: new Set() }
+ }
+
+ const matchedNodeIds = new Set<string>()
+ const matchedNodes = nodes.filter((node) => {
+ const haystack = [
+ node.label,
+ node.id,
+ node.type,
+ node.path,
+ ].join(" ").toLowerCase()
+ const matched = tokens.every((token) => haystack.includes(token))
+ if (matched) matchedNodeIds.add(node.id)
+ return matched
+ })
+
+ const visibleNodeIds = new Set(matchedNodes.map((node) => node.id))
+ const visibleEdges = edges.filter(
+ (edge) => visibleNodeIds.has(edge.source) && visibleNodeIds.has(edge.target),
+ )
+
+ return { nodes: matchedNodes, edges: visibleEdges, matchedNodeIds }
+}