U
    h,                     @  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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strzList[Union[str, int]]textreturnc                 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   G/tmp/pip-unpacked-wheel-9gdii04g/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                   @  sZ   e Zd ZU dZded< dZded< edd dd	d
ZdddddZdddddZ	dS )JsonSpeczBase class for JSON spec.r   dict_   r   max_value_lengthr   )pathr   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   zPt |}| j}|D ]}|r|| }qt|ts@td| dtt| W S  tk
r~ } zt	| W Y S d}~X Y nX dS )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   zpt |}| j}|D ]}|| }qt|trDtt|| jkrDW dS t|}t|| jkrl|d| j d }|W S  tk
r } zt| W Y S d}~X Y nX dS )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)
__name__
__module____qualname____doc____annotations__r%   classmethodr-   r2   r:   r   r   r   r   r"      s   
r"   c                   @  sZ   e Zd ZU dZdZded< dZded< ded< ddd
ddddZddd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#Optional[CallbackManagerForToolRun]
tool_inputrun_managerr   c                 C  s   | j |S N)rD   r2   r5   rG   rH   r   r   r   _run`   s    zJsonListKeysTool._run(Optional[AsyncCallbackManagerForToolRun]c                   s
   |  |S rI   rK   rJ   r   r   r   _arung   s    zJsonListKeysTool._arun)N)N	r;   r<   r=   r>   rB   r?   rC   rK   rN   r   r   r   r   rA   U   s   
 
 rA   c                   @  sZ   e Zd ZU dZdZded< dZded< ded< ddd
ddddZdddddddZd	S )JsonGetValueToolz(Tool for getting a value in a JSON spec.Zjson_spec_get_valuer   rB   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"]).
    rC   r"   rD   NrE   rF   c                 C  s   | j |S rI   )rD   r:   rJ   r   r   r   rK   z   s    zJsonGetValueTool._runrL   c                   s
   |  |S rI   rM   rJ   r   r   r   rN      s    zJsonGetValueTool._arun)N)NrO   r   r   r   r   rP   o   s   
 
 rP   )r>   
__future__r   r)   r   pathlibr   typingr   r   r   r   Zlangchain_core.pydantic_v1r   Zlangchain_core.callbacksr	   r
   Zlangchain_core.toolsr   r!   r"   rA   rP   r   r   r   r   <module>   s   	8