From d65e9c5786e596cd6afac7193955fcf3a1f2b93f Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Mon, 2 Dec 2024 19:52:41 +0100 Subject: [PATCH] Refactored secret handling --- .../Dialogs/ProviderDialog.razor.cs | 37 +++++++------------ .../Pages/Settings.razor.cs | 3 +- 2 files changed, 15 insertions(+), 25 deletions(-) diff --git a/app/MindWork AI Studio/Dialogs/ProviderDialog.razor.cs b/app/MindWork AI Studio/Dialogs/ProviderDialog.razor.cs index 80634f6c..55693522 100644 --- a/app/MindWork AI Studio/Dialogs/ProviderDialog.razor.cs +++ b/app/MindWork AI Studio/Dialogs/ProviderDialog.razor.cs @@ -11,7 +11,7 @@ namespace AIStudio.Dialogs; /// /// The provider settings dialog. /// -public partial class ProviderDialog : ComponentBase +public partial class ProviderDialog : ComponentBase, ISecretId { [CascadingParameter] private MudDialogInstance MudDialog { get; set; } = null!; @@ -144,23 +144,10 @@ public partial class ProviderDialog : ComponentBase return; } - var loadedProviderSettings = this.CreateProviderSettings(); - var provider = loadedProviderSettings.CreateProvider(this.Logger); - if(provider is NoProvider) - { - await base.OnInitializedAsync(); - return; - } - // Load the API key: - var requestedSecret = await this.RustService.GetAPIKey(provider, isTrying: this.DataLLMProvider is LLMProviders.SELF_HOSTED); - if(requestedSecret.Success) - { + var requestedSecret = await this.RustService.GetAPIKey(this, isTrying: this.DataLLMProvider is LLMProviders.SELF_HOSTED); + if (requestedSecret.Success) this.dataAPIKey = await requestedSecret.Secret.Decrypt(this.encryption); - - // Now, we try to load the list of available models: - await this.ReloadModels(); - } else { this.dataAPIKey = string.Empty; @@ -169,10 +156,9 @@ public partial class ProviderDialog : ComponentBase 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(); } - - // We still try to load the models. Some local hosts don't need an API key: - await this.ReloadModels(); } + + await this.ReloadModels(); } await base.OnInitializedAsync(); @@ -189,7 +175,15 @@ public partial class ProviderDialog : ComponentBase } #endregion + + #region Implementation of ISecretId + + public string SecretId => this.DataLLMProvider.ToName(); + public string SecretName => this.DataInstanceName; + + #endregion + private async Task Store() { await this.form.Validate(); @@ -205,11 +199,8 @@ public partial class ProviderDialog : ComponentBase var addedProviderSettings = this.CreateProviderSettings(); if (!string.IsNullOrWhiteSpace(this.dataAPIKey)) { - // We need to instantiate the provider to store the API key: - var provider = addedProviderSettings.CreateProvider(this.Logger); - // Store the API key in the OS secure storage: - var storeResponse = await this.RustService.SetAPIKey(provider, this.dataAPIKey); + var storeResponse = await this.RustService.SetAPIKey(this, 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."; diff --git a/app/MindWork AI Studio/Pages/Settings.razor.cs b/app/MindWork AI Studio/Pages/Settings.razor.cs index 8a2e1097..79bd79e7 100644 --- a/app/MindWork AI Studio/Pages/Settings.razor.cs +++ b/app/MindWork AI Studio/Pages/Settings.razor.cs @@ -114,8 +114,7 @@ public partial class Settings : ComponentBase, IMessageBusReceiver, IDisposable if (dialogResult is null || dialogResult.Canceled) return; - var providerInstance = provider.CreateProvider(this.Logger); - var deleteSecretResponse = await this.RustService.DeleteAPIKey(providerInstance); + var deleteSecretResponse = await this.RustService.DeleteAPIKey(provider); if(deleteSecretResponse.Success) { this.SettingsManager.ConfigurationData.Providers.Remove(provider);