@kbzowski/mcp-inbox 0.3.2 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/app.js +45 -13
- package/dist/app.js.map +2 -2
- package/dist/cache/queries.d.ts +8 -1
- package/dist/cache/sync.d.ts +8 -1
- package/package.json +3 -1
package/dist/app.js
CHANGED
|
@@ -544,20 +544,30 @@ function upsertEmail(db, row) {
|
|
|
544
544
|
function getEmail(db, folder, uid) {
|
|
545
545
|
return db.select().from(emails).where(and(eq(emails.folder, folder), eq(emails.uid, uid))).get();
|
|
546
546
|
}
|
|
547
|
-
function
|
|
547
|
+
function buildFolderConditions(folder, opts) {
|
|
548
548
|
const conditions = [eq(emails.folder, folder)];
|
|
549
549
|
if (opts.sinceMs !== void 0) conditions.push(gte(emails.date, opts.sinceMs));
|
|
550
550
|
if (opts.beforeMs !== void 0) conditions.push(lt(emails.date, opts.beforeMs));
|
|
551
|
-
let rows = db.select().from(emails).where(and(...conditions)).orderBy(desc(emails.date), desc(emails.uid)).limit(opts.limit ?? 100).offset(opts.offset ?? 0).all();
|
|
552
551
|
if (opts.unseenOnly) {
|
|
553
|
-
|
|
552
|
+
conditions.push(
|
|
553
|
+
sql`NOT EXISTS (SELECT 1 FROM json_each(${emails.flags}) WHERE value = ${"\\Seen"})`
|
|
554
|
+
);
|
|
554
555
|
}
|
|
555
|
-
return
|
|
556
|
+
return conditions;
|
|
556
557
|
}
|
|
557
|
-
function
|
|
558
|
-
const
|
|
558
|
+
function listEmailsByFolder(db, folder, opts = {}) {
|
|
559
|
+
const conditions = buildFolderConditions(folder, opts);
|
|
560
|
+
return db.select().from(emails).where(and(...conditions)).orderBy(desc(emails.date), desc(emails.uid)).limit(opts.limit ?? 100).offset(opts.offset ?? 0).all();
|
|
561
|
+
}
|
|
562
|
+
function countEmailsInFolder(db, folder, opts = {}) {
|
|
563
|
+
const conditions = buildFolderConditions(folder, opts);
|
|
564
|
+
const row = db.select({ n: sql`count(*)` }).from(emails).where(and(...conditions)).get();
|
|
559
565
|
return row?.n ?? 0;
|
|
560
566
|
}
|
|
567
|
+
function listCachedUidsForFolder(db, folder) {
|
|
568
|
+
const rows = db.select({ uid: emails.uid }).from(emails).where(eq(emails.folder, folder)).all();
|
|
569
|
+
return rows.map((r) => r.uid);
|
|
570
|
+
}
|
|
561
571
|
function setEmailFlags(db, folder, uid, flags) {
|
|
562
572
|
db.update(emails).set({ flags }).where(and(eq(emails.folder, folder), eq(emails.uid, uid))).run();
|
|
563
573
|
}
|
|
@@ -650,15 +660,24 @@ async function runSync(ctx, folderPath, box, cached, uidValidityChanged) {
|
|
|
650
660
|
}
|
|
651
661
|
const serverModseq = box.highestModseq !== void 0 ? Number(box.highestModseq) : null;
|
|
652
662
|
if (serverModseq !== null && cached.highestModseq !== null && cached.highestModseq !== void 0) {
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
}
|
|
656
|
-
const fetched2 = await fetchAndStoreRange(ctx, folderPath, "1:*", BigInt(cached.highestModseq));
|
|
657
|
-
return { syncType: "incremental", fetched: fetched2 };
|
|
663
|
+
const fetched2 = serverModseq === cached.highestModseq ? 0 : await fetchAndStoreRange(ctx, folderPath, "1:*", BigInt(cached.highestModseq));
|
|
664
|
+
await reconcileUids(ctx, folderPath);
|
|
665
|
+
return { syncType: serverModseq === cached.highestModseq ? "skipped" : "incremental", fetched: fetched2 };
|
|
658
666
|
}
|
|
659
667
|
const fetched = await fetchAndStoreRange(ctx, folderPath, "1:*");
|
|
668
|
+
await reconcileUids(ctx, folderPath);
|
|
660
669
|
return { syncType: "full", fetched };
|
|
661
670
|
}
|
|
671
|
+
async function reconcileUids(ctx, folderPath) {
|
|
672
|
+
const cachedUids = listCachedUidsForFolder(ctx.db, folderPath);
|
|
673
|
+
if (cachedUids.length === 0) return;
|
|
674
|
+
const result = await ctx.imap.search({ all: true }, { uid: true });
|
|
675
|
+
const serverUids = new Set(Array.isArray(result) ? result : []);
|
|
676
|
+
const ghosts = cachedUids.filter((uid) => !serverUids.has(uid));
|
|
677
|
+
if (ghosts.length === 0) return;
|
|
678
|
+
deleteEmailsByUids(ctx.db, folderPath, ghosts);
|
|
679
|
+
log3.info("reconciled ghost UIDs", { folder: folderPath, removed: ghosts.length });
|
|
680
|
+
}
|
|
662
681
|
async function fetchAndStoreRange(ctx, folderPath, range, changedSince) {
|
|
663
682
|
const now = Date.now();
|
|
664
683
|
let count = 0;
|
|
@@ -1025,7 +1044,7 @@ async function syncIfStale(ctx, folder, maxStalenessSec) {
|
|
|
1025
1044
|
}
|
|
1026
1045
|
function readEnvelopes(ctx, folder, opts) {
|
|
1027
1046
|
const rows = listEmailsByFolder(ctx.db, folder, opts);
|
|
1028
|
-
const total = countEmailsInFolder(ctx.db, folder);
|
|
1047
|
+
const total = countEmailsInFolder(ctx.db, folder, opts);
|
|
1029
1048
|
const offset = opts.offset ?? 0;
|
|
1030
1049
|
const limit = opts.limit ?? 100;
|
|
1031
1050
|
const hasMore = offset + rows.length < total;
|
|
@@ -1325,15 +1344,28 @@ var searchEmailsTool = defineTool({
|
|
|
1325
1344
|
let matchingUids;
|
|
1326
1345
|
const imap = await ctx.imap.connection();
|
|
1327
1346
|
const lock = await imap.getMailboxLock(args.folder);
|
|
1347
|
+
let topUids;
|
|
1328
1348
|
try {
|
|
1329
1349
|
const result = await imap.search(criteria, { uid: true });
|
|
1330
1350
|
matchingUids = Array.isArray(result) ? result : [];
|
|
1351
|
+
topUids = [...matchingUids].reverse().slice(0, args.limit);
|
|
1352
|
+
const missingUids = topUids.filter((uid) => getEmail(ctx.db, args.folder, uid) === void 0);
|
|
1353
|
+
if (missingUids.length > 0) {
|
|
1354
|
+
const cachedAt = ctx.now();
|
|
1355
|
+
for await (const msg of imap.fetch(
|
|
1356
|
+
missingUids,
|
|
1357
|
+
{ envelope: true, flags: true, internalDate: true, bodyStructure: true },
|
|
1358
|
+
{ uid: true }
|
|
1359
|
+
)) {
|
|
1360
|
+
const insert = messageToInsert(args.folder, msg, cachedAt);
|
|
1361
|
+
if (insert) upsertEmail(ctx.db, insert);
|
|
1362
|
+
}
|
|
1363
|
+
}
|
|
1331
1364
|
} catch (err) {
|
|
1332
1365
|
lock.release();
|
|
1333
1366
|
throw mapImapError(err);
|
|
1334
1367
|
}
|
|
1335
1368
|
lock.release();
|
|
1336
|
-
const topUids = [...matchingUids].reverse().slice(0, args.limit);
|
|
1337
1369
|
const rows = [];
|
|
1338
1370
|
for (const uid of topUids) {
|
|
1339
1371
|
const row = getEmail(ctx.db, args.folder, uid);
|