feat:隔离用户空间

This commit is contained in:
Ttt
2026-03-13 23:19:15 +08:00
parent fa93d4ffe0
commit 35dca0a435
11 changed files with 60 additions and 43 deletions

View File

@@ -14,8 +14,8 @@ public class ChatController {
private final BaseAgent baseAgent; private final BaseAgent baseAgent;
@GetMapping("/chat") @GetMapping("/chat")
public String chatDemo(@RequestParam("question") String question, @RequestParam("id") Long id) { public String chatDemo(@RequestParam("question") String question, @RequestParam("memoryId") Long memoryId, @RequestParam("userId") Long userId) {
baseAgent.chat(ModelTypeEnum.OPEN_AI, id, question); baseAgent.chat(ModelTypeEnum.OPEN_AI, memoryId, userId, question);
return "321"; return "321";
} }

View File

@@ -1,16 +1,14 @@
spring: spring:
datasource: datasource:
url: jdbc:mysql://rm-bp15t34gqx62jm069ro.mysql.rds.aliyuncs.com:3306/xservice-ai-center?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&allowMultiQueries=true url: jdbc:mysql://rm-bp15t34gqx62jm069ro.mysql.rds.aliyuncs.com:3306/xservice-ai-center?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&allowMultiQueries=true
username: root username: root
password: xb#UWqnhH24&XpX password: sdkljfikdfn@123
driver-class-name: com.mysql.cj.jdbc.Driver driver-class-name: com.mysql.cj.jdbc.Driver
ai:
dashscope: ai:
api-key: sk-07353fd191074c9c930b134230ba88ea openai:
chat: configs:
options: bai-lian:
model: qwen-plus apiKey: sk-70cb426d7d1e4b54b4ffe71022e7d815
http: modelName: qwen3.5-plus
connect-timeout: 30s baseUrl: https://dashscope.aliyuncs.com/compatible-mode/v1
read-timeout: 60s

View File

@@ -1,16 +1,14 @@
spring: spring:
datasource: datasource:
url: jdbc:mysql://rm-bp15t34gqx62jm069ro.mysql.rds.aliyuncs.com:3306/xservice-ai-center?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&allowMultiQueries=true url: jdbc:mysql://120.27.153.87:3306/xe-service-ai?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&allowMultiQueries=true
username: root username: root
password: xb#UWqnhH24&XpX password: sdkljfikdfn@123
driver-class-name: com.mysql.cj.jdbc.Driver driver-class-name: com.mysql.cj.jdbc.Driver
ai:
dashscope: ai:
api-key: sk-07353fd191074c9c930b134230ba88ea openai:
chat: configs:
options: bai-lian:
model: qwen-plus apiKey: sk-70cb426d7d1e4b54b4ffe71022e7d815
http: modelName: qwen3.5-plus
connect-timeout: 30s baseUrl: https://dashscope.aliyuncs.com/compatible-mode/v1
read-timeout: 60s

View File

@@ -1,5 +1,4 @@
spring: spring:
application: application:
name: xservice-ai-center name: xservice-ai-center

View File

@@ -3,5 +3,12 @@ package com.xiang.xservice.ai.agent;
import com.xiang.xservice.ai.core.enums.ModelTypeEnum; import com.xiang.xservice.ai.core.enums.ModelTypeEnum;
public interface BaseAgent { public interface BaseAgent {
void chat(ModelTypeEnum modelType, Long memoryId, String message); /**
* chat
* @param modelType 模型枚举
* @param memoryId
* @param userId
* @param message
*/
void chat(ModelTypeEnum modelType, Long memoryId, Long userId, String message);
} }

View File

@@ -32,16 +32,17 @@ public class SimpleChatAgent implements BaseAgent {
@Override @Override
public void chat(ModelTypeEnum modelType, Long id, String message) { public void chat(ModelTypeEnum modelType, Long memoryId, Long userId, String message) {
DbPersistentStore store = new DbPersistentStore(aiSimpleChatMessageManage); DbPersistentStore store = new DbPersistentStore(aiSimpleChatMessageManage);
ChatMemoryProvider chatMemoryProvider = memoryId -> MessageWindowChatMemory.builder() ChatMemoryProvider chatMemoryProvider = args -> MessageWindowChatMemory.builder()
.id(memoryId) .id(memoryId)
.maxMessages(10) .maxMessages(10)
.chatMemoryStore(store) .chatMemoryStore(store)
.build(); .build();
OpenAIConfig openAIConfig = openAIBaseConfig.getConfigs().get("bai-lian"); OpenAIConfig openAIConfig = openAIBaseConfig.getConfigs().get("bai-lian");
ModelConfig modelConfig = ModelConfig.builder() ModelConfig modelConfig = ModelConfig.builder()
.baseUrl(openAIConfig.getBaseUrl()) .baseUrl(openAIConfig.getBaseUrl())
@@ -60,6 +61,7 @@ public class SimpleChatAgent implements BaseAgent {
.build(); .build();
UserMessage userMessage = new UserMessage(message); UserMessage userMessage = new UserMessage(message);
SystemMessage systemMessage = new SystemMessage("你是一个能够与人聊天的AI智能助手名字叫龙虾"); SystemMessage systemMessage = new SystemMessage("你是一个能够与人聊天的AI智能助手名字叫龙虾");
String id = userId + "-" + memoryId;
assistant.chat(id, Lists.newArrayList(systemMessage, userMessage)) assistant.chat(id, Lists.newArrayList(systemMessage, userMessage))
.onPartialResponse(System.out::print) .onPartialResponse(System.out::print)
.onCompleteResponse(res -> System.out.println("\n完成")) .onCompleteResponse(res -> System.out.println("\n完成"))

View File

@@ -22,8 +22,12 @@ public class DbPersistentStore implements ChatMemoryStore {
private final IAiSimpleChatMessageManage aiSimpleChatMessageManage; private final IAiSimpleChatMessageManage aiSimpleChatMessageManage;
@Override @Override
public List<ChatMessage> getMessages(Object memoryId) { public List<ChatMessage> getMessages(Object args) {
List<AiSimpleChatMessageDO> msg = aiSimpleChatMessageManage.getMsgByMemoryId((Long) memoryId); String argsStr = String.valueOf(args);
String[] split = argsStr.split("-");
Long userId = Long.valueOf(split[0]);
Long memoryId = Long.valueOf(split[1]);
List<AiSimpleChatMessageDO> msg = aiSimpleChatMessageManage.getMsgByMemoryId(userId, memoryId);
if (CollectionUtils.isEmpty(msg)) { if (CollectionUtils.isEmpty(msg)) {
return Lists.newArrayList(); return Lists.newArrayList();
} }
@@ -52,7 +56,11 @@ public class DbPersistentStore implements ChatMemoryStore {
} }
@Override @Override
public void updateMessages(Object memoryId, List<ChatMessage> list) { public void updateMessages(Object args, List<ChatMessage> list) {
String argsStr = String.valueOf(args);
String[] split = argsStr.split("-");
Long userId = Long.valueOf(split[0]);
Long memoryId = Long.valueOf(split[1]);
List<AiSimpleChatMessageDO> result = Lists.newArrayList(); List<AiSimpleChatMessageDO> result = Lists.newArrayList();
for (ChatMessage chatMessage : list) { for (ChatMessage chatMessage : list) {
StringBuilder str = new StringBuilder(); StringBuilder str = new StringBuilder();
@@ -77,8 +85,8 @@ public class DbPersistentStore implements ChatMemoryStore {
} }
AiSimpleChatMessageDO message = AiSimpleChatMessageDO.builder() AiSimpleChatMessageDO message = AiSimpleChatMessageDO.builder()
.userId(1L) .userId(userId)
.memoryId((Long) memoryId) .memoryId(memoryId)
.message(str.toString()) .message(str.toString())
.createTime(LocalDateTime.now()) .createTime(LocalDateTime.now())
.delFlag(0) .delFlag(0)
@@ -91,7 +99,11 @@ public class DbPersistentStore implements ChatMemoryStore {
} }
@Override @Override
public void deleteMessages(Object memoryId) { public void deleteMessages(Object args) {
aiSimpleChatMessageManage.deleteByMemoryId((Long) memoryId); String argsStr = String.valueOf(args);
String[] split = argsStr.split("-");
Long userId = Long.valueOf(split[0]);
Long memoryId = Long.valueOf(split[1]);
aiSimpleChatMessageManage.deleteByMemoryId(userId, memoryId);
} }
} }

View File

@@ -12,15 +12,16 @@ import java.util.List;
@Service @Service
public class AiSimpleChatMessageManageImpl extends ServiceImpl<IAiSimpleChatMessageMapper, AiSimpleChatMessageDO> implements IAiSimpleChatMessageManage { public class AiSimpleChatMessageManageImpl extends ServiceImpl<IAiSimpleChatMessageMapper, AiSimpleChatMessageDO> implements IAiSimpleChatMessageManage {
@Override @Override
public List<AiSimpleChatMessageDO> getMsgByMemoryId(Long memoryId) { public List<AiSimpleChatMessageDO> getMsgByMemoryId(Long userId, Long memoryId) {
LambdaQueryWrapper<AiSimpleChatMessageDO> lambdaQueryWrapper = Wrappers.lambdaQuery(); LambdaQueryWrapper<AiSimpleChatMessageDO> lambdaQueryWrapper = Wrappers.lambdaQuery();
lambdaQueryWrapper.eq(AiSimpleChatMessageDO::getUserId, userId);
lambdaQueryWrapper.eq(AiSimpleChatMessageDO::getMemoryId, memoryId); lambdaQueryWrapper.eq(AiSimpleChatMessageDO::getMemoryId, memoryId);
lambdaQueryWrapper.eq(AiSimpleChatMessageDO::getDelFlag, 0); lambdaQueryWrapper.eq(AiSimpleChatMessageDO::getDelFlag, 0);
return baseMapper.selectList(lambdaQueryWrapper); return baseMapper.selectList(lambdaQueryWrapper);
} }
@Override @Override
public boolean deleteByMemoryId(Long memoryId) { public boolean deleteByMemoryId(Long userId, Long memoryId) {
return baseMapper.deleteByMemoryId(memoryId) > 0; return baseMapper.deleteByMemoryId(userId, memoryId) > 0;
} }
} }

View File

@@ -6,6 +6,6 @@ import com.xiang.xservice.ai.pojo.entity.AiSimpleChatMessageDO;
import java.util.List; import java.util.List;
public interface IAiSimpleChatMessageManage extends IService<AiSimpleChatMessageDO> { public interface IAiSimpleChatMessageManage extends IService<AiSimpleChatMessageDO> {
List<AiSimpleChatMessageDO> getMsgByMemoryId(Long memoryId); List<AiSimpleChatMessageDO> getMsgByMemoryId(Long userId, Long memoryId);
boolean deleteByMemoryId(Long memoryId); boolean deleteByMemoryId(Long userId, Long memoryId);
} }

View File

@@ -9,6 +9,6 @@ import org.springframework.stereotype.Repository;
@Mapper @Mapper
@Repository @Repository
public interface IAiSimpleChatMessageMapper extends BaseMapper<AiSimpleChatMessageDO> { public interface IAiSimpleChatMessageMapper extends BaseMapper<AiSimpleChatMessageDO> {
int deleteByMemoryId(@Param("id") Long memoryId); int deleteByMemoryId(@Param("userId") Long userId, @Param("memoryId") Long memoryId);
} }

View File

@@ -24,7 +24,7 @@
</sql> </sql>
<update id="deleteByMemoryId"> <update id="deleteByMemoryId">
update ai_simple_chat_message set del_flag = 1 where memory_id = #{id} update ai_simple_chat_message set del_flag = 1 where memory_id = #{memoryId} and user_id = #{userId}
</update> </update>
</mapper> </mapper>