Ë
    ãß²j'#  ã                  óì   — d Z ddlmZ ddlZddl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h d£«      Zdd„Ze	 G d	„ d
«      «       Ze	 G d„ d«      «       Ze	 G d„ d«      «       Z G d„ de«      Zy)zñAbstract backend interface for computer use. Any implementation (cua-driver over MCP,
pyautogui, noop, future Linux/Windows) returns the shapes below. All methods are synchronous;
async is handled inside the backend implementation if needed.é    )ÚannotationsN)ÚABCÚabstractmethod)Ú	dataclassÚfield)ÚAnyÚDictÚListÚOptionalÚTuple>   éÀ   éÁ   éÂ   éÃ   éÅ   éÆ   éÇ   éÉ   éÊ   éË   éÍ   éÎ   éÏ   c                ó  — | j                  d«      r@t        | «      dk\  r2t        j                  d| dd «      \  }}t	        |«      t	        |«      fS | j                  d«      �r!t        | «      dkD  �rd}|dz   t        | «      k  rÿ| |   d	k7  r|d
z  }Œ| |d
z      |dz   }}|d	k(  r,|t        | «      k  r| |   |d
z   }}|d	k(  r|t        | «      k  rŒ|dv rŒb|dk(  s|dz   t        | «      kD  r	 yt        j                  | ||dz    d«      }|dk  s||z   t        | «      kD  r	 y|t        v rC|dk\  r>t        j                  | |dz   |dz    d«      t        j                  | |dz   |dz    d«      fS ||z  }|dz   t        | «      k  rŒÿy)zØ(width, height) for PNG / JPEG bytes, or None when unreadable. PNG: IHDR. JPEG: walk
    segments (skipping 0xFF fill bytes) to the first SOF marker; stop at SOS. Used by the
    tool layer's provider min-size guard.s   ‰PNG

é   z>IIé   s   ÿØé   é   é	   éÿ   é   >   éØ   éÙ   éÚ   Úbigé   é   é   N)Ú
startswithÚlenÚstructÚunpackÚintÚ
from_bytesÚ_JPEG_SOF_MARKERS)ÚrawÚwidthÚheightÚiÚmarkerÚsegment_lens         ú?/home/ubuntu/.hermes/hermes-agent/tools/computer_use/backend.pyÚimage_dimensions_from_bytesr7      s°  € ð ‡~�~Ð*Ô+´°C³¸B²ÜŸ™ e¨S°°B¨ZÓ8‰ˆˆvÜ�5‹zœ3˜v›;Ð&Ð&Ø
‡~�~�kÕ"¤s¨3£x°!£|ØˆØ�!‰e”c˜#“hÒØ�1‰v˜Š~Ø�Q‘�ØØ˜A ™E™
 A¨¡E�AˆFØ˜D’. Q¬¨S«¢\Ø ™F A¨¡E˜�ð ˜D’. Q¬¨S«£\à˜Ñ%ØØ˜Š~  Q¡¬¨S«Ò!1Øð ô Ÿ.™.¨¨Q¨q°1©u¨°uÓ=ˆKØ˜QŠ ! k¡/´C¸³HÒ"<Øð ð Ô*Ñ*¨{¸aÒ/?Ü—~‘~ c¨!¨a©%°°A±Ð&6¸Ó>ÄÇÁÈsÐSTÐWXÑSXÐYZÐ]^ÑY^ÐO_ÐafÓ@gÐgÐgØ�ÑˆAð! �!‰e”c˜#“hÓð" ó    c                  ó–   — 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<   y)Ú	UIElementz/One interactable element on the current screen.r-   Ú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)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__r?   r@   rA   rB   rC   r   ÚdictrG   rI   © r8   r6   r:   r:   ,   sZ   … á9àƒJØ
ƒIØ€Eˆ3ƒOØ(4€FÐ%Ó4Ø€CˆƒMØ€CˆƒLØ€IˆsÓÙ!&°tÔ!<€J�Ó<ð $(€M�=Ô'r8   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Zded<   dZded<   y)ÚCaptureResultu   Result of a screen capture call. mode="vision" â†’ png_b64 only; mode="ax" â†’ elements
    only; mode="som" (default) â†’ both: the PNG already carries numbered overlays drawn by
    the backend and `elements` holds the matching index â†’ element mapping.r<   Úmoder-   r1   r2   NrH   Úpng_b64rD   zList[UIElement]Úelementsr>   rA   Úwindow_titler   Úpng_bytes_lenÚimage_mime_typeÚnoteÚax_max_elements)rJ   rK   rL   rM   rN   rT   r   ÚlistrU   rA   rV   rW   rX   rY   rZ   rP   r8   r6   rR   rR   >   sr   … ñPð ƒIØƒJØƒKØ!€Gˆ]Ó!Ù %°dÔ ;€HˆoÓ;Ø€CˆƒMØ€L�#ÓØ€M�3Óð &*€O�]Ó)ð €Dˆ#ƒNð €O�SÔr8   rR   c                  óÀ   — 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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Zded<   dZded<   y)ÚActionResultuh  Result of any action (click / type / scroll / drag / key / wait). ``ok`` is
    tool/transport success only â€” NOT the semantic verdict; read ``effect`` / ``escalation``
    (cua-driver's structured verdict) to pick the next rung of the verify â†’ escalate ladder.
    Structured fields are optional and additive: an older driver that omits
    ``structuredContent`` leaves them ``None``, behavior unchanged.

    Beyond the transport-level ``ok`` flag, this carries cua-driver's structured action verdict so the model
    can follow the documented verify â†’ escalate ladder (NousResearch/hermes-agent#67052).
    ÚboolÚokr<   Úactionr>   ÚmessageNzOptional[CaptureResult]ÚcapturerD   rF   ÚmetazOptional[bool]ÚverifiedrH   ÚeffectzOptional[Dict[str, Any]]Ú
escalationÚpathÚdegradedÚdelivery_modeÚcode)rJ   rK   rL   rM   rN   ra   rb   r   rO   rc   rd   re   rf   rg   rh   ri   rj   rP   r8   r6   r]   r]   X   s~   … ñð 	ƒHØƒKØ€GˆSÓØ'+€GÐ$Ó+Ù °Ô6€Dˆ.Ó6Ø#€HˆnÓ#Ø €FˆMÓ à+/€JÐ(Ó/Ø€Dˆ-ÓØ#€HˆnÓ#Ø#'€M�=Ó'Ø€Dˆ-Ôr8   r]   c            
      óž  — e Zd ZdZedd„«       Zedd„«       Zedd„«       Ze	 	 d	 	 	 dd„«       Zedddddddd	d
œ	 	 	 	 	 	 	 	 	 	 	 dd„«       Z	edddddddd	dœ	 	 	 	 	 	 	 	 	 	 	 	 	 dd„«       Z
eddddddd	dœ	 	 	 	 	 	 	 	 	 	 	 dd„«       Zedd	dœ	 	 	 d d„«       Zedd	dœd!d„«       Zed"d„«       Zd"d„Zed#d$d„«       Zed%d&d„«       Zd'd„Zy)(ÚComputerUseBackenda¿  Lifecycle: `start()` before first use, `stop()` at shutdown. Pointer/keyboard actions
    take ``delivery_mode`` (background (default) | foreground) and ``bring_to_front``;
    ``button`` is left | right | middle; ``modifiers`` a list of key names. ``element`` args
    are 1-based SOM indices from a prior capture. `direction` is up | down | left | right and
    `amount` is wheel ticks; `keys` is a combo such as 'cmd+s', 'ctrl+alt+t', 'return'.c                 ó   — y ©NrP   ©Úselfs    r6   ÚstartzComputerUseBackend.startz   s   € Ø r8   c                 ó   — y rn   rP   ro   s    r6   ÚstopzComputerUseBackend.stop}   s   € Ør8   c                 ó   — y rn   rP   ro   s    r6   Úis_availablezComputerUseBackend.is_available€   s   € Ø$'r8   Nc                 ó   — y rn   rP   )rp   rS   rA   rB   rC   s        r6   rb   zComputerUseBackend.captureƒ   s   € àCFr8   Úleftr!   F)ÚelementÚxÚyÚbuttonÚclick_countÚ	modifiersri   Úbring_to_frontc                ó   — y rn   rP   )	rp   rx   ry   rz   r{   r|   r}   ri   r~   s	            r6   ÚclickzComputerUseBackend.click‡   s	   € ð cfr8   )Úfrom_elementÚ
to_elementÚfrom_xyÚto_xyr{   r}   ri   r~   c                ó   — y rn   rP   )	rp   r�   r‚   rƒ   r„   r{   r}   ri   r~   s	            r6   ÚdragzComputerUseBackend.dragŒ   s	   € ð ber8   r(   )Úamountrx   ry   rz   r}   ri   r~   c                ó   — y rn   rP   )	rp   Ú	directionr‡   rx   ry   rz   r}   ri   r~   s	            r6   ÚscrollzComputerUseBackend.scroll’   s	   € ð dgr8   )ri   r~   c                ó   — y rn   rP   )rp   Útextri   r~   s       r6   Ú	type_textzComputerUseBackend.type_text—   s   € àADr8   c                ó   — y rn   rP   )rp   Úkeysri   r~   s       r6   ÚkeyzComputerUseBackend.key›   s   € Øtwr8   c                 ó   — y rn   rP   ro   s    r6   Ú	list_appszComputerUseBackend.list_appsž   s   € Ø14r8   c                ó   — g S )z¦Visible native windows with PID and window identifiers. Optional compatibility hook: backends that
        predate window discovery stay instantiable and report none.rP   ro   s    r6   Úlist_windowszComputerUseBackend.list_windows¡   s	   € ð ˆ	r8   c                 ó   — y rn   rP   )rp   rA   Úraise_windows      r6   Ú	focus_appzComputerUseBackend.focus_app¦   s   € ØORr8   c                 ó   — y rn   rP   )rp   Úvaluerx   s      r6   Ú	set_valuezComputerUseBackend.set_value©   s   € ØTWr8   c           	     óz   — t        j                  t        dt        |d«      «      «       t	        ddd|d›d�¬«      S )	Ng        g      >@TÚwaitzwaited z.2fÚs)r_   r`   ra   )ÚtimeÚsleepÚmaxÚminr]   )rp   Úsecondss     r6   rœ   zComputerUseBackend.wait¬   s7   € Ü�
‰
”3�sœC ¨Ó.Ó/Ô0Ü˜t¨F¸gÀgÈcÀ]ÐRSÐ<TÔUÐUr8   )ÚreturnÚNone)r£   r^   )ÚsomNNN)
rS   r<   rA   rH   rB   úOptional[int]rC   r¦   r£   rR   )rx   r¦   ry   r¦   rz   r¦   r{   r<   r|   r-   r}   úOptional[List[str]]ri   rH   r~   r^   r£   r]   )r�   r¦   r‚   r¦   rƒ   úOptional[Tuple[int, int]]r„   r¨   r{   r<   r}   r§   ri   rH   r~   r^   r£   r]   )r‰   r<   r‡   r-   rx   r¦   ry   r¦   rz   r¦   r}   r§   ri   rH   r~   r^   r£   r]   )rŒ   r<   ri   rH   r~   r^   r£   r]   )r�   r<   ri   rH   r~   r^   r£   r]   )r£   zList[Dict[str, Any]])F)rA   r<   r–   r^   r£   r]   rn   )r™   r<   rx   r¦   r£   r]   )r¢   Úfloatr£   r]   )rJ   rK   rL   rM   r   rq   rs   ru   rb   r€   r†   rŠ   r�   r�   r’   r”   r—   rš   rœ   rP   r8   r6   rl   rl   s   s  „ ñ[ð Ú ó Ø àÚó ØàÚ'ó Ø'àØY]Ø+/ðGØ(ðGØ4AòGó ðGð Ø04ÈÐbfØ"°qÐ[_Ø-1È%ñfØðfØ14ðfØEXðfà*ðfàCGðfàT`òfó ðfð Ø48ÐVZØ26Ð[_Ø!ÀDØ,0ÈñeØ/ðeØ?Xðeàðeà.Aðeð *ðeð CGðeð T`òeó ðeð
 Ø67ÐRVØ"&¸4ÐbfØ.2È5ñgØðgØ+8ðgØL_ðgà+ðgàDHðgàUaògó ðgð ØEIØ).ñEØ"&ðEØ3?òEó ðEð Ø?CÐ\aÔwó ØwàÚ4ó Ø4óð
 ÛRó ØRàÛWó ØWôVr8   rl   )r0   Úbytesr£   r¨   )rM   Ú
__future__r   r+   rž   Úabcr   r   Údataclassesr   r   Útypingr   r	   r
   r   r   Ú	frozensetr/   r7   r:   rR   r]   rl   rP   r8   r6   ú<module>r°      sŒ   ðñAõ #ã Û ß #ß (ß 3Õ 3áÒlÓmÐ óð: ÷(ð (ó ð(ð" ÷ð ó ðð2 ÷ð ó ðô4;V˜õ ;Vr8   