Package io.jmix.aitools.tool.impl
Class AiToolRegistryImpl
java.lang.Object
io.jmix.aitools.tool.impl.AiToolRegistryImpl
- All Implemented Interfaces:
AiToolRegistry,org.springframework.beans.factory.InitializingBean
@Component("aitls_AiToolRegistryImpl")
public class AiToolRegistryImpl
extends Object
implements AiToolRegistry, org.springframework.beans.factory.InitializingBean
Default
AiToolRegistry. On startup:
-
it scans all
JmixAiToolbeans for@Tool-annotated methods -
resolves
@ToolOverrideconflicts -
publishes an immutable set of
ResolvedAiTools.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static final recordImmutable consistent registry state.protected static final recordInternal candidate gathered during registry build, before override resolution. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected AiToolDescriptorProviderprotected List<JmixAiTool> protected AiToolRegistryImpl.ResolvedAiTools -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidprotected ResolvedAiToolbuildResolvedTool(String registryKey, AiToolRegistryImpl.ToolCandidate candidate, Set<Class<? extends JmixAiTool>> markers) Builds the resolved tool entry.protected voidcollectCandidates(JmixAiTool tool, Map<String, List<AiToolRegistryImpl.ToolCandidate>> regulars, Map<String, List<AiToolRegistryImpl.ToolCandidate>> overrideBuckets) protected Set<Class<? extends JmixAiTool>> collectMarkers(JmixAiTool tool) protected voidcollectMarkersRecursive(@Nullable Class<?> type, Set<Class<? extends JmixAiTool>> acc) protected List<JmixAiTool> deduplicate(List<JmixAiTool> tools) Removes references to the same bean instance appearing multiple times in the autowired list (this can happen when a single bean is exposed via severalJmixAiToolsub-interfaces and Spring resolves the injection through each of them).findByMarker(Class<? extends JmixAiTool> marker) Returns all resolved tools produced by beans implementing the given marker sub-interface ofJmixAiTool(e.g.findByName(String name) Returns a resolved tool by its name (the value passed to@Tool(name=...)or derived from the method name), or empty when no such tool is registered.getAll()Returns all resolved tools in registration order.List<org.springframework.ai.tool.ToolCallback> Shortcut returningToolCallbacks of all resolved tools in the same order asAiToolRegistry.getAll().protected Class<?> getTargetClass(JmixAiTool tool) protected booleanisToolAnnotatedMethod(Method method) protected @Nullable StringreadOverrideValue(Method method) protected voidrefresh()protected AiToolRegistryImpl.ToolCandidateresolveWinner(String name, List<AiToolRegistryImpl.ToolCandidate> candidates) protected Set<Class<? extends JmixAiTool>> unionMarkers(List<AiToolRegistryImpl.ToolCandidate> candidates) Union of marker sub-interfaces from every candidate in the bucket.
-
Field Details
-
aiTools
-
aiToolDescriptorProvider
-
resolvedAiTools
-
-
Constructor Details
-
AiToolRegistryImpl
public AiToolRegistryImpl()
-
-
Method Details
-
afterPropertiesSet
public void afterPropertiesSet()- Specified by:
afterPropertiesSetin interfaceorg.springframework.beans.factory.InitializingBean
-
refresh
protected void refresh() -
getAll
Description copied from interface:AiToolRegistryReturns all resolved tools in registration order.- Specified by:
getAllin interfaceAiToolRegistry- Returns:
- all resolved tools
-
findByName
Description copied from interface:AiToolRegistryReturns a resolved tool by its name (the value passed to@Tool(name=...)or derived from the method name), or empty when no such tool is registered.- Specified by:
findByNamein interfaceAiToolRegistry- Parameters:
name- tool name to look up- Returns:
- the matching tool, or empty when none is registered under that name
-
findByMarker
Description copied from interface:AiToolRegistryReturns all resolved tools produced by beans implementing the given marker sub-interface ofJmixAiTool(e.g.DataLoadAiTool.class).- Specified by:
findByMarkerin interfaceAiToolRegistry- Parameters:
marker- marker sub-interface to filter by- Returns:
- resolved tools whose source bean implements the marker
-
getAllCallbacks
Description copied from interface:AiToolRegistryShortcut returningToolCallbacks of all resolved tools in the same order asAiToolRegistry.getAll().- Specified by:
getAllCallbacksin interfaceAiToolRegistry- Returns:
- callbacks of all resolved tools
-
deduplicate
Removes references to the same bean instance appearing multiple times in the autowired list (this can happen when a single bean is exposed via severalJmixAiToolsub-interfaces and Spring resolves the injection through each of them). Deduplication is based on object identity, so multiple distinct beans of the same class are preserved.- Parameters:
tools- autowired tool beans, possibly containing the same instance more than once- Returns:
- tools with duplicate instances removed, preserving first-seen order
-
collectCandidates
protected void collectCandidates(JmixAiTool tool, Map<String, List<AiToolRegistryImpl.ToolCandidate>> regulars, Map<String, List<AiToolRegistryImpl.ToolCandidate>> overrideBuckets) -
resolveWinner
protected AiToolRegistryImpl.ToolCandidate resolveWinner(String name, List<AiToolRegistryImpl.ToolCandidate> candidates) -
unionMarkers
protected Set<Class<? extends JmixAiTool>> unionMarkers(List<AiToolRegistryImpl.ToolCandidate> candidates) Union of marker sub-interfaces from every candidate in the bucket. Ensures that an override does not silently drop a tool from amarker-specificview that the original implementation participated in.- Parameters:
candidates- all candidates grouped under the same tool name- Returns:
- union of marker sub-interfaces across the candidates' source beans
-
buildResolvedTool
protected ResolvedAiTool buildResolvedTool(String registryKey, AiToolRegistryImpl.ToolCandidate candidate, Set<Class<? extends JmixAiTool>> markers) Builds the resolved tool entry. TheregistryKeyis the bucket name under which all candidates were grouped: it is the override target when the winner is an override resolved against an existing original, or the winner's own@Toolname otherwise. Either way, this is the name the LLM will see, so we use it both as theregistry nameand as the underlyingToolDefinitionname.The
metadata(description and other attributes) comes from the winner's own@Toolannotation viaAiToolDescriptor, which means an override author can change the description while keeping the tool name stable.- Parameters:
registryKey- the name under which the tool is registered and exposed to the LLMcandidate- the winning candidate the entry is built frommarkers- marker sub-interfaces this tool participates in- Returns:
- the resolved tool entry
-
collectMarkers
-
collectMarkersRecursive
protected void collectMarkersRecursive(@Nullable Class<?> type, Set<Class<? extends JmixAiTool>> acc) -
isToolAnnotatedMethod
-
readOverrideValue
-
getTargetClass
-