
    Yj                        d dl mZ d dlmZmZ d dlmZmZmZ ddl	m
Z
 erddlmZ  G d de          Z G d	 d
e          Z G d de          ZdS )    )annotations)TYPE_CHECKINGClassVar)	BaseModel
ConfigDictField   )Message)Peerc                      e Zd ZU dZ edd          Zded<    edd          Zded<    edd	          Zded
<    edd          Z	ded<    edd          Z
ded<   dS )Summaryz1Represents a summary of a session's conversation..zThe summary textdescriptionstrcontentz4The ID of the message that this summary covers up to
message_idz#The type of summary (short or long)summary_typez:The timestamp of when the summary was created (ISO format)
created_atz(The number of tokens in the summary textinttoken_countN)__name__
__module____qualname____doc__r   r   __annotations__r   r   r   r        ]/home/ubuntu/.hermes/hermes-agent/venv/lib/python3.11/site-packages/honcho/session_context.pyr   r      s         ;;5*<===G====eO  J     c/TUUULUUUUeU  J     uC  K      r   r   c                      e Zd ZU dZ edd          Zded<    edd          Zd	ed
<    edd          Zd	ed<   dS )SessionSummariesz5Contains both short and long summaries for a session..zThe session IDr   r   idNzThe short summary if availableSummary | Noneshort_summaryzThe long summary if availablelong_summary)	r   r   r   r   r   r!   r   r#   r$   r   r   r   r    r       s         ??eC%5666B6666$)E:% % %M     $)59$ $ $L      r   r    c                  
   e Zd ZU dZ ed          Zded<    edd          Zd	ed
<    edd          Z	ded<    edd          Z
ded<    edd          Zded<    edd          Zded<   d!dZd!dZd"dZd#d ZdS )$SessionContexta  
    Represents the context of a session containing a curated list of messages.

    The SessionContext provides methods to convert message history into formats
    compatible with different LLM providers while staying within token limits
    and providing optimal conversation context.

    Attributes:
        messages: List of Message objects representing the conversation context
    T)arbitrary_types_allowedzClassVar[ConfigDict]model_config.z)ID of the session this context belongs tor   r   
session_idz1List of Message objects to include in the contextzlist[Message]messagesNz:Summary of the session history prior to the message cutoffr"   summaryzLThe peer representation, if context is requested from a specific perspectivez
str | Nonepeer_representationzBThe peer card, if context is requested from a specific perspectivezlist[str] | None	peer_card	assistant
str | Peerreturnlist[dict[str, str]]c               z   t          |t                    r|n|j        fd| j        D             }g }| j        r#dd| j         dd}|                    |           | j        r#dd| j         dd}|                    |           | j        r(dd| j        j         d	d}|                    |           ||z   S )
a  
        Convert the context to OpenAI-compatible message format.

        Transforms the message history and summary into the format expected by
        OpenAI's Chat Completions API, with proper role assignments based on the
        assistant's identity.

        Args:
            assistant: The assistant peer (Peer object or peer ID string) to use
            for determining message roles. Messages from this peer will be marked
            as "assistant", others as "user"

        Returns:
            A list of dictionaries in OpenAI format, where each dictionary contains
            "role" and "content" keys suitable for the OpenAI API
        c                J    g | ]}|j         k    rd nd|j         |j        d S )r.   user)rolenamer   peer_idr   .0messageassistant_ids     r   
<listcomp>z,SessionContext.to_openai.<locals>.<listcomp>`   sO     
 
 
 	 (/,'F'FF"? 
 
 
r   system<peer_representation></peer_representation>r5   r   <peer_card></peer_card>	<summary>
</summary>	
isinstancer   r!   r*   r,   appendr-   r+   r   selfr.   r*   system_messagespeer_representation_messagepeer_card_messagesummary_messager<   s          @r   	to_openaizSessionContext.to_openaiI   s$   , %/y#$>$>PyyIL
 
 
 
  =
 
 
 13# 	@ c43Kccc+ +' ""#>???> 	6 EEEE! ! ""#4555< 	4 Gt|';GGG O ""?333))r   c               z   t          |t                    r|n|j        fd| j        D             }g }| j        r#dd| j         dd}|                    |           | j        r#dd| j         dd}|                    |           | j        r(dd| j        j         d	d}|                    |           ||z   S )
a  
        Convert the context to Anthropic-compatible message format.

        Transforms the message history into the format expected by Anthropic's
        Claude API, with proper role assignments based on the assistant's identity.

        Args:
            assistant: The assistant peer (Peer object or peer ID string) to use
                       for determining message roles. Messages from this peer will
                       be marked as "assistant", others as "user"

        Returns:
            A list of dictionaries in Anthropic format, where each dictionary contains
            "role" and "content" keys suitable for the Anthropic API

        Note:
            Future versions may implement role alternation requirements for
            Anthropic's API compatibility
        c                b    g | ]+}|j         k    r
d |j        dnd|j          d|j         d,S )r.   rA   r4   z: r7   r9   s     r   r=   z/SessionContext.to_anthropic.<locals>.<listcomp>   sr     
 
 
  ,.. $"?   %oBBBB 
 
 
r   r4   r?   r@   rA   rB   rC   rD   rE   rF   rI   s          @r   to_anthropiczSessionContext.to_anthropic   s$   2 %/y#$>$>PyyIL
 
 
 
  =
 
 
 13# 	@c43Kccc+ +' ""#>???> 	6EEEE! ! ""#4555< 	4Gt|';GGG O ""?333))r   r   c                B    t          | j                  | j        rdndz   S )z
        Return the number of messages in the context.

        Returns:
            The number of messages in this context
        r	   r   lenr*   r+   rJ   s    r   __len__zSessionContext.__len__   s$     4=!!$,%=QQA>>r   c                J    dt          | j                   d| j        rdnd dS )z
        Return a string representation of the SessionContext.

        Returns:
            A string representation suitable for debugging
        zSessionContext(messages=z
, summary=presentNone)rT   rV   s    r   __repr__zSessionContext.__repr__   s6     q#dm*<*<ppUYUaHm		gmppppr   )r.   r/   r0   r1   )r0   r   )r0   r   )r   r   r   r   r   r(   r   r   r)   r*   r+   r,   r-   rO   rR   rW   r\   r   r   r   r&   r&   )   s        	 	 *4D)Q)Q)QLQQQQeD  J     $eL  H     $eV  G     ',eb' ' '     #(%X# # #I    
6* 6* 6* 6*p=* =* =* =*~? ? ? ?q q q q q qr   r&   N)
__future__r   typingr   r   pydanticr   r   r   r;   r
   peerr   r   r    r&   r   r   r   <module>ra      s   " " " " " " * * * * * * * * 1 1 1 1 1 1 1 1 1 1           i    	 	 	 	 	y 	 	 	gq gq gq gq gqY gq gq gq gq gqr   