o
    i'                     @   s   d Z ddlmZmZmZmZmZ ddlmZm	Z	m
Z
mZmZmZmZmZmZmZ ddlmZ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 dd	l m!Z! g d
Z"G dd dZ#G dd deZ$G dd de
Z%G dd dZ&dS )z$Testing utilities for OpenAI agents.    )AsyncIteratorCallableOptionalSequenceUnion)
AgentOutputSchemaBaseHandoffModelModelProviderModelResponseModelSettingsModelTracingToolTResponseInputItemUsage)TResponseOutputItemTResponseStreamEvent)ResponseFunctionToolCallResponseOutputMessageResponseOutputText)Client)StatefulMCPServerProviderStatelessMCPServerProvider)ModelActivityParameters)OpenAIAgentsPlugin)AgentEnvironmentResponseBuilders	TestModelTestModelProviderc                   @   sl   e Zd ZdZededefddZedede	fddZ
ed	ed
edefddZededefddZdS )r   zBuilders for creating model responses for testing.

    .. warning::
        This API is experimental and may change in the future.
    outputreturnc                 C   s   t | gt ddS )zCreate a ModelResponse with the given output.

        .. warning::
           This API is experimental and may change in the future.
        N)r   usageresponse_id)r   r   )r    r#   n/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/temporalio/contrib/openai_agents/testing.pymodel_response/   s
   zResponseBuilders.model_responsetextc                 C   s   t dt| g ddgddddS )zCreate a ResponseOutputMessage with text content.

        .. warning::
           This API is experimental and may change in the future.
         output_text)r&   annotationstype	assistant	completedmessage)idcontentrolestatusr*   )r   r   r&   r#   r#   r$   response_output_message<   s   z(ResponseBuilders.response_output_message	argumentsnamec              
   C   s   t t| d|ddddS )zCreate a ModelResponse with a function tool call.

        .. warning::
           This API is experimental and may change in the future.
        callfunction_callr.   r,   )r4   call_idr5   r*   r.   r1   )r   r%   r   )r4   r5   r#   r#   r$   	tool_callQ   s   zResponseBuilders.tool_callc                 C   s   t t | S )zCreate a ModelResponse with an output message.

        .. warning::
           This API is experimental and may change in the future.
        )r   r%   r3   r2   r#   r#   r$   output_messagec   s   zResponseBuilders.output_messageN)__name__
__module____qualname____doc__staticmethodr   r   r%   strr   r3   r9   r:   r#   r#   r#   r$   r   (   s    r   c                   @   s<   e Zd ZdZdZdefddZdeedf defd	d
Z	dS )r   zTest model provider which simply returns the given module.

    .. warning::
        This API is experimental and may change in the future.
    Fmodelc                 C   
   || _ dS )zInitialize a test model provider with a model.

        .. warning::
           This API is experimental and may change in the future.
        N_model)selfrA   r#   r#   r$   __init__x      
zTestModelProvider.__init__
model_nameNr    c                 C   s   | j S )zGet a model from the model provider.

        .. warning::
           This API is experimental and may change in the future.
        rC   )rE   rH   r#   r#   r$   	get_model   s   zTestModelProvider.get_model)
r;   r<   r=   r>   __test__r	   rF   r   r@   rI   r#   r#   r#   r$   r   o   s
    r   c                   @   s   e Zd ZdZdZdeg ef ddfddZdee	df d	ee	e
e f d
ede
e deedf de
e dedefddZdee	 d	ee	e
e f d
ede
e dee de
e dedee fddZede
e dd fddZdS )r   zTest model for use mocking model responses.

    .. warning::
        This API is experimental and may change in the future.
    Ffnr    Nc                 C   rB   )zInitialize a test model with a callable.

        .. warning::
           This API is experimental and may change in the future.
        NrK   )rE   rK   r#   r#   r$   rF      rG   zTestModel.__init__system_instructionsinputmodel_settingstoolsoutput_schemahandoffstracingc           	         s
   |   S )zXGet a response from the mocked model, by calling the callable passed to the constructor.rL   	rE   rM   rN   rO   rP   rQ   rR   rS   kwargsr#   r#   r$   get_response   s   zTestModel.get_responsec           	      K   s   t  )z6Get a streamed response from the model. Unimplemented.)NotImplementedErrorrT   r#   r#   r$   stream_response   s   zTestModel.stream_response	responsesc                    s   t |  t fddS )zCreate a mock model which sequentially returns responses from a list.

        .. warning::
           This API is experimental and may change in the future.
        c                      s   t  S )N)nextr#   ir#   r$   <lambda>   s    z/TestModel.returning_responses.<locals>.<lambda>)iterr   )rY   r#   r[   r$   returning_responses   s   zTestModel.returning_responses)r;   r<   r=   r>   rJ   r   r   rF   r   r@   listr   r   r   r   r   r   rV   r   r   r   rX   r?   r_   r#   r#   r#   r$   r      sP    





r   c                   @   s   e Zd ZdZdZ					ddee dee dee d	e	e
eef  d
eddfddZdddZdddZdedefddZedefddZdS )r   a  Testing environment for OpenAI agents with Temporal integration.

    This async context manager provides a convenient way to set up testing environments
    for OpenAI agents with mocked model calls and Temporal integration.

    .. warning::
        This API is experimental and may change in the future.

    Example:
        >>> from temporalio.contrib.openai_agents.testing import AgentEnvironment, TestModelProvider, ResponseBuilders
        >>> from temporalio.client import Client
        >>>
        >>> # Create a mock model that returns predefined responses
        >>> mock_model = TestModel.returning_responses([
        ...     ResponseBuilders.output_message("Hello, world!"),
        ...     ResponseBuilders.output_message("How can I help you?")
        ... ])
        >>>
        >>> async with AgentEnvironment(model=mock_model) as env:
        ...     client = env.applied_on_client(client)
        ...     # Use client for testing workflows with mocked model calls
    FNr#   Tmodel_paramsmodel_providerrA   mcp_server_providersregister_activitiesr    c                 C   sD   || _ d| _|dur|| _n	|durt|| _|| _|| _d| _dS )a  Initialize the AgentEnvironment.

        Args:
            model_params: Configuration parameters for Temporal activity execution
                of model calls. If None, default parameters will be used.
            model_provider: Optional model provider for custom model implementations.
                Only one of model_provider or model should be provided.
                If both are provided, model_provider will be used.
            model: Optional model for custom model implementations.
                Use TestModel for mocking model responses.
                Equivalent to model_provider=TestModelProvider(model).
                Only one of model_provider or model should be provided.
                If both are provided, model_provider will be used.
            mcp_server_providers: Sequence of MCP servers to automatically register with the worker.
            register_activities: Whether to register activities during worker execution.

        .. warning::
           This API is experimental and may change in the future.
        N)_model_params_model_providerr   _mcp_server_providers_register_activities_plugin)rE   ra   rb   rA   rc   rd   r#   r#   r$   rF      s   

zAgentEnvironment.__init__c                    s    t | j| j| j| jd| _| S )z Enter the async context manager.)ra   rb   rc   rd   )r   re   rf   rg   rh   ri   rE   r#   r#   r$   
__aenter__  s   zAgentEnvironment.__aenter__c                    s   dS )zExit the async context manager.Nr#   )rE   argsr#   r#   r$   	__aexit__  s   zAgentEnvironment.__aexit__clientc                 C   sH   | j du r	td| }|dg }t|| j g |d< tdi |S )ab  Apply the agent environment's plugin to a client and return a new client instance.

        Args:
            client: The base Temporal client to apply the plugin to.

        Returns:
            A new Client instance with the OpenAI agents plugin applied.

        .. warning::
           This API is experimental and may change in the future.
        Nz:AgentEnvironment must be entered before applying to clientpluginsr#   )ri   RuntimeErrorconfiggetr`   r   )rE   rn   
new_configexisting_pluginsr#   r#   r$   applied_on_client  s   
z"AgentEnvironment.applied_on_clientc                 C   s   | j du r	td| j S )zGet the underlying OpenAI agents plugin.

        .. warning::
           This API is experimental and may change in the future.
        Nz8AgentEnvironment must be entered before accessing plugin)ri   rp   rj   r#   r#   r$   openai_agents_plugin)  s
   
z%AgentEnvironment.openai_agents_plugin)NNNr#   T)r    r   )r    N)r;   r<   r=   r>   rJ   r   r   r
   r	   r   r   r   r   boolrF   rk   rm   r   ru   propertyr   rv   r#   r#   r#   r$   r      s8    
	

'
r   N)'r>   typingr   r   r   r   r   agentsr   r   r	   r
   r   r   r   r   r   r   agents.itemsr   r   openai.types.responsesr   r   r   temporalio.clientr   %temporalio.contrib.openai_agents._mcpr   r   2temporalio.contrib.openai_agents._model_parametersr   8temporalio.contrib.openai_agents._temporal_openai_agentsr   __all__r   r   r   r   r#   r#   r#   r$   <module>   s    0G8