# plastic-labs/tutor-gpt architecture

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

- Interactive diagram: https://gitdiagram.com/plastic-labs/tutor-gpt
- Repository: https://github.com/plastic-labs/tutor-gpt

## Overview

##### Project type and purpose
- This is a full-stack Next.js application (“app” directory uses the Next.js App Router).
- It provides a web UI (React components in `components/`) for an LLM-powered learning companion called Tutor-GPT.
- It uses Supabase for database/auth, Honcho for user modeling, Stripe for billing, PostHog for analytics, and an OpenAI-compatible inference endpoint for LLM calls.

##### File structure
- `app/`
- Next.js pages, layouts and server-handler API routes (chat, webhook, auth, settings).
- `components/`
- React components for UI: chat window, auth flows, settings, design system (`components/ui`).
- `hooks/`
- Custom React hooks for front-end behaviors (autoscroll, reactions).
- `utils/`
- Shared modules: AI client wrappers, Supabase clients, Stripe actions, PDF parsing, prompts, caching.
- `supabase/`
- SQL migrations, seed data, and email templates for Supabase Auth.
- `scripts/stripeSync.mjs`
- Lifecycle scripts for syncing subscription state.
- Config files
- Dockerfile/docker-compose.yaml for containerization; environment templates; Sentry/PostHog configs.

##### README for additional insights
- Confirms third-party integrations: Honcho (personalization), Supabase (auth + Postgres), Openrouter/open-ai API, PostHog, Stripe, Mistral OCR.
- Describes Supabase SSR auth flow, local dev with Docker, and environment variables needed.

##### Essential architectural elements
- Don’t break the Next.js app into overly fine details: treat `app/` and `utils/` as separate layers.
- Both real-time interactions (chat messages → AI) and event-driven flows (Stripe webhooks, script sync).

## Diagram (Mermaid)

```mermaid
flowchart TB
  %% Presentation Layer
  subgraph "Presentation Layer" 
    direction TB
    UI_Page["app/page.tsx"]:::frontend
    UI_Layout["app/layout.tsx"]:::frontend
    UI_MessageList["components/MessageList.tsx"]:::frontend
    UI_Autoscroll["hooks/autoscroll.ts"]:::frontend
  end

  %% API Layer
  subgraph "API Layer"
    direction TB
    ChatAPI["Chat API<br/>(app/api/chat/route.ts)"]:::backend
    ChatNameAPI["Chat Name API<br/>(app/api/chat/name/route.ts)"]:::backend
    WebhookAPI["Webhook API<br/>(app/api/webhook/route.ts)"]:::backend
    subgraph Auth["Auth Endpoints"]
      direction TB
      AuthActions["actions.ts"]:::backend
      AuthCallback["callback/route.ts"]:::backend
      AuthConfirm["confirm/route.ts"]:::backend
      AuthPage["page.tsx"]:::frontend
      AuthReset["reset/page.tsx"]:::frontend
    end
    subgraph Settings["Settings Feature"]
      direction TB
      SettingsPage["app/settings/page.tsx"]:::frontend
      SettingsLayout["SettingsLayout.tsx"]:::frontend
    end
  end

  %% Business Logic
  subgraph "Business Logic"
    direction TB
    AIUtils["utils/ai/"]:::backend
    SupabaseUtils["utils/supabase/"]:::backend
    StripeUtils["utils/stripe/actions.ts"]:::backend
    StripeScript["scripts/stripeSync.mjs"]:::backend
    HonchoUtils["utils/honcho.ts"]:::backend
  end

  %% Data Layer
  DataLayer((Supabase<br/>Postgres & Auth)):::data

  %% External Services
  subgraph "External Services"
    direction TB
    AIService["AI Provider"]:::external
    HonchoService["Honcho"]:::external
    StripeService["Stripe"]:::external
    PostHogService["PostHog"]:::external
    SentryService["Sentry"]:::external
  end

  %% Docker / Dev
  subgraph "Docker / Local Dev"
    direction TB
    DockerFile["Dockerfile"]:::external
    DockerCompose["docker-compose.yaml"]:::external
    EnvTemplate[".env.template"]:::external
    SupabaseConfig["supabase/config.toml"]:::external
  end

  %% Relationships
  UI_Page -->|"HTTP request"| ChatAPI
  UI_Page -->|"HTTP request"| AuthPage
  UI_Page -->|"HTTP request"| SettingsPage
  UI_Page -->|"CSR/SSR"| UI_Layout

  UI_MessageList --> UI_Page
  UI_Autoscroll --> UI_MessageList

  ChatAPI -->|calls| AIUtils
  AIUtils -->|LLM prompt/response| AIService
  ChatAPI -->|reads/writes| SupabaseUtils
  ChatNameAPI -->|metadata update| SupabaseUtils
  WebhookAPI -->|webhook events| StripeUtils
  StripeScript -->|sync| StripeService
  StripeUtils -->|update| SupabaseUtils

  AuthActions -->|PKCE flow| DataLayer
  AuthCallback -->|SSO callback| DataLayer
  AuthConfirm -->|confirm| DataLayer

  SettingsPage -->|uses| SupabaseUtils
  SettingsLayout --> SettingsPage

  SupabaseUtils -->|queries| DataLayer
  SupabaseUtils -->|migrations/templates| SupabaseConfig

  HonchoUtils -->|user modeling calls| HonchoService
  AIUtils -->|personalization calls| HonchoService

  UI_Page -->|analytics| PostHogService
  ChatAPI -->|server events| PostHogService

  UI_Page -->|error reporting| SentryService
  ChatAPI -->|error reporting| SentryService

  %% Click Events
  click UI_Page "https://github.com/plastic-labs/tutor-gpt/blob/main/app/page.tsx"
  click UI_Layout "https://github.com/plastic-labs/tutor-gpt/blob/main/app/layout.tsx"
  click UI_MessageList "https://github.com/plastic-labs/tutor-gpt/blob/main/components/MessageList.tsx"
  click UI_Autoscroll "https://github.com/plastic-labs/tutor-gpt/blob/main/hooks/autoscroll.ts"
  click ChatAPI "https://github.com/plastic-labs/tutor-gpt/blob/main/app/api/chat/route.ts"
  click ChatNameAPI "https://github.com/plastic-labs/tutor-gpt/blob/main/app/api/chat/name/route.ts"
  click WebhookAPI "https://github.com/plastic-labs/tutor-gpt/blob/main/app/api/webhook/route.ts"
  click AuthActions "https://github.com/plastic-labs/tutor-gpt/blob/main/app/auth/actions.ts"
  click AuthCallback "https://github.com/plastic-labs/tutor-gpt/blob/main/app/auth/callback/route.ts"
  click AuthConfirm "https://github.com/plastic-labs/tutor-gpt/blob/main/app/auth/confirm/route.ts"
  click AuthPage "https://github.com/plastic-labs/tutor-gpt/blob/main/app/auth/page.tsx"
  click AuthReset "https://github.com/plastic-labs/tutor-gpt/blob/main/app/auth/reset/page.tsx"
  click SettingsPage "https://github.com/plastic-labs/tutor-gpt/blob/main/app/settings/page.tsx"
  click SettingsLayout "https://github.com/plastic-labs/tutor-gpt/blob/main/app/settings/SettingsLayout.tsx"
  click AIUtils "https://github.com/plastic-labs/tutor-gpt/tree/main/utils/ai/"
  click SupabaseUtils "https://github.com/plastic-labs/tutor-gpt/blob/main/utils/supabase/client.ts"
  click StripeUtils "https://github.com/plastic-labs/tutor-gpt/blob/main/utils/stripe/actions.ts"
  click StripeScript "https://github.com/plastic-labs/tutor-gpt/blob/main/scripts/stripeSync.mjs"
  click HonchoUtils "https://github.com/plastic-labs/tutor-gpt/blob/main/utils/honcho.ts"

  %% Styles
  classDef frontend fill:#E0F7FA,stroke:#0288D1,color:#000
  classDef backend fill:#E8F5E9,stroke:#388E3C,color:#000
  classDef data fill:#FFF3E0,stroke:#F57C00,color:#000
  classDef external fill:#ECEFF1,stroke:#607D8B,color:#000
```
