Class ContextManager
java.lang.Object
uno.anahata.asi.agi.event.BasicPropertyChangeSource
uno.anahata.asi.agi.context.ContextManager
- All Implemented Interfaces:
PropertyChangeSource, Rebindable
The definitive manager for an Agi session's context in the V2 architecture.
This manager orchestrates the 'Working Memory' of Anahata, maintaining the
conversation history, coordinating Retrieval-Augmented Generation (RAG),
and enforcing context window limits via the ContextWindowGarbageCollector.
- Author:
- anahata
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final AgiThe parent agi session.private intThe maximum number of tokens for the context window.private final ContextWindowGarbageCollectorThe specialized garbage collector for this context.private final List<AbstractMessage> The canonical conversation history.private final AtomicLongCounter for assigning unique, sequential IDs to messages.private final AtomicLongCounter for assigning unique, sequential IDs to parts.private final List<ContextProvider> List of registered context providers.Fields inherited from class BasicPropertyChangeSource
propertyChangeSupport -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddMessage(AbstractMessage message) The definitive method for adding any message to the agi history.private voidaddMessageInternal(AbstractMessage message) Adds a message to the history without triggering hard pruning.Constructs the RAG (Retrieval-Augmented Generation) message by aggregating content from all enabled context providers.Builds the final, filtered list of messages to be sent to the API.voidclear()Clears the entire conversation history and resets all internal counters to zero.Gets the complete, canonical conversation history for this session.intGets the current total token count of all parts in the context.Gets the list of system instructions by processing all enabled providers.intGets the maximum number of tokens allowed in the context window.private voidPerforms a hard prune on the entire agi history.private voidAssigns sequential IDs to the message and all its parts.voididentifyPart(AbstractPart part) Assigns a sequential ID to a single part if it doesn't already have one.voidinit()Initializes the manager and registers default providers.voidregisterContextProvider(ContextProvider provider) Registers a new context provider.voidremoveMessage(AbstractMessage message) Removes a message from the history.voidResets the cached token counts on all messages and parts currently resident in the conversation history, forcing a lazy, background recalculation.Methods inherited from class BasicPropertyChangeSource
addPropertyChangeListener, addPropertyChangeListener, rebind, removePropertyChangeListener, removePropertyChangeListenerMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface PropertyChangeSource
getPropertyChangeSupportMethods inherited from interface Rebindable
rebind
-
Field Details
-
agi
The parent agi session. -
history
The canonical conversation history. Uses CopyOnWriteArrayList to allow thread-safe iteration during background serialization (e.g., auto-save) without blocking streaming updates. -
garbageCollector
The specialized garbage collector for this context. -
messageIdCounter
Counter for assigning unique, sequential IDs to messages. -
partIdCounter
Counter for assigning unique, sequential IDs to parts. -
providers
List of registered context providers. -
contextWindowSize
private int contextWindowSizeThe maximum number of tokens for the context window.
-
-
Constructor Details
-
ContextManager
Constructs a new ContextManager.- Parameters:
agi- The parent agi session.
-
-
Method Details
-
init
public void init()Initializes the manager and registers default providers. -
clear
public void clear()Clears the entire conversation history and resets all internal counters to zero. Fires a property change event for the "history" property. -
resetTokenCounts
public void resetTokenCounts()Resets the cached token counts on all messages and parts currently resident in the conversation history, forcing a lazy, background recalculation. -
registerContextProvider
Registers a new context provider.- Parameters:
provider- The provider to register.
-
getSystemInstructions
-
buildVisibleHistory
Builds the final, filtered list of messages to be sent to the API.- Returns:
- The filtered list of messages.
-
buildRagMessage
Constructs the RAG (Retrieval-Augmented Generation) message by aggregating content from all enabled context providers.- Returns:
- A fully populated RagMessage.
-
addMessage
The definitive method for adding any message to the agi history.- Parameters:
message- The message to add.
-
identifyMessage
Assigns sequential IDs to the message and all its parts.- Parameters:
msg- The message to identify.
-
identifyPart
Assigns a sequential ID to a single part if it doesn't already have one.- Parameters:
part- The part to identify.
-
addMessageInternal
Adds a message to the history without triggering hard pruning.- Parameters:
message- The message to add.
-
removeMessage
Removes a message from the history.- Parameters:
message- The message to remove.
-
hardPrune
private void hardPrune()Performs a hard prune on the entire agi history. In the strict atomic model, messages stay structurally intact until all their parts are collectable. -
getHistory
Gets the complete, canonical conversation history for this session.- Returns:
- A synchronized, unmodifiable list of all messages.
-
getLastTotalTokenCount
public int getLastTotalTokenCount()Gets the current total token count of all parts in the context.- Returns:
- The total token count.
-
getTokenThreshold
public int getTokenThreshold()Gets the maximum number of tokens allowed in the context window.- Returns:
- The token threshold.
-

