# pedrohenriquebr/horus architecture

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

- Interactive diagram: https://gitdiagram.com/pedrohenriquebr/horus
- Repository: https://github.com/pedrohenriquebr/horus

## Overview

##### Project type and purpose
- This is a full-featured AI assistant platform (“Horus AI Assistant”) built on .NET 8.0.
- It exposes an ASP.NET Core API (with a minimal static frontend in wwwroot), integrates with multiple LLM providers (Gemini, Ollama), implements a RAG-based memory layer (PostgreSQL + pgvector), uses Redis for caching and rate-limiting, and offers a Telegram Bot interface plus an extensible tool system (web search, file operations, etc.).

##### File structure
- Top-level:
- .docker & docker-compose.yaml – container definitions for Postgres, Redis, etc.
- backend/ – the main .NET solution (Horus.sln)
- legacy/ – older Python proof-of-concept (can be omitted or shown as “historical”).
- backend/ hierarchy:
- Api/RootBootstrapper – ASP.NET Core startup (Program.cs, middleware, static assets).
- Horus.Modules.Core.Application – Application layer (CQRS via MediatR, behaviors, decorators, services for embeddings, RAG, memory, Telegram Bot hosted service).
- Horus.Modules.Core.Domain – Domain entities, interfaces, value objects, domain services.
- Horus.Modules.Core.Infra – Infrastructure implementations (EF Core context, pgvector converter, Redis cache, LLM provider adapters for Gemini/Ollama, RAG/Postgres, rate limiter, file system tools).
- Shared & Contract projects – shared abstractions, configuration objects, events, audit logging.
- IntegrationTests / UnitTests / Benchmarks – test and performance projects illustrating pipelines and CI usage.
- Configuration:
- appsettings.json + environment overrides define connection strings, API keys, rate-limit settings.
- .env and Makefile/VSCode tasks for local dev.

##### Inspect the README for architectural insights
-   “API Layer”: ASP.NET Core + MediatR (CQRS).
-   “LLM Integration”: Multi-provider fallback system.
-   “Storage”: PostgreSQL + pgvector, Redis.
-   “Memory System”: RAG-based with similarity search.
-   “Tools”: Modular.
-   “Telegram Bot”: Hosted service.
-   Docker Compose orchestrates dependencies in development.

a. Main components
- Clients:
– Telegram Bot users (via Telegram API)
– (Optional) Web UI served from wwwroot
– CLI or Playground projects
- API Layer: ASP.NET Core (RootBootstrapper)
- Application Layer: MediatR handlers, CQRS pipeline (Validation, UnitOfWork, Caching)
- Domain Layer: Entities, domain services, interfaces (IRepository, IPromptTemplate, etc.)
- Infrastructure Layer:
– Database: PostgreSQL + pgvector
– Cache: Redis
– LLM Providers: Gemini API, Ollama local
– RAG Service: PostgresRagService, DefaultMemoryProvider
– Tools subsystem: WebSearch, FileSystem
– Rate Limiter: TokenBucketRateLimiter
- Background Jobs: Hangfire queues or hosted services (TelegramBotHostedService)
- External Services:
– Telegram API
– Gemini Cloud API
– Ollama daemon
– Supabase / HuggingFace API (for embedding model)

b. Relationships & data flows
1. Client → ASP.NET Core API → MediatR → Application services
2. Application service may:
- Query Domain Entities via Repository → PostgreSQL
- Write Domain Events or persist entities → PostgreSQL
- Enqueue background tasks → Hangfire
- Check or populate Redis cache → Redis
- Call LLM Providers → Gemini / Ollama
- Perform RAG retrieval → pgvector similarity search → Document store
- Invoke Tools → Web search / File operations
3. Results flow back through Application → API → Client

c. Architectural patterns & design principles
- Layered (Onion/Clean) architecture
- CQRS & MediatR pipeline with behaviors (Validation, UnitOfWork, Caching)
- Dependency Injection via IServiceCollection extensions
- Decorator pattern (UnitOfWorkDecorator, caching attribute)
- Hosted services for long-running tasks (TelegramBotHostedService)
- RAG (Retrieval-Augmented Generation) for memory

d. Key technologies/frameworks
- ASP.NET Core 8.0, MediatR, Entity Framework Core + pgvector
- Redis (StackExchange.Redis)
- Docker & Docker Compose
- LLM integration libraries (HTTP clients for Gemini, Ollama, HuggingFace)
- xUnit for tests, BenchmarkDotNet for benchmarks
- C# attributes for caching and domain events

##### Diagramming conventions
- Presentation (light blue)
- Application (green)
- Domain (yellow)
- Infrastructure (orange)
- External (gray)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Presentation Layer
    subgraph "Presentation Layer"
        style PresentationLayer fill:#E0F7FA,stroke:#006064,stroke-width:2px
        WebUI["Static Web UI (wwwroot)"]:::frontend
        TelegramUsers["Telegram Bot Users"]:::frontend
    end

    %% API Layer
    subgraph "API Layer"
        style APILayer fill:#B2EBF2,stroke:#004D40,stroke-width:2px
        ASPNETCore["ASP.NET Core API"]:::api
    end

    %% Application Layer
    subgraph "Application Layer"
        style ApplicationLayer fill:#E8F5E9,stroke:#1B5E20,stroke-width:2px
        MediatR["MediatR Pipeline (CQRS)"]:::app
        TelegramHosted["TelegramBotHostedService"]:::app
    end

    %% Domain Layer
    subgraph "Domain Layer"
        style DomainLayer fill:#FFFDE7,stroke:#F57F17,stroke-width:2px
        DomainEntities["Domain Entities & Services"]:::domain
    end

    %% Infrastructure Layer
    subgraph "Infrastructure Layer"
        style InfraLayer fill:#FFF3E0,stroke:#E65100,stroke-width:2px
        Postgres["PostgreSQL + pgvector"]:::db
        RedisCache["Redis Cache"]:::cache
        RateLimiter["TokenBucketRateLimiter"]:::infra
        GeminiAdapter["Gemini API Adapter"]:::infra
        OllamaAdapter["Ollama Adapter"]:::infra
        OllamaEmbed["OllamaEmbeddingService"]:::infra
        RAGProvider["DefaultMemoryProvider"]:::infra
        RagService["PostgresRagService"]:::infra
        DocumentsRepo["DocumentsRepository"]:::infra
        FileSystemTools["FileSystem Tools"]:::infra
    end

    %% Shared & Contracts
    subgraph "Shared & Contracts"
        style SharedLayer fill:#ECEFF1,stroke:#455A64,stroke-width:2px
        SharedContracts["Shared & Contract Projects"]:::shared
    end

    %% External Systems
    subgraph "External Systems"
        style ExternalLayer fill:#CFD8DC,stroke:#263238,stroke-width:2px
        TelegramAPI["Telegram API"]:::external
        GeminiAPI["Gemini Cloud API"]:::external
        OllamaDaemon["Ollama Daemon"]:::external
        HuggingFace["Supabase/HuggingFace API"]:::external
    end

    %% DevOps
    subgraph "Dev & Deployment"
        style DevOpsLayer fill:#F3E5F5,stroke:#4A148C,stroke-width:2px
        DockerCompose["Docker & Compose"]:::infra
        ConfigFiles["Config Files (appsettings.json, .env)"]:::infra
    end

    %% Connections
    WebUI -->|calls| ASPNETCore
    TelegramUsers -->|messages| TelegramAPI
    TelegramAPI -->|webhook| ASPNETCore
    ASPNETCore -->|dispatches| MediatR
    MediatR -->|invokes| TelegramHosted
    MediatR -->|uses| DomainEntities
    DomainEntities -->|persist/retrieve| Postgres
    DomainEntities -->|cache lookup| RedisCache
    MediatR -->|rate-limit| RateLimiter
    MediatR -->|call| GeminiAdapter
    MediatR -->|call| OllamaAdapter
    MediatR -->|embed| OllamaEmbed
    MediatR -->|RAG retrieve| RagService
    RagService -->|vector search| Postgres
    MediatR -->|memory| RAGProvider
    DomainEntities -->|repo| DocumentsRepo
    MediatR -->|tooling| FileSystemTools
    ASPNETCore -->|reads| ConfigFiles
    DockerCompose -->|orchestrates| Postgres
    DockerCompose -->|orchestrates| RedisCache

    %% External calls
    GeminiAdapter -->|API calls| GeminiAPI
    OllamaAdapter -->|local API| OllamaDaemon
    OllamaEmbed -->|model| HuggingFace

    %% Click Events
    click ASPNETCore "https://github.com/pedrohenriquebr/horus/blob/master/backend/Api/RootBootstrapper/Program.cs"
    click ASPNETCore "https://github.com/pedrohenriquebr/horus/tree/master/backend/Api/RootBootstrapper/Middlewares/"
    click MediatR "https://github.com/pedrohenriquebr/horus/tree/master/backend/Horus.Modules.Core.Application/"
    click TelegramHosted "https://github.com/pedrohenriquebr/horus/blob/master/backend/Horus.Modules.Core.Application/TelegramBot/TelegramBotHostedService.cs"
    click DomainEntities "https://github.com/pedrohenriquebr/horus/tree/master/backend/Horus.Modules.Core.Domain/"
    click Postgres "https://github.com/pedrohenriquebr/horus/blob/master/backend/Horus.Modules.Core.Infra/Context/HorusContext.cs"
    click Postgres "https://github.com/pedrohenriquebr/horus/blob/master/backend/Horus.Modules.Core.Infra/Context/VectorConverter.cs"
    click RedisCache "https://github.com/pedrohenriquebr/horus/blob/master/backend/Horus.Modules.Core.Infra/Services/Cache/RedisLlmCache.cs"
    click RateLimiter "https://github.com/pedrohenriquebr/horus/blob/master/backend/Horus.Modules.Core.Infra/Services/RateLimiter/TokenBucketRateLimiter.cs"
    click OllamaEmbed "https://github.com/pedrohenriquebr/horus/blob/master/backend/Horus.Modules.Core.Infra/Services/Embedding/OllamaEmbeddingService.cs"
    click GeminiAdapter "https://github.com/pedrohenriquebr/horus/tree/master/backend/Horus.Modules.Core.Infra/Services/LLMProvider/GeminiApi/"
    click OllamaAdapter "https://github.com/pedrohenriquebr/horus/tree/master/backend/Horus.Modules.Core.Infra/Services/LLMProvider/Ollama/"
    click RAGProvider "https://github.com/pedrohenriquebr/horus/blob/master/backend/Horus.Modules.Core.Infra/Services/RAG/DefaultMemoryProvider.cs"
    click RagService "https://github.com/pedrohenriquebr/horus/blob/master/backend/Horus.Modules.Core.Infra/Services/RAG/PostgresRagService.cs"
    click DocumentsRepo "https://github.com/pedrohenriquebr/horus/blob/master/backend/Horus.Modules.Core.Infra/Services/Repositories/DocumentsRepository.cs"
    click FileSystemTools "https://github.com/pedrohenriquebr/horus/tree/master/backend/Horus.Modules.Core.Infra/Services/Tools/FileSystem/"
    click SharedContracts "https://github.com/pedrohenriquebr/horus/tree/master/backend/Horus.Modules.Core.Contract/"
    click SharedContracts "https://github.com/pedrohenriquebr/horus/tree/master/backend/Shared/Horus.Modules.Shared.Infra/"
    click SharedContracts "https://github.com/pedrohenriquebr/horus/tree/master/backend/Shared/LuzInga.Contracts/"
    click DockerCompose "https://github.com/pedrohenriquebr/horus/tree/master/.docker/"
    click DockerCompose "https://github.com/pedrohenriquebr/horus/blob/master/docker-compose.yaml"
    click ConfigFiles "https://github.com/pedrohenriquebr/horus/blob/master/backend/Api/RootBootstrapper/appsettings.json"
    click ConfigFiles "https://github.com/pedrohenriquebr/horus/blob/master/.env"
    click ConfigFiles "https://github.com/pedrohenriquebr/horus/tree/master/Makefile"

    %% Styles
    classDef frontend fill:#B3E5FC,stroke:#0288D1,stroke-width:1px
    classDef api fill:#80DEEA,stroke:#006064,stroke-width:1px
    classDef app fill:#A5D6A7,stroke:#2E7D32,stroke-width:1px
    classDef domain fill:#FFF59D,stroke:#F9A825,stroke-width:1px
    classDef infra fill:#FFCC80,stroke:#EF6C00,stroke-width:1px
    classDef db fill:#FFE082,stroke:#FF8F00,stroke-width:1px
    classDef cache fill:#FFAB91,stroke:#D84315,stroke-width:1px
    classDef shared fill:#CFD8DC,stroke:#37474F,stroke-width:1px
    classDef external fill:#B0BEC5,stroke:#263238,stroke-width:1px
```
