Skip to content

Commit

Permalink
feat: settings and credentials refactoring (carlrobertoh#360)
Browse files Browse the repository at this point in the history
* refactor service credential managers

* refactor azure settings

* refactor openai settings

* refactor llama settings

* refactor you settings

* refactor included files settings

* refactor general settings

* refactor advanced settings

* fix advanced settings component init

* refactor project structure

* refactor service settings forms

* remove openai quota exceeded field validator

* fix credential modified conditions

* fix and rearrange minor stuff

* fix you auth logic, add credential cache
  • Loading branch information
carlrobertoh authored Feb 7, 2024
1 parent 7c067d9 commit 9314509
Show file tree
Hide file tree
Showing 80 changed files with 1,837 additions and 2,035 deletions.
13 changes: 5 additions & 8 deletions src/main/java/ee/carlrobert/codegpt/PluginStartupActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
import ee.carlrobert.codegpt.completions.you.auth.YouAuthenticationError;
import ee.carlrobert.codegpt.completions.you.auth.YouAuthenticationService;
import ee.carlrobert.codegpt.completions.you.auth.response.YouAuthenticationResponse;
import ee.carlrobert.codegpt.credentials.YouCredentialsManager;
import ee.carlrobert.codegpt.settings.state.SettingsState;
import ee.carlrobert.codegpt.credentials.YouCredentialManager;
import ee.carlrobert.codegpt.settings.GeneralSettings;
import ee.carlrobert.codegpt.settings.service.you.YouSettings;
import ee.carlrobert.codegpt.ui.OverlayUtil;
import org.jetbrains.annotations.NotNull;

Expand All @@ -29,12 +30,8 @@ public void runActivity(@NotNull Project project) {
}

private void handleYouServiceAuthentication() {
var settings = SettingsState.getInstance();
if (!settings.isPreviouslySignedIn()) {
return;
}

var password = YouCredentialsManager.getInstance().getAccountPassword();
var settings = YouSettings.getCurrentState();
var password = YouCredentialManager.getInstance().getCredential();
if (!settings.getEmail().isEmpty() && password != null && !password.isEmpty()) {
YouAuthenticationService.getInstance()
.signInAsync(settings.getEmail(), password, new AuthenticationHandler() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
import ee.carlrobert.codegpt.Icons;
import ee.carlrobert.codegpt.completions.CompletionRequestService;
import ee.carlrobert.codegpt.credentials.AzureCredentialsManager;
import ee.carlrobert.codegpt.credentials.OpenAICredentialsManager;
import ee.carlrobert.codegpt.credentials.OpenAICredentialManager;
import ee.carlrobert.codegpt.settings.GeneralSettings;
import ee.carlrobert.codegpt.settings.service.ServiceType;
import ee.carlrobert.codegpt.settings.state.SettingsState;
import ee.carlrobert.codegpt.ui.OverlayUtil;
import ee.carlrobert.llm.client.openai.completion.ErrorDetails;
import ee.carlrobert.llm.completion.CompletionEventListener;
Expand All @@ -53,11 +53,11 @@ public GenerateGitCommitMessageAction() {

@Override
public void update(@NotNull AnActionEvent event) {
var selectedService = SettingsState.getInstance().getSelectedService();
var selectedService = GeneralSettings.getCurrentState().getSelectedService();
if (selectedService == ServiceType.OPENAI || selectedService == ServiceType.AZURE) {
var filesSelected = !getReferencedFilePaths(event).isEmpty();
var callAllowed = (selectedService == ServiceType.OPENAI
&& OpenAICredentialsManager.getInstance().isApiKeySet())
&& OpenAICredentialManager.getInstance().isCredentialSet())
|| (selectedService == ServiceType.AZURE
&& AzureCredentialsManager.getInstance().isCredentialSet());
event.getPresentation().setEnabled(callAllowed && filesSelected);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static com.intellij.openapi.actionSystem.CommonDataKeys.VIRTUAL_FILE_ARRAY;
import static com.intellij.openapi.ui.DialogWrapper.OK_EXIT_CODE;
import static ee.carlrobert.codegpt.settings.IncludedFilesSettingsState.DEFAULT_PROMPT_TEMPLATE;
import static ee.carlrobert.codegpt.settings.IncludedFilesSettingsState.DEFAULT_REPEATABLE_CONTEXT;
import static java.lang.String.format;

import com.intellij.openapi.actionSystem.AnAction;
Expand All @@ -24,7 +26,7 @@
import ee.carlrobert.codegpt.CodeGPTBundle;
import ee.carlrobert.codegpt.CodeGPTKeys;
import ee.carlrobert.codegpt.EncodingManager;
import ee.carlrobert.codegpt.settings.state.IncludedFilesSettingsState;
import ee.carlrobert.codegpt.settings.IncludedFilesSettings;
import ee.carlrobert.codegpt.ui.UIUtil;
import ee.carlrobert.codegpt.ui.checkbox.FileCheckboxTree;
import ee.carlrobert.codegpt.ui.checkbox.PsiElementCheckboxTree;
Expand Down Expand Up @@ -70,7 +72,7 @@ public void nodeStateChanged(@NotNull CheckedTreeNode node) {
}
});

var includedFilesSettings = IncludedFilesSettingsState.getInstance();
var includedFilesSettings = IncludedFilesSettings.getCurrentState();
var promptTemplateTextArea = UIUtil.createTextArea(includedFilesSettings.getPromptTemplate());
var repeatableContextTextArea =
UIUtil.createTextArea(includedFilesSettings.getRepeatableContext());
Expand Down Expand Up @@ -225,12 +227,11 @@ private static JButton getRestoreButton(JBTextArea promptTemplateTextArea,
var restoreButton = new JButton(
CodeGPTBundle.get("action.includeFilesInContext.dialog.restoreToDefaults.label"));
restoreButton.addActionListener(e -> {
var includedFilesSettings = IncludedFilesSettingsState.getInstance();
includedFilesSettings.setPromptTemplate(IncludedFilesSettingsState.DEFAULT_PROMPT_TEMPLATE);
includedFilesSettings.setRepeatableContext(
IncludedFilesSettingsState.DEFAULT_REPEATABLE_CONTEXT);
promptTemplateTextArea.setText(IncludedFilesSettingsState.DEFAULT_PROMPT_TEMPLATE);
repeatableContextTextArea.setText(IncludedFilesSettingsState.DEFAULT_REPEATABLE_CONTEXT);
var includedFilesSettings = IncludedFilesSettings.getCurrentState();
includedFilesSettings.setPromptTemplate(DEFAULT_PROMPT_TEMPLATE);
includedFilesSettings.setRepeatableContext(DEFAULT_REPEATABLE_CONTEXT);
promptTemplateTextArea.setText(DEFAULT_PROMPT_TEMPLATE);
repeatableContextTextArea.setText(DEFAULT_REPEATABLE_CONTEXT);
});
return restoreButton;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.options.ShowSettingsUtil;
import ee.carlrobert.codegpt.CodeGPTBundle;
import ee.carlrobert.codegpt.settings.SettingsConfigurable;
import ee.carlrobert.codegpt.settings.GeneralSettingsConfigurable;
import org.jetbrains.annotations.NotNull;

public class OpenSettingsAction extends AnAction {
Expand All @@ -18,6 +18,7 @@ public OpenSettingsAction() {

@Override
public void actionPerformed(@NotNull AnActionEvent e) {
ShowSettingsUtil.getInstance().showSettingsDialog(e.getProject(), SettingsConfigurable.class);
ShowSettingsUtil.getInstance()
.showSettingsDialog(e.getProject(), GeneralSettingsConfigurable.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.intellij.openapi.progress.impl.BackgroundableProcessIndicator;
import com.intellij.openapi.project.Project;
import ee.carlrobert.codegpt.CodeGPTBundle;
import ee.carlrobert.codegpt.settings.state.SettingsState;
import ee.carlrobert.codegpt.settings.GeneralSettings;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
Expand Down Expand Up @@ -35,9 +35,10 @@ public void debounce(Object key, CallRunnable runnable, long delay, TimeUnit uni
Future<?> prev = delayedMap.put(key, scheduler.schedule(() -> {
try {
cancelPreviousCall();
var progressIndicator = LLAMA_CPP.equals(SettingsState.getInstance().getSelectedService())
? createProgressIndicator()
: null;
var progressIndicator =
LLAMA_CPP.equals(GeneralSettings.getCurrentState().getSelectedService())
? createProgressIndicator()
: null;
currentCall.set(runnable.call(progressIndicator));
} finally {
delayedMap.remove(key);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ee.carlrobert.codegpt.codecompletions;

import ee.carlrobert.codegpt.completions.llama.LlamaModel;
import ee.carlrobert.codegpt.settings.state.LlamaSettingsState;
import ee.carlrobert.codegpt.settings.service.llama.LlamaSettings;
import ee.carlrobert.llm.client.llama.completion.LlamaCompletionRequest;
import ee.carlrobert.llm.client.openai.completion.request.OpenAITextCompletionRequest;
import javax.annotation.ParametersAreNonnullByDefault;
Expand Down Expand Up @@ -38,7 +38,7 @@ public LlamaCompletionRequest buildLlamaRequest() {
}

private InfillPromptTemplate getLlamaInfillPromptTemplate() {
var settings = LlamaSettingsState.getInstance();
var settings = LlamaSettings.getCurrentState();
if (!settings.isRunLocalServer()) {
return settings.getRemoteModelInfillPromptTemplate();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
import ee.carlrobert.codegpt.CodeGPTPlugin;
import ee.carlrobert.codegpt.completions.you.YouUserManager;
import ee.carlrobert.codegpt.credentials.AzureCredentialsManager;
import ee.carlrobert.codegpt.credentials.LlamaCredentialsManager;
import ee.carlrobert.codegpt.credentials.OpenAICredentialsManager;
import ee.carlrobert.codegpt.settings.advanced.AdvancedSettingsState;
import ee.carlrobert.codegpt.settings.state.AzureSettingsState;
import ee.carlrobert.codegpt.settings.state.LlamaSettingsState;
import ee.carlrobert.codegpt.settings.state.OpenAISettingsState;
import ee.carlrobert.codegpt.credentials.LlamaCredentialManager;
import ee.carlrobert.codegpt.credentials.OpenAICredentialManager;
import ee.carlrobert.codegpt.settings.advanced.AdvancedSettings;
import ee.carlrobert.codegpt.settings.service.azure.AzureSettings;
import ee.carlrobert.codegpt.settings.service.llama.LlamaSettings;
import ee.carlrobert.codegpt.settings.service.openai.OpenAISettings;
import ee.carlrobert.llm.client.azure.AzureClient;
import ee.carlrobert.llm.client.azure.AzureCompletionRequestParams;
import ee.carlrobert.llm.client.llama.LlamaClient;
import ee.carlrobert.llm.client.llama.LlamaClient.Builder;
import ee.carlrobert.llm.client.openai.OpenAIClient;
import ee.carlrobert.llm.client.you.UTMParameters;
import ee.carlrobert.llm.client.you.YouClient;
Expand All @@ -27,8 +26,8 @@
public class CompletionClientProvider {

public static OpenAIClient getOpenAIClient() {
var settings = OpenAISettingsState.getInstance();
var builder = new OpenAIClient.Builder(OpenAICredentialsManager.getInstance().getApiKey())
var settings = OpenAISettings.getCurrentState();
var builder = new OpenAIClient.Builder(OpenAICredentialManager.getInstance().getCredential())
.setOrganization(settings.getOrganization());
var baseHost = settings.getBaseHost();
if (baseHost != null) {
Expand All @@ -38,12 +37,13 @@ public static OpenAIClient getOpenAIClient() {
}

public static AzureClient getAzureClient() {
var settings = AzureSettingsState.getInstance();
var settings = AzureSettings.getCurrentState();
var params = new AzureCompletionRequestParams(
settings.getResourceName(),
settings.getDeploymentId(),
settings.getApiVersion());
var builder = new AzureClient.Builder(AzureCredentialsManager.getInstance().getSecret(), params)
var builder = new AzureClient
.Builder(AzureCredentialsManager.getInstance().getCredential(), params)
.setActiveDirectoryAuthentication(settings.isUseAzureActiveDirectoryAuthentication());
var baseHost = settings.getBaseHost();
if (baseHost != null) {
Expand Down Expand Up @@ -74,12 +74,12 @@ public static YouClient getYouClient() {
}

public static LlamaClient getLlamaClient() {
LlamaSettingsState llamaSettingsState = LlamaSettingsState.getInstance();
Builder builder = new Builder()
.setPort(llamaSettingsState.getServerPort());
if (!llamaSettingsState.isRunLocalServer()) {
builder.setHost(llamaSettingsState.getBaseHost());
String apiKey = LlamaCredentialsManager.getInstance().getApiKey();
var llamaSettings = LlamaSettings.getCurrentState();
var builder = new LlamaClient.Builder()
.setPort(llamaSettings.getServerPort());
if (!llamaSettings.isRunLocalServer()) {
builder.setHost(llamaSettings.getBaseHost());
String apiKey = LlamaCredentialManager.getInstance().getCredential();
if (apiKey != null && !apiKey.isBlank()) {
builder.setApiKey(apiKey);
}
Expand All @@ -89,7 +89,7 @@ public static LlamaClient getLlamaClient() {

private static OkHttpClient.Builder getDefaultClientBuilder() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
var advancedSettings = AdvancedSettingsState.getInstance();
var advancedSettings = AdvancedSettings.getCurrentState();
var proxyHost = advancedSettings.getProxyHost();
var proxyPort = advancedSettings.getProxyPort();
if (!proxyHost.isEmpty() && proxyPort != 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package ee.carlrobert.codegpt.completions;

import com.intellij.openapi.diagnostic.Logger;
import ee.carlrobert.codegpt.settings.state.SettingsState;
import ee.carlrobert.codegpt.settings.GeneralSettings;
import ee.carlrobert.codegpt.settings.GeneralSettingsState;
import ee.carlrobert.codegpt.telemetry.TelemetryAction;
import ee.carlrobert.llm.client.openai.completion.ErrorDetails;
import ee.carlrobert.llm.client.you.completion.YouCompletionEventListener;
Expand Down Expand Up @@ -42,7 +43,7 @@ public void cancel() {

private EventSource startCall(
CallParameters callParameters,
CompletionEventListener eventListener) {
CompletionEventListener<String> eventListener) {
try {
return CompletionRequestService.getInstance()
.getChatCompletionAsync(callParameters, useContextualSearch, eventListener);
Expand Down Expand Up @@ -71,7 +72,7 @@ public CompletionRequestWorker(CallParameters callParameters) {
}

protected Void doInBackground() {
var settings = SettingsState.getInstance();
var settings = GeneralSettings.getCurrentState();
try {
eventSource = startCall(callParameters, new YouRequestCompletionEventListener());
} catch (TotalUsageExceededException e) {
Expand Down Expand Up @@ -124,7 +125,7 @@ public void onError(ErrorDetails error, Throwable ex) {
}
}

private void sendInfo(SettingsState settings) {
private void sendInfo(GeneralSettingsState settings) {
TelemetryAction.COMPLETION.createActionMessage()
.property("conversationId", callParameters.getConversation().getId().toString())
.property("model", callParameters.getConversation().getModel())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
import ee.carlrobert.codegpt.conversations.Conversation;
import ee.carlrobert.codegpt.conversations.ConversationsState;
import ee.carlrobert.codegpt.conversations.message.Message;
import ee.carlrobert.codegpt.settings.GeneralSettings;
import ee.carlrobert.codegpt.settings.IncludedFilesSettings;
import ee.carlrobert.codegpt.settings.configuration.ConfigurationSettings;
import ee.carlrobert.codegpt.settings.service.ServiceType;
import ee.carlrobert.codegpt.settings.state.IncludedFilesSettingsState;
import ee.carlrobert.codegpt.settings.state.LlamaSettingsState;
import ee.carlrobert.codegpt.settings.state.OpenAISettingsState;
import ee.carlrobert.codegpt.settings.state.SettingsState;
import ee.carlrobert.codegpt.settings.state.YouSettingsState;
import ee.carlrobert.codegpt.settings.service.llama.LlamaSettings;
import ee.carlrobert.codegpt.settings.service.openai.OpenAISettings;
import ee.carlrobert.codegpt.settings.service.you.YouSettings;
import ee.carlrobert.codegpt.telemetry.core.configuration.TelemetryConfiguration;
import ee.carlrobert.codegpt.telemetry.core.service.UserId;
import ee.carlrobert.embedding.EmbeddingsService;
Expand Down Expand Up @@ -51,9 +51,6 @@ public class CompletionRequestProvider {
public static final String FIX_COMPILE_ERRORS_SYSTEM_PROMPT = getResourceContent(
"/prompts/fix-compile-errors.txt");

public static final String INLINE_COMPLETION_PROMPT = getResourceContent(
"/prompts/inline-completion-prompt.txt");

private final EncodingManager encodingManager = EncodingManager.getInstance();
private final EmbeddingsService embeddingsService;
private final Conversation conversation;
Expand All @@ -67,7 +64,7 @@ public CompletionRequestProvider(Conversation conversation) {

public static String getPromptWithContext(List<ReferencedFile> referencedFiles,
String userPrompt) {
var includedFilesSettings = IncludedFilesSettingsState.getInstance();
var includedFilesSettings = IncludedFilesSettings.getCurrentState();
var repeatableContext = referencedFiles.stream()
.map(item -> includedFilesSettings.getRepeatableContext()
.replace("{FILE_PATH}", item.getFilePath())
Expand All @@ -89,7 +86,7 @@ public static OpenAIChatCompletionRequest buildOpenAILookupCompletionRequest(
new OpenAIChatCompletionMessage("system",
getResourceContent("/prompts/method-name-generator.txt")),
new OpenAIChatCompletionMessage("user", context)))
.setModel(OpenAISettingsState.getInstance().getModel())
.setModel(OpenAISettings.getCurrentState().getModel())
.setStream(false)
.build();
}
Expand All @@ -104,7 +101,7 @@ public static LlamaCompletionRequest buildLlamaLookupCompletionRequest(String co
public LlamaCompletionRequest buildLlamaCompletionRequest(
Message message,
ConversationType conversationType) {
var settings = LlamaSettingsState.getInstance();
var settings = LlamaSettings.getCurrentState();
PromptTemplate promptTemplate;
if (settings.isRunLocalServer()) {
promptTemplate = settings.isUseCustomModel()
Expand Down Expand Up @@ -136,7 +133,7 @@ public LlamaCompletionRequest buildLlamaCompletionRequest(

public YouCompletionRequest buildYouCompletionRequest(Message message) {
var requestBuilder = new YouCompletionRequest.Builder(message.getPrompt())
.setUseGPT4Model(YouSettingsState.getInstance().isUseGPT4Model())
.setUseGPT4Model(YouSettings.getCurrentState().isUseGPT4Model())
.setChatHistory(conversation.getMessages().stream()
.map(prevMessage -> new YouCompletionRequestMessage(
prevMessage.getPrompt(),
Expand Down Expand Up @@ -207,7 +204,8 @@ private List<OpenAIChatCompletionMessage> buildMessages(
boolean useContextualSearch) {
var messages = buildMessages(callParameters, useContextualSearch);

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

Expand Down
Loading

0 comments on commit 9314509

Please sign in to comment.