feat(plugins): make pre_api_request hook mutable#11112
Open
hermes-agent-dhabibi wants to merge 1 commit into
Open
feat(plugins): make pre_api_request hook mutable#11112hermes-agent-dhabibi wants to merge 1 commit into
hermes-agent-dhabibi wants to merge 1 commit into
Conversation
Pass api_kwargs into the pre_api_request hook. If any callback returns a dict, use the last non-None dict as the replacement api_kwargs for the API call. This lets plugins set tool_choice, inject parameters, or modify the request payload before it hits the provider. Convention: last non-None dict wins (allows layered plugin overrides). Co-authored-by: dhabibi <9087935+dhabibi@users.noreply.github.com>
19 tasks
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.
Summary
The
pre_api_requesthook is currently observe-only — plugins receive metadata (model name, token counts, etc.) but cannot modify the actual API request. The return value is discarded.This PR passes
api_kwargsinto the hook and allows callbacks to return a modified dict that replaces the originalapi_kwargsfor the API call.Changes
run_agent.py(~5 lines changed):api_kwargs=api_kwargsas an additional kwarg to thepre_api_requesthookNonedictbecomes the newapi_kwargsConvention
Last non-
Nonedict wins. If multiple plugins return modified kwargs, the last one in registration order takes precedence. Plugins that just observe can continue returningNone— fully backward compatible.Use cases
tool_choiceto force tool calls during specific execution phasesExample
Testing
Existing tests pass —
test_tool_calls_then_stop(exercises the hook path) and all 48 plugin tests.