Repository navigation
Migrate Spring AI samples to standalone package and Spring AI 2 #810
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
brianstrauch
wants to merge
2
commits into
main
Choose a base branch
from
brian/spring-ai-standalone-package
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
springai/basic/src/test/java/io/temporal/samples/springai/chat/ChatWorkflowTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| package io.temporal.samples.springai.chat; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import io.temporal.client.WorkflowClient; | ||
| import io.temporal.client.WorkflowOptions; | ||
| import io.temporal.client.WorkflowStub; | ||
| import io.temporal.springai.activity.ChatModelActivityImpl; | ||
| import io.temporal.testing.TestWorkflowEnvironment; | ||
| import io.temporal.testing.WorkflowReplayer; | ||
| import java.time.Duration; | ||
| import java.util.List; | ||
| import java.util.concurrent.CopyOnWriteArrayList; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.ai.chat.messages.AssistantMessage; | ||
| import org.springframework.ai.chat.messages.ToolResponseMessage; | ||
| import org.springframework.ai.chat.model.ChatModel; | ||
| import org.springframework.ai.chat.model.ChatResponse; | ||
| import org.springframework.ai.chat.model.Generation; | ||
| import org.springframework.ai.chat.prompt.Prompt; | ||
|
|
||
| class ChatWorkflowTest { | ||
|
|
||
| @Test | ||
| void toolsAndConversationMemorySurviveReplay() throws Exception { | ||
| List<Prompt> requests = new CopyOnWriteArrayList<>(); | ||
| ChatModel model = | ||
| prompt -> { | ||
| requests.add(prompt); | ||
| AssistantMessage response; | ||
| if (requests.size() == 1) { | ||
| response = | ||
| AssistantMessage.builder() | ||
| .toolCalls( | ||
| List.of( | ||
| new AssistantMessage.ToolCall( | ||
| "weather-1", "function", "getWeather", "{\"city\":\"Seattle\"}"))) | ||
| .build(); | ||
| } else { | ||
| response = | ||
| new AssistantMessage( | ||
| requests.size() == 2 ? "Seattle is rainy." : "You asked about Seattle."); | ||
| } | ||
| return new ChatResponse(List.of(new Generation(response))); | ||
| }; | ||
|
|
||
| try (TestWorkflowEnvironment env = TestWorkflowEnvironment.newInstance()) { | ||
| var worker = env.newWorker("spring-ai-chat-test"); | ||
| worker.registerWorkflowImplementationTypes(ChatWorkflowImpl.class); | ||
| worker.registerActivitiesImplementations( | ||
| new ChatModelActivityImpl(model), new WeatherActivityImpl()); | ||
| env.start(); | ||
|
|
||
| ChatWorkflow workflow = | ||
| env.getWorkflowClient() | ||
| .newWorkflowStub( | ||
| ChatWorkflow.class, | ||
| WorkflowOptions.newBuilder() | ||
| .setTaskQueue("spring-ai-chat-test") | ||
| .setWorkflowExecutionTimeout(Duration.ofSeconds(20)) | ||
| .build()); | ||
| WorkflowClient.start(workflow::run, "Answer weather questions."); | ||
| assertEquals("Seattle is rainy.", workflow.chat("What's the weather in Seattle?")); | ||
| assertEquals("You asked about Seattle.", workflow.chat("Which city did I ask about?")); | ||
| workflow.end(); | ||
| WorkflowStub stub = WorkflowStub.fromTyped(workflow); | ||
| assertEquals("Chat ended after 2 messages.", stub.getResult(String.class)); | ||
|
|
||
| assertEquals(3, requests.size()); | ||
| assertTrue( | ||
| requests.get(1).getInstructions().stream() | ||
| .filter(ToolResponseMessage.class::isInstance) | ||
| .map(ToolResponseMessage.class::cast) | ||
| .flatMap(message -> message.getResponses().stream()) | ||
| .anyMatch(response -> response.responseData().contains("Seattle"))); | ||
| assertTrue( | ||
| requests.get(2).getInstructions().stream() | ||
| .anyMatch(message -> "What's the weather in Seattle?".equals(message.getText()))); | ||
| assertTrue( | ||
| requests.get(2).getInstructions().stream() | ||
| .anyMatch(message -> "Seattle is rainy.".equals(message.getText()))); | ||
|
|
||
| WorkflowReplayer.replayWorkflowExecution( | ||
| env.getWorkflowClient().fetchHistory(stub.getExecution().getWorkflowId()), | ||
| ChatWorkflowImpl.class); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ spring: | |
| openai: | ||
| api-key: ${OPENAI_API_KEY} | ||
| chat: | ||
| max-retries: 0 | ||
| options: | ||
| model: gpt-4o-mini | ||
| mcp: | ||
|
|
||
154 changes: 154 additions & 0 deletions
154
springai/mcp/src/test/java/io/temporal/samples/springai/mcp/McpWorkflowTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| package io.temporal.samples.springai.mcp; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertInstanceOf; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import io.modelcontextprotocol.spec.McpSchema; | ||
| import io.temporal.client.WorkflowClient; | ||
| import io.temporal.client.WorkflowOptions; | ||
| import io.temporal.client.WorkflowStub; | ||
| import io.temporal.springai.activity.ChatModelActivityImpl; | ||
| import io.temporal.springai.mcp.McpClientActivity; | ||
| import io.temporal.testing.TestWorkflowEnvironment; | ||
| import io.temporal.testing.WorkflowReplayer; | ||
| import java.time.Duration; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.concurrent.CopyOnWriteArrayList; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.ai.chat.messages.AssistantMessage; | ||
| import org.springframework.ai.chat.messages.ToolResponseMessage; | ||
| import org.springframework.ai.chat.model.ChatModel; | ||
| import org.springframework.ai.chat.model.ChatResponse; | ||
| import org.springframework.ai.chat.model.Generation; | ||
| import org.springframework.ai.chat.prompt.Prompt; | ||
| import org.springframework.ai.mcp.McpToolUtils; | ||
| import org.springframework.ai.model.tool.ToolCallingChatOptions; | ||
|
|
||
| class McpWorkflowTest { | ||
|
|
||
| @Test | ||
| void discoveredToolsAndConversationMemorySurviveReplay() throws Exception { | ||
| String toolName = McpToolUtils.prefixedToolName("filesystem", "read_file"); | ||
| List<Prompt> requests = new CopyOnWriteArrayList<>(); | ||
| ChatModel model = | ||
| prompt -> { | ||
| requests.add(prompt); | ||
| AssistantMessage response; | ||
| if (requests.size() == 1) { | ||
| response = | ||
| AssistantMessage.builder() | ||
| .toolCalls( | ||
| List.of( | ||
| new AssistantMessage.ToolCall( | ||
| "read-1", "function", toolName, "{\"path\":\"/tmp/notes.txt\"}"))) | ||
| .build(); | ||
| } else { | ||
| response = | ||
| new AssistantMessage( | ||
| requests.size() == 2 ? "The file says hello." : "You asked about notes.txt."); | ||
| } | ||
| return new ChatResponse(List.of(new Generation(response))); | ||
| }; | ||
| FakeMcpClientActivity mcp = new FakeMcpClientActivity(); | ||
|
|
||
| try (TestWorkflowEnvironment env = TestWorkflowEnvironment.newInstance()) { | ||
| var worker = env.newWorker("spring-ai-mcp-test"); | ||
| worker.registerWorkflowImplementationTypes(McpWorkflowImpl.class); | ||
| worker.registerActivitiesImplementations(new ChatModelActivityImpl(model), mcp); | ||
|
brianstrauch marked this conversation as resolved.
|
||
| env.start(); | ||
|
|
||
| McpWorkflow workflow = | ||
| env.getWorkflowClient() | ||
| .newWorkflowStub( | ||
| McpWorkflow.class, | ||
| WorkflowOptions.newBuilder() | ||
| .setTaskQueue("spring-ai-mcp-test") | ||
| .setWorkflowExecutionTimeout(Duration.ofSeconds(30)) | ||
| .build()); | ||
| WorkflowClient.start(workflow::run); | ||
| // The signal may arrive while tool discovery is still initializing. | ||
| workflow.chat("Read /tmp/notes.txt."); | ||
| awaitResponse(workflow, "The file says hello."); | ||
| assertEquals("Available MCP tools:\n - " + toolName + ": Read a file", workflow.listTools()); | ||
| workflow.chat("Which file did I ask about?"); | ||
| awaitResponse(workflow, "You asked about notes.txt."); | ||
| workflow.end(); | ||
| WorkflowStub stub = WorkflowStub.fromTyped(workflow); | ||
| assertEquals("Chat ended after 2 messages.", stub.getResult(String.class)); | ||
|
|
||
| assertEquals(3, requests.size()); | ||
| var options = assertInstanceOf(ToolCallingChatOptions.class, requests.get(0).getOptions()); | ||
| assertEquals(1, options.getToolCallbacks().size()); | ||
| assertEquals(toolName, options.getToolCallbacks().get(0).getToolDefinition().name()); | ||
| assertEquals(List.of("filesystem"), mcp.toolClients); | ||
| assertEquals(1, mcp.toolRequests.size()); | ||
| assertEquals("read_file", mcp.toolRequests.get(0).name()); | ||
| assertEquals(Map.of("path", "/tmp/notes.txt"), mcp.toolRequests.get(0).arguments()); | ||
| assertTrue( | ||
| requests.get(1).getInstructions().stream() | ||
| .filter(ToolResponseMessage.class::isInstance) | ||
| .map(ToolResponseMessage.class::cast) | ||
| .flatMap(message -> message.getResponses().stream()) | ||
| .anyMatch(response -> response.responseData().contains("hello"))); | ||
| assertTrue( | ||
| requests.get(2).getInstructions().stream() | ||
| .anyMatch(message -> "Read /tmp/notes.txt.".equals(message.getText()))); | ||
| assertTrue( | ||
| requests.get(2).getInstructions().stream() | ||
| .anyMatch(message -> "The file says hello.".equals(message.getText()))); | ||
|
|
||
| WorkflowReplayer.replayWorkflowExecution( | ||
| env.getWorkflowClient().fetchHistory(stub.getExecution().getWorkflowId()), | ||
| McpWorkflowImpl.class); | ||
| assertEquals(3, requests.size()); | ||
| assertEquals(1, mcp.toolRequests.size()); | ||
| } | ||
| } | ||
|
|
||
| private static void awaitResponse(McpWorkflow workflow, String expected) | ||
| throws InterruptedException { | ||
| long deadline = System.nanoTime() + Duration.ofSeconds(10).toNanos(); | ||
| while (!expected.equals(workflow.getLastResponse()) && System.nanoTime() < deadline) { | ||
| Thread.sleep(10); | ||
| } | ||
| assertEquals(expected, workflow.getLastResponse()); | ||
| } | ||
|
|
||
| public static class FakeMcpClientActivity implements McpClientActivity { | ||
| final List<String> toolClients = new CopyOnWriteArrayList<>(); | ||
| final List<McpSchema.CallToolRequest> toolRequests = new CopyOnWriteArrayList<>(); | ||
|
|
||
| @Override | ||
| public Map<String, McpSchema.ServerCapabilities> getServerCapabilities() { | ||
| return Map.of(); | ||
| } | ||
|
|
||
| @Override | ||
| public Map<String, McpSchema.Implementation> getClientInfo() { | ||
| return Map.of("filesystem", McpSchema.Implementation.builder("filesystem", "1.0").build()); | ||
| } | ||
|
|
||
| @Override | ||
| public Map<String, McpSchema.ListToolsResult> listTools() { | ||
| McpSchema.Tool tool = | ||
| McpSchema.Tool.builder( | ||
| "read_file", | ||
| Map.of( | ||
| "type", "object", | ||
| "properties", Map.of("path", Map.of("type", "string")), | ||
| "required", List.of("path"))) | ||
| .description("Read a file") | ||
| .build(); | ||
| return Map.of("filesystem", McpSchema.ListToolsResult.builder(List.of(tool)).build()); | ||
| } | ||
|
|
||
| @Override | ||
| public McpSchema.CallToolResult callTool(String clientName, McpSchema.CallToolRequest request) { | ||
| toolClients.add(clientName); | ||
| toolRequests.add(request); | ||
| return McpSchema.CallToolResult.builder().addTextContent("hello").isError(false).build(); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.