
    i                        S r SSKJr  SSKrSSKJr   SSKJr  SSK
JrJr  SSKJr  SSKJrJr  SS	KJrJr  S
r " S S\5      r " S S\5      r " S S5      rg! \ a	    SSK	Jr   NOf = f)zN
Directory-based prompt registry implementation that stores prompts as files.
    )annotationsN)Path)Self)	BaseModelField)Prompt)InvalidPromptErrorPromptNotFoundError)DEFAULT_VERSIONPromptModelz
index.jsonc                  F    \ rS rSr% Sr\" SSS9rS\S'   \S
S j5       r	S	r
g)
PromptFile   z0Model representing a prompt file stored on disk.NT)defaultexcludezPath | Nonepathc                    X!R                    SUR                   S3-  nUR                  UR                  5        U " UR                  UR                   UR                  UR                  US9$ )z
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       T/home/james-whalen/.local/lib/python3.13/site-packages/banks/registries/directory.pyfrom_prompt_pathPromptFile.from_prompt_path    s`     }Afnn-=VDDvzz*&++v~~PVP_P_fq
 	
     )r   z
type[Self]r   r   r   r   returnr   )__name__
__module____qualname____firstlineno____doc__r   r   __annotations__classmethodr    __static_attributes__r#   r"   r   r   r      s(    :dD9D+9
 
r"   r   c                  0    \ rS rSr% Sr\" / S9rS\S'   Srg)PromptFileIndex3   z1Index tracking all prompt files in the directory.)r   zlist[PromptFile]filesr#   N)	r%   r&   r'   r(   r)   r   r0   r*   r,   r#   r"   r   r.   r.   3   s    ;#B/E/r"   r.   c                  ~    \ rS rSrSrSS.SS jjr\SS j5       rSS.SS	 jjrSS
.SS jjr	S r
S rS rSS jrSrg)DirectoryPromptRegistry9   z?Registry that stores prompts as files in a directory structure.F)force_reindexc               (   [        U5      nUR                  5       (       d  Sn[        U5      eX0l        U R                  [        -  U l        U R
                  R                  5       (       a  U(       a  U R                  5         gU R                  5         g)z
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_pathr4   dir_pathmsgs        r   __init__ DirectoryPromptRegistry.__init__<   sj     '  9CS/!
::(::&&((MJJLJJLr"   c                    U R                   $ )z0Get the directory path where prompts are stored.)r8   r>   s    r   r   DirectoryPromptRegistry.pathS   s     zzr"   N)r   c               H   U=(       d    [         nU R                  R                   Ht  nUR                  U:X  d  M  UR                  U:X  d  M'  UR
                  (       d  M:  UR
                  R                  5       (       d  M[  [        S0 UR                  5       D6s  $    [        e)z
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   _indexr0   r   r   r   r;   r   
model_dumpr
   )r>   r   r   pfs       r   getDirectoryPromptRegistry.getX   sl     ,_++##Bww$2::#8RWWWIYIY000 $ "!r"   )	overwritec                   UR                   =(       d    [        nU R                  UR                  US9u  pEU(       ai  [        R
                  " 5       UR                  S'   [        R                  XR                  5      U R                  R                  U'   U R                  5         gSUR                   S3n[        U5      e! [         ay    [        R
                  " 5       UR                  S'   [        R                  XR                  5      nU R                  R                  R                  U5        U R                  5          gf = f)z
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    r8   rH   r0   _saver	   r
   append)r>   r   rM   r   idxrJ   rA   s          r   setDirectoryPromptRegistry.setl   s    	nn7G++g+NGC04

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

*6;;-7hi(--" 	,0JJLFOOL),,VZZ@BKK$$R(JJL		s   B$C 'C B EEc                   [         R                  U R                  R                  5       5      U l        U R                  R
                   H@  nUR                  b  M  U R                  UR                   SUR                   S3-  Ul        MB     g)z Load the prompt index from disk.Nr   r   )
r.   model_validate_jsonr:   	read_textrH   r0   r   r8   r   r   )r>   rJ   s     r   r=   DirectoryPromptRegistry._load   se    %99$:J:J:T:T:VW++##Bww**"''!BJJ<v'FF $r"   c                j    U R                   R                  U R                  R                  5       5        g)zSave the prompt index to disk.N)r:   r   rH   model_dump_jsonrE   s    r   rS   DirectoryPromptRegistry._save   s#    ##DKK$?$?$ABr"   c           
     "   [        5       U l        U R                  R                  S5       H  nSUR                  ;   a  UR                  R                  SS5      OUR                  [        4u  p#UR                  S5       n[        UR                  5       X#U0 S9nU R                  R                  R                  U5        SSS5        M     U R                  R                  U R                  R                  5       5        g! , (       d  f       M  = f)z4Scan directory for prompt files and build the index.z*.jinja*r      r)r   r   r   r   r   N)r.   rH   r8   globstemrsplitr   openr   readr0   rT   r:   r   r]   )r>   r   r   r   frJ   s         r   r<   DirectoryPromptRegistry._scan   s    '6'8JJOOJ/D8;tyy8HDII,,S!4tyyZiNjMD31QVVXDPT_ab!!((,   0
 	##DKK$?$?$AB  s   ?A C??
D	c                   [        U R                  R                  5       H,  u  p4UR                  U:X  d  M  UR                  U:X  d  M)  X44s  $    SU SU S3n[        U5      e)z
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 '')	enumeraterH   r0   r   r   r
   )r>   r   r   irJ   rA   s         r   rP   (DirectoryPromptRegistry._get_prompt_file   s]     t{{001EAww$2::#8u 2 /tfOG9AN!#&&r"   )rH   r:   r8   )r?   strr4   bool)r$   r   )r   rn   r   
str | Noner$   r   )r   r   rM   ro   )r   rp   r   rn   r$   ztuple[int, PromptFile])r%   r&   r'   r(   r)   rB   propertyr   rK   rV   r=   rS   r<   rP   r,   r#   r"   r   r2   r2   9   sL    IEJ .   7; "( 8= 6GCC'r"   r2   )r)   
__future__r   rQ   pathlibr   typingr   ImportErrortyping_extensionspydanticr   r   banksr   banks.errorsr	   r
   banks.promptr   r   r9   r   r.   r2   r#   r"   r   <module>r{      sl    #  ' &  @ 5 " 
 
00i 0w' w'U  '&'s   A A*)A*