Skip to main content

react_agent

Macro react_agent 

Source
macro_rules! react_agent {
    ($provider:expr $(,)?) => { ... };
    ($provider:expr, $system:literal $(,)?) => { ... };
    ($provider:expr, [$($tool:expr),* $(,)?] $(,)?) => { ... };
    ($provider:expr, [$($tool:expr),* $(,)?], $system:expr $(,)?) => { ... };
    ($provider:expr, $registry:expr $(,)?) => { ... };
    ($provider:expr, $registry:expr, $system:expr $(,)?) => { ... };
}
Expand description

Convenience assembly macro: registers a list of tools (possibly heterogeneous) with automatic boxing, creating a ToolRegistry internally. The system prompt is optional (omitted = no system prompt). Six arms:

  • react_agent!(provider) — no tools, no system prompt;
  • react_agent!(provider, "system prompt") — no tools, with a system prompt (the string must be a literal, distinguishing it from the “existing registry” arm; to use a variable, write react_agent!(provider, [], system_var) or the three-arg arm);
  • react_agent!(provider, [tool_1, tool_2, ...]) — a heterogeneous tool list (types need not match), auto-registered, no system prompt;
  • react_agent!(provider, [tool_1, ...], "system prompt") — list + system prompt;
  • react_agent!(provider, registry) — an existing registry (e.g. a sub-agent trimmed via subset), no system prompt;
  • react_agent!(provider, registry, "system prompt") — registry + system prompt.

Returns ReActAgent; chained with_memory / with_config / with_state / with_event_channel / with_tool_round_executor work as usual. ReActAgent::new keeps a single signature; the macro handles the multiple shapes.

use molo::{react_agent, Agent, FakeProvider, FakeReply};

let mut agent = react_agent!(
    FakeProvider::new([FakeReply::Text("Hello".into())]),
    "You are a helpful assistant",
);
assert_eq!(agent.run("Are you there").await?, "Hello");