pub struct ReActLoop { /* private fields */ }Expand description
The ReAct agent loop.
Implementations§
Source§impl ReActLoop
impl ReActLoop
Sourcepub fn new(config: AgentConfig) -> Self
pub fn new(config: AgentConfig) -> Self
Create a new ReActLoop with the given configuration and an empty tool registry.
Sourcepub fn with_observer(self, observer: Arc<dyn Observer>) -> Self
pub fn with_observer(self, observer: Arc<dyn Observer>) -> Self
Attach an observer for agent loop events.
Sourcepub fn with_action_hook(self, hook: ActionHook) -> Self
pub fn with_action_hook(self, hook: ActionHook) -> Self
Attach an action hook called before each tool dispatch.
Sourcepub fn with_metrics(self, metrics: Arc<RuntimeMetrics>) -> Self
pub fn with_metrics(self, metrics: Arc<RuntimeMetrics>) -> Self
Attach a shared RuntimeMetrics instance.
When set, the loop increments total_tool_calls on every tool dispatch
and failed_tool_calls whenever a tool returns an error observation.
Sourcepub fn registry(&self) -> &ToolRegistry
pub fn registry(&self) -> &ToolRegistry
Return a read-only reference to the tool registry.
Sourcepub fn tool_count(&self) -> usize
pub fn tool_count(&self) -> usize
Return the number of tools currently registered.
Shorthand for loop_.registry().tool_count().
Sourcepub fn unregister_tool(&mut self, name: &str) -> bool
pub fn unregister_tool(&mut self, name: &str) -> bool
Remove a registered tool by name. Returns true if the tool was found.
Sourcepub fn register_tool(&mut self, spec: ToolSpec)
pub fn register_tool(&mut self, spec: ToolSpec)
Register a tool that the agent loop can invoke.
Sourcepub fn register_tools(&mut self, specs: impl IntoIterator<Item = ToolSpec>)
pub fn register_tools(&mut self, specs: impl IntoIterator<Item = ToolSpec>)
Register multiple tools at once.
Equivalent to calling register_tool for each spec.
Sourcepub async fn run<F, Fut>(
&self,
prompt: &str,
infer: F,
) -> Result<Vec<ReActStep>, AgentRuntimeError>
pub async fn run<F, Fut>( &self, prompt: &str, infer: F, ) -> Result<Vec<ReActStep>, AgentRuntimeError>
Execute the ReAct loop for the given prompt.
§Arguments
prompt— user input passed as the initial contextinfer— async inference function: receives context string, returns response string
§Errors
AgentRuntimeError::AgentLoop("loop timeout …")— ifloop_timeoutis configured and the loop runs past the deadlineAgentRuntimeError::AgentLoop("max iterations … reached")— if the loop exhaustsmax_iterationswithout emittingFINAL_ANSWERAgentRuntimeError::AgentLoop("could not parse …")— if the model response cannot be parsed into aReActStep
Sourcepub async fn run_streaming<F, Fut>(
&self,
prompt: &str,
infer_stream: F,
) -> Result<Vec<ReActStep>, AgentRuntimeError>
pub async fn run_streaming<F, Fut>( &self, prompt: &str, infer_stream: F, ) -> Result<Vec<ReActStep>, AgentRuntimeError>
Execute the ReAct loop using a streaming inference function.
Identical to run except the inference closure returns a
tokio::sync::mpsc::Receiver that streams token chunks. All chunks
are collected into a single String before each iteration’s response
is parsed. Stream errors result in an empty partial response (the
erroring chunk is silently dropped and a warning is logged).