
    (GjB                    b    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dS )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  returnstrc                 ~    ddl m}   |             }t          j                            t          |          dd          S )Nr   )get_hermes_homestatezrich_sent_index.json)hermes_constantsr   ospathjoinr   )r   homes     =/home/rurouni/.hermes/hermes-agent/gateway/rich_sent_store.py_store_pathr      s@    000000?D7<<D		7,BCCC    c                    |  d| S )N: )chat_id
message_ids     r   _keyr   #   s    $$
$$$r   textOptional[str]Nonec                   |r|| dS t                      }	 t          j        t          j                            |          d           	 t          |dd          5 }t          j        |          }ddd           n# 1 swxY w Y   t          |t                    si }n# t          t          f$ r i }Y nw xY w|dt                   t          t          j                              d|t          | |          <   t!          |          t"          k    r]t%          |                                d 	          dt!          |          t"          z
           D ]\  }}|                    |d           | d
t          j                     }t          |dd          5 }t          j        ||d           ddd           n# 1 swxY w Y   t          j        ||           dS # t0          $ r Y dS 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                              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   r   r   fhdatak_tmps	            r   recordrE   '   s    :%==D
BGOOD))D9999	dC'222 %by}}% % % % % % % % % % % % % % %dD)) !:. 	 	 	DDD	 &&'dikk""+
 +
T':&&'
 t99|##

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

3   s~   3G! B" A?3B" ?BB" BB" !G! "B85G! 7B88C#G! F?3G! ?GG! GG! !
G/.G/c                   || dS 	 t          t                      dd          5 }t          j        |          }ddd           n# 1 swxY w Y   |                    t          | |                    }t          |t                    r|                    d          pdS n# t          t          t          f$ r Y dS w xY wdS )z=Return stored text for ``(chat_id, message_id)`` or ``None``.Nr   r   r   r!   )r-   r   r.   r/   r%   r   r0   r1   r2   r3   AttributeError)r   r   r@   rA   entrys        r   lookuprI   G   s   W_t+--w777 	!29R==D	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	!gz2233eT"" 	*99S>>)T)	*z>:   tt4s4   B" AB" AB" AAB" "B=<B=)r   r   )r   r   r   r   )r   r   )__doc__
__future__r   r.   r   r6   typingr   r8   r4   r   r   rE   rI   r   r   r   <module>rM      s     # " " " " "  				       D D D D% % % %   @     r   