Add reasoning cache and viewer

This commit is contained in:
Jordan Wages 2025-06-27 01:39:02 -05:00
commit b6abe758b1
6 changed files with 240 additions and 7 deletions

View file

@ -231,7 +231,8 @@ async function clearCacheForMessages(idsInput) {
if (browser.messageDisplayScripts?.registerScripts) {
try {
await browser.messageDisplayScripts.registerScripts([
{ js: [browser.runtime.getURL("resources/clearCacheButton.js")] }
{ js: [browser.runtime.getURL("resources/clearCacheButton.js")] },
{ js: [browser.runtime.getURL("resources/reasonButton.js")] }
]);
} catch (e) {
logger.aiLog("failed to register message display script", { level: 'warn' }, e);
@ -313,6 +314,36 @@ async function clearCacheForMessages(idsInput) {
} catch (e) {
logger.aiLog("failed to clear cache from message script", { level: 'error' }, e);
}
} else if (msg?.type === "sortana:getReasons") {
try {
const id = msg.id;
const hdr = await messenger.messages.get(id);
const subject = hdr?.subject || "";
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 reasons = [];
for (const rule of aiRules) {
const key = await sha256Hex(`${id}|${rule.criterion}`);
const reason = AiClassifier.getReason(key);
if (reason) {
reasons.push({ criterion: rule.criterion, reason });
}
}
return { subject, reasons };
} catch (e) {
logger.aiLog("failed to collect reasons", { level: 'error' }, e);
return { subject: '', reasons: [] };
}
} else {
logger.aiLog("Unknown message type, ignoring", {level: 'warn'}, msg?.type);
}