# talgaon4/promptimzer architecture

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

- Interactive diagram: https://gitdiagram.com/talgaon4/promptimzer
- Repository: https://github.com/talgaon4/promptimzer

## Overview

##### Project type and purpose
- This is a Chrome extension (“Promptimizer”) that provides AI‐based prompt optimization for chat interfaces (ChatGPT, Claude, Gemini).
- It consists of a service worker (background.js), a content script (content_script.js), a popup UI, an options/settings page, and a local “prompts” template library.

##### File structure
- Top‐level files:
- manifest.json – extension metadata and permission declarations
- background.js – service worker handling API calls, classification, optimization logic, storage
- content_script.js – injects “Optimize Prompt” button into supported chat pages and relays user actions to background
- popup.html/js – quick‐access UI to input a prompt and get optimization
- options.html/js/css – settings page for entering API key and max tokens
- styles.css, options.css – shared and page‐specific styling
- prompts/*.txt – template files for different prompt categories
- No backend server: all logic runs client‐side, with remote calls only to OpenAI API.

##### README for additional insights
- Two‐step process: classification → optimization
- Privacy focus: API key and data are stored locally using Chrome storage
- Rate limiting and token‐limit controls

##### Diagramming the architecture
a. Main components
- Chrome Browser Extension Host
- Background Service Worker (background.js)
- Content Script (content_script.js)
- Popup UI (popup.html/js)
- Options/Settings Page (options.html/js/css)
- Local Storage (Chrome Storage API)
- OpenAI API (external service)
- Prompt Templates (prompts/*.txt)

b. Relationships and interactions
1. User clicks “Optimize Prompt” in injected UI (content script) or popup
2. Content script or popup sends a message to background.js via chrome.runtime.sendMessage
3. Background.js:
– Loads classification template (classifier.txt) from prompts/
– Classifies input prompt
– Loads category‐specific template (e.g. complex.txt)
– Calls OpenAI API with user’s API key and combined prompt+template
– Receives optimized text
– Sends response back to content script or popup
4. UI component updates the text field or clipboard
5. Options page reads/writes API key and token limit to Chrome Storage

c. Architectural patterns and principles
- Event‐driven by Chrome Extension messaging
- Single Responsibility: UI logic ↔ background logic ↔ storage
- Privacy by design: local storage only, no external servers
- Pluggable templates: file‐based strategy for extensibility

d. Key technologies and libraries
- JavaScript for all logic
- Chrome Extensions APIs: runtime, storage, tabs, permissions
- HTML/CSS for popup and options pages
- OpenAI REST API for prompt optimization

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% UI Layer
    subgraph "UI Layer"
        CS["Content Script"]:::ui
        subgraph "Popup UI"
            PHTML["popup.html"]:::ui
            PJS["popup.js"]:::ui
            PCSS["styles.css"]:::ui
        end
        subgraph "Options Page"
            OHTML["options.html"]:::ui
            OJS["options.js"]:::ui
            OCSS["options.css"]:::ui
        end
    end

    %% Core Logic
    subgraph "Core Logic"
        BG["Background Service Worker"]:::core
    end

    %% Data Layer
    subgraph "Data Layer"
        STORAGE["Chrome Storage"]:::data
        TEMPLATES["Prompt Templates"]:::data
    end

    %% External API
    subgraph "External API"
        API["OpenAI API"]:::external
    end

    %% Config
    subgraph "Extension Config"
        MANIFEST["Manifest (manifest.json)"]:::config
    end

    %% Connections
    CS -->|"sendMessage"| BG
    PJS -->|"sendMessage"| BG
    BG -->|"response"| CS
    BG -->|"response"| PJS

    OJS -->|"get/set API key, token limit"| STORAGE
    BG -->|"read/write settings"| STORAGE

    BG -->|"load templates"| TEMPLATES
    BG -->|"HTTP request"| API
    API -->|"response"| BG

    %% Click Events
    click MANIFEST "https://github.com/talgaon4/promptimzer/blob/main/manifest.json"
    click BG "https://github.com/talgaon4/promptimzer/blob/main/background.js"
    click CS "https://github.com/talgaon4/promptimzer/blob/main/content_script.js"
    click PHTML "https://github.com/talgaon4/promptimzer/blob/main/popup.html"
    click PJS "https://github.com/talgaon4/promptimzer/blob/main/popup.js"
    click PCSS "https://github.com/talgaon4/promptimzer/blob/main/styles.css"
    click OHTML "https://github.com/talgaon4/promptimzer/blob/main/options.html"
    click OJS "https://github.com/talgaon4/promptimzer/blob/main/options.js"
    click OCSS "https://github.com/talgaon4/promptimzer/blob/main/options.css"
    click TEMPLATES "https://github.com/talgaon4/promptimzer/tree/main/prompts/"

    %% Styles
    classDef ui fill:#ADD8E6,stroke:#333,stroke-width:1px;
    classDef core fill:#FFA500,stroke:#333,stroke-width:1px;
    classDef data fill:#90EE90,stroke:#333,stroke-width:1px;
    classDef external fill:#FF6347,stroke:#333,stroke-width:1px;
    classDef config fill:#D3D3D3,stroke:#333,stroke-width:1px;
```
