# yashsaxena27/setu-ai architecture

> Architecture diagram of the GitHub repository yashsaxena27/setu-ai, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-19). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/yashsaxena27/setu-ai
- Repository: https://github.com/yashsaxena27/setu-ai

## Overview

#### 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: [`server/src/app.ts`](https://github.com/yashsaxena27/setu-ai/blob/main/server/src/app.ts) (Express app), [`client/src/router/AppRouter.tsx`](https://github.com/yashsaxena27/setu-ai/blob/main/client/src/router/AppRouter.tsx) (App router), [`client/src/services/match.ts`](https://github.com/yashsaxena27/setu-ai/blob/main/client/src/services/match.ts) (Client API services), [`server/src/config/db.ts`](https://github.com/yashsaxena27/setu-ai/blob/main/server/src/config/db.ts) (MongoDB)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["React client"]
  node_client_entry["SPA entry<br/>React runtime<br/>[main.tsx]"]
  node_router["App router<br/>routing<br/>[AppRouter.tsx]"]
  node_auth_state["Auth session<br/>client context<br/>[AuthContext.tsx]"]
  node_profile_ui["Profile wizard<br/>workflow page<br/>[Profile.tsx]"]
  node_results_ui["Results and details<br/>recommendation pages<br/>[Results.tsx]"]
  node_simulator_ui["Eligibility simulator<br/>workflow page"]
  node_draft_ui["Application drafts<br/>workflow page"]
  node_client_api["Client API services<br/>HTTP client boundary<br/>[match.ts]"]
end

subgraph group_api["Express API"]
  node_server_entry["API server<br/>Express runtime<br/>[server.ts]"]
  node_api_app["Express app<br/>middleware and routes<br/>[app.ts]"]
  node_auth_api["Auth and authorization<br/>route/controller/middleware<br/>[auth.ts]"]
  node_profile_api["Profile normalization<br/>route/controller/service<br/>[parseProfile.ts]"]
  node_matching{{"Recommendation pipeline<br/>matching service<br/>[matchingService.ts]"}}
  node_ai_workflows["AI explanations and drafts<br/>AI services"]
  node_simulation_api["Simulation API<br/>route/controller<br/>[simulator.ts]"]
  node_reminders["Reminders and WhatsApp<br/>notification boundary<br/>[reminders.ts]"]
end

subgraph group_data["Catalog and storage"]
  node_mongodb[("MongoDB<br/>operational and vector store<br/>[db.ts]")]
  node_scheme_catalog["Scheme catalog<br/>Mongo document model<br/>[Scheme.ts]"]
  node_catalog_ingestion["Catalog enrichment<br/>offline pipeline"]
end

subgraph group_external["External services"]
  node_gemini{{"Gemini<br/>LLM and embeddings"}}
  node_twilio["Twilio WhatsApp<br/>delivery provider"]
end

node_client_entry -->|"starts"| node_router
node_router -->|"gates protected flows"| node_auth_state
node_router -->|"profile flow"| node_profile_ui
node_router -->|"recommendation flow"| node_results_ui
node_router -->|"what-if flow"| node_simulator_ui
node_router -->|"draft flow"| node_draft_ui
node_auth_state -->|"auth requests"| node_client_api
node_profile_ui -->|"submits profile"| node_client_api
node_results_ui -->|"gets matches and explanations"| node_client_api
node_simulator_ui -->|"submits scenario"| node_client_api
node_draft_ui -->|"requests draft"| node_client_api
node_client_api -->|"HTTP API"| node_api_app
node_server_entry -->|"runs"| node_api_app
node_api_app -->|"auth routes"| node_auth_api
node_api_app -->|"profile routes"| node_profile_api
node_api_app -->|"match routes"| node_matching
node_api_app -->|"explain and draft routes"| node_ai_workflows
node_api_app -->|"simulator routes"| node_simulation_api
node_api_app -->|"reminder routes"| node_reminders
node_auth_api -->|"users and JWT access"| node_mongodb
node_profile_api -->|"normalized profile"| node_matching
node_matching -->|"vector retrieval and match records"| node_mongodb
node_mongodb -->|"stores schemes"| node_scheme_catalog
node_catalog_ingestion -->|"seeds and indexes"| node_mongodb
node_catalog_ingestion -->|"generates embeddings"| node_gemini
node_ai_workflows -->|"generates explanations and drafts"| node_gemini
node_simulation_api -->|"evaluates adjusted profile"| node_matching
node_reminders -->|"stores reminders"| node_mongodb
node_reminders -.->|"sends WhatsApp"| node_twilio

click node_client_entry "https://github.com/yashsaxena27/setu-ai/blob/main/client/src/main.tsx"
click node_router "https://github.com/yashsaxena27/setu-ai/blob/main/client/src/router/AppRouter.tsx"
click node_auth_state "https://github.com/yashsaxena27/setu-ai/blob/main/client/src/context/AuthContext.tsx"
click node_profile_ui "https://github.com/yashsaxena27/setu-ai/blob/main/client/src/pages/Profile.tsx"
click node_results_ui "https://github.com/yashsaxena27/setu-ai/blob/main/client/src/pages/Results.tsx"
click node_simulator_ui "https://github.com/yashsaxena27/setu-ai/blob/main/client/src/pages/EligibilitySimulator.tsx"
click node_draft_ui "https://github.com/yashsaxena27/setu-ai/blob/main/client/src/pages/ApplicationDraft.tsx"
click node_client_api "https://github.com/yashsaxena27/setu-ai/blob/main/client/src/services/match.ts"
click node_server_entry "https://github.com/yashsaxena27/setu-ai/blob/main/server/src/server.ts"
click node_api_app "https://github.com/yashsaxena27/setu-ai/blob/main/server/src/app.ts"
click node_auth_api "https://github.com/yashsaxena27/setu-ai/blob/main/server/src/routes/auth.ts"
click node_profile_api "https://github.com/yashsaxena27/setu-ai/blob/main/server/src/services/parseProfile.ts"
click node_matching "https://github.com/yashsaxena27/setu-ai/blob/main/server/src/services/matchingService.ts"
click node_ai_workflows "https://github.com/yashsaxena27/setu-ai/blob/main/server/src/services/aiExplanationService.ts"
click node_simulation_api "https://github.com/yashsaxena27/setu-ai/blob/main/server/src/routes/simulator.ts"
click node_reminders "https://github.com/yashsaxena27/setu-ai/blob/main/server/src/routes/reminders.ts"
click node_mongodb "https://github.com/yashsaxena27/setu-ai/blob/main/server/src/config/db.ts"
click node_scheme_catalog "https://github.com/yashsaxena27/setu-ai/blob/main/server/src/models/Scheme.ts"
click node_catalog_ingestion "https://github.com/yashsaxena27/setu-ai/blob/main/server/src/scripts/generateEmbeddings.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_client_entry,node_router,node_auth_state,node_profile_ui,node_results_ui,node_simulator_ui,node_draft_ui,node_client_api toneBlue
class node_server_entry,node_api_app,node_auth_api,node_profile_api,node_matching,node_ai_workflows,node_simulation_api,node_reminders toneAmber
class node_mongodb,node_scheme_catalog,node_catalog_ingestion toneMint
class node_gemini,node_twilio toneRose
```

## Components

### React client

- [SPA entry](https://github.com/yashsaxena27/setu-ai/blob/main/client/src/main.tsx): React runtime · `client/src/main.tsx`
- [App router](https://github.com/yashsaxena27/setu-ai/blob/main/client/src/router/AppRouter.tsx): routing · Composes pages and protected navigation. · `client/src/router/AppRouter.tsx`
- [Auth session](https://github.com/yashsaxena27/setu-ai/blob/main/client/src/context/AuthContext.tsx): client context · `client/src/context/AuthContext.tsx`
- [Profile wizard](https://github.com/yashsaxena27/setu-ai/blob/main/client/src/pages/Profile.tsx): workflow page · Captures structured matching attributes. · `client/src/pages/Profile.tsx`
- [Results and details](https://github.com/yashsaxena27/setu-ai/blob/main/client/src/pages/Results.tsx): recommendation pages · `client/src/pages/Results.tsx`
- [Eligibility simulator](https://github.com/yashsaxena27/setu-ai/blob/main/client/src/pages/EligibilitySimulator.tsx): workflow page · `client/src/pages/EligibilitySimulator.tsx`
- [Application drafts](https://github.com/yashsaxena27/setu-ai/blob/main/client/src/pages/ApplicationDraft.tsx): workflow page · `client/src/pages/ApplicationDraft.tsx`
- [Client API services](https://github.com/yashsaxena27/setu-ai/blob/main/client/src/services/match.ts): HTTP client boundary · Auth, profile, match, explain, draft, and reminder calls. · `client/src/services/match.ts`

### Express API

- [API server](https://github.com/yashsaxena27/setu-ai/blob/main/server/src/server.ts): Express runtime · `server/src/server.ts`
- [Express app](https://github.com/yashsaxena27/setu-ai/blob/main/server/src/app.ts): middleware and routes · Configures CORS, Helmet, and route mounting. · `server/src/app.ts`
- [Auth and authorization](https://github.com/yashsaxena27/setu-ai/blob/main/server/src/routes/auth.ts): route/controller/middleware · JWT account boundary backed by user records. · `server/src/routes/auth.ts`
- [Profile normalization](https://github.com/yashsaxena27/setu-ai/blob/main/server/src/services/parseProfile.ts): route/controller/service · Normalizes form data before matching. · `server/src/services/parseProfile.ts`
- [Recommendation pipeline](https://github.com/yashsaxena27/setu-ai/blob/main/server/src/services/matchingService.ts): matching service · Combines deterministic eligibility with semantic retrieval and explainable formatting. · `server/src/services/matchingService.ts`
- [AI explanations and drafts](https://github.com/yashsaxena27/setu-ai/blob/main/server/src/services/aiExplanationService.ts): AI services · Provides eligibility explanations and application draft generation. · `server/src/services/aiExplanationService.ts`
- [Simulation API](https://github.com/yashsaxena27/setu-ai/blob/main/server/src/routes/simulator.ts): route/controller · Evaluates hypothetical profile adjustments. · `server/src/routes/simulator.ts`
- [Reminders and WhatsApp](https://github.com/yashsaxena27/setu-ai/blob/main/server/src/routes/reminders.ts): notification boundary · Stores reminders and optionally sends Twilio WhatsApp messages. · `server/src/routes/reminders.ts`

### Catalog and storage

- [MongoDB](https://github.com/yashsaxena27/setu-ai/blob/main/server/src/config/db.ts): operational and vector store · Stores users, schemes, matches, and reminders; supports vector search. · `server/src/config/db.ts`
- [Scheme catalog](https://github.com/yashsaxena27/setu-ai/blob/main/server/src/models/Scheme.ts): Mongo document model · `server/src/models/Scheme.ts`
- [Catalog enrichment](https://github.com/yashsaxena27/setu-ai/blob/main/server/src/scripts/generateEmbeddings.ts): offline pipeline · Seeds schemes and generates embeddings. · `server/src/scripts/generateEmbeddings.ts`

### External services

- **Gemini**: LLM and embeddings
- **Twilio WhatsApp**: delivery provider · Optional outbound reminder delivery.

## Connections

- 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
