
    sjc                        d Z ddlZddlmZmZ ddlmZmZ dZdZ	dZ
	 	 	 	 	 	 	 	 	 d2d	ed
ee         dee         dee         dee         dee         dee         dee         dee         defdZddddddddddd ee
          ddddZdddd ee          d dd ee	          d!ddd"ddd#ddd$dd ee
          d%dd&ed'd(d)d*dd+d	gd,d-Z ej        dd.ed/ d01           dS )3u  Run a guided tour (highlight + narrate UI elements) in the Hermes desktop GUI.

One generic tool, no baked-in tour definitions: the agent discovers what is on
screen (``action="targets"``), then highlights any element by CSS selector with
its own title/text — either one step at a time (``show``, agent-paced) or as a
full step list the user pages through with Next/Prev (``start``).

Two surfaces share the same engine (driver.js in the renderer):

- ``surface="app"`` — the Hermes desktop app's own DOM (tours of Hermes itself).
- ``surface="preview"`` — the page loaded in the in-app browser/preview pane
  (tours of ANY web app, e.g. a project open via open_preview).

Round-trips through the gateway's blocking-prompt bridge like ``read_preview``:
tui_gateway emits ``tour.request``, the renderer drives driver.js (injecting it
into the preview's webview when needed) and answers ``tour.respond`` with the
outcome, so the agent knows whether the selector matched. This module is just
schema + a thin dispatcher over the platform-injected callback.

Lives in the ``desktop_ui`` toolset, which the GUI gateway enables only for
desktop-sourced sessions.
    N)CallableOptional)registry
tool_error)targetsshowstartnextprevstop)apppreview)toprightbottomleft actionsurfaceselectortitletextsidesteps
step_indexcallbackreturnc	           
      "   |t          d          S | pd                                                                }	|	t          vr+t          dd                    t                     d          S |pd                                                                }
|
t
          vr+t          dd                    t
                     d          S |4|t          vr+t          d	d                    t                     d          S d
t          dt          fd}|	dk    r ||||d          rt          d          S |	dk    rt          |t                    r|st          d          S t          |          D ]O\  }}t          |t                    st          d| d          c S  ||          rt          d| d          c S Pd d|	fd|
fd|fd|fd|fd|fd|fd|ffD             }	  ||          }n)# t          $ r}t          d|           cY d}~S d}~ww xY w|st          d          S 	 t          j        t          j        |          d !          S # t           t"          f$ r( t          j        dt%          |          id !          cY S w xY w)"zHDispatch one tour action to the desktop renderer and return its outcome.Nz1tour is only available in the Hermes desktop app.r   zaction must be one of: z, .r   zsurface must be one of: zside must be one of: stepr   c                     |                      d          p)|                      d          p|                      d           S )Nr   r   r   )get)r    s    3/home/agent/.hermes/hermes-agent/tools/tour_tool.py_emptyztour_tool.<locals>._empty>   s9    HHZ((QDHHW,=,=Q&AQAQRR    r   )r   r   r   z:show needs a selector (and/or title/text for the popover).r	   z$start needs a non-empty steps array.zsteps[z] must be an object.z%] needs a selector and/or title/text.c                     i | ]
\  }}|||S )N ).0keyvals      r#   
<dictcomp>ztour_tool.<locals>.<dictcomp>M   s/       C ? 	S ??r%   r   r   r   r   r   r   r   r   zTour action failed: zsThe tour request timed out, or no GUI window answered. For surface='preview' open a page in the preview pane first.F)ensure_ascii)r   striplowerACTIONSjoinSURFACESSIDESdictbool
isinstancelist	enumerate	Exceptionjsondumpsloads	TypeError
ValueErrorstr)r   r   r   r   r   r   r   r   r   verbwherer$   ir    payloadrawexcs                    r#   	tour_toolrE   #   sS    MNNNLb!!''))D7IDIIg4F4FIIIJJJ$$&&,,..EHKTYYx5H5HKKKLLLD--E$))E2B2BEEEFFFST Sd S S S S v~~&&hPT!U!UVV~VWWWw%&& 	Fe 	FDEEE '' 	U 	UGAtdD)) D!"B1"B"B"BCCCCCvd|| U!"S1"S"S"STTTTTU  t"eTNTNe:&	
  G8hw 8 8 8666777777778  
K
 
 	
Bz$*S//>>>>z" B B Bz63s88,5AAAAAABs0   &G2 2
H<HHH-'I 6JJobjectstringzZCSS selector of the element this step highlights. Omit for a centered narration-only step.)typedescriptionzPopover title.zPopover body text.z+Preferred popover side. Omit to auto-place.)rH   enumrI   )r   r   r   r   )rH   
propertiestouru  Give a live guided tour in the Hermes desktop GUI: dim the screen, highlight an element, and attach a popover with your own title/text. Works on two surfaces — 'app' (the Hermes app itself) and 'preview' (whatever page is open in the in-app browser, so any web app can be toured). ALWAYS call action='targets' first to discover what is on screen instead of guessing selectors; each target reports `stable: true` when its selector keys off identity (data-tour, id, data-testid, aria-label) and survives a re-render — prefer those, and re-scan if a selector stops matching. Then either narrate at your own pace with action='show' (one highlight per call — replaces the previous one; pair each with a chat message describing it), or hand control to the user with action='start' + a steps array (driver.js renders Next/Prev buttons; 'next'/'prev' also page it programmatically). action='stop' clears the tour. Use when the user asks how something works, where something is, or for a walkthrough of an app or workflow.ztargets: list tourable elements. show: highlight one element. start: begin a multi-step user-paced tour. next/prev: page a started tour. stop: end the tour.zkWhere the tour runs: 'app' (Hermes desktop UI, default) or 'preview' (the page in the in-app browser pane).zFor show: CSS selector of the element to highlight (from action='targets', preferring a stable one). Omit for a centered narration popover.zFor show: popover title.zFor show: popover body text.z5For show: preferred popover side. Omit to auto-place.arrayz"For start: the ordered tour steps.)rH   itemsrI   integerz2For start: 0-indexed step to begin at (default 0).)r   r   r   r   r   r   r   r   )rH   rK   required)namerI   
parameters
desktop_uic                    t          |                     dd          |                     d          |                     d          |                     d          |                     d          |                     d          |                     d          |                     d	          |                    d
          	  	        S )Nr   r   r   r   r   r   r   r   r   r   )	r   r   r   r   r   r   r   r   r   )rE   r"   )argskws     r#   <lambda>rW      s    yxx"%%##*%%hhwXXfXXfhhw88L))
##
  
  
  r%   u   🧭)rQ   toolsetschemahandleremoji)	r   NNNNNNNN)__doc__r9   typingr   r   tools.registryr   r   r/   r1   r2   r>   r6   intrE   _STEP_SCHEMATOUR_SCHEMAregisterr'   r%   r#   <module>rc      s   .  % % % % % % % % / / / / / / / /
>* !"  $#'HB HBHBc]HB smHB C=	HB
 3-HB 3-HB D>HB HB x HB 	HB HB HB HBX  w
 
 #3CDD!2FGGDKKH
 
  $ 	$  !W  ~  !X  M  !  m  '7QRR%6TUU UV   %C  "S 7
 
@ JE# #)8 8v  	
 
      r%   