Module middleware

Module middleware 

Source
Expand description

M — Middleware composition.

Compose middleware in any order with |.

§Wiring status

TextAgent pipelines (via AgentBuilder::middleware + AgentBuilder::build) — fully wired. Every factory in this module produces a MiddlewareComposite whose layers are installed into the LlmTextAgent middleware chain at compile time. Hooks fire in this order per run() call:

  1. before_model (forward order) — may short-circuit with a cached response.
  2. LLM call (skipped if before_model returned Some).
  3. after_model (reverse order) — may replace the LLM response.
  4. before_tool (forward) / after_tool (reverse) / on_tool_error (forward) — called for each tool dispatch round.
  5. on_error (forward) — called once if run() returns an error.

Live sessions (via Live::middleware) — the tool-lifecycle hooks are wired: before_tool (a returned error vetoes the call), after_tool, and on_tool_error fire around every tool dispatch in the control lane, including background tools. Model-level hooks (before_model/after_model) do not apply to Live — a Live session streams over the wire and has no discrete LlmRequest/LlmResponse to intercept; use them on TextAgent pipelines instead.

Structs§

AuditEntry
An audit log entry.
AuditMiddleware
Records all tool calls for audit review.
CacheMiddleware
Caches model responses keyed by request hash to avoid redundant LLM calls.
CostMiddleware
Tracks the number of tool calls as a proxy for cost.
M
The M namespace — static factory methods for middleware.
MetricsMiddleware
Collects request and error counts.
MiddlewareComposite
A middleware composite — one or more middleware layers (M::a() | M::b()).