
    $j@                         d Z ddlmZmZ ddlmZ ddlmZmZ erddlm	Z	 ddl
mZmZ ddlmZ ddlmZ  G d	 d
e          ZdS )a  Abstract interface for MemoryEngine public methods.

This module defines the public API that HTTP endpoints and extensions should use
to interact with the memory system. All methods require a RequestContext for
authentication when a TenantExtension is configured.
    )ABCabstractmethod)datetime)TYPE_CHECKINGAny)Budget)RecallResultReflectResult)	TagsMatch)RequestContextc                      e Zd ZdZedefd            Zedddedeeee	f                  dd	d
ee         dz  deee	f         f
d            Z
edddddddddd	dededddededee         dz  dedz  dededededd	ddfd            Zeddddddededddedz  ded edz  dd	dd!fd"            Zedd	deeee	f                  fd#            Zed$d%dedd	d&edeee	f         dz  fd'            Zeded(eeef         dd	ddfd)            Zeded*edd	deee	f         fd+            Zeded,edd	deee	f         fd-            Zedd$d.dededz  d/edd	deeef         f
d0            Zeddd1d2d3dededz  d4edz  d5ed6edd	deee	f         fd7            Zedd8d9dededz  d5edd	deee	f         f
d:            Zeddd;d1d2d<ded4edz  d=ee         dz  d>d?d5ed6edd	deee	f         fd@            ZedAededd	deee	f         dz  fdB            ZedAededd	deeef         fdC            ZedDedd	deee	f         dz  fdE            Zed1d2dFded5ed6edd	deee	f         f
dG            Zededd	deee	f         fdH            ZededIedd	deee	f         dz  fdJ            Zededd	deee	f         fdK            ZededLedd	deee	f         fdM            Z edddNdedOedz  d,edz  dd	deee	f         f
dP            Z!edddedeeee	f                  dd	d
ee         dz  deee	f         f
dQ            Z"dS )RMemoryEngineInterfacez
    Abstract interface for the Memory Engine.

    This defines the public API that should be used by HTTP endpoints and extensions.
    All methods require a RequestContext for authentication.
    returnc                 
   K   dS )z
        Check the health of the memory system.

        Returns:
            Dict with 'status' key ('healthy' or 'unhealthy') and additional info.
        N )selfs    f/home/rurouni/.hermes/hermes-agent/venv/lib/python3.11/site-packages/hindsight_api/engine/interface.pyhealth_checkz"MemoryEngineInterface.health_check   s       	    N)document_tagsbank_idcontentsrequest_contextr   r   c                
   K   dS )a  
        Retain a batch of memory items.

        Args:
            bank_id: The memory bank ID.
            contents: List of content dicts with 'content', optional 'event_date',
                     'context', 'metadata', 'document_id', and per-item 'tags'.
            request_context: Request context for authentication.
            document_tags: Optional tags applied to all items in the batch.

        Returns:
            Dict with processing results.
        Nr   r   r   r   r   r   s        r   retain_batch_asyncz(MemoryEngineInterface.retain_batch_async-         , 	r   i   Fi  i    )	budget
max_tokensenable_trace	fact_typequestion_dateinclude_entitiesmax_entity_tokensinclude_chunksmax_chunk_tokensqueryr   zBudget | Noner   r    r!   r"   r#   r$   r%   r&   r	   c       
         
   K   dS )a  
        Recall memories relevant to a query.

        Args:
            bank_id: The memory bank ID.
            query: The search query.
            budget: Search budget (LOW, MID, HIGH).
            max_tokens: Maximum tokens in response.
            enable_trace: Include trace information.
            fact_type: Filter by fact types.
            question_date: Context date for temporal relevance.
            include_entities: Include entity observations.
            max_entity_tokens: Max tokens for entity observations.
            include_chunks: Include raw chunks.
            max_chunk_tokens: Max tokens for chunks.
            request_context: Request context for authentication.

        Returns:
            RecallResult with matching memories.
        Nr   )r   r   r'   r   r   r    r!   r"   r#   r$   r%   r&   r   s                r   recall_asyncz"MemoryEngineInterface.recall_asyncE   s      J 	r   )r   contextr   response_schemar*   r+   r
   c                
   K   dS )aB  
        Reflect on a query and generate a thoughtful response.

        Args:
            bank_id: The memory bank ID.
            query: The question to reflect on.
            budget: Search budget for retrieving context.
            context: Additional context for the reflection.
            max_tokens: Maximum tokens for the response.
            response_schema: Optional JSON Schema for structured output.
            request_context: Request context for authentication.

        Returns:
            ReflectResult with generated response and supporting facts.
        Nr   )r   r   r'   r   r*   r   r+   r   s           r   reflect_asyncz#MemoryEngineInterface.reflect_asyncl         6 	r   c                
   K   dS )z
        List all memory banks.

        Args:
            request_context: Request context for authentication.

        Returns:
            List of bank info dicts.
        Nr   )r   r   s     r   
list_banksz MemoryEngineInterface.list_banks   s       	r   T)create_if_missingr1   c                
   K   dS )uX  
        Get bank profile including disposition and mission.

        Args:
            bank_id: The memory bank ID.
            request_context: Request context for authentication.
            create_if_missing: If True (default), the bank is auto-created
                with defaults if it does not exist. Pass False to make this
                a strict read — returns None if the bank does not exist.

        Returns:
            Bank profile dict with bank_id, name, disposition, and mission,
            or None when create_if_missing=False and the bank does not
            exist.
        Nr   )r   r   r   r1   s       r   get_bank_profilez&MemoryEngineInterface.get_bank_profile   s      . 	r   dispositionc                
   K   dS )z
        Update bank disposition traits.

        Args:
            bank_id: The memory bank ID.
            disposition: Dict with trait values.
            request_context: Request context for authentication.
        Nr   )r   r   r4   r   s       r   update_bank_dispositionz-MemoryEngineInterface.update_bank_disposition   s        	r   new_infoc                
   K   dS )a'  
        Merge new mission information into bank profile.

        Args:
            bank_id: The memory bank ID.
            new_info: New mission information to merge.
            request_context: Request context for authentication.

        Returns:
            Updated mission info.
        Nr   )r   r   r7   r   s       r   merge_bank_missionz(MemoryEngineInterface.merge_bank_mission         & 	r   missionc                
   K   dS )a  
        Set the bank's mission (replaces existing).

        Args:
            bank_id: The memory bank ID.
            mission: The mission text.
            request_context: Request context for authentication.

        Returns:
            Dict with bank_id and mission.
        Nr   )r   r   r;   r   s       r   set_bank_missionz&MemoryEngineInterface.set_bank_mission   r:   r   )r!   delete_bank_profiler>   c                
   K   dS )a  
        Delete a bank or its memories.

        Args:
            bank_id: The memory bank ID.
            fact_type: If specified, only delete memories of this type.
            delete_bank_profile: If True, also delete the bank profile row itself.
                If False, only delete memories/entities/documents but preserve the bank.
            request_context: Request context for authentication.

        Returns:
            Dict with deletion counts.
        Nr   )r   r   r!   r>   r   s        r   delete_bankz!MemoryEngineInterface.delete_bank   r   r   d   r   )r!   search_querylimitoffsetrB   rC   rD   c                
   K   dS )a  
        List memory units with pagination.

        Args:
            bank_id: The memory bank ID.
            fact_type: Filter by fact type.
            search_query: Full-text search query.
            limit: Maximum results.
            offset: Pagination offset.
            request_context: Request context for authentication.

        Returns:
            Dict with 'items', 'total', 'limit', 'offset'.
        Nr   )r   r   r!   rB   rC   rD   r   s          r   list_memory_unitsz'MemoryEngineInterface.list_memory_units  s      2 	r   i  )r!   rC   c                
   K   dS )at  
        Get graph data for visualization.

        Args:
            bank_id: The memory bank ID.
            fact_type: Filter by fact type.
            limit: Maximum number of items to return (default: 1000).
            request_context: Request context for authentication.

        Returns:
            Dict with nodes, edges, table_rows, total_units, limit.
        Nr   )r   r   r!   rC   r   s        r   get_graph_dataz$MemoryEngineInterface.get_graph_data*        * 	r   
any_strict)rB   tags
tags_matchrC   rD   rK   rL   r   c                
   K   dS )a  
        List documents with pagination.

        Args:
            bank_id: The memory bank ID.
            search_query: Case-insensitive substring filter on document ID.
            tags: Filter by tags.
            tags_match: How to match tags (any, all, any_strict, all_strict).
            limit: Maximum results.
            offset: Pagination offset.
            request_context: Request context for authentication.

        Returns:
            Dict with 'items', 'total', 'limit', 'offset'.
        Nr   )r   r   rB   rK   rL   rC   rD   r   s           r   list_documentsz$MemoryEngineInterface.list_documentsE  r.   r   document_idc                
   K   dS )a  
        Get a specific document.

        Args:
            document_id: The document ID.
            bank_id: The memory bank ID.
            request_context: Request context for authentication.

        Returns:
            Document dict or None if not found.
        Nr   r   rO   r   r   s       r   get_documentz"MemoryEngineInterface.get_documentb  r:   r   c                
   K   dS )a  
        Delete a document and its memory units.

        Args:
            document_id: The document ID.
            bank_id: The memory bank ID.
            request_context: Request context for authentication.

        Returns:
            Dict with deletion counts.
        Nr   rQ   s       r   delete_documentz%MemoryEngineInterface.delete_documentw  r:   r   chunk_idc                
   K   dS )z
        Get a specific chunk.

        Args:
            chunk_id: The chunk ID.
            request_context: Request context for authentication.

        Returns:
            Chunk dict or None if not found.
        Nr   )r   rU   r   s      r   	get_chunkzMemoryEngineInterface.get_chunk        " 	r   )rC   rD   c                
   K   dS )aH  
        List entities for a bank with pagination.

        Args:
            bank_id: The memory bank ID.
            limit: Maximum results.
            offset: Offset for pagination.
            request_context: Request context for authentication.

        Returns:
            Dict with items, total, limit, offset.
        Nr   )r   r   rC   rD   r   s        r   list_entitiesz#MemoryEngineInterface.list_entities  rI   r   c                
   K   dS )aP  
        Get statistics about memory nodes and links for a bank.

        Args:
            bank_id: The memory bank ID.
            request_context: Request context for authentication.

        Returns:
            Dict with node_counts, link_counts, link_counts_by_fact_type,
            link_breakdown, and operations stats.
        Nr   r   r   r   s      r   get_bank_statsz$MemoryEngineInterface.get_bank_stats  s      $ 	r   	entity_idc                
   K   dS )a  
        Get entity details including metadata and observations.

        Args:
            bank_id: The memory bank ID.
            entity_id: The entity ID.
            request_context: Request context for authentication.

        Returns:
            Entity dict with id, canonical_name, mention_count, first_seen,
            last_seen, metadata, and observations. None if not found.
        Nr   )r   r   r^   r   s       r   
get_entityz MemoryEngineInterface.get_entity  s      ( 	r   c                
   K   dS )a  
        List async operations for a bank.

        Args:
            bank_id: The memory bank ID.
            request_context: Request context for authentication.

        Returns:
            Dict with 'total' (int) and 'operations' (list of operation dicts).
        Nr   r\   s      r   list_operationsz%MemoryEngineInterface.list_operations  rX   r   operation_idc                
   K   dS )ag  
        Cancel a pending async operation.

        Args:
            bank_id: The memory bank ID.
            operation_id: The operation ID to cancel.
            request_context: Request context for authentication.

        Returns:
            Dict with success status and message.

        Raises:
            ValueError: If operation not found.
        Nr   )r   r   rc   r   s       r   cancel_operationz&MemoryEngineInterface.cancel_operation  r   r   )namer;   rf   c                
   K   dS )aU  
        Update bank name and/or mission.

        Args:
            bank_id: The memory bank ID.
            name: New bank name (optional).
            mission: New mission text (optional, replaces existing).
            request_context: Request context for authentication.

        Returns:
            Updated bank profile dict.
        Nr   )r   r   rf   r;   r   s        r   update_bankz!MemoryEngineInterface.update_bank  rI   r   c                
   K   dS )a  
        Submit a batch retain operation to run asynchronously.

        Args:
            bank_id: The memory bank ID.
            contents: List of content dicts to retain.
            request_context: Request context for authentication.
            document_tags: Optional tags applied to all items in the async batch.

        Returns:
            Dict with operation_id and items_count.
        Nr   r   s        r   submit_async_retainz)MemoryEngineInterface.submit_async_retain*  rI   r   )#__name__
__module____qualname____doc__r   dictr   strlistr   r   intboolr   r)   r-   r0   r3   r6   r9   r=   r@   rF   rH   rN   rR   rT   rW   rZ   r]   r`   rb   re   rh   rj   r   r   r   r   r      s%	         D    ^  +/   tCH~&
 * Cy4' 
c3h   ^.  #'"&*)-!&!$$ $$ $ $$ $
  $ $ $ 9t#$  $$ $ $ $ $ *$ 
$ $ $ ^$L  #'"'+   
   t   * 
   ^@  * 
d38n		   ^   #'   *	
   
c3h$	   ^0  #s(^
 * 
   ^"  
 * 
c3h   ^(  
 * 
c3h   ^( 
 !%$(   :	
 " * 
c3h   ^6 
 !%#'   :	
 Dj   * 
c3h   ^4 
 !%   :	
  * 
c3h   ^4 
 $(!%".   Dj	
 3i$     * 
c3h   ^8  
 * 
c3h$	   ^(  
 * 
c3h   ^(  *	
 
c3h$	   ^, 
    	
  * 
c3h   ^4  *	
 
c3h   ^&  
 * 
c3h$	   ^*  *	
 
c3h   ^$  
 * 
c3h   ^. 
  "   Dj	
 t * 
c3h   ^,  +/   tCH~&
 * Cy4' 
c3h   ^  r   r   N)rn   abcr   r   r   typingr   r   "hindsight_api.engine.memory_enginer   $hindsight_api.engine.response_modelsr	   r
    hindsight_api.engine.search.tagsr   hindsight_api.modelsr   r   r   r   r   <module>rz      s     $ # # # # # # #       % % % % % % % % 4999999PPPPPPPP::::::333333l l l l lC l l l l lr   