Class ContextManager

java.lang.Object
uno.anahata.asi.agi.event.BasicPropertyChangeSource
uno.anahata.asi.agi.context.ContextManager
All Implemented Interfaces:
PropertyChangeSource, Rebindable

public class ContextManager extends BasicPropertyChangeSource implements 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 Details

    • agi

      private final Agi agi
      The parent agi session.
    • history

      private final List<AbstractMessage> history
      The canonical conversation history. Uses CopyOnWriteArrayList to allow thread-safe iteration during background serialization (e.g., auto-save) without blocking streaming updates.
    • garbageCollector

      private final ContextWindowGarbageCollector garbageCollector
      The specialized garbage collector for this context.
    • messageIdCounter

      private final AtomicLong messageIdCounter
      Counter for assigning unique, sequential IDs to messages.
    • partIdCounter

      private final AtomicLong partIdCounter
      Counter for assigning unique, sequential IDs to parts.
    • providers

      private final List<ContextProvider> providers
      List of registered context providers.
    • contextWindowSize

      private int contextWindowSize
      The maximum number of tokens for the context window.
  • Constructor Details

    • ContextManager

      public ContextManager(@NonNull @NonNull Agi agi)
      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

      public void registerContextProvider(ContextProvider provider)
      Registers a new context provider.
      Parameters:
      provider - The provider to register.
    • getSystemInstructions

      public List<String> getSystemInstructions()
      Gets the list of system instructions by processing all enabled providers.
      Returns:
      A list of formatted system instruction strings.
    • buildVisibleHistory

      public List<AbstractMessage> buildVisibleHistory()
      Builds the final, filtered list of messages to be sent to the API.
      Returns:
      The filtered list of messages.
    • buildRagMessage

      public RagMessage buildRagMessage()
      Constructs the RAG (Retrieval-Augmented Generation) message by aggregating content from all enabled context providers.
      Returns:
      A fully populated RagMessage.
    • addMessage

      public void addMessage(AbstractMessage message)
      The definitive method for adding any message to the agi history.
      Parameters:
      message - The message to add.
    • identifyMessage

      private void identifyMessage(AbstractMessage msg)
      Assigns sequential IDs to the message and all its parts.
      Parameters:
      msg - The message to identify.
    • identifyPart

      public void identifyPart(AbstractPart part)
      Assigns a sequential ID to a single part if it doesn't already have one.
      Parameters:
      part - The part to identify.
    • addMessageInternal

      private void addMessageInternal(AbstractMessage message)
      Adds a message to the history without triggering hard pruning.
      Parameters:
      message - The message to add.
    • removeMessage

      public void removeMessage(AbstractMessage message)
      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

      public List<AbstractMessage> 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.