
    iZ                      ~    d Z ddlZddlZddlZddlmZmZmZ ddl	m
Z
 ddlmZ d Zddeded	efd
Zdeded	efdZdS )a2  Interactive prompt callbacks for terminal_tool integration.

These bridge terminal_tool's interactive prompts (clarify, sudo, approval)
into prompt_toolkit's event loop. Each function takes the HermesCLI instance
as its first argument and uses its state (queues, app reference) to coordinate
with the TUI.
    N)cprint_DIM_RST)save_env_value_secure)display_hermes_homec                 :   ddl m} |                    di                               dd          }t          j                    }| }||s|ng d|d| _        t          j                    |z   | _        || _	        t          | d          r | j        r| j                                         	 	 |                    d	
          }d| _        |S # t          j        $ rV | j        t          j                    z
  }|dk    rY n7t          | d          r | j        r| j                                         Y nw xY wd| _        d| _	        d| _        t          | d          r | j        r| j                                         t          dt           d| dt                       	 dS )zPrompt for clarifying question through the TUI.

    Sets up the interactive selection UI, then blocks until the user
    responds. Returns the user's choice or a timeout message.
    r   
CLI_CONFIGclarifytimeoutx   )questionchoicesselectedresponse_queue_appT   r   NF
z(clarify timed out after u   s — agent will decide)zrThe user did not provide a response within the time limit. Use your best judgement to make the choice and proceed.)clir
   getqueueQueue_clarify_state_time	monotonic_clarify_deadline_clarify_freetexthasattrr   
invalidateEmptyr   r   r   )	r   r   r   r
   r   r   is_open_endedresult	remainings	            9/home/ubuntu/.hermes/hermes-agent/hermes_cli/callbacks.pyclarify_callbackr&      s    nnY++//	3??G[]]NKM "/777R(	 C "O--7C)CsF  
&		&#'''22F$%C!M{ 	& 	& 	&-0A0AAIA~~sF## & &##%%%	&
& C!CCsF  
VVVwVVPTVVWWW	B s   +C
 
0D/<0D/.D/var_namepromptreturnc           	         t          | dd          st          | d          sd| _        t          | d          sd| _        	 t	          j        | d          }n# t
          t          f$ r d}Y nw xY w|s(t          dt           d	t                      d
d|dd
ddS t          ||          }t                      }t          dt           d| d| t                      i |dddS d}t          j                    }|||pi |d| _        t          j                    |z   | _        t          | d          r&	 |                                  nW# t"          $ r Y nKw xY wt          | d          r7| j        r0	 | j        j                                         n# t"          $ r Y nw xY wt          | d          r | j        r| j                                         	 	 |                    d          }d| _        d| _        t          | d          r | j        r| j                                         |s(t          dt           d	t                      d
d|dd
ddS t          ||          }t                      }t          dt           d| d| t                      i |dddS # t          j        $ rV | j        t          j                    z
  }	|	dk    rY n8t          | d          r | j        r| j                                         Y nw xY w4d| _        d| _        t          | d          r&	 |                                  nW# t"          $ r Y nKw xY wt          | d          r7| j        r0	 | j        j                                         n# t"          $ r Y nw xY wt          | d          r | j        r| j                                         t          dt           dt                      d
d|dd
ddS )zPrompt for a secret value through the TUI (e.g. API keys for skills).

    Returns a dict with keys: success, stored_as, validated, skipped, message.
    The secret is stored in ~/.hermes/.env and never exposed to the model.
    r   N_secret_state_secret_deadliner   z' (hidden, ESC or empty Enter to skip):  r   u     ⏭ Secret entry skippedT	cancelledFzSecret setup was skipped.)successreason	stored_as	validatedskippedmessageu     ✓ Stored secret in z	/.env as zFSecret stored securely. The secret value was not exposed to the model.)r3   r4   r   )r'   r(   metadatar   _clear_secret_input_bufferr   r   u*     ⏱ Timeout — secret capture cancelledr   z'Secret setup timed out and was skipped.)getattrr   r+   r,   getpassEOFErrorKeyboardInterruptr   r   r   r   r   r   r   r   r   r6   	Exceptionr   current_bufferresetr    r   r!   )
r   r'   r(   r5   valuestored_dhhr   r   r$   s
             r%   prompt_for_secretrA   B   s(    3%% 
sO,, 	% $Cs.// 	%#$C 	Ov$V$V$VWWEE+, 	 	 	EEE	  		>>>>>???%%"6   'x77"$$PDPPPPP$PPQQQ

_
 
 
 	
 G[]]N N(	 C !?,,w6Cs011 		**,,,, 	 	 	D		f		 #( 	H#))++++ 	 	 	D	 sF   &	&"&&q&11E $C#$C sF## & &##%%% 	BDBBDBBCCC#)!)!&#:   +8U;;F&((DTTTTTTHTdTTUUU c   
 { 	& 	& 	&,u/@/@@IA~~sF## & &##%%%	&7 &D CCs011 		**,,,, 	 	 	D		f		 #( 	H#))++++ 	 	 	D	sF  
FFFFFGGG<  sz   A A/.A/6E 
EE3F 
FFA=J A
J 0L0LL&L; ;
MM#N 
NNcommanddescriptionc                    t          | dd          }|$ddl}|                                | _        | j        }|5  ddlm} |                    di                               dd          }t          j                    }g d}t          |          d	k    r|
                    d
           |||d|d| _        t          j                    |z   | _        t          | d          r | j        r| j                                         	 	 |                    d          }	d| _        d| _        t          | d          r | j        r| j                                         |	cddd           S # t          j        $ rV | j        t          j                    z
  }
|
dk    rY n7t          | d          r | j        r| j                                         Y nw xY wd| _        d| _        t          | d          r | j        r| j                                         t'          dt(           dt*                      	 ddd           dS # 1 swxY w Y   dS )a  Prompt for dangerous command approval through the TUI.

    Shows a selection UI with choices: once / session / always / deny.
    When the command is longer than 70 characters, a "view" option is
    included so the user can reveal the full text before deciding.

    Uses cli._approval_lock to serialize concurrent requests (e.g. from
    parallel delegation subtasks) so each prompt gets its own turn.
    _approval_lockNr   r	   	approvalsr   <   )oncesessionalwaysdenyF   view)rB   rC   r   r   r   r   Tr   r   r   u!     ⏱ Timeout — denying commandrK   )r7   	threadingLockrE   r   r
   r   r   r   lenappend_approval_stater   r   _approval_deadliner   r   r    r!   r   r   r   )r   rB   rC   lockrN   r
   r   r   r   r#   r$   s              r%   approval_callbackrU      s    3($//D|&^^--!	 ( (""""""..b1155iDD777w<<"NN6""" &,
 
 "'!2!2W!<3 	"CH 	"H!!!	**'++A+66&*#)*&3'' *CH *H'')))7( ( ( ( ( ( ( (8 ; * * *2U_5F5FF	>>E3'' *CH *H'')))*	* #!"3 	"CH 	"H!!!ADAA4AABBBQ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( (s>   C	H:AE&&0GH:0GH:
GA"H::H>H>)N)__doc__r   timer   r8   hermes_cli.bannerr   r   r   hermes_cli.configr   hermes_constantsr   r&   strdictrA   rU        r%   <module>r_      s           0 0 0 0 0 0 0 0 0 0 3 3 3 3 3 3 0 0 0 0 0 0- - -`u uS u# u u u u up8C 8c 8c 8 8 8 8 8 8r^   