
    `gj                         d Z ddlZddlZddlZddlZddlmZ ddlmZ  e       dz  Z	dZ
defdZd	eddfd
Zdedee   fdZ	 	 ddededededdf
dZ	 	 ddedededefdZddedefdZy)a1  
Sticker description cache for Telegram.

When users send stickers, we describe them via the vision tool and cache
the descriptions keyed by file_unique_id so we don't re-analyze the same
sticker image on every send. Descriptions are concise (1-2 sentences).

Cache location: ~/.hermes/sticker_cache.json
    N)Optional)get_hermes_homezsticker_cache.jsonzyDescribe this sticker in 1-2 sentences. Focus on what it depicts -- character, action, emotion. Be concise and objective.returnc                      t         j                         r*	 t        j                  t         j	                  d            S i S # t        j
                  t        f$ r i cY S w xY w)z!Load the sticker cache from disk.utf-8encoding)
CACHE_PATHexistsjsonloads	read_textJSONDecodeErrorOSError     H/root/.hermes/venv/lib/python3.12/site-packages/gateway/sticker_cache.py_load_cacher      sX    	::j22G2DEE I $$g. 	I	s   (A AAcachec                 H   t         j                  j                  dd       t        j                  t        t         j                        d      \  }}	 t        j                  |dd      5 }t        j                  | |dd	
       |j                          t        j                  |j                                ddd       t        j                  |t        t                      y# 1 sw Y   -xY w# t        $ r' 	 t        j                  |        # t         $ r Y  w xY ww xY w)z*Save the sticker cache to disk atomically.T)parentsexist_okz.tmp)dirsuffixwr   r      F)indentensure_asciiN)r
   parentmkdirtempfilemkstempstrosfdopenr   dumpflushfsyncfilenoreplaceBaseExceptionunlinkr   )r   fdtmp_pathfs       r   _save_cacher0   '   s    D48##
!!"6LBYYr31 	!QIIeQqu=GGIHHQXXZ 	! 	

8S_-		! 	!
  	IIh 	  		sI   C1 ,AC%9+C1 %C.*C1 1	D!;DD!	DD!DD!file_unique_idc                 8    t               }|j                  |       S )z
    Look up a cached sticker description.

    Returns:
        dict with keys {description, emoji, set_name, cached_at} or None.
    )r   get)r1   r   s     r   get_cached_descriptionr4   ;   s     ME99^$$r   descriptionemojiset_namec                 f    t               }|||t        j                         d|| <   t        |       y)u&  
    Store a sticker description in the cache.

    Args:
        file_unique_id: Telegram's stable sticker identifier.
        description:    Vision-generated description text.
        emoji:          Associated emoji (e.g. "😀").
        set_name:       Sticker set name if available.
    )r5   r6   r7   	cached_atN)r   timer0   )r1   r5   r6   r7   r   s        r   cache_sticker_descriptionr;   F   s3     ME"YY[	E. r   c                 B    d}|r|r
d| d| d}n|rd| }d| d|  dS )u   
    Build the warm-style injection text for a sticker description.

    Returns a string like:
      [The user sent a sticker 😀 from "MyPack"~ It shows: "A cat waving" (=^.w.^=)]
      z from ""z[The user sent a stickerz~ It shows: "z" (=^.w.^=)]r   )r5   r6   r7   contexts       r   build_sticker_injectionrA   _   sE     GEeWHXJb1	eW+%gYn[MWWr   c                     | r	d|  d|  dS y)zL
    Build injection text for animated/video stickers we can't analyze.
    z#[The user sent an animated sticker z9~ I can't see animated ones yet, but the emoji suggests: ]zB[The user sent an animated sticker~ I can't see animated ones yet]r   )r6   s    r    build_animated_sticker_injectionrD   s   s,     1% 9FFKWAO	
 Pr   )r=   r=   )r=   )__doc__r   r$   r!   r:   typingr   hermes_cli.configr   r
   STICKER_VISION_PROMPTdictr   r0   r#   r4   r;   rA   rD   r   r   r   <module>rJ      s     	    - !55
< T t  (%3 %8D> % 	  	
 
6 XXX X 		X(	PC 	P 	Pr   