
    `gj                        d Z ddlmZ ddlZddlmZmZ ddlmZ	 ddZ
ddZddd	 	 	 	 	 dd	Zdd
ZddZddZddZddZddZy)u  Shared handlers for the /memory and /skills write-approval subcommands.

Both the interactive CLI (``cli.py``) and the gateway (``gateway/run.py``) call
into this module so the pending-review UX (list / approve / reject / diff /
mode) lives in one place. Each caller owns only its surface concerns:
formatting the returned text and, for the gateway, persisting config + evicting
the cached agent on a mode change.

Every public handler returns a plain text string suitable for both a terminal
and a chat message. Skill diffs are intentionally NOT inlined here — the
``diff`` handler returns the full diff for the CLI pager, but on a messaging
platform the gateway truncates it and points the user at the dashboard / file.
    )annotationsN)ListOptional)write_approvalc                F    t        j                  |       }|  d|rd S d S )Nz.write_approval = onoff)wawrite_approval_enabled)	subsystemr   s     U/root/.hermes/venv/lib/python3.12/site-packages/hermes_cli/write_approval_commands.py
_fmt_stater      s2    		"	"9	-B[*24*ABB5*ABB    c                   t        j                  |       }|sd|  dS d|  dt        |       dg}|D ]I  }|j                  dd      }|dk(  rd	nd
}|j	                  d|d    | d|j                  dd
              K dj                  |       }|j	                  d
       |j	                  d| d|  d       | t         j                  k(  r|j	                  d       dj                  |      S )NNo pending  writes.zPending z	 writes (z):origin
foregroundbackground_reviewz [auto]   idsummaryz/{s} approve <id>)szApply: z   Reject: / reject <id>z#Review full diff: /skills diff <id>
)r
   list_pendinglengetappendformatSKILLSjoin)r   recordslinesrr   tagwheres          r   _fmt_pending_listr)   !   s    ooi(GYKx00	{)CL><=E Bx.!%88ibr!D'3%r!%%	2*>)?@AB  &&&3E	LL	LL75'i[EFBII:;99Ur   )memory_storeset_mode_fnc               6   |st        |        dt        |       z   S |d   j                         }|dd }|dk(  rt        |       S |dv rt        | ||      S |dv rt	        | |      S |dk(  r| t
        j                  k(  rt        |      S |d	v rt        | ||      S y)
a  Dispatch a /memory or /skills subcommand.

    Args:
        subsystem: ``memory`` or ``skills``.
        args: tokens after the slash command (e.g. ``["approve", "a1b2"]``).
        memory_store: live MemoryStore for applying approved memory writes
            (CLI passes ``self.agent._memory_store``; gateway applies against a
            freshly loaded store).
        set_mode_fn: optional callable ``(enabled: bool) -> None`` that
            persists the new write_approval boolean to config (gateway provides
            this; CLI uses its own ``save_config_value`` and passes a closure).

    Returns a text string to show the user. Returns None when the args are not
    a write-approval subcommand (caller falls through to its other handling,
    e.g. /skills search).
    z

r      Npending>   applyapprove>   denydroprejectdiff>   modeapproval)	r   r)   lower_approve_rejectr
   r"   _diff_set_approval)r   argsr*   r+   subrests         r   handle_pending_subcommandr?   6   s    . Y'(-0A)0LLL
q'--/C8D
i ++
""	466
((y$''
f}bii/T{
""Yk::r   c                $    |sd d|  dfS |d   d fS )NUsage: /z  approve|reject <id>  (or 'all')r    )r   r>   s     r   _resolve_onerC   f   s(    x	{*JKKK7D=r   c                2   t        | |      \  }}|s|
|xs d|  dS t        j                  |       }|sd|  dS |j                         dk(  rt	        |      }n$t        j
                  | |      }|s	d|  d| dS |g}dg }	}|D ]L  }t        | ||      \  }
}|
rt        j                  | |d	          |d
z  }4|	j                  |d	    d|        N d| d|  dg}|	r)|j                  d       |j                  d |	D               dj                  |      S )NrA   z approve <id>r   r   all write with id ''.r   r   r-   : z	Approved  
 write(s).zFailed:c              3  &   K   | ]	  }d |   yw)r   NrB   ).0fs     r   	<genexpr>z_approve.<locals>.<genexpr>   s     ,Rs8,s   r   )rC   r
   r   r7   listget_pending
_apply_onediscard_pendingr    extendr#   )r   r>   r*   targeterrr$   targetsrecappliedfailedokmsgouts                r   r8   r8   l   sA   y$/KFC
fn9=99ooi(GYKx00||~w-nnY/ +;F82FF%VG 1Y\:Cy#d)4qLGMMSYKr#/01 wiq:6
7C

9

,V,,99S>r   c                   |j                  di       }	 | t        j                  k(  r>|yddlm}  |||      }t        |j                  d            |j                  dd      fS ddlm} t        j                   ||            }t        |j                  d            |j                  dd      fS # t        $ r}d	t        |      fcY d }~S d }~ww xY w)
Npayload)Fzmemory store unavailabler   )apply_memory_pendingsuccesserrorr   )apply_skill_pendingF)r   r
   MEMORYtools.memory_toolr_   booltools.skill_manager_toolrb   jsonloads	Exceptionstr)r   rW   r*   r^   r_   resultrb   es           r   rQ   rQ      s    ggi$G		!#8>)'<@F

9-.

7B0GGGDZZ 3G <=F

9-.

7B0GGG c!f}s)   B2 :B2 %AB2 2	C;CCCc                :   t        | |      \  }}|s|
|xs d|  dS |j                         dk(  rDd}t        j                  |       D ]!  }t        j                  | |d         s|dz  }# d| d|  d	S t        j                  | |      r	d
|  d| dS d|  d| dS )NrA   r   rE   r   r   r-   z	Rejected z	 pending rJ   zRejected pending z write 'rG   r   rF   )rC   r7   r
   r   rR   )r   r>   rT   rU   nrW   s         r   r9   r9      s    y$/KFC
fn8<88||~??9- 	C!!)SY7Q	 1#Yyk<<	)V,"9+XfXR@@#3F82>>r   c                    | syt        j                  t         j                  | d         }|s	d| d    dS t        j                  |      }d|d    d|j	                  dd	       d
}|d
z   |z   S )NzUsage: /skills diff <id>r   z No pending skill write with id 'rG   z# Pending skill write r   rH   r   r   r   )r
   rP   r"   skill_pending_diffr   )r>   rW   r4   headers       r   r:   r:      sy    )
..DG
,C1$q'"==  %D%c$i[3779b3I2J"MFD=4r   c                4   |st        |        d|  dS |d   j                         j                         }h d}h d}||v rd}n||v rd}nd| d	S ||rdnd}d|  d|  d| S 	  ||       |  d|rd dS d dS # t        $ r}d|  d| cY d
}~S d
}~ww xY w)z|Turn the approval gate on/off for a subsystem.

    ``set_mode_fn`` (when provided) persists the new boolean to config.
    z
Set with: /z approval <on|off>r   >   1r   yestrueenableenabled>   0nor	   falsedisabledisabledTFzInvalid value 'z'. Use: on or off.Nru   rz   zTo change the z) approval gate, run:
  hermes config set z.write_approval zFailed to set z.write_approval: z.write_approval set to 'r   r	   rG   )r   stripr7   ri   )	r   r>   r+   argtruthyfalseyrw   valrl   s	            r   r;   r;      s   
 i() *'[(:< 	=
q'--/


!C<F?F
f}	 %788fW  ,''0k1A#H 	I@G [00LBOOe0LBOO  @	{*;A3??@s   %A< <	BBBB)r   rj   returnrj   )r   rj   r<   	List[str]r   zOptional[str])r   rj   r>   r   )r   rj   r>   r   r   rj   )r   rj   )r>   r   r   rj   )__doc__
__future__r   rg   typingr   r   toolsr   r
   r   r)   r?   rC   r8   rQ   r9   r:   r;   rB   r   r   <module>r      sk    #  ! &C2 --
- -`B"? Pr   