Class AbstractTool<P extends AbstractToolParameter, C extends AbstractToolCall>

java.lang.Object
uno.anahata.asi.agi.event.BasicPropertyChangeSource
uno.anahata.asi.agi.tool.spi.AbstractTool<P,C>
Type Parameters:
P - The specific subclass of AbstractToolParameter this tool uses.
C - The specific subclass of AbstractToolCall this tool creates.
All Implemented Interfaces:
PropertyChangeSource, Rebindable
Direct Known Subclasses:
BadTool, JavaMethodTool

public abstract class AbstractTool<P extends AbstractToolParameter, C extends AbstractToolCall> extends BasicPropertyChangeSource
The abstract base class for a tool, now generic on its Parameter and Call types.
Author:
anahata-gemini-pro-2.5
  • Field Details

    • tokenCount

      private Integer tokenCount
    • name

      @NonNull protected final @NonNull String name
      The fully qualified name of the tool, e.g., "LocalFiles.readFile". This is immutable.
    • description

      protected String description
      A detailed description of what the tool does.
    • toolkit

      protected AbstractToolkit toolkit
      A reference to the parent toolkit that owns this tool. Can be null for standalone tools.
    • permission

      protected ToolPermission permission
      The user's configured preference for this tool, determining its execution behavior.
    • maxDepth

      private int maxDepth
      The maximum depth this tool call should be retained in the context.
    • parameters

      private final List<P extends AbstractToolParameter> parameters
      A rich, ordered list of the tool's parameters.
    • responseJsonSchema

      protected String responseJsonSchema
      A pre-generated, language-agnostic JSON schema for the tool's return type. Can be null for void methods.
  • Constructor Details

    • AbstractTool

      protected AbstractTool(@NonNull @NonNull String name)
      Constructs a new AbstractTool with the given name.
      Parameters:
      name - The tool's name.
  • Method Details

    • setPermission

      public void setPermission(ToolPermission permission)
      Sets the user's execution permission preference for this tool and fires a property change event.
      Parameters:
      permission - The new permission level to apply.
    • getEffectiveMaxDepth

      public int getEffectiveMaxDepth()
      The effective maximum depth.
      Returns:
      the effective max depth.
    • getSelectedModel

      public AbstractModel getSelectedModel()
      Convenience method to resolve the currently selected model from the parent session.
      Returns:
      The active AbstractModel, or null if no session is active or no model is selected.
    • createCall

      public abstract C createCall(AbstractModelMessage message, String id, Map<String,Object> args)
      Factory method to create a tool-specific call object from raw model data.
      Parameters:
      message - the model message the call will belong to.
      id - The call ID.
      args - The raw arguments from the model.
      Returns:
      A new tool call instance.
    • getResponseType

      public abstract Type getResponseType()
      Template method hook for subclasses to provide their specific Response type.
      Returns:
      The reflection Type of the corresponding AbstractToolResponse subclass.
    • getTokenCount

      public int getTokenCount()
      Calculates the total token count of this tool on-the-fly using the active model.

      This method prioritizes counting the exact, provider-specific JSON declaration payload emitted by AbstractModel.getToolDeclarationJson(AbstractTool, RequestConfig) to ensure 100% billing-identical token metrics in the context window. It falls back to a naive summation of descriptions and schemas if the tool is not yet bound to an active session context.

      This value is cached to prevent redundant calculations during Context Window transitions.

      Returns:
      The precise token count representing the tool's declaration overhead.
    • resetTokenCount

      public void resetTokenCount()
      Resets the cached token count, forcing a recalculation on the next query.