# sma1lboy/codefox architecture

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

- Interactive diagram: https://gitdiagram.com/sma1lboy/codefox
- Repository: https://github.com/sma1lboy/codefox

## Overview

##### Main System Components

a) Frontend (Next.js Application)
- User Interface Layer
- Authentication System
- Chat Interface
- Project Management Interface

b) Backend (NestJS Server)
- API Gateway
- Authentication Service
- Build System
- Project Generation System
- Chat Service

c) LLM Server
- Model Provider System
- Model Downloader
- Remote Model Factory

##### Key Architectural Elements

Layer 1: Client Layer
- Next.js Frontend
- UI Components
- Authentication Flow
- WebSocket Connections for Chat

Layer 2: API Gateway Layer
- GraphQL API (evident from the schema files)
- Authentication Guards
- Role-based Access Control

Layer 3: Core Services Layer
- Project Builder Service
- Chat Service
- User Service
- Authentication Service
- File Management Service

Layer 4: AI/LLM Layer
- LLM Provider
- Model Management
- Remote Model Factory
- Model Downloader

Layer 5: Storage Layer
- Database (SQLite based on .db file)
- File Storage for Templates
- Token Cache

##### Interaction Flows to Highlight

- User Authentication Flow
- Project Generation Pipeline
- Chat Communication Flow
- Model Download and Management Flow

##### Specific Implementation Details

- WebSocket connections for real-time chat
- GraphQL API endpoints
- JWT authentication flow
- Model provider architecture

##### Additional Elements

Include:
- External service integrations
- Security boundaries
- Scaling components
- Error handling flow

##### Technical Stack Indicators

- Next.js
- NestJS
- GraphQL
- WebSocket
- JWT
- SQLite
- LLM Integration

This system design should effectively communicate both the overall architecture and the unique AI-powered features that make CodeFox a next-generation development tool.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Client
        UI["Frontend App"]:::frontend
        Auth_UI["Authentication UI"]:::frontend
        Chat_UI["Chat Interface"]:::frontend
        Proj_UI["Project Management"]:::frontend
    end

    subgraph API_Gateway
        GQL["GraphQL API"]:::api
        Guards["Security Guards"]:::security
        WS["WebSocket Handler"]:::api
    end

    subgraph Core_Services
        Auth_Svc["Authentication Service"]:::backend
        Chat_Svc["Chat Service"]:::backend
        User_Svc["User Service"]:::backend
        Proj_Build["Project Builder Service"]:::backend
        Template_Mgr["Template Management"]:::backend
        VDir["Virtual Directory System"]:::backend
    end

    subgraph LLM_Layer
        LLM_Provider["LLM Provider System"]:::ai
        Model_Down["Model Downloader"]:::ai
        Remote_Factory["Remote Model Factory"]:::ai
        Model_Provider["Model Provider Interface"]:::ai
    end

    subgraph Storage
        DB[(Database)]:::storage
        Templates[(Template Storage)]:::storage
        Cache[(Token Cache)]:::storage
    end

    %% Connections
    UI --> Auth_UI
    UI --> Chat_UI
    UI --> Proj_UI
    Auth_UI --> GQL
    Chat_UI --> WS
    Proj_UI --> GQL

    GQL --> Guards
    WS --> Guards
    Guards --> Auth_Svc
    Guards --> User_Svc

    GQL --> Chat_Svc
    WS --> Chat_Svc
    GQL --> Proj_Build
    
    Proj_Build --> Template_Mgr
    Proj_Build --> VDir
    Template_Mgr --> Templates
    
    Chat_Svc --> LLM_Provider
    LLM_Provider --> Model_Down
    LLM_Provider --> Remote_Factory
    LLM_Provider --> Model_Provider

    Auth_Svc --> DB
    User_Svc --> DB
    Chat_Svc --> Cache

    %% Click Events
    click UI "https://github.com/Sma1lboy/codefox/tree/main/frontend/src/app"
    click Auth_UI "https://github.com/Sma1lboy/codefox/tree/main/frontend/src/app/(auth)"
    click Chat_UI "https://github.com/Sma1lboy/codefox/tree/main/frontend/src/components/chat"
    click Proj_UI "https://github.com/Sma1lboy/codefox/tree/main/frontend/src/app/(main)"
    click GQL "https://github.com/Sma1lboy/codefox/blob/main/backend/src/app.resolver.ts"
    click Guards "https://github.com/Sma1lboy/codefox/tree/main/backend/src/guard"
    click WS "https://github.com/Sma1lboy/codefox/blob/main/backend/src/chat/protocol.ts"
    click Auth_Svc "https://github.com/Sma1lboy/codefox/tree/main/backend/src/auth"
    click Chat_Svc "https://github.com/Sma1lboy/codefox/tree/main/backend/src/chat"
    click User_Svc "https://github.com/Sma1lboy/codefox/tree/main/backend/src/user"
    click Proj_Build "https://github.com/Sma1lboy/codefox/blob/main/backend/src/build-system/project-builder.service.ts"
    click Template_Mgr "https://github.com/Sma1lboy/codefox/tree/main/backend/template"
    click VDir "https://github.com/Sma1lboy/codefox/tree/main/backend/src/build-system/virtual-dir"
    click LLM_Provider "https://github.com/Sma1lboy/codefox/tree/main/llm-server/src/model"
    click Model_Down "https://github.com/Sma1lboy/codefox/tree/main/llm-server/src/downloader"
    click Remote_Factory "https://github.com/Sma1lboy/codefox/blob/main/llm-server/src/model/remote-model-factory.ts"
    click Model_Provider "https://github.com/Sma1lboy/codefox/tree/main/backend/src/common/model-provider"
    click DB "https://github.com/Sma1lboy/codefox/blob/main/backend/database.db"

    %% Styles
    classDef frontend fill:#2196F3,color:white
    classDef backend fill:#4CAF50,color:white
    classDef api fill:#FF9800,color:white
    classDef security fill:#F44336,color:white
    classDef ai fill:#9C27B0,color:white
    classDef storage fill:#607D8B,color:white

    %% Legend
    subgraph Legend
        Frontend[Frontend]:::frontend
        Backend[Backend Services]:::backend
        API[API Components]:::api
        Security[Security Components]:::security
        AI[AI/LLM Components]:::ai
        Storage[Storage Components]:::storage
    end
```
