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: scope image captions by output language

nash_su3f662100a3fd
diff --git a/src/lib/image-caption-pipeline.test.ts b/src/lib/image-caption-pipeline.test.ts
index 96ae462c..3e297d6a 100644
--- a/src/lib/image-caption-pipeline.test.ts
+++ b/src/lib/image-caption-pipeline.test.ts
@@ -27,7 +27,7 @@ vi.mock("@/commands/fs", () => ({
readFileAsBase64: (p: string) => mockReadBase64(p),
}))
-import { captionMarkdownImages, __test } from "./image-caption-pipeline"
+import { captionMarkdownImages, loadCaptionCache, __test } from "./image-caption-pipeline"
import type { LlmConfig } from "@/stores/wiki-store"
const cfg: LlmConfig = {
@@ -133,6 +133,61 @@ describe("captionMarkdownImages", () => {
)
})
+ it("does not reuse a caption cached for a different output language", async () => {
+ mockReadBase64.mockResolvedValue({ base64: "AAAA", mimeType: "image/png" })
+ mockCaption.mockResolvedValue("ein rotes Quadrat")
+ const knownHash = await __test.sha256OfBase64("AAAA")
+ mockFileExists.mockResolvedValue(true)
+ mockReadFile.mockResolvedValue(JSON.stringify({
+ [knownHash]: {
+ caption: "a red square",
+ mimeType: "image/png",
+ model: "vl-old",
+ capturedAt: "2026-01-01T00:00:00Z",
+ },
+ }))
+
+ const out = await captionMarkdownImages("/proj", "![](/abs/img-1.png)", cfg, {
+ outputLanguage: "German",
+ })
+
+ expect(out.freshCaptions).toBe(1)
+ expect(out.cachedCaptions).toBe(0)
+ expect(mockCaption).toHaveBeenCalledTimes(1)
+ const written = JSON.parse(mockWriteFile.mock.calls[0][1] as string)
+ expect(written[__test.captionCacheKey(knownHash, "German")]).toMatchObject({
+ caption: "ein rotes Quadrat",
+ imageHash: knownHash,
+ outputLanguage: "German",
+ })
+ })
+
+ it("reuses a caption cached for the same output language", async () => {
+ mockReadBase64.mockResolvedValue({ base64: "AAAA", mimeType: "image/png" })
+ const knownHash = await __test.sha256OfBase64("AAAA")
+ const key = __test.captionCacheKey(knownHash, "German")
+ mockFileExists.mockResolvedValue(true)
+ mockReadFile.mockResolvedValue(JSON.stringify({
+ [key]: {
+ caption: "ein rotes Quadrat",
+ mimeType: "image/png",
+ model: "vl-old",
+ capturedAt: "2026-01-01T00:00:00Z",
+ imageHash: knownHash,
+ outputLanguage: "German",
+ },
+ }))
+
+ const out = await captionMarkdownImages("/proj", "![](/abs/img-1.png)", cfg, {
+ outputLanguage: "german",
+ })
+
+ expect(out.cachedCaptions).toBe(1)
+ expect(out.freshCaptions).toBe(0)
+ expect(mockCaption).not.toHaveBeenCalled()
+ expect(out.enrichedMarkdown).toContain("![ein rotes Quadrat]")
+ })
+
it("dedupes by SHA-256: two refs to the same bytes → one LLM call, both rewritten", async () => {
// Both URLs return the same base64 bytes — same hash → single
// caption call, both alt-texts populated.
@@ -381,3 +436,32 @@ describe("captionMarkdownImages", () => {
expect(opts.contextAfter).toBe("")
})
})
+
+describe("loadCaptionCache", () => {
+ it("selects the newest caption per image when no language is requested", async () => {
+ const knownHash = await __test.sha256OfBase64("AAAA")
+ const germanKey = __test.captionCacheKey(knownHash, "German")
+ mockFileExists.mockResolvedValue(true)
+ // Put the legacy entry last to prove selection does not depend on JSON key order.
+ mockReadFile.mockResolvedValue(JSON.stringify({
+ [germanKey]: {
+ caption: "neue Beschreibung",
+ mimeType: "image/png",
+ model: "vl-new",
+ capturedAt: "2026-08-18T00:00:00Z",
+ imageHash: knownHash,
+ outputLanguage: "German",
+ },
+ [knownHash]: {
+ caption: "old description",
+ mimeType: "image/png",
+ model: "vl-old",
+ capturedAt: "2026-01-01T00:00:00Z",
+ },
+ }))
+
+ const captions = await loadCaptionCache("/proj")
+
+ expect(captions.get(knownHash)).toBe("neue Beschreibung")
+ })
+})
diff --git a/src/lib/image-caption-pipeline.ts b/src/lib/image-caption-pipeline.ts
index 4998c90a..9f7e1336 100644
--- a/src/lib/image-caption-pipeline.ts
+++ b/src/lib/image-caption-pipeline.ts
@@ -15,7 +15,8 @@
* texted image carries enough semantic load that the model
* preserves it through paraphrasing.
*
- * Cache key = SHA-256 of image bytes. This makes duplicate images
+ * Cache key = SHA-256 of image bytes plus the requested output language.
+ * This makes duplicate images
* across PDFs (logos, page headers, recurring chart templates) a
* single LLM call across the whole project — without it, a corpus
* of slide decks with shared brand assets would caption the same
@@ -23,7 +24,9 @@
*
* Cache file lives at
* `<project>/.llm-wiki/image-caption-cache.json`
- * keyed `{ "<sha256>": { caption, mimeType, model, capturedAt } }`.
+ * Legacy language-neutral entries remain keyed by SHA-256. Language-aware
+ * entries include the image hash and language in their metadata so changing a
+ * project's output language cannot silently reuse a caption in the old one.
* The model + capturedAt fields aren't read by anything yet but
* shipping the metadata now means we can implement Phase 4's
* "re-caption with new model" without a second cache version.
@@ -44,6 +47,8 @@ interface CaptionEntry {
mimeType: string
model: string
capturedAt: string
+ imageHash?: string
+ outputLanguage?: string
}
type CaptionCache = Record<string, CaptionEntry>
@@ -79,15 +84,39 @@ async function sha256OfBase64(b64: string): Promise<string> {
*/
export async function loadCaptionCache(
projectPath: string,
+ outputLanguage?: string,
): Promise<Map<string, string>> {
const cache = await readCache(projectPath)
const out = new Map<string, string>()
- for (const [hash, entry] of Object.entries(cache)) {
+ const selectedAt = new Map<string, number>()
+ const language = normalizeCaptionLanguage(outputLanguage)
+ for (const [key, entry] of Object.entries(cache)) {
+ const hash = entry.imageHash ?? (/^[a-f0-9]{64}$/i.test(key) ? key : null)
+ if (!hash) continue
+ const entryLanguage = normalizeCaptionLanguage(entry.outputLanguage)
+ if (language && entryLanguage !== language) continue
+ if (!language) {
+ const capturedAt = Date.parse(entry.capturedAt)
+ const timestamp = Number.isFinite(capturedAt) ? capturedAt : 0
+ if ((selectedAt.get(hash) ?? -1) > timestamp) continue
+ selectedAt.set(hash, timestamp)
+ }
out.set(hash, entry.caption)
}
return out
}
+function normalizeCaptionLanguage(outputLanguage?: string): string {
+ const language = outputLanguage?.trim()
+ if (!language || language.toLowerCase() === "auto") return ""
+ return language.normalize("NFKC").toLowerCase()
+}
+
+function captionCacheKey(imageHash: string, outputLanguage?: string): string {
+ const language = normalizeCaptionLanguage(outputLanguage)
+ return language ? `${imageHash}::language:${encodeURIComponent(language)}` : imageHash
+}
+
async function readCache(projectPath: string): Promise<CaptionCache> {
const cachePath = `${normalizePath(projectPath)}/${CACHE_REL_PATH}`
if (!(await fileExists(cachePath))) return {}
@@ -384,7 +413,8 @@ export async function captionMarkdownImages(
}
const hash = await sha256OfBase64(bytes.base64)
- const hit = cache[hash]
+ const cacheKey = captionCacheKey(hash, options?.outputLanguage)
+ const hit = cache[cacheKey]
if (hit) {
captionByUrl.set(ref.url, hit.caption)
cachedCaptions++
@@ -409,11 +439,15 @@ export async function captionMarkdownImages(
outputLanguage: options?.outputLanguage,
},
)
- cache[hash] = {
+ cache[cacheKey] = {
caption,
mimeType: bytes.mimeType,
model: llmConfig.model,
capturedAt: new Date().toISOString(),
+ imageHash: hash,
+ ...(normalizeCaptionLanguage(options?.outputLanguage)
+ ? { outputLanguage: options?.outputLanguage?.trim() }
+ : {}),
}
captionByUrl.set(ref.url, caption)
freshCaptions++
@@ -482,4 +516,10 @@ export async function captionMarkdownImages(
// Exported for direct unit testing — keeps the module surface small
// while letting the test file pin behavior on the helpers.
-export const __test = { findImageReferences, sha256OfBase64, MD_IMAGE_RE }
+export const __test = {
+ captionCacheKey,
+ findImageReferences,
+ normalizeCaptionLanguage,
+ sha256OfBase64,
+ MD_IMAGE_RE,
+}
diff --git a/src/lib/ingest.ts b/src/lib/ingest.ts
index f8331ff4..f02fd0db 100644
--- a/src/lib/ingest.ts
+++ b/src/lib/ingest.ts
@@ -826,7 +826,13 @@ async function autoIngestImpl(
)
}
}
- await injectImagesIntoSourceSummary(pp, sourceIdentity, sourceSummarySlug, savedImages)
+ await injectImagesIntoSourceSummary(
+ pp,
+ sourceIdentity,
+ sourceSummarySlug,
+ savedImages,
+ getLanguagePromptName(getOutputLanguage(sourceContent)),
+ )
// Re-embed the source-summary page so caption text lands
// in the search index. Without this step, search by image
// content stays empty for files ingested before captioning
@@ -1347,7 +1353,13 @@ async function autoIngestImpl(
// want the safety-net section to slip image refs into the wiki
// through the back door.
if (mmCfg.enabled && savedImages.length > 0 && !signal?.aborted) {
- await injectImagesIntoSourceSummary(pp, sourceIdentity, sourceSummarySlug, savedImages)
+ await injectImagesIntoSourceSummary(
+ pp,
+ sourceIdentity,
+ sourceSummarySlug,
+ savedImages,
+ getLanguagePromptName(getOutputLanguage(sourceContent)),
+ )
}
if (writtenPaths.length > 0) {
@@ -3105,6 +3117,7 @@ async function injectImagesIntoSourceSummary(
sourceIdentity: string,
sourceSummarySlug: string,
savedImages: { relPath: string; page: number | null; sha256?: string }[],
+ outputLanguage?: string,
): Promise<void> {
if (savedImages.length === 0) return
const sourceSummaryPath = `wiki/sources/${sourceSummarySlug}.md`
@@ -3118,7 +3131,7 @@ async function injectImagesIntoSourceSummary(
// indexes whatever's in the wiki page, so without this, search
// by image content (e.g. "find the chart with revenue data")
// never matches because alt text was empty.
- const captionsBySha = await loadCaptionCache(pp)
+ const captionsBySha = await loadCaptionCache(pp, outputLanguage)
const newSection = buildImageMarkdownSection(
savedImages.map((img) => ({
...img,