From adc0d9d6227c685d82a37831582ee91a9752ec52 Mon Sep 17 00:00:00 2001 From: holden093 Date: Sat, 20 Jun 2026 13:07:56 +0200 Subject: [PATCH] fix(contacts): add view action, show phones in list, preserve fields on update - Add 'view' action to manage_contact: fetch a single contact by UID and display name, UID, emails, phones, and address. - Show phone numbers in 'list' output alongside emails. - Preserve existing emails/phones during partial update (rename-only, single-field updates) by fetching the current contact when either field is missing. Uses force=True on _fetch_contacts to avoid a stale cache masking a just-added contact. - Fix the preserve guard: use OR instead of AND so single-field updates like {uid, emails:[new]} don't wipe phones and vice versa. --- src/tool_schemas.py | 6 ++--- src/tools/contacts.py | 53 +++++++++++++++++++++++++++++++++++-------- 2 files changed, 46 insertions(+), 13 deletions(-) diff --git a/src/tool_schemas.py b/src/tool_schemas.py index 7585f3e9d..b8e256ed6 100644 --- a/src/tool_schemas.py +++ b/src/tool_schemas.py @@ -1079,12 +1079,12 @@ FUNCTION_TOOL_SCHEMAS = [ "type": "function", "function": { "name": "manage_contact", - "description": "Create, update, delete, or list the user's CardDAV contacts. Use to save a new contact, update an existing one (email/phone/address), or remove one. Add does not require email: name + phone or name + address is valid. For update/delete you need the contact's uid — call action='list' first to find it. Writes go through the same dedupe + validation as the Contacts UI.", + "description": "Create, update, delete, view, or list the user's CardDAV contacts. Use to save a new contact, update an existing one (email/phone/address), view full details by uid, or remove one. For update/delete/view you need the contact's uid — call action='list' first to find it. Writes go through the same dedupe + validation as the Contacts UI. Update preserves existing emails/phones when they aren't passed — safe for partial edits like renaming.", "parameters": { "type": "object", "properties": { - "action": {"type": "string", "enum": ["list", "add", "update", "delete"], - "description": "list = show all contacts (with uids); add = create; update = edit by uid; delete = remove by uid."}, + "action": {"type": "string", "enum": ["list", "view", "add", "update", "delete"], + "description": "list = show all contacts (with uids and phones); view = show full details of one contact by uid; add = create; update = edit by uid (preserves fields not passed); delete = remove by uid."}, "uid": {"type": "string", "description": "Contact UID (required for update/delete; get it from action=list)."}, "name": {"type": "string", "description": "Contact's display name (for add/update)."}, "email": {"type": "string", "description": "Single email address (convenience for add, or the primary email for update). Optional when phone or address is provided."}, diff --git a/src/tools/contacts.py b/src/tools/contacts.py index fa9e84d6d..fec55c6af 100644 --- a/src/tools/contacts.py +++ b/src/tools/contacts.py @@ -103,9 +103,28 @@ async def do_manage_contact(content: str, owner: Optional[str] = None) -> Dict: lines = [f"{len(rows)} contacts:"] for c in rows: em = ", ".join(c.get("emails") or []) - lines.append(f"- {c.get('name') or '(no name)'} <{em}> [uid={c.get('uid','')}]") + ph = ", ".join(c.get("phones") or []) + detail = f"<{em}>" if em else "" + if ph: + detail += f" phone: {ph}" + lines.append(f"- {c.get('name') or '(no name)'} {detail} [uid={c.get('uid','')}]") return {"output": "\n".join(lines), "exit_code": 0} + if action == "view": + uid = (args.get("uid") or "").strip() + if not uid: + return {"error": "uid is required for view (use action=list to find it)", "exit_code": 1} + rows = await asyncio.to_thread(cc._fetch_contacts, True) + c = next((r for r in rows if r.get("uid") == uid), None) + if not c: + return {"output": f"No contact found with uid={uid}.", "exit_code": 0} + detail = f"Name: {c.get('name') or '(no name)'}\n" + detail += f"UID: {c.get('uid','')}\n" + detail += f"Emails: {', '.join(c.get('emails') or ['(none)'])}\n" + detail += f"Phones: {', '.join(c.get('phones') or ['(none)'])}\n" + detail += f"Address: {c.get('address') or '(none)'}" + return {"output": detail, "exit_code": 0} + if action == "add": email = (args.get("email") or "").strip() phones = [str(p or "").strip() for p in (args.get("phones") or []) if str(p or "").strip()] @@ -136,14 +155,28 @@ async def do_manage_contact(content: str, owner: Optional[str] = None) -> Dict: if not uid: return {"error": "uid is required for update (use action=list to find it)", "exit_code": 1} name = (args.get("name") or "").strip() - emails = args.get("emails") - if emails is None and args.get("email"): - emails = [args["email"]] - emails = [e.strip() for e in (emails or []) if e and e.strip()] - phones = [p.strip() for p in (args.get("phones") or []) if p and p.strip()] - address = (args.get("address") or "").strip() - if not name and not emails and not phones and not address: - return {"error": "Provide a name, emails, phones, or address to update", "exit_code": 1} + # Preserve existing emails/phones when caller passes neither — + # prevents data loss on partial updates like "rename Lisa". + # Fetch whenever *either* field is missing (not just when both + # are absent), so single-field updates like {uid, emails:[new]} + # don't wipe the other field. + existing = None + if "emails" not in args and "email" not in args or "phones" not in args: + rows = await asyncio.to_thread(cc._fetch_contacts, True) + existing = next((r for r in rows if r.get("uid") == uid), None) + if "emails" in args or "email" in args: + emails = args.get("emails") + if emails is None and args.get("email"): + emails = [args["email"]] + emails = [e.strip() for e in (emails or []) if e and e.strip()] + else: + emails = list(existing.get("emails") or []) if existing else [] + if "phones" in args: + phones = [p.strip() for p in (args.get("phones") or []) if p and p.strip()] + else: + phones = list(existing.get("phones") or []) if existing else [] + if not name and not emails: + return {"error": "Provide a name or emails to update", "exit_code": 1} if not name and emails: name = emails[0].split("@")[0] ok = await asyncio.to_thread(cc._update_contact, uid, name, emails, phones, address) @@ -156,6 +189,6 @@ async def do_manage_contact(content: str, owner: Optional[str] = None) -> Dict: ok = await asyncio.to_thread(cc._delete_contact, uid) return {"output": "Contact deleted." if ok else "Delete failed.", "exit_code": 0 if ok else 1} - return {"error": f"Unknown action '{action}'. Use list, add, update, or delete.", "exit_code": 1} + return {"error": f"Unknown action '{action}'. Use list, view, add, update, or delete.", "exit_code": 1} except Exception as e: return {"error": f"Contact operation failed: {e}", "exit_code": 1}