How yashsaxena27/setu-ai works
1. Purpose and system shape
Setu AI is a welfare-scheme discovery application: citizens create a profile, receive explainable eligibility matches, simulate profile changes, prepare application drafts, and set reminders. It is a decoupled React SPA and Express API backed by MongoDB, Gemini, and optional Twilio WhatsApp delivery.
Primary runtime entry points are client/src/main.tsx, client/src/App.tsx, server/src/server.ts, and server/src/app.ts.
2. Client application and navigation
The Vite/React TypeScript client owns user interaction, page composition, and API calls. Routing is centralized in client/src/router/AppRouter.tsx; authenticated navigation is gated by client/src/components/auth/ProtectedRoute.tsx.
Major user-facing workflow pages are:
• onboarding/profile: client/src/pages/Profile.tsx
• recommendation results and details: client/src/pages/Results.tsx, client/src/pages/SchemeDetail.tsx
• eligibility what-if modeling: client/src/pages/EligibilitySimulator.tsx
• generated applications: client/src/pages/ApplicationDraft.tsx
client/src/context/AuthContext.tsx establishes client-side session state across protected flows.
3. Profile capture as the matching boundary
The multi-step profile wizard gathers the structured attributes needed for rule filtering—such as basic, location, education, occupation, and contact information. Its orchestrating UI is client/src/pages/Profile.tsx, with stages under client/src/components/Profile/steps/BasicInfo.tsx and client/src/components/Profile/steps/OccupationInfo.tsx.
The profile crosses the client/server boundary through client/src/services/profileApi.ts to server/src/routes/profile.ts and server/src/controllers/profileController.ts. Server-side parsing/normalization is separated in server/src/services/parseProfile.ts, protecting matching logic from raw client form shape.
4. Authentication and account boundary
Account registration/login and JWT-based authorization are exposed by server/src/routes/auth.ts and implemented in server/src/controllers/authController.ts. User persistence is defined by server/src/models/user.ts.
Protected API access is enforced in server/src/middleware/authMiddleware.ts; credentials and tokens are handled by server/src/utils/hash.ts and server/src/utils/generateToken.ts. The frontend consumes this boundary through client/src/services/auth.ts and retains session behavior in client/src/context/AuthContext.tsx.
5. Scheme catalog, ingestion, and vector enrichment
Schemes are persisted as MongoDB documents via server/src/models/Scheme.ts and served through server/src/routes/schemes.ts / server/src/controllers/schemeController.ts. The seed source is server/src/data/schemes.json.
The catalog has an offline preparation path: server/src/scripts/insertSchemes.ts loads schemes, while server/src/scripts/generateEmbeddings.ts generates semantic vectors using server/src/services/embeddingService.ts. MongoDB is therefore both the operational data store and vector-search backend.
6. Explainable recommendation pipeline
Matching is the core backend pipeline. server/src/routes/match.ts invokes server/src/controllers/matchController.ts, which delegates eligibility/rule and semantic matching to server/src/services/matchingService.ts. Results are shaped into client-ready, explainable responses by server/src/services/matchResponseFormatter.ts and consumed via client/src/services/match.ts.
This separates deterministic profile constraints (age, state, income, occupation) from semantic retrieval over scheme content. Persisted match records are represented by server/src/models/Match.ts.
7. AI explanation, simulation, and application drafting
Gemini-backed natural-language eligibility explanations are exposed by server/src/routes/explain.ts, server/src/controllers/explainController.ts, and server/src/services/aiExplanationService.ts; the client calls client/src/services/explain.ts.
Simulation uses its own route/controller boundary—server/src/routes/simulator.ts and server/src/controllers/simulatorController.ts—for hypothetical profile adjustments. Draft generation follows server/src/routes/draft.ts, server/src/controllers/draftController.ts, and server/src/services/draftService.ts; client/src/services/draft.ts supports the application-draft page.
8. Infrastructure and outbound notifications
The Express application is configured around MongoDB connectivity in server/src/config/db.ts; deployment is containerized by Dockerfile. The API uses CORS/Helmet, JWT, bcrypt, and environment-provided MongoDB, Gemini, and client-origin settings.
Reminder records and delivery are isolated behind server/src/routes/reminders.ts, server/src/models/Reminder.ts, and server/src/services/whatsappService.ts. WhatsApp delivery is exposed separately through server/src/routes/whatsapp.ts and requires Twilio credentials; the client entry surface is client/src/services/reminder.ts.
Main entry points: app.ts Express app · AppRouter.tsx App router · match.ts Client API services · db.ts MongoDB
Components 21
React client
- SPA entry
client/src/main.tsx - App router · Composes pages and protected navigation.
client/src/router/AppRouter.tsx - Auth session
client/src/context/AuthContext.tsx - Profile wizard · Captures structured matching attributes.
client/src/pages/Profile.tsx - Results and details
client/src/pages/Results.tsx - Eligibility simulator
client/src/pages/EligibilitySimulator.tsx - Application drafts
client/src/pages/ApplicationDraft.tsx - Client API services · Auth, profile, match, explain, draft, and reminder calls.
client/src/services/match.ts
Express API
- API server
server/src/server.ts - Express app · Configures CORS, Helmet, and route mounting.
server/src/app.ts - Auth and authorization · JWT account boundary backed by user records.
server/src/routes/auth.ts - Profile normalization · Normalizes form data before matching.
server/src/services/parseProfile.ts - Recommendation pipeline · Combines deterministic eligibility with semantic retrieval and explainable formatting.
server/src/services/matchingService.ts - AI explanations and drafts · Provides eligibility explanations and application draft generation.
server/src/services/aiExplanationService.ts - Simulation API · Evaluates hypothetical profile adjustments.
server/src/routes/simulator.ts - Reminders and WhatsApp · Stores reminders and optionally sends Twilio WhatsApp messages.
server/src/routes/reminders.ts
Catalog and storage
- MongoDB · Stores users, schemes, matches, and reminders; supports vector search.
server/src/config/db.ts - Scheme catalog
server/src/models/Scheme.ts - Catalog enrichment · Seeds schemes and generates embeddings.
server/src/scripts/generateEmbeddings.ts
External services
- Gemini
- Twilio WhatsApp · Optional outbound reminder delivery.
Connections 29
- SPA entry → App router: starts
- App router → Auth session: gates protected flows
- App router → Profile wizard: profile flow
- App router → Results and details: recommendation flow
- App router → Eligibility simulator: what-if flow
- App router → Application drafts: draft flow
- Auth session → Client API services: auth requests
- Profile wizard → Client API services: submits profile
- Results and details → Client API services: gets matches and explanations
- Eligibility simulator → Client API services: submits scenario
- Application drafts → Client API services: requests draft
- Client API services → Express app: HTTP API
- API server → Express app: runs
- Express app → Auth and authorization: auth routes
- Express app → Profile normalization: profile routes
- Express app → Recommendation pipeline: match routes
- Express app → AI explanations and drafts: explain and draft routes
- Express app → Simulation API: simulator routes
- Express app → Reminders and WhatsApp: reminder routes
- Auth and authorization → MongoDB: users and JWT access
- Profile normalization → Recommendation pipeline: normalized profile
- Recommendation pipeline → MongoDB: vector retrieval and match records
- MongoDB → Scheme catalog: stores schemes
- Catalog enrichment → MongoDB: seeds and indexes
- Catalog enrichment → Gemini: generates embeddings
- AI explanations and drafts → Gemini: generates explanations and drafts
- Simulation API → Recommendation pipeline: evaluates adjusted profile
- Reminders and WhatsApp → MongoDB: stores reminders
- Reminders and WhatsApp → Twilio WhatsApp: sends WhatsApp