Skip to content

Commit

Permalink
fix: Enable Generate message also in non-modal commit dialog (#443)
Browse files Browse the repository at this point in the history
  • Loading branch information
reneleonhardt committed Apr 12, 2024
1 parent a913143 commit b962066
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import ee.carlrobert.codegpt.Icons;
import ee.carlrobert.codegpt.completions.CompletionRequestService;
import ee.carlrobert.codegpt.settings.GeneralSettings;
import ee.carlrobert.codegpt.settings.service.ServiceType;
import ee.carlrobert.codegpt.ui.OverlayUtil;
import ee.carlrobert.llm.client.openai.completion.ErrorDetails;
import ee.carlrobert.llm.completion.CompletionEventListener;
Expand Down Expand Up @@ -61,16 +62,16 @@ public GenerateGitCommitMessageAction() {
@Override
public void update(@NotNull AnActionEvent event) {
var commitWorkflowUi = event.getData(VcsDataKeys.COMMIT_WORKFLOW_UI);
var selectedService = GeneralSettings.getCurrentState().getSelectedService();
if (selectedService == YOU || commitWorkflowUi == null) {
ServiceType selectedService;
if (commitWorkflowUi == null
|| YOU == (selectedService = GeneralSettings.getCurrentState().getSelectedService())) {
event.getPresentation().setVisible(false);
return;
}

var callAllowed = CompletionRequestService.isRequestAllowed(
GeneralSettings.getCurrentState().getSelectedService());
event.getPresentation().setEnabled(callAllowed
&& new CommitWorkflowChanges(commitWorkflowUi).isFilesSelected());
var callAllowed = CompletionRequestService.isRequestAllowed(selectedService);
boolean enabled = callAllowed && new CommitWorkflowChanges(commitWorkflowUi).isFilesSelected();
event.getPresentation().setEnabled(enabled);
event.getPresentation().setText(CodeGPTBundle.get(callAllowed
? "action.generateCommitMessage.title"
: "action.generateCommitMessage.missingCredentials"));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package ee.carlrobert.codegpt.completions;

import static ee.carlrobert.codegpt.settings.service.ServiceType.ANTHROPIC;
import static ee.carlrobert.codegpt.settings.service.ServiceType.AZURE;
import static ee.carlrobert.codegpt.settings.service.ServiceType.CUSTOM_OPENAI;
import static ee.carlrobert.codegpt.settings.service.ServiceType.LLAMA_CPP;
import static ee.carlrobert.codegpt.settings.service.ServiceType.OPENAI;
Expand All @@ -14,8 +12,6 @@
import ee.carlrobert.codegpt.codecompletions.InfillRequestDetails;
import ee.carlrobert.codegpt.completions.llama.LlamaModel;
import ee.carlrobert.codegpt.completions.llama.PromptTemplate;
import ee.carlrobert.codegpt.credentials.CredentialsStore;
import ee.carlrobert.codegpt.credentials.CredentialsStore.CredentialKey;
import ee.carlrobert.codegpt.settings.GeneralSettings;
import ee.carlrobert.codegpt.settings.configuration.ConfigurationSettings;
import ee.carlrobert.codegpt.settings.service.ServiceType;
Expand Down Expand Up @@ -210,19 +206,12 @@ public boolean isRequestAllowed() {
}

public static boolean isRequestAllowed(ServiceType serviceType) {
if (serviceType == OPENAI
&& CredentialsStore.INSTANCE.isCredentialSet(CredentialKey.OPENAI_API_KEY)) {
return true;
}

var azureCredentialKey = AzureSettings.getCurrentState().isUseAzureApiKeyAuthentication()
? CredentialKey.AZURE_OPENAI_API_KEY
: CredentialKey.AZURE_ACTIVE_DIRECTORY_TOKEN;
if (serviceType == AZURE && CredentialsStore.INSTANCE.isCredentialSet(azureCredentialKey)) {
return true;
}

return List.of(LLAMA_CPP, ANTHROPIC, CUSTOM_OPENAI).contains(serviceType);
return switch (serviceType) {
case OPENAI -> OpenAISettings.isCredentialSet();
case AZURE -> AzureSettings.isCredentialSet();
case LLAMA_CPP, ANTHROPIC, CUSTOM_OPENAI -> true;
case YOU -> false;
};
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public static AzureSettings getInstance() {
return ApplicationManager.getApplication().getService(AzureSettings.class);
}

public static boolean isCredentialSet() {
return getCurrentState().isCredentialSet();
}

public boolean isModified(AzureSettingsForm form) {
return !form.getCurrentState().equals(state)
|| !StringUtils.equals(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ee.carlrobert.codegpt.settings.service.azure;

import ee.carlrobert.codegpt.credentials.CredentialsStore;
import java.util.Objects;

public class AzureSettingsState {
Expand Down Expand Up @@ -51,6 +52,16 @@ public void setUseAzureActiveDirectoryAuthentication(
this.useAzureActiveDirectoryAuthentication = useAzureActiveDirectoryAuthentication;
}

public CredentialsStore.CredentialKey getCredentialKey() {
return isUseAzureApiKeyAuthentication()
? CredentialsStore.CredentialKey.AZURE_OPENAI_API_KEY
: CredentialsStore.CredentialKey.AZURE_ACTIVE_DIRECTORY_TOKEN;
}

public boolean isCredentialSet() {
return CredentialsStore.INSTANCE.isCredentialSet(getCredentialKey());
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,15 @@ public static OpenAISettings getInstance() {
return ApplicationManager.getApplication().getService(OpenAISettings.class);
}

public static boolean isCredentialSet() {
return CredentialsStore.INSTANCE.isCredentialSet(OPENAI_API_KEY);
}

public boolean isModified(OpenAISettingsForm form) {
return !form.getCurrentState().equals(state)
|| !StringUtils.equals(
form.getApiKey(),
CredentialsStore.INSTANCE.getCredential(OPENAI_API_KEY));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ object CredentialsStore {
private val credentialsMap = mutableMapOf<CredentialKey, String?>()

fun loadAll() {
CredentialKey.values().forEach {
CredentialKey.entries.forEach {
val credentialAttributes = CredentialAttributes(generateServiceName("CodeGPT", it.name))
val password = PasswordSafe.instance.getPassword(credentialAttributes)
setCredential(it, password)
Expand All @@ -22,7 +22,7 @@ object CredentialsStore {
credentialsMap[key] = password
}

fun isCredentialSet(key: CredentialKey): Boolean = !getCredential(key).isNullOrEmpty()
fun isCredentialSet(key: CredentialKey): Boolean = !getCredential(key).isNullOrBlank()

enum class CredentialKey {
OPENAI_API_KEY,
Expand All @@ -33,4 +33,4 @@ object CredentialsStore {
YOU_ACCOUNT_PASSWORD,
LLAMA_API_KEY
}
}
}

0 comments on commit b962066

Please sign in to comment.