fix: custom service request body serialization

This commit is contained in:
Carl-Robert Linnupuu 2024-02-24 01:12:21 +02:00
parent fbf43393cb
commit 557f9b0ca0
9 changed files with 41 additions and 22 deletions

View file

@ -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()) {

View file

@ -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<Conversation> {
public ConversationConverter() {
super(Conversation.class);
super(new TypeReference<>() {});
}
}

View file

@ -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<ConversationsContainer> {
public ConversationsConverter() {
super(ConversationsContainer.class);
super(new TypeReference<>() {});
}
}

View file

@ -43,7 +43,7 @@ public class CustomServiceForm {
private final JBLabel templateHelpText;
private final ComboBox<CustomServiceTemplate> 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(

View file

@ -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"}));

View file

@ -11,22 +11,22 @@ import org.jetbrains.annotations.NotNull;
@State(
name = "CodeGPT_CustomServiceSettings",
storages = @Storage("CodeGPT_CustomServiceSettings.xml"))
public class CustomServiceSettings implements PersistentStateComponent<CustomServiceState> {
public class CustomServiceSettings implements PersistentStateComponent<CustomServiceSettingsState> {
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();
}

View file

@ -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<String, String> headers = OPENAI.getHeaders();
@OptionTag(converter = MapConverter.class)
private Map<String, ?> 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)

View file

@ -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<T> extends Converter<T> {
public abstract class BaseConverter<T> extends Converter<T> {
private final Class<T> clazz;
private final TypeReference<T> typeReference;
private final ObjectMapper objectMapper = new ObjectMapper()
.registerModule(new Jdk8Module())
.registerModule(new JavaTimeModule());
BaseConverter(Class<T> clazz) {
this.clazz = clazz;
public BaseConverter(TypeReference<T> 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);
}

View file

@ -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<Map<String, Object>> {
public MapConverter() {
super(new TypeReference<>() {});
}
}