Inspiration
The catch. When we recorded the demo video for this project, the AI's one-line summaries came back with a placeholder missing — [NAME_2], the sender's signature, dropped from every version. The summaries still read as complete; nothing looked wrong. What caught it was not a person — it was SafeDraft's audit layer. It counts every placeholder in every generated version, and it flagged the loss on the version cards and in the transparency log instead of passing the result off as complete. That is the behavior SafeDraft is built around: the AI is never trusted with your original text, and it is never trusted with its own output either.
Everyone has a draft they hesitate to paste into a cloud AI: a support email with a customer's name and order number, an invoice dispute with a card number, a message to HR with a home address. The standard advice — "just remove the personal data first" — means manually rewriting your own document before the AI is allowed to rewrite it for you, and putting it all back afterwards. We wanted that redaction step to be automatic, verifiable, and impossible to get wrong silently.
What it does
SafeDraft is a privacy-shielded AI writing assistant with three layers:
- A deterministic PII shield (no AI). When you paste a draft, a rule-and-dictionary engine in
src/pii-shield.jsdetects 10 types of personal data — names (Japanese & English), emails, phone numbers, postal codes, dates, money amounts, street addresses, ID numbers, URLs and credit-card numbers (Luhn-validated; IBANs are mod-97 validated). Everything is replaced with placeholders in milliseconds, and the UI shows you exactly what the AI is allowed to see before anything is sent. The placeholder map lives only in tab memory. - Hybrid inference with Firebase AI Logic. Short texts are generated with
InferenceMode.PREFER_ON_DEVICE— Gemini Nano in Chrome first, cloud Gemini as fallback. Long documents are routed to aPREFER_IN_CLOUDmodel. Every response'sresponse.inferenceSourceis shown as an ⚡ On-device / ☁ Cloud badge, so the processing route is never a guess. Routing is decided on the masked text — the only text that ever leaves the device. - An audit-and-restore layer. Models occasionally mangle placeholders. SafeDraft repairs near-miss forms (lower-cased, re-bracketed, dropped brackets), then audits every generated version for lost, duplicated or invented placeholders and warns instead of failing silently. Only then are your original values restored — locally, in your browser.
Features: rewrite in 3 styles (polite / casual / concise), summarize at 3 lengths (~25% / ~50% / one line), 1–3 versions per generation — each independently audited — a one-click 30-second demo (a support email with 13 pieces of personal data), a live masked preview while you type, and a transparency log of every masking and routing decision.
How we built it
- Vanilla JS ES modules, no build step. The Firebase JS SDK v13.0.0 is loaded from the Firebase CDN; hybrid mode is configured with
getGenerativeModel(ai, { mode: InferenceMode.PREFER_ON_DEVICE, inCloudParams: { model: "gemini-3.5-flash-lite" } }). - Firebase AI Logic (Gemini Developer API backend) for generation; Firebase App Check (Fraud Defense / reCAPTCHA Enterprise) protects the endpoint. For judge testing, the app accepts an App Check debug token via a
?act=<token>link: the token is moved intosessionStorageand scrubbed from the address bar, so a judge can open one link and generate immediately. - The shield is a pure module shared between the browser UI and a Node test suite. Detection runs on a width-normalized copy of the text, so full-width IME input (
090-…,taro@…) and halfwidth katakana are caught as reliably as ASCII, while restoration reproduces the user's original characters exactly. - 80 unit tests cover the shield, the prompt builder, the variant splitter and the debug-token intake. The app went through 13 structured review rounds (judge / user / security / UX / correctness perspectives) with probe-driven fixes — e.g. a full-width input leak and a placeholder-collision bug were found and fixed by adversarial probing, not by reading the code.
- Prompts are built by a dedicated module whose first and strongest instruction is that placeholder tokens must be copied byte-for-byte; multi-version output uses
### Version kheaders that a tested parser splits back into individually audited versions.
Challenges we ran into
- Detection edge cases are endless. Japanese phone numbers written with middle dots (
090・…), addresses without a prefecture, surnames in old character forms (齋藤, 髙橋), building names and room numbers — each needed a pattern plus a false-positive guard (e.g. the place name 中山間部 must not be masked as a person). - Models don't respect placeholders perfectly. Lower-casing, bracket substitution, duplication and outright invention of tokens all happen in practice; the repair + audit pipeline exists because a restore step that fails silently would quietly corrupt user data.
- Performance traps. A naive email regex took ~500 ms on a 20,000 character document (quadratic backtracking); bounding it brought masking down to ~1–2 ms.
Accomplishments that we're proud of
- A complete, working privacy architecture — not a mock: mask → hybrid generation → audit → restore, with the route badge proving where each answer was computed.
- The 30-second demo: 13 personal items in one email, masked locally in milliseconds, zero of them sent to any model.
- Honest engineering: a limitations section in the README that says exactly what the shield can miss, and a UI that shows the masked text before generation so the user can verify.
What we learned
Hybrid inference is most valuable when routing is part of the product's promise, not a hidden optimization: here, "short and sensitive stays on-device first" is the privacy story, and inferenceSource lets the UI prove it. We also learned that a deterministic layer earns trust in a way a second AI never could — it can be unit-tested, audited, and shown to the user verbatim.
What's next for SafeDraft
- A Chrome extension build so the shield follows users into Gmail and help-desk tools.
- User-defined dictionaries (company names, project codenames) and per-field policies (e.g. "amounts may go to the cloud, names never").
- More locales for the shield, and an optional Firestore-backed history that stores only masked text.
Built with
Firebase AI Logic · Gemini Nano (on-device, Chrome Prompt API) · Gemini (cloud, gemini-3.5-flash-lite) · Firebase App Check · Firebase Hosting · JavaScript (ES modules) · Node.js (test suite)
Built With
- ai
- app
- check
- firebase
- gemini
- hosting
- javascript
- logic