Skip to content

Commit

Permalink
Feat: multi-context support
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 20, 2023
1 parent 13c86ca commit 298d7f7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
35 changes: 21 additions & 14 deletions src/main/java/ai/devchat/cli/DevChatWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ private String execCommand(List<String> commands) {
}
if (apiKey != null) {
pb.environment().put("OPENAI_API_KEY", apiKey);
Log.info("api_key: " + apiKey.substring(0, 5) + "..." + apiKey.substring(apiKey.length() - 4, apiKey.length()));
Log.info("api_key: " + apiKey.substring(0, 5) + "..."
+ apiKey.substring(apiKey.length() - 4, apiKey.length()));
}

try {
Expand Down Expand Up @@ -66,7 +67,8 @@ private void execCommand(List<String> commands, Consumer<String> callback) {
}
if (apiKey != null) {
pb.environment().put("OPENAI_API_KEY", apiKey);
Log.info("api_key: " + apiKey.substring(0, 5) + "..." + apiKey.substring(apiKey.length() - 4, apiKey.length()));
Log.info("api_key: " + apiKey.substring(0, 5) + "..."
+ apiKey.substring(apiKey.length() - 4, apiKey.length()));
}

try {
Expand All @@ -76,9 +78,11 @@ 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: " + String.join(" ", pb.command()) + " 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: " + String.join(" ", pb.command()) + " 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: " + String.join(" ", pb.command()));
Expand Down Expand Up @@ -108,7 +112,7 @@ private void readOutputByLine(InputStream inputStream, Consumer<String> callback
}
}

public void runPromptCommand(Map<String, String> flags, String message, Consumer<String> callback) {
public void runPromptCommand(Map<String, List<String>> flags, String message, Consumer<String> callback) {
try {
List<String> commands = prepareCommand("prompt", flags, message);
execCommand(commands, callback);
Expand All @@ -117,7 +121,7 @@ public void runPromptCommand(Map<String, String> flags, String message, Consumer
}
}

public String runLogCommand(Map<String, String> flags) {
public String runLogCommand(Map<String, List<String>> flags) {
try {
List<String> commands = prepareCommand(flags, "log");
return execCommand(commands);
Expand All @@ -141,7 +145,8 @@ public String[] getCommandNamesList() {
}

public JSONArray listConversationsInOneTopic(String topicHash) {
String result = runLogCommand(Map.of("topic", topicHash, "max-count", DEFAULT_LOG_MAX_COUNT));
String result = runLogCommand(Map.of("topic", Collections.singletonList(topicHash),
"max-count", Collections.singletonList(DEFAULT_LOG_MAX_COUNT)));
return JSON.parseArray(result);
}

Expand All @@ -150,7 +155,7 @@ public JSONArray listTopics() {
return JSON.parseArray(result);
}

public String runRunCommand(String subCommand, Map<String, String> flags) {
public String runRunCommand(String subCommand, Map<String, List<String>> flags) {
try {
List<String> commands = prepareCommand(flags, "run", subCommand);
return execCommand(commands);
Expand All @@ -160,7 +165,7 @@ public String runRunCommand(String subCommand, Map<String, String> flags) {
}
}

public String runTopicCommand(String subCommand, Map<String, String> flags) {
public String runTopicCommand(String subCommand, Map<String, List<String>> flags) {
try {
List<String> commands = prepareCommand(flags, "topic", subCommand);
return execCommand(commands);
Expand All @@ -169,21 +174,23 @@ public String runTopicCommand(String subCommand, Map<String, String> flags) {
}
}

private List<String> prepareCommand(Map<String, String> flags, String... subCommands) {
private List<String> prepareCommand(Map<String, List<String>> flags, String... subCommands) {
List<String> commands = new ArrayList<>();
commands.add(command);
Collections.addAll(commands, subCommands);
if (flags == null) {
return commands;
}
flags.forEach((flag, value) -> {
commands.add("--" + flag);
commands.add(value);
flags.forEach((flag, values) -> {
for (String value : values) {
commands.add("--" + flag);
commands.add(value);
}
});
return commands;
}

private List<String> prepareCommand(String subCommand, Map<String, String> flags, String message) {
private List<String> prepareCommand(String subCommand, Map<String, List<String>> flags, String message) {
List<String> commands = prepareCommand(flags, subCommand);
// Add the message to the command list
if (message != null && !message.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -45,9 +46,9 @@ public void executeAction() {
String callbackFunc = metadata.getString("callback");

try {
Map<String, String> flags = new HashMap<>();

Map<String, List<String>> flags = new HashMap<>();
JSONArray contextArray = payload.getJSONArray("contexts");

if (contextArray != null && contextArray.size() > 0) {
List<String> contextFilePaths = new ArrayList<>();
for (int i = 0; i < contextArray.size(); i++) {
Expand All @@ -68,11 +69,11 @@ public void executeAction() {
Log.info("Context file path: " + contextPath);
}
}
flags.put("context", String.join(",", contextFilePaths));
flags.put("context", contextFilePaths);
}

if (parent != null && !parent.isEmpty()) {
flags.put("parent", parent);
flags.put("parent", Collections.singletonList(parent));
}

Log.info("Preparing to retrieve the command in the message...");
Expand Down Expand Up @@ -109,7 +110,7 @@ public void executeAction() {
}
}

private String handleCommandAndInstruct(String message, Map<String, String> flags) throws IOException {
private String handleCommandAndInstruct(String message, Map<String, List<String>> flags) throws IOException {
DevChatWrapper devchatWrapper = new DevChatWrapper(DevChatPathUtil.getDevchatBinPath());
String[] commandNamesList = devchatWrapper.getCommandNamesList();
Log.info("Command names list: " + String.join(", ", commandNamesList));
Expand All @@ -130,7 +131,7 @@ private String handleCommandAndInstruct(String message, Map<String, String> flag
Files.write(tempFile, runResult.getBytes());

// Add the temporary file path to the flags with key --instruct
flags.put("instruct", tempFile.toString());
flags.put("instruct", Collections.singletonList(tempFile.toString()));
}

return message;
Expand Down

0 comments on commit 298d7f7

Please sign in to comment.