# williamwelsh/chatgpt-clone architecture

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

- Interactive diagram: https://gitdiagram.com/williamwelsh/chatgpt-clone
- Repository: https://github.com/williamwelsh/chatgpt-clone

## Overview

##### Project type and purpose
- This is a full-stack, server-rendered Next.js application that clones a ChatGPT-style chat interface.
- It uses Tailwind for styling, tRPC for type-safe RPC between client and server, Drizzle ORM with PlanetScale (MySQL) for persistence, and the OpenAI API as an external AI service.

##### File structure
- Top-level config files (.env.example, drizzle.config.ts, next.config.mjs, tailwind.config.ts, etc.) set up environment, build and linting.
- public/ holds static assets; styles/ has global CSS.
- src/components/Sidebar.tsx and other React components constitute the UI layer.
- src/pages/ contains Next.js pages:
- index.tsx – the main chat UI
- api/getAnswer.ts & api/panel.ts – two REST-style API endpoints
- api/trpc/[trpc].ts – the tRPC HTTP handler
- src/utils/api.ts – tRPC client setup and helper functions.
- src/server/api/ routers and root files define your tRPC backend.
- src/db/db.ts & src/db/schema.ts – Drizzle ORM initialization and table schema definitions.
- src/env.mjs – environment variable definitions.
- src/middleware.ts – any custom Next.js middleware.

##### README for additional insights
- The README describes how to wire up the PlanetScale database, run Drizzle migrations, install and run locally, and links to the live deployment.

a. Main components
- Client (Next.js front end)
- Backend (Next.js API layer / tRPC server / REST endpoints)
- Application logic (tRPC routers in server/api/routers/main.ts)
- ORM layer (Drizzle ORM in src/db)
- Database (PlanetScale MySQL)
- External AI service (OpenAI API)
b. Relationships & interactions
1. Browser → Next.js pages (React + Tailwind)
2. Pages call:
- tRPC client (via src/utils/api.ts) → /api/trpc → tRPC router → DB or OpenAI
- REST endpoints /api/getAnswer & /api/panel → handler functions → OpenAI or DB
3. tRPC router uses Drizzle ORM to read/write schema-defined tables in PlanetScale.
4. Handlers call OpenAI API to generate chat responses.
c. Architectural patterns
- Layered architecture (UI layer → API layer → Data layer).
- Type-safe RPC with tRPC to eliminate duplication of types between client & server.
- ORM abstraction (Drizzle) for SQL operations.
d. Key technologies
- Next.js for SSR/SSG and API routes
- tRPC for RPC endpoints
- Drizzle ORM + drizzle-kit for schema and migrations
- PlanetScale (MySQL) as the production database
- OpenAI API as an external LLM service
- Tailwind CSS for styling

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph "Frontend Layer"
        FE_Pages["Pages<br/>index.tsx"]:::frontend
        FE_Sidebar["Sidebar<br/>Sidebar.tsx"]:::frontend
        FE_UtilsAPI["tRPC Client<br/>api.ts"]:::frontend
        FE_Styles["Styling<br/>globals.css & tailwind.config.ts"]:::frontend
    end

    subgraph "Backend Layer"
        MW["Middleware<br/>middleware.ts"]:::backend
        API_Get["REST Endpoint<br/>getAnswer.ts"]:::backend
        API_Panel["REST Endpoint<br/>panel.ts"]:::backend
        TRPC_Handler["tRPC HTTP Handler<br/>[trpc].ts"]:::backend
        TRPC_Config["tRPC Config<br/>trpc.ts"]:::backend
        TRPC_Root["Root Router<br/>root.ts"]:::backend
        TRPC_Routers["Main Routers<br/>main.ts"]:::backend
    end

    subgraph "ORM Layer"
        Drizzle_DB["Drizzle Client<br/>db.ts"]:::orm
        Drizzle_Schema["Schema<br/>schema.ts"]:::orm
        Drizzle_Config["Drizzle Config<br/>drizzle.config.ts"]:::orm
    end

    DB["PlanetScale MySQL"]:::db
    ExtAI["OpenAI API"]:::external
    Env["Env Config<br/>src/env.mjs & .env.example"]:::config
    NextCfg["Next.js Config<br/>next.config.mjs"]:::config

    %% Frontend to Backend
    FE_Pages -->|"HTTP (REST)"| API_Get
    FE_Pages -->|"HTTP (REST)"| API_Panel
    FE_Pages -->|"tRPC (via tRPC Client)"| TRPC_Handler
    FE_UtilsAPI -->|"tRPC"| TRPC_Handler

    %% Middleware to endpoints
    MW --> API_Get
    MW --> API_Panel
    MW --> TRPC_Handler

    %% tRPC flow
    TRPC_Handler --> TRPC_Config
    TRPC_Config --> TRPC_Root
    TRPC_Root --> TRPC_Routers

    %% Business logic to ORM & External AI
    TRPC_Routers --> Drizzle_DB
    TRPC_Routers --> ExtAI
    API_Get --> ExtAI
    API_Panel --> ExtAI

    %% ORM to DB
    Drizzle_Schema --> Drizzle_DB
    Drizzle_DB --> DB
    Drizzle_Config --> Drizzle_DB

    %% Config flows
    Env --> Drizzle_Config
    Env --> NextCfg
    Env --> ExtAI

    classDef frontend fill:#BBE1FA,stroke:#333,stroke-width:1px
    classDef backend fill:#C1E1C1,stroke:#333,stroke-width:1px
    classDef orm fill:#FFD8A8,stroke:#333,stroke-width:1px
    classDef db fill:#FFB703,stroke:#333,stroke-width:1px
    classDef external fill:#A0E7E5,stroke:#333,stroke-width:1px
    classDef config fill:#FEE440,stroke:#333,stroke-width:1px

    %% Click Events
    click FE_Pages "https://github.com/williamwelsh/chatgpt-clone/blob/main/src/pages/index.tsx"
    click FE_Sidebar "https://github.com/williamwelsh/chatgpt-clone/blob/main/src/components/Sidebar.tsx"
    click FE_UtilsAPI "https://github.com/williamwelsh/chatgpt-clone/blob/main/src/utils/api.ts"
    click API_Get "https://github.com/williamwelsh/chatgpt-clone/blob/main/src/pages/api/getAnswer.ts"
    click API_Panel "https://github.com/williamwelsh/chatgpt-clone/blob/main/src/pages/api/panel.ts"
    click TRPC_Handler "https://github.com/williamwelsh/chatgpt-clone/blob/main/src/pages/api/trpc/[trpc].ts"
    click TRPC_Config "https://github.com/williamwelsh/chatgpt-clone/blob/main/src/server/api/trpc.ts"
    click TRPC_Root "https://github.com/williamwelsh/chatgpt-clone/blob/main/src/server/api/root.ts"
    click TRPC_Routers "https://github.com/williamwelsh/chatgpt-clone/blob/main/src/server/api/routers/main.ts"
    click Drizzle_DB "https://github.com/williamwelsh/chatgpt-clone/blob/main/src/db/db.ts"
    click Drizzle_Schema "https://github.com/williamwelsh/chatgpt-clone/blob/main/src/db/schema.ts"
    click Drizzle_Config "https://github.com/williamwelsh/chatgpt-clone/blob/main/drizzle.config.ts"
    click Env "https://github.com/williamwelsh/chatgpt-clone/blob/main/src/env.mjs"
    click NextCfg "https://github.com/williamwelsh/chatgpt-clone/blob/main/next.config.mjs"
```
