11package org .springframework .ai .tool .autoconfigure ;
22
3-
3+ import org .springframework .ai .tool .ToolCallbackProvider ;
4+ import org .springframework .ai .tool .annotation .Tool ;
45import org .springframework .ai .tool .autoconfigure .annotation .EnableToolCallbackAutoRegistration ;
56import org .springframework .beans .factory .config .BeanDefinition ;
67import org .springframework .beans .factory .config .ConstructorArgumentValues ;
78import org .springframework .beans .factory .support .BeanDefinitionRegistry ;
89import org .springframework .beans .factory .support .BeanNameGenerator ;
910import org .springframework .beans .factory .support .GenericBeanDefinition ;
11+ import org .springframework .boot .autoconfigure .condition .ConditionalOnClass ;
1012import org .springframework .context .annotation .ImportBeanDefinitionRegistrar ;
1113import org .springframework .core .type .AnnotationMetadata ;
1214import org .springframework .lang .NonNull ;
1618import java .util .Set ;
1719
1820/**
19- * {@link ImportBeanDefinitionRegistrar} implementation that registers a {@link ToolCallbackBeanRegistrar}
20- * bean based on the metadata from {@link EnableToolCallbackAutoRegistration}.
21+ * {@link ImportBeanDefinitionRegistrar} implementation that registers a
22+ * {@link ToolAnnotatedBeanProcessor} bean based on the metadata from
23+ * {@link EnableToolCallbackAutoRegistration}.
2124 *
22- * <p>This registrar extracts package scanning information from the annotation attributes
23- * and registers a {@link ToolCallbackBeanRegistrar} to process beans containing {@code @Tool}-annotated methods.
25+ * <p>
26+ * This registrar extracts package scanning information from the annotation attributes and
27+ * registers a {@link ToolAnnotatedBeanProcessor} to process beans containing
28+ * {@code @Tool}-annotated methods.
2429 *
2530 * @see EnableToolCallbackAutoRegistration
26- * @see ToolCallbackBeanRegistrar
31+ * @see ToolAnnotatedBeanProcessor
2732 */
28- public class AutoToolCallbacksRegistrar implements ImportBeanDefinitionRegistrar {
33+ @ ConditionalOnClass ({ Tool .class , ToolCallbackProvider .class })
34+ public class ToolCallbackAutoRegistrar implements ImportBeanDefinitionRegistrar {
2935
3036 @ Override
31- public void registerBeanDefinitions (AnnotationMetadata importingClassMetadata , BeanDefinitionRegistry registry , BeanNameGenerator importBeanNameGenerator ) {
32- Map <String , Object > attributes = importingClassMetadata .getAnnotationAttributes (EnableToolCallbackAutoRegistration .class .getName ());
37+ public void registerBeanDefinitions (AnnotationMetadata importingClassMetadata , BeanDefinitionRegistry registry ,
38+ BeanNameGenerator importBeanNameGenerator ) {
39+ Map <String , Object > attributes = importingClassMetadata
40+ .getAnnotationAttributes (EnableToolCallbackAutoRegistration .class .getName ());
3341
3442 if (attributes == null ) {
3543 return ;
@@ -38,34 +46,36 @@ public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, B
3846 Set <String > basePackages = getBasePackages (attributes );
3947
4048 GenericBeanDefinition beanDefinition = new GenericBeanDefinition ();
41- beanDefinition .setBeanClass (ToolCallbackBeanRegistrar .class );
49+ beanDefinition .setBeanClass (ToolAnnotatedBeanProcessor .class );
4250 beanDefinition .setScope (BeanDefinition .SCOPE_SINGLETON );
4351
4452 ConstructorArgumentValues args = new ConstructorArgumentValues ();
4553 args .addGenericArgumentValue (basePackages );
4654 beanDefinition .setConstructorArgumentValues (args );
4755
48- registry .registerBeanDefinition ("toolScannerConfigurer " , beanDefinition );
56+ registry .registerBeanDefinition ("ToolAnnotatedBeanProcessor " , beanDefinition );
4957 }
5058
5159 /**
52- * Extracts the base packages to scan from the {@code @EnableToolCallbackAutoRegistration} annotation attributes.
60+ * Extracts the base packages to scan from the
61+ * {@code @EnableToolCallbackAutoRegistration} annotation attributes.
5362 *
54- * <p>Supports the following attributes:
63+ * <p>
64+ * Supports the following attributes:
5565 * <ul>
56- * <li>{@code value} - Shorthand for base packages</li>
57- * <li>{@code basePackages} - Explicit list of packages</li>
58- * <li>{@code basePackageClasses} - Infers packages from class types</li>
66+ * <li>{@code value} - Shorthand for base packages</li>
67+ * <li>{@code basePackages} - Explicit list of packages</li>
68+ * <li>{@code basePackageClasses} - Infers packages from class types</li>
5969 * </ul>
60- *
6170 * @param attributes the annotation attributes
6271 * @return a set of base package names to scan
6372 */
6473 private Set <String > getBasePackages (@ NonNull Map <String , Object > attributes ) {
6574 Set <String > basePackages = new HashSet <>();
6675
6776 Object [] valuePackages = (Object []) attributes .get ("value" );
68- if (valuePackages == null ) valuePackages = new Object [0 ];
77+ if (valuePackages == null )
78+ valuePackages = new Object [0 ];
6979
7080 for (Object obj : valuePackages ) {
7181 if (obj instanceof String str && !str .isEmpty ()) {
@@ -74,7 +84,8 @@ private Set<String> getBasePackages(@NonNull Map<String, Object> attributes) {
7484 }
7585
7686 Object [] basePackagesAttr = (Object []) attributes .get ("basePackages" );
77- if (basePackagesAttr == null ) basePackagesAttr = new Object [0 ];
87+ if (basePackagesAttr == null )
88+ basePackagesAttr = new Object [0 ];
7889
7990 for (Object obj : basePackagesAttr ) {
8091 if (obj instanceof String str && !str .isEmpty ()) {
@@ -83,7 +94,8 @@ private Set<String> getBasePackages(@NonNull Map<String, Object> attributes) {
8394 }
8495
8596 Object [] basePackageClasses = (Object []) attributes .get ("basePackageClasses" );
86- if (basePackageClasses == null ) basePackageClasses = new Object [0 ];
97+ if (basePackageClasses == null )
98+ basePackageClasses = new Object [0 ];
8799
88100 for (Object obj : basePackageClasses ) {
89101 if (obj instanceof Class <?>) {
@@ -97,4 +109,5 @@ private Set<String> getBasePackages(@NonNull Map<String, Object> attributes) {
97109
98110 return basePackages ;
99111 }
112+
100113}
0 commit comments