pub struct ObserverHost { /* private fields */ }Expand description
Holds registered observers and dispatches notifications to each.
Observers run in registration order. All observers are always notified —
there is no short-circuiting (use the hook system, requires hooks feature, for flow control).
An empty host (no observers registered) is effectively zero-cost:
each notification call iterates an empty Vec.
Implementations§
Source§impl ObserverHost
impl ObserverHost
Sourcepub fn new() -> Self
pub fn new() -> Self
Create an empty observer host.
Equivalent to ObserverHost::default but more explicit.
Sourcepub fn register(&mut self, observer: Arc<dyn LoopObserver>)
pub fn register(&mut self, observer: Arc<dyn LoopObserver>)
Register an observer.
Observers are called in registration order at each lifecycle point. Registering the same observer twice will result in duplicate notifications.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Number of observers currently registered.
Cheap (O(1)) read of the observer vector length. Returns 0
for a freshly constructed host, in which case every dispatch
method is effectively a no-op (it iterates an empty Vec).
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Whether no observers are registered.
true when len is 0. When true, every
dispatch call short-circuits through an empty loop, so an
observerless host adds negligible overhead to the agent loop.
Sourcepub fn reset_all(&self)
pub fn reset_all(&self)
Reset all observers for a new session.
Calls LoopObserver::reset on every registered observer,
allowing them to clear per-session accumulators.
Sourcepub fn on_run_start(&self, ctx: &RunStartContext)
pub fn on_run_start(&self, ctx: &RunStartContext)
Dispatch LoopObserver::on_run_start to all observers.
Fired at the start of each run() call, before the first turn
begins. Iterates registered observers in registration order; use it
to initialize per-run observer state.
Sourcepub fn on_run_end(&self, ctx: &RunEndContext)
pub fn on_run_end(&self, ctx: &RunEndContext)
Dispatch LoopObserver::on_run_end to all observers.
Fired at the end of each run() call, after the loop has
terminated. Iterates registered observers in registration order;
check RunEndContext::success in each observer to distinguish a
normal exit from a failure.
Sourcepub fn on_turn_start(&self, ctx: &TurnStartContext)
pub fn on_turn_start(&self, ctx: &TurnStartContext)
Dispatch LoopObserver::on_turn_start to all observers.
Fired before the model is called for the turn. Iterates registered observers in registration order.
Sourcepub fn on_turn_end(&self, ctx: &TurnEndContext)
pub fn on_turn_end(&self, ctx: &TurnEndContext)
Dispatch LoopObserver::on_turn_end to all observers.
Fired after the turn’s model call and any tool dispatch have
completed. Iterates registered observers in registration order;
check TurnEndContext::success to detect turn-level failures.
Sourcepub fn on_stream_success(&self, ctx: &StreamContext)
pub fn on_stream_success(&self, ctx: &StreamContext)
Dispatch LoopObserver::on_stream_success to all observers.
Fired when a streaming model call completes successfully.
Iterates registered observers in registration order. Not fired
when the stream fails — see
on_stream_failure.
Sourcepub fn on_stream_failure(&self, ctx: &StreamFailureContext)
pub fn on_stream_failure(&self, ctx: &StreamFailureContext)
Dispatch LoopObserver::on_stream_failure to all observers.
Fired when a streaming model call fails (timeout, transport error, rate limit). Iterates registered observers in registration order; the loop may retry or fall back to another model after this notification.
Sourcepub fn on_response(&self, ctx: &ResponseContext)
pub fn on_response(&self, ctx: &ResponseContext)
Dispatch LoopObserver::on_response to all observers.
Fired once the full model response is assembled — the committed assistant text plus any tool calls, before the framework resolves tool results. Iterates registered observers in registration order.
Sourcepub fn on_text_delta(&self, ctx: &TextDeltaContext)
pub fn on_text_delta(&self, ctx: &TextDeltaContext)
Dispatch LoopObserver::on_text_delta to all observers.
Iterates registered observers in registration order. Called once per
streamed text delta, so each observer’s on_text_delta must be cheap.
Sourcepub fn on_thinking_delta(&self, ctx: &ThinkingDeltaContext)
pub fn on_thinking_delta(&self, ctx: &ThinkingDeltaContext)
Dispatch LoopObserver::on_thinking_delta to all observers.
Iterates registered observers in registration order. Called once per
streamed reasoning delta, so each observer’s on_thinking_delta must
be cheap.
Sourcepub fn on_tool_pre(&self, ctx: &ToolPreContext)
pub fn on_tool_pre(&self, ctx: &ToolPreContext)
Dispatch LoopObserver::on_tool_pre to all observers.
Fired before a tool is dispatched, carrying the call’s name and
input. Iterates registered observers in registration order.
Notification-only — use the hook system (requires the hooks
feature) for flow control.
Sourcepub fn on_tool_call_received(&self, ctx: &ToolCallReceivedContext)
pub fn on_tool_call_received(&self, ctx: &ToolCallReceivedContext)
Dispatch LoopObserver::on_tool_call_received to all observers.
Fired when the framework receives a tool call from the model response, before it is dispatched. Iterates registered observers in registration order; useful for pending-call tracking.
Sourcepub fn on_tool_post(&self, ctx: &ToolPostContext)
pub fn on_tool_post(&self, ctx: &ToolPostContext)
Dispatch LoopObserver::on_tool_post to all observers.
Fired after a tool completes, carrying its output and timing. Iterates registered observers in registration order; useful for loop-detection correlation.
Sourcepub fn on_compaction(&self, ctx: &CompactedContext)
pub fn on_compaction(&self, ctx: &CompactedContext)
Dispatch LoopObserver::on_compaction to all observers.
Fired after the context manager reduces the history, carrying the before/after token counts. Iterates registered observers in registration order; fired only when compaction actually occurred, not on no-action passes.
Sourcepub fn on_fallback(&self, ctx: &FallbackContext)
pub fn on_fallback(&self, ctx: &FallbackContext)
Dispatch LoopObserver::on_fallback to all observers.
Fired when the fallback manager activates a fallback model, carrying the reason and the selected model. Iterates registered observers in registration order; the fallback model will be used for subsequent requests.
Sourcepub fn on_model_switched(&self, ctx: &ModelSwitchedContext)
pub fn on_model_switched(&self, ctx: &ModelSwitchedContext)
Dispatch LoopObserver::on_model_switched to all observers.
Fired after an explicit model switch is accepted by the client, carrying the old and new model names. Iterates registered observers in registration order.
Sourcepub fn on_loop_detected(&self, ctx: &LoopDetectedContext)
pub fn on_loop_detected(&self, ctx: &LoopDetectedContext)
Dispatch LoopObserver::on_loop_detected to all observers.
Fired when the loop detector observes the same operation repeatedly, exceeding the configured threshold. Iterates registered observers in registration order.
Sourcepub fn on_convergence_detected(&self, ctx: &ConvergenceDetectedContext)
pub fn on_convergence_detected(&self, ctx: &ConvergenceDetectedContext)
Dispatch LoopObserver::on_convergence_detected to all
observers.
Fired when the convergence detector observes semantically similar assistant responses, as determined by the convergence detection policy. Iterates registered observers in registration order.