Skip to content

Commit 6019a27

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

File tree

13 files changed

+54
-42
lines changed

13 files changed

+54
-42
lines changed

Diff for: src/main/java/ee/carlrobert/codegpt/actions/GenerateGitCommitMessageAction.java

+7-6
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.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"));

Diff for: src/main/java/ee/carlrobert/codegpt/completions/CompletionRequestProvider.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,7 @@ private List<OpenAIChatCompletionMessage> buildMessages(
335335
CallParameters callParameters) {
336336
var messages = buildMessages(callParameters);
337337

338-
if (model == null
339-
|| GeneralSettings.getCurrentState().getSelectedService() == ServiceType.YOU) {
338+
if (model == null || GeneralSettings.getSelectedService() == ServiceType.YOU) {
340339
return messages;
341340
}
342341

Diff for: src/main/java/ee/carlrobert/codegpt/completions/CompletionRequestService.java

+11-22
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;
@@ -68,7 +64,7 @@ public EventSource getChatCompletionAsync(
6864
CallParameters callParameters,
6965
CompletionEventListener<String> eventListener) {
7066
var requestProvider = new CompletionRequestProvider(callParameters.getConversation());
71-
return switch (GeneralSettings.getCurrentState().getSelectedService()) {
67+
return switch (GeneralSettings.getSelectedService()) {
7268
case OPENAI -> CompletionClientProvider.getOpenAIClient().getChatCompletionAsync(
7369
requestProvider.buildOpenAIChatCompletionRequest(
7470
OpenAISettings.getCurrentState().getModel(),
@@ -100,7 +96,7 @@ public EventSource getChatCompletionAsync(
10096
public EventSource getCodeCompletionAsync(
10197
InfillRequestDetails requestDetails,
10298
CompletionEventListener<String> eventListener) {
103-
return switch (GeneralSettings.getCurrentState().getSelectedService()) {
99+
return switch (GeneralSettings.getSelectedService()) {
104100
case OPENAI -> CompletionClientProvider.getOpenAIClient()
105101
.getCompletionAsync(
106102
CodeCompletionRequestFactory.INSTANCE.buildOpenAIRequest(requestDetails),
@@ -124,7 +120,7 @@ public void generateCommitMessageAsync(
124120
new OpenAIChatCompletionStandardMessage("user", prompt)))
125121
.setModel(OpenAISettings.getCurrentState().getModel())
126122
.build();
127-
var selectedService = GeneralSettings.getCurrentState().getSelectedService();
123+
var selectedService = GeneralSettings.getSelectedService();
128124
switch (selectedService) {
129125
case OPENAI:
130126
CompletionClientProvider.getOpenAIClient()
@@ -182,7 +178,7 @@ public void generateCommitMessageAsync(
182178
}
183179

184180
public Optional<String> getLookupCompletion(String prompt) {
185-
var selectedService = GeneralSettings.getCurrentState().getSelectedService();
181+
var selectedService = GeneralSettings.getSelectedService();
186182
if (selectedService == YOU || selectedService == LLAMA_CPP) {
187183
return Optional.empty();
188184
}
@@ -206,23 +202,16 @@ public Optional<String> getLookupCompletion(String prompt) {
206202
}
207203

208204
public boolean isRequestAllowed() {
209-
return isRequestAllowed(GeneralSettings.getCurrentState().getSelectedService());
205+
return isRequestAllowed(GeneralSettings.getSelectedService());
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
/**

Diff for: src/main/java/ee/carlrobert/codegpt/conversations/ConversationService.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ public Conversation createConversation(String clientCode) {
4646
conversation.setClientCode(clientCode);
4747
conversation.setCreatedOn(LocalDateTime.now());
4848
conversation.setUpdatedOn(LocalDateTime.now());
49-
conversation.setModel(getModelForSelectedService(
50-
GeneralSettings.getCurrentState().getSelectedService()));
49+
conversation.setModel(getModelForSelectedService(GeneralSettings.getSelectedService()));
5150
return conversation;
5251
}
5352

@@ -110,7 +109,7 @@ public void saveConversation(Conversation conversation) {
110109
}
111110

112111
public Conversation startConversation() {
113-
var completionCode = GeneralSettings.getCurrentState().getSelectedService().getCompletionCode();
112+
var completionCode = GeneralSettings.getSelectedService().getCompletionCode();
114113
var conversation = createConversation(completionCode);
115114
conversationState.setCurrentConversation(conversation);
116115
addConversation(conversation);

Diff for: src/main/java/ee/carlrobert/codegpt/settings/GeneralSettings.java

+7
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ public static GeneralSettingsState getCurrentState() {
3434
return getInstance().getState();
3535
}
3636

37+
/**
38+
* Selected service of current state.
39+
*/
40+
public static ServiceType getSelectedService() {
41+
return getCurrentState().getSelectedService();
42+
}
43+
3744
public static GeneralSettings getInstance() {
3845
return ApplicationManager.getApplication().getService(GeneralSettings.class);
3946
}

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

+4
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(

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

+11
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) {

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

+4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ 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(

Diff for: src/main/java/ee/carlrobert/codegpt/toolwindow/chat/ChatToolWindowTabPanel.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import com.intellij.openapi.Disposable;
88
import com.intellij.openapi.actionSystem.ActionPlaces;
99
import com.intellij.openapi.diagnostic.Logger;
10-
import com.intellij.openapi.editor.impl.EditorImpl;
1110
import com.intellij.openapi.project.Project;
1211
import com.intellij.ui.JBColor;
1312
import com.intellij.util.ui.JBUI;
@@ -358,8 +357,7 @@ private JPanel createRootPanel() {
358357
gbc.weighty = 0;
359358
gbc.fill = GridBagConstraints.HORIZONTAL;
360359
gbc.gridy = 1;
361-
rootPanel.add(
362-
createUserPromptPanel(GeneralSettings.getCurrentState().getSelectedService()), gbc);
360+
rootPanel.add(createUserPromptPanel(GeneralSettings.getSelectedService()), gbc);
363361
return rootPanel;
364362
}
365363
}

Diff for: src/main/java/ee/carlrobert/codegpt/toolwindow/chat/ui/textarea/ModelComboBoxAction.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ private void subscribeToYouSignedOutTopic(
134134
if (!YouUserManager.getInstance().isSubscribed()
135135
&& youSettings.getChatMode() != YouCompletionMode.DEFAULT) {
136136
youSettings.setChatMode(YouCompletionMode.DEFAULT);
137-
updateTemplatePresentation(GeneralSettings.getCurrentState().getSelectedService());
137+
updateTemplatePresentation(GeneralSettings.getSelectedService());
138138
}
139139
}
140140
);

Diff for: src/main/java/ee/carlrobert/codegpt/toolwindow/chat/ui/textarea/TotalTokensPanel.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public void mouseEntered(MouseEvent e) {
163163
}
164164

165165
private String getIconToolTipText(String html) {
166-
if (GeneralSettings.getCurrentState().getSelectedService() != ServiceType.OPENAI) {
166+
if (GeneralSettings.getSelectedService() != ServiceType.OPENAI) {
167167
return """
168168
<html
169169
<p style="margin: 4px 0;">

Diff for: src/main/java/ee/carlrobert/codegpt/toolwindow/chat/ui/textarea/UserPromptTextArea.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ public void actionPerformed(@NotNull AnActionEvent e) {
190190
handleSubmit();
191191
}
192192
}));
193-
var selectedService = GeneralSettings.getCurrentState().getSelectedService();
193+
var selectedService = GeneralSettings.getSelectedService();
194194
if (selectedService == ANTHROPIC
195195
|| (selectedService == OPENAI
196196
&& GPT_4_VISION_PREVIEW.getCode().equals(OpenAISettings.getCurrentState().getModel()))) {

Diff for: src/main/kotlin/ee/carlrobert/codegpt/credentials/CredentialsStore.kt

+3-3
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)