From 06976f5ef0087a7fa9a3e3e14f04d5b6e140a140 Mon Sep 17 00:00:00 2001 From: firefly <1633489380@qq.com> Date: Wed, 13 Aug 2025 16:50:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E4=BA=86=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E5=AD=A6=E5=8F=B7=E5=AF=B9=E5=BA=94=E4=B8=80=E6=9D=A1=E5=AF=B9?= =?UTF-8?q?=E8=AF=9Did?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/aitutor/AiChatController.java | 87 +++++-------------- 1 file changed, 20 insertions(+), 67 deletions(-) diff --git a/srs-admin/src/main/java/com/srs/web/controller/aitutor/AiChatController.java b/srs-admin/src/main/java/com/srs/web/controller/aitutor/AiChatController.java index 4f9b5f4..a040f79 100644 --- a/srs-admin/src/main/java/com/srs/web/controller/aitutor/AiChatController.java +++ b/srs-admin/src/main/java/com/srs/web/controller/aitutor/AiChatController.java @@ -160,8 +160,21 @@ public class AiChatController extends BaseController { bodyMap.put("user", currentUsername); // 用户标识 bodyMap.put("response_mode", "streaming"); // 设置为流式响应模式 - // 如果存在对话ID,则添加到请求参数中 - if (requestData.containsKey("conversation_id")) { + // 如果请求中没有提供conversation_id,则尝试获取或创建一个 + if (!requestData.containsKey("conversation_id") || requestData.get("conversation_id") == null) { + try { + List conversations = getConversations(currentUsername, null, 1, "-updated_at"); + if (conversations != null && !conversations.isEmpty()) { + // 使用已有的会话ID + bodyMap.put("conversation_id", conversations.get(0).getId()); + } + // 如果没有会话,Dify会自动创建一个新的会话 + } catch (Exception e) { + System.out.println("获取用户会话时出错: " + e.getMessage()); + // 继续执行,让Dify自动创建新会话 + } + } else { + // 如果存在对话ID,则添加到请求参数中 bodyMap.put("conversation_id", requestData.get("conversation_id")); } @@ -465,41 +478,26 @@ public class AiChatController extends BaseController { // 权限标识为辅导员 @GetMapping("/getMessagesToAdmin") public AjaxResult getMessagesToAdmin(@RequestParam String user, - @RequestParam(required = false) String lastId, + @RequestParam(required = false) String firstId, @RequestParam(defaultValue = "20") int limit, @RequestParam(defaultValue = "-updated_at") String sortBy) { - System.out.println("进入getMessagesToAdmin方法"); - System.out.println("参数 user: " + user); - System.out.println("参数 lastId: " + lastId); - System.out.println("参数 limit: " + limit); - System.out.println("参数 sortBy: " + sortBy); try { - System.out.println("开始调用getConversations方法"); List conversations = getConversations(user); - System.out.println("getConversations方法调用完成,结果: " + (conversations != null ? conversations.size() : "null")); if (conversations == null || conversations.isEmpty()) { - System.out.println("会话列表为空"); return AjaxResult.error("暂无会话记录"); } String conversation_id = conversations.get(0).getId(); - System.out.println("获取到的会话ID: " + conversation_id); - - System.out.println("开始调用getConversationHistoryMessages方法"); - Map result = getConversationHistoryMessages(conversation_id, user, null, 20); - System.out.println("getConversationHistoryMessages方法调用完成"); + Map result = getConversationHistoryMessages(conversation_id, user, firstId, limit); AjaxResult successResult = success(result); - System.out.println("返回成功结果"); return successResult; } catch (IOException e) { - System.out.println("捕获到IOException: " + e.getMessage()); e.printStackTrace(); return AjaxResult.error("网络请求失败,请稍后重试"); } catch (Exception e) { - System.out.println("捕获到其他异常: " + e.getMessage()); e.printStackTrace(); return AjaxResult.error("请求处理失败: " + e.getMessage()); } @@ -507,44 +505,27 @@ public class AiChatController extends BaseController { // 权限标识为学生 @GetMapping("/getMessagesToUser") - public AjaxResult getMessagesToUser(@RequestParam(required = false) String lastId, + public AjaxResult getMessagesToUser(@RequestParam(required = false) String firstId, @RequestParam(defaultValue = "20") int limit, @RequestParam(defaultValue = "-updated_at") String sortBy) { - System.out.println("进入getMessagesToUser方法"); - System.out.println("参数 lastId: " + lastId); - System.out.println("参数 limit: " + limit); - System.out.println("参数 sortBy: " + sortBy); try { - System.out.println("开始获取当前登录用户"); String user = SecurityUtils.getLoginUser().getUsername(); - System.out.println("当前登录用户: " + user); - - System.out.println("开始调用getConversations方法"); List conversations = getConversations(user); - System.out.println("getConversations方法调用完成,结果: " + (conversations != null ? conversations.size() : "null")); if (conversations == null || conversations.isEmpty()) { - System.out.println("会话列表为空"); return AjaxResult.error("暂无会话记录"); } String conversation_id = conversations.get(0).getId(); - System.out.println("获取到的会话ID: " + conversation_id); - - System.out.println("开始调用getConversationHistoryMessages方法"); - Map result = getConversationHistoryMessages(conversation_id, user, null, 20); - System.out.println("getConversationHistoryMessages方法调用完成"); + Map result = getConversationHistoryMessages(conversation_id, user, firstId, limit); AjaxResult successResult = success(result); - System.out.println("返回成功结果"); return successResult; } catch (IOException e) { - System.out.println("捕获到IOException: " + e.getMessage()); e.printStackTrace(); return AjaxResult.error("网络请求失败,请稍后重试"); } catch (Exception e) { - System.out.println("捕获到其他异常: " + e.getMessage()); e.printStackTrace(); return AjaxResult.error("请求处理失败: " + e.getMessage()); } @@ -571,19 +552,15 @@ public class AiChatController extends BaseController { .get() .build(); - System.out.println("准备发送HTTP请求到: " + urlBuilder.build().toString()); - // 执行请求 try (Response response = client.newCall(request).execute()) { - System.out.println("收到HTTP响应,状态码: " + response.code()); if (!response.isSuccessful()) { - System.out.println("HTTP请求失败: " + response.code() + " " + response.message()); throw new IOException("Dify API 请求失败: " + response.code() + " " + response.message()); } // 读取响应体 String responseBodyString = response.body().string(); - System.out.println("响应体内容: " + responseBodyString); + System.out.println("Raw Response: " + responseBodyString); // 打印原始数据 JsonNode rootNode = mapper.readTree(responseBodyString); // 提取 data 数组 @@ -591,7 +568,6 @@ public class AiChatController extends BaseController { List conversations = new ArrayList<>(); if (dataArray.isArray()) { - System.out.println("解析到 " + dataArray.size() + " 个会话"); for (JsonNode node : dataArray) { ConversationDTO dto = new ConversationDTO(); dto.setId(node.path("id").asText(null)); @@ -609,8 +585,6 @@ public class AiChatController extends BaseController { } } - System.out.println("getConversations方法执行完成,返回 " + conversations.size() + " 个会话"); - return conversations; } } @@ -635,19 +609,11 @@ public class AiChatController extends BaseController { String firstId, Integer limit) throws ServiceException { - System.out.println("进入getConversationHistoryMessages方法"); - System.out.println("参数 conversationId: " + conversationId); - System.out.println("参数 user: " + user); - System.out.println("参数 firstId: " + firstId); - System.out.println("参数 limit: " + limit); - // 参数校验 if (conversationId == null || conversationId.trim().isEmpty()) { - System.out.println("conversationId 参数为空"); throw new IllegalArgumentException("conversationId 不能为空"); } if (user == null || user.trim().isEmpty()) { - System.out.println("user 参数为空"); throw new IllegalArgumentException("user 不能为空"); } @@ -671,31 +637,24 @@ public class AiChatController extends BaseController { .get() .build(); - System.out.println("准备发送HTTP请求到: " + urlBuilder.build().toString()); - try (Response response = client.newCall(request).execute()) { - System.out.println("收到HTTP响应,状态码: " + response.code()); if (!response.isSuccessful()) { String body = response.body() != null ? response.body().string() : "No body"; - System.out.println("HTTP请求失败,响应体: " + body); String msg = String.format("HTTP %d %s - %s", response.code(), response.message(), body); throw new ServiceException("请求失败"); } ResponseBody responseBody = response.body(); if (responseBody == null) { - System.out.println("响应体为空"); return emptyResult(finalLimit); } String responseBodyString = responseBody.string(); - System.out.println("响应体内容: " + responseBodyString); JsonNode rootNode; try { rootNode = mapper.readTree(responseBodyString); } catch (JsonProcessingException e) { - System.out.println("响应数据格式错误: " + e.getMessage()); throw new ServiceException("响应数据格式错误"); } @@ -703,27 +662,21 @@ public class AiChatController extends BaseController { List> data = new ArrayList<>(); JsonNode dataArray = rootNode.get("data"); if (dataArray != null && dataArray.isArray()) { - System.out.println("解析到 " + dataArray.size() + " 条消息"); data = mapper.convertValue(dataArray, new TypeReference>>() { }); } // 提取 has_more boolean hasMore = rootNode.path("has_more").asBoolean(false); - System.out.println("has_more: " + hasMore); // ✅ 直接构建前端需要的返回结构 Map result = new HashMap<>(); result.put("data", data); result.put("has_more", hasMore); result.put("limit", finalLimit); - - System.out.println("getConversationHistoryMessages方法执行完成"); - return result; // 直接返回,调用方直接丢给前端 } catch (IOException e) { - System.out.println("网络请求失败: " + e.getMessage()); throw new ServiceException("网络请求失败: " + e.getMessage()); } }