Class ResourceTracker

java.lang.Object
uno.anahata.ai.context.stateful.ResourceTracker

public class ResourceTracker extends Object
Tracks and manages stateful resources within the conversation context.

A stateful resource is typically a local file or system object that is loaded into the context via a tool call marked with ContextBehavior.STATEFUL_REPLACE.

This class is responsible for:

  • Identifying stateful resources in tool responses.
  • Monitoring the status of these resources on disk (VALID, STALE, DELETED, etc.).
  • Automatically pruning old versions of a resource when a new version is loaded.
  • Providing an overview of all stateful resources currently in the context.

  • Constructor Details

    • ResourceTracker

      public ResourceTracker(ContextManager contextManager)
      Constructs a new ResourceTracker for the given ContextManager.
      Parameters:
      contextManager - The ContextManager to track resources for.
  • Method Details

    • getResourceIdIfStateful

      public static Optional<String> getResourceIdIfStateful(com.google.genai.types.FunctionResponse fr, ToolManager toolManager)
      Checks a FunctionResponse to see if it's from a STATEFUL_REPLACE tool, and if so, extracts the resource ID.
      Parameters:
      fr - The FunctionResponse to check.
      toolManager - The ToolManager to query for tool behavior.
      Returns:
      An Optional containing the resource ID if the response is stateful, otherwise an empty Optional.
    • isStatefulToolCall

      public boolean isStatefulToolCall(String toolCallId)
      Checks if a specific tool call ID is associated with a stateful resource in the current context.
      Parameters:
      toolCallId - The tool call ID to check.
      Returns:
      true if the tool call produced a stateful resource.
    • isStatefulPart

      public boolean isStatefulPart(com.google.genai.types.Part part, ChatMessage message)
      Checks if a specific Part is associated with a stateful resource interaction. This includes both the FunctionCall and the FunctionResponse.
      Parameters:
      part - The part to check.
      message - The message the part belongs to.
      Returns:
      true if the part is part of a stateful resource interaction.
    • getResourceStatus

      public Optional<StatefulResourceStatus> getResourceStatus(com.google.genai.types.Part part, ChatMessage message)
      Gets the status of a stateful resource associated with a part. This method handles both FunctionCall and FunctionResponse parts.
      Parameters:
      part - The part to check.
      message - The message the part belongs to.
      Returns:
      An Optional containing the status if the part is stateful.
    • handleStatefulReplace

      public void handleStatefulReplace(ChatMessage newMessage, ToolManager toolManager)
      Scans a new message for stateful resources and prunes any older versions of the same resources from the conversation history.
      Parameters:
      newMessage - The message to scan.
      toolManager - The ToolManager to use for resource identification.
    • getStatefulResourcesOverview

      public List<StatefulResourceStatus> getStatefulResourcesOverview()
      Generates a list of statuses for all unique stateful resources currently in the context.
      Returns:
      A list of StatefulResourceStatus objects.
    • getResourceStatus

      public Optional<StatefulResourceStatus> getResourceStatus(com.google.genai.types.FunctionResponse fr)
      Gets the status of a single resource directly from its FunctionResponse.
      Parameters:
      fr - The FunctionResponse that created the stateful resource.
      Returns:
      An Optional containing the status, or empty if the response is not a valid stateful resource.
    • pruneStatefulResources

      public void pruneStatefulResources(List<String> resourceIds, String reason)
      Prunes all messages and parts associated with a list of stateful resource IDs.
      Parameters:
      resourceIds - The list of resource IDs (e.g., file paths) to prune.
      reason - The reason for pruning.