Ë
    uPêj  ã                   ó¬   — d dl 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
 d dlmZmZ  G d„ de j                  «      Z G d	„ d
e«      Z G d„ d«      Zy)é    N)ÚFinal)Ú	BaseModel)Úprint_verbose)Ú	DualCacheÚ
RedisCache)ÚDEFAULT_IN_MEMORY_TTLÚDEFAULT_POLLING_INTERVALc                   ó   — e Zd ZdZeZy)ÚSchedulerCacheKeyszscheduler:queueN)Ú__name__Ú
__module__Ú__qualname__Úqueuer   Údefault_in_memory_ttl© ó    úV/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/litellm/scheduler.pyr   r      s   „ Ø€EØ1Ñr   r   c                   ó,   — e Zd ZU eed<   eed<   eed<   y)ÚFlowItemÚpriorityÚ
request_idÚ
model_nameN)r   r   r   ÚintÚ__annotations__Ústrr   r   r   r   r      s   … ØƒMØƒOØ„Or   r   c                   ó´   — e Zd ZU eed<   	 	 ddedz  dedz  fd„Zdefd„Z	de
d	e
d
edefd„Zde
d	e
ddfd„Zde
d	e
d
edefd„Zd„ Zd	e
defd„Zded	e
ddfd„Zy)Ú	SchedulerÚcacheNÚpolling_intervalÚredis_cachec                 ó�   — g | _         d}|�t        j                  j                  }t	        ||¬«      | _        |xs t        | _        y)z_
        polling_interval: float or null - frequency of polling queue. Default is 3ms.
        N)r    r   )r   r   r   Úvaluer   r   r	   r   )Úselfr   r    r   s       r   Ú__init__zScheduler.__init__   sF   € ð ˆŒ
Ø.2ÐØÐ"ä$6×$LÑ$L×$RÑ$RÐ!Ü¨;ÐNcÔdˆŒ
Ø 0Ò LÔ4LˆÕr   Úrequestc              ƒ   óþ   K  — | j                  |j                  ¬«      ƒ d {  –—† }t        j                  ||j                  |j
                  f«       | j                  ||j                  ¬«      ƒ d {  –—†  y 7 ŒV7 Œ­w)N©r   ©r   r   )Ú	get_queuer   ÚheapqÚheappushr   r   Ú
save_queue)r#   r%   r   s      r   Úadd_requestzScheduler.add_request*   sk   è ø€ ð "Ÿ^™^°w×7IÑ7I˜^ÓJ×Jˆä�‰�u˜w×/Ñ/°×1CÑ1CÐDÔEð �o‰o E°g×6HÑ6HˆoÓI×IÑIð Køð
 	Jús"   ‚ A=¢A9£AA=Á3A;Á4A=Á;A=Úidr   Úhealth_deploymentsÚreturnc              ƒ   ój  K  — | j                  |¬«      ƒ d{  –—† }|st        d|› �«      ‚t        dt        |«      › �«       t        |«      dk(  r\t        d|› d|› �«       |d   d   |k(  r?t	        j
                  |«       | j                  ||¬	«      ƒ d{  –—†  t        d
|› �«       yyy7 Œ–7 Œ­w)a/  
        Return if request can be processed.

        Returns:
        - True:
            * If healthy deployments are available
            * OR If request at the top of queue
        - False:
            * If no healthy deployments available
            * AND request not at the top of queue
        r'   Nú+Incorrectly setup. Queue is invalid. Queue=zlen(health_deployments): r   zqueue: z, seeking id=é   r(   zPopped id: TF)r)   Ú	Exceptionr   Úlenr*   Úheappopr,   ©r#   r.   r   r/   r   s        r   ÚpollzScheduler.poll4   sÄ   è ø€ ð "Ÿ^™^°z˜^ÓB×BˆÙÜÐIÈ%ÈÐQÓRÐRô 	Ð1´#Ð6HÓ2IÐ1JÐKÔLÜÐ!Ó" aÒ'Ü˜G E 7¨-¸°tÐ<Ô=à�Q‰x˜‰{˜bÒ ä—‘˜eÔ$Ø—o‘o¨E¸j�oÓI×IÐIÜ ¨B¨4Ð0Ô1Øààð+ Cøð Jús"   ‚B3˜B/™B B3ÂB1ÂB3Â1B3r   c              ƒ   ó  K  — | j                  |¬«      ƒ d{  –—† }|D �cg c]  }|d   |k7  sŒ|‘Œ }}t        j                  |«       | j                  ||¬«      ƒ d{  –—†  t	        d|› d|› �«       y7 Œ^c c}w 7 Œ­w)z’
        Remove a specific request from the priority queue for a model.
        Used when a request times out while waiting in the queue.
        r'   Nr3   r(   zRemoved request_id: z from queue for model: )r)   r*   Úheapifyr,   r   )r#   r   r   r   ÚitemÚfiltered_queues         r   Úremove_requestzScheduler.remove_requestW   s   è ø€ ð
 "Ÿ^™^°z˜^ÓB×BˆØ27Ö Q¨$¸4À¹7ÀjÓ;P¢Ð QˆÐ QÜ�‰�nÔ%Ø�o‰o N¸zˆoÓJ×JÐJÜÐ,¨Z¨LÐ8OÐPZÈ|Ð\Õ]ð	 CúÚ QàJús0   ‚B ˜A7™B ¡A9¯A9³-B Á A>Á!B Á9B c              ƒ   ó|   K  — | j                  |¬«      ƒ d{  –—† }|st        d|› �«      ‚|d   d   |k(  ryy7 Œ!­w)zKReturn if the id is at the top of the queue. Don't pop the value from heap.r'   Nr2   r   r3   TF)r)   r4   r7   s        r   ÚpeekzScheduler.peekb   sM   è ø€ à!Ÿ^™^°z˜^ÓB×BˆÙÜÐIÈ%ÈÐQÓRÐRð �‰8�A‰;˜"ÒØàð Cús   ‚<˜:™"<c                 ó   — | j                   S )z$Get the status of items in the queue)r   )r#   s    r   Úget_queue_statuszScheduler.get_queue_statusr   s   € à�z‰zÐr   c              ƒ   ó  K  — | j                   �it        j                  j                  › d|› �}| j                   j	                  |¬«      ƒ d{  –—† }|�t        |t        «      sg S t        |t        «      r|S | j                  S 7 Œ6­w)z>
        Return a queue for that specific model group
        Nú:)Úkey)r   r   r   r"   Úasync_get_cacheÚ
isinstanceÚlist)r#   r   Ú
_cache_keyÚresponses       r   r)   zScheduler.get_queuev   s|   è ø€ ð �:‰:Ð!Ü#5×#;Ñ#;×#AÑ#AÐ"BÀ!ÀJÀ<Ð PˆJØ$(§J¡J×$>Ñ$>À:Ð$>Ó$N×NˆHØÐ¤z°(¼DÔ'AØ�	Ü˜H¤dÔ+Ø�Ø�z‰zÐð Oús   ‚ABÁBÁ7Br   c              ƒ   ó²   K  — | j                   �Et        j                  j                  › d|› �}| j                   j	                  ||¬«      ƒ d{  –—†  yy7 Œ­w)z;
        Save the updated queue of the model group
        NrC   )rD   r"   )r   r   r   r"   Úasync_set_cache)r#   r   r   rH   s       r   r,   zScheduler.save_queueƒ   sT   è ø€ ð �:‰:Ð!Ü#5×#;Ñ#;×#AÑ#AÐ"BÀ!ÀJÀ<Ð PˆJØ—*‘*×,Ñ,°À5Ð,ÓI×IÑIð "àIús   ‚AAÁAÁA)NN)r   r   r   r   r   Úfloatr   r$   r   r-   r   rG   Úboolr8   r=   r?   rA   r)   r,   r   r   r   r   r      sÚ   … ØÓð *.Ø)-ñMà $™,ðMð   $Ñ&óMð J¨ó Jð!˜Sð !¨cð !Àtð !ÐPTó !ðF	^¨sð 	^Àð 	^Èó 	^ð˜Sð ¨cð Àtð ÐPTó ò ð¨#ð °$ó ðJ dð J¸ð JÀô Jr   r   )Úenumr*   Útypingr   Úpydanticr   Úlitellmr   Úlitellm.caching.cachingr   r   Úlitellm.constantsr   r	   ÚEnumr   r   r   r   r   r   ú<module>rU      sD   ðÛ Û Ý å å !ß 9ß Mô2˜Ÿ™ô 2ô
ˆyô ÷rJò rJr   