
    `gjr                        d Z ddlmZ ddlZddlZddlZddlmZmZ 	 ddl	m
Z
 ej                  j                  d      Zdd	gZd
ZdZdZddZ G d d	e      Z G d d      Zy# e$ r dZ
dZY /w xY w)a  Windows ConPTY bridge for the `hermes dashboard` chat tab.

Drop-in counterpart to ``hermes_cli.pty_bridge.PtyBridge`` for native
Windows. Mirrors the exact public surface the ``/api/pty`` WebSocket
handler in ``hermes_cli.web_server`` consumes: ``spawn``, ``read``,
``write``, ``resize``, ``close``, ``is_available``, plus the
``PtyUnavailableError`` type.

Backed by ``pywinpty`` (already a declared win32 dependency in
pyproject.toml) instead of ``ptyprocess``/``fcntl``/``termios``, none of
which exist on native Windows. The read/write/terminate calls here match
the working winpty usage already shipping in ``tools/process_registry.py``.
    )annotationsN)OptionalSequence)
PtyProcesswinFWinPtyBridgePtyUnavailableError   i  i  c                    	 t        |       }|t        k  rt        S ||kD  r|S |S # t        t        t        f$ r	 t        cY S w xY wN)int	TypeError
ValueErrorOverflowError_MIN_DIMENSION)valuemaximumns      L/root/.hermes/venv/lib/python3.12/site-packages/hermes_cli/win_pty_bridge.py_clampr   )   sO    J 	>7{H z=1 s   % AAc                      e Zd ZdZy)r	   z5Raised when a PTY cannot be created on this platform.N)__name__
__module____qualname____doc__     r   r	   r	   5   s    ?r   c                      e Zd ZdZddZedd       Zeddddd	 	 	 	 	 	 	 	 	 	 	 dd       Zedd	       Z	dd
Z
dddZddZddZddZddZddZy)r   a#  pywinpty-backed bridge with the same interface as ``PtyBridge``.

    ``web_server`` calls :meth:`read` inside ``run_in_executor``, so a
    blocking/polling read here never stalls the event loop. ConPTY exposes
    no selectable fd, so we poll with a short sleep instead of ``select``.
    c                     || _         d| _        y NF)_proc_closed)selfprocs     r   __init__zWinPtyBridge.__init__A   s    
r   c                     t        t              S r   )bool_PTY_AVAILABLE)clss    r   is_availablezWinPtyBridge.is_availableG   s    N##r   NP      )cwdenvcolsrowsc                   t         st        t        d      t        d      |t        j                  j                         n
t        |      }|j                  d      sd|d<   t        j                  t        |      ||||f      } | |      S )Nz=pywinpty is not installed. Install with: pip install pywinptyz'ConPTY is unavailable on this platform.TERMzxterm-256color)r-   r.   
dimensions)
r(   r   r	   osenvironcopydictgetspawnlist)r)   argvr-   r.   r/   r0   	spawn_envr$   s           r   r9   zWinPtyBridge.spawnK   s     !)S  &&OPP*-+RZZ__&49	}}V$ 0If Jd|	
 4yr   c                @    t        | j                  j                        S r   )r   r!   pidr#   s    r   r>   zWinPtyBridge.pidh   s    4::>>""r   c                    | j                   ry	 t        | j                  j                               S # t        $ r Y yw xY wr    )r"   r'   r!   isalive	Exceptionr?   s    r   is_alivezWinPtyBridge.is_alivel   s;    <<	

**,-- 		s   "2 	>>c                   | j                   ry	 | j                  j                  d      }|s t        j                  t        |d             yt        |t              r|S |j                  dd      S # t        $ r Y yt        $ r Y yw xY w)zUp to 64 KiB of child output.

        Returns bytes, ``b""`` when nothing is available this tick, or
        ``None`` once the child has exited (EOF).
        Ni   g{Gz?r   utf-8replaceerrors)r"   r!   readEOFErrorrB   timesleepmin
isinstancebytesencode)r#   timeoutdatas      r   rI   zWinPtyBridge.readv   s     <<	::??5)D
  JJs7D)*dE"K
 {{79{55  	 		s   A1 1	B<BBc                    | j                   s|sy 	 | j                  j                  |j                  dd             y # t        $ r Y y w xY w)NrE   rF   rG   )r"   r!   writedecoderB   )r#   rR   s     r   rT   zWinPtyBridge.write   sD    <<t	JJT[[[CD 		s   ,> 	A
	A
c                    | j                   ry t        |t              }t        |t              }	 | j                  j                  ||       y # t        $ r Y y w xY wr   )r"   r   	_MAX_COLS	_MAX_ROWSr!   
setwinsizerB   )r#   r/   r0   s      r   resizezWinPtyBridge.resize   sO    <<dI&dI&	JJ!!$- 		s   A 	AAc                    | j                   ry d| _         	 | j                  j                  d       y # t        $ r Y y w xY w)NT)force)r"   r!   	terminaterB   r?   s    r   closezWinPtyBridge.close   s?    <<	JJ  t , 		s   3 	??c                    | S r   r   r?   s    r   	__enter__zWinPtyBridge.__enter__   s    r   c                $    | j                          y r   )r^   )r#   _excs     r   __exit__zWinPtyBridge.__exit__   s    

r   )r$   z'PtyProcess'returnNone)rd   r'   )r;   zSequence[str]r-   zOptional[str]r.   zOptional[dict]r/   r   r0   r   rd   'WinPtyBridge')rd   r   )g?)rQ   floatrd   zOptional[bytes])rR   rO   rd   re   )r/   r   r0   r   rd   re   )rd   re   )rd   rf   )r   r   r   r   r%   classmethodr*   r9   propertyr>   rC   rI   rT   rZ   r^   r`   rc   r   r   r   r   r   9   s     $ $ 
 "" 	
    
 8 # #66r   )r   r   r   r   rd   r   )r   
__future__r   r4   sysrK   typingr   r   winptyr   platform
startswithr(   ImportError__all__r   rW   rX   r   RuntimeErrorr	   r   r   r   r   <module>rs      s    # 	 
  %!\\,,U3N 0
1 			@, @z zA  JNs   !A# #	A/.A/