Update tools.py and android_handler.py to persist conversation text and intent metadata. A) tools.py changes 1) Update log_message() to this signature (keep backward compatibility): def log_message(agreement_ref, channel: str, direction: str, message_text: str = None, intent_label: str = None, template_slug: str = None, source: str = "ai", model: str = None, happened_at=None) -> dict: - agreement_ref can be agreement_number (str) or agreement_id (int); resolve to agreement_id internally. - Insert into contact_history with columns: (agreement_id, channel, direction, happened_at, message_text, intent_label, template_slug, source, model) - If happened_at is None, use now() in UTC. - Return the inserted row (id and fields). - Keep old call patterns working (e.g., when message_text=None). 2) Add a small helper inside tools.py if needed: _resolve_agreement_id(agreement_ref) -> int - If int, return; if str, use resolve_agreement_id to look up. B) android_handler.py changes 1) When a rule/template matches: - Build reply_text via personalise(). - Call log_message(...) with: agreement_ref=agreement_number, channel=channel (e.g. "whatsapp"), direction="outbound", message_text=reply_text, intent_label = matched_rule_label, # e.g., "not_ready_yet" template_slug = matched_template_slug, # e.g., "not_ready_yet" source="ai", model=None - Print console log: f"[TEMPLATE] intent={matched_rule_label} slug={matched_template_slug}" 2) When no rule matches (OpenAI fallback): - Generate reply via OpenAI (Lia UK prompt already in place). - Call log_message(...) with: intent_label="openai_fallback", template_slug=None, source="ai", model="gpt-4o-mini" (or whatever is used), message_text=the AI reply - Print console log: "[OPENAI] fallback reply logged" 3) Respect ok_to_reply(): if outside reply hours, do NOT send now: - Instead, schedule_followup(... reason="Reply deferred out of hours") and return without logging an outbound message. - Keep existing behaviour for deferrals. 4) For inbound messages (where the handler detects them), if we already insert inbound elsewhere, leave as-is. If not, ensure inbound logging uses log_message with message_text populated. Deliverable: - Modify both files accordingly. - Confirm with: "UPDATED tools.py and android_handler.py — message_text + intent stored"