
    @h                     p    S SK r S SKJrJrJr  S SKJrJr  S SKJ	r	  S SK
Jr  S\S\4S jr " S	 S
\5      rg)    N)LiteralOptionalUnion)AgentActionAgentFinish)Field)AgentOutputParsertextreturnc                 l    SSSSSSS.nUR                  5        H  u  p#U R                  X#5      n M     U $ )z1Convert custom tag delimiters back into XML tags.z<tool>z</tool>z<tool_input>z</tool_input>z<observation>z</observation>)z[[tool]]z	[[/tool]]z[[tool_input]]z[[/tool_input]]z[[observation]]z[[/observation]])itemsreplace)r
   replacementsreplorigs       [/var/www/html/shao/venv/lib/python3.13/site-packages/langchain/agents/output_parsers/xml.py	_unescaper   
   sG     (**,L #((*
||D' +K    c                       \ rS rSr% Sr\" SS9r\\S      \	S'    S\
S\\\4   4S jrS\
4S	 jr\S\
4S
 j5       rSrg)XMLAgentOutputParser   a1  Parses tool invocations and final answers from XML-formatted agent output.

This parser extracts structured information from XML tags to determine whether
an agent should perform a tool action or provide a final answer. It includes
built-in escaping support to safely handle tool names and inputs
containing XML special characters.

Args:
    escape_format: The escaping format to use when parsing XML content.
        Supports 'minimal' which uses custom delimiters like [[tool]] to replace
        XML tags within content, preventing parsing conflicts.
        Use 'minimal' if using a corresponding encoding format that uses
        the _escape function when formatting the output (e.g., with format_xml).

Expected formats:
    Tool invocation (returns AgentAction):
        <tool>search</tool>
        <tool_input>what is 2 + 2</tool_input>

    Final answer (returns AgentFinish):
        <final_answer>The answer is 4</final_answer>

Note:
    Minimal escaping allows tool names containing XML tags to be safely
    represented. For example, a tool named "search<tool>nested</tool>" would be
    escaped as "search[[tool]]nested[[/tool]]" in the XML and automatically
    unescaped during parsing.

Raises:
    ValueError: If the input doesn't match either expected XML format or
        contains malformed XML structure.
minimal)defaultescape_formatr
   r   c                    [         R                  " SU[         R                  5      nU(       a  [        U5      S:w  a  S[        U5       S3n[	        U5      eUS   n[         R                  " SU[         R                  5      n[        U5      S:  a  S[        U5       S3n[	        U5      eU(       a  US   OSnU R
                  S	:X  a  [        U5      n[        U5      n[        XFUS
9$ SU;   at  SU;   an  [         R                  " SU[         R                  5      n[        U5      S:w  a  Sn[	        U5      eUS   nU R
                  S	:X  a  [        U5      n[        SU0US9$ Sn[	        U5      e)Nz<tool>(.*?)</tool>   zHMalformed tool invocation: expected exactly one <tool> block, but found .r   z<tool_input>(.*?)</tool_input>zNMalformed tool invocation: expected at most one <tool_input> block, but found  r   )tool
tool_inputlogz<final_answer>z</final_answer>z"<final_answer>(.*?)</final_answer>zNMalformed output: expected exactly one <final_answer>...</final_answer> block.output)return_valuesr!   zTMalformed output: expected either a tool invocation or a final answer in XML format.)	refindallDOTALLlen
ValueErrorr   r   r   r   )	selfr
   tool_matchesmsg_toolinput_matches_tool_inputmatchesanswers	            r   parseXMLAgentOutputParser.parseD   s}   zz"7ryyI< A%!!$\!2 316  !o% OE JJ14M =!A%((+M(:';1>  !o%.;-*K !!Y.!%('4EtLLt#(9T(Ajj!FbiiXG7|q >  !o%QZF!!Y."6*h-?TJJ/ 	 or   c                     [         e)N)NotImplementedErrorr)   s    r   get_format_instructions,XMLAgentOutputParser.get_format_instructionsv   s    !!r   c                     g)Nz	xml-agent r5   s    r   _typeXMLAgentOutputParser._typey   s    r   r9   N)__name__
__module____qualname____firstlineno____doc__r   r   r   r   __annotations__strr   r   r   r1   r6   propertyr:   __static_attributes__r9   r   r   r   r      sm    B 38	2JM8GI./J0# 0%[(@"A 0d" " s  r   r   )r$   typingr   r   r   langchain_core.agentsr   r   pydanticr   langchain.agentsr	   rB   r   r   r9   r   r   <module>rI      s9    	 + + :  .C C b, br   