feat(agent): add add_hook convenience method for hook registration#1693
Closed
feat(agent): add add_hook convenience method for hook registration#1693
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
Assessment: Approve Clean implementation of a convenience method that improves hook registration DX. The code is well-tested and documented. Review Notes
Nice addition that aligns with SDK tenet #4 (the obvious path is the happy path). |
Unshure
commented
Feb 13, 2026
Member
Author
|
/strands |
Unshure
commented
Feb 13, 2026
Member
Author
|
/strands |
Unshure
commented
Feb 13, 2026
Member
Author
|
/strands |
Unshure
commented
Feb 13, 2026
Member
Author
|
/strands Support a list of event_types, and a union type for the callback event type. Any, or BaseEvent will support all lifecycle events |
Member
Author
|
/strands Lets actually just keep it to a single event for now. We can add a list or a union later |
Add a public add_hook method to the Agent class that provides a cleaner API for registering hook callbacks. This is a convenience method that delegates to self.hooks.add_callback but provides a more discoverable interface for users. Changes: - Add Agent.add_hook(callback, event_type=None) method - Support inferring event_type from callback's type hint - Update HookRegistry.add_callback to support type inference - Raise clear ValueError when type cannot be inferred The method: - Accepts both sync and async callbacks - Uses proper type hints with TEvent and HookCallback - Includes comprehensive Google-style docstring with examples Resolves #1686
dd0d1b8 to
ae504bd
Compare
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
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.
Motivation
Currently, registering a hook requires accessing the internal hooks registry:
This exposes implementation details and is less discoverable for users. Plugin authors and users who want to add hooks directly need a cleaner, more intuitive API.
Resolves #1686
Public API Changes
The Agent class now has an
add_hookmethod that provides a cleaner API for registering hook callbacks:The
add_callbackmethod onHookRegistryalso supports type inference:When
event_typeis not provided, it's inferred from the callback's first parameter type hint. AValueErroris raised with a helpful message if inference fails.Use Cases
hooksregistryinit_pluginimplementations can use this cleaner API