Skip to content

Commit

Permalink
Feat: sendMessage api now supports context logic
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Hu <[email protected]>
  • Loading branch information
daniel-hutao committed Nov 15, 2023
1 parent ca0f801 commit ee7b30b
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 26 deletions.
2 changes: 1 addition & 1 deletion api/chat.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"parent": "b67937dd845afdf59dec505efafd2d18854cb2b147ae0cecd7f266d856373137"
},
"payload": {
"context": [
"contexts": [
{
"type": "code",
"languageId": "python",
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/ai/devchat/cli/DevChatWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ private void execCommand(List<String> commands, Consumer<String> 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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<String, String> flags = new HashMap<>();
if (context != null && !context.isEmpty()) {
flags.put("context", context);

JSONArray contextArray = payload.getJSONArray("contexts");
List<String> 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);
}
Expand Down Expand Up @@ -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.");
});
}
}

Expand Down Expand Up @@ -112,22 +138,53 @@ private String handleCommandAndInstruct(String message, Map<String, String> flag
@NotNull
private DevChatResponseConsumer getResponseConsumer(String responseFunc) {
Consumer<DevChatResponse> 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;
}
Expand Down

0 comments on commit ee7b30b

Please sign in to comment.