Let any feature read an upload, a photo included, through PdfReading

Merged
JO
2d ago
+462-79

Top PR in stack #5504 (… → #5896 → #6086 DocumentFile → shared reading), based on feat/document-file (#6086). Plan: shared-document-reading.md §5. The receipt PRs build on this: #4425 (receipt splits) and #5703 (receipt scans).

Problem

Ai::PdfReading (whole PDF → text layer → page images → OCR) is wired to extract_pdf and PDFs. The receipt PRs each built their own reading, with no OCR fallback or a whole-PDF-only path that a vision-only or OCR-only setup can't read.

Change

  • The reading takes a file and a feature: Ai::PdfReading.new(file:, family:, user:, feature: :extract_pdf, max_pages: nil). A feature whose matrix row has route_of: :extract_pdf reads with PDF import's models and OCR entries, under its own lane and consent row. OCR requests carry the feature; chat OCR finds its prompt through the feature's source.
  • Photos: a one-page document with no PDF or text way: sent as its cleaned image to a vision model, otherwise transcribed by OCR.
  • Page cap: max_pages: raises Unavailable(:too_many_pages) before anything is rendered or sent.
  • #answer(Gen, start:, accept:) reads Auto's ways until accept takes an answer (keeping the first otherwise); #as_generation runs it as one Ai::GenerationJob step, so a feature keeps the outbox, delivery, sweep and ai_failed. Its requester must take its record from the run's window.
  • Ai::PdfReading::Remembered: PdfImport.last_passing_way moves into a concern (last_passing_way(model:, **scope), which raises ArgumentError without a scope, and remember_reading!) for any record with the same columns.
  • Ai::PdfReading::Pages no longer declares a feature; ReadPdf and ExtractBankStatement declare extract_pdf.

Statements read exactly as before: existing tests change only to build a DocumentFile.

For the receipt PRs

Matrix row { lane: :prompt, route_of: :extract_pdf, consent: :user, sends: %i[uploaded_document] }; generation includes Ai::PdfReading::Pages, declares its feature and MAX_PAGES; requester returns Ai::PdfReading.new(...).as_generation(Gen, start: Record.last_passing_way(account:, model:)) and calls remember_reading! in apply_ai. See the guide's new rule in docs/llm-guides/ai-pipeline.md.

Validation

  • TZ=Etc/UTC bin/rails test on the stack top: 20,471 runs; the only failures are two AccountStatementImportConcurrencyTest lock-wait timings under parallel load, which pass alone (an earlier run's LookbooksControllerTest 404 was the same).
  • New: test/models/ai/pdf_reading/feature_reading_test.rb (stubbed route_of row and requester): routes/needs/lane/consent, photo via vision (JPEG, no EXIF/GPS), via OCR, neither, page cap, accept, one job step end to end, failure through ai_failed, Remembered.
  • Rubocop and Brakeman clean.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • Document reading now supports uploaded photos as well as PDFs, with image-reading and OCR options.
    • Reading can be tailored to different features, respect page limits, and try alternative routes when a result isn’t accepted.
    • Successful reading methods can be remembered for future matching imports.
  • Bug Fixes

    • Improved handling of unsupported files and clearer errors when documents exceed page limits.
  • Documentation

    • Updated the AI pipeline guide with photo support, feature-based reading, and related examples.