
    i>4                    v   U d Z ddlmZ ddlZddlZddlZddlZddlZddlZddl	m
Z
 ddlmZmZmZmZ ddlmZ ddlmZ  ej        e          ZdZd	ZdZd
Z ej                    Zdaded<   d,dZedfd-dZ d.dZ!dddddddidgd d!Z"d"d#dd$dd%did$gd d!Z#d&d'di g d d!Z$ G d( d)e          Z%d/d+Z&dS )0u  ByteRover memory plugin — MemoryProvider interface.

Persistent memory via the ByteRover CLI (``brv``). Organizes knowledge into
a hierarchical context tree with tiered retrieval (fuzzy text → LLM-driven
search). Local-first with optional cloud sync.

Original PR #3499 by hieuntg81, adapted to MemoryProvider ABC.

Requires: ``brv`` CLI installed (npm install -g byterover-cli or
curl -fsSL https://byterover.dev/install.sh | sh).

Config via environment variables (profile-scoped via each profile's .env):
  BRV_API_KEY   — ByteRover API key (for cloud features, optional for local)

Working directory: $HERMES_HOME/byterover/ (profile-scoped context tree)
    )annotationsN)Path)AnyDictListOptional)MemoryProvider)
tool_error
   x      Optional[str]_cached_brv_pathreturnc                    t           5  t           t          dk    rt          ndcddd           S 	 ddd           n# 1 swxY w Y   t          j        d          } | sat	          j                    }|dz  dz  dz  t	          d          |dz  dz  dz  g}|D ]'}|                                rt          |          }  n(t           5  t           t          dk    rt          ndcddd           S | pdaddd           n# 1 swxY w Y   | S )z<Find the brv binary on PATH or well-known install locations.N brvz.brv-clibinz/usr/local/bin/brvz.npm-global)_brv_path_lockr   shutilwhichr   homeexistsstr)foundr   
candidatescs       F/home/piyush/.hermes/hermes-agent/plugins/memory/byterover/__init__.py_resolve_brv_pathr   3   s    
 H H''72'='=##4H H H H H H H H'H H H H H H H H H H H H H H H LE 
y{{:%-%&&= 5(50


  	 	Axxzz A 
 ' '''72'='=##4' ' ' ' ' ' ' ' !;B' ' ' ' ' ' ' ' ' ' ' ' ' ' ' Ls'   =AAC>.C>>DDargs	List[str]timeoutintcwdr   dictc                L   t                      }|sdddS |g| z   }|pt          t                                }t          |                              dd           t
          j                                        }t          t          |          j                  }|t
          j	        z   |
                    dd          z   |d<   	 t          j        |dd|||          }|j                                        }	|j                                        }
|j        d	k    rd|	d
S d|
p|	p	d|j         dS # t          j        $ r dd| ddcY S t$          $ r) t&          5  daddd           n# 1 swxY w Y   dddcY S t*          $ r}dt          |          dcY d}~S d}~ww xY w)z8Run a brv CLI command. Returns {success, output, error}.Fz8brv CLI not found. Install: npm install -g byterover-cli)successerrorTparentsexist_okPATHr   )capture_outputtextr"   r$   envr   )r'   outputzbrv exited zbrv timed out after sNzbrv CLI not found)r   r   _get_brv_cwdr   mkdirosenvironcopyparentpathsepget
subprocessrunstdoutstripstderr
returncodeTimeoutExpiredFileNotFoundErrorr   r   	Exception)r    r"   r$   brv_pathcmdeffective_cwdr/   brv_bin_dirresultr<   r>   es               r   _run_brvrI   N   sJ    !""H g +efff*t
C.3|~~..MdT:::
*//

Cd8nn+,,K
*SWWVR-@-@@CK34C
 
 
 $$&&$$&&!!#v666 6+`V+`?`VM^?`?`aaa$ N N N +L'+L+L+LMMMMM @ @ @ 	$ 	$#	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ +>????? 3 3 3 3q66222222223sU   AD3 "D3 3F#F#E, F#,E0	0F#3E0	4F#>	F#FF#F#r   c                 (    ddl m}   |             dz  S )z:Profile-scoped working directory for the brv context tree.r   get_hermes_home	byterover)hermes_constantsrL   rK   s    r   r2   r2   t   s'    000000?{**    	brv_queryzSearch ByteRover's persistent knowledge tree for relevant context. Returns memories, project knowledge, architectural decisions, and patterns from previous sessions. Use for any question where past context would help.objectquerystringzWhat to search for.)typedescription)rT   
propertiesrequired)namerU   
parameters
brv_curateu  Store important information in ByteRover's persistent knowledge tree. Use for architectural decisions, bug fixes, user preferences, project patterns — anything worth remembering across sessions. ByteRover's LLM automatically categorizes and organizes the memory.contentzThe information to remember.
brv_statusuM   Check ByteRover status — CLI version, context tree stats, cloud sync state.c                      e Zd ZdZd Zed&d            Zd'dZd Zd(dZ	d&dZ
ddd)dZddd*dZddd+dZd,dZd-dZd.dZd/d Zd0d!Zd1d"Zd1d#Zd&d$Zd%S )2ByteRoverMemoryProviderz,ByteRover persistent memory via the brv CLI.c                >    d| _         d| _        d| _        d | _        d S )Nr   r   )_cwd_session_id_turn_count_sync_threadselfs    r   __init__z ByteRoverMemoryProvider.__init__   s'    	8<rO   r   r   c                    dS )NrM    rd   s    r   rX   zByteRoverMemoryProvider.name   s    {rO   boolc                "    t                      duS )z0Check if brv CLI is installed. No network calls.Nr   rd   s    r   is_availablez$ByteRoverMemoryProvider.is_available   s     ""$..rO   c                    ddddddgS )Napi_keyz,ByteRover API key (optional, for cloud sync)TBRV_API_KEYzhttps://app.byterover.dev)keyrU   secretenv_varurlrh   rd   s    r   get_config_schemaz)ByteRoverMemoryProvider.get_config_schema   s'     !M(2 
 	
rO   
session_idNonec                    t          t                                | _        || _        d| _        t          | j                                      dd           d S )Nr   Tr)   )r   r2   r`   ra   rb   r   r3   )re   ru   kwargss      r   
initializez"ByteRoverMemoryProvider.initialize   sL    ''	%TYdT:::::rO   c                (    t                      sdS 	 dS )Nr   z# ByteRover Memory
Active. Persistent knowledge tree with hierarchical context.
Use brv_query to search past knowledge, brv_curate to store important facts, brv_status to check state.rk   rd   s    r   system_prompt_blockz+ByteRoverMemoryProvider.system_prompt_block   s$     "" 	2:	
 	
rO   r   )ru   rR   c               |   |r*t          |                                          t          k     rdS t          dd|                                dd         gt          | j                  }|d         rL|                    d          r7|d                                         }t          |          t          k    rd	| S dS )
zRun brv query synchronously before the agent's first LLM call.

        Blocks until the query completes (up to _QUERY_TIMEOUT seconds), ensuring
        the result is available as context before the model is called.
        r   rR   --N  r"   r$   r'   r0   z## ByteRover Context
)lenr=   _MIN_QUERY_LENrI   _QUERY_TIMEOUTr`   r9   _MIN_OUTPUT_LEN)re   rR   ru   rG   r0   s        r   prefetchz ByteRoverMemoryProvider.prefetch   s      	EKKMM**^;;2dEKKMM%4%01"	
 
 
 ) 	9H!5!5 	9H%++--F6{{_,,8888rrO   c                   dS )z7No-op: prefetch() now runs synchronously at turn start.Nrh   )re   rR   ru   s      r   queue_prefetchz&ByteRoverMemoryProvider.queue_prefetch   s    rO   user_contentassistant_contentc               r     xj         dz  c_         t                                                    t          k     rdS  fd} j        r4 j                                        r j                            d           t          j        |dd           _         j        	                                 dS )	z:Curate the conversation turn in background (non-blocking).   Nc                     	 dd d          dd d          } t          dd| gt          j                   d S # t          $ r&}t                              d|           Y d }~d S d }~ww xY w)NzUser: i  z
Assistant: curater}   r   zByteRover sync failed: %srI   _CURATE_TIMEOUTr`   rB   loggerdebug)combinedrH   r   re   r   s     r   _syncz0ByteRoverMemoryProvider.sync_turn.<locals>._sync   s    =`L$$7``FWX]Y]X]F^``tX.+       = = =8!<<<<<<<<<=s   7< 
A,A''A,g      @r"   Tzbrv-synctargetdaemonrX   )
rb   r   r=   r   rc   is_alivejoin	threadingThreadstart)re   r   r   ru   r   s   ```  r   	sync_turnz!ByteRoverMemoryProvider.sync_turn   s    A |!!##$$~55F	= 	= 	= 	= 	= 	= 	=  	0!2!;!;!=!= 	0""3"///%,J
 
 
 	!!!!!rO   actionr   r[   c                     |dvssdS  fd}t          j        |dd          }|                                 dS )z+Mirror built-in memory writes to ByteRover.)addreplaceNc                     	 dk    rdnd} t          ddd|  d gt          j                   d S # t          $ r&}t                              d	|           Y d }~d S d }~ww xY w)
NuserzUser profilezAgent memoryr   r}   [z] r   z"ByteRover memory mirror failed: %sr   )labelrH   r[   re   r   s     r   _writez7ByteRoverMemoryProvider.on_memory_write.<locals>._write  s    F*0F*:*:t%;%;%;'%;%;<+       F F FA1EEEEEEEEEFs   /4 
A$AA$Tzbrv-memwriter   )r   r   r   )re   r   r   r[   r   ts   ` ``  r   on_memory_writez'ByteRoverMemoryProvider.on_memory_write  so    +++7+F	F 	F 	F 	F 	F 	F 	F F4nMMM						rO   messagesList[Dict[str, Any]]c                    |sdS g |dd         D ]}}|                     dd          }|                     dd          }t          |t                    r:|                                r&|dv r"                    | d|dd                     ~sdS d	                               fd
}t          j        |dd          }|                                 dS )z;Extract insights before context compression discards turns.r   iNroler[   )r   	assistantz: i  
c                    	 t          ddd gt          j                   t                              dt                               d S # t          $ r&} t                              d|            Y d } ~ d S d } ~ ww xY w)Nr   r}   z[Pre-compression context]
r   z,ByteRover pre-compression flush: %d messagesz*ByteRover pre-compression flush failed: %s)rI   r   r`   r   infor   rB   r   )rH   r   partsre   s    r   _flushz7ByteRoverMemoryProvider.on_pre_compress.<locals>._flush,  s    Nt%M8%M%MN+    JCPUJJWWWWW N N NI1MMMMMMMMMNs   A
A 
A?A::A?Tz	brv-flushr   )	r9   
isinstancer   r=   appendr   r   r   r   )	re   r   msgr   r[   r   r   r   r   s	   `      @@r   on_pre_compressz'ByteRoverMemoryProvider.on_pre_compress  s    	2 CDD> 	9 	9C7762&&Dggi,,G'3'' 9GMMOO 9H]@]@]7777888 	299U##	N 	N 	N 	N 	N 	N 	N F4kJJJ				rrO   c                *    t           t          t          gS )N)QUERY_SCHEMACURATE_SCHEMASTATUS_SCHEMArd   s    r   get_tool_schemasz(ByteRoverMemoryProvider.get_tool_schemas:  s    m];;rO   	tool_namer    r%   c                    |dk    r|                      |          S |dk    r|                     |          S |dk    r|                                 S t          d|           S )NrP   rZ   r\   zUnknown tool: )_tool_query_tool_curate_tool_statusr
   )re   r   r    rx   s       r   handle_tool_callz(ByteRoverMemoryProvider.handle_tool_call=  st    ####D))),&&$$T***,&&$$&&&6966777rO   c                    | j         r6| j                                         r| j                             d           d S d S d S )Ng      $@r   )rc   r   r   rd   s    r   shutdownz ByteRoverMemoryProvider.shutdownF  sW     	1!2!;!;!=!= 	1""4"00000	1 	1 	1 	1rO   c                4   |                     dd          }|st          d          S t          dd|                                d d         gt          | j                  }|d         s#t          |                     dd	                    S |                     d
d                                          }|rt          |          t          k     rt          j	        ddi          S t          |          dk    r|d d         dz   }t          j	        d|i          S )NrR   r   zquery is requiredr}   r~   r   r'   r(   zQuery failedr0   rG   zNo relevant memories found.i@  z

[... truncated])
r9   r
   rI   r=   r   r`   r   r   jsondumps)re   r    rR   rG   r0   s        r   r   z#ByteRoverMemoryProvider._tool_queryL  s   "%% 	31222dEKKMM%4%01"	
 
 

 i  	Cfjj.AABBBHb))//11 	IV66:x)FGHHH v;;ETE]%::Fz8V,---rO   c                   |                     dd          }|st          d          S t          dd|gt          | j                  }|d         s#t          |                     dd	                    S t          j        d
di          S )Nr[   r   zcontent is requiredr   r}   r   r'   r(   zCurate failedrG   zMemory curated successfully.)r9   r
   rI   r   r`   r   r   )re   r    r[   rG   s       r   r   z$ByteRoverMemoryProvider._tool_curatec  s    ((9b)) 	53444tW%#
 
 

 i  	Dfjj/BBCCCz8%CDEEErO   c                    t          dgd| j                  }|d         s#t          |                    dd                    S t	          j        d|                    dd          i          S )	Nstatus   r   r'   r(   zStatus check failedr0   r   )rI   r`   r
   r9   r   r   )re   rG   s     r   r   z$ByteRoverMemoryProvider._tool_statusr  sh    8*bdi@@@i  	Jfjj2GHHIIIz8VZZ"%=%=>???rO   N)r   r   )r   ri   )ru   r   r   rv   )rR   r   ru   r   r   r   )rR   r   ru   r   r   rv   )r   r   r   r   ru   r   r   rv   )r   r   r   r   r[   r   r   rv   )r   r   r   r   )r   r   )r   r   r    r%   r   r   r   rv   )r    r%   r   r   )__name__
__module____qualname____doc__rf   propertyrX   rl   rt   ry   r{   r   r   r   r   r   r   r   r   r   r   r   rh   rO   r   r^   r^      s       66= = =    X/ / / /	
 	
 	
; ; ; ;
 
 
 
 9;      $ ?A       Y[ " " " " " "6   $   @< < < <8 8 8 81 1 1 1. . . ..F F F F@ @ @ @ @ @rO   r^   rv   c                H    |                      t                                 dS )z/Register ByteRover as a memory provider plugin.N)register_memory_providerr^   )ctxs    r   registerr   }  s#      !8!:!:;;;;;rO   )r   r   )r    r!   r"   r#   r$   r   r   r%   )r   r   r   )'r   
__future__r   r   loggingr4   r   r:   r   pathlibr   typingr   r   r   r   agent.memory_providerr	   tools.registryr
   	getLoggerr   r   r   r   r   r   Lockr   r   __annotations__r   rI   r2   r   r   r   r^   r   rh   rO   r   <module>r      sM    " # " " " " "   				                , , , , , , , , , , , , 0 0 0 0 0 0 % % % % % %		8	$	$    !!"&  & & & &   6 .<#3 #3 #3 #3 #3L+ + + + 	 h7LMM
 I  $ 	> 9WXX
 K  $ b#22FF K@ K@ K@ K@ K@n K@ K@ K@d< < < < < <rO   