Class ToolContext
java.lang.Object
uno.anahata.asi.agi.tool.ToolContext
- Direct Known Subclasses:
AnahataToolkit, OnTheFlyAgiTool, SwingToolContext, ToolContext.CapturedToolContext
The base class for Java code generated by the AI model or for toolkit
implementations. Extending this class provides convenient, thread-local
access to the current execution context, including logging, attachments, and
the parent agi session.
- Author:
- anahata-gemini-pro-2.5
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classA thread-safe, immutable snapshot of a tool's execution context. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected JavaObjectToolkitThe toolkit instance that owns this context. -
Constructor Summary
Constructors -
Method Summary
Modifier 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.voidAdds an exceptions stack trace 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.
-
Field Details
-
toolkit
The toolkit instance that owns this context.
-
-
Constructor Details
-
ToolContext
public ToolContext()
-
-
Method Details
-
getResponse
Gets the current tool response from the thread-local context.- Returns:
- The current response.
- Throws:
IllegalStateException- if called outside the scope of a tool execution.
-
peekResponse
Internal helper to retrieve the response if it exists, without throwing an exception. Supports both ThreadLocal and captured contexts.- Returns:
- The current response, or null.
-
isToolExecution
public boolean isToolExecution()Checks if the current thread or this context instance is associated with an active tool execution.- Returns:
- true if a tool is executing.
-
getModelId
The id of model that is executing the tool.- Returns:
- The id of model that is executing the tool.
-
getModelMessage
The id of model that is executing the tool.- Returns:
- The id of model that is executing the tool.
-
getToolContext
Returns a thread-safe snapshot of this context. If a tool response is currently active on this thread, it returns aToolContext.CapturedToolContextthat holds a fixed reference to that response, allowing it to be used in subthreads, the EDT, or classloaders.- Returns:
- A thread-safe ToolContext instance.
-
getCall
Convenience method to get the current tool call that initiated this execution.- Returns:
- The current tool call.
-
getTool
Convenience method to get the metadata for the tool currently being executed.- Returns:
- The current tool metadata.
-
getToolManager
Convenience method to get the application's global ToolManager.- Returns:
- The ToolManager instance.
-
getToolkit
Retrieves the singleton instance of a registered toolkit class.- Type Parameters:
T- The type of the toolkit.- Parameters:
toolkitClass- The class of the toolkit to find.- Returns:
- The toolkit instance.
- Throws:
IllegalArgumentException- if the toolkit is not found.
-
getAgi
Convenience method to get the parent agi container for the current execution.- Returns:
- The parent Agi instance.
-
getAsiContainer
Gets the current ASI container hosting this Agi container is running.- Returns:
- The container-scoped attributes map.
-
getResourceManager
Convenience method to get the application's ResourceManager.- Returns:
- The ResourceManager instance.
-
getExecutorService
Convenience method to get the executor service for the current agi session.- Returns:
- The session-scoped ExecutorService.
-
log
Adds a standard log message to the current tool's response. If called outside a tool execution thread, it logs to the SLF4J logger.- Parameters:
message- The log message to add.
-
error
Adds an error message to the current tool's response. If called outside a tool execution thread, it logs to the SLF4J logger.- Parameters:
message- The error message to add.
-
error
Adds an exceptions stack trace to the current tool's response. If called outside a tool execution thread, it logs to the SLF4J logger.- Parameters:
t- The error message to add.
-
addAttachment
Attaches a binary blob to the current tool's response.- Parameters:
data- The binary data to attach.mimeType- The MIME type of the data (e.g., 'image/png').
-
addAttachment
Convenience method to attach a file to the current tool's response. The MIME type is detected automatically using Tika.- Parameters:
file- The file to attach.- Throws:
IOException- if the file cannot be read or the MIME type detection fails.
-
addAttachment
Convenience method to attach a file from a Path to the current tool's response. The MIME type is detected automatically using Tika.- Parameters:
path- The path to the file to attach.- Throws:
IOException- if the file cannot be read or the MIME type detection fails.
-
getTurnMap
Gets a shared map for storing state across turns within the current agi session.- Returns:
- The turn-scoped attributes map.
-
getSessionMap
Gets a shared map for storing state across turns within the current agi session.- Returns:
- The session-scoped attributes map.
-
getAsiContainerMap
Gets a map for sharing objects across all sessions within the current ASI container.- Returns:
- The container-scoped attributes map.
-
getApplicationMap
Gets a static, application-scoped map for sharing objects across all containers in the current JVM.- Returns:
- The application-scoped attributes map.
-
runAsync
Executes a task asynchronously in the session's executor service, automatically propagating the current tool execution context to the background thread.- Parameters:
taskName- A descriptive name for the task (used for thread naming).task- The task to execute.
-
getThreadSafeLogger
-

