
    `gj0                        d Z ddlm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  eh d      ZdZddZddZdd	Zdd
ZddZddZdddZdddZy)u  
Cross-platform Computer Use readiness + macOS permission helpers.

cua-driver runs on macOS, Windows, and Linux, but "ready to drive" means
something different on each:

  * macOS — explicit TCC grants (Accessibility + Screen Recording). cua-driver
    reports/requests them via ``permissions status`` / ``permissions grant``.
    The grants attach to cua-driver's OWN identity (``com.trycua.driver`` /
    the installed ``CuaDriver.app``), NOT Hermes — so no Hermes entitlement is
    involved, and ``grant`` launches CuaDriver via LaunchServices so the macOS
    dialog is attributed correctly.
  * Windows — no TCC toggles; the UIAccess worker (``cua-driver-uia.exe``) may
    trip a SmartScreen prompt on first run. Readiness == driver health.
  * Linux — assistive control via the X11/XWayland stack. Readiness == driver
    health.

The universal signal on every platform is ``cua-driver doctor --json`` (binary
integrity + platform support). ``computer_use_status`` folds that together with
the macOS permission detail into one payload for the desktop card, the
``hermes computer-use permissions`` CLI, and ``/api/tools/computer-use/status``.
    )annotationsN)AnyDictListOptional>   linuxwin32darwin)accessibilityscreen_recordingscreen_recording_capturablec                    | r| S 	 ddl m}  |       S # t        $ r5 t        j                  j                  dd      j                         xs dcY S w xY w)Nr   )_cua_driver_cmdHERMES_CUA_DRIVER_CMD z
cua-driver)hermes_cli.tools_configr   	Exceptionosenvirongetstrip)overrider   s     Q/root/.hermes/venv/lib/python3.12/site-packages/tools/computer_use/permissions.py_driver_cmdr   &   sQ    S;   Szz~~5r:@@BRlRSs    ;AAc                     	 ddl m}   |        }	 ddlm}  ||      S # t        $ r t        t        j
                        }Y 3w xY w# t        $ r |cY S w xY w)u  cua-driver child env: telemetry opt-in policy + secret sanitization.

    cua-driver is a third-party binary — it must never inherit provider
    API keys (#53503/#55709/#58889 lineage). Each layer degrades
    gracefully so permission probes never break on a helper import error.
    r   )cua_driver_child_env)_sanitize_subprocess_env)tools.computer_use.cua_backendr   r   dictr   r   tools.environments.localr   )r   envr   s      r   
_child_envr"   1   sY    G"$E',,  2::  
s     A "AAAAc               j    t        j                  | g|dd|t               t         j                        S )NT)capture_outputtexttimeoutr!   stdin)
subprocessrunr"   DEVNULL)binaryr&   argss      r   _runr-   F   s3    >>	$L       c                   t        | g|d|ij                  xs dj                         }|rt        j                  |      S dS )zIRun ``binary args`` and parse stdout as JSON, or ``None`` on any failure.r&   r   N)r-   stdoutr   jsonloads)r+   r&   r,   raws       r   	_json_outr4   Q   s?    //w/66<"
C
C
EC!4::c?+t+r.   c                   	 t        | ddd      }t        |t              sy|j	                  dg       D cg c]c  }t        |t              rQt        |j	                  dd            t        |j	                  d	d            t        |j	                  d
d            de }}t        |j	                  d            |dS # t        $ r Y yw xY wc c}w )uK   ``cua-driver doctor --json`` → ``{ok, checks:[{label,status,message}]}``.doctor--json   r&   Nprobeslabelr   statusmessage)r;   r<   r=   ok)r>   checks)r4   r   
isinstancer   r   strbool)r+   datapr?   s       r   _doctorrE   W   s    8R@ dD! (B'$ a w+,!%%"-.155B/0	
$F $ txx~&&99  $s   B= A(C=	C	C	c           
        	 t        | dddd      }t	        |t
              rp|j                  t        D ci c](  }t	        |j                  |      t              s#|||   * c}       t	        |j                  d
      t
              r	|d
   |d
<   yyy# t        j                  $ r d|d<   Y yt        $ r}d	| |d<   Y d}~yd}~ww xY wc c}w )zDFold ``cua-driver permissions status --json`` booleans into ``out``.permissionsr<   r7   
   r9   z'cua-driver permissions status timed outerrorNz&cua-driver permissions status failed: source)
r4   r(   TimeoutExpiredr   r@   r   update_BOOLSr   rB   )r+   outrC   excks        r   _mac_permissionsrQ   k   s    (BO $

P1*TXXa[$2OAtAwJPQdhhx($/ NCM 0  $$ @G ?uEG Qs(   B $C
C
C-C5CCc           
        t         j                  }t        j                  t	        |             }||t
        v t        |      dd|dk(  g ddd	t        D ci c]  }|d c}}|s|S 	 t        |dd      j                  xs dj                         xs d|d<   t        |      }||d	   |d	<   |dk(  r&t        ||       |d
   |d   du xr |d   du |d<   |S ||d   |d<   |S c c}w # t        $ r Y \w xY w)a!  Unified, OS-aware Computer Use readiness for the desktop card.

    ``ready`` is the single signal the UI keys off: on macOS it's both TCC
    grants; elsewhere it's driver health (no TCC model). ``None`` means
    unknown (binary missing / probe failed). ``can_grant`` is macOS-only.
    Nr
   )	platformplatform_supported	installedversionready	can_grantr?   rJ   rI   z	--version   r9   r   rV   r?   rI   r   Tr   rW   r>   )sysrS   shutilwhichr   _RUNTIME_PLATFORMSrB   rM   r-   r0   r   r   rE   rQ   )
driver_cmdplatr+   rP   rN   r6   s         r   computer_use_statusr`   {   s>    <<D\\+j12F"&88&\X% #
#q1d7
#C 
v{A>EEKRRT\X\I V_Fx(Hx%w</47[C@R<SW[<[CL J 
	d|GJ+ $  s   
C$'1C) )	C54C5c                   t         j                  dk7  rt        d       yt        j                  t        |             }|st        d       yt        d       	 t        t        j                  |ddgt               t        j                  	      j                        S # t        $ r Y y
t        $ r(}t        d| t         j                         Y d}~yd}~ww xY w)a\  Run ``cua-driver permissions grant`` (macOS); stream its output.

    Launches CuaDriver via LaunchServices so the TCC dialog is attributed to
    ``com.trycua.driver``, then waits for the grant. Returns the driver's exit
    code (0 ok), 2 if the binary is missing, 64 on a non-macOS platform (which
    has no TCC permission model to grant).
    r
   zDComputer Use permissions are a macOS concept; nothing to grant here.@   z;cua-driver: not installed. Run: hermes computer-use install   u   Requesting Accessibility + Screen Recording for CuaDriver.
macOS will show a dialog attributed to CuaDriver (com.trycua.driver) — approve it, then return here.rG   grant)r!   r'      z%cua-driver permissions grant failed: )fileN)rZ   rS   printr[   r\   r   intr(   r)   r"   r*   
returncodeKeyboardInterruptr   stderr)r^   r+   rO   s      r   request_permissions_grantrl      s     ||xTU\\+j12FKL		(
NN0L (( j
 	
   5cU;#**Ms   AB 	C'C/CC)r   Optional[str]returnrA   )rn   zDict[str, str])r+   rA   r,   rA   r&   floatrn   zsubprocess.CompletedProcess)r+   rA   r,   rA   r&   ro   rn   r   )r+   rA   rn   zOptional[Dict[str, Any]])r+   rA   rN   Dict[str, Any]rn   None)N)r^   rm   rn   rp   )r^   rm   rn   rh   )__doc__
__future__r   r1   r   r[   r(   rZ   typingr   r   r   r   	frozensetr]   rM   r   r"   r-   r4   rE   rQ   r`   rl    r.   r   <module>rw      sa   . #  	   
 , , ;< 	MS*,:(+ (V"r.   