o
    ZhN                     @  s   d Z ddlmZ ddlZddlZddlmZ ddlmZm	Z	m
Z
mZ ddlmZ ddlmZmZ ddlmZ dddZG dd deZG dd deZG dd deZdS )z"Tools for working with JSON specs.    )annotationsN)Path)DictListOptionalUnion)	BaseModel)AsyncCallbackManagerForToolRunCallbackManagerForToolRun)BaseTooltextstrreturnList[Union[str, int]]c                 C  s,   t d| }dd |D }dd |D }|S )zDParse input of the form data["key1"][0]["key2"] into a list of keys.z\[.*?]c                 S  s(   g | ]}|d d  dd ddqS )   " ')replace.0i r   Z/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/tools/json/tool.py
<listcomp>   s   ( z _parse_input.<locals>.<listcomp>c                 S  s    g | ]}|  rt|n|qS r   )isdigitintr   r   r   r   r      s     )refindall)r   Z_resresr   r   r   _parse_input   s   r!   c                   @  sH   e Zd ZU dZded< dZded< edd
dZdddZdddZ	dS )JsonSpeczBase class for JSON spec.r   dict_   r   max_value_lengthpathr   r   c                 C  s.   |  std| t| }| |dS )zCreate a JsonSpec from a file.zFile not found: )r#   )existsFileNotFoundErrorjsonloads	read_text)clsr&   r#   r   r   r   	from_file#   s   
zJsonSpec.from_filer   r   c              
   C  s~   z(t |}| j}|D ]}|r|| }q
t|ts td| dtt| W S  ty> } z
t	|W  Y d}~S d}~ww )zReturn the keys of the dict at the given path.

        Args:
            text: Python representation of the path to the dict (e.g. data["key1"][0]["key2"]).
        zValue at path `z(` is not a dict, get the value directly.N)
r!   r#   
isinstancedict
ValueErrorr   listkeys	Exceptionrepr)selfr   itemsvalr   er   r   r   r2   +   s    

zJsonSpec.keysc              
   C  s   z8t |}| j}|D ]}|| }q
t|tr"tt|| jkr"W dS t|}t|| jkr6|d| j d }|W S  tyN } z
t|W  Y d}~S d}~ww )zReturn the value of the dict at the given path.

        Args:
            text: Python representation of the path to the dict (e.g. data["key1"][0]["key2"]).
        z=Value is a large dictionary, should explore its keys directlyNz...)	r!   r#   r.   r/   lenr   r%   r3   r4   )r5   r   r6   r7   r   Zstr_valr8   r   r   r   value?   s   
zJsonSpec.valueN)r&   r   r   r"   )r   r   r   r   )
__name__
__module____qualname____doc____annotations__r%   classmethodr-   r2   r:   r   r   r   r   r"      s   
 
r"   c                   @  N   e Zd ZU dZdZded< dZded< ded< 		ddddZ		ddddZd	S )JsonListKeysToolz%Tool for listing keys in a JSON spec.Zjson_spec_list_keysr   namez
    Can be used to list all keys at a given path. 
    Before calling this you should be SURE that the path to this exists.
    The input is a text representation of the path to the dict in Python syntax (e.g. data["key1"][0]["key2"]).
    descriptionr"   specN
tool_inputrun_manager#Optional[CallbackManagerForToolRun]r   c                 C     | j |S N)rE   r2   r5   rF   rG   r   r   r   _run`      zJsonListKeysTool._run(Optional[AsyncCallbackManagerForToolRun]c                      |  |S rJ   rL   rK   r   r   r   _arung      
zJsonListKeysTool._arunrJ   rF   r   rG   rH   r   r   rF   r   rG   rN   r   r   	r;   r<   r=   r>   rC   r?   rD   rL   rQ   r   r   r   r   rB   U      
 
rB   c                   @  rA   )JsonGetValueToolz(Tool for getting a value in a JSON spec.Zjson_spec_get_valuer   rC   z
    Can be used to see value in string format at a given path.
    Before calling this you should be SURE that the path to this exists.
    The input is a text representation of the path to the dict in Python syntax (e.g. data["key1"][0]["key2"]).
    rD   r"   rE   NrF   rG   rH   r   c                 C  rI   rJ   )rE   r:   rK   r   r   r   rL   z   rM   zJsonGetValueTool._runrN   c                   rO   rJ   rP   rK   r   r   r   rQ      rR   zJsonGetValueTool._arunrJ   rS   rT   rU   r   r   r   r   rW   o   rV   rW   )r   r   r   r   )r>   
__future__r   r)   r   pathlibr   typingr   r   r   r   Zpydanticr   Zlangchain_core.callbacksr	   r
   Zlangchain_core.toolsr   r!   r"   rB   rW   r   r   r   r   <module>   s   
	8