human hand off fix

This commit is contained in:
debu-2 2026-10-06 07:42:41 +05:30
parent 7b9e574b86
commit 0981262612
5 changed files with 154 additions and 24 deletions

View file

@ -1,3 +1,4 @@
from datetime import datetime
from html import escape
@ -25,13 +26,72 @@ def format_sender_line(sender: dict) -> str:
name = (sender.get("name") or "").strip()
address = (sender.get("address") or "").strip()
if not address:
return "unknown sender"
return name or "unknown sender"
local = address.split("@", 1)[0].lower()
if name and name.lower() not in {local, address.lower()}:
return f"{name} <{address}>"
return address
def sender_display_name(sender: dict | None) -> str:
sender = sender or {}
name = (sender.get("name") or "").strip()
address = (sender.get("address") or "").strip()
if name:
return name
if address:
return address.split("@", 1)[0].replace(".", " ").replace("_", " ").replace("-", " ").title()
return "unknown sender"
def _attribution_date(value) -> str:
if not isinstance(value, datetime):
return ""
return value.strftime("%a, %b %d, %Y at %I:%M %p").replace(" 0", " ")
def build_gmail_quote(
*,
sender: dict | None,
event_at,
body_text: str | None,
body_html: str | None,
) -> tuple[str, str]:
identity = format_sender_line(sender or {})
date = _attribution_date(event_at)
attribution = f"On {date} {identity} wrote:" if date else f"{identity} wrote:"
text_source = (body_text or "").rstrip()
quoted_lines = [f"> {line}" if line else ">" for line in text_source.splitlines()]
quoted_text = f"{attribution}\n" + "\n".join(quoted_lines)
inner_html = (body_html or "").strip() or (
f"<div>{escape(text_source).replace(chr(10), '<br>')}</div>"
)
quoted_html = (
f'<div class="gmail_quote">'
f'<div class="gmail_attr">{escape(attribution)}<br></div>'
f'<blockquote class="gmail_quote" '
f'style="margin:0 0 0 .8ex;border-left:1px solid #ccc;padding-left:1ex">'
f"{inner_html}"
f"</blockquote></div>"
)
return quoted_text, quoted_html
def attach_quoted_history(
*,
new_text: str,
new_html: str | None,
quoted_text: str,
quoted_html: str,
) -> tuple[str, str]:
body_text = f"{new_text.rstrip()}\n\n{quoted_text}".strip()
visible_html = (new_html or "").strip() or (
f"<div>{escape(new_text).replace(chr(10), '<br>')}</div>"
)
body_html = f"{visible_html}<br>{quoted_html}"
return body_text, body_html
def _footer_html(agent_email: str) -> str:
return f"""
<div class="gentic-handoff-footer" style="background:#F8F7FC;border-top:1px solid #E9E5F2;padding:12px 20px;">