Ë
    tPêj5  ã                   óŽ   — d Z ddlZddlmZ ddlmZmZ ddlZddlm	Z	m
Z
 ddlmZ dededz  fd	„Z G d
„ d«      Z G d„ d«      Zy)zq
Based on Google's GenAI Kit dotprompt implementation: https://google.github.io/dotprompt/reference/frontmatter/
é    N)ÚPath)ÚAnyÚFinal)Ú
DictLoaderÚselect_autoescape)ÚImmutableSandboxedEnvironmentÚ	prompt_idÚreturnc                 óZ   — | j                  d«      \  }}}|r|r|j                  «       r|S y )Nú.v)Ú
rpartitionÚisdigit)r	   ÚbaseÚ	separatorÚversions       úr/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/litellm/integrations/dotprompt/prompt_manager.pyÚstrip_version_suffixr      s/   € Ø(×3Ñ3°DÓ9Ñ€Dˆ)�WÙ‘T˜gŸo™oÔ/ØˆØó    c                   óF   — e Zd ZdZ	 	 ddedeeef   dz  dedz  fd„Zd„ Zy)	ÚPromptTemplatez>Represents a single prompt template with metadata and content.NÚcontentÚmetadataÚtemplate_idc                 ó  — || _         |xs i | _        || _        g d¢}| j                  j                  d«      | _        | j                  j                  di «      j                  di «      | _        | j                  j                  di «      j                  d«      | _        | j                  j                  di «      j                  di «      | _        i | _        | j                  D ]#  }||vsŒ| j                  |   | j                  |<   Œ% y )N)ÚmodelÚinputÚoutputr   r   Úschemar   Úformat)	r   r   r   Úgetr   Úinput_schemaÚoutput_formatÚoutput_schemaÚoptional_params)Úselfr   r   r   Úrestricted_keysÚkeys         r   Ú__init__zPromptTemplate.__init__   sç   € ð ˆŒØ š BˆŒØ&ˆÔò ">ˆØ—]‘]×&Ñ& wÓ/ˆŒ
Ø ŸM™M×-Ñ-¨g°rÓ:×>Ñ>¸xÈÓLˆÔØ!Ÿ]™]×.Ñ.¨x¸Ó<×@Ñ@ÀÓJˆÔØ!Ÿ]™]×.Ñ.¨x¸Ó<×@Ñ@ÀÈ2ÓNˆÔØ!ˆÔØ—=‘=ò 	?ˆCØ˜/Ò)Ø,0¯M©M¸#Ñ,>�×$Ñ$ SÒ)ñ	?r   c                 ó<   — d| j                   › d| j                  › d�S )NzPromptTemplate(id='z
', model='z'))r   r   ©r%   s    r   Ú__repr__zPromptTemplate.__repr__-   s"   € Ø$ T×%5Ñ%5Ð$6°jÀÇÁÀÈBÐOÐOr   ©NN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚstrÚdictr   r(   r+   © r   r   r   r      sE   „ ÙHð
 +/Ø"&ñ	?àð?ð �s˜C�x‘. 4Ñ'ð?ð ˜4‘Zó	?ó*Pr   r   c                   óJ  — e Zd ZdZ	 	 	 	 d!dedz  dedz  deeeeef   f   dz  dedz  fd„Zd"d	„Zd#deeeeef   f   dedz  ddfd
„Z	dee
z  dedefd„Zdedeeeef   ef   fd„Z	 	 d$dedeeef   dz  dedz  defd„Zdeeef   deeef   ddfd„Zdedeez  fd„Zd#dededz  dedz  fd„Zdee   fd„Zdedeeef   dz  fd„Zd"d„Zd#dededeeef   dz  ddfd„Zdee
z  deeef   fd„Zdeeef   defd„Zdeeeeef   f   fd„Zdeeeeef   f   ddfd „Zy)%ÚPromptManagera  
    Manager for loading and rendering .prompt files following the Dotprompt specification.

    Supports:
    - YAML frontmatter for metadata
    - Handlebars-style templating (using Jinja2)
    - Input/output schema validation
    - Model configuration
    Nr	   Úprompt_directoryÚprompt_dataÚprompt_filec           
      óŠ  — |rt        |«      nd | _        i | _        || _        t	        t        i «      t        ddg«      dddddd¬	«      | _        | j                  r| j                  «        | j                  r8|st        d
«      ‚| j                  | j                  |«      }|| j                  |<   |r| j                  ||«       y y )NÚhtmlÚxmlz{{z}}z{%z%}z{#z#})ÚloaderÚ
autoescapeÚvariable_start_stringÚvariable_end_stringÚblock_start_stringÚblock_end_stringÚcomment_start_stringÚcomment_end_stringz2prompt_id is required when prompt_file is provided)r   r6   Úpromptsr8   r   r   r   Ú	jinja_envÚ_load_promptsÚ
ValueErrorÚ_load_prompt_fileÚ_load_prompts_from_json)r%   r	   r6   r7   r8   Útemplates         r   r(   zPromptManager.__init__<   sÉ   € ñ ;K¤Ð%5Ô 6ÐPTˆÔØ24ˆŒØ&ˆÔô 7Ü˜b“>Ü(¨&°%¨Ó9à"&Ø $Ø#Ø!Ø!%Ø#ô

ˆŒð × Ò Ø×ÑÔ à×ÒÙÜ Ð!UÓVÐVà"×4Ñ4°T×5EÑ5EÀyÓQˆHØ&.ˆD�L‰L˜Ñ#ñ Ø×(Ñ(¨°iÕ@ð r   r
   c                 óP  — | j                   r| j                   j                  «       st        d| j                   › �«      ‚t        | j                   j	                  d«      «      }|D ]0  }	 |j
                  }| j                  ||«      }|| j                  |<   Œ2 y# t        $ r Y Œ?w xY w)z1Load all .prompt files from the prompt directory.z!Prompt directory does not exist: z*.promptN)	r6   ÚexistsrG   ÚlistÚglobÚstemrH   rD   Ú	Exception)r%   Úprompt_filesr8   r	   rJ   s        r   rF   zPromptManager._load_promptsd   s¤   € à×$Ò$¨D×,AÑ,A×,HÑ,HÔ,JÜÐ@À×AVÑAVÐ@WÐXÓYÐYä" 4×#8Ñ#8×#=Ñ#=¸jÓ#IÓJˆà'ò 	ˆKðØ'×,Ñ,�	Ø×1Ñ1°+¸yÓI�Ø*2�—‘˜YÒ'ñ		øô ò áðús   Á)-BÂ	B%Â$B%c                 óî   — |rd|v r||in|}|j                  «       D ]G  \  }}	 |j                  dd«      }|j                  di «      }t        |||¬«      }|| j                  |<   ŒI y# t        $ r Y ŒVw xY w)a:  Load prompts from JSON data structure.

        Expected format:
        {
            "prompt_id": {
                "content": "template content",
                "metadata": {"model": "gpt-4", "temperature": 0.7, ...}
            }
        }

        or

        {
            "content": "template content",
            "metadata": {"model": "gpt-4", "temperature": 0.7, ...}
        } + prompt_id

        A dict carrying a "content" key is a single flat template registered under
        prompt_id; anything else is treated as already keyed by template ID.
        r   Ú r   ©r   r   r   N)Úitemsr    r   rD   rP   )	r%   r7   r	   Úkeyed_promptsr   Úprompt_infor   r   rJ   s	            r   rI   z%PromptManager._load_prompts_from_jsonu   s•   € ñ* <EÈÐVaÑIa 	¨;Ñ7Ðgrˆà(5×(;Ñ(;Ó(=ò 	Ñ$ˆK˜ðØ%Ÿ/™/¨)°RÓ8�Ø&Ÿ?™?¨:°rÓ:�ä)Ø#Ø%Ø +ô�ð
 -5�—‘˜[Ò)ñ	øô ò Ùðús   ¤AA(Á(	A4Á3A4Ú	file_pathc                 ó¼   — t        |t        «      rt        |«      }|j                  d¬«      }| j	                  |«      \  }}t        |j                  «       ||¬«      S )z%Load and parse a single .prompt file.úutf-8©ÚencodingrT   )Ú
isinstancer1   r   Ú	read_textÚ_parse_frontmatterr   Ústrip)r%   rX   r	   r   ÚfrontmatterÚtemplate_contents         r   rH   zPromptManager._load_prompt_fileš   s_   € ä�i¤Ô%Ü˜Y›ˆIà"×,Ñ,°gÐ,Ó>ˆð )-×(?Ñ(?ÀÓ(HÑ%ˆÐ%äØ$×*Ñ*Ó,Ø Ø!ô
ð 	
r   r   c                 ó6  — d}t        j                  ||t         j                  «      }|r@|j                  d«      }|j                  d«      }	 t	        j
                  |«      xs i }||fS i }|}||fS # t        j                  $ r}t        d|› �«      ‚d}~ww xY w)z+Parse YAML frontmatter from prompt content.z^---\s*\n(.*?)\n---\s*\n(.*)$é   é   zInvalid YAML frontmatter: N)ÚreÚmatchÚDOTALLÚgroupÚyamlÚ	safe_loadÚ	YAMLErrorrG   )r%   r   Úfrontmatter_patternrg   Úfrontmatter_yamlrb   ra   Úes           r   r_   z PromptManager._parse_frontmatterª   s­   € ð &FÐÜ—x‘xÐ 3°W¼b¿i¹iÓHˆáØ&+§k¡k°!£nÐØ$Ÿ{™{¨1›~ÐðCÜ"Ÿn™nÐ-=Ó>ÒDÀ"�ð Ð,Ð,Ð,ð ˆKØ&ÐàÐ,Ð,Ð,øô —>‘>ò CÜ Ð#=¸a¸SÐ!AÓBÐBûðCús   ÁA2 Á2BÂBÂBÚprompt_variablesr   c                 óÂ  — | j                  ||¬«      }|€At        | j                  j                  «       «      }|rd|› d�nd}t	        d|› d|› d|› �«      ‚|xs i }|j
                  r| j                  ||j
                  «       	 | j                  j                  |j                  «      } |j                  di |¤Ž}	|	S # t        $ r}
t        d	|› d
|
› �«      ‚d}
~
ww xY w)aâ  
        Render a prompt template with the given variables.

        Args:
            prompt_id: The ID of the prompt template to render
            prompt_variables: Variables to substitute in the template
            version: Optional version number. If provided, looks for {prompt_id}.v{version}

        Returns:
            The rendered prompt string

        Raises:
            KeyError: If prompt_id is not found
            ValueError: If template rendering fails
        )r	   r   Nz
 (version ú)rS   zPrompt 'ú'z not found. Available prompts: zError rendering template 'z': r3   )Ú
get_promptrM   rD   ÚkeysÚKeyErrorr!   Ú_validate_inputrE   Úfrom_stringr   ÚrenderrP   rG   )r%   r	   rp   r   rJ   Úavailable_promptsÚversion_strÚ	variablesÚjinja_templateÚrenderedro   s              r   ry   zPromptManager.render¿   s÷   € ð, Ÿ/™/°IÀw˜/ÓOˆàÐÜ'+¨D¯L©L×,=Ñ,=Ó,?Ó'@ÐÙ<C :¨g¨Y°aÑ!8ÈˆKÜ˜X i [°°+°Ð>]Ð^oÐ]pÐqÓrÐrà+Ò1¨rˆ	ð × Ò Ø× Ñ  ¨H×,AÑ,AÔBð	Mà$(§N¡N×$>Ñ$>¸x×?OÑ?OÓ$PˆNØ3˜n×3Ñ3Ñ@°iÑ@ˆHØˆOøÜò 	MÜÐ9¸)¸ÀCÈÀsÐKÓLÐLûð	Mús   Â8B? Â?	CÃCÃCr|   r   c                 óø   — |j                  «       D ]g  \  }}||v sŒ||   }| j                  |«      }t        ||«      rŒ.t        d|› dt	        |dt        |«      «      › dt        |«      j                  › �«      ‚ y)z3Basic validation of input variables against schema.zInvalid type for field 'z': expected r-   z, got N)rU   Ú_get_python_typer]   rG   Úgetattrr1   Útyper-   )r%   r|   r   Ú
field_nameÚ
field_typeÚvalueÚexpected_types          r   rw   zPromptManager._validate_inputê   s�   € à&,§l¡l£nò 		Ñ"ˆJ˜
Ø˜YÒ&Ø! *Ñ-�Ø $× 5Ñ 5°jÓ A�ä! %¨Õ7Ü$Ø2°:°,ð ?$Ü$+¨M¸:ÄsÈ=ÓGYÓ$ZÐ#[Ð[aÔbfÐglÓbm×bvÑbvÐawðyóð ñ		r   Úschema_typec                 óÔ   — t         t         t        t        ft        t        t        t        t        t        t        t
        t
        dœ}|j                  |j                  «       t         «      S )z*Convert schema type string to Python type.)Ústringr1   ÚnumberÚintegerÚintÚfloatÚbooleanÚboolÚarrayrM   Úobjectr2   )r1   rŒ   r�   r�   rM   r2   r    Úlower)r%   r‡   Útype_mappings      r   r€   zPromptManager._get_python_type÷   sQ   € ô ÜÜœE�lÜÜÜÜÜÜÜÜÜñ8
ˆð ×Ñ × 1Ñ 1Ó 3´SÓ9Ð9r   c                 óà   — |�$|› d|› �}|| j                   v r| j                   |   S | j                   j                  |«      }|�|S t        |«      }|r| j                   j                  |«      S dS )a#  
        Get a prompt template by ID and optional version.

        Args:
            prompt_id: The base prompt ID
            version: Optional version number. If provided, looks for {prompt_id}.v{version}

        Returns:
            The prompt template if found, None otherwise
        Nr   )rD   r    r   )r%   r	   r   Úversioned_idÚdirect_matchÚbase_prompt_ids         r   rt   zPromptManager.get_prompt
  s{   € ð Ðà%. K¨r°'°Ð";ˆLØ˜tŸ|™|Ñ+Ø—|‘| LÑ1Ð1à"Ÿl™l×.Ñ.¨yÓ9ˆØÐ#ØÐä 4°YÓ ?ˆÙ3Aˆt�|‰|×Ñ Ó/ÐKÀtÐKr   c                 óH   — t        | j                  j                  «       «      S )z'Get a list of all available prompt IDs.)rM   rD   ru   r*   s    r   Úlist_promptszPromptManager.list_prompts"  s   € ä�D—L‘L×%Ñ%Ó'Ó(Ð(r   c                 óX   — | j                   j                  |«      }|r|j                  S dS )z#Get metadata for a specific prompt.N)rD   r    r   )r%   r	   rJ   s      r   Úget_prompt_metadataz!PromptManager.get_prompt_metadata&  s)   € àŸ,™,×*Ñ*¨9Ó5ˆÙ$,ˆx× Ñ Ð6°$Ð6r   c                 ór   — | j                   j                  «        | j                  r| j                  «        yy)zBReload all prompts from the directory (if directory was provided).N)rD   Úclearr6   rF   r*   s    r   Úreload_promptszPromptManager.reload_prompts+  s,   € à�‰×ÑÔØ× Ò Ø×ÑÕ ð !r   r   c                 óF   — t        ||xs i |¬«      }|| j                  |<   y)z'Add a prompt template programmatically.rT   N)r   rD   )r%   r	   r   r   rJ   s        r   Ú
add_promptzPromptManager.add_prompt1  s#   € ä(°À8Â>ÈrÐ_hÔiˆØ"*ˆ�‰�YÒr   c                 óŠ   — t        |«      }|j                  d¬«      }| j                  |«      \  }}|j                  «       |dœS )zºConvert a .prompt file to JSON format.

        Args:
            file_path: Path to the .prompt file

        Returns:
            Dictionary with 'content' and 'metadata' keys
        rZ   r[   ©r   r   )r   r^   r_   r`   )r%   rX   r   ra   rb   s        r   Úprompt_file_to_jsonz!PromptManager.prompt_file_to_json6  sL   € ô ˜“Oˆ	Ø"×,Ñ,°gÐ,Ó>ˆð )-×(?Ñ(?ÀÓ(HÑ%ˆÐ%à+×1Ñ1Ó3ÀÑMÐMr   c                 ó’   — |j                  dd«      }|j                  di «      }|s|S ddl} |j                  |d¬«      }d|› d|› �S )	zÓConvert JSON prompt data to .prompt file format.

        Args:
            prompt_data: Dictionary with 'content' and 'metadata' keys

        Returns:
            String content in .prompt file format
        r   rS   r   r   NF)Údefault_flow_stylez---
)r    rj   Údump)r%   r7   r   r   rj   rn   s         r   Újson_to_prompt_filez!PromptManager.json_to_prompt_fileG  sY   € ð %Ÿ™¨°BÓ7ˆØ%Ÿ/™/¨*°bÓ9ˆáàˆNó 	à"+ $§)¡)¨HÈÔ"OÐàÐ'Ð(¨¨g¨YÐ7Ð7r   c                 ó†   — i }| j                   j                  «       D ]!  \  }}|j                  |j                  dœ||<   Œ# |S )zyGet all loaded prompts in JSON format.

        Returns:
            Dictionary mapping prompt_id to prompt data
        r¢   )rD   rU   r   r   )r%   Úresultr	   rJ   s       r   Úget_all_prompts_as_jsonz%PromptManager.get_all_prompts_as_json^  sO   € ð ˆØ#'§<¡<×#5Ñ#5Ó#7ò 	ÑˆI�xà#×+Ñ+Ø$×-Ñ-ñ!ˆF�9Òð	ð
 ˆr   c                 ó&   — | j                  |«       y)zFLoad additional prompts from JSON data (merges with existing prompts).N)rI   )r%   r7   s     r   Úload_prompts_from_json_dataz)PromptManager.load_prompts_from_json_datal  s   € à×$Ñ$ [Õ1r   )NNNN)r
   N)Nr,   )r-   r.   r/   r0   r1   r2   r   r(   rF   rI   r   r   rH   Útupler_   rŒ   ry   rw   r‚   r€   rt   rM   r™   r›   rž   r    r£   r§   rª   r¬   r3   r   r   r5   r5   1   s€  „ ñð !%Ø'+Ø8<Ø"&ñ&Aà˜‘:ð&Að  ™*ð&Að ˜#˜t C¨ H™~Ð-Ñ.°Ñ5ð	&Að
 ˜4‘Zó&AóPñ"#°4¸¸TÀ#ÀsÀ(¹^Ð8KÑ3Lð #ÐY\Ð_cÑYcð #Ðosó #ðJ
¨3°©:ð 
À#ð 
È.ó 
ð -¨#ð -°%¸¸SÀ#¸X¹ÈÐ8KÑ2Ló -ð0 37Ø"ñ	)Màð)Mð ˜s C˜x™.¨4Ñ/ð)Mð �t‘ð	)Mð
 
ó)MðV¨¨c°3¨h©ð ÀÀcÈ3ÀhÁð ÐTXó ð:¨Cð :°D¸5±Ló :ñ&L Cð L°#¸±*ð LÈÐY]ÑH]ó Lð0)˜d 3™ió )ð7¨Sð 7°T¸#¸s¸(±^ÀdÑ5Jó 7ó
!ñ+ Cð +°#ð +ÀÀcÈ3ÀhÁÐRVÑAVð +Ðbfó +ð
N¨S°4©Zð N¸DÀÀcÀ¹Nó Nð"8¨t°C¸°H©~ð 8À#ó 8ð.¨¨c°4¸¸S¸±>Ð.AÑ)Bó ð2°t¸CÀÀcÈ3ÀhÁÐ<OÑ7Pð 2ÐUYô 2r   r5   )r0   rf   Úpathlibr   Útypingr   r   rj   Újinja2r   r   Újinja2.sandboxr   r1   r   r   r5   r3   r   r   ú<module>r²      sM   ðñó 
Ý ß ã ß 0Ý 8ð Cð ¨C°$©Jó ÷Pñ P÷8}2ò }2r   