AI invoice reader project (TypeScript) · Chapter 7 of 12
Confidence and the review queue
By the end of this chapter you can give every field the reader fills in a confidence and the reasons for it, found as evidence in the invoice's own text; store the invoice, its fields and, when a person must look at it, a review task, all in one transaction; and catch an invoice that is already stored. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Checking the model
- 3 tasks
- TypeScript
- In the all labs plan
The first 2 chapters of AI invoice reader project (TypeScript) are free. This one comes with the all labs plan.
Why you need this
Chapter 6's reader reads an invoice, checks it and asks again when a rule fails. Then it forgets everything: the answer goes back in the response and nothing is kept. And it cannot tell Vaishali which answers to look at.
Take swarnim-ssl-2219.pdf. Swarnim Sound & Light prints Date: 08/10/2026, the 8th of October. The model reads it month first, as 10 August. Every rule passes: the date is not in the future and not 120 days old. The retry of chapter 6 fixed the total, and the reader hands back a wrong date with no problem at all, exactly the "date typed month first" that went wrong in September. Mauli Bakery prints 1) Pav - 60 ladi x 30 = 1,800, and the model writes "Bread Roll". The amounts add up, so no rule says a word, but Vaishali can no longer match the bill line by line against what the bakery delivered.
And no rule can catch Swarnim's invoice being paid twice. It came on WhatsApp and by email, as swarnim-ssl-2219.pdf and swarnim-ssl-2219-copy.pdf, and each copy is right on its own. Only something that remembers the first one can.
So this chapter adds three things: evidence for every field, a database that keeps every invoice, and a review queue: the invoices Vaishali must look at, each with the reason, while the rest go through.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
confidence.ts: is the value on the invoice?
In src/review/confidence.ts write appearsIn(text, value, kind): null is true. kind 'text': true when the value is in the text ignoring case and runs of spaces (squash both, given). kind 'number' or 'date': true when one of renderings(value, kind) (given) is in the text as a whole token: not right after a digit, a dot or a comma, and not followed by a digit, or by a dot or comma and a digit. Build one RegExp per rendering with escape() (given) and a lookbehind and a lookahead.
The Check looks for sixteen values in the text of three invoices: the vendor's name in other spacing and case, a made-up name, 36698 printed 36,698.00, 698 and 50 (only parts of longer numbers), 1 October printed 01/10/2026, 10 January (the date read month first), 3 October in "Dt. 3.10.26", 4 October in "04-Oct-2026" and others.
- Task 2
confidence.ts: a score and reasons for every field
Write scoreFields(inv, text, problems): a FieldScore { name, value, confidence: 1, reasons: [] } per header field, HEADER's names in order (value: inv[name]), then one named
line_items[${i}]per line (value: the line item). A header field whose value is not appearsIn the text (with its KIND), or a line whose description (text) or quantity, rate or amount (numbers) is not: confidence NOT_FOUND and the reason "not found in the invoice text". Then, for each problem whose field is the name or starts with the name and a dot: confidence at most PROBLEM (Math.min), and its message as a reason.The Check scores an invoice read right, then one with a made-up name, a date read month first, an item named differently, a wrong line and a total that does not add up.
- Task 3 · interview question
Interview: pipeline.ts, store it all at once
In src/pipeline.ts write store({ file, sha256, user, invoice, problems, scores, attempts, usage, notes = [] }), all inside ONE db.transaction(async (tx) => ...), every query on tx. First, when the invoice has a supplier_gstin, select an invoice whose data has the same supplier_gstin and invoice_number (sql
${invoices.data}->>'supplier_gstin' = ${...}) and whose status is not 'rejected'; if there is one, add { field: 'invoice_number', code: 'duplicate', message: a sentence naming its id } to a copy of the problems. Then reason = reviewReason(problems, scores, notes). Insert the invoice (file, sha256, venue: user.venue, submittedBy: user.email, status 'needs_review' with a reason or 'accepted', data: invoice, problems, attempts, tokensIn, tokensOut) with .returning(); insert one invoice_fields row per score (invoiceId, name, value, confidence, reasons) and a review task with the reason when there is one. Return the invoice row.The Check stores an invoice, then the same vendor's same number again (a duplicate with a review task), then one whose field rows cannot be saved (nothing of it may stay), then POST /invoices for deccan-dpb-1152.pdf, whose printed total does not add up.
Nikhil will ask: why save the invoice, its fields and its review task in one transaction?
What you will learn
- Confidence as evidence: a number from 0 to 1 your code works out from what it found, not a feeling of the model's.
- Looking for a value in the text: one number, many printed forms, and regex lookarounds ((?<!...), (?!...)) built with new RegExp.
- The database: Drizzle's schema, a migration, three tables and the jsonb type.
- insert(...).values(...).returning(): the row Postgres made, with its new id.
- One transaction: db.transaction(async (tx) => ...), every query on tx, all or nothing.
- Duplicates: the same vendor's same invoice number, found with ->> inside jsonb.
More in Checking the model
Build "Confidence and the review queue" yourself
Parda's venues get invoices from sound, printing and canteen vendors, typed and messy. You build the pipeline that reads them, checks every rupee, and sends the doubtful ones to a person. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of AI invoice reader project (TypeScript) are free. This one comes with the all labs plan.