
    kKj/1                        U d Z ddlmZ ddlZddl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ZddlmZ ddlZdd	lmZ erdd
lmZ ddlmZ  ee      Z edd      Zded<    G d d      Zy)z;Lifespan and Docket task infrastructure for FastMCP Server.    )annotationsN)AsyncIterator)AsyncExitStackasynccontextmanagersuppress)
ContextVar)TYPE_CHECKINGAny)SharedContext)
get_logger)Docket)FastMCPfastmcp_lifespan_root_activeF)defaultzContextVar[bool]_lifespan_root_activec                  `    e Zd ZdZedd       Zed	d       Zed	d       Zed	d       Z	d
dZ
y)LifespanMixinzDMixin providing lifespan and Docket task infrastructure for FastMCP.c                    | j                   S )u   The Docket instance owned by this server.

        Returns the Docket that this server initialized as the root of a
        runtime tree. Mounted children do not own their own Docket — they
        share the root's via ``_current_docket`` ContextVar inheritance —
        so accessing ``.docket`` on a mounted child returns None even while
        its tasks run on the root's Docket. For "the Docket in scope right
        now," prefer reading ``_current_docket`` directly or use the
        ``CurrentDocket`` dependency injection.
        )_docketselfs    o/Users/ahmed/devFolder/Ultron/claude-voice/.venv/lib/python3.12/site-packages/fastmcp/server/mixins/lifespan.pydocketzLifespanMixin.docket,   s     ||    c                  K   t         j                         rd y| j                         4 d{    d ddd      d{    y7 7 # 1 d{  7  sw Y   yxY ww)uR  Manage Docket instance and Worker for background task execution.

        Docket is process-level, not server-level: only the first server in a
        runtime tree starts Docket and the Worker. Mounted children entered
        via ``FastMCPProvider.lifespan`` see ``_lifespan_root_active=True``
        (set by the provider before delegating to ``_lifespan_manager``) and
        become no-ops, sharing the root's Docket via ``_current_docket``.

        Independent servers entered as siblings — for example two unrelated
        ``FastMCP`` instances each entered through ``AsyncExitStack`` in the
        same async context — are not in a parent/child relationship; no
        provider has set the flag for them, so each runs the full root setup.

        Docket infrastructure is only initialized at the root if:
        1. pydocket is installed (fastmcp[tasks] extra)
        2. There are task-enabled components (task_config.mode != 'forbidden')

        Users with pydocket installed but no task-enabled components won't spin
        up Docket / Worker infrastructure even at the root.
        N)r   get_docket_lifespan_rootr   s    r   _docket_lifespanzLifespanMixin._docket_lifespan:   sE     2 !$$&--// 0//////sC   /A%AA%AA%AA%A%A"AA"A%c           
      K   ddl m}m} |j                  t	        j
                  |             }	  |       s;t               4 d{    d ddd      d{    	 |j                  |       y	 t        | j                          d{         }|s;t               4 d{    d ddd      d{    	 |j                  |       yddlm}m}m} ddlm}	 ddl m}
m} ddlm}  ||	j                   j0                  |	j                   j2                  	      4 d{   }|| _        |D ]  }|j7                  |        |
j                  |      }	 |	j                   j8                  |	j                   j:                  |	j                   j<                  |	j                   j>                  d
}|	j                   j@                  r|	j                   j@                  |d<    ||fd ||      gi|4 d{   }|| _!        |j                  |      }	 tE        jF                  |jI                               }	 d |jK                          tM        tD        jN                        5  | d{    ddd       |j                  |       d| _!        ddd      d{    |
j                  |       d| _        ddd      d{    |j                  |       y7 7 # 1 d{  7  sw Y   xY w7 m# t        $ r@}t        j                  d|        t        j                  j                  r g }Y d}~d}~ww xY w7 7 # 1 d{  7  sw Y   xY w7 >7 o7 # 1 sw Y   xY w# |jK                          tM        tD        jN                        5  | d{  	7   ddd       w # 1 sw Y   w xY wxY w# |j                  |       d| _!        w xY w7 E# 1 d{  7  sw Y   VxY w# |
j                  |       d| _        w xY w7 S# 1 d{  7  sw Y   dxY w# |j                  |       w xY ww)zBRoot-only Docket lifecycle. See _docket_lifespan for the dispatch.r   )_current_serveris_docket_availableNzFailed to get tasks: )Dependsr   Worker)settings)_current_docket_current_worker)restore_task_snapshot)nameurl)concurrencyredelivery_timeoutreconnection_delayminimum_check_intervalr(   dependencies)(fastmcp.server.dependenciesr    r!   setweakrefrefr   resetlist	get_tasks	Exceptionloggerwarningfastmcpr$   &mounted_components_raise_on_load_errorr   r"   r   r#   r%   r&   fastmcp.server.tasks.contextr'   r(   r)   r   register_with_docketr*   r+   r,   r-   worker_name_workerasynciocreate_taskrun_forevercancelr   CancelledError)r   r    r!   server_tokentask_componentser"   r   r#   r$   r%   r&   r'   r   	componentdocket_tokenworker_kwargsworkerworker_tokenworker_tasks                       r   r   z#LifespanMixin._docket_lifespan_rootZ   s     	U '**7;;t+<=V	0 '((?? +?` !!,/W%"&T^^-='="> #(?? +?| !!,/w 76( K __))OO''  % "1I226: "1  /226:#( (0'B'B.6oo.P.P.6oo.P.P2://2X2X	5M  22080K0Kf-  & &-.C&D%E  (     '-'6':':6'B
0*1*=*=f>P>P>R*SK6 % + 2 2 4%-g.D.D%E*5$5$5 &F ,11,?+/DL#   & $)),7#'DL] b !!,/e +???? (> %!6qc:;##JJ"$	% +????4  %6 &F%E !, 2 2 4%-g.D.D%E*5$5$5 &F%E%E ,11,?+/DL#       & $)),7#'DL]   b !!,/s  -QP1 KP1 	K"P1 KP1  Q3K; K8K; P1 $M%P1 (M.P1 9M
:P1 ?QAP1 %M#&P1 )1PBO;7M&8O;;O%#O9M7>)O'M+-M)
.M+2O:O%O;O"O;"P:P1 PP1 
QP1 P1 "K5(K+)K50P1 8K; ;	M5L?9P1 ?MP1 
P1 M MM 	P1 &O;)M++M40O7*O!N6'N*(N6-	O6N?;OOOO%"O;%O8	+O.,O8	3O;;PPP1 P.!P$"P.)P1 1QQc               z	  K   | j                   4 d {    | j                  r| xj                  dz  c_        d}n	d| _        d}d d d       d {    s^	 d  | j                   4 d {    | xj                  dz  c_        | j                  dk(  rd| _        d | _        d d d       d {    y t	               }	 |j                  | j                  |              d {   }|j                  | j                                d {    || _        d| _        | j                  D ])  }|j                  |j                                d {    + | j                  j                          	 d  | j                  j                          	 	 t        j                  d      5  |j                          d {    d d d        | j                   4 d {    | xj                  dz  c_        | j                  dk(  rd| _        d | _        d d d       d {    y 7 7 # 1 d {  7  sw Y   xY w7 7 # 1 d {  7  sw Y   y xY w# | j                   4 d {  7   | xj                  dz  c_        | j                  dk(  rd| _        d | _        d d d       d {  7   w # 1 d {  7  sw Y   w xY wxY w7 7 7 # | j                  j                          w xY w7 7# 1 sw Y   7xY w7 *7 # 1 d {  7  sw Y   y xY w# | j                   4 d {  7   | xj                  dz  c_        | j                  dk(  rd| _        d | _        d d d       d {  7   w # 1 d {  7  sw Y   w xY wxY w# 	 t        j                  d      5  |j                          d {  7   d d d        n# 1 sw Y   nxY w| j                   4 d {  7   | xj                  dz  c_        | j                  dk(  rd| _        d | _        d d d       d {  7   w # 1 d {  7  sw Y   w xY w# | j                   4 d {  7   | xj                  dz  c_        | j                  dk(  rd| _        d | _        d d d       d {  7   w # 1 d {  7  sw Y   w xY wxY wxY ww)N   FTr   )shield)_lifespan_lock_lifespan_result_set_lifespan_ref_count_lifespan_resultr   enter_async_context	_lifespanr   	providerslifespan_startedr0   clearanyioCancelScopeaclose)r   should_enter_lifespanstackuser_lifespan_resultproviders        r   _lifespan_managerzLifespanMixin._lifespan_manager   s    &&&((((A-((-%+,((,% '& %5...,,1,//1449104-	 /.
   	5).)B)B4>>RVCW)X#X ++D,A,A,CDDD$8D!(,D% !NN//0A0A0CDDD + MM&##%5&&d3,,.(( 4  ...,,1,//1449104-	 /..[ '&&&& /....
   ...,,1,//1449104-	 /.... $YD E ##% ) 43 /....4...,,1,//1449104-	 /....	5&&d3,,.(( 433  ...,,1,//1449104-	 /....4...,,1,//1449104-	 /....s  R;H R;.H&R;H#R;I R;-H<.R;13I$R;/H?0R; #N #K
$&N 
KAN K N 0K 5N L 'K5;K2<K5 L R;LR;3LR;LR;#R;&H9,H/-H94	R;?R;II	IR;K(I+
)K-3J2 K+J.,K2K8J;9K KR;
N N N K//N 2K55K?:L R;R;LLLR;N-L0
.N23M7%N0M31N7N	=N >N	NR;R8Q'O
;N>
<O
	Q
OQR8&O)
'R8+3P0R8)P,*R80Q6P97Q>R8R5QR53R R5RR5 R2&R)'R2.R55R88R;c                h   
 ddl m}  |       syddlm}m}m}m}m
 ddlm	m
mm d

 fd}d
 fd}d
 fd}d
 fd	}	| j                  j                  |<   | j                  j                  |<   | j                  j                  |<   |	 j                  j                  |<   y)zRegister SEP-1686 task protocol handlers with SDK.

        Only registers handlers if docket is installed. Without docket,
        task protocol requests will return "method not found" errors.
        r   )r!   N)CancelTaskRequestGetTaskPayloadRequestGetTaskRequestListTasksRequestServerResult)tasks_cancel_handlertasks_get_handlertasks_list_handlertasks_result_handlerc                |   K   | j                   j                  dd      } |       d {   } |      S 7 wNT)by_aliasexclude_noneparams
model_dump)reqrq   resultrg   r   ri   s      r   handle_get_taskzDLifespanMixin._setup_task_protocol_handlers.<locals>.handle_get_task  s?     ZZ**Dt*LF,T6::F'' ;   *<:<c                |   K   | j                   j                  dd      } |       d {   } |      S 7 wrm   rp   )rs   rq   rt   rg   r   rk   s      r   handle_get_task_resultzKLifespanMixin._setup_task_protocol_handlers.<locals>.handle_get_task_result  ?     ZZ**Dt*LF/f==F'' >rv   c                   K   | j                   r| j                   j                  dd      ni } |       d {   } |      S 7 wrm   rp   )rs   rq   rt   rg   r   rj   s      r   handle_list_taskszFLifespanMixin._setup_task_protocol_handlers.<locals>.handle_list_tasks  sR      :: 

%%t$%G 
 .dF;;F'' <s   8A
AA
c                |   K   | j                   j                  dd      } |       d {   } |      S 7 wrm   rp   )rs   rq   rt   rg   r   rh   s      r   handle_cancel_taskzGLifespanMixin._setup_task_protocol_handlers.<locals>.handle_cancel_task   ry   rv   )rs   re   returnrg   )rs   rd   r~   rg   )rs   rf   r~   rg   )rs   rc   r~   rg   )r/   r!   	mcp.typesrc   rd   re   rf   rg   fastmcp.server.tasks.requestsrh   ri   rj   rk   _mcp_serverrequest_handlers)r   r!   rc   rd   re   rf   ru   rx   r{   r}   rg   rh   ri   rj   rk   s   `         @@@@@r   _setup_task_protocol_handlersz+LifespanMixin._setup_task_protocol_handlers   s     	D"$	
 	
	
 	
	(
	(
	(	( =L)).9" 	))*?@ ?P))*:;?Q))*;<r   N)r   r   r~   zDocket | None)r   r   r~   zAsyncIterator[None])r   r   r~   None)__name__
__module____qualname____doc__propertyr   r   r   r   ra   r    r   r   r   r   )   s]    N   > ^0 ^0@ 25 25h;Rr   r   )r   
__future__r   r?   r1   collections.abcr   
contextlibr   r   r   contextvarsr   typingr	   r
   rZ   uncalled_forr   r9   fastmcp.utilities.loggingr   r   r   fastmcp.server.serverr   r   r7   r   __annotations__r   r   r   r   <module>r      sg    A "   ) D D " %  &  0-	H	 +5"E+ ' 
BR BRr   