
    Ji                        d Z ddlmZ ddlZddlmZ 	 ddlmZ 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e      Z G d de      Z G d d      Zy# e$ r	 ddl	mZ Y Nw xY w)zN
Directory-based prompt registry implementation that stores prompts as files.
    )annotationsN)Path)Self)	BaseModelField)Prompt)InvalidPromptErrorPromptNotFoundError)DEFAULT_VERSIONPromptModelz
index.jsonc                  B    e Zd ZU dZ edd      Zded<   edd       Zy)	
PromptFilez0Model representing a prompt file stored on disk.NT)defaultexcludezPath | Nonepathc                    ||j                    d|j                   dz  }|j                  |j                          | |j                  |j                   |j                  |j                  |      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
        ..jinja)textnameversionmetadatar   )r   r   
write_textrawr   )clspromptr   prompt_files       l/var/www/html/BankruptcyAI-uat/bankruptcy-ai/venv/lib/python3.12/site-packages/banks/registries/directory.pyfrom_prompt_pathzPromptFile.from_prompt_path    sb     }Afnn-=VDDvzz*&++v~~PVP_P_fq
 	
    )r   z
type[Self]r   r   r   r   returnr   )	__name__
__module____qualname____doc__r   r   __annotations__classmethodr    r    r   r   r      s(    :dD9D+9
 
r    r   c                  .    e Zd ZU dZ eg       Zded<   y)PromptFileIndexz1Index tracking all prompt files in the directory.)r   zlist[PromptFile]filesN)r"   r#   r$   r%   r   r+   r&   r(   r    r   r*   r*   3   s    ;#B/E/r    r*   c                  f    e Zd ZdZddddZedd       ZddddZdd	dd
Zd Z	d Z
d ZddZy)DirectoryPromptRegistryz?Registry that stores prompts as files in a directory structure.F)force_reindexc                  t        |      }|j                         sd}t        |      || _        | j                  t        z  | _        | j
                  j                         r|r| j                          y| j                          y)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)	r   is_dir
ValueError_pathDEFAULT_INDEX_NAME_index_pathexists_scan_load)selfdirectory_pathr.   dir_pathmsgs        r   __init__z DirectoryPromptRegistry.__init__<   sf     ' 9CS/!
::(::&&(MJJLJJLr    c                    | j                   S )z0Get the directory path where prompts are stored.)r2   r8   s    r   r   zDirectoryPromptRegistry.pathS   s     zzr    N)r   c                  |xs t         }| j                  j                  D ]d  }|j                  |k(  s|j                  |k(  s#|j
                  s0|j
                  j                         sKt        di |j                         c S  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
        r(   )
r   _indexr+   r   r   r   r5   r   
model_dumpr
   )r8   r   r   pfs       r   getzDirectoryPromptRegistry.getX   sl     ,_++## 	1Bww$2::#8RWWIY000	1 "!r    )	overwritec                  	 |j                   xs t        }| j                  |j                  |      \  }}|rit	        j
                         |j                  d<   t        j                  || j                        | j                  j                  |<   | j                          yd|j                   d}t        |      # t        $ ry t	        j
                         |j                  d<   t        j                  || 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 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   r2   r@   r+   _saver	   r
   append)r8   r   rD   r   idxrB   r;   s          r   setzDirectoryPromptRegistry.setl   s    	nn7G++g+NGC04

-)3)D)DVTZZ)X!!#&

*6;;-7hi(--" 	,0JJLFOOL),,VTZZ@BKK$$R(JJL		s   BB< !B< <A?D>=D>c                   t         j                  | j                  j                               | _        | j                  j
                  D ]=  }|j                  | j                  |j                   d|j                   dz  |_        ? y)z Load the prompt index from disk.Nr   r   )
r*   model_validate_jsonr4   	read_textr@   r+   r   r2   r   r   )r8   rB   s     r   r7   zDirectoryPromptRegistry._load   sl    %99$:J:J:T:T:VW++## 	GBww**"''!BJJ<v'FF	Gr    c                j    | j                   j                  | j                  j                                y)zSave the prompt index to disk.N)r4   r   r@   model_dump_jsonr>   s    r   rJ   zDirectoryPromptRegistry._save   s#    ##DKK$?$?$ABr    c           	        t               | _        | j                  j                  d      D ]  }d|j                  v r|j                  j                  dd      n|j                  t        f\  }}|j                  d      5 }t        |j                         |||i       }| j                  j                  j                  |       ddd        | j                  j                  | j                  j                                y# 1 sw Y   xY w)z4Scan directory for prompt files and build the index.z*.jinja*r      r)r   r   r   r   r   N)r*   r@   r2   globstemrsplitr   openr   readr+   rK   r4   r   rR   )r8   r   r   r   frB   s         r   r6   zDirectoryPromptRegistry._scan   s    '6'8JJOOJ/ 	-D8;tyy8HDII,,S!4tyyZiNjMD'3 -1QVVXD'PT_ab!!((,- -	-
 	##DKK$?$?$AB- -s   >AD  D		c                   t        | j                  j                        D ]*  \  }}|j                  |k(  s|j                  |k(  s&||fc S  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 '')	enumerater@   r+   r   r   r
   )r8   r   r   irB   r;   s         r   rG   z(DirectoryPromptRegistry._get_prompt_file   sf     t{{001 	EArww$2::#8"u	 /tfOG9AN!#&&r    )r9   strr.   bool)r!   r   )r   r`   r   
str | Noner!   r   )r   r   rD   ra   )r   rb   r   r`   r!   ztuple[int, PromptFile])r"   r#   r$   r%   r<   propertyr   rC   rM   r7   rJ   r6   rG   r(   r    r   r-   r-   9   sL    IEJ .   7; "( 8= 6GCC'r    r-   )r%   
__future__r   rH   pathlibr   typingr   ImportErrortyping_extensionspydanticr   r   banksr   banks.errorsr	   r
   banks.promptr   r   r3   r   r*   r-   r(   r    r   <module>rm      sl    #  ' &  @ 5 " 
 
00i 0w' w'U  '&'s   A A)(A)