Class ToolManager
java.lang.Object
uno.anahata.asi.agi.event.BasicPropertyChangeSource
uno.anahata.asi.agi.tool.ToolManager
- All Implemented Interfaces:
ContextProvider, PropertyChangeSource, Rebindable
Manages the lifecycle of all AI tools, including registration, configuration,
and lookup. It stores toolkits and generates the full list of tools on
demand.
This class also implements ContextProvider, acting as the root
provider for all toolkits and injecting metadata about available tools into
the AI's context.
- Author:
- anahata-gemini-pro-2.5
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final AgiThe parent agi session.private List<AbstractToolCall<?, ?>> A thread-safe list of currently executing tool calls.private booleanWhether this manager is currently providing context augmentation.A session-scoped map for tools to store state across turns.private final Map<String, AbstractToolkit<?>> A map of registered toolkits, keyed by their simple name.private booleanFlag to control whether the tool response schemas are wrapped in the standard JavaMethodToolResponse structure.Fields inherited from class BasicPropertyChangeSource
propertyChangeSupport -
Constructor Summary
ConstructorsConstructorDescriptionToolManager(@NonNull Agi agi) Primary constructor for use in a live agi session. -
Method Summary
Modifier and TypeMethodDescriptionprivate voidApplies application-wide tool preferences to all registered tools.createToolCall(AbstractModelMessage amm, String id, String name, Map<String, Object> jsonArgs) The primary factory method for creating a model-agnostic tool call from provider-specific data.Optional<? extends AbstractTool> findToolByName(String name) Finds a tool by its fully qualified name.Returns all context providers from enabled toolkits.Gets a list of all fully qualified tool names registered in this manager.List<? extends AbstractTool> Gets a dynamically aggregated list of all tools from all registered toolkits.Gets the list of immediate child context providers.Gets a detailed description of what this context provider does.Returns all context providers from enabled toolkits that are currently disabled.List<AbstractToolkit<?>> Gets a list of all toolkits that are currently disabled.List<AbstractToolkit<?>> Gets a list of all toolkits that are currently enabled.List<? extends AbstractTool> Gets a list of all tools that are currently enabled and allowed to be sent to the model.List<AbstractToolCall<?, ?>> Gets an unmodifiable view of the currently executing tool calls.getId()Gets the unique identifier for this context provider.getName()Gets the human-readable name of this context provider.Gets a list of system instruction strings provided by this context provider.<T> Optional<T> getToolkitInstance(Class<T> toolkitClass) Retrieves the singleton instance of a registered toolkit class.booleanChecks if this context provider is currently active and providing context.voidpopulateMessage(RagMessage ragMessage) Populates the givenRagMessagewith dynamic, just-in-time context parts.voidPropagates the post-activation signal to all registered toolkits.voidrebind()Re-initializes the transientPropertyChangeSupportafter deserialization.final voidregisterClasses(Class<?>... classes) Scans the given classes for methods annotated withAgiTool, creates the corresponding toolkits, and applies any application-wide preferences.voidregisterExecutingCall(AbstractToolCall<?, ?> call) Registers a tool call as currently executing and fires a property change event.voidreset()Resets the static tool call ID counter to zero.voidResets the cached token counts on all registered and enabled tools within this manager, forcing a lazy recalculation on the next query.voidunregisterExecutingCall(AbstractToolCall<?, ?> call) Unregisters a tool call from the executing list and fires a property change event.voidupdateToolkits(boolean enabled, List<String> toolkitNames) Enables or disables multiple toolkits by their names (IDs).Methods inherited from class BasicPropertyChangeSource
addPropertyChangeListener, addPropertyChangeListener, removePropertyChangeListener, removePropertyChangeListenerMethods 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, getParentProvider, getRagTokenCount, isEffectivelyProviding, setParentProvider, setProvidingMethods inherited from interface PropertyChangeSource
getPropertyChangeSupport
-
Field Details
-
providing
private boolean providingWhether this manager is currently providing context augmentation. -
wrapResponseSchemas
private boolean wrapResponseSchemasFlag to control whether the tool response schemas are wrapped in the standard JavaMethodToolResponse structure. If false, only the raw result type is used in the tool definition, and the wrapper is explained globally. -
agi
The parent agi session. -
toolkits
A map of registered toolkits, keyed by their simple name. -
executingCalls
A thread-safe list of currently executing tool calls. -
sessionAttributes
-
-
Constructor Details
-
ToolManager
Primary constructor for use in a live agi session. This constructor is self-initializing, registering all tools defined in the AgiConfig.- Parameters:
agi- The parent agi orchestrator.
-
-
Method Details
-
reset
public void reset()Resets the static tool call ID counter to zero. -
resetTokenCounts
public void resetTokenCounts()Resets the cached token counts on all registered and enabled tools within this manager, forcing a lazy recalculation on the next query. -
registerClasses
Scans the given classes for methods annotated withAgiTool, creates the corresponding toolkits, and applies any application-wide preferences.Implementation details: Uses a two-pass registration flow. The first pass instantiates and registers all toolkits in the map. The second pass triggers
initialize()on each toolkit. This eliminates race conditions where one toolkit needs to look up another during setup.- Parameters:
classes- The classes to scan for tools.
-
createToolCall
public AbstractToolCall createToolCall(AbstractModelMessage amm, String id, String name, Map<String, Object> jsonArgs) The primary factory method for creating a model-agnostic tool call from provider-specific data. This method orchestrates the creation and pre-rejection logic.- Parameters:
amm- The model message that initiated the call.id- The unique ID of the tool call (can be null).name- The name of the tool to call.jsonArgs- The raw arguments from the model.- Returns:
- An
AbstractToolCallwith its corresponding, possibly pre-rejected, response.
-
findToolByName
Finds a tool by its fully qualified name.- Parameters:
name- The tool name.- Returns:
- An Optional containing the tool if found.
-
getAllToolNames
-
getEnabledToolkits
Gets a list of all toolkits that are currently enabled.- Returns:
- The list of enabled toolkits.
-
getDisabledToolkits
Gets a list of all toolkits that are currently disabled.- Returns:
- The list of disabled toolkits.
-
getAllTools
Gets a dynamically aggregated list of all tools from all registered toolkits. This is a view and is generated on each call.- Returns:
- A list of all tools.
-
getEnabledTools
Gets a list of all tools that are currently enabled and allowed to be sent to the model.- Returns:
- A filtered list of enabled tools.
-
applyPreferences
private void applyPreferences()Applies application-wide tool preferences to all registered tools. -
getAllContextProviders
Returns all context providers from enabled toolkits.- Returns:
- A list of context providers.
-
getDisabledContextProviders
Returns all context providers from enabled toolkits that are currently disabled.- Returns:
- A list of disabled context providers.
-
registerExecutingCall
Registers a tool call as currently executing and fires a property change event.- Parameters:
call- The tool call to register.
-
unregisterExecutingCall
Unregisters a tool call from the executing list and fires a property change event.- Parameters:
call- The tool call to unregister.
-
getExecutingCalls
Gets an unmodifiable view of the currently executing tool calls.- Returns:
- The list of executing calls.
-
getToolkitInstance
-
updateToolkits
-
rebind
public void rebind()Description copied from class:BasicPropertyChangeSourceRe-initializes the transientPropertyChangeSupportafter deserialization.- Specified by:
rebindin interfaceRebindable- Overrides:
rebindin classBasicPropertyChangeSource
-
getId
Gets the unique identifier for this context provider.- Specified by:
getIdin interfaceContextProvider- Returns:
- The provider's ID.
-
getName
Gets the human-readable name of this context provider.- Specified by:
getNamein interfaceContextProvider- Returns:
- The provider's name.
-
getDescription
Gets a detailed description of what this context provider does.- Specified by:
getDescriptionin interfaceContextProvider- Returns:
- The provider's description.
-
isProviding
public boolean isProviding()Checks if this context provider is currently active and providing context.- Specified by:
isProvidingin interfaceContextProvider- Returns:
trueif providing,falseotherwise.
-
getSystemInstructions
Gets a list of system instruction strings provided by this context provider. These are typically prepended to the conversation as high-level guidance.- Specified by:
getSystemInstructionsin interfaceContextProvider- Returns:
- A list of system instruction strings.
- Throws:
Exception- if an error occurs during instruction generation.
-
populateMessage
Populates the givenRagMessagewith dynamic, just-in-time context parts. These parts are appended to the end of the user's prompt (RAG).- Specified by:
populateMessagein interfaceContextProvider- Parameters:
ragMessage- The message to be augmented with context.- Throws:
Exception- if an error occurs during context generation.
-
getChildrenProviders
Gets the list of immediate child context providers.- Specified by:
getChildrenProvidersin interfaceContextProvider- Returns:
- The list of children, or an empty list if none.
-
postActivate
public void postActivate()Propagates the post-activation signal to all registered toolkits.
-

