# yshashi/policy-copilot architecture

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

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

## Overview

##### Project Type and Purpose
- Full-stack Retrieval-Augmented Generation (RAG) system.
- Purpose: let users upload PDF documents, automatically chunk and embed them, then ask natural-language questions with AI-powered answers and source citations.

##### Main Components (Nodes
a. Frontend (Angular 18 SPA)
- Components: DocumentUpload, DocumentList, Chat (Q&A)
- Services: DocumentService (HTTP client)
- Configuration: app.routes.ts, signals-based state
b. Backend/API (.NET 8 Web API)
- Controllers:
– DocumentsController (upload & list)
– QueryController (handle user queries)
– TestController (healthchecks)
- Services (business logic):
– DocumentService (PDF chunking, text extraction via PdfPig)
– GeminiEmbeddingService (calls Google Gemini API for embeddings)
– RagService (semantic search, context-aware prompt assembly)
- Data Layer:
– ApplicationDbContext (EF Core + SQLite)
– Models: Document, Chunk, Embedding metadata
- File Storage: uploads folder (raw PDFs, chunk files if any)
c. Persistence Layer
- SQLite database (policycopilot.db)
- File system storage (PolicyCopilot.Api/uploads)
d. External Services
- Google Gemini Pro (AI model for embeddings & generation)
- (Optionally note environment variables / config: GeminiApiKey in appsettings.json)

1. User → Frontend UI (Browser)
2. UI → Backend (HTTPS REST calls)
- POST /api/documents/upload → DocumentsController → DocumentService
– PDFPig extracts text → chunks → saved to DB & file system
– Calls GeminiEmbeddingService to generate vector embeddings → stored in DB
- GET /api/documents → DocumentsController → fetch list from DB
- POST /api/query → QueryController → RagService
– Fetch top-k similar document embeddings from DB (semantic search)
– Assemble context + user query into prompt → call Gemini API to generate answer
– Return answer + source citations (document excerpts, similarity scores)
3. Backend reads/writes → SQLite DB & file system
4. Backend ↔ Google Gemini API (external HTTP)

##### Architectural Patterns & Design Principles
- Layered architecture (Presentation → API/Controllers → Services → Data)
- Separation of concerns: Controllers (API endpoints) vs. Services (business logic) vs. Data (EF Core)
- RAG pipeline (vector store + semantic search + LLM prompt generation)
- Single-page application (SPA) best-practices in Angular (standalone components, signals)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Frontend Layer
    subgraph "Frontend (Angular SPA)"
        direction TB
        DU["DocumentUpload"]:::frontend
        DL["DocumentList"]:::frontend
        CH["Chat (Q&A)"]:::frontend
        DS["DocumentService (HTTP client)"]:::frontend
        RC["app.routes.ts"]:::frontend
        AC["app.config.ts"]:::frontend
    end

    %% Backend/API Layer
    subgraph "Backend/API (.NET 8 Web API)"
        direction TB
        subgraph "Controllers"
            DC["DocumentsController"]:::backend
            QC["QueryController"]:::backend
            TC["TestController"]:::backend
        end
        subgraph "Services (Business Logic)"
            BDS["DocumentService"]:::backend
            GES["GeminiEmbeddingService"]:::backend
            RAS["RagService"]:::backend
            IDS["IDocumentService (Interface)"]:::backend
        end
        subgraph "Data Layer (EF Core)"
            ADC["ApplicationDbContext"]:::backend
            DM["Document Model"]:::backend
            CM["Chunk & Embedding Metadata Models"]:::backend
        end
        FS["appsettings.json\n(GeminiApiKey)"]:::config
        FSD["appsettings.Development.json"]:::config
    end

    %% Persistence Layer
    subgraph "Persistence"
        direction TB
        DB["SQLite Database\n(policycopilot.db)"]:::database
        UP["File Storage\n/uploads"]:::filesystem
    end

    %% External Services
    GEM["Google Gemini Pro API"]:::external

    %% User Interaction
    User["User (Browser)"]:::external

    %% Relationships
    User -->|"1. Uses UI"| DU
    User --> DL
    User --> CH

    DU -->|"upload PDF"| DS
    DL -->|"fetch list"| DS
    CH -->|"ask query"| DS

    DS -->|"POST /api/documents/upload"| DC
    DS -->|"GET /api/documents"| DC
    DS -->|"POST /api/query"| QC

    DC -->|"calls"| BDS
    QC -->|"calls"| RAS
    TC -->|"healthcheck"| TC

    BDS -->|"extract & chunk"| ADC
    BDS -->|"save file"| UP
    BDS -->|"call embed"| GES

    GES -->|"HTTP API"| GEM

    RAS -->|"semantic search"| ADC
    RAS -->|"assemble prompt"| RAS
    RAS -->|"generate answer"| GEM

    ADC --> DM
    ADC --> CM

    %% Configuration
    DC --> FS
    QC --> FS
    BDS --> FSD

    %% Persistence connections
    BDS --> DB
    RAS --> DB

    %% Click Events
    click DU "https://github.com/yshashi/policy-copilot/blob/main/policy-copilot-ui/src/app/components/document-upload/document-upload.component.ts"
    click DL "https://github.com/yshashi/policy-copilot/blob/main/policy-copilot-ui/src/app/components/document-list/document-list.component.ts"
    click CH "https://github.com/yshashi/policy-copilot/blob/main/policy-copilot-ui/src/app/components/chat/chat.component.ts"
    click DS "https://github.com/yshashi/policy-copilot/blob/main/policy-copilot-ui/src/app/services/document.service.ts"
    click RC "https://github.com/yshashi/policy-copilot/blob/main/policy-copilot-ui/src/app/app.routes.ts"
    click AC "https://github.com/yshashi/policy-copilot/blob/main/policy-copilot-ui/src/app/app.config.ts"
    click DC "https://github.com/yshashi/policy-copilot/blob/main/PolicyCopilot.Api/Controllers/DocumentsController.cs"
    click QC "https://github.com/yshashi/policy-copilot/blob/main/PolicyCopilot.Api/Controllers/QueryController.cs"
    click TC "https://github.com/yshashi/policy-copilot/blob/main/PolicyCopilot.Api/Controllers/TestController.cs"
    click BDS "https://github.com/yshashi/policy-copilot/blob/main/PolicyCopilot.Api/Services/DocumentService.cs"
    click GES "https://github.com/yshashi/policy-copilot/blob/main/PolicyCopilot.Api/Services/GeminiEmbeddingService.cs"
    click RAS "https://github.com/yshashi/policy-copilot/blob/main/PolicyCopilot.Api/Services/RagService.cs"
    click IDS "https://github.com/yshashi/policy-copilot/blob/main/PolicyCopilot.Api/Services/IDocumentService.cs"
    click ADC "https://github.com/yshashi/policy-copilot/blob/main/PolicyCopilot.Api/Data/ApplicationDbContext.cs"
    click DM "https://github.com/yshashi/policy-copilot/blob/main/PolicyCopilot.Api/Models/Document.cs"
    click UP "https://github.com/yshashi/policy-copilot/tree/main/PolicyCopilot.Api/uploads/"
    click DB "https://github.com/yshashi/policy-copilot/blob/main/PolicyCopilot.Api/policycopilot.db"
    click FS "https://github.com/yshashi/policy-copilot/blob/main/PolicyCopilot.Api/appsettings.json"
    click FSD "https://github.com/yshashi/policy-copilot/blob/main/PolicyCopilot.Api/appsettings.Development.json"

    %% Styles
    classDef frontend fill:#cce5ff,stroke:#3399ff,color:#000;
    classDef backend fill:#d4edda,stroke:#28a745,color:#000;
    classDef database fill:#ffe5b4,stroke:#ff8c00,color:#000,shape:cylinder;
    classDef filesystem fill:#e2e3e5,stroke:#6c757d,color:#000;
    classDef external fill:#f5c6cb,stroke:#dc3545,color:#000;
    classDef config fill:#fdfd96,stroke:#ffeb3b,color:#000;
```
