-
-
Architecture: cloud Gemini compiles the manual, Gemini Nano helps on the device, and rules plus supervisor approval decide what is safe.
-
Cloud Gemini (ONLY_IN_CLOUD) compiles the manual into a draft pack in 149 s. Every limit must quote the manual word for word.
-
The supervisor approves item by item. A safety line the AI paraphrased is flagged and excluded before publishing.
-
Network off: Gemini Nano checks the technician's note on the device (69.4 s). The label comes from response.inferenceSource.
-
82 °C is judged by a rule, not AI: alarm, manual quote, approved escalation rules, and a repeat deviation from an earlier shift.
-
Before a cloud review, the technician sees the exact text to be sent. Offline, the request is queued on the device.
-
Back online, the queued request runs on cloud Gemini (3.0 s) with the manual section and this check's history. Advisory only.
-
Gemini Nano drafts the shift handover on the device (49.1 s), above a facts table built by rules from the recorded readings.
-
Hybrid ledger: every AI call with its real source, plus the routing policy and the consented cloud review queue.
Inspiration
Building and plant maintenance runs on inspection rounds: walk the plant room, read the gauges, write a note, hand over to the next shift. Those rooms are often basements with no signal. Notes are short and inconsistent. A slow drift ("a bit warmer than yesterday") gets lost between shifts. In Japan, where I live, building maintenance is an ageing workforce that increasingly depends on newcomers working in a second language.
AI could help here, but only if it works offline and never gets the final say on safety. That combination is what Firebase AI Logic's hybrid inference makes possible.
What it does
- Supervisor:
- Uploads an equipment manual. Cloud Gemini, called through Firebase AI Logic, compiles it into a small procedure pack: steps, the measurement for each, limits, safety items and escalation rules.
- Every limit and rule must carry a verbatim quote from the manual. The app checks those quotes in code and flags any sentence the AI reworded.
- Approves or excludes each item. Nothing is published until every item is handled.
- Technician:
- Downloads the approved pack and runs the round fully offline.
- Readings are parsed and checked against the approved limits by rules, with no AI, and each result shows the manual's sentence. An alarm shows the escalation rule the supervisor approved.
- On the device, Gemini Nano in Chrome, called through the same Firebase AI Logic SDK, checks free-text notes against the step, answers questions only from the approved pack, and drafts the shift handover.
- Escalation:
- When the local result is invalid, the question goes beyond the pack, or the technician asks, a cloud review is offered.
- The cloud gets context the device calls don't carry: the verbatim manual section and this check's history across shifts.
- The technician sees and can edit the exact text that will be sent.
- Offline, the request waits in a queue and runs when the connection returns.
- Repeat deviations: when a check is out of limits again after an earlier shift's deviation, a rule-based banner shows the manual's escalation rule for repeated deviations.
- Next shift: gets a handover whose facts table is built from the recorded data. Open issues carry over, and a cloud trend analysis spots problems that repeat across shifts.
- Hybrid ledger: every AI result carries its real source (On-device or Cloud) from
response.inferenceSource, and every call is logged.
How I built it
- Firebase AI Logic (Web SDK 13.3) with the Gemini Developer API.
PREFER_ON_DEVICEfor device tasks. On a device with no local model, the SDK's cloud fallback is used only after the user opts in, because it would otherwise send notes to the cloud without asking.ONLY_IN_CLOUDfor compiling the manual, reviews and trends.
- One JSON Schema per task, shared by three consumers: the on-device
responseConstraint, the cloudresponseJsonSchema, and Ajv validation. - Step cards. A pack is split into per-step cards, capped at 900 tokens (150–210 in the demo), so each on-device call carries one step instead of the whole manual. Measured with Chrome's own token counter and Firebase AI Logic in
ONLY_ON_DEVICEmode: a note check costs 338 tokens with a step card vs 4,744 with the full 18 KB manual, took 34–49 s vs 109–140 s on my laptop, and caught the out-of-limit reading in 3 of 3 runs vs 0 of 3. - Deterministic domain layer (TypeScript, 31 tests):
- unit-aware measurement parser
- threshold checks
- quote verification
- publish lock
- output guards that reject invented numbers and drop numeric AI claims
- handover guards (no invented numbers, no reassuring wording next to an alarm) and action repair
- Cloud Firestore with offline persistence. Security rules (16 emulator tests) enforce:
- draft → approved → retired, with approved packs immutable
- only the assigned technician writes readings
- consent before escalation, with a payload that can't be altered
- no re-queuing of finished escalations
- an append-only ledger
- App Check (enforced) on AI Logic and Firestore, Anonymous Auth, and Remote Config for model choice and an escalation kill switch.
- React + Vite PWA, glove-friendly: 56 px targets, a numeric keypad, light and dark themes.
Challenges I ran into
- Small models make confident mistakes. In testing, the on-device model claimed a vibration of 5.2 mm/s was "≥ 7.1 mm/s". The fix was a rule: numbers are judged only by the rule engine, and AI concerns containing numbers are removed and shown as removed.
- The cloud compiler paraphrases. One run turned a sentence about an alarm into "Do not touch the housing if temperature is high". The quote it cited didn't say that. The paraphrase check now flags this for the supervisor.
- Offline writes. Firestore's web write promises only resolve on server acknowledgement, so every write is fire-and-forget and the UI reads from the local cache.
- On-device latency. My Intel MacBook Pro takes 10–80 s per on-device call. The app keeps the readings and rules instant and treats AI as an optional assistant, never a step the technician has to wait for.
Accomplishments I'm proud of
- A real offline round:
- readings checked offline
- a note checked by Gemini Nano with
navigator.onLine === false - a consented cloud review queued, then sent automatically after reconnecting
- a handover drafted on-device
- Safety is enforced in three places: deterministic code, a human approval gate, and server-side security rules. It doesn't rely on prompts.
- App Check enforcement was verified: a direct call without a valid token gets HTTP 401.
What I learned
The interesting hybrid pattern isn't "on-device, else cloud". It's the cloud model preparing work for the small model and for the code: once per manual revision, prose becomes typed limits the rule engine can enforce, each tied to a verified quote, plus small step cards so every on-device call carries one step instead of the whole manual. The same schema then lets either model answer, and code decides whether to trust the answer.
What's next
- Mobile field clients: on-device AI in the browser needs a desktop-class machine today, while technicians carry phones and tablets.
- iOS client using Apple Foundation Models through Firebase AI Logic. It was planned, but blocked by my Xcode version.
- PDF manuals with page-anchored quotes.
- Multiple users per site, with separate supervisor and technician roles.
- Photo notes once on-device image input is broadly available.
- A pilot with a building-maintenance team.
Built With
- ajv
- chrome-built-in-ai
- cloud-firestore
- firebase-ai-logic
- firebase-app-check
- firebase-auth
- firebase-remote-config
- gemini
- gemini-nano
- pwa
- react
- typescript
- vite
- vitest
