Skip to main content

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::{Graph, GraphBuilder, GraphError, GraphEvent, GraphFailure, GraphRun, GraphState};
56pub use human::{HumanNode, Question, human};
57pub use node::{FnNode, Node, NodeError, NodeIo, NodeOutput, node_fn};
58/// The stores runs keep their checkpoints in.
59pub use rig_memory;
60pub use swarm::{
61    ASK_TOOL, HANDOFF_TOOL, Handoff, Swarm, SwarmBuilder, SwarmError, SwarmEvent, SwarmFailure,
62    SwarmRun,
63};
64
65/// Why a [`Swarm`] or [`Graph`] could not be built.
66#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
67#[non_exhaustive]
68pub enum BuildError {
69    /// Nothing to run.
70    #[error("a swarm or graph needs at least one member")]
71    Empty,
72    /// A swarm agent without a name: the other agents hand off to it by name.
73    #[error("every swarm agent needs a name (AgentBuilder::name)")]
74    UnnamedAgent,
75    /// Two members share a name.
76    #[error("two members are named {0:?}")]
77    DuplicateName(String),
78    /// An entry point or edge names a member that does not exist.
79    #[error("no member is named {0:?}")]
80    UnknownName(String),
81    /// Every graph node has an incoming edge, so none is a natural start.
82    #[error("every node has an incoming edge; name where the graph starts with entry()")]
83    NoEntry,
84    /// A graph with a cycle and no bound on how many times nodes may run.
85    #[error("the graph has a cycle; bound it with max_node_executions()")]
86    UnboundedCycle,
87}