# code-byme/ai-knowledge architecture

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

- Interactive diagram: https://gitdiagram.com/code-byme/ai-knowledge
- Repository: https://github.com/code-byme/ai-knowledge

## Overview

AI Knowledge Hub is a document-grounded chat application: users register or sign in, upload private files, chat with an assistant, and revisit saved conversations; they can also manage their profile and account data. The dashboard composes the chat-history and document-management interfaces. API handlers provide chat, session, document, and account operations, with shared authentication and PostgreSQL access. The README describes OpenRouter-backed AI chat. Several UI-to-API links and unsampled implementation details are not visible in the excerpts, so those connections are omitted.

Main entry points: [`lib/database.ts`](https://github.com/code-byme/ai-knowledge/blob/main/lib/database.ts) (Database access), [`lib/auth.ts`](https://github.com/code-byme/ai-knowledge/blob/main/lib/auth.ts) (Auth configuration), [`app/dashboard/page.tsx`](https://github.com/code-byme/ai-knowledge/blob/main/app/dashboard/page.tsx) (Dashboard), [`app/api/chat/route.ts`](https://github.com/code-byme/ai-knowledge/blob/main/app/api/chat/route.ts) (Grounded chat API)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_experience["User experience"]
  node_landing["Landing page<br/>[page.tsx]"]
  node_dashboard["Dashboard<br/>[page.tsx]"]
  node_navbar["Navigation<br/>[Navbar.tsx]"]
end

subgraph group_identity["Identity"]
  node_signup["Registration page<br/>[page.tsx]"]
  node_login["Sign-in page<br/>[page.tsx]"]
  node_authapi["Auth handlers<br/>[route.ts]"]
  node_registerapi["Registration API<br/>[route.ts]"]
  node_auth["Auth configuration<br/>[auth.ts]"]
  node_protection["Route protection"]
end

subgraph group_knowledge["Documents"]
  node_uploadui["File upload UI<br/>[FileUpload.tsx]"]
  node_filelist["Document list<br/>[FileList.tsx]"]
  node_uploadapi["Document upload API<br/>[route.ts]"]
  node_documentsapi["Document listing API<br/>[route.ts]"]
  node_downloadapi["Document download API<br/>[route.ts]"]
end

subgraph group_conversation["Conversations"]
  node_chatui["Chat with history"]
  node_chatapi["Grounded chat API<br/>[route.ts]"]
  node_sessionsapi["Session API<br/>[route.ts]"]
  node_sessionapi["Session detail API<br/>[route.ts]"]
  node_messagesapi["Message history API<br/>[route.ts]"]
end

subgraph group_platform["Account and storage"]
  node_profile["Profile page<br/>[page.tsx]"]
  node_profileapi["Profile API<br/>[route.ts]"]
  node_deleteapi["Account deletion API<br/>[route.ts]"]
  node_dataapi["User data export API<br/>[route.ts]"]
  node_dbaccess["Database access<br/>[database.ts]"]
  node_postgres[("PostgreSQL")]
end

node_visitor(("User"))
node_openrouter["OpenRouter"]

node_visitor -->|"opens"| node_landing
node_visitor -.->|"registers"| node_signup
node_visitor -.->|"signs in"| node_login
node_dashboard -->|"renders"| node_navbar
node_dashboard -->|"uses"| node_protection
node_dashboard -->|"renders"| node_chatui
node_dashboard -->|"renders"| node_uploadui
node_dashboard -->|"renders"| node_filelist
node_profile -->|"renders"| node_navbar
node_profile -->|"uses"| node_protection
node_registerapi -->|"queries"| node_dbaccess
node_auth -->|"queries"| node_dbaccess
node_chatapi -->|"authenticates"| node_auth
node_chatapi -->|"reads and writes"| node_dbaccess
node_chatapi -.->|"requests answers"| node_openrouter
node_sessionsapi -->|"authenticates"| node_auth
node_sessionsapi -->|"queries"| node_dbaccess
node_sessionapi -->|"authenticates"| node_auth
node_sessionapi -->|"queries"| node_dbaccess
node_messagesapi -->|"authenticates"| node_auth
node_messagesapi -->|"queries"| node_dbaccess
node_uploadapi -->|"authenticates"| node_auth
node_uploadapi -->|"queries"| node_dbaccess
node_documentsapi -->|"authenticates"| node_auth
node_documentsapi -->|"queries"| node_dbaccess
node_downloadapi -->|"authenticates"| node_auth
node_downloadapi -->|"queries"| node_dbaccess
node_profileapi -->|"authenticates"| node_auth
node_profileapi -->|"queries"| node_dbaccess
node_deleteapi -->|"authenticates"| node_auth
node_deleteapi -->|"queries"| node_dbaccess
node_dataapi -->|"authenticates"| node_auth
node_dataapi -->|"queries"| node_dbaccess
node_dbaccess -->|"connects to"| node_postgres

click node_landing "https://github.com/code-byme/ai-knowledge/blob/main/app/page.tsx"
click node_signup "https://github.com/code-byme/ai-knowledge/blob/main/app/signup/page.tsx"
click node_login "https://github.com/code-byme/ai-knowledge/blob/main/app/login/page.tsx"
click node_authapi "https://github.com/code-byme/ai-knowledge/blob/main/app/api/auth/%5B...nextauth%5D/route.ts"
click node_registerapi "https://github.com/code-byme/ai-knowledge/blob/main/app/api/auth/register/route.ts"
click node_auth "https://github.com/code-byme/ai-knowledge/blob/main/lib/auth.ts"
click node_dashboard "https://github.com/code-byme/ai-knowledge/blob/main/app/dashboard/page.tsx"
click node_navbar "https://github.com/code-byme/ai-knowledge/blob/main/components/Navbar.tsx"
click node_protection "https://github.com/code-byme/ai-knowledge/blob/main/lib/auth-protection.tsx"
click node_uploadui "https://github.com/code-byme/ai-knowledge/blob/main/components/FileUpload.tsx"
click node_filelist "https://github.com/code-byme/ai-knowledge/blob/main/components/FileList.tsx"
click node_uploadapi "https://github.com/code-byme/ai-knowledge/blob/main/app/api/documents/upload/route.ts"
click node_documentsapi "https://github.com/code-byme/ai-knowledge/blob/main/app/api/documents/route.ts"
click node_downloadapi "https://github.com/code-byme/ai-knowledge/blob/main/app/api/documents/download/%5Bid%5D/route.ts"
click node_chatui "https://github.com/code-byme/ai-knowledge/blob/main/components/ChatBoxWithHistory.tsx"
click node_chatapi "https://github.com/code-byme/ai-knowledge/blob/main/app/api/chat/route.ts"
click node_sessionsapi "https://github.com/code-byme/ai-knowledge/blob/main/app/api/chat/sessions/route.ts"
click node_sessionapi "https://github.com/code-byme/ai-knowledge/blob/main/app/api/chat/sessions/%5Bid%5D/route.ts"
click node_messagesapi "https://github.com/code-byme/ai-knowledge/blob/main/app/api/chat/sessions/%5Bid%5D/messages/route.ts"
click node_profile "https://github.com/code-byme/ai-knowledge/blob/main/app/profile/page.tsx"
click node_profileapi "https://github.com/code-byme/ai-knowledge/blob/main/app/api/user/profile/route.ts"
click node_deleteapi "https://github.com/code-byme/ai-knowledge/blob/main/app/api/user/delete-account/route.ts"
click node_dataapi "https://github.com/code-byme/ai-knowledge/blob/main/app/api/user/download-data/route.ts"
click node_dbaccess "https://github.com/code-byme/ai-knowledge/blob/main/lib/database.ts"

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_landing,node_dashboard,node_navbar,node_visitor toneBlue
class node_signup,node_login,node_authapi,node_registerapi,node_auth,node_protection toneAmber
class node_uploadui,node_filelist,node_uploadapi,node_documentsapi,node_downloadapi,node_openrouter toneMint
class node_chatui,node_chatapi,node_sessionsapi,node_sessionapi,node_messagesapi toneRose
class node_profile,node_profileapi,node_deleteapi,node_dataapi,node_dbaccess,node_postgres toneIndigo
```

## Components

### User experience

- [Landing page](https://github.com/code-byme/ai-knowledge/blob/main/app/page.tsx): `app/page.tsx`
- [Dashboard](https://github.com/code-byme/ai-knowledge/blob/main/app/dashboard/page.tsx): `app/dashboard/page.tsx`
- [Navigation](https://github.com/code-byme/ai-knowledge/blob/main/components/Navbar.tsx): `components/Navbar.tsx`

### Identity

- [Registration page](https://github.com/code-byme/ai-knowledge/blob/main/app/signup/page.tsx): `app/signup/page.tsx`
- [Sign-in page](https://github.com/code-byme/ai-knowledge/blob/main/app/login/page.tsx): `app/login/page.tsx`
- [Auth handlers](https://github.com/code-byme/ai-knowledge/blob/main/app/api/auth/%5B...nextauth%5D/route.ts): `app/api/auth/[...nextauth]/route.ts`
- [Registration API](https://github.com/code-byme/ai-knowledge/blob/main/app/api/auth/register/route.ts): `app/api/auth/register/route.ts`
- [Auth configuration](https://github.com/code-byme/ai-knowledge/blob/main/lib/auth.ts): `lib/auth.ts`
- [Route protection](https://github.com/code-byme/ai-knowledge/blob/main/lib/auth-protection.tsx): `lib/auth-protection.tsx`

### Documents

- [File upload UI](https://github.com/code-byme/ai-knowledge/blob/main/components/FileUpload.tsx): `components/FileUpload.tsx`
- [Document list](https://github.com/code-byme/ai-knowledge/blob/main/components/FileList.tsx): `components/FileList.tsx`
- [Document upload API](https://github.com/code-byme/ai-knowledge/blob/main/app/api/documents/upload/route.ts): `app/api/documents/upload/route.ts`
- [Document listing API](https://github.com/code-byme/ai-knowledge/blob/main/app/api/documents/route.ts): `app/api/documents/route.ts`
- [Document download API](https://github.com/code-byme/ai-knowledge/blob/main/app/api/documents/download/%5Bid%5D/route.ts): `app/api/documents/download/[id]/route.ts`

### Conversations

- [Chat with history](https://github.com/code-byme/ai-knowledge/blob/main/components/ChatBoxWithHistory.tsx): `components/ChatBoxWithHistory.tsx`
- [Grounded chat API](https://github.com/code-byme/ai-knowledge/blob/main/app/api/chat/route.ts): `app/api/chat/route.ts`
- [Session API](https://github.com/code-byme/ai-knowledge/blob/main/app/api/chat/sessions/route.ts): `app/api/chat/sessions/route.ts`
- [Session detail API](https://github.com/code-byme/ai-knowledge/blob/main/app/api/chat/sessions/%5Bid%5D/route.ts): `app/api/chat/sessions/[id]/route.ts`
- [Message history API](https://github.com/code-byme/ai-knowledge/blob/main/app/api/chat/sessions/%5Bid%5D/messages/route.ts): `app/api/chat/sessions/[id]/messages/route.ts`

### Account and storage

- [Profile page](https://github.com/code-byme/ai-knowledge/blob/main/app/profile/page.tsx): `app/profile/page.tsx`
- [Profile API](https://github.com/code-byme/ai-knowledge/blob/main/app/api/user/profile/route.ts): `app/api/user/profile/route.ts`
- [Account deletion API](https://github.com/code-byme/ai-knowledge/blob/main/app/api/user/delete-account/route.ts): `app/api/user/delete-account/route.ts`
- [User data export API](https://github.com/code-byme/ai-knowledge/blob/main/app/api/user/download-data/route.ts): `app/api/user/download-data/route.ts`
- [Database access](https://github.com/code-byme/ai-knowledge/blob/main/lib/database.ts): `lib/database.ts`
- **PostgreSQL**

### Other components

- **User**
- **OpenRouter**

## Connections

- User → Landing page: opens (evidence: `app/page.tsx`)
- User → Registration page: registers (evidence: `app/signup/page.tsx`)
- User → Sign-in page: signs in (evidence: `app/login/page.tsx`)
- Dashboard → Navigation: renders (evidence: `app/dashboard/page.tsx`)
- Dashboard → Route protection: uses (evidence: `app/dashboard/page.tsx`)
- Dashboard → Chat with history: renders (evidence: `app/dashboard/page.tsx`)
- Dashboard → File upload UI: renders (evidence: `app/dashboard/page.tsx`)
- Dashboard → Document list: renders (evidence: `app/dashboard/page.tsx`)
- Profile page → Navigation: renders (evidence: `app/profile/page.tsx`)
- Profile page → Route protection: uses (evidence: `app/profile/page.tsx`)
- Registration API → Database access: queries (evidence: `app/api/auth/register/route.ts`)
- Auth configuration → Database access: queries (evidence: `lib/auth.ts`)
- Grounded chat API → Auth configuration: authenticates (evidence: `app/api/chat/route.ts`)
- Grounded chat API → Database access: reads and writes (evidence: `app/api/chat/route.ts`)
- Grounded chat API → OpenRouter: requests answers
- Session API → Auth configuration: authenticates (evidence: `app/api/chat/sessions/route.ts`)
- Session API → Database access: queries (evidence: `app/api/chat/sessions/route.ts`)
- Session detail API → Auth configuration: authenticates (evidence: `app/api/chat/sessions/[id]/route.ts`)
- Session detail API → Database access: queries (evidence: `app/api/chat/sessions/[id]/route.ts`)
- Message history API → Auth configuration: authenticates (evidence: `app/api/chat/sessions/[id]/messages/route.ts`)
- Message history API → Database access: queries (evidence: `app/api/chat/sessions/[id]/messages/route.ts`)
- Document upload API → Auth configuration: authenticates (evidence: `app/api/documents/upload/route.ts`)
- Document upload API → Database access: queries (evidence: `app/api/documents/upload/route.ts`)
- Document listing API → Auth configuration: authenticates (evidence: `app/api/documents/route.ts`)
- Document listing API → Database access: queries (evidence: `app/api/documents/route.ts`)
- Document download API → Auth configuration: authenticates (evidence: `app/api/documents/download/[id]/route.ts`)
- Document download API → Database access: queries (evidence: `app/api/documents/download/[id]/route.ts`)
- Profile API → Auth configuration: authenticates (evidence: `app/api/user/profile/route.ts`)
- Profile API → Database access: queries (evidence: `app/api/user/profile/route.ts`)
- Account deletion API → Auth configuration: authenticates (evidence: `app/api/user/delete-account/route.ts`)
- Account deletion API → Database access: queries (evidence: `app/api/user/delete-account/route.ts`)
- User data export API → Auth configuration: authenticates (evidence: `app/api/user/download-data/route.ts`)
- User data export API → Database access: queries (evidence: `app/api/user/download-data/route.ts`)
- Database access → PostgreSQL: connects to (evidence: `lib/database.ts`)
