
    Ki8                       d Z ddlmZ ddlZddlZddlmZmZm	Z	 ddl
mZmZ ddlZddl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mZ dd
lmZ ddl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*  e'e+      Z,	 d	 	 	 	 	 	 	 ddZ- ee   e      Z.e/ez  e0e/ef   z  e	e/ez  e0e/ef   z     z  Z1e1ee1   z  Z2 G d de)      Z G d de#      Z G d de      Z3y)z!Base classes for FastMCP prompts.    )annotationsN)	AwaitableCallableSequence)	AnnotatedAny)ContentBlockIconPromptMessageRoleTextContent)Prompt)PromptArgument)FieldTypeAdapter)PromptError)get_contextwithout_injected_parameters
TaskConfig)FastMCPComponent)compress_schema)
get_logger)FastMCPBaseModelget_cached_typeadapterc                `    t        | t              rt        d|       } |d}t        d| |d|S )z.A user-friendly constructor for PromptMessage.texttyper   user)contentrole )
isinstancestrr   r   )r!   r"   kwargss      b/home/jay/workspace/scripts/.codegraph-venv/lib/python3.12/site-packages/fastmcp/prompts/prompt.pyMessager(      s9     '368|>t>v>>    c                  j    e Zd ZU dZ ed      Zded<    edd      Zd	ed
<    edd      Zded<   y)r   z+An argument that can be passed to a prompt.zName of the argument)descriptionr%   nameNz%Description of what the argument doesdefaultr+   
str | Noner+   Fz Whether the argument is requiredboolrequired)	__name__
__module____qualname____doc__r   r,   __annotations__r+   r1   r#   r)   r'   r   r   4   sE    5"89D#9#"IK  #EHd r)   r   c                       e Zd ZU dZ edd      Zded<   d fdZd fdZdd		 	 	 	 	 dd
Z	e
	 	 	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd       Z	 d	 	 	 ddZ xZS )r   z7A prompt template that can be rendered with parameters.Nz*Arguments that can be passed to the promptr-   zlist[PromptArgument] | None	argumentsc                v    t         |           	 t               }|j                          y # t        $ r Y y w xY wN)superenabler   _queue_prompt_list_changedRuntimeErrorselfcontext	__class__s     r'   r<   zPrompt.enableG   s7    	!mG..0 		   , 	88c                v    t         |           	 t               }|j                          y # t        $ r Y y w xY wr:   )r;   disabler   r=   r>   r?   s     r'   rE   zPrompt.disableO   s7    	!mG..0 		rC   include_fastmcp_metac                  | j                   xs g D cg c].  }t        |j                  |j                  |j                        0 }}t        |j                  d| j                        |j                  d| j                        ||j                  d| j                        |j                  d| j                        |j                  d| j                  |                  S c c}w )	z$Convert the prompt to an MCP prompt.r,   r+   r1   r,   r+   titleicons_metarF   )r,   r+   r8   rJ   rK   rL   )
r8   SDKPromptArgumentr,   r+   r1   	SDKPromptgetrJ   rK   get_meta)r@   rG   	overridesargr8   s        r'   to_mcp_promptzPrompt.to_mcp_promptW   s     ~~+
  XXOO
	 
 vtyy1!mT5E5EF--4--4--<PQ	
 		

s   3C!c	                >    t         j                  | ||||||||	      S )Create a Prompt from a function.

        The function can return:
        - A string (converted to a message)
        - A Message object
        - A dict (converted to a message)
        - A sequence of any of the above
        	fnr,   rJ   r+   rK   tagsenabledmetatask)FunctionPromptfrom_functionrV   s	            r'   r]   zPrompt.from_functionr   s7    ( ++# , 

 
	
r)   c                    K   t        d      w)zRender the prompt with arguments.

        This method is not implemented in the base Prompt class and must be
        implemented by subclasses.
        z"Subclasses must implement render())NotImplementedError)r@   r8   s     r'   renderzPrompt.render   s      ""FGGs   )returnNone)rG   bool | NonerQ   r   ra   rN   NNNNNNNNrW   5Callable[..., PromptResult | Awaitable[PromptResult]]r,   r/   rJ   r/   r+   r/   rK   zlist[Icon] | NonerX   zset[str] | NonerY   rc   rZ   dict[str, Any] | Noner[   zbool | TaskConfig | Nonera   r\   r:   r8   rg   ra   zlist[PromptMessage])r2   r3   r4   r5   r   r8   r6   r<   rE   rS   staticmethodr]   r`   __classcell__)rB   s   @r'   r   r   @   s    A-2"N.I*  -1
 *
 	

 

6    "&#' $#&*)-
A

 
  	

 !
 
 
 $
 '
 

 
B ,0	H(	H 
	Hr)   r   c                      e Zd ZU dZded<    ed       Zded<   e	 	 	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd	       Zdd
Z		 d	 	 	 ddZ
y)r\   zA prompt that is a function.rf   rW   c                     t        d      S )N	forbiddenmoder   r#   r)   r'   <lambda>zFunctionPrompt.<lambda>   s    jk&B r)   )default_factoryz_Annotated[TaskConfig, Field(description='Background task execution configuration (SEP-1686).')]task_configNc
                   |xs% t        |dd      xs |j                  j                  }
|
dk(  rt        d      t	        j
                  |      }|j                  j                         D ]f  }|j                  t        j                  j                  k(  rt        d      |j                  t        j                  j                  k(  s]t        d       |xs t	        j                  |      }|	t        d      }n(t        |	t              rt        j                   |	      }n|	}|j#                  ||
       t	        j$                  |      s|j&                  }t        |t(              r|j*                  }t-        |      }t/        |      }|j1                         }t3        |d	
      }g }d|v r|d   j5                         D ]  \  }}|j7                  d      }||j                  v r|j                  |   }|j8                  t        j                  j:                  k7  r`|j8                  t<        urN	 t/        |j8                        }|j1                         }t?        j@                  |d      }d| }|r| d| }n|}|jE                  tG        ||||j7                  dg       v                | |
|||||xs
 tI               ||nd	|||
      S # tB        $ r Y ^w xY w)rU   r2   Nz<lambda>z,You must provide a name for lambda functionsz1Functions with *args are not supported as promptsz4Functions with **kwargs are not supported as promptsrm   rn   T)prune_titles
propertiesr+   ),:)
separatorsz8Provide as a JSON string matching the following schema: z

r1   rI   )
r,   rJ   r+   rK   r8   rX   rY   rW   rZ   rr   )%getattrrB   r2   
ValueErrorinspect	signature
parametersvalueskind	ParameterVAR_POSITIONALVAR_KEYWORDgetdocr   r$   r0   	from_boolvalidate_function	isroutine__call__ri   __func__r   r   json_schemar   itemsrO   
annotationemptyr%   jsondumps	Exceptionappendr   set)clsrW   r,   rJ   r+   rK   rX   rY   rZ   r[   	func_namesigparamrr   
wrapped_fntype_adapterr}   r8   
param_namearg_description	sig_paramparam_adapterparam_schema
schema_strschema_notes                            r'   r]   zFunctionPrompt.from_function   s   , RGB
D9RR\\=R=R	
"KLL#^^**, 	YEzzW..=== !TUUzzW..::: !WXX		Y "7W^^B%7 <$+6Kd#$..t4KK%%b)4   $Bb,'B 14
-j9!--/
$ZdC
 +-	:%%/%=%C%C%E #!
E"'))M": / #z :I!,,0A0A0G0GG%00;!,B9CWCW,XM+8+D+D+FL *.LZ)XJ -eeodp*qK.5D4ET+2W2=
   "'$3!+z~~j"/M!M;#J #&2G#
 	
  ) ! !s   +AK	KKc                   ddl m}  || j                        }t        j                  |      }i }|j                         D ]  \  }}||j                  v r|j                  |   }|j                  t        j                  j                  k(  s"|j                  t        u st        |t              s|||<   r	 t        |j                        }		 |	j                  |      ||<   |||<    |S # t        t        t         j"                  f$ r |	j%                  |      ||<   Y >w xY w# t        t        t         j"                  f$ r'}
t'        d| d| d|j                   d|
       |
d}
~
ww xY w)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   rW   r{   r|   r   r}   r   r   r   r%   r$   r   validate_jsonrz   	TypeErrorpydantic_coreValidationErrorvalidate_pythonr   )r@   r&   r   
wrapper_fnr   converted_kwargsr   param_valuer   adapteres              r'   _convert_string_argumentsz(FunctionPrompt._convert_string_arguments  s{   K09

+'-||~  	;#JS^^+z2 $$(9(9(?(??''3.#K53>$Z0!"89I9I"J;B;P;P +<,Z8  0; ,A 	;D   !+I}7T7TU ;B;R;R +<,Z8
 '	=3P3PQ !)::,nU`Ta b0050@0@/A1#O  !!!s6   3D	C''2DDDDE$="EE$c           
       K   | j                   rT| j                   D ch c]  }|j                  s|j                   }}t        |xs i       }||z
  }|rt	        d|       	 |r|j                         ni }| j                  |      } | j                  di |}t        j                  |      r
| d{   }t        |t        t        z        s|g}g }|D ]  }		 t        |	t              r|j                  |	       nt        |	t              r(|j                  t        dt!        d|	                   nPt#        j$                  |	t              j'                         }
|j                  t        dt!        d|
                    |S c c}w 7 # t(        $ r}t+        d      |d}~ww xY w# t(        $ rA}t,        j/                  d	| j                          t+        d	| j                   d
      |d}~ww xY ww)z!Render the prompt with arguments.zMissing required arguments: Nr    r   r   )r"   r!   )fallbackz+Could not convert prompt result to message.zError rendering prompt .r#   )r8   r1   r,   r   rz   copyr   rW   r{   isawaitabler$   listtupler   r   r%   r   r   to_jsondecoder   r   logger	exception)r@   r8   rR   r1   providedmissingr&   resultmessagesmsgr!   r   s               r'   r`   zFunctionPrompt.renderB  s     >>,0NNKScllKHK9?+H)G #?y!IJJ.	M)2Y^^%F 33F;F TWW&v&F""6*% fdUl3  -/H !#}5 ,#C- )%+(3c(J #0"7"7c"J"Q"Q"S )%+(3g(N0 Oc L" &6 ! %E  	M6tyykBC 7		{!DE1L	Msp   G;F
F
&G;$AF. 5F6%F. B*FF. 	G;F. 	F+F&&F++F. .	G87<G33G88G;rd   re   )r&   dict[str, Any]ra   r   r:   rh   )r2   r3   r4   r5   r6   r   rr   classmethodr]   r   r`   r#   r)   r'   r\   r\      s    &== 	BC   D
    "&#' $#&*)-l
Al
 l
 	l

  l
 !l
 l
 l
 $l
 'l
 
l
 l
\* \ ,0;M(;M 
;Mr)   r\   r:   )r!   zstr | ContentBlockr"   zRole | Noner&   r   ra   r   )4r5   
__future__r   _annotationsr{   r   collections.abcr   r   r   typingr   r   r   	mcp.typesr	   r
   r   r   r   r   rN   r   rM   pydanticr   r   fastmcp.exceptionsr   r   r   r   fastmcp.server.tasks.configr   fastmcp.utilities.componentsr   fastmcp.utilities.json_schemar   fastmcp.utilities.loggingr   fastmcp.utilities.typesr   r   r2   r   r(   message_validatorr%   dictSyncPromptResultPromptResultr\   r#   r)   r'   <module>r      s   ' 2   9 9 !  J J ) 9 ' * P 2 9 9 0
 
H	 6:??'2?EH?? /K.}=  
38n s]"T#s(^345   ),<"==	% 	[H [H|_MV _Mr)   