Skip to content

feat(adk): add ChatModelAgentMiddleware constructors for filesystem and reduction packages#749

Merged
shentongmartin merged 1 commit intoalpha/08from
refactor/handler
Feb 4, 2026
Merged

feat(adk): add ChatModelAgentMiddleware constructors for filesystem and reduction packages#749
shentongmartin merged 1 commit intoalpha/08from
refactor/handler

Conversation

@shentongmartin
Copy link
Contributor

Summary

Problem Solution
Existing NewMiddleware and NewToolResultMiddleware return AgentMiddleware struct which has limited extensibility Added new NewChatModelAgentMiddleware constructors that return ChatModelAgentMiddleware interface

Key Insight

The ChatModelAgentMiddleware interface provides better context propagation through wrapper methods (WrapInvokableToolCall, WrapStreamableToolCall) compared to the struct-based AgentMiddleware. The new constructors enable:

  1. Better context propagation - Wrapper methods can pass modified context to the next wrapper in the chain
  2. More flexible extension points - Interface-based design allows users to implement custom handlers
  3. Consistent API - Both packages now follow the same pattern as adk.ChatModelAgentMiddleware

Changes

Filesystem Package

  • Added NewChatModelAgentMiddleware with BeforeAgent, WrapInvokableToolCall, WrapStreamableToolCall
  • Marked NewMiddleware as deprecated

Reduction Package

  • Added NewChatModelAgentMiddleware with BeforeModelRewriteState, WrapInvokableToolCall, WrapStreamableToolCall
  • Marked NewToolResultMiddleware as deprecated

概要

问题 解决方案
现有的 NewMiddlewareNewToolResultMiddleware 返回 AgentMiddleware 结构体,扩展性有限 添加新的 NewChatModelAgentMiddleware 构造函数,返回 ChatModelAgentMiddleware 接口

关键洞察

ChatModelAgentMiddleware 接口通过包装方法(WrapInvokableToolCallWrapStreamableToolCall)提供了比基于结构体的 AgentMiddleware 更好的上下文传播能力。新构造函数实现了:

  1. 更好的上下文传播 - 包装方法可以将修改后的上下文传递给链中的下一个包装器
  2. 更灵活的扩展点 - 基于接口的设计允许用户实现自定义处理器
  3. 一致的 API - 两个包现在都遵循与 adk.ChatModelAgentMiddleware 相同的模式

变更

Filesystem 包

  • 添加了 NewChatModelAgentMiddleware,包含 BeforeAgentWrapInvokableToolCallWrapStreamableToolCall
  • NewMiddleware 标记为已弃用

Reduction 包

  • 添加了 NewChatModelAgentMiddleware,包含 BeforeModelRewriteStateWrapInvokableToolCallWrapStreamableToolCall
  • NewToolResultMiddleware 标记为已弃用

- Add NewChatModelAgentMiddleware in filesystem package with BeforeAgent,
  WrapInvokableToolCall, and WrapStreamableToolCall methods
- Add NewChatModelAgentMiddleware in reduction package with BeforeModelRewriteState,
  WrapInvokableToolCall, and WrapStreamableToolCall methods
- Mark old NewMiddleware and NewToolResultMiddleware as deprecated
- Add comprehensive tests for the new constructors

Change-Id: I299f2736462eb701951faa33e230dc3e4b3a0ee6
@shentongmartin shentongmartin changed the title feat(adk/middlewares): add ChatModelAgentMiddleware constructors for filesystem and reduction packages feat(adk): add ChatModelAgentMiddleware constructors for filesystem and reduction packages Feb 4, 2026
@codecov
Copy link

codecov bot commented Feb 4, 2026

Codecov Report

❌ Patch coverage is 85.29412% with 20 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (alpha/08@b1fbde2). Learn more about missing BASE report.

Files with missing lines Patch % Lines
adk/middlewares/filesystem/filesystem.go 84.84% 5 Missing and 5 partials ⚠️
adk/middlewares/reduction/tool_result.go 85.71% 5 Missing and 5 partials ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##             alpha/08     #749   +/-   ##
===========================================
  Coverage            ?   80.50%           
===========================================
  Files               ?      135           
  Lines               ?    14031           
  Branches            ?        0           
===========================================
  Hits                ?    11296           
  Misses              ?     1879           
  Partials            ?      856           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@shentongmartin shentongmartin merged commit 6524f7a into alpha/08 Feb 4, 2026
19 of 20 checks passed
@shentongmartin shentongmartin deleted the refactor/handler branch February 4, 2026 07:34
hi-pender pushed a commit that referenced this pull request Feb 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants