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
- Expose a centralized factory enum (
BuiltInAgentMiddleware) pluscreateBuiltInAgentMiddlewareso callers can request middleware by name. - Provide a lightweight
AgentMiddlewareBuilderthat accepts descriptors{ name, config, when }and returns the assembled array, mirroring the builder used in Mate-X. - Update docs/examples to illustrate building agent middleware stacks with conditional guards.
- Add unit tests ensuring the builder respects
whenflags 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.