troupekit-macros 0.4.0

Attribute macros #[tool] and #[agent] for the Troupekit LLM agent framework, built on the compiler proc_macro crate only.
Documentation
#![doc(
    html_logo_url = "https://raw.githubusercontent.com/ChaoticQubit/troupekit/main/assets/brand/logo-mark.svg",
    html_favicon_url = "https://raw.githubusercontent.com/ChaoticQubit/troupekit/main/assets/brand/favicon.svg"
)]

use proc_macro::TokenStream;

mod agent;
mod args;
mod doc;
mod item;
#[cfg(test)]
mod lex;
mod lit;
mod tokens;
mod tool;

/// Turns a fn into a tool value: `#[tool] async fn add(a: i64, b: i64)` makes
/// `add` a unit struct implementing `Tool`; the doc comment is the description,
/// the arguments the schema, and the body stays callable as `add::invoke(..)`.
/// An imported `add` turns a `let add` binding into a unit-struct pattern:
/// rename the binding or use the tool by path. Keys: name, description, crate.
#[proc_macro_attribute]
pub fn tool(attr: TokenStream, item: TokenStream) -> TokenStream {
    let mut source = tokens::Source::new();
    let attr = source.add(attr);
    let item = source.add(item);
    match tool::parse_tool(&attr, &item) {
        Ok(parsed) => tokens::emit(&tool::expand_tool(&parsed), &source),
        Err(error) => tokens::emit_error(&error, &source),
    }
}

/// Turns `fn calc() {}` into a fn returning a configured `Agent`: the doc
/// comment becomes the instructions and the fn name, `_` read as `-`,
/// the agent name. Keys such as `model`, `tools = [add]` and `evals`
/// map to the builder methods of the same name.
#[proc_macro_attribute]
pub fn agent(attr: TokenStream, item: TokenStream) -> TokenStream {
    let mut source = tokens::Source::new();
    let attr = source.add(attr);
    let item = source.add(item);
    match agent::parse_agent(&attr, &item) {
        Ok(parsed) => tokens::emit(&agent::expand_agent(&parsed), &source),
        Err(error) => tokens::emit_error(&error, &source),
    }
}