rig_multiagent/lib.rs
1//! Multi-agent patterns for [Rig](https://github.com/0xPlaygrounds/rig).
2//!
3//! - A [`Swarm`] is a team of agents that pass the work to each other: each
4//! agent gets a `handoff_to_agent` tool, and the first agent that answers
5//! without handing off gives the swarm's answer.
6//! - A [`Graph`] runs [nodes](Node) (agents, swarms, graphs or plain async
7//! functions) along edges, some of them conditional. Nodes that become ready
8//! together run in parallel, and each node gets the outputs of the nodes
9//! that led to it.
10//!
11//! ```no_run
12//! use rig_multiagent::{Graph, Swarm};
13//! # async fn run(
14//! # researcher: rig_agent::Agent,
15//! # coder: rig_agent::Agent,
16//! # reviewer: rig_agent::Agent,
17//! # ) -> Result<(), Box<dyn std::error::Error>> {
18//! // The researcher starts and hands off to the coder when it is done.
19//! let swarm = Swarm::builder().agent(researcher).agent(coder).build()?;
20//! let run = swarm.run("Add a retry to the HTTP client").await?;
21//! println!("{} answered: {}", run.agent, run.answer);
22//!
23//! // The same team as one step of a graph, followed by a review.
24//! let graph = Graph::builder()
25//! .node("build", swarm)
26//! .node("review", reviewer)
27//! .edge("build", "review")
28//! .build()?;
29//! let run = graph.run("Add a retry to the HTTP client").await?;
30//! println!("{}", run.answer());
31//! # Ok(())
32//! # }
33//! ```
34
35#![forbid(unsafe_code)]
36#![cfg_attr(docsrs, feature(doc_cfg))]
37#![cfg_attr(
38 test,
39 allow(
40 clippy::expect_used,
41 clippy::panic,
42 clippy::unwrap_used,
43 clippy::unreachable
44 )
45)]
46
47mod checkpoint;
48mod events;
49mod graph;
50mod human;
51mod node;
52mod swarm;
53
54pub use checkpoint::CheckpointError;
55pub use graph::{
56 Graph, GraphBuilder, GraphError, GraphEvent, GraphFailure, GraphRun, GraphState, RunState,
57};
58pub use human::{HumanNode, Question, human};
59pub use node::{
60 FnNode, Node, NodeError, NodeIo, NodeOutput, StateFnNode, node_fn, node_fn_with_state,
61};
62/// The stores runs keep their checkpoints in.
63pub use rig_memory;
64pub use swarm::{
65 ASK_TOOL, HANDOFF_TOOL, Handoff, Swarm, SwarmBuilder, SwarmError, SwarmEvent, SwarmFailure,
66 SwarmRun,
67};
68
69/// Why a [`Swarm`] or [`Graph`] could not be built.
70#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
71#[non_exhaustive]
72pub enum BuildError {
73 /// Nothing to run.
74 #[error("a swarm or graph needs at least one member")]
75 Empty,
76 /// A swarm agent without a name: the other agents hand off to it by name.
77 #[error("every swarm agent needs a name (AgentBuilder::name)")]
78 UnnamedAgent,
79 /// Two members share a name.
80 #[error("two members are named {0:?}")]
81 DuplicateName(String),
82 /// An entry point or edge names a member that does not exist.
83 #[error("no member is named {0:?}")]
84 UnknownName(String),
85 /// Every graph node has an incoming edge, so none is a natural start.
86 #[error("every node has an incoming edge; name where the graph starts with entry()")]
87 NoEntry,
88 /// A graph with a cycle and no bound on how many times nodes may run.
89 #[error("the graph has a cycle; bound it with max_node_executions()")]
90 UnboundedCycle,
91 /// A turn budget of zero: an agent could not make its first model call.
92 #[error("max_turns_per_agent must be at least 1")]
93 ZeroTurns,
94}
95
96#[cfg(doctest)]
97#[doc = include_str!("../README.md")]
98mod readme_doctests {}