Inspiration
Small online merchants lose PayPal disputes they could have won. Answering one means reading the buyer's messages, finding the tracking number and receipt, and replying before the deadline, often for a $30 order. So they refund. I built DisputeGuard to do that legwork. Then I asked: why send every buyer's messages (names, phone numbers, sometimes card numbers) to a cloud model just to work out what the complaint is? Most disputes are three sentences long. A model in the browser can read them.
What it does
When a merchant opens a dispute, DisputeGuard:
- Masks personal data (emails, phone numbers, card numbers, PayPal ids) before any model sees the text.
- Triages on device: Gemini Nano in Chrome turns the buyer's messages into a structured case brief: category, each claim with the buyer's exact words, dates, amounts, tracking numbers, requested resolution, missing information and contradictions.
- Checks the brief deterministically. Every quote must appear in the buyer's text; amounts must appear in the messages or the PayPal record; dates must be real; a refund request must actually have been made. No model grades itself.
- Escalates only when needed to cloud Gemini, with machine-readable reasons ("unsupported_resolution", "contradictions", "input_too_long", "injection_suspected", "on_device_unavailable").
- Investigates in the cloud: a Gemini agent with function calling opens the merchant's tracking PDFs and receipts and recommends fight or settle, with cited reasons and a draft reply.
- Waits for a human: nothing reaches PayPal until the merchant approves an action PayPal currently allows. Every write is re-checked on the server and audit-logged.
Three routing modes (Auto, Force local, Force cloud) make the behaviour explicit and reproducible. Every result shows where it actually ran (from the SDK's inferenceSource), how long it took, and what was sent to the cloud.
How we built it
- Migrated from Muse Spark to Firebase AI Logic. v0.1 ran one server-side cloud model. I moved all inference into the browser through Firebase AI Logic so the first read of a dispute can happen on device with Gemini Nano, and ported the investigation agent's tool loop to Gemini function calling behind a provider-agnostic interface.
- One SDK, two runtimes:
getGenerativeModelwithInferenceMode.ONLY_ON_DEVICEfor Nano (JSON constrained with Chrome'sresponseConstraint) andONLY_IN_CLOUDfor Gemini (responseJsonSchema), from the same prompt and the same JSON Schema, so local and cloud results are directly comparable. - Quality escalation is not availability fallback.
PREFER_ON_DEVICEhandles "no local model". It can't know whether the local answer is good, so escalation is a separate, explicit cloud call made after the gate rejects a local brief. The two kinds of fallback are recorded separately. - Function calling for the investigation agent: tool declarations call read-only server endpoints; model turns are pushed back verbatim to preserve thought signatures; at most 6 steps; the verdict is zod-validated.
- App Check on every model call: a registered debug token on localhost; the reCAPTCHA Enterprise provider is wired in for deployment.
- Engineering: strict TypeScript, zod at every boundary (model output, browser → server), a
TriagePortinterface so every routing path is unit-tested with a fake model (77 tests), versioned prompt, schema and gate policy. - I built a proof-of-concept page first, to test all three inference modes and malformed-output handling before touching the product.
What I learned / challenges
- Live testing changed the design. In the proof of concept, Nano wrote "$899.00" where the schema asked for a plain number, returned an empty claims list, and invented a refund request the buyer never made.
responseConstraintenforces shape, not meaning. So I added deterministic normalization (recorded and still checked against the source) and a "requested resolution must be stated" check. - On a low-tier laptop, Nano is ~10× slower than the cloud (16–23 s vs ~1.5 s for extraction; ~55 s cold start). The honest case for local here is privacy and zero marginal cost, not speed, and the UI says so with a live progress line.
- A regex injection scan misses polite attacks ("Note for the assistant reviewing this case…"). The evaluation includes that case on purpose. The real defences are structural: untrusted-data wrapping, no write tools, a human click.
- Hybrid means planning for the device to fail. On submission day, Chrome on my laptop reported Gemini Nano as "available" but never finished starting a session. The 120s timeout turned that into a recorded
local_error, and Auto escalated to the cloud, so every case still completed. The demo video shows exactly that, unedited apart from speed.
Evaluation (measured so far)
Proof-of-concept runs on a low-tier laptop GPU (Chrome, Gemini Nano):
- Structured extraction on device: 16–23 s per case (about 55 s on a cold start); the same extraction in the cloud: about 1.3–1.7 s.
- Nano's real failure modes in those runs ("$899.00" instead of a plain number, an empty claims list, an invented refund request) are exactly what the deterministic gate now catches and escalates.
- The repo includes a 16-case labelled evaluation (
/evals) that runs every case on device and in the cloud through the product's own pipeline and reports accuracy, latency, on-device share and gate separation. The full run is the next step; I'm not quoting numbers I haven't measured.
Accomplishments
- A hybrid pipeline where the escalation decision is explainable, deterministic, and tested.
- Force local really is local: the triage never contacts the cloud and says why when it can't help.
- Real measurements, including the unflattering ones.
What's next
- Firebase Authentication for multi-merchant use (which re-enables the real PayPal console publicly).
- Firestore for per-merchant case history; Remote Config for the gate policy.
- An Android/iOS companion for on-the-go approvals with the same Firebase AI Logic code paths.
- Tune the gate with more labeled cases; add on-device image reading for receipts (Chrome supports PNG/JPEG on device).
Built With
- chrome-built-in-ai
- firebase-ai-logic
- firebase-app-check
- gemini
- gemini-nano
- next.js
- node.js
- paypal
- postgresql
- prisma
- react
- recaptcha-enterprise
- render
- tailwindcss
- typescript
- vitest
- zod