Skip to content

Commit bd11883

Browse files
committed
Handle PR comments
Signed-off-by: Thomas Vitale <[email protected]>
1 parent bbaac7a commit bd11883

File tree

5 files changed

+11
-8
lines changed

5 files changed

+11
-8
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class JdbcChatMemory implements ChatMemory {
4949
INSERT INTO ai_chat_memory (conversation_id, content, type) VALUES (?, ?, ?)""";
5050

5151
private static final String QUERY_GET = """
52-
SELECT content, type FROM ai_chat_memory WHERE conversation_id = ? ORDER BY "timestamp" DESC LIMIT ?""";
52+
SELECT content, type FROM ai_chat_memory WHERE conversation_id = ? ORDER BY "timestamp" LIMIT ?""";
5353

5454
private static final String QUERY_CLEAR = "DELETE FROM ai_chat_memory WHERE conversation_id = ?";
5555

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
public class JdbcChatMemoryRepository implements ChatMemoryRepository {
4141

4242
private static final String QUERY_GET_IDS = """
43-
SELECT conversation_id FROM ai_chat_memory
43+
SELECT DISTINCT conversation_id FROM ai_chat_memory
4444
""";
4545

4646
private static final String QUERY_ADD = """
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
CREATE TABLE IF NOT EXISTS ai_chat_memory (
2-
conversation_id VARCHAR(36) NOT NULL,
2+
conversation_id VARCHAR(36) NOT NULL PRIMARY KEY,
33
content TEXT NOT NULL,
44
type VARCHAR(10) NOT NULL,
55
`timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
66
CONSTRAINT type_check CHECK (type IN ('USER', 'ASSISTANT', 'SYSTEM', 'TOOL'))
77
);
88

99
CREATE INDEX IF NOT EXISTS ai_chat_memory_conversation_id_timestamp_idx
10-
ON ai_chat_memory(conversation_id, `timestamp` DESC);
10+
ON ai_chat_memory(conversation_id, `timestamp`);
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
CREATE TABLE IF NOT EXISTS ai_chat_memory (
2-
conversation_id VARCHAR(36) NOT NULL,
2+
conversation_id VARCHAR(36) NOT NULL PRIMARY KEY,
33
content TEXT NOT NULL,
44
type VARCHAR(10) NOT NULL CHECK (type IN ('USER', 'ASSISTANT', 'SYSTEM', 'TOOL')),
55
"timestamp" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
66
);
77

88
CREATE INDEX IF NOT EXISTS ai_chat_memory_conversation_id_timestamp_idx
9-
ON ai_chat_memory(conversation_id, "timestamp" DESC);
9+
ON ai_chat_memory(conversation_id, "timestamp");

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
import org.springframework.util.Assert;
2222

2323
import java.util.ArrayList;
24+
import java.util.HashSet;
2425
import java.util.List;
26+
import java.util.Set;
2527

2628
/**
2729
* A chat memory implementation that maintains a message window of a specified size,
@@ -85,9 +87,10 @@ public void clear(String conversationId) {
8587
private List<Message> process(List<Message> memoryMessages, List<Message> newMessages) {
8688
List<Message> processedMessages = new ArrayList<>();
8789

90+
Set<Message> memoryMessagesSet = new HashSet<>(memoryMessages);
8891
boolean hasNewSystemMessage = newMessages.stream()
89-
.filter(message -> message instanceof SystemMessage)
90-
.anyMatch(message -> !memoryMessages.contains(message));
92+
.filter(SystemMessage.class::isInstance)
93+
.anyMatch(message -> !memoryMessagesSet.contains(message));
9194

9295
memoryMessages.stream()
9396
.filter(message -> !(hasNewSystemMessage && message instanceof SystemMessage))

0 commit comments

Comments
 (0)