How mazidulfarabi/melissa-ai works
Project Type & Purpose
• This is a lightweight, full-stack web application (a modern AI chatbot) with a static front-end and a serverless backend.
• The front-end delivers a chat UI in the browser (HTML/CSS/JS), while the backend is a Netlify Function (Node.js) that handles calls to the OpenRouter AI service with an automatic API-key fallback mechanism.
Main Architectural Components
a. Presentation Layer (Client-side)
• index.html + stylesheet.css: The chat interface and styling.
• jscript.js: Controls user input, local greeting checks, sessionStorage, rate-limit logic, UI updates, audio playback (chat.mp3).
• sessionStorage: Persists chat history and status flags in the browser.
b. Application Layer (Serverless Backend)
• Netlify Functions (functions/chat.js): Receives AJAX requests from the front-end, orchestrates AI API calls using environment variables (OPENROUTER_API_KEY, OPENROUTER_API_KEY_BACKUP), implements fallback logic, parses responses, and returns JSON.
• netlify.toml / _headers: Configure routes, headers, deploy settings.
c. Integration Layer (External Service)
• OpenRouter AI (Mistral 7B model): External AI inference service.
d. Hosting & Deployment
• Netlify CDN: Hosts static assets and serves the function, with automatic build & deploy from the repository.
Component Interactions & Data Flow
1. User types message in the browser UI.
2. jscript.js intercepts the input, runs a local-greeting check.
• If it matches a common greeting, generate a local response and update UI.
• Otherwise, send an HTTPS POST to /.netlify/functions/chat.
3. Netlify Function receives the payload, reads the primary API key from env vars.
4. Function calls OpenRouter AI.
• On success: return the AI’s response.
• On rate-limit or key failure: switch to backup key and retry.
5. Function returns JSON to jscript.js.
6. Front-end updates the chat UI (typing animation, sound alert), persists message/response in sessionStorage, updates online/offline status, and triggers smart alerts as needed.
Architectural Patterns & Principles
• Serverless API Gateway: the Netlify Function serves as a thin API layer.
• Automatic Fallback Circuit: a two-step key-switch strategy for high availability.
• Progressive Enhancement: local checks for trivial cases to reduce external calls.
• Stateless Functions + State in Browser: The function remains stateless; sessionStorage holds transient conversation state.
Diagramming Recommendations
• jscript.js (UI logic, rate-limit, audio)
• chat.js (Netlify Function, Node.js, fallback logic)
• OpenRouter AI (Mistral 7B)
d. Environment Variables & Config