agent-inbox 0.2.2 → 0.2.4

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 (149) hide show
  1. package/AGENTS.md +18 -0
  2. package/CLAUDE.md +92 -1
  3. package/README.md +73 -6
  4. package/bench/inbox-growth.bench.ts +224 -0
  5. package/dist/federation/connection-manager.d.ts +8 -0
  6. package/dist/federation/connection-manager.d.ts.map +1 -1
  7. package/dist/federation/connection-manager.js +12 -0
  8. package/dist/federation/connection-manager.js.map +1 -1
  9. package/dist/federation/delivery-queue.d.ts +11 -3
  10. package/dist/federation/delivery-queue.d.ts.map +1 -1
  11. package/dist/federation/delivery-queue.js +38 -8
  12. package/dist/federation/delivery-queue.js.map +1 -1
  13. package/dist/federation/queue-store.d.ts +42 -0
  14. package/dist/federation/queue-store.d.ts.map +1 -0
  15. package/dist/federation/queue-store.js +87 -0
  16. package/dist/federation/queue-store.js.map +1 -0
  17. package/dist/index.d.mts +2 -0
  18. package/dist/index.d.ts +29 -0
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.js +124 -1
  21. package/dist/index.js.map +1 -1
  22. package/dist/index.mjs +1 -0
  23. package/dist/index.mjs.map +1 -0
  24. package/dist/jsonrpc/mail-push-types.d.ts +9 -0
  25. package/dist/jsonrpc/mail-push-types.d.ts.map +1 -1
  26. package/dist/jsonrpc/mail-push-types.js +1 -0
  27. package/dist/jsonrpc/mail-push-types.js.map +1 -1
  28. package/dist/jsonrpc/mail-server.d.ts +8 -1
  29. package/dist/jsonrpc/mail-server.d.ts.map +1 -1
  30. package/dist/jsonrpc/mail-server.js +42 -1
  31. package/dist/jsonrpc/mail-server.js.map +1 -1
  32. package/dist/mail/address-book.d.ts +43 -0
  33. package/dist/mail/address-book.d.ts.map +1 -0
  34. package/dist/mail/address-book.js +95 -0
  35. package/dist/mail/address-book.js.map +1 -0
  36. package/dist/mail/attachment-store.d.ts +31 -0
  37. package/dist/mail/attachment-store.d.ts.map +1 -0
  38. package/dist/mail/attachment-store.js +74 -0
  39. package/dist/mail/attachment-store.js.map +1 -0
  40. package/dist/mail/email-mapper.d.ts +41 -0
  41. package/dist/mail/email-mapper.d.ts.map +1 -0
  42. package/dist/mail/email-mapper.js +216 -0
  43. package/dist/mail/email-mapper.js.map +1 -0
  44. package/dist/mail/fs-attachment-store.d.ts +38 -0
  45. package/dist/mail/fs-attachment-store.d.ts.map +1 -0
  46. package/dist/mail/fs-attachment-store.js +165 -0
  47. package/dist/mail/fs-attachment-store.js.map +1 -0
  48. package/dist/mail/mail-gateway.d.ts +114 -0
  49. package/dist/mail/mail-gateway.d.ts.map +1 -0
  50. package/dist/mail/mail-gateway.js +402 -0
  51. package/dist/mail/mail-gateway.js.map +1 -0
  52. package/dist/mail/provider-transport.d.ts +138 -0
  53. package/dist/mail/provider-transport.d.ts.map +1 -0
  54. package/dist/mail/provider-transport.js +434 -0
  55. package/dist/mail/provider-transport.js.map +1 -0
  56. package/dist/mail/rate-limiter.d.ts +20 -0
  57. package/dist/mail/rate-limiter.d.ts.map +1 -0
  58. package/dist/mail/rate-limiter.js +56 -0
  59. package/dist/mail/rate-limiter.js.map +1 -0
  60. package/dist/mail/smtp-transport.d.ts +141 -0
  61. package/dist/mail/smtp-transport.d.ts.map +1 -0
  62. package/dist/mail/smtp-transport.js +415 -0
  63. package/dist/mail/smtp-transport.js.map +1 -0
  64. package/dist/mail/types.d.ts +177 -0
  65. package/dist/mail/types.d.ts.map +1 -0
  66. package/dist/mail/types.js +11 -0
  67. package/dist/mail/types.js.map +1 -0
  68. package/dist/push/notifier.d.ts +21 -0
  69. package/dist/push/notifier.d.ts.map +1 -1
  70. package/dist/push/notifier.js +84 -2
  71. package/dist/push/notifier.js.map +1 -1
  72. package/dist/router/destination.d.ts +69 -0
  73. package/dist/router/destination.d.ts.map +1 -0
  74. package/dist/router/destination.js +106 -0
  75. package/dist/router/destination.js.map +1 -0
  76. package/dist/router/message-router.d.ts +15 -0
  77. package/dist/router/message-router.d.ts.map +1 -1
  78. package/dist/router/message-router.js +25 -3
  79. package/dist/router/message-router.js.map +1 -1
  80. package/dist/storage/interface.d.ts +21 -0
  81. package/dist/storage/interface.d.ts.map +1 -1
  82. package/dist/storage/memory.d.ts +12 -0
  83. package/dist/storage/memory.d.ts.map +1 -1
  84. package/dist/storage/memory.js +50 -0
  85. package/dist/storage/memory.js.map +1 -1
  86. package/dist/storage/sqlite.d.ts +14 -0
  87. package/dist/storage/sqlite.d.ts.map +1 -1
  88. package/dist/storage/sqlite.js +79 -1
  89. package/dist/storage/sqlite.js.map +1 -1
  90. package/dist/traceability/traceability.d.ts.map +1 -1
  91. package/dist/traceability/traceability.js +7 -17
  92. package/dist/traceability/traceability.js.map +1 -1
  93. package/dist/types.d.ts +80 -0
  94. package/dist/types.d.ts.map +1 -1
  95. package/docs/DESIGN.md +15 -0
  96. package/docs/MAIL-INTEROP-PLAN.md +660 -0
  97. package/package.json +29 -3
  98. package/renovate.json5 +6 -0
  99. package/rules/agent-inbox.md +1 -0
  100. package/src/federation/connection-manager.ts +12 -0
  101. package/src/federation/delivery-queue.ts +38 -8
  102. package/src/federation/queue-store.ts +124 -0
  103. package/src/index.ts +186 -1
  104. package/src/jsonrpc/mail-push-types.ts +10 -0
  105. package/src/jsonrpc/mail-server.ts +48 -1
  106. package/src/mail/address-book.ts +111 -0
  107. package/src/mail/attachment-store.ts +90 -0
  108. package/src/mail/email-mapper.ts +288 -0
  109. package/src/mail/fs-attachment-store.ts +163 -0
  110. package/src/mail/mail-gateway.ts +505 -0
  111. package/src/mail/provider-transport.ts +577 -0
  112. package/src/mail/rate-limiter.ts +51 -0
  113. package/src/mail/smtp-transport.ts +589 -0
  114. package/src/mail/types.ts +221 -0
  115. package/src/push/notifier.ts +98 -2
  116. package/src/router/destination.ts +140 -0
  117. package/src/router/message-router.ts +41 -4
  118. package/src/storage/interface.ts +22 -0
  119. package/src/storage/memory.ts +59 -0
  120. package/src/storage/sqlite.ts +114 -1
  121. package/src/traceability/traceability.ts +7 -16
  122. package/src/types.ts +74 -0
  123. package/test/federation/delivery-queue-sqlite.test.ts +158 -0
  124. package/test/load.test.ts +288 -0
  125. package/test/mail/address-book.test.ts +111 -0
  126. package/test/mail/attachment-store-contract.test.ts +92 -0
  127. package/test/mail/attachment-store.test.ts +69 -0
  128. package/test/mail/destination.test.ts +115 -0
  129. package/test/mail/dsn-parse.test.ts +239 -0
  130. package/test/mail/email-mapper.test.ts +341 -0
  131. package/test/mail/external-id.test.ts +43 -0
  132. package/test/mail/fs-attachment-store.test.ts +134 -0
  133. package/test/mail/full-flow-e2e.test.ts +200 -0
  134. package/test/mail/mail-gateway.test.ts +419 -0
  135. package/test/mail/mail-transport-contract.test.ts +134 -0
  136. package/test/mail/mock-mail.ts +161 -0
  137. package/test/mail/mock-postmark.ts +66 -0
  138. package/test/mail/provider-transport.test.ts +381 -0
  139. package/test/mail/rate-limiter.test.ts +48 -0
  140. package/test/mail/router-mail-integration.test.ts +138 -0
  141. package/test/mail/smtp-e2e.test.ts +98 -0
  142. package/test/mail/smtp-transport.test.ts +138 -0
  143. package/test/mail-presence.test.ts +149 -0
  144. package/test/mail-push.test.ts +44 -0
  145. package/test/mail-server.test.ts +25 -0
  146. package/test/push-notifier.test.ts +81 -0
  147. package/test/sqlite-storage.test.ts +106 -0
  148. package/test/storage.test.ts +92 -0
  149. package/vitest.bench.config.ts +8 -0
@@ -36,11 +36,21 @@ export class MailJsonRpcServer {
36
36
  private httpServer: http.Server | null = null;
37
37
  private subscribers = new Set<http.ServerResponse>();
38
38
 
39
+ /**
40
+ * Optional presence registry for resolving agent online/offline status.
41
+ * When provided, `mail/presence` enriches participants with live status.
42
+ */
43
+ private registry?: {
44
+ getStatus(agentId: string): string;
45
+ };
46
+
39
47
  constructor(
40
48
  private storage: Storage,
41
49
  private router: MessageRouter,
42
- private events: EventEmitter
50
+ private events: EventEmitter,
51
+ registry?: { getStatus(agentId: string): string }
43
52
  ) {
53
+ this.registry = registry;
44
54
  this.registerMethods();
45
55
  this.setupEventForwarding();
46
56
  }
@@ -95,6 +105,20 @@ export class MailJsonRpcServer {
95
105
  return { ok: true };
96
106
  });
97
107
 
108
+ // mail/reopen — reopen a completed conversation
109
+ this.methods.set("mail/reopen", (params) => {
110
+ const conv = this.storage.getConversation(params.id as string);
111
+ if (!conv) throw rpcError(-32001, "Conversation not found");
112
+ conv.status = "active";
113
+ conv.updated_at = new Date().toISOString();
114
+ this.storage.putConversation(conv);
115
+ this.events.emit("mail.reopened", {
116
+ conversation_id: conv.id,
117
+ status: conv.status,
118
+ });
119
+ return { conversationId: conv.id, status: "active" };
120
+ });
121
+
98
122
  // mail/join — add self as participant
99
123
  this.methods.set("mail/join", (params) => {
100
124
  const conv = this.storage.getConversation(
@@ -162,6 +186,7 @@ export class MailJsonRpcServer {
162
186
  );
163
187
  if (!conv) throw rpcError(-32001, "Conversation not found");
164
188
 
189
+ const importance = params.importance as Turn["importance"] | undefined;
165
190
  const turn: Turn = {
166
191
  id: `turn-${ulid()}`,
167
192
  conversation_id: conv.id,
@@ -172,6 +197,7 @@ export class MailJsonRpcServer {
172
197
  thread_id: params.threadId as string | undefined,
173
198
  in_reply_to: params.inReplyTo as string | undefined,
174
199
  created_at: new Date().toISOString(),
200
+ ...(importance ? { importance } : {}),
175
201
  };
176
202
 
177
203
  this.storage.addTurn(turn);
@@ -220,6 +246,24 @@ export class MailJsonRpcServer {
220
246
  threads: this.storage.getThreadsByConversation(conv.id),
221
247
  };
222
248
  });
249
+
250
+ // mail/presence — list participants with live presence status
251
+ this.methods.set("mail/presence", (params) => {
252
+ const conversationId = params.conversationId as string;
253
+ if (!conversationId) throw rpcError(-32602, "conversationId required");
254
+
255
+ const conv = this.storage.getConversation(conversationId);
256
+ if (!conv) throw rpcError(-32001, "Conversation not found");
257
+
258
+ const participants = (conv.participants ?? []).map((p) => ({
259
+ agent_id: p.agent_id,
260
+ role: p.role,
261
+ joined_at: p.joined_at,
262
+ presence: this.registry?.getStatus(p.agent_id) ?? "unknown",
263
+ }));
264
+
265
+ return { conversationId, participants };
266
+ });
223
267
  }
224
268
 
225
269
  /** Process a JSON-RPC request (used by both IPC and HTTP transports) */
@@ -355,6 +399,9 @@ export class MailJsonRpcServer {
355
399
  this.events.on("mail.closed", (data) => {
356
400
  this.broadcast("mail.closed", data);
357
401
  });
402
+ this.events.on("mail.reopened", (data) => {
403
+ this.broadcast("mail.reopened", data);
404
+ });
358
405
  }
359
406
 
360
407
  private broadcast(eventType: string, data: unknown): void {
@@ -0,0 +1,111 @@
1
+ /**
2
+ * Mail identity / address book.
3
+ *
4
+ * Maps between agent ids and external email addresses, and declares which
5
+ * domains we own (for inbound RCPT acceptance). Plus-addressing aware:
6
+ * `agent+scope@domain` resolves to agent `agent` in inbox scope `scope`.
7
+ *
8
+ * See docs/MAIL-INTEROP-PLAN.md §6.
9
+ */
10
+
11
+ import type { MailIdentityConfig } from "../types.js";
12
+ import type { MailAddress } from "./types.js";
13
+
14
+ export interface ResolvedInbound {
15
+ agentId: string;
16
+ /** Inbox scope from plus-addressing, if present. */
17
+ scope?: string;
18
+ }
19
+
20
+ export class AddressBook {
21
+ private localDomains: string[];
22
+ /** lower-cased address → agentId */
23
+ private byAddress = new Map<string, string>();
24
+ /** agentId → canonical address */
25
+ private byAgent = new Map<string, string>();
26
+ private catchAllAgentId?: string;
27
+ /** Address mappings that collided (same address → multiple agents). */
28
+ readonly conflicts: string[] = [];
29
+
30
+ constructor(config: MailIdentityConfig) {
31
+ this.localDomains = config.localDomains.map((d) => d.toLowerCase());
32
+ this.catchAllAgentId = config.catchAllAgentId;
33
+ for (const { agentId, address } of config.mappings) {
34
+ const lower = address.toLowerCase();
35
+ const existing = this.byAddress.get(lower);
36
+ if (existing !== undefined && existing !== agentId) {
37
+ // Two agents mapped to the same address — inbound routing would be
38
+ // ambiguous. Keep the first and record the collision for surfacing.
39
+ this.conflicts.push(`${address} → ${existing} & ${agentId}`);
40
+ continue;
41
+ }
42
+ this.byAddress.set(lower, agentId);
43
+ // First mapping for an agent is its canonical from-address.
44
+ if (!this.byAgent.has(agentId)) this.byAgent.set(agentId, address);
45
+ }
46
+ }
47
+
48
+ /** Primary local domain — used to mint Message-IDs and default from-addresses. */
49
+ primaryDomain(): string {
50
+ return this.localDomains[0] ?? "localhost";
51
+ }
52
+
53
+ /** True if `domain` (or a parent of it) is one we receive mail for. */
54
+ ownsDomain(domain: string): boolean {
55
+ const d = domain.toLowerCase();
56
+ return this.localDomains.some(
57
+ (own) => d === own || d.endsWith(`.${own}`)
58
+ );
59
+ }
60
+
61
+ /**
62
+ * Resolve an inbound recipient email to a local agent (+ scope).
63
+ * Returns undefined if the domain is not ours, or the mailbox is unknown and
64
+ * no catch-all is configured.
65
+ */
66
+ resolveInbound(address: string): ResolvedInbound | undefined {
67
+ const parsed = splitAddress(address);
68
+ if (!parsed) return undefined;
69
+ const { local, domain } = parsed;
70
+
71
+ if (!this.ownsDomain(domain)) return undefined;
72
+
73
+ // Plus-addressing: agent+scope → base agent, scope.
74
+ const plusIdx = local.indexOf("+");
75
+ const base = plusIdx === -1 ? local : local.slice(0, plusIdx);
76
+ const scope = plusIdx === -1 ? undefined : local.slice(plusIdx + 1) || undefined;
77
+
78
+ const canonical = `${base}@${domain}`.toLowerCase();
79
+ const agentId = this.byAddress.get(canonical);
80
+ if (agentId) return { agentId, scope };
81
+
82
+ if (this.catchAllAgentId) return { agentId: this.catchAllAgentId, scope };
83
+ return undefined;
84
+ }
85
+
86
+ /**
87
+ * Resolve the from-address for an outbound sender agent. Falls back to
88
+ * `<agentId>@<primaryDomain>` when the agent has no explicit mapping.
89
+ */
90
+ resolveFrom(agentId: string): MailAddress {
91
+ const mapped = this.byAgent.get(agentId);
92
+ if (mapped) return { address: mapped };
93
+ return { address: `${agentId}@${this.primaryDomain()}` };
94
+ }
95
+ }
96
+
97
+ /** Split an addr-spec into local-part and domain. Returns undefined if malformed. */
98
+ function splitAddress(
99
+ address: string
100
+ ): { local: string; domain: string } | undefined {
101
+ const at = address.indexOf("@");
102
+ if (at <= 0 || at !== address.lastIndexOf("@") || at === address.length - 1) {
103
+ // Reject empty local/domain and unquoted multiple "@" (e.g. "a@b@c"), so
104
+ // this layer agrees with parseAddress (which splits on the first "@").
105
+ return undefined;
106
+ }
107
+ const domain = address.slice(at + 1);
108
+ // A domain must not contain "/" (a federation scope separator) — defensive.
109
+ if (domain.includes("/")) return undefined;
110
+ return { local: address.slice(0, at), domain };
111
+ }
@@ -0,0 +1,90 @@
1
+ /**
2
+ * Attachment byte stores.
3
+ *
4
+ * `MailAttachment.contentRef` is opaque; bytes live behind an AttachmentStore.
5
+ * Default impl is SQLite-backed (atomic with the message, single-file backup,
6
+ * works with the in-memory DB used in tests). A filesystem impl is opt-in for
7
+ * large/high-volume deployments (added later). Refs are content-addressed
8
+ * (sha256) so identical bytes dedup. See docs/MAIL-INTEROP-PLAN.md §4c.
9
+ */
10
+
11
+ import type Database from "better-sqlite3";
12
+ import { createHash } from "node:crypto";
13
+ import type { AttachmentStore } from "./types.js";
14
+
15
+ export class SqliteAttachmentStore implements AttachmentStore {
16
+ private table: string;
17
+ private putStmt: Database.Statement;
18
+ private getStmt: Database.Statement;
19
+ private delStmt: Database.Statement;
20
+
21
+ /**
22
+ * @param db A better-sqlite3 handle (lifecycle owned by the caller).
23
+ * @param prefix Table name prefix to match the Storage convention. Default "".
24
+ */
25
+ constructor(
26
+ private db: Database.Database,
27
+ prefix: string = ""
28
+ ) {
29
+ this.table = `${prefix}mail_attachments`;
30
+ this.migrate();
31
+ this.putStmt = this.db.prepare(
32
+ `INSERT INTO ${this.table} (ref, content_type, filename, bytes, size_bytes, created_at)
33
+ VALUES (@ref, @content_type, @filename, @bytes, @size_bytes, @created_at)
34
+ ON CONFLICT(ref) DO NOTHING`
35
+ );
36
+ this.getStmt = this.db.prepare(
37
+ `SELECT bytes FROM ${this.table} WHERE ref = ?`
38
+ );
39
+ this.delStmt = this.db.prepare(`DELETE FROM ${this.table} WHERE ref = ?`);
40
+ }
41
+
42
+ private migrate(): void {
43
+ this.db.exec(`
44
+ CREATE TABLE IF NOT EXISTS ${this.table} (
45
+ ref TEXT PRIMARY KEY,
46
+ content_type TEXT NOT NULL,
47
+ filename TEXT,
48
+ bytes BLOB NOT NULL,
49
+ size_bytes INTEGER NOT NULL,
50
+ created_at TEXT NOT NULL
51
+ );
52
+ `);
53
+ }
54
+
55
+ async put(
56
+ bytes: Buffer,
57
+ meta: { contentType: string; filename?: string }
58
+ ): Promise<string> {
59
+ const ref = "sha256:" + createHash("sha256").update(bytes).digest("hex");
60
+ // ON CONFLICT DO NOTHING — content-addressed, so identical bytes are deduped.
61
+ this.putStmt.run({
62
+ ref,
63
+ content_type: meta.contentType,
64
+ filename: meta.filename ?? null,
65
+ bytes,
66
+ size_bytes: bytes.length,
67
+ created_at: new Date().toISOString(),
68
+ });
69
+ return ref;
70
+ }
71
+
72
+ async get(ref: string): Promise<Buffer> {
73
+ assertValidRef(ref);
74
+ const row = this.getStmt.get(ref) as { bytes: Buffer } | undefined;
75
+ if (!row) throw new Error(`Attachment not found: ${ref}`);
76
+ return row.bytes;
77
+ }
78
+
79
+ async delete(ref: string): Promise<void> {
80
+ assertValidRef(ref);
81
+ this.delStmt.run(ref);
82
+ }
83
+ }
84
+
85
+ /** Refs are always sha256-of-content; reject anything else (parity with FS store). */
86
+ function assertValidRef(ref: string): void {
87
+ if (!/^sha256:[0-9a-f]{64}$/.test(ref)) {
88
+ throw new Error(`Invalid attachment ref: ${ref}`);
89
+ }
90
+ }
@@ -0,0 +1,288 @@
1
+ /**
2
+ * Bidirectional mapping between RFC 5322 email and agent-inbox Messages.
3
+ *
4
+ * Parallels src/mesh/type-mapper.ts. Two pure functions:
5
+ * - inboundMailToMessage: external email → inbox Message
6
+ * - messageToOutboundMail: inbox Message → rendered OutboundMail
7
+ *
8
+ * Threading: RFC Message-ID / In-Reply-To / References map onto the inbox's
9
+ * thread_tag + in_reply_to. The RFC Message-ID is preserved in metadata so a
10
+ * later reply can reconstruct the In-Reply-To / References chain. See §5.
11
+ */
12
+
13
+ import { ulid } from "ulid";
14
+ import { createHash } from "node:crypto";
15
+ import { normalizeContent } from "../router/message-router.js";
16
+ import type { Message, Recipient, RecipientKind, Importance } from "../types.js";
17
+ import type { AddressBook } from "./address-book.js";
18
+ import type {
19
+ InboundMail,
20
+ OutboundMail,
21
+ MailAddress,
22
+ MailAttachment,
23
+ } from "./types.js";
24
+
25
+ // ---------------------------------------------------------------------------
26
+ // Contexts
27
+ // ---------------------------------------------------------------------------
28
+
29
+ export interface InboundMapContext {
30
+ addressBook: AddressBook;
31
+ /** Scope assigned when plus-addressing does not specify one. */
32
+ defaultScope?: string;
33
+ /**
34
+ * Resolve an RFC Message-ID we have already stored to its inbox Message id,
35
+ * so an inbound reply can set `in_reply_to` to the real parent.
36
+ */
37
+ lookupInboxIdByRfcMessageId?: (rfcMessageId: string) => string | undefined;
38
+ }
39
+
40
+ export interface OutboundMapContext {
41
+ addressBook: AddressBook;
42
+ /** Domain used to mint Message-IDs. Defaults to the address book primary domain. */
43
+ messageIdDomain?: string;
44
+ /** The RFC Message-ID we recorded when the parent inbox message was sent/received. */
45
+ lookupRfcMessageId?: (inboxId: string) => string | undefined;
46
+ /** The References chain recorded for the parent inbox message. */
47
+ lookupReferences?: (inboxId: string) => string[] | undefined;
48
+ }
49
+
50
+ /** Thrown when no inbound recipient resolves to a local agent. */
51
+ export class UnknownRecipientError extends Error {
52
+ constructor(public readonly recipients: string[]) {
53
+ super(
54
+ `No inbound recipient resolved to a local agent: ${recipients.join(", ")}`
55
+ );
56
+ this.name = "UnknownRecipientError";
57
+ }
58
+ }
59
+
60
+ // ---------------------------------------------------------------------------
61
+ // Inbound: email → Message
62
+ // ---------------------------------------------------------------------------
63
+
64
+ export function inboundMailToMessage(
65
+ mail: InboundMail,
66
+ ctx: InboundMapContext
67
+ ): Message {
68
+ const recipients: Recipient[] = [];
69
+ const unresolved: string[] = [];
70
+ let scope: string | undefined;
71
+
72
+ const addKind = (addrs: MailAddress[] | undefined, kind: RecipientKind) => {
73
+ for (const a of addrs ?? []) {
74
+ const resolved = ctx.addressBook.resolveInbound(a.address);
75
+ if (!resolved) {
76
+ unresolved.push(a.address);
77
+ continue;
78
+ }
79
+ recipients.push({ agent_id: resolved.agentId, kind });
80
+ // First resolved recipient with a plus-address scope wins. "to" is
81
+ // processed before "cc", so a to-scope still takes precedence, but a
82
+ // cc-only scoped recipient is no longer silently dropped.
83
+ if (scope === undefined && resolved.scope) {
84
+ scope = resolved.scope;
85
+ }
86
+ }
87
+ };
88
+
89
+ addKind(mail.to, "to");
90
+ addKind(mail.cc, "cc");
91
+
92
+ if (recipients.length === 0) {
93
+ throw new UnknownRecipientError(unresolved);
94
+ }
95
+
96
+ const rfcMessageId = mail.headers.messageId;
97
+ const rfcInReplyTo = mail.headers.inReplyTo;
98
+ const rfcReferences = mail.headers.references;
99
+
100
+ const metadata: Record<string, unknown> = {};
101
+ if (rfcMessageId) metadata.rfcMessageId = rfcMessageId;
102
+ if (rfcInReplyTo) metadata.rfcInReplyTo = rfcInReplyTo;
103
+ if (rfcReferences) metadata.rfcReferences = rfcReferences;
104
+ if (mail.html) metadata.htmlBody = mail.html;
105
+ if (mail.authResults) metadata.authResults = mail.authResults;
106
+ if (mail.remote) metadata.remote = mail.remote;
107
+ if (mail.envelopeFrom) metadata.envelopeFrom = mail.envelopeFrom;
108
+ if (mail.bounce) metadata.bounce = mail.bounce;
109
+ if (mail.attachments?.length) metadata.mailAttachments = mail.attachments;
110
+
111
+ // Resolve in_reply_to to a real inbox id when we can; otherwise leave it unset
112
+ // (the RFC id is retained in metadata for later correlation).
113
+ let inReplyTo: string | undefined;
114
+ if (rfcInReplyTo && ctx.lookupInboxIdByRfcMessageId) {
115
+ inReplyTo = ctx.lookupInboxIdByRfcMessageId(rfcInReplyTo);
116
+ }
117
+
118
+ const text = mail.text ?? "";
119
+
120
+ return {
121
+ id: ulid(),
122
+ scope: scope ?? ctx.defaultScope ?? "default",
123
+ sender_id: mail.from.address,
124
+ recipients,
125
+ subject: mail.subject,
126
+ content: normalizeContent(text),
127
+ thread_tag: deriveThreadTag(rfcReferences, rfcInReplyTo, rfcMessageId),
128
+ in_reply_to: inReplyTo,
129
+ importance: importanceFromHeaders(mail.headers.raw),
130
+ metadata,
131
+ created_at: mail.receivedAt,
132
+ };
133
+ }
134
+
135
+ // ---------------------------------------------------------------------------
136
+ // Outbound: Message → email
137
+ // ---------------------------------------------------------------------------
138
+
139
+ export function messageToOutboundMail(
140
+ msg: Message,
141
+ ctx: OutboundMapContext
142
+ ): OutboundMail {
143
+ const domain = ctx.messageIdDomain ?? ctx.addressBook.primaryDomain();
144
+
145
+ const to = recipientsOfKind(msg, "to");
146
+ const cc = recipientsOfKind(msg, "cc");
147
+ const bcc = recipientsOfKind(msg, "bcc");
148
+
149
+ const { inReplyTo, references } = buildThreadingHeaders(msg, ctx);
150
+
151
+ const text = textFromContent(msg);
152
+ const html = typeof msg.metadata.htmlBody === "string"
153
+ ? (msg.metadata.htmlBody as string)
154
+ : undefined;
155
+
156
+ const attachments = validateAttachments(msg.metadata.mailAttachments);
157
+
158
+ // Only forward an explicit, opt-in hints object to the backend. We do NOT
159
+ // dump arbitrary internal message metadata to the external mail provider.
160
+ const backendHints =
161
+ msg.metadata.mailBackendHints &&
162
+ typeof msg.metadata.mailBackendHints === "object"
163
+ ? (msg.metadata.mailBackendHints as Record<string, unknown>)
164
+ : undefined;
165
+
166
+ return {
167
+ idempotencyKey: msg.id,
168
+ from: ctx.addressBook.resolveFrom(msg.sender_id),
169
+ to,
170
+ ...(cc.length ? { cc } : {}),
171
+ ...(bcc.length ? { bcc } : {}),
172
+ subject: msg.subject,
173
+ text,
174
+ ...(html ? { html } : {}),
175
+ headers: {
176
+ messageId: mintMessageId(msg.id, domain),
177
+ ...(inReplyTo ? { inReplyTo } : {}),
178
+ ...(references && references.length ? { references } : {}),
179
+ },
180
+ ...(attachments ? { attachments } : {}),
181
+ ...(backendHints ? { backendHints } : {}),
182
+ };
183
+ }
184
+
185
+ /** Validate untrusted attachment metadata, dropping malformed entries. */
186
+ function validateAttachments(value: unknown): MailAttachment[] | undefined {
187
+ if (!Array.isArray(value)) return undefined;
188
+ const out: MailAttachment[] = [];
189
+ for (const a of value) {
190
+ if (
191
+ a &&
192
+ typeof a === "object" &&
193
+ typeof (a as MailAttachment).contentRef === "string" &&
194
+ typeof (a as MailAttachment).contentType === "string"
195
+ ) {
196
+ const att = a as MailAttachment;
197
+ out.push({
198
+ contentRef: att.contentRef,
199
+ contentType: att.contentType,
200
+ sizeBytes: typeof att.sizeBytes === "number" ? att.sizeBytes : 0,
201
+ ...(typeof att.filename === "string" ? { filename: att.filename } : {}),
202
+ ...(typeof att.contentId === "string" ? { contentId: att.contentId } : {}),
203
+ });
204
+ }
205
+ }
206
+ return out.length ? out : undefined;
207
+ }
208
+
209
+ // ---------------------------------------------------------------------------
210
+ // Helpers
211
+ // ---------------------------------------------------------------------------
212
+
213
+ function recipientsOfKind(msg: Message, kind: RecipientKind): MailAddress[] {
214
+ return msg.recipients
215
+ .filter((r) => r.kind === kind)
216
+ .map((r) => ({ address: r.agent_id }));
217
+ }
218
+
219
+ /** Stable thread tag from the root of the References chain. */
220
+ function deriveThreadTag(
221
+ references: string[] | undefined,
222
+ inReplyTo: string | undefined,
223
+ messageId: string | undefined
224
+ ): string | undefined {
225
+ const root =
226
+ (references && references.length > 0 ? references[0] : undefined) ??
227
+ inReplyTo ??
228
+ messageId;
229
+ if (!root) return undefined;
230
+ const hash = createHash("sha1").update(root).digest("hex").slice(0, 12);
231
+ return `mail-${hash}`;
232
+ }
233
+
234
+ function buildThreadingHeaders(
235
+ msg: Message,
236
+ ctx: OutboundMapContext
237
+ ): { inReplyTo?: string; references?: string[] } {
238
+ if (!msg.in_reply_to) {
239
+ // Fresh thread, but a recorded RFC References chain (e.g. from the inbound
240
+ // side) should still be carried if present in metadata.
241
+ const meta = msg.metadata.rfcReferences;
242
+ return { references: Array.isArray(meta) ? (meta as string[]) : undefined };
243
+ }
244
+
245
+ const domain = ctx.messageIdDomain ?? ctx.addressBook.primaryDomain();
246
+ const parentRfcId =
247
+ ctx.lookupRfcMessageId?.(msg.in_reply_to) ??
248
+ mintMessageId(msg.in_reply_to, domain);
249
+
250
+ const parentRefs =
251
+ ctx.lookupReferences?.(msg.in_reply_to) ??
252
+ (Array.isArray(msg.metadata.rfcReferences)
253
+ ? (msg.metadata.rfcReferences as string[])
254
+ : []);
255
+
256
+ return {
257
+ inReplyTo: parentRfcId,
258
+ references: [...parentRefs, parentRfcId],
259
+ };
260
+ }
261
+
262
+ function mintMessageId(inboxId: string, domain: string): string {
263
+ return `<${inboxId}@${domain}>`;
264
+ }
265
+
266
+ function textFromContent(msg: Message): string {
267
+ const c = msg.content;
268
+ if (c.type === "text" && typeof c.text === "string") return c.text;
269
+ return JSON.stringify(c);
270
+ }
271
+
272
+ function importanceFromHeaders(
273
+ raw: Record<string, string | string[]>
274
+ ): Importance {
275
+ const header = (k: string): string => {
276
+ const v = raw[k];
277
+ return (Array.isArray(v) ? v[0] : v ?? "").toString().toLowerCase();
278
+ };
279
+ const importance = header("importance");
280
+ const priority = header("x-priority");
281
+ if (importance === "high" || priority.startsWith("1") || priority.startsWith("2")) {
282
+ return "high";
283
+ }
284
+ if (importance === "low" || priority.startsWith("4") || priority.startsWith("5")) {
285
+ return "low";
286
+ }
287
+ return "normal";
288
+ }