Merge branch 'fix-text-encoding-clean' of https://github.com/MasuRii/chat-relay into MasuRii-fix-text-encoding-clean

This commit is contained in:
BinaryBeastMaster 2025-07-07 20:12:55 -07:00
commit 0b6f9798cc
2 changed files with 16 additions and 6 deletions

View file

@ -611,7 +611,13 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
}
} else { // No error from content.js, proceed to send data
try {
const responseText = message.text || "";
let responseText = message.text || "";
// Decode text if it was encoded by content script
if (message.encoded) {
responseText = decodeURIComponent(responseText);
}
console.log(BG_LOG_PREFIX, `Attempting to send FINAL CHAT_RESPONSE_CHUNK for requestId ${message.requestId}. Data length: ${responseText.length}`);
relaySocket.send(JSON.stringify({
type: "CHAT_RESPONSE_CHUNK",

View file

@ -813,22 +813,26 @@ function handleProviderResponse(requestId, responseText, isFinal) {
const MAX_RESPONSE_TEXT_LENGTH = 500 * 1024; // 500KB limit for safety
let messageToSendToBackground;
// Encode special Unicode characters before transmission
const encodedText = responseText ? encodeURIComponent(responseText) : "";
if (responseText && typeof responseText === 'string' && responseText.length > MAX_RESPONSE_TEXT_LENGTH) {
console.warn(CS_LOG_PREFIX, `ResponseText for requestId ${requestId} is too large (${responseText.length} bytes). Sending error and truncated text.`);
messageToSendToBackground = {
type: "FINAL_RESPONSE_TO_RELAY",
requestId: requestId,
error: `Response too large to transmit (length: ${responseText.length}). Check content script logs for truncated version.`,
// text: responseText.substring(0, MAX_RESPONSE_TEXT_LENGTH) + "... (truncated by content.js)", // Optionally send truncated
text: `Error: Response too large (length: ${responseText.length}). See AI Studio for full response.`, // Simpler error text
isFinal: true // This is a final error state
text: `Error: Response too large (length: ${responseText.length}). See AI Studio for full response.`,
isFinal: true,
encoded: true
};
} else {
messageToSendToBackground = {
type: "FINAL_RESPONSE_TO_RELAY",
requestId: requestId,
text: responseText, // Can be null if AIStudioProvider parsed it as such
isFinal: isFinal
text: encodedText,
isFinal: isFinal,
encoded: true
};
}