o
    |!g                     @  s   d Z ddlmZ ddlZddlmZ zddlmZ W n ey)   ddl	mZ Y nw ddl
mZmZ ddlmZ ddlmZmZ dd	lmZmZ d
ZG dd deZG dd deZG dd dZdS )zN
Directory-based prompt registry implementation that stores prompts as files.
    )annotationsN)Path)Self)	BaseModelField)Prompt)InvalidPromptErrorPromptNotFoundError)DEFAULT_VERSIONPromptModelz
index.jsonc                   @  s2   e Zd ZU dZeddZded< edddZdS )
PromptFilez0Model representing a prompt file stored on disk.T)excluder   pathcls
type[Self]promptr   returnr   c                 C  s>   ||j  d|j d }||j | |j|j |j|j|dS )a  
        Create a PromptFile instance from a Prompt object and save it to disk.

        Args:
            prompt: The Prompt object to save
            path: Directory path where the prompt file should be stored

        Returns:
            A new PromptFile instance
        .z.jinja)textnameversionmetadatar   )r   r   
write_textrawr   )r   r   r   prompt_file r   \/mnt/skqttb/ctump_chatbot/chatbot/lib/python3.10/site-packages/banks/registries/directory.pyfrom_prompt_path    s
   zPromptFile.from_prompt_pathN)r   r   r   r   r   r   r   r   )	__name__
__module____qualname____doc__r   r   __annotations__classmethodr   r   r   r   r   r      s
   
 r   c                   @  s$   e Zd ZU dZeg dZded< dS )PromptFileIndexz1Index tracking all prompt files in the directory.)defaultzlist[PromptFile]filesN)r   r   r    r!   r   r&   r"   r   r   r   r   r$   3   s   
 r$   c                   @  sp   e Zd ZdZddd$dd	Zed%ddZddd&ddZddd'ddZdd Z	dd Z
dd  Zd(d"d#ZdS ))DirectoryPromptRegistryz?Registry that stores prompts as files in a directory structure.F)force_reindexdirectory_pathr   r(   boolc                C  sL   |  s
d}t||| _| jt | _| j r|r |   dS |   dS )a.  
        Initialize the directory prompt registry.

        Args:
            directory_path: Path to directory where prompts will be stored
            force_reindex: Whether to force rebuilding the index from disk

        Raises:
            ValueError: If directory_path is not a directory
        z%{directory_path} must be a directory.N)is_dir
ValueError_pathDEFAULT_INDEX_NAME_index_pathexists_scan_load)selfr)   r(   msgr   r   r   __init__<   s   z DirectoryPromptRegistry.__init__r   c                 C  s   | j S )z0Get the directory path where prompts are stored.)r-   r3   r   r   r   r   R   s   zDirectoryPromptRegistry.pathN)r   r   strr   
str | Noner   c                C  sN   |pt }| jjD ]}|j|kr$|j|kr$|j r$tdi |   S qt	)a2  
        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 prompt doesn't exist
        Nr   )
r
   _indexr&   r   r   r   r0   r   
model_dumpr	   )r3   r   r   pfr   r   r   getW   s   zDirectoryPromptRegistry.get)	overwriter   r=   c                C  s   z5|j pt}| j|j|d\}}|r+t |jd< t|| j	| j
j|< |   W dS d|j d}t| tyX   t |jd< t|| j	}| j
j| |   Y dS w )a  
        Store a prompt in the registry.

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

        Raises:
            InvalidPromptError: If prompt exists and overwrite=False
        )r   r   
created_atzPrompt with name 'z1' already exists. Use overwrite=True to overwriteN)r   r
   _get_prompt_filer   timectimer   r   r   r-   r9   r&   _saver   r	   append)r3   r   r=   r   idxr;   r4   r   r   r   setk   s   
zDirectoryPromptRegistry.setc                 C  s   t | j | _dS )z Load the prompt index from disk.N)r$   model_validate_jsonr/   	read_textr9   r6   r   r   r   r2         zDirectoryPromptRegistry._loadc                 C  s   | j | j  dS )zSave the prompt index to disk.N)r/   r   r9   model_dump_jsonr6   r   r   r   rB      rH   zDirectoryPromptRegistry._savec              	   C  s   t  | _| jdD ]<}d|jv r|jddn|jtf\}}|d}t|	 |||i d}| jj
| W d   n1 sAw   Y  q
| j| j  dS )z4Scan directory for prompt files and build the index.z*.jinja*r      r)r   r   r   r   r   N)r$   r9   r-   globstemrsplitr
   openr   readr&   rC   r/   r   rI   )r3   r   r   r   fr;   r   r   r   r1      s   &zDirectoryPromptRegistry._scantuple[int, PromptFile]c                C  sP   t | jjD ]\}}|j|kr|j|kr||f  S qd| d| d}t|)a(  
        Find a prompt file in the index.

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

        Returns:
            Tuple of (index position, PromptFile)

        Raises:
            PromptNotFoundError: If prompt doesn't exist in index
        zcannot find prompt with name 'z' and version '')	enumerater9   r&   r   r   r	   )r3   r   r   ir;   r4   r   r   r   r?      s   z(DirectoryPromptRegistry._get_prompt_file)r)   r   r(   r*   )r   r   )r   r7   r   r8   r   r   )r   r   r=   r*   )r   r8   r   r7   r   rR   )r   r   r    r!   r5   propertyr   r<   rE   r2   rB   r1   r?   r   r   r   r   r'   9   s    
r'   )r!   
__future__r   r@   pathlibr   typingr   ImportErrortyping_extensionspydanticr   r   banksr   banks.errorsr   r	   banks.promptr
   r   r.   r   r$   r'   r   r   r   r   <module>   s"   