|
| 1 | +/* |
| 2 | + * Copyright 2024-2026 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.github.agentic.spring.ai.graph.agent.node; |
| 17 | + |
| 18 | +import io.github.agentic.spring.ai.graph.OverAllState; |
| 19 | +import io.github.agentic.spring.ai.graph.RunnableConfig; |
| 20 | + |
| 21 | +import org.junit.jupiter.api.DisplayName; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | + |
| 24 | +import org.springframework.ai.chat.messages.AssistantMessage; |
| 25 | +import org.springframework.ai.chat.messages.ToolResponseMessage; |
| 26 | +import org.springframework.ai.tool.ToolCallback; |
| 27 | +import org.springframework.ai.tool.execution.ToolExecutionExceptionProcessor; |
| 28 | +import org.springframework.ai.tool.function.FunctionToolCallback; |
| 29 | + |
| 30 | +import java.time.Duration; |
| 31 | +import java.util.ArrayList; |
| 32 | +import java.util.HashMap; |
| 33 | +import java.util.List; |
| 34 | +import java.util.Map; |
| 35 | + |
| 36 | +import static org.assertj.core.api.Assertions.assertThat; |
| 37 | + |
| 38 | +/** |
| 39 | + * Tests for the in-memory dynamic tool callback registry on {@link AgentToolNode}. |
| 40 | + * |
| 41 | + * <p> |
| 42 | + * Covers the Human-in-the-Loop resumption scenario reported in issue #35: skill tools are |
| 43 | + * disclosed dynamically at model-call time and passed to the tool node through |
| 44 | + * {@code config.context()}, which is cleared when a subgraph resumes after an interruption. |
| 45 | + * The registry (shared with {@link AgentLlmNode} via the agent builder) keeps the disclosed |
| 46 | + * callbacks resolvable across resumption. |
| 47 | + * </p> |
| 48 | + * |
| 49 | + * @author Kal'tsit |
| 50 | + * @since 1.0.0 |
| 51 | + */ |
| 52 | +@DisplayName("AgentToolNode Dynamic Tool Callback Registry Tests") |
| 53 | +class AgentToolNodeDynamicToolCallbackRegistryTest { |
| 54 | + |
| 55 | + private static final ToolExecutionExceptionProcessor DEFAULT_PROCESSOR = e -> "Error: " + e.getMessage(); |
| 56 | + |
| 57 | + private ToolCallback queryTool() { |
| 58 | + return FunctionToolCallback.builder("queryTool", (QueryInput input) -> "result:" + input.q()) |
| 59 | + .description("Query tool") |
| 60 | + .inputType(QueryInput.class) |
| 61 | + .build(); |
| 62 | + } |
| 63 | + |
| 64 | + record QueryInput(String q) { |
| 65 | + } |
| 66 | + |
| 67 | + private AgentToolNode buildNode() { |
| 68 | + return AgentToolNode.builder() |
| 69 | + .agentName("test-agent") |
| 70 | + .toolExecutionTimeout(Duration.ofMinutes(5)) |
| 71 | + .toolExecutionExceptionProcessor(DEFAULT_PROCESSOR) |
| 72 | + .build(); |
| 73 | + } |
| 74 | + |
| 75 | + private OverAllState stateWithToolCall(String toolName, String arguments) { |
| 76 | + AssistantMessage assistantMessage = AssistantMessage.builder() |
| 77 | + .content("") |
| 78 | + .toolCalls(List.of(new AssistantMessage.ToolCall("call_1", "function", toolName, arguments))) |
| 79 | + .build(); |
| 80 | + Map<String, Object> stateData = new HashMap<>(); |
| 81 | + stateData.put("messages", new ArrayList<>(List.of(assistantMessage))); |
| 82 | + return new OverAllState(stateData); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + @DisplayName("Dynamic registry resolves a tool when the config context is empty (HITL resume scenario)") |
| 87 | + void dynamicRegistryResolvesToolWhenConfigContextIsEmpty() throws Exception { |
| 88 | + AgentToolNode node = buildNode(); |
| 89 | + node.registerDynamicToolCallbacks(List.of(queryTool())); |
| 90 | + |
| 91 | + // Simulate a resumed run: a fresh config whose context no longer carries the |
| 92 | + // dynamically disclosed tool callbacks. |
| 93 | + RunnableConfig config = RunnableConfig.builder().threadId("test-thread").build(); |
| 94 | + |
| 95 | + Map<String, Object> result = node.apply(stateWithToolCall("queryTool", "{\"q\":\"hello\"}"), config); |
| 96 | + |
| 97 | + ToolResponseMessage responseMessage = (ToolResponseMessage) result.get("messages"); |
| 98 | + assertThat(responseMessage.getResponses()).hasSize(1); |
| 99 | + assertThat(responseMessage.getResponses().get(0).responseData()).contains("result:hello"); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + @DisplayName("A tool absent from the registry is reported as unavailable") |
| 104 | + void toolAbsentFromRegistryIsReportedUnavailable() throws Exception { |
| 105 | + AgentToolNode node = buildNode(); |
| 106 | + |
| 107 | + RunnableConfig config = RunnableConfig.builder().threadId("test-thread").build(); |
| 108 | + |
| 109 | + Map<String, Object> result = node.apply(stateWithToolCall("unknownTool", "{}"), config); |
| 110 | + |
| 111 | + ToolResponseMessage responseMessage = (ToolResponseMessage) result.get("messages"); |
| 112 | + assertThat(responseMessage.getResponses()).hasSize(1); |
| 113 | + assertThat(responseMessage.getResponses().get(0).responseData()).contains("Tool not available: unknownTool"); |
| 114 | + } |
| 115 | + |
| 116 | +} |
0 commit comments