
    }-je                     (    d Z d Zd ZdddefdZdS )a1  
Shared helpers for session compression anchor metadata.

Manual compression anchoring versus automatic compression paths
===============================================================

When ``auto_compression=True`` is passed to ``visible_messages_for_anchor()``,
the function accepts a broader set of message content types (including
provider-style ``input_text`` / ``output_text`` parts) and metadata markers
(``reasoning``, ``thinking``, etc.) from any non-tool role. This enables the
streaming auto-compression path to determine which messages should anchor
compression UI metadata without being limited to the legacy manual-compression
rules.

When ``auto_compression=False`` (the default), the function applies the
historical manual-compression rules: only plain ``text`` content parts from
non-assistant roles are counted.

Why this module exists
======================

Compression anchoring needs to identify which messages in a session transcript
are semantically significant enough to seed the compression UI metadata (e.g.,
message count, token budget display). The original implementation hard-coded
these rules in multiple places. This module consolidates the logic so that:

1. Manual compression anchoring (CLI/legacy path) uses the stricter ruleset.
2. Automatic compression (streaming/agent path) can leverage the relaxed ruleset
   when it knows it is handling provider-style messages.

Callers specify ``auto_compression=True`` when the messages may originate from
an automatic/compression-aware pipeline, and ``False`` (default) for manual
compression contexts.
c                    t          | t                    r3d                    fd| D                                                       S t	          | pd                                          S )N
c              3      K   | ]i}t          |t                    rR|                    d           v .t          |                    d          p|                    d          pd          V  jdS )typetextcontent N)
isinstancedictgetstr.0part
part_typess     ,/root/hermes-webui/api/compression_anchor.py	<genexpr>z _content_text.<locals>.<genexpr>'   s       
 
$%%
 +/((6*:*:j*H*H   =DHHY$7$7=2>>*H*H*H*H
 
    r   )r	   listjoinstripr   r   r   s    `r   _content_textr   %   s|    '4   yy 
 
 
 

 
 
 
 
 %''		
 w}"##%%%r   c                 h    t          | t                    sdS t          fd| D                       S )NFc              3   p   K   | ]0}t          |t                    o|                    d           v V  1dS )r   N)r	   r
   r   r   s     r   r   z)_content_has_part_type.<locals>.<genexpr>2   sW         	4A488F#3#3z#A     r   )r	   r   anyr   s    `r   _content_has_part_typer   /   sR    gt$$ u         r   F)auto_compressionr   c                2   g }|rh dndh}| pg D ]}t          |t                    s|                    d          }|r|dk    r7|                    dd          }t          |                    d                    }t	          ||          }|rt          t          |                    d	          t
                    o|                    d	                    }	t          |d
h          }
t          |                    d                    }|s|pt          |ddh          }|s|s|	s|
s|r|                    |           6|dk    rt          t          |                    d	          t
                    o|                    d	                    }	t          |d
h          }
t          |                    d                    pt          |ddh          }|s|s|	s|
s|r|                    |           |s|r|                    |           |S )a  Return transcript messages that can anchor compression UI metadata.

    Manual compression historically only counted plain ``text`` content parts
    for non-assistant messages, while the streaming auto-compression path also
    accepted provider-style ``input_text`` / ``output_text`` parts and metadata
    markers on any non-tool role. Keep that difference explicit at the call site
    instead of carrying two near-identical helper implementations.
    >   r   
input_textoutput_textr   roletoolr   r   attachments)r   
tool_callstool_use	reasoningthinking	assistant)r	   r
   r   boolr   r   r   append)messagesr   outtext_part_typesmessager!   r   has_attachmentsr   has_tool_callshas_tool_usehas_reasonings               r   visible_messages_for_anchorr3   8   sn    C?O];;;;V\U]O>r (  ( '4(( 	{{6"" 	tv~~++i,,w{{=99::WAAA 	!7;;|44d;;YL@Y@Y N 2'J<HHL [!9!9::M  - !1G-2 2  $ $. $L $M $

7###;!7;;|44d;;YL@Y@Y N 2'J<HHL [!9!9:: >T[)? ?M  $ $. $L $M $

7### 	 ? 	 JJwJr   N)__doc__r   r   r)   r3    r   r   <module>r6      sd   ! !H& & &   GL 4 4 4t 4 4 4 4 4 4r   