UPDATE 05-Nov
This commit is contained in:
@@ -20,6 +20,8 @@ public class ConversationContextMapper {
|
|||||||
|
|
||||||
@Value("${conversation.context.days.limit:30}")
|
@Value("${conversation.context.days.limit:30}")
|
||||||
private int daysLimit;
|
private int daysLimit;
|
||||||
|
|
||||||
|
private static final int MAX_HISTORY_BYTES = 50 * 1024; // 50 KB
|
||||||
|
|
||||||
public String toText(ConversationSessionDTO session, List<ConversationMessageDTO> messages) {
|
public String toText(ConversationSessionDTO session, List<ConversationMessageDTO> messages) {
|
||||||
if (messages == null || messages.isEmpty()) {
|
if (messages == null || messages.isEmpty()) {
|
||||||
@@ -41,7 +43,7 @@ public class ConversationContextMapper {
|
|||||||
.limit(messageLimit)
|
.limit(messageLimit)
|
||||||
.sorted(Comparator.comparing(ConversationMessageDTO::timestamp))
|
.sorted(Comparator.comparing(ConversationMessageDTO::timestamp))
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
return toTextFromMessages(recentEntries);
|
return toTextWithTruncation(recentEntries);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toTextFromMessages(List<ConversationMessageDTO> messages) {
|
public String toTextFromMessages(List<ConversationMessageDTO> messages) {
|
||||||
@@ -50,6 +52,27 @@ public class ConversationContextMapper {
|
|||||||
.collect(Collectors.joining("\n"));
|
.collect(Collectors.joining("\n"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String toTextWithTruncation(List<ConversationMessageDTO> messages) {
|
||||||
|
if (messages == null || messages.isEmpty()) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
StringBuilder textBlock = new StringBuilder();
|
||||||
|
List<String> formattedMessages = messages.stream()
|
||||||
|
.map(this::formatEntry)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
for (int i = formattedMessages.size() - 1; i >= 0; i--) {
|
||||||
|
String message = formattedMessages.get(i) + "\n";
|
||||||
|
if (textBlock.toString().getBytes(java.nio.charset.StandardCharsets.UTF_8).length + message.getBytes(java.nio.charset.StandardCharsets.UTF_8).length > MAX_HISTORY_BYTES) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
textBlock.insert(0, message);
|
||||||
|
}
|
||||||
|
|
||||||
|
return textBlock.toString().trim();
|
||||||
|
}
|
||||||
|
|
||||||
private String formatEntry(ConversationMessageDTO entry) {
|
private String formatEntry(ConversationMessageDTO entry) {
|
||||||
String prefix = "User: ";
|
String prefix = "User: ";
|
||||||
|
|
||||||
@@ -70,10 +93,6 @@ public class ConversationContextMapper {
|
|||||||
|
|
||||||
String text = prefix + entry.text();
|
String text = prefix + entry.text();
|
||||||
|
|
||||||
if (entry.parameters() != null && !entry.parameters().isEmpty()) {
|
|
||||||
text += " " + entry.parameters().toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (entry.type() == MessageType.AGENT) {
|
if (entry.type() == MessageType.AGENT) {
|
||||||
text = cleanAgentMessage(text);
|
text = cleanAgentMessage(text);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,15 @@
|
|||||||
package com.example.mapper.messagefilter;
|
package com.example.mapper.messagefilter;
|
||||||
|
|
||||||
|
import com.example.dto.dialogflow.conversation.ConversationMessageDTO;
|
||||||
|
import com.example.dto.dialogflow.conversation.MessageType;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
public class ConversationContextMapperTest {
|
public class ConversationContextMapperTest {
|
||||||
|
|
||||||
@@ -91,4 +98,24 @@ public class ConversationContextMapperTest {
|
|||||||
String result = (String) method.invoke(mapper, input);
|
String result = (String) method.invoke(mapper, input);
|
||||||
assertEquals(expected, result);
|
assertEquals(expected, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testToTextWithTruncation() {
|
||||||
|
ConversationContextMapper mapper = new ConversationContextMapper();
|
||||||
|
List<ConversationMessageDTO> messages = new ArrayList<>();
|
||||||
|
for (int i = 0; i < 1000; i++) {
|
||||||
|
messages.add(createMessage("This is message " + i, MessageType.USER));
|
||||||
|
}
|
||||||
|
for (int i = 1000; i < 2000; i++) {
|
||||||
|
messages.add(createMessage("This is message " + i, MessageType.AGENT));
|
||||||
|
}
|
||||||
|
|
||||||
|
String result = mapper.toTextWithTruncation(messages);
|
||||||
|
assertTrue(result.length() > 0);
|
||||||
|
assertTrue(result.getBytes(java.nio.charset.StandardCharsets.UTF_8).length <= 50 * 1024);
|
||||||
|
}
|
||||||
|
|
||||||
|
private ConversationMessageDTO createMessage(String text, MessageType type) {
|
||||||
|
return new ConversationMessageDTO(type, Instant.now(), text, null, null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user