Skip to main content

Swarm

Struct Swarm 

Source
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

Source

pub fn builder() -> SwarmBuilder

Start building a swarm.

Source§

impl Swarm

Source

pub async fn run(&self, task: impl Into<String>) -> Result<SwarmRun, SwarmError>

Run the swarm on task until an agent answers.

Source

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_-].

Source

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:?}"),
        _ => {}
    }
}
Source

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 Clone for Swarm

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Node for Swarm

Source§

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.

Auto Trait Implementations§

§

impl !RefUnwindSafe for Swarm

§

impl !UnwindSafe for Swarm

§

impl Forget for Swarm

§

impl Freeze for Swarm

§

impl Move for Swarm

§

impl Send for Swarm

§

impl Sync for Swarm

§

impl Unpin for Swarm

§

impl UnsafeUnpin for Swarm

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WasmCompatSend for T
where T: Send,

Source§

impl<T> WasmCompatSync for T
where T: Sync,

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more