From 3cb1fb0972b3e7c549c6aa1aeefaa5b4c26f0398 Mon Sep 17 00:00:00 2001 From: mic Date: Wed, 25 Mar 2026 23:43:48 +0100 Subject: [PATCH] correctly using the ai webchat if the relative option is configured accordingly. see #580 --- _locales/en/messages.json | 10 ++--- api_webchat/messagesArea.js | 25 ++++++++++- js/mzta-prompts.js | 10 +++++ mzta-background.js | 60 ++++++++++++++++++++++++--- pages/spamfilter/mzta-spamfilter.html | 3 +- 5 files changed, 95 insertions(+), 13 deletions(-) diff --git a/_locales/en/messages.json b/_locales/en/messages.json index 7f28055b..620a03a0 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -235,6 +235,10 @@ "message": "Close", "description": "" }, + "webchat_save_as_summary": { + "message": "Save as Summary", + "description": "Button label in the webchat window to save the AI response as a message summary" + }, "chatgpt_textarea_not_found_error": { "message": "It seems that the ChatGPT page is taking too long to load. If it finishes loading, click the button on the right. If the problem persists, please check the service status.", "description": "" @@ -1388,17 +1392,13 @@ "description": "" }, "spamfilter_no_reports": { - "message": "No messages have screened for spam yet. Here you'll find a list of the last 100 spam reports for the current session only.", + "message": "No messages have screened for spam yet. Here you'll find a list of the last 100 spam reports.", "description": "" }, "SpamReport_Title": { "message": "Spam Filter Reports", "description": "" }, - "SpamReport_infoline": { - "message": "This information is saved only for the current session.", - "description": "" - }, "Date": { "message": "Date", "description": "" diff --git a/api_webchat/messagesArea.js b/api_webchat/messagesArea.js index 5d9b5bc4..0b2f2d7c 100644 --- a/api_webchat/messagesArea.js +++ b/api_webchat/messagesArea.js @@ -449,11 +449,34 @@ class MessagesArea extends HTMLElement { closeButton.addEventListener('click', async () => { browser.runtime.sendMessage({command: "chatgpt_close", window_id: (await browser.windows.getCurrent()).id}); // close window }); - if(promptData.action != 0) { + if(promptData.action != 0) { actionButtons.appendChild(splitButton); selectionInfo.style.display = "block"; // show selection info } + // Save as Summary button (only shown for summary webchat sessions) + if(promptData.prompt_info?.headerMessageId) { + const saveSummaryButton = document.createElement('button'); + saveSummaryButton.textContent = browser.i18n.getMessage("webchat_save_as_summary"); + saveSummaryButton.classList.add('action_btn'); + saveSummaryButton.addEventListener('click', async () => { + let finalText = removeAloneBRs(fullTextHTMLAtAssignment); + const selectedHTML = this.getCurrentSelectionHTML(); + if(selectedHTML != "") { + finalText = removeAloneBRs(selectedHTML); + } + await browser.runtime.sendMessage({ + command: "chatgpt_saveSummary", + text: finalText, + headerMessageId: promptData.prompt_info.headerMessageId, + tabId: promptData.prompt_info.summaryTabId || promptData.tabId, + }); + browser.runtime.sendMessage({command: "chatgpt_close", window_id: (await browser.windows.getCurrent()).id}); + }); + actionButtons.appendChild(saveSummaryButton); + selectionInfo.style.display = "block"; + } + // diff viewer button if(promptData.prompt_info?.use_diff_viewer == "1") { const diffvButton = document.createElement('button'); diff --git a/js/mzta-prompts.js b/js/mzta-prompts.js index 4a73f20a..d7603ff1 100644 --- a/js/mzta-prompts.js +++ b/js/mzta-prompts.js @@ -52,6 +52,16 @@ 0: Do not use the diff viewer 1: Use the diff viewer + ================ DYNAMIC PROPERTIES (set at runtime via prompt_info) + + headerMessageId (set by _openSummaryWebchat in mzta-background.js): + When present, the webchat UI shows a "Save as Summary" button to capture + the AI response and save it as an inline summary for the message identified + by this headerMessageId. + + summaryTabId (set by _openSummaryWebchat in mzta-background.js): + The tab ID of the message display tab to update with the saved summary. + ================ USER PROPERTIES Enabled (enabled attribute): 0: Disabled diff --git a/mzta-background.js b/mzta-background.js index 49cc9cb8..07961049 100644 --- a/mzta-background.js +++ b/mzta-background.js @@ -282,13 +282,45 @@ messenger.runtime.onMessage.addListener((message, sender, sendResponse) => { async function _refreshSummary(message) { let tabId = sender.tab.id; await summaryStore.removeSummary(message.headerMessageId); - await _generateSummaryForMessage(message.headerMessageId, tabId); + let prefs_refresh = await browser.storage.sync.get({ summarize_display_mode: prefs_default.summarize_display_mode }); + if (prefs_refresh.summarize_display_mode === 'webchat') { + await _openSummaryWebchat(message.headerMessageId, tabId); + } else { + await _generateSummaryForMessage(message.headerMessageId, tabId); + } } _refreshSummary(message); break; case 'removeSummary': summaryStore.removeSummary(message.headerMessageId); break; + case 'chatgpt_saveSummary': + async function _saveSummaryFromWebchat(msg) { + try { + let cleanedSummary = cleanSummaryText(msg.text); + const summaryData = { + summary: cleanedSummary, + summary_date: new Date(), + headerMessageId: msg.headerMessageId + }; + await summaryStore.saveSummary(summaryData, msg.headerMessageId); + let prefs_summary = await browser.storage.sync.get({ + summarize_max_display_length: prefs_default.summarize_max_display_length + }); + try { + browser.tabs.sendMessage(msg.tabId, { + command: "showSummary", + data: { ...summaryData, maxDisplayLength: prefs_summary.summarize_max_display_length } + }); + } catch (e) { + taLog.error("Error sending showSummary to tab: " + e); + } + } catch (error) { + console.error("[ThunderAI] Error saving summary from webchat:", error); + } + } + _saveSummaryFromWebchat(message); + break; // case 'chatgpt_open': // openChatGPT(message.prompt,message.action,message.tabId); // return true; @@ -458,6 +490,17 @@ messenger.runtime.onMessage.addListener((message, sender, sendResponse) => { return false; }); +// Clean summary text by stripping HTML, markdown, and formatting artifacts. +// Used by both inline summary generation and webchat summary save. +function cleanSummaryText(text) { + let cleaned = text.replace(/<\/?[^>]+(>|$)/g, ''); // strip HTML tags + cleaned = cleaned.replace(/```[\s\S]*?```/g, ''); + cleaned = cleaned.replace(/[\*#_~`]/g, ''); + cleaned = cleaned.replace(/\s+/g, ' ').trim(); + cleaned = cleaned.replace(/^Summary:\s*/i, ''); + return cleaned; +} + async function _generateSummaryForMessage(headerMessageId, tabId) { try { let prefs = await browser.storage.sync.get({ @@ -514,10 +557,7 @@ async function _generateSummaryForMessage(headerMessageId, tabId) { await cmd.initWorker(); const aiResponse = await cmd.sendPrompt(); - let cleanedSummary = aiResponse.replace(/```[\s\S]*?```/g, ''); - cleanedSummary = cleanedSummary.replace(/[\*#_~`]/g, ''); - cleanedSummary = cleanedSummary.replace(/\s+/g, ' ').trim(); - cleanedSummary = cleanedSummary.replace(/^Summary:\s*/i, ''); + let cleanedSummary = cleanSummaryText(aiResponse); const summaryData = { summary: cleanedSummary, @@ -665,7 +705,17 @@ async function _openSummaryWebchat(headerMessageId, tabId) { const curr_message = messageResult.messages[0]; const curr_message_full = await browser.messages.getFull(curr_message.id); + const connectionType = getConnectionType(await browser.storage.sync.get(prefs_default), {}, 'summarize'); + if (connectionType === 'chatgpt_web') { + const errorMsg = browser.i18n.getMessage('summarize_chatgpt_web_not_supported'); + await summaryStore.saveError(headerMessageId, errorMsg); + browser.tabs.sendMessage(tabId, { command: "showSummary", data: { error: true, message: errorMsg } }); + return; + } + const { promptText, promptInfo } = await taPromptUtils.buildSummaryPrompt([{ message: curr_message, fullMessage: curr_message_full }]); + promptInfo.headerMessageId = headerMessageId; + promptInfo.summaryTabId = tabId; openChatGPT(promptText, promptInfo.action, tabId, promptInfo.name, promptInfo.need_custom_text, promptInfo); } catch (error) { diff --git a/pages/spamfilter/mzta-spamfilter.html b/pages/spamfilter/mzta-spamfilter.html index 4576314e..fb232647 100644 --- a/pages/spamfilter/mzta-spamfilter.html +++ b/pages/spamfilter/mzta-spamfilter.html @@ -66,8 +66,7 @@
-
__MSG_SpamReport_Title__ -
__MSG_SpamReport_infoline__
+
__MSG_SpamReport_Title__