From 89d8f6a20eabd11b570e871acbed6c4fcc2d38ef Mon Sep 17 00:00:00 2001 From: Ilayaperumal Gopinathan Date: Thu, 12 Jun 2025 18:37:29 +0100 Subject: [PATCH] Revert "fix: corrected a logic error in the `validateToolContextSupport` method caused by incorrect parameter order." This reverts commit 2ca1be2b838e87b9c0674b4b8b76f1010eb45951. --- .../ai/tool/method/MethodToolCallback.java | 4 +- .../MethodToolCallbackGenericTypesTest.java | 77 ------------------- 2 files changed, 2 insertions(+), 79 deletions(-) diff --git a/spring-ai-model/src/main/java/org/springframework/ai/tool/method/MethodToolCallback.java b/spring-ai-model/src/main/java/org/springframework/ai/tool/method/MethodToolCallback.java index d5f3970ea..cc320a54d 100644 --- a/spring-ai-model/src/main/java/org/springframework/ai/tool/method/MethodToolCallback.java +++ b/spring-ai-model/src/main/java/org/springframework/ai/tool/method/MethodToolCallback.java @@ -118,8 +118,8 @@ public final class MethodToolCallback implements ToolCallback { private void validateToolContextSupport(@Nullable ToolContext toolContext) { var isNonEmptyToolContextProvided = toolContext != null && !CollectionUtils.isEmpty(toolContext.getContext()); var isToolContextAcceptedByMethod = Stream.of(this.toolMethod.getParameterTypes()) - .anyMatch(type -> ClassUtils.isAssignable(ToolContext.class, type)); - if (isNonEmptyToolContextProvided && !isToolContextAcceptedByMethod) { + .anyMatch(type -> ClassUtils.isAssignable(type, ToolContext.class)); + if (isToolContextAcceptedByMethod && !isNonEmptyToolContextProvided) { throw new IllegalArgumentException("ToolContext is required by the method as an argument"); } } diff --git a/spring-ai-model/src/test/java/org/springframework/ai/tool/method/MethodToolCallbackGenericTypesTest.java b/spring-ai-model/src/test/java/org/springframework/ai/tool/method/MethodToolCallbackGenericTypesTest.java index 544d440a9..b99faa71a 100644 --- a/spring-ai-model/src/test/java/org/springframework/ai/tool/method/MethodToolCallbackGenericTypesTest.java +++ b/spring-ai-model/src/test/java/org/springframework/ai/tool/method/MethodToolCallbackGenericTypesTest.java @@ -22,12 +22,10 @@ import java.util.Map; import org.junit.jupiter.api.Test; -import org.springframework.ai.chat.model.ToolContext; import org.springframework.ai.tool.definition.DefaultToolDefinition; import org.springframework.ai.tool.definition.ToolDefinition; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; /** * Tests for {@link MethodToolCallback} with generic types. @@ -139,76 +137,6 @@ class MethodToolCallbackGenericTypesTest { assertThat(result).isEqualTo("2 maps processed: [{a=1, b=2}, {c=3, d=4}]"); } - @Test - void testToolContextType() throws Exception { - // Create a test object with a method that takes a List> - TestGenericClass testObject = new TestGenericClass(); - Method method = TestGenericClass.class.getMethod("processStringListInToolContext", ToolContext.class); - - // Create a tool definition - ToolDefinition toolDefinition = DefaultToolDefinition.builder() - .name("processToolContext") - .description("Process tool context") - .inputSchema("{}") - .build(); - - // Create a MethodToolCallback - MethodToolCallback callback = MethodToolCallback.builder() - .toolDefinition(toolDefinition) - .toolMethod(method) - .toolObject(testObject) - .build(); - - // Create an empty JSON input - String toolInput = """ - {} - """; - - // Create a toolContext - ToolContext toolContext = new ToolContext(Map.of("foo", "bar")); - - // Call the tool - String result = callback.call(toolInput, toolContext); - - // Verify the result - assertThat(result).isEqualTo("1 entries processed {foo=bar}"); - } - - @Test - void testToolContextTypeWithNonToolContextArgs() throws Exception { - // Create a test object with a method that takes a List - TestGenericClass testObject = new TestGenericClass(); - Method method = TestGenericClass.class.getMethod("processStringList", List.class); - - // Create a tool definition - ToolDefinition toolDefinition = DefaultToolDefinition.builder() - .name("processStringList") - .description("Process a list of strings") - .inputSchema("{}") - .build(); - - // Create a MethodToolCallback - MethodToolCallback callback = MethodToolCallback.builder() - .toolDefinition(toolDefinition) - .toolMethod(method) - .toolObject(testObject) - .build(); - - // Create a JSON input with a list of strings - String toolInput = """ - { - "strings": ["one", "two", "three"] - } - """; - - // Create a toolContext - ToolContext toolContext = new ToolContext(Map.of("foo", "bar")); - - // Call the tool and verify - assertThatThrownBy(() -> callback.call(toolInput, toolContext)).isInstanceOf(IllegalArgumentException.class) - .hasMessageContaining("ToolContext is required by the method as an argument"); - } - /** * Test class with methods that use generic types. */ @@ -226,11 +154,6 @@ class MethodToolCallbackGenericTypesTest { return listOfMaps.size() + " maps processed: " + listOfMaps; } - public String processStringListInToolContext(ToolContext toolContext) { - Map context = toolContext.getContext(); - return context.size() + " entries processed " + context; - } - } }