Encapsulate settings (#180)

This commit is contained in:
Carl-Robert 2023-08-27 18:16:08 +03:00 committed by GitHub
parent de971806d0
commit ef5fd5919f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
36 changed files with 697 additions and 369 deletions

View file

@ -5,7 +5,7 @@ import com.intellij.openapi.components.Service;
import com.knuddels.jtokkit.Encodings;
import com.knuddels.jtokkit.api.Encoding;
import com.knuddels.jtokkit.api.EncodingRegistry;
import ee.carlrobert.codegpt.settings.SettingsState;
import ee.carlrobert.codegpt.settings.state.ModelSettingsState;
import ee.carlrobert.openai.client.completion.chat.request.ChatCompletionMessage;
@Service
@ -15,8 +15,7 @@ public final class EncodingManager {
private Encoding encoding;
private EncodingManager() {
var settings = SettingsState.getInstance();
setEncoding(settings.isChatCompletionOptionSelected ? settings.chatCompletionBaseModel : settings.textCompletionBaseModel);
setEncoding(ModelSettingsState.getInstance().getCompletionModel());
}
public static EncodingManager getInstance() {

View file

@ -8,7 +8,7 @@ import com.intellij.openapi.project.ProjectManagerListener;
import com.intellij.openapi.startup.StartupActivity;
import ee.carlrobert.codegpt.actions.editor.EditorActionsUtil;
import ee.carlrobert.codegpt.credentials.UserCredentialsManager;
import ee.carlrobert.codegpt.settings.SettingsState;
import ee.carlrobert.codegpt.settings.state.SettingsState;
import ee.carlrobert.codegpt.user.UserManager;
import ee.carlrobert.codegpt.user.auth.AuthenticationHandler;
import ee.carlrobert.codegpt.user.auth.AuthenticationService;
@ -34,7 +34,7 @@ public class PluginStartupActivity implements StartupActivity {
var userManager = UserManager.getInstance();
var session = userManager.getSession();
if (session == null || session.isExpired()) {
handleAuthentication(project);
handleAuthentication();
} else {
startSessionVerificationJob();
}
@ -76,16 +76,16 @@ public class PluginStartupActivity implements StartupActivity {
}
}
private void handleAuthentication(Project project) {
private void handleAuthentication() {
var settings = SettingsState.getInstance();
if (settings == null || !settings.previouslySignedIn) {
if (settings == null || !settings.isPreviouslySignedIn()) {
return;
}
var password = UserCredentialsManager.getInstance().getAccountPassword();
if (!settings.email.isEmpty() && password != null && !password.isEmpty()) {
if (!settings.getEmail().isEmpty() && password != null && !password.isEmpty()) {
AuthenticationService.getInstance()
.signInAsync(settings.email, password, new AuthenticationHandler() {
.signInAsync(settings.getEmail(), password, new AuthenticationHandler() {
@Override
public void handleAuthenticated() {
OverlayUtils.showNotification("Authentication successful.", NotificationType.INFORMATION);

View file

@ -46,7 +46,7 @@ public class EditorActionsUtil {
group.add(new CustomPromptAction());
group.addSeparator();
var configuredActions = ConfigurationState.getInstance().tableData;
var configuredActions = ConfigurationState.getInstance().getTableData();
configuredActions.forEach((label, prompt) -> {
// using label as action description to prevent com.intellij.diagnostic.PluginException
// https://github.com/carlrobertoh/CodeGPT/issues/95
@ -61,7 +61,7 @@ public class EditorActionsUtil {
if (toolWindow != null) {
toolWindow.show();
if (ConfigurationState.getInstance().createNewChatOnEachAction) {
if (ConfigurationState.getInstance().isCreateNewChatOnEachAction()) {
toolWindowContentManager.createNewTabPanel();
}
}

View file

@ -2,7 +2,9 @@ package ee.carlrobert.codegpt.completions;
import ee.carlrobert.codegpt.credentials.AzureCredentialsManager;
import ee.carlrobert.codegpt.credentials.OpenAICredentialsManager;
import ee.carlrobert.codegpt.settings.SettingsState;
import ee.carlrobert.codegpt.settings.state.AzureSettingsState;
import ee.carlrobert.codegpt.settings.state.OpenAISettingsState;
import ee.carlrobert.codegpt.settings.state.SettingsState;
import ee.carlrobert.codegpt.settings.advanced.AdvancedSettingsState;
import ee.carlrobert.openai.client.AzureClient;
import ee.carlrobert.openai.client.Client;
@ -18,14 +20,11 @@ import java.util.concurrent.TimeUnit;
public class CompletionClientProvider {
public static EmbeddingsClient getEmbeddingsClient() {
var settings = SettingsState.getInstance();
if (settings.useOpenAIService) {
var baseHost = SettingsState.getInstance().openAIBaseHost;
return ((OpenAIClient.Builder) getOpenAIClientBuilder().setHost(baseHost)).buildEmbeddingsClient();
if (SettingsState.getInstance().isUseOpenAIService()) {
return getOpenAIClientBuilder().buildEmbeddingsClient();
}
return null; // TODO
// TODO
return null;
}
public static CompletionClient getChatCompletionClient(SettingsState settings) {
@ -38,49 +37,41 @@ public class CompletionClientProvider {
}
public static Client.Builder getClientBuilder(SettingsState settings) {
return settings.useAzureService ?
getAzureClientBuilder().setHost(settings.azureBaseHost) :
getOpenAIClientBuilder().setHost(settings.openAIBaseHost);
return settings.isUseAzureService() ? getAzureClientBuilder() : getOpenAIClientBuilder();
}
private static OpenAIClient.Builder getOpenAIClientBuilder() {
var settings = SettingsState.getInstance();
var builder = new OpenAIClient.Builder(OpenAICredentialsManager.getInstance().getApiKey());
if (settings.useOpenAIService) {
builder.setOrganization(settings.openAIOrganization);
}
return (OpenAIClient.Builder) addDefaultClientParams(builder);
var settings = OpenAISettingsState.getInstance();
var builder = new OpenAIClient
.Builder(OpenAICredentialsManager.getInstance().getApiKey())
.setOrganization(settings.getOrganization());
return (OpenAIClient.Builder) addDefaultClientParams(builder).setHost(settings.getBaseHost());
}
private static AzureClient.Builder getAzureClientBuilder() {
var settings = SettingsState.getInstance();
var params = new AzureClientRequestParams(settings.azureResourceName, settings.azureDeploymentId, settings.azureApiVersion);
var azureCredentials = AzureCredentialsManager.getInstance();
var secret = settings.useAzureActiveDirectoryAuthentication ?
azureCredentials.getAzureActiveDirectoryToken() :
azureCredentials.getAzureOpenAIApiKey();
var builder = new AzureClient.Builder(secret, params).setActiveDirectoryAuthentication(settings.useAzureActiveDirectoryAuthentication);
return (AzureClient.Builder) addDefaultClientParams(builder);
var settings = AzureSettingsState.getInstance();
var params = new AzureClientRequestParams(settings.getResourceName(), settings.getDeploymentId(), settings.getApiVersion());
var builder = new AzureClient.Builder(AzureCredentialsManager.getInstance().getSecret(), params)
.setActiveDirectoryAuthentication(settings.isUseAzureActiveDirectoryAuthentication());
return (AzureClient.Builder) addDefaultClientParams(builder).setHost(settings.getBaseHost());
}
private static Client.Builder addDefaultClientParams(Client.Builder builder) {
var advancedSettings = AdvancedSettingsState.getInstance();
var proxyHost = advancedSettings.proxyHost;
var proxyPort = advancedSettings.proxyPort;
var proxyHost = advancedSettings.getProxyHost();
var proxyPort = advancedSettings.getProxyPort();
if (!proxyHost.isEmpty() && proxyPort != 0) {
builder.setProxy(
new Proxy(advancedSettings.proxyType, new InetSocketAddress(proxyHost, proxyPort)));
if (advancedSettings.isProxyAuthSelected) {
new Proxy(advancedSettings.getProxyType(), new InetSocketAddress(proxyHost, proxyPort)));
if (advancedSettings.isProxyAuthSelected()) {
builder.setProxyAuthenticator(
new ProxyAuthenticator(advancedSettings.proxyUsername, advancedSettings.proxyPassword));
new ProxyAuthenticator(advancedSettings.getProxyUsername(), advancedSettings.getProxyPassword()));
}
}
return builder
.setConnectTimeout((long) advancedSettings.connectTimeout, TimeUnit.SECONDS)
.setReadTimeout((long) advancedSettings.readTimeout, TimeUnit.SECONDS)
.setConnectTimeout((long) advancedSettings.getConnectTimeout(), TimeUnit.SECONDS)
.setReadTimeout((long) advancedSettings.getReadTimeout(), TimeUnit.SECONDS)
.setRetryOnReadTimeout(true);
}
}

View file

@ -1,9 +1,9 @@
package ee.carlrobert.codegpt.completions;
import com.intellij.openapi.project.Project;
import ee.carlrobert.codegpt.conversations.Conversation;
import ee.carlrobert.codegpt.conversations.message.Message;
import ee.carlrobert.codegpt.settings.SettingsState;
import ee.carlrobert.codegpt.settings.state.ModelSettingsState;
import ee.carlrobert.codegpt.settings.state.SettingsState;
import ee.carlrobert.openai.client.completion.CompletionEventListener;
import ee.carlrobert.openai.client.completion.ErrorDetails;
import java.util.List;
@ -16,7 +16,6 @@ import org.jetbrains.annotations.Nullable;
public class CompletionRequestHandler {
private final Project project;
private final StringBuilder messageBuilder = new StringBuilder();
private SwingWorker<Void, String> swingWorker;
private EventSource eventSource;
@ -26,10 +25,6 @@ public class CompletionRequestHandler {
private @Nullable Runnable tokensExceededListener;
private boolean useContextualSearch;
public CompletionRequestHandler(@NotNull Project project) {
this.project = project;
}
public CompletionRequestHandler withContextualSearch(boolean useContextualSearch) {
this.useContextualSearch = useContextualSearch;
return this;
@ -109,13 +104,14 @@ public class CompletionRequestHandler {
boolean isRetry,
CompletionEventListener eventListener) {
var settings = SettingsState.getInstance();
var modelSettings = ModelSettingsState.getInstance();
var requestProvider = new CompletionRequestProvider(conversation);
if (settings.isChatCompletionOptionSelected) {
if (modelSettings.isUseChatCompletion()) {
return CompletionClientProvider.getChatCompletionClient(settings).stream(
requestProvider.buildChatCompletionRequest(settings.getChatCompletionModel(), message, isRetry, useContextualSearch), eventListener);
requestProvider.buildChatCompletionRequest(modelSettings.getChatCompletionModel(), message, isRetry, useContextualSearch), eventListener);
}
return CompletionClientProvider.getTextCompletionClient(settings).stream(
requestProvider.buildTextCompletionRequest(settings.getTextCompletionModel(), message, isRetry), eventListener);
requestProvider.buildTextCompletionRequest(modelSettings.getTextCompletionModel(), message, isRetry), eventListener);
}
}

View file

@ -9,7 +9,7 @@ import ee.carlrobert.codegpt.conversations.Conversation;
import ee.carlrobert.codegpt.conversations.ConversationsState;
import ee.carlrobert.codegpt.conversations.message.Message;
import ee.carlrobert.codegpt.embeddings.EmbeddingsService;
import ee.carlrobert.codegpt.settings.SettingsState;
import ee.carlrobert.codegpt.settings.state.SettingsState;
import ee.carlrobert.codegpt.settings.configuration.ConfigurationState;
import ee.carlrobert.codegpt.util.FileUtils;
import ee.carlrobert.openai.client.completion.chat.ChatCompletionModel;
@ -62,8 +62,8 @@ public class CompletionRequestProvider {
public ChatCompletionRequest buildChatCompletionRequest(String model, Message message, boolean isRetry, boolean useContextualSearch) {
return (ChatCompletionRequest) new ChatCompletionRequest.Builder(buildMessages(model, message, isRetry, useContextualSearch))
.setModel(model)
.setMaxTokens(ConfigurationState.getInstance().maxTokens)
.setTemperature(ConfigurationState.getInstance().temperature)
.setMaxTokens(ConfigurationState.getInstance().getMaxTokens())
.setTemperature(ConfigurationState.getInstance().getTemperature())
.build();
}
@ -71,8 +71,8 @@ public class CompletionRequestProvider {
return (TextCompletionRequest) new TextCompletionRequest.Builder(buildPrompt(model, message, isRetry))
.setStop(List.of(" Human:", " AI:"))
.setModel(model)
.setMaxTokens(ConfigurationState.getInstance().maxTokens)
.setTemperature(ConfigurationState.getInstance().temperature)
.setMaxTokens(ConfigurationState.getInstance().getMaxTokens())
.setTemperature(ConfigurationState.getInstance().getTemperature())
.build();
}
@ -87,7 +87,7 @@ public class CompletionRequestProvider {
LOG.info("Retrieved context:\n" + prompt);
messages.add(new ChatCompletionMessage("user", prompt));
} else {
var systemPrompt = ConfigurationState.getInstance().systemPrompt;
var systemPrompt = ConfigurationState.getInstance().getSystemPrompt();
messages.add(new ChatCompletionMessage("system",
systemPrompt.isEmpty() ? COMPLETION_SYSTEM_PROMPT : systemPrompt));
@ -103,7 +103,7 @@ public class CompletionRequestProvider {
int totalUsage = messages.parallelStream()
.mapToInt(encodingManager::countMessageTokens)
.sum() + ConfigurationState.getInstance().maxTokens;
.sum() + ConfigurationState.getInstance().getMaxTokens();
int modelMaxTokens = ChatCompletionModel.findByCode(model).getMaxTokens();
if (totalUsage <= modelMaxTokens) {
@ -145,7 +145,7 @@ public class CompletionRequestProvider {
}
private String buildPrompt(String model, Message message, boolean isRetry) {
var systemPrompt = ConfigurationState.getInstance().systemPrompt;
var systemPrompt = ConfigurationState.getInstance().getSystemPrompt();
var basePrompt = systemPrompt.isEmpty() ? getBasePrompt(model) : new StringBuilder(systemPrompt + "\n");
conversation.getMessages().forEach(prevMessage ->
basePrompt.append("Human: ")

View file

@ -3,7 +3,7 @@ package ee.carlrobert.codegpt.conversations;
import static java.util.stream.Collectors.toList;
import ee.carlrobert.codegpt.conversations.message.Message;
import ee.carlrobert.codegpt.settings.SettingsState;
import ee.carlrobert.codegpt.settings.state.ModelSettingsState;
import ee.carlrobert.openai.client.ClientCode;
import java.time.LocalDateTime;
import java.util.ArrayList;
@ -38,13 +38,10 @@ public class ConversationService {
}
public Conversation createConversation(ClientCode clientCode) {
var settings = SettingsState.getInstance();
var conversation = new Conversation();
conversation.setId(UUID.randomUUID());
conversation.setClientCode(clientCode);
conversation.setModel(settings.isChatCompletionOptionSelected ?
settings.getChatCompletionModel() :
settings.getTextCompletionModel());
conversation.setModel(ModelSettingsState.getInstance().getCompletionModel());
conversation.setCreatedOn(LocalDateTime.now());
conversation.setUpdatedOn(LocalDateTime.now());
return conversation;
@ -111,8 +108,7 @@ public class ConversationService {
}
public Conversation startConversation() {
var currentClientCode = SettingsState.getInstance().isChatCompletionOptionSelected ?
ClientCode.CHAT_COMPLETION : ClientCode.TEXT_COMPLETION;
var currentClientCode = ModelSettingsState.getInstance().isUseChatCompletion() ? ClientCode.CHAT_COMPLETION : ClientCode.TEXT_COMPLETION;
var conversation = createConversation(currentClientCode);
conversationState.setCurrentConversation(conversation);
addConversation(conversation);

View file

@ -1,7 +1,7 @@
package ee.carlrobert.codegpt.credentials;
import com.intellij.credentialStore.CredentialAttributes;
import ee.carlrobert.codegpt.settings.SettingsState;
import ee.carlrobert.codegpt.settings.state.AzureSettingsState;
import org.jetbrains.annotations.Nullable;
public class AzureCredentialsManager {
@ -27,6 +27,10 @@ public class AzureCredentialsManager {
return instance;
}
public String getSecret() {
return AzureSettingsState.getInstance().isUseAzureActiveDirectoryAuthentication() ? azureActiveDirectoryToken : azureOpenAIApiKey;
}
public @Nullable String getAzureOpenAIApiKey() {
return azureOpenAIApiKey;
}
@ -46,8 +50,7 @@ public class AzureCredentialsManager {
}
public boolean isCredentialSet() {
var settings = SettingsState.getInstance();
if (settings.useAzureApiKeyAuthentication) {
if (AzureSettingsState.getInstance().isUseAzureApiKeyAuthentication()) {
return isKeySet();
}
return isTokenSet();

View file

@ -16,7 +16,7 @@ import ee.carlrobert.codegpt.completions.CompletionClientProvider;
import ee.carlrobert.codegpt.embeddings.EmbeddingsService;
import ee.carlrobert.codegpt.embeddings.VectorStore;
import ee.carlrobert.codegpt.embeddings.CheckedFile;
import ee.carlrobert.codegpt.settings.SettingsState;
import ee.carlrobert.codegpt.settings.state.SettingsState;
import ee.carlrobert.codegpt.util.FileUtils;
import ee.carlrobert.codegpt.util.OverlayUtils;
import java.util.List;

View file

@ -6,6 +6,7 @@ import com.intellij.ui.components.JBRadioButton;
import com.intellij.util.ui.FormBuilder;
import com.intellij.util.ui.JBUI;
import ee.carlrobert.codegpt.CodeGPTBundle;
import ee.carlrobert.codegpt.settings.state.ModelSettingsState;
import ee.carlrobert.codegpt.util.SwingUtils;
import ee.carlrobert.openai.client.completion.CompletionModel;
import ee.carlrobert.openai.client.completion.chat.ChatCompletionModel;
@ -27,25 +28,26 @@ public class ModelSelectionForm {
private final JPanel chatCompletionModelsPanel;
private final JPanel textCompletionModelsPanel;
private CompletionModel findChatCompletionModelOrGetDefault(SettingsState settings) {
private CompletionModel findChatCompletionModelOrGetDefault(ModelSettingsState settings) {
try {
return ChatCompletionModel.findByCode(settings.chatCompletionBaseModel);
return ChatCompletionModel.findByCode(settings.getChatCompletionModel());
} catch (NoSuchElementException e) {
LOG.warn("Couldn't find completion model with code: " + settings.chatCompletionBaseModel);
LOG.warn("Couldn't find completion model with code: " + settings.getChatCompletionModel());
return ChatCompletionModel.GPT_3_5;
}
}
private CompletionModel findTextCompletionModelOrGetDefault(SettingsState settings) {
private CompletionModel findTextCompletionModelOrGetDefault(ModelSettingsState settings) {
try {
return TextCompletionModel.findByCode(settings.textCompletionBaseModel);
return TextCompletionModel.findByCode(settings.getTextCompletionModel());
} catch (NoSuchElementException e) {
LOG.warn("Couldn't find completion model with code: " + settings.textCompletionBaseModel);
LOG.warn("Couldn't find completion model with code: " + settings.getTextCompletionModel());
return TextCompletionModel.DAVINCI;
}
}
public ModelSelectionForm(SettingsState settings) {
public ModelSelectionForm() {
var settings = ModelSettingsState.getInstance();
chatCompletionBaseModelComboBox = new BaseModelComboBox(
new ChatCompletionModel[] {
ChatCompletionModel.GPT_3_5,
@ -69,10 +71,10 @@ public class ModelSelectionForm {
textCompletionModelsPanel.setBorder(JBUI.Borders.emptyLeft(16));
useChatCompletionRadioButton = new JBRadioButton(
CodeGPTBundle.get("settingsConfigurable.section.model.useChatCompletionRadioButtonLabel"),
settings.isChatCompletionOptionSelected);
settings.isUseChatCompletion());
useTextCompletionRadioButton = new JBRadioButton(
CodeGPTBundle.get("settingsConfigurable.section.model.useTextCompletionRadioButtonLabel"),
settings.isTextCompletionOptionSelected);
settings.isUseTextCompletion());
registerFields();
registerRadioButtons();

View file

@ -10,6 +10,9 @@ import com.intellij.util.ui.UI;
import ee.carlrobert.codegpt.CodeGPTBundle;
import ee.carlrobert.codegpt.credentials.AzureCredentialsManager;
import ee.carlrobert.codegpt.credentials.OpenAICredentialsManager;
import ee.carlrobert.codegpt.settings.state.AzureSettingsState;
import ee.carlrobert.codegpt.settings.state.OpenAISettingsState;
import ee.carlrobert.codegpt.settings.state.SettingsState;
import ee.carlrobert.codegpt.util.SwingUtils;
import java.util.Map;
import javax.swing.ButtonGroup;
@ -38,6 +41,8 @@ public class ServiceSelectionForm {
private final JPanel azureServiceSectionPanel;
public ServiceSelectionForm(SettingsState settings) {
var openAISettings = OpenAISettingsState.getInstance();
var azureSettings = AzureSettingsState.getInstance();
openAIApiKey = new JBPasswordField();
openAIApiKey.setColumns(30);
openAIApiKey.setText(OpenAICredentialsManager.getInstance().getApiKey());
@ -62,28 +67,28 @@ public class ServiceSelectionForm {
useAzureApiKeyAuthenticationRadioButton = new JBRadioButton(
"Use API Key authentication",
settings.useAzureApiKeyAuthentication);
azureSettings.isUseAzureApiKeyAuthentication());
useAzureActiveDirectoryAuthenticationRadioButton = new JBRadioButton(
"Use Active Directory authentication",
settings.useAzureActiveDirectoryAuthentication);
azureSettings.isUseAzureActiveDirectoryAuthentication());
useOpenAIServiceRadioButton = new JBRadioButton(
CodeGPTBundle.get("settingsConfigurable.section.service.useOpenAIServiceRadioButtonLabel"), settings.useOpenAIService);
CodeGPTBundle.get("settingsConfigurable.section.service.useOpenAIServiceRadioButtonLabel"), settings.isUseOpenAIService());
useAzureServiceRadioButton = new JBRadioButton(
CodeGPTBundle.get("settingsConfigurable.section.service.useAzureServiceRadioButtonLabel"), settings.useAzureService);
CodeGPTBundle.get("settingsConfigurable.section.service.useAzureServiceRadioButtonLabel"), settings.isUseAzureService());
openAIBaseHostField = new JBTextField(settings.openAIBaseHost, 30);
openAIOrganizationField = new JBTextField(settings.openAIOrganization, 30);
openAIBaseHostField = new JBTextField(openAISettings.getBaseHost(), 30);
openAIOrganizationField = new JBTextField(openAISettings.getOrganization(), 30);
azureBaseHostField = new JBTextField(settings.azureBaseHost, 30);
azureResourceNameField = new JBTextField(settings.azureResourceName, 30);
azureDeploymentIdField = new JBTextField(settings.azureDeploymentId, 30);
azureApiVersionField = new JBTextField(settings.azureApiVersion, 30);
azureBaseHostField = new JBTextField(azureSettings.getBaseHost(), 30);
azureResourceNameField = new JBTextField(azureSettings.getResourceName(), 30);
azureDeploymentIdField = new JBTextField(azureSettings.getDeploymentId(), 30);
azureApiVersionField = new JBTextField(azureSettings.getApiVersion(), 30);
openAIServiceSectionPanel = createOpenAIServiceSectionPanel();
azureServiceSectionPanel = createAzureServiceSectionPanel();
registerPanelsVisibility(settings);
registerPanelsVisibility(settings, azureSettings);
registerRadioButtons();
}
@ -262,11 +267,11 @@ public class ServiceSelectionForm {
return form;
}
private void registerPanelsVisibility(SettingsState settings) {
openAIServiceSectionPanel.setVisible(settings.useOpenAIService);
azureServiceSectionPanel.setVisible(settings.useAzureService);
azureApiKeyFieldPanel.setVisible(settings.useAzureApiKeyAuthentication);
azureActiveDirectoryTokenFieldPanel.setVisible(settings.useAzureActiveDirectoryAuthentication);
private void registerPanelsVisibility(SettingsState settings, AzureSettingsState azureSettings) {
openAIServiceSectionPanel.setVisible(settings.isUseOpenAIService());
azureServiceSectionPanel.setVisible(settings.isUseAzureService());
azureApiKeyFieldPanel.setVisible(azureSettings.isUseAzureApiKeyAuthentication());
azureActiveDirectoryTokenFieldPanel.setVisible(azureSettings.isUseAzureActiveDirectoryAuthentication());
}
private void registerRadioButtons() {

View file

@ -6,6 +6,7 @@ import com.intellij.ui.components.JBTextField;
import com.intellij.util.ui.FormBuilder;
import com.intellij.util.ui.UI;
import ee.carlrobert.codegpt.CodeGPTBundle;
import ee.carlrobert.codegpt.settings.state.SettingsState;
import javax.swing.JComponent;
import javax.swing.JPanel;
@ -18,10 +19,10 @@ public class SettingsComponent {
private final UserDetailsSettingsPanel userDetailsSettingsPanel;
public SettingsComponent(Disposable parentDisposable, SettingsState settings) {
modelSelectionForm = new ModelSelectionForm(settings);
modelSelectionForm = new ModelSelectionForm();
serviceSelectionForm = new ServiceSelectionForm(settings);
displayNameField = new JBTextField(settings.displayName, 20);
displayNameField = new JBTextField(settings.getDisplayName(), 20);
userDetailsSettingsPanel = new UserDetailsSettingsPanel(parentDisposable, settings);

View file

@ -8,6 +8,10 @@ import ee.carlrobert.codegpt.conversations.ConversationsState;
import ee.carlrobert.codegpt.credentials.AzureCredentialsManager;
import ee.carlrobert.codegpt.credentials.OpenAICredentialsManager;
import ee.carlrobert.codegpt.credentials.UserCredentialsManager;
import ee.carlrobert.codegpt.settings.state.AzureSettingsState;
import ee.carlrobert.codegpt.settings.state.ModelSettingsState;
import ee.carlrobert.codegpt.settings.state.OpenAISettingsState;
import ee.carlrobert.codegpt.settings.state.SettingsState;
import ee.carlrobert.codegpt.toolwindow.chat.standard.StandardChatToolWindowContentManager;
import ee.carlrobert.codegpt.toolwindow.chat.standard.StandardChatToolWindowTabPanel;
import ee.carlrobert.codegpt.util.ApplicationUtils;
@ -41,41 +45,48 @@ public class SettingsConfigurable implements Configurable, Disposable {
@Override
public boolean isModified() {
var settings = SettingsState.getInstance();
var openAISettings = OpenAISettingsState.getInstance();
var azureSettings = AzureSettingsState.getInstance();
var modelSettings = ModelSettingsState.getInstance();
var serviceSelectionForm = settingsComponent.getServiceSelectionForm();
return !settingsComponent.getEmail().equals(settings.email) ||
!settingsComponent.getDisplayName().equals(settings.displayName) ||
return !settingsComponent.getEmail().equals(settings.getEmail()) ||
!settingsComponent.getDisplayName().equals(settings.getDisplayName()) ||
serviceSelectionForm.isOpenAIServiceSelected() != settings.useOpenAIService ||
serviceSelectionForm.isAzureServiceSelected() != settings.useAzureService ||
serviceSelectionForm.isOpenAIServiceSelected() != settings.isUseOpenAIService() ||
serviceSelectionForm.isAzureServiceSelected() != settings.isUseAzureService() ||
!serviceSelectionForm.getOpenAIApiKey().equals(OpenAICredentialsManager.getInstance().getApiKey()) ||
!serviceSelectionForm.getOpenAIOrganization().equals(settings.openAIOrganization) ||
!serviceSelectionForm.getOpenAIBaseHost().equals(settings.openAIBaseHost) ||
!serviceSelectionForm.getOpenAIOrganization().equals(openAISettings.getOrganization()) ||
!serviceSelectionForm.getOpenAIBaseHost().equals(openAISettings.getBaseHost()) ||
serviceSelectionForm.isAzureActiveDirectoryAuthenticationSelected() != settings.useAzureActiveDirectoryAuthentication ||
serviceSelectionForm.isAzureApiKeyAuthenticationSelected() != settings.useAzureApiKeyAuthentication ||
serviceSelectionForm.isAzureActiveDirectoryAuthenticationSelected() != azureSettings.isUseAzureActiveDirectoryAuthentication() ||
serviceSelectionForm.isAzureApiKeyAuthenticationSelected() != azureSettings.isUseAzureApiKeyAuthentication() ||
!serviceSelectionForm.getAzureActiveDirectoryToken().equals(AzureCredentialsManager.getInstance().getAzureActiveDirectoryToken()) ||
!serviceSelectionForm.getAzureOpenAIApiKey().equals(AzureCredentialsManager.getInstance().getAzureOpenAIApiKey()) ||
!serviceSelectionForm.getAzureResourceName().equals(settings.azureResourceName) ||
!serviceSelectionForm.getAzureDeploymentId().equals(settings.azureDeploymentId) ||
!serviceSelectionForm.getAzureApiVersion().equals(settings.azureApiVersion) ||
!serviceSelectionForm.getAzureBaseHost().equals(settings.azureBaseHost) ||
!serviceSelectionForm.getAzureResourceName().equals(azureSettings.getResourceName()) ||
!serviceSelectionForm.getAzureDeploymentId().equals(azureSettings.getDeploymentId()) ||
!serviceSelectionForm.getAzureApiVersion().equals(azureSettings.getApiVersion()) ||
!serviceSelectionForm.getAzureBaseHost().equals(azureSettings.getBaseHost()) ||
isModelChanged(settings) ||
isCompletionOptionChanged(settings);
isModelChanged(modelSettings) ||
isCompletionOptionChanged(modelSettings);
}
@Override
public void apply() {
var settings = SettingsState.getInstance();
var isModelChanged = isModelChanged(settings);
var openAISettings = OpenAISettingsState.getInstance();
var azureSettings = AzureSettingsState.getInstance();
var modelSettings = ModelSettingsState.getInstance();
var isModelChanged = isModelChanged(modelSettings);
if (isModelChanged) {
EncodingManager.getInstance()
.setEncoding(settings.isChatCompletionOptionSelected ? settings.chatCompletionBaseModel
: settings.textCompletionBaseModel);
EncodingManager.getInstance().setEncoding(modelSettings.isUseChatCompletion() ?
modelSettings.getChatCompletionModel() :
modelSettings.getTextCompletionModel());
}
if (isCompletionOptionChanged(settings) || isModelChanged) {
if (isCompletionOptionChanged(modelSettings) || isModelChanged) {
ConversationsState.getInstance().setCurrentConversation(null);
var project = ApplicationUtils.findCurrentProject();
if (project == null) {
@ -100,57 +111,59 @@ public class SettingsConfigurable implements Configurable, Disposable {
AzureCredentialsManager.getInstance().setApiKey(serviceSelectionForm.getAzureOpenAIApiKey());
AzureCredentialsManager.getInstance().setAzureActiveDirectoryToken(serviceSelectionForm.getAzureActiveDirectoryToken());
settings.email = settingsComponent.getEmail();
settings.displayName = settingsComponent.getDisplayName();
settings.setEmail(settingsComponent.getEmail());
settings.setDisplayName(settingsComponent.getDisplayName());
settings.setUseOpenAIService(serviceSelectionForm.isOpenAIServiceSelected());
settings.setUseOpenAIService(serviceSelectionForm.isAzureServiceSelected());
settings.useOpenAIService = serviceSelectionForm.isOpenAIServiceSelected();
settings.useAzureService = serviceSelectionForm.isAzureServiceSelected();
openAISettings.setOrganization(serviceSelectionForm.getOpenAIOrganization());
openAISettings.setBaseHost(serviceSelectionForm.getOpenAIBaseHost());
settings.openAIOrganization = serviceSelectionForm.getOpenAIOrganization();
settings.openAIBaseHost = serviceSelectionForm.getOpenAIBaseHost();
azureSettings.setUseAzureActiveDirectoryAuthentication(serviceSelectionForm.isAzureActiveDirectoryAuthenticationSelected());
azureSettings.setUseAzureApiKeyAuthentication(serviceSelectionForm.isAzureApiKeyAuthenticationSelected());
azureSettings.setResourceName(serviceSelectionForm.getAzureResourceName());
azureSettings.setDeploymentId(serviceSelectionForm.getAzureDeploymentId());
azureSettings.setApiVersion(serviceSelectionForm.getAzureApiVersion());
azureSettings.setBaseHost(serviceSelectionForm.getAzureBaseHost());
settings.useAzureActiveDirectoryAuthentication = serviceSelectionForm.isAzureActiveDirectoryAuthenticationSelected();
settings.useAzureApiKeyAuthentication = serviceSelectionForm.isAzureApiKeyAuthenticationSelected();
settings.azureResourceName = serviceSelectionForm.getAzureResourceName();
settings.azureDeploymentId = serviceSelectionForm.getAzureDeploymentId();
settings.azureApiVersion = serviceSelectionForm.getAzureApiVersion();
settings.azureBaseHost = serviceSelectionForm.getAzureBaseHost();
settings.chatCompletionBaseModel = modelSelectionForm.getChatCompletionBaseModel().getCode();
settings.textCompletionBaseModel = modelSelectionForm.getTextCompletionBaseModel().getCode();
settings.isChatCompletionOptionSelected = modelSelectionForm.isChatCompletionOptionSelected();
settings.isTextCompletionOptionSelected = modelSelectionForm.isTextCompletionOptionSelected();
modelSettings.setUseChatCompletion(modelSelectionForm.isChatCompletionOptionSelected());
modelSettings.setUseTextCompletion(modelSelectionForm.isTextCompletionOptionSelected());
modelSettings.setChatCompletionModel(modelSelectionForm.getChatCompletionBaseModel().getCode());
modelSettings.setTextCompletionModel(modelSelectionForm.getTextCompletionBaseModel().getCode());
}
@Override
public void reset() {
var settings = SettingsState.getInstance();
var openAISettings = OpenAISettingsState.getInstance();
var azureSettings = AzureSettingsState.getInstance();
var modelSettings = ModelSettingsState.getInstance();
var serviceSelectionForm = settingsComponent.getServiceSelectionForm();
var modelSelectionForm = settingsComponent.getModelSelectionForm();
settingsComponent.setEmail(settings.email);
settingsComponent.setDisplayName(settings.displayName);
settingsComponent.setEmail(settings.getEmail());
settingsComponent.setDisplayName(settings.getDisplayName());
serviceSelectionForm.setOpenAIServiceSelected(settings.useAzureService);
serviceSelectionForm.setAzureServiceSelected(settings.useAzureService);
serviceSelectionForm.setOpenAIServiceSelected(settings.isUseOpenAIService());
serviceSelectionForm.setAzureServiceSelected(settings.isUseAzureService());
serviceSelectionForm.setOpenAIApiKey(OpenAICredentialsManager.getInstance().getApiKey());
serviceSelectionForm.setOpenAIOrganization(settings.openAIOrganization);
serviceSelectionForm.setOpenAIBaseHost(settings.openAIBaseHost);
serviceSelectionForm.setOpenAIOrganization(openAISettings.getOrganization());
serviceSelectionForm.setOpenAIBaseHost(openAISettings.getBaseHost());
serviceSelectionForm.setAzureApiKeyAuthenticationSelected(settings.useAzureApiKeyAuthentication);
serviceSelectionForm.setAzureApiKeyAuthenticationSelected(azureSettings.isUseAzureApiKeyAuthentication());
serviceSelectionForm.setAzureApiKey(AzureCredentialsManager.getInstance().getAzureOpenAIApiKey());
serviceSelectionForm.setAzureActiveDirectoryAuthenticationSelected(settings.useAzureActiveDirectoryAuthentication);
serviceSelectionForm.setAzureActiveDirectoryAuthenticationSelected(azureSettings.isUseAzureActiveDirectoryAuthentication());
serviceSelectionForm.setAzureActiveDirectoryToken(AzureCredentialsManager.getInstance().getAzureActiveDirectoryToken());
serviceSelectionForm.setAzureResourceName(settings.azureResourceName);
serviceSelectionForm.setAzureDeploymentId(settings.azureDeploymentId);
serviceSelectionForm.setAzureApiVersion(settings.azureApiVersion);
serviceSelectionForm.setAzureBaseHost(settings.azureBaseHost);
serviceSelectionForm.setAzureResourceName(azureSettings.getResourceName());
serviceSelectionForm.setAzureDeploymentId(azureSettings.getDeploymentId());
serviceSelectionForm.setAzureApiVersion(azureSettings.getApiVersion());
serviceSelectionForm.setAzureBaseHost(azureSettings.getBaseHost());
modelSelectionForm.setUseChatCompletionSelected(settings.isChatCompletionOptionSelected);
modelSelectionForm.setUseTextCompletionSelected(settings.isTextCompletionOptionSelected);
modelSelectionForm.setChatCompletionBaseModel(settings.chatCompletionBaseModel);
modelSelectionForm.setTextCompletionBaseModel(settings.textCompletionBaseModel);
modelSelectionForm.setUseChatCompletionSelected(modelSettings.isUseChatCompletion());
modelSelectionForm.setUseTextCompletionSelected(modelSettings.isUseTextCompletion());
modelSelectionForm.setChatCompletionBaseModel(modelSettings.getChatCompletionModel());
modelSelectionForm.setTextCompletionBaseModel(modelSettings.getTextCompletionModel());
}
@Override
@ -158,16 +171,16 @@ public class SettingsConfigurable implements Configurable, Disposable {
settingsComponent = null;
}
private boolean isCompletionOptionChanged(SettingsState settings) {
private boolean isCompletionOptionChanged(ModelSettingsState settings) {
var modelSelectionForm = settingsComponent.getModelSelectionForm();
return modelSelectionForm.isChatCompletionOptionSelected() != settings.isChatCompletionOptionSelected ||
modelSelectionForm.isTextCompletionOptionSelected() != settings.isTextCompletionOptionSelected;
return modelSelectionForm.isChatCompletionOptionSelected() != settings.isUseChatCompletion() ||
modelSelectionForm.isTextCompletionOptionSelected() != settings.isUseTextCompletion();
}
private boolean isModelChanged(SettingsState settings) {
private boolean isModelChanged(ModelSettingsState settings) {
var modelSelectionForm = settingsComponent.getModelSelectionForm();
return !modelSelectionForm.getChatCompletionBaseModel().getCode().equals(settings.chatCompletionBaseModel) ||
!modelSelectionForm.getTextCompletionBaseModel().getCode().equals(settings.textCompletionBaseModel);
return !modelSelectionForm.getChatCompletionBaseModel().getCode().equals(settings.getChatCompletionModel()) ||
!modelSelectionForm.getTextCompletionBaseModel().getCode().equals(settings.getTextCompletionModel());
}
@Override

View file

@ -1,84 +0,0 @@
package ee.carlrobert.codegpt.settings;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.util.xmlb.XmlSerializerUtil;
import ee.carlrobert.codegpt.conversations.Conversation;
import ee.carlrobert.openai.client.ClientCode;
import ee.carlrobert.openai.client.completion.chat.ChatCompletionModel;
import ee.carlrobert.openai.client.completion.text.TextCompletionModel;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@State(
name = "ee.carlrobert.codegpt.state.settings.SettingsState",
storages = @Storage("CodeGPTSettings_210.xml")
)
public class SettingsState implements PersistentStateComponent<SettingsState> {
public String email = "";
public boolean previouslySignedIn;
public boolean useOpenAIService = true;
public boolean useAzureService;
public String azureResourceName = "";
public String azureDeploymentId = "";
public String azureApiVersion = "";
public String azureBaseHost = "https://%s.openai.azure.com";
public boolean useAzureApiKeyAuthentication = true;
public boolean useAzureActiveDirectoryAuthentication;
public String openAIOrganization = "";
public String openAIBaseHost = "https://api.openai.com";
public String displayName = "";
public String textCompletionBaseModel = TextCompletionModel.DAVINCI.getCode();
public String chatCompletionBaseModel = ChatCompletionModel.GPT_3_5.getCode();
public boolean isChatCompletionOptionSelected = true;
public boolean isTextCompletionOptionSelected;
public static SettingsState getInstance() {
return ApplicationManager.getApplication().getService(SettingsState.class);
}
@Nullable
@Override
public SettingsState getState() {
return this;
}
@Override
public void loadState(@NotNull SettingsState state) {
XmlSerializerUtil.copyBean(state, this);
}
public void syncSettings(Conversation conversation) {
var isChatCompletions = ClientCode.CHAT_COMPLETION.equals(conversation.getClientCode());
var model = conversation.getModel();
if (isChatCompletions) {
chatCompletionBaseModel = model;
} else {
textCompletionBaseModel = model;
}
isChatCompletionOptionSelected = isChatCompletions;
isTextCompletionOptionSelected = !isChatCompletions;
}
public String getChatCompletionModel() {
return chatCompletionBaseModel;
}
public String getTextCompletionModel() {
return textCompletionBaseModel;
}
public String getDisplayName() {
if (displayName == null || displayName.isEmpty()) {
var systemUserName = System.getProperty("user.name");
if (systemUserName == null || systemUserName.isEmpty()) {
return "User";
}
return systemUserName;
}
return displayName;
}
}

View file

@ -15,6 +15,7 @@ import com.intellij.util.ui.JBFont;
import com.intellij.util.ui.JBUI;
import ee.carlrobert.codegpt.CodeGPTBundle;
import ee.carlrobert.codegpt.credentials.UserCredentialsManager;
import ee.carlrobert.codegpt.settings.state.SettingsState;
import ee.carlrobert.codegpt.user.UserManager;
import ee.carlrobert.codegpt.user.auth.AuthenticationHandler;
import ee.carlrobert.codegpt.user.auth.AuthenticationService;
@ -38,10 +39,10 @@ public class UserDetailsSettingsPanel extends JPanel {
public UserDetailsSettingsPanel(Disposable parentDisposable, SettingsState settings) {
super(new BorderLayout());
emailField = new JBTextField(settings.email, 25);
emailField = new JBTextField(settings.getEmail(), 25);
passwordField = new JBPasswordField();
passwordField.setColumns(25);
if (!settings.email.isEmpty()) {
if (!settings.getEmail().isEmpty()) {
passwordField.setText(UserCredentialsManager.getInstance().getAccountPassword());
}
signInButton = new JButton(CodeGPTBundle.get("settingsConfigurable.section.userAuthentication.signIn.label"));

View file

@ -35,21 +35,21 @@ public class AdvancedSettingsComponent {
Proxy.Type.HTTP,
Proxy.Type.DIRECT,
});
proxyTypeComboBox.setSelectedItem(advancedSettings.proxyType);
proxyHostField = new JBTextField(advancedSettings.proxyHost, 20);
proxyTypeComboBox.setSelectedItem(advancedSettings.getProxyType());
proxyHostField = new JBTextField(advancedSettings.getProxyHost(), 20);
proxyPortField = new PortField();
proxyAuthCheckbox = new JBCheckBox(CodeGPTBundle.get("advancedSettingsConfigurable.section.proxy.authCheckBoxField.label"));
proxyAuthUsername = new JBTextField(20);
proxyAuthUsername.setEnabled(advancedSettings.isProxyAuthSelected);
proxyAuthUsername.setEnabled(advancedSettings.isProxyAuthSelected());
proxyAuthPassword = new JBPasswordField();
proxyAuthPassword.setColumns(20);
proxyAuthPassword.setEnabled(advancedSettings.isProxyAuthSelected);
proxyAuthPassword.setEnabled(advancedSettings.isProxyAuthSelected());
proxyAuthCheckbox.addItemListener(itemEvent -> {
proxyAuthUsername.setEnabled(itemEvent.getStateChange() == ItemEvent.SELECTED);
proxyAuthPassword.setEnabled(itemEvent.getStateChange() == ItemEvent.SELECTED);
});
connectionTimeoutField = new PortField(advancedSettings.connectTimeout);
readTimeoutField = new PortField(advancedSettings.readTimeout);
connectionTimeoutField = new PortField(advancedSettings.getConnectTimeout());
readTimeoutField = new PortField(advancedSettings.getReadTimeout());
mainPanel = FormBuilder.createFormBuilder()
.addComponent(new TitledSeparator(CodeGPTBundle.get("advancedSettingsConfigurable.section.proxy.title")))

View file

@ -27,40 +27,40 @@ public class AdvancedSettingsConfigurable implements Configurable {
@Override
public boolean isModified() {
var advancedSettings = AdvancedSettingsState.getInstance();
return !advancedSettingsComponent.getProxyType().equals(advancedSettings.proxyType) ||
!advancedSettingsComponent.getProxyHost().equals(advancedSettings.proxyHost) ||
advancedSettingsComponent.getProxyPort() != advancedSettings.proxyPort ||
advancedSettingsComponent.isProxyAuthSelected() != advancedSettings.isProxyAuthSelected ||
!advancedSettingsComponent.getProxyAuthUsername().equals(advancedSettings.proxyUsername) ||
!advancedSettingsComponent.getProxyAuthPassword().equals(advancedSettings.proxyPassword) ||
advancedSettingsComponent.getConnectionTimeout() != advancedSettings.connectTimeout ||
advancedSettingsComponent.getReadTimeout() != advancedSettings.readTimeout;
return !advancedSettingsComponent.getProxyType().equals(advancedSettings.getProxyType()) ||
!advancedSettingsComponent.getProxyHost().equals(advancedSettings.getProxyHost()) ||
advancedSettingsComponent.getProxyPort() != advancedSettings.getProxyPort() ||
advancedSettingsComponent.isProxyAuthSelected() != advancedSettings.isProxyAuthSelected() ||
!advancedSettingsComponent.getProxyAuthUsername().equals(advancedSettings.getProxyUsername()) ||
!advancedSettingsComponent.getProxyAuthPassword().equals(advancedSettings.getProxyPassword()) ||
advancedSettingsComponent.getConnectionTimeout() != advancedSettings.getConnectTimeout() ||
advancedSettingsComponent.getReadTimeout() != advancedSettings.getReadTimeout();
}
@Override
public void apply() {
var advancedSettings = AdvancedSettingsState.getInstance();
advancedSettings.proxyType = advancedSettingsComponent.getProxyType();
advancedSettings.proxyHost = advancedSettingsComponent.getProxyHost();
advancedSettings.proxyPort = advancedSettingsComponent.getProxyPort();
advancedSettings.isProxyAuthSelected = advancedSettingsComponent.isProxyAuthSelected();
advancedSettings.proxyUsername = advancedSettingsComponent.getProxyAuthUsername();
advancedSettings.proxyPassword = advancedSettingsComponent.getProxyAuthPassword();
advancedSettings.connectTimeout = advancedSettingsComponent.getConnectionTimeout();
advancedSettings.readTimeout = advancedSettingsComponent.getReadTimeout();
advancedSettings.setProxyType(advancedSettingsComponent.getProxyType());
advancedSettings.setProxyHost(advancedSettingsComponent.getProxyHost());
advancedSettings.setProxyPort(advancedSettingsComponent.getProxyPort());
advancedSettings.setProxyAuthSelected(advancedSettingsComponent.isProxyAuthSelected());
advancedSettings.setProxyUsername(advancedSettingsComponent.getProxyAuthUsername());
advancedSettings.setProxyPassword(advancedSettingsComponent.getProxyAuthPassword());
advancedSettings.setConnectTimeout(advancedSettingsComponent.getConnectionTimeout());
advancedSettings.setReadTimeout(advancedSettingsComponent.getReadTimeout());
}
@Override
public void reset() {
var advancedSettings = AdvancedSettingsState.getInstance();
advancedSettingsComponent.setProxyType(advancedSettings.proxyType);
advancedSettingsComponent.setProxyHost(advancedSettings.proxyHost);
advancedSettingsComponent.setProxyPort(advancedSettings.proxyPort);
advancedSettingsComponent.setUseProxyAuthentication(advancedSettings.isProxyAuthSelected);
advancedSettingsComponent.setProxyUsername(advancedSettings.proxyUsername);
advancedSettingsComponent.setProxyPassword(advancedSettings.proxyPassword);
advancedSettingsComponent.setConnectionTimeoutField(advancedSettings.connectTimeout);
advancedSettingsComponent.setReadTimeout(advancedSettings.readTimeout);
advancedSettingsComponent.setProxyType(advancedSettings.getProxyType());
advancedSettingsComponent.setProxyHost(advancedSettings.getProxyHost());
advancedSettingsComponent.setProxyPort(advancedSettings.getProxyPort());
advancedSettingsComponent.setUseProxyAuthentication(advancedSettings.isProxyAuthSelected());
advancedSettingsComponent.setProxyUsername(advancedSettings.getProxyUsername());
advancedSettingsComponent.setProxyPassword(advancedSettings.getProxyPassword());
advancedSettingsComponent.setConnectionTimeoutField(advancedSettings.getConnectTimeout());
advancedSettingsComponent.setReadTimeout(advancedSettings.getReadTimeout());
}
@Override

View file

@ -9,20 +9,17 @@ import java.net.Proxy;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@State(
name = "ee.carlrobert.codegpt.state.settings.advanced.AdvancedSettingsState",
storages = @Storage("CodeGPTAdvancedSettings.xml")
)
@State(name = "CodeGPT_AdvancedSettings", storages = @Storage("CodeGPT_AdvancedSettings.xml"))
public class AdvancedSettingsState implements PersistentStateComponent<AdvancedSettingsState> {
public String proxyHost = "";
public int proxyPort;
public Proxy.Type proxyType = Proxy.Type.SOCKS;
public boolean isProxyAuthSelected;
public String proxyUsername;
public String proxyPassword;
public int connectTimeout = 30;
public int readTimeout = 30;
private String proxyHost = "";
private int proxyPort;
private Proxy.Type proxyType = Proxy.Type.SOCKS;
private boolean proxyAuthSelected;
private String proxyUsername;
private String proxyPassword;
private int connectTimeout = 30;
private int readTimeout = 30;
public static AdvancedSettingsState getInstance() {
return ApplicationManager.getApplication().getService(AdvancedSettingsState.class);
@ -38,4 +35,68 @@ public class AdvancedSettingsState implements PersistentStateComponent<AdvancedS
public void loadState(@NotNull AdvancedSettingsState state) {
XmlSerializerUtil.copyBean(state, this);
}
public String getProxyHost() {
return proxyHost;
}
public void setProxyHost(String proxyHost) {
this.proxyHost = proxyHost;
}
public int getProxyPort() {
return proxyPort;
}
public void setProxyPort(int proxyPort) {
this.proxyPort = proxyPort;
}
public Proxy.Type getProxyType() {
return proxyType;
}
public void setProxyType(Proxy.Type proxyType) {
this.proxyType = proxyType;
}
public boolean isProxyAuthSelected() {
return proxyAuthSelected;
}
public void setProxyAuthSelected(boolean proxyAuthSelected) {
this.proxyAuthSelected = proxyAuthSelected;
}
public String getProxyUsername() {
return proxyUsername;
}
public void setProxyUsername(String proxyUsername) {
this.proxyUsername = proxyUsername;
}
public String getProxyPassword() {
return proxyPassword;
}
public void setProxyPassword(String proxyPassword) {
this.proxyPassword = proxyPassword;
}
public int getConnectTimeout() {
return connectTimeout;
}
public void setConnectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
}
public int getReadTimeout() {
return readTimeout;
}
public void setReadTimeout(int readTimeout) {
this.readTimeout = readTimeout;
}
}

View file

@ -50,7 +50,7 @@ public class ConfigurationComponent {
public ConfigurationComponent(Disposable parentDisposable, ConfigurationState configuration) {
table = new JBTable(new DefaultTableModel(
EditorActionsUtil.toArray(configuration.tableData),
EditorActionsUtil.toArray(configuration.getTableData()),
new String[] {
CodeGPTBundle.get("configurationConfigurable.table.header.actionColumnLabel"),
CodeGPTBundle.get("configurationConfigurable.table.header.promptColumnLabel")
@ -62,7 +62,7 @@ public class ConfigurationComponent {
tablePanel.setBorder(BorderFactory.createTitledBorder(CodeGPTBundle.get("configurationConfigurable.table.title")));
temperatureField = new JBTextField(12);
temperatureField.setText(String.valueOf(configuration.temperature));
temperatureField.setText(String.valueOf(configuration.getTemperature()));
var temperatureFieldValidator = createInputValidator(parentDisposable, temperatureField);
temperatureField.getDocument().addDocumentListener(new DocumentListener() {
@ -84,7 +84,7 @@ public class ConfigurationComponent {
maxTokensField = new IntegerField("max_tokens", 100, 2000);
maxTokensField.setColumns(12);
maxTokensField.setValue(configuration.maxTokens);
maxTokensField.setValue(configuration.getMaxTokens());
systemPromptTextArea = new JTextArea();
systemPromptTextArea.setLineWrap(true);

View file

@ -29,32 +29,32 @@ public class ConfigurationConfigurable implements Configurable, Disposable {
@Override
public boolean isModified() {
var configuration = ConfigurationState.getInstance();
return !configurationComponent.getTableData().equals(configuration.tableData) ||
configurationComponent.getMaxTokens() != configuration.maxTokens ||
configurationComponent.getTemperature() != configuration.temperature ||
!configurationComponent.getSystemPrompt().equals(configuration.systemPrompt) ||
configurationComponent.isCreateNewChatOnEachAction() != configuration.createNewChatOnEachAction;
return !configurationComponent.getTableData().equals(configuration.getTableData()) ||
configurationComponent.getMaxTokens() != configuration.getMaxTokens() ||
configurationComponent.getTemperature() != configuration.getTemperature() ||
!configurationComponent.getSystemPrompt().equals(configuration.getSystemPrompt()) ||
configurationComponent.isCreateNewChatOnEachAction() != configuration.isCreateNewChatOnEachAction();
}
@Override
public void apply() {
var configuration = ConfigurationState.getInstance();
configuration.tableData = configurationComponent.getTableData();
configuration.maxTokens = configurationComponent.getMaxTokens();
configuration.temperature = configurationComponent.getTemperature();
configuration.systemPrompt = configurationComponent.getSystemPrompt();
configuration.createNewChatOnEachAction = configurationComponent.isCreateNewChatOnEachAction();
configuration.setTableData(configurationComponent.getTableData());
configuration.setMaxTokens(configurationComponent.getMaxTokens());
configuration.setTemperature(configurationComponent.getTemperature());
configuration.setSystemPrompt(configurationComponent.getSystemPrompt());
configuration.setCreateNewChatOnEachAction(configurationComponent.isCreateNewChatOnEachAction());
EditorActionsUtil.refreshActions();
}
@Override
public void reset() {
var configuration = ConfigurationState.getInstance();
configurationComponent.setTableData(configuration.tableData);
configurationComponent.setMaxTokens(configuration.maxTokens);
configurationComponent.setTemperature(configuration.temperature);
configurationComponent.setSystemPrompt(configuration.systemPrompt);
configurationComponent.setCreateNewChatOnEachAction(configuration.createNewChatOnEachAction);
configurationComponent.setTableData(configuration.getTableData());
configurationComponent.setMaxTokens(configuration.getMaxTokens());
configurationComponent.setTemperature(configuration.getTemperature());
configurationComponent.setSystemPrompt(configuration.getSystemPrompt());
configurationComponent.setCreateNewChatOnEachAction(configuration.isCreateNewChatOnEachAction());
EditorActionsUtil.refreshActions();
}

View file

@ -16,12 +16,11 @@ import org.jetbrains.annotations.Nullable;
)
public class ConfigurationState implements PersistentStateComponent<ConfigurationState> {
public String systemPrompt = "";
public int maxTokens = 1000;
public double temperature = 0.2;
public boolean createNewChatOnEachAction;
public Map<String, String> tableData = EditorActionsUtil.DEFAULT_ACTIONS;
private String systemPrompt = "";
private int maxTokens = 1000;
private double temperature = 0.2;
private boolean createNewChatOnEachAction;
private Map<String, String> tableData = EditorActionsUtil.DEFAULT_ACTIONS;
public static ConfigurationState getInstance() {
return ApplicationManager.getApplication().getService(ConfigurationState.class);
@ -37,4 +36,44 @@ public class ConfigurationState implements PersistentStateComponent<Configuratio
public void loadState(@NotNull ConfigurationState state) {
XmlSerializerUtil.copyBean(state, this);
}
public String getSystemPrompt() {
return systemPrompt;
}
public void setSystemPrompt(String systemPrompt) {
this.systemPrompt = systemPrompt;
}
public int getMaxTokens() {
return maxTokens;
}
public void setMaxTokens(int maxTokens) {
this.maxTokens = maxTokens;
}
public double getTemperature() {
return temperature;
}
public void setTemperature(double temperature) {
this.temperature = temperature;
}
public boolean isCreateNewChatOnEachAction() {
return createNewChatOnEachAction;
}
public void setCreateNewChatOnEachAction(boolean createNewChatOnEachAction) {
this.createNewChatOnEachAction = createNewChatOnEachAction;
}
public Map<String, String> getTableData() {
return tableData;
}
public void setTableData(Map<String, String> tableData) {
this.tableData = tableData;
}
}

View file

@ -0,0 +1,81 @@
package ee.carlrobert.codegpt.settings.state;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.util.xmlb.XmlSerializerUtil;
import org.jetbrains.annotations.NotNull;
@State(name = "CodeGPT_AzureSettings", storages = @Storage("CodeGPT_AzureSettings.xml"))
public class AzureSettingsState implements PersistentStateComponent<AzureSettingsState> {
private String resourceName = "";
private String deploymentId = "";
private String apiVersion = "";
private String baseHost = "https://%s.openai.azure.com";
private boolean useAzureApiKeyAuthentication = true;
private boolean useAzureActiveDirectoryAuthentication;
public static AzureSettingsState getInstance() {
return ApplicationManager.getApplication().getService(AzureSettingsState.class);
}
@Override
public AzureSettingsState getState() {
return this;
}
@Override
public void loadState(@NotNull AzureSettingsState state) {
XmlSerializerUtil.copyBean(state, this);
}
public String getResourceName() {
return resourceName;
}
public void setResourceName(String resourceName) {
this.resourceName = resourceName;
}
public String getDeploymentId() {
return deploymentId;
}
public void setDeploymentId(String deploymentId) {
this.deploymentId = deploymentId;
}
public String getApiVersion() {
return apiVersion;
}
public void setApiVersion(String apiVersion) {
this.apiVersion = apiVersion;
}
public String getBaseHost() {
return baseHost;
}
public void setBaseHost(String baseHost) {
this.baseHost = baseHost;
}
public boolean isUseAzureApiKeyAuthentication() {
return useAzureApiKeyAuthentication;
}
public void setUseAzureApiKeyAuthentication(boolean useAzureApiKeyAuthentication) {
this.useAzureApiKeyAuthentication = useAzureApiKeyAuthentication;
}
public boolean isUseAzureActiveDirectoryAuthentication() {
return useAzureActiveDirectoryAuthentication;
}
public void setUseAzureActiveDirectoryAuthentication(boolean useAzureActiveDirectoryAuthentication) {
this.useAzureActiveDirectoryAuthentication = useAzureActiveDirectoryAuthentication;
}
}

View file

@ -0,0 +1,82 @@
package ee.carlrobert.codegpt.settings.state;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.util.xmlb.XmlSerializerUtil;
import ee.carlrobert.codegpt.conversations.Conversation;
import ee.carlrobert.openai.client.ClientCode;
import ee.carlrobert.openai.client.completion.chat.ChatCompletionModel;
import ee.carlrobert.openai.client.completion.text.TextCompletionModel;
import org.jetbrains.annotations.NotNull;
@State(name = "CodeGPT_ModelSettings", storages = @Storage("CodeGPT_ModelSettings.xml"))
public class ModelSettingsState implements PersistentStateComponent<ModelSettingsState> {
private String textCompletionModel = TextCompletionModel.DAVINCI.getCode();
private String chatCompletionModel = ChatCompletionModel.GPT_3_5.getCode();
private boolean useChatCompletion = true;
private boolean useTextCompletion;
public static ModelSettingsState getInstance() {
return ApplicationManager.getApplication().getService(ModelSettingsState.class);
}
@Override
public ModelSettingsState getState() {
return this;
}
@Override
public void loadState(@NotNull ModelSettingsState state) {
XmlSerializerUtil.copyBean(state, this);
}
public void sync(Conversation conversation) {
var isChatCompletion = ClientCode.CHAT_COMPLETION.equals(conversation.getClientCode());
if (isChatCompletion) {
chatCompletionModel = conversation.getModel();
} else {
textCompletionModel = conversation.getModel();
}
useChatCompletion = isChatCompletion;
useTextCompletion = !isChatCompletion;
}
public String getCompletionModel() {
return useChatCompletion ? chatCompletionModel : textCompletionModel;
}
public String getTextCompletionModel() {
return textCompletionModel;
}
public void setTextCompletionModel(String textCompletionModel) {
this.textCompletionModel = textCompletionModel;
}
public String getChatCompletionModel() {
return chatCompletionModel;
}
public void setChatCompletionModel(String chatCompletionModel) {
this.chatCompletionModel = chatCompletionModel;
}
public boolean isUseChatCompletion() {
return useChatCompletion;
}
public void setUseChatCompletion(boolean useChatCompletion) {
this.useChatCompletion = useChatCompletion;
}
public boolean isUseTextCompletion() {
return useTextCompletion;
}
public void setUseTextCompletion(boolean useTextCompletion) {
this.useTextCompletion = useTextCompletion;
}
}

View file

@ -0,0 +1,45 @@
package ee.carlrobert.codegpt.settings.state;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.util.xmlb.XmlSerializerUtil;
import org.jetbrains.annotations.NotNull;
@State(name = "CodeGPT_OpenAISettings", storages = @Storage("CodeGPT_OpenAISettings.xml"))
public class OpenAISettingsState implements PersistentStateComponent<OpenAISettingsState> {
private String organization = "";
private String baseHost = "https://api.openai.com";
public static OpenAISettingsState getInstance() {
return ApplicationManager.getApplication().getService(OpenAISettingsState.class);
}
@Override
public OpenAISettingsState getState() {
return this;
}
@Override
public void loadState(@NotNull OpenAISettingsState state) {
XmlSerializerUtil.copyBean(state, this);
}
public String getOrganization() {
return organization;
}
public void setOrganization(String organization) {
this.organization = organization;
}
public String getBaseHost() {
return baseHost;
}
public void setBaseHost(String openAIBaseHost) {
this.baseHost = openAIBaseHost;
}
}

View file

@ -0,0 +1,85 @@
package ee.carlrobert.codegpt.settings.state;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.util.xmlb.XmlSerializerUtil;
import org.jetbrains.annotations.NotNull;
@State(
name = "ee.carlrobert.codegpt.state.settings.SettingsState",
storages = @Storage("CodeGPTSettings_210.xml")
)
public class SettingsState implements PersistentStateComponent<SettingsState> {
private String email = "";
private String displayName = "";
private boolean previouslySignedIn;
private boolean useOpenAIService = true;
private boolean useAzureService;
public SettingsState() {
}
public static SettingsState getInstance() {
return ApplicationManager.getApplication().getService(SettingsState.class);
}
@Override
public SettingsState getState() {
return this;
}
@Override
public void loadState(@NotNull SettingsState state) {
XmlSerializerUtil.copyBean(state, this);
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getDisplayName() {
if (displayName == null || displayName.isEmpty()) {
var systemUserName = System.getProperty("user.name");
if (systemUserName == null || systemUserName.isEmpty()) {
return "User";
}
return systemUserName;
}
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public boolean isPreviouslySignedIn() {
return previouslySignedIn;
}
public void setPreviouslySignedIn(boolean previouslySignedIn) {
this.previouslySignedIn = previouslySignedIn;
}
public boolean isUseOpenAIService() {
return useOpenAIService;
}
public void setUseOpenAIService(boolean useOpenAIService) {
this.useOpenAIService = useOpenAIService;
}
public boolean isUseAzureService() {
return useAzureService;
}
public void setUseAzureService(boolean useAzureService) {
this.useAzureService = useAzureService;
}
}

View file

@ -16,7 +16,7 @@ import ee.carlrobert.codegpt.conversations.ConversationService;
import ee.carlrobert.codegpt.conversations.message.Message;
import ee.carlrobert.codegpt.credentials.AzureCredentialsManager;
import ee.carlrobert.codegpt.credentials.OpenAICredentialsManager;
import ee.carlrobert.codegpt.settings.SettingsState;
import ee.carlrobert.codegpt.settings.state.SettingsState;
import ee.carlrobert.codegpt.util.EditorUtils;
import ee.carlrobert.codegpt.util.FileUtils;
import ee.carlrobert.codegpt.util.OverlayUtils;
@ -113,7 +113,7 @@ public abstract class BaseChatToolWindowTabPanel implements ChatToolWindowTabPan
}
private boolean isCredentialSet() {
if (SettingsState.getInstance().useAzureService) {
if (SettingsState.getInstance().isUseAzureService()) {
return AzureCredentialsManager.getInstance().isCredentialSet();
}
return OpenAICredentialsManager.getInstance().isApiKeySet();
@ -127,7 +127,7 @@ public abstract class BaseChatToolWindowTabPanel implements ChatToolWindowTabPan
return;
}
var requestHandler = new CompletionRequestHandler(project);
var requestHandler = new CompletionRequestHandler();
requestHandler.withContextualSearch(useContextualSearch);
requestHandler.addMessageListener(partialMessage -> {
try {

View file

@ -8,7 +8,7 @@ import com.intellij.util.ui.JBFont;
import com.intellij.util.ui.JBUI;
import com.intellij.util.ui.UIUtil;
import ee.carlrobert.codegpt.conversations.message.Message;
import ee.carlrobert.codegpt.settings.SettingsState;
import ee.carlrobert.codegpt.settings.state.SettingsState;
import java.awt.BorderLayout;
import javax.swing.JPanel;

View file

@ -48,7 +48,7 @@ public class StandardChatToolWindowContentManager {
toolWindow.show();
}
if (ConfigurationState.getInstance().createNewChatOnEachAction || ConversationsState.getCurrentConversation() == null) {
if (ConfigurationState.getInstance().isCreateNewChatOnEachAction() || ConversationsState.getCurrentConversation() == null) {
toolWindowTabPanel.startNewConversation(message);
} else {
toolWindowTabPanel.sendMessage(message);

View file

@ -4,7 +4,7 @@ import static ee.carlrobert.codegpt.util.ThemeUtils.getPanelBackgroundColor;
import static javax.swing.event.HyperlinkEvent.EventType.ACTIVATED;
import com.intellij.openapi.diagnostic.Logger;
import ee.carlrobert.codegpt.settings.SettingsState;
import ee.carlrobert.codegpt.settings.state.SettingsState;
import ee.carlrobert.codegpt.toolwindow.chat.ResponsePanel;
import ee.carlrobert.codegpt.util.SwingUtils;
import java.awt.event.MouseEvent;

View file

@ -8,7 +8,7 @@ import com.intellij.ui.components.JBLabel;
import com.intellij.ui.components.JBTabbedPane;
import com.intellij.util.ui.JBUI;
import ee.carlrobert.codegpt.conversations.ConversationsState;
import ee.carlrobert.codegpt.settings.SettingsState;
import ee.carlrobert.codegpt.settings.state.ModelSettingsState;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
@ -19,7 +19,6 @@ import java.util.TreeMap;
import java.util.UUID;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
@ -105,7 +104,7 @@ public class StandardChatToolWindowTabbedPane extends JBTabbedPane {
var conversation = toolWindowPanel.getConversation();
if (conversation != null) {
ConversationsState.getInstance().setCurrentConversation(conversation);
SettingsState.getInstance().syncSettings(conversation);
ModelSettingsState.getInstance().sync(conversation);
}
}
}

View file

@ -15,7 +15,7 @@ import ee.carlrobert.codegpt.actions.toolwindow.MoveUpAction;
import ee.carlrobert.codegpt.conversations.Conversation;
import ee.carlrobert.codegpt.conversations.ConversationService;
import ee.carlrobert.codegpt.conversations.ConversationsState;
import ee.carlrobert.codegpt.settings.SettingsState;
import ee.carlrobert.codegpt.settings.state.ModelSettingsState;
import ee.carlrobert.codegpt.toolwindow.chat.standard.StandardChatToolWindowContentManager;
import ee.carlrobert.codegpt.toolwindow.chat.standard.StandardChatToolWindowTabPanel;
import javax.swing.BoxLayout;
@ -76,7 +76,7 @@ public class ConversationsToolWindow {
private void addContent(Conversation conversation) {
var mainPanel = new RootConversationPanel(() -> {
SettingsState.getInstance().syncSettings(conversation);
ModelSettingsState.getInstance().sync(conversation);
var toolWindowContentManager = StandardChatToolWindowContentManager.getInstance(project);
toolWindowContentManager.displayChatTab();

View file

@ -6,7 +6,7 @@ import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.Service;
import com.intellij.openapi.diagnostic.Logger;
import ee.carlrobert.codegpt.credentials.UserCredentialsManager;
import ee.carlrobert.codegpt.settings.SettingsState;
import ee.carlrobert.codegpt.settings.state.SettingsState;
import ee.carlrobert.codegpt.user.ApiClient;
import ee.carlrobert.codegpt.user.UserManager;
import ee.carlrobert.codegpt.util.OverlayUtils;
@ -68,7 +68,7 @@ public final class AuthenticationService {
}
private void handleSuccessfulAuthentication(Session session) {
SettingsState.getInstance().previouslySignedIn = true;
SettingsState.getInstance().setPreviouslySignedIn(true);
var userManager = UserManager.getInstance();
userManager.setSession(session);
@ -111,7 +111,7 @@ public final class AuthenticationService {
try {
var session = new ObjectMapper().readValue(body.string(), Session.class);
handleSuccessfulAuthentication(session);
SettingsState.getInstance().email = email;
SettingsState.getInstance().setEmail(email);
UserCredentialsManager.getInstance().setAccountPassword(password);
authenticationHandler.handleAuthenticated();
return;