Migrated the get secret calls from Tauri JS to the runtime API

This commit is contained in:
tsommer committed 2024-08-28 21:00:19 +02:00
1 parent 273376ad97
commit 97854e7eaa
53 files changed
+299 -243

No files matched your search

@@ -5,7 +5,6 @@ using AIStudio.Assistants.RewriteImprove;
using AIStudio.Assistants.TextSummarizer;
using AIStudio.Assistants.EMail;
using AIStudio.Settings.DataModel;
using AIStudio.Tools;
using WritingStylesRewrite = AIStudio.Assistants.RewriteImprove.WritingStyles;
using WritingStylesEMail = AIStudio.Assistants.EMail.WritingStyles;
@@ -1,5 +1,4 @@
using AIStudio.Assistants.Agenda;
using AIStudio.Tools;
namespace AIStudio.Settings.DataModel;
@@ -1,5 +1,4 @@
using AIStudio.Assistants.EMail;
using AIStudio.Tools;
namespace AIStudio.Settings.DataModel;
@@ -1,5 +1,3 @@
using AIStudio.Tools;
namespace AIStudio.Settings.DataModel;
public sealed class DataGrammarSpelling
@@ -1,5 +1,4 @@
using AIStudio.Assistants.RewriteImprove;
using AIStudio.Tools;
namespace AIStudio.Settings.DataModel;
@@ -1,5 +1,4 @@
using AIStudio.Assistants.TextSummarizer;
using AIStudio.Tools;
namespace AIStudio.Settings.DataModel;
@@ -1,5 +1,3 @@
using AIStudio.Tools;
namespace AIStudio.Settings.DataModel;
public sealed class DataTranslation
@@ -1,7 +1,6 @@
using AIStudio.Assistants.Coding;
using AIStudio.Assistants.IconFinder;
using AIStudio.Assistants.TextSummarizer;
using AIStudio.Tools;
namespace AIStudio.Settings.DataModel.PreviousModels;
@@ -5,6 +5,7 @@ using AIStudio.Provider;
using Microsoft.AspNetCore.Components;
using Host = AIStudio.Provider.SelfHosted.Host;
using RustService = AIStudio.Tools.RustService;
namespace AIStudio.Settings;
@@ -78,6 +79,12 @@ public partial class ProviderDialog : ComponentBase
[Inject]
private ILogger<ProviderDialog> Logger { get; init; } = null!;
[Inject]
private RustService RustService { get; init; } = null!;
[Inject]
private Encryption Encryption { get; init; } = null!;
private static readonly Dictionary<string, object?> SPELLCHECK_ATTRIBUTES = new();
@@ -136,7 +143,7 @@ public partial class ProviderDialog : ComponentBase
}
var loadedProviderSettings = this.CreateProviderSettings();
var provider = loadedProviderSettings.CreateProvider(this.Logger);
var provider = loadedProviderSettings.CreateProvider(this.Logger, this.RustService);
if(provider is NoProvider)
{
await base.OnInitializedAsync();
@@ -144,10 +151,10 @@ public partial class ProviderDialog : ComponentBase
}
// Load the API key:
var requestedSecret = await this.SettingsManager.GetAPIKey(this.JsRuntime, provider);
var requestedSecret = await this.RustService.GetAPIKey(provider);
if(requestedSecret.Success)
{
this.dataAPIKey = requestedSecret.Secret;
this.dataAPIKey = await requestedSecret.Secret.Decrypt(this.Encryption);
// Now, we try to load the list of available models:
await this.ReloadModels();
@@ -190,7 +197,7 @@ public partial class ProviderDialog : ComponentBase
if (addedProviderSettings.UsedProvider != Providers.SELF_HOSTED)
{
// We need to instantiate the provider to store the API key:
var provider = addedProviderSettings.CreateProvider(this.Logger);
var provider = addedProviderSettings.CreateProvider(this.Logger, this.RustService);
// Store the API key in the OS secure storage:
var storeResponse = await this.SettingsManager.SetAPIKey(this.JsRuntime, provider, this.dataAPIKey);
@@ -321,7 +328,7 @@ public partial class ProviderDialog : ComponentBase
private async Task ReloadModels()
{
var currentProviderSettings = this.CreateProviderSettings();
var provider = currentProviderSettings.CreateProvider(this.Logger);
var provider = currentProviderSettings.CreateProvider(this.Logger, this.RustService);
if(provider is NoProvider)
return;
@@ -41,24 +41,6 @@ public sealed class SettingsManager(ILogger<SettingsManager> logger)
private bool IsSetUp => !string.IsNullOrWhiteSpace(ConfigDirectory) && !string.IsNullOrWhiteSpace(DataDirectory);
#region API Key Handling
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);