adam_macros/lib.rs
1//! The `#[tool]` attribute macro of adam-rs.
2//!
3//! Use it through the `adam` facade (`adam::prelude::*`), which also carries
4//! the paths the generated code refers to. This crate is only the macro: the
5//! expansion lives in a pure function over token streams (`expand`), so that
6//! it is unit-tested like any other code.
7//!
8//! ```ignore
9//! use adam::prelude::*;
10//!
11//! /// Ask the person who gave you the task a question and wait for the answer.
12//! #[tool(asks_user)]
13//! pub async fn ask_user(
14//! /// What you need to know
15//! question: String,
16//! ) -> Result<ToolOutput, ToolError> {
17//! Err(ToolError::needs_input(question))
18//! }
19//!
20//! let tools = tools![AskUser];
21//! ```
22//!
23//! See the `adam` crate for the full contract, and `docs/reference/agent-files.md`.
24
25use proc_macro::TokenStream;
26
27mod expand;
28
29/// Turns an `async fn` into a tool: keeps the function, and generates a unit
30/// struct (the function's name in `UpperCamelCase`) that implements
31/// `adam::Tool`.
32///
33/// * The doc comment of the function is the tool's description, and the doc
34/// comment of each parameter is that argument's description. Wrapped lines
35/// are joined, blank lines separate paragraphs.
36/// * A `&ToolCtx` parameter (at most one) receives the call's context, and a
37/// `State<T>` parameter receives the shared `T` the agent was given (and is
38/// reported by `Tool::required_state`). Every other parameter is an
39/// argument the model fills in.
40/// * `#[args] a: MyArgs` takes an existing `Deserialize + JsonSchema` struct
41/// as the whole argument object.
42/// * The return type is `Result<T, E>` or a bare `T`, with `T: IntoToolOutput`
43/// and `E: Into<ToolError>`.
44///
45/// Options: `name = "..."` (the tool's name, default the function's),
46/// `type = Ident` (the generated struct, default the function's name in
47/// `UpperCamelCase`), `strict` (unknown argument fields are an error),
48/// `classify` (the error is `adam_error::Classify`: retryable errors become
49/// `ToolError::Transient`, or `ToolError::Retryable` with the class for a rate limit or a conflict,
50/// others `Permanent`), `retry(attempts = 3, backoff = "1s", max_backoff = "30s",
51/// multiplier = 2.0, on = ["rate-limited"])` (the tool's retry policy, `Tool::retry_policy`:
52/// `attempts` is required, durations are `500ms`, `1s`, `30m` or `1h`, `on` lists `transient`,
53/// `rate-limited` and `conflict`, and what is left out is `RetryPolicy`'s default), `asks_user` (the tool can end a call with
54/// `ToolError::NeedsInput`: `Tool::asks_user` says `true`; a subagent may have it, and the loop decides per run whether
55/// the person can be asked) and
56/// `step = "subagent"`, `label = "Helper"` and `icon = "agent"` (how a call is drawn as a step,
57/// `Tool::step_style`: the kind, a label instead of the tool's name, and an icon; each from the closed
58/// vocabulary of the `steps/v1` extension, and any other word is an error that lists them),
59/// `parallel = "independent" | "read" | "write" | "exclusive"` with an optional `path = "<argument>"`,
60/// or `access = path::to::fn` (what a call shares with the other calls of its turn,
61/// `Tool::access`: `read` and `write` use `files`, or `files/<path>` when `path` names a string
62/// argument, normalised lexically and widened to `files` when absolute or with `..`; `access` is a
63/// `fn(&Value) -> ToolAccess`; without any of them the tool runs alone) and `crate = path` (where
64/// `Tool` and `__private` are, default `::adam`; `::adam_llm_agent` for a
65/// crate without the facade).
66#[proc_macro_attribute]
67pub fn tool(attr: TokenStream, item: TokenStream) -> TokenStream {
68 expand::expand(attr.into(), item.into()).into()
69}