# julesliu390/petgpt architecture

> Architecture diagram of the GitHub repository julesliu390/petgpt, 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/julesliu390/petgpt
- Repository: https://github.com/julesliu390/petgpt

## Overview

##### Project Type & Purpose
- PetGPT is a full-stack desktop application that brings AI companionship to users via customizable virtual pets. It leverages Electron as the desktop shell (backend) and a React-based frontend for user interaction.
- The README emphasizes its AI-powered conversational abilities, character customization, and integrations with multiple AI models (such as OpenAI, Gemini, Claude, and Ollama).

##### Analyzing the File Structure
- The project is split into two main directories:
- The “electron” directory holds the core desktop process, Electron’s main process, the preload script (for secure context bridging between the backend and frontend), assets (like images and static files), and models (modules that likely handle conversations, image processing, pet memory, and settings).
- The “frontend” directory contains the React application, including components for different UI panels (chat, character creation, settings), utility functions (e.g., for OpenAI interactions), and static assets. Configuration files (e.g., Vite, Tailwind, ESLint) reveal the development tooling.
- Configuration and build files (e.g., package.json, yarn.lock, build scripts in electron and frontend) indicate separate build and development processes between the Electron backend and the React UI.

##### Key Components
a. Desktop Shell (Electron):
- Main Process (main.js): The entry point that launches the application.
- Preload Script (Preload.js): Acts as a secure bridge façade for exposing a limited API interface to the frontend.
- IPC Handlers (ipcMainHandler.js): Manage communication between frontend and main process.
- Models (under electron/models): Modules for conversation, image processing, pet state/memory, and settings, encapsulating business logic and state management.

b. Frontend (React Application):
- React Components (under frontend/src/components): UI elements for chat windows, character selection, settings, and adding a chatbot.
- Content & State Management (under frontend/src/content): Setup for application state (using React context and reducer patterns).
- Utility Functions (frontend/src/utils): Functions to interact with third-party APIs (like OpenAI) and handle additional logic.
- Public & Asset Folders: Handling static assets and public files.

c. External Services and Libraries:
- AI Model Providers: APIs for OpenAI, Gemini, etc.
- Styling and Build Tools: TailwindCSS, Vite for frontend builds, and Electron-builder for desktop packaging.
- Testing and Linting Tools: ESLint, canvas libraries for image manipulation.

##### Relationships & Interactions
- The frontend sends UI events (chat messages, character customization commands) to the Electron backend via the preload script and IPC channels.
- The Electron backend, via its designated models, processes these requests, interacts with external AI APIs, and handles system-level tasks such as file system access and image processing.
- The processed responses or actions are sent back to the frontend to update the UI.
- Build and packaging flow: The frontend is built (using Vite/TailwindCSS) into a distributable bundle, which the Electron process then loads. Electron’s build scripts package everything for macOS/Windows.

##### Architectural Patterns & Design Principles
- Layered/MVC-like separation: The UI (frontend components) is decoupled from the business logic (Electron models and IPC handlers).
- Clear separation of concerns: The desktop wrapper handles system-level interactions, while the frontend manages presentation and user interactions.
- Modularity & Extensibility: The architecture is designed to support additional model providers by simply adding new configuration points and model modules under the electron directory.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Electron Backend"
        direction TB
        EM["Electron Main Process (main.js)"]:::electron
        PL["Preload.js (IPC Bridge)"]:::electron
        IPC["IPC Handlers"]:::electron

        subgraph "Models"
            direction TB
            CModel["Conversation Model"]:::model
            IModel["Image Processing Model"]:::model
            PMModel["Pet Memory/State Model"]:::model
            SModel["Settings Model"]:::model
            PetModule["Pet Module"]:::model
        end

        Assets["Desktop Assets (Static Files)"]:::electron
    end

    subgraph "React Frontend"
        direction TB
        UI["React UI Components"]:::frontend
        State["State Management"]:::frontend
        Utils["Utility Functions (API Clients)"]:::frontend
        PUBLIC["Public & HTML Entry"]:::frontend
    end

    External["External AI Model Providers (OpenAI, Gemini, Claude, Ollama)"]:::external
    Deployment["Build & Packaging (Vite, TailwindCSS, Electron-builder)"]:::deployment

    %% Data Flow
    UI -->|"UIEvent"| PL
    PL -->|"IPCRequest"| IPC
    IPC -->|"ProcessRequest"| CModel
    IPC -->|"ProcessRequest"| IModel
    IPC -->|"ProcessRequest"| PMModel
    IPC -->|"ProcessRequest"| SModel
    IPC -->|"ProcessRequest"| PetModule
    CModel -.->|"CallAI"| External
    IModel -.->|"CallAI"| External
    PMModel -.->|"CallAI"| External
    SModel -.->|"Manage"| Assets
    PetModule -.->|"ManagePet"| Assets
    External -->|"Response"| IPC
    IPC -->|"Response"| PL
    PL -->|"UpdateUI"| UI
    UI -->|"StateUpdate"| State
    UI -->|"Utilizes"| Utils

    %% Build & Deployment Connections
    UI ---|"BuiltBy"| Deployment
    Assets ---|"PackagedBy"| Deployment

    %% Click Events for Electron Backend Components
    click EM "https://github.com/julesliu390/petgpt/blob/main/electron/main.js"
    click PL "https://github.com/julesliu390/petgpt/blob/main/electron/Preload.js"
    click IPC "https://github.com/julesliu390/petgpt/blob/main/electron/ipcMainHandler.js"
    click CModel "https://github.com/julesliu390/petgpt/blob/main/electron/models/conversation.js"
    click IModel "https://github.com/julesliu390/petgpt/blob/main/electron/models/image_processor.js"
    click PMModel "https://github.com/julesliu390/petgpt/blob/main/electron/models/pet_memory.js"
    click SModel "https://github.com/julesliu390/petgpt/blob/main/electron/models/settings.js"
    click PetModule "https://github.com/julesliu390/petgpt/blob/main/electron/models/pet.js"
    click Assets "https://github.com/julesliu390/petgpt/tree/main/electron/assets"

    %% Click Events for React Frontend Components
    click UI "https://github.com/julesliu390/petgpt/tree/main/frontend/src/components"
    click State "https://github.com/julesliu390/petgpt/tree/main/frontend/src/content"
    click Utils "https://github.com/julesliu390/petgpt/tree/main/frontend/src/utlis"
    click PUBLIC "https://github.com/julesliu390/petgpt/blob/main/frontend/index.html"

    %% Styles & Colors
    classDef electron fill:#F9A825,stroke:#000,stroke-width:1px;
    classDef model fill:#FFB74D,stroke:#000,stroke-width:1px;
    classDef frontend fill:#42A5F5,stroke:#000,stroke-width:1px;
    classDef external fill:#66BB6A,stroke:#000,stroke-width:1px;
    classDef deployment fill:#AB47BC,stroke:#000,stroke-width:1px;
```
