# openchatai/copilot architecture

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

- Interactive diagram: https://gitdiagram.com/openchatai/copilot
- Repository: https://github.com/openchatai/copilot

## Overview

##### Main Components Overview

a) Dashboard (Frontend)
- Located in /dashboard directory
- Next.js-based web application
- Handles copilot configuration and management
- Contains authenticated and unauthenticated routes

b) LLM Server (Backend)
- Located in /llm-server directory
- Python-based FastAPI application
- Handles AI processing and API integrations
- Contains multiple services and workers

c) Copilot Widget
- Located in /copilot-widget directory
- Embeddable React component
- Client-facing chat interface

##### Key Architectural Components

Database Layer:
- PostgreSQL database connection
- Qdrant vector store for embeddings
- Alembic migrations system

External Services:
- OpenAI API integration
- Vector store connections
- API endpoints being managed

Message Flow:
- WebSocket connections for real-time chat
- REST API calls between components
- Authentication flow

##### Detailed Component Breakdown

LLM Server Services:
- Action Service
- Analytics Service
- Chat Service
- Flow Service
- Search Service
- Upload Service
- Worker Service (Celery)

Dashboard Modules:
- Authentication
- Copilot Management
- Analytics
- Knowledge Base
- Workflow Editor
- Settings Management

Widget Components:
- Chat Interface
- Form Handling
- Socket Management
- Message Processing

##### Important Workflows to Highlight

API Integration Flow:
**Dashboard → Swagger Upload → LLM Server → Vector Store → Action Repository**

Chat Processing Flow:
**Widget → WebSocket → LLM Server → OpenAI → API Execution → Response Transform**

Knowledge Base Flow:
**Upload → Worker Service → Vector Embedding → Vector Store → Search Service**

##### Additional Elements

Security Layer:
- Authentication boundaries
- API key management
- Request validation

Scaling Components:
- Worker distribution
- Database replication
- Cache layers

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Frontend
        Widget["Copilot Widget"]:::frontend
        Dashboard["Dashboard"]:::frontend
        Auth["Authentication"]:::security
        KnowledgeBase["Knowledge Base"]:::frontend
        WorkflowEditor["Workflow Editor"]:::frontend
        Socket["WebSocket Handler"]:::frontend
        MsgProc["Message Processing"]:::frontend
    end

    subgraph "LLM Server"
        LLMCore["LLM Server Core"]:::backend
        ActionSvc["Action Service"]:::backend
        AnalyticsSvc["Analytics Service"]:::backend
        ChatSvc["Chat Service"]:::backend
        FlowSvc["Flow Service"]:::backend
        SearchSvc["Search Service"]:::backend
        UploadSvc["Upload Service"]:::backend
        WorkerSvc["Worker Service"]:::backend
        APIIntegration["API Integration"]:::backend
    end

    subgraph "Data Layer"
        DB[(PostgreSQL)]:::database
        VectorStore[(Qdrant Vector Store)]:::database
    end

    subgraph "External Services"
        OpenAI["OpenAI API"]:::external
        ManagedAPIs["Managed APIs"]:::external
    end

    %% Frontend Connections
    Widget --> Socket
    Socket -.->|WebSocket| ChatSvc
    MsgProc --> Socket
    Dashboard --> Auth
    Auth --> LLMCore
    KnowledgeBase --> UploadSvc
    WorkflowEditor --> FlowSvc

    %% Backend Connections
    LLMCore --> ActionSvc
    LLMCore --> AnalyticsSvc
    LLMCore --> ChatSvc
    LLMCore --> FlowSvc
    LLMCore --> SearchSvc
    LLMCore --> UploadSvc
    LLMCore --> WorkerSvc
    
    %% Service Connections
    ChatSvc --> OpenAI
    SearchSvc --> VectorStore
    ActionSvc --> APIIntegration
    APIIntegration --> ManagedAPIs
    UploadSvc --> WorkerSvc
    WorkerSvc --> VectorStore
    
    %% Data Layer Connections
    LLMCore --> DB
    VectorStore --> SearchSvc

    %% Click Events
    click Widget "https://github.com/openchatai/copilot/blob/main/copilot-widget/lib/CopilotWidget.tsx"
    click Dashboard "https://github.com/openchatai/copilot/tree/main/dashboard/app/(authenticated)"
    click Auth "https://github.com/openchatai/copilot/blob/main/dashboard/app/(authenticated)/layout.tsx"
    click KnowledgeBase "https://github.com/openchatai/copilot/tree/main/dashboard/app/(authenticated)/(copilot)/copilot/[copilot_id]/knowledge"
    click WorkflowEditor "https://github.com/openchatai/copilot/tree/main/dashboard/components/domain/new-flows-editor"
    click Socket "https://github.com/openchatai/copilot/blob/main/copilot-widget/lib/contexts/SocketProvider.tsx"
    click MsgProc "https://github.com/openchatai/copilot/blob/main/copilot-widget/lib/contexts/messageHandler.tsx"
    click LLMCore "https://github.com/openchatai/copilot/blob/main/llm-server/app.py"
    click ActionSvc "https://github.com/openchatai/copilot/blob/main/llm-server/routes/action/action_controller.py"
    click AnalyticsSvc "https://github.com/openchatai/copilot/blob/main/llm-server/routes/analytics/analytics_service.py"
    click ChatSvc "https://github.com/openchatai/copilot/blob/main/llm-server/routes/chat/chat_controller.py"
    click FlowSvc "https://github.com/openchatai/copilot/blob/main/llm-server/routes/flow/flow_controller.py"
    click SearchSvc "https://github.com/openchatai/copilot/blob/main/llm-server/routes/search/search_controller.py"
    click UploadSvc "https://github.com/openchatai/copilot/blob/main/llm-server/routes/uploads/upload_controller.py"
    click WorkerSvc "https://github.com/openchatai/copilot/tree/main/llm-server/workers/tasks"
    click VectorStore "https://github.com/openchatai/copilot/blob/main/llm-server/shared/utils/opencopilot_utils/get_vector_store.py"
    click APIIntegration "https://github.com/openchatai/copilot/blob/main/llm-server/routes/flow/utils/run_openapi_ops.py"
    click DB "https://github.com/openchatai/copilot/blob/main/llm-server/models/models.py"

    classDef frontend fill:#2374ab
    classDef backend fill:#2a9d8f
    classDef database fill:#8338ec
    classDef external fill:#ffb703
    classDef security fill:#e63946
```
