Class ContextManager
java.lang.Object
uno.anahata.ai.context.ContextManager
Manages the conversation context (history) for a
Chat session.
This class is responsible for:
- Maintaining the list of
ChatMessageobjects. - Tracking the total token count of the conversation.
- Managing context listeners for history changes.
- Delegating specialized tasks to
SessionManager(persistence),ResourceTracker(stateful files), andContextPruner(pruning).
-
Constructor Summary
ConstructorsConstructorDescriptionContextManager(Chat chat) Constructs a new ContextManager for the given Chat instance. -
Method Summary
Modifier and TypeMethodDescriptionvoidadd(ChatMessage message) Adds a new message to the conversation context.voidaddListener(ContextListener listener) Adds a listener to be notified of changes to the conversation history.voidclear()Clears all messages from the context and resets the token count.static ContextManagerGets the ContextManager for the currently executing tool.getChatMessageForPart(com.google.genai.types.Part part) Finds the ChatMessage that contains a specific Part instance.Returns a copy of the current conversation history.intGets the total token count of the conversation.voidNotifies listeners of a change in the conversation history and triggers an automatic backup.voidpruneMessages(List<Long> sequentialIds, String reason) Prunes a set of messages from the context by their sequential IDs.voidpruneParts(long messageSequentialId, List<Number> partIndices, String reason) Prunes specific parts from a message.voidprunePartsByReference(List<com.google.genai.types.Part> partsToPrune, String reason) Prunes specific parts from the context by their object references.voidpruneToolCall(String toolCallId, String reason) Prunes a specific tool call and its associated response from the context.voidremoveListener(ContextListener listener) Removes a previously added context listener.voidsetContext(List<ChatMessage> newContext) Replaces the entire conversation history with a new list of messages.
-
Constructor Details
-
ContextManager
Constructs a new ContextManager for the given Chat instance.- Parameters:
chat- The Chat instance to manage context for.
-
-
Method Details
-
getCallingInstance
Gets the ContextManager for the currently executing tool.This method uses a
ThreadLocalto retrieve the active instance, allowing tools to interact with the context without needing an explicit reference.- Returns:
- The active ContextManager.
- Throws:
IllegalStateException- if not called from a tool execution thread.
-
addListener
Adds a listener to be notified of changes to the conversation history.- Parameters:
listener- The listener to add.
-
removeListener
Removes a previously added context listener.- Parameters:
listener- The listener to remove.
-
add
Adds a new message to the conversation context.This method performs several side effects:
- Calculates and sets the elapsed time since the last message.
- Handles stateful resource replacement via
ResourceTracker. - Updates the total token count if usage metadata is present.
- Triggers ephemeral tool call pruning if the message is from the user.
- Notifies listeners of the history change.
- Parameters:
message- The message to add.
-
getTotalTokenCount
public int getTotalTokenCount()Gets the total token count of the conversation.- Returns:
- The total token count.
-
clear
public void clear()Clears all messages from the context and resets the token count. -
getContext
Returns a copy of the current conversation history.- Returns:
- A list of
ChatMessageobjects.
-
setContext
Replaces the entire conversation history with a new list of messages.This is typically used when restoring a session. It recalculates elapsed times for all messages and updates the token count from the most recent usage metadata.
- Parameters:
newContext- The new list of messages.
-
pruneMessages
-
pruneParts
-
prunePartsByReference
-
pruneToolCall
-
notifyHistoryChange
public void notifyHistoryChange()Notifies listeners of a change in the conversation history and triggers an automatic backup. -
getChatMessageForPart
Finds the ChatMessage that contains a specific Part instance.- Parameters:
part- The Part to find.- Returns:
- An Optional containing the ChatMessage if found, otherwise empty.
-
