o
    {qi                     @   sj   d dl Z d dlmZmZmZ d dlmZmZ d dlm	Z	 d dl
mZ dedefdd	ZG d
d deZdS )    N)LiteralOptionalUnion)AgentActionAgentFinish)Field)AgentOutputParsertextreturnc                 C   s4   ddddddd}|  D ]
\}}| ||} q| 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	   Zreplacementsreplorig r   j/home/app/PaddleOCR-VL/.venv_paddleocr/lib/python3.10/site-packages/langchain/agents/output_parsers/xml.py	_unescape
   s   r   c                   @   sh   e Zd ZU dZeddZeed  ed< 	 de	de
eef fddZde	fd	d
Zede	fddZdS )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)defaultescape_formatr	   r
   c           	      C   s  t d|t j}|rVt|dkrdt| d}t||d }t d|t j}t|dkr:dt| d}t||r@|d nd}| jd	krOt|}t|}t|||d
S d|v rd|v rt d|t j}t|dkrrd}t||d }| jd	krt|}td|i|d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   )ZtoolZ
tool_inputlogz<final_answer>z</final_answer>z"<final_answer>(.*?)</final_answer>zNMalformed output: expected exactly one <final_answer>...</final_answer> block.output)Z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	   Ztool_matchesmsgZ_toolZinput_matchesZ_tool_inputmatchesZanswerr   r   r   parseD   sH   

zXMLAgentOutputParser.parsec                 C   s   t )N)NotImplementedErrorr    r   r   r   get_format_instructionsv   s   z,XMLAgentOutputParser.get_format_instructionsc                 C   s   dS )Nz	xml-agentr   r%   r   r   r   _typey   s   zXMLAgentOutputParser._typeN)__name__
__module____qualname____doc__r   r   r   r   __annotations__strr   r   r   r#   r&   propertyr'   r   r   r   r   r      s   
 !2r   )r   typingr   r   r   Zlangchain_core.agentsr   r   Zpydanticr   Zlangchain.agentsr   r-   r   r   r   r   r   r   <module>   s    