From ea22caa23c5dec11342d283041a45f7d56a387a9 Mon Sep 17 00:00:00 2001 From: mic Date: Sun, 25 May 2025 11:41:14 +0200 Subject: [PATCH 1/6] Refactor tag existence check to use label comparison and enhance logging for tag assignment. See #390 --- js/mzta-utils.js | 50 +++++++++++++++++++++++++++++++++++++++++++--- mzta-background.js | 6 +++--- 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/js/mzta-utils.js b/js/mzta-utils.js index 12904797..366cce44 100644 --- a/js/mzta-utils.js +++ b/js/mzta-utils.js @@ -350,14 +350,42 @@ export async function createTag(tag) { } } -export function checkIfTagExists(tag, tags_list) { - return tags_list.hasOwnProperty("$ta-" + sanitizeString(tag)); +// export function checkIfTagExists(tag, tags_list) { +// console.log(">>>>>>>>>>> checkIfTagExists tags_list: " + JSON.stringify(tags_list)); +// console.log(">>>>>>>>>>> checkIfTagExists tag: " + tag); +// return tags_list.hasOwnProperty("$ta-" + sanitizeString(tag)); +// } + +export function checkIfTagLabelExists(tag_label, tags_list) { + console.log(">>>>>>>>>>> checkIfTagExists tags_list: " + JSON.stringify(tags_list)); + console.log(">>>>>>>>>>> checkIfTagExists tag_label: " + tag_label); + const lowerTagLabel = tag_label.toLowerCase(); + return Object.values(tags_list).some(label => label.tag.toLowerCase() === lowerTagLabel); } +// export async function assignTagsToMessage(messageId, tags) { +// console.log(">>>>>>>>>>> assignTagsToMessage messageId: tags: " + JSON.stringify(tags)); +// tags = tags.map(tag => `$ta-${sanitizeString(tag)}`); +// let msg_prop = await browser.messages.get(messageId); +// tags = tags.concat(msg_prop.tags || []); +// try { +// return browser.messages.update(messageId, {tags: tags}); +// } catch (error) { +// console.error('[ThunderAI] Error assigning tag [messageId: ', messageId, ' - tag: ', tag, ']:', error); +// } +// } + export async function assignTagsToMessage(messageId, tags) { - tags = tags.map(tag => `$ta-${sanitizeString(tag)}`); + console.log(">>>>>>>>>>> assignTagsToMessage tags: " + JSON.stringify(tags)); + let all_tags_list = await getTagsList(); + all_tags_list = all_tags_list[1]; + tags = getTagsKeyFromLabel(tags, all_tags_list); + console.log(">>>>>>>>>>> assignTagsToMessage tags after conversion: " + JSON.stringify(tags)); let msg_prop = await browser.messages.get(messageId); + console.log(">>>>>>>>>>> assignTagsToMessage msg_prop.tags: " + JSON.stringify(msg_prop.tags)); tags = tags.concat(msg_prop.tags || []); + tags = [...new Set(tags)]; + console.log(">>>>>>>>>>> assignTagsToMessage tags after concat: " + JSON.stringify(tags)); try { return browser.messages.update(messageId, {tags: tags}); } catch (error) { @@ -365,6 +393,22 @@ export async function assignTagsToMessage(messageId, tags) { } } +function getTagsKeyFromLabel(tag_names, all_tags_list) { + const result = []; + + tag_names.forEach(name => { + const lowerName = name.toLowerCase(); + const match = Object.entries(all_tags_list).find( + ([, value]) => value.tag.toLowerCase() === lowerName + ); + if (match) { + result.push(match[0]); + } + }); + + return result; +} + function sanitizeString(input) { input = input.toLowerCase(); // Define the regex to match valid characters diff --git a/mzta-background.js b/mzta-background.js index 27b24601..2513ae37 100644 --- a/mzta-background.js +++ b/mzta-background.js @@ -20,7 +20,7 @@ import { mzta_script } from './js/mzta-chatgpt.js'; import { prefs_default } from './options/mzta-options-default.js'; import { mzta_Menus } from './js/mzta-menus.js'; import { taLogger } from './js/mzta-logger.js'; -import { getCurrentIdentity, getOriginalBody, replaceBody, setBody, i18nConditionalGet, generateCallID, migrateCustomPromptsStorage, migrateDefaultPromptsPropStorage, getGPTWebModelString, getTagsList, createTag, assignTagsToMessage, checkIfTagExists, getActiveSpecialPromptsIDs, checkSparksPresence, getMessages, getMailBody, extractJsonObject, contextMenuID_AddTags, contextMenuID_Spamfilter, sanitizeChatGPTModelData, sanitizeChatGPTWebCustomData, stripHtmlKeepLines } from './js/mzta-utils.js'; +import { getCurrentIdentity, getOriginalBody, replaceBody, setBody, i18nConditionalGet, generateCallID, migrateCustomPromptsStorage, migrateDefaultPromptsPropStorage, getGPTWebModelString, getTagsList, createTag, assignTagsToMessage, checkIfTagLabelExists, getActiveSpecialPromptsIDs, checkSparksPresence, getMessages, getMailBody, extractJsonObject, contextMenuID_AddTags, contextMenuID_Spamfilter, sanitizeChatGPTModelData, sanitizeChatGPTWebCustomData, stripHtmlKeepLines } from './js/mzta-utils.js'; import { taPromptUtils } from './js/mzta-utils-prompt.js'; import { mzta_specialCommand } from './js/mzta-special-commands.js'; import { getSpamFilterPrompt } from './js/mzta-prompts.js'; @@ -161,8 +161,8 @@ async function _assign_tags(_data, create_new_tags = true) { taLog.log("assign_tags data: " + JSON.stringify(_data)); let new_tags = []; for (const tag of _data.tags) { - // console.log(">>>>>>>>>>>>>>> tag: " + tag); - if (create_new_tags && !checkIfTagExists(tag, all_tags_list)) { + // console.log(">>>>>>>>>>>>>>> tag: " + JSON.stringify(tag)); + if (create_new_tags && !checkIfTagLabelExists(tag, all_tags_list)) { taLog.log("Creating tag: " + tag); await createTag(tag); } From bbf8eb7c5ea72ea15d0a96b926d87f0bff484c1f Mon Sep 17 00:00:00 2001 From: mic Date: Sun, 25 May 2025 22:03:08 +0200 Subject: [PATCH 2/6] Enhance tag assignment logging and prevent new tag creation when not needed. see #390 --- js/mzta-utils.js | 15 ++++++++------- mzta-background.js | 7 +++++-- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/js/mzta-utils.js b/js/mzta-utils.js index 366cce44..b044d2db 100644 --- a/js/mzta-utils.js +++ b/js/mzta-utils.js @@ -357,8 +357,8 @@ export async function createTag(tag) { // } export function checkIfTagLabelExists(tag_label, tags_list) { - console.log(">>>>>>>>>>> checkIfTagExists tags_list: " + JSON.stringify(tags_list)); - console.log(">>>>>>>>>>> checkIfTagExists tag_label: " + tag_label); + // console.log(">>>>>>>>>>> checkIfTagExists tags_list: " + JSON.stringify(tags_list)); + // console.log(">>>>>>>>>>> checkIfTagExists tag_label: " + tag_label); const lowerTagLabel = tag_label.toLowerCase(); return Object.values(tags_list).some(label => label.tag.toLowerCase() === lowerTagLabel); } @@ -376,18 +376,19 @@ export function checkIfTagLabelExists(tag_label, tags_list) { // } export async function assignTagsToMessage(messageId, tags) { - console.log(">>>>>>>>>>> assignTagsToMessage tags: " + JSON.stringify(tags)); + // console.log(">>>>>>>>>>> assignTagsToMessage tags: " + JSON.stringify(tags)); let all_tags_list = await getTagsList(); all_tags_list = all_tags_list[1]; tags = getTagsKeyFromLabel(tags, all_tags_list); - console.log(">>>>>>>>>>> assignTagsToMessage tags after conversion: " + JSON.stringify(tags)); + // console.log(">>>>>>>>>>> assignTagsToMessage tags after conversion: " + JSON.stringify(tags)); let msg_prop = await browser.messages.get(messageId); - console.log(">>>>>>>>>>> assignTagsToMessage msg_prop.tags: " + JSON.stringify(msg_prop.tags)); + // console.log(">>>>>>>>>>> assignTagsToMessage msg_prop.tags: " + JSON.stringify(msg_prop.tags)); tags = tags.concat(msg_prop.tags || []); tags = [...new Set(tags)]; - console.log(">>>>>>>>>>> assignTagsToMessage tags after concat: " + JSON.stringify(tags)); + // console.log(">>>>>>>>>>> assignTagsToMessage tags after concat: " + JSON.stringify(tags)); try { - return browser.messages.update(messageId, {tags: tags}); + await browser.messages.update(messageId, {tags: tags}); + return tags; // Return the updated tags for confirmation } catch (error) { console.error('[ThunderAI] Error assigning tag [messageId: ', messageId, ' - tag: ', tag, ']:', error); } diff --git a/mzta-background.js b/mzta-background.js index 2513ae37..df5dee37 100644 --- a/mzta-background.js +++ b/mzta-background.js @@ -160,6 +160,9 @@ async function _assign_tags(_data, create_new_tags = true) { // console.log(">>>>>>>>>>>>>>> all_tags_list: " + JSON.stringify(all_tags_list)); taLog.log("assign_tags data: " + JSON.stringify(_data)); let new_tags = []; + if(!create_new_tags){ + taLog.log("Not creating new tags, only assigning existing ones..."); + } for (const tag of _data.tags) { // console.log(">>>>>>>>>>>>>>> tag: " + JSON.stringify(tag)); if (create_new_tags && !checkIfTagLabelExists(tag, all_tags_list)) { @@ -168,8 +171,8 @@ async function _assign_tags(_data, create_new_tags = true) { } new_tags.push(tag); } - await assignTagsToMessage(_data.messageId, new_tags); - taLog.log("Assigned tags: " + JSON.stringify(new_tags)); + let added_tags = await assignTagsToMessage(_data.messageId, new_tags); + taLog.log("Assigned tags: " + JSON.stringify(added_tags)); } messenger.runtime.onMessage.addListener((message, sender, sendResponse) => { From 16cabb125f052e348cc8f7b4fa84c71b019e505d Mon Sep 17 00:00:00 2001 From: mic Date: Sun, 25 May 2025 22:09:03 +0200 Subject: [PATCH 3/6] move add tags force existing option and update label. see #391 --- _locales/en/messages.json | 2 +- pages/addtags/mzta-add-tags.css | 2 +- pages/addtags/mzta-add-tags.html | 18 +++++++++--------- pages/addtags/mzta-add-tags.js | 3 --- 4 files changed, 11 insertions(+), 14 deletions(-) diff --git a/_locales/en/messages.json b/_locales/en/messages.json index 1b7df299..22d88aa8 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -1116,7 +1116,7 @@ "description": "" }, "prefs_OptionText_add_tags_auto_force_existing": { - "message": "Force existing tags when autotagging", + "message": "Force existing tags when autotagging or using the context menu", "description": "" }, "prefs_OptionText_add_tags_auto_force_existing_Info": { diff --git a/pages/addtags/mzta-add-tags.css b/pages/addtags/mzta-add-tags.css index fc254dc4..198b6896 100644 --- a/pages/addtags/mzta-add-tags.css +++ b/pages/addtags/mzta-add-tags.css @@ -43,7 +43,7 @@ justify-content: space-between; } -#addtags_info_additional_statements, #add_tags_auto_force_existing_tr, #add_tags_auto_only_inbox_tr{ +#addtags_info_additional_statements, #add_tags_auto_only_inbox_tr{ display: none; } diff --git a/pages/addtags/mzta-add-tags.html b/pages/addtags/mzta-add-tags.html index 18f20d48..451100e7 100644 --- a/pages/addtags/mzta-add-tags.html +++ b/pages/addtags/mzta-add-tags.html @@ -59,15 +59,6 @@ - - __MSG_prefs_OptionText_add_tags_auto_force_existing__ - - - - __MSG_prefs_OptionText_add_tags_auto_only_inbox__ @@ -86,6 +77,15 @@ + + __MSG_prefs_OptionText_add_tags_auto_force_existing__ + + + +
__MSG_AddTags_prompt_text_title__ diff --git a/pages/addtags/mzta-add-tags.js b/pages/addtags/mzta-add-tags.js index 6e8662b2..50a6bd61 100644 --- a/pages/addtags/mzta-add-tags.js +++ b/pages/addtags/mzta-add-tags.js @@ -54,17 +54,14 @@ document.addEventListener('DOMContentLoaded', async () => { }); let add_tags_auto_el = document.getElementById('add_tags_auto'); - let add_tags_auto_force_existing_tr = document.getElementById('add_tags_auto_force_existing_tr'); let add_tags_auto_only_inbox_tr = document.getElementById('add_tags_auto_only_inbox_tr'); let account_selector_container = document.getElementById('account_selector_container'); let add_tags_auto_infoline = document.getElementById('add_tags_auto_infoline'); add_tags_auto_el.addEventListener('click', (event) => { - add_tags_auto_force_existing_tr.style.display = event.target.checked ? 'table-row' : 'none'; add_tags_auto_only_inbox_tr.style.display = event.target.checked ? 'table-row' : 'none'; account_selector_container.style.display = event.target.checked ? 'block' : 'none'; add_tags_auto_infoline.style.display = event.target.checked ? 'inline' : 'none'; }); - add_tags_auto_force_existing_tr.style.display = add_tags_auto_el.checked ? 'table-row' : 'none'; add_tags_auto_only_inbox_tr.style.display = add_tags_auto_el.checked ? 'table-row' : 'none'; account_selector_container.style.display = add_tags_auto_el.checked ? 'block' : 'none'; add_tags_auto_infoline.style.display = add_tags_auto_el.checked ? 'inline' : 'none'; From 6ce5bacfbe428fe10253691775262d6300098190 Mon Sep 17 00:00:00 2001 From: mic Date: Sun, 25 May 2025 22:11:05 +0200 Subject: [PATCH 4/6] release notes updated --- CHANGELOG.md | 1 + options/mzta-release-notes.html | 1 + 2 files changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9340f3b3..133cc365 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@
  • [All APIs] In the API WebChat is now possibile to select a part of the answer and use only that [#356].
  • [All APIs] Setting the "Max prompt length" to zero on the options page will disable the length check when sending a prompt to the AI. [#380].
  • [All APIs] Added a button to the options page to reset the 'Max prompt length' value to its default.
  • +
  • [All APIs] Adding tags automatically or with the context menu will now use also tags not created by ThunderAI [#390].
  • Fix: in the Spamfilter page the unsaved changes warning is now correctly shown.
  • Fix: The default keyboard shortcut is no longer enforced at every Thunderbird startup [#384].
  • Fix: Incoming email processing now works correctly when auto-tagging is enabled and the full tagging feature is subsequently disabled.
  • diff --git a/options/mzta-release-notes.html b/options/mzta-release-notes.html index 02e9565f..2253161e 100644 --- a/options/mzta-release-notes.html +++ b/options/mzta-release-notes.html @@ -15,6 +15,7 @@
  • [All APIs] In the API WebChat is now possibile to select a part of the answer and use only that [#356].
  • [All APIs] Setting the "Max prompt length" to zero on the options page will disable the length check when sending a prompt to the AI. [#380].
  • [All APIs] Added a button to the options page to reset the 'Max prompt length' value to its default.
  • +
  • [All APIs] Adding tags automatically or with the context menu will now use also tags not created by ThunderAI [#390].
  • Fix: In the Spamfilter page the unsaved changes warning is now correctly shown.
  • Fix: The default keyboard shortcut is no longer enforced at every Thunderbird startup [#384].
  • Fix: Incoming email processing now works correctly when auto-tagging is enabled and the full tagging feature is subsequently disabled.
  • From 2ba1905c460e9c498e2706a96b95a039a6e56962 Mon Sep 17 00:00:00 2001 From: mic Date: Sun, 25 May 2025 22:12:23 +0200 Subject: [PATCH 5/6] Fix formatting of comments in exclusion list file for consistency --- js/mzta-addatags-exclusion-list.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/js/mzta-addatags-exclusion-list.js b/js/mzta-addatags-exclusion-list.js index ae44eaba..06a103bd 100644 --- a/js/mzta-addatags-exclusion-list.js +++ b/js/mzta-addatags-exclusion-list.js @@ -16,8 +16,9 @@ * along with this program. If not, see . */ -// These methods are also defined in the file /js/mzta-compose-script.js + +// These methods are also defined in the file /js/mzta-compose-script.js export async function addTags_getExclusionList() { let prefs_excluded_tags = await browser.storage.local.get({add_tags_exclusions: []}); return prefs_excluded_tags.add_tags_exclusions; From c796adc05c513eb5104f587cc50ec2abc7359c3b Mon Sep 17 00:00:00 2001 From: mic Date: Sun, 25 May 2025 22:25:53 +0200 Subject: [PATCH 6/6] Add exclusion list for tag assignment to prevent unwanted tags. See #392 --- mzta-background.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/mzta-background.js b/mzta-background.js index df5dee37..da9e1fbf 100644 --- a/mzta-background.js +++ b/mzta-background.js @@ -26,6 +26,7 @@ import { mzta_specialCommand } from './js/mzta-special-commands.js'; import { getSpamFilterPrompt } from './js/mzta-prompts.js'; import { taSpamReport } from './js/mzta-spamreport.js'; import { taWorkingStatus } from './js/mzta-working-status.js'; +import { addTags_getExclusionList } from './js/mzta-addatags-exclusion-list.js'; browser.runtime.onInstalled.addListener(({ reason, previousVersion }) => { // console.log(">>>>>>>>>>> onInstalled: " + JSON.stringify(reason) + ", previousVersion: " + previousVersion); @@ -160,10 +161,17 @@ async function _assign_tags(_data, create_new_tags = true) { // console.log(">>>>>>>>>>>>>>> all_tags_list: " + JSON.stringify(all_tags_list)); taLog.log("assign_tags data: " + JSON.stringify(_data)); let new_tags = []; + let add_tags_exclusions_list = await addTags_getExclusionList(); + taLog.log("add_tags_exclusions_list: " + JSON.stringify(add_tags_exclusions_list)); + const tags_final = _data.tags.filter(tag => + !add_tags_exclusions_list.some(exclusion => + tag.toLowerCase().includes(exclusion.toLowerCase()) + ) + ); if(!create_new_tags){ taLog.log("Not creating new tags, only assigning existing ones..."); } - for (const tag of _data.tags) { + for (const tag of tags_final) { // console.log(">>>>>>>>>>>>>>> tag: " + JSON.stringify(tag)); if (create_new_tags && !checkIfTagLabelExists(tag, all_tags_list)) { taLog.log("Creating tag: " + tag);