修改获取历史记录

This commit is contained in:
wuchunlei
2025-05-20 15:39:55 +08:00
parent 1b06a41af6
commit b1b3a9531c

View File

@@ -3,6 +3,7 @@ package com.peanut.common.utils;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.peanut.modules.common.entity.AiChatContent; import com.peanut.modules.common.entity.AiChatContent;
import com.peanut.modules.common.service.AiChatContentService; import com.peanut.modules.common.service.AiChatContentService;
import org.apache.commons.lang.StringUtils;
import org.apache.http.Consts; import org.apache.http.Consts;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.CloseableHttpResponse;
@@ -63,32 +64,43 @@ public class RagFlowApiUtil {
//聊天助手下对话列表 //聊天助手下对话列表
public List<Map<String,Object>> getChats(Map<String,Object> params) throws Exception{ public List<Map<String,Object>> getChats(Map<String,Object> params) throws Exception{
CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpClient httpClient = HttpClients.createDefault();
String chatId = params.get("chatId").toString(); String id = params.get("chatId").toString();
String page = params.get("page").toString(); List<String> chatIds = new ArrayList<>();
String pageSize = params.get("pageSize").toString(); if (StringUtils.isEmpty(id)){
String sessionId = params.get("sessionId").toString(); List<Map<String,Object>> assistants = getChatAssistants("");
HttpGet get = new HttpGet(url+"/api/v1/chats/"+chatId+"/sessions?" + for (Map<String, Object> map:assistants) {
"page="+page+"&page_size="+pageSize+"&id="+sessionId+"&user_id="+ShiroUtils.getUId()); chatIds.add(map.get("id").toString());
get.setHeader("Authorization", authorization); }
get.setHeader("Content-Type", "application/json;chartset=utf-8"); }else {
CloseableHttpResponse response = httpClient.execute(get); chatIds.add(id);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode >= 400) {
throw new RuntimeException("API调用失败状态码" + statusCode);
} }
HttpEntity responseEntity = response.getEntity();
String responseString = EntityUtils.toString(responseEntity, Consts.UTF_8);
JSONObject jsonObject = JSONObject.parseObject(responseString);
List<Map<String,Object>> list = new ArrayList(); List<Map<String,Object>> list = new ArrayList();
if ("0".equals(jsonObject.get("code").toString())){ for (String chatId : chatIds){
List l = jsonObject.getJSONArray("data"); String page = params.get("page").toString();
for (Object o : l) { String pageSize = params.get("pageSize").toString();
Map<String,Object> map = new HashMap<>(); String sessionId = params.get("sessionId").toString();
Map<String,Object> m = (Map<String,Object>)o; HttpGet get = new HttpGet(url+"/api/v1/chats/"+chatId+"/sessions?" +
map.put("id",m.get("id")); "page="+page+"&page_size="+pageSize+"&id="+sessionId+"&user_id="+ShiroUtils.getUId());
map.put("name",m.get("name")); get.setHeader("Authorization", authorization);
map.put("messages",m.get("messages")); get.setHeader("Content-Type", "application/json;chartset=utf-8");
list.add(map); CloseableHttpResponse response = httpClient.execute(get);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode >= 400) {
throw new RuntimeException("API调用失败状态码" + statusCode);
}
HttpEntity responseEntity = response.getEntity();
String responseString = EntityUtils.toString(responseEntity, Consts.UTF_8);
JSONObject jsonObject = JSONObject.parseObject(responseString);
if ("0".equals(jsonObject.get("code").toString())){
List l = jsonObject.getJSONArray("data");
for (Object o : l) {
Map<String,Object> map = new HashMap<>();
Map<String,Object> m = (Map<String,Object>)o;
map.put("id",m.get("id"));
map.put("name",m.get("name"));
map.put("messages",m.get("messages"));
list.add(map);
}
} }
} }
return list; return list;