# chyyynh/magi architecture

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

- Interactive diagram: https://gitdiagram.com/chyyynh/magi
- Repository: https://github.com/chyyynh/magi

## Overview

##### Main components of the system
- Next.js Frontend: This includes the layout, pages (such as page.tsx), and the overall application routing. It is responsible for rendering the UI and controlling navigation.
- API Layer: Under the folder src/app/api, there is at least one API route (snapshot.ts) which interfaces with external services such as Snapshot’s API. Mark it as a serverless function or API endpoint.
- UI Components: A rich set of components exist in both src/app/components and src/components/ui (ChatBot, PropUI, ClippedRecBal, button, input, etc.). Group these as “UI Widgets” or “Reusable Components.”
- Utility and Service Modules: The project contains modules like aiService.ts (which implements the prompt for LLM-based decision support) and proposalUtils.ts. These encapsulate business logic and service communication.
- External Dependencies: The system references an external LLM (currently Gemini AI) for processing governance proposals along with connectivity to playback external API tools (e.g., Snapshot’s API as evident in the provided docs URL).

##### Relationships and interactions
- The Next.js application (client-side) renders the UI using the React components.
- Components make use of utility functions (e.g., proposalUtils, aiService) to process proposals and invoke AI analysis.
- API endpoints (like snapshot.ts) expose backend capabilities to interact with external systems such as the Snapshot API.
- The AI service receives details from the UI, processes the proposal using the provided prompt, and then returns analysis data in JSON format so the UI can display the decision results.
- The design shows clear separation of concerns: UI rendering, API processing, and business logic/LLM data handling.

##### Architectural patterns
- Next.js Page-based Routing: The src/app structure follows Next.js conventions, with clearly defined routing for both pages and API endpoints.
- Component-based Architecture: The various UI components are modular so that they can be reused across the application.
- API Layer as Serverless Endpoints: The use of API routes (e.g., /api/snapshot) follows a serverless approach typical in Next.js deployments.
- Utility and Service Layer: Encapsulation of AI-related logic (via aiService.ts) separates concerns between UI and external processing.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global Entities
    browser["User Browser"]:::external

    %% Next.js Frontend Subgraph
    subgraph "Next.js Frontend"
        layout["Layout"]:::frontend
        page["Page"]:::frontend
        layout --> page
        %% UI Components Subgraph
        subgraph "UI Components"
            chatbot["ChatBot"]:::ui
            propu["Proposal UI"]:::ui
            other["Other UI Components"]:::ui
        end
        %% Reusable UI Elements Subgraph
        subgraph "Reusable UI Elements"
            button["Button"]:::component
            hover["Hover Card"]:::component
            input["Input"]:::component
        end
    end

    %% Utility & Service Modules Subgraph
    subgraph "Utility & Service Modules"
        ai_service["AI Service"]:::utility
        proposal_utils["Proposal Utils"]:::utility
    end

    %% API Layer Subgraph
    subgraph "API Layer"
        snapshot["Snapshot API Endpoint"]:::backend
    end

    %% External Services
    external_snapshot["Snapshot API"]:::external
    external_gemini["Gemini AI"]:::external

    %% Data Flow Connections
    browser -->|"request"| page
    page -->|"renders UI"| chatbot
    page --> other
    page --> propu
    page -->|"uses"| button
    page --> hover
    page --> input

    chatbot -->|"analyzes"| ai_service
    propu -->|"processes"| proposal_utils

    ai_service -->|"LLMQuery"| external_gemini
    external_gemini -->|"JSONAnalysis"| ai_service

    page -->|"fetchData"| snapshot
    snapshot -- "calls" --- external_snapshot

    %% Styles
    classDef frontend fill:#C8E6C9,stroke:#388E3C,stroke-width:2px;
    classDef ui fill:#BBDEFB,stroke:#1565C0,stroke-width:2px;
    classDef component fill:#FFE082,stroke:#F9A825,stroke-width:2px;
    classDef utility fill:#F0F4C3,stroke:#C0CA33,stroke-width:2px;
    classDef backend fill:#FFCCBC,stroke:#D84315,stroke-width:2px;
    classDef external fill:#D1C4E9,stroke:#512DA8,stroke-width:2px;

    %% Click Events
    click layout "https://github.com/chyyynh/magi/blob/main/src/app/layout.tsx"
    click page "https://github.com/chyyynh/magi/blob/main/src/app/page.tsx"
    click snapshot "https://github.com/chyyynh/magi/blob/main/src/app/api/snapshot.ts"
    click chatbot "https://github.com/chyyynh/magi/blob/main/src/app/components/ChatBot.tsx"
    click propu "https://github.com/chyyynh/magi/blob/main/src/app/components/PropUI.tsx"
    click other "https://github.com/chyyynh/magi/tree/main/src/app/components/"
    click button "https://github.com/chyyynh/magi/blob/main/src/components/ui/button.tsx"
    click hover "https://github.com/chyyynh/magi/blob/main/src/components/ui/hover-card.tsx"
    click input "https://github.com/chyyynh/magi/blob/main/src/components/ui/input.tsx"
    click ai_service "https://github.com/chyyynh/magi/blob/main/src/app/utils/aiService.ts"
    click proposal_utils "https://github.com/chyyynh/magi/blob/main/src/app/utils/proposalUtils.ts"
```
