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: BLLM backend used for inference.
messages: Vec<Message>Full conversation history (system + user + assistant + tool messages).
tools: RegistryTool registry — tools the model may call.
max_steps: usizeMaximum number of inference turns per Agent::step call. Defaults to 10.
max_window: usizeMaximum 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: usizeMaximum 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: StringSession 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: boolUse 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>
impl<B: LlmBackend> Agent<B>
Sourcepub fn new(backend: B, system: &str) -> Self
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.
Sourcepub fn attach_store(
&mut self,
store: Arc<dyn MessageStore>,
session: &str,
) -> Result<(), String>
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.
Sourcepub fn step(&mut self, user_input: &str) -> Result<String, String>
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:
- Call the backend with the current message window
- If the response has no tool calls, return the assistant text
- Otherwise dispatch every tool call in parallel via
std::thread::scope, append the results to the message history, and loop.