AI, fully local on your computer.
TypeScript aiai-toolskiwixllama-cpplocal-ailocal-llmofflineoffline-aioffline-firstprivacy-firstragwikipediazim
import { existsSync, readdirSync } from 'node:fs'
import { join } from 'node:path'
import type { OcrEngine } from './index'
import { OCR_ORIENTATION_LANG } from './orientation'
import { createTesseractOcrEngine } from './tesseract'
// Availability-aware OCR selector, the transcriber/reranker pattern:
// NO mock fallback. A real `TesseractOcrEngine` is selected only when the drive's
// `ocr/` dir holds at least one vendored `<lang>.traineddata.gz`; otherwise the
// selector returns NULL — photo imports fail per-file with friendly copy and a
// detected scan shows its notice without the "Make searchable (OCR)" offer
// (availability-driven, no settings key).
/** The drive dir holding the vendored OCR language files (drive-layout.md). */
export function ocrAssetsDir(rootPath: string): string {
return join(rootPath, 'ocr')
}
/**
* Languages available in an assets dir: every `<lang>.traineddata.gz` (the shipped
* layout; plain `.traineddata` is NOT accepted — one layout, one code path). Sorted
* for determinism; 'deu' and 'eng' are what the build pipeline vendors. The orientation
* data (`osd.traineddata.gz`, #538) is not a language and is left out: the recognizer starts
* LSTM-only, which cannot load it.
*/
export function listOcrLanguages(assetsDir: string): string[] {
if (!existsSync(assetsDir)) return []
let names: string[]
try {
names = readdirSync(assetsDir)
} catch {
return []
}
return names
.filter((n) => n.endsWith('.traineddata.gz'))
.map((n) => n.slice(0, -'.traineddata.gz'.length))
.filter((lang) => /^[a-z_]{3,}$/i.test(lang) && lang !== OCR_ORIENTATION_LANG)
.sort()
}
export interface OcrSelectionDeps {
/** Drive root — the assets live at `<root>/ocr/`. */
rootPath: string
makeEngine?: (langDir: string, languages: string[]) => OcrEngine
onSelect?: (kind: 'tesseract' | 'none', reason: string) => void
/**
* Packaged builds start the engine `'probing'`: `ocrAvailable` stays false until the startup
* probe (`engine.probe()`, run by the main wiring) proves the worker loads. Dev builds start
* `'available'`. Presence still decides null-vs-engine. (#232)
*/
probeRequired?: boolean
}
/**
* Build the active `OcrEngine`, or null when the language files are absent.
* Construction is cheap (the tesseract worker starts lazily on first recognition),
* so this returns synchronously like the transcriber selector.
*/
export function createSelectedOcrEngine(deps: OcrSelectionDeps): OcrEngine | null {
const dir = ocrAssetsDir(deps.rootPath)
const makeEngine =
deps.makeEngine ??
((langDir: string, languages: string[]) =>
createTesseractOcrEngine({ langDir, languages, probeRequired: deps.probeRequired === true }))
const languages = listOcrLanguages(dir)
if (languages.length === 0) {
deps.onSelect?.('none', 'no OCR language files on the drive')
return null
}
deps.onSelect?.('tesseract', `language files present: ${languages.join(', ')}`)
return makeEngine(dir, languages)
}