
    >'h                    v    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e      Z G d	 d
      Zy)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                       e Zd ZU dZg Zded<   y)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__     \/home/kushmeetdev/Regenta/Chatbot/venv/lib/python3.12/site-packages/banks/registries/file.pyr   r      s     "$G#r   r   c                  D    e Zd ZdZddZddddZddddZdd	Zdd
Zy)FilePromptRegistryz@A prompt registry storing all prompt data in a single JSON file.c                   t        |      | _        t        g       | _        	 t        j	                  | j                  j                  d            | _        y# t        $ r* | j                  j                  j                  dd       Y yw xY 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   utf-8encodingT)parentsexist_okN)	r   _index_fpathr   _indexmodel_validate_json	read_textFileNotFoundErrorparentmkdir)selfregistry_indexs     r   __init__zFilePromptRegistry.__init__#   sz     #'~"6+>r+J	H-AA$BSBSB]B]gnB]BopDK  	H$$**4$*G	Hs   4A 0B
BN)versionc               ^    | j                  ||      \  }}t        di |j                         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
        r   )_get_prompt_modelr   
model_dump)r$   namer'   _models        r   getzFilePromptRegistry.get5   s1     ))$85+((*++r   F)	overwritec                  	 | j                  |j                  |j                        \  }}|r=t        j                  |      | j
                  j                  |<   | j                          yd|j                   d}t        |      # t        $ rM t        j                  |      }| j
                  j                  j                  |       | j                          Y yw xY 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$   promptr/   idxp_modelmsgs         r   setzFilePromptRegistry.setF   s    	11&++v~~NLC+6+B+B6+J##C(

*6;;-7hi(--" 	!--f5GKK&&w/JJL	s   A'B *B ACCc                    t        | j                  dd      5 }|j                  | j                  j	                                ddd       y# 1 sw Y   yxY w)zt
        Save the prompt index to the JSON file.

        Writes the current state of the registry to disk.
        wr   r   N)openr   writer   model_dump_json)r$   fs     r   r2   zFilePromptRegistry._save^   sF     $##S7; 	3qGGDKK//12	3 	3 	3s   *AAc                    t        | j                  j                        D ]*  \  }}|j                  |k(  s|j                  |k(  s&||fc S  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-   r7   s         r   r)   z$FilePromptRegistry._get_prompt_modelg   sg     "$++"5"56 	 HAuzzT!emmw&>%x	  /tfOG9AN!#&&r   )r%   strreturnNone)r+   rC   r'   
str | NonerD   r   )r4   r   r/   boolrD   rE   )rD   rE   )r+   rF   r'   rF   rD   ztuple[int, PromptModel])	r   r   r   r   r&   r.   r8   r2   r)   r   r   r   r   r       s)    JH$ 7; ," 8= 03'r   r   N)r   
__future__r   pathlibr   pydanticr   banks.errorsr   r   banks.promptr   r	   r   r   r   r   r   <module>rM      s3    #   @ ,$) $Z' Z'r   