UserSceneSessionService.java
3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package com.xly.service;
import cn.hutool.core.util.ObjectUtil;
import com.xly.agent.ChatiAgent;
import com.xly.agent.DynamicTableNl2SqlAiAgent;
import com.xly.agent.ErpAiAgent;
import com.xly.config.OperableChatMemoryProvider;
import com.xly.entity.SceneDto;
import com.xly.entity.ToolMeta;
import com.xly.entity.UserSceneSession;
import com.xly.mapper.UserSceneSessionMapper;
import com.xly.runner.AppStartupRunner;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.stream.Collectors;
@Service("userSceneSessionService")
public class UserSceneSessionService {
// 新增核心缓存:用户场景会话状态(记录是否选场景、当前场景、权限内场景)
public static final Map<String, UserSceneSession> USER_SCENE_SESSION_CACHE = new HashMap<>();
// 原有缓存:Agent实例、会话记忆
public static final Map<String, ErpAiAgent> ERP_AGENT_CACHE = new HashMap<>();
public static final Map<String, ChatiAgent> CHAT_AGENT_CACHE = new HashMap<>();
public static final Map<String, DynamicTableNl2SqlAiAgent> ERP_DynamicTableNl2SqlAiAgent_CACHE = new HashMap<>();
@Autowired
private UserSceneSessionMapper modelMapper;
public List<Map<String, Object>> getModelAllByUserId(String sUserId){
return modelMapper.findUserPermissions(sUserId);
}
public List<ToolMeta> getModle(String sUserId, String sUserType){
List<Map<String, Object>> modleList = getModelAllByUserId(sUserId);
Set<String> dataSet = new HashSet<>();
if(ObjectUtil.isNotEmpty(modleList)){
modleList.forEach(one->{
String sKeys = one.get("sKey").toString();
String[] sIds = sKeys.split("-");
for(String sId:sIds){
if(ObjectUtil.isNotEmpty(sId)){
dataSet.add(sId);
}
}
});
}
List<String> data = new ArrayList<>(dataSet);
if("sysadmin".equals(sUserType)){
return AppStartupRunner.getAllTools();
}else {
return AppStartupRunner.getTools(data);
}
}
/***
* @Author 钱豹
* @Date 14:03 2026/2/10
* @Param []
* @return void
* @Description 清除所有记忆
**/
public void cleanAllSession(){
USER_SCENE_SESSION_CACHE.clear();
ERP_AGENT_CACHE.clear();
CHAT_AGENT_CACHE.clear();
ERP_DynamicTableNl2SqlAiAgent_CACHE.clear();
}
public UserSceneSession getUserSceneSession(String sUserId, String sUserType,String authorization){
if (USER_SCENE_SESSION_CACHE.containsKey(sUserId)) {
return USER_SCENE_SESSION_CACHE.get(sUserId);
}
UserSceneSession userSceneSession = new UserSceneSession();
List<ToolMeta> tools = getModle(sUserId,sUserType);
List<SceneDto> sceneDtos = getAiAgent(tools);
userSceneSession.setSceneSelected(false); // 初始状态:未选择场景
userSceneSession.setAuthorization(authorization);//存入用户token
userSceneSession.setUserId(sUserId);//存入用户ID
userSceneSession.setAuthTool(tools);//方法
userSceneSession.setAuthScenes(sceneDtos);//场景
// 3. 缓存会话
USER_SCENE_SESSION_CACHE.put(sUserId, userSceneSession);
return userSceneSession;
}
public List<SceneDto> getAiAgent(List<ToolMeta> tools){
List<String> aiAgentIds = new ArrayList<>();
tools.forEach(tool->{
aiAgentIds.add(tool.getSSceneId());
});
List<SceneDto> agAll = AppStartupRunner.getAiAgentCache();
return agAll.stream().filter(one-> aiAgentIds.contains(one.getSId())).collect(Collectors.toList());
}
}