feat(deepagents): allow overriding default middleware for deepagent and subagents #204
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Adds middleware override capability for DeepAgents and subagents, allowing users to replace specific default middleware by name while preserving the middleware execution order.
Motivation
Previously, middleware arrays were merged through simple concatenation (
[...defaults, ...custom]), making it difficult to replace or customize default middleware behavior as langchain'screateAgentthrows an error if detects multiple same-named middleware.Users who wanted to modify a specific middleware (e.g., filesystem, skills, memory) had to either:
This commit introduces a smarter merge strategy that:
namepropertyThis gives users fine-grained control over agent behavior without requiring deep knowledge of the internal middleware ordering.
Changes
libs/deepagents/src/middleware/utils.tsAdded the
mergeMiddlewarefunction that implements the merge strategy:Example behavior:
libs/deepagents/src/middleware/utils.test.tsAdded test coverage for
mergeMiddlewarewith 10 test cases covering:libs/deepagents/src/agent.tsUpdated
createDeepAgentfunction:mergeMiddlewarefor the main agent's runtime middlewareBefore:
After:
libs/deepagents/src/middleware/subagents.tsUpdated
getSubagentsfunction:mergeMiddlewarefor subagent middlewareBefore:
After: