
    `gjB                    `    d Z ddlmZ ddlZddlZddlZddlmZ dZdZ	ddZ
ddZdd	Zdd
Zy)a  Local index of text we've sent via ``sendRichMessage`` (Bot API 10.1).

Telegram does NOT echo a rich message's content back in ``reply_to_message``
when a user replies to it (verified: ``.text``/``.caption`` empty,
``.api_kwargs`` None). So replies to the launchd briefings / any rich send
arrive with no quotable text and the agent is blind to what was referenced.

Fix: remember ``message_id -> text`` at send time, look it up by
``reply_to_id`` on inbound. This module is the single source of truth for that
index.

Best-effort and dependency-free: every operation swallows errors and degrades
to a no-op / ``None`` so it can never break a send or an inbound message.
    )annotationsN)Optionali  i  c                 p    ddl m}   |        }t        j                  j	                  t        |      dd      S )Nr   )get_hermes_homestatezrich_sent_index.json)hermes_constantsr   ospathjoinstr)r   homes     J/root/.hermes/venv/lib/python3.12/site-packages/gateway/rich_sent_store.py_store_pathr      s)    0D77<<D	7,BCC    c                    |  d| S )N: )chat_id
message_ids     r   _keyr   #   s    Ya
|$$r   c                h   |r|| yt               }	 t        j                  t        j                  j	                  |      d       	 t        |dd      5 }t        j                  |      }ddd       t        t              si }|dt         t        t        j                               d|t        | |      <   t!        |      t"        kD  rIt%        |j'                         d 	      dt!        |      t"        z
   D ]  \  }}|j)                  |d        | d
t        j*                          }t        |dd      5 }t        j,                  ||d       ddd       t        j.                  ||       y# 1 sw Y   
xY w# t        t        f$ r i }Y w xY w# 1 sw Y   ExY w# t0        $ r Y yw xY w)zEPersist ``text`` for ``(chat_id, message_id)``. No-op on any failure.NT)exist_okrutf-8encoding)ttsc                ,    | d   j                  dd      S )N   r   r   )get)kvs    r   <lambda>zrecord.<locals>.<lambda><   s    RUYYtQ-? r   )keyz.tmp.wF)ensure_ascii)r   r	   makedirsr
   dirnameopenjsonload
isinstancedictFileNotFoundError
ValueError_MAX_TEXT_CHARSinttimer   len_MAX_ENTRIESsorteditemspopgetpiddumpreplace	Exception)	r   r   textr
   fhdatak_tmps	            r   recordrB   '   s   :%=D
BGGOOD)D9	dC'2 %byy}%dD) &'diik"+
T':&'
 t9|#

"?(D	L(* "1 D!" eBIIK=)#sW- 	4IIdBU3	4


3'% % ":. 	D		4 	4  sf   4F% F E4,F B6F% <FF% 4E>9F FF% FF% F"F% %	F10F1c                F   || y	 t        t               dd      5 }t        j                  |      }ddd       j	                  t        | |            }t        |t              r|j	                  d      xs dS 	 y# 1 sw Y   KxY w# t        t        t        f$ r Y yw xY w)z=Return stored text for ``(chat_id, message_id)`` or ``None``.Nr   r   r   r   )r)   r   r*   r+   r!   r   r,   r-   r.   r/   AttributeError)r   r   r=   r>   entrys        r   lookuprF   G   s    W_+-w7 	!299R=D	!gz23eT"99S>)T) # 	! 	!
 z>: s(   B	 A=AB	 =BB	 	B B )returnr   )r<   Optional[str]rG   None)rG   rH   )__doc__
__future__r   r*   r	   r2   typingr   r4   r0   r   r   rB   rF   r   r   r   <module>rM      s;    #  	  D%@r   