From 35fa022e2eae2bcb83905c82f12d942e4c8121f8 Mon Sep 17 00:00:00 2001 From: Afonso Coutinho Date: Tue, 2 Jun 2026 16:39:31 +0100 Subject: [PATCH] fix: email pre-retrieval ignores contacts (reads non-existent email/phone keys) (#1241) * fix: match known email senders against the contact 'emails' list * fix: build contact-match snippets from emails/phones lists --- routes/email_helpers.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/routes/email_helpers.py b/routes/email_helpers.py index 264e5d8..68cb7fb 100644 --- a/routes/email_helpers.py +++ b/routes/email_helpers.py @@ -1154,7 +1154,10 @@ def _pre_retrieve_context(body: str, sender: str) -> tuple: try: from routes.contacts_routes import _fetch_contacts for c in _fetch_contacts() or []: - if (c.get("email") or "").lower() == sender_addr: + # Contacts are normalized to plural `emails` lists (see + # contacts_routes._normalize_contact); the old `c.get("email")` + # singular key never exists, so known senders were never matched. + if sender_addr in [(e or "").lower() for e in (c.get("emails") or [])]: is_known = True break except Exception: @@ -1248,13 +1251,13 @@ def _pre_retrieve_context(body: str, sender: str) -> tuple: t_lower = term.lower() matches = [c for c in all_contacts if t_lower in (c.get("name") or "").lower() - or t_lower in (c.get("email") or "").lower()] + or any(t_lower in (e or "").lower() for e in (c.get("emails") or []))] for c in matches[:2]: parts = [f"Name: {c.get('name','')}"] - if c.get("email"): - parts.append(f"Email: {c['email']}") - if c.get("phone"): - parts.append(f"Phone: {c['phone']}") + if c.get("emails"): + parts.append(f"Email: {', '.join(c['emails'])}") + if c.get("phones"): + parts.append(f"Phone: {', '.join(c['phones'])}") context_snippets.append(f"[Contact match for \"{term}\"] " + ", ".join(parts)) except Exception: pass