How williamwelsh/chatgpt-clone works
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