rsai-macros
Procedural macros that provide structured AI generation capabilities for the rsai crate. This crate enables type-safe tool calling and automatic JSON schema generation for AI interactions.
๐ Features
#[completion_schema]- Automatic JSON schema generation for AI response types#[tool]- Transform Rust functions into AI-callable tools with automatic schema generationtoolset!- Create collections of tools for AI agents- Type Safety - Compile-time validation of tool parameters and descriptions
- Async Support - First-class support for async tool functions
- Error Handling - Comprehensive error mapping and validation
๐ฆ Installation
Add this to your Cargo.toml:
[]
= "0.4.0"
๐ ๏ธ Macros
#[completion_schema]
Automatically adds the necessary derives and attributes for types used with the complete::<T>() method.
What it does
- Adds
#[derive(serde::Deserialize, schemars::JsonSchema)] - Adds
#[schemars(deny_unknown_fields)]for strict validation - Ensures AI responses match your expected structure
Example
use completion_schema;
// The macro expands to:
#[tool]
Transforms Rust functions into AI-callable tools with automatic JSON schema generation.
Features
- Docstring Parsing: Extracts function descriptions and parameter documentation
- Parameter Validation: Ensures all docstring parameters exist in the function signature
- Optional Parameters: Automatically detects
Option<T>types and marks them as non-required - Type Mapping: Converts Rust types to JSON schema types
- Async Support: Handles both sync and async functions; generated sync tools are offloaded to Tokio's blocking pool when run through
ToolRegistry - Error Handling: Maps errors to
LlmErrorwith proper context
Syntax
/// Function description (required)
/// param_name: Parameter description (required for each parameter)
/// optional_param: Description for optional parameters (also required)
Examples
Basic Tool:
use tool;
/// Get current weather for a city
/// city: The city to get weather for
/// unit: Temperature unit (celsius or fahrenheit)
Async Tool:
use tool;
use async_trait;
/// Send an email to a recipient
/// to: Email address of the recipient
/// subject: Email subject line
/// body: Email body content
async
Complex Types:
use tool;
use ;
/// Calculate distance between two locations
/// from: Starting location with coordinates
/// to: Destination location with coordinates
/// unit: Distance unit (km or miles)
toolset!
Creates a collection of tools from multiple #[tool]-annotated functions.
Syntax
let tools = toolset!;
Example
use ;
/// Get current weather for a city
/// city: The city to get weather for
/// Calculate distance between two locations
/// from: Starting location
/// to: Destination location
// Create a toolset containing both tools
let tools = toolset!;
assert_eq!;
๐ Type Mapping
The macros automatically convert Rust types to JSON schema types:
| Rust Type | JSON Schema Type |
|---|---|
String, &str |
string |
i8, i16, i32, i64, u8, u16, u32, u64, f32, f64 |
number |
bool |
boolean |
Vec<T> |
array |
Option<T> |
T (optional) |
struct |
object |
enum |
enum |
โ ๏ธ Error Handling
The macros provide comprehensive compile-time validation:
Missing Parameter Descriptions
/// This will cause a compile error
/// city: The city to get weather for
Mismatched Parameters
/// This will cause a compile error
/// city: The city to get weather for
/// temperature: Temperature (not in function signature)
Helpful Error Messages
The macros provide clear, actionable error messages to help you fix documentation issues quickly.
๐งช Testing
This crate includes comprehensive test coverage:
# Run all tests
# Run specific test modules
# Run UI tests (compilation failure tests)
๐ Advanced Usage
Custom Error Types
Your tool functions can return any type that implements Into<LlmError>:
use tool;
use LlmError;
/// Call external API
/// endpoint: API endpoint to call
async
๐ค Contributing
This crate is part of the rsai project. When contributing:
- Ensure all macros have comprehensive doc comments
- Add tests for new functionality
- Include UI tests for compile-time error cases
- Follow the existing code style and patterns
๐ License
This crate is licensed under the MIT License. See the LICENSE file for details.