# prateekkeshari/peek-ai architecture

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

- Interactive diagram: https://gitdiagram.com/prateekkeshari/peek-ai
- Repository: https://github.com/prateekkeshari/peek-ai

## Overview

##### Project type and purpose
- Peek is an Electron-based MacOS “menu bar” application that provides a unified UI for chatting with multiple AI chatbots (ChatGPT, Bard, Perplexity) plus a social Threads feed. It is a desktop app, not a web service or microservice suite.

##### File structure
- Top‐level Electron entry scripts:
- `.electron-main.js` (or `main.js`) – the Electron main process bootstrapper
- `screenshot.js` – handles screenshot capture and clipboard logic
- `preload.js` – exposes safe IPC and Node APIs to the renderer
- UI directory (`UI/`): contains all HTML, CSS, and JS for the renderer windows—`index.html` (main chat), `dialog.html` (modal dialogs), `onboarding.html` (first-run UX), with their corresponding CSS/JS.
- Configuration and metadata: `package.json`, `preferences.json`, `.gitignore`, GitHub Actions workflow, `dev-app-update.yml` (auto-update settings).

##### Key insights from the README
- Electron auto-updater integration
- Menu bar (tray) application with global keyboard shortcuts
- IPC bridging between UI and native features (screenshot, auto-update, preferences)
- External HTTP calls to AI chatbot services and Threads

##### Main components
a. Electron Main Process
- Window Management (open/close toggle for main UI via ⌘+J)
- Auto-Updater (dev-app-update.yml + electron-updater integration)
- Screenshot Service (screenshot.js)
- Preferences Management (read/write `preferences.json`)
- IPC channels to/from Renderer (via preload.js)

b. Renderer Processes (UI)
- Main Chat Window (`UI/index.html` + `UI/scripts.js`)
- Dialogs & Onboarding (`UI/dialog.*` + `UI/onboarding.*`)
- CSS styling (`UI/styles.css`, `UI/dialog.css`)
- Preload Script Sandbox (preload.js exposing limited Node/IPC)

c. External Services
- OpenAI ChatGPT API
- Google Bard API
- Perplexity AI API
- Threads Social Feed API

d. Local Persistence
- `preferences.json` for user settings
- Screenshot files saved to disk or clipboard

##### Relationships & data flow
2. Renderer initiates a chat message → Sends via IPC (preload) → Main Process performs HTTP request to selected chatbot API → Returns response via IPC → Renderer updates UI.
3. User presses screenshot shortcut → Main Process captures screen (screenshot.js) → Saves/clipboard → Optionally notifies Renderer.
4. Auto‐update check on launch/interval → Main Process fetches update metadata → If update available, notifies Renderer or shows menu item.
5. User changes settings in UI → Renderer sends IPC to Main → Main writes to `preferences.json`.

##### Architectural patterns & design principles
- Electron Multi‐Process Model (separate Main and Renderer)
- IPC Layer via Preload to isolate Node APIs from untrusted UI code
- Single‐Page Renderer windows (HTML/JS/CSS) driven by event handlers
- Modularization of native features (screenshot, menu, auto‐update) into self‐contained scripts
- Use of configuration files for CI/CD, updates, and preferences

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Main Process Layer
    subgraph "Main Process" 
        direction TB
        ElectronMain["Electron Main<br/>(.electron-main.js)"]:::main
        Menu["Tray Icon & Menu<br/>(menu.js)"]:::main
        Screenshot["Screenshot Service<br/>(screenshot.js)"]:::main
        Preload["IPC Preload Script<br/>(preload.js)"]:::main
        AutoUpdater["Auto-Updater Module<br/>(dev-app-update.yml)"]:::main
        PreferencesMgr["Preferences Manager<br/>(preferences.json)"]:::main
    end

    %% Renderer Layer
    subgraph "Renderer Processes" 
        direction TB
        subgraph "Main Chat Window" 
            ChatHTML["index.html"]:::renderer
            ChatScript["scripts.js"]:::renderer
            ChatStyle["styles.css"]:::renderer
        end
        subgraph "Dialogs & Onboarding"
            DialogHTML["dialog.html"]:::renderer
            DialogScript["dialog.js"]:::renderer
            DialogStyle["dialog.css"]:::renderer
            OnboardHTML["onboarding.html"]:::renderer
        end
    end

    %% External Services Layer
    subgraph "External AI & Social APIs"
        direction TB
        ChatGPT["ChatGPT API"]:::external
        Bard["Google Bard API"]:::external
        Perplexity["Perplexity API"]:::external
        Threads["Threads Feed API"]:::external
    end

    %% Local Storage Layer
    subgraph "Local Storage" 
        direction TB
        PreferencesFile["preferences.json"]:::storage
        ScreenshotFiles["Screenshots/Clipboard"]:::storage
    end

    %% Configuration & CI
    subgraph "Configuration & CI"
        direction TB
        PackageJSON["package.json<br/>package-lock.json"]:::storage
        CI["CodeQL Workflow<br/>(.github/workflows/codeql.yml)"]:::storage
        Icons["Tray Icons<br/>(icons/peek.icns)"]:::storage
    end

    %% Connections & Data Flows
    %% Flow 1: Toggle Window
    Menu -->|"1. toggleWindow"| ChatHTML
    Menu -->|"1. toggleWindow"| DialogHTML

    %% Preload IPC Channel
    ChatScript -->|"IPC: sendChatMessage"| Preload
    DialogScript -->|"IPC: dialogAction"| Preload
    Preload -->|"IPC: toMain"| ElectronMain
    ElectronMain -->|"IPC: fromMain"| Preload
    Preload -->|"IPC: toRenderer"| ChatScript

    %% Flow 2: Chat HTTP Calls
    ElectronMain -->|"2. HTTP POST /chat"| ChatGPT
    ElectronMain -->|"2. HTTP POST /chat"| Bard
    ElectronMain -->|"2. HTTP POST /chat"| Perplexity
    ElectronMain -->|"2. HTTP GET /feed"| Threads
    ChatGPT -->|"2. response"| ElectronMain
    Bard -->|"2. response"| ElectronMain
    Perplexity -->|"2. response"| ElectronMain
    Threads -->|"2. response"| ElectronMain

    %% Flow 3: Screenshot
    Screenshot -->|"3. capture"| ScreenshotFiles
    ElectronMain -->|"3. notify"| Preload

    %% Flow 4: Auto-Update
    ElectronMain -->|"4. fetch update meta"| AutoUpdater
    AutoUpdater -->|"4. update available"| ElectronMain
    ElectronMain -->|"4. notify"| ChatScript

    %% Flow 5: Preferences I/O
    ChatScript -->|"5. setPref"| Preload
    Preload -->|"5. write"| PreferencesMgr
    PreferencesMgr -->|"5. file I/O"| PreferencesFile
    ElectronMain -->|"loadPrefs"| PreferencesMgr

    %% Window Creation
    ElectronMain -->|"createWindow"| ChatHTML
    ElectronMain -->|"createWindow"| DialogHTML
    ElectronMain -->|"createWindow"| OnboardHTML

    %% Click events
    click ElectronMain "https://github.com/prateekkeshari/peek-ai/blob/main/.electron-main.js"
    click Menu "https://github.com/prateekkeshari/peek-ai/blob/main/menu.js"
    click Screenshot "https://github.com/prateekkeshari/peek-ai/blob/main/screenshot.js"
    click Preload "https://github.com/prateekkeshari/peek-ai/blob/main/preload.js"
    click AutoUpdater "https://github.com/prateekkeshari/peek-ai/blob/main/dev-app-update.yml"
    click PreferencesMgr "https://github.com/prateekkeshari/peek-ai/blob/main/preferences.json"
    click ChatHTML "https://github.com/prateekkeshari/peek-ai/blob/main/UI/index.html"
    click ChatScript "https://github.com/prateekkeshari/peek-ai/blob/main/UI/scripts.js"
    click ChatStyle "https://github.com/prateekkeshari/peek-ai/blob/main/UI/styles.css"
    click DialogHTML "https://github.com/prateekkeshari/peek-ai/blob/main/UI/dialog.html"
    click DialogScript "https://github.com/prateekkeshari/peek-ai/blob/main/UI/dialog.js"
    click DialogStyle "https://github.com/prateekkeshari/peek-ai/blob/main/UI/dialog.css"
    click OnboardHTML "https://github.com/prateekkeshari/peek-ai/blob/main/UI/onboarding.html"
    click Icons "https://github.com/prateekkeshari/peek-ai/blob/main/icons/peek.icns"
    click PackageJSON "https://github.com/prateekkeshari/peek-ai/blob/main/package.json"
    click CI "https://github.com/prateekkeshari/peek-ai/blob/main/.github/workflows/codeql.yml"

    %% Styles
    classDef main fill:#D8BFD8,stroke:#8B008B,stroke-width:1px
    classDef renderer fill:#ADD8E6,stroke:#00008B,stroke-width:1px
    classDef external fill:#98FB98,stroke:#006400,stroke-width:1px
    classDef storage fill:#D3D3D3,stroke:#696969,stroke-width:1px
```
