Package com.tnsai.context.ephemeral
Class EphemeralContextManager
java.lang.Object
com.tnsai.context.ephemeral.EphemeralContextManager
Manages ephemeral tool outputs to prevent context explosion.
Tracks tool call results and automatically prunes older results when the configured limit is exceeded.
Usage
EphemeralContextManager manager = new EphemeralContextManager();
// Register ephemeral action
manager.registerAction("getBrowserState", 3);
// Track tool results
manager.trackResult("getBrowserState", result1);
manager.trackResult("getBrowserState", result2);
manager.trackResult("getBrowserState", result3);
manager.trackResult("getBrowserState", result4);
// Get IDs of results to remove from context
List<String> toRemove = manager.getResultsToRemove("getBrowserState");
// Returns [result1_id] - oldest result exceeds keepLast=3
Thread Safety
This class is thread-safe and can be used from multiple threads.
- Author:
- TnsAI Team
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordConfiguration for an ephemeral action.static final recordTracks a single tool result. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclearAll()Clears all tracked results for all actions.voidclearResults(String actionName) Clears all tracked results for an action.generateSummary(String actionName, int removedCount) Generates a summary message for removed results.Gets the configuration for an action.Gets all registered ephemeral action names.Gets a specific result by ID.intgetResultCount(String actionName) Gets the count of tracked results for an action.getResults(String actionName) Gets all tracked results for an action.getResultsToRemove(String actionName) Gets the IDs of results that should be removed from context.getStats()Gets statistics about tracked results.booleanisEphemeral(String actionName) Checks if an action is registered as ephemeral.pruneAll()Prunes all ephemeral actions.pruneResults(String actionName) Prunes old results for an action.voidregisterAction(String actionName, int keepLast) Registers an action as ephemeral with the specified keepLast count.voidregisterAction(String actionName, EphemeralContextManager.EphemeralConfig config) Registers an action as ephemeral with full config.voidregisterFromMethod(Method method) Registers an action from its method annotation.trackResult(String actionName, Object result) Tracks a new tool result.
-
Constructor Details
-
EphemeralContextManager
public EphemeralContextManager()Creates a new ephemeral context manager.
-
-
Method Details
-
registerAction
Registers an action as ephemeral with the specified keepLast count.- Parameters:
actionName- the action namekeepLast- number of results to keep
-
registerAction
Registers an action as ephemeral with full config.- Parameters:
actionName- the action nameconfig- the ephemeral configuration
-
registerFromMethod
Registers an action from its method annotation.- Parameters:
method- the method with @Ephemeral annotation
-
isEphemeral
Checks if an action is registered as ephemeral.- Parameters:
actionName- the action name- Returns:
- true if ephemeral
-
getConfig
Gets the configuration for an action.- Parameters:
actionName- the action name- Returns:
- config or null if not ephemeral
-
trackResult
Tracks a new tool result.- Parameters:
actionName- the action nameresult- the result object- Returns:
- the tracked result with generated ID
-
getResultsToRemove
Gets the IDs of results that should be removed from context.These are the oldest results that exceed the keepLast limit.
- Parameters:
actionName- the action name- Returns:
- list of result IDs to remove (empty if none)
-
pruneResults
Prunes old results for an action.Removes results that exceed the keepLast limit.
- Parameters:
actionName- the action name- Returns:
- list of removed results
-
pruneAll
Prunes all ephemeral actions.- Returns:
- map of action name to removed results
-
generateSummary
Generates a summary message for removed results.- Parameters:
actionName- the action nameremovedCount- number of removed results- Returns:
- summary message
-
getResults
Gets all tracked results for an action.- Parameters:
actionName- the action name- Returns:
- list of tracked results (oldest first)
-
getResult
Gets a specific result by ID.- Parameters:
resultId- the result ID- Returns:
- the tracked result or null
-
getResultCount
Gets the count of tracked results for an action.- Parameters:
actionName- the action name- Returns:
- result count
-
clearResults
Clears all tracked results for an action.- Parameters:
actionName- the action name
-
clearAll
public void clearAll()Clears all tracked results for all actions. -
getRegisteredActions
Gets all registered ephemeral action names.- Returns:
- set of action names
-
getStats
Gets statistics about tracked results.- Returns:
- map of action name to result count
-