@agenticmail/api 0.5.60 → 0.5.61

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.
Files changed (2) hide show
  1. package/dist/index.js +50 -7
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -1539,7 +1539,31 @@ async function closeCaches() {
1539
1539
  }
1540
1540
  receiverCache.clear();
1541
1541
  }
1542
- async function notifyLocalRecipientsOfNewMail(accountManager, toField, ccField, bccField, fromAgent, subject, messageId) {
1542
+ async function findUidByMessageId(receiver, messageId, maxAttempts = 5) {
1543
+ const client = receiver.getImapClient();
1544
+ for (let i = 0; i < maxAttempts; i++) {
1545
+ try {
1546
+ const lock = await client.getMailboxLock("INBOX");
1547
+ try {
1548
+ const results = await client.search(
1549
+ { header: ["Message-ID", messageId] },
1550
+ { uid: true }
1551
+ );
1552
+ if (Array.isArray(results) && results.length > 0) {
1553
+ return results[results.length - 1];
1554
+ }
1555
+ } finally {
1556
+ lock.release();
1557
+ }
1558
+ } catch {
1559
+ }
1560
+ if (i < maxAttempts - 1) {
1561
+ await new Promise((r) => setTimeout(r, 200 * (i + 1)));
1562
+ }
1563
+ }
1564
+ return 0;
1565
+ }
1566
+ async function notifyLocalRecipientsOfNewMail(accountManager, toField, ccField, bccField, fromAgent, subject, messageId, config) {
1543
1567
  const collected = [];
1544
1568
  const push = (v) => {
1545
1569
  if (!v) return;
@@ -1574,12 +1598,29 @@ async function notifyLocalRecipientsOfNewMail(accountManager, toField, ccField,
1574
1598
  }
1575
1599
  if (!recipient || notified.has(recipient.id)) continue;
1576
1600
  notified.add(recipient.id);
1601
+ let uid = 0;
1602
+ let lookup = "no-message-id";
1603
+ if (messageId) {
1604
+ try {
1605
+ const recipientPassword = getAgentPassword(recipient);
1606
+ const receiver = await getReceiver(
1607
+ recipient.stalwartPrincipal,
1608
+ recipientPassword,
1609
+ config
1610
+ );
1611
+ uid = await findUidByMessageId(receiver, messageId);
1612
+ lookup = uid > 0 ? "resolved" : "failed";
1613
+ } catch {
1614
+ lookup = "failed";
1615
+ }
1616
+ }
1577
1617
  pushEventToAgent(recipient.id, {
1578
1618
  type: "new",
1579
- // uid is unknown without an IMAP fetch; use 0 as a sentinel —
1580
- // this matches the watcher's autoFetch=false path. SSE consumers
1581
- // that want full message detail can call /mail/inbox.
1582
- uid: 0,
1619
+ uid,
1620
+ // Tell consumers whether the UID is real or a sentinel — preserves
1621
+ // backwards compat (uid is still always a number) while giving
1622
+ // clients a reliable signal to fall back to /mail/inbox.
1623
+ uidLookup: lookup,
1583
1624
  internal: true,
1584
1625
  from: { name: fromAgent.name, address: fromAgent.email },
1585
1626
  subject,
@@ -1726,7 +1767,8 @@ function createMailRoutes(accountManager, config, db, gatewayManager) {
1726
1767
  bcc,
1727
1768
  agent,
1728
1769
  subject,
1729
- result.messageId
1770
+ result.messageId,
1771
+ config
1730
1772
  ).catch((err) => {
1731
1773
  console.warn(`[mail] Internal SSE notify failed: ${err.message}`);
1732
1774
  });
@@ -2336,7 +2378,8 @@ function createMailRoutes(accountManager, config, db, gatewayManager) {
2336
2378
  mailOpts.bcc,
2337
2379
  agent,
2338
2380
  mailOpts.subject,
2339
- result.messageId
2381
+ result.messageId,
2382
+ config
2340
2383
  ).catch((err) => {
2341
2384
  console.warn(`[mail] Internal SSE notify (approve) failed: ${err.message}`);
2342
2385
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agenticmail/api",
3
- "version": "0.5.60",
3
+ "version": "0.5.61",
4
4
  "description": "REST API server for AgenticMail — email and SMS endpoints for AI agents",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -27,7 +27,7 @@
27
27
  "prepublishOnly": "npm run build"
28
28
  },
29
29
  "dependencies": {
30
- "@agenticmail/core": "^0.5.59",
30
+ "@agenticmail/core": "^0.5.61",
31
31
  "cors": "^2.8.5",
32
32
  "dotenv": "^16.4.7",
33
33
  "express": "^4.21.0",