a
    •˜ëjù  ã                   @   st   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 dlmZ eedœdd	„ZG d
d„ deƒZdS )é    N)ÚLiteralÚOptionalÚUnion)ÚAgentActionÚAgentFinish)ÚField)Úoverride)Ú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   ÚreplacementsÚreplÚorig© r   úb/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/langchain/agents/output_parsers/xml.pyÚ	_unescape   s    úr   c                   @   sl   e Zd ZU dZedd�Zeed  ed< e	e
eeef dœdd„ƒZe	e
dœd	d
„ƒZee
dœ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
   c           	      C   s$  t  d|t j¡}|r¬t|ƒdkr8dt|ƒ› d�}t|ƒ‚|d }t  d|t j¡}t|ƒdkrtdt|ƒ› d�}t|ƒ‚|r€|d nd}| jd	kržt|ƒ}t|ƒ}t|||d
�S d|v �rd|v �rt  d|t j¡}t|ƒdkrèd}t|ƒ‚|d }| jd	k�rt|ƒ}td|i|d�S d}t|ƒ‚d S )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   ÚparseE   sH    ÿÿÿÿÿ
ÿÿzXMLAgentOutputParser.parse)r   c                 C   s   t ‚d S )N)ÚNotImplementedError©r#   r   r   r   Úget_format_instructionsx   s    z,XMLAgentOutputParser.get_format_instructionsc                 C   s   dS )Nz	xml-agentr   r(   r   r   r   Ú_type|   s    zXMLAgentOutputParser._typeN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   Ú__annotations__r   Ú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typing_extensionsr   Zlangchain.agentsr	   r0   r   r   r   r   r   r   Ú<module>   s   