mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-10-04 23:09:40 +00:00
Finished provider settings
This commit is contained in:
8 files changed
+293
-51
No files matched your search
@@ -7,23 +7,33 @@
|
||||
@* ReSharper disable once CSharpWarnings::CS8974 *@
|
||||
<MudTextField
|
||||
T="string"
|
||||
@bind-Text="@this.dataInstanceName"
|
||||
@bind-Text="@this.DataInstanceName"
|
||||
Label="Instance Name"
|
||||
Adornment="Adornment.Start"
|
||||
AdornmentIcon="@Icons.Material.Filled.Lightbulb"
|
||||
AdornmentColor="Color.Info"
|
||||
Required="@true"
|
||||
RequiredError="Please enter an instance name."
|
||||
Validation="@this.ValidatingInstanceName"
|
||||
/>
|
||||
|
||||
@* ReSharper disable once CSharpWarnings::CS8974 *@
|
||||
<MudSelect @bind-Value="@this.dataProvider" Label="Provider" OpenIcon="@Icons.Material.Filled.AccountBalance" AdornmentColor="Color.Info" Adornment="Adornment.Start" Validation="@this.ValidatingProvider">
|
||||
<MudSelect @bind-Value="@this.DataProvider" Label="Provider" OpenIcon="@Icons.Material.Filled.AccountBalance" AdornmentColor="Color.Info" Adornment="Adornment.Start" Validation="@this.ValidatingProvider">
|
||||
@foreach (Providers provider in Enum.GetValues(typeof(Providers)))
|
||||
{
|
||||
<MudSelectItem Value="@provider">@provider</MudSelectItem>
|
||||
}
|
||||
</MudSelect>
|
||||
|
||||
@* ReSharper disable once CSharpWarnings::CS8974 *@
|
||||
<MudTextField
|
||||
T="string"
|
||||
@bind-Text="@this.dataAPIKey"
|
||||
Label="API Key"
|
||||
Adornment="Adornment.Start"
|
||||
AdornmentIcon="@Icons.Material.Filled.VpnKey"
|
||||
AdornmentColor="Color.Info"
|
||||
InputType="InputType.Password"
|
||||
Validation="@this.ValidatingAPIKey"
|
||||
/>
|
||||
</MudForm>
|
||||
|
||||
@if (this.dataIssues.Any())
|
||||
@@ -42,7 +52,16 @@
|
||||
}
|
||||
</DialogContent>
|
||||
<DialogActions>
|
||||
<MudButton OnClick="@this.Cancel">Cancel</MudButton>
|
||||
<MudButton OnClick="@this.Add" Color="Color.Primary">Add</MudButton>
|
||||
<MudButton OnClick="@this.Cancel" Variant="Variant.Filled">Cancel</MudButton>
|
||||
<MudButton OnClick="@this.Store" Variant="Variant.Filled" Color="Color.Primary">
|
||||
@if(this.IsEditing)
|
||||
{
|
||||
@:Update
|
||||
}
|
||||
else
|
||||
{
|
||||
@:Add
|
||||
}
|
||||
</MudButton>
|
||||
</DialogActions>
|
||||
</MudDialog>
|
||||
@@ -3,6 +3,7 @@ using System.Text.RegularExpressions;
|
||||
using AIStudio.Provider;
|
||||
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using Microsoft.JSInterop;
|
||||
|
||||
using MudBlazor;
|
||||
|
||||
@@ -14,28 +15,98 @@ public partial class ProviderDialog : ComponentBase
|
||||
private MudDialogInstance MudDialog { get; set; } = null!;
|
||||
|
||||
[Parameter]
|
||||
public IList<string> UsedInstanceNames { get; set; } = new List<string>();
|
||||
public string DataId { get; set; } = Guid.NewGuid().ToString();
|
||||
|
||||
[Parameter]
|
||||
public string DataInstanceName { get; set; } = string.Empty;
|
||||
|
||||
[Parameter]
|
||||
public Providers DataProvider { get; set; } = Providers.NONE;
|
||||
|
||||
[Parameter]
|
||||
public bool IsEditing { get; init; }
|
||||
|
||||
[Inject]
|
||||
private SettingsManager SettingsManager { get; set; } = null!;
|
||||
|
||||
[Inject]
|
||||
private IJSRuntime JsRuntime { get; set; } = null!;
|
||||
|
||||
private List<string> usedInstanceNames { get; set; } = [];
|
||||
|
||||
private bool dataIsValid;
|
||||
private string[] dataIssues = [];
|
||||
private string dataInstanceName = string.Empty;
|
||||
private Providers dataProvider = Providers.NONE;
|
||||
private string dataAPIKey = string.Empty;
|
||||
private string dataAPIKeyStorageIssue = string.Empty;
|
||||
private string dataEditingPreviousInstanceName = string.Empty;
|
||||
|
||||
private MudForm form = null!;
|
||||
|
||||
private async Task Add()
|
||||
#region Overrides of ComponentBase
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
this.usedInstanceNames = this.SettingsManager.ConfigurationData.Providers.Select(x => x.InstanceName.ToLowerInvariant()).ToList();
|
||||
|
||||
if(this.IsEditing)
|
||||
{
|
||||
this.dataEditingPreviousInstanceName = this.DataInstanceName.ToLowerInvariant();
|
||||
var provider = this.DataProvider.CreateProvider();
|
||||
if(provider is NoProvider)
|
||||
return;
|
||||
|
||||
provider.InstanceName = this.DataInstanceName;
|
||||
|
||||
var requestedSecret = await this.SettingsManager.GetAPIKey(this.JsRuntime, provider);
|
||||
if(requestedSecret.Success)
|
||||
this.dataAPIKey = requestedSecret.Secret;
|
||||
else
|
||||
{
|
||||
this.dataAPIKeyStorageIssue = $"Failed to load the API key from the operating system. The message was: {requestedSecret.Issue}. You might ignore this message and provide the API key again.";
|
||||
await this.form.Validate();
|
||||
}
|
||||
}
|
||||
|
||||
await base.OnInitializedAsync();
|
||||
}
|
||||
|
||||
protected override async Task OnAfterRenderAsync(bool firstRender)
|
||||
{
|
||||
if(!this.IsEditing && firstRender)
|
||||
this.form.ResetValidation();
|
||||
|
||||
await base.OnAfterRenderAsync(firstRender);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private async Task Store()
|
||||
{
|
||||
await this.form.Validate();
|
||||
if (!string.IsNullOrWhiteSpace(this.dataAPIKeyStorageIssue))
|
||||
this.dataAPIKeyStorageIssue = string.Empty;
|
||||
|
||||
if (!this.dataIsValid)
|
||||
return;
|
||||
|
||||
var addedProvider = new Provider
|
||||
{
|
||||
Id = Guid.NewGuid().ToString(),
|
||||
InstanceName = this.dataInstanceName,
|
||||
UsedProvider = this.dataProvider,
|
||||
Id = this.DataId,
|
||||
InstanceName = this.DataInstanceName,
|
||||
UsedProvider = this.DataProvider,
|
||||
};
|
||||
|
||||
var provider = this.DataProvider.CreateProvider();
|
||||
provider.InstanceName = this.DataInstanceName;
|
||||
|
||||
var storeResponse = await this.SettingsManager.SetAPIKey(this.JsRuntime, provider, this.dataAPIKey);
|
||||
if (!storeResponse.Success)
|
||||
{
|
||||
this.dataAPIKeyStorageIssue = $"Failed to store the API key in the operating system. The message was: {storeResponse.Issue}. Please try again.";
|
||||
await this.form.Validate();
|
||||
return;
|
||||
}
|
||||
|
||||
this.MudDialog.Close(DialogResult.Ok(addedProvider));
|
||||
}
|
||||
|
||||
@@ -47,8 +118,14 @@ public partial class ProviderDialog : ComponentBase
|
||||
return null;
|
||||
}
|
||||
|
||||
[GeneratedRegex("^[a-zA-Z0-9 ]+$")]
|
||||
private static partial Regex InstanceNameRegex();
|
||||
|
||||
private string? ValidatingInstanceName(string instanceName)
|
||||
{
|
||||
if(string.IsNullOrWhiteSpace(instanceName))
|
||||
return "Please enter an instance name.";
|
||||
|
||||
if(instanceName.StartsWith(' '))
|
||||
return "The instance name must not start with a space.";
|
||||
|
||||
@@ -63,14 +140,23 @@ public partial class ProviderDialog : ComponentBase
|
||||
return "The instance name must not contain consecutive spaces.";
|
||||
|
||||
// The instance name must be unique:
|
||||
if (this.UsedInstanceNames.Contains(instanceName.ToLowerInvariant()))
|
||||
var lowerInstanceName = instanceName.ToLowerInvariant();
|
||||
if (lowerInstanceName != this.dataEditingPreviousInstanceName && this.usedInstanceNames.Contains(lowerInstanceName))
|
||||
return "The instance name must be unique; the chosen name is already in use.";
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private string? ValidatingAPIKey(string apiKey)
|
||||
{
|
||||
if(!string.IsNullOrWhiteSpace(this.dataAPIKeyStorageIssue))
|
||||
return this.dataAPIKeyStorageIssue;
|
||||
|
||||
if(string.IsNullOrWhiteSpace(apiKey))
|
||||
return "Please enter an API key.";
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private void Cancel() => this.MudDialog.Cancel();
|
||||
|
||||
[GeneratedRegex("^[a-zA-Z0-9 ]+$")]
|
||||
private static partial Regex InstanceNameRegex();
|
||||
}
|
||||
@@ -2,6 +2,8 @@ using System.Text.Json;
|
||||
using AIStudio.Provider;
|
||||
using Microsoft.JSInterop;
|
||||
|
||||
// ReSharper disable NotAccessedPositionalProperty.Local
|
||||
|
||||
namespace AIStudio.Settings;
|
||||
|
||||
public sealed class SettingsManager
|
||||
@@ -11,37 +13,60 @@ public sealed class SettingsManager
|
||||
public static string? ConfigDirectory { get; set; }
|
||||
|
||||
public static string? DataDirectory { get; set; }
|
||||
|
||||
public bool IsSetUp => !string.IsNullOrWhiteSpace(ConfigDirectory) && !string.IsNullOrWhiteSpace(DataDirectory);
|
||||
|
||||
private readonly record struct GetSecretRequest(string Destination, string UserName);
|
||||
public Data ConfigurationData { get; private set; } = new();
|
||||
|
||||
private bool IsSetUp => !string.IsNullOrWhiteSpace(ConfigDirectory) && !string.IsNullOrWhiteSpace(DataDirectory);
|
||||
|
||||
#region API Key Handling
|
||||
|
||||
public async Task<string> GetAPIKey(IJSRuntime jsRuntime, IProvider provider) => await jsRuntime.InvokeAsync<string>("window.__TAURI__.invoke", "get_secret", new GetSecretRequest($"provider::{provider.Id}::{provider.InstanceName}::api_key", Environment.UserName));
|
||||
private readonly record struct GetSecretRequest(string Destination, string UserName);
|
||||
|
||||
public readonly record struct RequestedSecret(bool Success, string Secret, string Issue);
|
||||
|
||||
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);
|
||||
|
||||
public async Task SetAPIKey(IJSRuntime jsRuntime, IProvider provider, string key) => await jsRuntime.InvokeVoidAsync("window.__TAURI__.invoke", "store_secret", new StoreSecretRequest($"provider::{provider.Id}::{provider.InstanceName}::api_key", Environment.UserName, key));
|
||||
|
||||
public async Task<Data> LoadSettings()
|
||||
{
|
||||
if(!this.IsSetUp)
|
||||
return new Data();
|
||||
|
||||
var settingsPath = Path.Combine(ConfigDirectory!, SETTINGS_FILENAME);
|
||||
if(!File.Exists(settingsPath))
|
||||
return new Data();
|
||||
|
||||
var settingsJson = await File.ReadAllTextAsync(settingsPath);
|
||||
return JsonSerializer.Deserialize<Data>(settingsJson) ?? new Data();
|
||||
}
|
||||
public readonly record struct StoreSecretResponse(bool Success, string Issue);
|
||||
|
||||
public async Task StoreSettings(Data data)
|
||||
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);
|
||||
|
||||
public readonly record struct DeleteSecretResponse(bool Success, string Issue);
|
||||
|
||||
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
|
||||
|
||||
public async Task LoadSettings()
|
||||
{
|
||||
if(!this.IsSetUp)
|
||||
return;
|
||||
|
||||
var settingsPath = Path.Combine(ConfigDirectory!, SETTINGS_FILENAME);
|
||||
var settingsJson = JsonSerializer.Serialize(data);
|
||||
if(!File.Exists(settingsPath))
|
||||
return;
|
||||
|
||||
var settingsJson = await File.ReadAllTextAsync(settingsPath);
|
||||
var loadedConfiguration = JsonSerializer.Deserialize<Data>(settingsJson);
|
||||
if(loadedConfiguration is null)
|
||||
return;
|
||||
|
||||
this.ConfigurationData = loadedConfiguration;
|
||||
}
|
||||
|
||||
public async Task StoreSettings()
|
||||
{
|
||||
if(!this.IsSetUp)
|
||||
return;
|
||||
|
||||
var settingsPath = Path.Combine(ConfigDirectory!, SETTINGS_FILENAME);
|
||||
if(!Directory.Exists(ConfigDirectory))
|
||||
Directory.CreateDirectory(ConfigDirectory!);
|
||||
|
||||
var settingsJson = JsonSerializer.Serialize(this.ConfigurationData);
|
||||
await File.WriteAllTextAsync(settingsPath, settingsJson);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user