
    YiL                       U d Z ddlmZ ddlZddlmZmZmZ ddlm	Z	 ddl
mZ  ej                  d      Zd	Zd
ed<   e	j                   e	j"                  e	j$                  dZded<   dZd
ed<   dZd
ed<   ddZ	 d	 	 	 	 	 	 	 	 	 	 	 ddZg dZy)a  RLM Neo-Cortex -- Telegram Inline Button Feedback Handler.

Handles Telegram callback_query events that carry thumbs-up / thumbs-down
feedback for a specific interaction.  Also provides a helper to build the
Telegram InlineKeyboardMarkup dict that is sent alongside AI replies.

Implements Story 5.07 of the RLM Neo-Cortex PRD (Module 5).

VERIFICATION_STAMP
Story: 5.07
Verified By: parallel-builder
Verified At: 2026-02-26
Tests: see tests/rlm/test_feedback.py
Coverage: 100%
    )annotationsN)AnyDictOptional   )FeedbackSignal)FeedbackCollectorzcore.rlm.feedback_telegramfeedbackstr_CALLBACK_PREFIX)positivenegativeneutralzDict[str, FeedbackSignal]_SIGNAL_MAPu   👍 Helpful_POSITIVE_LABELu   👎 Not helpful_NEGATIVE_LABELc                T    dt         t         d|  ddt        t         d|  ddggiS )a8  Build a Telegram InlineKeyboardMarkup dict for a given interaction.

    Returns a dict in the format expected by the Telegram Bot API:
    ``{"inline_keyboard": [[...buttons...]]}``

    Each button has ``text`` and ``callback_data`` fields.
    callback_data format: ``feedback:{interaction_id}:{signal}``
    inline_keyboard:z	:positive)textcallback_dataz	:negative)r   r   r   )interaction_ids    3/mnt/e/genesis-system/core/rlm/feedback_telegram.pybuild_feedback_keyboardr   .   sS     	 ,(8'9>:J)%T
 ,(8'9>:J)%T	
     c           
       K   | j                  dd      }t        |      dk7  s|d   t        k7  r
ddd|  dd	S |\  }}}|sddd
d	S t        j	                  |j                               }	|	
ddd| dd	S ddlm}
 |+	 ddl}|j                  |j                  t        |            }nt        ||
      r|n |
t        |            }	 |j                  |||	       d{   }d|dudS # t        $ r-  |
t        t        t        |                  ddz  z        }Y Yw xY w7 D# t        $ r/}t         j#                  d|       ddt        |      d	cY d}~S d}~ww xY ww)a  Parse a Telegram callback_data string and record the feedback signal.

    Parameters
    ----------
    callback_data:
        Raw callback_data from Telegram, expected format:
        ``feedback:{interaction_id}:{signal}``
    chat_id:
        Telegram chat_id (used as tenant_id if tenant_id not supplied).
    message_id:
        Telegram message_id (unused at runtime, available for ack).
    collector:
        FeedbackCollector instance to delegate recording to.
    tenant_id:
        Optional explicit UUID.  If omitted, chat_id is coerced to UUID.

    Returns
    -------
    dict with keys:
        status          -- "recorded" | "error"
        pair_generated  -- bool
        error           -- str (only on error)
    r         r   errorFzFInvalid callback_data format. Expected 'feedback:{id}:{signal}', got '')statuspair_generatedr   z interaction_id must not be emptyNzUnknown signal 'z%'. Must be positive/negative/neutral.)UUID   )int)	tenant_idr   signalrecorded)r!   r"   z3Error recording feedback from Telegram callback: %s)splitlenr   r   getloweruuidr#   uuid5NAMESPACE_URLr   	Exceptionabshash
isinstancerecord_feedbacklogger	exception)r   chat_id
message_id	collectorr&   parts_r   
raw_signalr'   r#   r-   resolved_tenantpairexcs                  r   handle_callback_queryr@   G   s    > Q'E
5zQ%(&66#??LoQP	
 	
 %*!A~z#7
 	
 __Z--/0F~#'
|3XY
 	
 	K"jj););S\JO (2)T'B)SQZ^H\
..%) / 
 
 !"$.
 	
  	K"s4G+='>!S&'IJO	K
  
NPST#X
 	

sl   A<E"?)C, ( E"	D'  D%!
D' +E",3D"E"!D""E"%D' '	E0$EEE"EE")r   r@   r   r   )r   r   returnDict[str, Any])N)r   r   r7   r%   r8   r%   r9   r	   r&   zOptional[Any]rA   rB   )__doc__
__future__r   loggingtypingr   r   r   	contractsr   r
   r	   	getLoggerr5   r   __annotations__POSITIVENEGATIVENEUTRALr   r   r   r   r@   __all__ r   r   <module>rO      s    #  & & % '			7	8 # # "''''&&*&  , +/ /<  $W
W
W
 W
 !	W

 W
 W
tr   