refactor: Expand and explicitly handle cases where a ServiceType is checked (#521)

This streamlines changes to ServiceType, where any additions or removals will be flagged at compile time to be handled, instead of silently falling back to a default value.
This commit is contained in:
Jack Boswell 2024-05-07 19:42:45 +12:00 committed by GitHub
parent 5f5c9cbfa1
commit f44fab551b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 35 additions and 33 deletions

View file

@ -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;
@ -226,19 +224,15 @@ public final class CompletionRequestService {
}
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 -> CredentialsStore.INSTANCE.isCredentialSet(CredentialKey.OPENAI_API_KEY);
case AZURE -> CredentialsStore.INSTANCE.isCredentialSet(
AzureSettings.getCurrentState().isUseAzureApiKeyAuthentication()
? CredentialKey.AZURE_OPENAI_API_KEY
: CredentialKey.AZURE_ACTIVE_DIRECTORY_TOKEN);
case CUSTOM_OPENAI, ANTHROPIC, LLAMA_CPP -> true;
case YOU -> false;
};
}
/**

View file

@ -17,28 +17,31 @@ abstract class CodeCompletionFeatureToggleActions(
override fun actionPerformed(e: AnActionEvent) {
GeneralSettings.getCurrentState().selectedService
.takeIf { it in listOf(OPENAI, CUSTOM_OPENAI, LLAMA_CPP) }
?.also { selectedService ->
if (OPENAI == selectedService) {
OpenAISettings.getCurrentState().isCodeCompletionsEnabled = enableFeatureAction
} else if (CUSTOM_OPENAI == selectedService) {
service<CustomServiceSettings>().state.codeCompletionSettings.codeCompletionsEnabled =
enableFeatureAction
} else {
LlamaSettings.getCurrentState().isCodeCompletionsEnabled = enableFeatureAction
}
}
when (GeneralSettings.getCurrentState().selectedService) {
OPENAI -> OpenAISettings.getCurrentState().isCodeCompletionsEnabled = enableFeatureAction
LLAMA_CPP -> LlamaSettings.getCurrentState().isCodeCompletionsEnabled = enableFeatureAction
CUSTOM_OPENAI -> service<CustomServiceSettings>().state.codeCompletionSettings.codeCompletionsEnabled =
enableFeatureAction
ANTHROPIC,
AZURE,
YOU,
null -> { /* no-op for these services */ }
}
}
override fun update(e: AnActionEvent) {
val selectedService = GeneralSettings.getCurrentState().selectedService
val codeCompletionEnabled = isCodeCompletionsEnabled(selectedService)
e.presentation.isEnabled = codeCompletionEnabled != enableFeatureAction
e.presentation.isVisible =
e.presentation.isEnabled && listOf(OPENAI, CUSTOM_OPENAI, LLAMA_CPP).contains(
selectedService
)
e.presentation.isVisible = when (selectedService) {
OPENAI,
CUSTOM_OPENAI,
LLAMA_CPP -> true
ANTHROPIC,
AZURE,
YOU,
null -> false
}
}
override fun getActionUpdateThread(): ActionUpdateThread {
@ -50,7 +53,9 @@ abstract class CodeCompletionFeatureToggleActions(
OPENAI -> OpenAISettings.getCurrentState().isCodeCompletionsEnabled
CUSTOM_OPENAI -> service<CustomServiceSettings>().state.codeCompletionSettings.codeCompletionsEnabled
LLAMA_CPP -> LlamaSettings.isCodeCompletionsPossible()
else -> false
ANTHROPIC,
AZURE,
YOU -> false
}
}
}

View file

@ -70,7 +70,10 @@ class CodeGPTInlineCompletionProvider : InlineCompletionProvider {
ServiceType.OPENAI -> OpenAISettings.getCurrentState().isCodeCompletionsEnabled
ServiceType.CUSTOM_OPENAI -> service<CustomServiceSettings>().state.codeCompletionSettings.codeCompletionsEnabled
ServiceType.LLAMA_CPP -> LlamaSettings.getCurrentState().isCodeCompletionsEnabled
else -> false
ServiceType.ANTHROPIC,
ServiceType.AZURE,
ServiceType.YOU,
null -> false
}
return event is InlineCompletionEvent.DocumentChange && codeCompletionsEnabled
}