fix: matcher 遍历有设备的用户(修空表永不 seed 新用户 bug)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
SerenNoble 2026-06-28 19:08:20 +08:00
parent 674f47ad27
commit 5786f64b47
3 changed files with 32 additions and 21 deletions

View file

@ -21,4 +21,10 @@ public interface PersonaMatcherService {
* 列出全部候选角色(system_prompt 非空的全局角色池,与自动匹配同源)。 * 列出全部候选角色(system_prompt 非空的全局角色池,与自动匹配同源)。
*/ */
List<PersonaCandidateVO> listCandidatePersonas(); List<PersonaCandidateVO> listCandidatePersonas();
/**
* 对所有「有设备的非 manual 用户」各做一次匹配(seed 新用户 + 重评估已有)。
* 遍历用户(来自设备),而非已有 assignment —— 否则空表永远 seed 不了新用户。
*/
void matchAllNonManualUsers();
} }

View file

@ -53,6 +53,30 @@ public class PersonaMatcherServiceImpl implements PersonaMatcherService {
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
@Override
public void matchAllNonManualUsers() {
// 遍历「有设备的用户」(而非已有 assignment —— 否则空表永远 seed 不了新用户)
java.util.Set<Long> userIds = deviceDao.selectList(null).stream()
.map(DeviceEntity::getUserId)
.filter(java.util.Objects::nonNull)
.collect(Collectors.toSet());
log.info("[persona-match] 开始匹配,候选用户 {} 人", userIds.size());
int matched = 0;
for (Long userId : userIds) {
UserPersonaAssignmentEntity a = userPersonaAssignmentService.getByUserId(userId);
if (a != null && a.getManual() != null && a.getManual() == 1) {
continue; // 家长手动设定,跳过
}
try {
matchForUser(userId, 14, 50, 5);
matched++;
} catch (Exception ex) {
log.warn("[persona-match] user={} 失败:{}", userId, ex.getMessage());
}
}
log.info("[persona-match] 匹配完成,处理 {} 人", matched);
}
@Override @Override
public void matchForUser(Long userId, int days, int limit, int minHistory) { public void matchForUser(Long userId, int days, int limit, int minHistory) {
// 0. 取当前角色,manual=1 的用户跳过(保护手动设定,不覆盖) // 0. 取当前角色,manual=1 的用户跳过(保护手动设定,不覆盖)

View file

@ -1,20 +1,16 @@
package xiaozhi.modules.agent.task; package xiaozhi.modules.agent.task;
import java.util.List;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import xiaozhi.modules.agent.entity.UserPersonaAssignmentEntity;
import xiaozhi.modules.agent.service.PersonaMatcherService; import xiaozhi.modules.agent.service.PersonaMatcherService;
import xiaozhi.modules.agent.service.UserPersonaAssignmentService;
/** /**
* 陪伴角色周匹配任务。 * 陪伴角色周匹配任务。
* 仿 modules/knowledge/task/DocumentStatusSyncTask。 * 仿 modules/knowledge/task/DocumentStatusSyncTask。
* 每周一 03:30 跑一次;只处理非 manual 用户(家长手动设定过的跳过)。 * 每周一 03:30 跑一次;遍历有设备的非 manual 用户(seed + 重评估)。
*/ */
@Component @Component
@AllArgsConstructor @AllArgsConstructor
@ -22,27 +18,12 @@ import xiaozhi.modules.agent.service.UserPersonaAssignmentService;
public class PersonaMatchTask { public class PersonaMatchTask {
private final PersonaMatcherService personaMatcherService; private final PersonaMatcherService personaMatcherService;
private final UserPersonaAssignmentService userPersonaAssignmentService;
/** 每周一 03:30(可按需改 cron) */ /** 每周一 03:30(可按需改 cron) */
@Scheduled(cron = "0 30 3 ? * MON") @Scheduled(cron = "0 30 3 ? * MON")
public void matchAll() { public void matchAll() {
try { try {
log.info("[persona-match] 开始周匹配"); personaMatcherService.matchAllNonManualUsers();
List<UserPersonaAssignmentEntity> all = userPersonaAssignmentService.list();
int matched = 0;
for (UserPersonaAssignmentEntity a : all) {
if (a.getManual() != null && a.getManual() == 1) {
continue; // 家长手动设定,跳过
}
try {
personaMatcherService.matchForUser(a.getUserId(), 14, 50, 5);
matched++;
} catch (Exception ex) {
log.warn("[persona-match] user={} 失败:{}", a.getUserId(), ex.getMessage());
}
}
log.info("[persona-match] 周匹配完成,处理 {} 人", matched);
} catch (Exception e) { } catch (Exception e) {
log.error("[persona-match] 周匹配任务异常", e); log.error("[persona-match] 周匹配任务异常", e);
} }