Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

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 @@ -16,15 +16,15 @@

package org.springframework.ai.chat.memory;

import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.SystemMessage;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.SystemMessage;
import org.springframework.util.Assert;

/**
* A chat memory implementation that maintains a message window of a specified size,
* ensuring that the total number of messages does not exceed the specified limit. When
Expand All @@ -36,14 +36,13 @@
* {@link SystemMessage} messages are preserved while evicting other types of messages.
*
* @author Thomas Vitale
* @author Ilayaperumal Gopinathan
* @since 1.0.0
*/
public final class MessageWindowChatMemory implements ChatMemory {

private static final int DEFAULT_MAX_MESSAGES = 20;

private static final ChatMemoryRepository DEFAULT_CHAT_MEMORY_REPOSITORY = new InMemoryChatMemoryRepository();

private final ChatMemoryRepository chatMemoryRepository;

private final int maxMessages;
Expand Down Expand Up @@ -124,7 +123,7 @@ public static Builder builder() {

public static class Builder {

private ChatMemoryRepository chatMemoryRepository = DEFAULT_CHAT_MEMORY_REPOSITORY;
private ChatMemoryRepository chatMemoryRepository;

private int maxMessages = DEFAULT_MAX_MESSAGES;

Expand All @@ -142,7 +141,10 @@ public Builder maxMessages(int maxMessages) {
}

public MessageWindowChatMemory build() {
return new MessageWindowChatMemory(chatMemoryRepository, maxMessages);
if (this.chatMemoryRepository == null) {
this.chatMemoryRepository = new InMemoryChatMemoryRepository();
}
return new MessageWindowChatMemory(this.chatMemoryRepository, this.maxMessages);
}

}
Expand Down