Skip to content

Commit

Permalink
Feat: new api updateSetting/request
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 21, 2023
1 parent 7df12ae commit 0cc3309
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 1 deletion.
2 changes: 1 addition & 1 deletion api/setting.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
},
"payload": {
"setting": {
"currentModel": "gpt-3.5"
"currentModel": "gpt-3.5-turbo"
}
}
}
Expand Down
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 @@ -24,6 +24,7 @@ public class ActionHandlerFactory {
put(DevChatActions.GET_KEY_REQUEST, GetKeyRequestHandler.class);
put(DevChatActions.COMMIT_CODE_REQUEST, CommitCodeRequestHandler.class);
put(DevChatActions.GET_SETTING_REQUEST, GetSettingRequestHandler.class);
put(DevChatActions.UPDATE_SETTING_REQUEST, UpdateSettingRequestHandler.class);
}
};

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/ai/devchat/devchat/DevChatActions.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ public class DevChatActions {
public static final String COMMIT_CODE_RESPONSE = "commitCode/response";
public static final String GET_SETTING_REQUEST = "getSetting/request";
public static final String GET_SETTING_RESPONSE = "getSetting/response";
public static final String UPDATE_SETTING_REQUEST = "updateSetting/request";
public static final String UPDATE_SETTING_RESPONSE = "updateSetting/response";
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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;
Expand All @@ -18,6 +19,7 @@ public GetSettingRequestHandler(DevChatActionHandler devChatActionHandler) {

@Override
public void executeAction() {
Log.info("Handling getSetting request");
String callbackFunc = metadata.getString("callback");

DevChatSettingsState settings = DevChatSettingsState.getInstance();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package ai.devchat.devchat.handler;

import ai.devchat.devchat.ActionHandler;
import ai.devchat.devchat.DevChatActionHandler;
import ai.devchat.devchat.DevChatActions;
import ai.devchat.idea.setting.DevChatSettingsState;
import com.alibaba.fastjson.JSONObject;

public class UpdateSettingRequestHandler implements ActionHandler {
private JSONObject metadata;
private JSONObject payload;
private final DevChatActionHandler devChatActionHandler;

public UpdateSettingRequestHandler(DevChatActionHandler devChatActionHandler) {
this.devChatActionHandler = devChatActionHandler;
}

@Override
public void executeAction() {
String callbackFunc = metadata.getString("callback");
JSONObject setting = payload.getJSONObject("setting");

try {
DevChatSettingsState settings = DevChatSettingsState.getInstance();
if (setting.containsKey("currentModel")) {
settings.defaultModel = setting.getString("currentModel");
}

devChatActionHandler.sendResponse(DevChatActions.UPDATE_SETTING_RESPONSE, callbackFunc, (metadata, payload) -> {
metadata.put("status", "success");
metadata.put("error", "");
});
} catch (Exception e) {
devChatActionHandler.sendResponse(DevChatActions.UPDATE_SETTING_RESPONSE, callbackFunc, (metadata, payload) -> {
metadata.put("status", "Failed");
metadata.put("error", "Failed to update setting." + e.getMessage());
});
}
}

@Override
public void setMetadata(JSONObject metadata) {
this.metadata = metadata;
}

@Override
public void setPayload(JSONObject payload) {
this.payload = payload;
}
}

0 comments on commit 0cc3309

Please sign in to comment.