# chatbot-builder-project/chatbot-builder-api architecture

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

- Interactive diagram: https://gitdiagram.com/chatbot-builder-project/chatbot-builder-api
- Repository: https://github.com/chatbot-builder-project/chatbot-builder-api

## Overview

##### Project Type and Purpose
- This is an API microservice designed to support end-to-end chatbot building, including managing chatbots, conversations, workflows, images, graphs (with nodes, links, and ports), and users.
- The README highlights integration with gRPC/protos (infrastructure layer using externally maintained “chatbot-builder-protos”), and notes on using Guid for entities and int ids for workflow components.
- It is built in .NET, following a CQRS-inspired approach where commands, queries, and event-driven behavior are managed separately within the Application layer.

##### Main Components
- Presentation Layer:
- Found in the “ChatbotBuilderApi.Presentation” project.
- Contains API controllers (e.g., ChatbotsController, ConversationsController, UsersController, etc.), request and response view models, query parameters, and filters.
- This layer accepts HTTP requests, validates input, and orchestrates calls into the Application layer.

- Application Layer:
- Located in the “ChatbotBuilderApi.Application” project.
- Contains the core business logic: Commands, CommandHandlers, Queries, QueryHandlers, Validators, and Domain Events.
- Manages operations for managing chatbots, conversations, images, graphs, users, and workflows.
- Uses abstractions for messaging (ICommand, IQuery, etc.) and domain notifications.

- Domain Layer:
- Found in “ChatbotBuilderApi.Domain”.
- Consists of pure business entities such as Chatbot, Conversation, Graph, various Graph nodes (API Action, Generation, Interaction, Prompt, etc.), Images, and Users.
- Implements business rules, value objects (e.g., ChatbotId, ConversationId, etc.), and domain events.
- Enforces business invariants and encapsulates core domain logic.

- Infrastructure Layer:
- Located in “ChatbotBuilderApi.Infrastructure”.
- Provides concrete implementations for cross-cutting concerns: file storage (AzureBlobStorageSettings, AzureFileService, LocalFileService), graph-related services (ApiActionService, GenerationService, SmartRoutingService), and pipeline behaviors (LoggingPipelineBehavior, ValidationPipelineBehavior).
- Contains the integration with the external “chatbot-builder-protos” project (as indicated in the README).

- Persistence Layer:
- Represented by “ChatbotBuilderApi.Persistence”.
- Implements the EF Core DbContext (AppDbContext), entity configurations/mappings (for chatbots, conversations, graphs, images, users, workflows), data migrations, and repositories (e.g., ChatbotRepository, ConversationRepository, WorkflowRepository, UnitOfWork).
- Acts as a bridge between the domain/application layers and the underlying database.

- Composition and Bootstrapping:
- The main “ChatbotBuilderApi” project and “DependencyInjection” folders in the solution handle the overall wiring of components via ServiceCollection extensions.
- The “Program.cs”, “Dockerfile”, and various appsettings.json files indicate multi-environment configuration and deployment via Docker.

##### Architectural Patterns & Design Principles
- That the solution is built upon Clean Architecture/CQRS principles:
- Separate layers for API, Application, Domain, Infrastructure, and Persistence.
- Use of command and query segregation (e.g., “CreateChatbotCommand”, “DeleteChatbotCommandHandler”, “GetChatbotQuery”).
- Domain events (e.g., ChatbotCreatedEvent, WorkflowDeletedEvent) that trigger further processing.
- Dependency Injection (DI) as the method for wiring services (see DependencyInjection folder).
- How validation (via validators in the Application and Core layers) and logging (pipeline behaviors) are inserted into the request processing lifecycle.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Client
    Client("Client"):::external

    %% Composition & Bootstrapping Subgraph
    subgraph "Composition & Bootstrapping"
        COMP1["Main App (ChatbotBuilderApi)"]:::composition
        COMP2["Dependency Injection (ChatbotBuilderApi/DependencyInjection)"]:::composition
    end

    %% Presentation Layer
    subgraph "Presentation Layer"
        PRES["API - Controllers, Requests & ViewModels"]:::presentation
    end

    %% Application Layer
    subgraph "Application Layer"
        APP["Commands, Command/Query Handlers & Validators"]:::application
    end

    %% Domain Layer
    subgraph "Domain Layer"
        DOM["Domain Entities, Value Objects & Domain Events"]:::domain
    end

    %% Infrastructure Layer
    subgraph "Infrastructure Layer"
        INFRA["File Storage, Graph Services, Pipeline Behaviors & gRPC Integration"]:::infrastructure
    end

    %% Persistence Layer
    subgraph "Persistence Layer"
        PERS["EF Core DbContext, Repositories & Mappings"]:::persistence
    end

    %% External Integration
    EXT("chatbot-builder-protos (External)"):::external

    %% Data Flow Connections
    Client -->|"HTTP-request"| PRES
    PRES -->|"API-call"| APP
    APP -->|"Triggers Domain Operation"| DOM
    APP -->|"Invokes Services"| INFRA
    APP -->|"Data Access"| PERS
    DOM -->|"Raises DomainEvents"| APP
    INFRA -->|"gRPC calls"| EXT

    %% Composition Wiring Connections
    COMP1 -->|"Wires Dependencies"| PRES
    COMP1 -->|"Wires Dependencies"| APP
    COMP1 -->|"Wires Dependencies"| INFRA
    COMP1 -->|"Wires Dependencies"| PERS

    %% Click Events
    click PRES "https://github.com/chatbot-builder-project/chatbot-builder-api/blob/main/ChatbotBuilderApi.Presentation"
    click APP "https://github.com/chatbot-builder-project/chatbot-builder-api/blob/main/ChatbotBuilderApi.Application"
    click DOM "https://github.com/chatbot-builder-project/chatbot-builder-api/blob/main/ChatbotBuilderApi.Domain"
    click INFRA "https://github.com/chatbot-builder-project/chatbot-builder-api/blob/main/ChatbotBuilderApi.Infrastructure"
    click PERS "https://github.com/chatbot-builder-project/chatbot-builder-api/blob/main/ChatbotBuilderApi.Persistence"
    click COMP1 "https://github.com/chatbot-builder-project/chatbot-builder-api/tree/main/ChatbotBuilderApi"
    click COMP2 "https://github.com/chatbot-builder-project/chatbot-builder-api/tree/main/ChatbotBuilderApi/DependencyInjection"

    %% Styling Classes
    classDef presentation fill:#B3E5FC,stroke:#0277BD,stroke-width:2px;
    classDef application fill:#C8E6C9,stroke:#2E7D32,stroke-width:2px;
    classDef domain fill:#FFE082,stroke:#FF8F00,stroke-width:2px;
    classDef infrastructure fill:#FFCDD2,stroke:#C62828,stroke-width:2px;
    classDef persistence fill:#D1C4E9,stroke:#673AB7,stroke-width:2px;
    classDef composition fill:#F0F4C3,stroke:#827717,stroke-width:2px;
    classDef external fill:#E1F5FE,stroke:#01579B,stroke-width:2px;
```
