Merge pull request #659 from micz/summarize_code_improvements_issue631
Summarize code improvements
This commit is contained in:
commit
519628b6f3
1 changed files with 237 additions and 224 deletions
|
|
@ -1040,83 +1040,16 @@ browser.menus.onClicked.addListener( (info, tab) => {
|
||||||
if(info.menuItemId === contextMenuID_Summarize) {
|
if(info.menuItemId === contextMenuID_Summarize) {
|
||||||
_summarize = true;
|
_summarize = true;
|
||||||
}
|
}
|
||||||
if(_add_tags || _spamfilter){
|
if(_add_tags || _spamfilter || _summarize){
|
||||||
processEmails(getMessages(info.selectedMessages), _add_tags, _spamfilter);
|
processEmails({
|
||||||
}
|
messages: getMessages(info.selectedMessages),
|
||||||
if(_summarize) {
|
addTagsAuto: _add_tags,
|
||||||
// info.selectedMessages is of type MessageList
|
spamFilter: _spamfilter,
|
||||||
summarizeEmails(getMessages(info.selectedMessages));
|
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
|
// Listening for new received emails
|
||||||
const newEmailListener = (folder, messagesList) => {
|
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;
|
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){
|
if(prefs_init.spamfilter){
|
||||||
taSpamReport.truncReportData();
|
taSpamReport.truncReportData();
|
||||||
|
|
@ -1145,9 +1082,20 @@ const newEmailListener = (folder, messagesList) => {
|
||||||
return _newEmailListener();
|
return _newEmailListener();
|
||||||
}
|
}
|
||||||
|
|
||||||
async function processEmails(messages, addTagsAuto, spamFilter) {
|
async function processEmails(args) {
|
||||||
|
const {
|
||||||
|
messages,
|
||||||
|
addTagsAuto = false,
|
||||||
|
spamFilter = false,
|
||||||
|
summarize = false
|
||||||
|
} = args;
|
||||||
|
|
||||||
taWorkingStatus.startWorking();
|
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({
|
let prefs_aats = await browser.storage.sync.get({
|
||||||
add_tags_maxnum: prefs_default.add_tags_maxnum,
|
add_tags_maxnum: prefs_default.add_tags_maxnum,
|
||||||
connection_type: prefs_default.connection_type,
|
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();
|
taWorkingStatus.stopWorking();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue