chat-relay/docs/provider-chatgpt.md
BinaryBeastMaster 25abcd2974 Initial commit
2025-05-10 15:09:23 -07:00

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


🔄 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

B. DOM Mode


🛡️ 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.