Class ContextPruner
This class implements complex logic to ensure that when a part is removed, all its dependent parts (e.g., the corresponding function call for a function response) are also identified and removed, maintaining the integrity of the conversation.
It supports manual pruning by ID, automatic pruning of old ephemeral tool calls, and stateful resource replacement.
-
Constructor Summary
ConstructorsConstructorDescriptionContextPruner(ContextManager contextManager) Constructs a new ContextPruner for the given ContextManager. -
Method Summary
Modifier and TypeMethodDescriptionvoidpruneEphemeralToolCall(String toolCallId, String reason) Prunes a single ephemeral tool call and its associated response.voidpruneEphemeralToolCalls(List<String> toolCallIds, String reason) Prunes a list of ephemeral tool calls and their associated responses.voidpruneEphemeralToolCalls(ToolManager toolManager) Implements the automatic pruning of old tool calls based on the "ephemeral" rules.voidpruneMessages(List<Long> sequentialIds, String reason) Prunes entire ChatMessage objects from the context based on their unique sequential IDs.voidpruneOther(List<com.google.genai.types.Part> parts, String reason) Prunes non-tool parts (text, blobs, etc.) from the context.voidpruneParts(long messageSequentialId, List<Number> partIndices, String reason) Prunes specific parts from a single ChatMessage, identified by the message's sequential ID and the parts' indices.voidprunePartsByReference(List<com.google.genai.types.Part> initialCandidates, String reason) The low-level workhorse method that removes a given set of Part objects from the context.voidpruneToolCall(String toolCallId, String reason) Prunes a single tool call (ephemeral or stateful) by its ID.voidpruneToolCalls(List<String> toolCallIds, String reason) Prunes a list of tool calls by their IDs.
-
Constructor Details
-
ContextPruner
Constructs a new ContextPruner for the given ContextManager.- Parameters:
contextManager- The ContextManager to prune for.
-
-
Method Details
-
pruneMessages
Prunes entire ChatMessage objects from the context based on their unique sequential IDs.This method identifies all parts within the targeted messages and delegates the actual removal to
prunePartsByReference(List, String)to ensure all dependencies are also pruned.- Parameters:
sequentialIds- A list of message sequential IDs to remove.reason- The reason for the pruning.
-
pruneParts
Prunes specific parts from a single ChatMessage, identified by the message's sequential ID and the parts' indices.This method identifies the specific Part objects and delegates the removal to
prunePartsByReference(List, String)to handle dependency resolution.- Parameters:
messageSequentialId- The sequential ID of the message containing the parts to prune.partIndices- A list of zero-based indices of the parts to remove.reason- The reason for the pruning.
-
prunePartsByReference
public void prunePartsByReference(List<com.google.genai.types.Part> initialCandidates, String reason) The low-level workhorse method that removes a given set of Part objects from the context.This method performs a recursive, cross-message dependency traversal to find all parts that must be removed along with the initial candidates. It then updates the affected messages and cleans their dependency maps.
- Parameters:
initialCandidates- A list of the actual Part objects to remove.reason- The reason for the pruning.
-
pruneEphemeralToolCall
-
pruneEphemeralToolCalls
Prunes a list of ephemeral tool calls and their associated responses.- Parameters:
toolCallIds- The list of tool call IDs.reason- The reason for pruning.- Throws:
IllegalArgumentException- if any of the IDs are associated with a stateful resource.
-
pruneOther
Prunes non-tool parts (text, blobs, etc.) from the context.- Parameters:
parts- The list of Part objects to prune.reason- The reason for pruning.- Throws:
IllegalArgumentException- if any of the parts are tool calls or responses.
-
pruneToolCall
-
pruneToolCalls
-
pruneEphemeralToolCalls
Implements the automatic pruning of old tool calls based on the "ephemeral" rules.A part is considered ephemeral if:
- The tool is explicitly marked as
ContextBehavior.EPHEMERAL. - It is a function call with no corresponding response (orphan).
- It is a function response from a stateful tool that failed to return a resource.
- Parameters:
toolManager- The ToolManager, needed to check tool metadata.
- The tool is explicitly marked as
-
