Skip to content

Commit b962066

Browse files
committed
fix: Enable Generate message also in non-modal commit dialog (#443)
1 parent a913143 commit b962066

File tree

6 files changed

+36
-26
lines changed

6 files changed

+36
-26
lines changed

src/main/java/ee/carlrobert/codegpt/actions/GenerateGitCommitMessageAction.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import ee.carlrobert.codegpt.Icons;
2929
import ee.carlrobert.codegpt.completions.CompletionRequestService;
3030
import ee.carlrobert.codegpt.settings.GeneralSettings;
31+
import ee.carlrobert.codegpt.settings.service.ServiceType;
3132
import ee.carlrobert.codegpt.ui.OverlayUtil;
3233
import ee.carlrobert.llm.client.openai.completion.ErrorDetails;
3334
import ee.carlrobert.llm.completion.CompletionEventListener;
@@ -61,16 +62,16 @@ public GenerateGitCommitMessageAction() {
6162
@Override
6263
public void update(@NotNull AnActionEvent event) {
6364
var commitWorkflowUi = event.getData(VcsDataKeys.COMMIT_WORKFLOW_UI);
64-
var selectedService = GeneralSettings.getCurrentState().getSelectedService();
65-
if (selectedService == YOU || commitWorkflowUi == null) {
65+
ServiceType selectedService;
66+
if (commitWorkflowUi == null
67+
|| YOU == (selectedService = GeneralSettings.getCurrentState().getSelectedService())) {
6668
event.getPresentation().setVisible(false);
6769
return;
6870
}
6971

70-
var callAllowed = CompletionRequestService.isRequestAllowed(
71-
GeneralSettings.getCurrentState().getSelectedService());
72-
event.getPresentation().setEnabled(callAllowed
73-
&& new CommitWorkflowChanges(commitWorkflowUi).isFilesSelected());
72+
var callAllowed = CompletionRequestService.isRequestAllowed(selectedService);
73+
boolean enabled = callAllowed && new CommitWorkflowChanges(commitWorkflowUi).isFilesSelected();
74+
event.getPresentation().setEnabled(enabled);
7475
event.getPresentation().setText(CodeGPTBundle.get(callAllowed
7576
? "action.generateCommitMessage.title"
7677
: "action.generateCommitMessage.missingCredentials"));

src/main/java/ee/carlrobert/codegpt/completions/CompletionRequestService.java

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package ee.carlrobert.codegpt.completions;
22

3-
import static ee.carlrobert.codegpt.settings.service.ServiceType.ANTHROPIC;
4-
import static ee.carlrobert.codegpt.settings.service.ServiceType.AZURE;
53
import static ee.carlrobert.codegpt.settings.service.ServiceType.CUSTOM_OPENAI;
64
import static ee.carlrobert.codegpt.settings.service.ServiceType.LLAMA_CPP;
75
import static ee.carlrobert.codegpt.settings.service.ServiceType.OPENAI;
@@ -14,8 +12,6 @@
1412
import ee.carlrobert.codegpt.codecompletions.InfillRequestDetails;
1513
import ee.carlrobert.codegpt.completions.llama.LlamaModel;
1614
import ee.carlrobert.codegpt.completions.llama.PromptTemplate;
17-
import ee.carlrobert.codegpt.credentials.CredentialsStore;
18-
import ee.carlrobert.codegpt.credentials.CredentialsStore.CredentialKey;
1915
import ee.carlrobert.codegpt.settings.GeneralSettings;
2016
import ee.carlrobert.codegpt.settings.configuration.ConfigurationSettings;
2117
import ee.carlrobert.codegpt.settings.service.ServiceType;
@@ -210,19 +206,12 @@ public boolean isRequestAllowed() {
210206
}
211207

212208
public static boolean isRequestAllowed(ServiceType serviceType) {
213-
if (serviceType == OPENAI
214-
&& CredentialsStore.INSTANCE.isCredentialSet(CredentialKey.OPENAI_API_KEY)) {
215-
return true;
216-
}
217-
218-
var azureCredentialKey = AzureSettings.getCurrentState().isUseAzureApiKeyAuthentication()
219-
? CredentialKey.AZURE_OPENAI_API_KEY
220-
: CredentialKey.AZURE_ACTIVE_DIRECTORY_TOKEN;
221-
if (serviceType == AZURE && CredentialsStore.INSTANCE.isCredentialSet(azureCredentialKey)) {
222-
return true;
223-
}
224-
225-
return List.of(LLAMA_CPP, ANTHROPIC, CUSTOM_OPENAI).contains(serviceType);
209+
return switch (serviceType) {
210+
case OPENAI -> OpenAISettings.isCredentialSet();
211+
case AZURE -> AzureSettings.isCredentialSet();
212+
case LLAMA_CPP, ANTHROPIC, CUSTOM_OPENAI -> true;
213+
case YOU -> false;
214+
};
226215
}
227216

228217
/**

src/main/java/ee/carlrobert/codegpt/settings/service/azure/AzureSettings.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ public static AzureSettings getInstance() {
3535
return ApplicationManager.getApplication().getService(AzureSettings.class);
3636
}
3737

38+
public static boolean isCredentialSet() {
39+
return getCurrentState().isCredentialSet();
40+
}
41+
3842
public boolean isModified(AzureSettingsForm form) {
3943
return !form.getCurrentState().equals(state)
4044
|| !StringUtils.equals(

src/main/java/ee/carlrobert/codegpt/settings/service/azure/AzureSettingsState.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package ee.carlrobert.codegpt.settings.service.azure;
22

3+
import ee.carlrobert.codegpt.credentials.CredentialsStore;
34
import java.util.Objects;
45

56
public class AzureSettingsState {
@@ -51,6 +52,16 @@ public void setUseAzureActiveDirectoryAuthentication(
5152
this.useAzureActiveDirectoryAuthentication = useAzureActiveDirectoryAuthentication;
5253
}
5354

55+
public CredentialsStore.CredentialKey getCredentialKey() {
56+
return isUseAzureApiKeyAuthentication()
57+
? CredentialsStore.CredentialKey.AZURE_OPENAI_API_KEY
58+
: CredentialsStore.CredentialKey.AZURE_ACTIVE_DIRECTORY_TOKEN;
59+
}
60+
61+
public boolean isCredentialSet() {
62+
return CredentialsStore.INSTANCE.isCredentialSet(getCredentialKey());
63+
}
64+
5465
@Override
5566
public boolean equals(Object o) {
5667
if (this == o) {

src/main/java/ee/carlrobert/codegpt/settings/service/openai/OpenAISettings.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,15 @@ public static OpenAISettings getInstance() {
3434
return ApplicationManager.getApplication().getService(OpenAISettings.class);
3535
}
3636

37+
public static boolean isCredentialSet() {
38+
return CredentialsStore.INSTANCE.isCredentialSet(OPENAI_API_KEY);
39+
}
40+
3741
public boolean isModified(OpenAISettingsForm form) {
3842
return !form.getCurrentState().equals(state)
3943
|| !StringUtils.equals(
4044
form.getApiKey(),
4145
CredentialsStore.INSTANCE.getCredential(OPENAI_API_KEY));
4246
}
47+
4348
}

src/main/kotlin/ee/carlrobert/codegpt/credentials/CredentialsStore.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ object CredentialsStore {
99
private val credentialsMap = mutableMapOf<CredentialKey, String?>()
1010

1111
fun loadAll() {
12-
CredentialKey.values().forEach {
12+
CredentialKey.entries.forEach {
1313
val credentialAttributes = CredentialAttributes(generateServiceName("CodeGPT", it.name))
1414
val password = PasswordSafe.instance.getPassword(credentialAttributes)
1515
setCredential(it, password)
@@ -22,7 +22,7 @@ object CredentialsStore {
2222
credentialsMap[key] = password
2323
}
2424

25-
fun isCredentialSet(key: CredentialKey): Boolean = !getCredential(key).isNullOrEmpty()
25+
fun isCredentialSet(key: CredentialKey): Boolean = !getCredential(key).isNullOrBlank()
2626

2727
enum class CredentialKey {
2828
OPENAI_API_KEY,
@@ -33,4 +33,4 @@ object CredentialsStore {
3333
YOU_ACCOUNT_PASSWORD,
3434
LLAMA_API_KEY
3535
}
36-
}
36+
}

0 commit comments

Comments
 (0)