|
| 1 | +package org.springframework.ai.openai.chat.client; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | +import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; |
| 7 | + |
| 8 | +import org.springframework.ai.chat.client.ChatClient; |
| 9 | +import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor; |
| 10 | +import org.springframework.ai.chat.memory.ChatMemory; |
| 11 | +import org.springframework.ai.chat.memory.InMemoryChatMemoryRepository; |
| 12 | +import org.springframework.ai.chat.memory.MessageWindowChatMemory; |
| 13 | +import org.springframework.ai.chat.messages.Message; |
| 14 | +import org.springframework.ai.chat.messages.UserMessage; |
| 15 | +import org.springframework.ai.chat.prompt.Prompt; |
| 16 | +import org.springframework.ai.openai.OpenAiTestConfiguration; |
| 17 | +import org.springframework.beans.factory.annotation.Autowired; |
| 18 | +import org.springframework.boot.test.context.SpringBootTest; |
| 19 | +import org.springframework.test.context.ActiveProfiles; |
| 20 | + |
| 21 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 22 | + |
| 23 | +@SpringBootTest(classes = OpenAiTestConfiguration.class) |
| 24 | +@EnabledIfEnvironmentVariable(named = "OPENAI_API_KEY", matches = ".+") |
| 25 | +@ActiveProfiles("logging-test") |
| 26 | +/** |
| 27 | + * Integration test for https://github.com/spring-projects/spring-ai/issues/2339 Verifies |
| 28 | + * that MessageChatMemoryAdvisor works when Prompt is initialized with List<Message>. |
| 29 | + */ |
| 30 | +class OpenAiChatClientMemoryAdvisorReproIT { |
| 31 | + |
| 32 | + @Autowired |
| 33 | + private org.springframework.ai.chat.model.ChatModel chatModel; |
| 34 | + |
| 35 | + @Test |
| 36 | + void messageChatMemoryAdvisor_withPromptMessages_throwsException() { |
| 37 | + // Arrange: create a Prompt with a List<Message> (including UserMessage) |
| 38 | + Message userMessage = new UserMessage("Tell me a joke."); |
| 39 | + List<Message> messages = List.of(userMessage); |
| 40 | + Prompt prompt = new Prompt(messages); |
| 41 | + ChatMemory chatMemory = MessageWindowChatMemory.builder() |
| 42 | + .chatMemoryRepository(new InMemoryChatMemoryRepository()) |
| 43 | + .build(); |
| 44 | + MessageChatMemoryAdvisor advisor = new MessageChatMemoryAdvisor(chatMemory); |
| 45 | + |
| 46 | + ChatClient chatClient = ChatClient.builder(chatModel).defaultAdvisors(advisor).build(); |
| 47 | + |
| 48 | + // Act: call should succeed without exception (issue #2339 is fixed) |
| 49 | + chatClient.prompt(prompt).call().chatResponse(); // Should not throw |
| 50 | + |
| 51 | + } |
| 52 | + |
| 53 | +} |
0 commit comments