Let any feature read an upload, a photo included, through PdfReading
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 hasroute_of: :extract_pdfreads 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:raisesUnavailable(:too_many_pages)before anything is rendered or sent. #answer(Gen, start:, accept:)reads Auto's ways untilaccepttakes an answer (keeping the first otherwise);#as_generationruns it as oneAi::GenerationJobstep, so a feature keeps the outbox, delivery, sweep andai_failed. Its requester must take its record from the run's window.Ai::PdfReading::Remembered:PdfImport.last_passing_waymoves into a concern (last_passing_way(model:, **scope), which raisesArgumentErrorwithout a scope, andremember_reading!) for any record with the same columns.Ai::PdfReading::Pagesno longer declares a feature;ReadPdfandExtractBankStatementdeclareextract_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 teston the stack top: 20,471 runs; the only failures are twoAccountStatementImportConcurrencyTestlock-wait timings under parallel load, which pass alone (an earlier run'sLookbooksControllerTest404 was the same).- New:
test/models/ai/pdf_reading/feature_reading_test.rb(stubbedroute_ofrow 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 throughai_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.