Add cache clearing feature and update UI
This commit is contained in:
parent
b92b8eacfe
commit
1ad1b7004d
4 changed files with 116 additions and 4 deletions
|
@ -147,6 +147,37 @@ async function applyAiRules(idsInput) {
|
||||||
return queue;
|
return queue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function clearCacheForMessages(idsInput) {
|
||||||
|
const ids = Array.isArray(idsInput) ? idsInput : [idsInput];
|
||||||
|
if (!ids.length) return;
|
||||||
|
|
||||||
|
if (!aiRules.length) {
|
||||||
|
const { aiRules: stored } = await storage.local.get("aiRules");
|
||||||
|
aiRules = Array.isArray(stored) ? stored.map(r => {
|
||||||
|
if (r.actions) return r;
|
||||||
|
const actions = [];
|
||||||
|
if (r.tag) actions.push({ type: 'tag', tagKey: r.tag });
|
||||||
|
if (r.moveTo) actions.push({ type: 'move', folder: r.moveTo });
|
||||||
|
const rule = { criterion: r.criterion, actions };
|
||||||
|
if (r.stopProcessing) rule.stopProcessing = true;
|
||||||
|
return rule;
|
||||||
|
}) : [];
|
||||||
|
}
|
||||||
|
|
||||||
|
const keys = [];
|
||||||
|
for (const msg of ids) {
|
||||||
|
const id = msg?.id ?? msg;
|
||||||
|
for (const rule of aiRules) {
|
||||||
|
const key = await sha256Hex(`${id}|${rule.criterion}`);
|
||||||
|
keys.push(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (keys.length) {
|
||||||
|
await AiClassifier.removeCacheEntries(keys);
|
||||||
|
showTransientIcon("resources/img/done.png");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
logger = await import(browser.runtime.getURL("logger.js"));
|
logger = await import(browser.runtime.getURL("logger.js"));
|
||||||
try {
|
try {
|
||||||
|
@ -191,6 +222,21 @@ async function applyAiRules(idsInput) {
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.aiLog("background.js loaded – ready to classify", {debug: true});
|
logger.aiLog("background.js loaded – ready to classify", {debug: true});
|
||||||
|
if (browser.messageDisplayAction) {
|
||||||
|
browser.messageDisplayAction.setTitle({ title: "Classify" });
|
||||||
|
if (browser.messageDisplayAction.setLabel) {
|
||||||
|
browser.messageDisplayAction.setLabel({ label: "Classify" });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (browser.messageDisplayScripts?.registerScripts) {
|
||||||
|
try {
|
||||||
|
await browser.messageDisplayScripts.registerScripts([
|
||||||
|
{ js: [browser.runtime.getURL("resources/clearCacheButton.js")] }
|
||||||
|
]);
|
||||||
|
} catch (e) {
|
||||||
|
logger.aiLog("failed to register message display script", { level: 'warn' }, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
browser.menus.create({
|
browser.menus.create({
|
||||||
id: "apply-ai-rules-list",
|
id: "apply-ai-rules-list",
|
||||||
|
@ -202,6 +248,16 @@ async function applyAiRules(idsInput) {
|
||||||
title: "Apply AI Rules",
|
title: "Apply AI Rules",
|
||||||
contexts: ["message_display_action"],
|
contexts: ["message_display_action"],
|
||||||
});
|
});
|
||||||
|
browser.menus.create({
|
||||||
|
id: "clear-ai-cache-list",
|
||||||
|
title: "Clear AI Cache",
|
||||||
|
contexts: ["message_list"],
|
||||||
|
});
|
||||||
|
browser.menus.create({
|
||||||
|
id: "clear-ai-cache-display",
|
||||||
|
title: "Clear AI Cache",
|
||||||
|
contexts: ["message_display_action"],
|
||||||
|
});
|
||||||
|
|
||||||
if (browser.messageDisplayAction) {
|
if (browser.messageDisplayAction) {
|
||||||
browser.messageDisplayAction.onClicked.addListener(async (tab) => {
|
browser.messageDisplayAction.onClicked.addListener(async (tab) => {
|
||||||
|
@ -220,6 +276,10 @@ async function applyAiRules(idsInput) {
|
||||||
const ids = info.selectedMessages?.messages?.map(m => m.id) ||
|
const ids = info.selectedMessages?.messages?.map(m => m.id) ||
|
||||||
(info.messageId ? [info.messageId] : []);
|
(info.messageId ? [info.messageId] : []);
|
||||||
await applyAiRules(ids);
|
await applyAiRules(ids);
|
||||||
|
} else if (info.menuItemId === "clear-ai-cache-list" || info.menuItemId === "clear-ai-cache-display") {
|
||||||
|
const ids = info.selectedMessages?.messages?.map(m => m.id) ||
|
||||||
|
(info.messageId ? [info.messageId] : []);
|
||||||
|
await clearCacheForMessages(ids);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -243,8 +303,17 @@ async function applyAiRules(idsInput) {
|
||||||
// rethrow so the caller sees the failure
|
// rethrow so the caller sees the failure
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
}
|
} else if (msg?.type === "sortana:clearCacheForDisplayed") {
|
||||||
else {
|
try {
|
||||||
|
const tabs = await browser.tabs.query({ active: true, currentWindow: true });
|
||||||
|
const tabId = tabs[0]?.id;
|
||||||
|
const msgs = tabId ? await browser.messageDisplay.getDisplayedMessages(tabId) : [];
|
||||||
|
const ids = msgs.map(m => m.id);
|
||||||
|
await clearCacheForMessages(ids);
|
||||||
|
} catch (e) {
|
||||||
|
logger.aiLog("failed to clear cache from message script", { level: 'error' }, e);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
logger.aiLog("Unknown message type, ignoring", {level: 'warn'}, msg?.type);
|
logger.aiLog("Unknown message type, ignoring", {level: 'warn'}, msg?.type);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -22,7 +22,9 @@
|
||||||
"default_icon": "resources/img/logo32.png"
|
"default_icon": "resources/img/logo32.png"
|
||||||
},
|
},
|
||||||
"message_display_action": {
|
"message_display_action": {
|
||||||
"default_icon": "resources/img/logo32.png"
|
"default_icon": "resources/img/logo32.png",
|
||||||
|
"default_title": "Classify",
|
||||||
|
"default_label": "Classify"
|
||||||
},
|
},
|
||||||
"background": { "scripts": [ "background.js" ] },
|
"background": { "scripts": [ "background.js" ] },
|
||||||
"options_ui": {
|
"options_ui": {
|
||||||
|
|
|
@ -210,6 +210,25 @@ function cacheResult(cacheKey, matched) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function removeCacheEntries(keys = []) {
|
||||||
|
if (!Array.isArray(keys)) {
|
||||||
|
keys = [keys];
|
||||||
|
}
|
||||||
|
if (!gCacheLoaded) {
|
||||||
|
await loadCache();
|
||||||
|
}
|
||||||
|
let removed = false;
|
||||||
|
for (let key of keys) {
|
||||||
|
if (gCache.delete(key)) {
|
||||||
|
removed = true;
|
||||||
|
aiLog(`[AiClassifier] Removed cache entry '${key}'`, {debug: true});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (removed) {
|
||||||
|
await saveCache();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function classifyTextSync(text, criterion, cacheKey = null) {
|
function classifyTextSync(text, criterion, cacheKey = null) {
|
||||||
if (!Services?.tm?.spinEventLoopUntil) {
|
if (!Services?.tm?.spinEventLoopUntil) {
|
||||||
throw new Error("classifyTextSync requires Services");
|
throw new Error("classifyTextSync requires Services");
|
||||||
|
@ -288,4 +307,4 @@ async function classifyText(text, criterion, cacheKey = null) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export { classifyText, classifyTextSync, setConfig };
|
export { classifyText, classifyTextSync, setConfig, removeCacheEntries };
|
||||||
|
|
22
resources/clearCacheButton.js
Normal file
22
resources/clearCacheButton.js
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
(function() {
|
||||||
|
function addButton() {
|
||||||
|
const toolbar = document.querySelector("#header-view-toolbar") ||
|
||||||
|
document.querySelector("#mail-toolbox toolbar");
|
||||||
|
if (!toolbar || document.getElementById('sortana-clear-cache-button')) return;
|
||||||
|
const button = document.createXULElement ?
|
||||||
|
document.createXULElement('toolbarbutton') :
|
||||||
|
document.createElement('button');
|
||||||
|
button.id = 'sortana-clear-cache-button';
|
||||||
|
button.setAttribute('label', 'Clear Cache');
|
||||||
|
button.className = 'toolbarbutton-1';
|
||||||
|
button.addEventListener('command', () => {
|
||||||
|
browser.runtime.sendMessage({ type: 'sortana:clearCacheForDisplayed' });
|
||||||
|
});
|
||||||
|
toolbar.appendChild(button);
|
||||||
|
}
|
||||||
|
if (document.readyState === 'complete' || document.readyState === 'interactive') {
|
||||||
|
addButton();
|
||||||
|
} else {
|
||||||
|
document.addEventListener('DOMContentLoaded', addButton, { once: true });
|
||||||
|
}
|
||||||
|
})();
|
Loading…
Add table
Add a link
Reference in a new issue