1.修改AIChat
This commit is contained in:
@@ -93,6 +93,7 @@ public class AiChatController extends BaseController {
|
||||
.writeTimeout(Duration.ofSeconds(12)) // 写超时
|
||||
.retryOnConnectionFailure(true)
|
||||
.build();
|
||||
|
||||
/** 为本次请求设置 “总超时”(含连接/读写),避免无限挂起 */
|
||||
private Response execWithTimeouts(Request req, int callSecs, int readSecs, int writeSecs) throws IOException {
|
||||
OkHttpClient shortClient = client.newBuilder()
|
||||
@@ -173,7 +174,8 @@ public class AiChatController extends BaseController {
|
||||
* @param currentUsername
|
||||
* @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<>();
|
||||
@@ -258,7 +260,8 @@ public class AiChatController extends BaseController {
|
||||
try (BufferedReader reader = new BufferedReader(httpResponse.body().charStream())) {
|
||||
String line;
|
||||
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();
|
||||
if ("[DONE]".equals(jsonData)) {
|
||||
break;
|
||||
@@ -313,7 +316,8 @@ public class AiChatController extends BaseController {
|
||||
* @return 统一响应结果
|
||||
*/
|
||||
@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");
|
||||
if (messageId == null || messageId.trim().isEmpty()) {
|
||||
@@ -357,8 +361,7 @@ public class AiChatController extends BaseController {
|
||||
String jsonBody = mapper.writeValueAsString(bodyMap);
|
||||
okhttp3.RequestBody body = okhttp3.RequestBody.create(
|
||||
MediaType.get("application/json; charset=utf-8"),
|
||||
jsonBody
|
||||
);
|
||||
jsonBody);
|
||||
|
||||
// 调用 Dify API
|
||||
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("message_id", feedbackNode.has("message_id") ? feedbackNode.get("message_id").asText() : null);
|
||||
feedbackItem.put("rating", feedbackNode.has("rating") ? feedbackNode.get("rating").asText() : null);
|
||||
feedbackItem.put("content", 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);
|
||||
feedbackItem.put("message_id",
|
||||
feedbackNode.has("message_id") ? feedbackNode.get("message_id").asText() : null);
|
||||
feedbackItem.put("rating",
|
||||
feedbackNode.has("rating") ? feedbackNode.get("rating").asText() : null);
|
||||
feedbackItem.put("content",
|
||||
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);
|
||||
}
|
||||
@@ -509,6 +519,7 @@ public class AiChatController extends BaseController {
|
||||
|
||||
try {
|
||||
String user = SecurityUtils.getLoginUser().getUsername();
|
||||
|
||||
List<ConversationDTO> conversations = getConversations(user);
|
||||
|
||||
if (conversations == null || conversations.isEmpty()) {
|
||||
@@ -516,6 +527,7 @@ public class AiChatController extends BaseController {
|
||||
}
|
||||
|
||||
String conversation_id = conversations.get(0).getId();
|
||||
|
||||
Map<String, Object> result = getConversationHistoryMessages(conversation_id, user, firstId, limit);
|
||||
|
||||
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
|
||||
HttpUrl.Builder urlBuilder = HttpUrl.parse(DIFY_CONVERSATIONS_URL).newBuilder();
|
||||
urlBuilder.addQueryParameter("user", user);
|
||||
@@ -599,7 +611,6 @@ public class AiChatController extends BaseController {
|
||||
return getConversations(user, null, limit, sortBy);
|
||||
}
|
||||
|
||||
|
||||
// 获取历史消息
|
||||
private Map<String, Object> getConversationHistoryMessages(
|
||||
String conversationId,
|
||||
@@ -688,11 +699,6 @@ public class AiChatController extends BaseController {
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 文件上传接口
|
||||
* <p>
|
||||
|
Reference in New Issue
Block a user