reactions.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. """Token-free (regex) detection of user *reactions* to the agent.
  2. The only kind today is ``vibe`` — affection/gratitude aimed at the agent (``ily``, ``<3``,
  3. ``good bot``, a heart emoji), NOT general positive sentiment ("this is great" does not fire).
  4. Single source of truth for the CLI pet, TUI heart and desktop hearts via
  5. ``AIAgent.reaction_callback``; :func:`detect_reaction` returns a *kind* string so new kinds
  6. can be added without touching callers.
  7. """
  8. from __future__ import annotations
  9. import re
  10. #: The affection/gratitude reaction — the only kind today.
  11. VIBE = "vibe"
  12. # Narrow lexicon: gratitude + love aimed at the agent, hearts, ``<3`` (not ``</3``).
  13. _VIBE_RE = re.compile(
  14. "|".join(
  15. (
  16. r"\bgood\s*bot\b",
  17. r"\bi\s*(?:love|luv)\s*(?:you|u|ya)\b",
  18. r"\b(?:love|luv)\s*(?:you|u|ya)\b",
  19. r"\bily(?:sm)?\b",
  20. r"\bthank\s*(?:you|u)\b",
  21. r"\b(?:thanks|thx|tysm|ty)\b",
  22. r"<3+", # <3, <33 … but not </3
  23. # Hearts + affection faces (❤ ♥ 🥰 😍 😘 💕 💖 💗 💞 💛 💜 💚 💙 💓 💘 💝 🩷).
  24. r"[\u2764\u2665"
  25. r"\U0001F970\U0001F60D\U0001F618"
  26. r"\U0001F495\U0001F496\U0001F497\U0001F49E"
  27. r"\U0001F49B\U0001F49C\U0001F49A\U0001F499"
  28. r"\U0001F493\U0001F498\U0001F49D\U0001FA77]",
  29. )
  30. ),
  31. re.IGNORECASE,
  32. )
  33. def detect_reaction(text: str | None) -> str | None:
  34. """Return the reaction kind for *text* (currently :data:`VIBE`), or ``None``."""
  35. return VIBE if text and _VIBE_RE.search(text) else None