
    `gj                          d 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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y)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)masked_secret_prompt)display_hermes_homec                 6   ddl m} |j                  di       j                  dd      }t        j                         }| }||s|ng d|d| _        t        j                         |z   | _        || _	        t        | d      r&| j                  r| j                  j                          	 	 |j                  d	      }d| _        |S # t        j                  $ r] | j                  t        j                         z
  }|dk  rY n9t        | d      r&| j                  r| j                  j                          Y nw xY wd
| _        d| _	        d| _        t        | d      r&| j                  r| j                  j                          t        dt         d| dt                 	 y)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_app   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	            G/root/.hermes/venv/lib/python3.12/site-packages/hermes_cli/callbacks.pyclarify_callbackr'      su    nnY+//	3?G[[]NKM "/7R(	C "OO-7C)CsF
		&#'''2F$%C!M{{ 	&--0AAIA~sF###%	&  C!CCsF
Rv.wi7OPTvVW	Bs   'C 9D2=2D21D2var_namepromptreturnc           	      b   t        | dd      st        | d      sd| _        t        | d      sd| _        	 t	        | d      }|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                         }|||xs i |d| _        t        j                         |z   | _        t        | d      r	 | j!                          n=t        | d      r1| j$                  r%	 | j$                  j&                  j)                          t        | d      r&| j$                  r| j$                  j+                          	 	 |j-                  d      }d| _        d| _        t        | d      r&| j$                  r| j$                  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
        t        f$ r d}Y w xY w# t"        $ r Y w xY w# t"        $ r Y w xY w# t        j.                  $ r] | j                  t        j                         z
  }	|	dk  rY n:t        | d      r&| j$                  r| j$                  j+                          Y nw xY wad| _        d| _        t        | d      r!	 | j!                          n\# t"        $ r Y nQw xY wt        | d      rA| j$                  r5	 | j$                  j&                  j)                          n# t"        $ r Y nw xY wt        | d      r&| j$                  r| j$                  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.)r4   r5   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-   r   EOFErrorKeyboardInterruptr   r   r   r   r   r   r   r   r   r7   	Exceptionr   current_bufferresetr!   r   r"   )
r   r(   r)   r6   valuestored_dhhr   r   r%   s
             r&   prompt_for_secretrA   B   s    3%sO, $Cs./#$C 	(F83Z)[\E Rv7v>?%%"6  'x7"$D60iz$PQ

_
 	
 G[[]N N(	C !??,w6Cs01	**, 
f	#((	HH##))+ sF
	&"&&q&1E $C#$C sF###%D6!;D6BC#)!)!&#:  +8U;F&(DRv4TF)H:dVTU c M +, 	E	J  		
  		B {{ 	&,,u/@@IA~sF###%	&7 D CCs01	**, 			f	#((	HH##))+ 		sF
Rv?vFG< sx   H( 3I  $I 5A5I  +<I  (H=<H= 	II	II 9K2KK0L 	LL)$M 	MMcommanddescriptionc                 h   t        | dd      }|%ddl}|j                         | _        | j                  }|5  ddlm} |j                  di       j                  dd      }t        j                         }g d}t        |      d	kD  r|j                  d
       |||d|d| _        t        j                         |z   | _        t        | d      r&| j                   r| j                   j#                          	 	 |j                  d      }	d| _        d| _        t        | d      r&| j                   r| j                   j#                          |	cddd       S # t        j$                  $ r] | j                  t        j                         z
  }
|
dk  rY n9t        | d      r&| j                   r| j                   j#                          Y nw xY wd| _        d| _        t        | d      r&| j                   r| j                   j#                          t'        dt(         dt*                	 ddd       y# 1 sw Y   yxY w)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   r   r   r   u!     ⏱ Timeout — denying commandrK   )r8   	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55iD7w<"NN6" &,
 "'!2W!<3CHHHH!*'++A+6&*#)*&3'CHHHH'')7( (8 ;; *22U__5FF	>3'CHHHH'')*  #!"3CHHHH!D6:4&ABQ( ( (s8   B<H(4AE9G
H(2G>H( GAH((H1)N)__doc__r   timer   hermes_cli.bannerr   r   r   hermes_cli.configr   hermes_cli.secret_promptr   hermes_constantsr   r'   strdictrA   rU        r&   <module>r`      s`      0 0 3 9 0-`uS u# u up8C 8c 8c 8r_   