o
    `iI,                     @   s  d Z ddlZddlZddlZddlmZ ddlZedZedZdd Z	dd	d
dddddddddgddddd
ddddidgddddd
ddddidgddddd
dddddddddgddd d!d
dd"ddd#d$dd%d&gddd'd(d
i d)dd*d+d
dd,ddd-ddd.d/gddd0d1d
d&dd2did&gddgZ
d3d4 Zd5d6 Zi Zd<d8d9Zd:d; ZdS )=up   
Chat agent backend — calls Claude API with tools for agent orchestration.
Loaded by serve.py as a blueprint.
    N)Pathz/data/cameron/para/.agentsz/data/cameron/parac            	      C   s   t d t d td d td d d} d}|  D ]\}}| r-|d| d	|  7 }qd}tt D ]%}|d
 }|d }| r[| rN|  nd}|d|j d| 7 }q6d| d| dS )Nz	CLAUDE.mdzscientist.MDsharedzGUIDELINES.mdzREPORT_FORMAT.md)zCLAUDE.md (project overview)z&scientist.MD (verification guidelines)z&GUIDELINES.md (communication protocol)z#REPORT_FORMAT.md (report structure) z

--- z ---
zROLE.md	status.mdunknownz
- **z**: status=a  You are the PARA project manager chat assistant. You help Cameron coordinate experiments, analyze results, and manage the agent team.

You have access to tools to interact with experiment agents running in tmux tabs, read files, and run commands.

ACTIVE AGENTS:z

PROJECT CONTEXT:z

Be concise and direct. When asked about experiment status, use your tools to check the latest state rather than relying on stale information.)	PROJECT_ROOT
AGENTS_DIRitemsexists	read_textsortediterdirstripname)	Zcontext_filescontextlabelpathZagent_status	agent_dirZ	role_filestatus_filestatus r   */data/cameron/para/.agents/reports/chat.py_load_system_prompt   s.   

r   	send_taskzoSend a task/message to an agent via tmux. The agent will see it as a user message in their Claude Code session.objectstringz*Agent name: backbones, vid_model, or droid)typedescriptionzThe task or message to send)agentmessager   r   )r   
propertiesrequired)r   r   Zinput_schemaread_outboxz9Read an agent's outbox for their latest results/response.z
Agent nameget_agent_statusz=Get an agent's current status (idle, working, done, blocked).capture_panezSCapture recent terminal output from an agent's tmux pane to see what they're doing.integerz'Number of lines to capture (default 30)   )r   r   default)r   lines	read_filez Read a file from the filesystem.zAbsolute file pathzMax lines to read (default 200)   )r   	max_linesr   list_reportsz-List all reports on the experiment dashboard.)r   r    run_commandzxRun a shell command. Use for checking GPU status, disk usage, process lists, etc. Do NOT use for destructive operations.zShell command to runzTimeout in seconds (default 30))commandtimeoutr.   list_directoryzList contents of a directory.zDirectory pathc                 C   s^   t jg dddd}|j dD ]}|dd}t|dkr,|d | kr,|d   S qd	S )
zFind tmux window by name.)tmuxzlist-windowsz-az-Fz.#{session_name}:#{window_index} #{window_name}Tcapture_outputtext
       r   N)
subprocessrunstdoutr   splitlen)Z
agent_nameresultlinepartsr   r   r   _find_window   s   rA   c              
      s  z| dkra|d }|d }t | d }ddlm} | d}t|d}|d	| d
| d W d   n1 s=w   Y  t|}|rZtddd||dg d| dW S d| dW S | dkr|d }t | d }	|		 r|	
  }
|
r~|
W S dW S W dS | dkr|d }t | d }|	 r|
  W S W dS | dkr|d }|dd}t|}|sd| d W S tjdd!d|d"d#d$| gd%d%d&}|jpd'W S | d(krt|d) }|d*d+}|	 sd,| W S |
 dd| }d|W S | d-krXt d. }g }t| D ]<}| rJ|jd/vrJt|d0d%d1D ]$}ddlm} || jd2}||j d3|j d4| d5 q%q|rUd|W S d6W S | d7kr|d8  |d9d}g d:}t fd;d<|D rzW d=S tj d%d%d%|ttd>}|j|j }|r|dd? W S d@W S | dAkrt|d) }| sdB| W S g }t| D ]}| rdCndD}|| |j  qd|ddE W S dF|  W S  ty } zdGt| W  Y d}~S d}~ww )Hz,Execute a tool and return the result string.r   r   r   zinbox.mdr   )datetimez%Y-%m-%d %H:%M:%Sau   
---
## Task — z

r5   Nr1   z	send-keysz-tZEnterzTask sent to z via tmux and written to inbox.zTask written to z$/inbox.md but tmux window not found.r"   z	outbox.mdz(outbox is empty)z(no outbox file)r#   r   r   r$   r(   r&   ztmux window 'z' not foundzcapture-panez-pz-S-Tr2   z(empty pane)r)   r   r+   r*   zFile not found: r,   reports)Zmediaz*.html)reversez%Y-%m-%d %H:%M/z ()zNo reports yet.r-   r.   r/   )zrm -rfZmkfszdd if=z> /dev/zkill -9shutdownZrebootc                 3   s    | ]}| v V  qd S )Nr   ).0dcmdr   r   	<genexpr>   s    zexecute_tool.<locals>.<genexpr>z)Blocked: potentially destructive command.)shellr3   r4   r/   cwdi  z(no output)r0   zNot a directory: u   📁 u   📄 d   zUnknown tool: zError: )r   rB   nowstrftimeopenwriterA   r9   r:   r
   r   r   getr;   r   r<   joinr   r   is_dirr   globfromtimestampstatst_mtimeappendanystrr   stderr	Exception)r   Z
input_datar   r   ZinboxrB   	timestampftargetZoutboxcontentr   r(   r>   r   r+   Zreports_dirrE   r   dater/   Z	dangerousoutputentriesitemprefixer   rL   r   execute_tool   s   
$


rl   claude-sonnet-4-20250514c                 C   s   t  }| tvrg t| < t|  }|d|d t }	 |jj|d|t|d}|j}|d|d dd |D }|sGd	d |D }	d
	|	S g }
|D ]}t
|j|j}|
d|j|d qK|d|
d q)z^
    Handle a chat message. Returns response text.
    Handles tool use loops internally.
    user)Zrolere   Ti   )modelZ
max_tokenssystemZtoolsmessagesZ	assistantc                 S   s   g | ]	}|j d kr|qS )tool_use)r   rJ   br   r   r   
<listcomp>  s    zchat.<locals>.<listcomp>c                 S   s   g | ]
}|j d kr|jqS )r4   )r   r4   rs   r   r   r   ru      s    r5   Ztool_result)r   Ztool_use_idre   )	anthropicZ	Anthropicconversationsr]   r   rq   createTOOLSre   rW   rl   r   inputid)
session_idZuser_messagero   clientrq   Zsystem_promptresponseZassistant_contentZ	tool_usesZ
text_partsZtool_resultsrr   r>   r   r   r   chat   s<   	

r   c                 C   s   | t v r	t | = dS dS )z)Clear conversation history for a session.N)rw   )r|   r   r   r   clear_conversation1  s   
r   )rm   )__doc__jsonr9   timepathlibr   rv   r   r   r   ry   rA   rl   rw   r   r   r   r   r   r   <module>   s    $



	

]`
1