# khanhthanhdev/comai architecture

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

- Interactive diagram: https://gitdiagram.com/khanhthanhdev/comai
- Repository: https://github.com/khanhthanhdev/comai

## Overview

##### Main Components

A. Frontend Layer:
- Next.js Application (App Router)
- Main Application Routes (/app directory)
- Authentication Routes (sign-in, sign-up)
- Sharing Feature (/share/[id])
- Component Preview System (Sandpack integration)

B. Backend Layer:
- API Routes (/app/api)
- Component Generation Endpoints
- OpenGraph Image Generation
- Authentication System
- Database Layer (Prisma + PostgreSQL)

C. External Services:
- Claude AI Integration
- Sandpack Code Sandbox
- Authentication Provider (implied by auth routes)

**[Client Layer]**

- Next.js Frontend
- UI Components (/components)
- Main App Routes
- Auth Routes
- Share Routes

- ▼
**[API Layer]**

- Next.js API Routes
- /api/generateCode
- /api/generateNextjsCode
- /api/og

- ▼
**[External Services]**

- Claude AI
- Sandpack
- Auth Provider

- ▼
**[Data Layer]**

- Prisma ORM ─► PostgreSQL Database

##### Important Elements to Highlight

A. Architecture Patterns:
- Next.js App Router structure
- API route handling
- Database access through Prisma
- Component generation flow

B. Key Interactions:
- User input → API → AI generation
- Generated code → Sandpack preview
- Share functionality flow
- Authentication flow

##### Implementation Details

- Rectangles: Core application components
- Clouds: External services
- Hexagons: API endpoints
- Rounded rectangles: UI components

Annotations for:
- Authentication flow
- Component generation process
- Preview system integration
- Database schema relationships

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Client
        NextApp["Next.js App Router"]
        MainRoutes["Main Routes"]
        AuthRoutes["Auth Routes"]
        ShareRoutes["Share Routes"]
        UIComponents["UI Components"]
        Preview["Component Preview"]
        Header["Header Component"]
        Footer["Footer Component"]
    end

    subgraph API
        APIRoutes["API Routes"]
        GenCode["Generate Code API"]
        GenNextCode["Generate Next.js Code API"]
        OGImage["OpenGraph API"]
        AuthMiddleware["Auth Middleware"]
    end

    subgraph External
        Claude["Claude AI"]
        Sandpack["Sandpack"]
        AuthProvider["Auth Provider"]
    end

    subgraph Data
        PrismaORM["Prisma ORM"]
        PostgreSQL[(PostgreSQL)]
    end

    %% Client Layer Connections
    NextApp --> MainRoutes
    NextApp --> AuthRoutes
    NextApp --> ShareRoutes
    NextApp --> UIComponents
    NextApp --> Preview
    NextApp --> Header
    NextApp --> Footer

    %% API Layer Connections
    APIRoutes --> GenCode
    APIRoutes --> GenNextCode
    APIRoutes --> OGImage
    AuthMiddleware --> APIRoutes

    %% External Services Connections
    GenCode --> Claude
    Preview --> Sandpack
    AuthRoutes --> AuthProvider

    %% Data Layer Connections
    APIRoutes --> PrismaORM
    PrismaORM --> PostgreSQL

    %% Click Events
    click NextApp "https://github.com/khanhthanhdev/comai/blob/main/app/(main)/layout.tsx"
    click MainRoutes "https://github.com/khanhthanhdev/comai/blob/main/app/(main)/page.tsx"
    click AuthRoutes "https://github.com/khanhthanhdev/comai/blob/main/app/sign-in/[[...sign-in]]/page.tsx"
    click ShareRoutes "https://github.com/khanhthanhdev/comai/blob/main/app/share/[id]/page.tsx"
    click Preview "https://github.com/khanhthanhdev/comai/blob/main/components/code-viewer.tsx"
    click GenCode "https://github.com/khanhthanhdev/comai/blob/main/app/api/generateCode/route.ts"
    click GenNextCode "https://github.com/khanhthanhdev/comai/blob/main/app/api/generateNextjsCode/route.ts"
    click OGImage "https://github.com/khanhthanhdev/comai/blob/main/app/api/og/route.tsx"
    click PrismaORM "https://github.com/khanhthanhdev/comai/blob/main/prisma/schema.prisma"
    click UIComponents "https://github.com/khanhthanhdev/comai/tree/main/components/ui"
    click Header "https://github.com/khanhthanhdev/comai/blob/main/components/Header.tsx"
    click Footer "https://github.com/khanhthanhdev/comai/blob/main/components/Footer.tsx"
    click AuthMiddleware "https://github.com/khanhthanhdev/comai/blob/main/middleware.ts"

    %% Styling
    classDef client fill:#2196F3,stroke:#1565C0,color:white
    classDef api fill:#4CAF50,stroke:#2E7D32,color:white
    classDef external fill:#9C27B0,stroke:#6A1B9A,color:white
    classDef data fill:#FF9800,stroke:#EF6C00,color:white

    class NextApp,MainRoutes,AuthRoutes,ShareRoutes,UIComponents,Preview,Header,Footer client
    class APIRoutes,GenCode,GenNextCode,OGImage,AuthMiddleware api
    class Claude,Sandpack,AuthProvider external
    class PrismaORM,PostgreSQL data
```
