Merge pull request #659 from micz/summarize_code_improvements_issue631

Summarize code improvements
This commit is contained in:
Mic 2026-02-15 22:13:10 +01:00 committed by GitHub
commit 519628b6f3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1040,83 +1040,16 @@ browser.menus.onClicked.addListener( (info, tab) => {
if(info.menuItemId === contextMenuID_Summarize) {
_summarize = true;
}
if(_add_tags || _spamfilter){
processEmails(getMessages(info.selectedMessages), _add_tags, _spamfilter);
}
if(_summarize) {
// info.selectedMessages is of type MessageList
summarizeEmails(getMessages(info.selectedMessages));
if(_add_tags || _spamfilter || _summarize){
processEmails({
messages: getMessages(info.selectedMessages),
addTagsAuto: _add_tags,
spamFilter: _spamfilter,
summarize: _summarize
});
}
});
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) => {
@ -1135,7 +1068,11 @@ const newEmailListener = (folder, messagesList) => {
let add_tags_auto_enabled = prefs_init.add_tags && prefs_init.add_tags_auto;
await processEmails(messages, add_tags_auto_enabled, prefs_init.spamfilter);
await processEmails({
messages: messages,
addTagsAuto: add_tags_auto_enabled,
spamFilter: prefs_init.spamfilter
});
if(prefs_init.spamfilter){
taSpamReport.truncReportData();
@ -1145,9 +1082,20 @@ const newEmailListener = (folder, messagesList) => {
return _newEmailListener();
}
async function processEmails(messages, addTagsAuto, spamFilter) {
async function processEmails(args) {
const {
messages,
addTagsAuto = false,
spamFilter = false,
summarize = false
} = args;
taWorkingStatus.startWorking();
// We keep two different loops, one for addTagsAuto and spamFilter and one for summarize
// because summarize is never called when an email is received, but only when using the context menu item
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,
@ -1305,6 +1253,71 @@ async function processEmails(messages, addTagsAuto, spamFilter) {
}
}
}
}
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();
}