This commit is contained in:
2025-08-19 08:48:26 +08:00

View File

@@ -93,6 +93,7 @@ public class AiChatController extends BaseController {
.writeTimeout(Duration.ofSeconds(12)) // 写超时 .writeTimeout(Duration.ofSeconds(12)) // 写超时
.retryOnConnectionFailure(true) .retryOnConnectionFailure(true)
.build(); .build();
/** 为本次请求设置 “总超时”(含连接/读写),避免无限挂起 */ /** 为本次请求设置 “总超时”(含连接/读写),避免无限挂起 */
private Response execWithTimeouts(Request req, int callSecs, int readSecs, int writeSecs) throws IOException { private Response execWithTimeouts(Request req, int callSecs, int readSecs, int writeSecs) throws IOException {
OkHttpClient shortClient = client.newBuilder() OkHttpClient shortClient = client.newBuilder()
@@ -173,7 +174,8 @@ public class AiChatController extends BaseController {
* @param currentUsername * @param currentUsername
* @throws IOException 当网络请求或IO操作失败时抛出 * @throws IOException 当网络请求或IO操作失败时抛出
*/ */
private void sendToDifyAndStream(Map<String, Object> requestData, SseEmitter emitter, String currentUsername) throws IOException { private void sendToDifyAndStream(Map<String, Object> requestData, SseEmitter emitter, String currentUsername)
throws IOException {
// 构建请求体参数 // 构建请求体参数
Map<String, Object> bodyMap = new HashMap<>(); Map<String, Object> bodyMap = new HashMap<>();
@@ -258,7 +260,8 @@ public class AiChatController extends BaseController {
try (BufferedReader reader = new BufferedReader(httpResponse.body().charStream())) { try (BufferedReader reader = new BufferedReader(httpResponse.body().charStream())) {
String line; String line;
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
if (line.isEmpty() || !line.startsWith("data:")) continue; if (line.isEmpty() || !line.startsWith("data:"))
continue;
String jsonData = line.substring(5).trim(); String jsonData = line.substring(5).trim();
if ("[DONE]".equals(jsonData)) { if ("[DONE]".equals(jsonData)) {
break; break;
@@ -313,7 +316,8 @@ public class AiChatController extends BaseController {
* @return 统一响应结果 * @return 统一响应结果
*/ */
@PostMapping("/feedback") @PostMapping("/feedback")
public AjaxResult submitFeedback(@org.springframework.web.bind.annotation.RequestBody Map<String, Object> feedbackData) { public AjaxResult submitFeedback(
@org.springframework.web.bind.annotation.RequestBody Map<String, Object> feedbackData) {
// 校验必要字段 // 校验必要字段
String messageId = (String) feedbackData.get("message_id"); String messageId = (String) feedbackData.get("message_id");
if (messageId == null || messageId.trim().isEmpty()) { if (messageId == null || messageId.trim().isEmpty()) {
@@ -357,8 +361,7 @@ public class AiChatController extends BaseController {
String jsonBody = mapper.writeValueAsString(bodyMap); String jsonBody = mapper.writeValueAsString(bodyMap);
okhttp3.RequestBody body = okhttp3.RequestBody.create( okhttp3.RequestBody body = okhttp3.RequestBody.create(
MediaType.get("application/json; charset=utf-8"), MediaType.get("application/json; charset=utf-8"),
jsonBody jsonBody);
);
// 调用 Dify API // 调用 Dify API
Request request = new Request.Builder() Request request = new Request.Builder()
@@ -431,12 +434,19 @@ public class AiChatController extends BaseController {
// 提取反馈信息 // 提取反馈信息
feedbackItem.put("id", feedbackNode.has("id") ? feedbackNode.get("id").asText() : null); feedbackItem.put("id", feedbackNode.has("id") ? feedbackNode.get("id").asText() : null);
feedbackItem.put("message_id", feedbackNode.has("message_id") ? feedbackNode.get("message_id").asText() : null); feedbackItem.put("message_id",
feedbackItem.put("rating", feedbackNode.has("rating") ? feedbackNode.get("rating").asText() : null); feedbackNode.has("message_id") ? feedbackNode.get("message_id").asText() : null);
feedbackItem.put("content", feedbackNode.has("content") ? feedbackNode.get("content").asText() : null); feedbackItem.put("rating",
feedbackItem.put("created_at", feedbackNode.has("created_at") ? feedbackNode.get("created_at").asLong() : null); feedbackNode.has("rating") ? feedbackNode.get("rating").asText() : null);
feedbackItem.put("app_id", feedbackNode.has("app_id") ? feedbackNode.get("app_id").asText() : null); feedbackItem.put("content",
feedbackItem.put("conversation_id", feedbackNode.has("conversation_id") ? feedbackNode.get("conversation_id").asText() : null); feedbackNode.has("content") ? feedbackNode.get("content").asText() : null);
feedbackItem.put("created_at",
feedbackNode.has("created_at") ? feedbackNode.get("created_at").asLong() : null);
feedbackItem.put("app_id",
feedbackNode.has("app_id") ? feedbackNode.get("app_id").asText() : null);
feedbackItem.put("conversation_id",
feedbackNode.has("conversation_id") ? feedbackNode.get("conversation_id").asText()
: null);
feedbackList.add(feedbackItem); feedbackList.add(feedbackItem);
} }
@@ -509,6 +519,7 @@ public class AiChatController extends BaseController {
try { try {
String user = SecurityUtils.getLoginUser().getUsername(); String user = SecurityUtils.getLoginUser().getUsername();
List<ConversationDTO> conversations = getConversations(user); List<ConversationDTO> conversations = getConversations(user);
if (conversations == null || conversations.isEmpty()) { if (conversations == null || conversations.isEmpty()) {
@@ -516,6 +527,7 @@ public class AiChatController extends BaseController {
} }
String conversation_id = conversations.get(0).getId(); String conversation_id = conversations.get(0).getId();
Map<String, Object> result = getConversationHistoryMessages(conversation_id, user, firstId, limit); Map<String, Object> result = getConversationHistoryMessages(conversation_id, user, firstId, limit);
AjaxResult successResult = success(result); AjaxResult successResult = success(result);
@@ -529,11 +541,11 @@ public class AiChatController extends BaseController {
} }
} }
/* /*
* 获取会话列表 * 获取会话列表
* */ */
public List<ConversationDTO> getConversations(String user, String lastId, int limit, String sortBy) throws IOException { public List<ConversationDTO> getConversations(String user, String lastId, int limit, String sortBy)
throws IOException {
// 构建带查询参数的 URL // 构建带查询参数的 URL
HttpUrl.Builder urlBuilder = HttpUrl.parse(DIFY_CONVERSATIONS_URL).newBuilder(); HttpUrl.Builder urlBuilder = HttpUrl.parse(DIFY_CONVERSATIONS_URL).newBuilder();
urlBuilder.addQueryParameter("user", user); urlBuilder.addQueryParameter("user", user);
@@ -599,7 +611,6 @@ public class AiChatController extends BaseController {
return getConversations(user, null, limit, sortBy); return getConversations(user, null, limit, sortBy);
} }
// 获取历史消息 // 获取历史消息
private Map<String, Object> getConversationHistoryMessages( private Map<String, Object> getConversationHistoryMessages(
String conversationId, String conversationId,
@@ -688,11 +699,6 @@ public class AiChatController extends BaseController {
return result; return result;
} }
/** /**
* 文件上传接口 * 文件上传接口
* <p> * <p>