# abrar-husain-870/sahayak-ai architecture

> Architecture diagram of the GitHub repository abrar-husain-870/sahayak-ai, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-09-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/abrar-husain-870/sahayak-ai
- Repository: https://github.com/abrar-husain-870/sahayak-ai

## Overview

##### Project Type and Purpose
- Sahayak-AI is a full-stack Next.js (v13+) application that serves as an AI-powered teaching assistant.
- It delivers a React-based dashboard UI, runs serverless API routes for auth, registration, history, and orchestrates AI flows (via OpenAI) to generate lesson content, visual aids, differentiated materials, etc.

##### File Structure Analysis
- src/app: Next.js “app” directory containing server-rendered and client components, layouts, and pages (dashboard, login, register).
- src/components: Shared UI primitives (buttons, cards, dialogs) and feature-specific result viewers.
- src/app/api: Next.js serverless routes for authentication (NextAuth), user registration, and history retrieval/storage.
- src/ai: Core AI orchestration module (genkit.ts) and individual flow definitions (e.g., week-plan, visual aids) that call out to OpenAI.
- src/lib: Firebase configuration (likely Firestore as the data store), utility functions, constants.
- Configuration: next.config.ts, tailwind.config.ts, postcss.config.mjs, apphosting.yaml (deployment), dev.nix (.idx/dev.nix)
- Middleware: src/middleware.ts for auth checks or routing guards.

##### Key Components
a. Client (Browser)
- React components under src/app and src/components.
- UI state managed via hooks (use-mobile, use-toast), theme/provider wrappers, global CSS.
b. Next.js Server
- Route handlers (server-rendered pages) and API endpoints (src/app/api).
- Middleware for auth gating.
c. Authentication Layer
- NextAuth engine ([...nextauth] route), sessions, OAuth providers.
- Firebase integration (src/lib/firebase.ts) for persisting user profiles or session data.
d. AI Engine
- AI genkit (src/ai/genkit.ts) orchestrates calls to OpenAI API.
- Per-flow modules under src/ai/flows define prompts and response parsing.
e. Data Layer
- Firestore (Firebase) storing: user data, history of generated content.
f. External Services
- OpenAI API for content generation.
- Firebase Auth/Firestore as managed services.
- Hosting provider (Vercel/GCP via apphosting.yaml).

##### Relationships & Interactions
- User → Frontend UI: HTTP(S) GET/POST for SSR or client navigation.
- UI → API Route (e.g., /api/history): fetch or mutate history records.
- UI → AI Endpoint (internal server function): triggers genkit → OpenAI → returns generated content.
- API Route (NextAuth) ↔ Firebase Auth: validate credentials, issue session.
- Data Persistance: successful AI runs or user actions → Firestore.

##### Architectural Patterns
- Serverless functions (API routes) for backend logic.
- Separation of concerns: UI layer vs. AI orchestration vs. data persistence.
- Component-driven design (reusable UI primitives).
- Middleware for cross-cutting concerns (auth).
- Flows-as-plugins: each AI feature is a distinct module implementing a common interface.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    subgraph "Client Layer"
        direction TB
        Browser["User Browser"]:::frontend
        AppDir["App Directory\nsrc/app"]:::frontend
        PageHome["Home Page\nsrc/app/page.tsx"]:::frontend
        LoginPage["Login Page\nsrc/app/login/page.tsx"]:::frontend
        RegisterPage["Register Page\nsrc/app/register/page.tsx"]:::frontend
        DashboardDir["Dashboard\nsrc/app/(dashboard)"]:::frontend
        GlobalsCSS["Global Styles\nglobals.css"]:::frontend
        Providers["Providers\nproviders.tsx"]:::frontend
        ComponentsDir["Components\nsrc/components"]:::frontend
        HookMobile["use-mobile\nsrc/hooks/use-mobile.tsx"]:::frontend
        HookToast["use-toast\nsrc/hooks/use-toast.ts"]:::frontend
    end

    %% Server Layer
    subgraph "Server Layer"
        direction TB
        NextJS["Next.js Server\nRuntime"]:::backend
        Middleware["Middleware\nsrc/middleware.ts"]:::backend
        APIDir["API Routes\nsrc/app/api"]:::backend
        NextAuthRoute["NextAuth API\nsrc/app/api/auth/[...nextauth]/route.ts"]:::backend
        RegisterRoute["Register API\nsrc/app/api/register/route.ts"]:::backend
        HistoryRoute["History API\nsrc/app/api/history/route.ts"]:::backend
    end

    %% AI Processing Layer
    subgraph "AI Processing Layer"
        direction TB
        Genkit["Genkit Core\nsrc/ai/genkit.ts"]:::ai
        FlowKB["Instant KB Flow\nsrc/ai/flows/act-as-instant-knowledge-base.ts"]:::ai
        FlowDiff["Differentiated Materials\nsrc/ai/flows/create-differentiated-materials.ts"]:::ai
        FlowWeekly["Weekly Plans\nsrc/ai/flows/create-weekly-lesson-plans.ts"]:::ai
        FlowVisual["Visual Aids\nsrc/ai/flows/design-visual-aids.ts"]:::ai
        FlowHyper["Hyper-local Content\nsrc/ai/flows/generate-hyper-local-content.ts"]:::ai
        DevHarness["Dev Harness\nsrc/ai/dev.ts"]:::ai
    end

    %% Data Layer
    subgraph "Data Layer"
        direction TB
        FirebaseInit["Firebase Init\nsrc/lib/firebase.ts"]:::data
        Constants["Constants\nsrc/lib/constants.ts"]:::data
        Firestore["Firestore Database"]:::data
        AuthService["Firebase Auth Service"]:::data
    end

    %% External Services
    subgraph "External Services"
        direction TB
        OpenAI["OpenAI API"]:::external
        Hosting["Hosting CDN\napphosting.yaml"]:::external
    end

    %% Configuration
    subgraph "Configuration"
        direction TB
        NextConfig["next.config.ts"]:::config
        Tailwind["tailwind.config.ts"]:::config
        PostCSS["postcss.config.mjs"]:::config
        DevNix[".idx/dev.nix"]:::config
    end

    %% Relationships
    Browser -->|"navigate"| NextJS
    NextJS -->|"render page"| PageHome
    NextJS -->|"render page"| LoginPage
    NextJS -->|"render page"| RegisterPage
    NextJS -->|"render dashboard"| DashboardDir

    PageHome --> NextJS
    LoginPage --> NextJS
    RegisterPage --> NextJS
    DashboardDir --> NextJS

    NextJS --> Middleware
    Middleware --> NextJS

    NextJS --> APIDir
    APIDir --> NextAuthRoute
    APIDir --> RegisterRoute
    APIDir --> HistoryRoute

    NextAuthRoute <-->|"validate credentials"| AuthService
    AuthService --> FirebaseInit
    FirebaseInit --> AuthService

    HistoryRoute --> FirebaseInit
    FirebaseInit --> Firestore
    Constants --> Firestore

    APIDir --> Genkit
    Genkit --> FlowKB
    Genkit --> FlowDiff
    Genkit --> FlowWeekly
    Genkit --> FlowVisual
    Genkit --> FlowHyper
    DevHarness --> Genkit

    Genkit -->|"OpenAI calls"| OpenAI
    OpenAI --> Genkit

    Genkit --> FirebaseInit

    Hosting --> NextJS
    NextConfig --> NextJS
    Tailwind --> NextJS
    PostCSS --> NextJS
    DevNix --> NextJS

    %% Click Events - Client Layer
    click AppDir "https://github.com/abrar-husain-870/sahayak-ai/tree/main/src/app"
    click PageHome "https://github.com/abrar-husain-870/sahayak-ai/blob/main/src/app/page.tsx"
    click LoginPage "https://github.com/abrar-husain-870/sahayak-ai/blob/main/src/app/login/page.tsx"
    click RegisterPage "https://github.com/abrar-husain-870/sahayak-ai/blob/main/src/app/register/page.tsx"
    click DashboardDir "https://github.com/abrar-husain-870/sahayak-ai/tree/main/src/app/(dashboard)"
    click GlobalsCSS "https://github.com/abrar-husain-870/sahayak-ai/blob/main/src/app/globals.css"
    click Providers "https://github.com/abrar-husain-870/sahayak-ai/blob/main/src/app/providers.tsx"
    click ComponentsDir "https://github.com/abrar-husain-870/sahayak-ai/tree/main/src/components"
    click HookMobile "https://github.com/abrar-husain-870/sahayak-ai/blob/main/src/hooks/use-mobile.tsx"
    click HookToast "https://github.com/abrar-husain-870/sahayak-ai/blob/main/src/hooks/use-toast.ts"

    %% Click Events - Server Layer
    click NextJS "https://github.com/abrar-husain-870/sahayak-ai/tree/main/src/app"
    click Middleware "https://github.com/abrar-husain-870/sahayak-ai/blob/main/src/middleware.ts"
    click APIDir "https://github.com/abrar-husain-870/sahayak-ai/tree/main/src/app/api"
    click NextAuthRoute "https://github.com/abrar-husain-870/sahayak-ai/blob/main/src/app/api/auth/[...nextauth]/route.ts"
    click RegisterRoute "https://github.com/abrar-husain-870/sahayak-ai/blob/main/src/app/api/register/route.ts"
    click HistoryRoute "https://github.com/abrar-husain-870/sahayak-ai/blob/main/src/app/api/history/route.ts"

    %% Click Events - AI Layer
    click Genkit "https://github.com/abrar-husain-870/sahayak-ai/blob/main/src/ai/genkit.ts"
    click FlowKB "https://github.com/abrar-husain-870/sahayak-ai/blob/main/src/ai/flows/act-as-instant-knowledge-base.ts"
    click FlowDiff "https://github.com/abrar-husain-870/sahayak-ai/blob/main/src/ai/flows/create-differentiated-materials.ts"
    click FlowWeekly "https://github.com/abrar-husain-870/sahayak-ai/blob/main/src/ai/flows/create-weekly-lesson-plans.ts"
    click FlowVisual "https://github.com/abrar-husain-870/sahayak-ai/blob/main/src/ai/flows/design-visual-aids.ts"
    click FlowHyper "https://github.com/abrar-husain-870/sahayak-ai/blob/main/src/ai/flows/generate-hyper-local-content.ts"
    click DevHarness "https://github.com/abrar-husain-870/sahayak-ai/blob/main/src/ai/dev.ts"

    %% Click Events - Data Layer
    click FirebaseInit "https://github.com/abrar-husain-870/sahayak-ai/blob/main/src/lib/firebase.ts"
    click Constants "https://github.com/abrar-husain-870/sahayak-ai/blob/main/src/lib/constants.ts"

    %% Click Events - External & Config
    click Hosting "https://github.com/abrar-husain-870/sahayak-ai/blob/main/apphosting.yaml"
    click NextConfig "https://github.com/abrar-husain-870/sahayak-ai/blob/main/next.config.ts"
    click Tailwind "https://github.com/abrar-husain-870/sahayak-ai/blob/main/tailwind.config.ts"
    click PostCSS "https://github.com/abrar-husain-870/sahayak-ai/blob/main/postcss.config.mjs"
    click DevNix "https://github.com/abrar-husain-870/sahayak-ai/blob/main/.idx/dev.nix"

    %% Styles
    classDef frontend fill:#cce5ff,stroke:#004085;
    classDef backend fill:#d4edda,stroke:#155724;
    classDef ai fill:#ffeeba,stroke:#856404;
    classDef data fill:#e2e3e5,stroke:#6c757d;
    classDef external fill:#f1f1f1,stroke:#333;
    classDef config fill:#d6d8db,stroke:#1b1e21;
```
