From 29face9f1f078443ee69921ede35a3f3c73af2a1 Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Tue, 2 Jul 2024 19:58:43 +0200 Subject: [PATCH] Implemented settings v2 as preparation for self-hosted providers --- app/MindWork AI Studio/Settings/Data.cs | 2 +- app/MindWork AI Studio/Settings/Provider.cs | 7 +++- .../Settings/SettingsManager.cs | 2 +- .../Settings/SettingsMigrations.cs | 39 +++++++++++++++++++ app/MindWork AI Studio/Settings/Version.cs | 2 + 5 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 app/MindWork AI Studio/Settings/SettingsMigrations.cs diff --git a/app/MindWork AI Studio/Settings/Data.cs b/app/MindWork AI Studio/Settings/Data.cs index 8052c271..8ce5120b 100644 --- a/app/MindWork AI Studio/Settings/Data.cs +++ b/app/MindWork AI Studio/Settings/Data.cs @@ -9,7 +9,7 @@ public sealed class Data /// The version of the settings file. Allows us to upgrade the settings /// when a new version is available. /// - public Version Version { get; init; } = Version.V1; + public Version Version { get; init; } = Version.V2; /// /// List of configured providers. diff --git a/app/MindWork AI Studio/Settings/Provider.cs b/app/MindWork AI Studio/Settings/Provider.cs index d1f6194a..48133267 100644 --- a/app/MindWork AI Studio/Settings/Provider.cs +++ b/app/MindWork AI Studio/Settings/Provider.cs @@ -9,8 +9,10 @@ namespace AIStudio.Settings; /// The provider's ID. /// The provider's instance name. Useful for multiple instances of the same provider, e.g., to distinguish between different OpenAI API keys. /// The provider used. +/// Whether the provider is self-hosted. +/// The hostname of the provider. Useful for self-hosted providers. /// The LLM model to use for chat. -public readonly record struct Provider(uint Num, string Id, string InstanceName, Providers UsedProvider, Model Model) +public readonly record struct Provider(uint Num, string Id, string InstanceName, Providers UsedProvider, Model Model, bool IsSelfHosted = false, string Hostname = "http://localhost:1234") { #region Overrides of ValueType @@ -21,6 +23,9 @@ public readonly record struct Provider(uint Num, string Id, string InstanceName, /// A string that represents the current provider in a human-readable format. public override string ToString() { + if(this.IsSelfHosted) + return $"{this.InstanceName} ({this.UsedProvider.ToName()}, {this.Hostname}, {this.Model})"; + return $"{this.InstanceName} ({this.UsedProvider.ToName()}, {this.Model})"; } diff --git a/app/MindWork AI Studio/Settings/SettingsManager.cs b/app/MindWork AI Studio/Settings/SettingsManager.cs index 4353056e..4a5d2991 100644 --- a/app/MindWork AI Studio/Settings/SettingsManager.cs +++ b/app/MindWork AI Studio/Settings/SettingsManager.cs @@ -103,7 +103,7 @@ public sealed class SettingsManager if(loadedConfiguration is null) return; - this.ConfigurationData = loadedConfiguration; + this.ConfigurationData = SettingsMigrations.Migrate(loadedConfiguration); } /// diff --git a/app/MindWork AI Studio/Settings/SettingsMigrations.cs b/app/MindWork AI Studio/Settings/SettingsMigrations.cs new file mode 100644 index 00000000..e5783787 --- /dev/null +++ b/app/MindWork AI Studio/Settings/SettingsMigrations.cs @@ -0,0 +1,39 @@ +namespace AIStudio.Settings; + +public static class SettingsMigrations +{ + public static Data Migrate(Data previousData) + { + switch (previousData.Version) + { + case Version.V1: + return MigrateFromV1(previousData); + + default: + Console.WriteLine("No migration needed."); + return previousData; + } + } + + private static Data MigrateFromV1(Data previousData) + { + // + // Summary: + // In v1 we had no self-hosted providers. Thus, we had no hostnames. + // + + Console.WriteLine("Migrating from v1 to v2..."); + return new() + { + Version = Version.V2, + + Providers = previousData.Providers.Select(provider => provider with { IsSelfHosted = false, Hostname = "" }).ToList(), + + EnableSpellchecking = previousData.EnableSpellchecking, + IsSavingEnergy = previousData.IsSavingEnergy, + NextProviderNum = previousData.NextProviderNum, + ShortcutSendBehavior = previousData.ShortcutSendBehavior, + UpdateBehavior = previousData.UpdateBehavior, + }; + } +} \ No newline at end of file diff --git a/app/MindWork AI Studio/Settings/Version.cs b/app/MindWork AI Studio/Settings/Version.cs index 047289ea..04e54efd 100644 --- a/app/MindWork AI Studio/Settings/Version.cs +++ b/app/MindWork AI Studio/Settings/Version.cs @@ -7,5 +7,7 @@ namespace AIStudio.Settings; public enum Version { UNKNOWN, + V1, + V2, } \ No newline at end of file