Skip to content

Commit 0f9d787

Browse files
committed
feat: GH-4433 Add a method to ChatMemory for retrieving all conversation IDs, and add corresponding unit tests.
Signed-off-by: Sun Yuhan <[email protected]>
1 parent 84efb6a commit 0f9d787

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

spring-ai-model/src/main/java/org/springframework/ai/chat/memory/ChatMemory.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
*
2727
* @author Christian Tzolov
2828
* @author Thomas Vitale
29+
* @author Sun Yuhan
2930
* @since 1.0.0
3031
*/
3132
public interface ChatMemory {
@@ -61,4 +62,10 @@ default void add(String conversationId, Message message) {
6162
*/
6263
void clear(String conversationId);
6364

65+
/**
66+
* Get all conversation IDs
67+
* @return All conversation IDs
68+
*/
69+
List<String> getConversations();
70+
6471
}

spring-ai-model/src/main/java/org/springframework/ai/chat/memory/MessageWindowChatMemory.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
*
3838
* @author Thomas Vitale
3939
* @author Ilayaperumal Gopinathan
40+
* @author Sun Yuhan
4041
* @since 1.0.0
4142
*/
4243
public final class MessageWindowChatMemory implements ChatMemory {
@@ -77,6 +78,11 @@ public void clear(String conversationId) {
7778
this.chatMemoryRepository.deleteByConversationId(conversationId);
7879
}
7980

81+
@Override
82+
public List<String> getConversations() {
83+
return this.chatMemoryRepository.findConversationIds();
84+
}
85+
8086
private List<Message> process(List<Message> memoryMessages, List<Message> newMessages) {
8187
List<Message> processedMessages = new ArrayList<>();
8288

spring-ai-model/src/test/java/org/springframework/ai/chat/memory/MessageWindowChatMemoryTests.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,4 +292,50 @@ void mixedMessagesWithLimitEqualToSystemMessageCount() {
292292
new SystemMessage("System instruction 2"));
293293
}
294294

295+
@Test
296+
void getConversationsReturnsAllConversationIds() {
297+
MessageWindowChatMemory chatMemory = MessageWindowChatMemory.builder().build();
298+
299+
String conversationId1 = UUID.randomUUID().toString();
300+
String conversationId2 = UUID.randomUUID().toString();
301+
String conversationId3 = UUID.randomUUID().toString();
302+
303+
chatMemory.add(conversationId1, new UserMessage("Hello from conversation 1"));
304+
chatMemory.add(conversationId2, new UserMessage("Hello from conversation 2"));
305+
chatMemory.add(conversationId3, new UserMessage("Hello from conversation 3"));
306+
307+
List<String> conversations = chatMemory.getConversations();
308+
309+
assertThat(conversations).contains(conversationId1, conversationId2, conversationId3);
310+
assertThat(conversations).hasSize(3);
311+
}
312+
313+
@Test
314+
void getConversationsWithCustomRepository() {
315+
InMemoryChatMemoryRepository customRepository = new InMemoryChatMemoryRepository();
316+
MessageWindowChatMemory chatMemory = MessageWindowChatMemory.builder()
317+
.chatMemoryRepository(customRepository)
318+
.build();
319+
320+
String conversationId1 = UUID.randomUUID().toString();
321+
String conversationId2 = UUID.randomUUID().toString();
322+
323+
chatMemory.add(conversationId1, new UserMessage("Message in conversation 1"));
324+
chatMemory.add(conversationId2, new UserMessage("Message in conversation 2"));
325+
326+
List<String> conversations = chatMemory.getConversations();
327+
328+
assertThat(conversations).contains(conversationId1, conversationId2);
329+
assertThat(conversations).hasSize(2);
330+
}
331+
332+
@Test
333+
void getConversationsReturnsEmptyListWhenNoConversations() {
334+
MessageWindowChatMemory chatMemory = MessageWindowChatMemory.builder().build();
335+
336+
List<String> conversations = chatMemory.getConversations();
337+
338+
assertThat(conversations).isEmpty();
339+
}
340+
295341
}

0 commit comments

Comments
 (0)