
    ip                       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mZm	Z	m
Z
mZ ddlmZ ddlmZ  ej        e          Zddd	d
ddddd
idddg ddZddd	d
dddddd
ddddgddZddd	d
ddd
dg dd d
ddd!dgddZd"d#d	d
d$dd
ddd%g ddZd&d'd	d
d(dd
d)dd
ddd*g ddZeeeeegZ G d+ d,e          Zd0d/ZdS )1uL  Honcho memory plugin — MemoryProvider for Honcho AI-native memory.

Provides cross-session user modeling with dialectic Q&A, semantic search,
peer cards, and persistent conclusions via the Honcho SDK. Honcho provides AI-native cross-session user
modeling with dialectic Q&A, semantic search, peer cards, and conclusions.

The 4 tools (profile, search, context, conclude) are exposed through
the MemoryProvider interface.

Config: Uses the existing Honcho config chain:
  1. $HERMES_HOME/honcho.json (profile-scoped)
  2. ~/.honcho/config.json (legacy global)
  3. Environment variables
    )annotationsN)AnyDictListOptional)MemoryProvider)
tool_errorhoncho_profileu   Retrieve or update a peer card from Honcho — a curated list of key facts about that peer (name, role, preferences, communication style, patterns). Pass `card` to update; omit `card` to read.objectstringzaPeer to query. Built-in aliases: 'user' (default), 'ai'. Or pass any peer ID from this workspace.)typedescriptionarrayr   zGNew peer card as a list of fact strings. Omit to read the current card.)r   itemsr   )peercard)r   
propertiesrequired)namer   
parametershoncho_searchu   Semantic search over Honcho's stored context about a peer. Returns raw excerpts ranked by relevance — no LLM synthesis. Cheaper and faster than honcho_reasoning. Good when you want to find specific past facts and reason over them yourself.z&What to search for in Honcho's memory.integerz:Token budget for returned context (default 800, max 2000).)query
max_tokensr   r   honcho_reasoningug  Ask Honcho a natural language question and get a synthesized answer. Uses Honcho's LLM (dialectic reasoning) — higher cost than honcho_profile or honcho_search. Can query about any peer via alias or explicit peer ID. Pass reasoning_level to control depth: minimal (fast/cheap), low (default), medium, high, max (deep/expensive). Omit for configured default.zA natural language question.a  Override the default reasoning depth. Omit to use the configured default (typically low). Guide:
- minimal: quick factual lookups (name, role, simple preference)
- low: straightforward questions with clear answers
- medium: multi-aspect questions requiring synthesis across observations
- high: complex behavioral patterns, contradictions, deep analysis
- max: thorough audit-level analysis, leave no stone unturnedminimallowmediumhighmax)r   r   enum)r   reasoning_levelr   honcho_contextu   Retrieve full session context from Honcho — summary, peer representation, peer card, and recent messages. No LLM synthesis. Cheaper than honcho_reasoning. Use this to see what Honcho knows about the current conversation and the specified peer.zOOptional focus query to filter context. Omit for full session context snapshot.)r   r   honcho_concludeuC  Write or delete a conclusion about a peer in Honcho's memory. Conclusions are persistent facts that build a peer's profile. You MUST pass exactly one of: `conclusion` (to create) or `delete_id` (to delete). Passing neither is an error. Deletion is only for PII removal — Honcho self-heals incorrect conclusions over time.zpA factual statement to persist. Provide this when creating a conclusion. Do not send it together with delete_id.zzConclusion ID to delete for PII removal. Provide this when deleting a conclusion. Do not send it together with conclusion.)
conclusion	delete_idr   c                  @   e Zd ZU dZd ZedCd            ZdDdZd Zd	 Z	dEdZ
dFdZdFdZdDdZdGdZdCdZdddHdZdIdZdddJdZddddddd Zd!ed"<   d#ZdKd&ZdLd*ZedMd,            ZdNd-ZdOd0ZedPd3            ZdddQd6ZdRd9ZdSd<ZdTd=Z dUd@Z!dVdAZ"dBS )WHonchoMemoryProviderzHHoncho AI-native memory with dialectic Q&A and persistent user modeling.c                   d | _         d | _        d| _        d| _        t	          j                    | _        d | _        d | _        d| _	        d | _
        t	          j                    | _        d| _        d| _        d| _        d| _        d| _        d | _        d | _        d| _        d| _        d| _        d | _        d | _        d| _        d S )	N hybridr   
every-turn      F)_manager_config_session_key_prefetch_result	threadingLock_prefetch_lock_prefetch_thread_sync_thread_recall_mode_base_context_cache_base_context_lock_turn_count_injection_frequency_context_cadence_dialectic_cadence_dialectic_depth_dialectic_depth_levels_reasoning_level_cap_last_context_turn_last_dialectic_turn_session_initialized_lazy_init_kwargs_lazy_init_session_id_cron_skippedselfs    F/home/agentuser/.hermes/hermes-agent/plugins/memory/honcho/__init__.py__init__zHonchoMemoryProvider.__init__   s     "'n..<@8< % 37 "+."2"2 $0! !"# !9=$37!"&$(! %*!1548" #    returnstrc                    dS )Nhoncho rJ   s    rL   r   zHonchoMemoryProvider.name   s    xrN   boolc                    	 ddl m} |                                }|j        ot	          |j        p|j                  S # t          $ r Y dS w xY w)z0Check if Honcho is configured. No network calls.r   )HonchoClientConfigF)plugins.memory.honcho.clientrV   from_global_configenabledrT   api_keybase_url	Exception)rK   rV   cfgs      rL   is_availablez!HonchoMemoryProvider.is_available   sj    	GGGGGG$7799C;D4(Cs|#D#DD 	 	 	55	s   ;> 
AAc                >   ddl }ddlm}  ||          dz  }i }|                                r4	  |j        |                                          }n# t          $ r Y nw xY w|                    |           |                     |j	        |d                     dS )zDWrite config to $HERMES_HOME/honcho.json (Honcho SDK native format).r   N)Pathzhoncho.json   )indent)
jsonpathlibr`   existsloads	read_textr\   update
write_textdumps)rK   valueshermes_homerc   r`   config_pathexistings          rL   save_configz HonchoMemoryProvider.save_config   s          d;''-7 	%4:k&;&;&=&=>>   ztz(1===>>>>>s   "A 
A A c                    ddddddddd	gS )
NrZ   zHoncho API keyTHONCHO_API_KEYzhttps://app.honcho.dev)keyr   secretenv_varurlbaseUrlz!Honcho base URL (for self-hosted))rr   r   rS   rJ   s    rL   get_config_schemaz&HonchoMemoryProvider.get_config_schema   s6    .>$[k  uM  N  N.QRR
 	
rN   rl   configdictNonec                T    ddl }ddlm}  ||                                           dS )z:Run the full Honcho setup wizard after provider selection.r   N)	cmd_setup)typesplugins.memory.honcho.clir|   SimpleNamespace)rK   rl   rx   r}   r|   s        rL   
post_setupzHonchoMemoryProvider.post_setup  s?    777777	%''))*****rN   
session_idc                   	 |                     dd          }|                     dd          }|dv s|dk    r%t                              d||           d| _        d	S d
dlm}m} d
dlm} |	                                }|j
        r|j        s#|j        st                              d           d	S |                     d          }	|	r|j        s|	|_        || _        |j        | _        t                              d| j                   	 |j        pi }
|
                     dd          | _        t'          |
                     dd                    | _        t'          |
                     dd                    | _        t-          dt/          |j        d                    | _        |j        | _        |
                     d          }|r|dv r|| _        n2# t:          $ r%}t                              d|           Y d	}~nd	}~ww xY w| j        dk    rI|j        r | j        ||fi | d	S || _         || _!        || _        t                              d           d	S  | j        ||fi | d	S # tD          $ r t                              d           Y d	S t:          $ r-}t          #                    d|           d	| _$        Y d	}~d	S d	}~ww xY w)zInitialize Honcho session manager.

        Handles: cron guard, recall_mode, session name resolution,
        peer memory mode, SOUL.md ai_peer sync, memory file migration,
        and pre-warming context at init.
        agent_contextr+   platformcli)cronflushr   zBHoncho skipped: cron/flush context (agent_context=%s, platform=%s)TNr   )rV   get_honcho_clientHonchoSessionManageru)   Honcho not configured — plugin inactiveuser_idzHoncho recall_mode: %sinjectionFrequencyr-   contextCadencer.   dialecticCadencer/   reasoningLevelCap)r   r   r   r    z,Honcho cost-awareness config parse error: %stoolsuG   Honcho tools-only mode — deferring session init until first tool callu3   honcho-ai package not installed — plugin inactivezHoncho init failed: %s)%getloggerdebugrI   rW   rV   r   plugins.memory.honcho.sessionr   rX   rY   rZ   r[   	peer_namer2   recall_moder:   rawr>   intr?   r@   r!   mindialectic_depthrA   dialectic_depth_levelsrB   rC   r\   init_on_session_start_do_session_initrG   rH   ImportErrorwarningr1   )rK   r   kwargsr   r   rV   r   r   r]   _gw_user_idr   capes                rL   
initializezHonchoMemoryProvider.initialize  sB   H	!"JJ;;Mzz*e44H 111X5G5Ga*H6 6 6%)"ZZZZZZZZJJJJJJ$7799C; s{ cl HIII !**Y//K ,3= , +DL !$DLL143DEEEPgm,/GG4H,,W,W)(+CGG4Da,H,H(I(I%*-cgg6H!.L.L*M*M'(+As33F/J/J(K(K%/2/I,gg122 43"FFF03D- P P PKQOOOOOOOOP  G++, )D)#zDDVDDDF)/&-7*"fggg "D!#z<<V<<<<< 	P 	P 	PLLNOOOOOO 	! 	! 	!NN3Q777 DMMMMMMM	!sc   AI+ AI+ 0AI+ 	C
G I+ 
HG>9I+ >H$I+ )/I+ I+ +$K	K"KKc                f   ddl m} ddlm}  ||          } ||||j                  | _        |                    d          }|                    d          }|                    |||          p|pd| _        t          
                    d	| j                   | j                            | j                  }	d
| _        	 |	j        sl|j        dk    raddlm}
 t#           |
            dz            }| j                            | j        |           t          
                    d| j                   n+|j        dk    r t          
                    d| j                   n2# t&          $ r%}t          
                    d|           Y d}~nd}~ww xY w| j        dv r	 | j                            | j                   | j                            | j        d           t          
                    d| j                   dS # t&          $ r&}t          
                    d|           Y d}~dS d}~ww xY wdS )zBShared session initialization logic for both eager and lazy paths.r   )r   r   )rR   rx   context_tokenssession_titlegateway_session_key)r   r   r   hermes-defaultzHoncho session key resolved: %sTzper-session)get_hermes_homememoriesz:Honcho memory file migration attempted for new session: %sz_Honcho memory file migration skipped: per-session strategy creates a fresh session per run (%s)z(Honcho memory file migration skipped: %sN)contextr,   z#What should I know about this user?z/Honcho pre-warm threads started for session: %szHoncho pre-warm failed: %s)rW   r   r   r   r   r1   r   resolve_session_namer3   r   r   get_or_createrF   messagessession_strategyhermes_constantsr   rP   migrate_memory_filesr\   r:   prefetch_contextprefetch_dialectic)rK   r]   r   r   r   r   clientr   r   sessionr   mem_dirr   s                rL   r   z%HonchoMemoryProvider._do_session_initY  s   BBBBBBFFFFFF""3'',,-
 
 
 

?33$jj)>??$$+%$7 %    
     	 	68IJJJ ---d.?@@$(!	H# 	(<(M(M<<<<<<oo//*<==2243DgNNNY[_[lmmmm%66u%    	H 	H 	HLLCQGGGGGGGG	H  555>..t/@AAA001BDijjjNPTPabbbbb > > >91=========> 65s2   BE! !
F+FFAG> >
H.H))H.c                *   | j         r	| j        rdS | j        rdS | j        r| j        sdS 	  | j        | j        | j        pdfi | j         d| _        d| _        | j         duS # t          $ r&}t          	                    d|           Y d}~dS d}~ww xY w)zLazily initialize the Honcho session (for tools-only mode).

        Returns True if the manager is ready, False otherwise.
        TFr   Nz#Honcho lazy session init failed: %s)
r1   rF   rI   r2   rG   r   rH   r\   r   r   )rK   r   s     rL   _ensure_sessionz$HonchoMemoryProvider._ensure_session  s    
 = 	T6 	4 	5| 	4#9 	5	!D!*>.>  (   &*D")-D&=,, 	 	 	NN@!DDD55555	s   6A" "
B,BBctxc                   g }|                     dd          }|r|                    d|            |                     dd          }|r|                    d|            |                     dd          }|r|                    d|            |                     dd          }|r|                    d	|            |                     d
d          }|r|                    d|            |sdS d                    |          S )zEFormat the prefetch context dict into a readable system prompt block.summaryr+   z## Session Summary
representationz## User Representation
r   z## User Peer Card
ai_representationz## AI Self-Representation
ai_cardz## AI Identity Card


)r   appendjoin)rK   r   partsr   repr   ai_repr   s           rL   _format_first_turn_contextz/HonchoMemoryProvider._format_first_turn_context  s9    '')R(( 	;LL999:::gg&++ 	;LL9C99:::wwvr"" 	7LL5t55666,b11 	ALL?v??@@@'')R(( 	<LL:::;;; 	2{{5!!!rN   c                    | j         rdS | j        r| j        s| j        dk    r
| j        r	 dS dS | j        dk    rd}n| j        dk    rd}nd}|S )u  Return system prompt text, adapted by recall_mode.

        Returns only the mode header and tool instructions — static text
        that doesn't change between turns (prompt-cache friendly).
        Live context (representation, card) is injected via prefetch().
        r+   r   z# Honcho Memory
Active (tools-only mode). Use honcho_profile, honcho_search, honcho_reasoning, honcho_context, and honcho_conclude tools to access user memory.r   u   # Honcho Memory
Active (context-injection mode). Relevant user context is automatically injected before each turn. No memory tools are available — context is managed automatically.u?  # Honcho Memory
Active (tools-only mode). Use honcho_profile for a quick factual snapshot, honcho_search for raw excerpts, honcho_context for raw peer context, honcho_reasoning for synthesized answers, honcho_conclude to save facts about the user. No automatic context injection — you must use tools to access memory.a5  # Honcho Memory
Active (hybrid mode). Relevant context is auto-injected AND memory tools are available. Use honcho_profile for a quick factual snapshot, honcho_search for raw excerpts, honcho_context for raw peer context, honcho_reasoning for synthesized answers, honcho_conclude to save facts about the user.)rI   r1   r3   r:   r2   )rK   headers     rL   system_prompt_blockz(HonchoMemoryProvider.system_prompt_block  s      	2} 	D$5 	 G+++i 
 2 	))) F '))Z F@  rN   r+   )r   r   c               D     j         rdS  j        dk    rdS  j        dk    r j        dk    rdS g } j        5   j        	  j                             j                  }|r 	                    |          nd _         j         _
        n9# t          $ r,}t                              d|           d _        Y d}~nd}~ww xY w j        }ddd           n# 1 swxY w Y    j        r` j                             j                  }|r? 	                    |          }|r( j        5  | _        ddd           n# 1 swxY w Y   |}|r|                    |            j        dk    rr j        r j        j        r j        j        nd}	g d fd}
t'          j        |
d          }|                                 |                    |	           |                                sUrd         nd}|r:|                                r& j        5  | _        ddd           n# 1 swxY w Y    j         _        nt                              d|	            j        r4 j                                        r j                            d            j        5   j        }d _        ddd           n# 1 swxY w Y   |r)|                                r|                    |           |sdS d                    |          }                     |          }|S )u  Return base context (representation + card) plus dialectic supplement.

        Assembles two layers:
        1. Base context from peer.context() — cached, refreshed on context_cadence
        2. Dialectic supplement — cached, refreshed on dialectic_cadence

        B1: Returns empty when recall_mode is "tools" (no injection).
        B5: Respects injection_frequency — "first-turn" returns cached/empty after turn 0.
        Port #3265: Truncates to context_tokens budget.
        r+   r   z
first-turnr.   Nz$Honcho base context fetch failed: %sr0   g       @rO   rz   c                     	                                                               d S # t          $ r&} t                              d|            Y d } ~ d S d } ~ ww xY w)Nz&Honcho first-turn dialectic failed: %s)r   _run_dialectic_depthr\   r   r   )exc_result_holderr   rK   s    rL   _run_first_turnz6HonchoMemoryProvider.prefetch.<locals>._run_first_turn;  s{    P"))$*C*CE*J*JKKKKK  P P PLL!I3OOOOOOOOOPs   (- 
AAAT)targetdaemontimeoutr   uZ   Honcho first-turn dialectic timed out (%.1fs) — will inject at next cadence-allowed turng      @r   rO   rz   )rI   r:   r>   r=   r<   r;   r1   get_prefetch_contextr3   r   rD   r\   r   r   pop_context_resultr   rE   r2   r   r5   Threadstartr   is_alivestripr7   r4   r8   _truncate_to_budget)rK   r   r   r   r   r   base_context	fresh_ctx	formatted_first_turn_timeoutr   _tfirst_turn_dialecticdialectic_resultresultr   s   ``             @rL   prefetchzHonchoMemoryProvider.prefetch  s     	2 ''2 $449IA9M9M2
 $ 
	4 
	4'/2-<<T=NOOCWZ/bt/N/Ns/S/S/S`bD,.2.>D++  2 2 2LL!GKKK/1D,,,,,,2  3L
	4 
	4 
	4 
	4 
	4 
	4 
	4 
	4 
	4 
	4 
	4 
	4 
	4 
	4 
	4 = 	-889JKKI - ;;IFF	 -0 = =3<0= = = = = = = = = = = = = = =#,L 	'LL&&& $,,,(,V9MV$$SV   )+NP P P P P P P P !FFFBHHJJJGG/G000;;== <J'R~a'8'8PR$' E,@,F,F,H,H E, E E0D-E E E E E E E E E E E E E E E,0,<))?'     	4T%:%C%C%E%E 	4!&&s&333  	' 	'#4$&D!	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	'  	+ 0 6 6 8 8 	+LL)*** 	2U## ))&11sr   CA	BC
C"B?:C?C
CC!C+D??EEH,,H03H0!J<<K K textc                    | j         r| j         j        s|S | j         j        dz  }t          |          |k    r|S |d|         }|                    d          }||dz  k    r
|d|         }|dz   S )z9Truncate text to fit within context_tokens budget if set.   N g?u    …)r2   r   lenrfind)rK   r   budget_chars	truncated
last_spaces        rL   r   z(HonchoMemoryProvider._truncate_to_budgetf  s    | 	4<#> 	K|2Q6t99$$K,'	__S))
s***!+:+.I6!!rN   c                    j         rdS  j        r	 j        rsdS  j        dk    rdS  j        dk    s j         j        z
   j        k    r` j         _        	  j                             j                   n2# t          $ r%}t          
                    d|           Y d}~nd}~ww xY w j        dk    rH j         j        z
   j        k     r0t          
                    d j         j         j        z
             dS  j         _         fd}t          j        |dd	           _         j                                         dS )
a  Fire background prefetch threads for the upcoming turn.

        B5: Checks cadence independently for dialectic and context refresh.
        Context refresh updates the base layer (representation + card).
        Dialectic fires the LLM reasoning supplement.
        Nr   r.   z"Honcho context prefetch failed: %szCHoncho dialectic prefetch skipped: cadence %d, turns since last: %dc                    	                                } | r=|                                 r+j        5  | _        d d d            d S # 1 swxY w Y   d S d S d S # t          $ r&}t
                              d|           Y d }~d S d }~ww xY w)NzHoncho prefetch failed: %s)r   r   r7   r4   r\   r   r   )r   r   r   rK   s     rL   _runz1HonchoMemoryProvider.queue_prefetch.<locals>._run  s   >22599 7fllnn 7, 7 706-7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 77 7 7 7  > > >91=========>s9   2A A
A 
AA AA 
B%BBTzhoncho-prefetchr   r   r   )rI   r1   r3   r:   r?   r=   rD   r   r\   r   r   r@   rE   r5   r   r8   r   )rK   r   r   r   r   s   ``   rL   queue_prefetchz#HonchoMemoryProvider.queue_prefetcht  s     	F} 	D$5 	U 	F ''F  A%%$*:T=T*TY]Yn)n)n&*&6D#F..t/@%HHHH F F FA1EEEEEEEEF
 "Q&& 4#<<@WWWb!4d6FIb6bd d d$($4!	> 	> 	> 	> 	> 	> !* 0+<!
 !
 !
 	##%%%%%s    A< <
B+B&&B+baser   r   ))r.   r   )ra   r   )ra   r.   )r/   r   )r/   r.   )r/   ra   zdict[tuple[int, int], str]_PROPORTIONAL_LEVELSr   pass_idxr   c                    | j         r%|t          | j                   k     r| j         |         S | j        r| j        j        nd}| j                            | j        |f          }||dk    r|S |S )zResolve reasoning level for a given pass index.

        Uses dialecticDepthLevels if configured, otherwise proportional
        defaults relative to dialecticReasoningLevel.
        r   Nr   )rB   r   r2   dialectic_reasoning_levelr   r   rA   )rK   r   r   mappings       rL   _resolve_pass_levelz(HonchoMemoryProvider._resolve_pass_level  s     ' 	:Hs4;W7X7X,X,X/99:>,Q66E+//1F0QRR?g//KrN   prior_results	list[str]is_coldc                    |dk    r|r	 dS 	 dS |dk    r|r|d         nd}d| dS d	t          |          dk    r|d         nd
 dt          |          dk    r|d         nd
 dS )a  Build the prompt for a given dialectic pass.

        Pass 0: cold start (general user query) or warm (session-scoped).
        Pass 1: self-audit / targeted synthesis against gaps from pass 0.
        Pass 2: reconciliation / contradiction check across prior passes.
        r   zWho is this person? What are their preferences, goals, and working style? Focus on facts that would help an AI assistant be immediately useful.zGiven what's been discussed in this session so far, what context about this user is most relevant to the current conversation? Prioritize active context over biographical facts.r.   r+   z Given this initial assessment:

z

What gaps remain in your understanding that would help going forward? Synthesize what you actually know about the user's current state and immediate needs, grounded in evidence from recent sessions.z Prior passes produced:

Pass 1:
z(empty)z


Pass 2:
z

Do these assessments cohere? Reconcile any contradictions and produce a final, concise synthesis of what matters most for the current conversation.)r   )rK   r   r   r   priors        rL   _build_dialectic_promptz,HonchoMemoryProvider._build_dialectic_prompt  s     q== 7 S 
 ]])6>M"%%BE4U 4 4 4003M0B0BQ0F0FM!,,I0 003M0B0BQ0F0FM!,,I0 0 0rN   r   c                :   | r%t          |                                           dk     rdS d| v rJd| v sDd| v s@t          j        d| t          j                  s t          j        d| t          j                  rdS t          |                                           d	k    S )
zCheck if a dialectic pass returned enough signal to skip further passes.

        Heuristic: a response longer than 100 chars with some structure
        (section headers, bullets, or an ordered list) is considered sufficient.
        d   F
z##u   •z^[*-] z
^\s*\d+\. Ti,  )r   r   research	MULTILINE)r   s    rL   _signal_sufficientz'HonchoMemoryProvider._signal_sufficient  s      	V\\^^,,s2256>>FNNyFBL99 y==  46<<>>""S((rN   c                   | j         r| j        sdS | j         }g }t          | j                  D ]}|dk    r|                     d||          }nW|r>|                     |d                   r#t                              d| j        |            n|                     |||          }| 	                    |          }t                              d| j        |||           | j         
                    | j        ||d          }|                    |pd           t          |          D ]}|r|                                r|c S dS )u[  Execute up to dialecticDepth .chat() calls with conditional bail-out.

        Cold start (no base context): general user-oriented query.
        Warm session (base context exists): session-scoped query.
        Each pass is conditional — bails early if prior pass returned strong signal.
        Returns the best (usually last) result.
        r+   r   r   zCHoncho dialectic depth %d: pass %d skipped, prior signal sufficientz5Honcho dialectic depth %d: pass %d, level=%s, cold=%suserr#   r   )r1   r3   r;   rangerA   r   r  r   r   r   dialectic_queryr   reversedr   )	rK   r   r   resultsipromptlevelr   rs	            rL   r   z)HonchoMemoryProvider._run_dialectic_depth  s    } 	D$5 	2..t,-- 	) 	)AAvv55a'JJ  t66wr{CC LL!f!%!6; ; ;E55a'JJ,,Q//ELLP.5'C C C ]22!6 % 3  F
 NN6<R(((( '"" 	 	A QWWYY rrN   turn_numbermessagec                    || _         dS )z;Track turn count for cadence and injection_frequency logic.N)r=   )rK   r  r  r   s       rL   on_turn_startz"HonchoMemoryProvider.on_turn_start+  s    &rN   contentlimitc                l   t          |           |k    r| gS d}t          |          }g }| }d}|r|r|n||z
  }t          |          |k    r|                    |r|n||z              n|d|         }|                    d          }	|	|dz  k     r |                    d          }	|	dk    r|	dz  }	|	|dz  k     r|                    d	          }	|	|dz  k     r|}	|d|	                                         }
||	d                                         }|s||
z   }
|                    |
           d
}||S )aR  Split content into chunks that fit within the Honcho message limit.

        Splits at paragraph boundaries when possible, falling back to
        sentence boundaries, then word boundaries. Each continuation
        chunk is prefixed with "[continued] " so Honcho's representation
        engine can reconstruct the full message.
        z[continued] TNr   g333333?z. r   ra   r   F)r   r   r   rstriplstrip)r  r  prefix
prefix_lenchunks	remainingfirst	effectivesegmentcutchunks              rL   _chunk_messagez#HonchoMemoryProvider._chunk_message/  s}    w<<5  9[[
	 	!&>EJ,>I9~~**5Hiify6HIII

+G --''CY_$$mmD))!881HCY_$$mmC((Y_$$dsdO**,,E!#$$..00I 'MM%   E1  	4 rN   user_contentassistant_contentc               Z     j         rdS  j        r j        sdS  j        r j        j        nd fd} j        r4 j                                        r j                            d           t          j	        |dd           _         j        
                                 dS )	zRecord the conversation turn in Honcho (non-blocking).

        Messages exceeding the Honcho API limit (default 25k chars) are
        split into multiple messages with continuation markers.
        Nia  c                    	 j                             j                  }                               D ]}|                     d|                                         D ]}|                     d|           j                             |            d S # t          $ r&}t                              d|           Y d }~d S d }~ww xY w)Nr	  	assistantzHoncho sync_turn failed: %s)	r1   r   r3   r%  add_message_flush_sessionr\   r   r   )r   r$  r   r'  	msg_limitrK   r&  s      rL   _syncz-HonchoMemoryProvider.sync_turn.<locals>._synci  s    ?-55d6GHH!00yII 7 7E''6666!001BINN < <E''U;;;;,,W55555 ? ? ?:A>>>>>>>>>?s   BB 
C&CC      @r   Tzhoncho-syncr   )rI   r1   r3   r2   message_max_charsr9   r   r   r5   r   r   )rK   r&  r'  r   r.  r-  s   ```  @rL   	sync_turnzHonchoMemoryProvider.sync_turn\  s      	F} 	D$5 	F6:lMDL22			? 		? 		? 		? 		? 		? 		? 		?  	0!2!;!;!=!= 	0""3"///%,M
 
 
 	!!!!!rN   actionr   c                     |dk    s|dk    ssdS  j         rdS  j        r j        sdS  fd}t          j        |dd          }|                                 dS )z:Mirror built-in user profile writes as Honcho conclusions.addr	  Nc                     	 j                             j                   d S # t          $ r&} t                              d|            Y d } ~ d S d } ~ ww xY w)NzHoncho memory mirror failed: %s)r1   create_conclusionr3   r\   r   r   )r   r  rK   s    rL   _writez4HonchoMemoryProvider.on_memory_write.<locals>._write  st    C//0A7KKKKK C C C>BBBBBBBBBCs    % 
AAATzhoncho-memwriter   )rI   r1   r3   r5   r   r   )rK   r2  r   r  r7  ts   `  `  rL   on_memory_writez$HonchoMemoryProvider.on_memory_write{  s    U??f..g.F 	F} 	D$5 	F	C 	C 	C 	C 	C 	C F4>OPPP						rN   r   List[Dict[str, Any]]c                :   | j         rdS | j        sdS | j        r4| j                                        r| j                            d           	 | j                                         dS # t          $ r&}t                              d|           Y d}~dS d}~ww xY w)z4Flush all pending messages to Honcho on session end.Ng      $@r   z#Honcho session-end flush failed: %s)	rI   r1   r9   r   r   	flush_allr\   r   r   )rK   r   r   s      rL   on_session_endz#HonchoMemoryProvider.on_session_end  s     	F} 	F 	1!2!;!;!=!= 	1""4"000	CM##%%%%% 	C 	C 	CLL>BBBBBBBBB	Cs   A* *
B4BBc                V    | j         rg S | j        dk    rg S t          t                    S )zeReturn tool schemas, respecting recall_mode.

        B1: context-only mode hides all tools.
        r   )rI   r:   listALL_TOOL_SCHEMASrJ   s    rL   get_tool_schemasz%HonchoMemoryProvider.get_tool_schemas  s6    
  	I	))I$%%%rN   	tool_nameargsc                j   | j         rt          d          S | j        s#|                                 st          d          S | j        r| j        st          d          S 	 |dk    r|                    dd          }|                    d          }|r[| j                            | j        ||          }|t          d
          S t          j	        dt          |           d|d          S | j                            | j        |          }|st          j	        ddi          S t          j	        d|i          S |dk    r|                    dd          }|st          d          S t          t          |                    dd                    d          }	|                    dd          }| j                            | j        ||	|          }|st          j	        ddi          S t          j	        d|i          S |dk    r|                    dd          }|st          d          S |                    dd          }|                    d          }
| j                            | j        ||
|          }| j        | _        t          j	        d|pdi          S |dk    rq|                    dd          }| j                            | j        |          }|st          j	        ddi          S g }|                    d          r|                    d |d                     |                    d!          r|                    d"|d!                     |                    d          r|                    d#|d                     |                    d$          rG|d$         }d%                    d& |d'd	         D                       }|                    d(|            t          j	        dd)                    |          pd*i          S |d+k    r<|                    d,          pd                                }|                    d-d                                          }|                    dd          }t-          |          }t-          |          }||k    rt          d.          S |rQ| j                            | j        ||          }|rt          j	        dd/| d0i          S t          d1| d2          S | j                            | j        ||          }|rt          j	        dd3| d4| i          S t          d5          S t          d6|           S # t2          $ r;}t4                              d7||           t          d8| d9|           cY d	}~S d	}~ww xY w):zFHandle a Honcho tool call, with lazy session init for tools-only mode.z$Honcho is not active (cron context).z(Honcho session could not be initialized.z&Honcho is not active for this session.r
   r   r	  r   )r   NzFailed to update peer card.zPeer card updated (z facts).)r   r   r   zNo profile facts available yet.r   r   r+   z!Missing required parameter: queryr   i   i  )r   r   zNo relevant context found.r   r#   r
  zNo result from Honcho.r$   zNo context available yet.r   z## Summary
r   z## Representation
z## Card
recent_messagesr  c              3  N   K   | ] }d |d          d|d         dd          V  !dS )z  [rolez] r  N   rS   ).0ms     rL   	<genexpr>z8HonchoMemoryProvider.handle_tool_call.<locals>.<genexpr>  sX       ( ( @ai??1Y<+=??( ( ( ( ( (rN   z## Recent messages
r   zNo context available.r%   r'   r&   z8Exactly one of conclusion or delete_id must be provided.zConclusion z	 deleted.zFailed to delete conclusion .zConclusion saved for z: zFailed to save conclusion.zUnknown tool: zHoncho tool %s failed: %szHoncho z	 failed: )rI   r	   rF   r   r1   r3   r   set_peer_cardrc   rj   r   get_peer_cardr   r   search_contextr  r=   rE   get_session_contextr   r   r   rT   delete_conclusionr6  r\   r   error)rK   rB  rC  r   r   card_updater   r   r   r   r#   r   r   msgsmsg_strr'   r&   has_delete_idhas_conclusionokr   s                        rL   handle_tool_callz%HonchoMemoryProvider.handle_tool_call  sT    	FDEEE ( 	N'')) N!"LMMM} 	HD$5 	HFGGGW	A,,,xx//"hhv.. o!]889JK^b8ccF~)*GHHH:1\s6{{1\1\1\fl&m&mnnn}2243D42PP U:x1R&STTTz8T"2333o--"-- K%&IJJJ TXXlC%@%@!A!A4HH
xx//55%u$ 6    P:x1M&NOOOz8V"4555000"-- K%&IJJJxx//"&((+<"="=66%u$3 7   -1,<)z8V-O7O"PQQQ...xx//m778IPT7UU O:x1L&MNNN779%% BLL!@I!@!@AAA77+,, PLL!Ns;K7L!N!NOOO776?? <LL!:S[!:!:;;;77,-- C01D"ii ( (!%bcc( ( (  G LL!A!A!ABBBz8V[[-?-?-ZCZ"[\\\///!XXk228b??AA	!XXlB77==??
xx// $Y!%j!1!1 N22%&`aaa  S889JI\`8aaB Z#z85W95W5W5W*XYYY%&QY&Q&Q&QRRR]44T5F
Y]4^^ `:x1]1]1]Q[1]1]&^___!">???:y::;;; 	A 	A 	ALL4iCCC?	??A??@@@@@@@@	As   A%U- 'U- -8U- &U- <,U- )BU- +U- ,U- .A1U-  AU- 6D!U- B U- 9?U- 9U- ?U- U- U- -
V270V-'V2-V2c                    | j         | j        fD ].}|r*|                                r|                    d           /| j        r-	 | j                                         d S # t          $ r Y d S w xY wd S )Nr/  r   )r8   r9   r   r   r1   r<  r\   )rK   r8  s     rL   shutdownzHonchoMemoryProvider.shutdown  s    '):; 	$ 	$A $QZZ\\ $s###= 	'')))))   	 	s   A! !
A/.A/N)rO   rP   )rO   rT   )rl   rP   rx   ry   rO   rz   )r   rP   rO   rz   )r   ry   rO   rP   )r   rP   r   rP   rO   rP   )r   rP   rO   rP   )r   rP   r   rP   rO   rz   )r   r   rO   rP   )r   r   r   r   r   rT   rO   rP   )r   rP   rO   rT   )r   rP   rO   rP   )r  r   r  rP   rO   rz   )r  rP   r  r   rO   r   )r&  rP   r'  rP   r   rP   rO   rz   )r2  rP   r   rP   r  rP   rO   rz   )r   r:  rO   rz   )rO   r:  )rB  rP   rC  ry   rO   rP   r   )#__name__
__module____qualname____doc__rM   propertyr   r^   ro   rw   r   r   r   r   r   r   r   r   r   r   __annotations___LEVEL_ORDERr   r   staticmethodr  r   r  r%  r1  r9  r=  rA  rZ  r\  rS   rN   rL   r)   r)      s        RR!# !# !#F    X   ? ? ?
 
 
+ + + +O! O! O! O!b8> 8> 8> 8>t   4" " " ":. . . .` 9; j j j j j jX" " " " ?A .& .& .& .& .& .&n 
8 
8 
 
 
 
 ?L   % % % %N ) ) ) \)&( ( ( (T' ' ' ' * * * \*X Y[ " " " " " ">   $C C C C	& 	& 	& 	&dA dA dA dAL	 	 	 	 	 	rN   r)   rO   rz   c                H    |                      t                                 dS )z,Register Honcho as a memory provider plugin.N)register_memory_providerr)   )r   s    rL   registerrg    s#      !5!7!788888rN   r   )r`  
__future__r   rc   loggingr  r5   typingr   r   r   r   agent.memory_providerr   tools.registryr	   	getLoggerr]  r   PROFILE_SCHEMASEARCH_SCHEMAREASONING_SCHEMACONTEXT_SCHEMACONCLUDE_SCHEMAr@  r)   rg  rS   rN   rL   <module>rs     s-    # " " " " "   				     , , , , , , , , , , , , 0 0 0 0 0 0 % % % % % %		8	$	$ 	6
  !  C 
   (+h 

 

   2 	X  !G 
 "[ 
 !  C 
 
 I!  : 	K  != 
 !T DCC    !  C '
 
0 I5 % % P 	;  !p 
 !  C 	
 	
   2 	b  !  R 
 !  \ 
 !  C 
 
 !  < #M3C^Ude [ [ [ [ [> [ [ [D9 9 9 9 9 9rN   