generated from cloudwego/.github
-
Notifications
You must be signed in to change notification settings - Fork 712
refactor(adk): introduce AgentHandler interface for runtime agent customization #660
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shentongmartin
wants to merge
9
commits into
main
Choose a base branch
from
refactor/agent_middleware
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,535
−187
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Change-Id: I9a84f2a3e13721d06a86f5f2739772831067f10b
- Rename BeforeChatModelHandler to MessageStatePreProcessor - Rename AfterChatModelHandler to MessageStatePostProcessor - Rename BeforeChatModel/AfterChatModel methods to PreProcessMessageState/PostProcessMessageState - Remove type aliases InvokableToolCallNext/StreamableToolCallNext (inline function types) - Update helper functions: WithBeforeChatModel -> WithMessageStatePreProcessor, WithAfterChatModel -> WithMessageStatePostProcessor - Update all references in chatmodel.go and react.go - Fix state.Messages assignment in no-tools case to maintain backward compatibility The new naming better conveys that message changes are persisted to agent state and visible to subsequent iterations of the agent loop. Change-Id: I0d7be02aeae82a9412a5615685ce7e6fb88d517c
…modification Key changes: - Move BeforeAgent callback from NewChatModelAgent to Run/Resume time - Add support for runtime tools modification with lazy graph rebuilding - Simplify AgentHandler interface with unified method signatures - Add runtime ReturnDirectly configuration via context - Support graph config compatibility checking to minimize rebuilds Implementation details: - Add graphConfig struct to track graph configuration (hasTools, hasReturnDirectly) - Add preAppliedBeforeAgentData to avoid duplicate BeforeAgent calls - Implement getRunFunc with lazy rebuild logic for incompatible configs - Support runtime ReturnDirectly via context in react.go - Update handler helpers to use new AgentConfig-based API Breaking changes: - AgentHandler interface methods now take AgentConfig instead of separate params - BeforeAgent errors now returned at Run time instead of NewChatModelAgent time Change-Id: Ic392eacba8f2c5acdc9df32f1d0e64e54711a900
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #660 +/- ##
==========================================
- Coverage 80.31% 80.28% -0.03%
==========================================
Files 124 126 +2
Lines 11904 12181 +277
==========================================
+ Hits 9561 9780 +219
- Misses 1613 1653 +40
- Partials 730 748 +18 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
…s in HandlerToolsConfig ToToolMetas and ToolMetasToHandlerToolsConfig now accept context.Context and return error when tool.Info() fails, instead of silently skipping the ReturnDirectly configuration. Change-Id: I850c29a4588c18a11b66b9171403314c420f3dcf
5298ecd to
a5a144c
Compare
…tions Change-Id: Icc84f849f03ca5bacbcdf7c6841d927c726eccd9
- Remove Name() method from AgentHandler interface (unused) - Replace WrapInvokableToolCall/WrapStreamableToolCall with GetToolMiddleware() - Use compose.ToolMiddleware directly for tool wrapping - Simplify BaseAgentHandler (remove name field) - Replace WithInvokableToolWrapper/WithStreamableToolWrapper with WithToolMiddleware - Add documentation for ToolInput field mutability This change: 1. Reduces interface complexity by using existing compose.ToolMiddleware 2. Makes it easier to wrap both invoke and stream tool calls together 3. Provides forward compatibility for future tool types Change-Id: I0a5247795abe954326f1eb633e079c54a0126560
- Add type aliases: ToolCall, ToolResult, StreamToolResult (from compose package) - Add ToolCallHandler interface with HandleInvoke/HandleStream methods - Add BaseToolCallHandler with pass-through implementations - Replace GetToolMiddleware() with GetToolCallHandler() in AgentHandler - Replace WithToolMiddleware with WithToolCallHandler helper - Add legacyToolMiddlewareAdapter for AgentMiddleware backward compatibility - Update collectToolMiddlewares to convert ToolCallHandler to compose.ToolMiddleware This change: 1. Combines invoke/stream handling into one interface (must implement both) 2. Avoids Middleware/Endpoint terminology 3. Uses clearer type names (ToolCall vs ToolInput) 4. Explicit next() signature: next(ctx, call) makes data flow clear Change-Id: I9b3918d9f491b611e31606f83cef8c48f5233f4a
- Rename AgentConfig to AgentRunContext with only Instruction and Tools fields - Remove unused AgentRunOptions struct and AgentConfig.Input field - Remove unused HandlerToolsConfig and related functions - Move ToolMeta and AgentRunContext to handler.go - Delete tools_config.go - Remove AgentRunOptions parameter from applyBeforeAgent This simplifies the handler interface by focusing only on instruction and tools configuration, which are the primary customization points for handlers. Change-Id: I4c4182e4f2708ecd9c2d6cda7ff71a04020f4676
…reAgent signature - Rename ToolCallHandler to ToolCallWrapper - Rename HandleInvoke/HandleStream to WrapInvoke/WrapStream - Rename BaseToolCallHandler to BaseToolCallWrapper - Rename GetToolCallHandler() to GetToolCallWrapper() - Rename WithToolCallHandler() to WithToolCallWrapper() - Change BeforeAgent signature to return *AgentRunContext for functional style The new signature: BeforeAgent(ctx, runCtx) -> (ctx, runCtx, error) This is consistent with BeforeModelRewriteHistory and AfterModelRewriteHistory, making the data flow explicit and avoiding in-place mutation. Change-Id: Ice503ed130c3bbd07ef93cc7baa92ac6812dc568
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
This PR introduces a new
AgentHandlerinterface to replace the struct-basedAgentMiddlewareapproach, enabling runtime customization of agent behavior.Motivation
The existing
AgentMiddlewarehas fundamental limitations:These limitations block important use cases like:
Solution
New
AgentHandlerInterfaceKey design decisions:
BeforeAgentruns atRuntime, not creation time, enabling per-request customizationAgentConfigis mutable, allowing handlers to modify instruction and toolsBaseAgentHandlerprovides default no-op implementations for easy compositionExample: Runtime Tool Filtering
Convenience Helpers
WithInstruction(text)/WithInstructionFunc(fn)- Modify instructionsWithTools(tools...)/WithToolsFunc(fn)- Add/remove/filter toolsWithBeforeModelRewriteHistory(fn)/WithAfterModelRewriteHistory(fn)- Rewrite message historyWithInvokableToolCallWrapper(fn)/WithStreamableToolCallWrapper(fn)- Wrap tool callsBackward Compatibility
AgentMiddlewarenow implementsAgentHandler, so existing code continues to work unchanged.