# msrks/vihub architecture

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

- Interactive diagram: https://gitdiagram.com/msrks/vihub
- Repository: https://github.com/msrks/vihub

## Overview

##### Project Type and Purpose
- VIHub is a full-stack web application built using Next.js. It has a sophisticated, multi-faceted UI (with areas for image uploading, annotation, insights, training jobs, etc.) and a robust backend built with API endpoints, TRPC routers, and database integration.
- The README shows an ERD image (./public/erd.png) and includes setup instructions for GCS authentication, confirming that the system interacts with cloud storage. The application also integrates with external services (evident from directories such as server/gcs, server/pinecone, server/replicate, and server/vertexai), likely for AI/ML functionalities and image/data processing.

##### File Structure
- Frontend/UI: The majority of the UI-related code is in the “src/app” directory. Notice subdirectories for workspaces and image stores (like annotate, dataset, insights, llm-playground, monitoring, etc.). Additionally, “src/components” provide reusable UI components.
- Backend/API: API endpoint definitions are found under “src/app/api” and “src/server/api”, with TRPC routers (in “src/trpc” and “src/server/api/routers”) that hint at remote procedure call patterns.
- Database & Migrations: The “drizzle” directory (with its SQL migration files) and “drizzle.config.ts” indicate that the project is using Drizzle ORM for database management. Additionally, “src/server/db” holds schema definitions and database functionality.
- External Integrations: Folders such as “src/server/gcs”, “src/server/pinecone”, “src/server/replicate”, and “src/server/vertexai” signal integration with cloud storage, vector search, image replication/processing, and Vertex AI services respectively.
- Other Configuration/Tooling: The presence of configuration files (next.config.js, tailwind.config.ts, tsconfig.json, etc.) and linters (eslintrc, prettier) support an organized development environment.

a. Main Components:
- Frontend (Next.js UI):
- Pages and sub-pages (organized under src/app) for various features such as image store management, annotation, dataset management, insights, monitoring, etc.
- Common/shared components (in src/components) that support UI consistency and theming.
- Backend / API Layer:
- API Routes: Authentication (using NextAuth in src/app/api/auth), image upload endpoints, workspace management, etc.
- TRPC Routers and centralized endpoints (src/server/api/routers and src/trpc) that handle business logic.
- Database Layer:
- Drizzle ORM migrations (drizzle folder) and schema definitions (src/server/db) defining the persistent store.
- External Services:
- GCS: For cloud storage (instructions in README and code in src/server/gcs).
- Other AI/ML components: Pinecone for vector search; Replicate and Vertex AI for processing, model inference, pipelines, and dataset handling.

b. Relationships and Interactions:
- The UI interacts with the API layer (via Next.js API routes and TRPC) for authentication, data requests, and actions like image upload.
- The API layer, in turn, communicates with the database to perform CRUD operations as defined by the schema.
- Certain API endpoints (like those in server/cron and integrations directories) interact with external services (e.g., GCS, Pinecone, Vertex AI) to manage storage, perform AI predictions, or process images.
- The ERD (referenced in the README) further connects the data model to the application’s flows.

c. Architectural Patterns / Design Principles:
- Separation of Concerns: The project clearly divides responsibilities among the UI (presentation), API/business logic (controllers/routers), data access (Drizzle ORM and database), and external integrations.
- Modular Design: With features organized into distinct subdirectories (e.g., image store features, training, monitoring, etc.), the design supports maintainability and scalability.
- API Abstraction: Use of TRPC and Next.js API routes abstracts the back-end services, making it easy to evolve the application.

d. Key Technologies & Frameworks:
- Next.js for UI and routing.
- Drizzle ORM for database migrations and schema.
- TRPC for API endpoints.
- External services and integrations (GCS, Pinecone, Replicate, Vertex AI).
- Tailwind CSS for styling (inferred from tailwind.config.ts) and additional UI tooling.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Frontend (Next.js)"
        FE1["UI Pages"]:::frontend
        FE2["Shared Components"]:::frontend
    end

    subgraph "API/Backend"
        BE1["API Endpoints"]:::backend
        BE2["TRPC Routers"]:::backend
        BE3["tRPC"]:::backend
        BE4["Cron Jobs"]:::backend
    end

    subgraph "Database"
        DB1["Drizzle Migrations"]:::database
        DB2["DB Schema"]:::database
    end

    subgraph "External Services"
        EX1["GCS"]:::external
        EX2["Pinecone"]:::external
        EX3["Replicate"]:::external
        EX4["Vertex AI"]:::external
    end

    %% Frontend to API/Backend
    FE1 -->|"calls"| BE1

    %% API/Backend interactions
    BE1 -->|"invokes"| BE2
    BE1 -->|"invokes"| BE3
    BE4 -->|"schedules"| BE1

    %% Database interactions
    BE1 -->|"queries"| DB2
    BE2 -->|"CRUD"| DB2
    BE3 -->|"CRUD"| DB2
    BE4 -->|"updates"| DB2

    %% External Service interactions from TRPC Routers
    BE2 -->|"calls"| EX1
    BE2 -->|"calls"| EX2
    BE2 -->|"calls"| EX3
    BE2 -->|"calls"| EX4

    %% Click Events
    click FE1 "https://github.com/msrks/vihub/tree/main/src/app"
    click FE2 "https://github.com/msrks/vihub/tree/main/src/components"
    click BE1 "https://github.com/msrks/vihub/tree/main/src/app/api"
    click BE2 "https://github.com/msrks/vihub/tree/main/src/server/api/routers"
    click BE3 "https://github.com/msrks/vihub/tree/main/src/trpc"
    click BE4 "https://github.com/msrks/vihub/tree/main/src/app/api/cron"
    click DB1 "https://github.com/msrks/vihub/tree/main/drizzle"
    click DB2 "https://github.com/msrks/vihub/tree/main/src/server/db"
    click EX1 "https://github.com/msrks/vihub/tree/main/src/server/gcs"
    click EX2 "https://github.com/msrks/vihub/tree/main/src/server/pinecone"
    click EX3 "https://github.com/msrks/vihub/tree/main/src/server/replicate"
    click EX4 "https://github.com/msrks/vihub/tree/main/src/server/vertexai"

    %% Styles
    classDef frontend fill:#ADD8E6,stroke:#000,stroke-width:2px;
    classDef backend fill:#90EE90,stroke:#000,stroke-width:2px;
    classDef database fill:#FFB6C1,stroke:#000,stroke-width:2px;
    classDef external fill:#FFFACD,stroke:#000,stroke-width:2px;
```
