|
| 1 | +use darling::{FromMeta, ast::NestedMeta}; |
| 2 | +use proc_macro2::TokenStream; |
| 3 | +use quote::{format_ident, quote}; |
| 4 | +use syn::{Expr, Ident, ImplItemFn, ReturnType}; |
| 5 | + |
| 6 | +use crate::common::{extract_doc_line, none_expr}; |
| 7 | + |
| 8 | +#[derive(FromMeta, Default, Debug)] |
| 9 | +#[darling(default)] |
| 10 | +pub struct PromptAttribute { |
| 11 | + /// The name of the prompt |
| 12 | + pub name: Option<String>, |
| 13 | + /// Optional description of what the prompt does |
| 14 | + pub description: Option<String>, |
| 15 | + /// Arguments that can be passed to the prompt |
| 16 | + pub arguments: Option<Expr>, |
| 17 | +} |
| 18 | + |
| 19 | +pub struct ResolvedPromptAttribute { |
| 20 | + pub name: String, |
| 21 | + pub description: Option<String>, |
| 22 | + pub arguments: Expr, |
| 23 | +} |
| 24 | + |
| 25 | +impl ResolvedPromptAttribute { |
| 26 | + pub fn into_fn(self, fn_ident: Ident) -> syn::Result<ImplItemFn> { |
| 27 | + let Self { |
| 28 | + name, |
| 29 | + description, |
| 30 | + arguments, |
| 31 | + } = self; |
| 32 | + let description = if let Some(description) = description { |
| 33 | + quote! { Some(#description.into()) } |
| 34 | + } else { |
| 35 | + quote! { None } |
| 36 | + }; |
| 37 | + let tokens = quote! { |
| 38 | + pub fn #fn_ident() -> rmcp::model::Prompt { |
| 39 | + rmcp::model::Prompt { |
| 40 | + name: #name.into(), |
| 41 | + description: #description, |
| 42 | + arguments: #arguments, |
| 43 | + } |
| 44 | + } |
| 45 | + }; |
| 46 | + syn::parse2::<ImplItemFn>(tokens) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +pub fn prompt(attr: TokenStream, input: TokenStream) -> syn::Result<TokenStream> { |
| 51 | + let attribute = if attr.is_empty() { |
| 52 | + Default::default() |
| 53 | + } else { |
| 54 | + let attr_args = NestedMeta::parse_meta_list(attr)?; |
| 55 | + PromptAttribute::from_list(&attr_args)? |
| 56 | + }; |
| 57 | + let mut fn_item = syn::parse2::<ImplItemFn>(input.clone())?; |
| 58 | + let fn_ident = &fn_item.sig.ident; |
| 59 | + |
| 60 | + let prompt_attr_fn_ident = format_ident!("{}_prompt_attr", fn_ident); |
| 61 | + |
| 62 | + // Try to find prompt parameters from function parameters |
| 63 | + let arguments_expr = if let Some(arguments) = attribute.arguments { |
| 64 | + arguments |
| 65 | + } else { |
| 66 | + // Look for a type named Parameters in the function signature |
| 67 | + let params_ty = crate::common::find_parameters_type_impl(&fn_item); |
| 68 | + |
| 69 | + if let Some(params_ty) = params_ty { |
| 70 | + // Generate arguments from the type's schema with caching |
| 71 | + syn::parse2::<Expr>(quote! { |
| 72 | + rmcp::handler::server::prompt::cached_arguments_from_schema::<#params_ty>() |
| 73 | + })? |
| 74 | + } else { |
| 75 | + // No arguments |
| 76 | + none_expr()? |
| 77 | + } |
| 78 | + }; |
| 79 | + |
| 80 | + let name = attribute.name.unwrap_or_else(|| fn_ident.to_string()); |
| 81 | + let description = attribute |
| 82 | + .description |
| 83 | + .or_else(|| fn_item.attrs.iter().fold(None, extract_doc_line)); |
| 84 | + let arguments = arguments_expr; |
| 85 | + |
| 86 | + let resolved_prompt_attr = ResolvedPromptAttribute { |
| 87 | + name: name.clone(), |
| 88 | + description: description.clone(), |
| 89 | + arguments: arguments.clone(), |
| 90 | + }; |
| 91 | + let prompt_attr_fn = resolved_prompt_attr.into_fn(prompt_attr_fn_ident.clone())?; |
| 92 | + |
| 93 | + // Modify the input function for async support (same as tool macro) |
| 94 | + if fn_item.sig.asyncness.is_some() { |
| 95 | + // 1. remove asyncness from sig |
| 96 | + // 2. make return type: `futures::future::BoxFuture<'_, #ReturnType>` |
| 97 | + // 3. make body: { Box::pin(async move { #body }) } |
| 98 | + let new_output = syn::parse2::<ReturnType>({ |
| 99 | + let mut lt = quote! { 'static }; |
| 100 | + if let Some(receiver) = fn_item.sig.receiver() { |
| 101 | + if let Some((_, receiver_lt)) = receiver.reference.as_ref() { |
| 102 | + if let Some(receiver_lt) = receiver_lt { |
| 103 | + lt = quote! { #receiver_lt }; |
| 104 | + } else { |
| 105 | + lt = quote! { '_ }; |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + match &fn_item.sig.output { |
| 110 | + syn::ReturnType::Default => { |
| 111 | + quote! { -> futures::future::BoxFuture<#lt, ()> } |
| 112 | + } |
| 113 | + syn::ReturnType::Type(_, ty) => { |
| 114 | + quote! { -> futures::future::BoxFuture<#lt, #ty> } |
| 115 | + } |
| 116 | + } |
| 117 | + })?; |
| 118 | + let prev_block = &fn_item.block; |
| 119 | + let new_block = syn::parse2::<syn::Block>(quote! { |
| 120 | + { Box::pin(async move #prev_block ) } |
| 121 | + })?; |
| 122 | + fn_item.sig.asyncness = None; |
| 123 | + fn_item.sig.output = new_output; |
| 124 | + fn_item.block = new_block; |
| 125 | + } |
| 126 | + |
| 127 | + Ok(quote! { |
| 128 | + #prompt_attr_fn |
| 129 | + #fn_item |
| 130 | + }) |
| 131 | +} |
| 132 | + |
| 133 | +#[cfg(test)] |
| 134 | +mod test { |
| 135 | + use super::*; |
| 136 | + |
| 137 | + #[test] |
| 138 | + fn test_prompt_macro() -> syn::Result<()> { |
| 139 | + let attr = quote! { |
| 140 | + name = "example-prompt", |
| 141 | + description = "An example prompt" |
| 142 | + }; |
| 143 | + let input = quote! { |
| 144 | + async fn example_prompt(&self, Parameters(args): Parameters<ExampleArgs>) -> Result<String> { |
| 145 | + Ok("Example prompt response".to_string()) |
| 146 | + } |
| 147 | + }; |
| 148 | + let result = prompt(attr, input)?; |
| 149 | + |
| 150 | + // Verify the output contains both the attribute function and the modified function |
| 151 | + let result_str = result.to_string(); |
| 152 | + assert!(result_str.contains("example_prompt_prompt_attr")); |
| 153 | + assert!( |
| 154 | + result_str.contains("rmcp") |
| 155 | + && result_str.contains("model") |
| 156 | + && result_str.contains("Prompt") |
| 157 | + ); |
| 158 | + |
| 159 | + Ok(()) |
| 160 | + } |
| 161 | + |
| 162 | + #[test] |
| 163 | + fn test_doc_comment_description() -> syn::Result<()> { |
| 164 | + let attr = quote! {}; // No explicit description |
| 165 | + let input = quote! { |
| 166 | + /// This is a test prompt description |
| 167 | + /// with multiple lines |
| 168 | + fn test_prompt(&self) -> Result<String> { |
| 169 | + Ok("Test".to_string()) |
| 170 | + } |
| 171 | + }; |
| 172 | + let result = prompt(attr, input)?; |
| 173 | + |
| 174 | + // The output should contain the description from doc comments |
| 175 | + let result_str = result.to_string(); |
| 176 | + assert!(result_str.contains("This is a test prompt description")); |
| 177 | + assert!(result_str.contains("with multiple lines")); |
| 178 | + |
| 179 | + Ok(()) |
| 180 | + } |
| 181 | +} |
0 commit comments