
    i=)                         d Z ddlZddlmZmZmZmZmZmZm	Z	m
Z
 ddlmZ ddlmZ ddlmZ ddlmZ ddlmZmZ dd	lmZmZ dd
lmZ ddlmZ erddlmZ neZ G d de      Z y)a  
This hook is used to inject cache control directives into the messages of a chat completion.

Users can define
- `cache_control_injection_points` in the completion params and litellm will inject the cache control directives into the messages at the specified injection points.

    N)TYPE_CHECKINGAnyDictListOptionalTupleUnioncast)verbose_logger)CustomLogger)CustomPromptManagementPromptManagementClient)CacheControlInjectionPoint!CacheControlMessageInjectionPoint)AllMessageValuesChatCompletionCachedContent)
PromptSpec)StandardCallbackDynamicParams)Loggingc                    |   e Zd Z	 	 	 	 	 ddedee   dedee   dee   dedee	   d	ee   d
ee
   dee   dee   deeee   ef   fdZededee   dee   fd       Zedededefd       Zedefd       Zdee   dee	   dedefdZ	 	 ddee   dee	   dee   ded	ee   d
ee
   defdZ	 	 	 ddee   dee   dedee	   d	ee   d
ee
   defdZ	 	 	 	 	 	 d dedee   dedee   dee   dededee	   deee      d	ee   d
ee
   dee   dee   deeee   ef   fdZededefd       Zededee   fd       Zy)!AnthropicCacheControlHookNmodelmessagesnon_default_params	prompt_idprompt_variablesdynamic_callback_paramsprompt_specprompt_labelprompt_versionignore_prompt_manager_model%ignore_prompt_manager_optional_paramsreturnc                    |j                  dg       }|s|||fS t        j                  |      }g }|D ]K  }|j                  d      dk(  r$t	        t
        |      }| j                  ||      };|j                  |       M |r||d<   |||fS )a,  
        Apply cache control directives based on specified injection points.

        Returns:
        - model: str - the model to use
        - messages: List[AllMessageValues] - messages with applied cache controls
        - non_default_params: dict - params with any global cache controls
        cache_control_injection_pointslocationmessage)pointr   )popcopydeepcopygetr
   r   _process_message_injectionappend)selfr   r   r   r   r   r   r   r    r!   r"   r#   injection_pointsprocessed_messagesremaining_pointsr)   s                   /Users/manta/Documents/Projects/TheRoad-I1/.venv/lib/python3.12/site-packages/litellm/integrations/anthropic_cache_control_hook.pyget_chat_completion_promptz4AnthropicCacheControlHook.get_chat_completion_prompt   s    . >P=S=S,b>
  ($666 "]]84 %Eyy$	1>F%)%D%D*< &E &" !''. & CS?@(*<<<    r)   c           	      D   | j                  dd      xs t        d      }| j                  dd      }d}t        |t              r	 t	        |      }n|}| j                  dd      }|t|}|dk  r|t        |      z  }d|cxk  rt        |      k  r!n nt        j                  ||   |      ||<   |S t        j                  d| d	t        |       d
| d       |S |3|D ].  }|j                  d      |k(  st        j                  ||      }0 |S # t
        $ r Y w xY w)z.Process message-level cache control injection.controlN	ephemeral)typeindexroler   z*AnthropicCacheControlHook: Provided index z- is out of bounds for message list of length z. Targeted index was z2. Skipping cache control injection for this point.)r(   r8   )r-   r   
isinstancestrint
ValueErrorlenr   %_safe_insert_cache_control_in_messager   warning)r)   r   r8   _targetted_indextargetted_indextargetted_roleoriginal_indexmsgs           r4   r.   z4AnthropicCacheControlHook._process_message_injectionP   s   
 05yyt0
 0;(k: 	 7<ii6N)-&,"%&6"7 /O640 &,N"3x=0O3c(m3 .SS_-w #&  &&@@PP}  B  CK  L  ~M M**9)::ln  '776?n41WW$' X     E  s   D 	DDr(   r8   c                     | j                  dd      }t        |t              r|| d<   | S t        |t              r)t	        |      dkD  rt        |d   t
              r||d   d<   | S )ap  
        Safe way to insert cache control in a message

        OpenAI Message content can be either:
            - string
            - list of objects

        This method handles inserting cache control in both cases.
        Per Anthropic's API specification, when using multiple content blocks,
        only the last content block can have cache_control.
        contentNcache_controlr   )r-   r=   r>   listrA   dict)r(   r8   message_contents      r4   rB   z?AnthropicCacheControlHook._safe_insert_cache_control_in_message   sl     "++i6 os+'.GO$
  .?#a'Jr7JD,Q7>#O4r6   c                      y)z*Return the integration name for this hook.anthropic_cache_control_hook )r0   s    r4   integration_namez*AnthropicCacheControlHook.integration_name   s     .r6   c                      y)zFAlways return False since this is not a true prompt management system.FrR   )r0   r   r   r   s       r4   should_run_prompt_managementz6AnthropicCacheControlHook.should_run_prompt_management   s     r6   c                 "    t        |g ddd      S )CNot used - this hook only modifies messages, doesn't fetch prompts.N)r   prompt_templateprompt_template_modelprompt_template_optional_paramscompleted_messagesr   )r0   r   r   r   r   r    r!   s          r4   _compile_prompt_helperz0AnthropicCacheControlHook._compile_prompt_helper   s      &"&,0#
 	
r6   c                 8   K   | j                  ||||||      S w)rW   )r   r   r   r   r    r!   )r\   )r0   r   r   r   r   r    r!   s          r4   async_compile_prompt_helperz5AnthropicCacheControlHook.async_compile_prompt_helper   s2      **#-$;%) + 
 	
s   litellm_logging_objtoolsc                 B   K   | j                  ||||||||
|||      S w)zCAsync version - delegates to sync since no async operations needed.)r   r   r   r   r   r   r   r    r!   r"   r#   )r5   )r0   r   r   r   r   r   r   r_   r   r`   r    r!   r"   r#   s                 r4    async_get_chat_completion_promptz:AnthropicCacheControlHook.async_get_chat_completion_prompt   sA     " ..1-$;#%)(C2W / 
 	
s   c                 *    | j                  dd       ryy)Nr&   TF)r-   )r   s    r4   'should_use_anthropic_cache_control_hookzAAnthropicCacheControlHook.should_use_anthropic_cache_control_hook   s    !!"BDIr6   c                 P    ddl m} t        j                  |       r |dd d       S y )Nr   )$_init_custom_logger_compatible_classrQ   )logging_integrationinternal_usage_cache
llm_router)*litellm.litellm_core_utils.litellm_loggingrf   r   rd   )r   rf   s     r4   2get_custom_logger_for_anthropic_cache_control_hookzLAnthropicCacheControlHook.get_custom_logger_for_anthropic_cache_control_hook   s7    	
 %LL
 8$B%) 
 r6   )NNNFF)NN)NNN)NNNNFF)__name__
__module____qualname__r>   r   r   rN   r   r   r   r?   boolr   r5   staticmethodr   r.   r   rB   propertyrS   rU   r   r\   r^   LiteLLMLoggingObjr   rb   rd   r   rk   rR   r6   r4   r   r      ss    -1&*(,6;@E/=/= '(/= !	/=
 C=/= #4./= "?/= j)/= sm/= !/= &.d^/= 08~/= 
sD)*D0	1/=b /0/<@AQ<R/		/ /b !,G	 2 .# . .C= j) "?	
 
 '+(,
C=
 j)
 #4.	

 "?
 sm
 !
 
 
. -1&*(,
C=
 #4.
 "?	

 j)
 sm
 !
 
 
8 -1&*&*(,6;@E

 '(
 !	

 C=
 #4.
 "?
 /
 j)
 T
#
 sm
 !
 &.d^
 08~
 
sD)*D0	1
> D T  
  	,	 r6   r   )!__doc__r+   typingr   r   r   r   r   r   r	   r
   litellm._loggingr   "litellm.integrations.custom_loggerr   -litellm.integrations.custom_prompt_managementr   +litellm.integrations.prompt_management_baser   7litellm.types.integrations.anthropic_cache_control_hookr   r   litellm.types.llms.openair   r   "litellm.types.prompts.init_promptsr   litellm.types.utilsr   rj   r   rr   r   rR   r6   r4   <module>r}      sP     O O O + ; P N T 9 =Wf 6 fr6   