
    $j                     n    d Z ddl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	d	S )
zXExtension context providing a controlled API for extensions to interact with the system.    )ABCabstractmethod)TYPE_CHECKING)MemoryEngineInterfacec                   J    e Zd ZdZededdfd            Zedd            ZdS )	ExtensionContexta  
    Abstract context providing a controlled API for extensions.

    Extensions receive this context instead of direct access to internal
    components like MemoryEngine or database connections. This provides:
    - A stable API that won't break when internals change
    - Security by limiting what extensions can access
    - Clear documentation of what extensions can do

    Built-in implementation:
        hindsight_api.extensions.builtin.context.DefaultExtensionContext

    Example usage in an extension:
        class MyTenantExtension(TenantExtension):
            async def on_startup(self) -> None:
                # Run migrations for a new tenant schema
                await self.context.run_migration("tenant_acme")

        class MyHttpExtension(HttpExtension):
            def get_router(self, memory):
                # Use memory engine for custom endpoints
                engine = self.context.get_memory_engine()
                ...
    schemareturnNc                 
   K   dS )a2  
        Run database migrations for a specific schema.

        This creates the schema if it doesn't exist and runs all pending
        migrations. Uses advisory locks to coordinate between distributed workers.

        Args:
            schema: PostgreSQL schema name (e.g., "tenant_acme").
                    The schema will be created if it doesn't exist.

        Raises:
            RuntimeError: If migrations fail to complete.

        Example:
            # Provision a new tenant schema
            await context.run_migration("tenant_acme")
        N )selfr	   s     h/home/rurouni/.hermes/hermes-agent/venv/lib/python3.11/site-packages/hindsight_api/extensions/context.pyrun_migrationzExtensionContext.run_migration$   s      & 	    r   c                     dS )a  
        Get the memory engine interface.

        Returns the MemoryEngineInterface for performing memory operations
        like retain, recall, reflect, and entity/document management.

        Returns:
            MemoryEngineInterface instance.

        Example:
            engine = context.get_memory_engine()
            result = await engine.recall_async(bank_id, query)
        Nr   r   s    r   get_memory_enginez"ExtensionContext.get_memory_engine9   s	     	r   r
   r   )__name__
__module____qualname____doc__r   strr   r   r   r   r   r   r   
   sl         2 # $    ^(    ^  r   r   c                   >    e Zd ZdZ	 ddeddfdZdeddfd	ZddZdS )DefaultExtensionContextzw
    Default implementation of ExtensionContext.

    Uses the system's database URL and migration infrastructure.
    Ndatabase_urlmemory_enginezMemoryEngineInterface | Nonec                 "    || _         || _        dS )z
        Initialize the context.

        Args:
            database_url: SQLAlchemy database URL for migrations.
            memory_engine: Optional MemoryEngine instance for memory operations.
        N)_database_url_memory_engine)r   r   r   s      r   __init__z DefaultExtensionContext.__init__R   s     *+r   r	   r
   c                    K   ddl }ddlm} ddlm}m}m}m} | j        }| j	        t          | j	        dd          }	|	r|	} |            }
|                    ||||
j                   d{V  | j	        Pt          | j	        dd          }|8t          |dd          }|%|                    |||||
j        	           d{V  |                    |||
j        |
           d{V  |                    |||
j        |           d{V  dS )z%Run migrations for a specific schema.r   N)
get_config)ensure_embedding_dimensionensure_text_search_extensionensure_vector_extensionrun_migrationsdb_url)r	   migration_database_url
embeddings	dimension)r	   vector_extension)r,   r	   )text_search_extensionr	   )asynciohindsight_api.configr#   hindsight_api.migrationsr$   r%   r&   r'   r   r    getattr	to_threadr)   r,   r-   )r   r	   r.   r#   r$   r%   r&   r'   r(   
engine_urlconfigr*   r+   s                r   r   z%DefaultExtensionContext.run_migrationa   s     333333	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 #* !4hEEJ $# F6&Jg   
 
 	
 	
 	
 	
 	
 	
 	
 * !4lDIIJ%#JTBB	(!++2!%)/)@ ,          #Vf>U^d   
 
 	
 	
 	
 	
 	
 	
 	

 (&Hdms   
 
 	
 	
 	
 	
 	
 	
 	
 	
 	
r   r   c                 <    | j         t          d          | j         S )z Get the memory engine interface.NzpMemory engine not configured in ExtensionContext. Ensure the context was created with a memory_engine parameter.)r    RuntimeErrorr   s    r   r   z)DefaultExtensionContext.get_memory_engine   s.    &Q   ""r   )Nr   )r   r   r   r   r   r!   r   r   r   r   r   r   r   K   s          9=, ,, 6, , , ,5
# 5
$ 5
 5
 5
 5
n# # # # # #r   r   N)
r   abcr   r   typingr   hindsight_api.engine.interfacer   r   r   r   r   r   <module>r:      s    ^ ^ # # # # # # # #             EDDDDDD> > > > >s > > >BT# T# T# T# T#. T# T# T# T# T#r   