Skip to main content

troupekit_macros/
lib.rs

1#![doc(
2    html_logo_url = "https://raw.githubusercontent.com/ChaoticQubit/troupekit/main/assets/brand/logo-mark.svg",
3    html_favicon_url = "https://raw.githubusercontent.com/ChaoticQubit/troupekit/main/assets/brand/favicon.svg"
4)]
5
6use proc_macro::TokenStream;
7
8mod agent;
9mod args;
10mod doc;
11mod item;
12#[cfg(test)]
13mod lex;
14mod lit;
15mod tokens;
16mod tool;
17
18/// Turns a fn into a tool value: `#[tool] async fn add(a: i64, b: i64)` makes
19/// `add` a unit struct implementing `Tool`; the doc comment is the description,
20/// the arguments the schema, and the body stays callable as `add::invoke(..)`.
21/// An imported `add` turns a `let add` binding into a unit-struct pattern:
22/// rename the binding or use the tool by path. Keys: name, description, crate.
23#[proc_macro_attribute]
24pub fn tool(attr: TokenStream, item: TokenStream) -> TokenStream {
25    let mut source = tokens::Source::new();
26    let attr = source.add(attr);
27    let item = source.add(item);
28    match tool::parse_tool(&attr, &item) {
29        Ok(parsed) => tokens::emit(&tool::expand_tool(&parsed), &source),
30        Err(error) => tokens::emit_error(&error, &source),
31    }
32}
33
34/// Turns `fn calc() {}` into a fn returning a configured `Agent`: the doc
35/// comment becomes the instructions and the fn name, `_` read as `-`,
36/// the agent name. Keys such as `model`, `tools = [add]` and `evals`
37/// map to the builder methods of the same name.
38#[proc_macro_attribute]
39pub fn agent(attr: TokenStream, item: TokenStream) -> TokenStream {
40    let mut source = tokens::Source::new();
41    let attr = source.add(attr);
42    let item = source.add(item);
43    match agent::parse_agent(&attr, &item) {
44        Ok(parsed) => tokens::emit(&agent::expand_agent(&parsed), &source),
45        Err(error) => tokens::emit_error(&error, &source),
46    }
47}