fix: 角色池改用全局,下拉框列全部候选角色

This commit is contained in:
SerenNoble 2026-06-28 18:32:46 +08:00
parent 6ef80fd76f
commit 674f47ad27
6 changed files with 63 additions and 1 deletions

View file

@ -1,8 +1,10 @@
package xiaozhi.modules.agent.controller; package xiaozhi.modules.agent.controller;
import java.util.List;
import java.util.Map; import java.util.Map;
import org.apache.shiro.authz.annotation.RequiresPermissions; import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
@ -10,7 +12,9 @@ import org.springframework.web.bind.annotation.RestController;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import xiaozhi.common.utils.Result; import xiaozhi.common.utils.Result;
import xiaozhi.modules.agent.service.PersonaMatcherService;
import xiaozhi.modules.agent.service.UserPersonaAssignmentService; import xiaozhi.modules.agent.service.UserPersonaAssignmentService;
import xiaozhi.modules.agent.vo.PersonaCandidateVO;
import xiaozhi.modules.security.user.SecurityUser; import xiaozhi.modules.security.user.SecurityUser;
@RestController @RestController
@ -19,6 +23,7 @@ import xiaozhi.modules.security.user.SecurityUser;
public class UserPersonaController { public class UserPersonaController {
private final UserPersonaAssignmentService userPersonaAssignmentService; private final UserPersonaAssignmentService userPersonaAssignmentService;
private final PersonaMatcherService personaMatcherService;
/** 家长手动切换角色(立即生效,标 manual=1,自动任务不再覆盖) */ /** 家长手动切换角色(立即生效,标 manual=1,自动任务不再覆盖) */
@PostMapping("/switch") @PostMapping("/switch")
@ -41,4 +46,11 @@ public class UserPersonaController {
userPersonaAssignmentService.resetAuto(userId); userPersonaAssignmentService.resetAuto(userId);
return new Result<Void>(); return new Result<Void>();
} }
/** 候选角色列表(全局角色池,system_prompt 非空,与自动匹配同源) */
@GetMapping("/candidates")
@RequiresPermissions("sys:role:normal")
public Result<List<PersonaCandidateVO>> candidates() {
return new Result<List<PersonaCandidateVO>>().ok(personaMatcherService.listCandidatePersonas());
}
} }

View file

@ -1,5 +1,9 @@
package xiaozhi.modules.agent.service; package xiaozhi.modules.agent.service;
import java.util.List;
import xiaozhi.modules.agent.vo.PersonaCandidateVO;
public interface PersonaMatcherService { public interface PersonaMatcherService {
/** /**
@ -12,4 +16,9 @@ public interface PersonaMatcherService {
* @param minHistory 历史不足此数则不匹配(冷启动) * @param minHistory 历史不足此数则不匹配(冷启动)
*/ */
void matchForUser(Long userId, int days, int limit, int minHistory); void matchForUser(Long userId, int days, int limit, int minHistory);
/**
* 列出全部候选角色(system_prompt 非空的全局角色池,与自动匹配同源)。
*/
List<PersonaCandidateVO> listCandidatePersonas();
} }

View file

@ -22,6 +22,7 @@ import xiaozhi.modules.agent.entity.UserPersonaAssignmentEntity;
import xiaozhi.modules.agent.service.AgentChatHistoryService; import xiaozhi.modules.agent.service.AgentChatHistoryService;
import xiaozhi.modules.agent.service.PersonaMatcherService; import xiaozhi.modules.agent.service.PersonaMatcherService;
import xiaozhi.modules.agent.service.UserPersonaAssignmentService; import xiaozhi.modules.agent.service.UserPersonaAssignmentService;
import xiaozhi.modules.agent.vo.PersonaCandidateVO;
import xiaozhi.modules.device.dao.DeviceDao; import xiaozhi.modules.device.dao.DeviceDao;
import xiaozhi.modules.device.entity.DeviceEntity; import xiaozhi.modules.device.entity.DeviceEntity;
import xiaozhi.modules.llm.service.LLMService; import xiaozhi.modules.llm.service.LLMService;
@ -39,6 +40,19 @@ public class PersonaMatcherServiceImpl implements PersonaMatcherService {
@Autowired private LLMService llmService; @Autowired private LLMService llmService;
@Autowired private UserPersonaAssignmentService userPersonaAssignmentService; @Autowired private UserPersonaAssignmentService userPersonaAssignmentService;
@Override
public List<PersonaCandidateVO> listCandidatePersonas() {
return agentDao.selectList(null).stream()
.filter(a -> a.getSystemPrompt() != null && !a.getSystemPrompt().isBlank())
.map(a -> {
PersonaCandidateVO vo = new PersonaCandidateVO();
vo.setId(a.getId());
vo.setAgentName(a.getAgentName());
return vo;
})
.collect(Collectors.toList());
}
@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

@ -0,0 +1,12 @@
package xiaozhi.modules.agent.vo;
import lombok.Data;
/**
* 候选角色 VO(system_prompt 非空的全局角色,与自动匹配器同源)
*/
@Data
public class PersonaCandidateVO {
private String id;
private String agentName;
}

View file

@ -2,6 +2,21 @@ import { getServiceUrl } from '../api';
import RequestService from '../httpRequest'; import RequestService from '../httpRequest';
export default { export default {
// 候选角色列表(全局角色池,与自动匹配同源)
candidates(callback) {
RequestService.sendRequest()
.url(`${getServiceUrl()}/persona/candidates`)
.method('GET')
.success((res) => {
RequestService.clearRequestTime();
callback(res);
})
.networkFail(() => {
RequestService.reAjaxFun(() => {
this.candidates(callback);
});
}).send();
},
// 手动切换角色(标 manual=1,自动任务不再覆盖) // 手动切换角色(标 manual=1,自动任务不再覆盖)
switchPersona(agentId, callback) { switchPersona(agentId, callback) {
RequestService.sendRequest() RequestService.sendRequest()

View file

@ -123,7 +123,7 @@ export default {
this.$router.push({ path: '/device-management', query: { agentId: this.device.agentId } }); this.$router.push({ path: '/device-management', query: { agentId: this.device.agentId } });
}, },
fetchPersonaOptions() { fetchPersonaOptions() {
Api.agent.getAgentList(({ data }) => { Api.persona.candidates(({ data }) => {
if (data?.data) { if (data?.data) {
this.personaOptions = data.data.map(item => ({ id: item.id, agentName: item.agentName })); this.personaOptions = data.data.map(item => ({ id: item.id, agentName: item.agentName }));
} }