
    yjl                     .   d dl mZ d dlmZ d dlmZ d dlmZmZmZ d dl	m
Z
 d dlmZ dedefd	Zd
ee         eee                  z  eeeef                  z  dee         fdZ G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d dee          Z G d dee          Z G d dee          Z G d dee          Z G d  d!e          Zd"eeef         d#edefd$Zd%S )&    )Sequence)datetime)Any)	BaseModelFieldfield_validator)models)parse_datetime_iso	timestampreturnc                 0    |                      dd          S )z]
    Remove microseconds and timezone info from a datetime for stable string formatting.
    r   N)microsecondtzinfo)replace)r   s    @/DATA/AppData/hermes/projects/honcho/src/utils/representation.py _strip_microseconds_and_timezoner      s     4888    message_idsc                     g }| D ]J}t          |t          t          z            r|                    |           5|                    |           Kt          t          |                    S )aV  
    Flatten message_ids that may be in old tuple format or nested list format.

    This handles backwards compatibility with the old schema where message_ids
    was list[tuple[int, int]] representing ranges, and the new schema where
    it's list[int] representing individual message IDs.

    Args:
        message_ids: Either a flat list of ints, nested list, or list of tuples

    Returns:
        A flat list of unique message IDs, sorted

    Examples:
        [1, 2, 3] -> [1, 2, 3]
        [[1, 2], [3, 4]] -> [1, 2, 3, 4]
        [(105, 105)] -> [105]
        [[105, 105]] -> [105]
    )
isinstancelisttupleextendappendsortedset)r   resultitems      r   flatten_message_idsr      sn    , F    dTE\++ 	 MM$ MM$#f++r   c                   h    e Zd ZU  edd          Zeed<   eed<   ee	         ed<   dZ
edz  ed<   dS )	ObservationMetadata z Document ID for this observation)defaultdescriptionid
created_atr   Nsession_name)__name__
__module____qualname__r   r%   str__annotations__r   r   intr'    r   r   r!   r!   4   sb         eB,NOOOBOOOc#L#*#####r   r!   c                   2    e Zd ZU  ed          Zeed<   dS )ExplicitObservationBasezThe explicit observationr$   contentN)r(   r)   r*   r   r2   r+   r,   r.   r   r   r0   r0   ;   s0         5%?@@@GS@@@@@r   r0   c                       e Zd ZU  ede          Zee         ed<    ede          Zee         ed<    ed          Z	eed<   d	S )
DeductiveObservationBasez7Document IDs of premise observations for tree traversalr$   default_factory
source_idsz'Human-readable premise text for displaypremiseszThe deductive conclusionr1   
conclusionN)
r(   r)   r*   r   r   r7   r+   r,   r8   r9   r.   r   r   r4   r4   ?   s         !EM  JS	     %=  Hd3i    e(BCCCJCCCCCr   r4   c                       e Zd ZU dZ ede          Zee         ed<    ede          Z	ee         ed<    edd	          Z
eed
<    ed          Zeed<    edd	          Zeed<   dS )InductiveObservationBasez\Base model for inductive observations - patterns, generalizations, and personality insights.z6Document IDs of source observations for tree traversalr5   r7   z&Human-readable source text for displaysourceszSType of pattern: 'preference', 'behavior', 'personality', 'tendency', 'correlation'pattern)r$   r#   pattern_typez'The inductive generalization or patternr1   r9   z)Confidence level: 'high', 'medium', 'low'medium
confidenceN)r(   r)   r*   __doc__r   r   r7   r+   r,   r<   r>   r9   r@   r.   r   r   r;   r;   K   s         ff!EL  JS	    <  GT#Y    i  L#    e(QRRRJRRRe?  J     r   r;   c                       e Zd ZU dZ ede          Zee         ed<    ede          Z	ee         ed<    ed          Z
eed	<   d
S )ContradictionObservationBasezVBase model for contradiction observations - when user has made conflicting statements.z.Document IDs of the contradicting observationsr5   r7   z3Human-readable text of the contradicting statementsr<   z Description of the contradictionr1   r2   N)r(   r)   r*   rA   r   r   r7   r+   r,   r<   r2   r.   r   r   rC   rC   a   s         ``!ED  JS	    I  GT#Y    5%GHHHGSHHHHHr   rC   c                       e Zd ZU dZ ede          Zee         ed<    e	dd          e
dedefd	                        Zd
S )PromptRepresentationz\
    The representation format that is used when getting structured output from an LLM.
    zFacts LITERALLY stated by the user - direct quotes or clear paraphrases only, no interpretation or inference. Example: ['The user is 25 years old', 'The user has a dog named Rover']r5   explicitbefore)modevr   c                     |g S |S )zGConvert None to empty list - handles LLMs returning null instead of [].r.   )clsrI   s     r   convert_none_to_empty_listz/PromptRepresentation.convert_none_to_empty_listy   s     9Ir   N)r(   r)   r*   rA   r   r   rF   r0   r,   r   classmethodr   rL   r.   r   r   rE   rE   o   s           /4e L/ / /Hd*+   
 _Zh///3 3    [ 0/  r   rE   c                   F    e Zd ZdZdefdZdefdZdefdZde	de
fdZdS )	ExplicitObservationz/Explicit observation with content and metadata.r   c                 @    dt          | j                   d| j         S )N[] )r   r&   r2   selfs    r   __str__zExplicitObservation.__str__   s%    V3DODDVVVVVr   c                 l    | j         rd| j          dnd}| dt          | j                   d| j         S )LFormat with ID prefix for use by agents that need to reference observations.[id:rR   r"   rQ   )r%   r   r&   r2   )rT   	id_prefixs     r   str_with_idzExplicitObservation.str_with_id   sL    *.'9&47&&&&r	aa>tOOaaSWS_aaar   c                 D    t          | j        | j        | j        f          S )zD
        Make ExplicitObservation hashable for use in sets.
        hashr2   r&   r'   rS   s    r   __hash__zExplicitObservation.__hash__   s      T\4?D4EFGGGr   otherc                     t          |t                    sdS | j        |j        k    o| j        |j        k    o| j        |j        k    S )z
        Define equality for ExplicitObservation objects.
        Two observations are equal if all their fields match.
        F)r   rO   r2   r&   r'   rT   r_   s     r   __eq__zExplicitObservation.__eq__   sR    
 %!455 	5LEM) 85#338!U%77	
r   N)r(   r)   r*   rA   r+   rU   rZ   r-   r^   objectboolrb   r.   r   r   rO   rO      s        99W W W W WbS b b b b
H# H H H H
F 
t 
 
 
 
 
 
r   rO   c                   R    e Zd ZdZdefdZdefdZdefdZdefdZ	de
defdZd	S )
DeductiveObservationzODeductive observation with multiple premises and one conclusion, plus metadata.r   c                     d                     d | j        D                       }dt          | j                   d| j         d| S )N
c              3       K   | ]	}d | V  
dS     - Nr.   .0premises     r   	<genexpr>z/DeductiveObservation.__str__.<locals>.<genexpr>   +      !R!R"47"4"4!R!R!R!R!R!Rr   rQ   rR   )joinr8   r   r&   r9   rT   premises_texts     r   rU   zDeductiveObservation.__str__   sP    		!R!RDM!R!R!RRRj3DODDjjjj[hjjjr   c                     | j         rd| j          dnd}d                    d | j        D                       }| dt          | j                   d| j         d| S )rW   rX   rR   r"   rh   c              3       K   | ]	}d | V  
dS rj   r.   rl   s     r   ro   z3DeductiveObservation.str_with_id.<locals>.<genexpr>   rp   r   rQ   )r%   rq   r8   r   r&   r9   )rT   rY   rs   s      r   rZ   z DeductiveObservation.str_with_id   sw    *.'9&47&&&&r			!R!RDM!R!R!RRRuu>tOOuuSWSbuufsuuur   c                 b    d                     d | j        D                       }| j         d| S )Nrh   c              3       K   | ]	}d | V  
dS rj   r.   rl   s     r   ro   z9DeductiveObservation.str_no_timestamps.<locals>.<genexpr>   rp   r   )rq   r8   r9   rr   s     r   str_no_timestampsz&DeductiveObservation.str_no_timestamps   s:    		!R!RDM!R!R!RRR/44]444r   c                 D    t          | j        | j        | j        f          S )zr
        Make DeductiveObservation hashable for use in sets. NOTE: premises are not included in the hash.
        r]   r9   r&   r'   rS   s    r   r^   zDeductiveObservation.__hash__   s      T_dot7HIJJJr   r_   c                     t          |t                    sdS | j        |j        k    o| j        |j        k    o| j        |j        k    S )z
        Define equality for DeductiveObservation objects.
        Two observations are equal if all their fields match -- NOTE: premises are not included in the equality check.
        F)r   rf   r9   r&   r'   ra   s     r   rb   zDeductiveObservation.__eq__   sS    
 %!566 	5Ou// 85#338!U%77	
r   Nr(   r)   r*   rA   r+   rU   rZ   rx   r-   r^   rc   rd   rb   r.   r   r   rf   rf      s        YYk k k k kvS v v v v53 5 5 5 5K# K K K K
F 
t 
 
 
 
 
 
r   rf   c                   R    e Zd ZdZdefdZdefdZdefdZdefdZ	de
defdZd	S )
InductiveObservationzPInductive observation with sources, pattern type, and confidence, plus metadata.r   c                     d}| j         r)d | j         D             }dd                    |          z   }dt          | j                   d| j         d| j         | S )Nr"   c                     g | ]}d | S rk   r.   rm   sources     r   
<listcomp>z0InductiveObservation.__str__.<locals>.<listcomp>   !    III&-V--IIIr   rh   rQ   ] [rR   )r<   rq   r   r&   r@   r9   rT   sources_textsource_liness      r   rU   zInductiveObservation.__str__   sq    < 	:IIDLIIIL$))L"9"99L{3DODD{{{{\`\k{my{{{r   c                     | j         rd| j          dnd}d}| j        r)d | j        D             }dd                    |          z   }| dt          | j                   d| j         d| j         | S )rW   rX   rR   r"   c                     g | ]}d | S r   r.   r   s     r   r   z4InductiveObservation.str_with_id.<locals>.<listcomp>   r   r   rh   rQ   r   )r%   r<   rq   r   r&   r@   r9   rT   rY   r   r   s       r   rZ   z InductiveObservation.str_with_id   s    *.'9&47&&&&r	< 	:IIDLIIIL$))L"9"99L  G  G>tOO  G  GTXTc  G  Ggkgv  G  yE  G  G  	Gr   c                     d}| j         r)d | j         D             }dd                    |          z   }d| j         d| j         | S )Nr"   c                     g | ]}d | S r   r.   r   s     r   r   z:InductiveObservation.str_no_timestamps.<locals>.<listcomp>   r   r   rh   rQ   rR   )r<   rq   r@   r9   r   s      r   rx   z&InductiveObservation.str_no_timestamps   s[    < 	:IIDLIIIL$))L"9"99LE4?EEdoE|EEEr   c                 D    t          | j        | j        | j        f          S )z3Make InductiveObservation hashable for use in sets.rz   rS   s    r   r^   zInductiveObservation.__hash__   s    T_dot7HIJJJr   r_   c                     t          |t                    sdS | j        |j        k    o| j        |j        k    o| j        |j        k    S )z1Define equality for InductiveObservation objects.F)r   r~   r9   r&   r'   ra   s     r   rb   zInductiveObservation.__eq__   sQ    %!566 	5Ou// 85#338!U%77	
r   Nr|   r.   r   r   r~   r~      s        ZZ| | | | |GS G G G GF3 F F F FK# K K K K
F 
t 
 
 
 
 
 
r   r~   c                   R    e Zd ZdZdefdZdefdZdefdZdefdZ	de
defdZd	S )
ContradictionObservationz[Contradiction observation - notes when user has made conflicting statements, plus metadata.r   c                     d}| j         r)d | j         D             }dd                    |          z   }dt          | j                   d| j         | S )Nr"   c                     g | ]}d | S r   r.   r   s     r   r   z4ContradictionObservation.__str__.<locals>.<listcomp>   r   r   rh   rQ   ] CONTRADICTION: )r<   rq   r   r&   r2   r   s      r   rU   z ContradictionObservation.__str__   sg    < 	:IIDLIIIL$))L"9"99Ls3DODDssW[Wcseqsssr   c                     | j         rd| j          dnd}d}| j        r)d | j        D             }dd                    |          z   }| dt          | j                   d| j         | S )rW   rX   rR   r"   c                     g | ]}d | S r   r.   r   s     r   r   z8ContradictionObservation.str_with_id.<locals>.<listcomp>   r   r   rh   rQ   r   )r%   r<   rq   r   r&   r2   r   s       r   rZ   z$ContradictionObservation.str_with_id   s    *.'9&47&&&&r	< 	:IIDLIIIL$))L"9"99L~~>tOO~~bfbn~p|~~~r   c                 ~    d}| j         r)d | j         D             }dd                    |          z   }d| j         | S )Nr"   c                     g | ]}d | S r   r.   r   s     r   r   z>ContradictionObservation.str_no_timestamps.<locals>.<listcomp>  r   r   rh   zCONTRADICTION: )r<   rq   r2   r   s      r   rx   z*ContradictionObservation.str_no_timestamps  sQ    < 	:IIDLIIIL$))L"9"99L==|===r   c                 D    t          | j        | j        | j        f          S )z7Make ContradictionObservation hashable for use in sets.r\   rS   s    r   r^   z!ContradictionObservation.__hash__	  s    T\4?D4EFGGGr   r_   c                     t          |t                    sdS | j        |j        k    o| j        |j        k    o| j        |j        k    S )z5Define equality for ContradictionObservation objects.F)r   r   r2   r&   r'   ra   s     r   rb   zContradictionObservation.__eq__  sP    %!9:: 	5LEM) 85#338!U%77	
r   Nr|   r.   r   r   r   r      s        eet t t t tS    >3 > > > >H# H H H H
F 
t 
 
 
 
 
 
r   r   c                      e Zd ZU dZ ede          Zee         ed<    ede          Z	ee
         ed<    ede          Zee         ed<    ed	e          Zee         ed
<   defdZdefdZd!dZ	 d"dd dedz  fdZdefdZdefdZdefdZd#dedefdZedeej                 dd fd            Zedddee         dededd f
d             Z dS )$Representationam  
    A Representation is a traversable and diffable map of observations.
    At the base, we have a list of explicit observations, derived from a peer's messages.

    From there, deductive observations can be made by establishing logical relationships between explicit observations.

    In the future, we can add more levels of reasoning on top of these.

    All of a peer's observations are stored as documents in a collection. These documents can be queried in various ways
    to produce this Representation object.

    Additionally, a "working representation" is a version of this data structure representing the most recent observations
    within a single session.

    A representation can have a maximum number of observations, which is applied individually to each level of reasoning.
    If a maximum is set, observations are added and removed in FIFO order.
    zFacts LITERALLY stated by the user - direct quotes or clear paraphrases only, no interpretation or inference. Example: ['The user is 25 years old', 'The user has a dog']r5   rF   zConclusions that MUST be true given explicit facts and premises - strict logical necessities. Each deduction should have premises and a single conclusion.	deductivezPatterns, generalizations, and personality insights inferred from multiple observations. Higher-level reasoning created by the Dreamer agent.	inductivezxConflicting statements made by the user that need clarification. The dialectic agent should surface these when relevant.contradictionr   c                     t          | j                  dk    oGt          | j                  dk    o/t          | j                  dk    ot          | j                  dk    S )z7
        Check if the representation is empty.
        r   lenrF   r   r   r   rS   s    r   is_emptyzRepresentation.is_empty<  sc    
 !# -DN##q(-DN##q(- D&''1,		
r   c                     t          | j                  t          | j                  z   t          | j                  z   t          | j                  z   S )zP
        Return the total number of observations in the representation.
        r   rS   s    r   r   zRepresentation.lenG  sO    
 $.!!"$.!!" $$%%&	
r   r_   c                      t                      } fd|j        D             |_         fd|j        D             |_         fd|j        D             |_         fd|j        D             |_        |S )z
        Given this and another representation, return a new representation with only observations that are unique to the other.
        Note that this only removes literal duplicates, not semantically equivalent ones.
        c                 &    g | ]}|j         v|S r.   rF   rm   orT   s     r   r   z6Representation.diff_representation.<locals>.<listcomp>X  s%    MMMqat}6L6L6L6L6Lr   c                 &    g | ]}|j         v|S r.   )r   r   s     r   r   z6Representation.diff_representation.<locals>.<listcomp>Y  %    PPP8O8O!8O8O8Or   c                 &    g | ]}|j         v|S r.   )r   r   s     r   r   z6Representation.diff_representation.<locals>.<listcomp>Z  r   r   c                 &    g | ]}|j         v|S r.   )r   r   s     r   r   z6Representation.diff_representation.<locals>.<listcomp>[  s-     
 
 
at7I.I.IA.I.I.Ir   )r   rF   r   r   r   )rT   r_   diffs   `  r   diff_representationz"Representation.diff_representationR  s    
 MMMMENMMMPPPPU_PPPPPPPU_PPP
 
 
 
*
 
 
 r   Nmax_observationsc                    t          t          | j        |j        z                       | _        t          t          | j        |j        z                       | _        t          t          | j        |j        z                       | _        t          t          | j        |j        z                       | _        | j                            d            | j                            d            | j                            d            | j                            d            |rV| j        | d         | _        | j        | d         | _        | j        | d         | _        | j        | d         | _        dS dS )a  
        Merge another representation object into this one.
        This will automatically deduplicate explicit, deductive, inductive, and contradiction observations.
        This *preserves order* of observations so that they retain FIFO order.

        NOTE: observations with the *same* timestamp will not have order preserved.
        That's fine though, because they are from the same timestamp...
        c                     | j         S Nr&   xs    r   <lambda>z5Representation.merge_representation.<locals>.<lambda>q  s     r   )keyc                     | j         S r   r   r   s    r   r   z5Representation.merge_representation.<locals>.<lambda>r      !, r   c                     | j         S r   r   r   s    r   r   z5Representation.merge_representation.<locals>.<lambda>s  r   r   c                     | j         S r   r   r   s    r   r   z5Representation.merge_representation.<locals>.<lambda>t  s    al r   N)r   r   rF   r   r   r   sort)rT   r_   r   s      r   merge_representationz#Representation.merge_representation`  sy    S!?@@AAc$.5?"BCCDDc$.5?"BCCDD!#d&85;N&N"O"OPP55666 6 6777 6 6777$:$:;;; 	H M+;*;*<*<=DM!^-=,=,>,>?DN!^-=,=,>,>?DN!%!35E4E4F4F!GD		H 	Hr   c                 (   g }|                     d           t          | j        d          D ]\  }}|                     | d|             |                     d           |                     d           t          | j        d          D ]\  }}|                     | d|             |                     d           |                     d           t          | j        d          D ]\  }}|                     | d|             |                     d           |                     d           t          | j        d          D ]\  }}|                     | d|             |                     d           d                    |          S )	a  
        Format representation into a clean, readable string for LLM prompts.
        NOTE: we always strip subsecond precision from the timestamps.

        Returns:
            Formatted string with clear sections and bullet points including temporal metadata
            Example:
            EXPLICIT:
            1. [2025-01-01 12:00:00] The user has a dog named Rover
            2. [2025-01-01 12:01:00] The user's dog is 5 years old
            3. [2025-01-01 12:05:00] The user is 25 years old
            DEDUCTIVE:
            1. [2025-01-01 12:01:00] Rover is 5 years old
                - The user has a dog named Rover
                - The user's dog is 5 years old

        
EXPLICIT:
   . r"   DEDUCTIVE:
INDUCTIVE:
CONTRADICTION:
rh   )r   	enumeraterF   r   r   r   rq   rT   partsiobservations       r   rU   zRepresentation.__str__|  s   & ]###'q99 	0 	0NA{LLA....////R^$$$':: 	0 	0NA{LLA....////R^$$$':: 	0 	0NA{LLA....////R'((('(:A>> 	0 	0NA{LLA....////Ryyr   c                    g }|                     d           t          | j        d          D ]1\  }}|                     | d|                                            2|                     d           |                     d           t          | j        d          D ]1\  }}|                     | d|                                            2|                     d           |                     d           t          | j        d          D ]1\  }}|                     | d|                                            2|                     d           |                     d           t          | j        d          D ]1\  }}|                     | d|                                            2|                     d           d                    |          S )	a  
        Format representation with observation IDs for agents that need to reference/delete observations.

        Returns:
            Formatted string with IDs included
            Example:
            EXPLICIT:
            1. [id:abc123] [2025-01-01 12:00:00] The user has a dog named Rover
            2. [id:def456] [2025-01-01 12:01:00] The user's dog is 5 years old
            DEDUCTIVE:
            1. [id:ghi789] [2025-01-01 12:01:00] Rover is 5 years old
                - The user has a dog named Rover
                - The user's dog is 5 years old
            INDUCTIVE:
            1. [id:jkl012] [2025-01-01 12:05:00] [high] User tends to be methodical
                - id:abc123
                - id:def456
        r   r   r   r"   r   r   r   rh   )r   r   rF   rZ   r   r   r   rq   r   s       r   str_with_idszRepresentation.str_with_ids  s   & ]###'q99 	> 	>NA{LLA<<!8!8!:!:<<====R^$$$':: 	> 	>NA{LLA<<!8!8!:!:<<====R^$$$':: 	> 	>NA{LLA<<!8!8!:!:<<====R'((('(:A>> 	> 	>NA{LLA<<!8!8!:!:<<====Ryyr   c                    g }|                     d           t          | j        d          D ]$\  }}|                     | d|j                    %|                     d           |                     d           t          | j        d          D ]1\  }}|                     | d|                                            2|                     d           |                     d           t          | j        d          D ]1\  }}|                     | d|                                            2|                     d           |                     d           t          | j        d          D ]1\  }}|                     | d|                                            2|                     d           d                    |          S )	a  
        Format representation into a clean, readable string for LLM prompts... but without timestamps.

        Returns:
            Formatted string with clear sections and bullet points including temporal metadata
            Example:
            EXPLICIT:
            1. The user has a dog named Rover
            2. The user's dog is 5 years old
            3. The user is 25 years old
            DEDUCTIVE:
            1. Rover is 5 years old
                - The user has a dog named Rover
                - The user's dog is 5 years old
            INDUCTIVE:
            1. [high] User tends to be methodical
                - id:abc123
                - id:def456

        r   r   r   r"   r   r   r   rh   )	r   r   rF   r2   r   rx   r   r   rq   r   s       r   rx   z Representation.str_no_timestamps  s   * ]###'q99 	8 	8NA{LLA66!4667777R^$$$':: 	D 	DNA{LLABB!>!>!@!@BBCCCCR^$$$':: 	D 	DNA{LLABB!>!>!@!@BBCCCCR'((('(:A>> 	D 	DNA{LLABB!>!>!@!@BBCCCCRyyr   Finclude_idsc                    g }| j         rJ|                    d           | j         D ]}|                    |            |                    d           | j        r|                    d           | j        D ]}|r|j        rd|j         dnd}t	          |j                  }|                    | d| d|j                    |j        r7|                    d           |j        D ]}|                    d|            |                    d           |                    d           | j        r8|                    d	           | j        D ]}|r|j        rd|j         dnd}|                    | d
|j	         d|j                    |j
        r|                    d|j
                    |j        r|                    d           |j        dd         D ]}|                    d|            t          |j                  dk    r.|                    dt          |j                  dz
   d           |                    d           |                    d           | j        r|                    d           | j        D ]}|r|j        rd|j         dnd}|                    | d|j                    |j        r7|                    d           |j        D ]}|                    d|            |                    d           |                    d           d                    |          S )a&  
        Format a Representation object as markdown.
        NOTE: we always strip subsecond precision from the timestamps.

        Args:
            include_ids: If True, include observation IDs for use with get_reasoning_chain

        Returns:
            Formatted markdown string
        z## Explicit Observations
r"   z## Deductive Observations
rX   rR   rQ   z   Premises:z   - z## Inductive Observations
z **Pattern** [z]: z   **Type**: z   **Sources**:N   z   - ... and z morez## Contradictions
z **CONTRADICTION**: z   **Conflicting statements**:rh   )rF   r   r   r%   r   r&   r9   r8   r   r@   r>   r<   r   r   r2   rq   )rT   r   r   obsrY   r   rn   r   s           r   format_as_markdownz!Representation.format_as_markdown  s     = 	LL5666} ' ' X&&&&LL > 	LL6777~ ! !1<OO-36----R	<S^LL		IIIIIIIJJJ< 8LL000#&< 8 8%6W%6%67777R    LL > 	LL6777~ ! !1<OO-36----R	 SSSS3>SS   # ELL!C1A!C!CDDD; RLL!2333"%+bqb/ 7 7%5V%5%566663;''!++%PS5E5E5I%P%P%PQQQR    LL  
	LL.///) ! !1<OO-36----R		LLs{LLMMM; 7LL!ABBB"%+ 7 7%5V%5%56666R    LLyyr   	documentsc                 p     | d |D             d |D             d |D             d |D                       S )Nc                     g | ]n}|j         d k    t          |j        t          |j        |j                  |j        t          |j                            dg                     |j	                  oS )rF   r   )r%   r&   r2   r   r'   )
levelrO   r%   _safe_datetime_from_metadatainternal_metadatar&   r2   r   getr'   rm   docs     r   r   z1Representation.from_documents.<locals>.<listcomp>I  s        9
** $v;-s~     K 3-11-DD! ! "%!1
 
 
 +**r   c                 \   g | ]}|j         d k    t          |j        t          |j        |j                  |j        t          |j                            dg                     |j	        |j
        p|j                            dg           |j                            dg                     S )r   r   premise_idsr8   )r%   r&   r9   r   r'   r7   r8   )r   rf   r%   r   r   r&   r2   r   r   r'   r7   r   s     r   r   z1Representation.from_documents.<locals>.<listcomp>X  s         9++! %v;-s~     #{ 3-11-DD! ! "%!1"~  D,00CC 266z2FF    ,++r   c                    g | ]}|j         d k    t          |j        t          |j        |j                  |j        |j                            dg           |j        |j	        p|j                            dg           |j                            dg           |j                            dd          |j                            dd          	  	        S )	r   r   r7   r<   r>   r=   r@   r?   )	r%   r&   r9   r   r'   r7   r<   r>   r@   )
r   r~   r%   r   r   r&   r2   r   r'   r7   r   s     r   r   z1Representation.from_documents.<locals>.<listcomp>k  s         9++! %v;-s~     #{ # 5 9 9- L L!$!1"~  C,00rBB155iDD!$!6!:!:>9!U!U"488xPP    ,++r   c                 B   g | ]}|j         d k    t          |j        t          |j        |j                  |j        |j                            dg           |j        |j	        p|j                            dg           |j                            dg                     S )r   r   r7   r<   )r%   r&   r2   r   r'   r7   r<   )
r   r   r%   r   r   r&   r2   r   r'   r7   r   s     r   r   z1Representation.from_documents.<locals>.<listcomp>~  s        9// )v;-s~     K # 5 9 9- L L!$!1"~  C,00rBB155iDD   0//r   )rF   r   r   r   r.   )rK   r   s     r   from_documentszRepresentation.from_documentsF  s    s  %     %!  &   %!  &  %  mG
 G
 G
 G	
r   prompt_representationrE   r   r'   r&   c                 J     | fd|j         D             g g           S )z/Convert PromptRepresentation to Representation.c                 @    g | ]}t          |j                   S ))r2   r&   r   r'   )rO   r2   )rm   er&   r   r'   s     r   r   z=Representation.from_prompt_representation.<locals>.<listcomp>  sH         $I) +!-	    r   )rF   r   r   r   )rK   r   r   r'   r&   s     ```r   from_prompt_representationz)Representation.from_prompt_representation  s]     s      /7   
 
 
 	
r   )r_   r   r   r   r   )F)!r(   r)   r*   rA   r   r   rF   rO   r,   r   rf   r   r~   r   r   rd   r   r-   r   r   r   r+   rU   r   rx   r   rM   r   r	   Documentr   r   r   r.   r   r   r   r     s         $ +0% @+ + +Hd&'    -2E q- - -It()    -2E d- - -It()    5:E O5 5 5M401   
	
$ 	
 	
 	
 	
	
S 	
 	
 	
 	
    GKH H%H9<tH H H H8)  )  )  )  ) V) c )  )  )  ) V+ 3 +  +  +  + ZE  E d E s E  E  E  E N H
x'@ H
EU H
 H
 H
 [H
T 
5
 #Y
 	

 
 

 
 
 [
 
 
r   r   r   fallback_datetimec                 V   |                      d          }|t          |          S t          |t                    r<	 t          t	          |                    S # t
          $ r t          |          cY S w xY wt          |t                    rt          |          S t          |          S )Nmessage_created_at)r   r   r   r+   r
   
ValueErrorr   )r   r   r   s      r   r   r     s     +../CDD!/0ABBB$c** G	G3"#566    	G 	G 	G34EFFFFF	G $h// D/0BCCC+,=>>>s   A A54A5N) collections.abcr   r   typingr   pydanticr   r   r   srcr	   src.utils.formattingr
   r   r   r-   r   r   r!   r0   r4   r;   rC   rE   rO   rf   r~   r   r   dictr+   r   r.   r   r   <module>r      s+   $ $ $ $ $ $             6 6 6 6 6 6 6 6 6 6       3 3 3 3 3 39 9X 9 9 9 9cT$s)_,tE#s(O/DD	#Y   D$ $ $ $ $) $ $ $A A A A Ai A A A	D 	D 	D 	D 	Dy 	D 	D 	D    y   ,I I I I I9 I I I    9   &
 
 
 
 
13F 
 
 
>"
 "
 "
 "
 "
35H "
 "
 "
J&
 &
 &
 &
 &
35H &
 &
 &
R&
 &
 &
 &
 &
;=P &
 &
 &
RN
 N
 N
 N
 N
Y N
 N
 N
b?CH~?:B?? ? ? ? ? ?r   