消息记录存储添加排序
This commit is contained in:
@@ -13,6 +13,7 @@ import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -43,8 +44,13 @@ public class MysqlChatMemoryRepository implements ChatMemoryRepository {
|
||||
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
|
||||
public void saveAll(String conversationId, List<Message> messages) {
|
||||
chatMessageHistoryMapper.deleteByConversationId(conversationId);
|
||||
List<ChatMessageHistory> messageHistoryList = messages.stream().map(message -> {
|
||||
ChatMessageHistory chatMessageHistory = new ChatMessageHistory();
|
||||
|
||||
ArrayList<ChatMessageHistory> messageHistoryList = new ArrayList<>();
|
||||
ChatMessageHistory chatMessageHistory;
|
||||
Message message;
|
||||
for (int i = 0; i < messages.size(); i++) {
|
||||
chatMessageHistory = new ChatMessageHistory();
|
||||
message = messages.get(i);
|
||||
chatMessageHistory.setId(UUID.randomUUID().toString());
|
||||
chatMessageHistory.setConversationId(conversationId);
|
||||
chatMessageHistory.setMessageType(message.getMessageType().getValue());
|
||||
@@ -53,9 +59,10 @@ public class MysqlChatMemoryRepository implements ChatMemoryRepository {
|
||||
if (message instanceof AssistantMessage am) {
|
||||
chatMessageHistory.setToolCalls(JSON.toJSONString(am.getToolCalls()));
|
||||
}
|
||||
chatMessageHistory.setSort(i);
|
||||
chatMessageHistory.setCreateTime(LocalDateTime.now());
|
||||
return chatMessageHistory;
|
||||
}).collect(Collectors.toList());
|
||||
messageHistoryList.add(chatMessageHistory);
|
||||
}
|
||||
chatMessageHistoryMapper.insert(messageHistoryList);
|
||||
}
|
||||
|
||||
|
||||
@@ -28,5 +28,7 @@ public class ChatMessageHistory {
|
||||
|
||||
private String toolCalls;
|
||||
|
||||
private int sort;
|
||||
|
||||
private LocalDateTime createTime;
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ import java.util.List;
|
||||
*/
|
||||
public interface ChatMessageHistoryMapper extends BaseMapper<ChatMessageHistory> {
|
||||
|
||||
@Select("select * from chat_message_history where conversation_id = #{conversationId}")
|
||||
@Select("select * from chat_message_history where conversation_id = #{conversationId} order by sort asc")
|
||||
List<ChatMessageHistory> selectByConversationId(String conversationId);
|
||||
|
||||
@Delete("delete from chat_message_history where conversation_id = #{conversationId}")
|
||||
|
||||
Reference in New Issue
Block a user