
    i_                        d 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m	Z	m
Z
mZmZ ddlmZmZ dZdZd.ded	efd
Zdeeeedf         d	efdZde
eee	f                  d	efdZefdededed	efdZdededeee	f         d	ee         fdZdZd/deded	efdZ	 	 	 	 d0dedededed	ef
dZd	efdZdddd d!d"d d#d"d$d%dd&d'g d(d)Zdd*l m!Z!m"Z"  e!j#        dded+ ed,-           dS )1a  
Session Search Tool - Long-Term Conversation Recall

Searches past session transcripts in SQLite via FTS5, then summarizes the top
matching sessions using a cheap/fast model (same pattern as web_extract).
Returns focused summaries of past conversations rather than raw transcripts,
keeping the main model's context window clean.

Flow:
  1. FTS5 search finds matching messages ranked by relevance
  2. Groups by session, takes the top N unique sessions (default 3)
  3. Loads each session's conversation, truncates to ~100k chars centered on matches
  4. Sends to Gemini Flash with a focused summarization prompt
  5. Returns per-session summaries with metadata
    N)DictAnyListOptionalUnion)async_call_llmextract_content_or_reasoningi i'     defaultreturnc                    	 ddl m}  |            }n# t          $ r | cY S w xY wt          |t                    r|                    di           ni }t          |t                    r|                    di           ni }t          |t                    s| S |                    d          }|| S 	 t          |          }n# t          t          f$ r | cY S w xY wt          dt          |d                    S )	z?Read auxiliary.session_search.max_concurrency with sane bounds.r   )load_config	auxiliarysession_searchmax_concurrencyN      )hermes_cli.configr   ImportError
isinstancedictgetint	TypeError
ValueErrormaxmin)r   r   configauxtask_configrawvalues          >/home/ubuntu/.hermes/hermes-agent/tools/session_search_tool.py#_get_session_search_max_concurrencyr$      s   111111   )3FD)A)A
I&**["
%
%
%rC3=c43H3HP#''*B///bKk4(( 
//+
,
,C
{Cz"   q#eQ--   s    ""0C   CCtsc                    | dS 	 t          | t          t          f          r0ddlm} |                    |           }|                    d          S t          | t                    r{|                     dd                              dd                                          r=ddlm} |                    t          |                     }|                    d          S | S nf# t          t          t          f$ r"}t          j        d	| |d
           Y d}~n2d}~wt          $ r"}t          j        d| |d
           Y d}~nd}~ww xY wt          |           S )zConvert a Unix timestamp (float/int) or ISO string to a human-readable date.

    Returns "unknown" for None, str(ts) if conversion fails.
    Nunknownr   )datetimez%B %d, %Y at %I:%M %p. -z!Failed to format timestamp %s: %sTexc_infoz,Unexpected error formatting timestamp %s: %s)r   r   floatr(   fromtimestampstrftimestrreplaceisdigitr   OSErrorOverflowErrorloggingdebug	Exception)r%   r(   dtes       r#   _format_timestampr;   3   s   
 
zy\b3,'' 	8))))))''++B;;6777b# 	zz#r""**333;;== <------++E"II66{{#:;;;I	 / Q Q Q92q4PPPPPPPPP \ \ \Db!VZ[[[[[[[[[\r77Ns1   AC# BC#  C# #E:DE$EEmessagesc                    g }| D ]}|                     dd                                          }|                     d          pd}|                     d          }|dk    rI|rGt          |          dk    r|dd	         d
z   |dd         z   }|                    d| d|            |dk    r |                     d          }|rt	          |t
                    rg }|D ]k}t	          |t                    rT|                     d          p)|                     di                                dd          }	|                    |	           l|r,|                    dd                    |           d           |r|                    d|            |                    d|            |                    d| d|            d                    |          S )zEFormat session messages into a readable transcript for summarization.roler'   contentr*   	tool_nameTOOL  N   z
...[truncated]...
iz[TOOL:z]: 	ASSISTANT
tool_callsnamefunction?z[ASSISTANT]: [Called: z, ]z[ASSISTANT]: [z

)r   upperlenappendr   listr   join)
r<   partsmsgr>   r?   r@   rE   tc_namestcrF   s
             r#   _format_conversationrT   M   s   E 1 1wwvy))//11'')$$*GGK((	6>>i>7||c!!!$3$-*AAGDEENRLL9)9999::::[  ..J 8jT:: 8$ . .B!"d++ .!vvf~~X
B1G1G1K1KFTW1X1X --- RLL!P$))H:M:M!P!P!PQQQ <LL!:!:!:;;;6W667777LL/T//g//0000;;u    	full_textquery	max_charsc                    t          |           |k    r| S |                                 }|                                                                }g }t          j        t          j        |                    }d |                    |          D             }|s|                                }t          |          dk    ri |D ]6}d t          j        t          j        |          |          D             |<   7t          |fd          	                    g           D ]4t          fd|D                       r|                               5|sj|                                }|D ]S}t          j        t          j        |          |          D ])}	|                    |	                                           *T|s&| d|         }
|t          |           k     rdnd	}|
|z   S |                                 d
}d
}|D ]}t          d
||dz  z
            |z   t          |           k    r/t          d
t          |           |z
            t          |           t          fd|D                       }||k    r|}}|}t          t          |           ||z             }| ||         }
|d
k    rdnd	}|t          |           k     rdnd	}||
z   |z   S )a%  
    Truncate a conversation transcript to *max_chars*, choosing a window
    that maximises coverage of positions where the *query* actually appears.

    Strategy (in priority order):
    1. Try to find the full query as a phrase (case-insensitive).
    2. If no phrase hit, look for positions where all query terms appear
       within a 200-char proximity window (co-occurrence).
    3. Fall back to individual term positions.

    Once candidate positions are collected the function picks the window
    start that covers the most of them.
    c                 6    g | ]}|                                 S  start.0ms     r#   
<listcomp>z,_truncate_around_matches.<locals>.<listcomp>   s     JJJQqwwyyJJJrU   r   c                 6    g | ]}|                                 S r[   r\   r^   s     r#   ra   z,_truncate_around_matches.<locals>.<listcomp>   s-     % % %"#AGGII% % %rU   c                 J    t                              | g                     S N)rL   r   )tterm_positionss    r#   <lambda>z*_truncate_around_matches.<locals>.<lambda>   s    c.2D2DQ2K2K.L.L rU   )keyc              3      K   | ]9}|k    t          fd                     |g           D                       V  :dS )c              3   D   K   | ]}t          |z
            d k     V  dS )   N)abs)r_   pposs     r#   	<genexpr>z5_truncate_around_matches.<locals>.<genexpr>.<genexpr>   s2      NNqAGs*NNNNNNrU   N)anyr   )r_   re   rn   rarestrf   s     r#   ro   z+_truncate_around_matches.<locals>.<genexpr>   sb        F{{ NNNNN4F4Fq"4M4MNNNNN"{{{ rU   Nz&

...[later conversation truncated]...r*   r      c              3   <   K   | ]}|cxk    rk     n nd V  dS )r   Nr[   )r_   rm   wewss     r#   ro   z+_truncate_around_matches.<locals>.<genexpr>   s7      ??!",,,,B,,,,,A,,,,??rU   z(...[earlier conversation truncated]...

)rL   lowerstriprecompileescapefinditersplitr   r   allrM   r]   sortr   sum)rV   rW   rX   
text_lowerquery_lowermatch_positions
phrase_pattermsre   r`   	truncatedsuffix
best_start
best_count	candidatecountr]   endprefixrn   rq   rf   rt   ru   s                      @@@@@r#   _truncate_around_matchesr   o   sr     9~~""""J++--%%''K!#O BIk2233JJJ**=*=j*I*IJJJO  0!!##u::>>35N  % %'){29Q<<'L'L% % %q!! $L$L$L$LMMMF%))&"55 0 0      "     0
 $**3///  2!!## 	2 	2A[1z:: 2 2&&qwwyy11112  "jyj)	?H3y>>?Y?Y;;_a6!! JJ$ 	 		I	Q.//)^IQI233BYB??????????:JJE
c)nnei/
0
0C%)$I=BQYY99BF;>Y;O;O77UWFI&&rU   conversation_textsession_metac           
        K   d}|                     dd          }t          |                     d                    }d| d| d| d|  d	| 
}d
}t          |          D ]}	 t          dd|dd|dgdt                     d{V }	t          |	          }
|
r|
c S t          j        d|dz   |           ||dz
  k     r!t          j	        d|dz   z             d{V  |
c S # t          $ r t          j        d           Y  dS t          $ rS}||dz
  k     r!t          j	        d|dz   z             d{V  nt          j        d||d           Y d}~ dS Y d}~d}~ww xY wdS )zDSummarize a single session conversation focused on the search query.aN  You are reviewing a past conversation transcript to help recall what happened. Summarize the conversation with a focus on the search topic. Include:
1. What the user asked about or wanted to accomplish
2. What actions were taken and what the outcomes were
3. Key decisions, solutions found, or conclusions reached
4. Any specific commands, files, URLs, or technical details that were important
5. Anything left unresolved or notable

Be thorough but concise. Preserve specific details (commands, paths, error messages) that would be useful to recall. Write in past tense as a factual recap.sourcer'   
started_atzSearch topic: z
Session source: z
Session date: z

CONVERSATION TRANSCRIPT:
z-

Summarize this conversation with focus on: r
   r   system)r>   r?   userg?)taskr<   temperature
max_tokensNz9Session search LLM returned empty content (attempt %d/%d)r   z6No auxiliary model available for session summarizationz2Session summarization failed after %d attempts: %sTr,   )r   r;   ranger   MAX_SUMMARY_TOKENSr	   r6   warningasynciosleepRuntimeErrorr8   )r   rW   r   system_promptr   starteduser_promptmax_retriesattemptresponser?   r:   s               r#   _summarize_sessionr      sh     
	R  h	22F 0 0 > >??G	> 	> 	>!	> 	> 	> 	> &7	> 	> 7<		> 	>  K%% ! ! 	+%%-@@#<<  -        H 38<<G OWY`cdYdfqrrrq((mA1$5666666666NNN 	 	 	OTUUU444 
	 
	 
	q((mA1$56666666666H!	    tttttt 76666
	/! !s,   #8C%AC%!C%%E#	E#AEE#)toollimitcurrent_session_idc                 2   	 |                      |dz   t          t                    d          }d}|r}	 |}t                      }|}|rU||vrQ|                    |           |}|                     |          }|r|                    d          nd}|r|nd}|r||vQn# t          $ r |}Y nw xY wg }	|D ]}|                    dd          }|r||k    s||k    r'|                    d          r=|	                    ||                    d          pd|                    d	d          |                    d
d          |                    dd          |                    dd          |                    dd          d           t          |	          |k    r nt          j        dd|	t          |	          dt          |	           ddd          S # t          $ r5}
t          j        d|
d           t          d|
 d          cY d}
~
S d}
~
ww xY w)z<Return metadata for the most recent sessions (no LLM calls).r   T)r   exclude_sourcesorder_by_last_activeNparent_session_ididr*   titler   r   last_activemessage_countr   preview)
session_idr   r   r   r   r   r   recentzShowing zE most recent sessions. Use a keyword query to search specific topics.)successmoderesultsr   messageFensure_asciiz!Error listing recent sessions: %sr,   z Failed to list recent sessions: r   )list_sessions_richrN   _HIDDEN_SESSION_SOURCESsetaddget_sessionr   r8   rM   rL   jsondumpsr6   error
tool_error)dbr   r   sessionscurrent_rootsidvisitedsparentr   r:   s              r#   _list_recent_sessionsr   
  s   4Q((!) !899!% ) 
 
  	22(%%1 5c00KK$$$#&Ls++A;<FQUU#6777$F$*4&&C  5c00  2 2 212  	 	A%%b//C !4!4?Q8Q8Quu()) NN!w/4%%"--eeL"55 uu]B77!"!:!:55B//     7||u$$ % z\\u#g,,uuu
 
    	  Q Q Q91tLLLL@Q@@%PPPPPPPPPQsB   1G A)B G B-*G ,B--D)G 
H!*HHHrole_filterc           	          t          dd          S t          |t                    s*	 t          |          }n# t          t          f$ r d}Y nw xY wt          dt          |d                    } r                                 st          ||          S                                   	 d}|r3|                                rd |	                    d	          D             }
                     |t          t                    d
d          }|st          j        d g dddd          S dt          dt          ffd}|r ||          nd}i }	|D ]Y}
|
d         } ||          }|r||k    r|r||k    r'||	vrt!          |
          }
||
d<   |
|	|<   t#          |	          |k    r nZg |	                                D ]\  }}	                     |          }|s                    |          pi }t+          |          }t-          |           }                    ||||f           n# t0          $ r"}t3          j        d||d           Y d}~d}~ww xY wdt6          t8          t          t0          f                  f fd}	 ddlm}  | |                      }nI# t>          j         j!        $ r2 t3          j        dd           t          j        dddd          cY S w xY wg }tE          |          D ]\  \  }}}}}
t          |
t0                    rt3          j        d||
d           d}
|tG          |$                    d                    |$                    dd          |$                    d          d }|
r|
|d!<   n|r|dd"         d#z   nd$}d%| |d!<   |                    |           t          j        d |t#          |          t#          |	          d&d          S # t0          $ rB}t3          j%        d'|d           t          d(t          |           d          cY d}~S d}~ww xY w))a  
    Search past sessions and return focused summaries of matching conversations.

    Uses FTS5 to find matches, then summarizes the top sessions with Gemini Flash.
    The current session is excluded from results since the agent already has that context.
    NzSession database not available.Fr   r
   r   r   c                 ^    g | ]*}|                                 |                                 +S r[   )rw   )r_   rs     r#   ra   z"session_search.<locals>.<listcomp>h  s-    PPPqaggiiPPPPrU   ,2   r   )rW   r   r   r   offsetTzNo matching sessions found.)r   rW   r   r   r   r   r   r   c                 2   t                      }| }|r||vr|                    |           	                     |          }|snQ|                    d          }|r|}nn6n/# t          $ r"}t          j        d||d           Y d}~nd}~ww xY w|r||v|S )z9Walk delegation chain to find the root parent session ID.r   z)Error resolving parent for session %s: %sTr,   N)r   r   r   r   r8   r6   r7   )r   r   r   sessionr   r:   r   s         r#   _resolve_to_parentz*session_search.<locals>._resolve_to_parent~  s    eeGC #W,,C    nnS11G" $[[)<==F $  !   MC!%	    EEEE  #W,,& Js   A" A" "
B,B		Bz Failed to prepare session %s: %sr,   c            	      ^  K   t          t                      t          dt                                        } t	          j        |           dt          dt          t          t          f         dt          t                   ffdfdD             }t	          j
        |ddi d	{V S )
z0Summarize all sessions with bounded concurrency.r   textmetar   c                    K   4 d {V  t          | |           d {V cd d d           d {V  S # 1 d {V swxY w Y   d S rd   )r   )r   r   rW   	semaphores     r#   _bounded_summaryz@session_search.<locals>._summarize_all.<locals>._bounded_summary  s     $ G G G G G G G G!3D%!F!FFFFFFFG G G G G G G G G G G G G G G G G G G G G G G G G G G G G Gs   6
A A c                 2    g | ]\  }}}} ||          S r[   r[   )r_   _r   r   r   s       r#   ra   z:session_search.<locals>._summarize_all.<locals>.<listcomp>  s>       $Aq$ ! t,,  rU   return_exceptionsTN)r   r$   r   rL   r   	Semaphorer1   r   r   r   gather)r   corosr   r   rW   taskss     @@r#   _summarize_allz&session_search.<locals>._summarize_all  s      !"E"G"GQPSTYPZPZI[I[\\O)/::IGS GS#X G8TW= G G G G G G G   (-  E !G$GGGGGGGGGrU   )
_run_asyncz0Session summarization timed out after 60 secondszOSession summarization timed out. Try a more specific query or reduce the limit.)r   r   z"Failed to summarize session %s: %ssession_startedr   r'   model)r   whenr   r   summaryrB   u   
…[truncated]zNo preview available.u,   [Raw preview — summarization unavailable]
)r   rW   r   r   sessions_searchedzSession search failed: %szSearch failed: )&r   r   r   r   r   r   r   rw   r   r|   search_messagesrN   r   r   r   r1   r   rL   itemsget_messages_as_conversationr   rT   r   rM   r8   r6   r   r   r   model_toolsr   
concurrentfuturesTimeoutErrorzipr;   r   r   )rW   r   r   r   r   	role_listraw_resultsr   current_lineage_rootseen_sessionsresultraw_sidresolved_sidr   
match_infor<   r   r   r:   r   r   r   	summariesr   entryr   r   s   `  `                      @r#   r   r   C  s)    
z;UKKKK
 eS!! 	JJEE:& 	 	 	EEE	3ua==!!E  D D$R0BCCCKKMMEeE	 	Q;,,.. 	QPPK,=,=c,B,BPPPI ((! !899 ) 
 
  	#:8  "# # # #	3 	3 	 	 	 	 	 	4 7IR1222d 	 ! 	 	F\*G--g66L $ 8L(L(L! g1C&C&C=00f'3|$.4l+=!!U** + &3&9&9&;&; 	 	"J
:::FF !~~j99?R$8$B$B!$<=NPU$V$V!j*6GVWWWW   6!	        	Hd5i+@&A 	H 	H 	H 	H 	H 	H 	H	# /..... j!1!122GG!. 	# 	# 	#OB    : j  "# # # # # #	# 	FI%QXFYFY 	$ 	$B:Z%6F&),, 8     ))*..9J*K*KLL$..9==#00	 E  ]#)i   M^z,TcT25GGGcz#\SZ#\#\i U####z ^^!$]!3!3
 
    	  E E E11tDDDD4CFF44eDDDDDDDDDEs   = AA0B O: 1BO: H6%O: &AH65O: 6
I" IO: I"".O: J+ *O: +AK1.O: 0K11DO: :
Q7Q;QQc                  d    	 ddl m}  | j                                        S # t          $ r Y dS w xY w)z;Requires SQLite state database and an auxiliary text model.r   DEFAULT_DB_PATHF)hermes_stater   r   existsr   r   s    r#   !check_session_search_requirementsr     sP    000000%,,...   uus   ! 
//r   u6  Search your long-term memory of past conversations, or browse recent sessions. This is your recall -- every past session is searchable, and this tool summarizes what happened.

TWO MODES:
1. Recent sessions (no query): Call with no arguments to see what was worked on recently. Returns titles, previews, and timestamps. Zero LLM cost, instant. Start here when the user asks what were we working on or what did we do recently.
2. Keyword search (with query): Search for specific topics across all past sessions. Returns LLM-generated summaries of matching sessions.

USE THIS PROACTIVELY when:
- The user says 'we did this before', 'remember when', 'last time', 'as I mentioned'
- The user asks about a topic you worked on before but don't have in current context
- The user references a project, person, or concept that seems familiar but isn't in memory
- You want to check if you've solved a similar problem before
- The user asks 'what did we do about X?' or 'how did we fix Y?'

Don't hesitate to search when it is actually cross-session -- it's fast and cheap. Better to search and confirm than to guess or ask the user to repeat themselves.

Search syntax: keywords joined with OR for broad recall (elevenlabs OR baseten OR funding), phrases for exact match ("docker networking"), boolean (python NOT java), prefix (deploy*). IMPORTANT: Use OR between keywords for best results — FTS5 defaults to AND which misses sessions that only mention some terms. If a broad OR query returns nothing, try individual keyword searches in parallel. Returns summaries of the top matching sessions.objectstringu   Search query — keywords, phrases, or boolean expressions to find in past sessions. Omit this parameter entirely to browse recent sessions instead (returns titles, previews, timestamps with no LLM cost).)typedescriptionzqOptional: only search messages from specific roles (comma-separated). E.g. 'user,assistant' to skip tool outputs.integerz/Max sessions to summarize (default: 3, max: 5).)r  r  r   )rW   r   r   )r  
propertiesrequired)rF   r  
parameters)registryr   c           	          t          |                     d          pd|                     d          |                     dd          |                    d          |                    d                    S )	NrW   r*   r   r   r
   r   r   )rW   r   r   r   r   )r   r   )argskws     r#   rg   rg   K  sf    ~hhw%2HH]++hhw""66$<<66"677 9  9  9 rU   u   🔍)rF   toolsetschemahandlercheck_fnemoji)r
   rd   )Nr
   NN)$__doc__r   concurrent.futuresr   r   r6   rx   typingr   r   r   r   r   agent.auxiliary_clientr   r	   MAX_SESSION_CHARSr   r   r$   r.   r1   r;   rT   r   r   r   r   r   boolr   SESSION_SEARCH_SCHEMAtools.registryr	  r   registerr[   rU   r#   <module>r     s5            				 3 3 3 3 3 3 3 3 3 3 3 3 3 3 O O O O O O O O  ! ! !S ! ! ! !*%UC 56 3    44S#X#7 C    F 2CR' R'R'R'+.R'R' R' R' R'j==#&=6:38n=c]= = = =F $ 6Q 6QS 6Qc 6QS 6Q 6Q 6Q 6Qv "FE FEFEFE FE
 FE 	FE FE FE FER4     	X.  !  n 
 !  S 
 "P 
 
 # 3, , ` 0 / / / / / / /  	 9 9 /
     rU   