Skip to main content

AgentFactory

Struct AgentFactory 

Source
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

Source

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.

Source

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.

Source

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.

Source

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

Source§

fn clone(&self) -> AgentFactory

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for AgentFactory

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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