
    $jM                       d Z ddlmZ ddlZddlZddlZddlZddlmZ ddl	m
Z
mZ ddlmZmZmZmZmZmZmZ ddlZddlmZ ddlmZ ddlZdd	lmZ dd
lmZ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) ddl*m+Z+m,Z, ddl-m.Z.m/Z/ ddl0m1Z1 ddl2m3Z3 ddl4m5Z5 erddl6m7Z7 ddl8m9Z9  ededef                   Z: e3e;          Z<e G d de                      Z= e
dd           G d d                      Z> G d d e           Z?ed?d#            Z@eddddddddd$d@d7            Z@e	 dAdddddddddd8	dBd;            Z@	 dAdddddddddd8	dCd>Z@dS )Dz)Standalone @prompt decorator for FastMCP.    )annotationsN)Callable)	dataclassfield)TYPE_CHECKINGAnyLiteralProtocolTypeVaroverloadruntime_checkable)Icon)SkipJsonSchema)resolve_task_config)FastMCPDeprecationWarningPromptError)PromptPromptArgumentPromptResult)	AuthCheck)transform_context_annotationswithout_injected_parameters)
TaskConfig)call_sync_fn_in_threadpoolis_coroutine_function)ParsedDocstringparse_docstring)compress_schema)
get_logger)get_cached_typeadapter)Docket)	ExecutionF.)boundc                  &    e Zd ZU dZded<   d
dZd	S )DecoratedPromptz.Protocol for functions decorated with @prompt.
PromptMeta__fastmcp__argsr   kwargsreturnc                    d S N )selfr)   r*   s      g/home/rurouni/.hermes/hermes-agent/venv/lib/python3.11/site-packages/fastmcp/prompts/function_prompt.py__call__zDecoratedPrompt.__call__;   s          N)r)   r   r*   r   r+   r   )__name__
__module____qualname____doc____annotations__r1   r.   r2   r0   r&   r&   5   s0         88======r2   r&   T)frozenkw_onlyc                      e Zd ZU dZ edd          Zded<   dZded	<   dZd
ed<   dZ	ded<   dZ
ded<   dZded<   dZded<   dZded<   dZded<   dZded<   dZded<   dS )r'   z8Metadata attached to functions by the @prompt decorator.promptF)defaultinitzLiteral['prompt']typeN
str | Nonenamestr | int | Noneversiontitledescriptionlist[Icon] | Noneiconsset[str] | Nonetagsdict[str, Any] | Nonemetabool | TaskConfig | Nonetask"AuthCheck | list[AuthCheck] | NoneauthTboolenabled)r3   r4   r5   r6   r   r>   r7   r@   rB   rC   rD   rF   rH   rJ   rL   rN   rP   r.   r2   r0   r'   r'   >   s         BB#eH5AAADAAAAD $G$$$$E"K""""#E#### D    "&D&&&&%)D))))/3D3333Gr2   r'   c                  z    e Zd ZU dZded<   eddddddddddd
d+d            Zd,dZ	 d-d.d Zd/d$Z	ddd%d0d*Z
dS )1FunctionPromptzA prompt that is a function.z"SkipJsonSchema[Callable[..., Any]]fnN)
metadatar@   rB   rC   rD   rF   rH   rJ   rL   rN   Callable[..., Any]rT   PromptMeta | Noner@   r?   rB   rA   rC   rD   rF   rE   rH   rG   rJ   rI   rL   rK   rN   rM   r+   c       
           t          d |||||||	|
|f	D                       }||rt          d          |t          |||||||	|
|	  	        }|j        pt	          |dd          p|j        j        }|dk    rt          d          t          j	        |          }|j
                                        D ]T}|j        t          j        j        k    rt          d          |j        t          j        j        k    rt          d	          Ut!          |          }|j        }|t%          d
          }n,t'          |t(                    rt%          j        |          }n|}|                    ||           t          j        |          s!t'          |t0          j                  s|j        }t'          |t6                    r|j        }t!          |          }t;          |j        p|j        |j
                  }|j        |j        n|j        }t?          |          }tA          |          }tC          |          }|"                                }tG          |d          }|j
        rN|$                    di           }|j
        %                                D ]\  }}||v rd||         vr|||         d<   g }d|v r4|d         %                                D ]\  }}|$                    d          }||j
        v r|j
        |         }|j&        t          j        j'        k    r|j&        tP          ur	 tC          |j&                  }|"                                }tS          j*        |d          } d|  }!|r| d|! }n|!}n3# tV          $ r&}"tX          -                    d||"           Y d}"~"nd}"~"ww xY w|.                    t_          ||||$                    dg           v                       | ||j0        tQ          |j0                  nd|j1        ||j2        ||j3        pti                      ||j5        ||j6                  S )a'  Create a Prompt from a function.

        Args:
            fn: The function to wrap
            metadata: PromptMeta object with all configuration. If provided,
                individual parameters must not be passed.
            name, title, etc.: Individual parameters for backwards compatibility.
                Cannot be used together with metadata parameter.

        The function can return:
        - str: wrapped as single user Message
        - list[Message | str]: converted to list[Message]
        - PromptResult: used directly
        c              3     K   | ]}|d uV  	d S r-   r.   ).0xs     r0   	<genexpr>z/FunctionPrompt.from_function.<locals>.<genexpr>t   s:       )
 )
 TM)
 )
 )
 )
 )
 )
r2   Nz|Cannot pass both 'metadata' and individual parameters to from_function(). Use metadata alone or individual parameters alone.	r@   rB   rC   rD   rF   rH   rJ   rL   rN   r3   z<lambda>z,You must provide a name for lambda functionsz1Functions with *args are not supported as promptsz4Functions with **kwargs are not supported as prompts	forbidden)mode)rD   
parametersT)prune_titles
propertiesrD   ),:)
separatorsz8Provide as a JSON string matching the following schema: z

z4Failed to generate schema for prompt argument %s: %srequired)r@   rD   re   )r@   rB   rC   rD   rF   	argumentsrH   rS   rJ   task_configrN   )7any	TypeErrorr'   r@   getattr	__class__r3   
ValueErrorinspect	signaturer_   valueskind	ParameterVAR_POSITIONALVAR_KEYWORDr   rL   r   
isinstancerO   	from_boolvalidate_function	isroutine	functoolspartialr1   staticmethod__func__r   rD   r   r   r    json_schemar   getitems
annotationemptystrjsondumps	Exceptionloggerdebugappendr   rB   rC   rF   rH   setrJ   rN   )#clsrS   rT   r@   rB   rC   rD   rF   rH   rJ   rL   rN   individual_params_provided	func_namesigparamouter_docstring
task_valuerg   inner_docstringparsed_docstring
wrapped_fntype_adapterr_   ra   
param_name
param_descrf   arg_description	sig_paramparam_adapterparam_schema
schema_strschema_notees#                                      r0   from_functionzFunctionPrompt.from_functionT   s/   @ &) )
 )
GUKdDRVW)
 )
 )
 &
 &
"
 $>E   !'
 
 
H MSWRT::Sbl>S 	 
""KLLL ##^**,, 	Y 	YEzW.=== !TUUUzW.::: !WXXX ;
 *"-- ]
$+666KK
D)) 	%$.z::KK$K%%b)444  $$ 	ZI<M-N-N 	Bb,'' 	B *"--*'3R7R&1
 
 
 #/   !- 	 +2.. 144
-j99!--//
$ZdCCC

 & 	G#b99J*:*E*K*K*M*M G G&
J*,,%Z
-CCC<FJz*=9 +-	:%%%/%=%C%C%E%E ' '!
E"'))M":": // #z :I!,0A0GGG%0;;,B9CW,X,XM+8+D+D+F+FL *.LZ)X)X)XJ +reo*q*qK. >5D2W2W+2W2W2=(   "LL V * !          "'$3!+z~~j"/M/M!M      s-5-=-IC()))t.#.'#%%#
 
 
 	
s   /AM??
N/	N**N/r*   dict[str, Any]c                   ddl m}  || j                  }t          j        |          }i }|                                D ]\  }}||j        v r|j        |         }|j        t          j        j	        k    s#|j        t          u st          |t                    s|||<   _	 t          |j                  }		 |	                    |          ||<   n:# t          t          t           j        f$ r |	                    |          ||<   Y nw xY w# t          t          t           j        f$ r&}
t'          d| d| d|j         d|
           |
d}
~
ww xY w|||<   |S )zGConvert string arguments to expected types based on function signature.r   )r   zCould not convert argument 'z' with value 'z' to expected type z	. Error: N)fastmcp.server.dependenciesr   rS   rm   rn   r~   r_   r   rq   r   r   rt   r    validate_jsonrl   ri   pydantic_coreValidationErrorvalidate_pythonr   )r/   r*   r   
wrapper_fnr   converted_kwargsr   param_valuer   adapterr   s              r0   _convert_string_argumentsz(FunctionPrompt._convert_string_arguments  s   KKKKKK0099

++'-||~~  	;  	;#JS^++z2 $(9(???'3..#K55 /3>$Z00!"89I"J"J;B;P;P +< <,Z88 !+I}7TU   ;B;R;R +< <,Z888
 '	=3PQ ! ! !)O: O OU` O O050@O OKLO O   !!! 0; ,,s<   !D
6CD
4DD
DD

E&!EErf   r   c                d  
K   | j         r;d | j         D             }t          |pi           }||z
  }|rt          d|           	 |r|                                ni }|                     |          }t          j        | j                  }t          |j        	                                          

fd|
                                D             }t          | j                  }t          | j                  r|                    |           d{V }n7t          |j        |           d{V }t          j        |          r| d{V }|                     |          S # t"          $ rB}	t$                              d| j                    t+          d| j        d|	           |	d}	~	ww xY w)z!Render the prompt with arguments.c                *    h | ]}|j         	|j        S r.   )re   r@   )rY   args     r0   	<setcomp>z(FunctionPrompt.render.<locals>.<setcomp>E  s!    KKKSclKKKKr2   zMissing required arguments: c                $    i | ]\  }}|v 	||S r.   r.   )rY   kvvalid_paramss      r0   
<dictcomp>z)FunctionPrompt.render.<locals>.<dictcomp>X  s)    KKKtq!l9J9Ja9J9J9Jr2   NzError rendering prompt z: )rf   r   rl   copyr   rm   rn   rS   r_   keysr~   r    r   r   r   isawaitableconvert_resultr   r   	exceptionr@   r   )r/   rf   re   providedmissingr*   r   r   resultr   r   s             @r0   renderzFunctionPrompt.render>  s      > 	KKKDNKKKH9?++H)G K !I!I!IJJJ#	S)2:Y^^%%%F 33F;;F #DG,,Cs~224455LKKKKv||~~KKKF 2$'::L %TW-- 	*+;;FCCCCCCCC  : 0&          &v.. *#)\\\\\\F&&v... 	S 	S 	SBtyBBCCCJ	JJqJJKKQRR	Ss   DE# #
F/-=F**F/docketr!   Nonec                    | j                                         sdS |                    | j        | j        g           dS )z:Register this prompt with docket for background execution.N)names)rg   supports_tasksregisterrS   key)r/   r   s     r0   register_with_docketz#FunctionPrompt.register_with_docketp  s@    ..00 	Fz22222r2   )fn_keytask_keyr   r   r   r"   c               ^   K   |p| j         }|r||d<     |j        |fi |di |pi  d{V S )a  Schedule this prompt for background execution via docket.

        FunctionPrompt splats the arguments dict since .fn expects **kwargs.

        Args:
            docket: The Docket instance
            arguments: Prompt arguments
            fn_key: Function lookup key in Docket registry (defaults to self.key)
            task_key: Redis storage key for the result
            **kwargs: Additional kwargs passed to docket.add()
        r   Nr.   )r   add)r/   r   rf   r   r   r*   
lookup_keys          r0   add_to_docketzFunctionPrompt.add_to_docketv  se      ( 'tx
 	%$F5M5ZVZ
55f55JJbJJJJJJJJJr2   )rS   rU   rT   rV   r@   r?   rB   rA   rC   r?   rD   r?   rF   rE   rH   rG   rJ   rI   rL   rK   rN   rM   r+   rR   )r*   r   r+   r   r-   )rf   rI   r+   r   )r   r!   r+   r   )r   r!   rf   rI   r   r?   r   r?   r*   r   r+   r"   )r3   r4   r5   r6   r7   classmethodr   r   r   r   r   r.   r2   r0   rR   rR   O   s         &&****
 '+$( "&#' $&*)-37{
 {
 {
 {
 {
 [{
z*  *  *  * \ ,00S 0S 0S 0S 0Sd3 3 3 3 "#K K K K K K K Kr2   rR   rS   r+   c                    d S r-   r.   )rS   s    r0   r;   r;     s    r2   )rB   rC   rD   rF   rH   rJ   rL   rN   
name_or_fnr   rB   rA   rC   r?   rD   rF   rE   rH   rG   rJ   rI   rL   rK   rN   rM   Callable[[F], F]c                   d S r-   r.   )	r   rB   rC   rD   rF   rH   rJ   rL   rN   s	            r0   r;   r;     s	     sr2   r\   r   r@   c       	            d S r-   r.   )
r   r@   rB   rC   rD   rF   rH   rJ   rL   rN   s
             r0   r;   r;     s	     sr2   str | Callable[..., Any] | Noner   c       	   	       	 t          | t                    rt          d          d	fdd	fd
dfdt          j        |           r | |          S t          | t
                    r|t          d          | n$| |nt          dt          |                      dfd}
|
S )zStandalone decorator to mark a function as an MCP prompt.

    Returns the original function with metadata attached. Register with a server
    using mcp.add_prompt().
    zwTo decorate a classmethod, use @classmethod above @prompt. See https://gofastmcp.com/servers/prompts#using-with-methodsrS   rU   prompt_namer?   r+   rR   c                    t          |
	t                    	  	        }t                              | |          S )Nr\   )rT   )r'   r   rR   r   )rS   r   prompt_metarN   rD   rF   rJ   rH   rL   rC   rB   s      r0   create_promptzprompt.<locals>.create_prompt  sW     !#$T**

 

 

 ++B+EEEr2   r#   c                x    t          |
		  	        }t          | d          r| j        n| }||_        | S )Nr\   r{   )r'   hasattrr{   r(   )rS   r   rT   targetrN   rD   rF   rJ   rH   rL   rC   rB   s       r0   attach_metadatazprompt.<locals>.attach_metadata  s[    #

 

 

 !(J 7 7?R%	r2   c                    t           j        j        dk    r(t          j        dt
          d            | |          S  | |          S )Nobjectzdecorator_mode='object' is deprecated and will be removed in a future version. Decorators now return the original function with metadata attached.   )
stacklevel)fastmcpsettingsdecorator_modewarningswarnr   )rS   r   r   r   s     r0   	decoratorzprompt.<locals>.decorator  s_    *h66MV)	    !=[111r;///r2   Nz6Cannot specify name both as first argument and keywordzInvalid first argument: c                     |           S r-   r.   )rS   r   r   s    r0   wrapperzprompt.<locals>.wrapper  s    y[)))r2   )rS   rU   r   r?   r+   rR   )rS   r#   r   r?   r+   r#   rS   r#   r+   r#   )rt   r   ri   rm   rw   r   r>   )r   r@   rB   rC   rD   rF   rH   rJ   rL   rN   r   r   r   r   r   s     ```````` @@@@r0   r;   r;     s   $ *k** 
K
 
 	

F F F F F F F F F F F F F"             	0 	0 	0 	0 	0 	0 	0 $$ 	GyT***	J	$	$ GTUUU 		E4
3C3CEEFFF* * * * * * * Nr2   r   )r   r   rB   rA   rC   r?   rD   r?   rF   rE   rH   rG   rJ   rI   rL   rK   rN   rM   r+   r   r-   )r   r   r@   r?   rB   rA   rC   r?   rD   r?   rF   rE   rH   rG   rJ   rI   rL   rK   rN   rM   r+   r   )r   r   r@   r?   rB   rA   rC   r?   rD   r?   rF   rE   rH   rG   rJ   rI   rL   rK   rN   rM   r+   r   )Ar6   
__future__r   rx   rm   r   r   collections.abcr   dataclassesr   r   typingr   r   r	   r
   r   r   r   r   	mcp.typesr   pydantic.json_schemar   r   fastmcp.decoratorsr   fastmcp.exceptionsr   r   fastmcp.prompts.baser   r   r   !fastmcp.server.auth.authorizationr   r   r   r   fastmcp.server.tasks.configr   fastmcp.utilities.async_utilsr   r   #fastmcp.utilities.docstring_parsingr   r   fastmcp.utilities.json_schemar   fastmcp.utilities.loggingr   fastmcp.utilities.typesr    r   r!   docket.executionr"   r#   r3   r   r&   r'   rR   r;   r.   r2   r0   <module>r      s!   / / " " " " " "        $ $ $ $ $ $ ( ( ( ( ( ( ( (                            / / / / / /  2 2 2 2 2 2 E E E E E E E E E E E E E E E E E E 7 7 7 7 7 7        3 2 2 2 2 2        Q P P P P P P P 9 9 9 9 9 9 0 0 0 0 0 0 : : : : : : +******GCxS)***	H		 > > > > >h > > > $%%%       &% ~K ~K ~K ~K ~KV ~K ~K ~KB
 
    
 	 !%"# "&%)/3     
 
  $"# "&%)/3     
  37R  $"# "&%)/3R R R R R R R Rr2   