langmail-mcp 0.1.0 → 0.2.1

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 (137) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +89 -169
  3. package/dist/chunk-ad2666h8.js +12696 -0
  4. package/dist/langmail-mcp.js +7 -0
  5. package/package.json +94 -98
  6. package/THIRD_PARTY_NOTICES.md +0 -388
  7. package/dist/attachments/download.js +0 -69
  8. package/dist/attachments/routes.js +0 -42
  9. package/dist/attachments/tickets.js +0 -45
  10. package/dist/attachments/upload-tickets.js +0 -43
  11. package/dist/attachments/upload.js +0 -136
  12. package/dist/caldav/account-settings.js +0 -92
  13. package/dist/caldav/agenda.js +0 -277
  14. package/dist/caldav/alarm.js +0 -97
  15. package/dist/caldav/client.js +0 -408
  16. package/dist/caldav/ical.js +0 -248
  17. package/dist/caldav/occurrence.js +0 -534
  18. package/dist/caldav/recurrence.js +0 -313
  19. package/dist/caldav/rsvp.js +0 -107
  20. package/dist/caldav/scheduling-handover.js +0 -22
  21. package/dist/caldav/timezone.js +0 -103
  22. package/dist/caldav/types.js +0 -5
  23. package/dist/config.js +0 -219
  24. package/dist/contacts/address-class.js +0 -79
  25. package/dist/contacts/birthdays.js +0 -81
  26. package/dist/contacts/card.js +0 -58
  27. package/dist/contacts/constants.js +0 -46
  28. package/dist/contacts/fold.js +0 -309
  29. package/dist/contacts/index.js +0 -10
  30. package/dist/contacts/merge.js +0 -388
  31. package/dist/contacts/observations.js +0 -115
  32. package/dist/contacts/profile.js +0 -402
  33. package/dist/contacts/scoring.js +0 -146
  34. package/dist/contacts/types.js +0 -7
  35. package/dist/contacts/upsert.js +0 -131
  36. package/dist/drafts/attachments.js +0 -254
  37. package/dist/drafts/idempotency.js +0 -60
  38. package/dist/drafts/send-guard.js +0 -184
  39. package/dist/email-render/email-viewer.html +0 -143
  40. package/dist/email-render/image-proxy.js +0 -389
  41. package/dist/email-render/meta.js +0 -48
  42. package/dist/email-render/sanitize.js +0 -93
  43. package/dist/email-render/viewer-resource.js +0 -104
  44. package/dist/http/bearer.js +0 -30
  45. package/dist/http/guard.js +0 -57
  46. package/dist/http/hosts.js +0 -10
  47. package/dist/http/listener.js +0 -170
  48. package/dist/http/routes.js +0 -72
  49. package/dist/ical/ical-parse.js +0 -357
  50. package/dist/ical/index.js +0 -4
  51. package/dist/ical/timezone.js +0 -22
  52. package/dist/ical/windows-zones.js +0 -155
  53. package/dist/index.js +0 -79
  54. package/dist/jmap/blob.js +0 -49
  55. package/dist/jmap/contacts.js +0 -226
  56. package/dist/jmap/drafts.js +0 -480
  57. package/dist/jmap/email.js +0 -668
  58. package/dist/jmap/index.js +0 -12
  59. package/dist/jmap/keywords.js +0 -22
  60. package/dist/jmap/live.js +0 -21
  61. package/dist/jmap/origin.js +0 -113
  62. package/dist/jmap/patch.js +0 -29
  63. package/dist/jmap/request.js +0 -37
  64. package/dist/jmap/session.js +0 -89
  65. package/dist/jmap/sieve.js +0 -71
  66. package/dist/jmap/submission.js +0 -107
  67. package/dist/jmap/timeout.js +0 -30
  68. package/dist/jmap/types.js +0 -6
  69. package/dist/logger.js +0 -48
  70. package/dist/mcp/handler.js +0 -255
  71. package/dist/mcp/http.js +0 -29
  72. package/dist/mcp/instructions.js +0 -44
  73. package/dist/mcp/links.js +0 -64
  74. package/dist/mcp/tool-logging.js +0 -67
  75. package/dist/observability/errors.js +0 -41
  76. package/dist/rules/apply.js +0 -236
  77. package/dist/rules/rule.js +0 -280
  78. package/dist/rules/store.js +0 -92
  79. package/dist/tools/annotations.js +0 -71
  80. package/dist/tools/archive-emails.js +0 -40
  81. package/dist/tools/calendar-lookup.js +0 -64
  82. package/dist/tools/contact-common.js +0 -146
  83. package/dist/tools/contact-format.js +0 -72
  84. package/dist/tools/contact-write-through.js +0 -37
  85. package/dist/tools/create-calendar-event.js +0 -231
  86. package/dist/tools/create-calendar.js +0 -54
  87. package/dist/tools/create-contact.js +0 -172
  88. package/dist/tools/create-draft.js +0 -369
  89. package/dist/tools/create-mail-rule.js +0 -101
  90. package/dist/tools/delete-calendar-event.js +0 -165
  91. package/dist/tools/delete-contact.js +0 -60
  92. package/dist/tools/delete-draft.js +0 -129
  93. package/dist/tools/delete-mail-rule.js +0 -38
  94. package/dist/tools/download-attachment.js +0 -90
  95. package/dist/tools/download-email.js +0 -112
  96. package/dist/tools/draft-common.js +0 -600
  97. package/dist/tools/email-types.js +0 -58
  98. package/dist/tools/error-detail.js +0 -10
  99. package/dist/tools/file-emails-common.js +0 -75
  100. package/dist/tools/format.js +0 -131
  101. package/dist/tools/forward-message.js +0 -237
  102. package/dist/tools/forward-quote.js +0 -179
  103. package/dist/tools/get-draft.js +0 -61
  104. package/dist/tools/get-email.js +0 -164
  105. package/dist/tools/get-thread.js +0 -67
  106. package/dist/tools/keywords.js +0 -43
  107. package/dist/tools/list-calendar-events.js +0 -180
  108. package/dist/tools/list-calendars.js +0 -44
  109. package/dist/tools/list-drafts.js +0 -134
  110. package/dist/tools/list-mail-rules.js +0 -28
  111. package/dist/tools/list-unsubscribe.js +0 -152
  112. package/dist/tools/list-upcoming-birthdays.js +0 -165
  113. package/dist/tools/mail-rule-common.js +0 -43
  114. package/dist/tools/mail-rule-format.js +0 -79
  115. package/dist/tools/mailbox-lookup.js +0 -13
  116. package/dist/tools/mark-emails.js +0 -78
  117. package/dist/tools/merge-contacts.js +0 -145
  118. package/dist/tools/move-calendar-events.js +0 -340
  119. package/dist/tools/move-emails.js +0 -64
  120. package/dist/tools/one-click-post.js +0 -275
  121. package/dist/tools/preview-email.js +0 -204
  122. package/dist/tools/preview-mail-rule.js +0 -79
  123. package/dist/tools/reply-quote.js +0 -426
  124. package/dist/tools/reply-to-message.js +0 -292
  125. package/dist/tools/respond-to-event.js +0 -110
  126. package/dist/tools/result.js +0 -9
  127. package/dist/tools/search-contacts.js +0 -431
  128. package/dist/tools/search-emails.js +0 -570
  129. package/dist/tools/send-draft.js +0 -308
  130. package/dist/tools/trash-emails.js +0 -36
  131. package/dist/tools/unsubscribe-from-list.js +0 -249
  132. package/dist/tools/update-calendar-event.js +0 -973
  133. package/dist/tools/update-calendar.js +0 -100
  134. package/dist/tools/update-contact.js +0 -382
  135. package/dist/tools/update-draft.js +0 -714
  136. package/dist/tools/update-mail-rule.js +0 -103
  137. package/dist/tools/upload-attachments.js +0 -126
@@ -1,388 +0,0 @@
1
- // Merging contact cards: the collected-history merge shared by the
2
- // collection fold (a Collected card absorbed into a personal one) and by
3
- // merge_contacts (cards an agent folds into one), plus the pure card merge
4
- // behind merge_contacts. No IO: the caller reads the cards and applies the
5
- // patch.
6
- //
7
- // "Remember the merge" needs no store of its own: the merged addresses
8
- // become addresses of the kept card, and collection already finds the card
9
- // holding an address, so a later sighting of any of them updates the kept
10
- // card (there is no contact graph outside Stalwart).
11
- import { normalizeAddress } from "./address-class.js";
12
- import { collectedNoteEntry, nextKey, normalizeName, readCollected, } from "./card.js";
13
- import { COLLECTED_NOTE_KEY, INTERACTION_LOG_SIZE, RELAY_LIST_SIZE, } from "./constants.js";
14
- import { formatBirthday, formatCardAddress, readBirthday, readOrganization, readRoles, withBirthday, withRoles, } from "./profile.js";
15
- import { bestAddress } from "./scoring.js";
16
- const CONFIDENCE_RANK = {
17
- confirmed: 2,
18
- strong: 1,
19
- weak: 0,
20
- };
21
- const time = (iso) => new Date(iso).getTime();
22
- const earlier = (a, b) => (time(b) < time(a) ? b : a);
23
- const later = (a, b) => (time(b) > time(a) ? b : a);
24
- const mergeEntry = (mine, theirs) => mine
25
- ? {
26
- confidence: CONFIDENCE_RANK[theirs.confidence] > CONFIDENCE_RANK[mine.confidence]
27
- ? theirs.confidence
28
- : mine.confidence,
29
- count: mine.count + theirs.count,
30
- firstSeen: earlier(mine.firstSeen, theirs.firstSeen),
31
- lastSeen: later(mine.lastSeen, theirs.lastSeen),
32
- }
33
- : { ...theirs };
34
- // Folds one card's collected history into another's: per address, counts
35
- // summed, the earliest first-seen, the latest last-seen and the highest
36
- // confidence; interaction log and relays unioned under their caps. `into`
37
- // keeps its own `source`, so an imported card never turns derived by
38
- // absorbing a collected one. The absorbed card's id is recorded, and a card
39
- // already recorded is not summed again: a merge whose destroy never landed
40
- // can be retried without doubling the counts.
41
- export const mergeCollectedExtensions = (into, from, fromId) => {
42
- if (into.absorbed?.includes(fromId)) {
43
- return into;
44
- }
45
- const addresses = { ...into.addresses };
46
- for (const [address, entry] of Object.entries(from.addresses)) {
47
- addresses[address] = mergeEntry(addresses[address], entry);
48
- }
49
- const logged = new Set(into.interactions.map((t) => t.recordId));
50
- const interactions = [
51
- ...into.interactions,
52
- ...from.interactions.filter((t) => !logged.has(t.recordId)),
53
- ]
54
- .sort((a, b) => time(b.date) - time(a.date))
55
- .slice(0, INTERACTION_LOG_SIZE);
56
- const relays = [
57
- ...new Set([...(into.relays ?? []), ...(from.relays ?? [])]),
58
- ].slice(0, RELAY_LIST_SIZE);
59
- return {
60
- ...into,
61
- // The absorbed card's own absorbed ids come along, so a nested merge
62
- // (C into B, then B into A) still guards a retry of C into A.
63
- absorbed: [
64
- ...new Set([...(into.absorbed ?? []), ...(from.absorbed ?? []), fromId]),
65
- ],
66
- addresses,
67
- interactions,
68
- ...(relays.length > 0 ? { relays } : {}),
69
- };
70
- };
71
- // --- preferred address -------------------------------------------------------
72
- // RFC 9553 §1.5.4: 1 is the most preferred.
73
- const PREFERRED = 1;
74
- const prefOf = (entry) => typeof entry.pref === "number" ? entry.pref : Number.POSITIVE_INFINITY;
75
- // The address the card says mail should go to: the one carrying the lowest
76
- // `pref`, else the best-graded one.
77
- export const preferredAddress = (card) => {
78
- const marked = Object.values(card.emails ?? {})
79
- .filter((e) => typeof e.pref === "number")
80
- .sort((a, b) => prefOf(a) - prefOf(b))[0];
81
- return marked
82
- ? normalizeAddress(marked.address)
83
- : bestAddress(card, "any")?.address;
84
- };
85
- // The emails with `address` as the single preferred one: `pref` set on it,
86
- // removed from every other entry. Undefined when the card does not hold it.
87
- export const withPreferredEmail = (emails, address) => {
88
- const wanted = normalizeAddress(address);
89
- if (!Object.values(emails).some((e) => normalizeAddress(e.address) === wanted)) {
90
- return;
91
- }
92
- return Object.fromEntries(Object.entries(emails).map(([key, entry]) => {
93
- const { pref: _dropped, ...rest } = entry;
94
- return [
95
- key,
96
- normalizeAddress(entry.address) === wanted
97
- ? { ...rest, pref: PREFERRED }
98
- : rest,
99
- ];
100
- }));
101
- };
102
- // --- card merge --------------------------------------------------------------
103
- // The JSContact properties the merge carries over or decides about. Anything
104
- // else a merged card holds (a photo, a nickname, a title) goes with the
105
- // card, and the merge reports it rather than dropping it silently.
106
- const MERGED_PROPERTIES = new Set([
107
- "@type",
108
- "addressBookIds",
109
- "addresses",
110
- "anniversaries",
111
- "created",
112
- "emails",
113
- "id",
114
- "kind",
115
- "name",
116
- "notes",
117
- "organizations",
118
- "phones",
119
- "prodId",
120
- "titles",
121
- "uid",
122
- "updated",
123
- "version",
124
- ]);
125
- const labelOf = (card) => `"${card.name?.full ?? Object.values(card.emails ?? {})[0]?.address ?? card.id}" (contact_id ${card.id})`;
126
- // Formatting punctuation only: letters stay, so `… ext. 7` is not taken for
127
- // the same number with a 7 appended.
128
- const phoneKey = (number) => number.toLowerCase().replace(/[\s().\-/]/gu, "");
129
- const noteText = (entry) => (entry?.note ?? "").trim();
130
- const unionInto = (target, source, prefix, identity) => {
131
- const present = new Set(Object.values(target).map(identity));
132
- let added = false;
133
- for (const value of Object.values(source ?? {})) {
134
- const id = identity(value);
135
- if (present.has(id)) {
136
- continue;
137
- }
138
- present.add(id);
139
- // A merged entry arrives without its own `pref`: the kept card's order
140
- // of preference stays as it was.
141
- const { pref: _dropped, ...rest } = value;
142
- target[nextKey(target, prefix)] = rest;
143
- added = true;
144
- }
145
- return added;
146
- };
147
- const mergeEmails = (kept, merged, preferredEmail) => {
148
- const emails = { ...(kept.emails ?? {}) };
149
- const before = new Set(Object.values(emails).map((e) => normalizeAddress(e.address)));
150
- for (const card of merged) {
151
- unionInto(emails, card.emails, "e", (e) => normalizeAddress(e.address));
152
- }
153
- const movedEmails = Object.values(emails)
154
- .map((e) => normalizeAddress(e.address))
155
- .filter((a) => !before.has(a));
156
- if (preferredEmail !== undefined) {
157
- const pinned = withPreferredEmail(emails, preferredEmail);
158
- if (!pinned) {
159
- const available = Object.values(emails)
160
- .map((e) => e.address)
161
- .join(", ");
162
- return {
163
- error: `${preferredEmail} is on none of these contacts. Their addresses: ${available}.`,
164
- movedEmails,
165
- };
166
- }
167
- return {
168
- emails: pinned,
169
- movedEmails,
170
- preferred: {
171
- address: normalizeAddress(preferredEmail),
172
- how: "requested",
173
- },
174
- };
175
- }
176
- const changed = movedEmails.length > 0 ? { emails } : {};
177
- // Without a choice from the caller, the kept card's preferred address stays
178
- // preferred. Both a joining address and the merged history (a higher grade
179
- // or a later sighting of an address the kept card already holds) could
180
- // otherwise outrank it, quietly changing who mail goes to.
181
- const current = preferredAddress(kept);
182
- if (current === undefined) {
183
- return { ...changed, movedEmails };
184
- }
185
- if (Object.values(kept.emails ?? {}).some((e) => typeof e.pref === "number")) {
186
- return {
187
- ...changed,
188
- movedEmails,
189
- preferred: { address: current, how: "unchanged" },
190
- };
191
- }
192
- // A single address has nothing to be preferred over: no mark to write.
193
- if (Object.keys(emails).length < 2) {
194
- return { ...changed, movedEmails };
195
- }
196
- const pinned = withPreferredEmail(emails, current);
197
- return {
198
- ...(pinned ? { emails: pinned } : changed),
199
- movedEmails,
200
- preferred: { address: current, how: "pinned" },
201
- };
202
- };
203
- const mergeName = (kept, merged, conflicts) => {
204
- let name = kept.name?.full?.trim() || undefined;
205
- // The whole name object of the card that fills a blank one, so structured
206
- // parts a CardDAV client stored beside `full` come along.
207
- let filled;
208
- for (const card of merged) {
209
- const theirs = card.name?.full?.trim();
210
- if (!theirs) {
211
- continue;
212
- }
213
- if (name === undefined) {
214
- name = theirs;
215
- filled = card.name;
216
- }
217
- else if (normalizeName(theirs) !== normalizeName(name)) {
218
- conflicts.push(`name: kept "${name}", dropped "${theirs}" from ${labelOf(card)}`);
219
- }
220
- }
221
- return filled ? { name: filled } : {};
222
- };
223
- const mergeOrganization = (kept, merged, conflicts) => {
224
- let organization = readOrganization(kept);
225
- let patch = {};
226
- for (const card of merged) {
227
- const theirs = readOrganization(card);
228
- if (!theirs) {
229
- continue;
230
- }
231
- if (organization === undefined) {
232
- organization = theirs;
233
- // The entry readOrganization shows, alone: any further ones are
234
- // reported with the card's other leftovers.
235
- const [key, entry] = Object.entries(card.organizations ?? {}).find(([, o]) => o.name === theirs) ?? [];
236
- patch = key && entry ? { organizations: { [key]: entry } } : {};
237
- }
238
- else if (theirs.toLowerCase() !== organization.toLowerCase()) {
239
- conflicts.push(`organization: kept "${organization}", dropped "${theirs}" from ${labelOf(card)}`);
240
- }
241
- }
242
- return patch;
243
- };
244
- const mergeBirthday = (kept, merged, conflicts) => {
245
- let birthday = readBirthday(kept);
246
- let patch = {};
247
- for (const card of merged) {
248
- const theirs = readBirthday(card);
249
- if (!theirs) {
250
- continue;
251
- }
252
- if (birthday === undefined) {
253
- birthday = theirs;
254
- patch = { anniversaries: withBirthday(kept, theirs) };
255
- }
256
- else if (formatBirthday(theirs) !== formatBirthday(birthday)) {
257
- conflicts.push(`birthday: kept ${formatBirthday(birthday)}, dropped ${formatBirthday(theirs)} from ${labelOf(card)}`);
258
- }
259
- }
260
- return patch;
261
- };
262
- // Roles are what the user said each card's person is to them, so all of
263
- // them carry over, without duplicates. A merged card's job titles do not:
264
- // the kept card's own job title stands, and the others are reported.
265
- const mergeRoles = (kept, merged, conflicts) => {
266
- const before = readRoles(kept);
267
- const titles = withRoles(kept, merged.flatMap(readRoles), []);
268
- const jobTitle = (t) => t.name.trim().toLowerCase();
269
- const keptJobs = new Set(Object.values(kept.titles ?? {})
270
- .filter((t) => t.kind !== "role")
271
- .map(jobTitle));
272
- for (const card of merged) {
273
- if (Object.values(card.titles ?? {}).some((t) => t.kind !== "role" && !keptJobs.has(jobTitle(t)))) {
274
- conflicts.push(`not carried over from ${labelOf(card)}: job titles`);
275
- }
276
- }
277
- return readRoles({ titles }).length > before.length ? { titles } : {};
278
- };
279
- // The kept card's note entries stay; each merged card's user notes join
280
- // under fresh keys unless the same text is already there. The collected
281
- // entry carries the merged history. Entries go through patch paths when the
282
- // card has a `notes` property, so a note another client added in the
283
- // meantime survives; a card without one takes a full set (RFC 8620 §5.3
284
- // rejects a patch path whose parent is missing).
285
- const mergeNotes = (kept, merged) => {
286
- const notes = { ...(kept.notes ?? {}) };
287
- const added = {};
288
- const present = new Set(Object.entries(notes)
289
- .filter(([key]) => key !== COLLECTED_NOTE_KEY)
290
- .map(([, entry]) => noteText(entry)));
291
- for (const card of merged) {
292
- for (const [key, entry] of Object.entries(card.notes ?? {})) {
293
- const text = noteText(entry);
294
- if (key === COLLECTED_NOTE_KEY ||
295
- text.length === 0 ||
296
- present.has(text)) {
297
- continue;
298
- }
299
- present.add(text);
300
- const fresh = nextKey(notes, "n");
301
- notes[fresh] = entry;
302
- added[fresh] = entry;
303
- }
304
- }
305
- // A card another merged card already absorbed (an earlier merge whose
306
- // destroy failed) goes last: by then that card has brought its id into
307
- // `absorbed`, so its history is not summed a second time, whatever order
308
- // the caller listed them in.
309
- const viaOthers = new Set(merged.flatMap((card) => readCollected(card).absorbed ?? []));
310
- const ordered = [
311
- ...merged.filter((c) => !viaOthers.has(c.id)),
312
- ...merged.filter((c) => viaOthers.has(c.id)),
313
- ];
314
- let ext = readCollected(kept);
315
- for (const card of ordered) {
316
- ext = mergeCollectedExtensions(ext, readCollected(card), card.id);
317
- }
318
- const collected = collectedNoteEntry(ext);
319
- if (typeof kept.notes !== "object" || kept.notes === null) {
320
- return { notes: { ...added, [COLLECTED_NOTE_KEY]: collected } };
321
- }
322
- return {
323
- ...Object.fromEntries(Object.entries(added).map(([key, entry]) => [`notes/${key}`, entry])),
324
- [`notes/${COLLECTED_NOTE_KEY}`]: collected,
325
- };
326
- };
327
- const unmodeled = (kept, merged, conflicts) => {
328
- for (const card of merged) {
329
- const extra = Object.entries(card)
330
- .filter(([key, value]) => value !== null && value !== undefined && !MERGED_PROPERTIES.has(key))
331
- .map(([key]) => key);
332
- const otherDates = Object.values(card.anniversaries ?? {}).filter((a) => a.kind !== "birth").length;
333
- if (otherDates > 0) {
334
- extra.push("anniversaries other than the birthday");
335
- }
336
- const kind = card.kind;
337
- if (typeof kind === "string" &&
338
- kind !== (kept.kind ?? "individual")) {
339
- extra.push(`kind "${kind}"`);
340
- }
341
- if (Object.keys(card.organizations ?? {}).length > 1) {
342
- extra.push("organizations after the first");
343
- }
344
- if (extra.length > 0) {
345
- conflicts.push(`not carried over from ${labelOf(card)}: ${extra.join(", ")}`);
346
- }
347
- }
348
- };
349
- // Folds `merged` into `kept`. The kept card's name, organization and
350
- // birthday win; a blank one is filled from the first merged card holding
351
- // it, and every other differing value is listed as a conflict. Emails,
352
- // phones and postal addresses are joined without duplicates, notes
353
- // combined, and the collected histories merged. The preferred address stays
354
- // the kept card's unless `preferredEmail` names another.
355
- export const mergeCards = (kept, merged, preferredEmail) => {
356
- const conflicts = [];
357
- const email = mergeEmails(kept, merged, preferredEmail);
358
- if (email.error) {
359
- return { error: email.error };
360
- }
361
- const phones = { ...(kept.phones ?? {}) };
362
- const phonesAdded = merged
363
- .map((card) => unionInto(phones, card.phones, "p", (p) => phoneKey(p.number)))
364
- .some(Boolean);
365
- const addresses = { ...(kept.addresses ?? {}) };
366
- // An address stored only as `full` text (as some CardDAV clients write it)
367
- // formats to an empty string, so its text is the identity instead.
368
- const addressesAdded = merged
369
- .map((card) => unionInto(addresses, card.addresses, "a", (a) => (formatCardAddress(a) || (a.full ?? "")).trim().toLowerCase()))
370
- .some(Boolean);
371
- const patch = {
372
- ...(email.emails ? { emails: email.emails } : {}),
373
- ...(phonesAdded ? { phones } : {}),
374
- ...(addressesAdded ? { addresses } : {}),
375
- ...mergeName(kept, merged, conflicts),
376
- ...mergeOrganization(kept, merged, conflicts),
377
- ...mergeBirthday(kept, merged, conflicts),
378
- ...mergeRoles(kept, merged, conflicts),
379
- ...mergeNotes(kept, merged),
380
- };
381
- unmodeled(kept, merged, conflicts);
382
- return {
383
- conflicts,
384
- movedEmails: email.movedEmails,
385
- patch,
386
- ...(email.preferred ? { preferred: email.preferred } : {}),
387
- };
388
- };
@@ -1,115 +0,0 @@
1
- // Observation extraction: turns the records the writers already hold —
2
- // email detail records in the poller, parsed attendee lists in the calendar
3
- // tools — into graded, content-free observations for the fold. The
4
- // confidence ladder from the design document is applied here, at collection
5
- // time; relationship tiers are read-time (scoring.ts).
6
- import { isRelayAddress, normalizeAddress } from "./address-class.js";
7
- import { LARGE_EVENT_ATTENDEES } from "./constants.js";
8
- // A display name that is just the address again carries no information.
9
- const cleanName = (name, address) => {
10
- const trimmed = name?.trim();
11
- if (!trimmed || normalizeAddress(trimmed) === address) {
12
- return;
13
- }
14
- return trimmed;
15
- };
16
- const PLUS_TAG_RE = /\+[^@]*@/u;
17
- // Self detection tolerates plus-addressed aliases (me+lists@…), so a tagged
18
- // form of the user's own address is never collected as a contact. `address`
19
- // must already be normalized.
20
- const isSelfAddress = (address, selfAddresses) => {
21
- if (selfAddresses.has(address)) {
22
- return true;
23
- }
24
- const stripped = address.replace(PLUS_TAG_RE, "@");
25
- return stripped !== address && selfAddresses.has(stripped);
26
- };
27
- const collect = (into, entry, base, confidence, selfAddresses) => {
28
- const address = normalizeAddress(entry.address);
29
- if (!address.includes("@") ||
30
- into.seen.has(address) ||
31
- isSelfAddress(address, selfAddresses)) {
32
- return;
33
- }
34
- into.seen.add(address);
35
- if (isRelayAddress(address)) {
36
- into.relays.push({ address, recordId: base.recordId });
37
- return;
38
- }
39
- const name = cleanName(entry.name, address);
40
- into.observations.push({
41
- address,
42
- confidence,
43
- ...(name ? { name } : {}),
44
- ...base,
45
- });
46
- };
47
- export const observationsFromEmail = (email, opts) => {
48
- const date = email.receivedAt;
49
- if (!date) {
50
- return { observations: [], relays: [] };
51
- }
52
- const sentByMailbox = Boolean(opts.sentMailboxId && email.mailboxIds?.[opts.sentMailboxId]);
53
- const sentBySelf = (email.from ?? []).some((a) => isSelfAddress(normalizeAddress(a.email), opts.selfAddresses));
54
- const outgoing = sentByMailbox || sentBySelf;
55
- const base = {
56
- channel: "email",
57
- date,
58
- direction: outgoing ? "outgoing" : "incoming",
59
- recordId: email.id,
60
- };
61
- const into = { observations: [], relays: [], seen: new Set() };
62
- const entries = (list) => (list ?? []).map((a) => ({ address: a.email, name: a.name }));
63
- if (outgoing) {
64
- // The user authored this mail: To recipients are confirmed pairings,
65
- // CC recipients were merely copied.
66
- for (const e of entries(email.to)) {
67
- collect(into, e, base, "confirmed", opts.selfAddresses);
68
- }
69
- for (const e of entries(email.cc)) {
70
- collect(into, e, base, "weak", opts.selfAddresses);
71
- }
72
- }
73
- else {
74
- // Received mail: sender and direct recipients are strong; CC is weak.
75
- for (const e of entries(email.from)) {
76
- collect(into, e, base, "strong", opts.selfAddresses);
77
- }
78
- for (const e of entries(email.to)) {
79
- collect(into, e, base, "strong", opts.selfAddresses);
80
- }
81
- for (const e of entries(email.cc)) {
82
- collect(into, e, base, "weak", opts.selfAddresses);
83
- }
84
- }
85
- return { observations: into.observations, relays: into.relays };
86
- };
87
- const STATUS_ACCEPTED = "ACCEPTED";
88
- export const observationsFromEvent = (event, opts) => {
89
- const selfOrganizer = Boolean(event.organizer &&
90
- isSelfAddress(normalizeAddress(event.organizer), opts.selfAddresses));
91
- const selfAccepted = event.attendees.some((a) => isSelfAddress(normalizeAddress(a.email), opts.selfAddresses) &&
92
- a.status?.toUpperCase() === STATUS_ACCEPTED);
93
- const base = opts.kind === "action" || selfOrganizer || selfAccepted
94
- ? "confirmed"
95
- : "strong";
96
- // A workshop-sized attendee list is not evidence of a relationship.
97
- const attendeeConfidence = event.attendees.length > LARGE_EVENT_ATTENDEES ? "weak" : base;
98
- const shared = {
99
- channel: "calendar",
100
- date: event.start,
101
- direction: selfOrganizer ? "outgoing" : "incoming",
102
- recordId: event.uid,
103
- };
104
- const into = { observations: [], relays: [], seen: new Set() };
105
- // Attendees first: their lines carry the CN display name, and the
106
- // organizer is almost always also listed as an attendee — collecting the
107
- // bare organizer first would win the dedup slot and drop the name.
108
- for (const a of event.attendees) {
109
- collect(into, { address: a.email, name: a.name }, shared, attendeeConfidence, opts.selfAddresses);
110
- }
111
- if (event.organizer) {
112
- collect(into, { address: event.organizer }, shared, base, opts.selfAddresses);
113
- }
114
- return { observations: into.observations, relays: into.relays };
115
- };