
    xj!                    "   U d Z ddlmZ ddl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d0dZd1dZd0dZd2dZd3dZd4d!Zd5d#Zd6d%Zd7d&Zd8d'Zd9d)Zd:d;d-Zd<d/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result
clarify_idr   dictc                    t          j                    | _        || _        d | _        |                    dd          pt          j                    j        d d         | _	        d S )Nr       )
	threadingEventr   r   r   getuuiduuid4hexr   )selfr   s     ./Users/bertmahoney/hermes-webui/api/clarify.py__init__z_ClarifyEntry.__init__   sO    _&&
	%)#xxb99RTZ\\=Mcrc=R    N)r   r   )__name__
__module____qualname____doc__	__slots__r    r   r   r   r      s9        779IS S S S S S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 N)r   popr   )r$   entriess     r   _clear_queue_lockedr1   ,   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/   r1   r   setr$   r0   entrys      r   unregister_gateway_notifyr6   2   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)   r1   r   r3   lenr4   s      r   clear_pendingr:   ;   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   floatr   timer7   DEFAULT_TIMEOUT_SECONDS)r   itemr<   r=   r>   s        r   _with_timeout_metadatarC   D   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	   r   
put_nowaitqueueFull)rD   rE   rG   payloadqs        r   _clarify_sse_notifyrP   O   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)rL   Queuer)   r	   
setdefaultappend)rD   rO   s     r   sse_subscriberX   Y   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rO   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	   r   remove
ValueErrorr/   )rD   rO   subss      r   sse_unsubscriber]   a   s    	 ? ?'++J77 	?A    ?(,,Z>>>? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?s1   A6<A6
A	A6A		 A66A:=A:c                4   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          |          }|j        |j        d<   |                    |           |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.questionr   choices_offeredr   Nr   )rC   r)   r   rV   r%   r   r   listr   r   r   	Exceptionr   r   rW   rP   r9   )r$   r   gw_queuelastr5   r*   s         r   submit_pendingrf   n   s   !$''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
++,,,,$   3"P "P "P "P "P "P "P "P6 d###(#3
<  ( 0 $$[11Khqk.>)?)?XOOOE"P "P "P "P "P "P "P "P "P "P "P "P "P "P "PF 
 	BtHHHH 	 	 	D	LsO   C6G.D,+G.,
D96G.8D99G.
BG..G25G2<H 
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   r   r   r   r   )r$   rL   rI   s      r   get_pendingrh      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)   ri   r   r   )r$   s    r   has_pendingrk      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   r   r   r/   rb   r   rP   r   r9   r1   r   r   r3   )r$   rl   rm   rO   r0   countr5   s          r   resolve_clarifyrq      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   c           	     t   t           5  t                              |           }|s)t                              | d           	 ddd           dS t          |          D ]\  }}|j        |k    r|                    |           |rL|d         j        t          | <   t          | t          |d         j                  t          |                     n t          |            t          | dd           ||_        |j                                          ddd           dS 	 ddd           dS # 1 swxY w Y   dS )zResolve a specific pending clarify request by its stable id.

    Returns True if the id was found and resolved, False otherwise.
    NFr   T)r)   r   r   r   r/   	enumerater   r   rP   r   r9   r1   r   r   r3   )r$   r   rl   rO   ir5   s         r   resolve_clarify_by_idru      s   
 
  ,, 	LLd+++	       
 "! 	 	HAu:--a >,-aDIH[)'T!A$)__c!ffMMMM'444'T1==='!!!!        . #                 s   9D-CD-D--D14D1)r$   r%   r&   r'   )r$   r%   r&   r,   )r$   r%   r&   r7   )r   r   r&   r   )rD   r%   rE   rF   rG   r7   r&   r'   )rD   r%   r&   rQ   )rD   r%   rO   rQ   r&   r'   )r$   r%   r   r   r&   r   )r$   r%   r&   rF   )r$   r%   r&   ri   )F)r$   r%   rl   r%   rm   ri   r&   r7   )r$   r%   r   r%   rl   r%   r&   ri   )r!   
__future__r   rL   r   r@   r   typingr   rA   Lockr)   r   __annotations__r   r   r	   r   r+   r1   r6   r:   rC   rP   rX   r]   rf   rh   rk   rq   ru   r#   r   r   <module>rz      s     # " " " " "               	    #% % % % %)+  + + + + :<  ; ; ; ;	S 	S 	S 	S 	S 	S 	S 	S. . . .                  
? 
? 
? 
?+ + + +\2 2 2 26 6 6 6
    ,     r   