
    Cj                         S SK r S SKrS SKJr  S SKJr  S SKJr  S SKJ	r	J
r
  S SKJrJr   " S S\ R                  5      r " S	 S
\5      r " S S5      rg)    N)Optional)	BaseModel)print_verbose)	DualCache
RedisCache)DEFAULT_IN_MEMORY_TTLDEFAULT_POLLING_INTERVALc                       \ rS rSrSr\rSrg)SchedulerCacheKeys   zscheduler:queue N)__name__
__module____qualname____firstlineno__queuer   default_in_memory_ttl__static_attributes__r       c/home/rurouni/.local/share/pipx/venvs/strix-agent/lib/python3.13/site-packages/litellm/scheduler.pyr   r      s    E r   r   c                   4    \ rS rSr% \\S'   \\S'   \\S'   Srg)FlowItem   priority
request_id
model_namer   N)r   r   r   r   int__annotations__strr   r   r   r   r   r      s    MOOr   r   c                       \ rS rSr% \\S'     SS\\   S\\   4S jjr	S\
4S jrS	\S
\S\S\4S jrS\S
\SS4S jrS	\S
\S\S\4S jrS rS
\S\4S jrS\S
\SS4S jrSrg)	Scheduler   cacheNpolling_intervalredis_cachec                     / U l         SnUb  [        R                  R                  n[	        X#S9U l        U=(       d    [        U l        g)zO
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__Scheduler.__init__   sK     
15"$6$L$L$R$R!#

 8 8 	r   requestc                    #    U R                  UR                  S9I S h  vN n[        R                  " X!R                  UR
                  45        U R                  X!R                  S9I S h  vN   g  NR N7f)Nr   r   r   )	get_queuer   heapqheappushr   r   
save_queue)r(   r+   r   s      r   add_requestScheduler.add_request0   se      nn0B0BnCCu//1C1CDE ooE6H6HoIII D
 	Js"   A6A2AA6,A4-A64A6idr   health_deploymentsreturnc                   #    U R                  US9I Sh  vN nU(       d  [        SR                  U5      5      e[        S[	        U5       35        [	        U5      S:X  aZ  [        SU SU 35        US   S   U:X  a<  [
        R                  " U5        U R                  XBS	9I Sh  vN   [        S
U 35        ggg N N7f)z
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/   	Exceptionformatr   lenr0   heappopr2   r(   r5   r   r6   r   s        r   pollScheduler.poll:   s      nn
n;;?FFuM  	1#6H2I1JKL!"a'GE7-t<=Qx{b e$ooEoIIIB401/ <" Js"   C B<BC &B>'C >C r   c                    #    U R                  US9I Sh  vN nU Vs/ s H  oDS   U:w  d  M  UPM     nn[        R                  " U5        U R                  XRS9I Sh  vN   [	        SU SU 35        g N_s  snf  N7f)zz
Remove a specific request from the priority queue for a model.
Used when a request times out while waiting in the queue.
r-   Nr:   r.   zRemoved request_id: z from queue for model: )r/   r0   heapifyr2   r   )r(   r   r   r   itemfiltered_queues         r   remove_requestScheduler.remove_request_   s{     
 nn
n;;+0J54Gz4I$5Jn%ooNoJJJ":,.Ej\R	
	 <JJs0   A>A5A>A7A7*A>A<A>7A>c                    #    U R                  US9I Sh  vN nU(       d  [        SR                  U5      5      eUS   S   U:X  a  gg N37f)zKReturn if the id is at the top of the queue. Don't pop the value from heap.r-   Nr9   r   r:   TF)r/   r;   r<   r?   s        r   peekScheduler.peekl   sS     nn
n;;?FFuM  8A;" <s   AA	4Ac                     U R                   $ )z$Get the status of items in the queue)r   )r(   s    r   get_queue_statusScheduler.get_queue_status~   s    zzr   c                 8  #    U R                   b|  SR                  [        R                  R                  U5      nU R                   R                  US9I Sh  vN nUb  [        U[        5      (       d  / $ [        U[        5      (       a  U$ U R                  $  NA7f)z.
Return a queue for that specific model group
N{}:{})key)r#   r<   r   r   r'   async_get_cache
isinstancelist)r(   r   
_cache_keyresponses       r   r/   Scheduler.get_queue   s      ::! (:(@(@(F(F
SJ!ZZ77J7GGHz(D'A'A	Hd++zz Hs   ABBABr   c                    #    U R                   bK  SR                  [        R                  R                  U5      nU R                   R                  X1S9I Sh  vN   g N7f)z+
Save the updated queue of the model group
NrO   )rP   r'   )r#   r<   r   r   r'   async_set_cache)r(   r   r   rT   s       r   r2   Scheduler.save_queue   sR      ::! (:(@(@(F(F
SJ**,,,III Js   AAAA)r#   r$   r   )NN)r   r   r   r   r   r   r   floatr   r)   r   r3   r   rS   boolr@   rF   rI   rL   r/   r2   r   r   r   r   r!   r!      s     -1,0
"5/
 j)
(J J#S #c #t #PT #J
s 
 
 
S c t PT $# $ d   r   r!   )enumr0   typingr   pydanticr   litellmr   litellm.caching.cachingr   r   litellm.constantsr   r	   Enumr   r   r!   r   r   r   <module>rc      sB        ! 9 M y } }r   