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