|
@@ -1,24 +1,25 @@
|
|
|
package com.bosshand.virgo.api.workark.service;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
-import com.bosshand.virgo.api.workark.dao.DifyCompletionDao;
|
|
|
-import com.bosshand.virgo.api.workark.dao.DifyTypeDao;
|
|
|
-import com.bosshand.virgo.api.workark.dao.DifyWorkFlowDao;
|
|
|
-import com.bosshand.virgo.api.workark.dao.HtmlCodeDao;
|
|
|
-import com.bosshand.virgo.api.workark.model.DifyCompletion;
|
|
|
-import com.bosshand.virgo.api.workark.model.DifyType;
|
|
|
-import com.bosshand.virgo.api.workark.model.DifyWorkFlow;
|
|
|
-import com.bosshand.virgo.api.workark.model.HtmlCode;
|
|
|
+import com.bosshand.virgo.api.workark.dao.*;
|
|
|
+import com.bosshand.virgo.api.workark.model.*;
|
|
|
import com.bosshand.virgo.core.model.UserContext;
|
|
|
import com.bosshand.virgo.core.utils.ContextUtils;
|
|
|
+import com.bosshand.virgo.core.utils.StringUtil;
|
|
|
+import io.github.imfangs.dify.client.DifyChatClient;
|
|
|
import io.github.imfangs.dify.client.DifyClientFactory;
|
|
|
import io.github.imfangs.dify.client.DifyCompletionClient;
|
|
|
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.ResponseMode;
|
|
|
import io.github.imfangs.dify.client.event.*;
|
|
|
import io.github.imfangs.dify.client.exception.DifyApiException;
|
|
|
+import io.github.imfangs.dify.client.model.chat.ChatMessage;
|
|
|
+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.workflow.WorkflowRunRequest;
|
|
|
import io.github.imfangs.dify.client.model.workflow.WorkflowRunStatusResponse;
|
|
@@ -42,6 +43,9 @@ public class DifyService {
|
|
|
@Autowired
|
|
|
DifyCompletionDao difyCompletionDao;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ DifyChatDao difyChatDao;
|
|
|
+
|
|
|
@Autowired
|
|
|
HtmlCodeDao htmlCodeDao;
|
|
|
|
|
@@ -72,7 +76,7 @@ public class DifyService {
|
|
|
htmlCodeDao.update(htmlCode);
|
|
|
}
|
|
|
|
|
|
- public String publishHtmlCode(long id){
|
|
|
+ public String publishHtmlCode(long id) {
|
|
|
HtmlCode htmlCode = htmlCodeDao.get(id);
|
|
|
htmlCode.setStatus(1);
|
|
|
htmlCodeDao.update(htmlCode);
|
|
@@ -312,4 +316,208 @@ public class DifyService {
|
|
|
public List<DifyCompletion> getCompletionList(DifyCompletion difyCompletion) {
|
|
|
return difyCompletionDao.getList(difyCompletion);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 对话型应用
|
|
|
+ */
|
|
|
+ public String chatRun(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();
|
|
|
+
|
|
|
+ if (inputs.containsKey("conversationId")) {
|
|
|
+ message.setConversationId(inputs.get("conversationId").toString());
|
|
|
+ }
|
|
|
+
|
|
|
+ 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;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public DifyChat getChat(String simpleUUID) {
|
|
|
+ return difyChatDao.getSimpleUUID(simpleUUID);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取会话历史消息
|
|
|
+ */
|
|
|
+ public MessageListResponse messageList(String conversationId) {
|
|
|
+
|
|
|
+ DifyChat difyChat = difyChatDao.getConversationId(conversationId);
|
|
|
+
|
|
|
+ DifyType difyType = difyTypeDao.get(difyChat.getDifyTypeId());
|
|
|
+
|
|
|
+ UserContext userContext = ContextUtils.getUserContext();
|
|
|
+
|
|
|
+ // 创建聊天客户端
|
|
|
+ DifyChatClient chatClient = DifyClientFactory.createChatClient("http://203.110.233.149:9000/v1", difyType.getApiKey());
|
|
|
+
|
|
|
+ // 获取会话历史消息
|
|
|
+ try {
|
|
|
+ MessageListResponse messages = chatClient.getMessages(conversationId, userContext.getUserName() + "-" + userContext.getUserId(), null, 10);
|
|
|
+ return messages;
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (DifyApiException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取会话列表
|
|
|
+ */
|
|
|
+ public ConversationListResponse conversations(long difyTypeId) {
|
|
|
+
|
|
|
+ DifyType difyType = difyTypeDao.get(difyTypeId);
|
|
|
+
|
|
|
+ UserContext userContext = ContextUtils.getUserContext();
|
|
|
+
|
|
|
+ // 创建聊天客户端
|
|
|
+ DifyChatClient chatClient = DifyClientFactory.createChatClient("http://203.110.233.149:9000/v1", difyType.getApiKey());
|
|
|
+ // 获取会话列表
|
|
|
+ try {
|
|
|
+ ConversationListResponse conversations = chatClient.getConversations(userContext.getUserName() + "-" + userContext.getUserId(), null, 10, "-updated_at");
|
|
|
+ return conversations;
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (DifyApiException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除会话
|
|
|
+ */
|
|
|
+ public void deleteConversation(String conversationId) {
|
|
|
+
|
|
|
+ DifyChat difyChat = difyChatDao.getConversationId(conversationId);
|
|
|
+
|
|
|
+ DifyType difyType = difyTypeDao.get(difyChat.getDifyTypeId());
|
|
|
+
|
|
|
+ UserContext userContext = ContextUtils.getUserContext();
|
|
|
+
|
|
|
+ // 创建聊天客户端
|
|
|
+ DifyChatClient chatClient = DifyClientFactory.createChatClient("http://203.110.233.149:9000/v1", difyType.getApiKey());
|
|
|
+
|
|
|
+ // 删除会话
|
|
|
+ try {
|
|
|
+ SimpleResponse deleteResponse = chatClient.deleteConversation(conversationId, userContext.getUserName() + "-" + userContext.getUserId());
|
|
|
+ difyChatDao.delete(conversationId);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (DifyApiException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
}
|