pub struct SubAgentPool { /* private fields */ }Expand description
A pool of named sub-agents: create and name sub-agents dynamically, then address them by name to continue their conversations.
Unlike the one-shot SubAgentTool::from_factory, the pool keeps the
instances it creates — they are not discarded when their task ends; the
user (or the model) can hand a new task to the same sub-agent by name
later, and it picks up the conversation with its prior memory. Suited to
“the main agent distributes multiple tasks, each with its own
independent-context sub-agent, and continues by name afterwards”.
The pool is a shared part: cloning is cheap (internal Arc), and
multiple loops / threads can hold the same pool; calls to the same
sub-agent name are serialized (inner lock), different names run in
parallel.
Cancellation semantics: same as SubAgentTool — sub-agent runs
receive no cancellation signal; when the caller is cancelled, unfinished
runs abort as their future is dropped.
§Examples
Create and name a sub-agent, run its first task immediately; continue by name afterwards:
use molo::agent::{Agent, AgentError, SubAgentPool};
/// Demo sub-agent: echoes the input back verbatim.
struct Echo;
#[molo::async_trait]
impl Agent for Echo {
async fn run(&mut self, input: &str) -> Result<String, AgentError> {
Ok(format!("echo: {input}"))
}
}
let pool = SubAgentPool::new();
// Create a named sub-agent and run its first task (the reply returns to
// the caller)
let reply = pool
.spawn(
"red",
|| -> Result<Box<dyn Agent + Send>, molo::tool::ToolError> { Ok(Box::new(Echo)) },
"task one",
)
.await?;
assert_eq!(reply, "echo: task one");
// Continue by name: the same instance, with its prior memory
let reply = pool.send("red", "task two").await?;
assert_eq!(reply, "echo: task two");
// Name enumeration and probing (UI list / model queries)
assert!(pool.contains("red").await);
assert_eq!(pool.names().await, vec!["red".to_string()]);Implementations§
Source§impl SubAgentPool
impl SubAgentPool
Sourcepub async fn spawn(
&self,
name: &str,
factory: impl FnOnce() -> Result<Box<dyn Agent + Send>, ToolError>,
input: &str,
) -> Result<String, PoolError>
pub async fn spawn( &self, name: &str, factory: impl FnOnce() -> Result<Box<dyn Agent + Send>, ToolError>, input: &str, ) -> Result<String, PoolError>
Create and name a sub-agent, running its first task immediately.
The factory only builds the sub-agent (type / tool subset / system
prompt, all up to you); once built, the instance goes into the pool
before running — even if the first task fails, the sub-agent stays in
the pool and can be continued or retried via
send.
§Errors
- The name already exists: returns
PoolError::Duplicate; - The factory failed: returns
PoolError::Factory; - The first task failed: returns
PoolError::Run(the instance is kept).
Sourcepub async fn spawn_react(
&self,
name: &str,
provider: impl Provider + 'static,
tools: ToolRegistry,
system_prompt: &str,
task: &str,
) -> Result<String, PoolError>
pub async fn spawn_react( &self, name: &str, provider: impl Provider + 'static, tools: ToolRegistry, system_prompt: &str, task: &str, ) -> Result<String, PoolError>
Convenience form: create and name a standard ReAct sub-agent (with a
given system prompt), running its first task immediately; afterwards
you can continue by name via send.
The only difference from spawn is how the
sub-agent is built — here the system prompt and task are given and
handled by the standard ReAct loop; the provider doesn’t need to be
Clone (a named creation calls the factory only once).
§Errors
Same as spawn: duplicate name
PoolError::Duplicate; first-task failure
PoolError::Run (the instance is kept, and the
conversation can continue).
Sourcepub async fn send(&self, name: &str, input: &str) -> Result<String, PoolError>
pub async fn send(&self, name: &str, input: &str) -> Result<String, PoolError>
Address by name: hand a new task to an already-created sub-agent, continuing its conversation (with its prior memory).
§Errors
Returns PoolError::NotFound when the name
doesn’t exist; PoolError::Run when the run fails.
Trait Implementations§
Source§impl Clone for SubAgentPool
impl Clone for SubAgentPool
Source§fn clone(&self) -> SubAgentPool
fn clone(&self) -> SubAgentPool
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more