Class ToolContext

java.lang.Object
uno.anahata.asi.agi.tool.ToolContext
Direct Known Subclasses:
AnahataToolkit, OnTheFlyAgiTool, SwingToolContext, ToolContext.CapturedToolContext

public class ToolContext extends Object
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
  • Field Details

    • toolkit

      protected JavaObjectToolkit toolkit
      The toolkit instance that owns this context.
  • Constructor Details

    • ToolContext

      public ToolContext()
  • Method Details

    • getResponse

      public JavaMethodToolResponse 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

      protected JavaMethodToolResponse 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

      public String getModelId()
      The id of model that is executing the tool.
      Returns:
      The id of model that is executing the tool.
    • getModelMessage

      public AbstractModelMessage getModelMessage()
      The id of model that is executing the tool.
      Returns:
      The id of model that is executing the tool.
    • getToolContext

      public ToolContext getToolContext()
      Returns a thread-safe snapshot of this context. If a tool response is currently active on this thread, it returns a ToolContext.CapturedToolContext that 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

      public JavaMethodToolCall getCall()
      Convenience method to get the current tool call that initiated this execution.
      Returns:
      The current tool call.
    • getTool

      public JavaMethodTool getTool()
      Convenience method to get the metadata for the tool currently being executed.
      Returns:
      The current tool metadata.
    • getToolManager

      public ToolManager getToolManager()
      Convenience method to get the application's global ToolManager.
      Returns:
      The ToolManager instance.
    • getToolkit

      public <T> T getToolkit(Class<T> toolkitClass)
      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

      public Agi getAgi()
      Convenience method to get the parent agi container for the current execution.
      Returns:
      The parent Agi instance.
    • getAsiContainer

      public AbstractAsiContainer getAsiContainer()
      Gets the current ASI container hosting this Agi container is running.
      Returns:
      The container-scoped attributes map.
    • getResourceManager

      public ResourceManager getResourceManager()
      Convenience method to get the application's ResourceManager.
      Returns:
      The ResourceManager instance.
    • getExecutorService

      public ExecutorService getExecutorService()
      Convenience method to get the executor service for the current agi session.
      Returns:
      The session-scoped ExecutorService.
    • log

      public void log(String message)
      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

      public void error(String message)
      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

      public void error(Throwable t)
      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

      public void addAttachment(byte[] data, String mimeType)
      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

      public void addAttachment(File file) throws IOException
      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

      public void addAttachment(Path path) throws IOException
      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

      public Map getTurnMap()
      Gets a shared map for storing state across turns within the current agi session.
      Returns:
      The turn-scoped attributes map.
    • getSessionMap

      public Map getSessionMap()
      Gets a shared map for storing state across turns within the current agi session.
      Returns:
      The session-scoped attributes map.
    • getAsiContainerMap

      public Map getAsiContainerMap()
      Gets a map for sharing objects across all sessions within the current ASI container.
      Returns:
      The container-scoped attributes map.
    • getApplicationMap

      public 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

      public void runAsync(String taskName, Runnable task)
      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

      public Consumer<String> getThreadSafeLogger()
      Gets a thread-safe logging consumer that automatically propagates the current tool execution context whenever a message is logged.
      Returns:
      A context-aware logging consumer.