"use client"; /* eslint-disable @next/next/no-img-element */ import { Check as CheckIcon, ChevronLeft, ChevronRight, CircleAlert, RotateCcw, Send, Sparkles, ZoomIn, ZoomOut } from "lucide-react"; import { useEffect, useMemo, useState } from "react"; import { check, type Receipt } from "@/lib/consistency"; import { useDecisions } from "@/lib/decisions"; import type { ReceiptRecord } from "@/lib/types"; import { CheckList, cloneReceipt as clone, ReceiptFields } from "./ReceiptFields"; import { Banner, Button, Card, Pill, Segmented } from "./ui"; type Source = "model" | "zeroshot" | "label"; const SOURCES: { value: Source; label: string }[] = [ { value: "model", label: "Fine-tuned model" }, { value: "zeroshot", label: "Before fine-tuning" }, { value: "label", label: "Human label" }, ]; export function ReceiptChecker({ record, prev, next }: { record: ReceiptRecord; prev: number | null; next: number | null }) { const { decisions, save } = useDecisions(); const decision = decisions[record.id]; const [source, setSource] = useState("model"); const [working, setWorking] = useState(() => clone(record.pred)); const [dirty, setDirty] = useState(false); const [zoom, setZoom] = useState(false); const [showDiff, setShowDiff] = useState(true); const [notice, setNotice] = useState(null); const original = (s: Source) => (s === "model" ? record.pred : s === "zeroshot" ? record.zeroshot : record.gold); // A saved edit from this browser replaces the model's reading when the page opens. useEffect(() => { if (decision?.edited && source === "model" && !dirty) setWorking(clone(decision.edited)); // eslint-disable-next-line react-hooks/exhaustive-deps }, [decision]); const switchSource = (s: Source) => { setSource(s); setWorking(clone(original(s))); setDirty(false); setNotice(null); }; const update = (fn: (r: Receipt) => void) => { setWorking((w) => { const n = clone(w); fn(n); return n; }); setDirty(true); setNotice(null); }; const checks = useMemo(() => check(working), [working]); const failed = checks.filter((c) => !c.passed).length; const compare = showDiff && source !== "label"; const decide = (status: "approved" | "review") => { save(record.id, { status, edited: dirty || decision?.edited ? working : undefined, at: new Date().toISOString() }); setNotice(status === "approved" ? "Approved. Saved in this browser." : "Sent to review. Saved in this browser."); }; const statusPill = decision ? ( decision.status === "approved" ? ( Approved by you ) : ( Sent to review by you ) ) : record.flagged ? ( Model: needs review ) : ( Model: auto-accepted ); return ( <> {checks.length ? `${checks.length - failed} / ${checks.length} rules pass` : "no rule applies"} {statusPill} {record.exact && matches human label} } meta="CORD-v2 test receipt ยท read by Qwen3.5-4B (QLoRA) with best-of-4 and the accounting checker" right={ <> {prev !== null ? ( ) : null} {next !== null ? ( ) : null} } />
setZoom((z) => !z)} className="flex items-center gap-1.5 rounded-lg border border-line bg-surface-solid px-2.5 py-1.5 text-[12.5px] font-semibold text-ink hover:border-accent" > {zoom ? : } {zoom ? "Fit" : "Zoom"} } >
{`Photo
}>

The model wrote one normal reading and four sampled ones. The checker keeps the reading that passes the most accounting rules; the model's confidence only breaks ties.

    {record.candidates.map((c, k) => (
  • {k === 0 ? "Greedy" : `Sample ${k}`}
    {c.valid ? `${Math.round((c.consistency ?? 0) * 100)}% rules` : "invalid JSON"} {c.chosen && picked}
  • ))}
switchSource(source)} className="flex items-center gap-1.5 rounded-lg px-2 py-1 text-[12.5px] font-semibold text-accent-deep hover:underline" > Reset edits ) : null } >
{source !== "label" && ( )}
no rule applies ) : failed === 0 ? ( would be accepted ) : ( goes to human review ) } > {checks.length === 0 ? (

None of the five rules can run: the fields each rule needs are not all present. Try adding a total or the item amounts.

) : ( )}

The rules recheck as you edit, using a TypeScript port of the Python checker that agrees with it on all 277 rule results of the test set. Amounts within 1% are treated as equal, to allow for rounding.

{decision && ( )} {notice && ( {notice} )}
); }