修改获取历史记录

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.peanut.modules.common.entity.AiChatContent;
import com.peanut.modules.common.service.AiChatContentService;
import org.apache.commons.lang.StringUtils;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
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{
CloseableHttpClient httpClient = HttpClients.createDefault();
String chatId = params.get("chatId").toString();
String page = params.get("page").toString();
String pageSize = params.get("pageSize").toString();
String sessionId = params.get("sessionId").toString();
HttpGet get = new HttpGet(url+"/api/v1/chats/"+chatId+"/sessions?" +
"page="+page+"&page_size="+pageSize+"&id="+sessionId+"&user_id="+ShiroUtils.getUId());
get.setHeader("Authorization", authorization);
get.setHeader("Content-Type", "application/json;chartset=utf-8");
CloseableHttpResponse response = httpClient.execute(get);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode >= 400) {
throw new RuntimeException("API调用失败状态码" + statusCode);
String id = params.get("chatId").toString();
List<String> chatIds = new ArrayList<>();
if (StringUtils.isEmpty(id)){
List<Map<String,Object>> assistants = getChatAssistants("");
for (Map<String, Object> map:assistants) {
chatIds.add(map.get("id").toString());
}
}else {
chatIds.add(id);
}
HttpEntity responseEntity = response.getEntity();
String responseString = EntityUtils.toString(responseEntity, Consts.UTF_8);
JSONObject jsonObject = JSONObject.parseObject(responseString);
List<Map<String,Object>> list = new ArrayList();
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);
for (String chatId : chatIds){
String page = params.get("page").toString();
String pageSize = params.get("pageSize").toString();
String sessionId = params.get("sessionId").toString();
HttpGet get = new HttpGet(url+"/api/v1/chats/"+chatId+"/sessions?" +
"page="+page+"&page_size="+pageSize+"&id="+sessionId+"&user_id="+ShiroUtils.getUId());
get.setHeader("Authorization", authorization);
get.setHeader("Content-Type", "application/json;chartset=utf-8");
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;