mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-08-11 18:32:12 +00:00
Added snapshot primitives for restoring the user value of a managed setting
This commit is contained in:
parent
db970ce780
commit
c4471213fa
@ -1,4 +1,5 @@
|
||||
using System.Linq.Expressions;
|
||||
using System.Text.Json;
|
||||
|
||||
using AIStudio.Settings.DataModel;
|
||||
|
||||
@ -57,6 +58,31 @@ public record ConfigMeta<TClass, TValue> : ConfigMetaBase
|
||||
/// <inheritdoc/>
|
||||
public override bool RemovePluginContribution(Guid configPluginId) => this.pluginContributions.Remove(configPluginId);
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override string SerializeCurrentValue() => ManagedConfiguration.SerializeManagedScalarValue(this.GetValue());
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override string SerializeCurrentValueAsJson() => JsonSerializer.Serialize(this.GetValue(), SettingsManager.JSON_OPTIONS);
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override bool TrySetValueFromJson(string json)
|
||||
{
|
||||
try
|
||||
{
|
||||
var value = JsonSerializer.Deserialize<TValue>(json, SettingsManager.JSON_OPTIONS);
|
||||
if (value is null)
|
||||
return false;
|
||||
|
||||
this.SetValue(value);
|
||||
return true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.LogWarning(e, $"Was not able to restore the value of the setting '{this.SettingName}' from its snapshot '{json}'. Using the default value instead.");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override void Reset()
|
||||
{
|
||||
|
||||
@ -12,6 +12,8 @@ public abstract record ConfigMetaBase(string SettingName) : IConfig
|
||||
{
|
||||
protected static SettingsManager SettingsManagerAccess => Program.SERVICE_PROVIDER.GetRequiredService<SettingsManager>();
|
||||
|
||||
protected static ILogger Log => Program.LOGGER_FACTORY.CreateLogger(nameof(ConfigMetaBase));
|
||||
|
||||
/// <summary>
|
||||
/// The persisted name of the configuration setting.
|
||||
/// </summary>
|
||||
@ -89,14 +91,14 @@ public abstract record ConfigMetaBase(string SettingName) : IConfig
|
||||
public void ResetLockedConfiguration()
|
||||
{
|
||||
SettingsManagerAccess.ConfigurationData.ManagedLockedConfigurations.Remove(this.SettingName);
|
||||
|
||||
|
||||
this.IsLocked = false;
|
||||
this.LockedByConfigPluginId = Guid.Empty;
|
||||
|
||||
|
||||
if (this.ManagedMode is ManagedConfigurationMode.LOCKED)
|
||||
this.ManagedMode = null;
|
||||
|
||||
this.Reset();
|
||||
this.RestoreUserValueOrDefault();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -144,6 +146,93 @@ public abstract record ConfigMetaBase(string SettingName) : IConfig
|
||||
/// <returns>True when that plugin had a contribution, otherwise false.</returns>
|
||||
public abstract bool RemovePluginContribution(Guid configPluginId);
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether the value the user had chosen before a configuration plugin took over
|
||||
/// this setting is still available.
|
||||
/// </summary>
|
||||
public bool HasUserValueSnapshot => SettingsManagerAccess.ConfigurationData.ManagedUserValueSnapshots.ContainsKey(this.SettingName);
|
||||
|
||||
/// <summary>
|
||||
/// Remembers the current value as the user's value, so that it can be restored once no
|
||||
/// configuration plugin manages this setting anymore.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Only an unmanaged setting holds a value which belongs to the user. When one configuration
|
||||
/// plugin takes a setting over from another, the current value belongs to the previous plugin,
|
||||
/// so the snapshot of the user's value must survive that handover untouched.
|
||||
/// </remarks>
|
||||
public void CaptureUserValueSnapshot()
|
||||
{
|
||||
if (this.ManagedMode is not null)
|
||||
return;
|
||||
|
||||
var snapshots = SettingsManagerAccess.ConfigurationData.ManagedUserValueSnapshots;
|
||||
if (snapshots.ContainsKey(this.SettingName))
|
||||
return;
|
||||
|
||||
snapshots[this.SettingName] = this.SerializeCurrentValueAsJson();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Restores the value the user had chosen before a configuration plugin took over this setting.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The snapshot is consumed either way: when it cannot be applied, keeping it would mean trying
|
||||
/// the same broken value again on every start.
|
||||
/// </remarks>
|
||||
/// <returns>True when a snapshot was available and could be applied, otherwise false.</returns>
|
||||
private bool TryRestoreUserValueSnapshot()
|
||||
{
|
||||
var snapshots = SettingsManagerAccess.ConfigurationData.ManagedUserValueSnapshots;
|
||||
if (!snapshots.Remove(this.SettingName, out var snapshot))
|
||||
return false;
|
||||
|
||||
return this.TrySetValueFromJson(snapshot);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Drops the snapshot of the user's value without changing the current value.
|
||||
/// </summary>
|
||||
/// <returns>True when a snapshot was dropped, otherwise false.</returns>
|
||||
public bool ClearUserValueSnapshot() => SettingsManagerAccess.ConfigurationData.ManagedUserValueSnapshots.Remove(this.SettingName);
|
||||
|
||||
/// <summary>
|
||||
/// Serializes the current value the same way the managed states record it.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This is meant for comparisons, e.g. to tell whether the user has changed an editable default
|
||||
/// in the meantime. It is not meant for restoring a value: the representation is lossy.
|
||||
/// </remarks>
|
||||
public abstract string SerializeCurrentValue();
|
||||
|
||||
/// <summary>
|
||||
/// Restores the user's value, or falls back to the default value when no snapshot is available.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Settings which a configuration plugin managed before this app version has no snapshot, and
|
||||
/// neither has a setting whose value the user never changed. The default value is the best
|
||||
/// answer in both cases.
|
||||
/// </remarks>
|
||||
private void RestoreUserValueOrDefault()
|
||||
{
|
||||
if (this.TryRestoreUserValueSnapshot())
|
||||
return;
|
||||
|
||||
this.Reset();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Serializes the current value as JSON, so that it can be restored without losing information.
|
||||
/// </summary>
|
||||
protected abstract string SerializeCurrentValueAsJson();
|
||||
|
||||
/// <summary>
|
||||
/// Applies a value which was serialized by SerializeCurrentValueAsJson.
|
||||
/// </summary>
|
||||
/// <param name="json">The serialized value.</param>
|
||||
/// <returns>True when the value could be applied, otherwise false.</returns>
|
||||
protected abstract bool TrySetValueFromJson(string json);
|
||||
|
||||
/// <summary>
|
||||
/// Resets the configuration property to its default value.
|
||||
/// </summary>
|
||||
|
||||
@ -1033,7 +1033,7 @@ public static partial class ManagedConfiguration
|
||||
return ManagedConfigurationMode.LOCKED;
|
||||
}
|
||||
|
||||
private static string SerializeManagedScalarValue<TValue>(TValue value) => value switch
|
||||
internal static string SerializeManagedScalarValue<TValue>(TValue value) => value switch
|
||||
{
|
||||
null => string.Empty,
|
||||
string text => text,
|
||||
|
||||
@ -23,7 +23,7 @@ public sealed class SettingsManager
|
||||
|
||||
private readonly record struct CurrentSettingsReadResult(Data? SettingsData, SettingsWriteBlockReason FailureReason);
|
||||
|
||||
private static readonly JsonSerializerOptions JSON_OPTIONS = new()
|
||||
internal static readonly JsonSerializerOptions JSON_OPTIONS = new()
|
||||
{
|
||||
WriteIndented = true,
|
||||
Converters = { new TolerantEnumConverter() },
|
||||
|
||||
Loading…
Reference in New Issue
Block a user