
    i|                        U d Z ddlm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  ej        e          Zddd	d
ddgddd
ddd
ddddgddZded<   	 	 	 d%d&dZd'd Z ej        dd!ed" ed#$           dS )(u1  Agent-facing tool: respond to a native JS dialog captured by the CDP supervisor.

This tool is response-only — the agent first reads ``pending_dialogs`` from
``browser_snapshot`` output, then calls ``browser_dialog(action=...)`` to
accept or dismiss.

Gated on the same ``_browser_cdp_check`` as ``browser_cdp`` so it only
appears when a CDP endpoint is reachable (Browserbase with a
``connectUrl``, local Chrome via ``/browser connect``, or
``browser.cdp_url`` set in config).

See ``website/docs/developer-guide/browser-supervisor.md`` for the full
design.
    )annotationsN)AnyDictOptional)SUPERVISOR_REGISTRY)registrybrowser_dialogu  Respond to a native JavaScript dialog (alert / confirm / prompt / beforeunload) that is currently blocking the page.

**Workflow:** call ``browser_snapshot`` first — if a dialog is open, it appears in the ``pending_dialogs`` field with ``id``, ``type``, and ``message``. Then call this tool with ``action='accept'`` or ``action='dismiss'``.

**Prompt dialogs:** pass ``prompt_text`` to supply the response string. Ignored for alert/confirm/beforeunload.

**Multiple dialogs:** if more than one dialog is queued (rare — happens when a second dialog fires while the first is still open), pass ``dialog_id`` from the snapshot to disambiguate.

**Availability:** only present when a CDP-capable backend is attached — Browserbase sessions, local Chrome via ``/browser connect``, or ``browser.cdp_url`` in config.yaml. Not available on Camofox (REST-only) or the default Playwright local browser (CDP port is hidden).objectstringacceptdismissz'accept' clicks OK / returns the prompt text. 'dismiss' clicks Cancel / returns null from prompt(). For ``beforeunload`` dialogs: 'accept' allows the navigation, 'dismiss' keeps the page.)typeenumdescriptionzdResponse string for a ``prompt()`` dialog. Ignored for other dialog types. Defaults to empty string.)r   r   z~Specific dialog to respond to, from ``browser_snapshot.pending_dialogs[].id``. Required only when multiple dialogs are queued.actionprompt_text	dialog_idr   )r   
propertiesrequired)namer   
parameterszDict[str, Any]BROWSER_DIALOG_SCHEMAstrr   Optional[str]r   task_idreturnc                l   |pd}t          j        |          }|t          j        ddd          S |                    | ||          }|                    d          r,t          j        d| |                    d	i           d
          S t          j        d|                    dd          d          S )z@Respond to a pending dialog on the active task's CDP supervisor.defaultNFzNo CDP supervisor is attached to this task. Either the browser backend doesn't expose CDP (Camofox, default Playwright) or no browser session has been started yet. Call browser_navigate or /browser connect first.)successerrorr   okTdialog)r    r   r#   r!   zunknown error)r   getjsondumpsrespond_to_dialog)r   r   r   r   effective_task_id
supervisorresults          >/home/ubuntu/.hermes/hermes-agent/tools/browser_dialog_tool.pyr	   r	   R   s      ,9$():;;Jz G 

 

 
	
 )) *  F
 zz$ 
z  **Xr22 
 
 	
 :%&**Wo2V2VWWXXX    boolc                     	 ddl m}  n3# t          $ r&}t                              d|           Y d}~dS d}~ww xY w |             S )uQ  Gate: same as ``browser_cdp`` — only offered when CDP is reachable.

    Kept identical so the two tools appear and disappear together. The
    supervisor itself is started lazily by ``browser_navigate`` /
    ``/browser connect`` / Browserbase session creation, so a reachable
    CDP URL is enough to commit to showing the tool.
    r   )_browser_cdp_checkz8browser_dialog check: browser_cdp_tool import failed: %sNF)tools.browser_cdp_toolr/   	Exceptionloggerdebug)r/   excs     r+   _browser_dialog_checkr5   x   so    =======   OQTUUUuuuuu s   	 
949zbrowser-cdpc                    t          |                     dd          |                     d          |                     d          |                    d                    S )Nr    r   r   r   )r   r   r   r   )r	   r$   )argskws     r+   <lambda>r:      sS    ~xx"%%HH]++((;''y!!	      r,   u   💬)r   toolsetschemahandlercheck_fnemoji)NNN)
r   r   r   r   r   r   r   r   r   r   )r   r-   )__doc__
__future__r   r%   loggingtypingr   r   r   tools.browser_supervisorr   tools.registryr   	getLogger__name__r2   r   __annotations__r	   r5   register r,   r+   <module>rK      s     # " " " " "   & & & & & & & & & & 8 8 8 8 8 8 # # # # # #		8	$	$ 	.$  !!9-<		 	 !D  != %
 
6 J; )3) 3)  3 3 3 3p "&#!	#Y #Y #Y #Y #YL          	   #
     r,   