fix: custom service request body value conversions

This commit is contained in:
Carl-Robert Linnupuu 2024-02-24 17:06:52 +02:00
parent eeda43b0e4
commit 88946343c5
2 changed files with 30 additions and 21 deletions

View file

@ -216,7 +216,12 @@ public class CompletionRequestProvider {
if (!streamRequest && "stream".equals(entry.getKey())) {
return false;
}
return processEntryValue(entry.getValue(), messages);
var value = entry.getValue();
if (value instanceof String && "$OPENAI_MESSAGES".equals(((String) value).trim())) {
return messages;
}
return value;
}
));
@ -312,23 +317,4 @@ public class CompletionRequestProvider {
return messages.stream().filter(Objects::nonNull).collect(toList());
}
private static Object processEntryValue(
Object value,
List<OpenAIChatCompletionMessage> messages) {
if (!(value instanceof String)) {
return value;
}
String stringValue = (String) value;
switch (stringValue.toLowerCase().trim()) {
case "$openai_messages":
return messages;
case "true":
case "false":
return Boolean.parseBoolean(stringValue);
default:
return value;
}
}
}

View file

@ -8,6 +8,7 @@ import com.intellij.ui.components.JBTabbedPane;
import com.intellij.ui.table.JBTable;
import com.intellij.util.ui.JBUI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.swing.JPanel;
@ -68,11 +69,33 @@ class CustomServiceFormTabbedPane extends JBTabbedPane {
var data = new HashMap<String, Object>();
for (int i = 0; i < model.getRowCount(); i++) {
var key = (String) model.getValueAt(i, 0);
data.put(key, model.getValueAt(i, 1));
data.put(key, parseValue(model.getValueAt(i, 1)));
}
return data;
}
private static Object parseValue(Object value) {
if (!(value instanceof String)) {
return value;
}
var stringValue = (String) value;
try {
return Integer.parseInt(stringValue);
} catch (NumberFormatException e) {
// ignore
}
try {
return Double.parseDouble(stringValue);
} catch (NumberFormatException e) {
// ignore
}
if (List.of("true", "false").contains(stringValue.toLowerCase().trim())) {
return Boolean.parseBoolean(stringValue);
}
return value;
}
public static Object[][] toArray(Map<?, ?> actionsMap) {
return actionsMap.entrySet()
.stream()