Add request preprocessing and response postprocessing hooks to TriviaAgentLoop#5
Closed
andreisavu wants to merge 2 commits intomainfrom
Closed
Add request preprocessing and response postprocessing hooks to TriviaAgentLoop#5andreisavu wants to merge 2 commits intomainfrom
andreisavu wants to merge 2 commits intomainfrom
Conversation
Add two overridable methods for transforming requests before agent processing and responses before returning to callers: - preprocess_request(request) - override to transform incoming requests - postprocess_response(response) - override to transform outgoing responses Both methods return their input unchanged by default. Subclass TriviaAgentLoop and override these methods to implement custom preprocessing/postprocessing logic.
- preprocess_request now receives Session for context-aware preprocessing - postprocess_response now receives both Prompt and Session - Preprocessing moved inside prepare() for proper request transformation - Use instance variables to pass context from prepare() to postprocess_response()
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
This PR adds extensibility hooks to
TriviaAgentLoopthat allow subclasses to transform requests before agent processing and responses after agent execution. This enables custom validation, normalization, formatting, and enrichment logic without modifying the core loop implementation.Key Changes
preprocess_request()method: Allows subclasses to transform incomingTriviaRequestobjects before they are processed by the agent. Default implementation returns the request unchanged.postprocess_response()method: Allows subclasses to transform outgoingTriviaResponseobjects after agent execution. Default implementation returns the response unchanged.execute()method: Implements the preprocessing/postprocessing pipeline by:preprocess_request()on the incoming requestMainLoop.execute()with the preprocessed requestpostprocess_response()on the response output (if present)NoneMappingfromcollections.abc, andBudget,PromptResponse, andHeartbeatfromweakincentivesprepare()method explaining the execution flowImplementation Details
execute()beforeprepare()is called, ensuring consistent request handling throughout the execution flowprompt_response.outputis notNone, preventing unnecessary processing of error responsesTesting
Added three new test classes with 8 test cases covering:
execute()method