pub struct AgentFactory { /* private fields */ }Expand description
A factory for creating multiple agents from a shared provider.
AgentFactory solves the “N agents from one provider” problem:
instead of repeating .provider(p) for each agent, create
a factory once and call spawn() with different prompts.
§Example
use traitclaw_core::factory::AgentFactory;
use traitclaw_core::traits::provider::Provider;
let factory = AgentFactory::new(provider);
let researcher = factory.spawn("You are a researcher.");
let writer = factory.spawn("You are a technical writer.");
let reviewer = factory.spawn("You are a code reviewer.");
// All three agents share the same provider config (via Arc)§How It Works
The factory wraps the provider in Arc<dyn Provider>, which is
cheaply cloneable. Each spawn() call clones the Arc
(incrementing the reference count) and creates a new agent.
Implementations§
Source§impl AgentFactory
impl AgentFactory
Sourcepub fn new(provider: impl Provider) -> Self
pub fn new(provider: impl Provider) -> Self
Create a new factory from a provider.
The provider is wrapped in an Arc for cheap cloning. Each
spawned agent shares the same underlying provider instance.
Sourcepub fn from_arc(provider: Arc<dyn Provider>) -> Self
pub fn from_arc(provider: Arc<dyn Provider>) -> Self
Create a factory from an already-wrapped Arc<dyn Provider>.
Use this when you already hold a shared provider reference.
Sourcepub fn spawn(&self, system: impl Into<String>) -> Agent
pub fn spawn(&self, system: impl Into<String>) -> Agent
Spawn an agent with the factory’s provider and a system prompt.
Each spawned agent holds its own Arc clone of the provider,
making agents fully independent (cheap reference-counted sharing).
§Example
use traitclaw_core::factory::AgentFactory;
use traitclaw_core::traits::provider::Provider;
let factory = AgentFactory::new(provider);
let agent = factory.spawn("You are a helpful assistant.");§Panics
This method cannot panic under normal usage — the internal builder always has a valid provider.
Sourcepub fn spawn_with(
&self,
f: impl FnOnce(AgentBuilder) -> AgentBuilder,
) -> Result<Agent>
pub fn spawn_with( &self, f: impl FnOnce(AgentBuilder) -> AgentBuilder, ) -> Result<Agent>
Spawn an agent with custom builder configuration.
Use this escape hatch when you need more than just a system prompt (e.g., adding tools, setting memory, configuring hooks).
The closure receives an AgentBuilder with the factory’s provider
already set. Call builder methods as needed.
§Example
use traitclaw_core::factory::AgentFactory;
use traitclaw_core::traits::provider::Provider;
let factory = AgentFactory::new(provider);
let agent = factory.spawn_with(|b| {
b.system("You are a researcher with tools.")
.max_iterations(10)
})?;§Errors
Returns an error if the builder customization produces an invalid agent configuration.
Trait Implementations§
Source§impl Clone for AgentFactory
impl Clone for AgentFactory
Source§fn clone(&self) -> AgentFactory
fn clone(&self) -> AgentFactory
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more