Ë
    ïñþ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
mZ ddlmZmZ ddlmZ erddlmZ  ed	ej&                  ¬
«      Z G d„ d«      Zy)u8  
Experimental server session features for serverâ†’client task operations.

This module provides the server-side equivalent of ExperimentalClientFeatures,
allowing the server to send task-augmented requests to the client and poll for results.

WARNING: These APIs are experimental and may change without notice.
é    )ÚAsyncIterator)ÚTYPE_CHECKINGÚAnyÚTypeVarN)Úvalidate_sampling_toolsÚ!validate_tool_use_result_messages)Ú"require_task_augmented_elicitationÚrequire_task_augmented_sampling)Úpoll_until_terminal)ÚServerSessionÚResultT)Úboundc                   óØ  — e Zd ZdZdd„Zdedej                  fd„Zdede	e
   de
fd„Zdedeej                     fd	„Zd
dœdedej                  dedej                   fd„Zd
dddddddddœ	deej&                     dedededz  dej(                  dz  dedz  dee   dz  deeef   dz  dej0                  dz  deej2                     dz  dej4                  dz  dej6                  fd„Zy)Ú!ExperimentalServerSessionFeaturesu¯  
    Experimental server session features for serverâ†’client task operations.

    This provides the server-side equivalent of ExperimentalClientFeatures,
    allowing the server to send task-augmented requests to the client and
    poll for results.

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

    Access via session.experimental:
        result = await session.experimental.elicit_as_task(...)
    ÚreturnNc                 ó   — || _         y )N)Ú_session)ÚselfÚsessions     úh/root/aria/mcps/aria-brain/venv/lib/python3.12/site-packages/mcp/server/experimental/session_features.pyÚ__init__z*ExperimentalServerSessionFeatures.__init__)   s	   € Øˆ�ó    Útask_idc           	   ƒ   óè   K  — | j                   j                  t        j                  t        j                  t        j
                  |¬«      ¬«      «      t        j                  «      ƒ d{  –—† S 7 Œ­w)zÁ
        Send tasks/get to the client to get task status.

        Args:
            task_id: The task identifier

        Returns:
            GetTaskResult containing the task status
        ©ÚtaskId©ÚparamsN)r   Úsend_requestÚtypesÚServerRequestÚGetTaskRequestÚGetTaskRequestParamsÚGetTaskResult)r   r   s     r   Úget_taskz*ExperimentalServerSessionFeatures.get_task,   sW   è ø€ ð —]‘]×/Ñ/Ü×Ñ¤× 4Ñ 4¼E×<VÑ<VÐ^eÔ<fÔ gÓhÜ×Ñó
÷ 
ð 	
ð 
ús   ‚A)A2Á+A0Á,A2Úresult_typec           	   ƒ   óÌ   K  — | j                   j                  t        j                  t        j                  t        j
                  |¬«      ¬«      «      |«      ƒ d{  –—† S 7 Œ­w)a  
        Send tasks/result to the client to retrieve the final result.

        Args:
            task_id: The task identifier
            result_type: The expected result type

        Returns:
            The task result, validated against result_type
        r   r   N)r   r   r    r!   ÚGetTaskPayloadRequestÚGetTaskPayloadRequestParams)r   r   r&   s      r   Úget_task_resultz1ExperimentalServerSessionFeatures.get_task_result;   sQ   è ø€ ð —]‘]×/Ñ/Ü×Ñ¤× ;Ñ ;Ä5×CdÑCdÐlsÔCtÔ uÓvØó
÷ 
ð 	
ð 
ús   ‚AA$ÁA"ÁA$c                ó\   K  — t        | j                  |«      2 3 d{  –—† }|­–— Œ7 Œ
6 y­w)a€  
        Poll a client task until it reaches terminal status.

        Yields GetTaskResult for each poll, allowing the caller to react to
        status changes. Exits when task reaches a terminal status.

        Respects the pollInterval hint from the client.

        Args:
            task_id: The task identifier

        Yields:
            GetTaskResult for each poll
        N)r   r%   )r   r   Ústatuss      r   Ú	poll_taskz+ExperimentalServerSessionFeatures.poll_taskO   s0   è ø€ ô 0°·±¸wÓG÷ 	ð 	�&ØŒLð	øÑGùs   ‚,™*�(ž*¡,¨*ª,i`ê  ©ÚttlÚmessageÚrequestedSchemar/   c             ƒ   óT  K  — | j                   j                  r | j                   j                  j                  nd}t        |«       | j                   j	                  t        j                  t        j                  t        j                  ||t        j                  |¬«      ¬«      ¬«      «      t
        j                  «      ƒ d{  –—† }|j                  j                  }| j                  |«      2 3 d{  –—† }Œ
7 Œ57 Œ6 | j                  |t
        j                  «      ƒ d{  –—†7  S ­w)a  
        Send a task-augmented elicitation to the client and poll until complete.

        The client will create a local task, process the elicitation asynchronously,
        and return the result when ready. This method handles the full flow:
        1. Send elicitation with task field
        2. Receive CreateTaskResult from client
        3. Poll client's task until terminal
        4. Retrieve and return the final ElicitResult

        Args:
            message: The message to present to the user
            requestedSchema: Schema defining the expected response
            ttl: Task time-to-live in milliseconds

        Returns:
            The client's elicitation response

        Raises:
            McpError: If client doesn't support task-augmented elicitation
        Nr.   )r0   r1   Útaskr   )r   Úclient_paramsÚcapabilitiesr	   r   r    r!   ÚElicitRequestÚElicitRequestFormParamsÚTaskMetadataÚCreateTaskResultr3   r   r-   r*   ÚElicitResult)r   r0   r1   r/   Úclient_capsÚcreate_resultr   Ú_s           r   Úelicit_as_taskz0ExperimentalServerSessionFeatures.elicit_as_taska   sö   è ø€ ð8 CGÇ-Á-×B]ÒB]�d—m‘m×1Ñ1×>Ò>ÐcgˆÜ*¨;Ô7à"Ÿm™m×8Ñ8Ü×ÑÜ×#Ñ#Ü ×8Ñ8Ø 'Ø(7Ü"×/Ñ/°CÔ8ôôóô ×"Ñ"ó
÷ 
ˆð  ×$Ñ$×+Ñ+ˆà—~‘~ gÓ.÷ 	ð 	�!Øð!
øð	øÐ.ð ×)Ñ)¨'´5×3EÑ3EÓF×FÐFÐFùsB   ‚CD(ÃC:Ã+D(Ã0C>Ã4C<Ã5C>Ã8D(Ã<C>Ã>$D(Ä"D%Ä#D()	r/   Úsystem_promptÚinclude_contextÚtemperatureÚstop_sequencesÚmetadataÚmodel_preferencesÚtoolsÚtool_choiceÚmessagesÚ
max_tokensr?   r@   rA   rB   rC   rD   rE   rF   c       
      ƒ   ó”  K  — | j                   j                  r | j                   j                  j                  nd}t        |«       t	        ||
|«       t        |«       | j                   j                  t        j                  t        j                  t        j                  ||||||||	|
|t        j                  |¬«      ¬«      ¬«      «      t        j                  «      ƒ d{  –—† }|j                  j                  }| j                  |«      2 3 d{  –—† }Œ
7 Œ57 Œ6 | j!                  |t        j"                  «      ƒ d{  –—†7  S ­w)a5  
        Send a task-augmented sampling request and poll until complete.

        The client will create a local task, process the sampling request
        asynchronously, and return the result when ready.

        Args:
            messages: The conversation messages for sampling
            max_tokens: Maximum tokens in the response
            ttl: Task time-to-live in milliseconds
            system_prompt: Optional system prompt
            include_context: Context inclusion strategy
            temperature: Sampling temperature
            stop_sequences: Stop sequences
            metadata: Additional metadata
            model_preferences: Model selection preferences
            tools: Optional list of tools the LLM can use during sampling
            tool_choice: Optional control over tool usage behavior

        Returns:
            The sampling result from the client

        Raises:
            McpError: If client doesn't support task-augmented sampling or tools
            ValueError: If tool_use or tool_result message structure is invalid
        Nr.   )rG   Ú	maxTokensÚsystemPromptÚincludeContextrA   ÚstopSequencesrC   ÚmodelPreferencesrE   Ú
toolChoicer3   r   )r   r4   r5   r
   r   r   r   r    r!   ÚCreateMessageRequestÚCreateMessageRequestParamsr8   r9   r3   r   r-   r*   ÚCreateMessageResult)r   rG   rH   r/   r?   r@   rA   rB   rC   rD   rE   rF   r;   r<   r   r=   s                   r   Úcreate_message_as_taskz8ExperimentalServerSessionFeatures.create_message_as_task”   s#  è ø€ ðR CGÇ-Á-×B]ÒB]�d—m‘m×1Ñ1×>Ò>ÐcgˆÜ'¨Ô4Ü ¨U°KÔ@Ü)¨(Ô3à"Ÿm™m×8Ñ8Ü×ÑÜ×*Ñ*Ü ×;Ñ;Ø!)Ø",Ø%2Ø'6Ø$/Ø&4Ø!)Ø):Ø#Ø#.Ü"×/Ñ/°CÔ8ôôóô" ×"Ñ"ó%
÷ 
ˆð*  ×$Ñ$×+Ñ+ˆà—~‘~ gÓ.÷ 	ð 	�!Øð1
øð.	øÐ.ð ×)Ñ)¨'´5×3LÑ3LÓM×MÐMÐMùsB   ‚C"EÃ$DÃ%+EÄDÄDÄDÄEÄDÄ$EÅEÅE)r   r   r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ústrr    r$   r%   Útyper   r*   r   r-   ÚElicitRequestedSchemaÚintr:   r>   ÚlistÚSamplingMessageÚIncludeContextÚfloatÚdictr   ÚModelPreferencesÚToolÚ
ToolChoicerR   rS   © r   r   r   r      sº  „ ñó ð
 cð 
¨e×.AÑ.Aó 
ð
àð
ð ˜'‘]ð
ð 
ó	
ð( sð ¨}¸U×=PÑ=PÑ/Qó ð. ò1Gàð1Gð ×4Ñ4ð1Gð
 ð1Gð 
×	Ñ	ó1Gðp Ø$(Ø7;Ø$(Ø+/Ø*.Ø;?Ø)-Ø/3òHNà�u×,Ñ,Ñ-ðHNð ð	HNð
 ðHNð ˜T‘zðHNð ×-Ñ-°Ñ4ðHNð ˜T‘\ðHNð ˜S™	 DÑ(ðHNð �s˜C�x‘. 4Ñ'ðHNð !×1Ñ1°DÑ8ðHNð �E—J‘JÑ $Ñ&ðHNð ×%Ñ%¨Ñ,ðHNð 
×	"Ñ	"ôHNr   r   )rW   Úcollections.abcr   Útypingr   r   r   Ú	mcp.typesr    Úmcp.server.validationr   r   Ú*mcp.shared.experimental.tasks.capabilitiesr	   r
   Ú%mcp.shared.experimental.tasks.pollingr   Úmcp.server.sessionr   ÚResultr   r   rd   r   r   ú<module>rm      sJ   ðñõ *ß .Ñ .å ß \÷õ FáÝ0á
�) 5§<¡<Ô
0€÷ANò ANr   