# shrived00/chatwithpdf architecture

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

- Interactive diagram: https://gitdiagram.com/shrived00/chatwithpdf
- Repository: https://github.com/shrived00/chatwithpdf

## Overview

##### Project type and purpose
- This is a full-stack Next.js application (Next.js 13 “app” router) that provides a chat interface with file/PDF upload and AI-driven embeddings/search.
- It uses server-side API routes for chat creation and messaging, a relational database via Drizzle ORM, vector search via Pinecone, file storage (S3), and Firebase for authentication.

##### File structure
- Top level: next.config.mjs, drizzle.config.ts, tailwind.config.ts, middleware.ts.
- src/app: React pages & layouts (app/page.tsx, app/layout.tsx), nested routes for /chat/[chatId], /sign-in, /sign-up.
- src/app/api: serverless routes chat (route.tsx) and create-chat (route.ts).
- src/components: reusable UI (ChatComponent, ChatSideBar, FileUpload, PDFViewer) and a local “ui” primitives library.
- src/lib: business logic & data layer
- lib/context.ts – React context/provider
- lib/db – Drizzle ORM schema & index
- lib/embeddings.ts – OpenAI embedding logic
- lib/pinecone.ts – Pinecone vector store integration
- lib/s3-server.ts – AWS S3 file storage helper
- lib/utils.ts – misc utilities
- firebase.ts: Firebase SDK initialization for auth.
- drizzle.config.ts: configuration for relational database.

##### Extract insights from README
- Bootstrapped with create-next-app, uses Next.js features and API routes, deployed on Vercel.

##### Elements
- Clear labels: “Browser (React)”, “Next.js API: create-chat”, “Next.js API: chat”, “Relational DB (Drizzle)”, “Vector DB (Pinecone)”, “File Storage (S3)”, “Auth (Firebase)”, “AI Service (OpenAI)”.
- From Browser to API routes (with HTTP verbs e.g. POST /api/chat)
- From API routes to each service or library layer
- Back from services to API then to Browser
- Blue rectangles for internal components (frontend, API)
- Orange clouds for external services (Firebase, Pinecone, S3, OpenAI)

## Diagram (Mermaid)

```mermaid
flowchart TB
  %% Client Side
  subgraph "Client Side"
    direction TB
    Browser["Browser (React)"]:::frontend
    subgraph "Pages & Layout"
      direction TB
      Layout["layout.tsx"]:::frontend
      HomePage["page.tsx"]:::frontend
      ChatPage["[chatId] page.tsx"]:::frontend
      SignIn["[[...sign-in]] page.tsx"]:::frontend
      SignUp["[[...sign-up]] page.tsx"]:::frontend
    end
    subgraph "Components"
      direction TB
      ChatCmp["ChatComponent.tsx"]:::frontend
      ChatSide["ChatSideBar.tsx"]:::frontend
      FileUp["FileUpload.tsx"]:::frontend
      MsgList["MessageList.tsx"]:::frontend
      PDFView["PDFViewer.tsx"]:::frontend
    end
    subgraph "UI Primitives Library"
      direction TB
      Avatar["avatar.tsx"]:::frontend
      Badge["badge.tsx"]:::frontend
      Button["button.tsx"]:::frontend
      Card["card.tsx"]:::frontend
      Command["command.tsx"]:::frontend
      Dialog["dialog.tsx"]:::frontend
      Dropdown["dropdown-menu.tsx"]:::frontend
      Input["input.tsx"]:::frontend
      Resizable["resizable.tsx"]:::frontend
      ScrollArea["scroll-area.tsx"]:::frontend
      Sheet["sheet.tsx"]:::frontend
      SidebarUI["sidebar.tsx"]:::frontend
      Tooltip["tooltip.tsx"]:::frontend
    end
    Provider["Provider.tsx"]:::frontend
    AuthSDK["firebase.ts"]:::external
  end

  %% Server Side
  subgraph "Server Side (Next.js)"
    direction TB
    subgraph "API Routes"
      direction TB
      CreateChat["POST /api/create-chat"]:::backend
      ChatAPI["POST /api/chat"]:::backend
    end
    subgraph "Business Logic"
      direction TB
      Context["context.ts"]:::backend
      Utils["utils.ts"]:::backend
    end
    subgraph "Integrations"
      direction TB
      EmbeddingsLib["embeddings.ts"]:::backend
      PineconeLib["pinecone.ts"]:::backend
      S3Lib["s3-server.ts"]:::backend
    end
    subgraph "ORM & Config"
      direction TB
      DrizzleConfig["drizzle.config.ts"]:::backend
      DrizzleSchema["schema.ts"]:::backend
      DrizzleIndex["index.ts"]:::backend
    end
  end

  %% Databases & External Services
  subgraph "Databases & Services"
    direction TB
    DB["Relational DB"]:::database
    PineconeDB["Pinecone Vector DB"]:::external
    S3["AWS S3"]:::external
    OpenAI["OpenAI API"]:::external
    FirebaseAuth["Firebase Auth"]:::external
  end

  %% Connections
  Browser -->|"POST /api/create-chat"| CreateChat
  Browser -->|"POST /api/chat"| ChatAPI
  Browser -->|"JWT Auth SDK"| AuthSDK
  AuthSDK -->|"Validate Token"| CreateChat
  AuthSDK -->|"Validate Token"| ChatAPI

  CreateChat -->|"Drizzle ORM"| DrizzleSchema
  DrizzleSchema --> DrizzleIndex
  DrizzleIndex --> DrizzleConfig
  DrizzleConfig --> DB

  ChatAPI -->|"Compute Embeddings"| EmbeddingsLib
  EmbeddingsLib -->|"OpenAI Embeddings"| OpenAI
  ChatAPI -->|"Vector Search"| PineconeLib
  PineconeLib --> PineconeDB
  ChatAPI -->|"File Access"| S3Lib
  S3Lib --> S3
  ChatAPI -->|"DB Metadata"| DrizzleSchema

  ChatAPI -->|"Streaming JSON"| Browser

  %% Click Events
  click Layout "https://github.com/shrived00/chatwithpdf/blob/main/src/app/layout.tsx"
  click HomePage "https://github.com/shrived00/chatwithpdf/blob/main/src/app/page.tsx"
  click ChatPage "https://github.com/shrived00/chatwithpdf/blob/main/src/app/chat/[chatId]/page.tsx"
  click SignIn "https://github.com/shrived00/chatwithpdf/blob/main/src/app/sign-in/[[...sign-in]]/page.tsx"
  click SignUp "https://github.com/shrived00/chatwithpdf/blob/main/src/app/sign-up/[[...sign-up]]/page.tsx"

  click ChatCmp "https://github.com/shrived00/chatwithpdf/blob/main/src/components/ChatComponent.tsx"
  click ChatSide "https://github.com/shrived00/chatwithpdf/blob/main/src/components/ChatSideBar.tsx"
  click FileUp "https://github.com/shrived00/chatwithpdf/blob/main/src/components/FileUpload.tsx"
  click MsgList "https://github.com/shrived00/chatwithpdf/blob/main/src/components/MessageList.tsx"
  click PDFView "https://github.com/shrived00/chatwithpdf/blob/main/src/components/PDFViewer.tsx"

  click Avatar "https://github.com/shrived00/chatwithpdf/blob/main/src/components/ui/avatar.tsx"
  click Badge "https://github.com/shrived00/chatwithpdf/blob/main/src/components/ui/badge.tsx"
  click Button "https://github.com/shrived00/chatwithpdf/blob/main/src/components/ui/button.tsx"
  click Card "https://github.com/shrived00/chatwithpdf/blob/main/src/components/ui/card.tsx"
  click Command "https://github.com/shrived00/chatwithpdf/blob/main/src/components/ui/command.tsx"
  click Dialog "https://github.com/shrived00/chatwithpdf/blob/main/src/components/ui/dialog.tsx"
  click Dropdown "https://github.com/shrived00/chatwithpdf/blob/main/src/components/ui/dropdown-menu.tsx"
  click Input "https://github.com/shrived00/chatwithpdf/blob/main/src/components/ui/input.tsx"
  click Resizable "https://github.com/shrived00/chatwithpdf/blob/main/src/components/ui/resizable.tsx"
  click ScrollArea "https://github.com/shrived00/chatwithpdf/blob/main/src/components/ui/scroll-area.tsx"
  click Sheet "https://github.com/shrived00/chatwithpdf/blob/main/src/components/ui/sheet.tsx"
  click SidebarUI "https://github.com/shrived00/chatwithpdf/blob/main/src/components/ui/sidebar.tsx"
  click Tooltip "https://github.com/shrived00/chatwithpdf/blob/main/src/components/ui/tooltip.tsx"

  click Context "https://github.com/shrived00/chatwithpdf/blob/main/src/lib/context.ts"
  click Utils "https://github.com/shrived00/chatwithpdf/blob/main/src/lib/utils.ts"
  click CreateChat "https://github.com/shrived00/chatwithpdf/blob/main/src/app/api/create-chat/route.ts"
  click ChatAPI "https://github.com/shrived00/chatwithpdf/blob/main/src/app/api/chat/route.tsx"
  click DrizzleConfig "https://github.com/shrived00/chatwithpdf/blob/main/drizzle.config.ts"
  click DrizzleSchema "https://github.com/shrived00/chatwithpdf/blob/main/src/lib/db/schema.ts"
  click DrizzleIndex "https://github.com/shrived00/chatwithpdf/blob/main/src/lib/db/index.ts"
  click EmbeddingsLib "https://github.com/shrived00/chatwithpdf/blob/main/src/lib/embeddings.ts"
  click PineconeLib "https://github.com/shrived00/chatwithpdf/blob/main/src/lib/pinecone.ts"
  click S3Lib "https://github.com/shrived00/chatwithpdf/blob/main/src/lib/s3-server.ts"
  click AuthSDK "https://github.com/shrived00/chatwithpdf/blob/main/src/firebase.ts"
  click Browser "https://github.com/shrived00/chatwithpdf/blob/main/src/app/page.tsx"

  click AuthSDK "https://github.com/shrived00/chatwithpdf/blob/main/src/firebase.ts"
  click Provider "https://github.com/shrived00/chatwithpdf/blob/main/src/components/Provider.tsx"

  %% Styles
  classDef frontend fill:#D0E8FF,stroke:#0366d6,stroke-width:1px
  classDef backend fill:#BDE5A8,stroke:#237804,stroke-width:1px
  classDef database fill:#C8F7C5,stroke:#2E8B57,stroke-width:1px
  classDef external fill:#FFE0B2,stroke:#E65100,stroke-width:1px
```
