# bitcrowd/exmeralda architecture

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

- Interactive diagram: https://gitdiagram.com/bitcrowd/exmeralda
- Repository: https://github.com/bitcrowd/exmeralda

## Overview

##### Project type and purpose
- Exmeralda is a full-stack Phoenix web application (Phoenix LiveView + Ecto) that lets users chat with documentation-style “libraries” using LLM embeddings (pgvector + external AI APIs).
- Key features: user accounts, chat sessions, embedding generation, background workers (Oban), Postgres with pgvector, multiple LLM sources (Groq, Jina, Ollama), and email notifications.

##### File structure for architectural clues
- Top-level directories:
- assets (JS/Tailwind CSS/front-end build)
- lib/exmeralda (contexts: accounts, chats, topics, repo, mailer)
- lib/exmeralda_web (Phoenix endpoint, LiveView, controllers, components)
- priv/repo (Postgres migrations, seeds)
- rel (release scripts for Docker/Fly.io)
- Config files for Oban, Tailwind, esbuild, Phoenix endpoint, mailer, database, and runtime environments
- Background jobs under lib/exmeralda/topics/*_worker.ex

##### Main components of the system
a. Frontend (Web Browser / Static Assets)
- assets/js/app.js, assets/css, Phoenix LiveView templates (HEEx)
- Served via Phoenix.Endpoint → static plug & LiveView WebSocket
b. Phoenix Web Server
- Endpoint (lib/exmeralda_web/endpoint.ex) handles HTTP/WS
- Router → Controllers & LiveViews (lib/exmeralda_web/controllers, lib/exmeralda_web/live)
c. Application Contexts (Hexagonal / “Domain” layer)
- Accounts (user registration & auth)
- Chats (chat sessions, messages, LLM orchestration)
- Topics (library ingestion, chunking, embeddings, vector search)
- Mailer (email delivery via external SMTP/API)
d. Persistence
- Ecto Repo (lib/exmeralda/repo.ex) → Postgres with pgvector extension
- Migrations & seeds under priv/repo
e. Background Processing
- Oban job queue (config/oban.ex)
- Workers: generate_embeddings_worker, ingest_library_worker, etc.
f. External Services
- AI Embedding & LLM APIs (Groq, Jina, Ollama CLI)
- Email provider (SMTP/API)
- Docker & Fly.io for deployment

##### Relationships and interactions
1. Browser ↔ Phoenix.Endpoint (HTTP & LiveView WS)
2. Endpoint → Router → Controller/LiveView → Context functions
3. Contexts → Ecto.Repo → Postgres DB
4. Contexts → Oban.enqueue → Worker processes
5. Workers → External LLM/Embedding APIs → Contexts → Repo
6. Contexts → Mailer → External Email API

##### Architectural patterns & design principles
- MVC + Phoenix LiveView for reactive UI
- Hexagonal/Layered architecture: Web adapters (lib/exmeralda_web) ↔ Domain contexts ↔ Infrastructure (Ecto, Oban, external APIs)
- Asynchronous processing via Oban for long-running tasks (embeddings, ingestion)
- Separation of concerns: authentication, chat messaging, topic ingest/search, and mailing all live in distinct contexts.

##### Elements
- Blue for UI/Web (assets, LiveView)
- Green for Domain Contexts
- Orange for Data stores (Postgres, vector store)
- Purple for External services/APIs

##### Final checklist
- Verify all contexts and their interactions are shown
- Background jobs and vector search path
- Call out LLM providers as external dependencies
- Environment/deployment artifacts (Docker, Fly.toml) at the periphery if desired

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Browser and Frontend Assets
    subgraph "User Interface" 
        direction TB
        Browser["Web Browser"]:::web
        AssetsJS["assets/js/app.js"]:::web
        AssetsCSS["assets/css/app.css"]:::web
        LayoutHEEX["lib/exmeralda_web/components/layouts/app.html.heex"]:::web
        ChatIndexHEEX["lib/exmeralda_web/live/chat_live/index.html.heex"]:::web
    end

    %% Phoenix Endpoint & Web Layer
    subgraph "Web Layer" 
        direction TB
        Endpoint["Phoenix Endpoint"]:::web
        Router["Router"]:::web
        AuthController["Auth Controller"]:::web
        ChatLive["Chat LiveView"]:::web
    end

    %% Domain Layer
    subgraph "Domain Contexts"
        direction TB
        AccountsContext["Accounts Context"]:::domain
        AccountsUser["User Schema"]:::domain
        ChatsContext["Chats Context"]:::domain
        ChatSession["Chat Session Schema"]:::domain
        ChatMessage["Message Schema"]:::domain
        ChatLLM["LLM Orchestration"]:::domain
        ChatSource["Source Config"]:::domain
        TopicsContext["Topics Context"]:::domain
        TopicsLibrary["Library Schema"]:::domain
        TopicsChunk["Chunk Schema"]:::domain
        TopicsDependency["Dependency Schema"]:::domain
        TopicsRAG["RAG Config"]:::domain
        TopicsLineCheck["Line Check"]:::domain
        MailerContext["Mailer Context"]:::domain
    end

    %% Infrastructure Layer
    subgraph "Infrastructure"
        direction TB
        Repo["Ecto Repo"]:::data
        Postgres["Postgres + pgvector"]:::data
        Migrations["Migrations"]:::data
        Seeds["Seeds"]:::data
        ObanConfig["Oban Configuration"]:::data
        GenerateWorker["Generate Embeddings Worker"]:::data
        IngestWorker["Ingest Library Worker"]:::data
    end

    %% External Services
    subgraph "External Services"
        direction TB
        GroqAPI["Groq API"]:::external
        JinaAPI["Jina API"]:::external
        OllamaCLI["Ollama CLI"]:::external
        EmailAPI["Email Provider API"]:::external
        DockerFile["Dockerfile"]:::external
        DockerPG["Dockerfile.pgvector"]:::external
        ServerScript["rel/overlays/bin/server"]:::external
        FlyToml["fly.toml"]:::external
    end

    %% Connections
    Browser -->|HTTP & WebSocket| Endpoint
    AssetsJS --> Endpoint
    AssetsCSS --> Endpoint
    LayoutHEEX --> Endpoint
    ChatIndexHEEX --> Endpoint

    Endpoint --> Router
    Router --> AuthController
    Router --> ChatLive

    AuthController -->|calls| AccountsContext
    ChatLive -->|calls| ChatsContext

    AccountsContext --> AccountsUser
    ChatsContext --> ChatSession
    ChatsContext --> ChatMessage
    ChatsContext --> ChatLLM
    ChatsContext --> ChatSource

    ChatsContext --> TopicsContext
    TopicsContext --> TopicsLibrary
    TopicsContext --> TopicsChunk
    TopicsContext --> TopicsDependency
    TopicsContext --> TopicsRAG
    TopicsContext --> TopicsLineCheck

    AccountsContext --> Repo
    ChatsContext --> Repo
    TopicsContext --> Repo
    MailerContext --> Repo

    AccountsContext --> ObanConfig
    ChatsContext --> ObanConfig
    TopicsContext --> ObanConfig

    ObanConfig --> GenerateWorker
    ObanConfig --> IngestWorker

    GenerateWorker -->|REST API| GroqAPI
    GenerateWorker -->|REST API| JinaAPI
    GenerateWorker -->|CLI| OllamaCLI
    IngestWorker -->|jobs| TopicsContext

    MailerContext -->|SMTP/API| EmailAPI

    Repo -->|SQL| Postgres
    Migrations -->|run| Postgres
    Seeds -->|run| Postgres

    %% Click Events
    click AssetsJS "https://github.com/bitcrowd/exmeralda/blob/main/assets/js/app.js"
    click AssetsCSS "https://github.com/bitcrowd/exmeralda/blob/main/assets/css/app.css"
    click LayoutHEEX "https://github.com/bitcrowd/exmeralda/blob/main/lib/exmeralda_web/components/layouts/app.html.heex"
    click ChatIndexHEEX "https://github.com/bitcrowd/exmeralda/blob/main/lib/exmeralda_web/live/chat_live/index.html.heex"
    click Endpoint "https://github.com/bitcrowd/exmeralda/blob/main/lib/exmeralda_web/endpoint.ex"
    click Router "https://github.com/bitcrowd/exmeralda/blob/main/lib/exmeralda_web/router.ex"
    click AuthController "https://github.com/bitcrowd/exmeralda/blob/main/lib/exmeralda_web/controllers/auth_controller.ex"
    click ChatLive "https://github.com/bitcrowd/exmeralda/blob/main/lib/exmeralda_web/live/chat_live/chat.ex"
    click AccountsContext "https://github.com/bitcrowd/exmeralda/blob/main/lib/exmeralda/accounts.ex"
    click AccountsUser "https://github.com/bitcrowd/exmeralda/blob/main/lib/exmeralda/accounts/user.ex"
    click ChatsContext "https://github.com/bitcrowd/exmeralda/blob/main/lib/exmeralda/chats.ex"
    click ChatSession "https://github.com/bitcrowd/exmeralda/blob/main/lib/exmeralda/chats/session.ex"
    click ChatMessage "https://github.com/bitcrowd/exmeralda/blob/main/lib/exmeralda/chats/message.ex"
    click ChatLLM "https://github.com/bitcrowd/exmeralda/blob/main/lib/exmeralda/chats/llm.ex"
    click ChatSource "https://github.com/bitcrowd/exmeralda/blob/main/lib/exmeralda/chats/source.ex"
    click TopicsContext "https://github.com/bitcrowd/exmeralda/blob/main/lib/exmeralda/topics.ex"
    click TopicsLibrary "https://github.com/bitcrowd/exmeralda/blob/main/lib/exmeralda/topics/library.ex"
    click TopicsChunk "https://github.com/bitcrowd/exmeralda/blob/main/lib/exmeralda/topics/chunk.ex"
    click TopicsDependency "https://github.com/bitcrowd/exmeralda/blob/main/lib/exmeralda/topics/dependency.ex"
    click TopicsRAG "https://github.com/bitcrowd/exmeralda/blob/main/lib/exmeralda/topics/rag.ex"
    click TopicsLineCheck "https://github.com/bitcrowd/exmeralda/blob/main/lib/exmeralda/topics/line_check.ex"
    click GenerateWorker "https://github.com/bitcrowd/exmeralda/blob/main/lib/exmeralda/topics/generate_embeddings_worker.ex"
    click IngestWorker "https://github.com/bitcrowd/exmeralda/blob/main/lib/exmeralda/topics/ingest_library_worker.ex"
    click ObanConfig "https://github.com/bitcrowd/exmeralda/blob/main/config/oban.exs"
    click MailerContext "https://github.com/bitcrowd/exmeralda/blob/main/lib/exmeralda/mailer.ex"
    click EmailAPI "https://github.com/bitcrowd/exmeralda/blob/main/config/mailer.exs"
    click Repo "https://github.com/bitcrowd/exmeralda/blob/main/lib/exmeralda/repo.ex"
    click Migrations "https://github.com/bitcrowd/exmeralda/tree/main/priv/repo/migrations"
    click Seeds "https://github.com/bitcrowd/exmeralda/blob/main/priv/repo/seeds.exs"
    click DockerFile "https://github.com/bitcrowd/exmeralda/tree/main/Dockerfile"
    click DockerPG "https://github.com/bitcrowd/exmeralda/blob/main/Dockerfile.pgvector"
    click ServerScript "https://github.com/bitcrowd/exmeralda/tree/main/rel/overlays/bin/server"
    click FlyToml "https://github.com/bitcrowd/exmeralda/blob/main/fly.toml"

    %% Styles
    classDef web fill:#add8e6,stroke:#333,stroke-width:1px
    classDef domain fill:#98fb98,stroke:#333,stroke-width:1px
    classDef data fill:#ffdead,stroke:#333,stroke-width:1px
    classDef external fill:#dda0dd,stroke:#333,stroke-width:1px
```
