mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2025-04-27 22:59:47 +00:00
Improved the developer experience by adding a tolerant enum converter (#398)
This commit is contained in:
parent
1ff27fe21f
commit
80c58ea749
@ -20,7 +20,7 @@ public sealed class SettingsManager(ILogger<SettingsManager> logger)
|
|||||||
private static readonly JsonSerializerOptions JSON_OPTIONS = new()
|
private static readonly JsonSerializerOptions JSON_OPTIONS = new()
|
||||||
{
|
{
|
||||||
WriteIndented = true,
|
WriteIndented = true,
|
||||||
Converters = { new JsonStringEnumConverter() },
|
Converters = { new TolerantEnumConverter() },
|
||||||
};
|
};
|
||||||
|
|
||||||
private readonly ILogger<SettingsManager> logger = logger;
|
private readonly ILogger<SettingsManager> logger = logger;
|
||||||
|
54
app/MindWork AI Studio/Settings/TolerantEnumConverter.cs
Normal file
54
app/MindWork AI Studio/Settings/TolerantEnumConverter.cs
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
|
namespace AIStudio.Settings;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tries to convert a JSON string to an enum value.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// 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.
|
||||||
|
/// </remarks>
|
||||||
|
public sealed class TolerantEnumConverter : JsonConverter<object>
|
||||||
|
{
|
||||||
|
private static readonly ILogger<TolerantEnumConverter> LOG = Program.LOGGER_FACTORY.CreateLogger<TolerantEnumConverter>();
|
||||||
|
|
||||||
|
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()!);
|
||||||
|
}
|
||||||
|
}
|
@ -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 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.
|
- 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 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.
|
- Fixed an issue where OpenAI `o3` models were not shown in the model selection.
|
||||||
|
Loading…
Reference in New Issue
Block a user