imsg-mcp 1.3.0 → 1.3.2

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.
@@ -0,0 +1 @@
1
+ {"version":3,"file":"imessage-db-Cz8MIIpG.js","sources":["../src/contacts-db.ts","../src/native-bridge.ts","../src/parsers/buffer-reader.ts","../src/parsers/typedstream-parser.ts","../src/attributed-body-text.ts","../src/conversation-snippet.ts","../src/plist-text.ts","../src/slug-store.ts","../src/imessage-db.ts"],"sourcesContent":["/**\n * macOS Contacts Database Reader\n *\n * Loads contacts from all available sources:\n * - Local: ~/Library/Application Support/AddressBook/AddressBook-v22.abcddb\n * - iCloud / other accounts: ~/Library/Application Support/AddressBook/Sources/<UUID>/AddressBook-v22.abcddb\n *\n * Merges all into a single lookup so phone/email resolve to display names from any source.\n */\n\nimport { existsSync, readdirSync } from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport { join } from \"node:path\";\nimport Database from \"better-sqlite3\";\n\nexport interface Contact {\n id: number;\n firstName: string | null;\n lastName: string | null;\n middleName: string | null;\n nickname: string | null;\n organization: string | null;\n displayName: string;\n phoneNumbers: string[];\n emails: string[];\n}\n\nexport interface ContactLookup {\n contactId: number;\n displayName: string;\n label?: string;\n}\n\n/**\n * Normalize phone number for comparison (digits only, canonical form).\n * - US: strip leading 1 so 10 digits.\n * - AU/international: keep full digits (e.g. 61... for Australia).\n */\nfunction normalizePhoneNumber(phone: string): string {\n const digits = phone.replace(/\\D/g, \"\");\n // US: 11 digits starting with 1 -> drop 1\n if (digits.length === 11 && digits.startsWith(\"1\")) {\n return digits.slice(1);\n }\n return digits;\n}\n\n/**\n * All normalized forms to use when storing a number in the map,\n * so lookups by +61..., 04..., 0... all hit.\n *\n * Exported for handle→chat matching too: Address Book cards often store local\n * formats (\"0408 315 498\") while chat identifiers are E.164 (\"+61408315498\").\n */\nexport function normalizedPhoneVariants(phone: string): string[] {\n const normalized = normalizePhoneNumber(phone);\n const variants = new Set<string>([normalized]);\n\n // Australia: 61 + 9 digits -> also store bare mobile digits and local 04... format\n if (normalized.length === 11 && normalized.startsWith(\"61\")) {\n const localMobile = normalized.slice(2);\n variants.add(localMobile);\n if (localMobile.length === 9 && localMobile.startsWith(\"4\")) {\n variants.add(`0${localMobile}`);\n }\n }\n\n // Australia: local 04... mobile -> also store bare mobile digits and 61-prefixed format\n if (normalized.length === 10 && normalized.startsWith(\"04\")) {\n const mobileDigits = normalized.slice(1);\n variants.add(mobileDigits);\n variants.add(`61${mobileDigits}`);\n }\n\n // US: also store with leading 1\n if (normalized.length === 10) {\n variants.add(`1${normalized}`);\n }\n // Australia: 9 digits starting with 4 (mobile) -> also store with 61 prefix\n if (normalized.length === 9 && normalized.startsWith(\"4\")) {\n variants.add(`61${normalized}`);\n variants.add(`0${normalized}`);\n }\n\n return [...variants];\n}\n\n/**\n * Normalize email for comparison (lowercase, trim)\n */\nfunction normalizeEmail(email: string): string {\n return email.toLowerCase().trim();\n}\n\n/** Normalize a name for cross-card equivalence: lowercase, strip diacritics, collapse whitespace. */\nfunction normalizeNameForMatch(name: string | null | undefined): string {\n if (!name) return \"\";\n return name\n .normalize(\"NFD\")\n .replace(/[\\u0300-\\u036f]/g, \"\")\n .toLowerCase()\n .replace(/\\s+/g, \" \")\n .trim();\n}\n\n/**\n * The set of normalized names a card can be known by (nickname, \"first last\",\n * organization). Two cards are considered the same entity when these sets\n * intersect — so a card nicknamed \"Dad\" still matches a \"Armen Grigorian\" card,\n * but an org card (\"ProperyStart VR\") sharing one utility email with a person\n * does NOT match them.\n */\nfunction nameCandidates(fields: {\n firstName?: string | null;\n lastName?: string | null;\n nickname?: string | null;\n organization?: string | null;\n}): Set<string> {\n const out = new Set<string>();\n const add = (n: string | null | undefined) => {\n const norm = normalizeNameForMatch(n);\n if (norm) out.add(norm);\n };\n add(fields.nickname);\n const full = [fields.firstName, fields.lastName].filter(Boolean).join(\" \");\n add(full);\n add(fields.organization);\n return out;\n}\n\nconst ADDRESS_BOOK_DIR = join(homedir(), \"Library\", \"Application Support\", \"AddressBook\");\nconst MAIN_DB_NAME = \"AddressBook-v22.abcddb\";\n\n/**\n * Discover all Address Book database paths: main DB plus each source (e.g. iCloud).\n */\nfunction discoverContactDbPaths(customPaths?: string | string[]): string[] {\n if (customPaths) {\n const list = Array.isArray(customPaths) ? customPaths : [customPaths];\n return list.filter((p) => existsSync(p));\n }\n\n const paths: string[] = [];\n const mainDb = join(ADDRESS_BOOK_DIR, MAIN_DB_NAME);\n if (existsSync(mainDb)) paths.push(mainDb);\n\n const sourcesDir = join(ADDRESS_BOOK_DIR, \"Sources\");\n if (existsSync(sourcesDir)) {\n try {\n const subdirs = readdirSync(sourcesDir, { withFileTypes: true });\n for (const d of subdirs) {\n if (!d.isDirectory()) continue;\n const sourceDb = join(sourcesDir, d.name, MAIN_DB_NAME);\n if (existsSync(sourceDb)) paths.push(sourceDb);\n }\n } catch {\n // ignore read errors\n }\n }\n\n return paths;\n}\n\nexport class ContactsDB {\n private dbPaths: string[] = [];\n private databases: Database.Database[] = [];\n private phoneMap: Map<string, ContactLookup> = new Map();\n private emailMap: Map<string, ContactLookup> = new Map();\n private contactCache: Map<number, Contact> = new Map();\n private initialized = false;\n /** Next id when loading from multiple DBs (avoids Z_PK collisions across sources). */\n private nextContactId = 1;\n\n constructor(dbPaths?: string | string[]) {\n this.dbPaths = discoverContactDbPaths(dbPaths);\n }\n\n /**\n * Initialize the contact lookup maps from all discovered DBs (local + iCloud/sources).\n */\n initialize(): void {\n if (this.initialized) return;\n\n for (const path of this.dbPaths) {\n try {\n const db = new Database(path, { readonly: true });\n this.databases.push(db);\n this.loadContactsFromDb(db);\n } catch (err) {\n console.warn(`ContactsDB: could not load ${path}:`, err);\n }\n }\n\n this.initialized = true;\n }\n\n /**\n * Load contacts from a single Address Book SQLite DB into the shared maps.\n */\n private loadContactsFromDb(db: Database.Database): void {\n const contacts = db\n .prepare(`\n SELECT \n Z_PK as localId,\n ZFIRSTNAME as firstName,\n ZLASTNAME as lastName,\n ZMIDDLENAME as middleName,\n ZNICKNAME as nickname,\n ZORGANIZATION as organization\n FROM ZABCDRECORD\n WHERE ZFIRSTNAME IS NOT NULL OR ZLASTNAME IS NOT NULL OR ZORGANIZATION IS NOT NULL\n `)\n .all() as any[];\n\n for (const row of contacts) {\n const localId = row.localId as number;\n\n const phones = (\n db\n .prepare(`\n SELECT ZFULLNUMBER as number, ZLABEL as label\n FROM ZABCDPHONENUMBER\n WHERE ZOWNER = ? OR Z22_OWNER = ?\n `)\n .all(localId, localId) as any[]\n ).filter((p) => p.number);\n\n const emails = (\n db\n .prepare(`\n SELECT ZADDRESS as email, ZLABEL as label\n FROM ZABCDEMAILADDRESS\n WHERE ZOWNER = ? OR Z22_OWNER = ?\n `)\n .all(localId, localId) as any[]\n ).filter((e) => e.email);\n\n // Cross-source dedup + union: if a handle already resolves to a contact\n // loaded from an earlier source DB AND the two cards plausibly name the\n // same entity, reuse that contact instead of minting a new id — that's\n // how a person split across a local card (phone) and an iCloud card\n // (email) merges into one identity.\n //\n // The name gate matters: distinct cards can legitimately share a handle\n // (a person's card and their business's org card both carrying the same\n // info@ email). Unioning those folds two different entities into one\n // contactId — mislabeling conversations and wrongly merging threads. So\n // union only when the cards' name-candidate sets intersect; otherwise\n // mint a separate contact and leave the shared handle with its first\n // claimant (Messages.app behaves the same way).\n const existingIds = this.findExistingContactIds(\n phones.map((p) => p.number as string),\n emails.map((e) => e.email as string),\n );\n const cardNames = nameCandidates(row);\n const sameEntityIds = existingIds.filter((id) => {\n const existing = this.contactCache.get(id);\n if (!existing) return false;\n const existingNames = nameCandidates(existing);\n // A card with no usable name can't be disproven — treat as a match.\n if (cardNames.size === 0 || existingNames.size === 0) return true;\n for (const n of cardNames) if (existingNames.has(n)) return true;\n return false;\n });\n\n let contact: Contact;\n if (sameEntityIds.length === 0) {\n contact = {\n id: this.nextContactId++,\n firstName: row.firstName,\n lastName: row.lastName,\n middleName: row.middleName,\n nickname: row.nickname,\n organization: row.organization,\n displayName: this.buildDisplayName(row),\n phoneNumbers: [],\n emails: [],\n };\n } else {\n const survivorId = sameEntityIds[0];\n for (let i = 1; i < sameEntityIds.length; i++) {\n this.mergeContacts(survivorId, sameEntityIds[i]);\n }\n contact = this.contactCache.get(survivorId) as Contact;\n }\n\n // Register handles with the DECLARING card's name (nickname-first), not\n // the survivor's — a handle is named by the card that carries it, exactly\n // like Messages.app. First claimant wins for a handle two cards share.\n const handleName = this.buildDisplayName(row);\n\n for (const phone of phones) {\n if (!contact.phoneNumbers.includes(phone.number)) contact.phoneNumbers.push(phone.number);\n for (const variant of normalizedPhoneVariants(phone.number)) {\n if (!this.phoneMap.has(variant)) {\n this.phoneMap.set(variant, {\n contactId: contact.id,\n displayName: handleName,\n label: phone.label,\n });\n }\n }\n }\n\n for (const email of emails) {\n if (!contact.emails.includes(email.email)) contact.emails.push(email.email);\n const key = normalizeEmail(email.email);\n if (!this.emailMap.has(key)) {\n this.emailMap.set(key, {\n contactId: contact.id,\n displayName: handleName,\n label: email.label,\n });\n }\n }\n\n this.contactCache.set(contact.id, contact);\n }\n }\n\n /**\n * Probe the already-loaded lookup maps for every existing contactId that\n * shares any of these handles (loaded from earlier source DBs). Returns the\n * distinct ids in first-seen order; the caller treats the first as the\n * survivor and unions the rest. Empty when none match.\n */\n private findExistingContactIds(phones: string[], emails: string[]): number[] {\n const ids: number[] = [];\n const seen = new Set<number>();\n const add = (id: number) => {\n if (!seen.has(id)) {\n seen.add(id);\n ids.push(id);\n }\n };\n for (const number of phones) {\n for (const variant of normalizedPhoneVariants(number)) {\n const hit = this.phoneMap.get(variant);\n if (hit) add(hit.contactId);\n }\n }\n for (const email of emails) {\n const hit = this.emailMap.get(normalizeEmail(email));\n if (hit) add(hit.contactId);\n }\n return ids;\n }\n\n /**\n * Fold contact `dropId` into `keepId`: union their phone/email lists and\n * re-point every lookup-map entry (preserving labels) from the dropped id to\n * the survivor, then forget the dropped id. Used when one card bridges two\n * previously-separate identities.\n */\n private mergeContacts(keepId: number, dropId: number): void {\n if (keepId === dropId) return;\n const keep = this.contactCache.get(keepId);\n const drop = this.contactCache.get(dropId);\n if (!keep || !drop) return;\n\n for (const p of drop.phoneNumbers)\n if (!keep.phoneNumbers.includes(p)) keep.phoneNumbers.push(p);\n for (const e of drop.emails) if (!keep.emails.includes(e)) keep.emails.push(e);\n\n // Repoint the dropped id but keep each entry's per-handle displayName —\n // the declaring card's name stays authoritative for its own handles.\n for (const [key, lookup] of this.phoneMap) {\n if (lookup.contactId === dropId) {\n this.phoneMap.set(key, { ...lookup, contactId: keepId });\n }\n }\n for (const [key, lookup] of this.emailMap) {\n if (lookup.contactId === dropId) {\n this.emailMap.set(key, { ...lookup, contactId: keepId });\n }\n }\n this.contactCache.delete(dropId);\n }\n\n /**\n * Look up a contact by phone number or email.\n */\n lookupContact(handle: string): ContactLookup | null {\n if (!this.initialized) {\n this.initialize();\n }\n\n if (/[\\d+\\-()\\s]/.test(handle)) {\n for (const variant of normalizedPhoneVariants(handle)) {\n const contact = this.phoneMap.get(variant);\n if (contact) {\n return contact;\n }\n }\n }\n\n if (handle.includes(\"@\")) {\n const normalized = normalizeEmail(handle);\n const contact = this.emailMap.get(normalized);\n if (contact) {\n return contact;\n }\n }\n\n return null;\n }\n\n /**\n * Look up a contact by phone number or email\n * Returns display name if found, or the original handle if not\n */\n lookupHandle(handle: string): string {\n return this.lookupContact(handle)?.displayName ?? handle;\n }\n\n /**\n * Get full contact details by ID\n */\n getContact(id: number): Contact | null {\n if (!this.initialized) {\n this.initialize();\n }\n return this.contactCache.get(id) || null;\n }\n\n /**\n * A load-order-independent identity anchor for a contact: the\n * lexicographically smallest normalized handle. Contact ids are assigned in\n * Address Book load order, so they renumber whenever any card is added or\n * removed — anything persisted across sessions (thread-slug hashes) must key\n * off this anchor instead, which only changes if the contact's own handles\n * change.\n */\n stableAnchor(contactId: number): string | null {\n const contact = this.getContact(contactId);\n if (!contact) return null;\n const handles = [\n ...contact.phoneNumbers.map((p) => normalizePhoneNumber(p)),\n ...contact.emails.map((e) => normalizeEmail(e)),\n ].filter((h) => h.length > 0);\n if (handles.length === 0) return null;\n handles.sort();\n return handles[0];\n }\n\n /**\n * Search contacts by name\n */\n searchContacts(query: string): Contact[] {\n if (!this.initialized) {\n this.initialize();\n }\n\n const lowerQuery = query.toLowerCase();\n return Array.from(this.contactCache.values()).filter(\n (c) =>\n c.displayName.toLowerCase().includes(lowerQuery) ||\n c.phoneNumbers.some((p) => p.includes(query)) ||\n c.emails.some((e) => e.toLowerCase().includes(lowerQuery)),\n );\n }\n\n /**\n * Build display name from contact fields\n */\n private buildDisplayName(row: any): string {\n if (row.nickname) return row.nickname;\n\n const parts: string[] = [];\n\n if (row.firstName) parts.push(row.firstName);\n if (row.lastName) parts.push(row.lastName);\n\n if (parts.length > 0) {\n return parts.join(\" \");\n }\n\n if (row.organization) return row.organization;\n\n return \"Unknown Contact\";\n }\n\n /**\n * Close all database connections\n */\n close(): void {\n for (const db of this.databases) {\n try {\n db.close();\n } catch {\n // ignore\n }\n }\n this.databases = [];\n }\n\n /**\n * Paginated list of all loaded contacts, sorted by displayName.\n * Used by the MCP `list_contacts` tool.\n */\n listContacts(offset = 0, limit = 20): { contacts: Contact[]; total: number } {\n if (!this.initialized) {\n this.initialize();\n }\n const all = Array.from(this.contactCache.values()).sort((a, b) =>\n a.displayName.localeCompare(b.displayName),\n );\n const slice = limit === 0 ? all.slice(offset) : all.slice(offset, offset + limit);\n return { contacts: slice, total: all.length };\n }\n\n /**\n * Get statistics about loaded contacts\n */\n getStats() {\n if (!this.initialized) {\n this.initialize();\n }\n\n return {\n totalContacts: this.contactCache.size,\n phoneNumbers: this.phoneMap.size,\n emails: this.emailMap.size,\n };\n }\n}\n","/**\n * Native bridge — tries to load the Rust native module for accelerated\n * SQLite queries and blob parsing. Falls back to the TypeScript implementation\n * if the native module is unavailable.\n */\n\nimport { createRequire } from \"node:module\";\nimport { dirname, join } from \"node:path\";\nimport { fileURLToPath } from \"node:url\";\n\nconst __dirname = dirname(fileURLToPath(import.meta.url));\n\n/**\n * The native module accelerates exactly two things — attributedBody blob\n * parsing and Address Book contact resolution. Conversation/message listing,\n * cross-handle merge, and slugs live in the TypeScript `IMessageDB` (the single\n * source of truth); the engine label is \"Rust parser + TS DB\" to match.\n */\nexport interface NativeModule {\n parseAttributedBody(blob: Buffer): string | null;\n\n resolveContacts(\n contactsMainPath: string,\n contactsSourcesDir: string | null,\n handles: string[],\n ): Promise<Record<string, string>>;\n}\n\nlet _native: NativeModule | null | undefined;\n\n/**\n * Try to load the native module. Returns null if unavailable.\n * Result is cached after first attempt.\n *\n * Set `IMSG_DISABLE_NATIVE=1` to force the TypeScript fallback (useful for\n * testing the fallback path or comparing performance).\n */\nexport function tryLoadNative(): NativeModule | null {\n if (_native !== undefined) return _native;\n\n if (process.env.IMSG_DISABLE_NATIVE === \"1\") {\n _native = null;\n return null;\n }\n\n try {\n const require = createRequire(import.meta.url);\n // Try loading from the native/ directory relative to dist/\n const nativePath = join(__dirname, \"..\", \"native\", \"index.js\");\n _native = require(nativePath) as NativeModule;\n return _native;\n } catch {\n // Native module not available — fall back to TS\n _native = null;\n return null;\n }\n}\n\n/**\n * Check if the native module is available.\n */\nexport function hasNativeModule(): boolean {\n return tryLoadNative() !== null;\n}\n","/**\n * Buffer reader with cursor tracking for binary format parsing.\n * Forked from imessage-parser's BufferReader to remove the upstream dependency.\n */\nexport class BufferReader {\n private buffer: Buffer;\n private offset: number;\n\n constructor(buffer: Buffer, initialOffset = 0) {\n this.buffer = buffer;\n this.offset = initialOffset;\n }\n\n get position(): number {\n return this.offset;\n }\n\n get length(): number {\n return this.buffer.length;\n }\n\n get remaining(): number {\n return this.buffer.length - this.offset;\n }\n\n seek(position: number): void {\n if (position < 0 || position > this.buffer.length) {\n throw new Error(`Invalid seek position: ${position}`);\n }\n this.offset = position;\n }\n\n skip(bytes: number): void {\n this.offset += bytes;\n }\n\n readUInt8(): number {\n const value = this.buffer.readUInt8(this.offset);\n this.offset += 1;\n return value;\n }\n\n readUInt16LE(): number {\n const value = this.buffer.readUInt16LE(this.offset);\n this.offset += 2;\n return value;\n }\n\n readBytes(length: number): Buffer {\n if (this.offset + length > this.buffer.length) {\n throw new Error(\"Attempt to read beyond buffer length\");\n }\n const bytes = this.buffer.subarray(this.offset, this.offset + length);\n this.offset += length;\n return bytes;\n }\n\n readString(length: number, encoding: BufferEncoding = \"utf8\"): string {\n const bytes = this.readBytes(length);\n return bytes.toString(encoding);\n }\n\n findPattern(pattern: Buffer | string): number {\n const searchBuffer = typeof pattern === \"string\" ? Buffer.from(pattern) : pattern;\n const index = this.buffer.indexOf(searchBuffer, this.offset);\n return index >= 0 ? index : -1;\n }\n\n peekByte(): number | null {\n if (this.offset >= this.buffer.length) return null;\n return this.buffer[this.offset];\n }\n\n peekBytes(length: number): Buffer | null {\n if (this.offset + length > this.buffer.length) return null;\n return this.buffer.subarray(this.offset, this.offset + length);\n }\n}\n","/**\n * Parser for Apple's typedstream format used in NSArchiver.\n * Forked from imessage-parser's TypedStreamParser to remove the upstream dependency.\n *\n * This handles the binary `attributedBody` column in the iMessage database,\n * which stores NSAttributedString data serialised via NSArchiver (typedstream format).\n *\n * SAFETY: All loops have iteration guards to prevent infinite loops on malformed blobs.\n */\nimport { BufferReader } from \"./buffer-reader.js\";\n\nexport interface NSStringData {\n className: string;\n content: string;\n encoding: string;\n}\n\n/**\n * Preamble bytes following \"NSString\" before the length byte.\n * Apple uses variants of this 5-byte sequence — most commonly 0x94, but\n * 0x95 appears for messages that contain DataDetector annotations\n * (phone numbers, dates, auth codes). Both must be recognized or the\n * length byte will be misread (causing extracted text leaks like\n * \"()*+Z$classname...\").\n */\nconst PREAMBLE_BYTE_2_VARIANTS = new Set([0x94, 0x95]);\nconst PREAMBLE_LEN = 5;\n\nfunction matchesNSStringPreamble(bytes: Buffer | null): boolean {\n if (!bytes || bytes.length < PREAMBLE_LEN) return false;\n return (\n bytes[0] === 0x01 &&\n PREAMBLE_BYTE_2_VARIANTS.has(bytes[1]) &&\n bytes[2] === 0x84 &&\n bytes[3] === 0x01 &&\n bytes[4] === 0x2b\n );\n}\n\nconst METADATA_KEYWORDS = [\n \"streamtyped\",\n \"NSMutableAttributedString\",\n \"NSAttributedString\",\n \"NSObject\",\n \"NSMutableString\",\n \"NSString\",\n \"NSDictionary\",\n \"NSNumber\",\n \"NSValue\",\n \"NSFont\",\n \"NSParagraphStyle\",\n \"__kIM\",\n \"NSData\",\n \"bplist\",\n \"NSKeyedArchiver\",\n \"NS.rangeval\",\n \"Z$classname\",\n \"$class\",\n \"$classname\",\n];\n\nexport class TypedStreamParser {\n private reader: BufferReader;\n private headerParsed = false;\n\n constructor(buffer: Buffer) {\n this.reader = new BufferReader(buffer);\n }\n\n /** Parse all NSString objects found in the buffer. */\n parseAllNSStrings(): NSStringData[] {\n const strings: NSStringData[] = [];\n this.parseHeader();\n\n const maxIter = this.reader.length;\n let iter = 0;\n while (this.reader.remaining > 0 && iter++ < maxIter) {\n const posBefore = this.reader.position;\n const found = this.parseNSString();\n if (found) {\n strings.push(found);\n } else if (this.reader.remaining > 0) {\n // If parseNSString didn't advance, skip ahead\n if (this.reader.position <= posBefore) {\n this.reader.skip(1);\n }\n }\n // Stall detection: if position hasn't changed, force advance\n if (this.reader.position <= posBefore && this.reader.remaining > 0) {\n this.reader.skip(1);\n }\n }\n\n return strings;\n }\n\n /** Fallback: extract readable text segments by scanning for printable byte runs. */\n extractReadableText(): string[] {\n this.reader.seek(0);\n const texts: string[] = [];\n let current = \"\";\n let inText = false;\n\n const maxIter = this.reader.length;\n let iter = 0;\n while (this.reader.remaining > 0 && iter++ < maxIter) {\n const posBefore = this.reader.position;\n const byte = this.reader.readUInt8();\n\n if ((byte >= 32 && byte <= 126) || byte === 9 || byte === 10 || byte === 13) {\n // ASCII printable or whitespace\n current += String.fromCharCode(byte);\n inText = true;\n } else if (byte >= 0xc0 && byte <= 0xf7) {\n // Potential UTF-8 multi-byte sequence start\n const extraBytes = byte < 0xe0 ? 1 : byte < 0xf0 ? 2 : 3;\n if (this.reader.remaining >= extraBytes) {\n // Read continuation bytes\n const bytes = [byte];\n let valid = true;\n for (let i = 0; i < extraBytes; i++) {\n const cont = this.reader.readUInt8();\n if ((cont & 0xc0) !== 0x80) {\n valid = false;\n break;\n }\n bytes.push(cont);\n }\n if (valid) {\n try {\n current += Buffer.from(bytes).toString(\"utf8\");\n inText = true;\n } catch {\n this.flushSegment(current, inText, texts);\n current = \"\";\n inText = false;\n }\n } else {\n this.flushSegment(current, inText, texts);\n current = \"\";\n inText = false;\n }\n } else {\n // Not enough bytes for UTF-8 sequence\n this.flushSegment(current, inText, texts);\n current = \"\";\n inText = false;\n }\n } else {\n // Control char or invalid byte\n this.flushSegment(current, inText, texts);\n current = \"\";\n inText = false;\n }\n\n // Stall detection: position must have advanced\n if (this.reader.position <= posBefore) {\n if (this.reader.remaining > 0) this.reader.skip(1);\n else break;\n }\n }\n\n this.flushSegment(current, true, texts);\n return texts;\n }\n\n // ── Private ──────────────────────────────────────────────────────────\n\n private parseHeader(): void {\n if (this.headerParsed) return;\n const magic = this.reader.peekBytes(11);\n if (!magic || magic.toString(\"ascii\") !== \"streamtyped\") return;\n\n this.reader.skip(11);\n // Skip until we find an uppercase letter (class name start), with iteration guard\n const maxSkip = Math.min(this.reader.remaining, 256);\n let skipped = 0;\n while (this.reader.remaining > 0 && skipped++ < maxSkip) {\n const next4 = this.reader.peekBytes(4);\n if (next4 && /^[A-Z]/.test(next4.toString(\"ascii\"))) break;\n this.reader.skip(1);\n }\n this.headerParsed = true;\n }\n\n private parseNSString(): NSStringData | null {\n const pos = this.reader.findPattern(\"NSString\");\n if (pos === -1) {\n // No more NSString patterns — skip to end to stop the loop\n this.reader.seek(this.reader.length);\n return null;\n }\n\n this.reader.seek(pos + 8); // skip \"NSString\"\n\n const preamble = this.reader.peekBytes(PREAMBLE_LEN);\n if (matchesNSStringPreamble(preamble)) {\n this.reader.skip(PREAMBLE_LEN);\n }\n\n if (this.reader.remaining < 1) return null;\n const lengthByte = this.reader.readUInt8();\n let length: number;\n if (lengthByte === 0x81) {\n if (this.reader.remaining < 2) return null;\n length = this.reader.readUInt16LE();\n } else {\n length = lengthByte;\n }\n\n if (length === 0 || length > this.reader.remaining) return null;\n\n return {\n className: \"NSString\",\n content: this.reader.readString(length, \"utf8\"),\n encoding: \"utf8\",\n };\n }\n\n private flushSegment(text: string, inText: boolean, out: string[]): void {\n if (!inText || text.length <= 3) return;\n const cleaned = this.cleanText(text);\n if (cleaned.length > 3) out.push(cleaned);\n }\n\n private cleanText(text: string): string {\n let cleaned = text;\n for (const kw of METADATA_KEYWORDS) {\n cleaned = cleaned.replaceAll(kw, \"\");\n }\n return (\n cleaned\n .replace(/\\{[^}]*\\}/g, \"\")\n .replace(/\\[[^\\]]*\\]/g, \"\")\n // biome-ignore lint/suspicious/noControlCharactersInRegex: typedstream blobs contain binary control bytes.\n .replace(/[\\x00-\\x1f]/g, \"\")\n .replace(/\\s+/g, \" \")\n .trim()\n );\n }\n}\n","import { tryLoadNative } from \"./native-bridge.js\";\nimport { TypedStreamParser } from \"./parsers/typedstream-parser.js\";\n\n/** Cache the native lookup once — null means TS fallback path. */\nlet _nativeParseAttributedBody: ((blob: Buffer) => string | null) | null | undefined;\nfunction getNativeParser(): ((blob: Buffer) => string | null) | null {\n if (_nativeParseAttributedBody !== undefined) return _nativeParseAttributedBody;\n const native = tryLoadNative();\n _nativeParseAttributedBody = native ? native.parseAttributedBody.bind(native) : null;\n return _nativeParseAttributedBody;\n}\n\n/**\n * Decide whether to trust a native parser result. The Rust path uses simpler\n * heuristics and may return attachment IDs / metadata that the TS path knows\n * to filter. When we're unsure, fall through to TS for safety.\n */\nfunction nativeResultLooksTrustworthy(text: string): boolean {\n // Attachment GUID markers — never user-visible text\n if (/at_\\d+_[0-9A-F-]{8,}/i.test(text)) return false;\n // Apple internal attribute name markers\n if (/__kIM[A-Z]/.test(text)) return false;\n // Class / archiver metadata\n if (/\\$class|streamtyped|NSKeyedArchiver/.test(text)) return false;\n if (/^NS[A-Z][a-z]+/.test(text)) return false;\n // Doubled-uppercase-letter prefix — almost certainly a typedstream length-byte\n // leak (e.g. \"HHeres the question...\" where the byte 'H' = 0x48 = 72 is the\n // length of the actual content \"Heres the question...\"). The Rust path uses\n // simple split-on-control heuristics that don't understand typedstream framing,\n // so it can include the length byte as the first char of the result.\n // Reject so we fall through to the TS path's structured parser.\n if (/^([A-Z])\\1[a-z]/.test(text)) return false;\n return true;\n}\n\nconst METADATA_PATTERNS = [\n /^streamtyped$/i,\n /^NS[A-Z]/,\n /^[^A-Za-z0-9]*__kIM/,\n /^MessagePartAttributeName$/i,\n /^DataDetectedAttributeName$/i,\n /^CalendarEventAttributeName$/i,\n /^X\\$version/,\n /^WversionYdd-result/,\n /^bplist/,\n /^\\$class/,\n /^RMSV\\$class/,\n /^NSData$/,\n /^NSDictionary$/,\n /^NSNumber$/,\n /^NSValue$/,\n /^at_\\d+_[0-9A-F-]+$/i,\n /FileTransferGUIDAttributeName/i,\n /BaseWritingDirectionAttributeName/i,\n /^\\d{2}.*��/,\n];\n\n// biome-ignore lint/suspicious/noControlCharactersInRegex: attributedBody payloads contain binary control bytes.\nconst CONTROL_OR_DEL_RE = /[\\x00-\\x1F\\x7F]/g;\n// biome-ignore lint/suspicious/noControlCharactersInRegex: attributedBody payloads contain binary control bytes.\nconst CONTROL_SPLIT_RE = /[\\x00-\\x1F\\x7F]+/g;\n\n/** Max time (ms) to spend parsing a single attributedBody blob before giving up. */\nconst PARSE_TIMEOUT_MS = 200;\n\nfunction normalizeCandidate(text: string): string | null {\n const normalized = text\n .replace(/^[+;:\"'()&\\s]+/, \"\")\n .replace(/^[^A-Za-z\\d\\s](?=[A-Z])/, \"\")\n .replace(/^[a-z\\d](?=[A-Z])/, \"\")\n // Strip leading single uppercase letter when followed by another uppercase + lowercase\n // (length-byte artifact from typedstream — e.g. \"OYes\" -> \"Yes\" for a 79-char string,\n // \"JThat\" -> \"That\" for a 74-char string). Only triggers when the leading letter is\n // immediately followed by an uppercase + lowercase pattern indicating the real\n // sentence start.\n .replace(/^[A-Z](?=[A-Z][a-z])/, \"\")\n .replace(CONTROL_OR_DEL_RE, \" \")\n .replace(/\\uFFFD+/g, \"\")\n .replace(/\\s+/g, \" \")\n .trim();\n\n if (!normalized || normalized === \"�\" || normalized === \"\") return null;\n if (normalized.length < 2 && !/[\\u{1F300}-\\u{1FAFF}]/u.test(normalized)) return null;\n if (/^[A-Za-z0-9]{1,2}$/.test(normalized)) return null;\n if (METADATA_PATTERNS.some((pattern) => pattern.test(normalized))) return null;\n return normalized;\n}\n\nfunction scoreCandidate(text: string): number {\n let score = 0;\n if (/[A-Za-z]/.test(text)) score += 50;\n if (/\\d/.test(text)) score += 10;\n if (/\\s/.test(text)) score += 30;\n if (/[.!?]/.test(text)) score += 15;\n if (/[\\u{1F300}-\\u{1FAFF}]/u.test(text)) score += 100;\n if (/^[A-Za-z0-9]{1,3}$/.test(text)) score -= 100;\n score += Math.min(text.length, 200);\n return score;\n}\n\n/** Boost factor for structured NSString candidates over heuristic byte-scan ones. */\nconst STRUCTURED_BOOST = 500;\n\nexport function extractAttributedBodyText(blob: Buffer | null): string | undefined {\n if (!blob) return undefined;\n\n // Fast path: native Rust parser, when available. We only trust the native\n // result when it looks like a real message (passes our metadata filter and\n // doesn't start with attachment-id markers). Otherwise fall through to the\n // TS implementation, which has more sophisticated heuristics.\n const native = getNativeParser();\n if (native) {\n try {\n const result = native(blob);\n if (result && result.length > 1 && nativeResultLooksTrustworthy(result)) {\n return result;\n }\n } catch {\n // Fall through to TS implementation on any native failure\n }\n }\n\n const parser = new TypedStreamParser(blob);\n const candidates = new Map<string, number>();\n const deadline = Date.now() + PARSE_TIMEOUT_MS;\n\n const remember = (values: string[], boost = 0) => {\n for (const value of values) {\n const normalized = normalizeCandidate(value);\n if (!normalized) continue;\n const score = scoreCandidate(normalized) + boost;\n // Use the highest score we've seen for this candidate\n const prev = candidates.get(normalized) ?? -Infinity;\n if (score > prev) candidates.set(normalized, score);\n }\n };\n\n // Phase 1: structured NSString parsing — these have correct length bytes,\n // so they avoid the \"leading length-byte\" artifact (e.g. \"OYes lmk...\" for a 79-char string).\n // Boost their score so they win over byte-scan fallbacks.\n try {\n if (Date.now() < deadline) {\n remember(\n parser.parseAllNSStrings().map((entry) => entry.content),\n STRUCTURED_BOOST,\n );\n }\n } catch {\n // Ignore parser failures — continue with fallback strategies\n }\n\n // Phase 2: heuristic text extraction (byte-by-byte scan)\n // Lower priority — may pick up length bytes as text artifacts.\n try {\n if (Date.now() < deadline) {\n remember(parser.extractReadableText());\n }\n } catch {\n // Ignore parser failures — continue with fallback strategies\n }\n\n // Phase 3: raw UTF-8 split (always works, cheapest fallback)\n if (Date.now() < deadline) {\n remember(blob.toString(\"utf8\").split(CONTROL_SPLIT_RE));\n }\n\n const best = [...candidates.entries()].sort((a, b) => b[1] - a[1])[0];\n return best?.[0];\n}\n","import { OBJECT_REPLACEMENT_CHAR } from \"./db-schema.js\";\n\nexport function normalizeSnippetText(text: string | null | undefined): string | null {\n if (!text) return null;\n\n const attachmentMarker = new RegExp(\n `${OBJECT_REPLACEMENT_CHAR.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\")}+`,\n \"g\",\n );\n\n const normalized = text\n .replace(/^[#$]/, \"\")\n .replace(attachmentMarker, \"📎 \")\n .replace(/\\uFFFD/g, \"\")\n .replace(/\\s+/g, \" \")\n .trim();\n\n if (!normalized || normalized === \"📎\" || /^(📎\\s*)+$/.test(normalized)) {\n return normalized ? \"(image/attachment)\" : null;\n }\n\n return normalized;\n}\n\nexport function isMetadataOnlySnippet(text: string | null | undefined): boolean {\n if (!text) return false;\n const normalized = text.trim();\n return (\n /^[$#]?https?:\\/\\//i.test(normalized) ||\n /HttpURL\\/?$/i.test(normalized) ||\n /^[$#]https?:\\/\\/.*HttpURL\\/?$/i.test(normalized)\n );\n}\n\nexport function normalizeRichMetadataText(text: string | null | undefined): string | null {\n const normalized = normalizeSnippetText(text);\n if (!normalized) return null;\n\n if (/^https?:\\/\\/.*HttpURL\\/?$/i.test(normalized)) {\n return normalized.replace(/HttpURL\\/?$/i, \"\");\n }\n\n return normalized;\n}\n\nexport function pickConversationSnippet(options: {\n rawText?: string | null;\n parsedText?: string | null;\n summaryText?: string | null;\n}): string | null {\n return (\n normalizeSnippetText(options.rawText) ??\n normalizeSnippetText(options.parsedText) ??\n normalizeSnippetText(options.summaryText)\n );\n}\n","import { parseBuffer } from \"bplist-parser\";\n\nfunction normalizeText(value: string | undefined): string | undefined {\n const normalized = value?.replace(/\\s+/g, \" \").trim();\n return normalized && normalized.length >= 4 ? normalized : undefined;\n}\n\nexport function extractNullPaddedAsciiText(blob: Buffer | null): string | undefined {\n if (!blob) return undefined;\n\n let best = \"\";\n let current = \"\";\n\n const flush = () => {\n if (current.length > best.length) {\n best = current;\n }\n current = \"\";\n };\n\n for (let index = 0; index < blob.length - 1; index += 1) {\n const first = blob[index];\n const second = blob[index + 1];\n\n if (first === 0 && second >= 32 && second <= 126) {\n current += String.fromCharCode(second);\n index += 1;\n continue;\n }\n\n flush();\n }\n\n flush();\n\n return normalizeText(best);\n}\n\nexport function extractArchivedAttributedStringText(blob: Buffer | null): string | undefined {\n if (!blob) return undefined;\n\n try {\n const [plist] = parseBuffer(blob) as Array<{ $objects?: unknown[] }>;\n const objects = plist?.$objects;\n if (!Array.isArray(objects)) {\n return extractNullPaddedAsciiText(blob);\n }\n\n const attributedString = objects.find(\n (value): value is { NSString?: { UID?: number } } =>\n typeof value === \"object\" && value !== null && \"NSString\" in value,\n );\n const stringUid = attributedString?.NSString?.UID;\n if (typeof stringUid === \"number\" && typeof objects[stringUid] === \"string\") {\n return normalizeText(objects[stringUid] as string);\n }\n\n const firstString = objects.find(\n (value): value is string =>\n typeof value === \"string\" && !value.startsWith(\"$\") && !value.startsWith(\"NS\"),\n );\n return normalizeText(firstString) ?? extractNullPaddedAsciiText(blob);\n } catch {\n return extractNullPaddedAsciiText(blob);\n }\n}\n\nexport function extractChatSummaryText(blob: Buffer | null): string | undefined {\n if (!blob) return undefined;\n\n try {\n const [plist] = parseBuffer(blob) as Array<{\n chatSummaryDictionary?: { chatSummary?: Buffer | null };\n }>;\n const chatSummary = plist?.chatSummaryDictionary?.chatSummary;\n if (chatSummary) {\n return extractArchivedAttributedStringText(chatSummary);\n }\n } catch {\n // Fall back to heuristic extraction below.\n }\n\n return extractNullPaddedAsciiText(blob);\n}\n","/**\n * Persistent SQLite store for conversation thread slugs.\n * Stores slug-to-chat mappings at ~/.imsg-mcp/slugs.db so they survive\n * MCP server restarts and can be referenced by agents/scripts.\n */\n\nimport { existsSync, mkdirSync } from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport { dirname, join } from \"node:path\";\nimport Database from \"better-sqlite3\";\n\nexport interface SlugRecord {\n slug: string;\n chatGuid: string;\n chatIdentifier: string;\n displayName: string | null;\n service: string;\n isGroup: boolean;\n participants: string;\n updatedAt: number;\n}\n\nconst DEFAULT_DIR = join(homedir(), \".imsg-mcp\");\nconst DEFAULT_DB = join(DEFAULT_DIR, \"slugs.db\");\n\nexport class SlugStore {\n private db: Database.Database;\n\n constructor(dbPath?: string) {\n const path = dbPath ?? DEFAULT_DB;\n const dir = dirname(path);\n if (!existsSync(dir)) {\n mkdirSync(dir, { recursive: true });\n }\n this.db = new Database(path);\n this.db.pragma(\"journal_mode = WAL\");\n this.migrate();\n }\n\n /**\n * Schema. v2 splits identity-level slug rows from a many-to-one guid map so a\n * single canonical slug can cover every chat leg of one contact (phone + email,\n * SMS + iMessage). v1 keyed a slug to exactly one guid, so a merged identity\n * produced multiple slugs whose surfaced one flipped with recency.\n */\n private migrate(): void {\n const version = (this.db.pragma(\"user_version\", { simple: true }) as number) ?? 0;\n if (version < 2) {\n // v1 slugs hashed the per-chat guid, so they are stale under the v2\n // identity hash. Slugs are derived data (rebuilt by the next sync), so we\n // drop and recreate rather than attempt an in-place value migration.\n this.db.exec(\"DROP TABLE IF EXISTS thread_slugs\");\n this.db.exec(\"DROP TABLE IF EXISTS slug_chat_guids\");\n }\n this.db.exec(`\n CREATE TABLE IF NOT EXISTS thread_slugs (\n slug TEXT PRIMARY KEY,\n chat_identifier TEXT NOT NULL,\n display_name TEXT,\n service TEXT NOT NULL DEFAULT 'iMessage',\n is_group INTEGER NOT NULL DEFAULT 0,\n participants TEXT NOT NULL DEFAULT '',\n updated_at INTEGER NOT NULL DEFAULT 0\n );\n CREATE TABLE IF NOT EXISTS slug_chat_guids (\n chat_guid TEXT PRIMARY KEY,\n slug TEXT NOT NULL\n );\n CREATE INDEX IF NOT EXISTS idx_slug_chat_guids_slug ON slug_chat_guids(slug);\n `);\n this.db.pragma(\"user_version = 2\");\n }\n\n /**\n * Upsert one chat leg under its identity slug. The identity row is keyed by\n * slug (idempotent across legs); the leg's guid is mapped to that slug. When\n * an identity already has a phone `chat_identifier`, a later email leg does\n * NOT overwrite it — phone is preferred as the canonical handle for stability.\n */\n upsert(record: SlugRecord): void {\n const params = {\n slug: record.slug,\n chatGuid: record.chatGuid,\n chatIdentifier: record.chatIdentifier,\n displayName: record.displayName,\n service: record.service,\n isGroup: record.isGroup ? 1 : 0,\n participants: record.participants,\n updatedAt: record.updatedAt,\n };\n\n const tx = this.db.transaction(() => {\n this.db\n .prepare(`\n INSERT INTO thread_slugs (slug, chat_identifier, display_name, service, is_group, participants, updated_at)\n VALUES (@slug, @chatIdentifier, @displayName, @service, @isGroup, @participants, @updatedAt)\n ON CONFLICT(slug) DO UPDATE SET\n chat_identifier = CASE\n WHEN instr(thread_slugs.chat_identifier, '@') = 0 AND instr(excluded.chat_identifier, '@') > 0\n THEN thread_slugs.chat_identifier\n ELSE excluded.chat_identifier\n END,\n display_name = excluded.display_name,\n service = excluded.service,\n is_group = excluded.is_group,\n participants = excluded.participants,\n updated_at = excluded.updated_at\n `)\n .run(params);\n\n this.db\n .prepare(`\n INSERT INTO slug_chat_guids (chat_guid, slug) VALUES (@chatGuid, @slug)\n ON CONFLICT(chat_guid) DO UPDATE SET slug = excluded.slug\n `)\n .run(params);\n });\n tx();\n }\n\n upsertMany(records: SlugRecord[]): void {\n const tx = this.db.transaction((recs: SlugRecord[]) => {\n for (const r of recs) this.upsert(r);\n });\n tx(records);\n }\n\n lookupBySlug(slug: string): SlugRecord | null {\n const row = this.db.prepare(\"SELECT * FROM thread_slugs WHERE slug = ?\").get(slug) as any;\n return row ? this.rowToRecord(row, this.representativeGuid(slug)) : null;\n }\n\n lookupByGuid(chatGuid: string): SlugRecord | null {\n const link = this.db\n .prepare(\"SELECT slug FROM slug_chat_guids WHERE chat_guid = ?\")\n .get(chatGuid) as { slug: string } | undefined;\n if (!link) return null;\n const row = this.db.prepare(\"SELECT * FROM thread_slugs WHERE slug = ?\").get(link.slug) as any;\n // Return the queried guid as the representative — it's the one the caller has.\n return row ? this.rowToRecord(row, chatGuid) : null;\n }\n\n lookupByChatIdentifier(chatIdentifier: string): SlugRecord | null {\n const row = this.db\n .prepare(\"SELECT * FROM thread_slugs WHERE chat_identifier = ?\")\n .get(chatIdentifier) as any;\n return row ? this.rowToRecord(row, this.representativeGuid(row.slug)) : null;\n }\n\n all(): SlugRecord[] {\n const rows = this.db\n .prepare(\"SELECT * FROM thread_slugs ORDER BY updated_at DESC\")\n .all() as any[];\n return rows.map((r) => this.rowToRecord(r, this.representativeGuid(r.slug)));\n }\n\n /** Every guid→slug link (used to rebuild the in-memory guid map on startup). */\n guidLinks(): { chatGuid: string; slug: string }[] {\n const rows = this.db.prepare(\"SELECT chat_guid, slug FROM slug_chat_guids\").all() as {\n chat_guid: string;\n slug: string;\n }[];\n return rows.map((r) => ({ chatGuid: r.chat_guid, slug: r.slug }));\n }\n\n /** A stable representative guid for a slug (lowest guid, or \"\" if none). */\n private representativeGuid(slug: string): string {\n const row = this.db\n .prepare(\"SELECT MIN(chat_guid) as guid FROM slug_chat_guids WHERE slug = ?\")\n .get(slug) as { guid: string | null } | undefined;\n return row?.guid ?? \"\";\n }\n\n /**\n * Drop guid links whose chat_guid is not in the valid set, then any identity\n * slug left with no guids. Returns the number of guid links removed.\n */\n prune(validGuids: Set<string>): number {\n const links = this.db.prepare(\"SELECT chat_guid FROM slug_chat_guids\").all() as {\n chat_guid: string;\n }[];\n const toDelete = links.filter((r) => !validGuids.has(r.chat_guid)).map((r) => r.chat_guid);\n const tx = this.db.transaction(() => {\n const delLink = this.db.prepare(\"DELETE FROM slug_chat_guids WHERE chat_guid = ?\");\n for (const guid of toDelete) delLink.run(guid);\n this.db.exec(\n \"DELETE FROM thread_slugs WHERE slug NOT IN (SELECT DISTINCT slug FROM slug_chat_guids)\",\n );\n });\n tx();\n return toDelete.length;\n }\n\n close(): void {\n this.db.close();\n }\n\n private rowToRecord(row: any, chatGuid: string): SlugRecord {\n return {\n slug: row.slug,\n chatGuid,\n chatIdentifier: row.chat_identifier,\n displayName: row.display_name,\n service: row.service,\n isGroup: Boolean(row.is_group),\n participants: row.participants,\n updatedAt: row.updated_at,\n };\n }\n}\n","/**\n * iMessage Database Reader\n *\n * All database access goes through a single better-sqlite3 connection.\n * attributedBody parsing uses our local TypedStreamParser fork.\n *\n * See docs/IMESSAGE_DB_SCHEMA.md for database structure reference.\n * Schema constants and epoch/timestamp helpers live in db-schema.ts.\n */\n\nimport { homedir } from \"node:os\";\nimport { join } from \"node:path\";\nimport Database from \"better-sqlite3\";\n\n/** Minimal chat row from the chat table. */\nexport interface ChatRow {\n ROWID: number;\n guid: string;\n chat_identifier: string;\n display_name: string | null;\n}\n\n/** Raw message row from the message table (columns used throughout this module). */\nexport interface MessageRow {\n ROWID: number;\n guid: string;\n text: string | null;\n attributedBody: Buffer | null;\n date: number;\n is_from_me: number;\n handle_id: string | null;\n cache_has_attachments: number;\n}\n\nexport interface MessageExportCursor {\n date: number;\n rowid: number;\n}\n\nexport interface MessageExportPage {\n messages: Message[];\n nextCursor: MessageExportCursor | null;\n rawCount: number;\n}\n\n/** A chat that appears to be the same person as the exported conversation but was not merged into it. */\nexport interface UnmergedSiblingChat {\n chatIdentifier: string;\n reason: string;\n}\n\nimport { extractAttributedBodyText } from \"./attributed-body-text.js\";\nimport { ContactsDB } from \"./contacts-db.js\";\nimport {\n isMetadataOnlySnippet,\n normalizeRichMetadataText,\n pickConversationSnippet,\n} from \"./conversation-snippet.js\";\nimport {\n AssociatedMessageType,\n isReactionType,\n MAC_EPOCH_OFFSET,\n macAutoTimestampToDate,\n NANOS_PER_SECOND,\n OBJECT_REPLACEMENT_CHAR,\n macTimestampToDate as schemaMacTimestampToDate,\n parseAssociatedMessageGuid as schemaParseAssociatedMessageGuid,\n Tables,\n} from \"./db-schema.js\";\nimport { perf } from \"./logger.js\";\nimport { extractChatSummaryText } from \"./plist-text.js\";\nimport { type SlugRecord, SlugStore } from \"./slug-store.js\";\nimport { generateThreadSlug, isGroupChatIdentifier, isGroupGuid } from \"./thread-slug.js\";\nimport type {\n Attachment,\n Conversation,\n Message,\n Reaction,\n ReplyContext,\n RichContentType,\n TapbackType,\n} from \"./types.js\";\n\n/** Chat row with last message date and service from join (for sorting by activity). */\ntype ChatWithLastDate = ChatRow & { last_date: number | null; service_name?: string | null };\ntype LastConversationRow = {\n chatId: number;\n lastDate: number;\n lastMessageId: number;\n lastService: string | null;\n lastIsFromMe: boolean;\n balloonBundleId: string | null;\n snippet: string | null;\n chatProperties: Buffer | null;\n};\ntype PreparedConversationEntry = {\n last?: LastConversationRow;\n mergeKey: string;\n conversation: Conversation;\n};\n\nfunction toChatRow(c: ChatWithLastDate): ChatRow {\n return {\n ROWID: c.ROWID,\n guid: c.guid,\n chat_identifier: c.chat_identifier,\n display_name: c.display_name,\n };\n}\n\n/**\n * Tapback type codes from iMessage database\n * 2000-2005: Add reaction, 3000-3005: Remove reaction\n */\nconst TAPBACK_TYPE_MAP: Record<number, { type: TapbackType; isRemoval: boolean }> = {\n 2000: { type: \"love\", isRemoval: false },\n 2001: { type: \"like\", isRemoval: false },\n 2002: { type: \"dislike\", isRemoval: false },\n 2003: { type: \"laugh\", isRemoval: false },\n 2004: { type: \"emphasize\", isRemoval: false },\n 2005: { type: \"question\", isRemoval: false },\n 2006: { type: \"emoji\", isRemoval: false }, // iOS 18+ custom emoji\n 3000: { type: \"love\", isRemoval: true },\n 3001: { type: \"like\", isRemoval: true },\n 3002: { type: \"dislike\", isRemoval: true },\n 3003: { type: \"laugh\", isRemoval: true },\n 3004: { type: \"emphasize\", isRemoval: true },\n 3005: { type: \"question\", isRemoval: true },\n 3006: { type: \"emoji\", isRemoval: true },\n 1000: { type: \"sticker\", isRemoval: false },\n};\n\n/** Use schema helper for parsing associated_message_guid. */\nconst parseAssociatedMessageGuid = schemaParseAssociatedMessageGuid;\n\nfunction isPlaceholderText(text: string | null | undefined): boolean {\n if (!text) return true;\n return /^[\\uFFFC\\uFFFD\\s]+$/u.test(text);\n}\n\nfunction isHiddenSystemItem(itemType: number | null | undefined): boolean {\n return (itemType ?? 0) !== 0;\n}\n\n/**\n * Determine rich content type from balloon_bundle_id\n */\nfunction getRichContentType(balloonBundleId: string | null): RichContentType | undefined {\n if (!balloonBundleId) return undefined;\n\n if (balloonBundleId === \"com.apple.messages.URLBalloonProvider\") {\n return \"link_preview\";\n }\n if (balloonBundleId === \"com.apple.DigitalTouchBalloonProvider\") {\n return \"digital_touch\";\n }\n if (balloonBundleId === \"com.apple.Handwriting.HandwritingProvider\") {\n return \"handwriting\";\n }\n if (balloonBundleId.includes(\"findmy\") || balloonBundleId.includes(\"Maps\")) {\n return \"location\";\n }\n if (balloonBundleId.includes(\"MSMessageExtensionBalloonPlugin\")) {\n return \"app_message\";\n }\n return \"unknown\";\n}\n\n/** Use schema helper for Mac epoch timestamps. */\nconst macTimestampToDate = schemaMacTimestampToDate;\n\nfunction dateToMacTimestamp(date: Date): number {\n return Math.floor((date.getTime() / 1000 - MAC_EPOCH_OFFSET) * NANOS_PER_SECOND);\n}\n\nexport class IMessageDB {\n private raw: Database.Database;\n private dbPath: string;\n /** Address-book reader. Public so MCP `*_contacts` tools can wrap it. */\n readonly contacts: ContactsDB;\n private slugStore: SlugStore;\n /** In-memory slug -> ChatWithLastDate for fast lookups during a session. */\n private slugMap = new Map<string, ChatWithLastDate>();\n /** In-memory chat guid -> slug for stable per-chat slug lookups. */\n private guidToSlug = new Map<string, string>();\n /** Per-sync canonical service per identity key (prefer iMessage) for slugs. */\n private identityServiceMap: Map<string, \"iMessage\" | \"SMS\"> | null = null;\n\n // ── TTL caches ───────────────────────────────────────────────────────\n private static readonly CACHE_TTL_MS = 30_000;\n private cachedAllChats: { data: ChatWithLastDate[]; ts: number } | null = null;\n private cachedLastByChat: { data: Record<number, LastConversationRow>; ts: number } | null = null;\n private cachedUnreadByChat: { data: Record<number, number>; ts: number } | null = null;\n private cachedParticipants = new Map<number, string[]>();\n private cachedMergeKeys = new Map<string, string>();\n private cachedSnippets = new Map<number, string | null>();\n /**\n * Per-chat reaction map (guid → reactions), TTL-cached. A large export pages\n * the same merged chats dozens of times; without this, every page re-scanned\n * each chat's full reaction set — O(pages × chats) — which dominated big\n * exports and spiked event-loop lag.\n *\n * Invalidation contract: entries expire via their per-entry TTL on read, and\n * the whole map is cleared only in scheduleBackgroundRefresh() — every read\n * path that can observe new messages schedules that refresh, so there is no\n * other clear site. Add one if you introduce a read path that bypasses it.\n */\n private cachedReactionsByChat = new Map<number, { data: Map<string, Reaction[]>; ts: number }>();\n private backgroundSyncNeeded = true;\n private backgroundRefreshScheduled = false;\n /** Set on close() so background chunked work stops touching a closed DB. */\n private closed = false;\n\n constructor(dbPath?: string, contactsDbPaths?: string | string[], slugStorePath?: string) {\n const span = perf(\"IMessageDB.constructor\");\n this.dbPath = dbPath || join(homedir(), \"Library\", \"Messages\", \"chat.db\");\n this.raw = new Database(this.dbPath, { readonly: true });\n this.contacts = new ContactsDB(contactsDbPaths);\n this.slugStore = new SlugStore(slugStorePath);\n\n const contactsSpan = perf(\"contacts.initialize\");\n try {\n this.contacts.initialize();\n contactsSpan.end();\n } catch (err) {\n contactsSpan.end({ error: String(err) });\n console.warn(\"Failed to initialize contacts database:\", err);\n }\n\n this.loadCachedSlugs();\n span.end();\n }\n\n /**\n * Fast startup: load persisted slugs from SlugStore into in-memory maps.\n * No chat.db queries -- uses the SQLite slug store populated by previous runs.\n */\n private loadCachedSlugs(): void {\n const span = perf(\"loadCachedSlugs\");\n // One slug can cover many chat legs — map every guid to its slug.\n for (const link of this.slugStore.guidLinks()) {\n this.guidToSlug.set(link.chatGuid, link.slug);\n }\n const records = this.slugStore.all();\n for (const r of records) {\n this.slugMap.set(r.slug, {\n ROWID: 0, // placeholder -- will be filled on full sync\n guid: r.chatGuid,\n chat_identifier: r.chatIdentifier,\n display_name: r.displayName,\n service_name: r.service ?? null,\n last_date: null,\n });\n }\n span.end({ loaded: records.length });\n }\n\n /**\n * Compute the canonical slug for a chat without persisting anything.\n *\n * Identity key: every chat leg of one contact (phone + email, SMS +\n * iMessage) shares this, so they collapse to ONE stable slug. Groups stay\n * per-guid. Mirrors getConversationMergeKey so the slug tracks the merge.\n * The service segment is identity-wide (prefer iMessage) — otherwise the\n * SMS leg and iMessage leg of one contact would produce `…~sms~h` and\n * `…~imsg~h`, i.e. two slugs.\n */\n private computeSlugForChat(chat: ChatWithLastDate): {\n slug: string;\n isGroup: boolean;\n resolvedName: string | null;\n canonicalService: \"iMessage\" | \"SMS\";\n } {\n const isGroup = isGroupGuid(chat.guid) || isGroupChatIdentifier(chat.chat_identifier);\n\n // Merge key groups legs within a session; the service map is keyed on it.\n const mergeKey = isGroup\n ? `group:${chat.guid}`\n : this.getConversationMergeKey(chat.chat_identifier, chat.guid, false);\n\n const canonicalService = isGroup\n ? this.detectServiceForChat(chat)\n : (this.identityServiceMap?.get(mergeKey) ?? this.detectServiceForChat(chat));\n\n // The persisted slug hash must NOT embed the session contactId (ids are\n // assigned in Address Book load order and renumber when any card changes).\n // Anchor on the contact's smallest normalized handle instead, and use the\n // identity-level (contact card) name so every leg of a union produces the\n // same name part even when per-handle display names differ.\n let identityKey = mergeKey;\n let resolvedName: string | null = null;\n if (!isGroup && chat.chat_identifier) {\n const lookup = this.contacts.lookupContact(chat.chat_identifier);\n if (lookup) {\n const anchor = this.contacts.stableAnchor(lookup.contactId);\n if (anchor) identityKey = `contact:${anchor}`;\n resolvedName =\n this.contacts.getContact(lookup.contactId)?.displayName ?? lookup.displayName;\n } else {\n const fallback = this.contacts.lookupHandle(chat.chat_identifier);\n resolvedName = fallback !== chat.chat_identifier ? fallback : null;\n }\n }\n\n const slug = generateThreadSlug({\n chatIdentifier: chat.chat_identifier,\n guid: chat.guid,\n displayName: chat.display_name,\n serviceName: canonicalService,\n resolvedContactName: resolvedName,\n identityKey,\n });\n return { slug, isGroup, resolvedName, canonicalService };\n }\n\n /** Generate and cache a slug for a single chat. */\n private syncSlugForChat(chat: ChatWithLastDate): string {\n const { slug, isGroup, resolvedName, canonicalService } = this.computeSlugForChat(chat);\n\n const participants = isGroup ? this.fetchChatParticipants(chat.ROWID) : [chat.chat_identifier];\n\n this.slugStore.upsert({\n slug,\n chatGuid: chat.guid,\n chatIdentifier: chat.chat_identifier,\n displayName: resolvedName ?? chat.display_name ?? null,\n service: canonicalService,\n isGroup,\n participants: participants.join(\",\"),\n updatedAt: Date.now(),\n });\n\n this.slugMap.set(slug, chat);\n this.guidToSlug.set(chat.guid, slug);\n return slug;\n }\n\n /**\n * Schedule a full slug sync in the background using setImmediate chunks.\n * Each chunk processes up to 100 chats then yields the event loop.\n */\n scheduleBackgroundSlugSync(): void {\n if (!this.backgroundSyncNeeded) return;\n this.backgroundSyncNeeded = false;\n\n const span = perf(\"backgroundSlugSync\");\n const chats = this.getAllChatsWithLastDate();\n\n // Precompute one canonical service per identity (prefer iMessage) so every\n // leg of a contact hashes into the SAME slug regardless of SMS/iMessage.\n const identityService = new Map<string, \"iMessage\" | \"SMS\">();\n for (const c of chats) {\n if (isGroupGuid(c.guid) || isGroupChatIdentifier(c.chat_identifier)) continue;\n const key = this.getConversationMergeKey(c.chat_identifier, c.guid, false);\n const svc = this.detectServiceForChat(c);\n if (svc === \"iMessage\" || !identityService.has(key)) identityService.set(key, svc);\n }\n this.identityServiceMap = identityService;\n\n const validGuids = new Set<string>();\n let index = 0;\n let synced = 0;\n const CHUNK = 100;\n\n const processChunk = () => {\n if (this.closed) return; // DB closed mid-sync (e.g. shutdown) — stop cleanly.\n const end = Math.min(index + CHUNK, chats.length);\n for (; index < end; index++) {\n const chat = chats[index];\n validGuids.add(chat.guid);\n // Self-heal: re-sync not only unknown guids but any guid whose expected\n // canonical slug changed (contact data or merge policy changed since it\n // was stored — e.g. a false contact union corrected). The store's\n // guid→slug upsert remaps the leg; prune sweeps orphaned slug rows.\n const expected = this.computeSlugForChat(chat).slug;\n if (this.guidToSlug.get(chat.guid) !== expected) {\n this.syncSlugForChat(chat);\n synced++;\n } else {\n // Update the in-memory map with fresh data (ROWID, last_date)\n this.slugMap.set(expected, chat);\n }\n }\n if (index < chats.length) {\n setImmediate(processChunk);\n } else {\n this.slugStore.prune(validGuids);\n span.end({ chats: chats.length, newSlugs: synced });\n }\n };\n\n setImmediate(processChunk);\n }\n\n /** Look up a chat by thread slug. */\n findChatBySlug(slug: string): ChatRow | null {\n const cached = this.slugMap.get(slug);\n if (cached) return toChatRow(cached);\n const record = this.slugStore.lookupBySlug(slug);\n if (!record) return null;\n return this.findChatByIdentifier(record.chatIdentifier);\n }\n\n /** Get the slug for a specific chat GUID. */\n getSlugForChatGuid(chatGuid: string): string | null {\n const cached = this.guidToSlug.get(chatGuid);\n if (cached) return cached;\n const record = this.slugStore.lookupByGuid(chatGuid);\n return record?.slug ?? null;\n }\n\n /** Get the slug record for a chat_identifier (for attaching to output). */\n getSlugForChatIdentifier(chatIdentifier: string): string | null {\n const matches = [...this.slugMap.entries()].filter(\n ([, chat]) => chat.chat_identifier === chatIdentifier,\n );\n if (matches.length === 1) return matches[0][0];\n if (matches.length > 1) return null;\n const record = this.slugStore.lookupByChatIdentifier(chatIdentifier);\n return record?.slug ?? null;\n }\n\n /** Get slug record by slug string. */\n getSlugRecord(slug: string): SlugRecord | null {\n return this.slugStore.lookupBySlug(slug);\n }\n\n /** Get all slug records. */\n getAllSlugs(): SlugRecord[] {\n return this.slugStore.all();\n }\n\n /** Fetch group chat participants from chat_handle_join. */\n private fetchChatParticipants(chatRowId: number): string[] {\n const cached = this.cachedParticipants.get(chatRowId);\n if (cached) return cached;\n const stmt = this.raw.prepare(`\n SELECT h.id\n FROM ${Tables.CHAT_HANDLE_JOIN} chj\n JOIN ${Tables.HANDLE} h ON chj.handle_id = h.ROWID\n WHERE chj.chat_id = ?\n `);\n const rows = stmt.all(chatRowId) as { id: string }[];\n const result = rows.map((r) => r.id);\n this.cachedParticipants.set(chatRowId, result);\n return result;\n }\n\n /** Detect service type from chat data. */\n private detectServiceForChat(chat: ChatWithLastDate): \"iMessage\" | \"SMS\" {\n if (chat.service_name) {\n return chat.service_name.toLowerCase().includes(\"sms\") ? \"SMS\" : \"iMessage\";\n }\n if (chat.guid) {\n return chat.guid.toLowerCase().startsWith(\"sms\") ? \"SMS\" : \"iMessage\";\n }\n return \"iMessage\";\n }\n\n /**\n * Get the N most recent messages across all conversations\n * By default excludes reactions (tapbacks) for cleaner output\n */\n async getRecentMessages(\n limit: number = 20,\n includeReactions: boolean = false,\n ): Promise<Message[]> {\n const span = perf(\"getRecentMessages\");\n const chats = this.getAllChatsWithLastDate()\n .sort((a, b) => (b.last_date ?? 0) - (a.last_date ?? 0))\n .slice(0, 10);\n\n const allMessages: Message[] = [];\n\n for (const chat of chats) {\n try {\n const rows = this.fetchMessagesForChatRowId(chat.ROWID, Math.min(limit * 2, 40));\n const extBatch = this.fetchExtendedMessageDataBatch(rows.map((r) => r.ROWID));\n\n for (const msg of rows) {\n const text = this.parseMessageText(msg);\n const ext = extBatch.get(msg.ROWID) ?? {};\n if (isHiddenSystemItem(ext.item_type)) continue;\n const converted = this.convertMessage(msg, text, chat.chat_identifier, ext);\n\n if (!includeReactions && converted.isReaction) continue;\n\n allMessages.push(converted);\n }\n } catch {\n // Skip chats that fail to load\n }\n }\n\n const result = allMessages.sort((a, b) => b.date.getTime() - a.date.getTime()).slice(0, limit);\n span.end({ limit, returned: result.length });\n return result;\n }\n\n /**\n * Get messages from a specific conversation, sorted by date ascending (chronological).\n * By default excludes reactions (tapbacks) for cleaner output.\n */\n async getMessagesForChat(\n chatIdentifier: string,\n limit: number = 50,\n options: {\n includeReactions?: boolean;\n includeReactionDetails?: boolean;\n beforeMessageId?: number;\n afterMessageId?: number;\n } = {},\n ): Promise<Message[]> {\n const span = perf(\"getMessagesForChat\");\n const {\n includeReactions = false,\n includeReactionDetails = false,\n beforeMessageId,\n afterMessageId,\n } = options;\n const chats = this.resolveChatsForConversation(chatIdentifier);\n if (chats.length === 0) {\n span.end({ limit, returned: 0 });\n return [];\n }\n\n const perChatLimit = Math.max(limit * 2, 50);\n // The pagination cursors are ids, but ordering is by date — resolve each\n // cursor's date so paging bounds messages by the composite (date, ROWID).\n const beforeDate = beforeMessageId != null ? this.dateOfMessage(beforeMessageId) : undefined;\n const afterDate = afterMessageId != null ? this.dateOfMessage(afterMessageId) : undefined;\n const result = new Map<number, Message>();\n for (const chat of chats) {\n const rows = this.fetchMessagesForChatRowId(\n chat.ROWID,\n perChatLimit,\n beforeMessageId,\n afterMessageId,\n beforeDate,\n afterDate,\n );\n const extBatch = this.fetchExtendedMessageDataBatch(rows.map((r) => r.ROWID));\n\n // Batch-fetch reactions for the entire chat instead of per-message LIKE queries\n const reactionsByGuid = includeReactionDetails\n ? this.fetchReactionsForChat(chat.ROWID)\n : undefined;\n\n for (const msg of rows) {\n const text = this.parseMessageText(msg);\n const ext = extBatch.get(msg.ROWID) ?? {};\n if (isHiddenSystemItem(ext.item_type)) continue;\n const converted = this.convertMessage(\n msg,\n text,\n chat.chat_identifier,\n ext,\n false, // don't let convertMessage fetch reactions individually\n );\n\n // Attach pre-fetched reactions\n if (reactionsByGuid && !converted.isReaction) {\n const rxns = reactionsByGuid.get(msg.guid);\n if (rxns && rxns.length > 0) {\n converted.reactions = this.consolidateReactions(rxns);\n if (converted.reactions.length === 0) converted.reactions = undefined;\n }\n }\n\n if (!includeReactions && converted.isReaction) continue;\n\n result.set(converted.id, converted);\n }\n }\n\n // Sort by timestamp ascending (chronological conversation order)\n const sorted = [...result.values()]\n .sort((a, b) => a.date.getTime() - b.date.getTime())\n .slice(-limit);\n span.end({ limit, chats: chats.length, returned: sorted.length });\n return sorted;\n }\n /**\n * Get one chronological export page for a conversation.\n *\n * This deliberately does not use getMessagesForChat's ROWID-only\n * beforeMessageId contract. Exports need to walk the whole conversation in\n * display order across every chat row that resolves to the same visible\n * thread, dedupe messages linked to multiple chats before applying LIMIT,\n * and avoid letting reaction/system rows consume page slots.\n */\n async getMessagesForChatExportPage(\n chatIdentifier: string,\n limit: number = 1000,\n options: {\n includeReactions?: boolean;\n includeReactionDetails?: boolean;\n afterCursor?: MessageExportCursor | null;\n since?: Date | null;\n until?: Date | null;\n } = {},\n ): Promise<MessageExportPage> {\n const span = perf(\"getMessagesForChatExportPage\");\n const {\n includeReactions = false,\n includeReactionDetails = false,\n afterCursor,\n since,\n until,\n } = options;\n const pageLimit = Math.max(1, limit);\n const chats = this.resolveChatsForConversation(chatIdentifier);\n if (chats.length === 0) {\n span.end({ limit: pageLimit, returned: 0 });\n return { messages: [], nextCursor: null, rawCount: 0 };\n }\n\n const chatIds = chats.map((chat) => chat.ROWID);\n const chatPlaceholders = chatIds.map(() => \"?\").join(\",\");\n const conditions: string[] = [\n `cmj.chat_id IN (${chatPlaceholders})`,\n \"COALESCE(m.item_type, 0) = 0\",\n ];\n const params: unknown[] = [...chatIds];\n\n if (!includeReactions) {\n conditions.push(`COALESCE(m.associated_message_type, 0) = ${AssociatedMessageType.NORMAL}`);\n }\n if (afterCursor) {\n conditions.push(\"(m.date > ? OR (m.date = ? AND m.ROWID > ?))\");\n params.push(afterCursor.date, afterCursor.date, afterCursor.rowid);\n }\n if (since) {\n conditions.push(\"m.date >= ?\");\n params.push(dateToMacTimestamp(since));\n }\n if (until) {\n conditions.push(\"m.date <= ?\");\n params.push(dateToMacTimestamp(until));\n }\n params.push(pageLimit);\n\n const stmt = this.raw.prepare(`\n WITH target_messages AS (\n SELECT\n m.ROWID,\n m.date,\n MIN(c.chat_identifier) as chat_identifier\n FROM ${Tables.MESSAGE} m\n JOIN ${Tables.CHAT_MESSAGE_JOIN} cmj ON m.ROWID = cmj.message_id\n JOIN ${Tables.CHAT} c ON c.ROWID = cmj.chat_id\n WHERE ${conditions.join(\"\\n AND \")}\n GROUP BY m.ROWID, m.date\n ORDER BY m.date ASC, m.ROWID ASC\n LIMIT ?\n )\n SELECT\n m.ROWID,\n m.guid,\n m.text,\n m.attributedBody,\n m.date,\n m.is_from_me,\n h.id as handle_id,\n h.service as handle_service,\n m.cache_has_attachments,\n tm.chat_identifier,\n m.is_read,\n m.date_read,\n m.is_delivered,\n m.date_delivered,\n m.associated_message_type,\n m.associated_message_guid,\n m.associated_message_emoji,\n m.thread_originator_guid,\n m.thread_originator_part,\n m.balloon_bundle_id,\n m.item_type,\n m.date_edited,\n m.date_retracted,\n m.message_summary_info,\n m.payload_data\n FROM target_messages tm\n JOIN ${Tables.MESSAGE} m ON m.ROWID = tm.ROWID\n LEFT JOIN ${Tables.HANDLE} h ON m.handle_id = h.ROWID\n ORDER BY tm.date ASC, tm.ROWID ASC\n `);\n\n const rows = stmt.all(...params) as (MessageRow &\n ExtendedMessageData & { chat_identifier: string | null })[];\n const reactionsByGuid = new Map<string, Reaction[]>();\n if (includeReactionDetails) {\n for (const chat of chats) {\n for (const [guid, reactions] of this.fetchReactionsForChat(chat.ROWID)) {\n const existing = reactionsByGuid.get(guid);\n if (existing) existing.push(...reactions);\n else reactionsByGuid.set(guid, [...reactions]);\n }\n }\n }\n\n const messages: Message[] = [];\n for (const row of rows) {\n const text = this.parseMessageText(row);\n if (isHiddenSystemItem(row.item_type)) continue;\n const converted = this.convertMessage(\n row,\n text,\n row.chat_identifier ?? chatIdentifier,\n row,\n false,\n );\n if (!includeReactions && converted.isReaction) continue;\n\n if (includeReactionDetails && !converted.isReaction) {\n const reactions = reactionsByGuid.get(row.guid);\n if (reactions && reactions.length > 0) {\n converted.reactions = this.consolidateReactions(reactions);\n if (converted.reactions.length === 0) converted.reactions = undefined;\n }\n }\n messages.push(converted);\n }\n\n const lastRow = rows[rows.length - 1];\n const nextCursor = lastRow ? { date: lastRow.date, rowid: lastRow.ROWID } : null;\n span.end({\n limit: pageLimit,\n chats: chats.length,\n rows: rows.length,\n returned: messages.length,\n });\n return { messages, nextCursor, rawCount: rows.length };\n }\n\n /**\n * Get unread messages across all conversations, sorted by date descending (newest first).\n * Excludes reactions for cleaner output.\n * @param limit Max number of messages to return (default 100).\n */\n async getUnreadMessages(limit: number = 100): Promise<Message[]> {\n const span = perf(\"getUnreadMessages\");\n const stmt = this.raw.prepare(`\n SELECT\n m.ROWID,\n m.guid,\n m.text,\n m.attributedBody,\n m.date,\n m.is_from_me,\n h.id as handle_id,\n m.cache_has_attachments,\n c.chat_identifier\n FROM ${Tables.MESSAGE} m\n LEFT JOIN ${Tables.HANDLE} h ON m.handle_id = h.ROWID\n LEFT JOIN ${Tables.CHAT_MESSAGE_JOIN} cmj ON m.ROWID = cmj.message_id\n LEFT JOIN ${Tables.CHAT} c ON cmj.chat_id = c.ROWID\n WHERE m.is_from_me = 0\n AND m.is_read = 0\n AND m.associated_message_type = ${AssociatedMessageType.NORMAL}\n AND COALESCE(m.item_type, 0) = 0\n ORDER BY m.date DESC\n LIMIT ?\n `);\n\n const rows = stmt.all(limit) as (MessageRow & { chat_identifier: string | null })[];\n const validRows = rows.filter((r) => r.chat_identifier != null);\n const extBatch = this.fetchExtendedMessageDataBatch(validRows.map((r) => r.ROWID));\n\n const result: Message[] = [];\n for (const row of validRows) {\n const text = this.parseMessageText(row);\n const ext = extBatch.get(row.ROWID) ?? {};\n result.push(this.convertMessage(row, text, row.chat_identifier!, ext));\n }\n\n result.sort((a, b) => b.date.getTime() - a.date.getTime());\n const sliced = result.slice(0, limit);\n span.end({ limit, rows: rows.length, returned: sliced.length });\n return sliced;\n }\n\n /**\n * Get the most recent message in a conversation\n */\n async getLastMessage(chatIdentifier: string): Promise<Message | null> {\n const messages = await this.getMessagesForChat(chatIdentifier, 1);\n return messages.length > 0 ? messages[messages.length - 1] : null;\n }\n\n /**\n * Get messages after a specific message ID (for polling new messages).\n * Returns incoming messages only, sorted by date ascending (chronological).\n */\n async getMessagesAfter(chatIdentifier: string, afterMessageId: number): Promise<Message[]> {\n const messages = await this.getMessagesForChat(chatIdentifier, 1000, { afterMessageId });\n // \"After\" means after the boundary in (date, ROWID) order — NOT a bare id\n // comparison. A reply synced with a lower ROWID but later date is still\n // new; an id-only filter here silently dropped it (wait_for_reply missed\n // the reply). The SQL bound already excludes the boundary itself.\n const filtered = messages.filter((m) => m.id !== afterMessageId && !m.isFromMe);\n filtered.sort((a, b) => a.date.getTime() - b.date.getTime());\n return filtered;\n }\n\n /**\n * List all conversations with metadata, sorted by last message date (newest first).\n * Populates lastMessageDate, lastMessageSnippet, and unreadCount to match Messages.app left pane.\n */\n async listConversations(limit: number = 200): Promise<Conversation[]> {\n const span = perf(\"listConversations\");\n const chats = this.getAllChats();\n const lastByChat = this.getLastMessageByChat();\n const unreadByChat = this.getUnreadByChat();\n\n // ── Pass 1: lightweight sort entries for ALL chats (no DB lookups) ──\n type SortEntry = {\n chat: ChatRow;\n lastDate: number;\n isGroup: boolean;\n last?: LastConversationRow;\n };\n const sortEntries: SortEntry[] = chats.map((chat) => ({\n chat,\n lastDate: lastByChat[chat.ROWID]?.lastDate ?? 0,\n isGroup: isGroupGuid(chat.guid) || isGroupChatIdentifier(chat.chat_identifier),\n last: lastByChat[chat.ROWID],\n }));\n\n // Sort by last message date descending\n sortEntries.sort((a, b) => b.lastDate - a.lastDate);\n\n // ── Pass 2: enrich lazily in chunks until enough DEDUPED rows exist ──\n // A fixed over-fetch factor (previously limit*3) starves offset paging\n // when many chat rows merge into few conversations: the deduped list came\n // up short of the requested window and the page silently truncated. Keep\n // enriching until the deduped count reaches `limit` or chats run out.\n const enrich = ({ chat, isGroup, last }: SortEntry) => {\n const lastDate = last ? macTimestampToDate(last.lastDate) : null;\n const rawIdentifier = chat.chat_identifier;\n\n let displayName = chat.display_name;\n if (!displayName && rawIdentifier && !isGroup) {\n const resolved = this.contacts.lookupHandle(rawIdentifier);\n displayName = resolved !== rawIdentifier ? resolved : null;\n }\n\n const participants = isGroup ? this.fetchChatParticipants(chat.ROWID) : [rawIdentifier];\n const mergeKey = this.getConversationMergeKey(rawIdentifier, chat.guid, isGroup);\n const slug = this.getSlugForChatGuid(chat.guid) ?? rawIdentifier;\n const chatData = this.slugMap.get(slug);\n const serviceType = chatData ? this.detectServiceForChat(chatData) : \"iMessage\";\n\n return {\n last,\n mergeKey,\n conversation: {\n chatId: chat.guid,\n chatIdentifier: rawIdentifier,\n displayName: displayName || null,\n rawIdentifier,\n participants,\n lastMessageDate: lastDate,\n lastMessageSnippet: null,\n unreadCount: unreadByChat[chat.ROWID] ?? 0,\n threadSlug: slug,\n isGroupChat: isGroup,\n serviceType,\n } satisfies Conversation,\n };\n };\n\n const CHUNK = Math.max(limit, 200);\n const prepared: ReturnType<typeof enrich>[] = [];\n let cursor = 0;\n let deduped = this.mergeDuplicateConversations(prepared);\n while (deduped.length < limit && cursor < sortEntries.length) {\n const chunk = sortEntries.slice(cursor, cursor + CHUNK);\n cursor += chunk.length;\n for (const entry of chunk) prepared.push(enrich(entry));\n deduped = this.mergeDuplicateConversations(prepared);\n }\n const selected = deduped.slice(0, limit);\n\n const result = selected.map(({ conversation, last }) => ({\n ...conversation,\n lastMessageSnippet: this.resolveConversationSnippet(last),\n }));\n span.end({\n chats: chats.length,\n enriched: cursor,\n deduped: deduped.length,\n returned: result.length,\n });\n return result;\n }\n\n /**\n * Find a chat by phone number, email, or chat identifier.\n * Uses a single join (chats + last message date), then filters and sorts by date.\n * When multiple chats match (e.g. same number, different threads), returns the one with the most recent message.\n */\n async findChatByHandle(handle: string): Promise<Conversation | null> {\n const chats = this.getAllChatsWithLastDate();\n\n // Normalize the search handle\n const normalized = handle.replace(/[\\s\\-()]/g, \"\").toLowerCase();\n\n const matches = chats.filter((chat) => {\n const chatNorm = chat.chat_identifier?.replace(/[\\s\\-()]/g, \"\").toLowerCase() || \"\";\n return chatNorm.includes(normalized) || normalized.includes(chatNorm);\n });\n\n if (matches.length === 0) return null;\n\n const found = this.pickMostRecentChat(matches);\n const rawIdentifier = found.chat_identifier;\n const isGroup = isGroupGuid(found.guid) || isGroupChatIdentifier(rawIdentifier);\n let displayName = found.display_name;\n if (!displayName && rawIdentifier && !isGroup) {\n const resolved = this.contacts.lookupHandle(rawIdentifier);\n displayName = resolved !== rawIdentifier ? resolved : null;\n }\n\n const slug = this.getSlugForChatGuid(found.guid) ?? rawIdentifier;\n const chatData = this.slugMap.get(slug);\n const serviceType = chatData ? this.detectServiceForChat(chatData) : \"iMessage\";\n\n return {\n chatId: found.guid,\n chatIdentifier: rawIdentifier,\n displayName: displayName || null,\n rawIdentifier,\n participants: isGroup ? this.fetchChatParticipants(found.ROWID) : [rawIdentifier],\n lastMessageDate: null,\n lastMessageSnippet: null,\n unreadCount: 0,\n threadSlug: slug,\n isGroupChat: isGroup,\n serviceType,\n };\n }\n\n /**\n * Search messages across all conversations.\n *\n * Strategy: search the `text` column via LIKE first (covers most messages).\n * For messages where text is NULL but attributedBody exists, we fetch a larger\n * window and post-filter after parsing the blob. The upstream imessage-parser\n * had a bug here: its SQL `WHERE text LIKE ? OR attributedBody IS NOT NULL`\n * with a LIMIT meant the LIMIT was consumed by non-matching attributedBody rows,\n * hiding older text matches entirely.\n */\n async searchMessages(query: string, limit: number = 20): Promise<Message[]> {\n const span = perf(\"searchMessages\");\n // Phase 1: direct text match -- fast and reliable\n const textStmt = this.raw.prepare(`\n SELECT\n m.ROWID, m.guid, m.text, m.attributedBody, m.date,\n m.is_from_me, h.id as handle_id, m.cache_has_attachments,\n c.chat_identifier\n FROM ${Tables.MESSAGE} m\n LEFT JOIN ${Tables.HANDLE} h ON m.handle_id = h.ROWID\n LEFT JOIN ${Tables.CHAT_MESSAGE_JOIN} cmj ON m.ROWID = cmj.message_id\n LEFT JOIN ${Tables.CHAT} c ON cmj.chat_id = c.ROWID\n WHERE m.text LIKE ?\n AND m.associated_message_type = ${AssociatedMessageType.NORMAL}\n AND COALESCE(m.item_type, 0) = 0\n ORDER BY m.date DESC\n LIMIT ?\n `);\n\n const textRows = textStmt.all(`%${query}%`, limit * 2) as (MessageRow & {\n chat_identifier: string | null;\n })[];\n const seenIds = new Set<number>();\n const messages: Message[] = [];\n\n for (const row of textRows) {\n if (!row.chat_identifier) continue;\n seenIds.add(row.ROWID);\n const text = this.parseMessageText(row);\n const ext = this.fetchExtendedMessageData(row.ROWID);\n if (isHiddenSystemItem(ext.item_type)) continue;\n const converted = this.convertMessage(row, text, row.chat_identifier, ext);\n if (converted.isReaction) continue;\n messages.push(converted);\n if (messages.length >= limit) return messages;\n }\n\n // Phase 2: scan attributedBody-only messages (text IS NULL) for matches\n // Use a generous window so we don't miss older messages\n const blobStmt = this.raw.prepare(`\n SELECT\n m.ROWID, m.guid, m.text, m.attributedBody, m.date,\n m.is_from_me, h.id as handle_id, m.cache_has_attachments,\n c.chat_identifier\n FROM ${Tables.MESSAGE} m\n LEFT JOIN ${Tables.HANDLE} h ON m.handle_id = h.ROWID\n LEFT JOIN ${Tables.CHAT_MESSAGE_JOIN} cmj ON m.ROWID = cmj.message_id\n LEFT JOIN ${Tables.CHAT} c ON cmj.chat_id = c.ROWID\n WHERE m.text IS NULL\n AND m.attributedBody IS NOT NULL\n AND m.associated_message_type = ${AssociatedMessageType.NORMAL}\n AND COALESCE(m.item_type, 0) = 0\n ORDER BY m.date DESC\n LIMIT ?\n `);\n\n const blobRows = blobStmt.all(limit * 20) as (MessageRow & {\n chat_identifier: string | null;\n })[];\n const queryLower = query.toLowerCase();\n\n for (const row of blobRows) {\n if (!row.chat_identifier || seenIds.has(row.ROWID)) continue;\n const text = this.parseMessageText(row);\n if (!text || !text.toLowerCase().includes(queryLower)) continue;\n const ext = this.fetchExtendedMessageData(row.ROWID);\n if (isHiddenSystemItem(ext.item_type)) continue;\n const converted = this.convertMessage(row, text, row.chat_identifier, ext);\n if (converted.isReaction) continue;\n messages.push(converted);\n if (messages.length >= limit) break;\n }\n\n span.end({\n query,\n limit,\n textHits: textRows.length,\n blobScanned: blobRows.length,\n returned: messages.length,\n });\n return messages;\n }\n\n /**\n * Get a deep link to open a specific conversation in Messages.app\n */\n getConversationLink(chatIdentifier: string): string {\n return `imessage://${encodeURIComponent(chatIdentifier)}`;\n }\n\n private resolveConversationSnippet(last?: LastConversationRow): string | null {\n if (!last) return null;\n const cached = this.cachedSnippets.get(last.lastMessageId);\n if (cached !== undefined) return cached;\n\n let result: string | null = null;\n\n const directSnippet = pickConversationSnippet({ rawText: last.snippet });\n if (directSnippet && !this.shouldFallbackToPreviousSnippet(directSnippet, last)) {\n result = directSnippet;\n }\n\n if (result == null) {\n const parsedSnippet = pickConversationSnippet({\n parsedText: this.getMessageTextByRowId(last.lastMessageId),\n });\n if (parsedSnippet && !this.shouldFallbackToPreviousSnippet(parsedSnippet, last)) {\n result = parsedSnippet;\n }\n }\n\n if (result == null) {\n result = this.getPreviousConversationSnippet(last);\n }\n\n if (result == null) {\n result = pickConversationSnippet({\n summaryText: extractChatSummaryText(last.chatProperties) ?? null,\n });\n }\n\n this.cachedSnippets.set(last.lastMessageId, result);\n return result;\n }\n\n private shouldFallbackToPreviousSnippet(snippet: string, last: LastConversationRow): boolean {\n return Boolean(\n last.balloonBundleId?.includes(\"URLBalloonProvider\") && isMetadataOnlySnippet(snippet),\n );\n }\n\n private getPreviousConversationSnippet(last: LastConversationRow): string | null {\n const rows = this.raw\n .prepare(`\n SELECT m.ROWID, m.text, m.attributedBody, m.date, m.is_from_me\n FROM ${Tables.MESSAGE} m\n JOIN ${Tables.CHAT_MESSAGE_JOIN} cmj ON cmj.message_id = m.ROWID\n WHERE cmj.chat_id = ?\n AND m.associated_message_type = ${AssociatedMessageType.NORMAL}\n AND COALESCE(m.item_type, 0) = 0\n AND (m.date < ? OR (m.date = ? AND m.ROWID < ?))\n ORDER BY m.date DESC, m.ROWID DESC\n LIMIT 5\n `)\n .all(last.chatId, last.lastDate, last.lastDate, last.lastMessageId) as Array<{\n ROWID: number;\n text: string | null;\n attributedBody: Buffer | null;\n date: number;\n is_from_me: number;\n }>;\n\n for (const row of rows) {\n if (Boolean(row.is_from_me) !== last.lastIsFromMe) break;\n if (Math.abs(last.lastDate - row.date) > 60_000_000_000) break;\n\n const snippet = pickConversationSnippet({\n rawText: row.text,\n parsedText: this.extractMessageText(row),\n });\n\n if (snippet && !isMetadataOnlySnippet(snippet)) {\n return snippet;\n }\n }\n\n return null;\n }\n\n private mergeDuplicateConversations(prepared: PreparedConversationEntry[]) {\n const merged: typeof prepared = [];\n const indexByKey = new Map<string, number>();\n\n for (const entry of prepared) {\n const existingIndex = indexByKey.get(entry.mergeKey);\n if (existingIndex === undefined) {\n indexByKey.set(entry.mergeKey, merged.length);\n merged.push(entry);\n continue;\n }\n\n merged[existingIndex] = this.mergeConversationEntries(merged[existingIndex], entry);\n }\n\n return merged;\n }\n\n private mergeConversationEntries(\n left: PreparedConversationEntry,\n right: PreparedConversationEntry,\n ) {\n const preferred = this.pickPreferredConversationEntry(left, right);\n const other = preferred === left ? right : left;\n const sameIdentifier =\n preferred.conversation.chatIdentifier === other.conversation.chatIdentifier;\n\n return {\n mergeKey: preferred.mergeKey,\n last: preferred.last ?? other.last,\n conversation: {\n ...preferred.conversation,\n displayName: preferred.conversation.displayName ?? other.conversation.displayName,\n participants: [\n ...new Set([...preferred.conversation.participants, ...other.conversation.participants]),\n ],\n unreadCount: sameIdentifier\n ? Math.max(preferred.conversation.unreadCount, other.conversation.unreadCount)\n : preferred.conversation.unreadCount + other.conversation.unreadCount,\n },\n };\n }\n\n private pickPreferredConversationEntry(\n left: PreparedConversationEntry,\n right: PreparedConversationEntry,\n ): PreparedConversationEntry {\n const leftTime = left.conversation.lastMessageDate?.getTime() ?? 0;\n const rightTime = right.conversation.lastMessageDate?.getTime() ?? 0;\n if (leftTime !== rightTime) {\n return leftTime > rightTime ? left : right;\n }\n\n const preferredService = left.last?.lastService ?? right.last?.lastService ?? null;\n if (preferredService) {\n const preferredType = preferredService.toLowerCase().includes(\"sms\") ? \"SMS\" : \"iMessage\";\n if (\n left.conversation.serviceType === preferredType &&\n right.conversation.serviceType !== preferredType\n ) {\n return left;\n }\n if (\n right.conversation.serviceType === preferredType &&\n left.conversation.serviceType !== preferredType\n ) {\n return right;\n }\n }\n\n if (left.conversation.displayName && !right.conversation.displayName) return left;\n if (right.conversation.displayName && !left.conversation.displayName) return right;\n\n if (left.conversation.serviceType === \"iMessage\" && right.conversation.serviceType === \"SMS\") {\n return left;\n }\n if (right.conversation.serviceType === \"iMessage\" && left.conversation.serviceType === \"SMS\") {\n return right;\n }\n\n return left;\n }\n\n private getConversationMergeKey(\n chatIdentifier: string,\n chatGuid: string,\n isGroup: boolean,\n ): string {\n const cacheKey = `${chatIdentifier}::${chatGuid}::${isGroup}`;\n const cached = this.cachedMergeKeys.get(cacheKey);\n if (cached) return cached;\n\n let key: string;\n if (isGroup) {\n key = `group:${chatGuid}`;\n } else {\n const contact = this.contacts.lookupContact(chatIdentifier);\n key = contact\n ? `contact:${contact.contactId}`\n : `identifier:${chatIdentifier.replace(/[\\s\\-()]/g, \"\").toLowerCase()}`;\n }\n\n this.cachedMergeKeys.set(cacheKey, key);\n return key;\n }\n\n private resolveChatsForConversation(identifier: string): ChatRow[] {\n const chats = this.getAllChatsWithLastDate();\n const normalized = identifier.replace(/[\\s\\-()]/g, \"\").toLowerCase();\n\n const directMatches = chats.filter(\n (chat) =>\n chat.chat_identifier === identifier ||\n chat.guid === identifier ||\n (chat.chat_identifier != null &&\n (chat.chat_identifier\n .replace(/[\\s\\-()]/g, \"\")\n .toLowerCase()\n .includes(normalized) ||\n normalized.includes(chat.chat_identifier.replace(/[\\s\\-()]/g, \"\").toLowerCase()))),\n );\n\n if (directMatches.length === 0) return [];\n\n const representative = this.pickMostRecentChat(directMatches);\n const isGroup =\n isGroupGuid(representative.guid) || isGroupChatIdentifier(representative.chat_identifier);\n if (isGroup) return [representative];\n\n const mergeKey = this.getConversationMergeKey(\n representative.chat_identifier,\n representative.guid,\n false,\n );\n\n return chats\n .filter(\n (chat) =>\n !(isGroupGuid(chat.guid) || isGroupChatIdentifier(chat.chat_identifier)) &&\n this.getConversationMergeKey(chat.chat_identifier, chat.guid, false) === mergeKey,\n )\n .map((chat) => toChatRow(chat));\n }\n\n /**\n * Completeness diagnostic for exports. Finds non-group chats that belong to\n * the same identity as the exported conversation but that the merge-key logic\n * did NOT fold in — e.g. a contact whose phone and email landed on separate\n * Address Book cards, or chats Apple linked via `person_centric_id`.\n *\n * Two independent signals, unioned and deduped by chat ROWID:\n * 1. **contactId invariant** (primary): any non-group chat whose handle\n * resolves (via the loaded Address Books, incl. iCloud sources) to the\n * same contactId as the merged set but wasn't merged. Catches merge\n * regressions and normalization gaps.\n * 2. **person_centric_id** (fallback): Apple's own cross-handle link. Still\n * fires when the contact isn't in any Address Book — but note this column\n * is NULL on many real chat.dbs, so signal (1) is the dependable one.\n *\n * Inherent limit: a contact in NO Address Book *and* with a NULL\n * person_centric_id cannot be linked from data alone — that case is guarded\n * by the cross-source merge tests, not this diagnostic. Read-only.\n */\n findUnmergedSiblingChats(chatIdentifier: string): UnmergedSiblingChat[] {\n const merged = this.resolveChatsForConversation(chatIdentifier);\n if (merged.length === 0) return [];\n\n const mergedIds = new Set(merged.map((c) => c.ROWID));\n const siblings = new Map<number, UnmergedSiblingChat>();\n\n const isGroupChat = (guid: string, ident: string | null): boolean =>\n isGroupGuid(guid) || isGroupChatIdentifier(ident ?? \"\");\n\n // ── Signal 1: same resolved contactId, not merged ──\n const mergedContactIds = new Set<number>();\n for (const chat of merged) {\n const contact = this.contacts.lookupContact(chat.chat_identifier);\n if (contact) mergedContactIds.add(contact.contactId);\n }\n if (mergedContactIds.size > 0) {\n for (const chat of this.getAllChatsWithLastDate()) {\n if (mergedIds.has(chat.ROWID)) continue;\n if (isGroupChat(chat.guid, chat.chat_identifier)) continue;\n const contact = this.contacts.lookupContact(chat.chat_identifier);\n if (contact && mergedContactIds.has(contact.contactId)) {\n siblings.set(chat.ROWID, {\n chatIdentifier: chat.chat_identifier,\n reason: \"resolves to the same contact but was not merged\",\n });\n }\n }\n }\n\n // ── Signal 2: shared person_centric_id, not merged ──\n const mergedIdList = [...mergedIds];\n const placeholders = mergedIdList.map(() => \"?\").join(\",\");\n const rows = this.raw\n .prepare(`\n SELECT DISTINCT c.ROWID as rowid, c.guid, c.chat_identifier\n FROM ${Tables.CHAT} c\n JOIN ${Tables.CHAT_HANDLE_JOIN} chj ON chj.chat_id = c.ROWID\n JOIN ${Tables.HANDLE} h ON h.ROWID = chj.handle_id\n WHERE c.ROWID NOT IN (${placeholders})\n AND h.person_centric_id IN (\n SELECT DISTINCT h2.person_centric_id\n FROM ${Tables.CHAT_HANDLE_JOIN} chj2\n JOIN ${Tables.HANDLE} h2 ON h2.ROWID = chj2.handle_id\n WHERE chj2.chat_id IN (${placeholders})\n AND h2.person_centric_id IS NOT NULL\n AND h2.person_centric_id != ''\n )\n `)\n .all(...mergedIdList, ...mergedIdList) as {\n rowid: number;\n guid: string;\n chat_identifier: string | null;\n }[];\n for (const row of rows) {\n if (isGroupChat(row.guid, row.chat_identifier)) continue;\n if (siblings.has(row.rowid)) continue;\n siblings.set(row.rowid, {\n chatIdentifier: row.chat_identifier ?? row.guid,\n reason: \"shares this contact's identity (person_centric_id) but was not merged\",\n });\n }\n\n return [...siblings.values()];\n }\n\n /**\n * Get all chats with their last message date in one join query.\n * Used to resolve the correct chat for a contact when multiple chats exist (e.g. same number).\n * Results are suitable for filtering by handle/identifier then sorting by last_date descending.\n */\n private getAllChatsWithLastDate(): ChatWithLastDate[] {\n const now = Date.now();\n if (this.cachedAllChats && now - this.cachedAllChats.ts < IMessageDB.CACHE_TTL_MS) {\n return this.cachedAllChats.data;\n }\n const stmt = this.raw.prepare(`\n SELECT\n c.ROWID as rowid,\n c.guid,\n c.chat_identifier,\n c.display_name,\n c.service_name,\n (SELECT MAX(m.date)\n FROM ${Tables.CHAT_MESSAGE_JOIN} cmj\n JOIN ${Tables.MESSAGE} m ON cmj.message_id = m.ROWID\n WHERE cmj.chat_id = c.ROWID\n AND m.associated_message_type = ${AssociatedMessageType.NORMAL}\n AND COALESCE(m.item_type, 0) = 0) as last_date\n FROM ${Tables.CHAT} c\n `);\n const rows = stmt.all() as {\n rowid: number;\n guid: string;\n chat_identifier: string;\n display_name: string | null;\n service_name: string | null;\n last_date: number | null;\n }[];\n const data = rows.map((r) => ({\n ROWID: r.rowid,\n guid: r.guid,\n chat_identifier: r.chat_identifier,\n display_name: r.display_name,\n service_name: r.service_name,\n last_date: r.last_date,\n }));\n this.cachedAllChats = { data, ts: now };\n return data;\n }\n\n /** Last message metadata per chat (window function over message table). Cached with TTL. */\n private getLastMessageByChat(): Record<number, LastConversationRow> {\n const now = Date.now();\n if (this.cachedLastByChat && now - this.cachedLastByChat.ts < IMessageDB.CACHE_TTL_MS) {\n return this.cachedLastByChat.data;\n }\n const stmt = this.raw.prepare(`\n SELECT chat_id, last_date, last_message_id, last_service, last_is_from_me, balloon_bundle_id, snippet, chat_properties FROM (\n SELECT cmj.chat_id, m.date as last_date, m.ROWID as last_message_id,\n m.service as last_service,\n m.is_from_me as last_is_from_me,\n m.balloon_bundle_id as balloon_bundle_id,\n COALESCE(TRIM(SUBSTR(m.text, 1, 200)), '') as snippet,\n c.properties as chat_properties,\n ROW_NUMBER() OVER (PARTITION BY cmj.chat_id ORDER BY m.date DESC) as rn\n FROM ${Tables.MESSAGE} m\n JOIN ${Tables.CHAT_MESSAGE_JOIN} cmj ON m.ROWID = cmj.message_id\n JOIN ${Tables.CHAT} c ON c.ROWID = cmj.chat_id\n WHERE m.associated_message_type = ${AssociatedMessageType.NORMAL}\n AND COALESCE(m.item_type, 0) = 0\n ) WHERE rn = 1\n `);\n const data = (\n stmt.all() as {\n chat_id: number;\n last_date: number;\n last_message_id: number;\n last_service: string | null;\n last_is_from_me: number;\n balloon_bundle_id: string | null;\n snippet: string;\n chat_properties: Buffer | null;\n }[]\n ).reduce(\n (acc, row) => {\n acc[row.chat_id] = {\n chatId: row.chat_id,\n lastDate: row.last_date,\n lastMessageId: row.last_message_id,\n lastService: row.last_service,\n lastIsFromMe: Boolean(row.last_is_from_me),\n balloonBundleId: row.balloon_bundle_id,\n snippet: row.snippet || null,\n chatProperties: row.chat_properties,\n };\n return acc;\n },\n {} as Record<number, LastConversationRow>,\n );\n this.cachedLastByChat = { data, ts: now };\n return data;\n }\n\n /** Unread count per chat. Cached with TTL. */\n private getUnreadByChat(): Record<number, number> {\n const now = Date.now();\n if (this.cachedUnreadByChat && now - this.cachedUnreadByChat.ts < IMessageDB.CACHE_TTL_MS) {\n return this.cachedUnreadByChat.data;\n }\n const stmt = this.raw.prepare(`\n SELECT cmj.chat_id, COUNT(*) as unread\n FROM ${Tables.MESSAGE} m\n JOIN ${Tables.CHAT_MESSAGE_JOIN} cmj ON m.ROWID = cmj.message_id\n WHERE m.associated_message_type = ${AssociatedMessageType.NORMAL}\n AND COALESCE(m.item_type, 0) = 0\n AND m.is_from_me = 0 AND m.is_read = 0\n GROUP BY cmj.chat_id\n `);\n const data = (stmt.all() as { chat_id: number; unread: number }[]).reduce(\n (acc, row) => {\n acc[row.chat_id] = row.unread;\n return acc;\n },\n {} as Record<number, number>,\n );\n this.cachedUnreadByChat = { data, ts: now };\n return data;\n }\n\n /** Get all chats (without last_date subquery -- lighter for listing). */\n private getAllChats(): ChatRow[] {\n const rows = this.raw\n .prepare(\n `SELECT ROWID, guid, chat_identifier, display_name FROM ${Tables.CHAT} ORDER BY ROWID DESC`,\n )\n .all() as ChatRow[];\n return rows;\n }\n\n /** Resolve a message ROWID to its date (for composite pagination cursors). */\n private dateOfMessage(rowid: number): number | undefined {\n const row = this.raw.prepare(`SELECT date FROM ${Tables.MESSAGE} WHERE ROWID = ?`).get(rowid) as\n | { date: number }\n | undefined;\n return row?.date;\n }\n\n /**\n * Fetch message rows for a chat ROWID, ordered by date DESC.\n *\n * Pagination boundaries are composite `(date, ROWID)` cursors, NOT bare\n * ROWID comparisons: results are ordered by date, and in restored/merged\n * threads ROWID order diverges from date order — a ROWID-only bound both\n * skips older-date/higher-ROWID messages and re-shows newer-date/lower-ROWID\n * ones (paginating a real thread that way reached only ~47% of it; the same\n * defect made wait_for_reply able to miss replies entirely). ROWID-only is\n * kept solely as a fallback when the boundary message's date is unknown\n * (e.g. the boundary row was deleted).\n */\n private fetchMessagesForChatRowId(\n chatRowId: number,\n limit: number,\n beforeMessageId?: number,\n afterMessageId?: number,\n beforeDate?: number,\n afterDate?: number,\n ): MessageRow[] {\n const conditions: string[] = [\"cmj.chat_id = ?\"];\n const params: unknown[] = [chatRowId];\n\n if (beforeMessageId != null) {\n if (beforeDate != null) {\n conditions.push(\"(m.date < ? OR (m.date = ? AND m.ROWID < ?))\");\n params.push(beforeDate, beforeDate, beforeMessageId);\n } else {\n conditions.push(\"m.ROWID < ?\");\n params.push(beforeMessageId);\n }\n }\n if (afterMessageId != null) {\n if (afterDate != null) {\n conditions.push(\"(m.date > ? OR (m.date = ? AND m.ROWID > ?))\");\n params.push(afterDate, afterDate, afterMessageId);\n } else {\n conditions.push(\"m.ROWID > ?\");\n params.push(afterMessageId);\n }\n }\n params.push(limit);\n\n const stmt = this.raw.prepare(`\n SELECT\n m.ROWID, m.guid, m.text, m.attributedBody, m.date,\n m.is_from_me, h.id as handle_id, m.cache_has_attachments\n FROM ${Tables.MESSAGE} m\n LEFT JOIN ${Tables.HANDLE} h ON m.handle_id = h.ROWID\n LEFT JOIN ${Tables.CHAT_MESSAGE_JOIN} cmj ON m.ROWID = cmj.message_id\n WHERE ${conditions.join(\" AND \")}\n ORDER BY m.date DESC, m.ROWID DESC\n LIMIT ?\n `);\n return stmt.all(...params) as MessageRow[];\n }\n\n /**\n * Extract readable text from a message row.\n * Prefers the plain text column; falls back to parsing the attributedBody blob.\n * Replaces the upstream IMessageDatabase.parseMessage().\n */\n private parseMessageText(row: MessageRow): string | null {\n if (row.text && !isPlaceholderText(row.text)) return row.text;\n return extractAttributedBodyText(row.attributedBody) || null;\n }\n\n /**\n * From a list of chats with last_date, return the one with the most recent message (sort by last_date desc).\n */\n private pickMostRecentChat(chats: ChatWithLastDate[]): ChatRow {\n if (chats.length === 0) throw new Error(\"pickMostRecentChat requires at least one chat\");\n if (chats.length === 1) return toChatRow(chats[0]);\n const sorted = chats.slice().sort((a, b) => {\n const aDate = a.last_date ?? 0;\n const bDate = b.last_date ?? 0;\n return bDate - aDate;\n });\n return toChatRow(sorted[0]);\n }\n\n /**\n * Helper to find chat by identifier.\n * When multiple chats match (e.g. same number in different services), returns the one with the most recent message.\n */\n private findChatByIdentifier(identifier: string): ChatRow | null {\n const chats = this.getAllChatsWithLastDate();\n const matches = chats.filter(\n (c) =>\n c.chat_identifier === identifier ||\n c.guid === identifier ||\n (c.chat_identifier != null &&\n (c.chat_identifier.includes(identifier) || identifier.includes(c.chat_identifier))),\n );\n if (matches.length === 0) return null;\n return this.pickMostRecentChat(matches);\n }\n\n /**\n * Convert a raw message row to our Message type with full extended data\n */\n private convertMessage(\n raw: MessageRow,\n text: string | null,\n chatId: string,\n extended?: ExtendedMessageData,\n includeReactions: boolean = false,\n ): Message {\n // If no extended data provided, fetch it\n const ext = extended || this.fetchExtendedMessageData(raw.ROWID);\n\n // Clean up text - handle object replacement characters and other special chars\n let cleanText = text || raw.text || null;\n if (isPlaceholderText(cleanText)) {\n const attributedText = extractAttributedBodyText(raw.attributedBody ?? null) || null;\n if (attributedText) {\n cleanText = attributedText;\n }\n }\n if (cleanText) {\n // U+FFFC is Object Replacement Character (inline attachment placeholder); see db-schema\n // U+FFFD is Replacement Character (invalid UTF-8)\n const re = new RegExp(\n `${OBJECT_REPLACEMENT_CHAR.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\")}+`,\n \"g\",\n );\n cleanText = cleanText\n .replace(re, \"📎 \") // Use paperclip emoji for inline attachments\n .replace(/\\uFFFD/g, \"\")\n .trim();\n\n // If only attachment markers remain, indicate it's an attachment-only message\n if (!cleanText || cleanText === \"📎\" || /^(📎\\s*)+$/.test(cleanText)) {\n cleanText = \"(image/attachment)\";\n }\n\n if (ext.balloon_bundle_id?.includes(\"URLBalloonProvider\")) {\n cleanText = normalizeRichMetadataText(cleanText) ?? cleanText;\n }\n }\n\n // Determine if this is a reaction (see db-schema.ts and docs)\n const associatedType = ext.associated_message_type ?? AssociatedMessageType.NORMAL;\n const isReaction = isReactionType(associatedType);\n\n // Parse reaction info\n let reaction: Reaction | undefined;\n if (isReaction && ext.associated_message_guid) {\n const typeInfo = TAPBACK_TYPE_MAP[associatedType] || {\n type: \"unknown\" as TapbackType,\n isRemoval: false,\n };\n const parsed = parseAssociatedMessageGuid(ext.associated_message_guid);\n\n reaction = {\n type: typeInfo.type,\n emoji: ext.associated_message_emoji || undefined,\n fromHandle: raw.is_from_me ? \"me\" : ext.handle_id || \"unknown\",\n isRemoval: typeInfo.isRemoval,\n targetMessageGuid: parsed?.targetGuid || \"\",\n targetMessagePart: parsed?.partIndex || 0,\n };\n }\n\n // Determine if this is a reply\n const isReply = Boolean(ext.thread_originator_guid);\n let replyTo: ReplyContext | undefined;\n if (isReply && ext.thread_originator_guid) {\n const originalText = this.getMessageTextByGuid(ext.thread_originator_guid);\n replyTo = {\n replyToGuid: ext.thread_originator_guid,\n replyToText: originalText,\n };\n }\n\n // Get rich content type\n const richContentType = getRichContentType(ext.balloon_bundle_id || null);\n\n // Get attachments if needed\n const hasAttachments = Boolean(ext.cache_has_attachments);\n let attachments: Attachment[] | undefined;\n if (hasAttachments) {\n attachments = this.fetchAttachments(raw.ROWID);\n }\n\n // Get reactions for this message if requested\n let reactions: Reaction[] | undefined;\n if (includeReactions && !isReaction) {\n reactions = this.fetchReactionsForMessage(raw.guid);\n // Filter out removal reactions that cancel out adds\n if (reactions.length > 0) {\n reactions = this.consolidateReactions(reactions);\n }\n if (reactions.length === 0) reactions = undefined;\n }\n\n // Resolve display name from contacts\n const rawHandle = raw.is_from_me ? \"me\" : ext.handle_id || \"unknown\";\n const displayName = rawHandle === \"me\" ? undefined : this.contacts.lookupHandle(rawHandle);\n\n // Parse rich content summary if available.\n //\n // `message_summary_info` is set on many plain-text messages too\n // (delivery receipts / typing-indicator metadata, depending on iOS\n // version). The canonical \"this message renders as a rich balloon\"\n // marker is `balloon_bundle_id` — only fall back to parsing the\n // summary blob when that is set, otherwise we paint a misleading\n // \"[Rich Content]\" badge on plain text like \"hello world\".\n let richContentSummary: string | undefined;\n if (ext.balloon_bundle_id && ext.message_summary_info) {\n richContentSummary = this.parseRichContentSummary(ext.message_summary_info);\n }\n\n return {\n id: raw.ROWID,\n guid: raw.guid,\n text: cleanText,\n handle: rawHandle,\n displayName: displayName !== rawHandle ? displayName : undefined,\n isFromMe: Boolean(raw.is_from_me),\n date: macTimestampToDate(raw.date) || new Date(0),\n dateRead: macTimestampToDate(ext.date_read ?? null),\n dateDelivered: macTimestampToDate(ext.date_delivered ?? null),\n isRead: ext.is_read != null ? Boolean(ext.is_read) : true,\n isDelivered: ext.is_delivered != null ? Boolean(ext.is_delivered) : true,\n chatId: chatId,\n service: this.detectServiceForMessage(ext) as \"iMessage\" | \"SMS\",\n isReaction,\n reaction,\n isReply,\n replyTo,\n reactions,\n richContentType,\n richContentSummary,\n isEdited: Boolean(ext.date_edited && ext.date_edited > 0),\n isRetracted: Boolean(ext.date_retracted && ext.date_retracted > 0),\n hasAttachments,\n attachments,\n };\n }\n\n /**\n * Consolidate reactions by removing reactions that are canceled by removal messages\n */\n private consolidateReactions(reactions: Reaction[]): Reaction[] {\n const reactionMap = new Map<string, Reaction>();\n\n for (const r of reactions) {\n const key = `${r.fromHandle}-${r.type}-${r.targetMessagePart}`;\n if (r.isRemoval) {\n reactionMap.delete(key);\n } else {\n reactionMap.set(key, r);\n }\n }\n\n return Array.from(reactionMap.values());\n }\n\n /**\n * Detect service from extended message data (handle.service column).\n */\n private detectServiceForMessage(ext: ExtendedMessageData): \"iMessage\" | \"SMS\" {\n if (ext.handle_service) {\n return ext.handle_service.toLowerCase().includes(\"sms\") ? \"SMS\" : \"iMessage\";\n }\n return \"iMessage\";\n }\n\n /**\n * Schedule a non-blocking background refresh of caches.\n * Call after responding to an MCP tool call or TUI refresh.\n */\n scheduleBackgroundRefresh(): void {\n if (this.backgroundRefreshScheduled) return;\n this.backgroundRefreshScheduled = true;\n setImmediate(() => {\n this.backgroundRefreshScheduled = false;\n try {\n // Invalidate caches so the next request gets fresh data\n this.cachedAllChats = null;\n this.cachedLastByChat = null;\n this.cachedUnreadByChat = null;\n this.cachedReactionsByChat.clear();\n // Pre-warm the cache\n this.getAllChatsWithLastDate();\n // Sync any new slugs\n this.scheduleBackgroundSlugSync();\n } catch {\n // Refresh is best-effort; foreground requests will surface real DB errors.\n }\n });\n }\n\n /** Resolve participant handles to display names via contacts DB. */\n resolveParticipantNames(handles: string[]): string[] {\n return handles.map((h) => this.contacts.lookupHandle(h));\n }\n\n /**\n * Close database connections\n */\n async close(): Promise<void> {\n this.closed = true;\n this.raw.close();\n this.contacts.close();\n this.slugStore.close();\n }\n\n /**\n * Fetch extended message data using raw DB\n * Includes: is_read, date_read, handle_id, reaction data, reply data, edit status, attachments\n */\n private fetchExtendedMessageData(rowid: number): ExtendedMessageData {\n const stmt = this.raw.prepare(`\n SELECT \n m.is_read,\n m.date_read,\n m.is_delivered,\n m.date_delivered,\n h.id as handle_id,\n h.service as handle_service,\n m.associated_message_type,\n m.associated_message_guid,\n m.associated_message_emoji,\n m.thread_originator_guid,\n m.thread_originator_part,\n m.balloon_bundle_id,\n m.item_type,\n m.date_edited,\n m.date_retracted,\n m.cache_has_attachments,\n m.message_summary_info,\n m.payload_data\n FROM ${Tables.MESSAGE} m\n LEFT JOIN ${Tables.HANDLE} h ON m.handle_id = h.ROWID\n WHERE m.ROWID = ?\n LIMIT 1\n `);\n return (stmt.get(rowid) as ExtendedMessageData) || {};\n }\n\n /**\n * Batch-fetch extended message data for multiple ROWIDs in a single query.\n * Falls back to individual fetches for very large batches (SQLite bind param limit).\n */\n private fetchExtendedMessageDataBatch(rowids: number[]): Map<number, ExtendedMessageData> {\n const result = new Map<number, ExtendedMessageData>();\n if (rowids.length === 0) return result;\n\n const CHUNK = 500;\n for (let i = 0; i < rowids.length; i += CHUNK) {\n const chunk = rowids.slice(i, i + CHUNK);\n const placeholders = chunk.map(() => \"?\").join(\",\");\n const stmt = this.raw.prepare(`\n SELECT\n m.ROWID as _rowid,\n m.is_read, m.date_read, m.is_delivered, m.date_delivered,\n h.id as handle_id, h.service as handle_service,\n m.associated_message_type, m.associated_message_guid,\n m.associated_message_emoji, m.thread_originator_guid,\n m.thread_originator_part, m.balloon_bundle_id,\n m.item_type, m.date_edited, m.date_retracted,\n m.cache_has_attachments, m.message_summary_info, m.payload_data\n FROM ${Tables.MESSAGE} m\n LEFT JOIN ${Tables.HANDLE} h ON m.handle_id = h.ROWID\n WHERE m.ROWID IN (${placeholders})\n `);\n const rows = stmt.all(...chunk) as (ExtendedMessageData & { _rowid: number })[];\n for (const row of rows) {\n result.set(row._rowid, row);\n }\n }\n return result;\n }\n\n /** Max ROWID currently in the message table — used as a cache key. */\n getMaxMessageRowId(): number {\n const row = this.raw\n .prepare(`SELECT COALESCE(MAX(ROWID), 0) AS m FROM ${Tables.MESSAGE}`)\n .get() as { m: number };\n return Number(row.m) || 0;\n }\n\n /**\n * Fetch every message newer than `cutoffMs` across all chats. Used by the\n * chat_analytics tool — bounded by date, not by per-chat limit. Reactions\n * are included (tapback analytics need them); hidden system items dropped.\n */\n async getMessagesInWindow(cutoffMs: number, capPerWindow = 200_000): Promise<Message[]> {\n const span = perf(\"getMessagesInWindow\");\n const cutoffNanos = Math.floor((cutoffMs / 1000 - MAC_EPOCH_OFFSET) * NANOS_PER_SECOND);\n const sql = `\n SELECT m.ROWID, m.guid, m.text, m.attributedBody, m.date, m.date_read, m.date_delivered,\n m.is_read, m.is_delivered, m.is_from_me, h.id as handle_id,\n h.service as handle_service,\n m.cache_has_attachments, m.associated_message_type,\n m.associated_message_guid, m.associated_message_emoji,\n m.thread_originator_guid, m.thread_originator_part,\n m.balloon_bundle_id, m.message_summary_info,\n m.date_edited, m.date_retracted,\n m.item_type, c.chat_identifier\n FROM ${Tables.MESSAGE} m\n LEFT JOIN ${Tables.HANDLE} h ON m.handle_id = h.ROWID\n LEFT JOIN ${Tables.CHAT_MESSAGE_JOIN} cmj ON m.ROWID = cmj.message_id\n LEFT JOIN ${Tables.CHAT} c ON cmj.chat_id = c.ROWID\n WHERE m.date >= ?\n ORDER BY m.date ASC\n LIMIT ?\n `;\n const rows = this.raw.prepare(sql).all(cutoffNanos, capPerWindow) as any[];\n const out: Message[] = [];\n for (const r of rows) {\n if (isHiddenSystemItem(r.item_type)) continue;\n const text = this.parseMessageText(r);\n // IMPORTANT: ext must carry every field convertMessage uses to\n // classify the row (reaction / edit / reply / service / etc.) —\n // without these, analytics like tapback_summary see every message\n // as a normal text and return 0 reactions.\n const ext: ExtendedMessageData = {\n is_read: r.is_read,\n date_read: r.date_read,\n is_delivered: r.is_delivered,\n date_delivered: r.date_delivered,\n handle_id: r.handle_id,\n handle_service: r.handle_service,\n associated_message_type: r.associated_message_type,\n associated_message_guid: r.associated_message_guid,\n associated_message_emoji: r.associated_message_emoji,\n thread_originator_guid: r.thread_originator_guid,\n thread_originator_part: r.thread_originator_part,\n balloon_bundle_id: r.balloon_bundle_id,\n message_summary_info: r.message_summary_info,\n date_edited: r.date_edited,\n date_retracted: r.date_retracted,\n cache_has_attachments: r.cache_has_attachments,\n item_type: r.item_type,\n };\n const msg = this.convertMessage(r, text, r.chat_identifier || \"\", ext);\n out.push(msg);\n }\n span.end({ cutoffMs, returned: out.length });\n return out;\n }\n\n /**\n * Search attachments by MIME prefix, date window, and/or chat identifier.\n * Excludes stickers (is_sticker=1) and Apple plugin-payload UTIs which\n * aren't meaningful as user-facing attachments.\n *\n * Returns metadata only — use getAttachmentByRowId for the file bytes.\n */\n searchAttachments(opts: {\n mimePrefix?: string;\n chatIdentifier?: string;\n sinceMs?: number;\n untilMs?: number;\n limit: number;\n }): Array<{\n rowId: number;\n filename: string;\n mimeType: string | null;\n transferName: string | null;\n totalBytes: number;\n createdDate: Date;\n chatId: string;\n }> {\n const span = perf(\"searchAttachments\");\n const conds: string[] = [\n \"a.is_sticker = 0\",\n \"(a.uti IS NULL OR a.uti NOT LIKE 'com.apple.messages.plugin%')\",\n ];\n const params: any[] = [];\n if (opts.mimePrefix) {\n conds.push(\"a.mime_type LIKE ?\");\n params.push(`${opts.mimePrefix}%`);\n }\n if (opts.chatIdentifier) {\n conds.push(\"c.chat_identifier = ?\");\n params.push(opts.chatIdentifier);\n }\n const toMacNanos = (ms: number): number =>\n Math.floor((ms / 1000 - MAC_EPOCH_OFFSET) * NANOS_PER_SECOND);\n if (opts.sinceMs !== undefined) {\n conds.push(\"a.created_date >= ?\");\n params.push(toMacNanos(opts.sinceMs));\n }\n if (opts.untilMs !== undefined) {\n conds.push(\"a.created_date <= ?\");\n params.push(toMacNanos(opts.untilMs));\n }\n const lim = opts.limit > 0 ? opts.limit : 1000;\n params.push(lim);\n\n const sql = `\n SELECT\n a.ROWID as rowId,\n a.filename,\n a.mime_type,\n a.transfer_name,\n a.total_bytes,\n a.created_date,\n c.chat_identifier\n FROM ${Tables.ATTACHMENT} a\n JOIN ${Tables.MESSAGE_ATTACHMENT_JOIN} maj ON a.ROWID = maj.attachment_id\n JOIN ${Tables.MESSAGE} m ON maj.message_id = m.ROWID\n JOIN ${Tables.CHAT_MESSAGE_JOIN} cmj ON m.ROWID = cmj.message_id\n JOIN ${Tables.CHAT} c ON cmj.chat_id = c.ROWID\n WHERE ${conds.join(\" AND \")}\n ORDER BY a.created_date DESC\n LIMIT ?\n `;\n const rows = this.raw.prepare(sql).all(...params) as any[];\n const out = rows.map((r) => ({\n rowId: Number(r.rowId),\n filename: r.filename || \"\",\n mimeType: r.mime_type ?? null,\n transferName: r.transfer_name ?? null,\n totalBytes: Number(r.total_bytes) || 0,\n // `attachment.created_date` stores seconds since 2001-01-01,\n // not nanoseconds. The auto-detecting converter handles both\n // legacy seconds-formatted rows and any modern ns-formatted rows\n // that may show up in a heterogeneous DB.\n createdDate: macAutoTimestampToDate(Number(r.created_date)) ?? new Date(0),\n chatId: r.chat_identifier || \"\",\n }));\n span.end({ count: out.length });\n return out;\n }\n\n /** Fetch a single attachment record by ROWID. */\n getAttachmentByRowId(rowId: number): {\n rowId: number;\n filename: string;\n mimeType: string | null;\n transferName: string | null;\n totalBytes: number;\n } | null {\n const row = this.raw\n .prepare(\n `SELECT ROWID as rowId, filename, mime_type, transfer_name, total_bytes\n FROM ${Tables.ATTACHMENT}\n WHERE ROWID = ?`,\n )\n .get(rowId) as any;\n if (!row) return null;\n return {\n rowId: Number(row.rowId),\n filename: row.filename || \"\",\n mimeType: row.mime_type ?? null,\n transferName: row.transfer_name ?? null,\n totalBytes: Number(row.total_bytes) || 0,\n };\n }\n\n /**\n * Fetch attachments for a message\n */\n private fetchAttachments(messageRowId: number): Attachment[] {\n const stmt = this.raw.prepare(`\n SELECT \n a.filename,\n a.mime_type,\n a.transfer_name,\n a.total_bytes\n FROM ${Tables.ATTACHMENT} a\n JOIN ${Tables.MESSAGE_ATTACHMENT_JOIN} maj ON a.ROWID = maj.attachment_id\n WHERE maj.message_id = ?\n `);\n const rows = stmt.all(messageRowId) as any[];\n return rows.map((r) => ({\n filename: r.filename || \"\",\n mimeType: r.mime_type,\n transferName: r.transfer_name,\n totalBytes: r.total_bytes || 0,\n }));\n }\n\n /**\n * Batch-fetch all reactions in a chat, grouped by target message GUID.\n * One query replaces N individual LIKE queries (the main perf bottleneck).\n */\n private fetchReactionsForChat(chatRowId: number): Map<string, Reaction[]> {\n const cached = this.cachedReactionsByChat.get(chatRowId);\n if (cached && Date.now() - cached.ts < IMessageDB.CACHE_TTL_MS) {\n return cached.data;\n }\n const stmt = this.raw.prepare(`\n SELECT\n m.associated_message_type,\n m.associated_message_guid,\n m.associated_message_emoji,\n h.id as handle_id,\n m.is_from_me\n FROM ${Tables.MESSAGE} m\n LEFT JOIN ${Tables.HANDLE} h ON m.handle_id = h.ROWID\n JOIN ${Tables.CHAT_MESSAGE_JOIN} cmj ON m.ROWID = cmj.message_id\n WHERE cmj.chat_id = ?\n AND m.associated_message_type >= 2000\n `);\n\n const rows = stmt.all(chatRowId) as any[];\n const result = new Map<string, Reaction[]>();\n\n for (const r of rows) {\n const typeInfo = TAPBACK_TYPE_MAP[r.associated_message_type] || {\n type: \"unknown\" as TapbackType,\n isRemoval: false,\n };\n const parsed = parseAssociatedMessageGuid(r.associated_message_guid);\n const targetGuid = parsed?.targetGuid;\n if (!targetGuid) continue;\n\n const reaction: Reaction = {\n type: typeInfo.type,\n emoji: r.associated_message_emoji || undefined,\n fromHandle: r.is_from_me ? \"me\" : r.handle_id || \"unknown\",\n isRemoval: typeInfo.isRemoval,\n targetMessageGuid: targetGuid,\n targetMessagePart: parsed?.partIndex || 0,\n };\n\n const existing = result.get(targetGuid);\n if (existing) existing.push(reaction);\n else result.set(targetGuid, [reaction]);\n }\n\n this.cachedReactionsByChat.set(chatRowId, { data: result, ts: Date.now() });\n return result;\n }\n\n /**\n * Fetch reactions for a specific message GUID\n */\n private fetchReactionsForMessage(messageGuid: string): Reaction[] {\n const stmt = this.raw.prepare(`\n SELECT \n m.associated_message_type,\n m.associated_message_guid,\n m.associated_message_emoji,\n h.id as handle_id,\n m.is_from_me\n FROM ${Tables.MESSAGE} m\n LEFT JOIN ${Tables.HANDLE} h ON m.handle_id = h.ROWID\n WHERE m.associated_message_guid LIKE ?\n AND m.associated_message_type >= 2000\n `);\n\n const rows = stmt.all(`%/${messageGuid}`) as any[];\n\n return rows.map((r) => {\n const typeInfo = TAPBACK_TYPE_MAP[r.associated_message_type] || {\n type: \"unknown\" as TapbackType,\n isRemoval: false,\n };\n const parsed = parseAssociatedMessageGuid(r.associated_message_guid);\n\n return {\n type: typeInfo.type,\n emoji: r.associated_message_emoji || undefined,\n fromHandle: r.is_from_me ? \"me\" : r.handle_id || \"unknown\",\n isRemoval: typeInfo.isRemoval,\n targetMessageGuid: parsed?.targetGuid || messageGuid,\n targetMessagePart: parsed?.partIndex || 0,\n };\n });\n }\n\n /**\n * Parse rich content summary from message_summary_info BLOB\n * This contains metadata about link previews, rich messages, etc.\n */\n private parseRichContentSummary(blob: Buffer | null): string | undefined {\n if (!blob) return undefined;\n\n try {\n // message_summary_info is a binary plist - try to extract text\n const str = blob.toString(\"utf8\");\n\n // Look for common patterns in the summary\n // Link URLs\n const urlMatch = str.match(/https?:\\/\\/\\S+/);\n if (urlMatch) {\n return `Link: ${urlMatch[0]}`;\n }\n\n // Title text (often in plists as <string>Title</string>)\n const titleMatch = str.match(/<string>([^<]+)<\\/string>/);\n if (titleMatch) {\n return titleMatch[1];\n }\n\n // For now, just indicate rich content exists\n return \"[Rich Content]\";\n } catch {\n return undefined;\n }\n }\n\n /**\n * Look up the text of a message by GUID (for reply context)\n * Falls back to parsing attributedBody if text is null\n */\n private getMessageTextByRowId(rowId: number): string | null {\n const row = this.raw\n .prepare(`SELECT ROWID, text, attributedBody FROM ${Tables.MESSAGE} WHERE ROWID = ? LIMIT 1`)\n .get(rowId) as\n | { ROWID: number; text: string | null; attributedBody: Buffer | null }\n | undefined;\n return this.extractMessageText(row);\n }\n\n private getMessageTextByGuid(guid: string): string | null {\n const row = this.raw\n .prepare(`SELECT ROWID, text, attributedBody FROM ${Tables.MESSAGE} WHERE guid = ? LIMIT 1`)\n .get(guid) as\n | { ROWID: number; text: string | null; attributedBody: Buffer | null }\n | undefined;\n return this.extractMessageText(row);\n }\n\n private extractMessageText(\n row: { ROWID: number; text: string | null; attributedBody: Buffer | null } | undefined,\n ): string | null {\n if (!row) return null;\n if (row.text && !isPlaceholderText(row.text)) return row.text;\n return extractAttributedBodyText(row.attributedBody) || null;\n }\n}\n\n/**\n * Extended message data from raw database query\n */\ninterface ExtendedMessageData {\n is_read?: number;\n date_read?: number;\n is_delivered?: number;\n date_delivered?: number;\n handle_id?: string | null;\n handle_service?: string | null;\n associated_message_type?: number;\n associated_message_guid?: string | null;\n associated_message_emoji?: string | null;\n thread_originator_guid?: string | null;\n thread_originator_part?: string | null;\n balloon_bundle_id?: string | null;\n item_type?: number;\n date_edited?: number;\n date_retracted?: number;\n cache_has_attachments?: number;\n message_summary_info?: Buffer | null;\n payload_data?: Buffer | null;\n}\n"],"names":["__dirname","require","schemaParseAssociatedMessageGuid","schemaMacTimestampToDate"],"mappings":";;;;;;;;AAsCA,SAAS,qBAAqB,OAAuB;AACnD,QAAM,SAAS,MAAM,QAAQ,OAAO,EAAE;AAEtC,MAAI,OAAO,WAAW,MAAM,OAAO,WAAW,GAAG,GAAG;AAClD,WAAO,OAAO,MAAM,CAAC;AAAA,EACvB;AACA,SAAO;AACT;AASO,SAAS,wBAAwB,OAAyB;AAC/D,QAAM,aAAa,qBAAqB,KAAK;AAC7C,QAAM,WAAW,oBAAI,IAAY,CAAC,UAAU,CAAC;AAG7C,MAAI,WAAW,WAAW,MAAM,WAAW,WAAW,IAAI,GAAG;AAC3D,UAAM,cAAc,WAAW,MAAM,CAAC;AACtC,aAAS,IAAI,WAAW;AACxB,QAAI,YAAY,WAAW,KAAK,YAAY,WAAW,GAAG,GAAG;AAC3D,eAAS,IAAI,IAAI,WAAW,EAAE;AAAA,IAChC;AAAA,EACF;AAGA,MAAI,WAAW,WAAW,MAAM,WAAW,WAAW,IAAI,GAAG;AAC3D,UAAM,eAAe,WAAW,MAAM,CAAC;AACvC,aAAS,IAAI,YAAY;AACzB,aAAS,IAAI,KAAK,YAAY,EAAE;AAAA,EAClC;AAGA,MAAI,WAAW,WAAW,IAAI;AAC5B,aAAS,IAAI,IAAI,UAAU,EAAE;AAAA,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AAa,OAAO,EAAE,aAAa;AAAA,QACjD,WAAW,SAAS;AAAA,QACpB,mBAAmB,QAAQ,cAAc;AAAA,QACzC,mBAAmB,QAAQ,aAAa;AAAA,MAAA;AAAA,IAE5C,CAAC;AAAA,EACH;AAAA;AAAA;AAAA;AAAA;AAAA,EAMQ,wBAAwB,MAAyC;AACvE,QAAI,CAAC,KAAM,QAAO;AAElB,QAAI;AAEF,YAAM,MAAM,KAAK,SAAS,MAAM;AAIhC,YAAM,WAAW,IAAI,MAAM,gBAAgB;AAC3C,UAAI,UAAU;AACZ,eAAO,SAAS,SAAS,CAAC,CAAC;AAAA,MAC7B;AAGA,YAAM,aAAa,IAAI,MAAM,2BAA2B;AACxD,UAAI,YAAY;AACd,eAAO,WAAW,CAAC;AAAA,MACrB;AAGA,aAAO;AAAA,IACT,QAAQ;AACN,aAAO;AAAA,IACT;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA,EAMQ,sBAAsB,OAA8B;AAC1D,UAAM,MAAM,KAAK,IACd,QAAQ,2CAA2C,OAAO,OAAO,0BAA0B,EAC3F,IAAI,KAAK;AAGZ,WAAO,KAAK,mBAAmB,GAAG;AAAA,EACpC;AAAA,EAEQ,qBAAqB,MAA6B;AACxD,UAAM,MAAM,KAAK,IACd,QAAQ,2CAA2C,OAAO,OAAO,yBAAyB,EAC1F,IAAI,IAAI;AAGX,WAAO,KAAK,mBAAmB,GAAG;AAAA,EACpC;AAAA,EAEQ,mBACN,KACe;AACf,QAAI,CAAC,IAAK,QAAO;AACjB,QAAI,IAAI,QAAQ,CAAC,kBAAkB,IAAI,IAAI,UAAU,IAAI;AACzD,WAAO,0BAA0B,IAAI,cAAc,KAAK;AAAA,EAC1D;AACF;;;;;"}
package/dist/index.js CHANGED
@@ -6,14 +6,14 @@ import { promisify } from "node:util";
6
6
  import { Server } from "@modelcontextprotocol/sdk/server/index.js";
7
7
  import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
8
8
  import { ListToolsRequestSchema, CallToolRequestSchema, ListResourcesRequestSchema, ListResourceTemplatesRequestSchema, ReadResourceRequestSchema } from "@modelcontextprotocol/sdk/types.js";
9
- import { c as checkLocalAccess, f as formatAccessReport, s as shutdown, g as getImsgDbPath, a as getContactsDbPaths, b as getSlugsDbPath, d as appendLog, e as getFileLogLines, h as getLogDirectory, p as perf, i as checkMessagesAvailable, j as sendToChat, k as sendToChatId, l as sendMessageReliable, m as sendMessageAlt, n as sendAttachment, o as checkImessageAvailability, q as installShutdownHandlers, r as registerCleanup, t as enableStdinEofDetection, u as enableOrphanWatchdog, v as logStartup, w as info, x as startHeapMonitor, y as getLogs, z as setLastSendError, A as logShutdown, B as stopHeapMonitor, C as getLogFilePath, D as getLastSendError } from "./shutdown-CikCSZru.js";
10
- import { m as minMessageId, r as resolveRecipient, d as defaultCountryFromEnv, a as rankFuzzy, b as dispatchAnalytic, i as installWatchdog, n as noteActivity, c as readWatchdogState } from "./watchdog-CnbBZnfg.js";
9
+ import { c as checkLocalAccess, f as formatAccessReport, s as shutdown, g as getImsgDbPath, a as getContactsDbPaths, b as getSlugsDbPath, d as appendLog, e as getFileLogLines, h as getLogDirectory, p as perf, i as checkMessagesAvailable, j as sendToChat, k as sendToChatId, l as sendMessageReliable, m as sendMessageAlt, n as sendAttachment, o as checkImessageAvailability, q as installShutdownHandlers, r as registerCleanup, t as enableStdinEofDetection, u as enableOrphanWatchdog, v as logStartup, w as info, x as startHeapMonitor, y as getLogs, z as setLastSendError, A as logShutdown, B as stopHeapMonitor, C as getLogFilePath, D as getLastSendError } from "./shutdown-pPAPSnk3.js";
10
+ import { m as minMessageId, r as resolveRecipient, d as defaultCountryFromEnv, a as rankFuzzy, b as dispatchAnalytic, i as installWatchdog, n as noteActivity, c as readWatchdogState } from "./watchdog-Cbgm050b.js";
11
11
  import { homedir } from "node:os";
12
12
  import Database from "better-sqlite3";
13
- import { I as IMessageDB, n as normalizedPhoneVariants, h as hasNativeModule } from "./imessage-db-C7w6mu1Q.js";
14
- import { streamExport } from "./exportStream-B86sO7Mg.js";
13
+ import { I as IMessageDB, n as normalizedPhoneVariants, h as hasNativeModule } from "./imessage-db-Cz8MIIpG.js";
14
+ import { streamExport } from "./exportStream-BCoZYUUZ.js";
15
15
  import { z } from "zod";
16
- import { A as APP_VERSION, a as APP_NAME } from "./meta-D3NoTAjA.js";
16
+ import { A as APP_VERSION, a as APP_NAME } from "./meta-DzE7CSrZ.js";
17
17
  import { randomUUID } from "node:crypto";
18
18
  import { parseUserDate } from "./dateParse-DJXMfq3a.js";
19
19
  const DEFAULT_TTL_MS = 24 * 60 * 60 * 1e3;
@@ -1966,7 +1966,7 @@ const TOOLS = [
1966
1966
  },
1967
1967
  {
1968
1968
  name: "check_imessage_availability",
1969
- description: "Preflight check: is this handle reachable via iMessage or SMS? Call BEFORE send_message to avoid wasted send attempts to unreachable recipients. Returns the best-guess service plus a human-readable hint when unreachable.",
1969
+ description: "Preflight check: is this handle reachable via iMessage or SMS? Authoritative when a conversation with the handle already exists (service comes from chat.db history); best-effort otherwise (Messages.app cannot verify iMessage registration without sending). send_message routes on the same ground truth automatically, so this is mainly useful for never-messaged handles and format validation.",
1970
1970
  annotations: annotations.status,
1971
1971
  inputSchema: {
1972
1972
  type: "object",
@@ -2757,6 +2757,7 @@ Call again with one of the contact:N labels.`,
2757
2757
  normalizedRecipient = resolution.handle;
2758
2758
  resolvedTarget = resolution.handle;
2759
2759
  }
2760
+ let preferredService;
2760
2761
  if (threadSlug) {
2761
2762
  const slugRecord = this.db.getSlugRecord(threadSlug);
2762
2763
  if (!slugRecord) {
@@ -2765,6 +2766,7 @@ Call again with one of the contact:N labels.`,
2765
2766
  {}
2766
2767
  );
2767
2768
  }
2769
+ preferredService = slugRecord.service === "SMS" ? "SMS" : "iMessage";
2768
2770
  if (slugRecord.isGroup) {
2769
2771
  if (slugRecord.displayName && !slugRecord.displayName.startsWith("chat")) {
2770
2772
  result = await sendToChat(slugRecord.displayName, message);
@@ -2772,14 +2774,18 @@ Call again with one of the contact:N labels.`,
2772
2774
  result = await sendToChatId(slugRecord.chatGuid, message);
2773
2775
  }
2774
2776
  } else {
2775
- result = await sendMessageReliable(slugRecord.chatIdentifier, message);
2777
+ result = await sendMessageReliable(slugRecord.chatIdentifier, message, preferredService);
2776
2778
  if (!result.success) {
2777
2779
  result = await sendMessageAlt(slugRecord.chatIdentifier, message);
2778
2780
  }
2779
2781
  }
2780
2782
  resolvedTarget = slugRecord.displayName || slugRecord.chatIdentifier;
2781
2783
  } else {
2782
- result = await sendMessageReliable(normalizedRecipient, message);
2784
+ const existingChat = await this.db.findChatByHandle(normalizedRecipient);
2785
+ if (existingChat && !existingChat.isGroupChat) {
2786
+ preferredService = existingChat.serviceType === "SMS" ? "SMS" : "iMessage";
2787
+ }
2788
+ result = await sendMessageReliable(normalizedRecipient, message, preferredService);
2783
2789
  if (!result.success) {
2784
2790
  result = await sendMessageAlt(normalizedRecipient, message);
2785
2791
  }
@@ -2787,7 +2793,7 @@ Call again with one of the contact:N labels.`,
2787
2793
  if (result.success) {
2788
2794
  const attachmentResults = [];
2789
2795
  if (attachments?.length) {
2790
- const targetHandle = threadSlug ? this.db.getSlugRecord(threadSlug)?.chatIdentifier : recipient;
2796
+ const targetHandle = threadSlug ? this.db.getSlugRecord(threadSlug)?.chatIdentifier : normalizedRecipient;
2791
2797
  const isGroupTarget = threadSlug ? Boolean(this.db.getSlugRecord(threadSlug)?.isGroup) : false;
2792
2798
  if (isGroupTarget) {
2793
2799
  for (const p of attachments) {
@@ -2799,7 +2805,7 @@ Call again with one of the contact:N labels.`,
2799
2805
  }
2800
2806
  } else if (targetHandle) {
2801
2807
  for (const p of attachments) {
2802
- const r = await sendAttachment(targetHandle, p);
2808
+ const r = await sendAttachment(targetHandle, p, preferredService);
2803
2809
  attachmentResults.push({
2804
2810
  path: p,
2805
2811
  success: r.success,
@@ -3132,8 +3138,18 @@ ${withThreads.map((t) => ` ${t.handle} → ${t.threadSlug}`).join("\n")}` : "";
3132
3138
  const { handle } = CheckImessageAvailabilitySchema.parse(args);
3133
3139
  const selectorHit = resolveContactSelector(handle);
3134
3140
  const effectiveHandle = selectorHit?.handle ?? handle;
3141
+ const existingChat = await this.db.findChatByHandle(effectiveHandle);
3142
+ if (existingChat && !existingChat.isGroupChat) {
3143
+ const service = existingChat.serviceType === "SMS" ? "SMS" : "iMessage";
3144
+ return toolText(`${handle} reachable via ${service} (existing conversation).`, {
3145
+ handle,
3146
+ service,
3147
+ reachable: true,
3148
+ hint: "Derived from existing conversation history — authoritative. send_message will route on this service."
3149
+ });
3150
+ }
3135
3151
  const result = await checkImessageAvailability(effectiveHandle);
3136
- const text = result.reachable ? `${handle} reachable via ${result.service}.` : `${handle} not reachable. ${result.hint ?? ""}`.trim();
3152
+ const text = result.reachable ? `${handle} reachable via ${result.service} (best-effort probe — no conversation history for this handle; Messages.app cannot verify iMessage registration without sending).` : `${handle} not reachable. ${result.hint ?? ""}`.trim();
3137
3153
  return toolText(text, {
3138
3154
  handle,
3139
3155
  service: result.service,