2.1 KiB
ChatGPT WebSocket Capture Implementation Plan
This document outlines the plan to implement WebSocket-based response capture for the ChatGPT provider in the Chat Relay browser extension.
1. Project Goals
- Implement a new method for capturing ChatGPT responses using WebSockets, as an alternative to the current debugger-based approach.
- Allow users to configure the capture method (debugger or WebSocket) on a per-provider basis through the extension's UI.
- Ensure the new WebSocket method supports streaming responses back to the API relay server in an OpenAI-compatible format.
- Refactor the existing
ChatGptProviderto be modular and easily support both capture methods.
2. Technical Plan
The implementation will be broken down into the following steps:
-
Analyze
extension/manifest.jsonandextension/background.js: Determine the best approach for injecting a new content script that will proxy thewindow.WebSocketobject. -
Create a WebSocket Proxy Content Script: This script will be responsible for:
- Replacing the page's native
WebSocketobject with a proxy. - Intercepting calls to the
WebSocketconstructor to identify the connection to ChatGPT's backend. - Listening for
onmessageevents on the proxied WebSocket to capture incoming data.
- Replacing the page's native
-
Forward Captured Data: The content script will forward the captured WebSocket messages to the background script for processing by the
ChatGptProvider. -
Add Configuration UI: A new section will be added to the extension's options page to allow users to select the desired capture method for each provider that supports multiple methods.
-
Refactor
ChatGptProvider: The provider will be updated to:- Read the configured capture method.
- Dynamically switch between the existing debugger-based logic and the new WebSocket-based logic.
- Process the incoming WebSocket data stream and send it back to the API relay server in chunks to support streaming.
-
Testing: Both the new WebSocket method and the existing debugger method will be thoroughly tested to ensure they function correctly and that the configuration UI works as expected.