o
    |!g                     @  sj   d Z ddlmZ ddlmZ ddlmZ ddlmZm	Z	 ddl
mZmZ G dd deZG d	d
 d
ZdS )a#  
File-based prompt registry implementation that stores all prompts in a single JSON file.

This module provides functionality to store and retrieve prompts using a single JSON file
as the storage backend. The file contains an index of all prompts with their associated
metadata and content.
    )annotations)Path)	BaseModel)InvalidPromptErrorPromptNotFoundError)PromptPromptModelc                   @  s   e Zd ZU dZg Zded< dS )PromptRegistryIndexz
    Model representing the registry index containing all prompts.

    Stores a list of PromptModel objects that represent all prompts in the registry.
    zlist[PromptModel]promptsN)__name__
__module____qualname____doc__r
   __annotations__ r   r   W/mnt/skqttb/ctump_chatbot/chatbot/lib/python3.10/site-packages/banks/registries/file.pyr	      s   
 r	   c                   @  sN   e Zd ZdZdddZdd	dddZdddddZd ddZd!ddZdS )"FilePromptRegistryz@A prompt registry storing all prompt data in a single JSON file.registry_indexr   returnNonec                 C  sR   || _ tg d| _zt| j  | _W dS  ty(   | j jjddd Y dS w )z
        Initialize the file prompt registry.

        Args:
            registry_index: Path to the JSON file that will store the prompts

        Note:
            Creates parent directories if they don't exist.
        )r
   T)parentsexist_okN)_index_fpathr	   _indexmodel_validate_json	read_textFileNotFoundErrorparentmkdir)selfr   r   r   r   __init__#   s   
zFilePromptRegistry.__init__N)versionnamestrr!   
str | Noner   c                C  s"   |  ||\}}tdi | S )a@  
        Retrieve a prompt by name and version.

        Args:
            name: Name of the prompt to retrieve
            version: Version of the prompt (optional)

        Returns:
            The requested Prompt object

        Raises:
            PromptNotFoundError: If the requested prompt doesn't exist
        Nr   )_get_prompt_modelr   
model_dump)r   r"   r!   _modelr   r   r   get5   s   zFilePromptRegistry.getF)	overwritepromptr*   boolc                C  s   z'|  |j|j\}}|rt|| jj|< |   W dS d|j d}t| t	yA   t|}| jj
| |   Y dS w )a  
        Store a prompt in the registry.

        Args:
            prompt: The Prompt object to store
            overwrite: Whether to overwrite an existing prompt

        Raises:
            InvalidPromptError: If prompt exists and overwrite=False
        zPrompt with name 'z1' already exists. Use overwrite=True to overwriteN)r%   r"   r!   r   from_promptr   r
   _saver   r   append)r   r+   r*   idxp_modelmsgr   r   r   setF   s   
zFilePromptRegistry.setc                 C  sF   t | jddd}|| j  W d   dS 1 sw   Y  dS )zt
        Save the prompt index to the JSON file.

        Writes the current state of the registry to disk.
        wzutf-8)encodingN)openr   writer   model_dump_json)r   fr   r   r   r.   ^   s   "zFilePromptRegistry._savetuple[int, PromptModel]c                 C  sP   t | jjD ]\}}|j|kr|j|kr||f  S qd| d| d}t|)aF  
        Find a prompt model in the index by name and version.

        Args:
            name: Name of the prompt
            version: Version of the prompt

        Returns:
            Tuple of (index position, PromptModel)

        Raises:
            PromptNotFoundError: If the prompt doesn't exist in the index
        zcannot find prompt with name 'z' and version '')	enumerater   r
   r"   r!   r   )r   r"   r!   ir(   r2   r   r   r   r%   g   s   z$FilePromptRegistry._get_prompt_model)r   r   r   r   )r"   r#   r!   r$   r   r   )r+   r   r*   r,   r   r   )r   r   )r"   r$   r!   r$   r   r:   )	r   r   r   r   r    r)   r3   r.   r%   r   r   r   r   r       s    

	r   N)r   
__future__r   pathlibr   pydanticr   banks.errorsr   r   banks.promptr   r   r	   r   r   r   r   r   <module>   s   
