Skip to main content

AgentBuilder

Struct AgentBuilder 

Source
pub struct AgentBuilder { /* private fields */ }
Expand description

Fluent builder for LoopConfig.

let config = AgentBuilder::new()
    .stream(provider)
    .tools(registry)
    .event_sink(channel_sink)
    .before_tool_call(retired_path_gate)
    .after_tool_call(repeat_detector)
    .context_transform(token_budget_pruner)
    .steering(steering_source)
    .max_iterations(50)
    .build();

Implementations§

Source§

impl AgentBuilder

Source

pub fn new() -> Self

Source

pub fn stream(self, stream: Arc<dyn StreamFn>) -> Self

Source

pub fn tools(self, tools: ToolRegistry) -> Self

Source

pub fn tools_arc(self, tools: Arc<ToolRegistry>) -> Self

Variant for callers that already share a registry by Arc.

Source

pub fn event_sink(self, sink: Arc<dyn EventSink>) -> Self

Source

pub fn default_execution_mode(self, mode: ExecutionMode) -> Self

Source

pub fn max_tool_calls_per_turn(self, max: usize) -> Self

Source

pub fn temperature(self, t: f32) -> Self

Source

pub fn max_output_tokens(self, t: u32) -> Self

Source

pub fn reasoning(self, level: ReasoningEffort) -> Self

Set the reasoning-effort knob forwarded to the stream transport on every turn. Per-run overrides flow through this typed surface rather than through stringly-typed provider extras.

Source

pub fn provider_extras(self, extras: Value) -> Self

Set provider-specific extras forwarded to the stream transport on every turn (e.g., response_format for structured output enforcement).

Source

pub fn max_output_tokens_recovery(self, recovery: MaxTokensRecovery) -> Self

Enable max-output-tokens recovery. When the provider returns StopReason::MaxTokens, the loop discards the truncated turn and re-streams with a larger cap up to recovery.max_attempts times. Off by default — opt in by passing a configured MaxTokensRecovery. See the type for cost discussion.

Source

pub fn max_iterations(self, n: usize) -> Self

Source

pub fn empty_outcome_retry_budget(self, n: usize) -> Self

Enable the no-tool outcome watchdog. n is the number of no-tool assistant stops that recovery plugins may handle; the next no-tool stop ends the run with a typed crate::error::LoopError.

Source

pub fn plain_text_terminal_fallback_tool( self, tool_name: impl Into<String>, ) -> Self

Convert plain assistant text into a terminal tool result on terminal-only compatibility turns. Intended for providers that reject tool_choice: "required" and therefore can leak final prose even while the host advertises only delivery tools.

Source

pub fn plain_text_terminal_fallback_eager(self, eager: bool) -> Self

Make Self::plain_text_terminal_fallback_tool fire on the FIRST plain-text stop instead of waiting for the turn allowlist to be narrowed to terminators by a downstream tool gate. Use this for providers in the “auto-when-forced” class where wire-level forcing isn’t available, so prose is the model’s default failure mode and the nudge cycle just burns turns. Has no effect unless Self::plain_text_terminal_fallback_tool is also set.

Source

pub fn plain_text_terminal_fallback_eager_nudge(self, on: bool) -> Self

Make the eager plain-text fallback path nudge the model with an explicit protocol-recovery system message before synthesizing a terminal tool result, giving up to a bounded number of retries to follow the protocol. Off by default — opt in for evals and runs where silently laundering prose into delivery is a worse outcome than a small number of extra streaming turns. Has no effect unless both Self::plain_text_terminal_fallback_tool and Self::plain_text_terminal_fallback_eager are set.

Source

pub fn grace_iterations(self, n: usize) -> Self

Override the grace window used by the auto-installed graceful-turn-limit plugin. Pass 0 to disable the soft warning entirely (the loop will hit max_iterations with no advance notice). Default is DEFAULT_GRACE_ITERATIONS.

Source

pub fn graceful_turn_limit_message_provider<F>(self, provider: F) -> Self
where F: Fn() -> String + Send + Sync + 'static,

Override the one-shot wrap-up message emitted by the auto-installed graceful-turn-limit plugin. Hosts can use this to make the warning aware of product state while keeping the core loop independent of product-specific types.

Source

pub fn graceful_turn_limit_grace_provider<F>(self, provider: F) -> Self
where F: Fn() -> usize + Send + Sync + 'static,

Supply a dynamic grace-iterations provider for the auto-installed graceful-turn-limit plugin. The callback is invoked on every steering poll, and its return value is clamped into [1, max_iterations - 1]. Use this to scale the wrap-up window with the size of the work in flight (e.g. more open plan phases ⇒ a longer wrap-up window so a partial delivery can still land). When unset, the static grace_iterations value is used.

Source

pub fn conversation_id(self, id: impl Into<String>) -> Self

Attach a conversation identifier so plugins can include conversation-scoped diagnostics or policy. The agent core itself does not consume this — it’s just metadata threaded through ToolGateContext. Optional; absent for tests and isolated subagent runs.

Source

pub fn model_id(self, id: impl Into<String>) -> Self

Attach a model identifier so context transforms can read it via crate::plugin::TransformContext::model_id. The loop itself does not consume this; the active StreamFn already knows its model. Optional — defaults to None (transforms see the empty string).

Source

pub fn token_estimator<E: TokenEstimator>(self, est: E) -> Self

Plug in a token estimator for budgeting and compaction. Defaults to the char-heuristic estimator when not set. Pass an Arc if the estimator is shared across multiple builders.

Source

pub fn token_estimator_arc(self, est: Arc<dyn TokenEstimator>) -> Self

Variant for callers that already share an estimator by Arc.

Source

pub fn protocol_policy(self, policy: Arc<dyn ProtocolPolicy>) -> Self

Install a ProtocolPolicy — the seam through which a downstream product supplies its tool vocabulary (plain-text recovery prose, tool-call alias repair, hidden-tool errors, terminal-tool classification). Defaults to crate::DefaultProtocolPolicy when not set, which keeps the core free of any product tool names. See crate::protocol.

Source

pub fn before_tool_call<P: BeforeToolCall + 'static>(self, plugin: P) -> Self

Source

pub fn after_tool_call<P: AfterToolCall + 'static>(self, plugin: P) -> Self

Source

pub fn context_transform<P: ContextTransform + 'static>(self, plugin: P) -> Self

Source

pub fn event_observer<P: EventObserver + 'static>(self, plugin: P) -> Self

Source

pub fn steering<P: SteeringSource + 'static>(self, plugin: P) -> Self

Source

pub fn follow_up<P: FollowUpSource + 'static>(self, plugin: P) -> Self

Source

pub fn before_tool_call_arc(self, plugin: Arc<dyn BeforeToolCall>) -> Self

Variant that takes pre-Arc’d trait objects, useful when the caller already has shared plugin instances.

Source

pub fn after_tool_call_arc(self, plugin: Arc<dyn AfterToolCall>) -> Self

Source

pub fn context_transform_arc(self, plugin: Arc<dyn ContextTransform>) -> Self

Source

pub fn event_observer_arc(self, plugin: Arc<dyn EventObserver>) -> Self

Source

pub fn follow_up_arc(self, plugin: Arc<dyn FollowUpSource>) -> Self

Source

pub fn steering_arc(self, plugin: Arc<dyn SteeringSource>) -> Self

Source

pub fn tool_gate_arc(self, plugin: Arc<dyn ToolGate>) -> Self

Source

pub fn plugin<P>(self, plugin: Arc<P>) -> Self

Generic plugin registration. Inspects Plugin::capabilities to decide which dispatch lists to add the plugin to. Same Arc is shared across all enabled capabilities so a single plugin instance can implement multiple traits.

Source

pub fn build(self) -> Result<LoopConfig, BuilderError>

Trait Implementations§

Source§

impl Default for AgentBuilder

Source§

fn default() -> Self

Returns the “default value” for a type. 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> 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, 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