|
@@ -1,9 +1,7 @@
|
|
|
package cn.cslg.pas.service.common;
|
|
|
|
|
|
|
|
|
-import cn.cslg.pas.common.model.dify.ChatMessageDTO;
|
|
|
-import cn.cslg.pas.common.model.dify.DifyChatMessageDTO;
|
|
|
-import cn.cslg.pas.common.model.dify.DifyHistoryMessageDTO;
|
|
|
+import cn.cslg.pas.common.model.dify.*;
|
|
|
|
|
|
import cn.cslg.pas.common.model.dify.confessionSession.AddConfessionSessionDTO;
|
|
|
import cn.cslg.pas.common.model.dify.confessionSession.UpdateConfessionSessionDTO;
|
|
@@ -14,6 +12,7 @@ import cn.cslg.pas.common.utils.LoginUtils;
|
|
|
import cn.cslg.pas.domain.business.TechnicalCase;
|
|
|
import cn.cslg.pas.domain.dify.ConfessionSession;
|
|
|
import cn.cslg.pas.domain.report.AssoProjectConfession;
|
|
|
+import cn.cslg.pas.mapper.dify.ConfessionSessionMapper;
|
|
|
import cn.cslg.pas.service.business.TechnicalCaseService;
|
|
|
import cn.cslg.pas.service.dify.ConfessionSessionService;
|
|
|
import cn.cslg.pas.service.dify.DifySessionService;
|
|
@@ -21,6 +20,7 @@ import cn.cslg.pas.service.report.AssoProjectConfessionService;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.google.gson.Gson;
|
|
@@ -28,13 +28,17 @@ import com.google.gson.GsonBuilder;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import okhttp3.*;
|
|
|
+import org.apache.commons.lang3.ObjectUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.w3c.dom.DocumentType;
|
|
|
import reactor.core.publisher.Flux;
|
|
|
|
|
|
+import javax.swing.text.Document;
|
|
|
import java.io.*;
|
|
|
|
|
|
|
|
@@ -54,6 +58,8 @@ import java.util.concurrent.TimeUnit;
|
|
|
public class DifyService {
|
|
|
@Value("${DIFY.apiKey}")
|
|
|
private String apiKey;
|
|
|
+ @Value("${DIFY.OAApiKey}")
|
|
|
+ private String OAApiKey;
|
|
|
@Value("${DIFY.url}")
|
|
|
private String url;
|
|
|
@Value("${FileDownloadUrl}")
|
|
@@ -68,6 +74,8 @@ public class DifyService {
|
|
|
@Autowired
|
|
|
private LoginUtils loginUtils;
|
|
|
private final ConfessionSessionService confessionSessionService;
|
|
|
+ @Autowired
|
|
|
+ private ConfessionSessionMapper confessionSessionMapper;
|
|
|
|
|
|
/**
|
|
|
* 调用文件系统删除文件接口
|
|
@@ -360,4 +368,103 @@ public class DifyService {
|
|
|
System.out.println(res);
|
|
|
return reMap;
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ public Flux<String> successGetOAHttp(Integer confessionSessionId) {
|
|
|
+ ConfessionSession confessionSession = confessionSessionService.getById(confessionSessionId);
|
|
|
+ String fileUrl = fileDownloadUrl + confessionSession.getGuid();
|
|
|
+ String userId = loginUtils.getId().toString();
|
|
|
+
|
|
|
+ OkHttpClient client = new OkHttpClient.Builder()
|
|
|
+ .connectTimeout(600, TimeUnit.SECONDS)
|
|
|
+ .writeTimeout(600, TimeUnit.SECONDS)
|
|
|
+ .readTimeout(600, TimeUnit.SECONDS)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("fileUrl", fileUrl);
|
|
|
+
|
|
|
+ OAMessageDTO oaMessageDTO = new OAMessageDTO();
|
|
|
+ oaMessageDTO.setInputs(map);
|
|
|
+ oaMessageDTO.setResponseMode("streaming");
|
|
|
+ oaMessageDTO.setUser(userId);
|
|
|
+ oaMessageDTO.setFiles(new ArrayList<>());
|
|
|
+
|
|
|
+ String param = new Gson().toJson(oaMessageDTO);
|
|
|
+ RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), param);
|
|
|
+ Request request = new Request.Builder()
|
|
|
+ .url(url + "workflows/run")
|
|
|
+ .addHeader("Authorization", "Bearer " + OAApiKey)
|
|
|
+ .addHeader(HttpHeaders.CONTENT_TYPE, "application/json")
|
|
|
+ .post(requestBody)
|
|
|
+ .build();
|
|
|
+ return Flux.create(emitter -> {
|
|
|
+ client.newCall(request).enqueue(new Callback() {
|
|
|
+ @Override
|
|
|
+ public void onFailure(Call call, IOException e) {
|
|
|
+ emitter.error(e);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onResponse(Call call, Response response) throws IOException {
|
|
|
+
|
|
|
+ if (!response.isSuccessful()) {
|
|
|
+ emitter.error(new IOException("Unexpected code: " + response));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ try (Reader reader = response.body().charStream()) {
|
|
|
+ BufferedReader bufferedReader = new BufferedReader(reader);
|
|
|
+ String line;
|
|
|
+ String prefixToRemove = "data: ";
|
|
|
+ String runId = "";
|
|
|
+ while ((line = bufferedReader.readLine()) != null) {
|
|
|
+ if (line.isEmpty()) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (line.startsWith(prefixToRemove)) {
|
|
|
+ line = line.substring(prefixToRemove.length());
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ JSONObject jsonObject = JSON.parseObject(line);
|
|
|
+ String workflowRunId = jsonObject.get("workflow_run_id").toString();
|
|
|
+ String event = jsonObject.get("event").toString();
|
|
|
+ if (StringUtils.isNotEmpty(workflowRunId)) {
|
|
|
+ if (StringUtils.isEmpty(runId)) {
|
|
|
+ runId = workflowRunId;
|
|
|
+ confessionSessionMapper.updateSingleField(confessionSessionId, "conversation_id", workflowRunId);
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (event.equals("workflow_finished")) {
|
|
|
+ String data = jsonObject.get("data").toString();
|
|
|
+ String content = "";
|
|
|
+ if (StringUtils.isNotEmpty(data)) {
|
|
|
+ JSONObject dataObject = JSON.parseObject(data);
|
|
|
+ String outputs = dataObject.get("outputs").toString();
|
|
|
+ if (StringUtils.isNotEmpty(outputs)) {
|
|
|
+ JSONObject outputsObject = JSON.parseObject(outputs);
|
|
|
+ String output = outputsObject.get("output").toString();
|
|
|
+ content = DataUtils.unicodeDecode(output);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (StringUtils.isNotEmpty(content)) {
|
|
|
+ JSONObject obj = new JSONObject();
|
|
|
+ obj.put("answer", content);
|
|
|
+ confessionSessionMapper.updateSingleField(confessionSessionId, "content", obj.toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ }
|
|
|
+ emitter.next(line); // 将每行数据发送到 Flux
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ emitter.error(e);
|
|
|
+ } finally {
|
|
|
+ emitter.complete();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|