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