Make it clear that you.com is free in case people run out of OpenAI credits (#238)

* Make it clear that you.com is free in case people run out of OpenAI credits

* Clear quota exceeded flag on new api key, minor refactoring

---------

Co-authored-by: Carl-Robert Linnupuu <carlrobertoh@gmail.com>
This commit is contained in:
keith siilats 2023-10-17 03:50:20 -04:00 committed by GitHub
parent 41b3956bf4
commit 4519b2ade6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 36 additions and 9 deletions

View file

@ -10,7 +10,7 @@ import org.jetbrains.annotations.NotNull;
public class AskAction extends AnAction {
public AskAction() {
super("Ask CodeGPT", "Chat with CodeGPT", AllIcons.Actions.Find);
super("New CodeGPT Chat", "Chat with CodeGPT", AllIcons.Actions.Find);
EditorActionsUtil.registerOrReplaceAction(this);
}

View file

@ -2,15 +2,15 @@ package ee.carlrobert.codegpt.settings;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.colors.EditorColorsManager;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.ui.JBColor;
import com.intellij.ui.TitledSeparator;
import com.intellij.ui.components.JBCheckBox;
import com.intellij.ui.components.JBPasswordField;
import com.intellij.ui.components.JBRadioButton;
import com.intellij.ui.components.JBTextField;
import com.intellij.ui.components.*;
import com.intellij.util.ui.FormBuilder;
import com.intellij.util.ui.JBUI;
import com.intellij.util.ui.UI;
import com.intellij.util.ui.UIUtil;
import ee.carlrobert.codegpt.CodeGPTBundle;
import ee.carlrobert.codegpt.credentials.AzureCredentialsManager;
import ee.carlrobert.codegpt.credentials.OpenAICredentialsManager;
@ -20,10 +20,12 @@ import ee.carlrobert.codegpt.settings.state.SettingsState;
import ee.carlrobert.codegpt.completions.you.YouUserManager;
import ee.carlrobert.codegpt.completions.you.auth.AuthenticationNotifier;
import ee.carlrobert.codegpt.settings.state.YouSettingsState;
import ee.carlrobert.codegpt.telemetry.ui.utils.JBLabelUtils;
import ee.carlrobert.codegpt.util.SwingUtils;
import ee.carlrobert.llm.client.openai.completion.chat.OpenAIChatCompletionModel;
import ee.carlrobert.llm.completion.CompletionModel;
import java.awt.FlowLayout;
import java.awt.*;
import java.util.List;
import java.util.Map;
import javax.swing.Box;
@ -155,6 +157,10 @@ public class ServiceSelectionForm {
public JPanel getForm() {
var panel = new JPanel(new FlowLayout(FlowLayout.LEADING, 0, 0));
panel.add(useOpenAIServiceRadioButton);
if (OpenAISettingsState.getInstance().isOpenAIQuotaExceeded()) {
panel.add(Box.createHorizontalStrut(4));
panel.add(new JBLabel("<html><sup style=\"color: #cc3300;\">quota exceeded</sup></html>"));
}
// flow layout's horizontal gap adds annoying horizontal padding on each sides
panel.add(Box.createHorizontalStrut(16));
panel.add(useAzureServiceRadioButton);

View file

@ -69,6 +69,11 @@ public class SettingsConfigurable implements Configurable {
var modelChanged = openAISettings.getModel().equals(serviceSelectionForm.getOpenAIModel()) ||
azureSettings.getModel().equals(serviceSelectionForm.getAzureModel());
var prevKey = OpenAICredentialsManager.getInstance().getApiKey();
if (prevKey != null && !prevKey.equals(serviceSelectionForm.getOpenAIApiKey())) {
OpenAISettingsState.getInstance().setOpenAIQuotaExceeded(false);
}
OpenAICredentialsManager.getInstance().setApiKey(serviceSelectionForm.getOpenAIApiKey());
AzureCredentialsManager.getInstance().setApiKey(serviceSelectionForm.getAzureOpenAIApiKey());
AzureCredentialsManager.getInstance()

View file

@ -19,6 +19,7 @@ public class OpenAISettingsState implements PersistentStateComponent<OpenAISetti
private String baseHost = "https://api.openai.com";
private String path = BASE_PATH;
private String model = OpenAIChatCompletionModel.GPT_3_5.getCode();
private boolean openAIQuotaExceeded;
public static OpenAISettingsState getInstance() {
return ApplicationManager.getApplication().getService(OpenAISettingsState.class);
@ -92,4 +93,12 @@ public class OpenAISettingsState implements PersistentStateComponent<OpenAISetti
public void setPath(String path) {
this.path = path;
}
public boolean isOpenAIQuotaExceeded() {
return openAIQuotaExceeded;
}
public void setOpenAIQuotaExceeded(boolean openAIQuotaExceeded) {
this.openAIQuotaExceeded = openAIQuotaExceeded;
}
}

View file

@ -138,6 +138,9 @@ public abstract class BaseChatToolWindowTabPanel implements ChatToolWindowTabPan
if (SettingsState.getInstance().isUseAzureService()) {
return AzureCredentialsManager.getInstance().isCredentialSet();
}
if (SettingsState.getInstance().isUseYouService()) {
return true;
}
return OpenAICredentialsManager.getInstance().isApiKeySet();
}
@ -203,6 +206,9 @@ public abstract class BaseChatToolWindowTabPanel implements ChatToolWindowTabPan
requestHandler.addErrorListener((error, ex) -> {
try {
if ("insufficient_quota".equals(error.getCode())) {
if (SettingsState.getInstance().isUseOpenAIService()) {
OpenAISettingsState.getInstance().setOpenAIQuotaExceeded(true);
}
responseContainer.displayQuotaExceeded();
} else {
responseContainer.displayError(error.getMessage());

View file

@ -119,6 +119,7 @@ public class ChatMessageResponseBody extends JPanel {
+ "You exceeded your current quota, please check your plan and billing details, "
+ "or <a href=\"#CHANGE_PROVIDER\">change</a> to a different LLM provider.</p>"
+ "</html>");
currentlyProcessedTextPane.addHyperlinkListener(e -> {
if (e.getEventType() == ACTIVATED) {
ShowSettingsUtil.getInstance().showSettingsDialog(project, SettingsConfigurable.class);