Skip to content

Fix text encoding issues #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions extension/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,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",
Expand Down Expand Up @@ -996,7 +1002,7 @@ chrome.debugger.onEvent.addListener((debuggeeId, message, params) => {
}
console.log(BG_LOG_PREFIX, `Raw responseBodyData for debugger requestId ${params.requestId} (first 200 chars):`, JSON.stringify(responseBodyData).substring(0, 200) + "...");

const rawBodyText = responseBodyData.base64Encoded ? atob(responseBodyData.body) : responseBodyData.body;
const rawBodyText = responseBodyData.base64Encoded ? new TextDecoder('utf-8').decode(Uint8Array.from(atob(responseBodyData.body), c => c.charCodeAt(0))) : responseBodyData.body;

if (rawBodyText === undefined || rawBodyText === null) {
console.error(BG_LOG_PREFIX, `Extracted rawBodyText is undefined or null for debugger requestId ${params.requestId}.`);
Expand Down
14 changes: 9 additions & 5 deletions extension/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -812,22 +812,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
};
}

Expand Down