
    6j?                       U d Z ddlmZ ddlZddlZddlZddlmZ dZ ej	                    Z
i Zded<   i Zded	<   i Zd
ed<   i Zded<    G d d          Zd.dZd/dZd.dZd0dZd1dZd2d!Zd3d#Zd4d%Zd5d&Zd6d'Zd7d)Zd8d9d-ZdS ):zClarify prompt state for the WebUI.

This mirrors the approval flow structure, but the response is a free-form
clarification string instead of an approval decision.
    )annotationsN)Optionalx   zdict[str, dict]_pendingzdict[str, list]_gateway_queueszdict[str, object]_gateway_notify_cbszdict[str, list[queue.Queue]]_clarify_sse_subscribersc                      e Zd ZdZdZddZdS )_ClarifyEntryz-One pending clarify request inside a session.)eventdataresultr   dictc                R    t          j                    | _        || _        d | _        d S N)	threadingEventr   r   r   )selfr   s     !/root/hermes-webui/api/clarify.py__init__z_ClarifyEntry.__init__   s#    _&&
	%)    N)r   r   )__name__
__module____qualname____doc__	__slots__r    r   r   r   r      s3        77+I* * * * * *r   r   session_keystrreturnNonec                Z    t           5  |t          | <   ddd           dS # 1 swxY w Y   dS )zGRegister a per-session callback for sending clarify requests to the UI.N)_lockr   )r   cbs     r   register_gateway_notifyr%   $   sw    	 . .+-K(. . . . . . . . . . . . . . . . . .s    $$list[_ClarifyEntry]c                r    t                               | g           }t                              | d            |S r   )r   popr   )r   entriess     r   _clear_queue_lockedr*   *   s0    !!+r22GLLd###Nr   c                    t           5  t                              | d           t          |           }ddd           n# 1 swxY w Y   |D ]}|j                                         dS )zKUnregister the per-session callback and unblock any waiting clarify prompt.N)r#   r   r(   r*   r   setr   r)   entrys      r   unregister_gateway_notifyr/   0   s    	 3 3T222%k223 3 3 3 3 3 3 3 3 3 3 3 3 3 3    s   +?AAintc                    t           5  t          |           }ddd           n# 1 swxY w Y   |D ]}|j                                         t	          |          S )zPClear any pending clarify prompts for the session without removing the callback.N)r#   r*   r   r,   lenr-   s      r   clear_pendingr3   9   s    	 3 3%k223 3 3 3 3 3 3 3 3 3 3 3 3 3 3  w<<s   $((r   r   c                P   t          | pi           }t          |                    d          pt          j                              }t	          |                    d          pt
                    }t          |                    d          p||z             }||d<   ||d<   ||d<   |S )Nrequested_attimeout_seconds
expires_at)r   floatgettimer0   DEFAULT_TIMEOUT_SECONDS)r   itemr5   r6   r7   s        r   _with_timeout_metadatar=   B   s    
D.11@TY[[AAL$((#455P9PQQOtxx--O1OPPJ'D-D	#DKr   
session_idheaddict | Nonetotalc                    |rt          |          nd|d}t                              | d          D ]-}	 |                    |           # t          j        $ r Y *w xY wdS )z:Push a clarify event to all SSE subscribers for a session.N)pendingpending_countr   )r   r	   r9   
put_nowaitqueueFull)r>   r?   rA   payloadqs        r   _clarify_sse_notifyrJ   M   s    (,6$t***$OOG%))*b99  	LL!!!!z 	 	 	D	 s   AAAqueue.Queuec                    t          j        d          }t          5  t                              | g                               |           ddd           n# 1 swxY w Y   |S )z9Register a bounded Queue for SSE push to a given session.   )maxsizeN)rF   Queuer#   r	   
setdefaultappend)r>   rI   s     r   sse_subscriberR   W   s    [,,,A	 F F ++J;;BB1EEEF F F F F F F F F F F F F F FHs   /AAArI   c                   t           5  t                              |           }|rD	 |                    |           n# t          $ r Y nw xY w|st                              | d           ddd           dS # 1 swxY w Y   dS )z:Remove a subscriber Queue; clean up empty session entries.N)r#   r	   r9   remove
ValueErrorr(   )r>   rI   subss      r   sse_unsubscriberW   _   s    	 ? ?'++J77 	?A    ?(,,Z>>>? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?s1   A6<A6
A	A6A		 A66A:=A:c                   t          |          }t          5  t                              | g           }|r|d         }t	          |j                            dd                    t	          |                    dd                    k    rt          |j                            d          pg           t          |                    d          pg           k    rp|}t                              |           }|d         j        t          | <   |r/	  |t          |j                             n# t          $ r Y nw xY w|cddd           S t          |          }|                    |           |d         j        t          | <   t                              |           }t          | t          |d         j                  t          |                     ddd           n# 1 swxY w Y   |r	  ||           n# t          $ r Y nw xY w|S )zIQueue a pending clarify request and notify the UI callback if registered.question choices_offeredr   N)r=   r#   r   rP   r   r   r9   listr   r   r   	Exceptionr   rQ   rJ   r2   )r   r   gw_queuelastr.   r$   s         r   submit_pendingra   l   sy   !$''D	 P P"--k2>>  	B<DDIMM*b1122c$((:r:R:R6S6SSS'899?R@@!2339r::; ; (,,[99(0(8% 4
++,,,,$   )P P P P P P P P, d## ( 0 $$[11Khqk.>)?)?XOOO7P P P P P P P P P P P P P P P8 
 	BtHHHH 	 	 	D	LsO   C6GD,+G,
D96G8D99G
B	GG#&G#-G9 9
HHc                &   t           5  t                              |           pg }|r&t          |d         j                  cddd           S t
                              |           }|rt          |          ndcddd           S # 1 swxY w Y   dS )zCReturn the oldest pending clarify request for this session, if any.r   N)r#   r   r9   r   r   r   )r   rF   rC   s      r   get_pendingrc      s    	 2 2##K006B 	'a&&2 2 2 2 2 2 2 2 ,,{++ '1tG}}}T2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2s   8B,BB
B
boolc                    t           5  t          t                              |                     cd d d            S # 1 swxY w Y   d S r   )r#   rd   r   r9   )r   s    r   has_pendingrf      s    	 6 6O''44556 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6s   '<A A Fresponseresolve_allc                P   t           5  t                              |           }|s)t                              | d           	 ddd           dS |rt          |          n|                    d          g}|rL|d         j        t          | <   t          | t          |d         j                  t          |                     n t          |            t          | dd           ddd           n# 1 swxY w Y   d}|D ]'}||_        |j                                         |dz  }(|S )z9Resolve the oldest pending clarify request for a session.Nr      )r#   r   r9   r   r(   r]   r   rJ   r   r2   r*   r   r   r,   )r   rg   rh   rI   r)   countr.   s          r   resolve_clarifyrl      sw   	 6 6,, 	LLd+++	6 6 6 6 6 6 6 6
 )8$q'''quuQxxj 	6$%aDIH[!T!A$)__c!ffEEEE,,,T15556 6 6 6 6 6 6 6 6 6 6 6 6 6 6 E  
Ls   9C/BC//C36C3)r   r   r    r!   )r   r   r    r&   )r   r   r    r0   )r   r   r    r   )r>   r   r?   r@   rA   r0   r    r!   )r>   r   r    rK   )r>   r   rI   rK   r    r!   )r   r   r   r   r    r   )r   r   r    r@   )r   r   r    rd   )F)r   r   rg   r   rh   rd   r    r0   )r   
__future__r   rF   r   r:   typingr   r;   Lockr#   r   __annotations__r   r   r	   r   r%   r*   r/   r3   r=   rJ   rR   rW   ra   rc   rf   rl   r   r   r   <module>rq      s     # " " " " "              	    #% % % % %)+  + + + + :<  ; ; ; ;* * * * * * * *. . . .                  
? 
? 
? 
?$ $ $ $N2 2 2 26 6 6 6
      r   