From 630cee4d27c23a7ee1260a4a716feddce6fc9274 Mon Sep 17 00:00:00 2001 From: mic Date: Sat, 8 Feb 2025 16:04:18 +0100 Subject: [PATCH] added mzta-utils-core.js and added some methods. see #237 --- js/mzta-utils-core.js | 59 +++++++++++++++++++++++++++++++++++++++++++ js/mzta-utils.js | 14 ++++++++++ 2 files changed, 73 insertions(+) create mode 100644 js/mzta-utils-core.js diff --git a/js/mzta-utils-core.js b/js/mzta-utils-core.js new file mode 100644 index 00000000..173cb101 --- /dev/null +++ b/js/mzta-utils-core.js @@ -0,0 +1,59 @@ +/* + * ThunderAI [https://micz.it/thunderbird-addon-thunderai/] + * Copyright (C) 2024 - 2025 Mic (m@micz.it) + + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +import { placeholdersUtils } from './mzta-placeholders.js'; + +export const taCoreUtils = { + + async getDefaultSignature(){ + let prefs = await browser.storage.sync.get({default_sign_name: ''}); + if(prefs.default_sign_name===''){ + return ''; + }else{ + return browser.i18n.getMessage("sign_msg_as") + " " + prefs.default_sign_name + "."; + } + }, + + async preparePrompt(curr_prompt, curr_message, chatgpt_lang, selection_text, body_text, subject_text, msg_text, only_typed_text, tags_full_list){ + let fullPrompt = ''; + + if(!placeholdersUtils.hasPlaceholder(curr_prompt.text)){ + // no placeholders, do as usual + fullPrompt = curr_prompt.text + (String(curr_prompt.need_signature) == "1" ? " " + await taCoreUtils.getDefaultSignature():"") + " " + chatgpt_lang + " \"" + (selection_text=='' ? body_text : selection_text) + "\" "; + }else{ + // we have at least a placeholder, do the magic! + let finalSubs = placeholdersUtils.getPlaceholdersValues(curr_prompt.text, curr_message, subject_text, body_text, msg_text, only_typed_text, selection_text, tags_full_list); + // console.log(">>>>>>>>>> finalSubs: " + JSON.stringify(finalSubs)); + let prefs_ph = await browser.storage.sync.get({placeholders_use_default_value: false}); + fullPrompt = (placeholdersUtils.replacePlaceholders(curr_prompt.text, finalSubs, prefs_ph.placeholders_use_default_value, true) + (String(curr_prompt.need_signature) == "1" ? " " + await taCoreUtils.getDefaultSignature():"") + " " + chatgpt_lang).trim(); + } + + return fullPrompt; + }, + + finalizePrompt_add_tags(fullPrompt, add_tags_maxnum, add_tags_force_lang, default_chatgpt_lang){ + if(add_tags_maxnum > 0){ + fullPrompt += " " + browser.i18n.getMessage("prompt_add_tags_maxnum") + " " + add_tags_maxnum +"."; + } + if(add_tags_force_lang && default_chatgpt_lang !== ''){ + fullPrompt += " " + browser.i18n.getMessage("prompt_add_tags_force_lang") + " " + default_chatgpt_lang + "."; + } + + return fullPrompt; + }, +}; \ No newline at end of file diff --git a/js/mzta-utils.js b/js/mzta-utils.js index 691ec949..d9b995c7 100644 --- a/js/mzta-utils.js +++ b/js/mzta-utils.js @@ -403,3 +403,17 @@ export async function migrateDefaultPromptsPropStorage(){ await browser.storage.sync.remove("_default_prompts_properties"); // console.log("migrateDefaultPromptsPropStorage: migrated default prompts properties from storage.sync to storage.local"); } + +export async function* getMessages(list) { + let page = await list; + for (let message of page.messages) { + yield message; + } + + while (page.id) { + page = await messenger.messages.continueList(page.id); + for (let message of page.messages) { + yield message; + } + } +} \ No newline at end of file