# layeddie/ahpta architecture

> Architecture diagram of the GitHub repository layeddie/ahpta, 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/layeddie/ahpta
- Repository: https://github.com/layeddie/ahpta

## Overview

##### Main Components

a. Frontend Layer:
- Phoenix LiveView UI (assets/js and lib/ahpta_web/live)
- WebSocket connections for real-time updates
- TailwindCSS for styling (assets/css)

b. Backend Layer:
- Phoenix Web Server (lib/ahpta_web)
- Authentication System (lib/ahpta/accounts)
- Core Business Logic Modules:
- Conversations (lib/ahpta/conversations)
- Messages (lib/ahpta/messages)
- Bots (lib/ahpta/bots)

c. External Services:
- OpenAI Integration (lib/ahpta/openai_mock.ex)
- QDrant Vector Database (lib/ahpta/qdrant.ex)
- Email Service (lib/ahpta/mailer.ex)

d. Data Layer:
- PostgreSQL Database (indicated by presence of priv/repo/migrations)

**[Client Layer (Browser)]**
- ↕
**[Frontend Layer (Phoenix LiveView)]**
- ↕
**[Backend Layer (Phoenix)]**
- ↕
**[Data & External Services Layer]**

##### Component Relationships

a. User Flow:
- Browser → LiveView (WebSocket connection)
- LiveView → User Authentication
- LiveView → Conversation Management
- LiveView → Message Management
- LiveView → Bot Management

b. Bot Integration:
- Bot Module → OpenAI Service
- Bot Module → QDrant Vector DB
- Bot Module → Message System

c. Data Flow:
- All Core Modules → PostgreSQL Database
- Message System → QDrant (for vector storage)
- User System → Mailer (for notifications)

##### Implementation Details to Note

- Database schema representations for key tables
- Authentication flow with special emphasis
- WebSocket connections explicitly

## Diagram (Mermaid)

```mermaid
graph TB
    %% Client Layer
    Browser["Browser/Client"]

    %% Frontend Layer
    subgraph Frontend
        LiveView["LiveView UI\n/lib/ahpta_web/live"]
        CSS["CSS/TailwindCSS\n/assets/css/app.css"]
        JS["JavaScript\n/assets/js"]
        WSClient["WebSocket Client\n/assets/js/app.js"]
    end

    %% Backend Layer
    subgraph Backend
        WebServer["Phoenix Web Server\n/lib/ahpta_web"]
        WSEndpoint["WebSocket Endpoint\n/lib/ahpta_web/endpoint.ex"]
        Router["Router\n/lib/ahpta_web/router.ex"]
        Controllers["Controllers\n/lib/ahpta_web/controllers"]
        Components["UI Components\n/lib/ahpta_web/components"]
        
        subgraph Auth["Authentication System"]
            UserAuth["User Authentication\n/lib/ahpta_web/user_auth.ex"]
            UserLogin["User Login\n/lib/ahpta_web/live/user_login_live.ex"]
        end
        
        subgraph CoreLogic["Core Business Logic"]
            Conversations["Conversations Manager\n/lib/ahpta/conversations"]
            Messages["Messages Manager\n/lib/ahpta/messages"]
            Bots["Bots Manager\n/lib/ahpta/bots"]
        end
    end

    %% External Services Layer
    subgraph External
        OpenAI["OpenAI Service\n/lib/ahpta/openai_mock.ex"]
        QDrant["QDrant Vector DB\n/lib/ahpta/qdrant.ex"]
        Mailer["Email Service\n/lib/ahpta/mailer.ex"]
    end

    %% Data Layer
    subgraph Data
        PostgreSQL[(PostgreSQL Database\n/config/dev.exs)]
        Repo["Database Interface\n/lib/ahpta/repo.ex"]
        Migrations["Database Migrations\n/priv/repo/migrations"]
    end

    %% Relationships
    Browser <--> LiveView
    Browser <--> WSClient
    WSClient <--> WSEndpoint
    LiveView --> Router
    Router --> Controllers
    Router --> Components
    
    LiveView --> UserAuth
    UserAuth --> UserLogin
    
    Components --> Conversations
    Components --> Messages
    Components --> Bots
    
    Conversations --> PostgreSQL
    Messages --> PostgreSQL
    Bots --> PostgreSQL
    
    Bots --> OpenAI
    Messages --> QDrant
    UserAuth --> Mailer
    
    PostgreSQL <--> Repo
    Repo --> Migrations

    %% Styling
    classDef frontend fill:#3498db
    classDef backend fill:#2ecc71
    classDef auth fill:#e74c3c
    classDef external fill:#f1c40f
    classDef data fill:#9b59b6
    
    class LiveView,CSS,JS,WSClient frontend
    class WebServer,WSEndpoint,Router,Controllers,Components,Conversations,Messages,Bots backend
    class UserAuth,UserLogin auth
    class OpenAI,QDrant,Mailer external
    class PostgreSQL,Repo,Migrations data

    %% Click Events
    click LiveView "https://github.com/layeddie/Ahpta/tree/main/lib/ahpta_web/live/"
    click CSS "https://github.com/layeddie/Ahpta/blob/main/assets/css/app.css"
    click JS "https://github.com/layeddie/Ahpta/tree/main/assets/js/"
    click WSEndpoint "https://github.com/layeddie/Ahpta/blob/main/lib/ahpta_web/endpoint.ex"
    click UserAuth "https://github.com/layeddie/Ahpta/blob/main/lib/ahpta_web/user_auth.ex"
    click UserLogin "https://github.com/layeddie/Ahpta/blob/main/lib/ahpta_web/live/user_login_live.ex"
    click Conversations "https://github.com/layeddie/Ahpta/tree/main/lib/ahpta/conversations/"
    click Messages "https://github.com/layeddie/Ahpta/tree/main/lib/ahpta/messages/"
    click Bots "https://github.com/layeddie/Ahpta/tree/main/lib/ahpta/bots/"
    click OpenAI "https://github.com/layeddie/Ahpta/blob/main/lib/ahpta/openai_mock.ex"
    click QDrant "https://github.com/layeddie/Ahpta/blob/main/lib/ahpta/qdrant.ex"
    click Mailer "https://github.com/layeddie/Ahpta/blob/main/lib/ahpta/mailer.ex"
    click Migrations "https://github.com/layeddie/Ahpta/tree/main/priv/repo/migrations/"
    click Repo "https://github.com/layeddie/Ahpta/blob/main/lib/ahpta/repo.ex"
    click Router "https://github.com/layeddie/Ahpta/blob/main/lib/ahpta_web/router.ex"
    click Controllers "https://github.com/layeddie/Ahpta/tree/main/lib/ahpta_web/controllers/"
    click Components "https://github.com/layeddie/Ahpta/tree/main/lib/ahpta_web/components/"
```
