AI Chat
OpenAI or Gemini via Cloud Function. The key never touches the app. Streaming, history, and system prompt.
The API key never lives in the app: values in --dart-define end up in the binary and can be extracted. Flutter only knows the function's URL; the key stays on the server.
App → POST {message, history} → Cloud/Edge Function → OpenAI / GeminiImplementation
Via MCP: ask your assistant "add ai_chat to my project". It runs add_feature for you. See Kasy MCP.
-
Run:
kasy add ai_chat -
Answer what the CLI asks: provider (OpenAI or Gemini), system prompt, and key.
-
Confirm the result for your backend:
Backend Deploy Firebase Automatic ( functions:aiChat)Supabase Automatic ( ai-chat --no-verify-jwt)REST API You implement the endpoint that receives {message, history}The CLI already fills in
AI_CHAT_ENDPOINTinlaunch.jsonon its own.
API usage is billed to you, not to the user. Every conversation inside the app spends credit from your provider key. Before opening the chat to everyone, set a spending limit in the provider dashboard and watch the usage.
Information
What comes in the UI
- WhatsApp-style chat bubbles (user on the right, AI on the left)
- Streaming via SSE: words appear one by one as the AI generates them, just like ChatGPT
- "Assistant is typing" indicator until the first chunk arrives
- Auto-scroll and a multiline field that sends on keyboard submit
- History persisted per backend (Firestore / Supabase / REST)
System prompt
The invisible instruction that defines the agent's personality. The user never sees it. It stays on the server.
No system prompt: generic ChatGPT, answers anything.
With a system prompt: an agent with a defined focus.Example:
"You are the assistant for the Fitsync app. Only answer questions about
workouts and nutrition. If asked about anything else, politely say you can't help."Changing it after setup:
# Firebase: edit functions/.env and redeploy
firebase deploy --only functions:aiChat
# Supabase
supabase secrets set AI_SYSTEM_PROMPT="New instruction here" --project-ref YOUR_REF
supabase functions deploy ai-chat --no-verify-jwt --project-ref YOUR_REFContext limit
By default the app sends only the last 20 messages (10 exchanges) as context. The full history stays saved in the database (the user sees all of it), but the AI only receives the recent chunk. This lowers cost and improves quality.
To adjust it, edit lib/features/ai_chat/providers/ai_chat_notifier.dart:
const int _kMaxContextMessages = 20; // change thisNo server changes are needed.
Using OpenAI Assistants or Gemini Gems
For a larger knowledge base (docs, extensive FAQs), create an Assistant and point the app at it. Flutter doesn't change: just swap AI_CHAT_ENDPOINT in launch.json:
"--dart-define=AI_CHAT_ENDPOINT=https://your-server.com/api/assistant"Your server receives {message, history} and forwards it to the Assistant. The app's interface stays identical.
Never put the API key in --dart-define or in the app's .env. Always use a server-side secret.
Last updated on 08/02/2026

