Skip to main content

act_sdk_macros/
lib.rs

1mod component;
2mod tool;
3
4use darling::FromMeta;
5use proc_macro::TokenStream;
6
7/// Attribute macro for ACT component modules.
8///
9/// Transforms a module containing `#[act_tool]` functions into a complete
10/// WIT component implementation with `wit_bindgen::generate!()`, `export!()`,
11/// and a `Guest` trait implementation.
12///
13/// # Attributes
14///
15/// - `name = "..."` (required) — Component name
16/// - `version = "..."` (required) — Component version
17/// - `description = "..."` (required) — Component description
18/// - `default_language = "..."` (optional) — BCP 47 language tag
19///
20/// # Example
21///
22/// ```ignore
23/// #[act_component(
24///     name = "my-component",
25///     version = "0.1.0",
26///     description = "My ACT component",
27/// )]
28/// mod component {
29///     use super::*;
30///
31///     #[act_tool(description = "Say hello")]
32///     fn greet(args: GreetArgs) -> ActResult<String> {
33///         Ok(format!("Hello, {}!", args.name))
34///     }
35/// }
36/// ```
37#[proc_macro_attribute]
38pub fn act_component(attr: TokenStream, item: TokenStream) -> TokenStream {
39    let attr_args = match darling::ast::NestedMeta::parse_meta_list(attr.into()) {
40        Ok(a) => a,
41        Err(e) => return TokenStream::from(darling::Error::from(e).write_errors()),
42    };
43    let attrs = match component::ComponentAttrs::from_list(&attr_args) {
44        Ok(a) => a,
45        Err(e) => return TokenStream::from(e.write_errors()),
46    };
47    let module = match syn::parse::<syn::ItemMod>(item) {
48        Ok(m) => m,
49        Err(e) => return e.to_compile_error().into(),
50    };
51    match component::generate(attrs, &module) {
52        Ok(tokens) => tokens.into(),
53        Err(e) => e.to_compile_error().into(),
54    }
55}
56
57/// Attribute macro for ACT tool functions.
58///
59/// When used inside an `#[act_component]` module, marks a function as a tool.
60/// The `#[act_component]` macro processes these attributes during code generation.
61///
62/// When used standalone (outside `#[act_component]`), this is a no-op pass-through.
63///
64/// # Attributes
65///
66/// - `description = "..."` (required) — Tool description
67/// - `read_only` — Mark tool as read-only
68/// - `idempotent` — Mark tool as idempotent
69/// - `destructive` — Mark tool as destructive
70/// - `streaming` — Mark tool as streaming (auto-detected if ActContext param present)
71/// - `timeout_ms = N` — Set timeout in milliseconds
72#[proc_macro_attribute]
73pub fn act_tool(_attr: TokenStream, item: TokenStream) -> TokenStream {
74    // When used standalone, pass through unchanged.
75    // When inside #[act_component], the component macro processes this.
76    item
77}