
    g3fi                        d Z ddlmZ ddlmZ ddlmZ ddlmZm	Z	 ddl
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      Zy)z%Tool emulator middleware for testing.    )annotations)TYPE_CHECKING)BaseChatModel)HumanMessageToolMessage)AgentMiddleware)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 )
LLMToolEmulatora8  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-4o",
                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modelc                  t         |           |du | _        t               | _        | j                  sZ|X|D ]S  }t        |t              r| j                  j                  |       /| j                  j                  |j                         U |t        dd      | _
        yt        |t              r|| _
        yt        |d      | _
        y)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/auto_recruiter/arenv/lib/python3.12/site-packages/langchain/agents/middleware/tool_emulator.pyr   zLLMToolEmulator.__init__B   s    ( 	 !D=*-%E$5 9dC())--d3 ))--dii89 =()O]^_DJ}-DJ(A>DJ    c                ~   |j                   d   }| j                  xs || j                  v }|s ||      S |j                   d   }|j                  r|j                  j                  nd}d| d| d| d}| j
                  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   r-   	r   requesthandler	tool_nameshould_emulate	tool_argstool_descriptionpromptresponses	            r"   wrap_tool_callzLLMToolEmulator.wrap_tool_callm   s     %%f-	 ))OY$:O:O-O7## %%f-	7>||7<<33IcK  ,- .# %78 	 ::$$l6&:%;< $$ **40
 	
r#   c                  K   |j                   d   }| j                  xs || j                  v }|s ||       d{   S |j                   d   }|j                  r|j                  j                  nd}d| d| d| d}| j
                  j                  t        |      g       d{   }t        |j                  |j                   d	   |
      S 7 7 +w)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   Nr%   r&   r'   r(   r)   r*   r+   r,   )
r/   r   r   r    r0   r   ainvoker   r   r-   r2   s	            r"   awrap_tool_callzLLMToolEmulator.awrap_tool_call   s     " %%f-	 ))OY$:O:O-O ))) %%f-	7>||7<<33IcK  ,- .# %78 	 ++\&-A,BCC $$ **40
 	
- *& Ds"   9CCA+C'C(*CC)r   zlist[str | BaseTool] | Noner   zstr | BaseChatModel | NonereturnNone)r3   r   r4   z2Callable[[ToolCallRequest], ToolMessage | Command]r?   ToolMessage | Command)r3   r   r4   z=Callable[[ToolCallRequest], Awaitable[ToolMessage | Command]]r?   rA   )__name__
__module____qualname____doc__r   r;   r>   __classcell__)r!   s   @r"   r   r      sz    )\ .2,0	)? +)? *	)?
 
)?V0
 0
 D0
 
	0
d2
 2
 O2
 
	2
r#   r   N)rE   
__future__r   typingr   *langchain_core.language_models.chat_modelsr   langchain_core.messagesr   r   !langchain.agents.middleware.typesr   langchain.chat_models.baser	   collections.abcr
   r   langgraph.typesr   r   langchain.toolsr   r    r#   r"   <module>rQ      s7    + "   D = = 63'A({
o {
r#   