
    
1i                        d dl Z d dlZd dlZd dlmZmZmZ d dlZ ej                  e	      Z
 G d d      Ze	dk(  rE ej                  ej                          e       Z ed       ej!                         Z ee       yy)    N)ListDictOptionalc                       e Zd ZdZddedefdZddee   dee   dej                  fd	Z
d
edefdZdefdZddededefdZy)JulesBridgez
    Programmatic bridge to interact with the @google/jules CLI.
    Allows Genesis to autonomously spin up Deep Think sessions and pull insights.
    repo_dir	repo_namec                 `    || _         || _        t        j                  dk(  rdnd}|dg| _        y )Nntznpx.cmdnpxz@google/jules@latest)r   r	   osnamebase_cmd)selfr   r	   npx_execs       5/mnt/e/genesis-system/genesis_v2/core/jules_bridge.py__init__zJulesBridge.__init__   s.     " "49U!#9:    Nargs
input_datareturnc           	      :   | j                   |z   }t        j                  ddj                  |              	 t	        j
                  || j                  |r|j                  d      nddddd      }|S # t        $ r}t        j                  d|         d}~ww xY w)	z4Helper to run Jules CLI commands and capture output.zRunning Jules command:  zutf-8NTF)cwdinputcapture_outputtextencodingcheckz!Failed to execute Jules command: )
r   loggerinfojoin
subprocessrunr   encode	Exceptionerror)r   r   r   cmdresultes         r   _run_commandzJulesBridge._run_command   s    mmd"-chhsm_=>	  ^^MM4>j''0D# F M 	LL<QC@A	s   :A3 3	B<BBpromptc                 4   t         j                  d       dd| j                  g}| j                  ||      }|j                  dk7  r0t         j                  d|j                   d|j                          yt         j                  d	|j                          y
)z
        Spins up a new Jules session.
        Since jules new accepts prompt via stdin to avoid escaping issues, we use input_data.
        z'Creating new Jules Strategic Session...newz--repo)r   r   z"Failed to create session:
STDOUT:
z	
STDERR:
FzSession creation output:
T)r    r!   r	   r+   
returncoder'   stdoutstderr)r   r,   r   r)   s       r   create_sessionzJulesBridge.create_session+   s    
 	=> x0""4F";!LL?kZ`ZgZgYhij0@Ar   c                     g d}| j                  |      }|j                  dk7  r#t        j                  d|j                          y|j
                  S )z
        Returns the raw output of `jules remote list --session`.
        Parsing the table output is tricky without JSON output, so returning string for now.
        )remotelist	--sessionr   zFailed to list sessions:
 )r+   r/   r    r'   r1   r0   )r   r   r)   s      r   list_sessionszJulesBridge.list_sessions>   sL    
 /""4(!LL5fmm_EF}}r   
session_idapply_patchc                     dddt        |      g}|r|j                  d       | j                  |      }|j                  dk7  r&t        j                  d| d|j                          y|j                  S )	zs
        Pulls the output of a given session.
        Can optionally apply the patch directly to the repo.
        r4   pullr6   z--applyr   zFailed to pull session z:
r7   )strappendr+   r/   r    r'   r1   r0   )r   r9   r:   r   r)   s        r   pull_sessionzJulesBridge.pull_sessionJ   sn    
 &+s:?KK	"""4(!LL2:,c&--QR}}r   )ze:/genesis-systemzKinan27/genesis-system)N)F)__name__
__module____qualname____doc__r=   r   r   r   r#   CompletedProcessr+   boolr2   r8   r?    r   r   r   r   	   s|    ; ;s ;c  Q[QlQl ,S T &
s 
s  # r   r   __main__)levelzListing current sessions:)r#   jsonloggingtypingr   r   r   r   	getLoggerr@   r    r   basicConfigINFObridgeprintr8   sessionsrF   r   r   <module>rR      s~       ' ' 				8	$N N` zGgll+]F	
%&##%H	(O r   