pub struct Swarm { /* private fields */ }Expand description
A team of agents that pass the work to each other.
The run starts with the entry agent. Each agent
sees the task, who worked on it before, what they shared and what they
wrote and read before handing off, and the other agents it can hand off
to; it either answers, which ends the run with that answer, or calls
handoff_to_agent, which ends its turn and starts the
agent it named. Every agent run starts with a fresh conversation: the
earlier agents’ work reaches the next one as text in its prompt, not as
messages it made itself. The text and tool results of an agent that hands
off are forwarded to the next agent, so secrets in tool output reach it
too; the forwarded work is cut to a fixed budget, oldest first.
Cloning is cheap; clones share the agents.
Implementations§
Source§impl Swarm
impl Swarm
Sourcepub async fn run(&self, task: impl Into<String>) -> Result<SwarmRun, SwarmError>
pub async fn run(&self, task: impl Into<String>) -> Result<SwarmRun, SwarmError>
Run the swarm on task until an agent answers.
Sourcepub async fn run_as(
&self,
run_id: &str,
task: impl Into<String>,
) -> Result<SwarmRun, SwarmError>
pub async fn run_as( &self, run_id: &str, task: impl Into<String>, ) -> Result<SwarmRun, SwarmError>
Run the swarm as run run_id: resume it from its last checkpoint if
it has one and did not finish, or else start it on task. Progress
is saved before every turn, so a run that stopped (the process
restarted, an agent failed) carries on with the turn it was on. Each
turn keeps a journal of its tool results (answers to ask_human
included), so a turn run again replays them rather than running the
tools twice.
Needs checkpoints; without them it is
run. A run_id is 1-128 of [A-Za-z0-9_-].
Sourcepub fn stream(&self, task: impl Into<String>) -> WasmBoxedStream<'_, SwarmEvent>
pub fn stream(&self, task: impl Into<String>) -> WasmBoxedStream<'_, SwarmEvent>
Run the swarm on task, reporting each turn, the text its model
writes and each handoff as they happen, and the outcome last.
The run makes progress only while the stream is polled, and stops when the stream is dropped.
use futures::StreamExt;
use rig_multiagent::SwarmEvent;
let mut events = swarm.stream("Fix the flaky test");
while let Some(event) = events.next().await {
match event {
SwarmEvent::AgentStarted { agent } => println!("\n[{agent}]"),
SwarmEvent::Text { text, .. } => print!("{text}"),
SwarmEvent::Handoff(handoff) => println!("\n-> {}", handoff.to),
SwarmEvent::Finished(outcome) => println!("\n{outcome:?}"),
_ => {}
}
}Sourcepub fn stream_as<'a>(
&'a self,
run_id: &'a str,
task: impl Into<String>,
) -> WasmBoxedStream<'a, SwarmEvent>
pub fn stream_as<'a>( &'a self, run_id: &'a str, task: impl Into<String>, ) -> WasmBoxedStream<'a, SwarmEvent>
stream as run run_id, resuming it as
run_as does. A resumed run starts with
SwarmEvent::Resumed.
Trait Implementations§
Source§impl Node for Swarm
impl Node for Swarm
Source§fn run<'a>(
&'a self,
input: String,
io: &'a NodeIo<'a>,
) -> WasmBoxedFuture<'a, Result<NodeOutput, NodeError>>
fn run<'a>( &'a self, input: String, io: &'a NodeIo<'a>, ) -> WasmBoxedFuture<'a, Result<NodeOutput, NodeError>>
Streams every agent’s text, handing-off turns included, and passes on their questions. In a checkpointed graph run, the swarm keeps its own progress (in its store, or else the graph’s), so a resumed graph resumes the swarm inside the node too.