
    j                         d Z ddlZddl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mZ ddlmZmZ dd	lm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)DictListOptionalTupleUnioncast)verbose_logger)CustomLogger)CustomPromptManagement)CacheControlInjectionPoint!CacheControlMessageInjectionPoint)AllMessageValuesChatCompletionCachedContent)StandardCallbackDynamicParamsc                      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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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_labelprompt_versionreturnc	                     |j                  dg       }	|	s|||fS t        j                  |      }
|	D ]:  }|j                  d      dk(  st	        t
        |      }| j                  ||
      }
< ||
|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)selfr   r   r   r   r   r   r   r   injection_pointsprocessed_messagesr    s               /Users/manta/Documents/Projects/TheRoad-I1/backend/.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 &" & (*<<<    r    c           	      D   | j                  dd      xs t        d      }| j                  dd      }d}t        |t              r|j	                         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 )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   r-   )r$   r   
isinstancestrisdigitintlenr   %_safe_insert_cache_control_in_messager	   warning)r    r   r-   _targetted_indextargetted_indextargetted_roleoriginal_indexmsgs           r)   r%   z4AnthropicCacheControlHook._process_message_injection?   sm   
 05yyt0
 0;(k: 	 7<ii6N)-&,'')"%&6"7.O640 &,N"3x=0O3c(m3-SS 17 )&  &&@@PP}  B  CK  L  ~M M**9)::ln  '776?n41WW$' X     r+   r   r-   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$   r2   r3   listr6   dict)r   r-   message_contents      r)   r7   z?AnthropicCacheControlHook._safe_insert_cache_control_in_messageo   sl     "++i6 os+'.GO$
  .?#a'Jr7JD,Q7>#O4r+   c                      y)z*Return the integration name for this hook.anthropic_cache_control_hook )r&   s    r)   integration_namez*AnthropicCacheControlHook.integration_name   s     .r+   c                 *    | j                  dd       ryy)Nr   TF)r$   )r   s    r)   'should_use_anthropic_cache_control_hookzAAnthropicCacheControlHook.should_use_anthropic_cache_control_hook   s    !!"BDIr+   c                 P    ddl m} t        j                  |       r |dd d       S y )Nr   )$_init_custom_logger_compatible_classrF   )logging_integrationinternal_usage_cache
llm_router)*litellm.litellm_core_utils.litellm_loggingrL   r   rJ   )r   rL   s     r)   2get_custom_logger_for_anthropic_cache_control_hookzLAnthropicCacheControlHook.get_custom_logger_for_anthropic_cache_control_hook   s7    	
 %LL
 8$B%) 
 r+   )NN)__name__
__module____qualname__r3   r   r   rC   r   r   r5   r   r*   staticmethodr   r%   r   r7   propertyrH   r   boolrJ   r
   rQ   rG   r+   r)   r   r      s[    '+(,%=%= '(%= !	%=
 C=%= #4.%= "?%= sm%= !%= 
sD)*D0	1%=N -0-<@AQ<R-		- -^ !,G	 2 .# . . D T  
  	,	 r+   r   )__doc__r"   typingr   r   r   r   r   r   litellm._loggingr	   "litellm.integrations.custom_loggerr
   -litellm.integrations.custom_prompt_managementr   7litellm.types.integrations.anthropic_cache_control_hookr   r   litellm.types.llms.openair   r   litellm.types.utilsr   r   rG   r+   r)   <module>r`      s;     ; ; + ; P T =M 6 Mr+   