Skip to main content

Agent

Struct Agent 

Source
pub struct Agent<B: LlmBackend> {
    pub backend: B,
    pub messages: Vec<Message>,
    pub tools: Registry,
    pub max_steps: usize,
    pub max_window: usize,
    pub max_tool_result_bytes: usize,
    pub store: Option<Arc<dyn MessageStore>>,
    pub session: String,
    pub observer: Arc<dyn Observer>,
    pub on_token: Option<Box<dyn FnMut(&str) + Send>>,
    pub stream: bool,
}
Expand description

The agent loop.

Fields§

§backend: B

LLM backend used for inference.

§messages: Vec<Message>

Full conversation history (system + user + assistant + tool messages).

§tools: Registry

Tool registry — tools the model may call.

§max_steps: usize

Maximum number of inference turns per Agent::step call. Defaults to 10.

§max_window: usize

Maximum messages sent to the backend per turn. Truncation advances to a user-message boundary so tool_use/tool_result pairs are never split. Defaults to 40.

§max_tool_result_bytes: usize

Maximum raw bytes per tool result, truncated before <tool_output> framing. Defaults to DEFAULT_MAX_TOOL_RESULT_BYTES (64KB).

§store: Option<Arc<dyn MessageStore>>

Optional persistence layer.

§session: String

Session identifier for the store (defaults to “default”).

§observer: Arc<dyn Observer>

Lifecycle observer. Defaults to NoOpObserver.

§on_token: Option<Box<dyn FnMut(&str) + Send>>

Token callback — if set, streamed deltas are pushed here and stream is ignored. This is the preferred streaming sink as of v0.2.

Migration: replace agent.stream = true (which prints to stdout) with:

agent.on_token = Some(Box::new(|tok| {
    print!("{}", tok);
    std::io::stdout().flush().ok();
}));
§stream: bool
👎Deprecated since 0.2.0:

Use Agent::on_token for a user-controlled token sink. stream = true still prints to stdout when on_token is None.

Legacy: stream to stdout when on_token is not set. Kept for v0.1 compatibility; prefer Agent::on_token.

Implementations§

Source§

impl<B: LlmBackend> Agent<B>

Source

pub fn new(backend: B, system: &str) -> Self

Create a new agent with the given backend and system prompt.

The agent starts with a fresh message history containing only the system prompt. No tools are registered and no persistence is attached.

Source

pub fn attach_store( &mut self, store: Arc<dyn MessageStore>, session: &str, ) -> Result<(), String>

Attach a persistent message store and resume the named session.

If the session has no prior history, the current in-memory messages (i.e. the system prompt) are persisted. Otherwise, the agent’s history is replaced with the loaded session.

Source

pub fn step(&mut self, user_input: &str) -> Result<String, String>

Run the agent loop on a new user input.

Iterates up to max_steps times:

  1. Call the backend with the current message window
  2. If the response has no tool calls, return the assistant text
  3. Otherwise dispatch every tool call in parallel via std::thread::scope, append the results to the message history, and loop.

Auto Trait Implementations§

§

impl<B> Freeze for Agent<B>
where B: Freeze,

§

impl<B> !RefUnwindSafe for Agent<B>

§

impl<B> Send for Agent<B>

§

impl<B> !Sync for Agent<B>

§

impl<B> Unpin for Agent<B>
where B: Unpin,

§

impl<B> UnsafeUnpin for Agent<B>
where B: UnsafeUnpin,

§

impl<B> !UnwindSafe for Agent<B>

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