# mem0ai/mem0-chrome-extension architecture

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

- Interactive diagram: https://gitdiagram.com/mem0ai/mem0-chrome-extension
- Repository: https://github.com/mem0ai/mem0-chrome-extension

## Overview

##### Project type and purpose
- This is a browser (Chrome) extension that injects code into multiple AI-assistant web pages (ChatGPT, Claude, Perplexity, etc.) to capture, store, and replay conversational context via the Mem0 API.
- Its main goal is to provide “universal memory” across different LLM chat interfaces.

##### High-level architecture and key components
a. Chrome Extension Host
– manifest.json: declares permissions, scripts, UI entry points.

b. Background Module
– background.js: central event handler (message routing, API calls, storage sync).

c. Content-Script Modules
– chatgpt/content.js, claude/content.js, perplexity/content.js, etc.: site-specific injectors that
- listen to page events (message sends, UI clicks)
- extract conversational context
- relay messages to the background script

d. UI Modules
– popup.html + popup.js: “one-click sync” and configuration interface.
– sidebar.js: in-page sidebar interface for memory dashboard.

e. Data Storage
– chrome.storage.local (or sync): caches user API key, user ID, cached memories.

f. External Services
– Mem0 REST API: endpoints for storing/retrieving memories.
– Google OAuth (sign-in) endpoint.

##### Relationships and data flows
- Content Script ↔ Background
– Uses chrome.runtime.sendMessage / onMessage listeners.
– Flow: user types → content script captures → sends “storeMemory” to background → background calls Mem0 API → response → content script injects suggestions.

- UI ↔ Background
– Popup or sidebar invokes background API (chrome.runtime.sendMessage) to perform actions (fetch all stored memories, update API key).

- Background ↔ Mem0 API
– HTTP(S) requests (fetch/XHR) using stored API key and user ID.
– Handles JSON payloads for memory extract/retrieve.

- Background ↔ chrome.storage
– Persists API credentials and optionally caches recent memories.

##### Architectural patterns and design principles
- Event-driven messaging: loose coupling between injectors, UI, and background.
- Single-source-of-truth: background script orchestrates all external calls and storage.
- Modular content scripts: one per supported assistant, share common messaging interface.
- Separation of concerns:
– Content scripts only handle DOM events and message passing.
– Background handles business logic & network.
– Popup/sidebar handle user interactions & configuration.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Extension Host
    subgraph "Chrome Extension Host"
        manifest["manifest.json"]:::config
        icons_dir["icons/"]:::asset
    end

    %% Background
    background["Background Script (background.js)"]:::logic

    %% Content Scripts
    subgraph "Content Scripts (Injectors)" 
        chatgpt["ChatGPT Injector (chatgpt/content.js)"]:::injector
        claude["Claude Injector (claude/content.js)"]:::injector
        perplexity["Perplexity Injector (perplexity/content.js)"]:::injector
        deepseek["Deepseek Injector (deepseek/content.js)"]:::injector
        gemini["Gemini Injector (gemini/content.js)"]:::injector
        grok["Grok Injector (grok/content.js)"]:::injector
        mem0cs["Mem0 Self‐Injector (mem0/content.js)"]:::injector
        replit["Replit Injector (replit/content.js)"]:::injector
    end

    %% UI Modules
    subgraph "UI Modules"
        popupUI["Popup UI (popup.html)"]:::ui
        popupLogic["Popup Logic (popup.js)"]:::ui
        sidebar["Sidebar UI (sidebar.js)"]:::ui
    end

    %% Storage
    storage["Chrome Storage<br/>(chrome.storage)"]:::storage

    %% External Services
    subgraph "External Services"
        mem0api["Mem0 REST API"]:::external
        oauth["Google OAuth"]:::external
    end

    %% Connections
    manifest --> background
    manifest --> chatgpt
    manifest --> claude
    manifest --> perplexity
    manifest --> deepseek
    manifest --> gemini
    manifest --> grok
    manifest --> mem0cs
    manifest --> replit
    manifest --> popupUI

    chatgpt -->|storeMemory / retrieveMemory| background
    claude -->|storeMemory / retrieveMemory| background
    perplexity -->|storeMemory / retrieveMemory| background
    deepseek -->|storeMemory / retrieveMemory| background
    gemini -->|storeMemory / retrieveMemory| background
    grok -->|storeMemory / retrieveMemory| background
    mem0cs -->|storeMemory / retrieveMemory| background
    replit -->|storeMemory / retrieveMemory| background

    popupUI --> popupLogic
    popupLogic -->|getMemories / setAPIKey| background
    sidebar -->|getMemories / display| background

    background -->|get / set| storage
    background -->|HTTP POST / GET| mem0api
    background -->|OAuth Flow| oauth
    background -->|inject suggestions| chatgpt
    background -->|inject suggestions| claude
    background -->|inject suggestions| perplexity
    background -->|inject suggestions| deepseek
    background -->|inject suggestions| gemini
    background -->|inject suggestions| grok
    background -->|inject suggestions| mem0cs
    background -->|inject suggestions| replit

    %% Click Events
    click manifest "https://github.com/mem0ai/mem0-chrome-extension/blob/main/manifest.json"
    click icons_dir "https://github.com/mem0ai/mem0-chrome-extension/tree/main/icons/"
    click background "https://github.com/mem0ai/mem0-chrome-extension/blob/main/background.js"
    click chatgpt "https://github.com/mem0ai/mem0-chrome-extension/blob/main/chatgpt/content.js"
    click claude "https://github.com/mem0ai/mem0-chrome-extension/blob/main/claude/content.js"
    click perplexity "https://github.com/mem0ai/mem0-chrome-extension/blob/main/perplexity/content.js"
    click deepseek "https://github.com/mem0ai/mem0-chrome-extension/blob/main/deepseek/content.js"
    click gemini "https://github.com/mem0ai/mem0-chrome-extension/blob/main/gemini/content.js"
    click grok "https://github.com/mem0ai/mem0-chrome-extension/blob/main/grok/content.js"
    click mem0cs "https://github.com/mem0ai/mem0-chrome-extension/blob/main/mem0/content.js"
    click replit "https://github.com/mem0ai/mem0-chrome-extension/blob/main/replit/content.js"
    click popupUI "https://github.com/mem0ai/mem0-chrome-extension/blob/main/popup.html"
    click popupLogic "https://github.com/mem0ai/mem0-chrome-extension/blob/main/popup.js"
    click sidebar "https://github.com/mem0ai/mem0-chrome-extension/blob/main/sidebar.js"
    click README_md "https://github.com/mem0ai/mem0-chrome-extension/blob/main/README.md"
    click privacy "https://github.com/mem0ai/mem0-chrome-extension/blob/main/privacy-policy.md"
    click license "https://github.com/mem0ai/mem0-chrome-extension/tree/main/LICENSE"

    %% Styles
    classDef ui fill:#2196F3,stroke:#000,color:#fff
    classDef logic fill:#4CAF50,stroke:#000,color:#000
    classDef injector fill:#FF9800,stroke:#000,color:#000
    classDef external fill:#9C27B0,stroke:#000,color:#fff
    classDef storage fill:#BDBDBD,stroke:#000,color:#000
    classDef config fill:#FFEB3B,stroke:#000,color:#000
    classDef asset fill:#9E9E9E,stroke:#000,color:#000
```
