Ë
    tPêj  ã                   ó–   — d Z ddlZddlZddlmZmZmZ ddlZddlm	Z	 ddl
mZ ddlmZ ddlmZmZmZ erddlmZ neZ G d	„ d
«      Zy)zË
Class to check for LLM API hanging requests


Notes:
- Do not create tasks that sleep, that can saturate the event loop
- Do not store large objects (eg. messages in memory) that can increase RAM usage
é    N)ÚTYPE_CHECKINGÚAnyÚFinal)Úverbose_proxy_logger)ÚInMemoryCache)Ú get_litellm_metadata_from_kwargs)Ú!HANGING_ALERT_BUFFER_TIME_SECONDSÚ$MAX_OLDEST_HANGING_REQUESTS_TO_CHECKÚHangingRequestData)ÚSlackAlertingc                   óJ   — e Zd ZdZdefd„Z	 ddedz  fd„Zd„ Zd„ Z	d	e
fd
„Zy)ÚAlertingHangingRequestCheckzA
    Class to safely handle checking hanging requests alerts
    Úslack_alerting_objectc                 ó¤   — || _         t        | j                   j                  dz  t        z   «      | _        t        | j                  ¬«      | _        y )Ng      ø?)Údefault_ttl)r   ÚintÚalerting_thresholdr	   Úhanging_request_cache_ttlr   Úhanging_request_cache)Úselfr   s     ú}/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/litellm/integrations/SlackAlerting/hanging_request_check.pyÚ__init__z$AlertingHangingRequestCheck.__init__#   sM   € ð &;ˆÔ"ô *-Ø×&Ñ&×9Ñ9¸CÑ?ÔBcÑcó*
ˆÔ&ô &3Ø×6Ñ6ô&
ˆÕ"ó    NÚrequest_datac           
   ƒ   óè  K  — |€yt        |¬«      }|j                  dd«      }d}|j                  dd«      �=t        |d   t        «      r*t	        j
                  ||d   j                  di «      ¬«      }t        |j                  dd«      |||j                  d	d«      |j                  d
d«      ¬«      }| j                  j                  |j                  || j                  ¬«      ƒ d{  –—†  y7 Œ­w)z�
        Add a request to the hanging request cache. This is the list of request_ids that gets periodicall checked for hanging requests
        N)ÚkwargsÚmodelÚ Ú
deploymentÚlitellm_params)r   Úoptional_paramsÚlitellm_call_idÚuser_api_key_aliasÚuser_api_key_team_alias)Ú
request_idr   Úapi_baseÚ	key_aliasÚ
team_alias)ÚkeyÚvalueÚttl)r   ÚgetÚ
isinstanceÚdictÚlitellmÚget_api_baser   r   Úasync_set_cacher%   r   )r   r   Úrequest_metadatar   r&   Úhanging_request_datas         r   Ú$add_request_to_hanging_request_checkz@AlertingHangingRequestCheck.add_request_to_hanging_request_check2   s
  è ø€ ð ÐØä"BÈ,Ô"WÐØ#×'Ñ'¨°Ó4ˆØ#ˆà×Ñ˜L¨$Ó/Ð;Ä
È<ÐXdÑKeÔgkÔ@lÜ×+Ñ+ØØ ,¨\Ñ :× >Ñ >Ð?OÐQSÓ TôˆHô
 '9Ø#×'Ñ'Ð(9¸2Ó>ØØØ&×*Ñ*Ð+?ÀÓDØ'×+Ñ+Ð,EÀrÓJô'
Ðð ×(Ñ(×8Ñ8Ø$×/Ñ/Ø&Ø×.Ñ.ð 9ó 
÷ 	
ð 	
ð
 	ð	
ús   ‚C(C2Ã*C0Ã+C2c              ƒ   ód  K  — ddl m} |j                  €y| j                  j	                  t
        ¬«      ƒ d{  –—† }|D ]ä  }| j                  j                  |¬«      ƒ d{  –—† }|€Œ*|j                  rŒ7|j                  j                  d|j                  › �dd¬«      ƒ d{  –—† }|�| j                  j                  |¬«       Œ‰t        j                  «       |j                  z
  }|| j                  j                  k  rŒÄ| j                  |¬	«      ƒ d{  –—†  d|_        Œæ y7 Œî7 ŒÇ7 Œ†7 Œ­w)
z2
        Send alerts for hanging requests
        r   )Úproxy_logging_objN)Ún)r)   zrequest_status:T)r)   Úlitellm_parent_otel_spanÚ
local_only)r3   )Úlitellm.proxy.proxy_serverr6   Úinternal_usage_cacher   Úasync_get_oldest_n_keysr
   Úasync_get_cacheÚalertedr%   Ú_remove_keyÚtimeÚ
created_atr   r   Úsend_hanging_request_alert)r   r6   Úhanging_requestsr%   r3   Úrequest_statusÚrequest_age_secondss          r   Ú send_alerts_for_hanging_requestsz<AlertingHangingRequestCheck.send_alerts_for_hanging_requestsU   si  è ø€ õ 	Að ×1Ñ1Ð9Øà(,×(BÑ(B×(ZÑ(ZÜ2ð )[ó )
÷ #
Ðð +ò %	0ˆJØDH×D^ÑD^×DnÑDnØð Eoó E÷ ?Ð ð $Ð+Øà#×+Ò+Øà#4×#IÑ#I×#YÑ#YØ%Ð&:×&EÑ&EÐ%FÐGØ)-Øð $Zó $÷ ˆNð Ð)à×*Ñ*×6Ñ6Ø"ð 7ô ð ä"&§)¡)£+Ð0D×0OÑ0OÑ"OÐØ" T×%?Ñ%?×%RÑ%RÒRð ð
 ×1Ñ1ÐG[Ð1Ó\×\Ð\ð ,0Ð Õ(ðK%	0ðN 	ðW#
øð
?øðøð. ]úsG   ‚7D0¹D(º(D0Á"D*Á#AD0Â%D,Â&A3D0ÄD.ÄD0Ä*D0Ä,D0Ä.D0c              ƒ   óØ   K  — 	 t        j                  d«       | j                  «       ƒ d{  –—†  t        j                  | j
                  j                  dz  «      ƒ d{  –—†  Œb7 Œ97 Œ­w)zÊ
        Background task that checks all request ids in self.hanging_request_cache to check if they have completed

        Runs every alerting_threshold/2 seconds to check for hanging requests
        z!Checking for hanging requests....Né   )r   ÚdebugrF   ÚasyncioÚsleepr   r   )r   s    r   Úcheck_for_hanging_requestsz6AlertingHangingRequestCheck.check_for_hanging_requests�   s\   è ø€ ð Ü ×&Ñ&Ð'JÔKØ×7Ñ7Ó9×9Ð9Ü—-‘- × :Ñ :× MÑ MÐPQÑ QÓR×RÐRð à9øØRús!   ‚*A*¬A&­3A*Á A(Á!A*Á(A*r3   c           	   ƒ   ót  K  — ddl m} d|j                  › d|j                  › d|j                  › d|j
                  › d�	}d| j                  j                  › d	�}| j                  j                  |d
z   |z   d|j                  |j                  xs i |j                  |j                  ¬«      ƒ d{  –—†  y7 Œ­w)z.
        Send a hanging request alert
        r   )Ú	AlertTypezRequest Model: `z`
API Base: `z`
Key Alias: `z`
Team Alias: `ú`z`Requests are hanging - zs+ request time`ú
ÚMedium)ÚmessageÚlevelÚ
alert_typeÚalerting_metadataÚrequest_modelr&   N)Ú1litellm.integrations.SlackAlerting.slack_alertingrN   r   r&   r'   r(   r   r   Ú
send_alertÚllm_requests_hangingrU   )r   r3   rN   Úrequest_infoÚalerting_messages        r   rB   z6AlertingHangingRequestCheck.send_hanging_request_alert�   sÕ   è ø€ õ 	Pð
 #3Ð3G×3MÑ3MÐ2Nð OØ ×)Ñ)Ð*ð +Ø!×+Ñ+Ð,ð -Ø"×-Ñ-Ð.¨að3ˆð
 6°d×6PÑ6P×6cÑ6cÐ5dÐdtÐuÐØ×(Ñ(×3Ñ3Ø$ tÑ+¨lÑ:ØØ ×5Ñ5Ø2×DÑDÒJÈØ.×4Ñ4Ø)×2Ñ2ð 4ó 
÷ 	
ò 	
ús   ‚B.B8Â0B6Â1B8)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r.   r4   rF   rL   r   rB   © r   r   r   r      sD   „ ñð
à,ó
ð" %)ñ!à˜T‘kó!òF9òvSð
à0ô
r   r   )r_   rJ   r@   Útypingr   r   r   r/   Úlitellm._loggingr   Úlitellm.caching.in_memory_cacher   Ú'litellm.litellm_core_utils.core_helpersr   Ú)litellm.types.integrations.slack_alertingr	   r
   r   rW   r   r   r`   r   r   ú<module>rf      sG   ðñó Û ß ,Ñ ,ã Ý 1Ý 9Ý T÷ñ ñ ÞOà€M÷X
ò X
r   