
    9jd                        d Z ddlmZ ddlmZmZ ddlmZmZ ddl	m
Z
mZmZmZmZ e G d d                      Ze G d d	                      Ze G d
 d                      Z G d de          ZdS )zAbstract backend interface for computer use.

Any implementation (cua-driver over MCP, pyautogui, noop, future Linux/Windows)
must return the shape described below. All methods synchronous; async is
handled inside the backend implementation if needed.
    )annotations)ABCabstractmethod)	dataclassfield)AnyDictListOptionalTuplec                      e Zd ZU dZded<   ded<   dZded<   dZd	ed
<   dZded<   dZded<   dZ	ded<    e
e          Zded<   dZded<   ddZdS )	UIElementz/One interactable element on the current screen.intindexstrrole label)r   r   r   r   zTuple[int, int, int, int]boundsappr   pid	window_iddefault_factoryDict[str, Any]
attributesNOptional[str]element_tokenreturnTuple[int, int]c                :    | j         \  }}}}||dz  z   ||dz  z   fS )N   )r   )selfxywhs        @/home/rurouni/.hermes/hermes-agent/tools/computer_use/backend.pycenterzUIElement.center#   s,    [
1a16z1qAv:%%    )r   r    )__name__
__module____qualname____doc____annotations__r   r   r   r   r   r   dictr   r   r)    r*   r(   r   r      s         99JJJIIIEOOOO(4F4444CMMMMCLLLLI!&t!<!<!<J<<<< $(M''''& & & & & &r*   r   c                      e Zd ZU dZded<   ded<   ded<   dZded	<    ee
          Zded<   dZ	ded<   dZ
ded<   dZded<   dZded<   dS )CaptureResultu  Result of a screen capture call.

    At least one of png_b64 / elements is populated depending on capture mode:
      * mode="vision" → png_b64 only
      * mode="ax"     → elements only
      * mode="som"    → both (default): PNG already has numbered overlays
                         drawn by the backend, and `elements` holds the
                         matching index → element mapping.
    r   moder   widthheightNr   png_b64r   zList[UIElement]elementsr   r   window_titler   png_bytes_lenimage_mime_type)r+   r,   r-   r.   r/   r7   r   listr8   r   r9   r:   r;   r1   r*   r(   r3   r3   (   s           IIIJJJKKK!G!!!! %d ; ; ;H;;;;CMMMMLM &*O))))))r*   r3   c                  f    e Zd ZU dZded<   ded<   dZded<   dZd	ed
<    ee          Z	ded<   dS )ActionResultzAResult of any action (click / type / scroll / drag / key / wait).boolokr   actionr   messageNzOptional[CaptureResult]capturer   r   meta)
r+   r,   r-   r.   r/   rB   rC   r   r0   rD   r1   r*   r(   r>   r>   F   sl         KKHHHKKKG (,G++++ 5666D666666r*   r>   c                  n   e Zd ZdZed8d            Zed8d            Zed9d            Zed:d;d            Zed	d	d	ddd	dd<d            Z	ed	d	d	d	dd	dd=d$            Z
ed%d	d	d	d	d&d>d)            Zed?d+            Zed@d-            ZedAd/            ZedBdCd2            ZedDdEd4            ZdFd7Zd	S )GComputerUseBackendz<Lifecycle: `start()` before first use, `stop()` at shutdown.r   Nonec                    d S Nr1   r#   s    r(   startzComputerUseBackend.startW   s     Sr*   c                    d S rI   r1   rJ   s    r(   stopzComputerUseBackend.stopZ   s    Cr*   r?   c                    dS )zReturn True if the backend can be used on this host right now.

        Used by check_fn gating and by the post-setup wizard.
        Nr1   rJ   s    r(   is_availablezComputerUseBackend.is_available]         r*   somNr4   r   r   r   r3   c                    d S rI   r1   )r#   r4   r   s      r(   rC   zComputerUseBackend.capturee   s    VYVYr*   left   )elementr$   r%   buttonclick_count	modifiersrU   Optional[int]r$   r%   rV   rW   r   rX   Optional[List[str]]r>   c                   d S rI   r1   )r#   rU   r$   r%   rV   rW   rX   s          r(   clickzComputerUseBackend.clicki   	     sr*   )from_element
to_elementfrom_xyto_xyrV   rX   r^   r_   r`   Optional[Tuple[int, int]]ra   c                   d S rI   r1   )r#   r^   r_   r`   ra   rV   rX   s          r(   dragzComputerUseBackend.dragu   r]   r*      )amountrU   r$   r%   rX   	directionrf   c                   d S rI   r1   )r#   rg   rf   rU   r$   r%   rX   s          r(   scrollzComputerUseBackend.scroll   r]   r*   textc                    d S rI   r1   )r#   rj   s     r(   	type_textzComputerUseBackend.type_text   s    47Cr*   keysc                    dS )z7Send a key combo, e.g. 'cmd+s', 'ctrl+alt+t', 'return'.Nr1   )r#   rm   s     r(   keyzComputerUseBackend.key   rP   r*   List[Dict[str, Any]]c                    dS )z9Return running apps with bundle IDs, PIDs, window counts.Nr1   rJ   s    r(   	list_appszComputerUseBackend.list_apps   rP   r*   Fraise_windowc                    dS )zJRoute input to `app` (by name or bundle ID). Default: focus without raise.Nr1   )r#   r   rs   s      r(   	focus_appzComputerUseBackend.focus_app   rP   r*   valuec                    dS )zSet a native value on an element (e.g. AXPopUpButton selection).

        `element` is the 1-based SOM index returned by a prior capture call.
        Nr1   )r#   rv   rU   s      r(   	set_valuezComputerUseBackend.set_value   rP   r*   secondsfloatc           	         ddl }|                    t          dt          |d                               t	          ddd|dd	
          S )z#Default implementation: time.sleep.r   Ng        g      >@Twaitzwaited z.2fs)r@   rA   rB   )timesleepmaxminr>   )r#   ry   r~   s      r(   r|   zComputerUseBackend.wait   sU    

3sC..//000tF<Tg<T<T<T<TUUUUr*   )r   rG   )r   r?   )rQ   N)r4   r   r   r   r   r3   )rU   rY   r$   rY   r%   rY   rV   r   rW   r   rX   rZ   r   r>   )r^   rY   r_   rY   r`   rb   ra   rb   rV   r   rX   rZ   r   r>   )rg   r   rf   r   rU   rY   r$   rY   r%   rY   rX   rZ   r   r>   )rj   r   r   r>   )rm   r   r   r>   )r   rp   )F)r   r   rs   r?   r   r>   rI   )rv   r   rU   rY   r   r>   )ry   rz   r   r>   )r+   r,   r-   r.   r   rK   rM   rO   rC   r\   rd   ri   rl   ro   rr   ru   rx   r|   r1   r*   r(   rF   rF   T   s       FF    ^  ^   ^ YYYY ^Y  "&)-	 	 	 	 	 ^	  '+$(-1+/)-	 	 	 	 	 ^	 
 !%)-	 	 	 	 	 ^	 777 ^7F F F ^F H H H ^H Y Y Y Y ^Y     ^V V V V V Vr*   rF   N)r.   
__future__r   abcr   r   dataclassesr   r   typingr   r	   r
   r   r   r   r3   r>   rF   r1   r*   r(   <module>r      sa    # " " " " " # # # # # # # # ( ( ( ( ( ( ( ( 3 3 3 3 3 3 3 3 3 3 3 3 3 3 & & & & & & & &0 * * * * * * * *: 
7 
7 
7 
7 
7 
7 
7 
7WV WV WV WV WV WV WV WV WV WVr*   