From 80c58ea7495864109a2d30b786c4196dee13fc94 Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Sat, 12 Apr 2025 10:08:36 +0200 Subject: [PATCH] Improved the developer experience by adding a tolerant enum converter (#398) --- .../Settings/SettingsManager.cs | 2 +- .../Settings/TolerantEnumConverter.cs | 54 +++++++++++++++++++ .../wwwroot/changelog/v0.9.40.md | 1 + 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 app/MindWork AI Studio/Settings/TolerantEnumConverter.cs diff --git a/app/MindWork AI Studio/Settings/SettingsManager.cs b/app/MindWork AI Studio/Settings/SettingsManager.cs index ec74cab8..dcbe48cb 100644 --- a/app/MindWork AI Studio/Settings/SettingsManager.cs +++ b/app/MindWork AI Studio/Settings/SettingsManager.cs @@ -20,7 +20,7 @@ public sealed class SettingsManager(ILogger logger) private static readonly JsonSerializerOptions JSON_OPTIONS = new() { WriteIndented = true, - Converters = { new JsonStringEnumConverter() }, + Converters = { new TolerantEnumConverter() }, }; private readonly ILogger logger = logger; diff --git a/app/MindWork AI Studio/Settings/TolerantEnumConverter.cs b/app/MindWork AI Studio/Settings/TolerantEnumConverter.cs new file mode 100644 index 00000000..c3f8fcd0 --- /dev/null +++ b/app/MindWork AI Studio/Settings/TolerantEnumConverter.cs @@ -0,0 +1,54 @@ +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace AIStudio.Settings; + +/// +/// Tries to convert a JSON string to an enum value. +/// +/// +/// When the target enum value does not exist, the value will be the default value. +/// This converter handles enum values as property names and values. +/// +public sealed class TolerantEnumConverter : JsonConverter +{ + private static readonly ILogger LOG = Program.LOGGER_FACTORY.CreateLogger(); + + public override bool CanConvert(Type typeToConvert) => typeToConvert.IsEnum; + + public override object? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + // Is this token a string? + if (reader.TokenType == JsonTokenType.String) + // Try to use that string as the name of the enum value: + if (Enum.TryParse(typeToConvert, reader.GetString(), out var result)) + return result; + + // In any other case, we will return the default enum value: + LOG.LogWarning($"Cannot read '{reader.GetString()}' as '{typeToConvert.Name}' enum; token type: {reader.TokenType}"); + return Activator.CreateInstance(typeToConvert); + } + + public override object ReadAsPropertyName(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + // Is this token a property name? + if (reader.TokenType == JsonTokenType.PropertyName) + // Try to use that property name as the name of the enum value: + if (Enum.TryParse(typeToConvert, reader.GetString(), out var result)) + return result; + + // In any other case, we will return the default enum value: + LOG.LogWarning($"Cannot read '{reader.GetString()}' as '{typeToConvert.Name}' enum; token type: {reader.TokenType}"); + return Activator.CreateInstance(typeToConvert)!; + } + + public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options) + { + writer.WriteStringValue(value.ToString()); + } + + public override void WriteAsPropertyName(Utf8JsonWriter writer, object value, JsonSerializerOptions options) + { + writer.WritePropertyName(value.ToString()!); + } +} \ No newline at end of file diff --git a/app/MindWork AI Studio/wwwroot/changelog/v0.9.40.md b/app/MindWork AI Studio/wwwroot/changelog/v0.9.40.md index c1bb8fc8..19cee1ed 100644 --- a/app/MindWork AI Studio/wwwroot/changelog/v0.9.40.md +++ b/app/MindWork AI Studio/wwwroot/changelog/v0.9.40.md @@ -2,4 +2,5 @@ - Added support for the announced OpenAI `o4` models. We hope that these `o4` models will be usable by the well-known chat completion API instead of the new responses API, though. AI Studio cannot use the new responses API right now. - Added Alibaba Cloud as a new provider. - Improved the provider selection by showing the name of the provider in the provider selection instead of its identifier. +- Improved the developer experience by adding a tolerant enum converter for better configuration handling. - Fixed an issue where OpenAI `o3` models were not shown in the model selection.