
    )`i                         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
 erddlmZ  edej                  Z G d	 d
          ZdS )a  
Experimental client-side task support.

This module provides client methods for interacting with MCP tasks.

WARNING: These APIs are experimental and may change without notice.

Example:
    # Call a tool as a task
    result = await session.experimental.call_tool_as_task("tool_name", {"arg": "value"})
    task_id = result.task.taskId

    # Get task status
    status = await session.experimental.get_task(task_id)

    # Get task result when complete
    if status.status == "completed":
        result = await session.experimental.get_task_result(task_id, CallToolResult)

    # List all tasks
    tasks = await session.experimental.list_tasks()

    # Cancel a task
    await session.experimental.cancel_task(task_id)
    )AsyncIterator)TYPE_CHECKINGAnyTypeVarN)poll_until_terminal)ClientSessionResultT)boundc                      e Zd ZdZddZ	 ddddd	ed
eeef         dz  dedeeef         dz  de	j
        f
dZdede	j        fdZdedee         defdZ	 ddedz  de	j        fdZdede	j        fdZdedee	j                 fdZdS )ExperimentalClientFeaturesz
    Experimental client features for tasks and other experimental APIs.

    WARNING: These APIs are experimental and may change without notice.

    Access via session.experimental:
        status = await session.experimental.get_task(task_id)
    sessionr   returnNc                     || _         d S N)_session)selfr   s     q/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/mcp/client/experimental/tasks.py__init__z#ExperimentalClientFeatures.__init__1   s        i`  )ttlmetaname	argumentsr   r   c                ,  K   d}|t          j        j        di |}| j                            t          j        t          j        t          j        ||t          j        |          |                              t           j	                   d{V S )a  Call a tool as a task, returning a CreateTaskResult for polling.

        This is a convenience method for calling tools that support task execution.
        The server will return a task reference instead of the immediate result,
        which can then be polled via `get_task()` and retrieved via `get_task_result()`.

        Args:
            name: The tool name
            arguments: Tool arguments
            ttl: Task time-to-live in milliseconds (default: 60000 = 1 minute)
            meta: Optional metadata to include in the request

        Returns:
            CreateTaskResult containing the task reference

        Example:
            # Create task
            result = await session.experimental.call_tool_as_task(
                "long_running_tool", {"input": "data"}
            )
            task_id = result.task.taskId

            # Poll for completion
            while True:
                status = await session.experimental.get_task(task_id)
                if status.status == "completed":
                    break
                await asyncio.sleep(0.5)

            # Get result
            final = await session.experimental.get_task_result(task_id, CallToolResult)
        N)r   )r   r   task_metaparams )
typesRequestParamsMetar   send_requestClientRequestCallToolRequestCallToolRequestParamsTaskMetadataCreateTaskResult)r   r   r   r   r   r   s         r   call_tool_as_taskz,ExperimentalClientFeatures.call_tool_as_task4   s      P 26',44t44E]//% 6!"+"/C888#	    	 	 "
 
 
 
 
 
 
 
 	
r   task_idc           	         K   | j                             t          j        t          j        t          j        |                              t          j                   d{V S )z
        Get the current status of a task.

        Args:
            task_id: The task identifier

        Returns:
            GetTaskResult containing the task status and metadata
        taskIdr   N)r   r#   r    r$   GetTaskRequestGetTaskRequestParamsGetTaskResultr   r*   s     r   get_taskz#ExperimentalClientFeatures.get_taskn   s~       ]//$ 5WEEE   
 
 
 
 
 
 
 
 
 	
r   result_typec           	         K   | j                             t          j        t          j        t          j        |                              |           d{V S )a  
        Get the result of a completed task.

        The result type depends on the original request type:
        - tools/call tasks return CallToolResult
        - Other request types return their corresponding result type

        Args:
            task_id: The task identifier
            result_type: The expected result type (e.g., CallToolResult)

        Returns:
            The task result, validated against result_type
        r,   r   N)r   r#   r    r$   GetTaskPayloadRequestGetTaskPayloadRequestParams)r   r*   r3   s      r   get_task_resultz*ExperimentalClientFeatures.get_task_result   s{      & ]//+ <GLLL   
 
 
 
 
 
 
 
 
 	
r   cursorc                    K   |rt          j        |          nd}| j                            t          j        t          j        |                    t           j                   d{V S )z
        List all tasks.

        Args:
            cursor: Optional pagination cursor

        Returns:
            ListTasksResult containing tasks and optional next cursor
        )r8   Nr   )r    PaginatedRequestParamsr   r#   r$   ListTasksRequestListTasksResult)r   r8   r   s      r   
list_tasksz%ExperimentalClientFeatures.list_tasks   s       AGP-V<<<<D]//&f555  !	
 
 
 
 
 
 
 
 	
r   c           	         K   | j                             t          j        t          j        t          j        |                              t          j                   d{V S )z
        Cancel a running task.

        Args:
            task_id: The task identifier

        Returns:
            CancelTaskResult with the updated task state
        r,   r   N)r   r#   r    r$   CancelTaskRequestCancelTaskRequestParamsCancelTaskResultr1   s     r   cancel_taskz&ExperimentalClientFeatures.cancel_task   s~       ]//' 8HHH   
 "
 
 
 
 
 
 
 
 	
r   c                P   K   t          | j        |          2 3 d{V }|W V  6 dS )ak  
        Poll a task until it reaches a terminal status.

        Yields GetTaskResult for each poll, allowing the caller to react to
        status changes (e.g., handle input_required). Exits when task reaches
        a terminal status (completed, failed, cancelled).

        Respects the pollInterval hint from the server.

        Args:
            task_id: The task identifier

        Yields:
            GetTaskResult for each poll

        Example:
            async for status in session.experimental.poll_task(task_id):
                print(f"Status: {status.status}")
                if status.status == "input_required":
                    # Handle elicitation request via tasks/result
                    pass

            # Task is now terminal, get the result
            result = await session.experimental.get_task_result(task_id, CallToolResult)
        N)r   r2   )r   r*   statuss      r   	poll_taskz$ExperimentalClientFeatures.poll_task   sU      4 0wGG 	 	 	 	 	 	 	&LLLLL HGGs   %)r   r   r   Nr   )__name__
__module____qualname____doc__r   strdictr   intr    r(   r)   r0   r2   typer	   r7   r<   r=   rA   rB   r   rE   r   r   r   r   r   '   s                ,08

 &*8
 8
 8
8
 S>D(8

 8
 38nt#8
 
	8
 8
 8
 8
t
c 
e.A 
 
 
 
&

 ']
 
	
 
 
 
< "
 
d

 
	
 
 
 
*
 
1G 
 
 
 
&s }U=P/Q      r   r   )rI   collections.abcr   typingr   r   r   	mcp.typesr    %mcp.shared.experimental.tasks.pollingr   mcp.client.sessionr   Resultr	   r   r   r   r   <module>rT      s    4 * ) ) ) ) ) . . . . . . . . . .       E E E E E E 1000000
')5<
0
0
0y y y y y y y y y yr   