a
    bgN                     @  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   q/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/tools/json/tool.py
<listcomp>       z _parse_input.<locals>.<listcomp>c                 S  s    g | ]}|  rt|n|qS r   )isdigitintr   r   r   r   r      r   )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y| } zt	|W  Y d}~S d}~0 0 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   r3   +   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y } zt|W  Y d}~S d}~0 0 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/   r0   lenr   r&   r4   r5   )r6   r   r7   r8   r   Zstr_valr9   r   r   r   value?   s    
zJsonSpec.valueN)
__name__
__module____qualname____doc____annotations__r&   classmethodr.   r3   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)rE   r3   r6   rH   rI   r   r   r   _run`   s    zJsonListKeysTool._run(Optional[AsyncCallbackManagerForToolRun]c                   s
   |  |S rJ   rL   rK   r   r   r   _arung   s    zJsonListKeysTool._arun)N)N	r<   r=   r>   r?   rC   r@   rD   rL   rO   r   r   r   r   rB   U   s   
 
 rB   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   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   c                 C  s   | j |S rJ   )rE   r;   rK   r   r   r   rL   z   s    zJsonGetValueTool._runrM   c                   s
   |  |S rJ   rN   rK   r   r   r   rO      s    zJsonGetValueTool._arun)N)NrP   r   r   r   r   rQ   o   s   
 
 rQ   )r?   
__future__r   r*   r   pathlibr   typingr   r   r   r   Zpydanticr   Zlangchain_core.callbacksr	   r
   Zlangchain_core.toolsr   r"   r#   rB   rQ   r   r   r   r   <module>   s   	8