
    Jj[                         d Z ddlZddlmZmZmZ ddedededdfd	Zdededefd
Z	de
dee
e
f         fdZ	 	 ddeee
ef                  de
dedeee
ef                  fdZdS )uB  Anthropic prompt caching strategy.

Single layout: ``system_and_3``. 4 cache_control breakpoints — system
prompt + last 3 non-system messages, all at the same TTL (5m or 1h).
Reduces input token costs by ~75% on multi-turn conversations within a
single session.

Pure functions -- no class state, no AIAgent dependency.
    N)AnyDictListFmsgcache_markernative_anthropicreturnc                    |                      dd          }|                      d          }|dk    r	|r|| d<   dS ||dk    r|dk    r|sdS |dk    r|sdS || d<   dS t          |t                    rd||d	g| d<   dS t          |t                    r&|r&|d
         }t          |t                    r||d<   dS dS dS dS )zFAdd cache_control to a single message, handling all format variations.role contenttoolcache_controlN	assistanttext)typer   r   )get
isinstancestrlistdict)r   r   r   r   r   lasts         :/home/rurouni/.hermes/hermes-agent/agent/prompt_caching.py_apply_cache_markerr      s!   7762Dggi  Gv~~*~  ,O'R--6>>"2>
 F;'7 F+O'3 W|LL
I 	'4   1W 1r{dD!! 	1$0D!!!1 1 1 1	1 	1    c                     |rdS |                      d          }||dk    rdS t          |t                    r*t          |          ot          |d         t                    S t          |t
                    S )u  True if a marker on this message is actually honored by the provider.

    On the native Anthropic layout every message works (top-level markers are
    relocated by the adapter). On the envelope layout (OpenRouter et al.) only
    markers inside content parts are honored: empty-content messages (e.g.
    assistant turns that are pure tool_calls) and empty tool messages would
    receive a top-level marker the provider ignores — wasting one of the four
    breakpoints. Skip those so the breakpoints land on messages that count.
    Tr   Nr   Fr   )r   r   r   boolr   r   )r   r   r   s      r   _can_carry_markerr   4   sw      tggi  G'R--u'4   ?
 G}}>GBK!>!>>gs###r   ttlc                 $    ddi}| dk    rd|d<   |S )zCBuild a cache_control marker dict for the given TTL ('5m' or '1h').r   	ephemeral1hr     )r    markers     r   _build_markerr&   L   s#    $k2F
d{{uMr   5mapi_messages	cache_ttlc                 |   t          j        |           sS t          |          }d}d                             d          dk    rt	          d         |           |dz  }d|z
  }fdt          t                              D             }|| d         D ]}t	          |         |           S )	a  Apply system_and_3 caching strategy to messages for Anthropic models.

    Places up to 4 cache_control breakpoints: system prompt + last 3 non-system
    messages, all at the same TTL.

    Returns:
        Deep copy of messages with cache_control breakpoints injected.
    r   r   systemr         c                     g | ]:}|                              d           dk    !t          |                   8|;S )r   r+   r,   )r   r   ).0imessagesr   s     r   
<listcomp>z1apply_anthropic_cache_control.<locals>.<listcomp>n   sX       A;??6""h..hqk<LMMM / 	
...r   N)copydeepcopyr&   r   r   rangelen)	r(   r)   r   r%   breakpoints_used	remainingnon_sysidxr2   s	     `     @r   apply_anthropic_cache_controlr<   T   s     }\**H 9%%F{v(**HQKBRSSSSA$$I    s8}}%%  G 	z{{# V VHSM6DTUUUUUOr   )F)r'   F)__doc__r4   typingr   r   r   r   r   r   r   r   r&   r<   r$   r   r   <module>r?      s+     " " " " " " " " " ""1 "1T "1 "1 "1Z^ "1 "1 "1 "1J$4 $4 $D $ $ $ $0s tCH~     "# #tCH~&## # 
$sCx.	# # # # # #r   