Class ToolManager

java.lang.Object
uno.anahata.ai.tools.ToolManager

public class ToolManager extends Object
Manages the registration, identification, and execution of local tools (functions).

This class uses reflection to scan provided classes for methods annotated with AIToolMethod. It converts these methods into FunctionDeclarations for the Gemini API and handles the invocation of these methods when the model requests a tool call.

It also manages tool call IDs, user confirmation workflows via FunctionPrompter, and asynchronous tool execution.

  • Constructor Details

    • ToolManager

      public ToolManager(Chat chat, FunctionPrompter prompter)
      Constructs a new ToolManager for the given Chat instance and prompter.
      Parameters:
      chat - The Chat instance.
      prompter - The prompter for user confirmation.
  • Method Details

    • resetIdCounter

      public void resetIdCounter(int value)
      Resets the tool call ID counter to a specific value.
      Parameters:
      value - The new starting value.
    • processFunctionCalls

      public FunctionProcessingResult processFunctionCalls(ChatMessage modelResponseMessage)
      Processes a list of function calls proposed by the model.

      This method assigns IDs to the calls, prompts the user for confirmation (if necessary), and executes the approved calls.

      Parameters:
      modelResponseMessage - The message from the model containing the function calls.
      Returns:
      A FunctionProcessingResult containing the outcomes and execution results.
    • getFunctionTool

      public com.google.genai.types.Tool getFunctionTool()
      Gets the Tool object containing all registered function declarations.
      Returns:
      The Tool object.
    • getFunctionInfos

      public List<FunctionInfo> getFunctionInfos()
      Gets a list of information about all registered functions.
      Returns:
      An unmodifiable list of FunctionInfo objects.
    • getToolConfig

      public com.google.genai.types.ToolConfig getToolConfig()
      Gets the tool configuration for API calls.
      Returns:
      The ToolConfig object.
    • getContextBehavior

      public ContextBehavior getContextBehavior(String toolName)
      Gets the context behavior (EPHEMERAL or STATEFUL_REPLACE) for a specific tool.
      Parameters:
      toolName - The name of the tool.
      Returns:
      The ContextBehavior.
    • getToolMethod

      public Method getToolMethod(String toolName)
      Gets the Java Method associated with a specific tool name.
      Parameters:
      toolName - The name of the tool.
      Returns:
      The Method object, or null if not found.
    • getAlwaysApproveFunctions

      public Set<String> getAlwaysApproveFunctions()
      Gets the set of tool names that are configured to be always approved.
      Returns:
      The set of always-approve tool names.
    • getNeverApproveFunctions

      public Set<String> getNeverApproveFunctions()
      Gets the set of tool names that are configured to be never approved.
      Returns:
      The set of never-approve tool names.