
    Ki;                       U d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddlZddlmZmZmZmZ ddlmZ ddlmZ 	 ddlmZ n# e$ r d ZY nw xY w	 ddlmZ n# e$ r d	 ZY nw xY wdd
lmZ ddlm Z  ddl!m"Z" ddl#m$Z$ 	 ddl%m&Z' n# e($ r d Z'Y nw xY w ej)        e*          Z+dZ,de-e.         fdZ/i Z0e1e.e2f         e3d<   dZ4dZ5dZ6de7fdZ8dee.         fdZ9dee.         fdZ:de.de.fdZ;de.fdZ<e e"dZ=ee.e>f         e3d<   da?ee         e3d<   d a@d aAdaBeeC         e3d!<   dee         fd"ZDdeee.ef                  fd#ZEdee.ef         fd$ZFdeCfd%ZGdeCfd&ZHdeCfd'ZIde.fd(ZJi ZKee.ee.e.f         f         e3d)<    eL            ZMeLe3d*<   d aN e7ejO        P                    d+d,                    ZQi ZRee.e2f         e3d-<   daSd aT ejU                    ZVd. ZW ejX        eW           d/ ZYd0 ZZd1 Z[d2 Z\d3e.fd4Z] ejX        e\           d5d6d7d8d9d:d;id8gd<d=d>d?d7d@dAdBd dCig d<d=dDdEd7dFd9dGd;idFgd<d=dHdId7d9dJd;d9dKd;dLdFdMgd<d=dNdOd7dPd9dQdRgdSdTidPgd<d=dUdVd7i g d<d=dWdXd7dYd9dZd;idYgd<d=d[d\d7i g d<d=d]d^d7i g d<d=d_d`d7d9dad;dAd dbdcdddegd<d=dfdgd7dhdAd didcig d<d=gZ^d3e.dee.e.f         fdjZ_d3e.de.dee.e.f         fdkZ`dd3ee.         dee.e.f         fdlZade.fdmZbdMe.dee.         fdnZc	 	 dd3e.doe.dpee.         dqee7         dee.ef         f
drZd	 ddse.dtee.         de.fduZeddse.dve7de.fdwZfdd8e.d3ee.         de.fdxZg	 	 	 dd@eCd3ee.         dtee.         de.fdyZhddFe.d3ee.         de.fdzZiddFe.dMe.d3ee.         de.fd{ZjddPe.d3ee.         de.fd|Zkdd3ee.         de.fd}ZlddYe.d3ee.         de.fd~Zmdd3ee.         de.fdZnddheCd3ee.         de.fdZod3e.fdZpd3e.fdZqdd3ee.         de.fdZrddee.deCd3ee.         de.fdZsddZtddZudd3ee.         ddfdZvddZwdee.ee.e.f         f         fdZxdeCfdZye*dk    r=	  ezd            ezd            eD            Z{e{dnde{|                                 dZ} ezde}             ey            r ezd           nz ezd           	  eb             n!# e~$ r  ezd            ezd           Y nw xY we{@e{                                s, ezde{|                                 d            ezd            ezd           e^D ](Z ezded          ded         dd          d           ) ezd            ezd            ezd            ezd           ddlmZ d e^D             Z ejX        d5ded5         d eyd            ejX        d>ded>         d eyd            ejX        dDdedD         d eyd            ejX        dHdedH         d eyd            ejX        dNdedN         d eyd            ejX        dUdedU         d eyd            ejX        dWdedW         d eyd            ejX        d[ded[         d eyd            ejX        d]ded]         d eyd            ejX        d_ded_         d eyd            ejX        dfdedf         d eyd           dS )u  
Browser Tool Module

This module provides browser automation tools using agent-browser CLI.  It
supports two backends — **Browserbase** (cloud) and **local Chromium** — with
identical agent-facing behaviour.  The backend is auto-detected: if
``BROWSERBASE_API_KEY`` is set the cloud service is used; otherwise a local
headless Chromium instance is launched automatically.

The tool uses agent-browser's accessibility tree (ariaSnapshot) for text-based
page representation, making it ideal for LLM agents without vision capabilities.

Features:
- **Local mode** (default): zero-cost headless Chromium via agent-browser.
  Works on Linux servers without a display.  One-time setup:
  ``agent-browser install`` (downloads Chromium) or
  ``agent-browser install --with-deps`` (also installs system libraries for
  Debian/Ubuntu/Docker).
- **Cloud mode**: Browserbase cloud execution with stealth features, proxies,
  and CAPTCHA solving.  Activated when BROWSERBASE_API_KEY is set.
- Session isolation per task ID
- Text-based page snapshots using accessibility tree
- Element interaction via ref selectors (@e1, @e2, etc.)
- Task-aware content extraction using LLM summarization
- Automatic cleanup of browser sessions

Environment Variables:
- BROWSERBASE_API_KEY: API key for Browserbase (enables cloud mode)
- BROWSERBASE_PROJECT_ID: Project ID for Browserbase (required for cloud mode)
- BROWSERBASE_PROXIES: Enable/disable residential proxies (default: "true")
- BROWSERBASE_ADVANCED_STEALTH: Enable advanced stealth mode with custom Chromium,
  requires Scale Plan (default: "false")
- BROWSERBASE_KEEP_ALIVE: Enable keepAlive for session reconnection after disconnects,
  requires paid plan (default: "true")
- BROWSERBASE_SESSION_TIMEOUT: Custom session timeout in milliseconds. Set to extend
  beyond project default. Common values: 600000 (10min), 1800000 (30min) (default: none)

Usage:
    from tools.browser_tool import browser_navigate, browser_snapshot, browser_click
    
    # Navigate to a page
    result = browser_navigate("https://example.com", task_id="task_123")
    
    # Get page snapshot
    snapshot = browser_snapshot(task_id="task_123")
    
    # Click an element
    browser_click("@e5", task_id="task_123")
    N)DictAnyOptionalListPath)call_llm)check_website_accessc                     d S N urls    //home/ubuntu/hermes-agent/tools/browser_tool.py<lambda>r   H   s    t     )is_safe_urlc                     dS NFr   r   s    r   r   r   M       u r   )CloudBrowserProvider)BrowserbaseProvider)BrowserUseProvider) normalize_browser_cloud_provider)is_camofox_modec                      dS r   r   r   r   r   r   r   Y   r   r   za/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binreturnc                     g } d}t           j                            |          s| S 	 t          j        |          D ]r}|                    d          r[|dk    rUt           j                            ||d          }t           j                            |          r|                     |           sn# t          $ r Y nw xY w| S )a;  Find Homebrew versioned Node.js bin directories (e.g. node@20, node@24).

    When Node is installed via ``brew install node@24`` and NOT linked into
    /opt/homebrew/bin, the binary lives only in /opt/homebrew/opt/node@24/bin/.
    This function discovers those paths so they can be added to subprocess PATH.
    z/opt/homebrew/optnodebin)ospathisdirlistdir
startswithjoinappendOSError)dirshomebrew_optentrybin_dirs       r   _discover_homebrew_node_dirsr-   e   s     D&L7==&& Z-- 	) 	)E'' )EVOO',,|UEBB7==)) )KK(((	)    Ks   BB/ /
B<;B<_last_screenshot_cleanup_by_dir   ,  @  c                  @   	 t          t          j                            dt          j                    dz                      } | dz  }|                                rddl}t          |          5 }|                    |          pi }ddd           n# 1 swxY w Y   |                    di                               d          }|t          t          |          d          S n2# t          $ r%}t                              d	|           Y d}~nd}~ww xY wt          S )
zReturn the configured browser command timeout from config.yaml.

    Reads ``config["browser"]["command_timeout"]`` and falls back to
    ``DEFAULT_COMMAND_TIMEOUT`` (30s) if unset or unreadable.
    HERMES_HOME.hermesconfig.yamlr   Nbrowsercommand_timeout   z.Could not read command_timeout from config: %s)r   r!   environgethomeexistsyamlopen	safe_loadmaxint	ExceptionloggerdebugDEFAULT_COMMAND_TIMEOUT)hermes_homeconfig_pathr=   fcfgvales          r   _get_command_timeoutrL      sX   J2:>>-y9PQQRR!M1 	(KKKk"" .annQ''-2. . . . . . . . . . . . . . .'')R((,,->??C3s88Q''' J J JEqIIIIIIIIJ""s=   A-C' /BC' BC' BA
C' '
D1DDc                  T    t          j        dd                                          pdS )u>   Model for browser_vision (screenshot analysis — multimodal).AUXILIARY_VISION_MODEL Nr!   getenvstripr   r   r   _get_vision_modelrS      s&    9-r2288::BdBr   c                  T    t          j        dd                                          pdS )uC   Model for page snapshot text summarization — same as web_extract.AUXILIARY_WEB_EXTRACT_MODELrO   NrP   r   r   r   _get_extraction_modelrV      s&    92B77==??G4Gr   cdp_urlc                 z   | pd                                 }|sdS |                                }d|v r|S |}|                    d          s|                    d          r|                    d          dk    r|                    d                              dd          d	                                         rUd|                    dd          d	         vr7|                    d          rd
nd|                    dd          d         z   }n|S |                                                    d          r|}n|                    d          dz   }	 t          j
        |d          }|                                 |                                }n6# t          $ r)}t                              d|||           |cY d}~S d}~ww xY wt!          |
                    d          pd                                           }|rt                              d||           |S t                              d|           |S )a  Normalize a user-supplied CDP endpoint into a concrete connectable URL.

    Accepts:
    - full websocket endpoints: ws://host:port/devtools/browser/...
    - HTTP discovery endpoints: http://host:port or http://host:port/json/version
    - bare websocket host:port values like ws://host:port

    For discovery-style endpoints we fetch /json/version and return the
    webSocketDebuggerUrl so downstream tools always receive a concrete browser
    websocket instead of an ambiguous host:port URL.
    rO   z/devtools/browser/zws://zwss://:   /   zhttp://zhttps://z://z/json/version
   timeoutz,Failed to resolve CDP endpoint %s via %s: %sNwebSocketDebuggerUrlzResolved CDP endpoint %s -> %szKCDP discovery at %s did not return webSocketDebuggerUrl; using raw endpoint)rR   lowerr%   countrstriprsplitisdigitsplitendswithrequestsr:   raise_for_statusjsonrB   rC   warningstrinfo)	rW   rawlowereddiscovery_urlversion_urlresponsepayloadexcws_urls	            r   _resolve_cdp_overriderw      sM    =b


!
!C riikkGw&&
M'"" g&8&8&B&B 99S>>Q3::c??#9#9#q#A#A"#E#M#M#O#OTW_b_h_hilno_p_pqs_tTtTt*1*<*<W*E*EUYY:Y\YbYbchjkYlYlmnYooMMJ%%o66 B##**3///A<R888!!###--//   EsKY\]]]





 344:;;AACCF 4c6BBB
NN`bmnnnJs   >F 
GG<GGc                  \    t          t          j                            dd                    S )a  Return a normalized user-supplied CDP URL override, or empty string.

    When ``BROWSER_CDP_URL`` is set (e.g. via ``/browser connect``), we skip
    both Browserbase and the local headless launcher and connect directly to
    the supplied Chrome DevTools Protocol endpoint.
    BROWSER_CDP_URLrO   )rw   r!   r9   r:   r   r   r   _get_cdp_overriderz      s#     !0A2!F!FGGGr   )browserbasezbrowser-use_PROVIDER_REGISTRY_cached_cloud_providerF_cached_allow_private_urlsc                     t           rt          S da 	 t          t          j                            dt          j                    dz                      } | dz  }|                                rddl}t          |          5 }|
                    |          pi }ddd           n# 1 swxY w Y   |                    di           }d}t          |t                    r0d|v r,t          |                    d                    }|d	k    rdadS |r|t          v rt          |                     an2# t          $ r%}t                               d
|           Y d}~nd}~ww xY wt          $t%                      }|                                r|at          S )aW  Return the configured cloud browser provider, or None for local mode.

    Reads ``config["browser"]["cloud_provider"]`` once and caches the result
    for the process lifetime. An explicit ``local`` provider disables cloud
    fallback. If unset, fall back to Browserbase when direct or managed
    Browserbase credentials are available.
    Tr3   r4   r5   r   Nr6   cloud_providerlocalz-Could not read cloud_provider from config: %s)_cloud_provider_resolvedr}   r   r!   r9   r:   r;   r<   r=   r>   r?   
isinstancedictr   r|   rB   rC   rD   r   is_configured)	rF   rG   r=   rH   rI   browser_cfgprovider_keyrK   fallback_providers	            r   _get_cloud_providerr      s      &%%#I2:>>-y9PQQRR!M1 	LKKKk"" .annQ''-2. . . . . . . . . . . . . . .'')R00KL+t,,  1A[1P1P?OO$455     7**-1*4 L0B B B);L)I)K)K& I I IDaHHHHHHHHI %/11**,, 	7%6"!!sC   A-D+ ?B#D+ #B''D+ *B'+AD+ D+ +
E5EEc                  B    t                                                      S )zFReturn Browserbase direct or managed config, or None when unavailable.)r   _get_config_or_noner   r   r   _get_browserbase_config_or_noner     s      44666r   c                  B    t                                                      S )zUReturn Browserbase config or raise when neither direct nor managed mode is available.)r   _get_configr   r   r   _get_browserbase_configr   $  s      ,,...r   c                  B    t                      rdS t                      du S )zCReturn True when the browser tool will use a local browser backend.FN)rz   r   r   r   r   _is_local_moder   )  s&     u  D((r   c                  >    t                      pt                      du S )u  Return True when the browser runs locally (no cloud provider).

    SSRF protection is only meaningful for cloud backends (Browserbase,
    BrowserUse) where the agent could reach internal resources on a remote
    machine.  For local backends — Camofox, or the built-in headless
    Chromium without a cloud provider — the user already has full terminal
    and network access on the same machine, so the check adds no security
    value.
    N)_is_camofox_moder   r   r   r   _is_local_backendr   0  s!     >!4!6!6$!>>r   c                  @   t           rt          S da da	 t          t          j                            dt          j                    dz                      } | dz  }|                                rxddl}t          |          5 }|
                    |          pi }ddd           n# 1 swxY w Y   t          |                    di                               d	                    an2# t          $ r%}t                              d
|           Y d}~nd}~ww xY wt          S )zReturn whether the browser is allowed to navigate to private/internal addresses.

    Reads ``config["browser"]["allow_private_urls"]`` once and caches the result
    for the process lifetime.  Defaults to ``False`` (SSRF protection active).
    TFr3   r4   r5   r   Nr6   allow_private_urlsz1Could not read allow_private_urls from config: %s)_allow_private_urls_resolvedr~   r   r!   r9   r:   r;   r<   r=   r>   r?   boolrB   rC   rD   )rF   rG   r=   rH   rI   rK   s         r   _allow_private_urlsr   =  sc    $ *))#' !&	M2:>>-y9PQQRR!M1 	`KKKk"" .annQ''-2. . . . . . . . . . . . . . .)-cggi.D.D.H.HI].^.^)_)_& M M MH!LLLLLLLLM%%s<   A-C' B%C' %B))C' ,B)-9C' '
D1DDc                  L    t           j        dk    rdS t          j                    S )ue  Return a short temp directory path suitable for Unix domain sockets.

    macOS sets ``TMPDIR`` to ``/var/folders/xx/.../T/`` (~51 chars).  When we
    append ``agent-browser-hermes_…`` the resulting socket path exceeds the
    104-byte macOS limit for ``AF_UNIX`` addresses, causing agent-browser to
    fail with "Failed to create socket directory" or silent screenshot failures.

    Linux ``tempfile.gettempdir()`` already returns ``/tmp``, so this is a
    no-op there.  On macOS we bypass ``TMPDIR`` and use ``/tmp`` directly
    (symlink to ``/private/tmp``, sticky-bit protected, always available).
    darwinz/tmp)sysplatformtempfile
gettempdirr   r   r   _socket_safe_tmpdirr   V  s%     |xv   r   _active_sessions_recording_sessionsBROWSER_INACTIVITY_TIMEOUT300_session_last_activityc                     t           rdS da t          sdS t                              dt	          t                               	 t                       n2# t          $ r%} t                              d|            Y d} ~ nd} ~ ww xY wt          5  t          	                                 t          	                                 ddd           n# 1 swxY w Y   t          	                                 dS # t          5  t          	                                 t          	                                 ddd           n# 1 swxY w Y   t          	                                 w xY w)z
    Emergency cleanup of all active browser sessions.
    Called on process exit or interrupt to prevent orphaned sessions.
    NTz2Emergency cleanup: closing %s active session(s)...zEmergency cleanup error: %s)_cleanup_doner   rC   rn   lencleanup_all_browsersrB   error_cleanup_lockclearr   r   )rK   s    r   _emergency_cleanup_all_sessionsr     s     M 
KKD$%%' ' '$ 7 7 72A666666667  	+ 	+""$$$"((***	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	!!#####  	+ 	+""$$$"((***	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	!!####sf   A C0 
BA<7C0 <BC0 3C

CC0E83D7+E7D;;E>D;?Ec                     t          j                     } g }t          5  t          t                                                    D ](\  }}| |z
  t
          k    r|                    |           )	 ddd           n# 1 swxY w Y   |D ]}	 t          | t                              ||           z
            }t          
                    d||           t          |           t          5  |t          v rt          |= ddd           n# 1 swxY w Y   # t          $ r&}t                              d||           Y d}~d}~ww xY wdS )a7  
    Clean up browser sessions that have been inactive for longer than the timeout.
    
    This function is called periodically by the background cleanup thread to
    automatically close sessions that haven't been used recently, preventing
    orphaned sessions (local or Browserbase) from accumulating.
    Nz<Cleaning up inactive session for task: %s (inactive for %ss)z)Error cleaning up inactive session %s: %s)timer   listr   items"BROWSER_SESSION_INACTIVITY_TIMEOUTr'   rA   r:   rC   rn   cleanup_browserrB   rl   )current_timesessions_to_cleanuptask_id	last_timeelapsedrK   s         r   "_cleanup_inactive_browser_sessionsr     s    9;;L	 4 4"&'='C'C'E'E"F"F 	4 	4GYi'*LLL#**7333	44 4 4 4 4 4 4 4 4 4 4 4 4 4 4
 ' 	T 	T	T,)?)C)CG\)Z)ZZ[[GKKVX_ahiiiG$$$ 8 8444.w78 8 8 8 8 8 8 8 8 8 8 8 8 8 8  	T 	T 	TNNFQRSSSSSSSS	T	T 	TsO   AA::A>A>
AD'D9DD			DD		D
ED<<Ec                     t           rz	 t                       n2# t          $ r%} t                              d|            Y d} ~ nd} ~ ww xY wt          d          D ]}t           s nt          j        d            t           xdS dS )z
    Background thread that periodically cleans up inactive browser sessions.
    
    Runs every 30 seconds and checks for sessions that haven't been used
    within the BROWSER_SESSION_INACTIVITY_TIMEOUT period.
    zCleanup thread error: %sNr/   r\   )_cleanup_runningr   rB   rC   rl   ranger   sleep)rK   _s     r   _browser_cleanup_thread_workerr     s      
	:.0000 	: 	: 	:NN5q99999999	: r 	 	A# JqMMMM  
 
 
 
 
s    
AAAc                  4   t           5  t          t                                          sWdat	          j        t          dd          at                                           t          	                    dt                     ddd           dS # 1 swxY w Y   dS )z;Start the background cleanup thread if not already running.NTzbrowser-cleanup)targetdaemonnamez0Started inactivity cleanup thread (timeout: %ss))r   _cleanup_threadis_aliver   	threadingThreadr   startrC   rn   r   r   r   r   _start_browser_cleanup_threadr     s     
 	p 	p"/*B*B*D*D"#'.5&  O
 !!###KKJLnooo	p 	p 	p 	p 	p 	p 	p 	p 	p 	p 	p 	p 	p 	p 	p 	p 	p 	ps   A8BBBc                  R    da t          t                              d           dS dS )z#Stop the background cleanup thread.FNr8   r_   )r   r   r&   r   r   r   _stop_browser_cleanup_threadr     s5     "Q''''' #"r   r   c                 |    t           5  t          j                    t          | <   ddd           dS # 1 swxY w Y   dS )z1Update the last activity timestamp for a session.N)r   r   r   r   s    r   _update_session_activityr     s}    	 6 6*.)++w'6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6s   155browser_navigatea,  Navigate to a URL in the browser. Initializes the session and loads the page. Must be called before other browser tools. For simple information retrieval, prefer web_search or web_extract (faster, cheaper). Use browser tools when you need to interact with a page (click, fill forms, dynamic content).objectr   stringz4The URL to navigate to (e.g., 'https://example.com'))typedescription)r   
propertiesrequired)r   r   
parametersbrowser_snapshota]  Get a text-based snapshot of the current page's accessibility tree. Returns interactive elements with ref IDs (like @e1, @e2) for browser_click and browser_type. full=false (default): compact view with interactive elements. full=true: complete page content. Snapshots over 8000 chars are truncated or LLM-summarized. Requires browser_navigate first.fullbooleanzpIf true, returns complete page content. If false (default), returns compact view with interactive elements only.)r   r   defaultbrowser_clickzClick on an element identified by its ref ID from the snapshot (e.g., '@e5'). The ref IDs are shown in square brackets in the snapshot output. Requires browser_navigate and browser_snapshot to be called first.refz=The element reference from the snapshot (e.g., '@e5', '@e12')browser_typezType text into an input field identified by its ref ID. Clears the field first, then types the new text. Requires browser_navigate and browser_snapshot to be called first.z5The element reference from the snapshot (e.g., '@e3')zThe text to type into the field)r   textr   browser_scrollzScroll the page in a direction. Use this to reveal more content that may be below or above the current viewport. Requires browser_navigate to be called first.	directionupdownzDirection to scroll)r   enumr   browser_backzdNavigate back to the previous page in browser history. Requires browser_navigate to be called first.browser_presszPress a keyboard key. Useful for submitting forms (Enter), navigating (Tab), or keyboard shortcuts. Requires browser_navigate to be called first.keyz:Key to press (e.g., 'Enter', 'Tab', 'Escape', 'ArrowDown')browser_closez}Close the browser session and release resources. Call this when done with browser tasks to free up Browserbase session quota.browser_get_imageszGet a list of all images on the current page with their URLs and alt text. Useful for finding images to analyze with the vision tool. Requires browser_navigate to be called first.browser_visiona  Take a screenshot of the current page and analyze it with vision AI. Use this when you need to visually understand what's on the page - especially useful for CAPTCHAs, visual verification challenges, complex layouts, or when the text snapshot doesn't capture important visual information. Returns both the AI analysis and a screenshot_path that you can share with the user by including MEDIA:<screenshot_path> in your response. Requires browser_navigate to be called first.zYWhat you want to know about the page visually. Be specific about what you're looking for.zIf true, overlay numbered [N] labels on interactive elements. Each [N] maps to ref @eN for subsequent browser commands. Useful for QA and spatial reasoning about page layout.)r   r   r   )questionannotater   browser_consolea  Get browser console output and JavaScript errors from the current page. Returns console.log/warn/error/info messages and uncaught JS exceptions. Use this to detect silent JavaScript errors, failed API calls, and application warnings. Requires browser_navigate to be called first.r   z0If true, clear the message buffers after readingc                     dd l }d|                                j        d d          }t                              d||            |d d ddidS )Nr   h_r^   z,Created local browser session %s for task %sr   Tsession_namebb_session_idrW   featuresuuiduuid4hexrC   rn   )r   r   r   s      r   _create_local_sessionr     sk    KKK/

("-//L
KK>g' ' ' %dO	  r   c                     ddl }d|                                j        dd          }t                              d|||            |d|ddidS )	z?Create a session that connects to a user-supplied CDP endpoint.r   Ncdp_r^   u1   Created CDP browser session %s → %s for task %scdp_overrideTr   r   )r   rW   r   r   s       r   _create_cdp_sessionr     sn    KKK1$**,,*3B3/11L
KKCgw0 0 0 %#T*	  r   c                    | d} t                       t          |            t          5  | t          v rt          |          cddd           S 	 ddd           n# 1 swxY w Y   t	                      }|rt          | |          }n5t                      }|t          |           }n|                    |           }t          5  | t          v rt          |          cddd           S |t          | <   ddd           n# 1 swxY w Y   |S )a  
    Get or create session info for the given task.
    
    In cloud mode, creates a Browserbase session with proxies enabled.
    In local mode, generates a session name for agent-browser --session.
    Also starts the inactivity cleanup thread and updates activity tracking.
    Thread-safe: multiple subagents can call this concurrently.
    
    Args:
        task_id: Unique identifier for the task
        
    Returns:
        Dict with session_name (always), bb_session_id + cdp_url (cloud only)
    Nr   )	r   r   r   r   rz   r   r   r   create_session)r   r   session_infoproviders       r   _get_session_infor     s     "### W%%%	 - -&&&#G,- - - - - - - -&- - - - - - - - - - - - - - - %&&L <*7LAA&((099LL#227;;L	 1 1 &&&#G,1 1 1 1 1 1 1 1 %1!1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 s)   AA AC:$
C::C>C>c                     t          j        d          } | r| S g }dD ]6}t          j                            |          r|                    |           7|                    t                                 t          t          j	        
                    dt          j                    dz                      }t          |dz  dz            }t          j                            |          r|                    |           |r9t          j                            |          }t          j        d|          } | r| S t          t                    j        j        }|dz  d	z  dz  }|                                rt          |          S t          j        d
          }|s5|r3t          j        d
t          j                            |                    }|rdS t%          d          )a@  
    Find the agent-browser CLI executable.
    
    Checks in order: current PATH, Homebrew/common bin dirs, Hermes-managed
    node, local node_modules/.bin/, npx fallback.
    
    Returns:
        Path to agent-browser executable
        
    Raises:
        FileNotFoundError: If agent-browser is not installed
    zagent-browser)z/opt/homebrew/binz/usr/local/binr3   r4   r   r    )r"   node_modulesz.binnpxznpx agent-browserzagent-browser CLI not found. Install it with: npm install -g agent-browser
Or run 'npm install' in the repo root to install locally.
Or ensure npx is available in your PATH.)shutilwhichr!   r"   r#   r'   extendr-   r   r9   r:   r;   rm   pathsepr&   __file__parentr<   FileNotFoundError)	which_result
extra_dirsdrF   hermes_node_binextended_path	repo_root	local_binnpx_paths	            r   _find_agent_browserr    s    <00L  J4 ! !7== 	!a   244555rz~~mTY[[95LMMNNK+.677O	w}}_%% +/***  

33|O-HHH 	  X%,IN*V3oEI 9~~ |E""H I
 I<BJOOJ,G,GHHH #""
	3  r   c                     | sdS g d}|D ]Y}t          j        ||           }|r@|                    d                                                              d          }|r|c S ZdS )zHExtract a screenshot file path from agent-browser human-readable output.N)z6Screenshot saved to ['\"](?P<path>/[^'\"]+?\.png)['\"]z0Screenshot saved to (?P<path>/\S+?\.png)(?:\s|$)z(?P<path>/\S+?\.png)(?:\s|$)r"   z'")researchgrouprR   )r   patternspatternmatchr"   s        r   "_extract_screenshot_path_from_textr  +  s     t  H   	'4(( 	;;v&&,,..44U;;D 4r   commandargsr`   c           	      &   |t                      }|pg }	 t                      }nD# t          $ r7}t                              d|           dt          |          dcY d}~S d}~ww xY wddlm}  |            rdddS 	 t          |           }nH# t          $ r;}t                              d| |           dd	t          |           dcY d}~S d}~ww xY w|
                    d
          rd|d
         g}n
d|d         g}|                                |z   d|gz   |z   }		 t          j                            t                      d|d                    }
t          j        |
dd           t                              d|| |
t%          |
                     i t          j        }t)          t          j        
                    dt)          j                    dz                      }t          |dz  dz            }|
                    dd          }d |                    d          D             }|gt-                      z   d t.                              d          D             z   }t1          |          D ];}t          j                            |          r||vr|                    d|           <d                    |          |d<   |
|d<   t          j                            |
d|           }t          j                            |
d|           }t          j        |t          j        t          j        z  t          j        z  d           }t          j        |t          j        t          j        z  t          j        z  d           }	 t?          j         |	||t>          j!        |!          }t          j"        |           t          j"        |           n-# t          j"        |           t          j"        |           w xY w	 |#                    |"           nd# t>          j$        $ rR |%                                 |#                                 t                              d#||| |
           dd$| d%dcY S w xY wt7          |d&          5 }|&                                }ddd           n# 1 swxY w Y   t7          |d&          5 }|&                                }ddd           n# 1 swxY w Y   |j'        }||fD ]'}	 t          j(        |           # tR          $ r Y $w xY w|ri|*                                rU|dk    rtV          j,        ntV          j-        }t          .                    |d'||*                                dd(                    |*                                sK|dk    rEt                              d)|d*                    |	dd+                   d,z   |pddd-                    |*                                }|rp	 t_          j0        |          }|d.k    rp|
                    d/          r[|
                    d0i           }|
                    d.          s0|
                    d1          st                              d2|           |S # t^          j1        $ r |dd3         } t                              d4||| dd(                    |d5k    r|pd*                                }!d6                    d7 ||!fD                       }"te          |"          }#|#rFt)          |#          3                                r%t          4                    d8|#           d|#| d9d:cY S dd;| d<|  dcY S w xY w|dk    rE|r|*                                nd=| }$t                              d>|||$dd?                    d|$dS di d:S # t          $ r:}t                              d@||dA           dt          |          dcY d}~S d}~ww xY w)Ba  
    Run an agent-browser CLI command using our pre-created Browserbase session.
    
    Args:
        task_id: Task identifier to get the right session
        command: The command to run (e.g., "open", "click")
        args: Additional arguments for the command
        timeout: Command timeout in seconds.  ``None`` reads
                 ``browser.command_timeout`` from config (default 30s).
        
    Returns:
        Parsed JSON response from agent-browser
    Nzagent-browser CLI not found: %sFsuccessr   r   )is_interruptedInterruptedz0Failed to create browser session for task=%s: %sz"Failed to create browser session: rW   z--cdpz	--sessionr   z--jsonagent-browser-i  T)modeexist_okz/browser cmd=%s task=%s socket_dir=%s (%d chars)r3   r4   r   r    PATHrO   c                     g | ]}||S r   r   .0ps     r   
<listcomp>z(_run_browser_command.<locals>.<listcomp>  s    ???AQ?a???r   rY   c                     g | ]}||S r   r   r(  s     r   r+  z(_run_browser_command.<locals>.<listcomp>  s    555Q15q555r   AGENT_BROWSER_SOCKET_DIR_stdout__stderr_i  )stdoutstderrstdinenvr_   z9browser '%s' timed out after %ds (task=%s, socket_dir=%s)zCommand timed out after z secondsrzbrowser '%s' stderr: %si  z>browser '%s' returned empty stdout with rc=0. cmd=%s stderr=%s    ...   snapshotr   datarefsz]snapshot returned empty content. Possible stale daemon or CDP connection issue. returncode=%s  z1browser '%s' returned non-JSON output (rc=%s): %s
screenshot
c              3      K   | ]}||V  	d S r   r   )r)  parts     r   	<genexpr>z'_run_browser_command.<locals>.<genexpr>  s;       . .!%t.. . . . . .r   z<browser 'screenshot' recovered file from non-JSON output: %s)r"   ro   )r   r:  z(Non-JSON output from agent-browser for 'z': zCommand failed with code zbrowser '%s' failed (rc=%s): %sr0   zbrowser '%s' exception: %sexc_info)5rL   r  r
  rC   rl   rm   tools.interruptr!  r   rB   r:   rg   r!   r"   r&   r   makedirsrD   r   r9   r   r;   r-   
_SANE_PATHreversedr#   insertr>   O_WRONLYO_CREATO_TRUNC
subprocessPopenDEVNULLclosewaitTimeoutExpiredkillread
returncodeunlinkr(   rR   loggingWARNINGDEBUGlogrk   loadsJSONDecodeErrorr  r<   rn   )%r   r  r  r`   browser_cmdrK   r!  r   backend_args	cmd_partstask_socket_dirbrowser_envrF   r  existing_path
path_partscandidate_dirsr@  stdout_pathstderr_path	stdout_fd	stderr_fdprocrH   r0  r1  rT  r*  levelstdout_textparsed	snap_dataro   stderr_textcombined_textrecovered_path	error_msgs%                                        r   _run_browser_commandrq  @  s	   & &((:2D3)++ 3 3 38!<<< 3q66222222223 /.....~ : =999Z(11 Z Z ZI7TUVVV +XPSTUPVPV+X+XYYYYYYYYZ 	"" C  i!89 $\.%AB!!##l26  	I
N3 ',,!!;\.9;;
 
 	O%$????FgO8L8L	N 	N 	N %n 2:>>-y9PQQRRkF2U:;;#33??!4!4S!9!9???
*,,-55***3//5556 	 ^,, 	+ 	+Dw}}T"" +t:'='=!!!T***!hhz22F2A./ gll?4Hw4H4HIIgll?4Hw4H4HIIGKrz)ABJ)NPUVV	GKrz)ABJ)NPUVV	
	 #   (  D HYHY HYHY	]IIgI&&&&( 	] 	] 	]IIKKKIIKKKNNV"GWoG G G$/['/[/[/[\\\\\	] +s## 	qVVXXF	 	 	 	 	 	 	 	 	 	 	 	 	 	 	+s## 	qVVXXF	 	 	 	 	 	 	 	 	 	 	 	 	 	 	_
 {+ 	 	A	!     	Xfllnn 	X'1QGOOGMEJJu7&,,..QURUQUBVWWW ||~~ 	1*//NN ."CHHYrr]$;$;e$C"Lb$3$/1 1 1
 llnn '	&K00j((VZZ	-B-B( &

62 6 6I$==44 DY]]6=R=R D (78BD D D '   !%4%(R&
CI? ? ? l**#)<R"6"6"8"8K$(II . .*5{)C. . . % %M &H%V%VN% $~*>*>*E*E*G*G Z*  
 (,(6'*% %        %YYYTWYY    3> ??*0^6^R\6^6^INN<gzS\]a^a]aSbccc$y999,,, 3 3 33Wa$OOO 3q66222222223s4  % 
A&,A!A&!A&?B 
C0C	CC!I)_ #O .)_ *P_ P _ AQ=:_ <Q==_ R1%_ 1R55_ 8R59_ S-!_ -S11_ 4S15_ T_ 
T(%_ 'T((C$_ BZ C]9)_ +]96_ 8]99A_ _ 
`/```snapshot_text	user_taskc                 j   |r
d| d|  d}nd|  d}ddl m}  ||          }	 dd	|d
gddd}t                      }|r||d<   t          di |}|j        d         j        j        pd                                pt          |           } ||          S # t          $ r t          |           cY S w xY w)zUse LLM to extract relevant content from a snapshot based on the user's task.

    Falls back to simple truncation when no auxiliary text model is configured.
    zQYou are a content extractor for a browser automation agent.

The user's task is: a  

Given the following page snapshot (accessibility tree representation), extract and summarize the most relevant information for completing this task. Focus on:
1. Interactive elements (buttons, links, inputs) that might be needed
2. Text content relevant to the task (prices, descriptions, headings, important info)
3. Navigation structure if relevant

Keep ref IDs (like [ref=e5]) for interactive elements so the agent can use them.

Page Snapshot:
zW

Provide a concise summary that preserves actionable information and relevant content.zSummarize this page snapshot, preserving:
1. All interactive elements with their ref IDs (like [ref=e5])
2. Key text content and headings
3. Important information visible on the page

Page Snapshot:
zL

Provide a concise summary focused on interactive elements and key content.r   redact_sensitive_textweb_extractuserrolecontenti  皙?)taskmessages
max_tokenstemperaturemodelrO   Nr   )
agent.redactrv  rV   r	   choicesmessager{  rR   _truncate_snapshotrB   )rr  rs  extraction_promptrv  call_kwargsr  rs   	extracteds           r   _extract_relevant_contentr    sV     
	e#,	e 	e  -	e 	e 	e 	Z  -	Z Z Z 	 322222--.?@@1!"(5FGGH	
 
 &'' 	)#(K **k**%a(08>BEEGGlK]^kKlKl	$$Y/// 1 1 1!-000001s   A0B B21B2	max_charsc                 F    t          |           |k    r| S | d|         dz   S )z
    Simple truncation fallback for snapshots.
    
    Args:
        snapshot_text: The snapshot text to truncate
        max_chars: Maximum characters to keep
        
    Returns:
        Truncated text with indicator if truncated
    Nz

[... content truncated ...])r   )rr  r  s     r   r  r  D  s2     =Y&&)$'HHHr   c           	         ddl m} |                    |           rt          j        ddd          S t                      s4t                      s&t          |           st          j        ddd          S t          |           }|r4t          j        d|d         |d         |d	         |d
         dd          S t                      rddl
m}  || |          S |pd}t          |          }|                    dd          }|rd|d<   t          |           t          |d| gt!          t#                      d                    }|                    d          rC|                    di           }	|	                    dd          }
|	                    d|           }t                      sPt                      sB|r@|| k    r:t          |          s+t          |ddgd           t          j        ddd          S d||
d}g d}|
                                t'          fd|D                       r	d|
 d |d!<   |rId"|v rE|d"         }d# |                                D             }|                    d$          sd%|d&<   ||d'<   t          j        |d(          S t          j        d|                    d)d*          dd(          S )+a  
    Navigate to a URL in the browser.
    
    Args:
        url: The URL to navigate to
        task_id: Task identifier for session isolation
        
    Returns:
        JSON string with navigation result (includes stealth features info on first nav)
    r   )
_PREFIX_REFz_Blocked: URL contains what appears to be an API key or token. Secrets must not be sent in URLs.r  z2Blocked: URL targets a private or internal addressr  hostrulesource)r  r  r  )r   r   blocked_by_policy)camofox_navigater   
_first_navTr>   <   r_   r   r:  titlerO   r   zabout:blankr^   z6Blocked: redirect landed on a private/internal address)r   r   r  )zaccess deniedz#access to this page has been deniedblockedzbot detectedzverification requiredzplease verifyzare you a robotcaptcha
cloudflarezddos protectionzchecking your browserzjust a momentzattention requiredc              3       K   | ]}|v V  	d S r   r   )r)  r  title_lowers     r   rA  z#browser_navigate.<locals>.<genexpr>  s(      FF'w+%FFFFFFr   zPage title 'a0  ' suggests bot detection. The site may have blocked this request. Options: 1) Try adding delays between actions, 2) Access different pages first, 3) Enable advanced stealth (BROWSERBASE_ADVANCED_STEALTH=true, requires Scale plan), 4) Some sites have very aggressive bot detection that may be unavoidable.bot_detection_warningr   c                     g | ]	\  }}||
S r   r   )r)  kvs      r   r+  z$browser_navigate.<locals>.<listcomp>  s!    CCCTQCqCCCr   proxieszRunning WITHOUT residential proxies. Bot detection may be more aggressive. Consider upgrading Browserbase plan for proxy support.stealth_warningstealth_featuresensure_asciir   zNavigation failed)r  r  r  rk   dumpsr   r   _is_safe_urlr
   r   tools.browser_camofoxr  r   r:   _maybe_start_recordingrq  r@   rL   rb   anyr   )r   r   r  r  r  effective_task_idr   is_first_navresultr:  r  	final_urlrs   blocked_patternsr   active_featuresr  s                   @r   r   r   Y  s    (''''' z9
 
   	  ':'<'< \RUEVEV zI
 
   	 #3''G zY'*1&/76?^efn^o!p!p
 
   	  .::::::W---,9 %%677L##L$77L  2%*\"0111!"3VcUCPdPfPfhjLkLklllFzz) 9zz&"%%"%%HHUC((	 !"" 	+>+@+@ 	Y 	S\`cScSclx  zC  mD  mDSc !2F]OUWXXXX: Q     
 

 
 
 kkmmFFFF5EFFFFF 	\u \ \ \ ,-  	;J,66#J/HCCX^^-=-=CCCO<<	** M *+ ,;H'(z(7777zZZ)<==
 
    	r   c                    t                      rddlm}  || ||          S |pd}g }| s|                    dg           t	          |d|          }|                    d          r|                    di           }|                    dd          }|                    d	i           }	t          |          t          k    r|rt          ||          }n't          |          t          k    rt          |          }d
||	rt          |	          ndd}
t          j        |
d          S t          j        d|                    dd          dd          S )ag  
    Get a text-based snapshot of the current page's accessibility tree.
    
    Args:
        full: If True, return complete snapshot. If False, return compact view.
        task_id: Task identifier for session isolation
        user_task: The user's current task (for task-aware extraction)
        
    Returns:
        JSON string with page snapshot
    r   )camofox_snapshotr   z-cr9  r   r:  rO   r;  T)r   r9  element_countFr  r   zFailed to get snapshotr  )r   r  r  r  rq  r:   r   SNAPSHOT_SUMMARIZE_THRESHOLDr  r  rk   r  )r   r   rs  r  r  r  r  r:  rr  r;  rs   s              r   r   r     s      :::::::gy999,9 D TF!"3ZFFFzz) zz&"%%R00xx## } <<<<5mYOOMM">>>.}==M %*.5SYYYA
 
 z(7777zZZ)ABB
 
    	r   c                 ^   t                      rddlm}  || |          S |pd}|                     d          sd|  } t	          |d| g          }|                    d          rt          j        d| dd	
          S t          j        d	|                    dd|            dd	
          S )z
    Click on an element.
    
    Args:
        ref: Element reference (e.g., "@e5")
        task_id: Task identifier for session isolation
        
    Returns:
        JSON string with click result
    r   )camofox_clickr   @clickr   T)r   clickedFr  r   zFailed to click r  )r   r  r  r%   rq  r:   rk   r  )r   r   r  r  r  s        r   r   r     s      +777777}S'***,9 >># #ii!"3WseDDFzz) 	z
 
    	
 zZZ)AC)A)ABB
 
    	r   c                 d   t                      rddlm}  || ||          S |pd}|                     d          sd|  } t	          |d| |g          }|                    d          rt          j        d|| dd	
          S t          j        d	|                    dd|            dd	
          S )z
    Type text into an input field.
    
    Args:
        ref: Element reference (e.g., "@e3")
        text: Text to type
        task_id: Task identifier for session isolation
        
    Returns:
        JSON string with type result
    r   )camofox_typer   r  fillr   T)r   typedelementFr  r   zFailed to type into r  )r   r  r  r%   rq  r:   rk   r  )r   r   r   r  r  r  s         r   r   r   +  s     0666666|Cw///,9 >># #ii ""3Vc4[IIFzz) 
z
 
 	   	 zZZ)E)E)EFF
 
    	r   c                 l   t                      rddlm}  || |          S |pd}| dvrt          j        dd|  ddd	          S t          |d
| g          }|                    d          rt          j        d| dd	          S t          j        d|                    dd|            dd	          S )z
    Scroll the page.
    
    Args:
        direction: "up" or "down"
        task_id: Task identifier for session isolation
        
    Returns:
        JSON string with scroll result
    r   )camofox_scrollr   )r   r   FzInvalid direction 'z'. Use 'up' or 'down'.r  r  scrollr   T)r   scrolledr   zFailed to scroll )r   r  r  rk   r  rq  r:   )r   r   r  r  r  s        r   r   r   Q  s&     2888888~i111,9 &&zL9LLL
 
    	
 ""3X	{KKFzz) 	z!
 
    	
 zZZ)HY)H)HII
 
    	r   c                 t   t                      rddlm}  ||           S | pd}t          |dg           }|                    d          rC|                    di           }t          j        d|                    dd	          d
d          S t          j        d|                    dd          dd          S )z
    Navigate back in browser history.
    
    Args:
        task_id: Task identifier for session isolation
        
    Returns:
        JSON string with navigation result
    r   )camofox_backr   backr   r:  Tr   rO   )r   r   Fr  r   zFailed to go backr  )r   r  r  rq  r:   rk   r  )r   r  r  r  r:  s        r   r   r   w  s      %666666|G$$$,9!"3VR@@Fzz) 
zz&"%%z88E2&&
 
    	
 zZZ)<==
 
    	r   c                 *   t                      rddlm}  || |          S |pd}t          |d| g          }|                    d          rt          j        d| dd	          S t          j        d|                    d
d|            dd	          S )z
    Press a keyboard key.
    
    Args:
        key: Key to press (e.g., "Enter", "Tab")
        task_id: Task identifier for session isolation
        
    Returns:
        JSON string with key press result
    r   )camofox_pressr   pressr   T)r   pressedFr  r   zFailed to press r  )r   r  r  rq  r:   rk   r  )r   r   r  r  r  s        r   r   r     s      +777777}S'***,9!"3WseDDFzz) 	z
 
    	
 zZZ)AC)A)ABB
 
    	r   c                     t                      rddlm}  ||           S | pd}t          5  |t          v }ddd           n# 1 swxY w Y   t          |           ddd}|sd|d<   t          j        |d	
          S )z
    Close the browser session.

    Args:
        task_id: Task identifier for session isolation

    Returns:
        JSON string with close result
    r   )camofox_closer   NT)r   closedz Session may not have been activerl   Fr  )r   r  r  r   r   r   rk   r  )r   r  r  had_sessionrs   s        r   r   r     s      &777777}W%%%,9	 < <'+;;< < < < < < < < < < < < < < < %&&&  H  A@:hU3333s   
AAAc           	          t                      rddlm}  || |          S |pd}| rdgng }| rdgng }t          |d|          }t          |d|          }g }|                    d          rn|                    di                               d	g           D ]C}	|                    |	                    d
d          |	                    dd          dd           Dg }
|                    d          rY|                    di                               dg           D ].}|
                    |                    dd          dd           /t          j        d||
t          |          t          |
          dd          S )a  Get browser console messages and JavaScript errors.
    
    Returns both console output (log/warn/error/info from the page's JS)
    and uncaught exceptions (crashes, unhandled promise rejections).
    
    Args:
        clear: If True, clear the message/error buffers after reading
        task_id: Task identifier for session isolation
        
    Returns:
        JSON string with console messages and JS errors
    r   )camofox_consoler   z--clearconsoleerrorsr   r:  r~  r   rY  r   rO   )r   r   r  r  	exception)r  r  T)r   console_messages	js_errorstotal_messagestotal_errorsFr  )	r   r  r  rq  r:   r'   rk   r  r   )r   r   r  r  console_args
error_argsconsole_resulterrors_resultr~  msgr  errs               r   r   r     s     /999999ug...,9"'/I;;RL %-)2J)*;YUUN():HjQQMH)$$ !%%fb1155j"EE 	 	COO..++#      F##  $$VR0044XrBB 	 	CMM779b11%     
 :$h--F     r   c                    | t           v rdS 	 t          t          j                            dt          j                    dz                      }|dz  }d}|                                rlddl}t          |          5 }|	                    |          pi }ddd           n# 1 swxY w Y   |                    di                               dd          }|sdS |d	z  }|
                    d
d
           t          d           ddl} |j        d          }	|d|	 d| dd          dz  }
t          | ddt          |
          g          }|                    d          r8t                               |            t"                              d| |
           dS t"                              d|                    d                     dS # t(          $ r&}t"                              d|           Y d}~dS d}~ww xY w)z@Start recording if browser.record_sessions is enabled in config.Nr3   r4   r5   Fr   r6   record_sessionsbrowser_recordingsTparentsr%  H   max_age_hoursz%Y%m%d_%H%M%Ssession_r      z.webmrecordr   r   z'Auto-recording browser session %s to %sz"Could not start auto-recording: %sr   zAuto-recording setup failed: %s)r   r   r!   r9   r:   r;   r<   r=   r>   r?   mkdir_cleanup_old_recordingsr   strftimerq  rm   addrC   rn   rD   rB   )r   rF   rG   record_enabledr=   rH   rI   recordings_dirr   	timestamprecording_pathr  rK   s                r   r  r    sZ   %%%;2:>>-y9PQQRR!M1 	RKKKk"" .annQ''-2. . . . . . . . . . . . . . . WWY33778I5QQN 	F$';;TD999b1111!DM/22	'*TY*T*T"*T*T*TT%gx'3~CVCV9WXX::i   	T##G,,,KKA7N[[[[[LL=vzz'?R?RSSSSS ; ; ;6:::::::::;sI   A/G
 <B G
  B$$G
 'B$(/G
 B?G
 .G
 

G:G55G:c                    | t           vrdS 	 t          | ddg          }|                    d          rF|                    di                               dd          }t                              d| |           n3# t
          $ r&}t                              d	| |           Y d}~nd}~ww xY wt                               |            dS # t                               |            w xY w)
z1Stop recording if one is active for this session.Nr  stopr   r:  r"   rO   z*Saved browser recording for session %s: %sz#Could not stop recording for %s: %s)r   rq  r:   rC   rn   rB   rD   discard)r   r  r"   rK   s       r   _maybe_stop_recordingr  &  s    )))-%gx&BB::i   	U::fb))--fb99DKKDgtTTT H H H:GQGGGGGGGGH 	##G,,,,,##G,,,,s0   A-A; :C
 ;
B+B&!C
 &B++C
 
C&c                 T   t                      rddlm}  ||           S | pd}d}t          |d|g          }|                    d          r|                    di           }|                    dd	          }	 t          |t                    rt          j        |          }n|}t          j	        d
|t          |          dd          S # t          j        $ r t          j	        d
g dddd          cY S w xY wt          j	        d|                    dd          dd          S )z
    Get all images on the current page.
    
    Args:
        task_id: Task identifier for session isolation
        
    Returns:
        JSON string with list of images (src and alt)
    r   )camofox_get_imagesr   a  JSON.stringify(
        [...document.images].map(img => ({
            src: img.src,
            alt: img.alt || '',
            width: img.naturalWidth,
            height: img.naturalHeight
        })).filter(img => img.src && !img.src.startsWith('data:'))
    )evalr   r:  r  z[]T)r   imagesrc   Fr  zCould not parse image data)r   r  rc   rl   r   zFailed to get imagesr  )r   r  r  rq  r:   r   rm   rk   rZ  r  r   r[  )r   r  r  js_coder  r:  
raw_resultr  s           r   r   r   5  s     +<<<<<<!!'***,9	G ""3VgYGGFzz) zz&"%%XXh--
	#*c** $J//#: V  "	# # # #
 # 	# 	# 	#:7	 
 "# # # # # #	# zZZ)?@@
 
    	s   :AC *C:9C:r   c                 z   t                      rddlm}  || ||          S ddl}ddl}ddlm} |pd}ddlm}  |dd          }	|	d	|	                                j
         d
z  }
	 |	                    dd           t          |	d           g }|r|                    d           |                    d           |                    t          |
                     t          |d|          }|                    d          s_|                    dd          }t#                      }|dnd|                                 d}t'          j        dd| d| dd          S |                    di                               d          }|r ||          }
|
                                sJt#                      }|dnd|                                 d}t'          j        dd|
 d | d!dd          S |
                                }|                    |                              d"          }d#| }d$|  d%}t3                      }t4                              d&t9          |                     d'}	 dd(lm}  |            }|                    d)i                               d*i                               d+          }|t?          |          }n# t@          $ r Y nw xY wd*d,d-|d.d/d0|id1gd2gd3d4|d5}|r||d6<   tC          dBi |}|j"        d         j#        j$        pd7%                                }dd8l&m'}  ||          }d|pd9t          |
          d:}|r:|                    di                               d;          r|d         d;         |d;<   t'          j        |d          S # t@          $ r}}t4          (                    d<|d=           dd>t          |           d} |
                                rt          |
          | d?<   d@| dA<   t'          j        | d          cY d}~S d}~ww xY w)Ca6  
    Take a screenshot of the current page and analyze it with vision AI.
    
    This tool captures what's visually displayed in the browser and sends it
    to Gemini for analysis. Useful for understanding visual content that the
    text-based snapshot may not capture (CAPTCHAs, verification challenges,
    images, complex layouts, etc.).
    
    The screenshot is saved persistently and its file path is returned alongside
    the analysis, so it can be shared with users via MEDIA:<path> in the response.
    
    Args:
        question: What you want to know about the page visually
        annotate: If True, overlay numbered [N] labels on interactive elements
        task_id: Task identifier for session isolation
        
    Returns:
        JSON string with vision analysis results and screenshot_path
    r   )camofox_visionNr   r   )get_hermes_dirzcache/screenshotsbrowser_screenshotsbrowser_screenshot_z.pngTr     r  z
--annotatez--fullr=  r   r   zUnknown errorr   cloud ()FzFailed to take screenshot (z mode): r  r  r:  r"   z#Screenshot file was not created at z (z mode). This may indicate a socket path issue (macOS /var/folders/), a missing Chromium install ('agent-browser install'), or a stale daemon process.asciizdata:image/png;base64,zCYou are analyzing a screenshot of a web browser.

User's question: a"  

Provide a detailed and helpful answer based on what you see in the screenshot. If there are interactive elements, describe them. If there are verification challenges or CAPTCHAs, describe what type they are and what action might be needed. Focus on answering the user's specific question.z/browser_vision: analysing screenshot (%d bytes)g      ^@)load_config	auxiliaryvisionr`   rx  r   )r   r   	image_urlr   )r   r  ry  r<  r|  )r}  r~  r  r  r`   r  rO   ru  z$Vision analysis returned no content.)r   analysisscreenshot_pathannotationszbrowser_vision failed: %srB  zError during vision analysis: r  z\Screenshot was captured but vision analysis failed. You can still share it via MEDIA:<path>.noter   ))r   r  r  base64r   pathlibr   hermes_constantsr  r   r   r  _cleanup_old_screenshotsr'   rm   rq  r:   r   provider_namerk   r  r<   
read_bytes	b64encodedecoderS   rC   rD   r   hermes_cli.configr  floatrB   r	   r  r  r{  rR   r  rv  rl   )!r   r   r   r  r	  uuid_modr   r  r  screenshots_dirr  screenshot_argsr  error_detail_cpr$  actual_screenshot_path
image_dataimage_base64data_urlvision_promptvision_modelvision_timeoutr  _cfg_vtr  rs   r  rv  response_datarK   
error_infos!                                    r   r   r   o  s   (  ;888888~h':::MMM,9 0/////$n%8:OPPO%(Xhnn>N>N>R(X(X(XXOx:dT::: 	!CCCC  	1""<000x(((s?33444%
 
 zz)$$ 	#!::g??L%''C!k77/O9J9J9L9L/O/O/OD: StSS\SS  "# # # #
 "(FB!7!7!;!;F!C!C! 	;"d#9::O %%'' 	#%''C!k77/O9J9J9L9L/O/O/OD: 2/ 2 2T 2 2 2  "# # # # %//11
''
33::7CC:L::@ (@ @ @ 	 )**F__	& 	& 	& 	555555;==D((;++//"==AA)LLC!&s 	 	 	D	  #!'??!,E8;LMM   %
 
  	0#/K **k**$Q'/7=2DDFF666666((22 J$J"?33
 
  	I

62..22=AA 	I+1&>-+HM-(z-e<<<< 
: 
: 
:
 	2AEEE!&1ZRUVWRXRX1Z1Z[[
!!## 	@,/,@,@J()!Jvz*5999999999
:sT   'C<N3 $BN3 8A?N3 8AK N3 
K$!N3 #K$$CN3 3
P:=A2P5/P:5P:r  c                 :   t          |           }t          j                    }|t                              |d          z
  dk     rdS |t          |<   	 t          j                    |dz  z
  }|                     d          D ]g}	 |                                j        |k     r|                                 5# t          $ r&}t          
                    d||           Y d}~`d}~ww xY wdS # t          $ r&}t          
                    d|           Y d}~dS d}~ww xY w)zRemove browser screenshots older than max_age_hours to prevent disk bloat.

    Throttled to run at most once per hour per directory to avoid repeated
    scans on screenshot-heavy workflows.
    g          Nzbrowser_screenshot_*.pngz%Failed to clean old screenshot %s: %sz+Screenshot cleanup error (non-critical): %s)rm   r   r.   r:   globstatst_mtimerU  rB   rC   rD   )r  r  r   nowcutoffrH   rK   s          r   r  r    sS    o

C
)++C
,00c:::TAA+.#C(	G 45 %%&@AA 	L 	LAL6688$v--HHJJJ L L LDaKKKKKKKKL		L 	L  G G GBAFFFFFFFFFGsB   0C* 1B54C* 5
C%?C C*  C%%C* *
D4DDr  c                 R   ddl }	 t          t          j                            dt          j                    dz                      }|dz  }|                                sdS  |j                     | dz  z
  }|                    d          D ]g}	 |                                j	        |k     r|
                                 5# t          $ r&}t                              d||           Y d}~`d}~ww xY wdS # t          $ r&}t                              d	|           Y d}~dS d}~ww xY w)
zIRemove browser recordings older than max_age_hours to prevent disk bloat.r   Nr3   r4   r  r$  zsession_*.webmz$Failed to clean old recording %s: %sz*Recording cleanup error (non-critical): %s)r   r   r!   r9   r:   r;   r<   r%  r&  r'  rU  rB   rC   rD   )r  r   rF   r  r)  rH   rK   s          r   r  r  %  s_   KKKF2:>>-y9PQQRR$';;$$&& 	F 45$$%566 	K 	KAK6688$v--HHJJJ K K KCQJJJJJJJJK		K 	K  F F FA1EEEEEEEEEFsH   AC6 ",C6 1C C6 
C1C,'C6 ,C11C6 6
D& D!!D&c                 H   | d} t                               d|            t                               dt          t                                                               t
          5  t                              |           }ddd           n# 1 swxY w Y   |r|                    dd          }t                               d| |           t          |            	 t          | dg d	
           t                               d|            n3# t          $ r&}t           
                    d| |           Y d}~nd}~ww xY wt
          5  t                              | d           t                              | d           ddd           n# 1 swxY w Y   |rYt                      }|I	 |                    |           n2# t          $ r%}t           
                    d|           Y d}~nd}~ww xY w|                    dd          }|rbt          j                            t%                      d|           }t          j                            |          rt          j                            || d          }t          j                            |          r	 t+          t-          |                                                                                    }t          j        |t4          j                   t                               d||           n># t8          t:          t<          t>          f$ r t                               d|           Y nw xY wtA          j!        |d           t                               d|            dS t                               d|            dS )a  
    Clean up browser session for a task.
    
    Called automatically when a task completes or when inactivity timeout is reached.
    Closes both the agent-browser session and the Browserbase session.
    
    Args:
        task_id: Task identifier to clean up
    Nr   z&cleanup_browser called for task_id: %szActive sessions: %sr   unknownz+Found session for task %s: bb_session_id=%srO  r^   r_   z1agent-browser close command completed for task %sz*agent-browser close failed for task %s: %sz)Could not close cloud browser session: %sr   rO   r#  z.pidzKilled daemon pid %s for %sz?Could not kill daemon pid for %s (already dead or inaccessible)T)ignore_errorsz$Removed task %s from active sessionsz'No active session found for task_id: %s)"rC   rD   r   r   keysr   r:   r  rq  rB   rl   popr   r   close_sessionr!   r"   r&   r   r<   isfilerA   r   	read_textrR   rR  signalSIGTERMProcessLookupError
ValueErrorPermissionErrorr(   r  rmtree)	r   r   r   rK   r   r   
socket_dirpid_file
daemon_pids	            r   r   r   <  s    
LL97CCC
LL&-=-B-B-D-D(E(EFFF 
 5 5'++G445 5 5 5 5 5 5 5 5 5 5 5 5 5 5  .I$(()DDBG][[[ 	g&&&	U '2rBBBBLLLgVVVV 	U 	U 	UNNGRSTTTTTTTT	U  	6 	6  $///"&&w555	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6
  	S*,,H#S**=9999  S S SNN#NPQRRRRRRRRS $'';; 	>&9&;&;=\l=\=\]]Jw~~j)) 
>7<<
|4I4I4IJJ7>>(++ vv%(h)A)A)C)C)I)I)K)K%L%L

FN;;;%BJP\]]]].
OWU v v v%fhtuuuuuvj====;WEEEEE>HHHHHsg   &BBB.D 
D<D77D<7F		FF'F= =
G,G''G,A;L 8MMc                      t           5  t          t                                                    } ddd           n# 1 swxY w Y   | D ]}t	          |           dS )zX
    Clean up all active browser sessions.
    
    Useful for cleanup on shutdown.
    N)r   r   r   r.  r   )task_idsr   s     r   r   r     s     
 1 1(--//001 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ! !    ! !s   ';??c                  v    t           5  t                                          cddd           S # 1 swxY w Y   dS )z
    Get information about active browser sessions.
    
    Returns:
        Dict mapping task_id to session info (session_name, bb_session_id, cdp_url)
    N)r   r   copyr   r   r   get_active_browser_sessionsr@    s}     
 ' '$$&&' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' 's   .22c                      t                      rdS 	 t                       n# t          $ r Y dS w xY wt                      } | |                                 sdS dS )az  
    Check if browser tool requirements are met.

    In **local mode** (no Browserbase credentials): only the ``agent-browser``
    CLI must be findable.

    In **cloud mode** (BROWSERBASE_API_KEY set): the CLI *and* both
    ``BROWSERBASE_API_KEY`` / ``BROWSERBASE_PROJECT_ID`` must be present.
    
    Returns:
        True if all requirements are met, False otherwise
    TF)r   r  r
  r   r   )r   s    r   check_browser_requirementsrB    s{      t   uu #$$HH$:$:$<$<u4s   ! 
//__main__u   🌐 Browser Tool Modulez(========================================r   r  r  z	   Mode: u   ✅ All requirements metu   ❌ Missing requirements:z    - agent-browser CLI not foundzO     Install: npm install -g agent-browser && agent-browser install --with-depsz   - z credentials not configuredzL   Tip: set browser.cloud_provider to 'local' to use free local mode insteadu   
📋 Available Browser Tools:u     🔹 r   z: r   r  r7  u   
💡 Usage:zC  from tools.browser_tool import browser_navigate, browser_snapshotzE  result = browser_navigate('https://example.com', task_id='my_task')z0  snapshot = browser_snapshot(task_id='my_task'))registryc                      i | ]}|d          |S )r   r   )r)  ss     r   
<dictcomp>rG    s    BBBqy!BBBr   r6   c                 r    t          |                     dd          |                    d                    S )Nr   rO   r   )r   r   )r   r:   r  kws     r   r   r     s2    /DHHUB4G4GQSQWQWXaQbQbccc r   u   🌐)r   toolsetschemahandlercheck_fnemojic                     t          |                     dd          |                    d          |                    d                    S )Nr   Fr   rs  )r   r   rs  )r   r:   rI  s     r   r   r     sG    /XXfe$$bffY.?.?266R]K^K^ `  `  ` r   u   📸c                 r    t          |                     dd          |                    d                    S )Nr   rO   r   )r   r   )r   r:   rI  s     r   r   r     .    }%1D1DbffU^N_N_``` r   u   👆c                     t          |                     dd          |                     dd          |                    d                    S )Nr   rO   r   r   )r   r   r   )r   r:   rI  s     r   r   r     sC    |0C0C$((SY[]J^J^hjhnhnoxhyhyzzz r   u   ⌨️c                 r    t          |                     dd          |                    d                    S )Nr   r   r   )r   r   )r   r:   rI  s     r   r   r     s1    ~f8U8U_a_e_efo_p_pqqq r   u   📜c                 H    t          |                    d                    S Nr   r   )r   r:   rI  s     r   r   r     s    |BFF94E4EFFF r   u   ◀️c                 r    t          |                     dd          |                    d                    S )Nr   rO   r   )r   r   )r   r:   rI  s     r   r   r     rR  r   c                 H    t          |                    d                    S rV  )r   r:   rI  s     r   r   r   '  s    }RVVI5F5FGGG r   u   🚪c                 H    t          |                    d                    S rV  )r   r:   rI  s     r   r   r   /  s    1"&&:K:KLLL r   u   🖼️c                     t          |                     dd          |                     dd          |                    d                    S )Nr   rO   r   Fr   )r   r   r   )r   r:   rI  s     r   r   r   7  sg    ~txx
B7O7OZ^ZbZbcmotZuZu  @B  @F  @F  GP  @Q  @Q   R   R   R r   u   👁️c                 r    t          |                     dd          |                    d                    S )Nr   Fr   )r   r   )r   r:   rI  s     r   r   r   ?  s1    TXXgu5M5MWYW]W]^gWhWhiii r   u   🖥️r   )NN)r1   )FNN)FN)r  )r  )r   N)__doc__atexitrk   rV  r!   r  r3  rL  r  r   r   r   r   ri   typingr   r   r   r   r
  r   agent.auxiliary_clientr	   tools.website_policyr
   rB   tools.url_safetyr   r  tools.browser_providers.baser   #tools.browser_providers.browserbaser   #tools.browser_providers.browser_user   tools.tool_backend_helpersr   r  r   r   ImportError	getLogger__name__rC   rF  r   rm   r-   r.   r   r  __annotations__rE   DEFAULT_SESSION_TIMEOUTr  rA   rL   rS   rV   rw   rz   r|   r   r}   r   r   r~   r   r   r   r   r   r   r   r   r   setr   r   r9   r:   r   r   r   r   Lockr   r   registerr   r   r   r   r   BROWSER_TOOL_SCHEMASr   r   r   r  r  rq  r  r  r   r   r   r   r   r   r   r   r   r  r  r   r   r  r  r   r   r@  rB  printr  r  r$  r
  r   rL  tools.registryrD  _BROWSER_SCHEMA_MAPr   r   r   <module>rr     s  0 0 0d    				 				       



        , , , , , , , , , , , ,       + + + + + +,9999999 , , ,++,%<<<<<<< % % %$$LLL% = = = = = = C C C C C C B B B B B B G G G G G G
%IIIIIII % % %$}% 
	8	$	$
C d3i    . 57 c5j!1 6 6 6      $ #c # # # #*C8C= C C C C
Hx} H H H H
.3 .3 . . . .bH3 H H H H '%' ' DdO   
 :> !56 = = =  $ -1 HTN 1 1 1'"X&:; '" '" '" '"T7$sCx.)A 7 7 7 7
/c3h / / / /
) ) ) ) )
?4 
? 
? 
? 
?&T & & & &2!S ! ! ! !& /1 $sDcN*+ 0 0 0355 S        &)S8TV[)\)\%]%] " ,. S%Z( - - -   	  $ $ $B / 0 0 0T T T8  ,p p p ( ( (6c 6 6 6 6 , - - - # F$#Y  	
 	
  # w% $V$  

 

     k$#b  	
 	
   E %#Z 
 %#D 	 	 
 
 & ! x$!6N#8  %

 

   }
 
    k$#_  	
 	
    W
 
  % M
 
  ! s %#~ 
 &$ $T 
 
 $
 
 ( " q%$#U  

 

 SX ~
3 
4S> 
 
 
 
 s tCH~    0 0x} 0S#X 0 0 0 0h7S 7 7 7 7tS Xc]    0 !	I3 I3I3I3 s)I3 c]	I3
 
#s(^I3 I3 I3 I3\  $51 5151}51 	51 51 51 51pI Ic Ic IS I I I I*v v# v v v v v vt !#3 3
3c]3 }3 		3 3 3 3l   s  Xc]  c        F# #c # #x} # # # # #L# #c #HSM #S # # # #L (3- 3    < s Xc] c    <4 48C= 4C 4 4 4 4:0 04 0(3- 03 0 0 0 0f ;C  ;  ;  ;  ;F-3 - - - -7 7 7 7 7 7 7t[: [:S [:D [:8C= [:\_ [: [: [: [:|G G G G0F F F F.CI CIXc] CId CI CI CI CIL	! 	! 	! 	!'T#tCH~*=%> ' ' ' 'D    F z 
E
$%%%	E(OOO



Ck77'G1B1B1D1D'G'G'GD	E
d

 "!## b()))))***	e!!!!  	e 	e 	eE4555Ecddddd	e ?3#4#4#6#6?EJ#++--JJJKKKE`aaa	E
+,,,& K KIvII&*?*DIIIJJJJ	E/	E
OPPP	E
QRRR	E
<=== $ # # # # #BB-ABBB   	12cc'
     	12` `'
     	/``'
     	~.zz'
     	/0qq'
     	~.FF'
     	/``'
     	/GG'
     	34LL'
     	/0 R  R'
     	01ii'
     sH   A A#"A#'A. .A98A9B B'&B'
S S76S7