Merge pull request #84 from wagesj45/codex/add-processmessage-function-and-updates
Refactor message classification
This commit is contained in:
commit
f6b67b3407
1 changed files with 67 additions and 66 deletions
|
@ -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;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue