o
    iR                     @  s  U d dl mZ d dlZd dlZd dlmZmZmZmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZ d dl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 d dlZd dlZd dl Zd dl!m"Z" d dl#m$Z$ d dl%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+ erd dl,ZedZ-de.d< edZ/de.d< edZ0de.d< eddG dd dZ1d:ddZ2d;dd Z3d<d"d#Z4d=d%d&Z5d>d(d)Z6e
d?d+d,Z7d:d-d.Z8eG d/d0 d0Z9G d1d2 d2eZ:eddG d3d4 d4Z;eddG d5d6 d6Z<G d7d8 d8ej=Z=e=e>d9dZ?dS )@    )annotationsN)	AwaitableMappingMutableMappingSequence)contextmanager)
ContextVar)	dataclass)	timedelta)TYPE_CHECKINGAnyCallable	GeneratorOptionalUnionoverload)CancelOperationContextStartOperationContext)Concatenate)_link_conversion)WorkflowHandle)MethodAsyncNoParamMethodAsyncSingleParamMultiParamSpec	ParamType
ReturnTypeSelfTypez temporal-start-operation-contextz*ContextVar[_TemporalStartOperationContext]!_temporal_start_operation_contextz!temporal-cancel-operation-contextz+ContextVar[_TemporalCancelOperationContext]"_temporal_cancel_operation_contextz-temporal-nexus-backing-workflow-start-contextzContextVar[bool]._temporal_nexus_backing_workflow_start_contextT)frozenc                   @  s   e Zd ZU dZded< dS )InfozInformation about the running Nexus operation.

    .. warning::
        This API is experimental and unstable.

    Retrieved inside a Nexus operation handler via :py:func:`info`.
    str
task_queueN__name__
__module____qualname____doc____annotations__ r*   r*   i/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/temporalio/nexus/_operation_context.pyr!   =   s   
 r!   returnboolc                   C  s
   t  duS )z5Whether the current code is inside a Nexus operation.N)_try_temporal_contextr*   r*   r*   r+   in_operationK      
r/   c                   C  s
   t   S )z,Get the current Nexus operation information.)_temporal_contextinfor*   r*   r*   r+   r2   P   r0   r2   temporalio.client.Clientc                   C  s   t  jS )zPGet the Temporal client used by the worker handling the current Nexus operation.)r1   clientr*   r*   r*   r+   r4   U   s   r4   FUnion[_TemporalStartOperationContext, _TemporalCancelOperationContext]c                  C  s   t  } | d u rtd| S NzNot in Nexus operation context.)r.   RuntimeErrorctxr*   r*   r+   r1   Z   s   r1   POptional[Union[_TemporalStartOperationContext, _TemporalCancelOperationContext]]c                  C  s,   t d } td }| r|rtd| p|S )Nz6Cannot be in both start and cancel operation contexts.)r   getr   r7   )	start_ctx
cancel_ctxr*   r*   r+   r.   c   s
   

r.   Generator[None, None, None]c               	   c  s0    t d} zd V  W t |  d S t |  w )NT)r   setreset)tokenr*   r*   r+   %_nexus_backing_workflow_start_contextm   s
   
rB   c                   C  s
   t dS )NF)r   r;   r*   r*   r*   r+   (_in_nexus_backing_workflow_start_contextv   s   
rC   c                   @  sf   e Zd ZU dZded< 	 ded< 	 ded< 	 edd	d
ZdddZdddZdddZ	dddZ
dS )_TemporalStartOperationContextzMContext for a Nexus start operation being handled by a Temporal Nexus Worker.r   nexus_contextCallable[[], Info]r2   r3   r4   r,   c                 C     t d }|d u rtd|S r6   )r   r;   r7   clsr9   r*   r*   r+   r;         
z"_TemporalStartOperationContext.getNonec                 C     t |  d S N)r   r?   selfr*   r*   r+   r?         z"_TemporalStartOperationContext.set list[temporalio.client.Callback]c                 C  s"   | j }|jrt|j|jdgS g S )N)urlheaders)rE   callback_urlNexusCallbackcallback_headers)rO   r9   r*   r*   r+   _get_callbacks   s   z-_TemporalStartOperationContext._get_callbacks1list[temporalio.api.common.v1.Link.WorkflowEvent]c                 C  s.   g }| j jD ]}t| }r|| q|S rM   )rE   inbound_linksr   nexus_link_to_workflow_eventappend)rO   event_linksinbound_linklinkr*   r*   r+   _get_workflow_event_links   s   
z8_TemporalStartOperationContext._get_workflow_event_linksworkflow_handle*temporalio.client.WorkflowHandle[Any, Any]c              
   C  s   g }z8t |jtjjjjr$|jdr$|jj }r$|dr$|	|j
 |s,t|g}| jjdd |D  W |S  tyX } ztd| d|  W Y d }~|S d }~ww )Nr^   workflow_eventc                 s  s    | ]}t |V  qd S rM   )r   workflow_event_to_nexus_link).0r^   r*   r*   r+   	<genexpr>   s
    
zE_TemporalStartOperationContext._add_outbound_links.<locals>.<genexpr>zCFailed to create WorkflowExecutionStarted event links for workflow z: )
isinstance_start_workflow_response
temporalioapiworkflowservicev1StartWorkflowExecutionResponseHasFieldr^   r[   rb   r   :workflow_execution_started_event_link_from_workflow_handlerE   outbound_linksextend	Exceptionloggerwarning)rO   r`   wf_event_linksr^   er*   r*   r+   _add_outbound_links   s4   


z2_TemporalStartOperationContext._add_outbound_linksN)r,   rD   r,   rK   )r,   rQ   )r,   rX   )r`   ra   )r%   r&   r'   r(   r)   classmethodr;   r?   rW   r_   rv   r*   r*   r*   r+   rD   z   s   
 


	rD   c                      s  e Zd ZdZdE fddZedFddZeddddej	j
jej	jjddddddddg i ddej	jjdddGd6d7Zeddddej	j
jej	jjddddddddg i ddej	jjdddHd;d7Zeddddej	j
jej	jjddddddddg i ddej	jjdddId=d7Zeej	jfg dddddej	j
jej	jjddddddddg i ddej	jjdd>dJdAd7Zej	jfg dddddej	j
jej	jjddddddddg i ddej	jjdd>dKdDd7Z  ZS )LWorkflowRunOperationContextzsContext received by a workflow run operation.

    .. warning::
        This API is experimental and unstable.
    argsr   kwargsr,   rK   c                   s    t  j|i | t | _dS )z.Initialize the workflow run operation context.N)super__init__rD   r;   r1   )rO   rz   r{   	__class__r*   r+   r}      s   z$WorkflowRunOperationContext.__init__r9   r   c                   s"   | di  fddt  D S )Nc                   s   i | ]
}|j t |j qS r*   )namegetattr)rd   fr8   r*   r+   
<dictcomp>   s    zMWorkflowRunOperationContext._from_start_operation_context.<locals>.<dictcomp>r*   )dataclassesfieldsrH   r*   r8   r+   _from_start_operation_context   s   z9WorkflowRunOperationContext._from_start_operation_contextN F)r#   execution_timeoutrun_timeouttask_timeoutid_reuse_policyid_conflict_policyretry_policycron_schedulememosearch_attributesstatic_summarystatic_detailsstart_delaystart_signalstart_signal_argsrpc_metadatarpc_timeoutrequest_eager_startpriorityversioning_overrideworkflow(MethodAsyncNoParam[SelfType, ReturnType]idr"   r#   Optional[str]r   Optional[timedelta]r   r   r   'temporalio.common.WorkflowIDReusePolicyr   *temporalio.common.WorkflowIDConflictPolicyr   'Optional[temporalio.common.RetryPolicy]r   r   Optional[Mapping[str, Any]]r   \Optional[Union[temporalio.common.TypedSearchAttributes, temporalio.common.SearchAttributes]]r   r   r   r   r   Sequence[Any]r   Mapping[str, Union[str, bytes]]r   r   r-   r   temporalio.common.Priorityr   .Optional[temporalio.common.VersioningOverride]WorkflowHandle[ReturnType]c                     d S rM   r*   )rO   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+   start_workflow   s   z*WorkflowRunOperationContext.start_workflow7MethodAsyncSingleParam[SelfType, ParamType, ReturnType]argr   c                  r   rM   r*   )rO   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      s    FCallable[Concatenate[SelfType, MultiParamSpec], Awaitable[ReturnType]]c                  r   rM   r*   )rO   r   rz   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   "     ")rz   r#   result_typer   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Optional[type[ReturnType]]c                  r   rM   r*   )rO   r   r   rz   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   G  r   0Union[str, Callable[..., Awaitable[ReturnType]]]Optional[type]c                  s  t  v | jjjdi d|d|d|d|d|p| j jd|d|d|d	|	d
|
d|d|d|d|d|d|d|d|d|d|d|d|d|d|d|d| j d| j d| jjj	I dH }W d   n1 s}w   Y  | j
| tt |S )a  Start a workflow that will deliver the result of the Nexus operation.

        The workflow will be started in the same namespace as the Nexus worker, using
        the same client as the worker. If task queue is not specified, the worker's task
        queue will be used.

        See :py:meth:`temporalio.client.Client.start_workflow` for all arguments.

        The return value is :py:class:`temporalio.nexus.WorkflowHandle`.

        The workflow will be started as usual, with the following modifications:

        - On workflow completion, Temporal server will deliver the workflow result to
            the Nexus operation caller, using the callback from the Nexus operation start
            request.

        - The request ID from the Nexus operation start request will be used as the
            request ID for the start workflow request.

        - Inbound links to the caller that were submitted in the Nexus start operation
            request will be attached to the started workflow and, outbound links to the
            started workflow will be added to the Nexus start operation response. If the
            Nexus caller is itself a workflow, this means that the workflow in the caller
            namespace web UI will contain links to the started workflow, and vice versa.
        r   r   rz   r   r#   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   	callbacksworkflow_event_links
request_idNr*   )rB   r1   r4   r   r2   r#   rW   r_   rE   r   rv   r   r   #_unsafe_from_client_workflow_handle)rO   r   r   rz   r   r#   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   	wf_handler*   r*   r+   r   k  s~   D	



 )rz   r   r{   r   r,   rK   )r9   r   r,   ry   ).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   r   r   r   r   r   r   r   r   r   r   r   r-   r   r   r   r   r,   r   )0r   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   r   r   r   r   r   r   r   r   r   r   r   r   r-   r   r   r   r   r,   r   )0r   r   rz   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   r   r   r   r   r   r   r   r   r   r   r-   r   r   r   r   r,   r   )4r   r"   r   r   rz   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   r   r   r   r   r   r   r   r   r   r   r   r   r-   r   r   r   r   r,   r   )4r   r   r   r   rz   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   r   r   r   r   r   r   r   r   r   r   r   r   r-   r   r   r   r   r,   r   )r%   r&   r'   r(   r}   rx   r   r   rh   commonWorkflowIDReusePolicyALLOW_DUPLICATEWorkflowIDConflictPolicyUNSPECIFIEDPrioritydefaultr   
_arg_unset__classcell__r*   r*   r~   r+   ry      s    !"	$&ry   c                   @  s$   e Zd ZU dZded< 	 ded< dS )rU   zNexus callback to attach to events such as workflow completion.

    .. warning::
        This API is experimental and unstable.
    r"   rR   zMapping[str, str]rS   Nr$   r*   r*   r*   r+   rU     s   
 rU   c                   @  sH   e Zd ZU dZded< 	 ded< 	 ded< 	 edd	d
ZdddZdS )_TemporalCancelOperationContextzNContext for a Nexus cancel operation being handled by a Temporal Nexus Worker.r   rE   rF   r2   r3   r4   r,   c                 C  rG   )Nz&Not in Nexus cancel operation context.)r   r;   r7   rH   r*   r*   r+   r;     rJ   z#_TemporalCancelOperationContext.getrK   c                 C  rL   rM   )r   r?   rN   r*   r*   r+   r?     rP   z#_TemporalCancelOperationContext.setN)r,   r   rw   )r%   r&   r'   r(   r)   rx   r;   r?   r*   r*   r*   r+   r     s   
 r   c                      s,   e Zd ZdZd fddZdddZ  ZS )LoggerAdapterz=Logger adapter that adds Nexus operation context information.rr   logging.Loggerextrar   c                   s   t  ||pi  dS )zInitialize the logger adapter.N)r|   r}   )rO   rr   r   r~   r*   r+   r}     s   zLoggerAdapter.__init__msgr   r{   MutableMapping[str, Any]r,   $tuple[Any, MutableMapping[str, Any]]c                 C  sZ   t | jpi }t  }r|jj|d< |jj|d< | j|d< ||di B |d< ||fS )z3Process log records to add Nexus operation context.service	operationr#   r   )	dictr   r.   rE   r   r   r2   r#   r;   )rO   r   r{   r   tctxr*   r*   r+   process  s   
zLoggerAdapter.process)rr   r   r   r   )r   r   r{   r   r,   r   )r%   r&   r'   r(   r}   r   r   r*   r*   r~   r+   r     s    r   ztemporalio.nexus)r,   r-   )r,   r!   )r,   r3   )r,   r5   )r,   r:   )r,   r>   )@
__future__r   r   loggingcollections.abcr   r   r   r   
contextlibr   contextvarsr   r	   datetimer
   typingr   r   r   r   r   r   r   nexusrpc.handlerr   r   typing_extensionsr   temporalio.api.common.v1rh   !temporalio.api.workflowservice.v1temporalio.commontemporalio.nexusr   temporalio.nexus._tokenr   temporalio.typesr   r   r   r   r   r   temporalio.clientr   r)   r   r   r!   r/   r2   r4   r1   r.   rB   rC   rD   ry   rU   r   r   	getLoggerrr   r*   r*   r*   r+   <module>   s`    $
 	






	

M  