import type { Buffer } from "node:buffer"; /** Per-page OCR result. Shape mirrors `PdfPage` from src/pdf.ts. */ export interface OcrPdfPage { pageNumber: number; /** Extracted text. Empty string if Tesseract found no recognizable text. */ text: string; /** True if the page yielded no OCR text (rare even for blank pages). */ isEmpty: boolean; charCount: number; /** * Mean confidence score from Tesseract, 0-100. Page-level. Useful for * agents to detect bad OCR and recommend a higher-DPI rerun. */ confidence: number; } export interface OcrPdfResult { pages: OcrPdfPage[]; fullText: string; pageCount: number; /** True if at least one page yielded any OCR text. */ hasText: boolean; /** Mean confidence across all pages with text. NaN if no text. */ meanConfidence: number; /** Languages used for OCR (whatever was passed to extractPdfWithOcr). */ langs: string; } export interface ExtractPdfWithOcrOptions { /** * Tesseract language pack(s). Default `'eng'`. Multi-lang via `'+'`, * e.g. `'eng+rus'` for English+Russian mixed documents. * * v3.9.0-rc.10 (overclaim #16, ENFORCED) — language trained-data files * (`.traineddata`, ~10 MB each) must be PRE-DOWNLOADED into the local * tessdata cache ({@link resolveTessdataDir}). Tesseract.js would otherwise * silently CDN-fetch them on first use, violating README/SECURITY.md's "zero * outbound network calls in serve mode" guarantee. {@link assertOcrLangsInstalled} * verifies every requested pack exists locally BEFORE the worker is created * and throws (fail-closed) if any are missing — directing the user to run * `enquire-mcp install-ocr-lang ` (an explicit, opt-in network step, * parity with `install-model` for embeddings). The worker is additionally * pinned to the local cache (`langPath` + `cacheMethod: "readOnly"`), so no * runtime download path remains. */ langs?: string; /** * Directory holding the Tesseract `.traineddata` packs. Defaults to * {@link resolveTessdataDir}. Overriding it points both the pre-flight * existence check and the worker's `langPath` at a custom location (used by * the env-gated offline test). */ langPath?: string; /** * Page range (1-indexed inclusive). Default: all pages. Useful for * partial OCR of long documents — OCR is the slowest step in the * pipeline (~1-2s per page), so a 100-page paper takes minutes. * * v3.7.16 P1-2 — when omitted, the page count is capped at * `maxPages` (default 200) to bound CPU on adversarial / runaway * inputs. Caller can opt to process more by setting `maxPages` * explicitly OR by passing an explicit `pages` range. */ pages?: [number, number]; /** * v3.7.16 P1-2 — defense-in-depth safety cap. Even when the caller * doesn't pass `pages`, refuse to process more than this many pages * in one call to bound CPU/memory on adversarial PDFs. Default 200 * (~5-10 min on M1 CPU; longer than any realistic interactive use). * Pass `Number.POSITIVE_INFINITY` to opt out (background-job mode). */ maxPages?: number; /** * Render scale multiplier passed to pdfjs-dist's `getViewport({scale})`. * Higher = more pixels = better OCR accuracy on small text but more * memory + slower render. Default 2 (~150 DPI). Capped at 4 server-side * to prevent adversarial-PDF OOM. */ scale?: number; /** * Optional progress hook called once per page completion. Lets * long-running OCR jobs surface progress to stderr / agents. */ onProgress?: (page: number, total: number) => void; } /** v3.7.16 P1-2 — default safety cap on per-call OCR work. See * {@link ExtractPdfWithOcrOptions.maxPages}. */ export declare const DEFAULT_OCR_MAX_PAGES = 200; /** * v3.9.0-rc.10 — absolute cap on a rendered OCR page's largest pixel side. * The `scale` clamp ([0.5, 4]) bounds the MULTIPLIER, not the absolute pixel * count: a PDF MediaBox can be up to 14400×14400 pt (per the PDF spec), which * even at scale 1 renders to a multi-GB canvas → OOM. `extractPdfWithOcr` * lowers the per-page scale so the larger rendered side never exceeds this. */ export declare const MAX_OCR_CANVAS_DIM = 5000; /** * v3.9.0-rc.10 — compute the effective render scale so the LARGER rendered page * side never exceeds {@link MAX_OCR_CANVAS_DIM}. The `scale` clamp ([0.5, 4]) * bounds only the multiplier; this bounds the absolute pixel count, preventing * an OOM on an adversarially huge PDF MediaBox. Returns the requested scale * unchanged for normal-size pages. * * @param baseWidth - Page width in pt at scale 1. * @param baseHeight - Page height in pt at scale 1. * @param requestedScale - The caller's (already [0.5,4]-clamped) scale. * @returns The effective scale to render at (≤ requestedScale). */ export declare function clampOcrScale(baseWidth: number, baseHeight: number, requestedScale: number): number; /** * v3.9.0-rc.10 — resolve + validate a 1-indexed inclusive OCR page range * against the document's page count. Clamps to `[1, pageCount]`; throws on an * inverted/empty range (e.g. `pages:[5,2]`) instead of silently returning zero * pages (which a caller could misread as "image-only scan, no text"). * * @param pages - Optional `[from, to]` request (1-indexed inclusive). * @param pageCount - Total pages in the document. * @returns The clamped `[from, to]` range. * @throws {Error} If the resolved range is empty/inverted. */ export declare function resolveOcrPageRange(pages: [number, number] | undefined, pageCount: number): [number, number]; /** * Resolve the local directory that holds Tesseract `.traineddata` packs * (v3.9.0-rc.10 — overclaim #16 offline enforcement). Precedence: * 1. `$ENQUIRE_TESSDATA_DIR` (explicit override), * 2. `$XDG_CACHE_HOME/enquire-mcp/tessdata`, * 3. `~/.cache/enquire-mcp/tessdata`. * This is where `enquire-mcp install-ocr-lang ` downloads packs and where * `extractPdfWithOcr` reads them — so `serve` makes no runtime CDN fetch. * * @returns Absolute path to the tessdata cache directory. */ export declare function resolveTessdataDir(): string; /** * True iff an UNCOMPRESSED Tesseract language pack `.traineddata` exists under `dir`. * * v3.10.0-rc.44 — require the uncompressed form ONLY. The worker is created with * `gzip:false` + `cacheMethod:"readOnly"` pinned to `dir`, so it reads exactly * `.traineddata` (no `.gz`). Accepting a `.gz`-only install made this pre-flight * PASS while `createWorker` then failed to load the pack — a false "installed" verdict. * `install-ocr-lang` always writes the uncompressed form, so this matches reality. */ export declare function ocrLangIsInstalled(lang: string, dir?: string): boolean; /** * Offline-enforcement guard (v3.9.0-rc.10 — overclaim #16). Throws, FAIL-CLOSED, * if any requested language in `langs` (a `+`-joined Tesseract spec, e.g. * `"eng+rus"`) has no locally-cached trained-data under `dir`. Runs BEFORE the * Tesseract worker is created, so a missing pack never triggers the silent CDN * download that would violate the "zero outbound network calls in serve mode" * guarantee. The error names the exact `install-ocr-lang` command to run. * * @param langs - `+`-joined Tesseract language spec. * @param dir - Tessdata cache dir (defaults to {@link resolveTessdataDir}). * @throws {Error} If any requested language pack is not installed locally. */ export declare function assertOcrLangsInstalled(langs: string, dir?: string): void; /** * Extract text from an image-only / scanned PDF via Tesseract OCR. * * Caller has already loaded the file into a Buffer (use * `vault.readBinaryFile(relPath)` for vault-aware reading with * privacy-filter + max-bytes guards applied). * * v3.9.0-rc.10 (overclaim #16, ENFORCED) — offline guarantee. Calls * {@link assertOcrLangsInstalled} BEFORE loading any optional dep or creating * the worker, throwing (fail-closed) if a requested `.traineddata` isn't * cached locally — Tesseract.js would otherwise silently CDN-fetch it, the one * thing that could violate "zero outbound network calls in serve mode". The * worker is additionally pinned to the local cache (`langPath` + * `cacheMethod: "readOnly"`). Install packs via `enquire-mcp install-ocr-lang`. * * v3.7.16 P1-2 — refuses to process more than `opts.maxPages` (default * {@link DEFAULT_OCR_MAX_PAGES} = 200) in a single call. The check * runs BEFORE the Tesseract worker spawns, so adversarial inputs don't * allocate resources. Pass an explicit `pages: [from, to]` slice to * narrow the work, or raise `maxPages` to opt out of the default cap. * * v3.9.0-rc.10 — additionally clamps each rendered page's ABSOLUTE pixel * dimensions to {@link MAX_OCR_CANVAS_DIM} (the `scale` clamp alone doesn't * bound a giant-MediaBox OOM) and rejects an inverted/empty page range. * * Throws on encrypted PDFs, hard-corrupt files, missing optional deps, an * uninstalled language pack, an invalid page range, or when the requested page * span exceeds `maxPages`. Returns empty pages (`isEmpty: true`) where * Tesseract found nothing. */ export declare function extractPdfWithOcr(buffer: Buffer, opts?: ExtractPdfWithOcrOptions): Promise; export declare function isOcrAvailable(): Promise; //# sourceMappingURL=ocr.d.ts.map