|
@@ -784,5 +784,101 @@ public class DifyService {
|
|
|
|
|
|
return res;
|
|
|
}
|
|
|
+ public Flux<String> chatMessageReFux(DifyChatMessageDTO difyChatMessageDTO,String key) {
|
|
|
+ Integer projectId = chatMessageDTO.getProjectId();
|
|
|
+ Integer confessionSessionId = chatMessageDTO.getConfessionSessionId();
|
|
|
+ String conversationId = chatMessageDTO.getConversationId();
|
|
|
+ String userId = loginUtils.getId().toString();
|
|
|
+ String query = chatMessageDTO.getQuery();
|
|
|
+ if (conversationId == null) {
|
|
|
+ conversationId = difySessionService.getSessionId(projectId, userId);
|
|
|
+ }
|
|
|
+
|
|
|
+ String temConversationId = conversationId;
|
|
|
+ OkHttpClient client = new OkHttpClient.Builder()
|
|
|
+ .connectTimeout(600, TimeUnit.SECONDS)
|
|
|
+ .writeTimeout(600, TimeUnit.SECONDS)
|
|
|
+ .readTimeout(600, TimeUnit.SECONDS)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ DifyChatMessageDTO difyChatMessageDTO = new DifyChatMessageDTO();
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ String fileContent = "";
|
|
|
+ String inventionPoint = "";
|
|
|
+ if (projectId != null) {
|
|
|
+ fileContent = this.getConfression(projectId);
|
|
|
+ inventionPoint = this.getInventPoint(projectId);
|
|
|
+ } else if (confessionSessionId != null) {
|
|
|
+ ConfessionSession confessionSession = confessionSessionService.getById(confessionSessionId);
|
|
|
+ fileContent = fileDownloadUrl + confessionSession.getGuid();
|
|
|
+ inventionPoint = confessionSession.getInventionPoint();
|
|
|
+ }
|
|
|
+
|
|
|
+ map.put("file_path", fileContent);
|
|
|
+ map.put("invention_point", inventionPoint);
|
|
|
+ difyChatMessageDTO.setInputs(map);
|
|
|
+ difyChatMessageDTO.setConversationId(conversationId);
|
|
|
+ difyChatMessageDTO.setResponseMode("streaming");
|
|
|
+ difyChatMessageDTO.setUser(userId);
|
|
|
+ difyChatMessageDTO.setQuery(query);
|
|
|
+ difyChatMessageDTO.setFiles(new ArrayList<>());
|
|
|
+
|
|
|
+ String param = new Gson().toJson(difyChatMessageDTO);
|
|
|
+ RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), param);
|
|
|
+ Request request = this.getChatMessageRequest(requestBody);
|
|
|
+ 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 temConversationId2 = temConversationId;
|
|
|
+ String prefixToRemove = "data: ";
|
|
|
+ while ((line = bufferedReader.readLine()) != null) {
|
|
|
+ if (line.isEmpty()) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (line.startsWith(prefixToRemove)) {
|
|
|
+ line = line.substring(prefixToRemove.length());
|
|
|
+ }
|
|
|
+ if (temConversationId2 == null || temConversationId2.isEmpty()) {
|
|
|
+ try {
|
|
|
+ JSONObject jsonObject = JSON.parseObject(line);
|
|
|
+ temConversationId2 = jsonObject.get("conversation_id").toString();
|
|
|
+ if (projectId != null) {
|
|
|
+ difySessionService.addDifySession(projectId, userId, temConversationId2);
|
|
|
+ }
|
|
|
+ if (confessionSessionId != null) {
|
|
|
+ UpdateConfessionSessionDTO updateConfessionSessionDTO = new UpdateConfessionSessionDTO();
|
|
|
+ updateConfessionSessionDTO.setConfessionSessionId(confessionSessionId);
|
|
|
+ updateConfessionSessionDTO.setConversationId(temConversationId2);
|
|
|
+ confessionSessionService.updateConfessionSession(updateConfessionSessionDTO);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ emitter.next(line); // 将每行数据发送到 Flux
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ emitter.error(e);
|
|
|
+ } finally {
|
|
|
+ emitter.complete();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|