Refactor message processing

This commit is contained in:
Jordan Wages 2025-07-08 19:45:57 -05:00
commit 1b7fa3d5ee

View file

@ -169,24 +169,23 @@ function buildEmailText(full) {
const headers = Object.entries(full.headers || {}) const headers = Object.entries(full.headers || {})
.map(([k, v]) => `${k}: ${v.join(' ')}`) .map(([k, v]) => `${k}: ${v.join(' ')}`)
.join('\n'); .join('\n');
const attachInfo = `Attachments: ${attachments.length}` + (attachments.length ? "\n" + attachments.map(a => ` - ${a}`).join('\n') : ""); const attachInfo = `Attachments: ${attachments.length}` +
(attachments.length ? "\n" + attachments.map(a => ` - ${a}`).join('\n') : "");
const combined = `${headers}\n${attachInfo}\n\n${bodyParts.join('\n')}`.trim(); const combined = `${headers}\n${attachInfo}\n\n${bodyParts.join('\n')}`.trim();
return sanitizeString(combined); return sanitizeString(combined);
} }
async function applyAiRules(idsInput) {
const ids = Array.isArray(idsInput) ? idsInput : [idsInput];
if (!ids.length) return queue;
if (!aiRules.length) { function updateTimingStats(elapsed) {
const { aiRules: stored } = await storage.local.get("aiRules"); const t = timingStats;
aiRules = normalizeRules(stored); t.count += 1;
} t.total += elapsed;
t.last = elapsed;
const delta = elapsed - t.mean;
t.mean += delta / t.count;
t.m2 += delta * (elapsed - t.mean);
}
for (const msg of ids) { async function processMessage(id) {
const id = msg?.id ?? msg;
queuedCount++;
updateActionIcon();
queue = queue.then(async () => {
processing = true; processing = true;
currentStart = Date.now(); currentStart = Date.now();
queuedCount--; queuedCount--;
@ -226,31 +225,33 @@ async function applyAiRules(idsInput) {
processing = false; processing = false;
const elapsed = Date.now() - currentStart; const elapsed = Date.now() - currentStart;
currentStart = 0; currentStart = 0;
const t = timingStats; updateTimingStats(elapsed);
t.count += 1; await storage.local.set({ classifyStats: timingStats });
t.total += elapsed;
t.last = elapsed;
const delta = elapsed - t.mean;
t.mean += delta / t.count;
t.m2 += delta * (elapsed - t.mean);
await storage.local.set({ classifyStats: t });
showTransientIcon(ICONS.circle); showTransientIcon(ICONS.circle);
} catch (e) { } catch (e) {
processing = false; processing = false;
const elapsed = Date.now() - currentStart; const elapsed = Date.now() - currentStart;
currentStart = 0; currentStart = 0;
const t = timingStats; updateTimingStats(elapsed);
t.count += 1; await storage.local.set({ classifyStats: timingStats });
t.total += elapsed;
t.last = elapsed;
const delta = elapsed - t.mean;
t.mean += delta / t.count;
t.m2 += delta * (elapsed - t.mean);
await storage.local.set({ classifyStats: t });
logger.aiLog("failed to apply AI rules", { level: 'error' }, e); logger.aiLog("failed to apply AI rules", { level: 'error' }, e);
showTransientIcon(ICONS.average); showTransientIcon(ICONS.average);
} }
}); }
async function applyAiRules(idsInput) {
const ids = Array.isArray(idsInput) ? idsInput : [idsInput];
if (!ids.length) return queue;
if (!aiRules.length) {
const { aiRules: stored } = await storage.local.get("aiRules");
aiRules = normalizeRules(stored);
}
for (const msg of ids) {
const id = msg?.id ?? msg;
queuedCount++;
updateActionIcon();
queue = queue.then(() => processMessage(id));
} }
return queue; return queue;