# f0lio/discontext architecture

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

- Interactive diagram: https://gitdiagram.com/f0lio/discontext
- Repository: https://github.com/f0lio/discontext

## Overview

##### Project Type and Purpose
- This is a Discord bot application whose purpose is to index and semantically search messages using OpenAI embeddings. It interacts with Discord, uses an external service (OpenAI for embeddings), and stores/retrieves embeddings from a vector database (Qdrant or Milvus).
- The project is organized as a Node.js application written in TypeScript, with clear modular separation between Discord interactions, embedding generation, and storage management.

##### Analyzing the File Structure
- Top-Level Files and Scripts: Files like .env.template, package.json, and nodemon.json indicate a Node.js development environment. The milvus-docker-compose.yml and start-db.sh suggest that a vector database (Milvus) can be hosted via Docker.
- src Directory:
- app.ts serves as the main entry point.
- The discord folder houses everything related to Discord integration – a client (client.ts), commands (grouped by functionality like info and search), events (like messageCreate.ts and interactionCreate.ts), and utility functions to load handlers.
- The embedding folder abstracts the functionality for computing word embeddings with OpenAI, with an abstract file hinting at potential extension or swapping of services.
- The storage folder abstracts vector database access. There are implementations for both Milvus and Qdrant, along with an abstract storage interface to allow swapping between providers.
- The shared folder includes general utilities likely used across modules.

##### Main Components
a. Discord Bot Core:
- Discord Client: Responsible for connecting to Discord’s API.
- Command Handlers: Manage the processing of commands such as “info” (e.g., ping, yo) and “search”.
- Event Handlers: Process Discord events like message creation and interactions.

b. Embedding Service:
- OpenAI Integration: Uses OpenAI’s ADA model to generate vector representations for messages.
- Abstraction Layer: Allows for potential extension or swapping between embedding providers.

c. Storage Engine:
- Vector Database Abstraction: Contains interfaces and adapters for different databases.
- Qdrant & Milvus Integrations: Concrete implementations that enable storage and retrieval of embeddings.
- Docker Deployment: Use of milvus-docker-compose.yml and start-db.sh indicates that the vector databases may be containerized.

d. Shared Utilities and Configuration:
- Shared utilities (src/shared/utils.ts) for common tasks.
- Configuration files (in the root and within src/discord and src/storage) help set up the environment and connection parameters.

##### Relationships and Interactions
- The Discord Client receives events (messages, interactions) from Discord.
- Incoming messages are processed by event handlers and then handed off to command handlers (especially for a search command).
- When a command that requires semantic understanding is initiated, the bot:
- Sends the message data to the Embedding Service to generate word embeddings (using OpenAI’s API).
- Passes the embedding vector to the appropriate Storage Engine adapter (either Qdrant or Milvus) to either store (if it’s a new message) or search (for semantically similar messages).

##### Architectural Patterns and Design Principles
- Modularity and Abstraction: The project neatly separates Discord interaction, embedding generation, and storage, which allows for swapping out the underlying vector database or embedding service without significant changes.
- Extensibility: The use of abstract classes/interfaces (e.g., in storage and embedding folders) means the design supports future growth (like adding new databases or even different embedding providers).
- Service Integration: Emphasis on third-party service integration (Discord API, OpenAI API) is a key aspect of the design.

## Diagram (Mermaid)

```mermaid
flowchart TD
    A["Application Entry Point"]:::appNode
    subgraph "Discord Bot Core"
        DC["Discord Client"]:::discordLayer
        CH["Command Handlers"]:::discordLayer
        EH["Event Handlers"]:::discordLayer
    end
    subgraph "Embedding Service"
        EA["Abstract Embedding"]:::embeddingLayer
        EO["OpenAI Integration"]:::embeddingLayer
        EC["Embedding Config"]:::embeddingLayer
    end
    subgraph "Storage Engine"
        SA["Storage Abstraction"]:::storageLayer
        SM["Milvus Integration"]:::storageLayer
        SQ["Qdrant Integration"]:::storageLayer
        SC["Storage Config"]:::storageLayer
        SI["Storage Index"]:::storageLayer
    end
    subgraph "Shared & Configuration"
        SU["Shared Utilities"]:::sharedLayer
    end
    subgraph "Docker Deployment"
        D1["milvus-docker-compose.yml"]:::dockerLayer
        D2["start-db.sh"]:::dockerLayer
    end
    DA["Discord API"]:::apiLayer
    OA["OpenAI API"]:::externalLayer

    %% Data Flow
    DA --> DC
    A --> DC
    DC --> CH
    DC --> EH
    CH --> EA
    EH --> EA
    EA --> EO
    EO --> EC
    EC --> SA
    SA --> SM
    SA --> SQ
    SM --> SC
    SQ --> SI
    SI --> CH
    SU --- A

    OA --> EO

    %% Click Events for Discord Bot Core
    click DC "https://github.com/f0lio/discontext/blob/main/src/discord/client.ts"
    click CH "https://github.com/f0lio/discontext/tree/main/src/discord/commands/"
    click EH "https://github.com/f0lio/discontext/tree/main/src/discord/events/"

    %% Click Events for Embedding Service
    click EA "https://github.com/f0lio/discontext/blob/main/src/embedding/abstract.embedding.ts"
    click EO "https://github.com/f0lio/discontext/blob/main/src/embedding/openai.ts"
    click EC "https://github.com/f0lio/discontext/blob/main/src/embedding/config.ts"

    %% Click Events for Storage Engine
    click SA "https://github.com/f0lio/discontext/blob/main/src/storage/storage.abstract.ts"
    click SM "https://github.com/f0lio/discontext/blob/main/src/storage/milvus.ts"
    click SQ "https://github.com/f0lio/discontext/blob/main/src/storage/qdrant.ts"
    click SC "https://github.com/f0lio/discontext/blob/main/src/storage/config.ts"
    click SI "https://github.com/f0lio/discontext/blob/main/src/storage/index.ts"

    %% Click Event for Shared Utilities and Configuration
    click SU "https://github.com/f0lio/discontext/blob/main/src/shared/utils.ts"

    %% Click Events for Docker Deployment Files
    click D1 "https://github.com/f0lio/discontext/blob/main/milvus-docker-compose.yml"
    click D2 "https://github.com/f0lio/discontext/blob/main/start-db.sh"

    %% Click Event for Application Entry Point
    click A "https://github.com/f0lio/discontext/blob/main/src/app.ts"

    %% Styles
    classDef appNode fill:#ffccbc,stroke:#d84315,stroke-width:2px;
    classDef discordLayer fill:#bbdefb,stroke:#0d47a1,stroke-width:2px;
    classDef embeddingLayer fill:#c8e6c9,stroke:#2e7d32,stroke-width:2px;
    classDef storageLayer fill:#fff9c4,stroke:#f9a825,stroke-width:2px;
    classDef sharedLayer fill:#d1c4e9,stroke:#6a1b9a,stroke-width:2px;
    classDef dockerLayer fill:#ffe0b2,stroke:#ef6c00,stroke-width:2px;
    classDef apiLayer fill:#eeeeee,stroke:#616161,stroke-width:2px,font-style:italic;
    classDef externalLayer fill:#f8bbd0,stroke:#880e4f,stroke-width:2px,font-style:italic;
```
