How snarktank/ai-chatbot works
Project type and purpose
• This is a full-stack web application template (“Chat SDK”) for building AI chatbot UIs.
• It provides authentication, chat interfaces, file uploads, history storage, and AI-driven artifacts (text, code, images, sheets).
File structure
• app/ directory uses Next.js App Router with two main segments:
• (auth) – auth pages, Auth.js config, /api/auth routes.
• (chat) – chat UI pages, server actions, and API routes under /api/chat, /api/history, /api/files, /api/suggestions, etc.
• lib/ contains core business logic:
• lib/ai/ – wrappers around the AI SDK, prompts, tools.
• lib/db/ – Drizzle schema, migrations, queries.
• lib/artifacts/ – server functions for persisting and retrieving chat “artifacts.”
• components/ – React UI components (sidebar, editors, message lists, etc.).
• Config and infra files: drizzle.config.ts, next.config.ts, playwright.config.ts, .github/workflows.
README for additional insights
• Confirms tech stack: Next.js App Router, React Server Components & Server Actions, AI SDK, shadcn/ui + Tailwind, Neon Postgres, Vercel Blob, Auth.js.
• Mentions model providers can be swapped (xAI, OpenAI, Anthropic, etc.).
Main components
a. Frontend (Browser)
• Next.js React Components (Client & Server Components)
• UI Library (shadcn/ui + Tailwind)
• Pages under app/(auth) and app/(chat)
b. Next.js Backend (Server)
• Server Actions & API Routes under /api/* (chat, history, files, suggestions, vote)
• Auth.js handlers for /api/auth/[...nextauth]
• Drizzle ORM layer (lib/db) connecting to Postgres
• AI service layer (lib/ai) calling out to LLM providers via AI SDK
• Artifact management layer (lib/artifacts) for saving and retrieving chat outputs
c. Database
• Neon Serverless Postgres (using Drizzle schema & migrations)
d. Blob Storage
• Vercel Blob for file uploads (images, documents)
e. External Services
• AI Providers (xAI, OpenAI, etc.) via AI SDK
• Authentication provider (OAuth, Email via Auth.js)
f. CI/CD & Testing
• GitHub Actions (lint, Playwright e2e)
Relationships & data flows
• Browser → Next.js pages & Server Actions (UI interactions like submitting a message)
• Next.js Server Actions → AI SDK → External LLM → response back to Server Action
• Server Actions → Drizzle ORM → Postgres (save chat history, user data)
• /api/files/upload → Vercel Blob (store and retrieve attachments)
• Frontend polls or streams chat history via /api/history
• Auth flows: Browser → /api/auth → Auth.js → session cookie → protected pages
Important architectural patterns
• React Server Components & Server Actions for minimal bundle size & reduced client JS.
• API-centric micro-routes under app/(chat)/api for each domain (chat, files, history, suggestions).
• Modular “artifact” pattern: text, code, image, sheet components each have client/server handlers (artifacts/*).
• Drizzle ORM with migration metadata ensures safe schema evolution.
• AI SDK abstraction isolates provider-specific code under lib/ai/providers.ts.
Technologies & frameworks
• Next.js 14 App Router
• React, React Server Components, Server Actions
• Vercel AI SDK
• Auth.js
• Drizzle ORM & Neon Postgres
• Vercel Blob
• shadcn/ui + Tailwind CSS
• Playwright & GitHub Actions for testing