# nthan2005/promptoptimizer architecture

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

- Interactive diagram: https://gitdiagram.com/nthan2005/promptoptimizer
- Repository: https://github.com/nthan2005/promptoptimizer

## Overview

##### Project Type and Purpose
- Type: Browser extension (Chrome/Edge) built on Manifest V3.
- Purpose: Intercept or accept user-entered prompts, send them to ChatGPT for optimization, return optimized prompts to the user—all while respecting privacy and personalization.

##### Main Architectural Components
a. Manifest V3 Configuration
- manifest.json (not shown in file tree but implicit to any MV3 extension).
b. UI Layer
- Optional Options Page (for saving user preferences).
c. Content Script
- Injected into web pages to detect prompt fields or add “Optimize” buttons inline.
d. Background Service Worker
- Central event hub in MV3: listens for UI events, content-script messages, alarm/timers.
- Manages API calls to the ChatGPT endpoint.
- Handles chrome.storage (user tokens, preferences, history).
e. External Service
- OpenAI ChatGPT API (HTTPS).
f. Storage
- chrome.storage.local or chrome.storage.sync for personalization data, API key, and usage logs.

##### Interactions and Data Flow
- Service worker receives prompt, retrieves stored API key/credentials, issues HTTPS request to ChatGPT optimization endpoint.
- ChatGPT responds with an optimized prompt; service worker stores the result (optionally) and forwards it to the popup.
- Popup displays optimized prompt; user can copy or replace the original.
- If a content script is used, the popup or background can send a message to the content script to inject results directly into the page’s input fields.

##### Architectural Patterns and Principles
- Event-Driven Architecture: Chrome extension APIs use message passing (publish/subscribe).
- Separation of Concerns: UI logic (popup), background orchestration (service worker), and DOM injection (content script) are in separate modules.
- Privacy-First: No data saved on external servers; all data flows directly between client and ChatGPT API; local storage is encrypted/per-user.

##### Technologies and Frameworks
- Chrome/Edge MV3 Extension APIs (manifest.json, chrome.runtime, chrome.storage, chrome.action).
- JavaScript or TypeScript for popup, background, content scripts.
- Fetch or Axios for HTTP calls to the OpenAI API.
- Optional bundler (Webpack/Rollup) if using ES modules or TS.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Browser-hosted modules
    subgraph "Browser Extension Modules"
        style Manifest fill:#D0E8FF,stroke:#0366D6
        Manifest["Manifest (manifest.json)"]:::config
        Popup["Popup UI"]:::ui
        Options["Options Page"]:::ui
        ContentScript["Content Script"]:::content
        ServiceWorker["Background Service Worker"]:::background
    end

    %% Storage
    Storage[(chrome.storage)]:::storage

    %% External Service
    ChatGPTCloud["OpenAI ChatGPT API"]:::external

    %% Documentation
    Docs["Project Documentation"]:::docs

    %% Connections
    Manifest -->|"binds & declares permissions"|ServiceWorker
    Manifest -->|declares UI pages|Popup
    Manifest -->|declares UI pages|Options

    Popup -->|"optimizeRequest(prompt)"|ServiceWorker
    ServiceWorker -->|"get/set userPrefs, apiKey"|Storage
    ServiceWorker -->|"HTTP POST /optimize"|ChatGPTCloud
    ChatGPTCloud -->|"optimizedPrompt"|ServiceWorker
    ServiceWorker -->|"optimizedPrompt"|Popup
    ServiceWorker -->|"injectPrompt(optimizedPrompt)"|ContentScript

    %% Documentation link
    click Docs "https://github.com/nthan2005/promptoptimizer/blob/main/README.md"

    %% Styles
    classDef config fill:#D0E8FF,stroke:#0366D6,color:#000
    classDef ui fill:#B3DCEF,stroke:#0366D6,color:#000
    classDef background fill:#A3C9F1,stroke:#024A92,color:#000
    classDef content fill:#B3DCEF,stroke:#0366D6,color:#000
    classDef storage fill:#DFF2BF,stroke:#4F8A10,color:#000
    classDef external fill:#FFE5B4,stroke:#CC8400,color:#000
    classDef docs fill:#F0F0F0,stroke:#999999,color:#000,stroke-dasharray: 2 2

    %% Legend
    subgraph Legend
        L1["■ In-Browser Modules"]:::ui
        L2["● Storage"]:::storage
        L3["☁ External Service"]:::external
        L4["- Documentation"]:::docs
    end
```
