agent_io_macros/lib.rs
1//! Procedural macros for the agent-io SDK.
2//!
3//! # `#[tool]` attribute macro
4//!
5//! Annotate an `async fn` to automatically generate a `Tool` implementation.
6//! The function's doc comment becomes the tool description.
7//! Parameter descriptions are provided as key=value pairs in the attribute.
8//!
9//! ## Usage
10//!
11//! ```rust,ignore
12//! use agent_io::tool;
13//!
14//! /// Get the current weather for a location
15//! #[tool(location = "The city name to get weather for")]
16//! async fn get_weather(location: String) -> agent_io::Result<String> {
17//! Ok(format!("Weather in {location}: Sunny, 25\u{00B0}C"))
18//! }
19//!
20//! // Use in agent:
21//! let agent = Agent::builder()
22//! .with_llm(Arc::new(llm))
23//! .tool(get_weather())
24//! .build()?;
25//! ```
26
27mod attr;
28mod codegen;
29mod params;
30mod utils;
31
32use proc_macro::TokenStream;
33use syn::parse_macro_input;
34
35use attr::ToolAttr;
36
37/// Derive a `Tool` implementation from an annotated async function.
38///
39/// See the [crate-level documentation](self) for full usage.
40#[proc_macro_attribute]
41pub fn tool(attr: TokenStream, item: TokenStream) -> TokenStream {
42 let tool_attr = parse_macro_input!(attr as ToolAttr);
43 let input = parse_macro_input!(item as syn::ItemFn);
44 match codegen::expand_tool(tool_attr, input) {
45 Ok(ts) => ts.into(),
46 Err(e) => e.to_compile_error().into(),
47 }
48}