
    ihN                        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y)z"Tools for working with JSON specs.    )annotationsN)Path)DictListOptionalUnion)	BaseModel)AsyncCallbackManagerForToolRunCallbackManagerForToolRun)BaseToolc                    t        j                  d|       }|D cg c]'  }|dd j                  dd      j                  dd      ) }}|D cg c]  }|j                         rt	        |      n|! }}|S c c}w c c}w )zDParse input of the form data["key1"][0]["key2"] into a list of keys.z\[.*?]   " ')refindallreplaceisdigitint)text_resiress       a/var/www/html/dev/engine/venv/lib/python3.12/site-packages/langchain_community/tools/json/tool.py_parse_inputr      su    ::i&D>B
C1Qr7??3#++C4
CC
C14
5AQYY[3q6a'
5C
5J D
5s   ,A5$A:c                  L    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	y
)JsonSpeczBase class for JSON spec.r   dict_   r   max_value_lengthc                    |j                         st        d|       t        j                  |j	                               } | |      S )zCreate a JsonSpec from a file.zFile not found: )r    )existsFileNotFoundErrorjsonloads	read_text)clspathr    s      r   	from_filezJsonSpec.from_file#   s@     {{}#&6tf$=>>

4>>+,    c                   	 t        |      }| j                  }|D ]
  }|s||   } t        |t              st	        d| d      t        t        |j                                     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
ValueErrorstrlistkeys	Exceptionrepr)selfr   itemsvalr   es         r   r3   zJsonSpec.keys+   s    	 &E**C !a&C! c4( %dV+ST  tCHHJ'(( 	7N	s#   A) AA) )	B2
B<BBc                ^   	 t        |      }| j                  }|D ]  }||   }	 t        |t              r"t	        t        |            | j                  kD  ryt        |      }t	        |      | j                  kD  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    r.   r/   lenr1   r"   r4   r5   )r6   r   r7   r8   r   str_valr9   s          r   valuezJsonSpec.value?   s    	 &E**C !f #t$SX9N9N)NV#hG7|d333!"9D$9$9:UBN 	7N	s$   AB 6B 	B,
B'!B,'B,N)r*   r   returnr   )r   r1   r>   r1   )
__name__
__module____qualname____doc____annotations__r"   classmethodr+   r3   r=    r,   r   r   r      s1    #Kc   (r,   r   c                  d    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y	)JsonListKeysToolz%Tool for listing keys in a JSON spec.json_spec_list_keysr1   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   specNc                8    | j                   j                  |      S N)rK   r3   r6   
tool_inputrun_managers      r   _runzJsonListKeysTool._run`   s    
 yy~~j))r,   c                ,   K   | j                  |      S wrM   rQ   rN   s      r   _arunzJsonListKeysTool._arung        
 yy$$   rM   rO   r1   rP   z#Optional[CallbackManagerForToolRun]r>   r1   rO   r1   rP   z(Optional[AsyncCallbackManagerForToolRun]r>   r1   	r?   r@   rA   rB   rI   rC   rJ   rQ   rT   rE   r,   r   rG   rG   U   ss    /%D#%K 
 N
 <@** 9* 
	* AE%% >% 
	%r,   rG   c                  d    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y	)JsonGetValueToolz(Tool for getting a value in a JSON spec.json_spec_get_valuer1   rI   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"]).
    rJ   r   rK   Nc                8    | j                   j                  |      S rM   )rK   r=   rN   s      r   rQ   zJsonGetValueTool._runz   s    
 yyz**r,   c                ,   K   | j                  |      S wrM   rS   rN   s      r   rT   zJsonGetValueTool._arun   rU   rV   rM   rW   rX   rY   rE   r,   r   r[   r[   o   ss    2%D#%K 
 N
 <@++ 9+ 
	+ AE%% >% 
	%r,   r[   )r   r1   r>   zList[Union[str, int]])rB   
__future__r   r&   r   pathlibr   typingr   r   r   r   pydanticr	   langchain_core.callbacksr
   r   langchain_core.toolsr   r   r   rG   r[   rE   r,   r   <module>re      sQ    ( "  	  . .  *5y 5p%x %4%x %r,   