How naeha-s/chatbridge works
Project type and purpose
• ChatBridge is a Chrome extension (Manifest V3) that “bridges” AI conversations across multiple web platforms (ChatGPT, Claude, Gemini, Copilot, etc.). It scans chat DOMs, uses adapter modules to extract messages, invokes AI services for transformation (summaries, rewrites, translations), and maintains a local “connected memory graph” of insights.
File structure
• Top-level manifest.json declares extension permissions, content scripts, background service worker, and UI pages.
• content_script.js (and BACKUP) plus adapters.js implement page-scanning logic, platform-specific extractors.
• background.js (service worker) handles long-running tasks: API calls (ai_client.js), chunking/processing (aiUtils.js, ragEngine.js), error fixing (auto_fix.js, fix_errors.js), bridge logic (mcpBridge.js).
• storage.js wraps Chrome Storage API + LocalStorage fallback.
• popup.html/js, sidebar.html/js, options.html/js, summarize_tab.html/js are the front-end UI surfaces.
• documentation/ holds deep-dive architecture, API reference, developer guide, features, security, troubleshooting.
• Scripts like restore_mvp.ps1 and .github configuration are ancillary.
README for additional insights
• Describes adapter • observer • strategy • singleton patterns.
• Outlines core features: extraction, tone syncing, summarization, rewriting, translation, memory graph, contradiction tracking, multi-hop discovery, smart query, export/import.
• Tech stack: vanilla JS, Canvas, Shadow DOM, Chrome Storage API, Google Gemini API, parallel processing pipelines.
Main components
a. Content Script & Adapter Layer
• content_script.js + adapters.js scan page DOM, filter UI elements, distinguish user/assistant messages.
b. Background Processing & AI Bridge
• background.js coordinates tasks; invokes ai_client.js (API wrappers), aiUtils.js (chunking, retries), ragEngine.js (memory graph), mcpBridge.js (cross-context memory).
c. Storage Layer
• storage.js abstracts Chrome Storage API & LocalStorage fallback; persists conversations, memory graph, settings.
d. UI Components
• Popup (popup.html/js), Sidebar (sidebar.html/js), Summarize Tab (summarize_tab.html/js), Options (options.html/js) provide user entry points to functions.
e. External Services
• Google Gemini API (and other AI model endpoints via ai_client.js), optionally user-provided keys.
f. Static Assets & Config
Relationships and interactions
• Popup asks content script to “scan chat” → content script uses adapters to extract messages → sends extracted payload to background.
• Background invokes AI services (via ai_client) → processes responses (aiUtils, auto_fix, fix_errors) → stores results in storage.
• Sidebar UI listens for storage changes (Observer pattern) → retrieves data (memory graph, summaries) → renders via Canvas.
• Summarize Tab UI similarly requests background for summaries.
• Options UI reads/writes settings to storage (Singleton pattern for config).
• Background monitors storage changes to maintain memory graph and contradiction tracking (Observer pattern).
• Export/Import UI calls background to read/write JSON snapshots.
Architectural patterns and design principles
• Adapter pattern: adapters.js encapsulates platform-specific DOM parsing.
• Observer pattern: storage events notify UIs (sidebar, popup) of state changes.
• Strategy pattern: aiUtils and ragEngine select summarization, translation, rewriting strategies.
• Singleton pattern: storage.js, ai_client.js hold single instances of storage and API clients.
Crucial details to capture
• manifest.json roles: permissions, background script, content script matches.
• Extraction adapters per platform.
• Parallel summarization pipeline (aiUtils + Promise.all).
• Memory graph engine (ragEngine).
• Retry & fallback logic (auto_fix, fix_errors).
• Local-first, encrypted API key storage.
• UI isolation via Shadow DOM.