refactor(dify模块): 对dify模块的apikey进行了Redis缓存并对dify访问的userid进行了自动获取
This commit is contained in:
parent
a4ebc7875c
commit
1dfc8ea017
@ -2,10 +2,12 @@ package com.zsc.edu.dify;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.context.annotation.EnableAspectJAutoProxy;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableAspectJAutoProxy
|
||||
@EnableCaching
|
||||
public class DifyBackendApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
@ -1,7 +1,10 @@
|
||||
package com.zsc.edu.dify.modules.dify.controller;
|
||||
|
||||
import com.zsc.edu.dify.exception.ExceptionUtil;
|
||||
import com.zsc.edu.dify.framework.security.SecurityUtil;
|
||||
import com.zsc.edu.dify.modules.dify.service.AppEntityService;
|
||||
import com.zsc.edu.dify.modules.operationLog.entity.OperationLogAnnotation;
|
||||
import com.zsc.edu.dify.modules.system.service.UserService;
|
||||
import io.github.guoshiqiufeng.dify.chat.DifyChat;
|
||||
import io.github.guoshiqiufeng.dify.chat.dto.request.ChatMessageSendRequest;
|
||||
import io.github.guoshiqiufeng.dify.chat.dto.request.MessageConversationsRequest;
|
||||
@ -43,8 +46,9 @@ public class V1ChatController {
|
||||
@RequestBody ChatMessageSendRequest sendRequest,
|
||||
@PathVariable String appId
|
||||
) {
|
||||
sendRequest.setApiKey(appEntityService.getApikeyByRedis(appId));
|
||||
return difyChat.send(sendRequest);
|
||||
sendRequest.setApiKey(appEntityService.getApikey(appId));
|
||||
sendRequest.setUserId(SecurityUtil.getUserInfo().id.toString());
|
||||
return ExceptionUtil.difyException(()->difyChat.send(sendRequest));
|
||||
}
|
||||
|
||||
|
||||
@ -61,7 +65,7 @@ public class V1ChatController {
|
||||
@RequestBody ChatMessageSendRequest sendRequest,
|
||||
@PathVariable String appid
|
||||
) {
|
||||
sendRequest.setApiKey(appEntityService.getApikeyByRedis(appid));
|
||||
sendRequest.setApiKey(appEntityService.getApikey(appid));
|
||||
return difyChat.sendChatMessageStream(sendRequest);
|
||||
}
|
||||
|
||||
@ -77,7 +81,8 @@ public class V1ChatController {
|
||||
@RequestBody MessageConversationsRequest request,
|
||||
@PathVariable String appId
|
||||
) {
|
||||
request.setApiKey(appEntityService.getApikeyByRedis(appId));
|
||||
request.setApiKey(appEntityService.getApikey(appId));
|
||||
request.setUserId(SecurityUtil.getUserInfo().id.toString());
|
||||
return difyChat.conversations(request);
|
||||
}
|
||||
|
||||
@ -85,26 +90,26 @@ public class V1ChatController {
|
||||
* 停止流式消息
|
||||
*
|
||||
* @param taskId 任务id
|
||||
* @param userId 用户id
|
||||
*/
|
||||
@PatchMapping("/stream/stop")
|
||||
public void stopMessagesStream(@RequestParam String taskId, @RequestParam String userId, @RequestParam String appId) {
|
||||
String apiKey = appEntityService.getApikeyByRedis(appId);
|
||||
difyChat.stopMessagesStream(apiKey, taskId, userId);
|
||||
public void stopMessagesStream(@RequestParam String taskId, @RequestParam String appId) {
|
||||
String apiKey = appEntityService.getApikey(appId);
|
||||
String userId = SecurityUtil.getUserInfo().id.toString();
|
||||
difyChat.stopMessagesStream(apiKey, taskId,userId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除会话
|
||||
*
|
||||
* @param conversationId 会话id
|
||||
* @param userId 用户id
|
||||
*/
|
||||
@DeleteMapping("/conversation")
|
||||
@PreAuthorize("hasAuthority('dify:chat:delete')")
|
||||
@OperationLogAnnotation(content = "'dify对话", operationType = "删除")
|
||||
public void deleteConversation(@RequestParam String conversationId, @RequestParam String userId, @RequestParam String appId) {
|
||||
String apiKey = appEntityService.getApikeyByRedis(appId);
|
||||
difyChat.deleteConversation(conversationId, apiKey, userId);
|
||||
public void deleteConversation(@RequestParam String conversationId, @RequestParam String appId) {
|
||||
String apiKey = appEntityService.getApikey(appId);
|
||||
String userId = SecurityUtil.getUserInfo().id.toString();
|
||||
difyChat.deleteConversation(conversationId, apiKey,userId);
|
||||
}
|
||||
|
||||
|
||||
|
@ -1,8 +1,8 @@
|
||||
package com.zsc.edu.dify.modules.dify.controller;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.zsc.edu.dify.framework.security.SecurityUtil;
|
||||
import com.zsc.edu.dify.modules.dify.entity.WorkflowData;
|
||||
import com.zsc.edu.dify.modules.dify.repo.AppEntityRepository;
|
||||
import com.zsc.edu.dify.modules.dify.service.AppEntityService;
|
||||
import com.zsc.edu.dify.modules.dify.service.DifyWorkflowService;
|
||||
import com.zsc.edu.dify.modules.operationLog.entity.OperationLogAnnotation;
|
||||
@ -41,6 +41,7 @@ public class V1WorkflowController {
|
||||
@PreAuthorize("hasAuthority('dify:workflow:run')")
|
||||
@OperationLogAnnotation(content = "'dify工作流'", operationType = "运行")
|
||||
public WorkflowRunResponse runWorkflow(@RequestBody WorkflowRunRequest request, @PathVariable String appId) {
|
||||
request.setUserId(SecurityUtil.getUserInfo().id.toString());
|
||||
return difyWorkflowService.run(request, appId);
|
||||
}
|
||||
|
||||
@ -59,13 +60,13 @@ public class V1WorkflowController {
|
||||
* 停止工作流(流式)
|
||||
*
|
||||
* @param taskId
|
||||
* @param userId
|
||||
* @return
|
||||
*/
|
||||
@PatchMapping("/stop/{appId}")
|
||||
public WorkflowStopResponse stopWorkflowStream(String taskId, String userId, @PathVariable String appId) {
|
||||
String apiKey =appEntityService.getApikeyByRedis(appId);
|
||||
return difyWorkflow.stopWorkflowStream(apiKey, taskId, userId);
|
||||
public WorkflowStopResponse stopWorkflowStream(String taskId, @PathVariable String appId) {
|
||||
String apiKey =appEntityService.getApikey(appId);
|
||||
String userId = SecurityUtil.getUserInfo().id.toString();
|
||||
return difyWorkflow.stopWorkflowStream(apiKey, taskId,userId);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -77,7 +78,7 @@ public class V1WorkflowController {
|
||||
@GetMapping("/info/{appId}")
|
||||
@PreAuthorize("hasAuthority('dify:workflow:info')")
|
||||
public WorkflowInfoResponse info(String workflowRunId, @PathVariable String appId) {
|
||||
String apiKey =appEntityService.getApikeyByRedis(appId);
|
||||
String apiKey =appEntityService.getApikey(appId);
|
||||
return difyWorkflow.info(workflowRunId, apiKey);
|
||||
}
|
||||
|
||||
@ -90,7 +91,7 @@ public class V1WorkflowController {
|
||||
@PostMapping("/logs/{appId}")
|
||||
@PreAuthorize("hasAuthority('dify:workflow:log')")
|
||||
public DifyPageResult<WorkflowLogs> logs(@RequestBody WorkflowLogsRequest request, @PathVariable String appId) {
|
||||
String apiKey = appEntityService.getApikeyByRedis(appId);
|
||||
String apiKey = appEntityService.getApikey(appId);
|
||||
request.setApiKey(apiKey);
|
||||
final DifyPageResult<WorkflowLogs> logs = difyWorkflow.logs(request);
|
||||
return logs;
|
||||
|
@ -11,5 +11,5 @@ public interface AppEntityService extends IService<AppEntity> {
|
||||
|
||||
boolean enabledApp(String id);
|
||||
|
||||
String getApikeyByRedis(String appId);
|
||||
String getApikey(String appId);
|
||||
}
|
||||
|
@ -35,6 +35,11 @@ public class AppEntityServiceImpl extends ServiceImpl<AppEntityRepository, AppEn
|
||||
return addApps(difyServer.apps(mode, name));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加应用到数据库中
|
||||
* @param appsResponseList
|
||||
* @return
|
||||
*/
|
||||
public List<AppsResponseVO> addApps(List<AppsResponseVO> appsResponseList) {
|
||||
for(AppsResponseVO appsResponse : appsResponseList){
|
||||
boolean isHave = baseMapper.exists(new QueryWrapper<AppEntity>().eq("id", appsResponse.getId()));
|
||||
@ -58,6 +63,11 @@ public class AppEntityServiceImpl extends ServiceImpl<AppEntityRepository, AppEn
|
||||
return appsResponseList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 启用禁用应用
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean enabledApp(String id) {
|
||||
AppEntity appEntity = baseMapper.selectById(id);
|
||||
@ -69,9 +79,15 @@ public class AppEntityServiceImpl extends ServiceImpl<AppEntityRepository, AppEn
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据appId获取apikey并缓存至redis
|
||||
* @param appId
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
@Cacheable(cacheNames="apikey",key="#apikey")
|
||||
public String getApikeyByRedis(String appId){
|
||||
@Cacheable(cacheNames="dify:flow:apikey",key="#appId")
|
||||
public String getApikey(String appId){
|
||||
return appEntityRepository.selectApiKey(appId);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -3,8 +3,8 @@ package com.zsc.edu.dify.modules.dify.service.Impl;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.zsc.edu.dify.modules.dify.entity.WorkflowData;
|
||||
import com.zsc.edu.dify.modules.dify.mapper.WorkflowMapper;
|
||||
import com.zsc.edu.dify.modules.dify.repo.AppEntityRepository;
|
||||
import com.zsc.edu.dify.modules.dify.repo.WorkflowRepository;
|
||||
import com.zsc.edu.dify.modules.dify.service.AppEntityService;
|
||||
import com.zsc.edu.dify.modules.dify.service.DifyWorkflowService;
|
||||
import io.github.guoshiqiufeng.dify.workflow.DifyWorkflow;
|
||||
import io.github.guoshiqiufeng.dify.workflow.dto.request.WorkflowRunRequest;
|
||||
@ -17,18 +17,24 @@ import org.springframework.stereotype.Service;
|
||||
@Service
|
||||
public class DifyWorkflowServiceImpl extends ServiceImpl<WorkflowRepository, WorkflowData> implements DifyWorkflowService {
|
||||
@Resource
|
||||
AppEntityRepository appEntityRepository;
|
||||
AppEntityService appEntityService;
|
||||
@Resource
|
||||
DifyWorkflow difyWorkflow;
|
||||
@Resource
|
||||
WorkflowMapper mapper;
|
||||
|
||||
/**
|
||||
* 运行保存信息
|
||||
* @param request
|
||||
* @param appId
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public WorkflowRunResponse run(WorkflowRunRequest request, String appId){
|
||||
String apiKey = appEntityRepository.selectApiKey(appId);
|
||||
String apiKey = appEntityService.getApikey(appId);
|
||||
request.setApiKey(apiKey);
|
||||
WorkflowRunResponse response = difyWorkflow.runWorkflow(request);
|
||||
WorkflowData workflowData =mapper.toEntity(response);
|
||||
WorkflowData workflowData =mapper.toEntity(response);
|
||||
workflowData.setAppId(appId);
|
||||
baseMapper.insert(workflowData);
|
||||
return response;
|
||||
|
@ -4,6 +4,7 @@ import com.zsc.edu.dify.framework.security.UserDetailsImpl;
|
||||
import com.zsc.edu.dify.modules.system.dto.*;
|
||||
import com.zsc.edu.dify.modules.system.entity.User;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
|
@ -14,8 +14,11 @@ import com.zsc.edu.dify.modules.system.mapper.UserMapper;
|
||||
import com.zsc.edu.dify.modules.system.repo.*;
|
||||
import com.zsc.edu.dify.modules.system.service.UserService;
|
||||
import com.zsc.edu.dify.modules.system.utils.sendMail;
|
||||
import com.zsc.edu.dify.modules.system.vo.UserDetail;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
@ -169,4 +172,5 @@ public class UserServiceImpl extends ServiceImpl<UserRepository, User> implement
|
||||
userRolesRepository.insert(userRoles);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user