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

fix: naturally sort source imports

nash_sud8e7b5b93ff8
diff --git a/src/components/sources/sources-view.tsx b/src/components/sources/sources-view.tsx
index 993b9537..ba51a43c 100644
--- a/src/components/sources/sources-view.tsx
+++ b/src/components/sources/sources-view.tsx
@@ -11,6 +11,7 @@ import { useTranslation } from "react-i18next"
import { normalizePath } from "@/lib/path-utils"
import { decideDeleteClick } from "@/lib/sources-tree-delete"
import { rescanProjectFileSync } from "@/lib/project-file-sync"
+import { naturalCompare } from "@/lib/natural-sort"
import {
deleteSourceFile,
deleteSourceFolder,
@@ -397,7 +398,7 @@ function sortSourceNodes(nodes: readonly FileNode[]): FileNode[] {
return [...nodes].sort((a, b) => {
if (a.is_dir && !b.is_dir) return -1
if (!a.is_dir && b.is_dir) return 1
- return a.name.localeCompare(b.name)
+ return naturalCompare(a.name, b.name)
})
}
diff --git a/src/lib/natural-sort.test.ts b/src/lib/natural-sort.test.ts
new file mode 100644
index 00000000..7efc3289
--- /dev/null
+++ b/src/lib/natural-sort.test.ts
@@ -0,0 +1,41 @@
+import { describe, expect, it } from "vitest"
+
+import { naturalCompare } from "./natural-sort"
+
+describe("naturalCompare", () => {
+ it("sorts numbered filenames in human numeric order", () => {
+ const names = ["100.md", "11.md", "1.md", "10.md", "file10.md", "2.md", "file2.md"]
+
+ expect([...names].sort(naturalCompare)).toEqual([
+ "1.md",
+ "2.md",
+ "10.md",
+ "11.md",
+ "100.md",
+ "file2.md",
+ "file10.md",
+ ])
+ })
+
+ it("uses a deterministic fallback for natural ties", () => {
+ const names = ["cafe.md", "Café.md", "1.md", "01.md"]
+
+ expect([...names].sort(naturalCompare)).toEqual([
+ "01.md",
+ "1.md",
+ "Café.md",
+ "cafe.md",
+ ])
+ })
+
+ it("sorts case-insensitively before applying the fallback", () => {
+ const names = ["Beta.md", "alpha.md", "10.md", "2.md"]
+
+ expect([...names].sort(naturalCompare)).toEqual([
+ "2.md",
+ "10.md",
+ "alpha.md",
+ "Beta.md",
+ ])
+ })
+})
diff --git a/src/lib/natural-sort.ts b/src/lib/natural-sort.ts
new file mode 100644
index 00000000..7e329908
--- /dev/null
+++ b/src/lib/natural-sort.ts
@@ -0,0 +1,11 @@
+const NATURAL_COLLATOR = new Intl.Collator(undefined, {
+ numeric: true,
+ sensitivity: "base",
+})
+
+export function naturalCompare(a: string, b: string): number {
+ const natural = NATURAL_COLLATOR.compare(a, b)
+ if (natural !== 0) return natural
+ if (a === b) return 0
+ return a < b ? -1 : 1
+}
diff --git a/src/lib/source-lifecycle.test.ts b/src/lib/source-lifecycle.test.ts
index 39804d6f..2af9abba 100644
--- a/src/lib/source-lifecycle.test.ts
+++ b/src/lib/source-lifecycle.test.ts
@@ -181,4 +181,54 @@ describe("source-lifecycle path helpers", () => {
},
])
})
+
+ it("naturally orders imported folder files before enqueueing ingest tasks", async () => {
+ mocks.listDirectory.mockResolvedValue([
+ { name: "10.md", path: "/external/imported/10.md", is_dir: false },
+ { name: "2.md", path: "/external/imported/2.md", is_dir: false },
+ { name: "1.md", path: "/external/imported/1.md", is_dir: false },
+ ])
+
+ const copied = await importSourceFolder(
+ { id: "p1", name: "Project", path: "/project" },
+ "/external/imported",
+ {
+ provider: "openai",
+ endpoint: "https://api.example.com/v1",
+ apiKey: "key",
+ model: "model",
+ customModel: "",
+ reasoning: { enabled: false, effort: "low" },
+ } as never,
+ {
+ enabled: true,
+ autoIngest: true,
+ includeExtensions: ["md"],
+ excludeExtensions: [],
+ excludeDirs: [],
+ excludeGlobs: [],
+ maxFileSizeMb: 100,
+ },
+ )
+
+ expect(copied).toEqual([
+ "/project/raw/sources/imported/1.md",
+ "/project/raw/sources/imported/2.md",
+ "/project/raw/sources/imported/10.md",
+ ])
+ expect(mocks.enqueueBatch).toHaveBeenCalledWith("p1", [
+ {
+ sourcePath: "/project/raw/sources/imported/1.md",
+ folderContext: "imported",
+ },
+ {
+ sourcePath: "/project/raw/sources/imported/2.md",
+ folderContext: "imported",
+ },
+ {
+ sourcePath: "/project/raw/sources/imported/10.md",
+ folderContext: "imported",
+ },
+ ])
+ })
})
diff --git a/src/lib/source-lifecycle.ts b/src/lib/source-lifecycle.ts
index d6088286..4d863246 100644
--- a/src/lib/source-lifecycle.ts
+++ b/src/lib/source-lifecycle.ts
@@ -34,6 +34,7 @@ import {
} from "@/lib/wiki-cleanup"
import { collectAllFilesIncludingDot } from "@/lib/sources-tree-delete"
import { isPathAllowedBySourceWatch, normalizeSourceWatchConfig } from "@/lib/source-watch-config"
+import { naturalCompare } from "@/lib/natural-sort"
import type { SourceWatchConfig } from "@/stores/wiki-store"
export const INGESTABLE_SOURCE_EXTENSIONS = new Set([
@@ -231,14 +232,18 @@ export async function importSourceFolder(
preprocessFile(destPath).catch(() => {})
}
+ const naturallyOrderedFiles = [...allowedFiles].sort((a, b) =>
+ naturalCompare(getRelativePath(a, destDir), getRelativePath(b, destDir)),
+ )
+
if (hasUsableLlm(llmConfig)) {
- await enqueueSourceIngest(project, allowedFiles, llmConfig, {
+ await enqueueSourceIngest(project, naturallyOrderedFiles, llmConfig, {
sourceRoot: destDir,
rootContext: folderName,
})
}
- return allowedFiles
+ return naturallyOrderedFiles
}
export async function deleteSourceFile(