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); }