Refactor message processing
This commit is contained in:
parent
3c292096eb
commit
1b7fa3d5ee
1 changed files with 67 additions and 66 deletions
|
@ -169,24 +169,23 @@ function buildEmailText(full) {
|
|||
const headers = Object.entries(full.headers || {})
|
||||
.map(([k, v]) => `${k}: ${v.join(' ')}`)
|
||||
.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();
|
||||
return sanitizeString(combined);
|
||||
}
|
||||
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);
|
||||
}
|
||||
function updateTimingStats(elapsed) {
|
||||
const t = timingStats;
|
||||
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) {
|
||||
const id = msg?.id ?? msg;
|
||||
queuedCount++;
|
||||
updateActionIcon();
|
||||
queue = queue.then(async () => {
|
||||
async function processMessage(id) {
|
||||
processing = true;
|
||||
currentStart = Date.now();
|
||||
queuedCount--;
|
||||
|
@ -226,31 +225,33 @@ async function applyAiRules(idsInput) {
|
|||
processing = false;
|
||||
const elapsed = Date.now() - currentStart;
|
||||
currentStart = 0;
|
||||
const t = timingStats;
|
||||
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);
|
||||
await storage.local.set({ classifyStats: t });
|
||||
updateTimingStats(elapsed);
|
||||
await storage.local.set({ classifyStats: timingStats });
|
||||
showTransientIcon(ICONS.circle);
|
||||
} catch (e) {
|
||||
processing = false;
|
||||
const elapsed = Date.now() - currentStart;
|
||||
currentStart = 0;
|
||||
const t = timingStats;
|
||||
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);
|
||||
await storage.local.set({ classifyStats: t });
|
||||
updateTimingStats(elapsed);
|
||||
await storage.local.set({ classifyStats: timingStats });
|
||||
logger.aiLog("failed to apply AI rules", { level: 'error' }, e);
|
||||
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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue