Improved prompts method args handling. see #471
This commit is contained in:
parent
87303adfb1
commit
5af9a82951
5 changed files with 99 additions and 20 deletions
|
|
@ -191,7 +191,11 @@ browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
|
|||
// we have the additional_text placeholder, do the magic!
|
||||
let finalSubs = {};
|
||||
finalSubs["additional_text"] = userInput;
|
||||
promptData.prompt = placeholdersUtils.replacePlaceholders(promptData.prompt, finalSubs, ph_def_val==='1')
|
||||
promptData.prompt = placeholdersUtils.replacePlaceholders({
|
||||
text: promptData.prompt,
|
||||
replacements: finalSubs,
|
||||
use_default_value: ph_def_val==='1'
|
||||
})
|
||||
}
|
||||
sendPrompt(promptData);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -144,7 +144,19 @@ export class mzta_Menus {
|
|||
break;
|
||||
}
|
||||
|
||||
fullPrompt = await taPromptUtils.preparePrompt(curr_prompt, curr_message, chatgpt_lang, selection_text, selection_html, body_text, await getMailSubject(tabs[0]), msg_text, only_typed_text, only_quoted_text, tags_full_list);
|
||||
fullPrompt = await taPromptUtils.preparePrompt({
|
||||
curr_prompt: curr_prompt,
|
||||
curr_message: curr_message,
|
||||
chatgpt_lang: chatgpt_lang,
|
||||
selection_text: selection_text,
|
||||
selection_html: selection_html,
|
||||
body_text: body_text,
|
||||
subject_text: await getMailSubject(tabs[0]),
|
||||
msg_text: msg_text,
|
||||
only_typed_text: only_typed_text,
|
||||
only_quoted_text: only_quoted_text,
|
||||
tags_full_list: tags_full_list
|
||||
});
|
||||
|
||||
switch(curr_prompt.id){
|
||||
case 'prompt_translate_this':
|
||||
|
|
|
|||
|
|
@ -337,19 +337,25 @@ export const placeholdersUtils = {
|
|||
return matches;
|
||||
},
|
||||
|
||||
replacePlaceholders(text, replacements, use_default_value = false, skip_additional_text = false) {
|
||||
replacePlaceholders(args) {
|
||||
const {
|
||||
text = "",
|
||||
replacements = {},
|
||||
use_default_value = false,
|
||||
skip_additional_text = false
|
||||
} = args || {};
|
||||
// Regular expression to match patterns like {%...%}
|
||||
return text.replace(/{%\s*(.*?)\s*%}/g, function(match, p1) {
|
||||
// p1 contains the key inside {% %}
|
||||
if (skip_additional_text && (p1 === 'additional_text')) {
|
||||
return match;
|
||||
}
|
||||
const currPlaceholder = defaultPlaceholders.find(ph => ph.id === p1);
|
||||
if (!currPlaceholder) {
|
||||
return match;
|
||||
}
|
||||
// Replace if found, otherwise keep the original or substitute with default value
|
||||
return replacements[p1] || (use_default_value ? currPlaceholder.default_value : match);
|
||||
// p1 contains the key inside {% %}
|
||||
if (skip_additional_text && (p1 === 'additional_text')) {
|
||||
return match;
|
||||
}
|
||||
const currPlaceholder = defaultPlaceholders.find(ph => ph.id === p1);
|
||||
if (!currPlaceholder) {
|
||||
return match;
|
||||
}
|
||||
// Replace if found, otherwise keep the original or substitute with default value
|
||||
return replacements[p1] || (use_default_value ? currPlaceholder.default_value : match);
|
||||
});
|
||||
},
|
||||
|
||||
|
|
@ -398,9 +404,22 @@ export const placeholdersUtils = {
|
|||
return regex.test(text);
|
||||
},
|
||||
|
||||
async getPlaceholdersValues(prompt_text, curr_message, mail_subject, body_text, msg_text, only_typed_text, only_quoted_text, selection_text, selection_html, tags_full_list) {
|
||||
async getPlaceholdersValues(args) {
|
||||
const {
|
||||
prompt_text = "",
|
||||
curr_message = {},
|
||||
mail_subject = "",
|
||||
body_text = "",
|
||||
msg_text = {},
|
||||
only_typed_text = "",
|
||||
only_quoted_text = "",
|
||||
selection_text = "",
|
||||
selection_html = "",
|
||||
tags_full_list = ["", []]
|
||||
} = args || {};
|
||||
let currPHs = await placeholdersUtils.extractPlaceholders(prompt_text);
|
||||
// console.log(">>>>>>>>>> currPHs: " + JSON.stringify(currPHs));
|
||||
console.log(">>>>>>>>>> curr_message: " + JSON.stringify(curr_message));
|
||||
let finalSubs = {};
|
||||
for(let currPH of currPHs){
|
||||
switch(currPH.id){
|
||||
|
|
|
|||
|
|
@ -30,7 +30,21 @@ export const taPromptUtils = {
|
|||
}
|
||||
},
|
||||
|
||||
async preparePrompt(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){
|
||||
async preparePrompt(args){ console.log(">>>>>>>>>> taPromptUtils.preparePrompt args: " + JSON.stringify(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)){
|
||||
|
|
@ -42,10 +56,25 @@ export const taPromptUtils = {
|
|||
if(placeholdersUtils.hasCustomPlaceholder(curr_prompt.text)){
|
||||
curr_prompt.text = await placeholdersUtils.replaceCustomPlaceholders(curr_prompt.text);
|
||||
}
|
||||
let finalSubs = await placeholdersUtils.getPlaceholdersValues(curr_prompt.text, curr_message, subject_text, body_text, msg_text, only_typed_text, only_quoted_text, selection_text, selection_html, tags_full_list);
|
||||
// console.log(">>>>>>>>>> finalSubs: " + JSON.stringify(finalSubs));
|
||||
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(curr_prompt.text, finalSubs, prefs_ph.placeholders_use_default_value, true) + (String(curr_prompt.need_signature) == "1" ? " " + await taPromptUtils.getDefaultSignature():"") + " " + chatgpt_lang).trim();
|
||||
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;
|
||||
|
|
|
|||
|
|
@ -1005,7 +1005,15 @@ async function processEmails(messages, addTagsAuto, spamFilter) {
|
|||
let tags_full_list = await getTagsList();
|
||||
// console.log(">>>>>>>>>>>>> curr_prompt_add_tags: " + curr_prompt_add_tags);
|
||||
let chatgpt_lang = await taPromptUtils.getDefaultLang(curr_prompt_add_tags);
|
||||
specialFullPrompt_add_tags = await taPromptUtils.preparePrompt(curr_prompt_add_tags, message, chatgpt_lang, '', '', body_text, curr_fullMessage.headers.subject, msg_text, '', '', tags_full_list);
|
||||
specialFullPrompt_add_tags = await taPromptUtils.preparePrompt({
|
||||
curr_prompt: curr_prompt_add_tags,
|
||||
curr_message: curr_message,
|
||||
chatgpt_lang: chatgpt_lang,
|
||||
body_text: body_text,
|
||||
subject_text: curr_fullMessage.headers.subject,
|
||||
msg_text: msg_text,
|
||||
tags_full_list: tags_full_list
|
||||
});
|
||||
specialFullPrompt_add_tags = taPromptUtils.finalizePrompt_add_tags(specialFullPrompt_add_tags, prefs_aats.add_tags_maxnum, prefs_aats.add_tags_force_lang, prefs_aats.default_chatgpt_lang);
|
||||
taLog.log("Special prompt: " + specialFullPrompt_add_tags);
|
||||
let cmd_addTags = new mzta_specialCommand(specialFullPrompt_add_tags, prefs_aats.connection_type, prefs_init.do_debug);
|
||||
|
|
@ -1032,7 +1040,14 @@ async function processEmails(messages, addTagsAuto, spamFilter) {
|
|||
let curr_prompt_spamfilter = await getSpamFilterPrompt();
|
||||
// console.log(">>>>>>>>>>>>> curr_prompt_spamfilter: " + curr_prompt_spamfilter);
|
||||
let chatgpt_lang = await taPromptUtils.getDefaultLang(curr_prompt_spamfilter);
|
||||
let specialFullPrompt_spamfilter = await taPromptUtils.preparePrompt(curr_prompt_spamfilter, message, chatgpt_lang, '', '', body_text, curr_fullMessage.headers.subject, msg_text, '', '', []);
|
||||
let specialFullPrompt_spamfilter = await taPromptUtils.preparePrompt({
|
||||
curr_prompt: curr_prompt_spamfilter,
|
||||
curr_message: message,
|
||||
chatgpt_lang: chatgpt_lang,
|
||||
body_text: body_text,
|
||||
subject_text: curr_fullMessage.headers.subject,
|
||||
msg_text: msg_text
|
||||
});
|
||||
taLog.log("Special prompt: " + specialFullPrompt_spamfilter);
|
||||
console.log(">>>>>>>> Special prompt for spamfilter: " + specialFullPrompt_spamfilter);
|
||||
let cmd_spamfilter = new mzta_specialCommand(specialFullPrompt_spamfilter, prefs_init.connection_type, prefs_init.do_debug);
|
||||
|
|
|
|||
Loading…
Reference in a new issue