Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions gxspringboot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@
<artifactId>urlrewritefilter</artifactId>
<version>5.1.3</version>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-model</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.10.2</version>
</dependency>
</dependencies>

<build>
Expand Down
61 changes: 61 additions & 0 deletions gxspringboot/src/main/java/com/genexus/springboot/GXUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.genexus.springboot;

import com.genexus.diagnostics.core.ILogger;
import com.genexus.specific.java.Connect;
import com.genexus.specific.java.LogManager;
import org.springframework.ai.tool.ToolCallbackProvider;
import org.springframework.ai.tool.method.MethodToolCallbackProvider;
import org.springframework.ai.tool.annotation.Tool;
import org.reflections.Reflections;

import java.lang.reflect.Method;
import java.util.Set;

public class GXUtils {
public static final ILogger logger = com.genexus.diagnostics.core.LogManager.getLogger(GXUtils.class);

public static ToolCallbackProvider operationsTools(String packageName) {
Connect.init();
LogManager.initialize(".");

Reflections reflections = new Reflections(
new org.reflections.util.ConfigurationBuilder()
.forPackages(packageName)
.addScanners(org.reflections.scanners.Scanners.MethodsAnnotated)
);

Set<Method> toolMethods = reflections.getMethodsAnnotatedWith(Tool.class);

MethodToolCallbackProvider.Builder builder = MethodToolCallbackProvider.builder();

// Keep track of classes that have already been added
Set<Class<?>> processedClasses = new java.util.HashSet<>();

for (Method method : toolMethods) {
Class<?> clazz = method.getDeclaringClass();

// Skip if we've already processed this class
if (processedClasses.contains(clazz)) {
continue;
}

try {
Object instance = clazz.getConstructor(int.class).newInstance(-1);
builder.toolObjects(instance);
processedClasses.add(clazz);

Tool toolAnnotation = method.getAnnotation(Tool.class);
logger.debug(String.format("Registered tool: %s - %s",
toolAnnotation.name().isEmpty() ? method.getName() : toolAnnotation.name(),
toolAnnotation.description()));
} catch (Exception e) {
logger.error("Error instantiating tool class: " + clazz.getName(), e);
}
}

if (!toolMethods.isEmpty())
return builder.build();

return null;
}
}
Loading