From 6a0fb9f46ad8008b61b47ed3eb55a9c74a01a4e9 Mon Sep 17 00:00:00 2001 From: Daniel Hu Date: Thu, 23 Nov 2023 16:02:20 +0800 Subject: [PATCH] Feat: new api deleteLastConversation/request Signed-off-by: Daniel Hu --- api/topic.md | 33 ++++++++++ .../devchat/devchat/ActionHandlerFactory.java | 1 + .../ai/devchat/devchat/DevChatActions.java | 2 + .../DeleteLastConversationRequestHandler.java | 63 +++++++++++++++++++ .../handler/ListModelsRequestHandler.java | 1 - 5 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 src/main/java/ai/devchat/devchat/handler/DeleteLastConversationRequestHandler.java diff --git a/api/topic.md b/api/topic.md index c40efee..647669a 100644 --- a/api/topic.md +++ b/api/topic.md @@ -98,3 +98,36 @@ } } ``` + +## Delete Last Conversation in One Topic + +### JS to Java + +```json +{ + "action": "deleteLastConversation/request", + "metadata": { + "callback": "responseFunctionName" + }, + "payload": { + "promptHash": "xxx" + } +} +``` + +### Java to JS + +- success + +```json +{ + "action": "deleteLastConversation/response", + "metadata": { + "status": "success", + "error": "" + }, + "payload": { + "promptHash": "xxx" + } +} +``` diff --git a/src/main/java/ai/devchat/devchat/ActionHandlerFactory.java b/src/main/java/ai/devchat/devchat/ActionHandlerFactory.java index 8cb9054..a1f0762 100644 --- a/src/main/java/ai/devchat/devchat/ActionHandlerFactory.java +++ b/src/main/java/ai/devchat/devchat/ActionHandlerFactory.java @@ -26,6 +26,7 @@ public class ActionHandlerFactory { put(DevChatActions.GET_SETTING_REQUEST, GetSettingRequestHandler.class); put(DevChatActions.UPDATE_SETTING_REQUEST, UpdateSettingRequestHandler.class); put(DevChatActions.SHOW_SETTING_DIALOG_REQUEST, ShowSettingDialogRequestHandler.class); + put(DevChatActions.DELETE_LAST_CONVERSATION_REQUEST, DeleteLastConversationRequestHandler.class); } }; diff --git a/src/main/java/ai/devchat/devchat/DevChatActions.java b/src/main/java/ai/devchat/devchat/DevChatActions.java index ce1ee60..f7e5cc6 100644 --- a/src/main/java/ai/devchat/devchat/DevChatActions.java +++ b/src/main/java/ai/devchat/devchat/DevChatActions.java @@ -33,4 +33,6 @@ public class DevChatActions { public static final String UPDATE_SETTING_REQUEST = "updateSetting/request"; public static final String UPDATE_SETTING_RESPONSE = "updateSetting/response"; public static final String SHOW_SETTING_DIALOG_REQUEST = "showSettingDialog/request"; + public static final String DELETE_LAST_CONVERSATION_REQUEST = "deleteLastConversation/request"; + public static final String DELETE_LAST_CONVERSATION_RESPONSE = "deleteLastConversation/response"; } diff --git a/src/main/java/ai/devchat/devchat/handler/DeleteLastConversationRequestHandler.java b/src/main/java/ai/devchat/devchat/handler/DeleteLastConversationRequestHandler.java new file mode 100644 index 0000000..5d5fab2 --- /dev/null +++ b/src/main/java/ai/devchat/devchat/handler/DeleteLastConversationRequestHandler.java @@ -0,0 +1,63 @@ +package ai.devchat.devchat.handler; + +import ai.devchat.cli.DevChatWrapper; +import ai.devchat.common.DevChatPathUtil; +import ai.devchat.common.Log; +import ai.devchat.devchat.ActionHandler; +import ai.devchat.devchat.DevChatActionHandler; +import ai.devchat.devchat.DevChatActions; +import com.alibaba.fastjson.JSONObject; + +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class DeleteLastConversationRequestHandler implements ActionHandler { + private JSONObject metadata; + private JSONObject payload; + private final DevChatActionHandler devChatActionHandler; + + public DeleteLastConversationRequestHandler(DevChatActionHandler devChatActionHandler) { + this.devChatActionHandler = devChatActionHandler; + } + + @Override + public void executeAction() { + Log.info("Handling delete last conversation request"); + String callbackFunc = metadata.getString("callback"); + String promptHash = payload.getString("promptHash"); + + Map> flags = new HashMap<>(); + flags.put("delete", Collections.singletonList(promptHash)); + + String devchatCommandPath = DevChatPathUtil.getDevchatBinPath(); + DevChatWrapper devchatWrapper = new DevChatWrapper(devchatCommandPath); + + try { + devchatWrapper.runLogCommand(flags); + + devChatActionHandler.sendResponse(DevChatActions.DELETE_LAST_CONVERSATION_RESPONSE, callbackFunc, (metadata, payload) -> { + metadata.put("status", "success"); + metadata.put("error", ""); + payload.put("promptHash", promptHash); + }); + } catch (Exception e) { + devChatActionHandler.sendResponse(DevChatActions.DELETE_LAST_CONVERSATION_RESPONSE, callbackFunc, (metadata, payload) -> { + metadata.put("status", "error"); + metadata.put("error", e.getMessage()); + payload.put("promptHash", promptHash); + }); + } + } + + @Override + public void setMetadata(JSONObject metadata) { + this.metadata = metadata; + } + + @Override + public void setPayload(JSONObject payload) { + this.payload = payload; + } +} diff --git a/src/main/java/ai/devchat/devchat/handler/ListModelsRequestHandler.java b/src/main/java/ai/devchat/devchat/handler/ListModelsRequestHandler.java index 8ddc518..5519d66 100644 --- a/src/main/java/ai/devchat/devchat/handler/ListModelsRequestHandler.java +++ b/src/main/java/ai/devchat/devchat/handler/ListModelsRequestHandler.java @@ -18,7 +18,6 @@ public ListModelsRequestHandler(DevChatActionHandler devChatActionHandler) { this.devChatActionHandler = devChatActionHandler; } - @Override public void executeAction() { Log.info("Handling list model request");