AI 对话web、wx优化
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package org.xyzh.ai.service.impl;
|
||||
|
||||
import org.apache.dubbo.config.annotation.DubboReference;
|
||||
import org.apache.dubbo.config.annotation.DubboService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -13,15 +14,18 @@ import org.xyzh.ai.client.dto.DocumentUploadResponse;
|
||||
import org.xyzh.api.ai.dto.TbAgent;
|
||||
import org.xyzh.api.ai.service.AIFileUploadService;
|
||||
import org.xyzh.api.ai.service.AgentService;
|
||||
import org.xyzh.api.file.dto.TbSysFileDTO;
|
||||
import org.xyzh.api.file.service.FileService;
|
||||
import org.xyzh.common.auth.utils.LoginUtil;
|
||||
import org.xyzh.common.core.domain.ResultDomain;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @description AI文件上传服务实现(只负责与Dify交互,不处理minio和数据库)
|
||||
* @description AI文件上传服务实现(同时上传到MinIO和Dify)
|
||||
* @filename AIFileUploadServiceImpl.java
|
||||
* @author yslg
|
||||
* @copyright xyzh
|
||||
@@ -37,6 +41,9 @@ public class AIFileUploadServiceImpl implements AIFileUploadService {
|
||||
@Autowired
|
||||
private AgentService agentService;
|
||||
|
||||
@DubboReference(version = "1.0.0", group = "file", timeout = 30000, retries = 0)
|
||||
private FileService fileService;
|
||||
|
||||
// ============================ 对话文件管理 ============================
|
||||
|
||||
@Override
|
||||
@@ -56,31 +63,58 @@ public class AIFileUploadServiceImpl implements AIFileUploadService {
|
||||
}
|
||||
TbAgent agent = agentResult.getData();
|
||||
|
||||
// 3. 获取当前用户
|
||||
String userId = LoginUtil.getCurrentUserId();
|
||||
if (!StringUtils.hasText(userId)) {
|
||||
userId = "anonymous";
|
||||
}
|
||||
|
||||
File tempFile = null;
|
||||
String sysFileId = null;
|
||||
String sysFileUrl = null;
|
||||
|
||||
try {
|
||||
// 3. 将MultipartFile转换为临时File
|
||||
// 4. 上传到MinIO(通过FileService,使用字节数组方式)
|
||||
byte[] fileBytes = file.getBytes();
|
||||
String fileName = file.getOriginalFilename();
|
||||
String contentType = file.getContentType();
|
||||
ResultDomain<TbSysFileDTO> fileResult = fileService.uploadFileBytes(fileBytes, fileName, contentType, "ai-chat", agentId);
|
||||
if (fileResult.getSuccess() && fileResult.getData() != null) {
|
||||
TbSysFileDTO sysFile = fileResult.getData();
|
||||
sysFileId = sysFile.getFileId();
|
||||
sysFileUrl = sysFile.getUrl();
|
||||
logger.info("上传文件到MinIO成功: fileId={}, url={}", sysFileId, sysFileUrl);
|
||||
} else {
|
||||
logger.warn("上传文件到MinIO失败: {}", fileResult.getMessage());
|
||||
// MinIO上传失败不阻断流程,继续上传到Dify
|
||||
}
|
||||
|
||||
// 5. 将MultipartFile转换为临时File用于Dify上传
|
||||
tempFile = File.createTempFile("upload_", "_" + file.getOriginalFilename());
|
||||
file.transferTo(tempFile);
|
||||
|
||||
// 4. 获取当前用户
|
||||
String userId = LoginUtil.getCurrentUserId();
|
||||
if (!StringUtils.hasText(userId)) {
|
||||
userId = "anonymous";
|
||||
}
|
||||
|
||||
// 5. 上传到Dify
|
||||
// 6. 上传到Dify
|
||||
DifyFileInfo difyFile = difyApiClient.uploadFileForChat(tempFile, file.getOriginalFilename(), userId, agent.getApiKey());
|
||||
if (difyFile != null && StringUtils.hasText(difyFile.getId())) {
|
||||
logger.info("上传对话文件成功: agentId={}, fileId={}", agentId, difyFile.getId());
|
||||
Map<String, Object> result = new java.util.HashMap<>();
|
||||
logger.info("上传对话文件到Dify成功: agentId={}, difyFileId={}", agentId, difyFile.getId());
|
||||
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
// Dify返回的信息
|
||||
result.put("id", difyFile.getId());
|
||||
result.put("name", difyFile.getName());
|
||||
result.put("size", difyFile.getSize());
|
||||
result.put("type", difyFile.getType());
|
||||
result.put("extension", difyFile.getExtension());
|
||||
result.put("mime_type", difyFile.getMimeType());
|
||||
result.put("upload_file_id", difyFile.getUploadFileId());
|
||||
// 系统文件信息(用于前端展示和数据库存储)
|
||||
result.put("sys_file_id", sysFileId);
|
||||
result.put("preview_url", sysFileUrl);
|
||||
result.put("source_url", sysFileUrl);
|
||||
|
||||
return ResultDomain.success("上传成功", result);
|
||||
}
|
||||
return ResultDomain.failure("上传文件失败");
|
||||
return ResultDomain.failure("上传文件到Dify失败");
|
||||
} catch (Exception e) {
|
||||
logger.error("上传对话文件异常: {}", e.getMessage(), e);
|
||||
return ResultDomain.failure("上传文件异常: " + e.getMessage());
|
||||
|
||||
@@ -346,6 +346,18 @@ public class AgentChatServiceImpl implements AgentChatService {
|
||||
userMessage.setChatId(chatId);
|
||||
userMessage.setRole("user");
|
||||
userMessage.setContent(query);
|
||||
|
||||
// 提取系统文件ID列表保存到消息中
|
||||
if (filesData != null && !filesData.isEmpty()) {
|
||||
List<String> sysFileIds = filesData.stream()
|
||||
.map(DifyFileInfo::getSysFileId)
|
||||
.filter(StringUtils::hasText)
|
||||
.collect(java.util.stream.Collectors.toList());
|
||||
if (!sysFileIds.isEmpty()) {
|
||||
userMessage.setFiles(sysFileIds);
|
||||
}
|
||||
}
|
||||
|
||||
chatMessageMapper.insertChatMessage(userMessage);
|
||||
|
||||
// 5. 构建Dify请求
|
||||
|
||||
Reference in New Issue
Block a user