# applelamps/gpt architecture

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

- Interactive diagram: https://gitdiagram.com/applelamps/gpt
- Repository: https://github.com/applelamps/gpt

## Overview

##### Project Type & Purpose
- This is a browser‑only, single‑page application (SPA) frontend that mimics ChatGPT’s UI and connects directly to OpenAI APIs (gpt‑4o, o3‑mini, DALL·E 3, TTS).
- Purpose: Provide chat functionality, file‐based “knowledge” injection, Custom GPT management, TTS playback, and image generation—all with local persistence in localStorage.

##### File Structure Analysis
Top‑level:
- index.html – the shell that loads ES modules
- css/ – base.css, components.css, layout.css, styles.css
- js/ – main modules (api.js, state.js, parser.js, utils.js, chatStore.js), an events.js bus, UI components directory, customGpt directory
- template_custom_gpt.json – JSON schema for Custom GPT configs
- script.js/styles.css at root—for backward compatibility or loader scripts

js/components: header, sidebar, chatInput, messageList, notification, settingsModal, welcomeScreen
js/customGpt: creatorScreen, customGptManager (gptStore.js), knowledgeHandler

##### README Insights
- Dual‑API routing based on “effective” model
- Streaming support, markdown rendering via marked.js
- PDF text extraction via pdf.js
- localStorage‐backed persistence for chats and Custom GPT configs
- UI state (toggles, image‑gen mode, file attachments) is centralized in state.js

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Presentation / UI Layer
    subgraph "Presentation / UI Layer"
        direction TB
        chatInput["chatInput"]:::ui
        sidebar["sidebar"]:::ui
        settingsModal["settingsModal"]:::ui
        header["header"]:::ui
        messageList["messageList"]:::ui
        notification["notification"]:::ui
        welcomeScreen["welcomeScreen"]:::ui
        dialog["dialog"]:::ui
        sandbox["sandbox"]:::ui
        webSearch["webSearch"]:::ui
    end

    %% Business Logic Layer
    subgraph "Business Logic"
        direction TB
        state["state.js"]:::state
        events["events.js"]:::state
        apiRouter["api.js"]:::api
        gemini["geminiapi.js"]:::api
        parser["parser.js"]:::util
        utils["utils.js"]:::util
        notificationHelper["notificationHelper.js"]:::util
        subgraph "Custom GPT Management"
            direction TB
            creator["creatorScreen"]:::ui
            customGptManager["customGptManager.js"]:::state
            knowledgeHandler["knowledgeHandler.js"]:::util
        end
    end

    %% Persistence Layer
    subgraph "Persistence Layer"
        direction TB
        chatStore["chatStore.js"]:::persistence
        gptStore["gptStore.js"]:::persistence
        localStorage["localStorage"]:::persistence
    end

    %% Entry & Shell
    subgraph "Entry & Shell"
        direction TB
        main["main.js"]:::state
        loader["script.js"]:::util
        html["index.html"]:::ui
    end

    %% External Services
    subgraph "External Services"
        direction TB
        openAI["OpenAI API Endpoints"]:::external
        pdfjs["pdf.js Worker"]:::external
        marked["marked.js"]:::external
        audioApi["Browser Audio API"]:::external
    end

    %% Presentation to State
    chatInput -->|"user input"| state
    sidebar -->|select chat/GPT| state
    settingsModal -->|toggle settings| state
    header -->|display context| state

    %% State to Persistence
    state -->|load/save| chatStore
    state -->|load/save| gptStore
    chatStore -->|persist| localStorage
    gptStore -->|persist| localStorage

    %% Sending Message Flow
    messageList -->|send event| apiRouter
    state -->|inject GPT config| apiRouter
    apiRouter -->|stream responses| parser
    parser -->|HTML output| messageList

    %% TTS Flow
    messageList -->|request speech| apiRouter
    apiRouter -->|fetchSpeech| openAI
    openAI -->|audio data| audioApi

    %% Image Generation Flow
    chatInput -->|image toggle| apiRouter
    apiRouter -->|fetchImageGeneration| openAI
    openAI -->|image data| messageList

    %% Utilities and Helpers
    utils -->|file read/pdf| pdfjs
    utils -->|markdown extraction| marked
    notificationHelper -->|toasts| notification

    %% Custom GPT Loop
    creator -->|create GPT| knowledgeHandler
    knowledgeHandler -->|store knowledge| gptStore
    gptStore -->|make available| sidebar
    sidebar -->|select custom GPT| header
    header -->|context| apiRouter

    %% Entry Sequence
    html --> main
    main --> state
    main --> events
    main --> apiRouter

    %% Class Definitions
    classDef ui fill:#cce5ff,stroke:#3399ff,stroke-width:1px;
    classDef state fill:#d4edda,stroke:#28a745,stroke-width:1px;
    classDef persistence fill:#fff3cd,stroke:#ffc107,stroke-width:1px,shape:cylinder;
    classDef api fill:#f8d7da,stroke:#dc3545,stroke-width:1px;
    classDef external fill:#e2e3e5,stroke:#6c757d,stroke-width:1px,stroke-dasharray: 5 5;
    classDef util fill:#fdfdfe,stroke:#adb5bd,stroke-width:1px,shape:roundrect;

    %% Click Events
    click chatInput "https://github.com/applelamps/gpt/blob/main/js/components/chatInput.js"
    click sidebar "https://github.com/applelamps/gpt/blob/main/js/components/sidebar.js"
    click settingsModal "https://github.com/applelamps/gpt/blob/main/js/components/settingsModal.js"
    click header "https://github.com/applelamps/gpt/blob/main/js/components/header.js"
    click messageList "https://github.com/applelamps/gpt/blob/main/js/components/messageList.js"
    click notification "https://github.com/applelamps/gpt/blob/main/js/components/notification.js"
    click welcomeScreen "https://github.com/applelamps/gpt/blob/main/js/components/welcomeScreen.js"
    click dialog "https://github.com/applelamps/gpt/blob/main/js/components/dialog.js"
    click sandbox "https://github.com/applelamps/gpt/blob/main/js/components/sandbox.js"
    click webSearch "https://github.com/applelamps/gpt/blob/main/js/components/webSearch.js"
    click state "https://github.com/applelamps/gpt/blob/main/js/state.js"
    click events "https://github.com/applelamps/gpt/blob/main/js/events.js"
    click chatStore "https://github.com/applelamps/gpt/blob/main/js/chatStore.js"
    click gptStore "https://github.com/applelamps/gpt/blob/main/js/customGpt/gptStore.js"
    click apiRouter "https://github.com/applelamps/gpt/blob/main/js/api.js"
    click gemini "https://github.com/applelamps/gpt/blob/main/js/geminiapi.js"
    click parser "https://github.com/applelamps/gpt/blob/main/js/parser.js"
    click utils "https://github.com/applelamps/gpt/blob/main/js/utils.js"
    click notificationHelper "https://github.com/applelamps/gpt/blob/main/js/notificationHelper.js"
    click creator "https://github.com/applelamps/gpt/blob/main/js/customGpt/creatorScreen.js"
    click customGptManager "https://github.com/applelamps/gpt/blob/main/js/customGpt/customGptManager.js"
    click knowledgeHandler "https://github.com/applelamps/gpt/blob/main/js/customGpt/knowledgeHandler.js"
    click main "https://github.com/applelamps/gpt/blob/main/js/main.js"
    click loader "https://github.com/applelamps/gpt/blob/main/script.js"
    click html "https://github.com/applelamps/gpt/blob/main/index.html"
```
