refactor(dify): 重构 Dify 相关服务和控制器- 重命名 DifyServerService 为 AppEntityService

- 更新相关实现类和控制器中的引用- 新增 AppEntityService 中的 getApikeyByRedis 方法
- 修改 V1ChatController、V1DatasetController、V1ServerController 和 V1WorkflowController 中的相关调用
This commit is contained in:
zhuangtianxiang 2025-05-08 17:00:06 +08:00
parent 9626d39c19
commit a4ebc7875c
6 changed files with 39 additions and 32 deletions

View File

@ -1,6 +1,6 @@
package com.zsc.edu.dify.modules.dify.controller;
import com.zsc.edu.dify.modules.dify.repo.AppEntityRepository;
import com.zsc.edu.dify.modules.dify.service.AppEntityService;
import com.zsc.edu.dify.modules.operationLog.entity.OperationLogAnnotation;
import io.github.guoshiqiufeng.dify.chat.DifyChat;
import io.github.guoshiqiufeng.dify.chat.dto.request.ChatMessageSendRequest;
@ -27,7 +27,7 @@ public class V1ChatController {
@Resource
private DifyChat difyChat;
@Resource
private AppEntityRepository appEntityRepository;
private AppEntityService appEntityService;
/**
* 发送消息
@ -36,15 +36,14 @@ public class V1ChatController {
* 用户 id可以改为从上下文token获取
* apikey 建议在数据库进行存储前端调用时传智能体 id从数据库查询
*/
@PostMapping("/completions/{appid}")
@PostMapping("/completions/{appId}")
@PreAuthorize("hasAuthority('dify:chat:query')")
@OperationLogAnnotation(content = "'dify对话'", operationType = "发送")
public ChatMessageSendResponse sendChatMessage(
@RequestBody ChatMessageSendRequest sendRequest,
@PathVariable String appid
@PathVariable String appId
) {
String apiKey = appEntityRepository.selectApiKey(appid);
sendRequest.setApiKey(apiKey);
sendRequest.setApiKey(appEntityService.getApikeyByRedis(appId));
return difyChat.send(sendRequest);
}
@ -62,8 +61,7 @@ public class V1ChatController {
@RequestBody ChatMessageSendRequest sendRequest,
@PathVariable String appid
) {
String apiKey = appEntityRepository.selectApiKey(appid);
sendRequest.setApiKey(apiKey);
sendRequest.setApiKey(appEntityService.getApikeyByRedis(appid));
return difyChat.sendChatMessageStream(sendRequest);
}
@ -73,14 +71,13 @@ public class V1ChatController {
* @param request 请求参数
* @return 会话列表
*/
@PostMapping("/conversations/{appid}")
@PostMapping("/conversations/{appId}")
@PreAuthorize("hasAuthority('dify:chat:query')")
public DifyPageResult<MessageConversationsResponse> conversations(
@RequestBody MessageConversationsRequest request,
@PathVariable String appid
@PathVariable String appId
) {
String apiKey = appEntityRepository.selectApiKey(appid);
request.setApiKey(apiKey);
request.setApiKey(appEntityService.getApikeyByRedis(appId));
return difyChat.conversations(request);
}
@ -91,8 +88,8 @@ public class V1ChatController {
* @param userId 用户id
*/
@PatchMapping("/stream/stop")
public void stopMessagesStream(@RequestParam String taskId, @RequestParam String userId, @RequestParam String appid) {
String apiKey = appEntityRepository.selectApiKey(appid);
public void stopMessagesStream(@RequestParam String taskId, @RequestParam String userId, @RequestParam String appId) {
String apiKey = appEntityService.getApikeyByRedis(appId);
difyChat.stopMessagesStream(apiKey, taskId, userId);
}
@ -105,8 +102,10 @@ public class V1ChatController {
@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 = appEntityRepository.selectApiKey(appid);
public void deleteConversation(@RequestParam String conversationId, @RequestParam String userId, @RequestParam String appId) {
String apiKey = appEntityService.getApikeyByRedis(appId);
difyChat.deleteConversation(conversationId, apiKey, userId);
}
}

View File

@ -12,7 +12,6 @@ import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
//TODO 操作日志
@RestController
@RequestMapping("/api/v1/dataset")
public class V1DatasetController {
@ -35,7 +34,6 @@ public class V1DatasetController {
return difyDataset.page(request);
}
//TODO 接口报错
/**
* 通过文件创建文档
*

View File

@ -1,16 +1,14 @@
package com.zsc.edu.dify.modules.dify.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zsc.edu.dify.modules.dify.entity.AppEntity;
import com.zsc.edu.dify.modules.dify.service.DifyServerService;
import com.zsc.edu.dify.modules.dify.service.AppEntityService;
import com.zsc.edu.dify.modules.operationLog.entity.OperationLogAnnotation;
import io.github.guoshiqiufeng.dify.server.DifyServer;
import io.github.guoshiqiufeng.dify.server.dto.response.ApiKeyResponseVO;
import io.github.guoshiqiufeng.dify.server.dto.response.AppsResponseVO;
import io.github.guoshiqiufeng.dify.server.dto.response.DatasetApiKeyResponseVO;
import jakarta.annotation.Resource;
import org.apache.ibatis.annotations.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
@ -23,7 +21,7 @@ public class V1ServerController {
@Resource
private DifyServer difyServer;
@Resource
private DifyServerService difyServerService;
private AppEntityService appEntityService;
/**
* 获取应用列表
@ -34,7 +32,7 @@ public class V1ServerController {
@GetMapping("/apps")
@PreAuthorize("hasAuthority('dify:server:query')")
public List<AppsResponseVO> getApps(String mode, String name) {
return difyServerService.getApps(mode, name);
return appEntityService.getApps(mode, name);
}
/**
@ -100,7 +98,7 @@ public class V1ServerController {
@PreAuthorize("hasAuthority('dify:server:update')")
@OperationLogAnnotation(content = "'dify服务启用状态'", operationType = "更新")
public boolean enabledApp(@PathVariable String id) {
return difyServerService.enabledApp(id);
return appEntityService.enabledApp(id);
}
/**
@ -112,7 +110,7 @@ public class V1ServerController {
public List<AppEntity> getEnableApps() {
LambdaQueryWrapper<AppEntity> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(AppEntity::isEnabled, true);
return difyServerService.list(queryWrapper);
return appEntityService.list(queryWrapper);
}
}

View File

@ -3,6 +3,7 @@ package com.zsc.edu.dify.modules.dify.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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;
import io.github.guoshiqiufeng.dify.core.pojo.DifyPageResult;
@ -25,7 +26,7 @@ public class V1WorkflowController {
private DifyWorkflow difyWorkflow;
@Resource
private AppEntityRepository appEntityRepository;
private AppEntityService appEntityService;
@Resource
private DifyWorkflowService difyWorkflowService;
@ -63,7 +64,7 @@ public class V1WorkflowController {
*/
@PatchMapping("/stop/{appId}")
public WorkflowStopResponse stopWorkflowStream(String taskId, String userId, @PathVariable String appId) {
String apiKey = appEntityRepository.selectApiKey(appId);
String apiKey =appEntityService.getApikeyByRedis(appId);
return difyWorkflow.stopWorkflowStream(apiKey, taskId, userId);
}
@ -76,7 +77,7 @@ public class V1WorkflowController {
@GetMapping("/info/{appId}")
@PreAuthorize("hasAuthority('dify:workflow:info')")
public WorkflowInfoResponse info(String workflowRunId, @PathVariable String appId) {
String apiKey = appEntityRepository.selectApiKey(appId);
String apiKey =appEntityService.getApikeyByRedis(appId);
return difyWorkflow.info(workflowRunId, apiKey);
}
@ -89,7 +90,7 @@ public class V1WorkflowController {
@PostMapping("/logs/{appId}")
@PreAuthorize("hasAuthority('dify:workflow:log')")
public DifyPageResult<WorkflowLogs> logs(@RequestBody WorkflowLogsRequest request, @PathVariable String appId) {
String apiKey = appEntityRepository.selectApiKey(appId);
String apiKey = appEntityService.getApikeyByRedis(appId);
request.setApiKey(apiKey);
final DifyPageResult<WorkflowLogs> logs = difyWorkflow.logs(request);
return logs;

View File

@ -6,8 +6,10 @@ import io.github.guoshiqiufeng.dify.server.dto.response.AppsResponseVO;
import java.util.List;
public interface DifyServerService extends IService<AppEntity> {
public interface AppEntityService extends IService<AppEntity> {
List<AppsResponseVO> getApps(String mode, String name);
boolean enabledApp(String id);
String getApikeyByRedis(String appId);
}

View File

@ -6,12 +6,13 @@ import com.zsc.edu.dify.exception.ConstraintException;
import com.zsc.edu.dify.modules.dify.entity.AppEntity;
import com.zsc.edu.dify.modules.dify.mapper.AppEntityMapper;
import com.zsc.edu.dify.modules.dify.repo.AppEntityRepository;
import com.zsc.edu.dify.modules.dify.service.DifyServerService;
import com.zsc.edu.dify.modules.dify.service.AppEntityService;
import io.github.guoshiqiufeng.dify.server.DifyServer;
import io.github.guoshiqiufeng.dify.server.dto.response.ApiKeyResponseVO;
import io.github.guoshiqiufeng.dify.server.dto.response.AppsResponseVO;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.*;
@ -21,11 +22,13 @@ import java.util.*;
*/
@AllArgsConstructor
@Service
public class DifyServerServiceImpl extends ServiceImpl<AppEntityRepository, AppEntity> implements DifyServerService {
public class AppEntityServiceImpl extends ServiceImpl<AppEntityRepository, AppEntity> implements AppEntityService {
@Resource
private AppEntityMapper appEntityMapper;
@Resource
private DifyServer difyServer;
@Resource
private AppEntityRepository appEntityRepository;
@Override
public List<AppsResponseVO> getApps(String mode, String name) {
@ -65,4 +68,10 @@ public class DifyServerServiceImpl extends ServiceImpl<AppEntityRepository, AppE
baseMapper.updateById(appEntity);
return true;
}
@Override
@Cacheable(cacheNames="apikey",key="#apikey")
public String getApikeyByRedis(String appId){
return appEntityRepository.selectApiKey(appId);
}
}