Implemented settings v2 as preparation for self-hosted providers

This commit is contained in:
Thorsten Sommer 2024-07-02 19:58:43 +02:00
parent 6cc1d37db8
commit 29face9f1f
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
5 changed files with 49 additions and 3 deletions

View File

@ -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.
/// </summary>
public Version Version { get; init; } = Version.V1;
public Version Version { get; init; } = Version.V2;
/// <summary>
/// List of configured providers.

View File

@ -9,8 +9,10 @@ namespace AIStudio.Settings;
/// <param name="Id">The provider's ID.</param>
/// <param name="InstanceName">The provider's instance name. Useful for multiple instances of the same provider, e.g., to distinguish between different OpenAI API keys.</param>
/// <param name="UsedProvider">The provider used.</param>
/// <param name="IsSelfHosted">Whether the provider is self-hosted.</param>
/// <param name="Hostname">The hostname of the provider. Useful for self-hosted providers.</param>
/// <param name="Model">The LLM model to use for chat.</param>
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,
/// <returns>A string that represents the current provider in a human-readable format.</returns>
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})";
}

View File

@ -103,7 +103,7 @@ public sealed class SettingsManager
if(loadedConfiguration is null)
return;
this.ConfigurationData = loadedConfiguration;
this.ConfigurationData = SettingsMigrations.Migrate(loadedConfiguration);
}
/// <summary>

View File

@ -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,
};
}
}

View File

@ -7,5 +7,7 @@ namespace AIStudio.Settings;
public enum Version
{
UNKNOWN,
V1,
V2,
}