
    (Gj                     ,   U d Z ddlZddlZddlmZmZ ddlmZ daeee	e	e	gdf                  e
d<   deee	e	e	gdf                  ddfdZdee	         fd	Zd
ee	         dee	         de	ddfdZde	fdZd(d
ee	         de	fdZd)de	dee	         d
ee	         de	fdZd(de	d
ee	         de	fdZ ej        dddddi ddd             ej        dddddddddd dd!dgd"dd#             ej        d$dd$d%dddd&didgd"dd'            dS )*u  Project tools — the agent's INTENTIONAL handle on first-class Projects.

Projects (per-profile ``projects.db``) are the named workspaces the desktop
sidebar groups sessions into. Creating / switching a project is a deliberate act
expressed as explicit tools — never a side effect of a terminal ``cd``.

Exposed only on GUI sessions: the tools live in the `project` toolset (kept off
``_HERMES_CORE_TOOLS``) which the desktop/TUI gateway folds into its resolved
toolsets, so no CLI/messaging/cron schema carries them. The GUI also wires
``set_project_workspace_callback`` so a create/switch re-anchors the live
session's cwd and the sidebar follows the move; the DB write is the durable part.
    N)CallableOptional)registry_workspace_callbackfnreturnc                 
    | a d S N)r   )r   s    9/home/rurouni/.hermes/hermes-agent/tools/project_tools.pyset_project_workspace_callbackr      s        c                     t          | dd           r| j        S | j        D ]}|j        r	|j        c S | j        r| j        d         j        nd S )Nprimary_pathr   )getattrr   folders
is_primarypath)projfolders     r   _primary_pathr   !   si    t^T** !  ,   	;	#'<94<?T9r   task_idr   namec                 j    t           }|r%| r%|r%	  || ||           d S # t          $ r Y d S w xY wd S d S d S r
   )r   	Exception)r   r   r   cbs       r   _apply_workspacer   *   s|    	B	 g $ 	Bwd##### 	 	 	DD	     s    
,,tokenc                 j   ddl m} |pd                                }|sd S |                    | d          }|D ]!}||j        |j        fv s|j        |k    r|c S "|                                }|D ]@}|j                                        |k    s|j                                        |k    r|c S Ad S )Nr   projects_db T)include_archived)
hermes_clir    striplist_projectsidslugr   lower)connr   pdbprojectsr   lows         r   _resolver-   3   s    ------[b!!E t   ==H  TWdi(((DI,>,>KKK -?
++--C  9??##ty'8'8C'?'?KKK (@4r   c                    ddl m} |                                5 }|                    |          |                    |          }d d d            n# 1 swxY w Y   t          j        fd|D             d          S )Nr   r   c                 j    g | ]/}|j         |j        |j        t          |          |j         k    d 0S ))r&   r'   r   r   active)r&   r'   r   r   ).0pr0   s     r   
<listcomp>z project_list.<locals>.<listcomp>N   sU     	
 	
 	
  d -a 0 0$&. 	
 	
 	
r   )	active_idr+   )r#   r    connect_closingget_active_idr%   jsondumps)r   r*   r)   r+   r0   s       @r   project_listr9   E   s    ------					 +$""4(($$T**+ + + + + + + + + + + + + + + :	
 	
 	
 	
 	
 	
 	
    s   +AAAc                 L   | pd                                 } | st          j        ddd          S ddlm} |pd                                 }|r<t
          j                            t
          j                            |                    }	 |	                                5 }|
                    || |r|gng |pd           }|                    ||           |                    ||          }d d d            n# 1 swxY w Y   n;# t          $ r.}t          j        dt          |          d          cY d }~S d }~ww xY w|t          j        ddd          S t          |          }	t!          ||	|j                   t          j        d	|j        |j        |j        |	d
          S )Nr!   Fzname is requiredsuccesserrorr   r   )r   r   r   zproject vanished after createTr<   r&   r'   r   r   )r$   r7   r8   r#   r    osr   abspath
expanduserr5   create_project
set_activeget_project
ValueErrorstrr   r   r   r&   r'   )
r   r   r   r*   r   r)   pidr   excprimarys
             r   project_createrJ   [   s   JBD Kze6HIIJJJ------jb!!F =!3!3F!;!;<<A  "" 	.d$$T&>XvhhVXgmguqu$vvCNN4%%%??4--D	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	.  A A Azec#hh??@@@@@@@@A |ze6UVVWWWD!!GWgty111:$dgtyRVR[mtuuvvvsC   D AC8,D 8C<<D ?C< D 
D<#D71D<7D<projectc                    ddl m} |                                5 }t          ||           }|'t	          j        dd|  dd          cd d d            S |                    ||j                   d d d            n# 1 swxY w Y   t          |          }t          |||j
                   t	          j        d|j        |j        |j
        |d          S )	Nr   r   Fzno project matching ''r;   Tr>   )r#   r    r5   r-   r7   r8   rC   r&   r   r   r   r'   )rK   r   r*   r)   r   rI   s         r   project_switchrN   w   s8   ------					 &$g&&<:%:\RY:\:\:\]]^^& & & & & & & & 	tTW%%%	& & & & & & & & & & & & & & & D!!GWgty111:$dgtyRVR[mtuuvvvs   -A<A<<B B r9   zEList the desktop Projects (named workspaces) and which one is active.object)type
properties)r   description
parametersc                 H    t          |                    d                    S )Nr   )r   )r9   getargskws     r   <lambda>rY      s    |BFF94E4EFFF r   )r   toolsetschemahandlerrJ   u  Create a desktop Project (a named workspace) and switch this chat into it. Pass `path` to anchor it to a repo/folder — this chat's workspace moves there and the sidebar follows. Use when starting work in a new repo/folder; this is the intentional way to move the session, not `cd`.stringzHuman name, e.g. 'Aurora Demo')rP   rR   z,Primary repo/folder to anchor the project to)r   r   )rP   rQ   requiredc                     t          |                     dd          |                     d          |                    d                    S )Nr   r!   r   r   )r   r   r   )rJ   rU   rV   s     r   rY   rY      sB    ~XXfb!!(8(8"&&BSBS      r   rN   zSwitch this chat into an existing desktop Project (by name, slug, or id). Moves the session's workspace to the project's primary folder and the sidebar follows. The intentional way to move between projects, not `cd`.zProject name, slug, or idc                 r    t          |                     dd          |                    d                    S )NrK   r!   r   )rK   r   )rN   rU   rV   s     r   rY   rY      s1    ~dhhy"6M6MWYW]W]^gWhWhiii r   r
   )NN)__doc__r7   r?   typingr   r   tools.registryr   r   rF   __annotations__r   r   r   r-   r9   rJ   rN   register r   r   <module>rg      s      				 % % % % % % % % # # # # # # BF XhS#'<=> E E Ex#sC$9N0O'P UY    
:8C= : : : :hsm 8C=  PT    #    $ (3- 3    ,w w wHSM w8C= w\_ w w w w8w wC w(3- w3 w w w w  	^'r:: 
 GF	 	 	 	  	 A !):Z[[!):hii   
 
 " )   2  	 O
 H=XYY #
 
  ji%     r   