/** * tesseract.js (WASM Tesseract, dual-environment) OCR specialist adapter battery. * * @module @nhtio/adk/batteries/specialists/ocr/tesseract_js/adapter * * @remarks * OCR battery backed by `tesseract.js` — Node and browsers, no native binary. Environment-neutral, * mirroring the transformers.js embeddings battery's dual-environment posture. * * **Divergence from `src/batteries/media/engines/tesseract_js.ts`:** that `MediaEngine` creates a * fresh worker **per convert call** and terminates it in a `finally` — correct for a stateless, * possibly-concurrent conversion pipeline, but it pays tesseract's ~1-2s WASM boot on every single * call. This adapter is a construct-once specialist object (the same posture as * {@link @nhtio/adk/batteries/embeddings/transformers_js!TransformersJsEmbeddingsAdapter}): a * consumer builds it once and calls {@link TesseractJsOcrAdapter.recognize} repeatedly, so it holds * **one cached worker**, created single-flight on first use (or via {@link preload}), and reused * across calls. `dispose()` terminates it; `reset()` also terminates it (see its own TSDoc for why * that differs from the embeddings adapter's `reset()`). Consumers who need the per-call-worker, * leak-free-by-construction posture should use the media engine instead. * * `tesseract.js` is an optional peer dependency, lazily imported on first actual use. */ import type { SpecialistImageInput } from "../../_shared/index"; import type { RecognizeOptions, RecognizeResult } from "./types"; /** * OCR adapter for `tesseract.js`. * * @remarks * Reusable: construct once, call {@link recognize} as many times as needed. The worker is * resolved lazily on first use (or via {@link preload}) and cached with single-flight semantics so * concurrent calls share one load. See the module remarks for how this deliberately diverges from * the per-call-worker `MediaEngine` in `src/batteries/media/engines/tesseract_js.ts`. */ export declare class TesseractJsOcrAdapter { #private; /** * Whether this battery is available. tesseract.js is environment-neutral (Node + browser), so * this is `true` whenever the runtime can import the peer. */ static isAvailable(): boolean; /** * @param options - Constructor options. Validated eagerly. * @throws {@link @nhtio/adk/batteries!E_INVALID_TESSERACT_JS_OCR_OPTIONS} when invalid. */ constructor(options: unknown); /** Instance availability probe (honours an injected `isAvailable`). */ isAvailable(): boolean; /** Eagerly loads (and caches) the worker so the first `recognize` call is fast. Idempotent. */ preload(): Promise; /** * Terminates the cached worker (if any) and drops the cached handle + in-flight load, so the * next call creates a fresh worker. * * @remarks * Unlike {@link @nhtio/adk/batteries/embeddings/transformers_js!TransformersJsEmbeddingsAdapter.reset}, * which only nulls the JS reference and leaves native resources for {@link dispose} to reclaim, * this `reset()` also terminates the worker. A live tesseract.js worker is the SAME heavy WASM * resource `dispose()` releases — no lighter "just drop the reference" tier exists for it (there * is no separate pipeline-session handle to keep warm), so leaving it running after `reset()` * would just leak it under a different method name. Swallows a terminate error (teardown must * not throw). Idempotent. */ reset(): Promise; /** * Terminates the cached worker and drops the cached handle. Alias of {@link reset} — both * reclaim the same underlying resource for this adapter (see {@link reset}'s TSDoc). */ dispose(): Promise; /** * Recognizes text in an image. * * @param input - The image/document input (bytes, bytes+MIME, or a `Media`-like value). * @param opts - Per-call options. `opts.languages`, when given, must equal the constructor's * `languages` (order-insensitive) — tesseract.js v7 workers do not support re-initializing an * already-created worker's languages via a public, stable API (there is no * `worker.reinitialize` re-language call safe to make on a warm worker without risking * cross-call state bleed), so a genuinely different subset throws * {@link @nhtio/adk/batteries!E_TESSERACT_JS_OCR_ENGINE_ERROR} explaining that per-call language * switching requires a new adapter instance. * @returns The recognized text and, when tesseract reports a numeric confidence, the mean * confidence (`0..100`). * @throws {@link @nhtio/adk/batteries!E_TESSERACT_JS_OCR_ENGINE_ERROR} when the worker fails to * load, the recognize call throws, or a per-call language override cannot be honored. */ recognize(input: SpecialistImageInput, opts?: RecognizeOptions): Promise; }