|
@@ -1,5 +1,6 @@
|
|
|
package com.bosshand.virgo.api.workark.service;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.bosshand.virgo.api.workark.dao.*;
|
|
|
import com.bosshand.virgo.api.workark.model.*;
|
|
@@ -14,6 +15,8 @@ import io.github.imfangs.dify.client.DifyWorkflowClient;
|
|
|
import io.github.imfangs.dify.client.callback.ChatStreamCallback;
|
|
|
import io.github.imfangs.dify.client.callback.CompletionStreamCallback;
|
|
|
import io.github.imfangs.dify.client.callback.WorkflowStreamCallback;
|
|
|
+import io.github.imfangs.dify.client.enums.FileTransferMethod;
|
|
|
+import io.github.imfangs.dify.client.enums.FileType;
|
|
|
import io.github.imfangs.dify.client.enums.ResponseMode;
|
|
|
import io.github.imfangs.dify.client.event.*;
|
|
|
import io.github.imfangs.dify.client.exception.DifyApiException;
|
|
@@ -22,12 +25,14 @@ import io.github.imfangs.dify.client.model.chat.ConversationListResponse;
|
|
|
import io.github.imfangs.dify.client.model.chat.MessageListResponse;
|
|
|
import io.github.imfangs.dify.client.model.common.SimpleResponse;
|
|
|
import io.github.imfangs.dify.client.model.completion.CompletionRequest;
|
|
|
+import io.github.imfangs.dify.client.model.file.FileInfo;
|
|
|
import io.github.imfangs.dify.client.model.workflow.WorkflowRunRequest;
|
|
|
import io.github.imfangs.dify.client.model.workflow.WorkflowRunStatusResponse;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.UUID;
|
|
@@ -318,6 +323,121 @@ public class DifyService {
|
|
|
return difyCompletionDao.getList(difyCompletion);
|
|
|
}
|
|
|
|
|
|
+ public String getChat(long difyTypeId, Map<String, Object> inputs) {
|
|
|
+
|
|
|
+ String question = inputs.get("query").toString();
|
|
|
+
|
|
|
+ DifyType difyType = difyTypeDao.get(difyTypeId);
|
|
|
+
|
|
|
+ UserContext userContext = ContextUtils.getUserContext();
|
|
|
+
|
|
|
+ String simpleUUID = UUID.randomUUID().toString().replace("-", "");
|
|
|
+
|
|
|
+ DifyChat difyChat = new DifyChat();
|
|
|
+ difyChat.setDifyTypeId(difyTypeId);
|
|
|
+ difyChat.setStatus("running");
|
|
|
+ difyChat.setUserId(userContext.getUserId());
|
|
|
+ difyChat.setSimpleUUID(simpleUUID);
|
|
|
+ JSONObject json = new JSONObject(inputs);
|
|
|
+ difyChat.setInputs(json.toJSONString());
|
|
|
+ difyChatDao.save(difyChat);
|
|
|
+
|
|
|
+ // 创建聊天客户端
|
|
|
+ DifyChatClient chatClient = DifyClientFactory.createChatClient("http://203.110.233.149:9000/v1", difyType.getApiKey());
|
|
|
+
|
|
|
+ // 创建聊天消息
|
|
|
+ ChatMessage message = ChatMessage.builder()
|
|
|
+ .query(question)
|
|
|
+ .user(userContext.getUserName() + "-" + userContext.getUserId())
|
|
|
+ .responseMode(ResponseMode.STREAMING)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ StringBuilder chatM = new StringBuilder();
|
|
|
+
|
|
|
+ StringBuilder agentM = new StringBuilder();
|
|
|
+
|
|
|
+ // 发送流式消息
|
|
|
+ try {
|
|
|
+ chatClient.sendChatMessageStream(message, new ChatStreamCallback() {
|
|
|
+ @Override
|
|
|
+ public void onMessage(MessageEvent event) {
|
|
|
+ //System.out.println("收到消息片段: " + event.getAnswer());
|
|
|
+ chatM.append(event.getAnswer());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onMessageEnd(MessageEndEvent event) {
|
|
|
+ DifyChat difyChat = difyChatDao.getSimpleUUID(simpleUUID);
|
|
|
+ difyChat.setConversationId(event.getConversationId());
|
|
|
+ difyChat.setMessageId(event.getMessageId());
|
|
|
+ if (StringUtil.notBlank(chatM.toString())) {
|
|
|
+ difyChat.setOutputs(chatM.toString());
|
|
|
+ }
|
|
|
+ if (StringUtil.notBlank(agentM.toString())) {
|
|
|
+ difyChat.setOutputs(agentM.toString());
|
|
|
+ }
|
|
|
+ difyChat.setStatus("succeeded");
|
|
|
+ difyChatDao.update(difyChat);
|
|
|
+ System.out.println("消息结束,完整消息ID: " + event.getMessageId());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onMessageFile(MessageFileEvent event) {
|
|
|
+ System.out.println("收到文件: " + event);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onTTSMessage(TtsMessageEvent event) {
|
|
|
+ System.out.println("收到TTS消息: " + event);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onTTSMessageEnd(TtsMessageEndEvent event) {
|
|
|
+ System.out.println("TTS消息结束: " + event);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onMessageReplace(MessageReplaceEvent event) {
|
|
|
+ System.out.println("消息替换: " + event);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onAgentMessage(AgentMessageEvent event) {
|
|
|
+ //System.out.println("收到Agent消息片段: " + event.getAnswer());
|
|
|
+ agentM.append(event.getAnswer());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onAgentThought(AgentThoughtEvent event) {
|
|
|
+ System.out.println("Agent思考: " + event);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onError(ErrorEvent event) {
|
|
|
+ DifyChat difyChat = difyChatDao.getSimpleUUID(simpleUUID);
|
|
|
+ difyChat.setError(event.getMessage());
|
|
|
+ difyChat.setStatus("error");
|
|
|
+ difyChatDao.update(difyChat);
|
|
|
+ System.err.println("错误: " + event.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onException(Throwable throwable) {
|
|
|
+ DifyChat difyChat = difyChatDao.getSimpleUUID(simpleUUID);
|
|
|
+ difyChat.setError(throwable.getMessage());
|
|
|
+ difyChat.setStatus("error");
|
|
|
+ difyChatDao.update(difyChat);
|
|
|
+ System.err.println("异常: " + throwable.getMessage());
|
|
|
+ }
|
|
|
+ });
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (DifyApiException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return simpleUUID;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 对话型应用
|
|
|
*/
|
|
@@ -350,10 +470,26 @@ public class DifyService {
|
|
|
.responseMode(ResponseMode.STREAMING)
|
|
|
.build();
|
|
|
|
|
|
+ // 添加会话id
|
|
|
if (inputs.containsKey("conversationId")) {
|
|
|
message.setConversationId(inputs.get("conversationId").toString());
|
|
|
}
|
|
|
|
|
|
+ // 添加图片
|
|
|
+ if (inputs.containsKey("files")) {
|
|
|
+ List<FileInfo> infoList = new ArrayList<>();
|
|
|
+ JSONArray files = json.getJSONArray("files");
|
|
|
+ for (int i = 0; i < files.size(); i++) {
|
|
|
+ JSONObject jsonObject = files.getJSONObject(i);
|
|
|
+ FileInfo info = new FileInfo();
|
|
|
+ info.setType(FileType.IMAGE);
|
|
|
+ info.setTransferMethod(FileTransferMethod.REMOTE_URL);
|
|
|
+ info.setUrl(jsonObject.getString("url"));
|
|
|
+ infoList.add(info);
|
|
|
+ }
|
|
|
+ message.setFiles(infoList);
|
|
|
+ }
|
|
|
+
|
|
|
StringBuilder chatM = new StringBuilder();
|
|
|
|
|
|
StringBuilder agentM = new StringBuilder();
|