ThunderAI/js/mzta-utils-prompt.js
2025-09-04 22:46:00 +02:00

155 lines
No EOL
6.6 KiB
JavaScript

/*
* 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 <http://www.gnu.org/licenses/>.
*/
import { placeholdersUtils } from './mzta-placeholders.js';
import { extractJsonObject } from './mzta-utils.js';
export const taPromptUtils = {
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(args){
const {
curr_prompt = {},
curr_message = {},
chatgpt_lang = '',
selection_text = '',
selection_html = '',
body_text = '',
subject_text = '',
msg_text = {},
only_typed_text = '',
only_quoted_text = '',
tags_full_list = ["", []]
} = args || {};
let fullPrompt = '';
if(!placeholdersUtils.hasPlaceholder(curr_prompt.text)){
// no placeholders, do as usual
fullPrompt = curr_prompt.text + (String(curr_prompt.need_signature) == "1" ? " " + await taPromptUtils.getDefaultSignature():"") + " " + chatgpt_lang + " \"" + (selection_text=='' ? body_text : selection_text) + "\" ";
}else{
// we have at least a placeholder, do the magic!
// check if we have custom placeholders
if(placeholdersUtils.hasCustomPlaceholder(curr_prompt.text)){
curr_prompt.text = await placeholdersUtils.replaceCustomPlaceholders(curr_prompt.text);
}
let finalSubs = await placeholdersUtils.getPlaceholdersValues({
prompt_text: curr_prompt.text,
curr_message: curr_message,
mail_subject: subject_text,
body_text: body_text,
msg_text: msg_text,
only_typed_text: only_typed_text,
only_quoted_text: only_quoted_text,
selection_text: selection_text,
selection_html: selection_html,
tags_full_list: tags_full_list
});
let prefs_ph = await browser.storage.sync.get({placeholders_use_default_value: false});
fullPrompt = (placeholdersUtils.replacePlaceholders({
text: curr_prompt.text,
replacements: finalSubs,
use_default_value: prefs_ph.placeholders_use_default_value,
skip_additional_text: true
}) + (String(curr_prompt.need_signature) == "1" ? " " + await taPromptUtils.getDefaultSignature():"") + " " + chatgpt_lang).trim();
}
return fullPrompt;
},
finalizePrompt_add_tags(fullPrompt, add_tags_maxnum, add_tags_force_lang, default_chatgpt_lang, add_tags_auto_uselist = false, add_tags_auto_uselist_list = ''){
if(add_tags_maxnum > 0){
fullPrompt += " \n" + browser.i18n.getMessage("prompt_add_tags_maxnum") + " " + add_tags_maxnum +".";
}
if(add_tags_force_lang && default_chatgpt_lang !== ''){
fullPrompt += " \n" + browser.i18n.getMessage("prompt_add_tags_force_lang") + " " + default_chatgpt_lang + ".";
}
if(add_tags_auto_uselist && add_tags_auto_uselist_list && add_tags_auto_uselist_list.length > 0){
fullPrompt += " \n" + browser.i18n.getMessage("prompt_add_tags_use_list") + ": " + add_tags_auto_uselist_list + ".";
}
return fullPrompt;
},
finalizePrompt_get_calendar_event(fullPrompt){
fullPrompt = fullPrompt.replace("{%cc_list%}", "");
fullPrompt = fullPrompt.replace("{%recipients%}", "");
return fullPrompt;
},
async getDefaultLang(curr_prompt){
let chatgpt_lang = '';
if(String(curr_prompt.define_response_lang) == "1"){
let prefs = await browser.storage.sync.get({default_chatgpt_lang: ''});
chatgpt_lang = prefs.default_chatgpt_lang;
if(chatgpt_lang === ''){
chatgpt_lang = browser.i18n.getMessage("reply_same_lang");
}else{
chatgpt_lang = browser.i18n.getMessage("prompt_lang") + " " + chatgpt_lang + ".";
}
}
return chatgpt_lang;
},
/**
* Extracts tags from the response text.
* @param {string} response_text - The response text from which to extract tags.
* @returns {Array} An array of tags extracted from the response text.
*
* The response text should be a JSON with this structure:
* {
* "tags": ["tag1", "tag2", ...]
* }
*
* For backwords compatibility, if the response text is not a valid JSON,
* it will try to split the text by commas and return the resulting array.
*/
getTagsFromResponse(response_text, filter_tags = false, filter_tags_list = ''){
let tags = [];
if(response_text && response_text.length > 0){
try {
// Try to parse the response text as JSON
let response_json = extractJsonObject(response_text.trim());
if(response_json && Array.isArray(response_json.tags)){
tags = response_json.tags;
} else if(response_json && response_json.tags && typeof response_json.tags === 'string'){
// If tags is a string, split it by commas
tags = response_json.tags.split(/,\s*/).map(tag => tag.trim());
}
} catch (e) {
// If parsing fails, fallback to splitting by commas
tags = response_text.split(/,\s*/).map(tag => tag.trim());
}
}
if(filter_tags && filter_tags_list && filter_tags_list.length > 0){
const allowedTags = filter_tags_list.split(',').map(tag => tag.trim().toLowerCase()).filter(tag => tag.length > 0);
tags = tags.filter(tag => allowedTags.includes(tag.toLowerCase()));
}
return tags;
}
};