Skip to content

Commit c821c2f

Browse files
workwindjoey-zhou
authored andcommitted
debug
1 parent af454c2 commit c821c2f

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

src/main/java/com/xiaozhi/dialogue/llm/tool/function/ChangeRoleFunction.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.xiaozhi.common.web.PageFilter;
44
import com.xiaozhi.communication.common.ChatSession;
5+
import com.xiaozhi.dialogue.llm.memory.ChatMemory;
6+
import com.xiaozhi.dialogue.llm.memory.Conversation;
57
import com.xiaozhi.dialogue.llm.tool.ToolCallStringResultConverter;
68
import com.xiaozhi.dialogue.llm.tool.ToolsGlobalRegistry;
79
import com.xiaozhi.entity.SysDevice;
@@ -32,6 +34,8 @@ public class ChangeRoleFunction implements ToolsGlobalRegistry.GlobalFunction {
3234
private SysRoleService sysRoleService;
3335
@Resource
3436
private SysDeviceService sysDeviceService;
37+
@Resource
38+
ChatMemory chatMemory;
3539

3640
@Override
3741
public ToolCallback getFunctionCallTool(ChatSession chatSession) {
@@ -46,13 +50,18 @@ public ToolCallback getFunctionCallTool(ChatSession chatSession) {
4650
String roleName = params.get("roleName");
4751
try{
4852
// 获取参数
49-
Optional<Integer> role_id = roleList.stream()
53+
Optional<SysRole> changedRole = roleList.stream()
5054
.filter(role -> role.getRoleName().equals(roleName))
51-
.findFirst().map(SysRole::getRoleId);
55+
.findFirst();
56+
5257

53-
if(role_id.isPresent()){
54-
sysDevice.setRoleId(role_id.get());//测试,固定角色
58+
if(changedRole.isPresent()){
59+
SysRole role = changedRole.get();
60+
sysDevice.setRoleId(role.getRoleId());//测试,固定角色
5561
sysDeviceService.update(sysDevice);
62+
// TODO 切换了角色,需要更换Conversation
63+
Conversation conversation = chatMemory.initConversation(sysDevice, role, chatSession.getSessionId());
64+
chatSession.setConversation( conversation);
5665
return "角色已切换至" + roleName;
5766
}else{
5867
return "角色切换失败, 没有对应角色哦";

src/main/java/com/xiaozhi/service/impl/SysDeviceServiceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public int update(SysDevice device) {
182182
device = deviceMapper.selectDeviceById(device.getDeviceId());
183183
ChatSession session = sessionManager.getSessionByDeviceId(device.getDeviceId());
184184
if (session != null) {
185-
session.setConversation(null);
185+
186186
session.setSysDevice(device);
187187
}
188188
return rows;

0 commit comments

Comments
 (0)