
    #j0                        d Z ddlmZ ddlmZ ddlmZ e G d d                      Zddlm	Z	 ddl
mZmZmZmZmZmZmZ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mZmZmZ ddlm Z   G d de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 ) z*
SQLAlchemy models for the memory system.
    )	dataclass)datetime)UUIDc                       e Zd ZU dZdZedz  ed<   dZedz  ed<   dZedz  ed<   dZ	e
ed<   dZe
ed<   dZe
ed	<   dZee         dz  ed
<   dZeed<   dS )RequestContexta  
    Context for request authentication and authorization.

    This dataclass carries authentication data from HTTP requests to the
    memory engine operations. It can be extended to include additional
    context like headers, tokens, user info, etc.
    Napi_key
api_key_id	tenant_idFinternalmcp_authenticateduser_initiatedallowed_bank_idsr   retry_count)__name__
__module____qualname____doc__r   str__annotations__r	   r
   r   boolr   r   r   listr   int     \/home/rurouni/.hermes/hermes-agent/venv/lib/python3.11/site-packages/hindsight_api/models.pyr   r   
   s           GS4Z!Jd
!!! IsTz   Hd#t### ND   )-d3i$&--- Kr   r   )Vector)CheckConstraintFloat
ForeignKeyForeignKeyConstraintIndexIntegerTextfunc)text)JSONB	TIMESTAMPr   )
AsyncAttrs)DeclarativeBaseMappedmapped_columnrelationship   )EMBEDDING_DIMENSIONc                       e Zd ZdZdS )BasezBase class for all models.N)r   r   r   r   r   r   r   r0   r0   8   s        $$Dr   r0   c                      e Zd ZU dZdZ eed          Zee	         e
d<    eed          Zee	         e
d<    ee          Zee	dz           e
d<    ee          Zee	dz           e
d	<    e ed
           ej                              Zee         e
d<    e ed
           ej                              Zee         e
d<    eddd          Z edd           edd	          fZdS )Documentz"Source documents for memory units.	documentsTprimary_keyidbank_idNoriginal_textcontent_hashtimezoneserver_default
created_at
updated_at
MemoryUnitdocumentall, delete-orphanback_populatescascadeidx_documents_bank_ididx_documents_content_hash)r   r   r   r   __tablename__r+   r#   r6   r*   r   r   r7   r8   r9   r'   r$   nowr>   r   r?   r,   memory_unitsr!   __table_args__r   r   r   r2   r2   >   se        ,,M#mDd;;;Bs;;;(=4@@@GVC[@@@(5d(;(;M6#*%;;;'4}T':':L&t$:::#0=D1I1I1IZbZ^ZbZdZd#e#e#eJx eee#0=D1I1I1IZbZ^ZbZdZd#e#e#eJx eee  <ZQefffL 	%y11*N;;NNNr   r2   c                   `   e Zd ZU dZdZ e ed          d ed                    Ze	e
         ed<    eed	          Ze	e         ed
<    ee          Ze	edz           ed<    eed	          Ze	e         ed<    e ee                    Z ee          Ze	edz           ed<    e ed          d	          Ze	e         ed<    e ed                    Ze	edz           ed<    e ed                    Ze	edz           ed<    e ed                    Ze	edz           ed<    eedd          Ze	e         ed<    ede ed                    Ze	e         ed<    e ed           ej                               Z!e	e         ed<    e ed           ej                               Z"e	e         ed<    e#dd          Z$ e#dd d!"          Z% e#d#d$d%d!&          Z& e#d#d'd(d!&          Z' e(dd
gd)d*gd+d,-           e)d.           e*d/d
           e*d0d           e*d1ddd2i3           e*d4d
ddd2i3           e*d5d           e*d6d
d           e*d7d
dddd2i3           e*d8d
d ed9          dd2i:           e*d;d<d=d<d>i?          fZ+dS )@r@   z#Individual sentence-level memories.rJ   Tas_uuidgen_random_uuid()r5   r=   r6   Fnullabler7   Ndocument_idr%   contextr:   
event_dateoccurred_startoccurred_endmentioned_atworldrR   r=   	fact_typemetadata'{}'::jsonbr<   unit_metadatar>   r?   r2   rD   
UnitEntitymemory_unitrB   rC   
MemoryLinkzMemoryLink.from_unit_id	from_unitforeign_keysrD   rE   zMemoryLink.to_unit_idto_unitzdocuments.idzdocuments.bank_idmemory_units_document_fkeyCASCADE)nameondeletez3fact_type IN ('world', 'experience', 'observation')idx_memory_units_bank_ididx_memory_units_document_ididx_memory_units_event_dateDESCpostgresql_opsidx_memory_units_bank_dateidx_memory_units_fact_typeidx_memory_units_bank_fact_typeidx_memory_units_bank_type_date!idx_memory_units_observation_datezfact_type = 'observation'postgresql_whererp   idx_memory_units_embedding	embeddinghnswvector_cosine_ops)postgresql_usingrp   ),r   r   r   r   rH   r+   r   sql_textr6   r*   PyUUIDr   r#   r7   r   rS   r%   r   r.   ry   rT   r'   rU   r   rV   rW   rX   r[   r&   r^   dictr$   rI   r>   r?   r,   rA   unit_entitiesoutgoing_linksincoming_linksr    r   r!   rK   r   r   r   r@   r@   S   s        --"M&TXXFY=Z=Z  Bv    )=>>>GVC[>>>&3mD&9&9Kd
#999%dU;;;D&+;;;ff%899::I"/-"5"5GVC$J555#0=	4   5$ $ $Jx     /<m	4   / /NF8d?+    -:M))T:R:R:R,S,SL&D)SSS,9M))T:R:R:R,S,SL&D)SSS*]4%PWXXXIvc{XXX"/-E((=*A*A# # #M6$<    $1=D1I1I1IZbZ^ZbZdZd#e#e#eJx eee#0=D1I1I1IZbZ^ZbZdZd#e#e#eJx eee |J~FFFH LmUijjjM!\#<[bv  N "\#:9^r  N
 	I&01-		
 	
 	
 	MNN()44,m<<+\<Y_J`aaa*I|UaciTjkkk*K88/KHH-(&1	
 	
 	
 	/%X&ABB(&1	
 	
 	
 	(#')<=		
 	
 	
9"NNNr   r@   c                      e Zd ZU dZdZ e ed          d ed                    Ze	e
         ed<    eed	          Ze	e         ed
<    eed	          Ze	e         ed<    ede ed                    Ze	e         ed<    e ed           ej                              Ze	e         ed<    e ed           ej                              Ze	e         ed<    eed          Ze	e         ed<    eddd          Z eddd          Z edddd          Z edddd          Z  e!d d           e!d!d
           e!d"dd
          fZ"d#S )$Entityz;Resolved entities (people, organizations, locations, etc.).entitiesTrM   rO   rP   r6   FrQ   canonical_namer7   r\   r]   r<   entity_metadatar:   
first_seen	last_seen1mention_countr`   entityrB   rC   rb   EntityCooccurrencezEntityCooccurrence.entity_id_1entity_1rd   zEntityCooccurrence.entity_id_2entity_2idx_entities_bank_ididx_entities_canonical_nameidx_entities_bank_nameN)#r   r   r   r   rH   r+   r   r}   r6   r*   r~   r   r#   r   r   r7   r&   r   r   r'   r$   rI   r   r   r   r"   r   r   r,   r   memory_linkscooccurrences_1cooccurrences_2r!   rK   r   r   r   r   r      s1        EEM&TXXFY=Z=Z  Bv    #0-u"E"E"ENF3KEEE(=>>>GVC[>>>$1M*eT\T\]jTkTk$l$l$lOVD\lll#0=D1I1I1IZbZ^ZbZdZd#e#e#eJx eee"/-		40H0H0HYaY]YaYcYc"d"d"dIvhddd!.ws!K!K!KM6#;KKK !LhPdeeeM<XOcdddL"l5!$	  O #l5!$	  O 	$i00+-=>>&	3CDDNNNr   r   c                   4   e Zd ZU dZdZ e ed           edd          d          Ze	e
         ed	<    e ed           ed
d          d          Ze	e
         ed<    edd          Z edd          Z edd	           edd          fZdS )r`   z.Association between memory units and entities.r   TrM   memory_units.idrh   rj   r4   unit_identities.id	entity_idr@   r_   r   idx_unit_entities_unitidx_unit_entities_entityN)r   r   r   r   rH   r+   r   r   r   r*   r~   r   r   r,   ra   r   r!   rK   r   r   r   r`   r`      s        88#M+mTJJ'89MMM[_  GVF^    !.TJJ}yIIIW[! ! !Ivf~   
 ,|OLLLK\(?CCCF 	&	22(+66NNNr   r`   c            	           e Zd ZU dZdZ e ed           edd          d          Ze	e
         ed	<    e ed           edd          d          Ze	e
         ed
<    eed          Ze	e         ed<    e ed           ej                              Ze	e         ed<    edegd          Z edegd          Z edd           edd	           edd
           eddddi          fZdS )r   z,Materialized cache of entity co-occurrences.entity_cooccurrencesTrM   r   rh   r   r4   entity_id_1entity_id_2r   r<   cooccurrence_countr:   last_cooccurredr   r   re   rD   r   zentity_id_1 < entity_id_2entity_cooccurrence_order_checkri    idx_entity_cooccurrences_entity1 idx_entity_cooccurrences_entity2idx_entity_cooccurrences_countrn   ro   N)r   r   r   r   rH   r+   r   r   r   r*   r~   r   r   r"   r   r   r'   r$   rI   r   r   r,   r   r   r   r!   rK   r   r   r   r   r      s        66*M"/-TJJ}yIIIW[# # #K    #0-TJJ}yIIIW[# # #K    '4mGC&P&P&PsPPP(5ii6N6N6N_g_c_g_i_i(j(j(jOVH%jjj |HK=QbcccH|HK=QbcccH 	3:[\\\0-@@0-@@.0DVjlrUsttt	NNNr   r   c                      e Zd ZU dZdZ e ed           edd          d          Ze	e
         ed	<    e ed           edd          d          Ze	e
         ed
<    eed          Ze	e         ed<    e ed           edd          d          Ze	e
dz           ed<    eedd          Ze	e         ed<    e ed           ej                              Ze	e         ed<    edegd          Z edegd          Z edd          Z edd           edd            ed!d	           ed"d
           ed#d           ed$d ed%          &           ed'd	d ed(          dd)i*          fZ dS )+rb   z8Links between memory units (temporal, semantic, entity).r   TrM   r   rh   r   r4   from_unit_id
to_unit_id	link_typer   Nr   Fz1.0rZ   weightr:   r<   r>   r@   r   r   r   r   r_   z]link_type IN ('temporal', 'semantic', 'entity', 'causes', 'caused_by', 'enables', 'prevents')memory_links_link_type_checkr   zweight >= 0.0 AND weight <= 1.0memory_links_weight_checkidx_memory_links_fromidx_memory_links_toidx_memory_links_typeidx_memory_links_entityzentity_id IS NOT NULL)rw   idx_memory_links_from_weightzweight >= 0.1rn   rv   )!r   r   r   r   rH   r+   r   r   r   r*   r~   r   r   r#   r   r   r   r   r   floatr'   r$   rI   r>   r   r,   rc   rf   r   r   r!   r}   rK   r   r   r   rb   rb      s        BB"M#0=TJJ'89MMM[_$ $ $L&.    "/TJJ'89MMM[_" " "Jv    +]4TBBBIvc{BBB'4}TJJ}yIIIW[( ( (Ivftm$    *M%%PUVVVFF5MVVV#0=D1I1I1IZbZ^ZbZdZd#e#e#eJx eee \WghhhIl<zlScdddG\(>BBBF 	k/	
 	
 	
 	9@[\\\%~66#\22%{33'xxPgGhGhiii*%Xo66$f-	
 	
 	
NNNr   rb   c                      e Zd ZU dZdZ eed          Zee	         e
d<    eed ed                    Zee         e
d	<    eedd
          Zee	         e
d<    e ed           ej                              Zee         e
d<    e ed           ej                              Zee         e
d<    edd          fZdS )Bankz<Memory bank profiles with disposition traits and background.banksTr4   r7   Fz9'{"skepticism": 3, "literalism": 3, "empathy": 3}'::jsonbrZ   disposition 
backgroundr:   r<   r>   r?   idx_banks_bank_idN)r   r   r   r   rH   r+   r#   r7   r*   r   r   r&   r}   r   r   r   r'   r$   rI   r>   r   r?   r!   rK   r   r   r   r   r     s4        FFM(=4@@@GVC[@@@ -hh7t.u.u! ! !K    ,mD5QSTTTJsTTT#0=D1I1I1IZbZ^ZbZdZd#e#e#eJx eee#0=D1I1I1IZbZ^ZbZdZd#e#e#eJx eeee/;;=NNNr   r   N))r   dataclassesr   r   uuidr   r~   r   pgvector.sqlalchemyr   
sqlalchemyr   r   r   r    r!   r"   r#   r$   r%   r}   sqlalchemy.dialects.postgresqlr&   r'   sqlalchemy.ext.asyncior(   sqlalchemy.ormr)   r*   r+   r,   configr.   r0   r2   r@   r   r`   r   rb   r   r   r   r   <module>r      s    " ! ! ! ! !                    0 ' & & & & &	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	      B A A A A A A A A A - - - - - - O O O O O O O O O O O O ' ' ' ' ' '	 	 	 	 	: 	 	 	    t   *H H H H H H H HV# # # # #T # # #L       ,       4( ( ( ( ( ( ( (V> > > > >4 > > > > >r   