Skip to main content

Agent

Struct Agent 

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

A headless model/tool loop over caller-supplied context and capabilities.

Resource lifetime belongs to ToolExecutor; persistence belongs to the caller. The context-window setting is informational and does not enforce a token limit or perform automatic compaction.

Implementations§

Source§

impl Agent

Source

pub fn new( model: Arc<dyn GenerativeModel>, tools: Arc<dyn ToolExecutor>, sink: Arc<dyn EventSink>, ) -> Self

Source

pub fn with_context( model: Arc<dyn GenerativeModel>, tools: Arc<dyn ToolExecutor>, sink: Arc<dyn EventSink>, context: TraceContext, ) -> Self

Source

pub fn set_tools(&mut self, tools: Arc<dyn ToolExecutor>)

Replace dispatch capabilities without rebuilding the model’s tool catalog. Keep both in sync when adding or removing advertised tools.

Source

pub fn set_context(&mut self, context: TraceContext)

Source

pub fn set_checkpoint(&mut self, checkpoint: Option<Checkpoint>)

Install the durable effect checkpoint (see Checkpoint).

Source

pub fn set_before_generation_notice( &mut self, notice: Option<BeforeGenerationNotice>, )

Source

pub fn checkpoint_failed(&self) -> bool

Source

pub fn checkpoint(&self) -> Result<(), AgentInteractionError>

Retry persistence without advancing or replaying an effect.

Source

pub fn history(&self) -> &[Message]

Source

pub fn replace_context( &mut self, history: Vec<Message>, usage: Option<TokenUsage>, ) -> Result<(), StateError>

Replace model context and its usage estimate without changing live tool state.

Source

pub fn state(&self) -> &AgentState

Source

pub fn set_retry_policy(&mut self, retry_policy: RetryPolicy)

Source

pub fn set_model(&mut self, model: Arc<dyn GenerativeModel>)

Swap the generative model (e.g. mid-session /effort rebuild). History is kept.

Source

pub fn append_input( &mut self, message: Message, ) -> Result<(), AgentInteractionError>

Append input at a well-formed context boundary and checkpoint it.

Source

pub fn append_system( &mut self, parts: Vec<Content>, ) -> Result<(), AgentInteractionError>

Source

pub fn truncate_history( &mut self, index: usize, ) -> Result<Vec<Message>, AgentInteractionError>

Source

pub fn set_context_window_tokens(&mut self, tokens: u64)

Set the model context budget available to callers.

Source

pub fn context_window_tokens(&self) -> u64

Source

pub fn set_max_truncated_resumes(&mut self, resumes: u32)

Set how many consecutive max_tokens truncations one turn resumes through (the active model’s max_truncated_resumes; 0 never resumes).

Source

pub fn last_usage(&self) -> Option<TokenUsage>

Last observed prompt/context token usage (from the provider), if any.

Source

pub fn context(&self) -> &TraceContext

Source

pub async fn run( &mut self, cancel: CancelToken, ) -> Result<Vec<Content>, AgentInteractionError>

Drive the existing model context to completion. The caller supplies input separately. The supplied tool executor determines the lifetime of live resources.

Returns answer content from the final generation; intervening responses and tool rounds remain in Self::history. Emits AgentEvent::TurnFinished on success, error, or cooperative cancellation. Checkpoints persist both pending effects and the final settled state.

Source

pub async fn run_with_outcome( &mut self, cancel: CancelToken, ) -> Result<RunOutcome, AgentInteractionError>

Drive context and retain the stop reason and measured usage for evaluators.

Source

pub fn start_run(&mut self) -> Result<(), AgentInteractionError>

Source

pub async fn continue_run( &mut self, cancel: CancelToken, ) -> Result<RunOutcome, AgentInteractionError>

Continue after a checkpoint failure without repeating completed effects. A dropped in-flight effect requires explicit recover_interrupted first.

Source

pub fn replace_at_boundary( &mut self, history: Vec<Message>, usage: Option<TokenUsage>, ) -> Result<(), StateError>

Source

pub fn recover_interrupted(&mut self) -> Result<(), AgentInteractionError>

Source

pub fn cancel_at_boundary(&mut self) -> Result<(), AgentInteractionError>

Source

pub async fn step( &mut self, cancel: CancelToken, ) -> Result<Option<RunOutcome>, AgentInteractionError>

Execute one generation or tool batch and checkpoint its outcome. None yields to the caller at the next effect, allowing compaction or inspection.

Auto Trait Implementations§

§

impl !Freeze for Agent

§

impl !RefUnwindSafe for Agent

§

impl !UnwindSafe for Agent

§

impl Send for Agent

§

impl Sync for Agent

§

impl Unpin for Agent

§

impl UnsafeUnpin for Agent

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.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

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.

§

impl<T> PolicyExt for T
where T: ?Sized,

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + 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
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + 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, 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.
§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more