rig_derive/
lib.rs

1extern crate proc_macro;
2
3use convert_case::{Case, Casing};
4use proc_macro::TokenStream;
5use quote::{format_ident, quote};
6use std::{collections::HashMap, ops::Deref};
7use syn::{
8    DeriveInput, Expr, ExprLit, Ident, Lit, Meta, PathArguments, ReturnType, Token, Type,
9    parse::{Parse, ParseStream},
10    parse_macro_input,
11    punctuated::Punctuated,
12};
13
14mod basic;
15mod client;
16mod custom;
17mod embed;
18
19pub(crate) const EMBED: &str = "embed";
20
21#[proc_macro_derive(ProviderClient, attributes(client))]
22pub fn derive_provider_client(input: TokenStream) -> TokenStream {
23    client::provider_client(input)
24}
25
26//References:
27//<https://doc.rust-lang.org/book/ch19-06-macros.html#how-to-write-a-custom-derive-macro>
28//<https://doc.rust-lang.org/reference/procedural-macros.html>
29/// A macro that allows you to implement the `rig::embedding::Embed` trait by deriving it.
30/// Usage can be found below:
31///
32/// ```rust
33/// #[derive(Embed)]
34/// struct Foo {
35///     id: String,
36///     #[embed] // this helper shows which field to embed
37///     description: String
38///}
39/// ```
40#[proc_macro_derive(Embed, attributes(embed))]
41pub fn derive_embedding_trait(item: TokenStream) -> TokenStream {
42    let mut input = parse_macro_input!(item as DeriveInput);
43
44    embed::expand_derive_embedding(&mut input)
45        .unwrap_or_else(syn::Error::into_compile_error)
46        .into()
47}
48
49struct MacroArgs {
50    description: Option<String>,
51    param_descriptions: HashMap<String, String>,
52    required: Vec<String>,
53}
54
55impl Parse for MacroArgs {
56    fn parse(input: ParseStream) -> syn::Result<Self> {
57        let mut description = None;
58        let mut param_descriptions = HashMap::new();
59        let mut required = Vec::new();
60
61        // If the input is empty, return default values
62        if input.is_empty() {
63            return Ok(MacroArgs {
64                description,
65                param_descriptions,
66                required,
67            });
68        }
69
70        let meta_list: Punctuated<Meta, Token![,]> = Punctuated::parse_terminated(input)?;
71
72        for meta in meta_list {
73            match meta {
74                Meta::NameValue(nv) => {
75                    let ident = nv.path.get_ident().unwrap().to_string();
76                    if let Expr::Lit(ExprLit {
77                        lit: Lit::Str(lit_str),
78                        ..
79                    }) = nv.value
80                        && ident.as_str() == "description"
81                    {
82                        description = Some(lit_str.value());
83                    }
84                }
85                Meta::List(list) if list.path.is_ident("params") => {
86                    let nested: Punctuated<Meta, Token![,]> =
87                        list.parse_args_with(Punctuated::parse_terminated)?;
88
89                    for meta in nested {
90                        if let Meta::NameValue(nv) = meta
91                            && let Expr::Lit(ExprLit {
92                                lit: Lit::Str(lit_str),
93                                ..
94                            }) = nv.value
95                        {
96                            let param_name = nv.path.get_ident().unwrap().to_string();
97                            param_descriptions.insert(param_name, lit_str.value());
98                        }
99                    }
100                }
101                Meta::List(list) if list.path.is_ident("required") => {
102                    let required_variables: Punctuated<Ident, Token![,]> =
103                        list.parse_args_with(Punctuated::parse_terminated)?;
104
105                    required_variables.into_iter().for_each(|x| {
106                        required.push(x.to_string());
107                    });
108                }
109                _ => {}
110            }
111        }
112
113        Ok(MacroArgs {
114            description,
115            param_descriptions,
116            required,
117        })
118    }
119}
120
121fn get_json_type(ty: &Type) -> proc_macro2::TokenStream {
122    match ty {
123        Type::Path(type_path) => {
124            let segment = &type_path.path.segments[0];
125            let type_name = segment.ident.to_string();
126
127            // Handle Vec types
128            if type_name == "Vec" {
129                if let syn::PathArguments::AngleBracketed(args) = &segment.arguments
130                    && let syn::GenericArgument::Type(inner_type) = &args.args[0]
131                {
132                    let inner_json_type = get_json_type(inner_type);
133                    return quote! {
134                        "type": "array",
135                        "items": { #inner_json_type }
136                    };
137                }
138                return quote! { "type": "array" };
139            }
140
141            // Handle primitive types
142            match type_name.as_str() {
143                "i8" | "i16" | "i32" | "i64" | "u8" | "u16" | "u32" | "u64" | "f32" | "f64" => {
144                    quote! { "type": "number" }
145                }
146                "String" | "str" => {
147                    quote! { "type": "string" }
148                }
149                "bool" => {
150                    quote! { "type": "boolean" }
151                }
152                // Handle other types as objects
153                _ => {
154                    quote! { "type": "object" }
155                }
156            }
157        }
158        _ => {
159            quote! { "type": "object" }
160        }
161    }
162}
163
164/// A procedural macro that transforms a function into a `rig::tool::Tool` that can be used with a `rig::agent::Agent`.
165///
166/// # Examples
167///
168/// Basic usage:
169/// ```rust
170/// use rig_derive::rig_tool;
171///
172/// #[rig_tool]
173/// fn add(a: i32, b: i32) -> Result<i32, rig::tool::ToolError> {
174///     Ok(a + b)
175/// }
176/// ```
177///
178/// With description:
179/// ```rust
180/// use rig_derive::rig_tool;
181///
182/// #[rig_tool(description = "Perform basic arithmetic operations")]
183/// fn calculator(x: i32, y: i32, operation: String) -> Result<i32, rig::tool::ToolError> {
184///     match operation.as_str() {
185///         "add" => Ok(x + y),
186///         "subtract" => Ok(x - y),
187///         "multiply" => Ok(x * y),
188///         "divide" => Ok(x / y),
189///         _ => Err(rig::tool::ToolError::ToolCallError("Unknown operation".into())),
190///     }
191/// }
192/// ```
193///
194/// With parameter descriptions:
195/// ```rust
196/// use rig_derive::rig_tool;
197///
198/// #[rig_tool(
199///     description = "A tool that performs string operations",
200///     params(
201///         text = "The input text to process",
202///         operation = "The operation to perform (uppercase, lowercase, reverse)"
203///     )
204/// )]
205/// fn string_processor(text: String, operation: String) -> Result<String, rig::tool::ToolError> {
206///     match operation.as_str() {
207///         "uppercase" => Ok(text.to_uppercase()),
208///         "lowercase" => Ok(text.to_lowercase()),
209///         "reverse" => Ok(text.chars().rev().collect()),
210///         _ => Err(rig::tool::ToolError::ToolCallError("Unknown operation".into())),
211///     }
212/// }
213/// ```
214#[proc_macro_attribute]
215pub fn rig_tool(args: TokenStream, input: TokenStream) -> TokenStream {
216    let args = parse_macro_input!(args as MacroArgs);
217    let input_fn = parse_macro_input!(input as syn::ItemFn);
218
219    // Extract function details
220    let fn_name = &input_fn.sig.ident;
221    let fn_name_str = fn_name.to_string();
222    let is_async = input_fn.sig.asyncness.is_some();
223
224    // Extract return type and get Output and Error types from Result<T, E>
225    let return_type = &input_fn.sig.output;
226    let (output_type, error_type) = match return_type {
227        ReturnType::Type(_, ty) => {
228            if let Type::Path(type_path) = ty.deref() {
229                if let Some(last_segment) = type_path.path.segments.last() {
230                    if last_segment.ident == "Result" {
231                        if let PathArguments::AngleBracketed(args) = &last_segment.arguments {
232                            if args.args.len() == 2 {
233                                let output = args.args.first().unwrap();
234                                let error = args.args.last().unwrap();
235
236                                (quote!(#output), quote!(#error))
237                            } else {
238                                panic!("Expected Result with two type parameters");
239                            }
240                        } else {
241                            panic!("Expected angle bracketed type parameters for Result");
242                        }
243                    } else {
244                        panic!("Return type must be a Result");
245                    }
246                } else {
247                    panic!("Invalid return type");
248                }
249            } else {
250                panic!("Invalid return type");
251            }
252        }
253        _ => panic!("Function must have a return type"),
254    };
255
256    // Generate PascalCase struct name from the function name
257    let struct_name = format_ident!("{}", { fn_name_str.to_case(Case::Pascal) });
258
259    // Use provided description or generate a default one
260    let tool_description = match args.description {
261        Some(desc) => quote! { #desc.to_string() },
262        None => quote! { format!("Function to {}", Self::NAME) },
263    };
264
265    // Extract parameter names, types, and descriptions
266    let mut param_names = Vec::new();
267    let mut param_types = Vec::new();
268    let mut param_descriptions = Vec::new();
269    let mut json_types = Vec::new();
270
271    let required_args = args.required;
272
273    for arg in input_fn.sig.inputs.iter() {
274        if let syn::FnArg::Typed(pat_type) = arg
275            && let syn::Pat::Ident(param_ident) = &*pat_type.pat
276        {
277            let param_name = &param_ident.ident;
278            let param_name_str = param_name.to_string();
279            let ty = &pat_type.ty;
280            let default_parameter_description = format!("Parameter {param_name_str}");
281            let description = args
282                .param_descriptions
283                .get(&param_name_str)
284                .map(|s| s.to_owned())
285                .unwrap_or(default_parameter_description);
286
287            param_names.push(param_name);
288            param_types.push(ty);
289            param_descriptions.push(description);
290            json_types.push(get_json_type(ty));
291        }
292    }
293
294    let params_struct_name = format_ident!("{}Parameters", struct_name);
295    let static_name = format_ident!("{}", fn_name_str.to_uppercase());
296
297    // Generate the call implementation based on whether the function is async
298    let call_impl = if is_async {
299        quote! {
300            async fn call(&self, args: Self::Args) -> Result<Self::Output, Self::Error> {
301                #fn_name(#(args.#param_names,)*).await
302            }
303        }
304    } else {
305        quote! {
306            async fn call(&self, args: Self::Args) -> Result<Self::Output, Self::Error> {
307                #fn_name(#(args.#param_names,)*)
308            }
309        }
310    };
311
312    let expanded = quote! {
313        #[derive(serde::Deserialize)]
314        pub(crate) struct #params_struct_name {
315            #(#param_names: #param_types,)*
316        }
317
318        #input_fn
319
320        #[derive(Default)]
321        pub(crate) struct #struct_name;
322
323        impl rig::tool::Tool for #struct_name {
324            const NAME: &'static str = #fn_name_str;
325
326            type Args = #params_struct_name;
327            type Output = #output_type;
328            type Error = #error_type;
329
330            fn name(&self) -> String {
331                #fn_name_str.to_string()
332            }
333
334            async fn definition(&self, _prompt: String) -> rig::completion::ToolDefinition {
335                let parameters = serde_json::json!({
336                    "type": "object",
337                    "properties": {
338                        #(
339                            stringify!(#param_names): {
340                                #json_types,
341                                "description": #param_descriptions
342                            }
343                        ),*
344                    },
345                    "required": [#(#required_args),*]
346                });
347
348                rig::completion::ToolDefinition {
349                    name: #fn_name_str.to_string(),
350                    description: #tool_description.to_string(),
351                    parameters,
352                }
353            }
354
355            #call_impl
356        }
357
358        pub static #static_name: #struct_name = #struct_name;
359    };
360
361    TokenStream::from(expanded)
362}