mirror of
https://github.com/BinaryBeastMaster/chat-relay.git
synced 2026-04-28 03:39:27 +00:00
4.5 KiB
4.5 KiB
Claude Provider (claude.js)
The ClaudeProvider is designed to interface with Anthropic's Claude AI models via the claude.ai web interface. It primarily uses the Chrome DevTools Debugger API to intercept and process Server-Sent Events (SSE) for streaming responses.
Key Features
- Supported Domain:
claude.ai - Capture Method:
debugger(primary)- Intercepts network responses matching the
debuggerUrlPattern. - Parses Server-Sent Events (SSE) to extract message content.
- Intercepts network responses matching the
- DOM Fallback: Basic DOM capture logic exists but is secondary to the debugger method.
- Stream Handling:
- Accumulates text from
content_block_deltaSSE events. - Identifies the end of a message by detecting
message_stopevents ormessage_deltaevents with astop_reason. - When
includeThinkingInMessageisfalse(default), it sends the complete, accumulated message once the stream indicates completion.
- Accumulates text from
Configuration Properties
Located at the beginning of the ClaudeProvider class in extension/providers/claude.js:
this.captureMethod: (String) Set to"debugger"for SSE interception. Can be set to"dom"for DOM-based capture (less reliable for streaming).this.debuggerUrlPattern: (String) URL pattern used by the debugger to identify Claude's response stream. Currently set to"*\/completion*".this.includeThinkingInMessage: (Boolean) Iftrue, the provider attempts to include intermediate "thinking" steps (not fully implemented/tested for Claude's SSE structure). Defaults tofalse, focusing on the final answer.this.ENABLE_CLAUDE_FUNCTION_CALLING: (Boolean) Intended for future use if Claude exposes a UI toggle for function calling. Currently, the related code is commented out as no such toggle is present.
DOM Selectors
These selectors are used to interact with the Claude web interface:
this.inputSelector:'div.ProseMirror[contenteditable="true"]'(The main chat input field)this.sendButtonSelector:'button[aria-label="Send message"]'(The button to send a message)this.responseSelector: A general selector for identifying response areas, primarily for DOM fallback (.response-container, .response-text, .model-response, ...).this.thinkingIndicatorSelector: Selectors for loading/thinking indicators, primarily for DOM fallback.
Debugger Stream Parsing (parseDebuggerResponse)
This method is crucial for handling the SSE stream from Claude:
- Splits Chunks: Each raw data chunk from the debugger can contain multiple SSE messages (e.g.,
event: ...\ndata: ...\n\n). The method splits these. - Event Extraction: For each SSE message, it extracts the
event:type anddata:payload. - Text Accumulation:
- If
event: content_block_deltaanddata.delta.type === "text_delta", thedata.delta.textis appended to the current chunk's text.
- If
- End-of-Message Detection:
- If
event: message_stopis encountered, the message is considered complete. - If
event: message_deltaand thedata.delta.stop_reasonfield is present, the message is considered complete.
- If
- Output: Returns an object
{ text: "accumulated_text_from_this_chunk", isFinalResponse: true/false }.
Message Handling (handleDebuggerData)
- Buffering: Uses
this.requestBuffersto accumulate text for eachrequestIdacross multiple data chunks. - Callback Invocation:
- If
includeThinkingInMessageisfalse:- The main
responseCallback(which sends data back to the extension's background script) is only called with the fully accumulated text whenparseDebuggerResponseindicatesisFinalResponse: truefor a chunk, OR when the background script signals that this is the absolute final chunk from the debugger (isFinalFromBackground: true).
- The main
- If
includeThinkingInMessageistrue(not the current default):- It would send intermediate text chunks.
- If
Known Issues & Considerations
debuggerUrlPatternSpecificity: The accuracy ofthis.debuggerUrlPatternis critical. It must precisely match the URL endpoint from which Claude serves its chat responses. The current value is"*\/completion*".- Function Calling: The
ensureFunctionCallingEnabledlogic is currently commented out as there is no visible UI toggle for this feature onclaude.ai. - Error Handling: Basic error handling is in place, but complex network error scenarios or unexpected API changes from Claude might require more robust handling.