# barmanji/t3-chat-clone architecture

> Architecture diagram of the GitHub repository barmanji/t3-chat-clone, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-05). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/barmanji/t3-chat-clone
- Repository: https://github.com/barmanji/t3-chat-clone

## Overview

T3 Chat Clone is a multi-model AI chat application: users sign in, choose a free OpenRouter model, send prompts, receive streamed answers, and revisit persisted chat history. The graph separates authentication, chat UI and model discovery, request orchestration, and persistence. README documents OAuth, chat CRUD/search, markdown rendering and date-grouped history; some links for unsampled UI/routes are omitted rather than inferred. The Prisma schema and server layout were not sampled, so persistence and page wiring are limited to documented or indexed relationships.

Main entry points: [`modules/chat/components/messages/message-view-form.tsx`](https://github.com/barmanji/t3-chat-clone/blob/main/modules/chat/components/messages/message-view-form.tsx) (Chat view), [`modules/chat/hooks/use-chats.ts`](https://github.com/barmanji/t3-chat-clone/blob/main/modules/chat/hooks/use-chats.ts) (Chat queries), [`app/api/chat/route.ts`](https://github.com/barmanji/t3-chat-clone/blob/main/app/api/chat/route.ts) (Streaming endpoint), [`lib/db.ts`](https://github.com/barmanji/t3-chat-clone/blob/main/lib/db.ts) (Prisma client)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_auth["Authentication"]
  node_signin["Sign-in page<br/>[page.tsx]"]
  node_authclient["Auth client<br/>[auth-client.ts]"]
  node_authapi["Auth handler<br/>[route.ts]"]
  node_authconfig["Auth configuration<br/>[auth.ts]"]
  node_authactions["Session actions<br/>[index.ts]"]
  node_userbutton["User menu<br/>[user-button.tsx]"]
end

subgraph group_chatui["Chat experience"]
  node_chatpage["Chat pages<br/>[page.tsx]"]
  node_chatlayout["Chat layout<br/>[layout-client.tsx]"]
  node_sidebar["Chat sidebar<br/>[chat-sidebar.tsx]"]
  node_chatview["Chat view"]
  node_messageui["Message renderer<br/>[message.tsx]"]
  node_reasoningui["Reasoning display<br/>[reasoning.tsx]"]
  node_promptui["Prompt input<br/>[prompt-input.tsx]"]
  node_modelsui["Model selector<br/>[model-selector.tsx]"]
  node_modelhook["Model query<br/>[use-ai-models.ts]"]
  node_chathook["Chat queries<br/>[use-chats.ts]"]
  node_chatdetail["Chat detail page<br/>[page.tsx]"]
end

subgraph group_runtime["Chat runtime"]
  node_modelapi["Free model API<br/>[route.ts]"]
  node_chatapi["Streaming endpoint<br/>[route.ts]"]
  node_prompt["System prompt<br/>[prompt.ts]"]
end

subgraph group_data["Persistence"]
  node_chatactions["Chat operations<br/>[index.ts]"]
  node_dbclient["Prisma client<br/>[db.ts]"]
  node_postgres[("Postgres")]
  node_history["Chat history<br/>[chat-sidebar.tsx]"]
end

node_visitor(("User"))
node_oauth["GitHub / Google"]
node_openrouter["OpenRouter"]

node_visitor -->|"opens"| node_signin
node_signin -->|"signs in via"| node_authclient
node_visitor -->|"authenticates with"| node_oauth
node_authapi -->|"dispatches to"| node_authconfig
node_authconfig -->|"uses adapter"| node_dbclient
node_authconfig -->|"configures providers"| node_oauth
node_chatpage -->|"checks session"| node_authactions
node_chatlayout -->|"renders"| node_sidebar
node_sidebar -->|"loads chats"| node_chathook
node_sidebar -->|"renders"| node_userbutton
node_userbutton -->|"signs out via"| node_authclient
node_chatpage -->|"renders"| node_chatview
node_chatdetail -->|"renders"| node_chatview
node_chatview -->|"renders input"| node_promptui
node_chatview -->|"renders messages"| node_messageui
node_chatview -->|"renders reasoning"| node_reasoningui
node_chatview -->|"renders selector"| node_modelsui
node_chatview -->|"loads models"| node_modelhook
node_modelhook -.->|"fetches models"| node_modelapi
node_visitor -.->|"sends prompt"| node_chatview
node_chatview -->|"posts stream request"| node_chatapi
node_chatapi -->|"streams generation"| node_openrouter
node_chatapi -->|"sets system prompt"| node_prompt
node_chatapi -->|"saves messages"| node_dbclient
node_dbclient -.->|"persists records"| node_postgres
node_chathook -->|"invokes operations"| node_chatactions
node_chatactions -->|"reads and writes"| node_dbclient
node_chathook -->|"navigates after create"| node_chatdetail
node_chathook -->|"provides chat list"| node_history

click node_signin "https://github.com/barmanji/t3-chat-clone/blob/main/app/(auth)/sign-in/page.tsx"
click node_authclient "https://github.com/barmanji/t3-chat-clone/blob/main/lib/auth-client.ts"
click node_authapi "https://github.com/barmanji/t3-chat-clone/blob/main/app/api/auth/%5B...all%5D/route.ts"
click node_authconfig "https://github.com/barmanji/t3-chat-clone/blob/main/lib/auth.ts"
click node_authactions "https://github.com/barmanji/t3-chat-clone/blob/main/modules/authentication/actions/index.ts"
click node_chatpage "https://github.com/barmanji/t3-chat-clone/blob/main/app/(root)/page.tsx"
click node_chatlayout "https://github.com/barmanji/t3-chat-clone/blob/main/app/(root)/layout-client.tsx"
click node_sidebar "https://github.com/barmanji/t3-chat-clone/blob/main/modules/chat/components/chat-sidebar.tsx"
click node_chatview "https://github.com/barmanji/t3-chat-clone/blob/main/modules/chat/components/messages/message-view-form.tsx"
click node_messageui "https://github.com/barmanji/t3-chat-clone/blob/main/components/ai-elements/message.tsx"
click node_reasoningui "https://github.com/barmanji/t3-chat-clone/blob/main/components/ai-elements/reasoning.tsx"
click node_promptui "https://github.com/barmanji/t3-chat-clone/blob/main/components/ai-elements/prompt-input.tsx"
click node_modelsui "https://github.com/barmanji/t3-chat-clone/blob/main/modules/chat/components/chat-view/model-selector.tsx"
click node_modelhook "https://github.com/barmanji/t3-chat-clone/blob/main/modules/chat/hooks/use-ai-models.ts"
click node_modelapi "https://github.com/barmanji/t3-chat-clone/blob/main/app/api/ai/get-models/route.ts"
click node_chathook "https://github.com/barmanji/t3-chat-clone/blob/main/modules/chat/hooks/use-chats.ts"
click node_chatactions "https://github.com/barmanji/t3-chat-clone/blob/main/modules/chat/actions/index.ts"
click node_chatapi "https://github.com/barmanji/t3-chat-clone/blob/main/app/api/chat/route.ts"
click node_prompt "https://github.com/barmanji/t3-chat-clone/blob/main/lib/prompt.ts"
click node_dbclient "https://github.com/barmanji/t3-chat-clone/blob/main/lib/db.ts"
click node_chatdetail "https://github.com/barmanji/t3-chat-clone/blob/main/app/(root)/chat/%5BchatId%5D/page.tsx"
click node_history "https://github.com/barmanji/t3-chat-clone/blob/main/modules/chat/components/chat-sidebar.tsx"
click node_userbutton "https://github.com/barmanji/t3-chat-clone/blob/main/modules/authentication/components/user-button.tsx"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_signin,node_authclient,node_authapi,node_authconfig,node_authactions,node_userbutton,node_visitor toneBlue
class node_chatpage,node_chatlayout,node_sidebar,node_chatview,node_messageui,node_reasoningui,node_promptui,node_modelsui,node_modelhook,node_chathook,node_chatdetail toneAmber
class node_modelapi,node_chatapi,node_prompt,node_openrouter toneMint
class node_chatactions,node_dbclient,node_postgres,node_history toneRose
class node_oauth toneIndigo
```

## Components

### Authentication

- [Sign-in page](https://github.com/barmanji/t3-chat-clone/blob/main/app/(auth)/sign-in/page.tsx): `app/(auth)/sign-in/page.tsx`
- [Auth client](https://github.com/barmanji/t3-chat-clone/blob/main/lib/auth-client.ts): `lib/auth-client.ts`
- [Auth handler](https://github.com/barmanji/t3-chat-clone/blob/main/app/api/auth/%5B...all%5D/route.ts): `app/api/auth/[...all]/route.ts`
- [Auth configuration](https://github.com/barmanji/t3-chat-clone/blob/main/lib/auth.ts): `lib/auth.ts`
- [Session actions](https://github.com/barmanji/t3-chat-clone/blob/main/modules/authentication/actions/index.ts): `modules/authentication/actions/index.ts`
- [User menu](https://github.com/barmanji/t3-chat-clone/blob/main/modules/authentication/components/user-button.tsx): `modules/authentication/components/user-button.tsx`

### Chat experience

- [Chat pages](https://github.com/barmanji/t3-chat-clone/blob/main/app/(root)/page.tsx): `app/(root)/page.tsx`
- [Chat layout](https://github.com/barmanji/t3-chat-clone/blob/main/app/(root)/layout-client.tsx): `app/(root)/layout-client.tsx`
- [Chat sidebar](https://github.com/barmanji/t3-chat-clone/blob/main/modules/chat/components/chat-sidebar.tsx): `modules/chat/components/chat-sidebar.tsx`
- [Chat view](https://github.com/barmanji/t3-chat-clone/blob/main/modules/chat/components/messages/message-view-form.tsx): `modules/chat/components/messages/message-view-form.tsx`
- [Message renderer](https://github.com/barmanji/t3-chat-clone/blob/main/components/ai-elements/message.tsx): `components/ai-elements/message.tsx`
- [Reasoning display](https://github.com/barmanji/t3-chat-clone/blob/main/components/ai-elements/reasoning.tsx): `components/ai-elements/reasoning.tsx`
- [Prompt input](https://github.com/barmanji/t3-chat-clone/blob/main/components/ai-elements/prompt-input.tsx): `components/ai-elements/prompt-input.tsx`
- [Model selector](https://github.com/barmanji/t3-chat-clone/blob/main/modules/chat/components/chat-view/model-selector.tsx): `modules/chat/components/chat-view/model-selector.tsx`
- [Model query](https://github.com/barmanji/t3-chat-clone/blob/main/modules/chat/hooks/use-ai-models.ts): `modules/chat/hooks/use-ai-models.ts`
- [Chat queries](https://github.com/barmanji/t3-chat-clone/blob/main/modules/chat/hooks/use-chats.ts): `modules/chat/hooks/use-chats.ts`
- [Chat detail page](https://github.com/barmanji/t3-chat-clone/blob/main/app/(root)/chat/%5BchatId%5D/page.tsx): `app/(root)/chat/[chatId]/page.tsx`

### Chat runtime

- [Free model API](https://github.com/barmanji/t3-chat-clone/blob/main/app/api/ai/get-models/route.ts): `app/api/ai/get-models/route.ts`
- [Streaming endpoint](https://github.com/barmanji/t3-chat-clone/blob/main/app/api/chat/route.ts): `app/api/chat/route.ts`
- [System prompt](https://github.com/barmanji/t3-chat-clone/blob/main/lib/prompt.ts): `lib/prompt.ts`

### Persistence

- [Chat operations](https://github.com/barmanji/t3-chat-clone/blob/main/modules/chat/actions/index.ts): `modules/chat/actions/index.ts`
- [Prisma client](https://github.com/barmanji/t3-chat-clone/blob/main/lib/db.ts): `lib/db.ts`
- **Postgres**
- [Chat history](https://github.com/barmanji/t3-chat-clone/blob/main/modules/chat/components/chat-sidebar.tsx): `modules/chat/components/chat-sidebar.tsx`

### Other components

- **User**
- **GitHub / Google**
- **OpenRouter**

## Connections

- User → Sign-in page: opens
- Sign-in page → Auth client: signs in via (evidence: `app/(auth)/sign-in/page.tsx`)
- User → GitHub / Google: authenticates with
- Auth handler → Auth configuration: dispatches to (evidence: `app/api/auth/[...all]/route.ts`)
- Auth configuration → Prisma client: uses adapter (evidence: `lib/auth.ts`)
- Auth configuration → GitHub / Google: configures providers (evidence: `lib/auth.ts`)
- Chat pages → Session actions: checks session (evidence: `app/(root)/page.tsx`)
- Chat layout → Chat sidebar: renders (evidence: `app/(root)/layout-client.tsx`)
- Chat sidebar → Chat queries: loads chats (evidence: `modules/chat/components/chat-sidebar.tsx`)
- Chat sidebar → User menu: renders (evidence: `modules/chat/components/chat-sidebar.tsx`)
- User menu → Auth client: signs out via (evidence: `modules/authentication/components/user-button.tsx`)
- Chat pages → Chat view: renders (evidence: `app/(root)/page.tsx`)
- Chat detail page → Chat view: renders (evidence: `app/(root)/chat/[chatId]/page.tsx`)
- Chat view → Prompt input: renders input (evidence: `modules/chat/components/messages/message-view-form.tsx`)
- Chat view → Message renderer: renders messages (evidence: `modules/chat/components/messages/message-view-form.tsx`)
- Chat view → Reasoning display: renders reasoning (evidence: `modules/chat/components/messages/message-view-form.tsx`)
- Chat view → Model selector: renders selector (evidence: `modules/chat/components/messages/message-view-form.tsx`)
- Chat view → Model query: loads models (evidence: `modules/chat/components/messages/message-view-form.tsx`)
- Model query → Free model API: fetches models
- User → Chat view: sends prompt
- Chat view → Streaming endpoint: posts stream request (evidence: `modules/chat/components/messages/message-view-form.tsx`)
- Streaming endpoint → OpenRouter: streams generation (evidence: `app/api/chat/route.ts`)
- Streaming endpoint → System prompt: sets system prompt (evidence: `app/api/chat/route.ts`)
- Streaming endpoint → Prisma client: saves messages (evidence: `app/api/chat/route.ts`)
- Prisma client → Postgres: persists records
- Chat queries → Chat operations: invokes operations (evidence: `modules/chat/hooks/use-chats.ts`)
- Chat operations → Prisma client: reads and writes (evidence: `modules/chat/actions/index.ts`)
- Chat queries → Chat detail page: navigates after create (evidence: `modules/chat/hooks/use-chats.ts`)
- Chat queries → Chat history: provides chat list (evidence: `modules/chat/components/chat-sidebar.tsx`)
