correctly handling errors. see #183

This commit is contained in:
Mic 2024-12-15 23:48:00 +01:00
parent 3039b5d7ef
commit a2d36c08e6
3 changed files with 81 additions and 37 deletions

View file

@ -834,5 +834,13 @@
"addtags_exclude_tag": { "addtags_exclude_tag": {
"message": "Exclude tag", "message": "Exclude tag",
"description": "" "description": ""
},
"addtags_no_tags_received": {
"message": "No tags received from the AI.",
"description": ""
},
"addtags_no_valid_tags": {
"message": "No valid tags found, after filtering with the exclusion list.",
"description": ""
} }
} }

View file

@ -167,7 +167,7 @@ switch (message.command) {
case "getTags": case "getTags":
console.log(">>>>>>>>>>>>>> getTags: " + JSON.stringify(message.tags)); console.log(">>>>>>>>>>>>>> getTags: " + JSON.stringify(message.tags));
// These methods are also defined in the file /js/mzta-addatags-exclusion-list.js // ===== These methods are also defined in the file /js/mzta-addatags-exclusion-list.js
async function addTags_getExclusionList() { async function addTags_getExclusionList() {
let prefs_excluded_tags = await browser.storage.local.get({add_tags_exclusions: []}); let prefs_excluded_tags = await browser.storage.local.get({add_tags_exclusions: []});
console.log(">>>>>>>>>>>>>>> addTags_getExclusionList prefs_excluded_tags: " + JSON.stringify(prefs_excluded_tags)); console.log(">>>>>>>>>>>>>>> addTags_getExclusionList prefs_excluded_tags: " + JSON.stringify(prefs_excluded_tags));
@ -177,7 +177,7 @@ switch (message.command) {
function addTags_setExclusionList(add_tags_exclusions) { function addTags_setExclusionList(add_tags_exclusions) {
browser.storage.local.set({add_tags_exclusions: add_tags_exclusions}); browser.storage.local.set({add_tags_exclusions: add_tags_exclusions});
} }
// ================================================================================ // ===================================================================================
// Create and append the styles // Create and append the styles
const style = document.createElement('style'); const style = document.createElement('style');
@ -235,6 +235,10 @@ switch (message.command) {
font-size: 14px; font-size: 14px;
} }
.mzta_dialog_message2{
margin: 20px auto;
}
.div_btns{ .div_btns{
width: 90%; width: 90%;
display: flex; display: flex;
@ -286,32 +290,47 @@ switch (message.command) {
const content = document.createElement('div'); const content = document.createElement('div');
content.className = 'mzta_dialog_content'; content.className = 'mzta_dialog_content';
inputString = ''; //TEST
let no_submit = false;
// Parse the input string into labels // Parse the input string into labels
const words = inputString.split(',').map(word => word.trim()); const words = inputString.split(',').map(word => word.trim()).filter(word => word !== '');
console.log(">>>>>>>>>>>>> words: " + JSON.stringify(words)); console.log(">>>>>>>>>>>>> words: " + JSON.stringify(words));
if(words.length == 0){
const message = document.createElement('div');
message.className = 'mzta_dialog_message2';
message.textContent = browser.i18n.getMessage("addtags_no_tags_received");
content.appendChild(message);
no_submit = true;
}
let prefs_tags = await browser.storage.sync.get({add_tags_hide_exclusions: false}); let prefs_tags = await browser.storage.sync.get({add_tags_hide_exclusions: false});
let add_tags_exclusions_list = await addTags_getExclusionList(); let add_tags_exclusions_list = await addTags_getExclusionList();
console.log(">>>>>>>>>>>>> add_tags_exclusions_list: " + JSON.stringify(add_tags_exclusions_list)); console.log(">>>>>>>>>>>>> add_tags_exclusions_list: " + JSON.stringify(add_tags_exclusions_list));
const words_final = words.map(word => { const words_final = words
const isExcluded = add_tags_exclusions_list.some(exclusion => .filter(word => word !== '')
word.includes(exclusion) .map(word => {
); const isExcluded = add_tags_exclusions_list.some(exclusion =>
word.includes(exclusion)
return { );
tag: word,
excluded: isExcluded ? 1 : 0 return {
}; tag: word,
}); excluded: isExcluded ? 1 : 0
};
});
console.log(">>>>>>>>>>>>> words_final: " + JSON.stringify(words_final)); console.log(">>>>>>>>>>>>> words_final: " + JSON.stringify(words_final));
// Create the form // Create the form
const form = document.createElement('form'); const form = document.createElement('form');
let tags_shown = 0;
words_final.forEach(word => { words_final.forEach(word => {
const label = document.createElement('label'); const label = document.createElement('label');
label.style.display = 'block'; label.style.display = 'block';
@ -346,6 +365,7 @@ switch (message.command) {
label.appendChild(img); label.appendChild(img);
label.appendChild(document.createTextNode(` ${word.tag}`)); label.appendChild(document.createTextNode(` ${word.tag}`));
if(!word.excluded || (word.excluded && !prefs_tags.add_tags_hide_exclusions)){ if(!word.excluded || (word.excluded && !prefs_tags.add_tags_hide_exclusions)){
tags_shown++;
form.appendChild(label); form.appendChild(label);
} }
if(word.excluded){ if(word.excluded){
@ -354,6 +374,14 @@ switch (message.command) {
} }
}); });
if((!no_submit) && (tags_shown == 0)){
const message = document.createElement('div');
message.className = 'mzta_dialog_message2';
message.textContent = browser.i18n.getMessage("addtags_no_valid_tags");
content.appendChild(message);
no_submit = true;
}
// Add a div for buttons // Add a div for buttons
const buttonsDiv = document.createElement('div'); const buttonsDiv = document.createElement('div');
buttonsDiv.className = 'div_btns'; buttonsDiv.className = 'div_btns';
@ -367,18 +395,20 @@ switch (message.command) {
closeButton.addEventListener('click', closeDialog); closeButton.addEventListener('click', closeDialog);
buttonsDiv.appendChild(closeButton); buttonsDiv.appendChild(closeButton);
// Add the submit button if(!no_submit){
const submitButton = document.createElement('button'); // Add the submit button
submitButton.type = 'button'; const submitButton = document.createElement('button');
submitButton.id = 'mzta_dialog_submit'; submitButton.type = 'button';
submitButton.textContent = 'Submit'; submitButton.id = 'mzta_dialog_submit';
submitButton.className = 'mzta_dialog_btn'; submitButton.textContent = 'Submit';
submitButton.addEventListener('click', () => { submitButton.className = 'mzta_dialog_btn';
const selected = Array.from(form.querySelectorAll('input[type=checkbox]:checked')).map(cb => cb.value); submitButton.addEventListener('click', () => {
onSubmit(selected); const selected = Array.from(form.querySelectorAll('input[type=checkbox]:checked')).map(cb => cb.value);
closeDialog(); onSubmit(selected);
}); closeDialog();
buttonsDiv.appendChild(submitButton); });
buttonsDiv.appendChild(submitButton);
}
content.appendChild(form); content.appendChild(form);
tags_dialog.appendChild(content); tags_dialog.appendChild(content);

View file

@ -206,23 +206,29 @@ export class mzta_Menus {
switch(curr_prompt.id){ switch(curr_prompt.id){
case 'prompt_add_tags': // Add tags to the email case 'prompt_add_tags': // Add tags to the email
let mail_tags = ''; let mail_tags = '';
let prefs_maxt = await browser.storage.sync.get({add_tags_maxnum: 3}); let prefs_at = await browser.storage.sync.get({add_tags_maxnum: 3, connection_type: ''});
let add_tags_maxnum = prefs_maxt.add_tags_maxnum; if((prefs_at.connection_type === '')||(prefs_at.connection_type === null)||(prefs_at.connection_type === undefined)||(prefs_at.connection_type === 'chatgpt_web')){
console.error("[ThunderAI | AddTags] Invalid connection type: " + prefs_at.connection_type);
return {ok:'0'};
}
let add_tags_maxnum = prefs_at.add_tags_maxnum;
if(add_tags_maxnum > 0){ if(add_tags_maxnum > 0){
fullPrompt += " " + browser.i18n.getMessage("prompt_add_tags_maxnum") + " " + add_tags_maxnum +"."; fullPrompt += " " + browser.i18n.getMessage("prompt_add_tags_maxnum") + " " + add_tags_maxnum +".";
} }
this.logger.log("fullPrompt: " + fullPrompt); this.logger.log("fullPrompt: " + fullPrompt);
// TODO: use the current API, abrto if using chatgpt web // TODO: use the current API, abort if using chatgpt web
// COMMENTED TO DO TESTS // COMMENTED TO DO TESTS
mail_tags = "recipients, test, home, work, car, light"; // mail_tags = "recipients, test, home, work, car, light";
// let cmd_addTags = new mzta_specialCommand_AddTags(fullPrompt,"chatgpt_api",true); let cmd_addTags = new mzta_specialCommand_AddTags(fullPrompt,prefs_at.connection_type,true);
// await cmd_addTags.initWorker(); await cmd_addTags.initWorker();
// try{ try{
// mail_tags = await cmd_addTags.sendPrompt(); mail_tags = await cmd_addTags.sendPrompt();
// // console.log(">>>>>>>>>>> mail_tags: " + mail_tags); // console.log(">>>>>>>>>>> mail_tags: " + mail_tags);
// }catch(err){ }catch(err){
// console.error("[ThunderAI] Error getting tags: ", err); console.error("[ThunderAI] Error getting tags: ", err);
// } browser.tabs.sendMessage(tabs[0].id, { command: "sendAlert", curr_tab_type: tabs[0].type, message: "Error getting tags: " + err });
return {ok:'0'};
}
this.logger.log("mail_tags: " + mail_tags); this.logger.log("mail_tags: " + mail_tags);
browser.tabs.sendMessage(tabs[0].id, {command: "getTags", tags: mail_tags }); browser.tabs.sendMessage(tabs[0].id, {command: "getTags", tags: mail_tags });
return {ok:'1'}; return {ok:'1'};