
    dh                        S r SSKJr  SSKrSSKrSSKJr  SSKJrJ	r	J
r
Jr  SSKJr  SSKJrJr  SSKJr  SS	 jr " S
 S\5      r " S S\5      r " S S\5      rg)z"Tools for working with JSON specs.    )annotationsN)Path)DictListOptionalUnion)	BaseModel)AsyncCallbackManagerForToolRunCallbackManagerForToolRun)BaseToolc                   [         R                  " SU 5      nU Vs/ sH'  o"SS R                  SS5      R                  SS5      PM)     nnU Vs/ sH$  o"R                  5       (       a  [	        U5      OUPM&     nnU$ s  snf s  snf )zDParse input of the form data["key1"][0]["key2"] into a list of keys.z\[.*?]   " ')refindallreplaceisdigitint)text_resiress       [/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/tools/json/tool.py_parse_inputr      su    ::i&D>B
CdQr7??3#++C4dC
C14
5AYY[[3q6a'C
5J D
5s   -A=*Bc                  Z    \ rS rSr% SrS\S'   SrS\S'   \SS j5       rSS	 jr	SS
 jr
Srg)JsonSpec   zBase class for JSON spec.r   dict_   r   max_value_lengthc                    UR                  5       (       d  [        SU 35      e[        R                  " UR	                  5       5      nU " US9$ )zCreate a JsonSpec from a file.zFile not found: )r!   )existsFileNotFoundErrorjsonloads	read_text)clspathr!   s      r   	from_fileJsonSpec.from_file#   sB     {{}}#&6tf$=>>

4>>+,    c                .    [        U5      nU R                  nU H  nU(       d  M  X4   nM     [        U[        5      (       d  [	        SU S35      e[        [        UR                  5       5      5      $ ! [         a  n[        U5      s SnA$ SnAff = f)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   r5   JsonSpec.keys+   s    	 &E**C1&C  c4(( %dV+ST  tCHHJ'(( 	7N	s#   !A5 AA5 5
B?
B	BBc                p    [        U5      nU R                  nU H  nX4   nM	     [        U[        5      (       a#  [	        [        U5      5      U R                  :  a  g[        U5      n[	        U5      U R                  :  a  USU R                   S-   nU$ ! [         a  n[        U5      s SnA$ SnAff = f)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!   r0   r1   lenr3   r#   r6   r7   )r8   r   r9   r:   r   str_valr;   s          r   value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 7B 
B5 
B0*B50B5 N)r+   r   returnr   )r   r3   rC   r3   )__name__
__module____qualname____firstlineno____doc____annotations__r#   classmethodr,   r5   r@   __static_attributes__rB   r.   r   r   r      s1    #Kc   (r.   r   c                  t    \ rS rSr% SrSrS\S'   SrS\S'   S\S	'    S     SS jjr S     SS jjr	Sr
g
)JsonListKeysToolU   z%Tool for listing keys in a JSON spec.json_spec_list_keysr3   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    U R                   R                  U5      $ N)rR   r5   r8   
tool_inputrun_managers      r   _runJsonListKeysTool._run`   s    
 yy~~j))r.   c                ,   #    U R                  U5      $ 7frT   rX   rU   s      r   _arunJsonListKeysTool._arung        
 yy$$   rB   rT   rV   r3   rW   z#Optional[CallbackManagerForToolRun]rC   r3   rV   r3   rW   z(Optional[AsyncCallbackManagerForToolRun]rC   r3   rD   rE   rF   rG   rH   rP   rI   rQ   rX   r\   rK   rB   r.   r   rM   rM   U   sx    /%D#%K 
 N
 <@** 9* 
	* AE%% >% 
	% %r.   rM   c                  t    \ rS rSr% SrSrS\S'   SrS\S'   S\S	'    S     SS jjr S     SS jjr	Sr
g
)JsonGetValueToolo   z(Tool for getting a value in a JSON spec.json_spec_get_valuer3   rP   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"]).
    rQ   r   rR   Nc                8    U R                   R                  U5      $ rT   )rR   r@   rU   s      r   rX   JsonGetValueTool._runz   s    
 yyz**r.   c                ,   #    U R                  U5      $ 7frT   r[   rU   s      r   r\   JsonGetValueTool._arun   r^   r_   rB   rT   r`   ra   rb   rB   r.   r   rd   rd   o   sx    2%D#%K 
 N
 <@++ 9+ 
	+ AE%% >% 
	% %r.   rd   )r   r3   rC   zList[Union[str, int]])rH   
__future__r   r'   r   pathlibr   typingr   r   r   r   pydanticr	   langchain_core.callbacksr
   r   langchain_core.toolsr   r   r   rM   rd   rB   r.   r   <module>rq      sQ    ( "  	  . .  *5y 5p%x %4%x %r.   