
    $j%@                     $   d Z ddlmZ ddlmZmZmZmZ  eg 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          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          Z G d! d"e          Z G d# d$e          Z G d% d&e          Zd'S )(a  
Core response models for Hindsight memory system.

These models define the structure of data returned by the core MemoryEngine class.
API response models should be kept separate and convert from these core models to maintain
API stability even if internal models change.
    )Any)	BaseModel
ConfigDictFieldfield_validator)world
experienceobservationc                       e Zd ZU dZ ed          Zeed<    ed          Zeed<    ed          Z	e
eef         ed<    ed	d
          Zed	z  ed<   d	S )LLMToolCallz!A tool call requested by the LLM.z$Unique identifier for this tool calldescriptionidzName of the tool to callnamezArguments to pass to the tool	argumentsNz]Opaque token required by Gemini 3.1+ thinking models to preserve thought context across turnsdefaultr   thought_signature)__name__
__module____qualname____doc__r   r   str__annotations__r   r   dictr   r        l/home/rurouni/.hermes/hermes-agent/venv/lib/python3.11/site-packages/hindsight_api/engine/response_models.pyr   r      s         ++e FGGGBGGG"<===D#=== %2Q R R RItCH~RRR$)Es% % %sTz     r   r   c                       e Zd ZU dZ edd          Zedz  ed<    eed          Z	ee
         ed<    edd	          Zedz  ed
<    edd          Zeed<    edd          Zeed<   dS )LLMToolCallResultz4Result from an LLM call that may include tool calls.NzText content if anyr   contentzTool calls requested by the LLMdefault_factoryr   
tool_callsz2Reason the LLM stopped: 'stop', 'tool_calls', etc.finish_reasonr   zInput tokens used in this callinput_tokenszOutput tokens used in this calloutput_tokens)r   r   r   r   r   r!   r   r   listr$   r   r%   r&   intr'   r   r   r   r    r       s         >>%:OPPPGS4ZPPP$)E$Lm$n$n$nJ[!nnn %d@t u u uM3:uuua5UVVVL#VVVq6WXXXM3XXXXXr   r    c                       e Zd ZU dZ ed          Zeed<    edd          Zedz  ed<    ed	          Z	e
ed
<    ed          Ze
ed<    ed          Zeed<    edd          Zeed<   dS )ToolCallTracez'A single tool call made during reflect.z(Tool name: lookup, recall, learn, expandr   toolNz+Agent's reasoning for making this tool callr   reasonzTool input parametersinputzTool output/resultoutputExecution time in millisecondsduration_msr   z4Iteration number (1-based) when this tool was called	iteration)r   r   r   r   r   r,   r   r   r-   r.   r   r/   r1   r)   r2   r   r   r   r+   r+   &   s         11"LMMMD#MMMt9fgggFC$Jggg%$;<<<E4<<<5%9:::FD:::u)IJJJKJJJU12hiiiIsiiiiir   r+   c                   X    e Zd ZU dZ ed          Zeed<    ed          Ze	ed<   dS )LLMCallTracez&A single LLM call made during reflect.z)Call scope: agent_1, agent_2, final, etc.r   scoper0   r1   N)
r   r   r   r   r   r5   r   r   r1   r)   r   r   r   r4   r4   1   sT         00#NOOOE3OOOu)IJJJKJJJJJr   r4   c                       e Zd ZU dZ ed          Zeed<    ed          Zeed<    ed          Z	eed<    ed	          Z
eed
<    ed          Zeed<    edd          Zedz  ed<   dS )ObservationRefz4Reference to an observation accessed during reflect.zObservation IDr   r   zObservation namer   z(Observation type: entity, concept, eventtypez2Observation subtype: structural, emergent, learnedsubtypezBrief descriptionr   Nz'Full summary (when looked up in detail)r   summary)r   r   r   r   r   r   r   r   r   r8   r9   r   r:   r   r   r   r7   r7   8   s         >>e 0111B111"4555D#555"LMMMD#MMM5%YZZZGSZZZu)<===K===%:cdddGS4Zdddddr   r7   c                   z    e Zd ZU dZ ed          Zeed<    ed          Zeed<    ed          Z	eed<   d	S )
DirectiveRefz9Reference to a directive that was applied during reflect.zDirective mental model IDr   r   zDirective namer   zDirective contentr!   N)
r   r   r   r   r   r   r   r   r   r!   r   r   r   r<   r<   C   sr         CCe ;<<<B<<<"2333D#3335%8999GS99999r   r<   c                       e Zd ZU dZ edddddi          Z edd	
          Zee	d<    edd
          Z
ee	d<    edd
          Zee	d<   ddZdS )
TokenUsagez
    Token usage metrics for LLM calls.

    Tracks input/output tokens for a single request to enable
    per-request cost tracking and monitoring.
    example      r&   r'   total_tokensjson_schema_extrar   z&Number of input/prompt tokens consumedr   r&   z,Number of output/completion tokens generatedr'   zTotal tokens (input + output)rD   otherreturnc                 t    t          | j        |j        z   | j        |j        z   | j        |j        z             S )z2Allow aggregating token usage from multiple calls.rC   )r>   r&   r'   rD   )selfrG   s     r   __add__zTokenUsage.__add__a   sC    *U-??,u/BB*U-??
 
 
 	
r   N)rG   r>   rH   r>   )r   r   r   r   r   model_configr   r&   r)   r   r'   rD   rK   r   r   r   r>   r>   K   s           : $!$ $ 
  L a5]^^^L#^^^q6deeeM3eeea5TUUUL#UUU
 
 
 
 
 
r   r>   c                       e Zd ZU dZ eddd          Zeed<    eddd          Zeed<    eddd	          Z	eed
<    e
dddddi          ZdS )DispositionTraitsaj  
    Disposition traits for a memory bank.

    All traits are scored 1-5 where:
    - skepticism: 1=trusting, 5=skeptical (how much to doubt or question information)
    - literalism: 1=flexible interpretation, 5=literal interpretation (how strictly to interpret information)
    - empathy: 1=detached, 5=empathetic (how much to consider emotional context)
          z3How skeptical vs trusting (1=trusting, 5=skeptical))geler   
skepticismz>How literally to interpret information (1=flexible, 5=literal)
literalismzAHow much to consider emotional context (1=detached, 5=empathetic)empathyr?      )rS   rT   rU   rE   N)r   r   r   r   r   rS   r)   r   rT   rU   r   rL   r   r   r   rN   rN   j   s           eqQ4ijjjJjjjeqQ4tuuuJuuu5A!1tuuuGSuuu:1\]jk<l<l0mnnnLLLr   rN   c                      e Zd ZU dZ eddddddgdd	d	d	d
ddiddddgdi          Z ed          Zee	d<    ed          Z
ee	d<    ed          Zee	d<    edd          Zee         dz  e	d<    edd          Zedz  e	d<    edd          Zedz  e	d <    edd!          Zedz  e	d"<    edd#          Zedz  e	d$<    edd%          Zedz  e	d&<    edd'          Zeeef         dz  e	d(<    ed(d)*          ed+ed,eeef         dz  fd-                        Z edd.          Zedz  e	d/<    edd0          Zee         dz  e	d1<    edd2          Zee         dz  e	d3<   dS )4
MemoryFactz
    A single memory fact returned by search or think operations.

    This represents a unit of information stored in the memory system,
    including both the content and metadata.
    r?   $123e4567-e89b-12d3-a456-426614174000$Alice works at Google on the AI teamr   AliceGoogle	work info2024-01-15T10:30:00Zsession_abc123sourceslackbank123_session_abc123_0ffffff?user_asession_123)r   text	fact_typeentitiescontextoccurred_startoccurred_endmentioned_atdocument_idmetadatachunk_id
activationtagsrE   z%Unique identifier for the memory factr   r   z%The actual text content of the memoryrf   z@Type of fact: 'world', 'experience', 'opinion', or 'observation'rg   Nz#Entity names mentioned in this factrh   z!Additional context for the memoryri   z0ISO format date when the event started occurringrj   z.ISO format date when the event ended occurringrk   z3ISO format date when the fact was mentioned/learnedrl   z)ID of the document this memory belongs torm   zUser-defined metadatarn   before)modevrH   c                 j    |dS t          |t                    rddl}|                    |          S |S )zLParse metadata from JSON string if needed (asyncpg may return JSONB as str).Nr   )
isinstancer   jsonloads)clsrt   rw   s      r   parse_metadatazMemoryFact.parse_metadata   s>     94a 	!KKK::a== r   zVID of the chunk this fact was extracted from (format: bank_id_document_id_chunk_index)ro   z/Visibility scope tags associated with this factrq   zkIDs of source facts this observation was derived from (observation type only, when source_facts is enabled)source_fact_ids)r   r   r   r   r   rL   r   r   r   r   rf   rg   rh   r(   ri   rj   rk   rl   rm   rn   r   r   classmethodr   rz   ro   rq   r{   r   r   r   rX   rX   {   s          :<>$$h/&"8 6 6/%w/6"!=1 
  L( e GHHHBHHH"IJJJD#JJJU'ijjjIsjjj!&t9^!_!_!_Hd3i$___%2UVVVGS4ZVVV!&t9k!l!l!lNC$Jlll$uT7ghhhL#*hhh$uT7lmmmL#*mmm#eD6abbbKtbbb&+eD>U&V&V&VHd38nt#VVV_Zh///s tCH~'<    [ 0/ !5r  HcDj    #U45fgggD$s)d
ggg(- B) ) )OT#Y%     r   rX   c                   |    e Zd ZU dZ ed          Zeed<    ed          Ze	ed<    edd	          Z
eed
<   dS )	ChunkInfozInformation about a chunk.zThe raw chunk textr   
chunk_textz&Index of the chunk within the documentchunk_indexFz3Whether the chunk was truncated due to token limitsr   	truncatedN)r   r   r   r   r   r   r   r   r   r)   r   boolr   r   r   r~   r~      st         $$e(<===J===u)QRRRKRRReE7lmmmItmmmmmr   r~   c                      e Zd ZU dZ ed          Zeed<    ed          Zeed<    ed          Z	e
ed<    ed	d
          Zeed<    edd          Zee         dz  ed<    eed          Zee         ed<   dS )ObservationResultzRAn observation result from recall (consolidated knowledge synthesized from facts).zUnique observation IDr   r   The observation textrf   z+Number of facts supporting this observationproof_count        Relevance score to the queryr   	relevanceNzTags for visibility scopingrq   z0IDs of facts that contribute to this observationr"   source_memory_ids)r   r   r   r   r   r   r   r   rf   r   r)   r   floatrq   r(   r   r   r   r   r   r      s         \\e 7888B888"8999D#999u)VWWWKWWWuS6TUUUIuUUU"U4=Z[[[D$s)d
[[[#(5*\$ $ $tCy     r   r   c                       e Zd ZU dZ ed          Zeed<    ed          Zeed<    ed          Z	eed<    ed	d
          Z
eed<   dS )MentalModelResultz<A mental model result from recall (stored reflect response).zUnique mental model IDr   r   zHuman-readable namer   zThe synthesized contentr!   r   r   r   r   N)r   r   r   r   r   r   r   r   r   r!   r   r   r   r   r   r   r      s         FFe 8999B999"7888D#8885%>???GS???uS6TUUUIuUUUUUr   r   c                   d   e Zd ZU dZ eddddddddd	gd
dddi          Z ed          Zee	         e
d<    edd          Zeeef         dz  e
d<    edd          Zeedf         dz  e
d<    edd          Zeeef         dz  e
d<    edd          Zeee	f         dz  e
d<   dS )RecallResultz
    Result from a recall operation.

    Contains a list of matching memory facts and optional trace information
    for debugging and transparency.
    r?   rY   rZ   r   r]   r^   rc   )r   rf   rg   ri   rj   rk   rp   z*What did Alice say about machine learning?rO   )querynum_results)resultstracerE   z'List of memory facts matching the queryr   r   NzTrace information for debuggingr   zIEntity states for entities mentioned in results (keyed by canonical name)EntityStaterh   z8Chunks for facts, keyed by '{document_id}_{chunk_index}'chunksz;Source facts for observation-type results, keyed by fact IDsource_facts)r   r   r   r   r   rL   r   r   r(   rX   r   r   r   r   r   rh   r   r~   r   r   r   r   r   r      s          : E F%,#.*@(>&* 
 $P`abb 
  L& !&2[ \ \ \GT*\\\#(5;\#]#]#]E4S>D ]]]05e1 1 1Hd3%&-    +0%T+ + +FDi 4'    27W2 2 2L$sJ'$.     r   r   c                      e Zd ZU dZ eddddddddd	gg g g d
ddgdgdddddddddi          Z ed          Zee	d<    ed          Z
eeef         e	d<    edd          Zeeef         dz  e	d <    edd!          Zedz  e	d"<    eed#$          Zee         e	d%<    eed&$          Zee         e	d'<    eed($          Zee         e	d)<   dS )*ReflectResulta  
    Result from a reflect operation.

    Contains the formulated answer, the facts it was based on (organized by type),
    any new opinions that were formed during the reflection process, and optionally
    structured output if a response schema was provided.
    r?   zOBased on my knowledge, machine learning is being actively used in healthcare...rY   z-Machine learning is used in medical diagnosisr   
healthcarer^   )r   rf   rg   ri   rj   rk   zdirective-123zResponse StylezAlways be concise)r   r   rules)r   r	   opinionmental_models
directiveszML in healthcareg?)r:   
confidencer@   rA   rB   rC   )rf   based_onstructured_outputusagerE   zThe formulated answer textr   rf   zdFacts used to formulate the answer, organized by type (world, experience, mental_models, directives)r   NzsStructured output parsed according to the provided response schema. Only present when response_schema was provided.r   r   zIToken usage metrics for the LLM calls made during this reflect operation.r   z\Trace of tool calls made during reflection. Only present when include.tool_calls is enabled.r"   
tool_tracez[Trace of LLM calls made during reflection. Only present when include.tool_calls is enabled.	llm_tracezADirective mental models that were applied during this reflection.directives_applied)r   r   r   r   r   rL   r   rf   r   r   r   r   r   r   r   r>   r(   r   r+   r   r4   r   r<   r   r   r   r   r      s$          :i #I$S)0'3.D,B 	 #%!%' #2$4&9%: # , 2DSV%W%W*.VZ[[3 
  L@ ">???D#???$uz     Hd38n    05u J0 0 0tCH~,     %u_     E:    ',er' ' 'J]#    %*Eq% % %ItL!    .3UW. . .\*     r   r   c                       e Zd ZU dZ eddddi          Z ed          Zee	d	<    ed
d          Z
ed
z  e	d<   d
S )EntityObservationz
    An observation about an entity.

    Observations are objective facts synthesized from multiple memory facts
    about an entity, without personality influence.
    r?   z+John is detail-oriented and works at Googler^   rf   rl   rE   r   r   rf   Nz1ISO format date when this observation was createdrl   )r   r   r   r   r   rL   r   rf   r   r   rl   r   r   r   r   r   A  s           : M_uvv
  L "8999D#999$uT7jkkkL#*kkkkkr   r   c            	           e Zd ZU dZ edddddddd	dgd
i          Z ed          Zee	d<    ed          Z
ee	d<    eed          Zee         e	d<   dS )r   zp
    Current mental model of an entity.

    Contains observations synthesized from facts about the entity.
    r?   rY   JohnzJohn is detail-orientedr^   r   z#John works at Google on the AI teamz2024-01-14T09:00:00Z)	entity_idcanonical_nameobservationsrE   z Unique identifier for the entityr   r   zCanonical name of the entityr   z&List of observations about this entityr"   r   N)r   r   r   r   r   rL   r   r   r   r   r   r(   r   r   r   r   r   r   r   S  s           :C"(6H^__BTjkk! 	
  L U'IJJJIsJJJ%,JKKKNCKKK,1E*R- - -L$()     r   r   c            
          e Zd ZU dZ edddddddd	i
          Z ed          Zee	d<    ed          Z
ee	d<    ed          Zee	d<    edd          Zedz  e	d<    edd          Zedz  e	d<    ed          Zee	d<   dS )MentalModelz
    A manually configured mental model for tracking specific topics/areas.

    Mental models are user-defined focus areas that the agent should track
    and maintain summaries for, unlike auto-extracted entities.
    r?   zteam-dynamicszTeam DynamicszSTrack how the team collaborates, communication patterns, conflicts, and resolutionsz$The team has strong collaboration...r^   z2024-01-10T08:00:00Z)r   r   r   r:   summary_updated_at
created_atrE   z*Unique identifier (alphanumeric lowercase)r   r   z!Display name for the mental modelr   z1Prompt/directions for what to track and summarizer   Nz)Generated summary based on relevant factsr:   z-ISO format date when summary was last updatedr   z1ISO format date when the mental model was createdr   )r   r   r   r   r   rL   r   r   r   r   r   r   r:   r   r   r   r   r   r   r   n  s          :%'tA&<4 	
  L e LMMMBMMM"EFFFD#FFFu)\]]]K]]]%2]^^^GS4Z^^^%*U4=l%m%m%md
mmme([\\\J\\\\\r   r   N)r   typingr   pydanticr   r   r   r   	frozensetVALID_RECALL_FACT_TYPESr   r    r+   r4   r7   r<   r>   rN   rX   r~   r   r   r   r   r   r   r   r   r   r   <module>r      s          B B B B B B B B B B B B#)$J$J$JKK 	 	 	 	 	) 	 	 	Y Y Y Y Y	 Y Y Yj j j j jI j j jK K K K K9 K K Ke e e e eY e e e: : : : :9 : : :
 
 
 
 
 
 
 
>o o o o o	 o o o": : : : : : : :zn n n n n	 n n n
 
 
 
 
	 
 
 
V V V V V	 V V V% % % % %9 % % %P@ @ @ @ @I @ @ @Fl l l l l	 l l l$    )   6] ] ] ] ]) ] ] ] ] ]r   