mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-10-14 07:33:48 +00:00
Added documentation
This commit is contained in:
11 files changed
+162
-3
No files matched your search
@@ -6,14 +6,26 @@ using Microsoft.JSInterop;
|
||||
|
||||
namespace AIStudio.Settings;
|
||||
|
||||
/// <summary>
|
||||
/// The settings manager.
|
||||
/// </summary>
|
||||
public sealed class SettingsManager
|
||||
{
|
||||
private const string SETTINGS_FILENAME = "settings.json";
|
||||
|
||||
/// <summary>
|
||||
/// The directory where the configuration files are stored.
|
||||
/// </summary>
|
||||
public static string? ConfigDirectory { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The directory where the data files are stored.
|
||||
/// </summary>
|
||||
public static string? DataDirectory { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The configuration data.
|
||||
/// </summary>
|
||||
public Data ConfigurationData { get; private set; } = new();
|
||||
|
||||
private bool IsSetUp => !string.IsNullOrWhiteSpace(ConfigDirectory) && !string.IsNullOrWhiteSpace(DataDirectory);
|
||||
@@ -22,24 +34,62 @@ public sealed class SettingsManager
|
||||
|
||||
private readonly record struct GetSecretRequest(string Destination, string UserName);
|
||||
|
||||
/// <summary>
|
||||
/// Data structure for any requested secret.
|
||||
/// </summary>
|
||||
/// <param name="Success">True, when the secret was successfully retrieved.</param>
|
||||
/// <param name="Secret">The secret, e.g., API key.</param>
|
||||
/// <param name="Issue">The issue, when the secret could not be retrieved.</param>
|
||||
public readonly record struct RequestedSecret(bool Success, string Secret, string Issue);
|
||||
|
||||
/// <summary>
|
||||
/// Try to get the API key for the given provider.
|
||||
/// </summary>
|
||||
/// <param name="jsRuntime">The JS runtime to access the Rust code.</param>
|
||||
/// <param name="provider">The provider to get the API key for.</param>
|
||||
/// <returns>The requested secret.</returns>
|
||||
public async Task<RequestedSecret> GetAPIKey(IJSRuntime jsRuntime, IProvider provider) => await jsRuntime.InvokeAsync<RequestedSecret>("window.__TAURI__.invoke", "get_secret", new GetSecretRequest($"provider::{provider.Id}::{provider.InstanceName}::api_key", Environment.UserName));
|
||||
|
||||
private readonly record struct StoreSecretRequest(string Destination, string UserName, string Secret);
|
||||
|
||||
/// <summary>
|
||||
/// Data structure for storing a secret response.
|
||||
/// </summary>
|
||||
/// <param name="Success">True, when the secret was successfully stored.</param>
|
||||
/// <param name="Issue">The issue, when the secret could not be stored.</param>
|
||||
public readonly record struct StoreSecretResponse(bool Success, string Issue);
|
||||
|
||||
/// <summary>
|
||||
/// Try to store the API key for the given provider.
|
||||
/// </summary>
|
||||
/// <param name="jsRuntime">The JS runtime to access the Rust code.</param>
|
||||
/// <param name="provider">The provider to store the API key for.</param>
|
||||
/// <param name="key">The API key to store.</param>
|
||||
/// <returns>The store secret response.</returns>
|
||||
public async Task<StoreSecretResponse> SetAPIKey(IJSRuntime jsRuntime, IProvider provider, string key) => await jsRuntime.InvokeAsync<StoreSecretResponse>("window.__TAURI__.invoke", "store_secret", new StoreSecretRequest($"provider::{provider.Id}::{provider.InstanceName}::api_key", Environment.UserName, key));
|
||||
|
||||
private readonly record struct DeleteSecretRequest(string Destination, string UserName);
|
||||
|
||||
/// <summary>
|
||||
/// Data structure for deleting a secret response.
|
||||
/// </summary>
|
||||
/// <param name="Success">True, when the secret was successfully deleted.</param>
|
||||
/// <param name="Issue">The issue, when the secret could not be deleted.</param>
|
||||
public readonly record struct DeleteSecretResponse(bool Success, string Issue);
|
||||
|
||||
/// <summary>
|
||||
/// Tries to delete the API key for the given provider.
|
||||
/// </summary>
|
||||
/// <param name="jsRuntime">The JS runtime to access the Rust code.</param>
|
||||
/// <param name="provider">The provider to delete the API key for.</param>
|
||||
/// <returns>The delete secret response.</returns>
|
||||
public async Task<DeleteSecretResponse> DeleteAPIKey(IJSRuntime jsRuntime, IProvider provider) => await jsRuntime.InvokeAsync<DeleteSecretResponse>("window.__TAURI__.invoke", "delete_secret", new DeleteSecretRequest($"provider::{provider.Id}::{provider.InstanceName}::api_key", Environment.UserName));
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Loads the settings from the file system.
|
||||
/// </summary>
|
||||
public async Task LoadSettings()
|
||||
{
|
||||
if(!this.IsSetUp)
|
||||
@@ -57,6 +107,9 @@ public sealed class SettingsManager
|
||||
this.ConfigurationData = loadedConfiguration;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stores the settings to the file system.
|
||||
/// </summary>
|
||||
public async Task StoreSettings()
|
||||
{
|
||||
if(!this.IsSetUp)
|
||||
|
||||
Reference in new issue
Block a user