
    NgN                        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Z G d de          Z G d de          Z G 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                `    t          j        d|           }d |D             }d |D             }|S )zDParse input of the form data["key1"][0]["key2"] into a list of keys.z\[.*?]c                r    g | ]4}|d d                              dd                               dd          5S )   " ')replace.0is     _/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/tools/json/tool.py
<listcomp>z _parse_input.<locals>.<listcomp>   s>    
C
C
C1QrT7??3##++C44
C
C
C    c                X    g | ]'}|                                 rt          |          n|(S  )isdigitintr   s     r   r   z _parse_input.<locals>.<listcomp>   s/    
5
5
5AQYY[['3q666a
5
5
5r   )refindall)r   _resress      r   _parse_inputr'      s>    :i&&D
C
Cd
C
C
CC
5
5
5
5
5CJr   c                  T    e Zd ZU dZded<   dZded<   edd
            ZddZddZ	dS )JsonSpeczBase class for JSON spec.r   dict_   r"   max_value_lengthpathr   r   c                    |                                 st          d|           t          j        |                                          } | |          S )zCreate a JsonSpec from a file.zFile not found: )r*   )existsFileNotFoundErrorjsonloads	read_text)clsr-   r*   s      r   	from_filezJsonSpec.from_file#   sW     {{}} 	?#$=t$=$=>>>
4>>++,,sr   r   r   c                F   	 t          |          }| j        }|D ]}|r||         }t          |t                    st	          d| d          t          t          |                                                    S # t          $ r}t          |          cY d}~S d}~ww xY 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   es         r   r;   zJsonSpec.keys+   s    	 &&E*C ! ! !a&Cc4((  TdTTT   tCHHJJ''((( 	 	 	77NNNNNN	s   A:A= =
B BB B c                   	 t          |          }| j        }|D ]
}||         }t          |t                    r't	          t          |                    | j        k    rdS t          |          }t	          |          | j        k    r|d| j                 dz   }|S # t          $ r}t          |          cY d}~S d}~ww xY 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*   r7   r8   lenr   r,   r<   r=   )r>   r   r?   r@   r   str_valrA   s          r   valuezJsonSpec.value?   s    	 &&E*C  !f#t$$ WSXX9N)N)NVV#hhG7||d333!"9D$9"9:UBN 	 	 	77NNNNNN	s$   AB !:B 
B?&B:4B?:B?N)r-   r   r   r)   )r   r   r   r   )
__name__
__module____qualname____doc____annotations__r,   classmethodr5   r;   rE   r    r   r   r)   r)      s}         ##KKK      [    (     r   r)   c                  R    e Zd ZU dZdZded<   dZded<   ded<   	 dddZ	 dddZd	S )JsonListKeysToolz%Tool for listing keys in a JSON spec.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                6    | j                             |          S N)rQ   r;   r>   rR   rS   s      r   _runzJsonListKeysTool._run`   s    
 y~~j)))r   (Optional[AsyncCallbackManagerForToolRun]c                0   K   |                      |          S rV   rX   rW   s      r   _arunzJsonListKeysTool._arung         
 yy$$$r   rV   rR   r   rS   rT   r   r   rR   r   rS   rY   r   r   	rF   rG   rH   rI   rO   rJ   rP   rX   r\   r    r   r   rM   rM   U   s         //%D%%%%K    
 NNN
 <@* * * * * AE% % % % % % %r   rM   c                  R    e Zd ZU dZdZded<   dZded<   ded<   	 dddZ	 dddZd	S )JsonGetValueToolz(Tool for getting a value in a JSON spec.json_spec_get_valuer   rO   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"]).
    rP   r)   rQ   NrR   rS   rT   r   c                6    | j                             |          S rV   )rQ   rE   rW   s      r   rX   zJsonGetValueTool._runz   s    
 yz***r   rY   c                0   K   |                      |          S rV   r[   rW   s      r   r\   zJsonGetValueTool._arun   r]   r   rV   r^   r_   r`   r    r   r   rb   rb   o   s         22%D%%%%K    
 NNN
 <@+ + + + + AE% % % % % % %r   rb   )r   r   r   r   )rI   
__future__r   r1   r#   pathlibr   typingr   r   r   r   pydanticr	   langchain_core.callbacksr
   r   langchain_core.toolsr   r'   r)   rM   rb   r    r   r   <module>rl      sP   ( ( " " " " " "  				       . . . . . . . . . . . .              * ) ) ) ) )   5 5 5 5 5y 5 5 5p% % % % %x % % %4% % % % %x % % % % %r   