From 8b7ebcbd614584ef506756d131266a31e0392d41 Mon Sep 17 00:00:00 2001 From: Daniel Hu Date: Wed, 15 Nov 2023 16:35:58 +0800 Subject: [PATCH] Feat: api addContext/request is ready Signed-off-by: Daniel Hu --- .../devchat/devchat/ActionHandlerFactory.java | 1 + .../handler/AddContextRequestHandler.java | 100 ++++++++++++++++++ src/main/resources/static/main.js | 2 +- 3 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 src/main/java/ai/devchat/devchat/handler/AddContextRequestHandler.java diff --git a/src/main/java/ai/devchat/devchat/ActionHandlerFactory.java b/src/main/java/ai/devchat/devchat/ActionHandlerFactory.java index bf49cbd..0be1668 100644 --- a/src/main/java/ai/devchat/devchat/ActionHandlerFactory.java +++ b/src/main/java/ai/devchat/devchat/ActionHandlerFactory.java @@ -20,6 +20,7 @@ public class ActionHandlerFactory { put(DevChatActions.VIEW_DIFF_REQUEST, ViewDiffRequestHandler.class); put(DevChatActions.LIST_CONTEXTS_REQUEST, ListContextsRequestHandler.class); put(DevChatActions.LIST_MODELS_REQUEST, ListModelsRequestHandler.class); + put(DevChatActions.ADD_CONTEXT_REQUEST, AddContextRequestHandler.class); } }; diff --git a/src/main/java/ai/devchat/devchat/handler/AddContextRequestHandler.java b/src/main/java/ai/devchat/devchat/handler/AddContextRequestHandler.java new file mode 100644 index 0000000..2ae19bc --- /dev/null +++ b/src/main/java/ai/devchat/devchat/handler/AddContextRequestHandler.java @@ -0,0 +1,100 @@ +package ai.devchat.devchat.handler; + +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.io.BufferedReader; +import java.io.File; +import java.io.IOException; +import java.io.InputStreamReader; + +public class AddContextRequestHandler implements ActionHandler { + private JSONObject metadata; + private JSONObject payload; + private final DevChatActionHandler devChatActionHandler; + + public AddContextRequestHandler(DevChatActionHandler devChatActionHandler) { + this.devChatActionHandler = devChatActionHandler; + } + + @Override + public void executeAction() { + Log.info("Handling add context request"); + + String command = payload.getString("command"); + StringBuilder result = new StringBuilder(); + StringBuilder error = new StringBuilder(); + + BufferedReader reader = null; + BufferedReader errorReader = null; + + try { + String projectDir = devChatActionHandler.getProject().getBasePath(); + Process process = Runtime.getRuntime().exec(command, null, new File(projectDir)); + reader = new BufferedReader(new InputStreamReader(process.getInputStream())); + String line; + while ((line = reader.readLine()) != null) { + result.append(line).append("\n"); + } + + errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream())); + while ((line = errorReader.readLine()) != null) { + error.append(line).append("\n"); + } + } catch (IOException e) { + error.append(e.getMessage()); + } finally { + if (reader != null) { + try { + reader.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + if (errorReader != null) { + try { + errorReader.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + String callbackFunc = metadata.getString("callback"); + + Log.info("1111111"); + Log.info(result.toString()); + Log.info(error.toString()); + + if (error.isEmpty()) { + final String finalResult = result.toString(); + devChatActionHandler.sendResponse(DevChatActions.ADD_CONTEXT_RESPONSE, callbackFunc, (metadata, payload) -> { + metadata.put("status", "success"); + metadata.put("error", ""); + payload.put("command", command); + payload.put("content", finalResult); + }); + } else { + final String finalError = error.toString(); + devChatActionHandler.sendResponse(DevChatActions.ADD_CONTEXT_RESPONSE, callbackFunc, (metadata, payload) -> { + metadata.put("status", "error"); + metadata.put("error", finalError); + payload.put("command", command); + payload.put("content", ""); + }); + } + } + + @Override + public void setMetadata(JSONObject metadata) { + this.metadata = metadata; + } + + @Override + public void setPayload(JSONObject payload) { + this.payload = payload; + } +} diff --git a/src/main/resources/static/main.js b/src/main/resources/static/main.js index 47af98d..62ca485 100644 --- a/src/main/resources/static/main.js +++ b/src/main/resources/static/main.js @@ -3,7 +3,7 @@ setTimeout(function () { }, 2000); function displayResponseFromJava(message) { - document.getElementById('response').innerHTML = message; + document.getElementById('response').innerHTML = JSON.stringify(message); } document.getElementById('sendButton').addEventListener('click', function () {