telegram_entities.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. """Telegram ``text_link`` expansion, kept out of the adapter facade.
  2. A rich-text hyperlink arrives as visible anchor text plus a ``text_link`` entity carrying the
  3. URL; the model only ever sees the anchor unless the URL is inlined (#31071).
  4. """
  5. from __future__ import annotations
  6. from typing import Any
  7. def _utf16_length(text: str) -> int:
  8. return len(text.encode("utf-16-le")) // 2
  9. def _code_point_index(text: str, utf16_offset: int) -> int | None:
  10. """Python index for a UTF-16 code-unit offset; None when it splits a surrogate pair."""
  11. try:
  12. return len(text.encode("utf-16-le")[: utf16_offset * 2].decode("utf-16-le"))
  13. except UnicodeDecodeError:
  14. return None
  15. def expand_link_entities(message: Any) -> str:
  16. """Message text (or caption) with every hidden ``text_link`` URL inlined after its anchor.
  17. Entity offsets are UTF-16 code units (emoji before the anchor count twice), so they are mapped
  18. to code-point indices before slicing. Malformed entities are skipped; an anchor that already
  19. reads as its own URL, or text already carrying the inline form, is left untouched.
  20. """
  21. text = getattr(message, "text", None)
  22. if text:
  23. entities = getattr(message, "entities", None) or []
  24. else:
  25. text = getattr(message, "caption", None) or ""
  26. entities = getattr(message, "caption_entities", None) or []
  27. if not text or not entities:
  28. return text
  29. utf16_length = _utf16_length(text)
  30. links: list[tuple[int, int, str]] = []
  31. for entity in entities:
  32. entity_type = str(getattr(entity, "type", "")).split(".")[-1].lower()
  33. raw_url = getattr(entity, "url", None)
  34. url = raw_url.strip() if isinstance(raw_url, str) else ""
  35. if entity_type != "text_link" or not url:
  36. continue
  37. try:
  38. offset = int(getattr(entity, "offset", -1))
  39. length = int(getattr(entity, "length", 0))
  40. except (TypeError, ValueError):
  41. continue
  42. if offset < 0 or length <= 0 or offset + length > utf16_length:
  43. continue
  44. start, end = _code_point_index(text, offset), _code_point_index(text, offset + length)
  45. if start is None or end is None or end <= start:
  46. continue
  47. if text[start:end].strip() == url:
  48. continue
  49. links.append((start, end, url))
  50. expanded = text
  51. for _start, end, url in sorted(links, reverse=True):
  52. inline = f" ({url})"
  53. if expanded[end:].startswith(inline):
  54. continue
  55. expanded = f"{expanded[:end]}{inline}{expanded[end:]}"
  56. return expanded