o
    iy                     @  s   d dl mZ d dlmZmZm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 d dlmZ d dlmZ ddlmZ G d	d
 d
ee	ef ZdddZdS )    )annotations)Any	AwaitableCallable)HandlerErrorHandlerErrorTypeInputTOperationInfoOutputT)CancelOperationContextFetchOperationInfoContextFetchOperationResultContextOperationHandlerStartOperationContextStartOperationResultAsync)"_temporal_cancel_operation_context)WorkflowHandle   )is_async_callablec                   @  sB   e Zd ZdZdddZdddZdddZd ddZd!ddZdS )"WorkflowRunOperationHandleraJ  Operation handler for Nexus operations that start a workflow.

    Use this class to create an operation handler that starts a workflow by passing your
    ``start`` method to the constructor. Your ``start`` method must use
    :py:func:`temporalio.nexus.WorkflowRunOperationContext.start_workflow` to start the
    workflow.
    startMCallable[[StartOperationContext, InputT], Awaitable[WorkflowHandle[OutputT]]]returnNonec                 C  sH   t |st| d|| _|jr t| jdd }r"|j|_dS dS dS )z.Initialize the workflow run operation handler.zp is not an `async def` method. WorkflowRunOperationHandler must be initialized with an `async def` start method.__func__N)r   RuntimeError_start__doc__getattrr   )selfr   
start_func r!   j/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/temporalio/nexus/_operation_handlers.py__init__,   s   z$WorkflowRunOperationHandler.__init__ctxr   inputr   r   c                   sD   |  ||I dH }t|tstd| dt| dt| S )zJStart the operation, by starting a workflow and completing asynchronously.Nz	Expected z' to be a nexus.WorkflowHandle, but got aJ  . When using @workflow_run_operation you must use WorkflowRunOperationContext.start_workflow() to start a workflow that will deliver the result of the Nexus operation, and you must return the nexus.WorkflowHandle that it returns. It is not possible to use client.Client.start_workflow() and client.WorkflowHandle for this purpose.)r   
isinstancer   r   typer   to_token)r   r$   r%   handler!   r!   r"   r   ?   s   
	z!WorkflowRunOperationHandler.startr   tokenstrc                   s   t |I dH  dS )z1Cancel the operation, by cancelling the workflow.N)_cancel_workflowr   r$   r*   r!   r!   r"   cancelP   s   z"WorkflowRunOperationHandler.cancelr   r	   c                   
   t d)zCFetch operation info (not supported for Temporal Nexus operations).zITemporal Nexus operation handlers do not support fetching operation info.NotImplementedErrorr-   r!   r!   r"   
fetch_infoT      z&WorkflowRunOperationHandler.fetch_infor   r
   c                   r/   )zEFetch operation result (not supported for Temporal Nexus operations).zOTemporal Nexus operation handlers do not support fetching the operation result.r0   r-   r!   r!   r"   fetch_result\   r3   z(WorkflowRunOperationHandler.fetch_resultN)r   r   r   r   )r$   r   r%   r   r   r   )r$   r   r*   r+   r   r   )r$   r   r*   r+   r   r	   )r$   r   r*   r+   r   r
   )	__name__
__module____qualname__r   r#   r   r.   r2   r4   r!   r!   r!   r"   r   #   s    



r   r*   r+   kwargsr   r   r   c              
     s   z	t t | }W n ty } ztdtjd|d}~ww t }z|	|j
}W n ty? } ztdtjd|d}~ww |jdi |I dH  dS )a  Cancel a workflow that is backing a Nexus operation.

    This function is used by the Nexus worker to cancel a workflow that is backing a
    Nexus operation, i.e. started by a
    :py:func:`temporalio.nexus.workflow_run_operation`-decorated method.

    Args:
        token: The token of the workflow to cancel. kwargs: Additional keyword arguments
        to pass to the workflow cancel method.
    zsFailed to decode operation token as a workflow operation token. Canceling non-workflow operations is not supported.)r'   NzAFailed to construct workflow handle from workflow operation tokenr!   )r   r   
from_token	Exceptionr   r   	NOT_FOUNDr   get_to_client_workflow_handleclientr.   )r*   r8   nexus_workflow_handleerrr$   client_workflow_handler!   r!   r"   r,   e   s6   r,   N)r*   r+   r8   r   r   r   )
__future__r   typingr   r   r   nexusrpcr   r   r   r	   r
   nexusrpc.handlerr   r   r   r   r   r   #temporalio.nexus._operation_contextr   temporalio.nexus._tokenr   _utilr   r   r,   r!   r!   r!   r"   <module>   s     	B