# elliott-chong/chatpdf-yt architecture

> Architecture diagram of the GitHub repository elliott-chong/chatpdf-yt, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-02-07). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/elliott-chong/chatpdf-yt
- Repository: https://github.com/elliott-chong/chatpdf-yt

## Overview

##### Main Components

A. Client-Side (Frontend) Layer:
- Next.js Pages and Components
- React Query for state management
- PDF Viewer component
- Chat Interface components

B. Server-Side (Backend) Layer:
- Next.js API Routes
- Authentication System (Clerk)
- File Storage (AWS S3)
- Database (PostgreSQL with Drizzle ORM)
- Vector Database (Pinecone)
- AI Integration (OpenAI)
- Payment Processing (Stripe)

**┌─ Client Layer ─────────────────────┐**
- Pages (/, /chat/[chatId], etc.)  │
- React Components                 │
- UI Components                    │
- ┬───────────────────┘
- ↓
**┌─ API Layer ────────────────────────┐**
- Authentication Middleware        │
- API Routes (/api/*)             │
- Webhook Handlers                │
- ┬───────────────────┘
- ↓
**┌─ Service Layer ─────────────────────┐**
- PDF Processing                   │
- Chat Management                 │
- Subscription Handling           │
- File Storage Operations         │
- ┬───────────────────┘
- ↓
**┌─ External Services ────────────────┐**
- Clerk Auth                      │
- AWS S3                         │
- OpenAI                         │
- Pinecone                       │
- Stripe                         │
- ┬───────────────────┘
- ↓
**┌─ Database Layer ────────────────────┐**
- PostgreSQL (via Drizzle ORM)     │
- Schema (users, chats, messages)  │
- ┘

##### Key Flows to Highlight

A. PDF Processing Flow:
**User → FileUpload → S3 Storage → Pinecone Embeddings → Database**

B. Chat Flow:
**User → ChatComponent → API Routes → OpenAI → Database → Response**

C. Subscription Flow:
**User → SubscriptionButton → Stripe API → Webhook → Database**

##### Important Details

- Relationship between components in /src/components and their corresponding API routes
- How the middleware.ts file handles authentication across the application
- Demonstrate the connection between the database schema (schema.ts) and the API routes
- How the Providers component wraps the application for global state and authentication

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Client
        Home["Home Page"]:::frontend
        ChatPage["Chat Page"]:::frontend
        Layout["Root Layout"]:::frontend
        ChatUI["Chat Interface"]:::frontend
        PDFView["PDF Viewer"]:::frontend
        FileUp["File Upload"]:::frontend
        SubBtn["Subscription Button"]:::frontend
        Providers["App Providers"]:::frontend
    end

    subgraph APILayer
        Auth["Authentication Middleware"]:::api
        ChatAPI["Chat API Routes"]:::api
        StripeAPI["Stripe API Routes"]:::api
        CreateChat["Create Chat Route"]:::api
        GetMessages["Get Messages Route"]:::api
        WebhookHandler["Webhook Handler"]:::api
    end

    subgraph Services
        PDFProcess["PDF Processing"]:::service
        ChatManage["Chat Management"]:::service
        SubHandle["Subscription Handler"]:::service
        FileStore["File Storage"]:::service
        Embed["Embeddings Service"]:::service
    end

    subgraph External
        Clerk["Clerk Auth"]:::external
        S3["AWS S3"]:::external
        OpenAI["OpenAI"]:::external
        Pinecone["Pinecone Vector DB"]:::external
        Stripe["Stripe Payment"]:::external
    end

    subgraph Database
        PostgreSQL["PostgreSQL"]:::db
        Schema["Database Schema"]:::db
    end

    %% Client Layer Connections
    Home --> ChatPage
    ChatPage --> ChatUI
    ChatPage --> PDFView
    ChatUI --> FileUp
    Layout --> Providers

    %% API Layer Connections
    ChatUI --> ChatAPI
    FileUp --> CreateChat
    ChatUI --> GetMessages
    SubBtn --> StripeAPI
    StripeAPI --> WebhookHandler

    %% Service Layer Connections
    FileUp --> PDFProcess
    PDFProcess --> FileStore
    PDFProcess --> Embed
    ChatAPI --> ChatManage
    StripeAPI --> SubHandle

    %% External Service Connections
    Auth --> Clerk
    FileStore --> S3
    ChatManage --> OpenAI
    Embed --> Pinecone
    SubHandle --> Stripe

    %% Database Connections
    ChatManage --> PostgreSQL
    SubHandle --> PostgreSQL
    PostgreSQL --- Schema

    %% Click Events
    click ChatUI "https://github.com/Elliott-Chong/chatpdf-yt/blob/main/src/components/ChatComponent.tsx"
    click PDFView "https://github.com/Elliott-Chong/chatpdf-yt/blob/main/src/components/PDFViewer.tsx"
    click FileUp "https://github.com/Elliott-Chong/chatpdf-yt/blob/main/src/components/FileUpload.tsx"
    click SubBtn "https://github.com/Elliott-Chong/chatpdf-yt/blob/main/src/components/SubscriptionButton.tsx"
    click Providers "https://github.com/Elliott-Chong/chatpdf-yt/blob/main/src/components/Providers.tsx"
    click Auth "https://github.com/Elliott-Chong/chatpdf-yt/blob/main/src/middleware.ts"
    click ChatAPI "https://github.com/Elliott-Chong/chatpdf-yt/blob/main/src/app/api/chat/route.ts"
    click CreateChat "https://github.com/Elliott-Chong/chatpdf-yt/blob/main/src/app/api/create-chat/route.ts"
    click GetMessages "https://github.com/Elliott-Chong/chatpdf-yt/blob/main/src/app/api/get-messages/route.ts"
    click WebhookHandler "https://github.com/Elliott-Chong/chatpdf-yt/blob/main/src/app/api/webhook/route.ts"
    click Embed "https://github.com/Elliott-Chong/chatpdf-yt/blob/main/src/lib/embeddings.ts"
    click Schema "https://github.com/Elliott-Chong/chatpdf-yt/blob/main/src/lib/db/schema.ts"
    click PostgreSQL "https://github.com/Elliott-Chong/chatpdf-yt/blob/main/src/lib/db/index.ts"
    click FileStore "https://github.com/Elliott-Chong/chatpdf-yt/blob/main/src/lib/s3.ts"
    click Pinecone "https://github.com/Elliott-Chong/chatpdf-yt/blob/main/src/lib/pinecone.ts"
    click Stripe "https://github.com/Elliott-Chong/chatpdf-yt/blob/main/src/lib/stripe.ts"
    click Home "https://github.com/Elliott-Chong/chatpdf-yt/blob/main/src/app/page.tsx"
    click ChatPage "https://github.com/Elliott-Chong/chatpdf-yt/blob/main/src/app/chat/[chatId]/page.tsx"
    click Layout "https://github.com/Elliott-Chong/chatpdf-yt/blob/main/src/app/layout.tsx"

    %% Styles
    classDef frontend fill:#2196F3,color:white
    classDef api fill:#4CAF50,color:white
    classDef service fill:#FF9800,color:white
    classDef external fill:#FFC107,color:black
    classDef db fill:#9C27B0,color:white

    %% Legend
    subgraph Legend
        Frontend["Frontend Component"]:::frontend
        APIRoute["API Route"]:::api
        ServiceComp["Service Component"]:::service
        ExternalServ["External Service"]:::external
        Database["Database Component"]:::db
    end
```
