diff --git a/app/MindWork AI Studio/Settings/ConfigMeta.cs b/app/MindWork AI Studio/Settings/ConfigMeta.cs index e11d1df1..53247b6f 100644 --- a/app/MindWork AI Studio/Settings/ConfigMeta.cs +++ b/app/MindWork AI Studio/Settings/ConfigMeta.cs @@ -1,4 +1,5 @@ using System.Linq.Expressions; +using System.Text.Json; using AIStudio.Settings.DataModel; @@ -57,6 +58,31 @@ public record ConfigMeta : ConfigMetaBase /// public override bool RemovePluginContribution(Guid configPluginId) => this.pluginContributions.Remove(configPluginId); + /// + public override string SerializeCurrentValue() => ManagedConfiguration.SerializeManagedScalarValue(this.GetValue()); + + /// + protected override string SerializeCurrentValueAsJson() => JsonSerializer.Serialize(this.GetValue(), SettingsManager.JSON_OPTIONS); + + /// + protected override bool TrySetValueFromJson(string json) + { + try + { + var value = JsonSerializer.Deserialize(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; + } + } + /// protected override void Reset() { diff --git a/app/MindWork AI Studio/Settings/ConfigMetaBase.cs b/app/MindWork AI Studio/Settings/ConfigMetaBase.cs index 4f6549ad..c8dd0aff 100644 --- a/app/MindWork AI Studio/Settings/ConfigMetaBase.cs +++ b/app/MindWork AI Studio/Settings/ConfigMetaBase.cs @@ -12,6 +12,8 @@ public abstract record ConfigMetaBase(string SettingName) : IConfig { protected static SettingsManager SettingsManagerAccess => Program.SERVICE_PROVIDER.GetRequiredService(); + protected static ILogger Log => Program.LOGGER_FACTORY.CreateLogger(nameof(ConfigMetaBase)); + /// /// The persisted name of the configuration setting. /// @@ -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(); } /// @@ -144,6 +146,93 @@ public abstract record ConfigMetaBase(string SettingName) : IConfig /// True when that plugin had a contribution, otherwise false. public abstract bool RemovePluginContribution(Guid configPluginId); + /// + /// Indicates whether the value the user had chosen before a configuration plugin took over + /// this setting is still available. + /// + public bool HasUserValueSnapshot => SettingsManagerAccess.ConfigurationData.ManagedUserValueSnapshots.ContainsKey(this.SettingName); + + /// + /// Remembers the current value as the user's value, so that it can be restored once no + /// configuration plugin manages this setting anymore. + /// + /// + /// 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. + /// + 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(); + } + + /// + /// Restores the value the user had chosen before a configuration plugin took over this setting. + /// + /// + /// The snapshot is consumed either way: when it cannot be applied, keeping it would mean trying + /// the same broken value again on every start. + /// + /// True when a snapshot was available and could be applied, otherwise false. + private bool TryRestoreUserValueSnapshot() + { + var snapshots = SettingsManagerAccess.ConfigurationData.ManagedUserValueSnapshots; + if (!snapshots.Remove(this.SettingName, out var snapshot)) + return false; + + return this.TrySetValueFromJson(snapshot); + } + + /// + /// Drops the snapshot of the user's value without changing the current value. + /// + /// True when a snapshot was dropped, otherwise false. + public bool ClearUserValueSnapshot() => SettingsManagerAccess.ConfigurationData.ManagedUserValueSnapshots.Remove(this.SettingName); + + /// + /// Serializes the current value the same way the managed states record it. + /// + /// + /// 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. + /// + public abstract string SerializeCurrentValue(); + + /// + /// Restores the user's value, or falls back to the default value when no snapshot is available. + /// + /// + /// 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. + /// + private void RestoreUserValueOrDefault() + { + if (this.TryRestoreUserValueSnapshot()) + return; + + this.Reset(); + } + + /// + /// Serializes the current value as JSON, so that it can be restored without losing information. + /// + protected abstract string SerializeCurrentValueAsJson(); + + /// + /// Applies a value which was serialized by SerializeCurrentValueAsJson. + /// + /// The serialized value. + /// True when the value could be applied, otherwise false. + protected abstract bool TrySetValueFromJson(string json); + /// /// Resets the configuration property to its default value. /// diff --git a/app/MindWork AI Studio/Settings/ManagedConfiguration.Parsing.cs b/app/MindWork AI Studio/Settings/ManagedConfiguration.Parsing.cs index 7b33a546..0aadf8f6 100644 --- a/app/MindWork AI Studio/Settings/ManagedConfiguration.Parsing.cs +++ b/app/MindWork AI Studio/Settings/ManagedConfiguration.Parsing.cs @@ -1033,7 +1033,7 @@ public static partial class ManagedConfiguration return ManagedConfigurationMode.LOCKED; } - private static string SerializeManagedScalarValue(TValue value) => value switch + internal static string SerializeManagedScalarValue(TValue value) => value switch { null => string.Empty, string text => text, diff --git a/app/MindWork AI Studio/Settings/SettingsManager.cs b/app/MindWork AI Studio/Settings/SettingsManager.cs index 336d4f95..43d86255 100644 --- a/app/MindWork AI Studio/Settings/SettingsManager.cs +++ b/app/MindWork AI Studio/Settings/SettingsManager.cs @@ -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() },