fix: 修复chatlog会话元数据回写与覆盖问题
- 为消息追加命令补充用户、助手与会话标题元信息 - 持久化后按会话聚合触发createOrTouch并优化空值覆盖策略 - 增加ChatPersistMySqlApplyService单测覆盖会话回写链路
This commit is contained in:
@@ -106,6 +106,7 @@ public class ChatPersistMySqlApplyService {
|
||||
insertedCommands = logRepository.appendMessages(appendCommands);
|
||||
}
|
||||
if (!insertedCommands.isEmpty()) {
|
||||
sessionRepository.createOrTouchBatch(buildSessionUpserts(insertedCommands));
|
||||
summaryCommands.clear();
|
||||
for (ChatAppendMessageCommand insertedCommand : insertedCommands) {
|
||||
accumulateSummary(summaryCommands, insertedCommand);
|
||||
@@ -139,9 +140,74 @@ public class ChatPersistMySqlApplyService {
|
||||
}
|
||||
}
|
||||
|
||||
List<ChatSessionUpsertCommand> buildSessionUpserts(List<ChatAppendMessageCommand> commands) {
|
||||
if (commands == null || commands.isEmpty()) {
|
||||
return List.of();
|
||||
}
|
||||
Map<BigInteger, ChatSessionUpsertCommand> upserts = new LinkedHashMap<>();
|
||||
for (ChatAppendMessageCommand command : commands) {
|
||||
if (command == null || command.getSessionId() == null) {
|
||||
continue;
|
||||
}
|
||||
ChatSessionUpsertCommand upsert = upserts.computeIfAbsent(command.getSessionId(), key -> {
|
||||
ChatSessionUpsertCommand created = new ChatSessionUpsertCommand();
|
||||
created.setSessionId(command.getSessionId());
|
||||
created.setTenantId(command.getTenantId());
|
||||
created.setDeptId(command.getDeptId());
|
||||
created.setUserId(command.getUserId());
|
||||
created.setAssistantId(command.getAssistantId());
|
||||
created.setOperateAt(null);
|
||||
return created;
|
||||
});
|
||||
if (upsert.getTenantId() == null) {
|
||||
upsert.setTenantId(command.getTenantId());
|
||||
}
|
||||
if (upsert.getDeptId() == null) {
|
||||
upsert.setDeptId(command.getDeptId());
|
||||
}
|
||||
if (upsert.getUserId() == null) {
|
||||
upsert.setUserId(command.getUserId());
|
||||
}
|
||||
if (upsert.getAssistantId() == null) {
|
||||
upsert.setAssistantId(command.getAssistantId());
|
||||
}
|
||||
if (isBlank(upsert.getUserAccount()) && !isBlank(command.getUserAccount())) {
|
||||
upsert.setUserAccount(command.getUserAccount().trim());
|
||||
}
|
||||
if (isBlank(upsert.getAssistantCode()) && !isBlank(command.getAssistantCode())) {
|
||||
upsert.setAssistantCode(command.getAssistantCode().trim());
|
||||
}
|
||||
if (isBlank(upsert.getAssistantName()) && !isBlank(command.getAssistantName())) {
|
||||
upsert.setAssistantName(command.getAssistantName().trim());
|
||||
}
|
||||
if (isBlank(upsert.getTitle()) && !isBlank(command.getSessionTitle())) {
|
||||
upsert.setTitle(command.getSessionTitle().trim());
|
||||
}
|
||||
Date operateAt = defaultDate(command.getCreated());
|
||||
if (upsert.getOperateAt() == null || !operateAt.before(upsert.getOperateAt())) {
|
||||
upsert.setOperateAt(operateAt);
|
||||
upsert.setOperatorId(command.getCreatedBy());
|
||||
}
|
||||
}
|
||||
for (ChatSessionUpsertCommand upsert : upserts.values()) {
|
||||
if (isBlank(upsert.getUserAccount())) {
|
||||
upsert.setUserAccount("");
|
||||
}
|
||||
if (isBlank(upsert.getTitle())) {
|
||||
upsert.setTitle("会话-" + upsert.getSessionId());
|
||||
}
|
||||
if (upsert.getOperateAt() == null) {
|
||||
upsert.setOperateAt(new Date());
|
||||
}
|
||||
if (upsert.getOperatorId() == null) {
|
||||
upsert.setOperatorId(upsert.getUserId());
|
||||
}
|
||||
}
|
||||
return new ArrayList<>(upserts.values());
|
||||
}
|
||||
|
||||
private YearMonth resolveMonth(Date createdAt) {
|
||||
Date created = createdAt == null ? new Date() : createdAt;
|
||||
return YearMonth.from(created.toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
|
||||
return YearMonth.from(defaultDate(createdAt).toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
|
||||
}
|
||||
|
||||
private String trimPreview(String text) {
|
||||
@@ -150,4 +216,12 @@ public class ChatPersistMySqlApplyService {
|
||||
}
|
||||
return text.length() <= 200 ? text : text.substring(0, 200);
|
||||
}
|
||||
|
||||
private boolean isBlank(String value) {
|
||||
return value == null || value.isBlank();
|
||||
}
|
||||
|
||||
private Date defaultDate(Date value) {
|
||||
return value == null ? new Date() : value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -105,7 +105,11 @@ public class ChatlogRuntimeListener implements ChatRuntimeListener {
|
||||
command.setDeptId(defaultNumber(context.getDeptId()));
|
||||
command.setSessionId(context.getSessionId());
|
||||
command.setUserId(defaultNumber(context.getUserId()));
|
||||
command.setUserAccount(context.getUserAccount());
|
||||
command.setAssistantId(defaultNumber(context.getAssistantId()));
|
||||
command.setAssistantCode(context.getAssistantCode());
|
||||
command.setAssistantName(context.getAssistantName());
|
||||
command.setSessionTitle(context.getSessionTitle());
|
||||
command.setSenderId(defaultNumber(message.getSenderId()));
|
||||
command.setSenderName(message.getSenderName());
|
||||
command.setSenderRole(message.getRole());
|
||||
|
||||
Reference in New Issue
Block a user