Skip to content

Commit

Permalink
Feat: api addContext/request is ready
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 ba3ba1e commit 8b7ebcb
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/main/java/ai/devchat/devchat/ActionHandlerFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};

Expand Down
100 changes: 100 additions & 0 deletions src/main/java/ai/devchat/devchat/handler/AddContextRequestHandler.java
Original file line number Diff line number Diff line change
@@ -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));

Check warning on line 36 in src/main/java/ai/devchat/devchat/handler/AddContextRequestHandler.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Nullability and data flow problems

Argument `projectDir` might be null
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;
}
}
2 changes: 1 addition & 1 deletion src/main/resources/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit 8b7ebcb

Please sign in to comment.