
    *j                     4    d Z d Z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     9/Users/bertmahoney/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   c                    t          | t                    sdS |                     d          }|r|dk    rdS t          |                     dd          h d                                                                          }|                    d          p)|                    d	          p|                    d
          S )zFReturn true for synthetic compression/reference cards, not user turns.Froletoolr   r   >   r   
input_textoutput_textr   z[context compactionzcontext compactionz@[your active task list was preserved across context compression])r	   r
   r   r   lowerlstrip
startswith)messager   r   s      r   is_context_compression_markerr'   8   s    gt$$ u;;vD 46>>uIr""888   eggffhh 	
 	-.. 	_??/00	_??]^^r   c                      t          |           S )zABackward-compatible alias for callers that have not switched yet.)r'   )r&   s    r   _is_context_compression_markerr)   J   s    (111r   F)auto_compressionr*   c                R   g }|rh dndh}| pg D ]}t          |t                    s|                    d          }|r|dk    r7t          |          rG|                    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|                    |           F|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   r    r!   r   r   r   r   r   attachmentsr"   
tool_callstool_use	reasoningthinking	assistant)	r	   r
   r   r)   boolr   r   r   append)messagesr*   outtext_part_typesr&   r   r   has_attachmentsr   has_tool_callshas_tool_usehas_reasonings               r   visible_messages_for_anchorr;   O   s    C?O];;;;V\U]O>r *  * '4(( 	{{6"" 	tv~~)'22 	++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'   r)   r2   r;    r   r   <module>r>      s   ! !H& & &    $2 2 2
 GL 6 6 6t 6 6 6 6 6 6r   