|
@@ -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.UpdateConfessionSessionDTO;
|
|
|
import cn.cslg.pas.common.utils.CacheUtils;
|
|
@@ -18,6 +16,7 @@ import cn.cslg.pas.service.dify.DifySessionService;
|
|
|
import cn.cslg.pas.service.report.AssoProjectConfessionService;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
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;
|
|
@@ -25,13 +24,16 @@ import com.google.gson.GsonBuilder;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import okhttp3.*;
|
|
|
+import org.apache.commons.lang3.ObjectUtils;
|
|
|
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 +56,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}")
|
|
@@ -314,4 +318,90 @@ public class DifyService {
|
|
|
map1.put("confessionSessionId",confessionSessionId);
|
|
|
return map1;
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ public Flux<String> successGetOAHttp(String guid) {
|
|
|
+ String fileUrl = fileDownloadUrl + guid;
|
|
|
+ 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<>();
|
|
|
+ DifyFile difyFile = new DifyFile();
|
|
|
+ difyFile.setTransferMethod("remote_url");
|
|
|
+ difyFile.setType("document");
|
|
|
+ difyFile.setUrl(fileUrl);
|
|
|
+ map.put("file", difyFile);
|
|
|
+
|
|
|
+ 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: ";
|
|
|
+ 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();
|
|
|
+ if (workflowRunId != null) {
|
|
|
+ ConfessionSession session = confessionSessionService.getOne(new LambdaQueryWrapper<ConfessionSession>()
|
|
|
+ .eq(ConfessionSession::getConversationId, workflowRunId));
|
|
|
+ if (ObjectUtils.isEmpty(session)) {
|
|
|
+ ConfessionSession confessionSession = new ConfessionSession();
|
|
|
+ confessionSession.setConversationId(workflowRunId);
|
|
|
+ confessionSession.setCreateId(userId);
|
|
|
+ confessionSession.setGuid(guid);
|
|
|
+ confessionSession.insert();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ }
|
|
|
+ emitter.next(line); // 将每行数据发送到 Flux
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ emitter.error(e);
|
|
|
+ } finally {
|
|
|
+ emitter.complete();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|