Hapiimood is a Production-Ready AI Mental Wellness Ecosystem designed to provide 100% anonymous, immediate, and empathetic support for students. In an era of high-pressure academics, Hapiimood bridges the gap between distress and professional care using state-of-the-art Natural Language Processing.
graph TD
User([User / Browser]) -->|Next-Auth/Clerk| Clerk[Clerk Authentication]
User -->|Message Stream| NextJS[Next.js App Server]
NextJS -->|Middleware| RateLimit[IP Rate Limiter]
RateLimit -->|Verified| Groq[Groq Llama 3.1 AI]
Groq -->|Sentiment Analytics| Sentiment[Emotional Valency Engine]
Groq -->|Empathetic Tokens| NextJS
NextJS -->|Vercel waitUntil| SBAdmin[Supabase Admin Client]
SBAdmin -->|Secure Log| Supabase[(PostgreSQL / Storage)]
NextJS -->|Secure Bridge| User
%% Analytics Flow
Supabase -->|Filtered by RLS| Actions[Server Actions]
Actions -->|Visualized Dashboard| User
- Hapi (AI Companion): Real-time, zero-latency empathetic chat powered by Groq Llama-3.1 models natively integrated with
@ai-sdk/react. - Background Sentiment Analysis: Evaluates every message continuously to formulate an autonomous Emotional Valency score.
- Deep Analytics Dashboard: A personalized
Rechartsbased visualization hub. Tracks Mood Trends, Sentiment Distribution (Pie Chart), and Weekly Engagement (Bar Chart). - Interactive Wellness Tools:
- Automated sliding UI hooks mirroring professional conversational AI platforms.
- Quick Calm visual breathing pacers and Framer-Motion powered Meditation carousels.
Hapiimood is engineered not just to work, but to scale securely. Here is the highly optimized technical stack and systemic solutions deployed:
- Next.js 16 (App Router): Leverages aggressive Server Components for routing and Client Components for high-interactivity states.
- Tailwind CSS v4: Modern utility-first styling utilizing high-end Glassmorphism (blur, frosted overlays).
- Framer Motion: High-fidelity micro-interactions ensuring the application feels clinically calming yet highly responsive.
This system solves deeply complex production bottlenecks standard in Serverless architectures:
-
The RLS/Clerk Identity Bridge (Zero-Trust Security):
- Supabase operates under strict Row-Level Security (RLS). A direct client-query combining Clerk Auth with Supabase fails natively because Supabase
auth.uid()evaluates to null. - Solution: Designed a secure Next.js Server Actions Bridge (
getAnalyticsData). The server dynamically authenticates the session viaauth(), executes a hidden Service Role bypass explicitly scoped to the user, and delivers the data matrix to the frontend instantly without API exposure.
- Supabase operates under strict Row-Level Security (RLS). A direct client-query combining Clerk Auth with Supabase fails natively because Supabase
-
Serverless Background Optimizations (
waitUntil):- The Problem: Modern Edge runtimes like Vercel aggressively terminate background tasks (like Database insertions) exactly when the streaming HTTP connection finishes, risking data loss.
- The Solution: Wrapped Supabase transactional logging (
onFinishblocks) deeply within isolated@vercel/functionsutilizing thewaitUntil()operator. This instructs Vercel strictly to freeze the serverless lifecycle until a guaranteed201 Createdis resolved, ensuring absolute data integrity tracking user moods.
-
Advanced Global Rate Limiting:
- Designed a high-speed In-Memory Map capping IP usage directly in Next.js Server logic (
60 requests / minute / IP), completely neutralizing rogue LLM credit drain vectors.
- Designed a high-speed In-Memory Map capping IP usage directly in Next.js Server logic (
-
DNS Auth Routing & CSP Hardening:
- Advanced Namecheap CNAME DNS configuration routing third-party Clerk identity services directly through
clerk.hapiimood.meto bypass aggressive privacy blockers (like Safari ITP). - Dynamic Content Security Policies (CSP) surgically crafted to isolate DOM scripts while permitting Cloudflare Turnstile token authorizations safely.
- Advanced Namecheap CNAME DNS configuration routing third-party Clerk identity services directly through
git clone https://github.com/Umair-IITD/Hapiimood.git
cd Hapiimood
npm installCreate a .env.local file strictly hiding these values:
GROQ_API_KEY=your_key
NEXT_PUBLIC_SUPABASE_URL=your_url
NEXT_PUBLIC_SUPABASE_ANON_KEY=your_key
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=your_key
CLERK_SECRET_KEY=your_secret
SUPABASE_SERVICE_ROLE_KEY=your_secret_admin_keyRun the scripts in supabase/migrations/ to initialize the chat_messages and mood_logs tables. Ensure Supabase RLS is unconditionally active on all tables.
npm run dev
Developed by Umair for the IITD Student Body.
Live at: hapiimood.me