§
    œŠtj+  ã                  óä   — d Z ddlmZ ddlmZmZmZ ddlmZ ddl	m
Z
mZ ddlmZmZmZ ddlmZ erddlmZmZ dd	lmZ dd
lmZ ddlmZ  G d„ deee         ef         ee         ¦  «        ZdS )z%Tool emulator middleware for testing.é    )Úannotations)ÚTYPE_CHECKINGÚAnyÚGeneric)ÚBaseChatModel)ÚHumanMessageÚToolMessage)ÚAgentMiddlewareÚ
AgentStateÚContextT)Úinit_chat_model)Ú	AwaitableÚCallable)ÚCommand)ÚToolCallRequest)ÚBaseToolc                  ó<   ‡ — e Zd ZdZdddœdˆ fd
„Zdd„Zdd„Zˆ xZS )ÚLLMToolEmulatora9  Emulates specified tools using an LLM instead of executing them.

    This middleware allows selective emulation of tools for testing purposes.

    By default (when `tools=None`), all tools are emulated. You can specify which
    tools to emulate by passing a list of tool names or `BaseTool` instances.

    Examples:
        !!! example "Emulate all tools (default behavior)"

            ```python
            from langchain.agents.middleware import LLMToolEmulator

            middleware = LLMToolEmulator()

            agent = create_agent(
                model="openai:gpt-5.5",
                tools=[get_weather, get_user_location, calculator],
                middleware=[middleware],
            )
            ```

        !!! example "Emulate specific tools by name"

            ```python
            middleware = LLMToolEmulator(tools=["get_weather", "get_user_location"])
            ```

        !!! example "Use a custom model for emulation"

            ```python
            middleware = LLMToolEmulator(
                tools=["get_weather"], model="anthropic:claude-sonnet-4-5-20250929"
            )
            ```

        !!! example "Emulate specific tools by passing tool instances"

            ```python
            middleware = LLMToolEmulator(tools=[get_weather, get_user_location])
            ```
    N)ÚtoolsÚmodelr   úlist[str | BaseTool] | Noner   ústr | BaseChatModel | NoneÚreturnÚNonec               óÖ  •— t          ¦   «                              ¦   «          |du | _        t          ¦   «         | _        | j        sV|�T|D ]Q}t          |t          ¦  «        r| j                             |¦  «         Œ2| j                             |j        ¦  «         ŒR|€t          dd¬¦  «        | _
        dS t          |t          ¦  «        r	|| _
        dS t          |d¬¦  «        | _
        dS )a¿  Initialize the tool emulator.

        Args:
            tools: List of tool names (`str`) or `BaseTool` instances to emulate.

                If `None`, ALL tools will be emulated.

                If empty list, no tools will be emulated.
            model: Model to use for emulation.

                Defaults to `'anthropic:claude-sonnet-4-5-20250929'`.

                Can be a model identifier string or `BaseChatModel` instance.
        Nz$anthropic:claude-sonnet-4-5-20250929é   )Útemperature)ÚsuperÚ__init__Úemulate_allÚsetÚtools_to_emulateÚ
isinstanceÚstrÚaddÚnamer   r   r   )Úselfr   r   ÚtoolÚ	__class__s       €úg/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/langchain/agents/middleware/tool_emulator.pyr   zLLMToolEmulator.__init__B   sô   ø€ õ( 	‰Œ×ÒÑÔÐð ! D˜=ˆÔÝ*-©%¬%ˆÔàÔð 	9 EÐ$5Øð 9ð 9�Ý˜d¥CÑ(Ô(ð 9ØÔ)×-Ò-¨dÑ3Ô3Ð3Ð3ð Ô)×-Ò-¨d¬iÑ8Ô8Ð8Ð8ð ˆ=Ý(Ð)OÐ]^Ð_Ñ_Ô_ˆDŒJˆJˆJÝ˜�}Ñ-Ô-ð 	?ØˆDŒJˆJˆJå(¨¸AÐ>Ñ>Ô>ˆDŒJˆJˆJó    Úrequestr   Úhandlerú7Callable[[ToolCallRequest], ToolMessage | Command[Any]]úToolMessage | Command[Any]c                óF  — |j         d         }| j        p|| j        v }|s ||¦  «        S |j         d         }|j        r|j        j        nd}d|› d|› d|› d�}| j                             t          |¦  «        g¦  «        }t          |j	        |j         d         |¬	¦  «        S )
a€  Emulate tool execution using LLM if tool should be emulated.

        Args:
            request: Tool call request to potentially emulate.
            handler: Callback to execute the tool (can be called multiple times).

        Returns:
            ToolMessage with emulated response if tool should be emulated,
                otherwise calls handler for normal execution.
        r&   ÚargsúNo description availableú;You are emulating a tool call for testing purposes.

Tool: ú
Description: ú
Arguments: ú¶

Generate a realistic response that this tool would return given these arguments.
Return ONLY the tool's output, no explanation or preamble. Introduce variation into your responses.Úid©ÚcontentÚtool_call_idr&   )
Ú	tool_callr    r"   r(   Údescriptionr   Úinvoker   r	   r9   ©	r'   r,   r-   Ú	tool_nameÚshould_emulateÚ	tool_argsÚtool_descriptionÚpromptÚresponses	            r*   Úwrap_tool_callzLLMToolEmulator.wrap_tool_callm   sî   € ð Ô% fÔ-ˆ	ð Ô)ÐO¨Y¸$Ô:OÐ-Oˆàð 	$à�7˜7Ñ#Ô#Ð#ð Ô% fÔ-ˆ	Ø7>´|Ðc˜7œ<Ô3Ð3ÐIcÐð8Øð8ð 8à,ð8ð 8ð $ð8ð 8ð 8ð 	ð ”:×$Ò$¥l°6Ñ&:Ô&:Ð%;Ñ<Ô<ˆõ ØÔ$Ø Ô*¨4Ô0Øð
ñ 
ô 
ð 	
r+   úBCallable[[ToolCallRequest], Awaitable[ToolMessage | Command[Any]]]c              ƒ  ób  K  — |j         d         }| j        p|| j        v }|s ||¦  «        ƒ d{V —†S |j         d         }|j        r|j        j        nd}d|› d|› d|› d�}| j                             t          |¦  «        g¦  «        ƒ d{V —†}t          |j	        |j         d	         |¬
¦  «        S )a²  Async version of `wrap_tool_call`.

        Emulate tool execution using LLM if tool should be emulated.

        Args:
            request: Tool call request to potentially emulate.
            handler: Async callback to execute the tool (can be called multiple times).

        Returns:
            ToolMessage with emulated response if tool should be emulated,
                otherwise calls handler for normal execution.
        r&   Nr1   r2   r3   r4   r5   r6   r7   r8   )
r;   r    r"   r(   r<   r   Úainvoker   r	   r9   r>   s	            r*   Úawrap_tool_callzLLMToolEmulator.awrap_tool_callŸ   s  è è € ð" Ô% fÔ-ˆ	ð Ô)ÐO¨Y¸$Ô:OÐ-Oˆàð 	*à ˜ Ñ)Ô)Ð)Ð)Ð)Ð)Ð)Ð)Ð)ð Ô% fÔ-ˆ	Ø7>´|Ðc˜7œ<Ô3Ð3ÐIcÐð8Øð8ð 8à,ð8ð 8ð $ð8ð 8ð 8ð 	ð œ×+Ò+­\¸&Ñ-AÔ-AÐ,BÑCÔCÐCÐCÐCÐCÐCÐCˆõ ØÔ$Ø Ô*¨4Ô0Øð
ñ 
ô 
ð 	
r+   )r   r   r   r   r   r   )r,   r   r-   r.   r   r/   )r,   r   r-   rF   r   r/   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   rE   rI   Ú__classcell__)r)   s   @r*   r   r      s„   ø€ € € € € ð)ð )ð\ .2Ø,0ð	)?ð )?ð )?ð )?ð )?ð )?ð )?ð )?ðV0
ð 0
ð 0
ð 0
ðd2
ð 2
ð 2
ð 2
ð 2
ð 2
ð 2
ð 2
r+   r   N)rM   Ú
__future__r   Útypingr   r   r   Ú*langchain_core.language_models.chat_modelsr   Úlangchain_core.messagesr   r	   Ú!langchain.agents.middleware.typesr
   r   r   Úlangchain.chat_models.baser   Úcollections.abcr   r   Úlanggraph.typesr   r   Úlangchain.toolsr   r   © r+   r*   ú<module>rY      s>  ðØ +Ð +à "Ð "Ð "Ð "Ð "Ð "à .Ð .Ð .Ð .Ð .Ð .Ð .Ð .Ð .Ð .à DÐ DÐ DÐ DÐ DÐ DØ =Ð =Ð =Ð =Ð =Ð =Ð =Ð =à SÐ SÐ SÐ SÐ SÐ SÐ SÐ SÐ SÐ SØ 6Ð 6Ð 6Ð 6Ð 6Ð 6àð )Ø3Ð3Ð3Ð3Ð3Ð3Ð3Ð3à'Ð'Ð'Ð'Ð'Ð'àAÐAÐAÐAÐAÐAØ(Ð(Ð(Ð(Ð(Ð(ð{
ð {
ð {
ð {
ð {
�o j°¤o°xÐ&?Ô@À'È(ÔBSñ {
ô {
ð {
ð {
ð {
r+   