Class Session
- All Implemented Interfaces:
ContextProvider, PropertyChangeSource, Rebindable
This toolkit provides high-level control over the session environment, allowing the model to optimize its own context window, toggle available toolkits, and manage the lifecycle of stateful resources.
- Author:
- anahata-ai
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate DateThe last time the session got deserialized.private DateThe time when the session started.Fields inherited from class AnahataToolkit
childrenProviders, propertyChangeSupportModifier and TypeFieldDescriptionprotected List<ContextProvider> The list of child context providers managed by this toolkit.protected PropertyChangeSupportSupport for firing property change events.Fields inherited from class ToolContext
toolkitModifier and TypeFieldDescriptionprotected JavaObjectToolkitThe toolkit instance that owns this context. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionSwitches the session to server-side tool mode.Gets a list of system instruction strings provided by this context provider.killRunningTools(List<Long> toolCallIds) Signals one or more currently executing background tools to stop.voidpopulateMessage(RagMessage ragMessage) Populates the RAG message with comprehensive session metadata.voidCaptures the sessionRestored time during deserialization;voidsetContextProviderProviding(boolean providing, List<String> providerIds) Enables or disables specific context providers within the session hierarchy.voidsetToolkitEnabled(boolean enabled, List<String> toolkitNames) Enables or disables multiple toolkits by their names.updateSessionNickname(String nickName) Updates the AGIs nickname.updateSessionSummary(String summary) Updates the human-readable summary of the current session.Methods inherited from class AnahataToolkit
getChildrenProviders, getDescription, getId, getName, getParentProvider, getPropertyChangeSupport, initialize, isEffectivelyProviding, rebindModifier and TypeMethodDescriptionGets the list of immediate child context providers.Gets a detailed description of what this context provider does.getId()Gets the unique identifier for this context provider.getName()Gets the human-readable name of this context provider.Gets the parent context provider in the hierarchy, if any.Gets the PropertyChangeSupport instance for this object.voidPerforms initial setup for a newly created toolkit instance.booleanOverride to also take into consideration theAbstractToolkit.enabledvoidrebind()Rebinds the toolkit after deserialization.Methods inherited from class ToolContext
addAttachment, addAttachment, addAttachment, error, getAgi, getApplicationMap, getAsiContainer, getAsiContainerMap, getCall, getExecutorService, getModelId, getModelMessage, getResourceManager, getResponse, getSessionMap, getThreadSafeLogger, getTool, getToolContext, getToolkit, getToolManager, getTurnMap, isToolExecution, log, peekResponse, runAsyncModifier and TypeMethodDescriptionvoidaddAttachment(byte[] data, String mimeType) Attaches a binary blob to the current tool's response.voidaddAttachment(File file) Convenience method to attach a file to the current tool's response.voidaddAttachment(Path path) Convenience method to attach a file from a Path to the current tool's response.voidAdds an error message to the current tool's response.getAgi()Convenience method to get the parent agi container for the current execution.Gets a static, application-scoped map for sharing objects across all containers in the current JVM.Gets the current ASI container hosting this Agi container is running.Gets a map for sharing objects across all sessions within the current ASI container.getCall()Convenience method to get the current tool call that initiated this execution.Convenience method to get the executor service for the current agi session.The id of model that is executing the tool.The id of model that is executing the tool.Convenience method to get the application's ResourceManager.Gets the current tool response from the thread-local context.Gets a shared map for storing state across turns within the current agi session.Gets a thread-safe logging consumer that automatically propagates the current tool execution context whenever a message is logged.getTool()Convenience method to get the metadata for the tool currently being executed.Returns a thread-safe snapshot of this context.<T> TgetToolkit(Class<T> toolkitClass) Retrieves the singleton instance of a registered toolkit class.Convenience method to get the application's global ToolManager.Gets a shared map for storing state across turns within the current agi session.booleanChecks if the current thread or this context instance is associated with an active tool execution.voidAdds a standard log message to the current tool's response.protected JavaMethodToolResponseInternal helper to retrieve the response if it exists, without throwing an exception.voidExecutes a task asynchronously in the session's executor service, automatically propagating the current tool execution context to the background thread.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface ContextProvider
getAgi, getContextPosition, getFlattenedHierarchy, getFullyQualifiedId, getHeader, getIconId, getInstructionsTokenCount, getRagTokenCount, isProviding, setParentProvider, setProvidingModifier and TypeMethodDescriptiongetAgi()Gets the parent AGI session this context provider is registered under.default ContextPositionGets the designated position of this provider's content in the AI prompt.default List<ContextProvider> getFlattenedHierarchy(boolean providingOnly) Gets a flattened list of this provider and all its descendants in the hierarchy.default StringGets the fully qualified ID of this provider, reflecting its position in the hierarchy.default StringGenerates a machine-readable header string for this context provider, used to identify the source of injected context in the final prompt.default StringGets an optional icon identifier for this provider.default intCalculates the token count for the system instructions provided by this instance using the active model.default intCalculates the token count for the RAG content provided by this instance using the active model.booleanChecks if this context provider is currently active and providing context.default voidsetParentProvider(ContextProvider parent) Sets the parent context provider for this instance.default voidsetProviding(boolean enabled) Sets whether this context provider is enabled.
-
Field Details
-
sessionStart
The time when the session started. -
sessionRestored
The last time the session got deserialized.
-
-
Constructor Details
-
Session
public Session()
-
-
Method Details
-
postActivate
public void postActivate()Captures the sessionRestored time during deserialization;- Overrides:
postActivatein classAnahataToolkit
-
getSystemInstructions
Description copied from interface:ContextProviderGets a list of system instruction strings provided by this context provider. These are typically prepended to the conversation as high-level guidance.- Returns:
- A list of system instruction strings.
- Throws:
Exception- if an error occurs during instruction generation.
-
updateSessionNickname
-
updateSessionSummary
Updates the human-readable summary of the current session.STRICT USAGE RULE: This tool MUST ONLY be called if there are other "task-related" tool calls (e.g., file manipulation, shell commands, pruning) being made in the same turn. It should NEVER be called as the sole tool in a turn.
The summary is visible to the user in the container dashboard and the parent agi if spawned by another agi.
- Parameters:
summary- A concise summary of the conversation's current state or topic.- Returns:
- A confirmation message.
-
setContextProviderProviding
Enables or disables specific context providers within the session hierarchy.This tool allows the model to surgically control which providers contribute to the RAG message, helping to reduce noise and optimize token usage.
- Parameters:
providing- Whether to enable or disable the providers.providerIds- The fully qualified IDs of the context providers to update.
-
setToolkitEnabled
Enables or disables multiple toolkits by their names.Disabling a toolkit makes its tools invisible to the model and can reduce complexity when certain capabilities are no longer needed.
- Parameters:
enabled- Whether to enable or disable the toolkits.toolkitNames- The names (IDs) of the toolkits to update (e.g., 'Audio', 'Chrome').
-
killRunningTools
Signals one or more currently executing background tools to stop.This is useful for cancelling long-running tasks like large file reads, complex shell commands, or audio recording.
- Parameters:
toolCallIds- The unique IDs of the tool calls to stop.- Returns:
- A detailed report of the stopping operations.
-
enableHostedTools
Switches the session to server-side tool mode.CRITICAL: After calling this, local Java tools are disabled until manually re-enabled by the user. Use this only when a capability provided by the model host (e.g., Google Search) is explicitly required.
- Returns:
- Confirmation message describing the impact.
-
populateMessage
Populates the RAG message with comprehensive session metadata.Provides a Markdown summary of session identity, model configuration, context window usage, and the status of executing tools.
- Parameters:
ragMessage- The target RAG message.- Throws:
Exception- if metadata extraction fails.
-

