fix: Clarify-UX — Emoji-Zahlen in Buttons + Plain-Digit Text-Intercept

PROBLEM 1: Button-Labels waren nackte Zahlen ('1','2','3').
→ Jetzt Emoji-Zahlen ('1️⃣','2️⃣','3️⃣') via EMOJI_NUMBERS.
  Payload bleibt plain-digit (CLARIFY:id::1) damit isdigit() greift.

PROBLEM 2: Eingabe '1' als Text wurde nicht als CLARIFY-Antwort erkannt.
→ Neues _room_clarify mapping (room_id → clarify_id) in send_clarify.
→ Plain-digit text intercept VOR CLARIFY-Button-Handler:
  Wenn Text.strip().isdigit() und room_id in _room_clarify → als
  CLARIFY-Antwort verarbeiten (resolve_gateway_clarify).
→ Cleanup in Button-Handler und _cmd_reset.

P-013 beachtet: is_clarify_response (CLARIFY:-Prefix) wird weiterhin
zuerst geprüft; der neue Block fängt NUR plain-digit Texte ab.
This commit is contained in:
Bernd (Hermes)
2026-08-10 10:28:12 +02:00
parent fa9091eb6c
commit bd579bd112
+67 -4
View File
@@ -1300,6 +1300,8 @@ class RocketChatAdapter(BasePlatformAdapter):
# The router's _check_never_route_topic handles per-topic blocking now. # The router's _check_never_route_topic handles per-topic blocking now.
# Clarify state: maps clarify_id → session_key for gateway resolution # Clarify state: maps clarify_id → session_key for gateway resolution
self._clarify_state: Dict[str, str] = {} self._clarify_state: Dict[str, str] = {}
# Clarify room mapping: room_id → clarify_id for plain-digit text intercept
self._room_clarify: Dict[str, str] = {}
# FIX 2 (P0): ISSUE 3 — Pending Clarify Dispatch Slot # FIX 2 (P0): ISSUE 3 — Pending Clarify Dispatch Slot
# {room_id: {text, username, msg_id, msg_obj, is_button_response}} # {room_id: {text, username, msg_id, msg_obj, is_button_response}}
self._pending_clarify_dispatch: Dict[str, dict] = {} self._pending_clarify_dispatch: Dict[str, dict] = {}
@@ -2402,10 +2404,52 @@ class RocketChatAdapter(BasePlatformAdapter):
return return
# ── Ende Thema B Onboarding-Guard ──────────────────────────────────── # ── Ende Thema B Onboarding-Guard ────────────────────────────────────
# ── CLARIFY Plain-Digit Text Intercept ──────────────────────────
# Wenn der User eine einfache Zahl (1, 2, 3) tippt statt auf einen
# Button zu klicken, als CLARIFY-Antwort verarbeiten.
# P-013: Button-Detection VOR CLARIFY-Filter — is_clarify_response
# (CLARIFY: prefix) wird weiterhin zuerst geprüft, dieser Block
# fängt NUR plain-digit Texte ab.
is_archivar_clarify = text.startswith("ARCHIVAR_CLARIFY:") if text else False
if not is_clarify_response and not is_archivar_clarify:
stripped = text.strip()
if stripped.isdigit() and room_id in self._room_clarify:
clarify_id = self._room_clarify[room_id]
idx = int(stripped) - 1 # 1-basierte Eingabe → 0-basierter Index
resolved_text: Optional[str] = None
try:
from tools.clarify_gateway import _entries as _clarify_entries
entry = _clarify_entries.get(clarify_id)
if entry and entry.choices and 0 <= idx < len(entry.choices):
resolved_text = entry.choices[idx]
except Exception:
resolved_text = None
if resolved_text is None:
resolved_text = f"choice {idx + 1}"
# Resolve via gateway and clean up
self._clarify_state.pop(clarify_id, None)
self._room_clarify.pop(room_id, None)
try:
from tools.clarify_gateway import resolve_gateway_clarify
resolved = resolve_gateway_clarify(clarify_id, resolved_text)
if resolved:
logger.info(
"[rocketchat] Clarify %s resolved via plain-digit: %s (session=%s)",
clarify_id, resolved_text[:60],
self._clarify_state.get(clarify_id, "?"),
)
else:
logger.warning("[rocketchat] Clarify %s resolve returned False", clarify_id)
except Exception as exc:
logger.error("[rocketchat] resolve_gateway_clarify failed: %s", exc)
routing_counter.increment('clarify_plain_digit_bypass')
logger.info("ROUTING BYPASS: reason=clarify_plain_digit_bypass, origin_room=%s", room_id)
return
# ── Ende CLARIFY Plain-Digit Text Intercept ─────────────────────
# ── CLARIFY Response Handling ─────────────────────────────────── # ── CLARIFY Response Handling ───────────────────────────────────
# Handle CLARIFY button responses: CLARIFY:<clarify_id>:<choice> or CLARIFY:<clarify_id>:other # Handle CLARIFY button responses: CLARIFY:<clarify_id>:<choice> or CLARIFY:<clarify_id>:other
# V8.2.1 A2: Handle ARCHIVAR_CLARIFY (Nachklärung) responses # V8.2.1 A2: Handle ARCHIVAR_CLARIFY (Nachklärung) responses
is_archivar_clarify = text.startswith("ARCHIVAR_CLARIFY:") if text else False
if is_clarify_response and text.startswith("CLARIFY:"): if is_clarify_response and text.startswith("CLARIFY:"):
routing_counter.increment('clarify_answer_bypass') routing_counter.increment('clarify_answer_bypass')
logger.info("ROUTING BYPASS: reason=clarify_answer_bypass, origin_room=%s", room_id) logger.info("ROUTING BYPASS: reason=clarify_answer_bypass, origin_room=%s", room_id)
@@ -2446,6 +2490,10 @@ class RocketChatAdapter(BasePlatformAdapter):
resolved_text = f"choice {idx + 1}" resolved_text = f"choice {idx + 1}"
# Resolve via gateway and clean up # Resolve via gateway and clean up
self._clarify_state.pop(clarify_id, None) self._clarify_state.pop(clarify_id, None)
# Clean up room→clarify mapping (button click resolved it)
for rid, cid in list(self._room_clarify.items()):
if cid == clarify_id:
self._room_clarify.pop(rid, None)
try: try:
from tools.clarify_gateway import resolve_gateway_clarify from tools.clarify_gateway import resolve_gateway_clarify
resolved = resolve_gateway_clarify(clarify_id, resolved_text) resolved = resolve_gateway_clarify(clarify_id, resolved_text)
@@ -4026,6 +4074,7 @@ class RocketChatAdapter(BasePlatformAdapter):
self._explicit_switch_sticky.clear() self._explicit_switch_sticky.clear()
self._pending_move_requests.clear() self._pending_move_requests.clear()
self._rooms_with_pending_questions.clear() self._rooms_with_pending_questions.clear()
self._room_clarify.clear()
# Onboarding-State zurücksetzen falls aktiv # Onboarding-State zurücksetzen falls aktiv
if self._onboarding_state: if self._onboarding_state:
@@ -7475,9 +7524,19 @@ class RocketChatAdapter(BasePlatformAdapter):
) )
text += f"\n\n{option_lines}" text += f"\n\n{option_lines}"
# Build buttons with short labels to avoid truncation # Build buttons with emoji-number labels (UX: shorter, clearer).
button_labels = [str(i + 1) for i in range(len(choices))] # Payload uses plain digits so the CLARIFY handler's isdigit()
buttons = _build_custom_buttons(button_labels, prefix=f"CLARIFY:{clarify_id}") # check still works.
buttons = []
for i in range(len(choices)):
emoji = self.EMOJI_NUMBERS[i] if i < len(self.EMOJI_NUMBERS) else str(i + 1)
buttons.append({
"type": "button",
"text": emoji,
"msg": f"CLARIFY:{clarify_id}::{i + 1}",
"msg_in_chat_window": True,
"style": "default",
})
# Add "Other" button with special handling # Add "Other" button with special handling
buttons.append({ buttons.append({
@@ -7502,6 +7561,10 @@ class RocketChatAdapter(BasePlatformAdapter):
# Store clarify state for gateway identification # Store clarify state for gateway identification
self._clarify_state = getattr(self, '_clarify_state', {}) self._clarify_state = getattr(self, '_clarify_state', {})
self._clarify_state[clarify_id] = session_key self._clarify_state[clarify_id] = session_key
# Store room→clarify mapping for plain-digit text intercept
self._room_clarify = getattr(self, '_room_clarify', {})
self._room_clarify[real_room_id] = clarify_id
# For "Other" button, we need to enable text capture mode # For "Other" button, we need to enable text capture mode
# This is done by calling mark_awaiting_text when the user clicks "Other" # This is done by calling mark_awaiting_text when the user clicks "Other"