Skip to main content

Orchestration

Struct Orchestration 

Source
pub struct Orchestration {
    pub id: String,
    pub name: String,
    /* private fields */
}
Expand description

The orchestration engine that coordinates multiple Agents in a chosen OrchestrationMode.

An Orchestration owns a set of agents, a collaboration mode, and a running conversation history. Call Orchestration::run to execute a multi-agent conversation and receive an OrchestrationResponse.

§Examples

use cloudllm::{Agent, orchestration::{Orchestration, OrchestrationMode}};
use cloudllm::clients::openai::OpenAIClient;
use std::sync::Arc;

let client = || Arc::new(OpenAIClient::new_with_model_string("key", "gpt-4o"));

let mut orch = Orchestration::new("team", "My Team")
    .with_mode(OrchestrationMode::RoundRobin)
    .with_system_context("You are expert engineers.")
    .with_max_tokens(16384);

orch.add_agent(Agent::new("alice", "Alice", client())).unwrap();
orch.add_agent(Agent::new("bob", "Bob", client())).unwrap();

let result = orch.run("Design a REST API", 2).await.unwrap();
println!("{} messages over {} rounds", result.messages.len(), result.round);

Fields§

§id: String

Stable identifier used for logging, metrics, and external integrations.

§name: String

Human-readable name of this orchestration.

Implementations§

Source§

impl Orchestration

Source

pub fn new(id: impl Into<String>, name: impl Into<String>) -> Self

Create an orchestration with the provided identifiers.

Defaults to OrchestrationMode::Parallel, a generic system context, and an 8 192-token budget. Use the with_* builder methods to customise.

§Examples
use cloudllm::orchestration::Orchestration;

let orch = Orchestration::new("qa-team", "QA Review Team");
assert_eq!(orch.id, "qa-team");
assert_eq!(orch.name, "QA Review Team");
Source

pub fn with_mode(self, mode: OrchestrationMode) -> Self

Select the collaboration mode used during Orchestration::run (builder pattern).

§Examples
use cloudllm::orchestration::{Orchestration, OrchestrationMode};

let orch = Orchestration::new("id", "name")
    .with_mode(OrchestrationMode::Debate {
        max_rounds: 3,
        convergence_threshold: Some(0.8),
    });
Source

pub fn with_system_context(self, context: impl Into<String>) -> Self

Override the default system context prompt shared across agents (builder pattern).

This string is passed as the system prompt in every LLM call made during Orchestration::run.

§Examples
use cloudllm::orchestration::Orchestration;

let orch = Orchestration::new("id", "name")
    .with_system_context("You are senior Rust engineers reviewing a PR.");
Source

pub fn with_max_tokens(self, max_tokens: usize) -> Self

Override the soft token budget used for context trimming (builder pattern).

The budget is forwarded to each agent’s LLM call so that overly-long conversation histories can be trimmed before transmission.

§Examples
use cloudllm::orchestration::Orchestration;

let orch = Orchestration::new("id", "name")
    .with_max_tokens(32768);
Source

pub fn with_event_handler(self, handler: Arc<dyn EventHandler>) -> Self

Attach an EventHandler for orchestration and agent lifecycle events (builder pattern).

The handler receives OrchestrationEvents directly from the orchestration engine (run start/end, round boundaries, RALPH progress, etc.). Additionally, the handler is automatically propagated to every agent added via add_agent, so those agents will also emit their AgentEvents through the same handler. This gives you a unified stream of both orchestration-level and agent-level events through a single callback.

§Example
use cloudllm::orchestration::{Orchestration, OrchestrationMode};
use cloudllm::event::{EventHandler, OrchestrationEvent};
use async_trait::async_trait;
use std::sync::Arc;

struct MyHandler;
#[async_trait]
impl EventHandler for MyHandler {
    async fn on_orchestration_event(&self, event: &OrchestrationEvent) {
        println!("{:?}", event);
    }
}

let orch = Orchestration::new("id", "name")
    .with_mode(OrchestrationMode::RoundRobin)
    .with_event_handler(Arc::new(MyHandler));
Source

pub fn add_agent(&mut self, agent: Agent) -> Result<(), OrchestrationError>

Register a new agent with the orchestration.

Returns an error if an agent with the same Agent::id is already registered. The insertion order determines the round-robin sequence used by RoundRobin, Debate, and Ralph modes.

§Examples
use cloudllm::{Agent, orchestration::Orchestration};
use cloudllm::clients::openai::OpenAIClient;
use std::sync::Arc;

let mut orch = Orchestration::new("id", "name");
let client = Arc::new(OpenAIClient::new_with_model_string("key", "gpt-4o"));

orch.add_agent(Agent::new("analyst", "Analyst", client)).unwrap();

// Duplicate ID is an error
assert!(orch.add_agent(Agent::new("analyst", "Analyst 2", client2)).is_err());
Source

pub fn remove_agent(&mut self, id: &str) -> Option<Agent>

Remove and return an agent by its identifier.

Returns None if no agent with the given ID exists. Removing an agent also removes it from the round-robin order.

§Examples
let mut orch = Orchestration::new("id", "name");

let removed = orch.remove_agent("a1");
assert!(removed.is_some());
assert!(orch.remove_agent("a1").is_none()); // already gone
Source

pub fn get_agent(&self, id: &str) -> Option<&Agent>

Borrow a registered agent by its identifier.

§Examples
let mut orch = Orchestration::new("id", "name");

if let Some(agent) = orch.get_agent("a1") {
    println!("Found agent: {}", agent.name);
}
Source

pub fn list_agents(&self) -> Vec<&Agent>

List agents in their insertion order.

The returned order matches the round-robin sequence used by RoundRobin, Debate, and Ralph modes.

§Examples
let mut orch = Orchestration::new("id", "name");

for agent in orch.list_agents() {
    println!("{}: {}", agent.id, agent.name);
}
Source

pub async fn run( &mut self, prompt: &str, rounds: usize, ) -> Result<OrchestrationResponse, Box<dyn Error + Send + Sync>>

Execute a multi-agent discussion according to the configured OrchestrationMode.

The prompt is broadcast to all agents according to the active mode.

§Parameters
  • prompt — The user question or task description.
  • rounds — Number of iterations for fixed-round modes (Parallel, RoundRobin, Moderated). Ignored by Hierarchical (which runs once per layer), Debate (uses max_rounds), and Ralph (uses max_iterations).
§Errors

Returns OrchestrationError::NoAgents if no agents have been registered. May also surface errors from individual agent LLM calls or tokio task joins.

§Examples
let response = orch.run("Summarise this paper", 2).await?;
assert!(response.is_complete);
Source

pub fn get_conversation_history(&self) -> &[OrchestrationMessage]

Return a slice of all messages accumulated since the orchestration was created (or since the last Orchestration::clear_history call).

This includes the initial user prompt(s) and every agent response across all rounds.

§Examples
let _ = orch.run("Hello", 1).await?;

let history = orch.get_conversation_history();
println!("{} messages in history", history.len());
Source

pub fn clear_history(&mut self)

Remove all historical messages, resetting the orchestration state.

Call this between unrelated discussions when you want to reuse the same Orchestration instance without carrying over prior context.

§Examples
let _ = orch.run("First topic", 1).await?;
orch.clear_history();

// Start fresh — agents will not see "First topic" responses
let _ = orch.run("Second topic", 1).await?;

Auto Trait Implementations§

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> 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, 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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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> 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