
    `gj                       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       y)!uB  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 Chromium-family browser 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 Chromium-family browser 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_SCHEMAc                X   |xs d}t        j                  |      }|t        j                  ddd      S |j	                  | ||      }|j                  d      r)t        j                  d| |j                  di       d	      S t        j                  d|j                  d
d      d      S )z@Respond to a pending dialog on the active task's CDP supervisor.defaultFzNo 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   task_ideffective_task_id
supervisorresults          L/root/.hermes/venv/lib/python3.12/site-packages/tools/browser_dialog_tool.pyr	   r	   R   s      ,9$(():;Jzz G

 
	
 )) * F
 zz$zz  **Xr2
 	
 ::%&**Wo2VWXX    c                 v    	 ddl m}   |        S # t        $ r }t        j	                  d|       Y d}~yd}~ww xY w)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_checkr1   x   s:    =   OQTUs    	838zbrowser-cdpc                    t        | j                  dd      | j                  d      | j                  d      |j                  d            S )Nr    r   r   r$   )r   r   r   r$   )r	   r    )argskws     r(   <lambda>r6      s=    ~xx"%HH]+((;'y!	  r)   u   💬)r   toolsetschemahandlercheck_fnemoji)NNN)
r   strr   Optional[str]r   r=   r$   r=   returnr<   )r>   bool)__doc__
__future__r   r!   loggingtypingr   r   r   tools.browser_supervisorr   tools.registryr   	getLogger__name__r.   r   __annotations__r	   r1   register r)   r(   <module>rK      s   #   & & 8 #			8	$ 	.$  !!9-<		 !D !=%
6 J;)3) ~ 3p "&#!	#Y#Y#Y #Y 	#Y
 	#YL     	  #
r)   