Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
*
* @author Christian Tzolov
* @author Thomas Vitale
* @author Sun Yuhan
* @since 1.0.0
*/
public interface ChatMemory {
Expand Down Expand Up @@ -61,4 +62,10 @@ default void add(String conversationId, Message message) {
*/
void clear(String conversationId);

/**
* Get all conversation IDs
* @return All conversation IDs
*/
List<String> getConversations();

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
*
* @author Thomas Vitale
* @author Ilayaperumal Gopinathan
* @author Sun Yuhan
* @since 1.0.0
*/
public final class MessageWindowChatMemory implements ChatMemory {
Expand Down Expand Up @@ -77,6 +78,11 @@ public void clear(String conversationId) {
this.chatMemoryRepository.deleteByConversationId(conversationId);
}

@Override
public List<String> getConversations() {
return this.chatMemoryRepository.findConversationIds();
}

private List<Message> process(List<Message> memoryMessages, List<Message> newMessages) {
List<Message> processedMessages = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,50 @@ void mixedMessagesWithLimitEqualToSystemMessageCount() {
new SystemMessage("System instruction 2"));
}

@Test
void getConversationsReturnsAllConversationIds() {
MessageWindowChatMemory chatMemory = MessageWindowChatMemory.builder().build();

String conversationId1 = UUID.randomUUID().toString();
String conversationId2 = UUID.randomUUID().toString();
String conversationId3 = UUID.randomUUID().toString();

chatMemory.add(conversationId1, new UserMessage("Hello from conversation 1"));
chatMemory.add(conversationId2, new UserMessage("Hello from conversation 2"));
chatMemory.add(conversationId3, new UserMessage("Hello from conversation 3"));

List<String> conversations = chatMemory.getConversations();

assertThat(conversations).contains(conversationId1, conversationId2, conversationId3);
assertThat(conversations).hasSize(3);
}

@Test
void getConversationsWithCustomRepository() {
InMemoryChatMemoryRepository customRepository = new InMemoryChatMemoryRepository();
MessageWindowChatMemory chatMemory = MessageWindowChatMemory.builder()
.chatMemoryRepository(customRepository)
.build();

String conversationId1 = UUID.randomUUID().toString();
String conversationId2 = UUID.randomUUID().toString();

chatMemory.add(conversationId1, new UserMessage("Message in conversation 1"));
chatMemory.add(conversationId2, new UserMessage("Message in conversation 2"));

List<String> conversations = chatMemory.getConversations();

assertThat(conversations).contains(conversationId1, conversationId2);
assertThat(conversations).hasSize(2);
}

@Test
void getConversationsReturnsEmptyListWhenNoConversations() {
MessageWindowChatMemory chatMemory = MessageWindowChatMemory.builder().build();

List<String> conversations = chatMemory.getConversations();

assertThat(conversations).isEmpty();
}

}