o
    ß­j\  ã                   @  sÌ   d Z ddlmZ ddlmZmZ ddlmZ ddlmZ ddlm	Z	 ddlm
Z
 ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ G dd„ dƒZddd„ZdS )zMagicsEngine class.é    )Úannotations)ÚAbstractSetÚSequence)Úargument_parser)Úcmd_line_parser)Úcommand)Úcompare_cmd)Úcompile_cmd)Úeval_cmd)Úipython_env)Úmodel_registry)Úparsed_args_lib)Úpost_process_utils)Úrun_cmd)Úprompt_utilsc                   @  s>   e Zd ZdZ		dddd„Zddd„Zddd„Zddd„ZdS )ÚMagicsEnginezÂImplementation of functionality used by Magics.

    This class provides the implementation for Magics, decoupled from the
    details of integrating with Colab Magics such as registration.
    NÚregistryú#model_registry.ModelRegistry | NoneÚenvúipython_env.IPythonEnv | Nonec                 C  sb   || _ |pt ¡ }tjjtj||d�tjjt	j
||d�tjjtj|d�tjjtj||d�i| _d S )N)Úmodelsr   )r   )Ú_ipython_envr   ÚModelRegistryr   ÚCommandNameÚRUN_CMDr   Ú
RunCommandÚCOMPILE_CMDr	   ÚCompileCommandÚCOMPARE_CMDr   ÚCompareCommandÚEVAL_CMDr
   ÚEvalCommandÚ_cmd_handlers)Úselfr   r   r   © r$   úg/var/www/html/CropPilot/venv/lib/python3.10/site-packages/google/generativeai/notebook/magics_engine.pyÚ__init__)   s   
ÿ
úzMagicsEngine.__init__ÚlineÚstrÚplaceholdersúAbstractSet[str]ÚreturnúGtuple[parsed_args_lib.ParsedArgs, parsed_args_lib.PostProcessingTokens]c                 C  s   t  ¡  ||¡S )N)r   ÚCmdLineParserÚ
parse_line)r#   r'   r)   r$   r$   r%   r.   9   s   zMagicsEngine.parse_lineúftuple[command.Command, parsed_args_lib.ParsedArgs, Sequence[post_process_utils.ParsedPostProcessExpr]]c                 C  s4   |   ||¡\}}|j}| j| }| |¡}|||fS )a}  Given the command line, parse and return all components.

        Args:
          line: The LLM Magics command line.
          placeholders: Placeholders from prompts in the cell contents.

        Returns:
          A three-tuple containing:
          - The command (e.g. "run")
          - Parsed arguments for the command,
          - Parsed post-processing expressions
        )r.   Úcmdr"   Úparse_post_processing_tokens)r#   r'   r)   Úparsed_argsÚpost_processing_tokensÚcmd_nameÚhandlerÚpost_processing_fnsr$   r$   r%   Ú_get_handler@   s
   


zMagicsEngine._get_handlerÚcell_contentc           
   
   C  sÂ   t |ƒ}t |¡}z|  ||¡\}}}| |||¡W S  tjy: } z| jdur.| | j¡ |W  Y d}~S d}~w tj	y` } z| 
| j¡ | ¡ rUd | ¡ d ¡nd}	t|	ƒ|‚d}~ww )z2Executes the supplied magic line and cell payload.NzGot parser error: {}éÿÿÿÿÚ )Ú_clean_cellr   Úget_placeholdersr7   Úexecuter   ÚParserNormalExitr   Úset_ipython_envÚParserErrorÚdisplayÚmsgsÚformatÚ
SystemExit)
r#   r'   r8   Úcellr)   r5   r2   r6   ÚeÚ	error_msgr$   r$   r%   Úexecute_cellW   s    

€

€ôzMagicsEngine.execute_cell)NN)r   r   r   r   )r'   r(   r)   r*   r+   r,   )r'   r(   r)   r*   r+   r/   )r'   r(   r8   r(   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r&   r.   r7   rH   r$   r$   r$   r%   r   "   s    ý

r   r8   r(   r+   c                 C  s   | }|  d¡r|d d… }|S )NÚ
r9   )Úendswith)r8   rE   r$   r$   r%   r;   t   s   
r;   N)r8   r(   r+   r(   )rL   Ú
__future__r   Útypingr   r   Úgoogle.generativeai.notebookr   r   r   r   r	   r
   r   r   r   r   r   Ú google.generativeai.notebook.libr   r   r;   r$   r$   r$   r%   Ú<module>   s"   R