diff --git a/src/main/java/ee/carlrobert/codegpt/completions/CompletionRequestProvider.java b/src/main/java/ee/carlrobert/codegpt/completions/CompletionRequestProvider.java index eb92cdb8..8fa39ee9 100644 --- a/src/main/java/ee/carlrobert/codegpt/completions/CompletionRequestProvider.java +++ b/src/main/java/ee/carlrobert/codegpt/completions/CompletionRequestProvider.java @@ -21,7 +21,7 @@ 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.service.custom.CustomServiceState; +import ee.carlrobert.codegpt.settings.service.custom.CustomServiceSettingsState; import ee.carlrobert.codegpt.settings.service.llama.LlamaSettings; import ee.carlrobert.codegpt.settings.service.openai.OpenAISettings; import ee.carlrobert.codegpt.settings.service.you.YouSettings; @@ -258,7 +258,7 @@ public class CompletionRequestProvider { } public Request buildCustomOpenAIChatCompletionRequest( - CustomServiceState customConfiguration, + CustomServiceSettingsState customConfiguration, CallParameters callParameters) { var requestBuilder = new Request.Builder().url(customConfiguration.getUrl().trim()); for (var entry : customConfiguration.getHeaders().entrySet()) { diff --git a/src/main/java/ee/carlrobert/codegpt/conversations/converter/ConversationConverter.java b/src/main/java/ee/carlrobert/codegpt/conversations/converter/ConversationConverter.java index 41ccc9fe..c1849b70 100644 --- a/src/main/java/ee/carlrobert/codegpt/conversations/converter/ConversationConverter.java +++ b/src/main/java/ee/carlrobert/codegpt/conversations/converter/ConversationConverter.java @@ -1,10 +1,12 @@ package ee.carlrobert.codegpt.conversations.converter; +import com.fasterxml.jackson.core.type.TypeReference; import ee.carlrobert.codegpt.conversations.Conversation; +import ee.carlrobert.codegpt.util.BaseConverter; public class ConversationConverter extends BaseConverter { public ConversationConverter() { - super(Conversation.class); + super(new TypeReference<>() {}); } } diff --git a/src/main/java/ee/carlrobert/codegpt/conversations/converter/ConversationsConverter.java b/src/main/java/ee/carlrobert/codegpt/conversations/converter/ConversationsConverter.java index 38ad2618..c3c0b3de 100644 --- a/src/main/java/ee/carlrobert/codegpt/conversations/converter/ConversationsConverter.java +++ b/src/main/java/ee/carlrobert/codegpt/conversations/converter/ConversationsConverter.java @@ -1,10 +1,12 @@ package ee.carlrobert.codegpt.conversations.converter; +import com.fasterxml.jackson.core.type.TypeReference; import ee.carlrobert.codegpt.conversations.ConversationsContainer; +import ee.carlrobert.codegpt.util.BaseConverter; public class ConversationsConverter extends BaseConverter { public ConversationsConverter() { - super(ConversationsContainer.class); + super(new TypeReference<>() {}); } } diff --git a/src/main/java/ee/carlrobert/codegpt/settings/service/custom/CustomServiceForm.java b/src/main/java/ee/carlrobert/codegpt/settings/service/custom/CustomServiceForm.java index 7070f5f2..63b9edc4 100644 --- a/src/main/java/ee/carlrobert/codegpt/settings/service/custom/CustomServiceForm.java +++ b/src/main/java/ee/carlrobert/codegpt/settings/service/custom/CustomServiceForm.java @@ -43,7 +43,7 @@ public class CustomServiceForm { private final JBLabel templateHelpText; private final ComboBox templateComboBox; - public CustomServiceForm(CustomServiceState settings) { + public CustomServiceForm(CustomServiceSettingsState settings) { apiKeyField = new JBPasswordField(); apiKeyField.setColumns(30); apiKeyField.setText(CustomServiceCredentialManager.getInstance().getCredential()); @@ -104,8 +104,8 @@ public class CustomServiceForm { return apiKey.isEmpty() ? null : apiKey; } - public CustomServiceState getCurrentState() { - var state = new CustomServiceState(); + public CustomServiceSettingsState getCurrentState() { + var state = new CustomServiceSettingsState(); state.setUrl(urlField.getText()); state.setTemplate(templateComboBox.getItem()); state.setHeaders(tabbedPane.getHeaders()); @@ -136,7 +136,7 @@ public class CustomServiceForm { } } - private void testConnection(CustomServiceState customConfiguration) { + private void testConnection(CustomServiceSettingsState customConfiguration) { var conversation = new Conversation(); var request = new CompletionRequestProvider(conversation) .buildCustomOpenAIChatCompletionRequest( diff --git a/src/main/java/ee/carlrobert/codegpt/settings/service/custom/CustomServiceFormTabbedPane.java b/src/main/java/ee/carlrobert/codegpt/settings/service/custom/CustomServiceFormTabbedPane.java index 0d7e5d13..d9d46bfa 100644 --- a/src/main/java/ee/carlrobert/codegpt/settings/service/custom/CustomServiceFormTabbedPane.java +++ b/src/main/java/ee/carlrobert/codegpt/settings/service/custom/CustomServiceFormTabbedPane.java @@ -18,7 +18,7 @@ class CustomServiceFormTabbedPane extends JBTabbedPane { private final JBTable headersTable; private final JBTable bodyTable; - CustomServiceFormTabbedPane(CustomServiceState customConfiguration) { + CustomServiceFormTabbedPane(CustomServiceSettingsState customConfiguration) { headersTable = new JBTable( new DefaultTableModel(toArray(customConfiguration.getHeaders()), new Object[]{"Key", "Value"})); diff --git a/src/main/java/ee/carlrobert/codegpt/settings/service/custom/CustomServiceSettings.java b/src/main/java/ee/carlrobert/codegpt/settings/service/custom/CustomServiceSettings.java index 0a60825f..9c5e8b7e 100644 --- a/src/main/java/ee/carlrobert/codegpt/settings/service/custom/CustomServiceSettings.java +++ b/src/main/java/ee/carlrobert/codegpt/settings/service/custom/CustomServiceSettings.java @@ -11,22 +11,22 @@ import org.jetbrains.annotations.NotNull; @State( name = "CodeGPT_CustomServiceSettings", storages = @Storage("CodeGPT_CustomServiceSettings.xml")) -public class CustomServiceSettings implements PersistentStateComponent { +public class CustomServiceSettings implements PersistentStateComponent { - private CustomServiceState state = new CustomServiceState(); + private CustomServiceSettingsState state = new CustomServiceSettingsState(); @Override @NotNull - public CustomServiceState getState() { + public CustomServiceSettingsState getState() { return state; } @Override - public void loadState(@NotNull CustomServiceState state) { + public void loadState(@NotNull CustomServiceSettingsState state) { this.state = state; } - public static CustomServiceState getCurrentState() { + public static CustomServiceSettingsState getCurrentState() { return getInstance().getState(); } diff --git a/src/main/java/ee/carlrobert/codegpt/settings/service/custom/CustomServiceState.java b/src/main/java/ee/carlrobert/codegpt/settings/service/custom/CustomServiceSettingsState.java similarity index 84% rename from src/main/java/ee/carlrobert/codegpt/settings/service/custom/CustomServiceState.java rename to src/main/java/ee/carlrobert/codegpt/settings/service/custom/CustomServiceSettingsState.java index 393a21d1..cc93872e 100644 --- a/src/main/java/ee/carlrobert/codegpt/settings/service/custom/CustomServiceState.java +++ b/src/main/java/ee/carlrobert/codegpt/settings/service/custom/CustomServiceSettingsState.java @@ -2,13 +2,16 @@ package ee.carlrobert.codegpt.settings.service.custom; import static ee.carlrobert.codegpt.settings.service.custom.CustomServiceTemplate.OPENAI; +import com.intellij.util.xmlb.annotations.OptionTag; +import ee.carlrobert.codegpt.util.MapConverter; import java.util.Map; import java.util.Objects; -public class CustomServiceState { +public class CustomServiceSettingsState { private String url = OPENAI.getUrl(); private Map headers = OPENAI.getHeaders(); + @OptionTag(converter = MapConverter.class) private Map body = OPENAI.getBody(); private CustomServiceTemplate template = OPENAI; @@ -52,7 +55,7 @@ public class CustomServiceState { if (o == null || getClass() != o.getClass()) { return false; } - CustomServiceState that = (CustomServiceState) o; + CustomServiceSettingsState that = (CustomServiceSettingsState) o; return Objects.equals(url, that.url) && Objects.equals(headers, that.headers) && Objects.equals(body, that.body) diff --git a/src/main/java/ee/carlrobert/codegpt/conversations/converter/BaseConverter.java b/src/main/java/ee/carlrobert/codegpt/util/BaseConverter.java similarity index 72% rename from src/main/java/ee/carlrobert/codegpt/conversations/converter/BaseConverter.java rename to src/main/java/ee/carlrobert/codegpt/util/BaseConverter.java index b2fceb35..0a4f2950 100644 --- a/src/main/java/ee/carlrobert/codegpt/conversations/converter/BaseConverter.java +++ b/src/main/java/ee/carlrobert/codegpt/util/BaseConverter.java @@ -1,6 +1,7 @@ -package ee.carlrobert.codegpt.conversations.converter; +package ee.carlrobert.codegpt.util; import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; @@ -8,20 +9,20 @@ import com.intellij.util.xmlb.Converter; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -abstract class BaseConverter extends Converter { +public abstract class BaseConverter extends Converter { - private final Class clazz; + private final TypeReference typeReference; private final ObjectMapper objectMapper = new ObjectMapper() .registerModule(new Jdk8Module()) .registerModule(new JavaTimeModule()); - BaseConverter(Class clazz) { - this.clazz = clazz; + public BaseConverter(TypeReference typeReference) { + this.typeReference = typeReference; } public @Nullable T fromString(@NotNull String value) { try { - return objectMapper.readValue(value, clazz); + return objectMapper.readValue(value, typeReference); } catch (JsonProcessingException e) { throw new RuntimeException("Unable to deserialize conversations", e); } diff --git a/src/main/java/ee/carlrobert/codegpt/util/MapConverter.java b/src/main/java/ee/carlrobert/codegpt/util/MapConverter.java new file mode 100644 index 00000000..e7de065d --- /dev/null +++ b/src/main/java/ee/carlrobert/codegpt/util/MapConverter.java @@ -0,0 +1,11 @@ +package ee.carlrobert.codegpt.util; + +import com.fasterxml.jackson.core.type.TypeReference; +import java.util.Map; + +public class MapConverter extends BaseConverter> { + + public MapConverter() { + super(new TypeReference<>() {}); + } +}