diff --git a/api/chat.md b/api/chat.md index 42e47c6..296e35f 100644 --- a/api/chat.md +++ b/api/chat.md @@ -10,7 +10,7 @@ "parent": "b67937dd845afdf59dec505efafd2d18854cb2b147ae0cecd7f266d856373137" }, "payload": { - "context": [ + "contexts": [ { "type": "code", "languageId": "python", diff --git a/src/main/java/ai/devchat/cli/DevChatWrapper.java b/src/main/java/ai/devchat/cli/DevChatWrapper.java index aa47caa..1d4ad5d 100644 --- a/src/main/java/ai/devchat/cli/DevChatWrapper.java +++ b/src/main/java/ai/devchat/cli/DevChatWrapper.java @@ -76,13 +76,13 @@ private void execCommand(List commands, Consumer callback) { int exitCode = process.waitFor(); if (exitCode != 0) { String error = readOutput(process.getErrorStream()); - Log.error("Failed to execute command: " + commands + " Exit Code: " + exitCode + " Error: " + error); + Log.error("Failed to execute command: " + String.join(" ", pb.command()) + " Exit Code: " + exitCode + " Error: " + error); throw new RuntimeException( - "Failed to execute command: " + commands + " Exit Code: " + exitCode + " Error: " + error); + "Failed to execute command: " + String.join(" ", pb.command()) + " Exit Code: " + exitCode + " Error: " + error); } } catch (IOException | InterruptedException e) { - Log.error("Failed to execute command: " + commands); - throw new RuntimeException("Failed to execute command: " + commands, e); + Log.error("Failed to execute command: " + String.join(" ", pb.command())); + throw new RuntimeException("Failed to execute command: " + String.join(" ", pb.command()), e); } } diff --git a/src/main/java/ai/devchat/devchat/handler/SendMessageRequestHandler.java b/src/main/java/ai/devchat/devchat/handler/SendMessageRequestHandler.java index b79f864..335fd7e 100644 --- a/src/main/java/ai/devchat/devchat/handler/SendMessageRequestHandler.java +++ b/src/main/java/ai/devchat/devchat/handler/SendMessageRequestHandler.java @@ -11,13 +11,18 @@ import ai.devchat.idea.setting.DevChatSettingsState; import ai.devchat.idea.storage.SensitiveDataStorage; +import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import org.jetbrains.annotations.NotNull; +import java.io.File; +import java.io.FileWriter; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.function.Consumer; @@ -36,15 +41,35 @@ public void executeAction() { Log.info("Handling send message request"); String message = payload.getString("message"); - String context = payload.getString("context"); String parent = metadata.getString("parent"); String callbackFunc = metadata.getString("callback"); try { Map flags = new HashMap<>(); - if (context != null && !context.isEmpty()) { - flags.put("context", context); + + JSONArray contextArray = payload.getJSONArray("contexts"); + List contextFilePaths = new ArrayList<>(); + for (int i = 0; i < contextArray.size(); i++) { + JSONObject context = contextArray.getJSONObject(i); + String contextType = context.getString("type"); + String contextPath = null; + + if ("code".equals(contextType)) { + String path = context.getString("path"); + String filename = path.substring(path.lastIndexOf("/") + 1, path.length()); + contextPath = createTempFileFromContext(context, filename); + } else if ("command".equals(contextType)) { + contextPath = createTempFileFromContext(context, "custom.txt"); + } + + if (contextPath != null) { + contextFilePaths.add(contextPath); + Log.info("Context file path: " + contextPath); + } } + + flags.put("context", String.join(",", contextFilePaths)); + if (parent != null && !parent.isEmpty()) { flags.put("parent", parent); } @@ -73,12 +98,13 @@ public void executeAction() { } catch (Exception e) { Log.error("Exception occurred while executing DevChat command. Exception message: " + e.getMessage()); - devChatActionHandler.sendResponse(DevChatActions.SEND_MESSAGE_RESPONSE, callbackFunc, (metadata, payload) -> { - metadata.put("currentChunkId", 0); - metadata.put("isFinalChunk", true); - metadata.put("finishReason", "error"); - metadata.put("error", "Exception occurred while executing 'devchat' command."); - }); + devChatActionHandler.sendResponse(DevChatActions.SEND_MESSAGE_RESPONSE, callbackFunc, + (metadata, payload) -> { + metadata.put("currentChunkId", 0); + metadata.put("isFinalChunk", true); + metadata.put("finishReason", "error"); + metadata.put("error", "Exception occurred while executing 'devchat' command."); + }); } } @@ -112,22 +138,53 @@ private String handleCommandAndInstruct(String message, Map flag @NotNull private DevChatResponseConsumer getResponseConsumer(String responseFunc) { Consumer jsCallback = response -> { - devChatActionHandler.sendResponse(DevChatActions.SEND_MESSAGE_RESPONSE, responseFunc, (metadata, payload) -> { - currentChunkId += 1; - metadata.put("currentChunkId", currentChunkId); - metadata.put("isFinalChunk", response.getPromptHash() != null); - metadata.put("finishReason", response.getPromptHash() != null ? "success" : ""); - metadata.put("error", ""); - - payload.put("message", response.getMessage()); - payload.put("user", response.getUser()); - payload.put("date", response.getDate()); - payload.put("promptHash", response.getPromptHash()); - }); + devChatActionHandler.sendResponse(DevChatActions.SEND_MESSAGE_RESPONSE, responseFunc, + (metadata, payload) -> { + currentChunkId += 1; + metadata.put("currentChunkId", currentChunkId); + metadata.put("isFinalChunk", response.getPromptHash() != null); + metadata.put("finishReason", response.getPromptHash() != null ? "success" : ""); + metadata.put("error", ""); + + payload.put("message", response.getMessage()); + payload.put("user", response.getUser()); + payload.put("date", response.getDate()); + payload.put("promptHash", response.getPromptHash()); + }); }; return new DevChatResponseConsumer(jsCallback); } + private String createTempFileFromContext(JSONObject context, String filename) { + File tempFile; + + try { + tempFile = File.createTempFile("devchat-tmp-", "-" + filename); + } catch (IOException e) { + Log.error("Failed to create a temporary file." + e.getMessage()); + return null; + } + + JSONObject newJson = new JSONObject(); + if (context.getString("type").equals("code")) { + newJson.put("languageId", context.getString("languageId")); + newJson.put("path", context.getString("path")); + newJson.put("startLine", context.getInteger("startLine")); + newJson.put("content", context.getString("content")); + } else if (context.getString("type").equals("command")) { + newJson.put("command", context.getString("command")); + newJson.put("content", context.getString("content")); + } + + try (FileWriter fileWriter = new FileWriter(tempFile)) { + fileWriter.write(newJson.toJSONString()); + } catch (IOException e) { + Log.error("Failed to write to the temporary file." + e.getMessage()); + } + + return tempFile.getAbsolutePath(); + } + public void setMetadata(JSONObject metadata) { this.metadata = metadata; }