# sultanoid/ai-companion architecture

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

- Interactive diagram: https://gitdiagram.com/sultanoid/ai-companion
- Repository: https://github.com/sultanoid/ai-companion

## Overview

##### Project Type and Purpose
- **Type**: This is a backend‐centric, AI‐driven messaging agent (“Ava”) that exposes a WhatsApp interface and a developer UI (via Chainlit) and orchestrates multiple AI modules.
- **Purpose**: To receive user inputs (text, voice, images) over WhatsApp (and Chainlit), process them through a LangGraph‐based “brain” with memory, vision, speech and scheduling modules, and return rich, context‐aware responses (text, TTS voice notes, generated images).

##### File Structure Analysis
- **Top‐level**
- `.chainlit/` – Chainlit dev UI config and translations
- `Dockerfile`, `docker-compose.yml`, `cloudbuild.yaml` – container build & deploy scripts
- `notebooks/` – exploratory Jupyter notebooks
- **Source code** (`src/ai_companion/`)
- `core/` – central orchestrator: exceptions, prompt templates, scheduling logic
- `graph/` – LangGraph workflow definitions: nodes, edges, state management
- `interfaces/` – external entry points:
– `chainlit/app.py` – dev UI server
– `whatsapp/webhook_endpoint.py` – receives WhatsApp callbacks; `whatsapp_response.py` – formats replies
- `modules/` – plugin modules for each capability:
– `speech/` – speech‑to‑text & text‑to‑speech wrappers (Whisper, ElevenLabs)
– `image/` – image recognition & generation (Vision models, Together AI)
– `memory/long_term/` – vector‐store memory manager (Qdrant) & SQLite for short‐term
– `schedules/` – periodic “daily activity” context generation
- `settings.py` – environment & API key configurations

##### Key Architecture Insights from README
- **AI Providers**: Groq Llama 3.x (text & vision), Whisper (speech recognition), ElevenLabs TTS, Together AI image gen
- **Memory**: SQLite for session/short‑term; Qdrant for long‑term recall
- **Workflow Engine**: LangGraph for defining and executing multi‐step AI workflows
- **Deployment**: Docker, Cloud Run on GCP, Cloud Build for CI/CD

##### Diagramming the System
a. **Main Components**
- **User Interfaces**:
- WhatsApp Chat (via webhook)
- Chainlit Dev UI
- **API Gateway / Web Server**: Single Python service exposing `/webhook` and Chainlit endpoints
- **Core Orchestrator**: LangGraph controller that routes inputs through nodes & edges
- **Modules (Plugin Layer)**: Speech, Image, Memory, Schedule
- **Data Stores**:
- SQLite (short‑term memory)
- Qdrant (long‑term vector memory)
- **External AI Services**:
- Groq Llama 3.x (text/vision inference)
- Whisper (speech‐to‐text)
- ElevenLabs (text‐to‐speech)
- Together AI (image generation)
- **Deployment Infrastructure**: Docker containers, Cloud Run, Cloud Build, Docker Compose (local dev)

b. **Relationships & Data Flow**
1. **Incoming Message**
User → WhatsApp API → Webhook Endpoint
2. **Pre‑processing**
If voice → Speech‑to‑Text module; if image → Vision node
3. **Core Graph Execution**
Orchestrator uses LangGraph to run a workflow:
- Retrieves context from SQLite/Qdrant
- Invokes AI inference (Llama) for response generation
- Uses modules (image/text/speech) as needed
- Updates memory stores
4. **Post‑processing & Response**
If response is text → return as WhatsApp text
If response is voice → ElevenLabs TTS → send voice note
If image → send image media
5. **Developer UI**
Chainlit UI taps into the same orchestrator for real‑time testing

c. **Architectural Patterns & Principles**
- **Hexagonal/Ports & Adapters**: `interfaces/` directory isolates transport from core logic
- **Plugin Architecture**: each capability lives in its own module, easily extended
- **Workflow Engine**: LangGraph graph of nodes & edges models complex multistep reasoning
- **Separation of Concerns**: clear division between I/O (interfaces), orchestration (core/graph), and capability modules

d. **Technologies & Frameworks**
- Python 3.x (pyproject.toml)
- Chainlit (dev UI)
- FastAPI/Flask (implied by webhook, but abstracted behind Chainlit)
- LangGraph (workflow definitions in `graph/`)
- Qdrant (vector DB), SQLite
- Docker, Docker Compose, Cloud Build, Cloud Run

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Interfaces Layer
    subgraph "Interfaces" 
        direction TB
        WhatsAppWebhook["WhatsApp Webhook Endpoint"]:::interface
        WhatsAppResponse["WhatsApp Response Formatter"]:::interface
        ChainlitApp["Chainlit Dev UI"]:::interface
        ChainlitConfig[".chainlit Config"]:::interface
    end

    %% API Server
    API["API Server"]:::compute

    %% Core Layer
    subgraph "Core Orchestrator" 
        direction TB
        Exceptions["Exceptions"]:::compute
        Prompts["Prompt Templates"]:::compute
        CoreSchedules["Scheduling Logic"]:::compute
    end

    %% Workflow Engine
    subgraph "LangGraph Engine" 
        direction TB
        GraphDef["graph.py"]:::compute
        NodesDef["nodes.py"]:::compute
        EdgesDef["edges.py"]:::compute
        StateDef["state.py"]:::compute
        Utils["utils/*"]:::compute
    end

    %% Modules Layer
    subgraph "Modules" 
        direction TB
        subgraph "Speech Module"
            STT["Speech-to-Text"]:::compute
            TTS["Text-to-Speech"]:::compute
        end
        subgraph "Image Module"
            Img2Text["Image-to-Text"]:::compute
            Text2Img["Text-to-Image"]:::compute
        end
        subgraph "Memory Module"
            LongTermMem["Long-Term Memory Manager"]:::compute
            VectorStore["Vector Store"]:::compute
        end
        subgraph "Schedule Module"
            ContextGen["Context Generation"]:::compute
        end
    end

    %% Data Stores
    subgraph "Data Stores"
        direction TB
        SQLite["SQLite (Short-Term)"]:::storage
        Qdrant["Qdrant (Long-Term)"]:::storage
    end

    %% External AI Providers
    subgraph "External AI Services"
        direction TB
        Groq["Groq Llama 3.x"]:::external
        WhisperAI["Whisper"]:::external
        ElevenLabs["ElevenLabs TTS"]:::external
        TogetherAI["Together AI"]:::external
    end

    %% Deployment Infrastructure
    subgraph "Deployment" 
        direction TB
        DockerfileNode["Dockerfile"]:::infra
        DockerCompose["docker-compose.yml"]:::infra
        CloudBuild["cloudbuild.yaml"]:::infra
    end

    %% Connections - Ingress
    WhatsAppWebhook -->|"incoming message"| API
    ChainlitApp -->|"dev UI request"| API

    %% API to Core
    API -->|"route request"| Exceptions
    API --> Prompts
    API --> CoreSchedules

    %% Core to Engine
    Exceptions --> GraphDef
    Prompts --> GraphDef
    CoreSchedules --> GraphDef

    %% Engine to Modules
    GraphDef --> STT
    GraphDef --> TTS
    GraphDef --> Img2Text
    GraphDef --> Text2Img
    GraphDef --> LongTermMem
    GraphDef --> VectorStore
    GraphDef --> ContextGen

    %% Modules to External
    STT -->|"audio"| WhisperAI
    TTS -->|"text"| ElevenLabs
    Img2Text -->|"image"| Groq
    Text2Img -->|"prompt"| TogetherAI

    %% Modules to Data Stores
    LongTermMem -->|"vector read/write"| Qdrant
    VectorStore --> Qdrant
    ContextGen -.->|"scheduled task"| GraphDef
    GraphDef -->|"session context"| SQLite
    GraphDef -->|"update memory"| SQLite

    %% Response Flow
    GraphDef -->|response data| API
    API --> WhatsAppResponse
    API --> ChainlitApp

    %% Deployment Links
    API --> DockerfileNode
    API --> DockerCompose
    API --> CloudBuild

    %% Click Events
    click WhatsAppWebhook "https://github.com/sultanoid/ai-companion/blob/main/src/ai_companion/interfaces/whatsapp/webhook_endpoint.py"
    click WhatsAppResponse "https://github.com/sultanoid/ai-companion/blob/main/src/ai_companion/interfaces/whatsapp/whatsapp_response.py"
    click ChainlitApp "https://github.com/sultanoid/ai-companion/blob/main/src/ai_companion/interfaces/chainlit/app.py"
    click ChainlitConfig "https://github.com/sultanoid/ai-companion/blob/main/.chainlit/config.toml"
    click Exceptions "https://github.com/sultanoid/ai-companion/blob/main/src/ai_companion/core/exceptions.py"
    click Prompts "https://github.com/sultanoid/ai-companion/blob/main/src/ai_companion/core/prompts.py"
    click CoreSchedules "https://github.com/sultanoid/ai-companion/blob/main/src/ai_companion/core/schedules.py"
    click GraphDef "https://github.com/sultanoid/ai-companion/blob/main/src/ai_companion/graph/graph.py"
    click NodesDef "https://github.com/sultanoid/ai-companion/blob/main/src/ai_companion/graph/nodes.py"
    click EdgesDef "https://github.com/sultanoid/ai-companion/blob/main/src/ai_companion/graph/edges.py"
    click StateDef "https://github.com/sultanoid/ai-companion/blob/main/src/ai_companion/graph/state.py"
    click Utils "https://github.com/sultanoid/ai-companion/tree/main/src/ai_companion/graph/utils/*"
    click STT "https://github.com/sultanoid/ai-companion/blob/main/src/ai_companion/modules/speech/speech_to_text.py"
    click TTS "https://github.com/sultanoid/ai-companion/blob/main/src/ai_companion/modules/speech/text_to_speech.py"
    click Img2Text "https://github.com/sultanoid/ai-companion/blob/main/src/ai_companion/modules/image/image_to_text.py"
    click Text2Img "https://github.com/sultanoid/ai-companion/blob/main/src/ai_companion/modules/image/text_to_image.py"
    click LongTermMem "https://github.com/sultanoid/ai-companion/blob/main/src/ai_companion/modules/memory/long_term/memory_manager.py"
    click VectorStore "https://github.com/sultanoid/ai-companion/blob/main/src/ai_companion/modules/memory/long_term/vector_store.py"
    click ContextGen "https://github.com/sultanoid/ai-companion/blob/main/src/ai_companion/modules/schedules/context_generation.py"
    click DockerfileNode "https://github.com/sultanoid/ai-companion/tree/main/Dockerfile"
    click DockerCompose "https://github.com/sultanoid/ai-companion/blob/main/docker-compose.yml"
    click CloudBuild "https://github.com/sultanoid/ai-companion/blob/main/cloudbuild.yaml"

    %% Styles
    classDef interface fill:#DDEBF7,stroke:#4F81BD,color:#000;
    classDef compute fill:#E2EFDA,stroke:#6AA84F,color:#000;
    classDef storage fill:#DEEAF6,stroke:#3D85C6,color:#000;
    classDef external fill:#FCE4D6,stroke:#E69138,color:#000;
    classDef infra fill:#F4CCCC,stroke:#CC0000,color:#000;
```
