# rezabrizi/litcoach architecture

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

- Interactive diagram: https://gitdiagram.com/rezabrizi/litcoach
- Repository: https://github.com/rezabrizi/litcoach

## Overview

##### Project Type & Purpose
- LitCoach is a Chrome extension that interacts with both a frontend (extension UI) and a backend API.
- Its purpose is to offer coding assistance and premium features using third-party integrations (GitHub, Stripe, OpenAI) and a MongoDB store.

##### Analyzing the File Structure
- The project has two primary parts:
- The backend: Located in the ‘api’ directory, it includes:
- app.py (entry point), config.py, and separate folders for db (database connections), models (data structures and business logic for AI help, GitHub, Leetcode, Stripe, user management), routes (API endpoints), and services (external integrations with AI, GitHub, Stripe).
- The frontend/extension: Found under ‘src’ with subdirectories like:
- background (background scripts crucial for Chrome extension event handling)
- options and sidepanel (the UI aspects of the extension presented to the user)
- components (reusable UI components in React/JSX), hooks, lib, and styles
- Other supporting files hint at build tools and configurations (Vite, ESLint, Tailwind, etc.), which means a modern JavaScript framework (likely React) is being used.

##### Insights from the README
- Provides clear instructions for setting up a Python environment for the backend and an npm environment for the frontend.
- Lists necessary external dependencies such as GitHub OAuth, Stripe, MongoDB, and the OpenAI API for AI functionalities.
- Mentions specific activities like Stripe webhook setup and loading the Chrome extension, emphasizing the integration between extension frontends and backend services.

a. Main Components:
- Chrome Extension Frontend:
- Options Page (user settings/configuration)
- Sidepanel (primary interactive display within the browser)
- Background Script (for event handling and communication)
- API Backend:
- API Application (built with Uvicorn/ASGI serving routes defined in ‘api/routes’)
- Controllers/Routes (each file corresponds to different endpoints like ai_assistance, GitHub auth, subscription management, etc.)
- Data Models (in ‘api/models’ outlining the domains such as ai_help, create_repo, user, etc.)
- Services (wrappers around third-party APIs: GitHub, Stripe, and OpenAI)
- Database Module (MongoDB access as per the ‘api/db’ directory)
- External Services:
- GitHub (OAuth authentication and code integration)
- Stripe (subscription management and webhook integration)
- OpenAI (AI assistance services)
- MongoDB (database cluster for persistent storage)

b. Relationships & Interactions:
- The Chrome extension UI sends requests to the backend API endpoints.
- API endpoints interact with the database for CRUD operations.
- The backend calls external APIs:
- GitHub for authentication and operations.
- Stripe for payment processing and subscription renewals.
- OpenAI for generating AI-based responses.
- Stripe also sends webhook events directly to a designated API endpoint (as set up with the CLI forwarding).
- The communication flows from the extension (client) → API server → external services / database.

c. Highlighting Architectural Patterns and Design Principles:
- Separation of Concerns: The frontend (UI/extension code) and the backend (API with business logic) are cleanly separated.
- Modularization: The backend is split into models, routes, and services, each responsible for a specific domain.
- API-Driven Architecture: The Chrome extension interacts with a structured API layer.
- Use of Config Files & Environment Variables: Sensitive external keys and configurations are abstracted in the .env file.

d. Relevant Technologies and Frameworks:
- Frontend: JavaScript/React (JSX files, Vite, Tailwind CSS, ESLint configuration)
- Backend: Python, FastAPI or similar ASGI framework running via Uvicorn (as per “uvicorn api.app:app --reload”)
- External Integrations:
- Stripe (payments and subscriptions)
- GitHub OAuth
- OpenAI API
- MongoDB for database operations

##### Elements
- One for frontend components (perhaps in blue)
- One for backend components (in green)
- One for external services (in orange or red)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Chrome Extension Frontend
    subgraph "Chrome Extension Frontend"
        CE1("Chrome Extension Options Page"):::frontend
        CE2("Chrome Extension Sidepanel"):::frontend
        CE3("Chrome Extension Background Script"):::frontend
    end

    %% API Backend Layer
    subgraph "API Backend"
        BE1("API Application Entry Point"):::backend
        BE2("API Configuration Module"):::backend
        BE3("API Routes / Controllers"):::backend
        BE4("Data Models"):::backend
        subgraph "Services Integrations"
            S_AI("AI Service Integration"):::backend
            S_GH("GitHub Service Integration"):::backend
            S_Stripe("Stripe Service Integration"):::backend
        end
        BE6("API Database Module"):::backend
    end

    %% External Services
    subgraph "External Services"
        EX1("GitHub"):::external
        EX2("Stripe"):::external
        EX3("OpenAI"):::external
        EX4("MongoDB"):::external
    end

    %% Connections from Frontend to API Backend
    CE1 -->|"request"| BE1
    CE2 -->|"request"| BE1
    CE3 -->|"request"| BE1

    %% Internal API Backend Connections
    BE1 -->|"calls"| BE3
    BE2 -->|"configures"| BE1
    BE3 -->|"uses"| BE4
    BE3 -->|"queries"| BE6
    BE3 -->|"invokes"| S_AI
    BE3 -->|"invokes"| S_GH
    BE3 -->|"invokes"| S_Stripe

    %% Services to External Services
    S_AI -->|"calls"| EX3
    S_GH -->|"calls"| EX1
    S_Stripe -->|"calls"| EX2

    %% Database connection
    BE6 -->|"stores data in"| EX4

    %% Stripe webhook interaction
    EX2 -->|"webhook"| BE3

    %% Styles
    classDef frontend fill:#cce5ff,stroke:#0066cc,stroke-width:2px;
    classDef backend fill:#ccffcc,stroke:#009900,stroke-width:2px;
    classDef external fill:#ffcccb,stroke:#cc0000,stroke-width:2px;

    %% Click Events
    click CE1 "https://github.com/rezabrizi/litcoach/tree/main/src/options"
    click CE2 "https://github.com/rezabrizi/litcoach/tree/main/src/sidepanel"
    click CE3 "https://github.com/rezabrizi/litcoach/blob/main/src/background/background.js"
    click BE1 "https://github.com/rezabrizi/litcoach/blob/main/api/app.py"
    click BE2 "https://github.com/rezabrizi/litcoach/blob/main/api/config.py"
    click BE6 "https://github.com/rezabrizi/litcoach/tree/main/api/db"
    click BE4 "https://github.com/rezabrizi/litcoach/tree/main/api/models"
    click BE3 "https://github.com/rezabrizi/litcoach/tree/main/api/routes"
    click S_AI "https://github.com/rezabrizi/litcoach/blob/main/api/services/ai_client.py"
    click S_GH "https://github.com/rezabrizi/litcoach/blob/main/api/services/github.py"
    click S_Stripe "https://github.com/rezabrizi/litcoach/blob/main/api/services/stripe.py"
```
