# kethmark/optimize architecture

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

- Interactive diagram: https://gitdiagram.com/kethmark/optimize
- Repository: https://github.com/kethmark/optimize

## Overview

##### Main System Components

a) Frontend Layer:
- Next.js App Router architecture
- Client-side components (under /components)
- Theme and UI components system
- Authentication flows

b) Backend Layer:
- API Routes (under /app/api)
- Authentication system
- RAG (Retrieval-Augmented Generation) AI middleware
- Database interactions

c) External Services:
- Supabase integration
- AI service integration

**┌─────────────────────────────────────────┐**
- Client Layer                            │
- Pages (app/*)                       │
- Components                          │
- UI Component Library               │
- ┘

**┌─────────────────────────────────────────┐**
- API Layer                               │
- Auth Routes                         │
- Chat Routes                         │
- Document Routes                     │
- Profile Routes                      │
- ┘

**┌─────────────────────────────────────────┐**
- Service Layer                           │
- Supabase                            │
- RAG AI System                       │
- Database (Drizzle ORM)              │
- ┘

##### Key Relationships to Highlight

- User authentication flow
- Chat message flow
- Document upload and embedding process
- AI interaction pipeline

##### Technical Stack Callouts

- Next.js 13+ (App Router)
- TypeScript
- Tailwind CSS
- Drizzle ORM
- Supabase
- AI Integration

##### Data Flow Patterns

Highlight:
- Server-side rendering paths
- Client-side interactions
- API route handling
- Database queries
- AI processing pipeline

Remember to include:
- Error handling flows
- Authentication state management
- Data persistence patterns
- Real-time update mechanisms
- AI processing pipeline

This will give other engineers a clear understanding of how the application works from both a high-level architectural perspective and a detailed implementation view.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Client
        AppRouter["Next.js App Router"]
        ClientComp["Client Components"]
        UISystem["UI Component System"]:::ui
        ThemeSystem["Theme System"]:::ui
        ChatUI["Chat Interface"]:::ui
        NavComp["Navigation Components"]:::ui
        DocUI["Document Management UI"]:::ui
    end

    subgraph APILayer
        AuthAPI["Authentication API"]:::api
        ChatAPI["Chat API"]:::api
        DocAPI["Document API"]:::api
        ProfileAPI["Profile API"]:::api
        ConvAPI["Conversation API"]:::api
        EmbedAPI["Embedding API"]:::api
    end

    subgraph Services
        AuthSystem["Authentication System"]
        RAGSystem["RAG AI System"]
        SupabaseClient["Supabase Client"]
        SupabaseServer["Supabase Server"]
        DB[(Database)]
    end

    %% Client Layer Relationships
    AppRouter --> AuthSystem
    AppRouter --> ClientComp
    ClientComp --> UISystem
    ClientComp --> ThemeSystem
    ClientComp --> ChatUI
    ClientComp --> NavComp
    ClientComp --> DocUI

    %% API Layer Relationships
    ChatUI --> ChatAPI
    DocUI --> DocAPI
    NavComp --> ProfileAPI
    ChatUI --> ConvAPI
    DocUI --> EmbedAPI
    AuthSystem --> AuthAPI

    %% Service Layer Relationships
    ChatAPI --> RAGSystem
    DocAPI --> RAGSystem
    RAGSystem --> SupabaseServer
    AuthAPI --> SupabaseServer
    SupabaseServer --> DB
    SupabaseClient --> DB

    %% Styling
    classDef ui fill:#3498db
    classDef api fill:#2ecc71
    classDef service fill:#9b59b6
    classDef database fill:#95a5a6

    %% Click Events
    click AppRouter "https://github.com/KethMark/optimize/tree/main//app/*"
    click ClientComp "https://github.com/KethMark/optimize/tree/main//components/*"
    click UISystem "https://github.com/KethMark/optimize/tree/main//components/ui/*"
    click AuthSystem "https://github.com/KethMark/optimize/blob/main//app/api/auth/[...nextauth]/route.ts"
    click ChatAPI "https://github.com/KethMark/optimize/blob/main//app/api/chat/route.ts"
    click DocAPI "https://github.com/KethMark/optimize/blob/main//app/api/document/route.ts"
    click ProfileAPI "https://github.com/KethMark/optimize/blob/main//app/api/profile/route.ts"
    click ConvAPI "https://github.com/KethMark/optimize/blob/main//app/api/conversation/route.ts"
    click RAGSystem "https://github.com/KethMark/optimize/blob/main//ai/rag-middleware.ts"
    click DB "https://github.com/KethMark/optimize/blob/main//db/schema.ts"
    click SupabaseClient "https://github.com/KethMark/optimize/blob/main//lib/supabase/client.ts"
    click SupabaseServer "https://github.com/KethMark/optimize/blob/main//lib/supabase/server.ts"
    click ChatUI "https://github.com/KethMark/optimize/blob/main//components/chat.tsx"
    click ThemeSystem "https://github.com/KethMark/optimize/blob/main//components/theme-provider.tsx"
    click DocUI "https://github.com/KethMark/optimize/blob/main//components/upload.tsx"
    click NavComp "https://github.com/KethMark/optimize/blob/main//components/nav-main.tsx"
    click EmbedAPI "https://github.com/KethMark/optimize/blob/main//app/api/embed/route.ts"
```
