Skip to content

Commit

Permalink
Feat: new api getSetting/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 0bbd607 commit 7df12ae
Show file tree
Hide file tree
Showing 11 changed files with 220 additions and 46 deletions.
93 changes: 93 additions & 0 deletions api/setting.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
## Get Setting

### JS to Java

```json
{
"action": "getSetting/request",
"metadata": {
"callback": "responseFunctionName"
},
"payload": null
}
```

### Java to JS

- success

```json
{
"action": "getSetting/response",
"metadata": {
"status": "success",
"error": ""
},
"payload": {
"setting": {
"apiKey": "DC.abcdef123456",
"apiBase": "https://api.example.com/v1",
"currentModel": "gpt-4"
}
}
}
```

- failed

```json
{
"action": "getSetting/response",
"metadata": {
"status": "Failed",
"error": "Failed to get setting."
},
"payload": null
}
```

## Set or Update Setting

### JS to Java

```json
{
"action": "updateSetting/request",
"metadata": {
"callback": "responseFunctionName"
},
"payload": {
"setting": {
"currentModel": "gpt-3.5"
}
}
}
```

### Java to JS

- success

```json
{
"action": "updateSetting/response",
"metadata": {
"status": "success",
"error": ""
},
"payload": null
}
```

- failed

```json
{
"action": "updateSetting/response",
"metadata": {
"status": "Failed",
"error": "Failed to update setting."
},
"payload": null
}
```
4 changes: 3 additions & 1 deletion src/main/java/ai/devchat/cli/DevChatWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@
public class DevChatWrapper {
private String apiBase;
private String apiKey;
private String currentModel;
private String command;
private final String DEFAULT_LOG_MAX_COUNT = "100";

public DevChatWrapper(String command) {
this.command = command;
}

public DevChatWrapper(String apiBase, String apiKey, String command) {
public DevChatWrapper(String apiBase, String apiKey, String currentModel, String command) {
this.apiBase = apiBase;
this.apiKey = apiKey;
this.currentModel = currentModel;
this.command = command;
}

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 @@ -23,6 +23,7 @@ public class ActionHandlerFactory {
put(DevChatActions.ADD_CONTEXT_REQUEST, AddContextRequestHandler.class);
put(DevChatActions.GET_KEY_REQUEST, GetKeyRequestHandler.class);
put(DevChatActions.COMMIT_CODE_REQUEST, CommitCodeRequestHandler.class);
put(DevChatActions.GET_SETTING_REQUEST, GetSettingRequestHandler.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 @@ -28,4 +28,6 @@ public class DevChatActions {
public static final String GET_KEY_RESPONSE = "getKey/response";
public static final String COMMIT_CODE_REQUEST = "commitCode/request";
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";
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import ai.devchat.devchat.ActionHandler;
import ai.devchat.devchat.DevChatActionHandler;
import ai.devchat.devchat.DevChatActions;
import ai.devchat.idea.storage.SensitiveDataStorage;
import com.alibaba.fastjson.JSONObject;

Expand All @@ -20,13 +21,13 @@ public void executeAction() {
String key = SensitiveDataStorage.getKey();

if (key != null && !key.isEmpty()) {
devChatActionHandler.sendResponse("getKey/response", callbackFunc, (metadata, payload) -> {
devChatActionHandler.sendResponse(DevChatActions.GET_KEY_RESPONSE, callbackFunc, (metadata, payload) -> {
metadata.put("status", "success");
metadata.put("error", "");
payload.put("key", key);
});
} else {
devChatActionHandler.sendResponse("getKey/response", callbackFunc, (metadata, payload) -> {
devChatActionHandler.sendResponse(DevChatActions.GET_KEY_RESPONSE, callbackFunc, (metadata, payload) -> {
metadata.put("status", "error");
metadata.put("error", "key is empty");
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
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 ai.devchat.idea.storage.SensitiveDataStorage;
import com.alibaba.fastjson.JSONObject;

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

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

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

DevChatSettingsState settings = DevChatSettingsState.getInstance();
String apiKey = SensitiveDataStorage.getKey();
if (settings.apiBase == null || settings.apiBase.isEmpty()) {
if (apiKey.startsWith("sk-")) {
settings.apiBase = "https://api.openai.com/v1";
} else if (apiKey.startsWith("DC.")) {
settings.apiBase = "https://api.devchat.ai/v1";
}
}

String apiBase = settings.apiBase;
String currentModel = settings.defaultModel;

devChatActionHandler.sendResponse(DevChatActions.GET_SETTING_RESPONSE, callbackFunc, (metadata, payload) -> {
metadata.put("status", "success");
metadata.put("error", "");
JSONObject setting = new JSONObject();
setting.put("apiKey", apiKey);
setting.put("apiBase", apiBase);
setting.put("currentModel", currentModel);
payload.put("setting", setting);
});
}

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

@Override
public void setPayload(JSONObject payload) {
this.payload = payload;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public void executeAction() {
List<String> modelList = new ArrayList<>();
modelList.add("gpt-3.5-turbo");
modelList.add("gpt-4");
modelList.add("gpt-3.5-turbo-16k");
modelList.add("claude-2");

devChatActionHandler.sendResponse(DevChatActions.LIST_MODELS_RESPONSE, callbackFunc, (metadata, payload) -> {
metadata.put("status", "success");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,12 @@ public void executeAction() {
DevChatSettingsState settings = DevChatSettingsState.getInstance();
if (settings.apiBase != null && !settings.apiBase.isEmpty()) {
apiBase = settings.apiBase;
} else {
settings.apiBase = apiBase;
}

DevChatResponseConsumer responseConsumer = getResponseConsumer(callbackFunc);
DevChatWrapper devchatWrapper = new DevChatWrapper(apiBase, apiKey, devchatCommandPath);
DevChatWrapper devchatWrapper = new DevChatWrapper(apiBase, apiKey, settings.defaultModel, devchatCommandPath);
devchatWrapper.runPromptCommand(flags, message, responseConsumer);
} catch (Exception e) {
Log.error("Exception occurred while executing DevChat command. Exception message: " + e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ public class DevChatSettingsComponent {
private final JPanel jPanel;
private final JBTextField apiBaseText = new JBTextField();
private final JBTextField apiKeyText = new JBTextField();
private final JBTextField defaultModelText = new JBTextField();

public DevChatSettingsComponent() {
jPanel = FormBuilder.createFormBuilder()
.addLabeledComponent(new JBLabel("api_base"), apiBaseText, 1, false)

Check warning on line 22 in src/main/java/ai/devchat/idea/setting/DevChatSettingsComponent.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Incorrect string capitalization

String 'api_base' is not properly capitalized. It should have sentence capitalization
.addLabeledComponent(new JBLabel("api_key"), apiKeyText, 2, false)

Check warning on line 23 in src/main/java/ai/devchat/idea/setting/DevChatSettingsComponent.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Incorrect string capitalization

String 'api_key' is not properly capitalized. It should have sentence capitalization
.addLabeledComponent(new JBLabel("default_model"), defaultModelText, 3, false)

Check warning on line 24 in src/main/java/ai/devchat/idea/setting/DevChatSettingsComponent.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Incorrect string capitalization

String 'default_model' is not properly capitalized. It should have sentence capitalization
.addComponentFillVertically(new JPanel(), 0)
.getPanel();
}
Expand All @@ -41,11 +43,19 @@ public String getApiKey() {
return apiKeyText.getText();
}

public String getDefaultModel() {
return defaultModelText.getText();
}

public void setApiBase(@NotNull String apiBase) {
apiBaseText.setText(apiBase);
}

public void setApiKey(@NotNull String apiKey) {
apiKeyText.setText(apiKey);
}

public void setDefaultModel(@NotNull String defaultModel) {
defaultModelText.setText(defaultModel);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,54 +12,57 @@
*/
public class DevChatSettingsConfigurable implements Configurable {

private DevChatSettingsComponent devChatSettingsComponent;
private DevChatSettingsComponent devChatSettingsComponent;

// A default constructor with no arguments is required because this implementation
// is registered in an applicationConfigurable EP
// A default constructor with no arguments is required because this implementation
// is registered in an applicationConfigurable EP

@Nls(capitalization = Nls.Capitalization.Title)
@Override
public String getDisplayName() {
return "DevChat";
}
@Nls(capitalization = Nls.Capitalization.Title)
@Override
public String getDisplayName() {
return "DevChat";
}

@Override
public JComponent getPreferredFocusedComponent() {
return devChatSettingsComponent.getPreferredFocusedComponent();
}
@Override
public JComponent getPreferredFocusedComponent() {
return devChatSettingsComponent.getPreferredFocusedComponent();
}

@Nullable
@Override
public JComponent createComponent() {
devChatSettingsComponent = new DevChatSettingsComponent();
return devChatSettingsComponent.getPanel();
}
@Nullable
@Override
public JComponent createComponent() {
devChatSettingsComponent = new DevChatSettingsComponent();
return devChatSettingsComponent.getPanel();
}

@Override
public boolean isModified() {
DevChatSettingsState settings = DevChatSettingsState.getInstance();
return !devChatSettingsComponent.getApiBase().equals(settings.apiBase) ||
!devChatSettingsComponent.getApiKey().equals(settings.apiKey);
}
@Override
public boolean isModified() {
DevChatSettingsState settings = DevChatSettingsState.getInstance();
return !devChatSettingsComponent.getApiBase().equals(settings.apiBase) ||
!devChatSettingsComponent.getApiKey().equals(settings.apiKey) ||
!devChatSettingsComponent.getDefaultModel().equals(settings.defaultModel);
}

@Override
public void apply() {
DevChatSettingsState settings = DevChatSettingsState.getInstance();
settings.apiBase = devChatSettingsComponent.getApiBase();
settings.apiKey = devChatSettingsComponent.getApiKey();
SensitiveDataStorage.setKey(settings.apiKey);
}
@Override
public void apply() {
DevChatSettingsState settings = DevChatSettingsState.getInstance();
settings.apiBase = devChatSettingsComponent.getApiBase();
settings.apiKey = devChatSettingsComponent.getApiKey();
settings.defaultModel = devChatSettingsComponent.getDefaultModel();
SensitiveDataStorage.setKey(settings.apiKey);
}

@Override
public void reset() {
DevChatSettingsState settings = DevChatSettingsState.getInstance();
devChatSettingsComponent.setApiBase(settings.apiBase);
devChatSettingsComponent.setApiKey(settings.apiKey);
SensitiveDataStorage.setKey(settings.apiKey);
}
@Override
public void reset() {
DevChatSettingsState settings = DevChatSettingsState.getInstance();
devChatSettingsComponent.setApiBase(settings.apiBase);
devChatSettingsComponent.setApiKey(settings.apiKey);
devChatSettingsComponent.setDefaultModel(settings.defaultModel);
SensitiveDataStorage.setKey(settings.apiKey);
}

@Override
public void disposeUIResources() {
devChatSettingsComponent = null;
}
@Override
public void disposeUIResources() {
devChatSettingsComponent = null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class DevChatSettingsState implements PersistentStateComponent<DevChatSet

public String apiKey = "change_me";

public String defaultModel = "gpt-3.5-turbo";

public static DevChatSettingsState getInstance() {
return ApplicationManager.getApplication().getService(DevChatSettingsState.class);
}
Expand Down

0 comments on commit 7df12ae

Please sign in to comment.