Support conditional construction of agent middleware stacks

Summary

Agent creation often requires toggling multiple middleware components (todo list, human-in-the-loop, prompt caching, etc.) based on runtime conditions. Today every caller must import each factory and manually push them into arrays, which leads to duplicated logic and error-prone configuration handling.

Proposal

  1. Expose a centralized factory enum (BuiltInAgentMiddleware) plus createBuiltInAgentMiddleware so callers can request middleware by name.
  2. Provide a lightweight AgentMiddlewareBuilder that accepts descriptors { name, config, when } and returns the assembled array, mirroring the builder used in Mate-X.
  3. Update docs/examples to illustrate building agent middleware stacks with conditional guards.
  4. Add unit tests ensuring the builder respects when flags and that factory entries require the correct config shape.

Motivation

  • Improves ergonomics for teams composing agents with many optional behaviors.
  • Avoids repeated imports and misconfiguration (e.g. missing retry configs).
  • Gives us a single extension point when introducing new built-in middleware.

Could you write up an API spec for this? A bit confused about interface, but intrigued by the idea, thanks!

1 Like

Thank you for taking the time to read my topic. I really appreciate it. :slight_smile:
I’ve submitted a PR to the carefreelife98/agent-middleware-builder repository.
When you get a chance, please review it and let me know if you have any feedback.
Thanks again!