1616
1717package org .springframework .ai .chat .memory ;
1818
19- import org .springframework .ai .chat .messages .Message ;
20- import org .springframework .ai .chat .messages .SystemMessage ;
21- import org .springframework .util .Assert ;
22-
2319import java .util .ArrayList ;
2420import java .util .HashSet ;
2521import java .util .List ;
2622import java .util .Set ;
2723
24+ import org .springframework .ai .chat .messages .Message ;
25+ import org .springframework .ai .chat .messages .SystemMessage ;
26+ import org .springframework .util .Assert ;
27+
2828/**
2929 * A chat memory implementation that maintains a message window of a specified size,
3030 * ensuring that the total number of messages does not exceed the specified limit. When
3636 * {@link SystemMessage} messages are preserved while evicting other types of messages.
3737 *
3838 * @author Thomas Vitale
39+ * @author Ilayaperumal Gopinathan
3940 * @since 1.0.0
4041 */
4142public final class MessageWindowChatMemory implements ChatMemory {
4243
4344 private static final int DEFAULT_MAX_MESSAGES = 20 ;
4445
45- private static final ChatMemoryRepository DEFAULT_CHAT_MEMORY_REPOSITORY = new InMemoryChatMemoryRepository ();
46-
4746 private final ChatMemoryRepository chatMemoryRepository ;
4847
4948 private final int maxMessages ;
@@ -124,7 +123,7 @@ public static Builder builder() {
124123
125124 public static class Builder {
126125
127- private ChatMemoryRepository chatMemoryRepository = DEFAULT_CHAT_MEMORY_REPOSITORY ;
126+ private ChatMemoryRepository chatMemoryRepository ;
128127
129128 private int maxMessages = DEFAULT_MAX_MESSAGES ;
130129
@@ -142,7 +141,10 @@ public Builder maxMessages(int maxMessages) {
142141 }
143142
144143 public MessageWindowChatMemory build () {
145- return new MessageWindowChatMemory (chatMemoryRepository , maxMessages );
144+ if (this .chatMemoryRepository == null ) {
145+ this .chatMemoryRepository = new InMemoryChatMemoryRepository ();
146+ }
147+ return new MessageWindowChatMemory (this .chatMemoryRepository , this .maxMessages );
146148 }
147149
148150 }
0 commit comments