
    i              
           d Z ddlZddlZddlmZ ddlmZ  ej        e          Z	dZ
ddeded	ed
ee         fdZdededed
dfdZdedededed
df
dZdS )zAuto-generate short session titles from the first user/assistant exchange.

Runs asynchronously after the first response is delivered so it never
adds latency to the user-facing reply.
    N)Optional)call_llmzGenerate a short, descriptive title (3-7 words) for a conversation that starts with the following exchange. The title should capture the main topic or intent. Return ONLY the title text, nothing else. No quotes, no punctuation at the end, no prefixes.      >@user_messageassistant_responsetimeoutreturnc                 F   | r
| dd         nd}|r
|dd         nd}dt           ddd| d| dg}	 t          d	|d
d|          }|j        d         j        j        pd                                }|                    d          }|                                                    d          r|dd                                         }t          |          dk    r|dd         dz   }|r|ndS # t          $ r&}t                              d|           Y d}~dS d}~ww xY w)zGenerate a session title from the first exchange.

    Uses the auxiliary LLM client (cheapest/fastest available model).
    Returns the title string or None on failure.
    Ni   system)rolecontentuserzUser: z

Assistant: title_generation   g333333?)taskmessages
max_tokenstemperaturer   r   z"'ztitle:   P   M   z...zTitle generation failed: %s)_TITLE_PROMPTr   choicesmessager   striplower
startswithlen	Exceptionloggerdebug)	r   r   r   user_snippetassistant_snippetr   responsetitlees	            =/home/agentuser/.hermes/hermes-agent/agent/title_generator.pygenerate_titler)      sl    *6=<%%2L4FN*4C400B m44$]\$]$]J[$]$]^^H
#
 
 
 !!$,4:AACCE"";;==##H-- 	&!""IOO%%Eu::??#2#J&E'uu4'   2A666ttttts   B<C0 0
D :DD 
session_idc                 \   | r|sdS 	 |                      |          }|rdS n# t          $ r Y dS w xY wt          ||          }|sdS 	 |                     ||           t                              d|           dS # t          $ r&}t                              d|           Y d}~dS d}~ww xY w)a!  Generate and set a session title if one doesn't already exist.

    Called in a background thread after the first exchange completes.
    Silently skips if:
    - session_db is None
    - session already has a title (user-set or previously auto-generated)
    - title generation fails
    Nz Auto-generated session title: %sz&Failed to set auto-generated title: %s)get_session_titler    r)   set_session_titler!   r"   )
session_dbr*   r   r   existingr&   r'   s          r(   auto_title_sessionr0   ;   s     Z //
;; 	F	    <);<<E B$$Z7777????? B B B=qAAAAAAAAABs$   " 
001A; ;
B+B&&B+conversation_historyc                     | r|r|r|sdS t          d |pg D                       }|dk    rdS t          j        t          | |||fdd          }|                                 dS )u   Fire-and-forget title generation after the first exchange.

    Only generates a title when:
    - This appears to be the first user→assistant exchange
    - No title is already set
    Nc              3   L   K   | ]}|                     d           dk    dV   dS )r   r      N)get).0ms     r(   	<genexpr>z#maybe_auto_title.<locals>.<genexpr>s   s7      \\qAEE&MMU[D[D[D[D[D[D[\\       Tz
auto-title)targetargsdaemonname)sum	threadingThreadr0   start)r.   r*   r   r   r1   user_msg_countthreads          r(   maybe_auto_titlerE   _   s      Z | CU  \\%9%?R\\\\\N!*l4FG	  F LLNNNNNr9   )r   )__doc__loggingr@   typingr   agent.auxiliary_clientr   	getLogger__name__r!   r   strfloatr)   r0   listrE    r9   r(   <module>rP      s5               + + + + + +		8	$	$c " " "# " "YabeYf " " " "J!B!B !B 	!B
 
!B !B !B !BH  	
  
     r9   