Inspiration
Most AI chat apps assume you are always online and send every word you type to a server. But many questions are quick or private, and sometimes there is no good connection at all. Chrome now ships Gemini Nano, and Firebase AI Logic can run a model either on the device or in the cloud. I wanted to build a real app around that choice and let people see and control it: Offline for private, on-device answers, and Online for the cloud model, with an account and a chat history that follows you.
What it does
Chat++ is a hybrid AI chat app with two clear modes:
- Offline mode: Gemini Nano runs inside Chrome on your own device, with no account needed. The chat is saved in a local SQLite database.
- Online mode: the Gemini cloud model answers through Firebase AI Logic. You create an account, and your chat is saved in the cloud under your name.
- Accounts: sign-up is verified with a 6-digit code sent by email. There are also sign-in, "forgot password" with another code, and sign out.
- Conversation memory: the AI remembers recent messages, but only a small slice is sent each time, to save tokens.
- Extras: an incognito mode (those messages are not kept in the history), Markdown replies, a clear chat button, and a greeting with your name.
If a browser or computer can't run the on-device model, the app explains why and offers the online mode instead.
How I built it
- Frontend: plain HTML, CSS and JavaScript. The Firebase SDK is bundled with Vite. I create two separate models, one with
InferenceMode.ONLY_ON_DEVICEand one withONLY_IN_CLOUD, so each mode does exactly what it says and never switches silently. - Backend: Flask. The browser sends its current mode with every request, and each route (history, save, clear) has an online part and an offline part.
- Storage: SQLite for offline chats. For online, Turso through its HTTP API, with accounts in one table and each user's chat in its own table.
- Accounts: hashed passwords, Flask session cookies, and OTP emails sent with Brevo. The codes are kept on the server, expire after 5 minutes, allow 5 tries, and work only once.
- Protection: Firebase App Check with reCAPTCHA v3 and limited-use tokens guards the AI API. DOMPurify cleans the Markdown, and user-supplied names and email addresses are escaped.
- Memory: I send only the newest question and answer pairs that fit a character budget (about 2000 characters online and 1000 offline), and I shorten old answers. This keeps the cost low.
Challenges I faced
- The on-device model has no chat mode. Firebase's hybrid on-device inference supports only single-turn text, so I couldn't use
startChatoffline. I solved it by writing the recent messages into one prompt, and keeping the real chat session for the cloud model. - App Check blocked my own tests. Local testing returned a 403 error until I learned that each browser and address needs its own registered debug token.
- Email delivery. Brevo blocks API calls from unknown IP addresses, and my first verification emails landed in Spam. I had to authorize the IP, and I added a tip in the app flow to check the Spam folder.
- Security. My first version stored the signed-in user and the OTP in global variables, which would let one visitor affect another. I rewrote it with per-browser sessions and a server-side OTP store, fixed an SQL injection risk in the table names, and made sure names can't inject HTML.
- Hardware limits. Gemini Nano needs desktop Chrome, a recent version, and about 22 GB of free space. So the offline start screen shows download progress and a clear fallback.
Accomplishments I'm proud of
- One app where the user decides where the AI runs, with a complete account system behind the online mode.
- Real conversation memory in both modes, even though the on-device model can't do chat sessions.
- A secure sign-up and password reset flow, built and tested end to end.
What I learned
Hybrid AI is a product decision as much as a technical one. Deciding when to use the device and when to use the cloud affects privacy, cost, speed and trust. I also learned how much work sits around the model itself: sessions, email verification, rate limits, and what to do when a device can't run it.
What's next
- Streaming answers with a Stop button
- Several chats per account
- Image input in online mode
- A hosted demo, with a verified sending domain so the emails stop landing in Spam