Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: fix: Enable Generate message also in non-modal commit dialog (#443) #453

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.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
Expand Up @@ -335,8 +335,7 @@ private List<OpenAIChatCompletionMessage> buildMessages(
CallParameters callParameters) {
var messages = buildMessages(callParameters);

if (model == null
|| GeneralSettings.getCurrentState().getSelectedService() == ServiceType.YOU) {
if (model == null || GeneralSettings.getSelectedService() == ServiceType.YOU) {
return messages;
}

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 @@ -68,7 +64,7 @@ public EventSource getChatCompletionAsync(
CallParameters callParameters,
CompletionEventListener<String> eventListener) {
var requestProvider = new CompletionRequestProvider(callParameters.getConversation());
return switch (GeneralSettings.getCurrentState().getSelectedService()) {
return switch (GeneralSettings.getSelectedService()) {
case OPENAI -> CompletionClientProvider.getOpenAIClient().getChatCompletionAsync(
requestProvider.buildOpenAIChatCompletionRequest(
OpenAISettings.getCurrentState().getModel(),
Expand Down Expand Up @@ -100,7 +96,7 @@ public EventSource getChatCompletionAsync(
public EventSource getCodeCompletionAsync(
InfillRequestDetails requestDetails,
CompletionEventListener<String> eventListener) {
return switch (GeneralSettings.getCurrentState().getSelectedService()) {
return switch (GeneralSettings.getSelectedService()) {
case OPENAI -> CompletionClientProvider.getOpenAIClient()
.getCompletionAsync(
CodeCompletionRequestFactory.INSTANCE.buildOpenAIRequest(requestDetails),
Expand All @@ -124,7 +120,7 @@ public void generateCommitMessageAsync(
new OpenAIChatCompletionStandardMessage("user", prompt)))
.setModel(OpenAISettings.getCurrentState().getModel())
.build();
var selectedService = GeneralSettings.getCurrentState().getSelectedService();
var selectedService = GeneralSettings.getSelectedService();
switch (selectedService) {
case OPENAI:
CompletionClientProvider.getOpenAIClient()
Expand Down Expand Up @@ -182,7 +178,7 @@ public void generateCommitMessageAsync(
}

public Optional<String> getLookupCompletion(String prompt) {
var selectedService = GeneralSettings.getCurrentState().getSelectedService();
var selectedService = GeneralSettings.getSelectedService();
if (selectedService == YOU || selectedService == LLAMA_CPP) {
return Optional.empty();
}
Expand All @@ -206,23 +202,16 @@ public Optional<String> getLookupCompletion(String prompt) {
}

public boolean isRequestAllowed() {
return isRequestAllowed(GeneralSettings.getCurrentState().getSelectedService());
return isRequestAllowed(GeneralSettings.getSelectedService());
}

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 @@ -46,8 +46,7 @@ public Conversation createConversation(String clientCode) {
conversation.setClientCode(clientCode);
conversation.setCreatedOn(LocalDateTime.now());
conversation.setUpdatedOn(LocalDateTime.now());
conversation.setModel(getModelForSelectedService(
GeneralSettings.getCurrentState().getSelectedService()));
conversation.setModel(getModelForSelectedService(GeneralSettings.getSelectedService()));
return conversation;
}

Expand Down Expand Up @@ -110,7 +109,7 @@ public void saveConversation(Conversation conversation) {
}

public Conversation startConversation() {
var completionCode = GeneralSettings.getCurrentState().getSelectedService().getCompletionCode();
var completionCode = GeneralSettings.getSelectedService().getCompletionCode();
var conversation = createConversation(completionCode);
conversationState.setCurrentConversation(conversation);
addConversation(conversation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ public static GeneralSettingsState getCurrentState() {
return getInstance().getState();
}

/**
* Selected service of current state.
*/
public static ServiceType getSelectedService() {
return getCurrentState().getSelectedService();
}

public static GeneralSettings getInstance() {
return ApplicationManager.getApplication().getService(GeneralSettings.class);
}
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,6 +34,10 @@ 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.intellij.openapi.Disposable;
import com.intellij.openapi.actionSystem.ActionPlaces;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.impl.EditorImpl;
import com.intellij.openapi.project.Project;
import com.intellij.ui.JBColor;
import com.intellij.util.ui.JBUI;
Expand Down Expand Up @@ -358,8 +357,7 @@ private JPanel createRootPanel() {
gbc.weighty = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridy = 1;
rootPanel.add(
createUserPromptPanel(GeneralSettings.getCurrentState().getSelectedService()), gbc);
rootPanel.add(createUserPromptPanel(GeneralSettings.getSelectedService()), gbc);
return rootPanel;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ private void subscribeToYouSignedOutTopic(
if (!YouUserManager.getInstance().isSubscribed()
&& youSettings.getChatMode() != YouCompletionMode.DEFAULT) {
youSettings.setChatMode(YouCompletionMode.DEFAULT);
updateTemplatePresentation(GeneralSettings.getCurrentState().getSelectedService());
updateTemplatePresentation(GeneralSettings.getSelectedService());
}
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public void mouseEntered(MouseEvent e) {
}

private String getIconToolTipText(String html) {
if (GeneralSettings.getCurrentState().getSelectedService() != ServiceType.OPENAI) {
if (GeneralSettings.getSelectedService() != ServiceType.OPENAI) {
return """
<html
<p style="margin: 4px 0;">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public void actionPerformed(@NotNull AnActionEvent e) {
handleSubmit();
}
}));
var selectedService = GeneralSettings.getCurrentState().getSelectedService();
var selectedService = GeneralSettings.getSelectedService();
if (selectedService == ANTHROPIC
|| (selectedService == OPENAI
&& GPT_4_VISION_PREVIEW.getCode().equals(OpenAISettings.getCurrentState().getModel()))) {
Expand Down
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
}
}
}