
    3fi                     r    d dl Z d dlmZ d dlmZmZ d dlmZ d dlm	Z	 d dl
mZ dedefd	Z G d
 de      Zy)    N)Literal)AgentActionAgentFinish)Field)override)AgentOutputParsertextreturnc                 l    ddddddd}|j                         D ]  \  }}| j                  ||      }  | S )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       i/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_classic/agents/output_parsers/xml.py	_unescaper      sP     (**,L #((* (
d||D$'(K    c                       e Zd ZU dZ ed      Zed   dz  ed<   	 ede	de
ez  fd       Zede	fd	       Zede	fd
       Zy)XMLAgentOutputParsera  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)defaultNescape_formatr	   r
   c                    t        j                  d|t         j                        }|rt        |      dk7  rdt        |       d}t	        |      |d   }t        j                  d|t         j                        }t        |      dkD  rdt        |       d}t	        |      |r|d   nd}| j
                  d	k(  rt        |      }t        |      }t        |||
      S d|v rrd|v rnt        j                  d|t         j                        }t        |      dk7  rd}t	        |      |d   }| j
                  d	k(  rt        |      }t        d|i|      S d}t	        |      )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   parsezXMLAgentOutputParser.parseE   s    zz"7ryyI< A%!!$\!2 316  !o% OE JJ14M =!A%((+M(:';1>  !o%.;-*K !!Y.!%('4EktLLt#(9T(Ajj!FbiiXG7|q >  !o%QZF!!Y."6*h-?TJJ/ 	 or   c                     t         )N)NotImplementedErrorr'   s    r   get_format_instructionsz,XMLAgentOutputParser.get_format_instructionsx   s    !!r   c                      y)Nz	xml-agent r2   s    r   _typezXMLAgentOutputParser._type|   s    r   )__name__
__module____qualname____doc__r   r   r   __annotations__r   strr   r   r/   r3   propertyr6   r5   r   r   r   r      s    B 05Y/GM79%,G 0# 0+"; 0 0d " " " s  r   r   )r"   typingr   langchain_core.agentsr   r   pydanticr   typing_extensionsr   langchain_classic.agentsr   r<   r   r   r5   r   r   <module>rC      s9    	  :  & 6C C d, dr   