pub struct LoopRunner<'a> { /* private fields */ }Expand description
The ReAct loop execution context.
Holds references to the agent configuration and tool registry and drives
the Thought -> Action -> Observation -> FinalAnswer cycle. It never calls a
model itself: you pass each model response to step (text
ReAct) or step_response (native tool calling).
Use Agent if you would rather not manage the borrows.
Implementations§
Source§impl<'a> LoopRunner<'a>
impl<'a> LoopRunner<'a>
Sourcepub fn new(config: &'a AgentConfig, registry: &'a ToolRegistry) -> Self
pub fn new(config: &'a AgentConfig, registry: &'a ToolRegistry) -> Self
Creates a new runner with its own conversation history.
Sourcepub fn with_system_prompt(self) -> Self
pub fn with_system_prompt(self) -> Self
Adds the standard ReAct system prompt (format rules plus the tool list) as the first message. System messages are never evicted from history.
Sourcepub fn step(&mut self, llm_response: &str) -> Result<ReActStep, AgentError>
pub fn step(&mut self, llm_response: &str) -> Result<ReActStep, AgentError>
Processes one text LLM response, dispatching the tool when it is an Action.
With config.recover_from_errors, an unknown tool or an unparseable
reply becomes an Error: ... observation the model can correct,
instead of an error that ends the loop.
§Errors
AgentError::MaxIterationsExceeded: more thanconfig.max_iterationssteps.AgentError::ParseError: the response does not match any ReAct format.AgentError::ToolNotFound: the requested tool is not in the registry.
Sourcepub fn step_at(
&mut self,
llm_response: &str,
now_ms: f64,
) -> Result<ReActStep, AgentError>
pub fn step_at( &mut self, llm_response: &str, now_ms: f64, ) -> Result<ReActStep, AgentError>
Like step, and also enforces config.timeout_ms. Pass
the current time in milliseconds (for example Date.now()); the first
call starts the clock.
§Errors
AgentError::Timeout once more than timeout_ms has passed since the
first call, plus everything step returns.
Sourcepub fn step_response(
&mut self,
response_json: &str,
) -> Result<NativeStep, AgentError>
pub fn step_response( &mut self, response_json: &str, ) -> Result<NativeStep, AgentError>
Processes one native tool-calling response: the raw JSON body from
OpenAI Chat Completions or Responses, Anthropic Messages, or Ollama’s
chat API. Every tool call in it is dispatched (arguments arrive as JSON;
see ToolRegistry::dispatch_call) and the results are returned and
kept in the history with their call ids.
§Errors
AgentError::ParseError if the body is not a recognised response,
AgentError::MaxIterationsExceeded, AgentError::ToolNotFound
(unless recover_from_errors).
Sourcepub fn run_scripted(&mut self, responses: &[&str]) -> Result<String, AgentError>
pub fn run_scripted(&mut self, responses: &[&str]) -> Result<String, AgentError>
Runs a pre-scripted sequence of LLM responses, returning the final answer.
Useful for offline simulation and deterministic testing.
§Errors
AgentError::MaxIterationsExceeded: sequence exhausted before aFinalAnswer.- Any error from
step.
Sourcepub fn transcript(&self) -> String
pub fn transcript(&self) -> String
The conversation so far as one prompt string, ready to send to a
completion-style model: system and user messages as-is, model turns
as-is, tool results as Observation: ... lines.
Sourcepub fn history(&self) -> &ConversationHistory
pub fn history(&self) -> &ConversationHistory
Returns a reference to the accumulated conversation history.
Sourcepub fn iteration_count(&self) -> usize
pub fn iteration_count(&self) -> usize
Number of model responses processed so far.