mirror of
https://github.com/BinaryBeastMaster/chat-relay.git
synced 2026-04-28 11:49:29 +00:00
2.9 KiB
2.9 KiB
ChatGPT Provider Architecture
This document describes the implementation details of the ChatGptProvider class used to automate and capture interactions with chatgpt.com.
🧩 Overview
ChatGptProvider enables message injection, UI automation, and response capture from ChatGPT via DOM or Chrome Debugger methods. It uses retry logic for robust message delivery and streaming capture for response chunks.
⚙️ Configurable Parameters
this.captureMethod = "debugger"; // or "dom"
this.debuggerUrlPattern = "*chatgpt.com/backend-api/conversation*";
this.includeThinkingInMessage = true;
These control the response source (network or UI) and message formatting behavior.
📌 DOM Elements
- Input:
#prompt-textarea - Send button:
#composer-submit-button - Response area:
.message-bubble .text-content - Loading spinner:
.loading-spinner - Fallback DOM:
.message-container .response-text
🔄 Lifecycle
1. Initialization
- Sets up DOM selectors and state containers
- Initializes request tracking maps and debug logs
2. Sending Messages
sendChatMessage():- Finds input + button, injects message
- Retries on failure (up to 5 times)
- Waits between attempts and checks element readiness
3. Capturing Responses
A. Debugger Mode
- Callback registration via
initiateResponseCapture() - Processes data in
handleDebuggerData() - Uses accumulator map for text chunk assembly and
parseDebuggerResponse()to interpret SSE stream format
B. DOM Mode
- Starts DOM observer loop via
_startDOMMonitoring() - Stops when
_isResponseStillGeneratingDOM()returns false
🛡️ Error Handling
_reportSendError()reports issues back to callback- Handles:
- Missing input or button
- Disabled controls
- Empty raw debugger data
- Unparseable or non-relevant JSON payloads
🧠 Streaming SSE Parse Logic
sequenceDiagram
participant Debugger
participant Provider
participant ContentScript
Debugger-->>Provider: data: { chunk }
Provider->>Provider: parseDebuggerResponse()
Provider-->>ContentScript: Accumulated text + isFinal
✅ Summary
The ChatGPT provider is designed for robust interaction with ChatGPT's UI or network. It supports retries, error recovery, and chunked response reconstruction, ensuring high reliability and compatibility across updates to the site's frontend.