UPDATE 05-Nov
This commit is contained in:
@@ -20,6 +20,8 @@ public class ConversationContextMapper {
|
||||
|
||||
@Value("${conversation.context.days.limit:30}")
|
||||
private int daysLimit;
|
||||
|
||||
private static final int MAX_HISTORY_BYTES = 50 * 1024; // 50 KB
|
||||
|
||||
public String toText(ConversationSessionDTO session, List<ConversationMessageDTO> messages) {
|
||||
if (messages == null || messages.isEmpty()) {
|
||||
@@ -41,7 +43,7 @@ public class ConversationContextMapper {
|
||||
.limit(messageLimit)
|
||||
.sorted(Comparator.comparing(ConversationMessageDTO::timestamp))
|
||||
.collect(Collectors.toList());
|
||||
return toTextFromMessages(recentEntries);
|
||||
return toTextWithTruncation(recentEntries);
|
||||
}
|
||||
|
||||
public String toTextFromMessages(List<ConversationMessageDTO> messages) {
|
||||
@@ -50,6 +52,27 @@ public class ConversationContextMapper {
|
||||
.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) {
|
||||
String prefix = "User: ";
|
||||
|
||||
@@ -70,10 +93,6 @@ public class ConversationContextMapper {
|
||||
|
||||
String text = prefix + entry.text();
|
||||
|
||||
if (entry.parameters() != null && !entry.parameters().isEmpty()) {
|
||||
text += " " + entry.parameters().toString();
|
||||
}
|
||||
|
||||
if (entry.type() == MessageType.AGENT) {
|
||||
text = cleanAgentMessage(text);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user