# raj-28/tohorizonsai architecture

> Architecture diagram of the GitHub repository raj-28/tohorizonsai, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-01-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/raj-28/tohorizonsai
- Repository: https://github.com/raj-28/tohorizonsai

## Overview

##### Main Components

a) Client-Side Layer:
- Next.js Frontend (App Router based)
- UI Components (shadcn/ui based)
- Authentication Interface
- Chat Interface
- File Upload Interface

b) Server-Side Layer:
- Next.js Server Components
- Server Actions
- Authentication System (NextAuth.js)
- API Routes

c) External Services:
- AI Models Integration (AI SDK)
- Vercel Postgres Database
- Vercel Blob Storage
- Authentication Providers

**┌─ Client Layer ─────────────────────┐**
- React Components                 │
- UI Components                    │
- Client-side State Management     │
- ┬───────────────────┘
- ↕
**┌─ Server Layer ─────────────────────┐**
- ┌─ Next.js App Router ───────────┐ │
- Server Components            │ │
- API Routes                   │ │
- Server Actions              │ │
- ┘ │
- ┌─ Authentication ──────────────┐  │
- NextAuth.js                 │  │
- ┘  │
- ┬───────────────────┘
- ↕
**┌─ External Services ────────────────┐**
- AI Models (via AI SDK)          │
- Vercel Postgres                 │
- Vercel Blob Storage            │
- ┘

##### Detailed Component Breakdown

a) Chat Implementation:
- Flow from `/app/(chat)` through the AI SDK to the model providers
- Message stream handling (components/block-stream-handler.tsx)
- File upload flow through Vercel Blob

b) Authentication Flow:
- Flow from `/app/(auth)` through NextAuth.js
- Integration with the database for user management

c) Database Schema:
- A simplified version of the schema from `lib/db/schema.ts`
- Relationships between users, chats, and messages

##### Implementation Details

- API routes under `/app/(chat)/api/`
- Authentication routes under `/app/(auth)/api/`
- Database migrations and queries
- UI component hierarchy
- Server action flows

##### The real-time chat functionality

Remember to include version information and any relevant environment configurations that affect the architecture.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Client["Client Layer"]
        UI["UI Components"]
        Chat["Chat Interface"]:::chat
        Auth["Authentication Interface"]:::auth
        Upload["File Upload Interface"]:::upload
        Editor["Editor System"]:::editor
    end

    subgraph Server["Server Layer"]
        subgraph AppRouter["Next.js App Router"]
            ServerComp["Server Components"]
            APIRoutes["API Routes"]
            ServerActions["Server Actions"]
        end
        
        subgraph AuthSystem["Authentication System"]
            NextAuth["NextAuth.js"]:::auth
            AuthConfig["Auth Configuration"]:::auth
            AuthRoutes["Auth Routes"]:::auth
        end
        
        subgraph ChatSystem["Chat System"]
            ChatAPI["Chat API"]:::chat
            ChatActions["Chat Actions"]:::chat
            StreamHandler["Stream Handler"]:::realtime
        end
    end

    subgraph External["External Services"]
        subgraph AI["AI Integration"]
            AICore["Core AI Logic"]:::ai
            AIModels["AI Models"]:::ai
            AIPrompts["AI Prompts"]:::ai
        end
        
        Database[(Vercel Postgres)]:::db
        BlobStorage[(Vercel Blob Storage)]:::storage
    end

    %% Relationships
    UI --> ServerComp
    Chat --> ChatAPI
    Auth --> NextAuth
    Upload --> BlobStorage
    Editor --> ServerActions
    
    ChatAPI --> AICore
    AICore --> AIModels
    AICore --> AIPrompts
    
    NextAuth --> Database
    ChatSystem --> Database
    StreamHandler --> ChatAPI
    
    %% Click Events
    click Chat "https://github.com/raj-28/tohorizonsai/blob/main/components/chat.tsx"
    click ChatAPI "https://github.com/raj-28/tohorizonsai/blob/main/app/(chat)/api/chat/route.ts"
    click ChatActions "https://github.com/raj-28/tohorizonsai/blob/main/app/(chat)/actions.ts"
    click AuthConfig "https://github.com/raj-28/tohorizonsai/blob/main/app/(auth)/auth.config.ts"
    click AuthRoutes "https://github.com/raj-28/tohorizonsai/blob/main/app/(auth)/api/auth/[...nextauth]/route.ts"
    click AICore "https://github.com/raj-28/tohorizonsai/blob/main/lib/ai/index.ts"
    click AIModels "https://github.com/raj-28/tohorizonsai/blob/main/lib/ai/models.ts"
    click AIPrompts "https://github.com/raj-28/tohorizonsai/blob/main/lib/ai/prompts.ts"
    click Upload "https://github.com/raj-28/tohorizonsai/blob/main/app/(chat)/api/files/upload/route.ts"
    click Editor "https://github.com/raj-28/tohorizonsai/blob/main/components/editor.tsx"
    click StreamHandler "https://github.com/raj-28/tohorizonsai/blob/main/components/block-stream-handler.tsx"
    click Auth "https://github.com/raj-28/tohorizonsai/tree/main/app/(auth)"

    %% Styles
    classDef frontend fill:#2196F3,color:white
    classDef backend fill:#4CAF50,color:white
    classDef external fill:#9C27B0,color:white
    classDef auth fill:#FF9800,color:white
    classDef chat fill:#03A9F4,color:white
    classDef ai fill:#E91E63,color:white
    classDef db fill:#607D8B,color:white
    classDef storage fill:#795548,color:white
    classDef realtime fill:#009688,color:white
    classDef editor fill:#673AB7,color:white
    classDef upload fill:#FFC107,color:white

    %% Legend
    subgraph Legend
        Frontend["Frontend"]:::frontend
        Backend["Backend"]:::backend
        External_["External Services"]:::external
        Auth_["Authentication"]:::auth
        Chat_["Chat System"]:::chat
        AI_["AI Components"]:::ai
        DB["Database"]:::db
        Storage["Storage"]:::storage
        Realtime["Real-time"]:::realtime
    end
```
