processEmails now also does the summarize. see #631

This commit is contained in:
mic 2026-02-15 11:08:38 +01:00
parent a7a17b7cb5
commit ca2323ba88

View file

@ -1040,87 +1040,16 @@ browser.menus.onClicked.addListener( (info, tab) => {
if(info.menuItemId === contextMenuID_Summarize) {
_summarize = true;
}
if(_add_tags || _spamfilter){
if(_add_tags || _spamfilter || _summarize){
processEmails({
messages: getMessages(info.selectedMessages),
addTagsAuto: _add_tags,
spamFilter: _spamfilter
spamFilter: _spamfilter,
summarize: _summarize
});
}
if(_summarize) {
// info.selectedMessages is of type MessageList
summarizeEmails(getMessages(info.selectedMessages));
}
});
async function summarizeEmails(messages) {
taWorkingStatus.startWorking();
// we have three prompts, the actual assignment for the LLM, the email
// template prompt, and the email separator prompt
const specialPrompts = await getSpecialPrompts();
const prompt = specialPrompts.find((prompt) => prompt.id === 'prompt_summarize');
const prompt_email = specialPrompts.find((prompt) => prompt.id === 'prompt_summarize_email_template');
const prompt_email_separator = specialPrompts.find((prompt) => prompt.id === 'prompt_summarize_email_separator');
const tabs = await browser.tabs.query({ active: true, currentWindow: true });
const chatgpt_lang = await taPromptUtils.getDefaultLang(prompt);
// replace placeholders in the prompts the assignment prompt and email
// separator prompt do not have a message as context, so there is only
// limited things to replace
const prompt_string = await taPromptUtils.preparePrompt({
curr_prompt: prompt,
chatgpt_lang: chatgpt_lang,
});
const prompt_email_separator_string = await taPromptUtils.preparePrompt({
curr_prompt: prompt_email_separator,
chatgpt_lang: chatgpt_lang,
});
// assemble all email messages into one string and add the assignment prompt
const messages_list = [];
for await (let curr_message of messages) {
// extract body of current message as text
const curr_message_full = await browser.messages.getFull(curr_message.id);
const curr_body_full_html = getMailBody(curr_message_full);
const curr_body_full_text = htmlBodyToPlainText(curr_body_full_html.html);
if( curr_body_full_text.length === 0) {
taLog.log("No HTML found in the message body, using plain text...");
curr_body_full_text = curr_message_full.text;
}
messages_list.push(await taPromptUtils.preparePrompt({
curr_prompt: prompt_email,
curr_message: curr_message,
chatgpt_lang: chatgpt_lang,
body_text: curr_body_full_text,
subject_text: curr_message_full.headers.subject,
msg_text: curr_body_full_html,
}));
};
const messages_string = messages_list.join(prompt_email_separator_string);
const full_prompt = prompt_string + prompt_email_separator_string + messages_string;
// console.log(full_prompt);
// send the prompt to the chat interface
openChatGPT(
full_prompt,
prompt.action,
tabs[0].id,
prompt.name,
prompt.need_custom_text,
prompt
)
taWorkingStatus.stopWorking();
return {ok : '1'};
}
// Listening for new received emails
const newEmailListener = (folder, messagesList) => {
@ -1157,11 +1086,13 @@ async function processEmails(args) {
const {
messages,
addTagsAuto = false,
spamFilter = false
spamFilter = false,
summarize = false
} = args;
taWorkingStatus.startWorking();
if (addTagsAuto || spamFilter) {
let prefs_aats = await browser.storage.sync.get({
add_tags_maxnum: prefs_default.add_tags_maxnum,
connection_type: prefs_default.connection_type,
@ -1319,6 +1250,71 @@ async function processEmails(args) {
}
}
}
}
if (summarize) {
// we have three prompts, the actual assignment for the LLM, the email
// template prompt, and the email separator prompt
const specialPrompts = await getSpecialPrompts();
const prompt = specialPrompts.find((prompt) => prompt.id === 'prompt_summarize');
const prompt_email = specialPrompts.find((prompt) => prompt.id === 'prompt_summarize_email_template');
const prompt_email_separator = specialPrompts.find((prompt) => prompt.id === 'prompt_summarize_email_separator');
const tabs = await browser.tabs.query({ active: true, currentWindow: true });
const chatgpt_lang = await taPromptUtils.getDefaultLang(prompt);
// replace placeholders in the prompts the assignment prompt and email
// separator prompt do not have a message as context, so there is only
// limited things to replace
const prompt_string = await taPromptUtils.preparePrompt({
curr_prompt: prompt,
chatgpt_lang: chatgpt_lang,
});
const prompt_email_separator_string = await taPromptUtils.preparePrompt({
curr_prompt: prompt_email_separator,
chatgpt_lang: chatgpt_lang,
});
// assemble all email messages into one string and add the assignment prompt
const messages_list = [];
for await (let curr_message of messages) {
// extract body of current message as text
const curr_message_full = await browser.messages.getFull(curr_message.id);
const curr_body_full_html = getMailBody(curr_message_full);
let curr_body_full_text = htmlBodyToPlainText(curr_body_full_html.html);
if( curr_body_full_text.length === 0) {
taLog.log("No HTML found in the message body, using plain text...");
curr_body_full_text = curr_message_full.text;
}
messages_list.push(await taPromptUtils.preparePrompt({
curr_prompt: prompt_email,
curr_message: curr_message,
chatgpt_lang: chatgpt_lang,
body_text: curr_body_full_text,
subject_text: curr_message_full.headers.subject,
msg_text: curr_body_full_html,
}));
};
const messages_string = messages_list.join(prompt_email_separator_string);
const full_prompt = prompt_string + prompt_email_separator_string + messages_string;
// console.log(full_prompt);
// send the prompt to the chat interface
openChatGPT(
full_prompt,
prompt.action,
tabs[0].id,
prompt.name,
prompt.need_custom_text,
prompt
);
}
taWorkingStatus.stopWorking();
}