Class ResourceTracker
java.lang.Object
uno.anahata.ai.context.stateful.ResourceTracker
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 Summary
ConstructorsConstructorDescriptionResourceTracker(ContextManager contextManager) Constructs a new ResourceTracker for the given ContextManager. -
Method Summary
Modifier and TypeMethodDescriptiongetResourceIdIfStateful(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.getResourceStatus(com.google.genai.types.FunctionResponse fr) Gets the status of a single resource directly from its FunctionResponse.getResourceStatus(com.google.genai.types.Part part, ChatMessage message) Gets the status of a stateful resource associated with a part.Generates a list of statuses for all unique stateful resources currently in the context.voidhandleStatefulReplace(ChatMessage newMessage, ToolManager toolManager) Scans a new message for stateful resources and prunes any older versions of the same resources from the conversation history.booleanisStatefulPart(com.google.genai.types.Part part, ChatMessage message) Checks if a specific Part is associated with a stateful resource interaction.booleanisStatefulToolCall(String toolCallId) Checks if a specific tool call ID is associated with a stateful resource in the current context.voidpruneStatefulResources(List<String> resourceIds, String reason) Prunes all messages and parts associated with a list of stateful resource IDs.
-
Constructor Details
-
ResourceTracker
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
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
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
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
Generates a list of statuses for all unique stateful resources currently in the context.- Returns:
- A list of
StatefulResourceStatusobjects.
-
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
-
