U
    hQ                     @   s   d dl Z d dlZd dlZd dlmZmZmZmZmZ d dl	m
Z
 d dlmZmZmZ d dlmZ e eZG dd deZedd	d
ZedddZG dd deZdS )    N)AnyListOptionalTypeUnion)CallbackManagerForToolRun)	BaseModelFieldroot_validator)BaseToolc                   @   sL   e Zd ZU dZedddZeeee f e	d< e
ddeedd	d
ZdS )
ShellInputz!Commands for the Bash Shell tool..z<List of shell commands to run. Deserialized using json.loads)descriptioncommandsT)pre)valuesreturnc                 C   s,   | d}t|ts|g|d< td |S )zValidate commands.r   zBThe shell tool has no safeguards by default. Use at your own risk.)get
isinstancelistwarningswarn)clsr   r    r   H/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/tools/shell/tool.py_validate_commands   s    


zShellInput._validate_commandsN)__name__
__module____qualname____doc__r	   r   r   strr   __annotations__r
   dictr   r   r   r   r   r      s   
r   )r   c                  C   s8   zddl m}  W n tk
r,   tdY nX | ddS )zGet default bash process.r   BashProcesszBashProcess has been moved to langchain experimental.To use this tool, install langchain-experimental with `pip install langchain-experimental`.T)Zreturn_err_output)Z$langchain_experimental.llm_bash.bashr#   ImportErrorr"   r   r   r   _get_default_bash_process&   s    
r%   c                  C   s   t  } | dkrdS | S )zGet platform.DarwinZMacOS)platformsystem)r(   r   r   r   _get_platform3   s    r)   c                   @   s   e Zd ZU dZeedZeed< dZ	e
ed< de  dZe
ed< eZee ed	< d
Zeed< dee
ee
 f ee e
dddZdS )	ShellToolzTool to run shell commands.)default_factoryprocessZterminalnamezRun shell commands on this z	 machine.r   args_schemaFask_human_inputN)r   run_managerr   c              
   C   s   t d|  zL| jrJtd }|dkr8| j|W S td W dS n| j|W S W n8 tk
r } zt	d|  W Y dS d}~X Y nX dS )z%Run commands and return final output.zExecuting command:
 z'Proceed with command execution? (y/n): yz.Invalid input. User aborted command execution.Nz Error during command execution: )
printr/   inputlowerr,   runloggerinfo	Exceptionerror)selfr   r0   Z
user_inputer   r   r   _runP   s    
zShellTool._run)N)r   r   r   r   r	   r%   r,   r   r    r-   r   r)   r   r   r.   r   r   r/   boolr   r   r   r   r<   r   r   r   r   r*   ;   s   
	 r*   )loggingr'   r   typingr   r   r   r   r   Zlangchain_core.callbacksr   Zlangchain_core.pydantic_v1r   r	   r
   Zlangchain_core.toolsr   	getLoggerr   r6   r   r%   r   r)   r*   r   r   r   r   <module>   s   
