Class ContextWindow

java.lang.Object
uno.anahata.ai.tools.spi.ContextWindow

public class ContextWindow extends Object
A set of tools for inspecting and managing the AI's conversation context window.

These tools allow the model to monitor its own token usage, prune redundant information, and toggle dynamic context providers.

  • Constructor Details

    • ContextWindow

      public ContextWindow()
  • Method Details

    • getContextSummary

      public static String getContextSummary()
      Generates a detailed Markdown summary of all messages and parts in the context.
      Returns:
      The context summary string.
    • pruneOther

      public static String pruneOther(List<String> partIds, String reason) throws Exception
      Prunes specific non-tool parts from the context.
      Parameters:
      partIds - A list of part identifiers in 'messageId/partIndex' format.
      reason - The rationale for removal.
      Returns:
      A status message indicating the result of the pruning.
      Throws:
      Exception - if pruning fails.
    • pruneEphemeralToolCalls

      public static String pruneEphemeralToolCalls(List<String> toolCallIds, String reason) throws Exception
      Prunes ephemeral tool calls and their responses by ID.
      Parameters:
      toolCallIds - A list of tool call IDs.
      reason - The rationale for removal.
      Returns:
      A success message.
      Throws:
      Exception - if pruning fails.
    • pruneStatefulResources

      public static String pruneStatefulResources(List<String> resourceIds, String reason) throws Exception
      Prunes all parts associated with specific stateful resources.
      Parameters:
      resourceIds - A list of resource IDs (full paths).
      reason - The rationale for removal.
      Returns:
      A success message.
      Throws:
      Exception - if pruning fails.
    • getTokenCount

      public static int getTokenCount() throws Exception
      Gets the current total token count in the context window.
      Returns:
      The token count.
      Throws:
      Exception - if the count cannot be retrieved.
    • getTokenThreshold

      public static int getTokenThreshold()
      Gets the current token threshold.
      Returns:
      The token threshold.
    • setTokenThreshold

      public static String setTokenThreshold(int newThreshold)
      Sets a new token threshold.
      Parameters:
      newThreshold - The new threshold value.
      Returns:
      A success message.
    • toggleContextProviders

      public static String toggleContextProviders(List<String> contextProviderIds, boolean enabled)
      Enables or disables specific context providers.
      Parameters:
      contextProviderIds - A list of provider IDs.
      enabled - The desired state.
      Returns:
      A feedback message listing the updated providers.