AI-Studio/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogBase.cs

94 lines
2.8 KiB
C#
Raw Normal View History

using System.Diagnostics.CodeAnalysis;
2025-03-12 17:05:27 +00:00
using AIStudio.Settings;
2025-03-16 13:03:43 +00:00
using AIStudio.Tools.Services;
using Microsoft.AspNetCore.Components;
namespace AIStudio.Dialogs.Settings;
public abstract class SettingsDialogBase : ComponentBase, IMessageBusReceiver, IDisposable
{
[CascadingParameter]
2025-03-12 18:12:56 +00:00
protected IMudDialogInstance MudDialog { get; set; } = null!;
[Inject]
protected SettingsManager SettingsManager { get; init; } = null!;
[Inject]
protected IDialogService DialogService { get; init; } = null!;
[Inject]
protected MessageBus MessageBus { get; init; } = null!;
2025-03-16 13:03:43 +00:00
[Inject]
protected RustService RustService { get; init; } = null!;
protected readonly List<ConfigurationSelectData<string>> availableLLMProviders = new();
protected readonly List<ConfigurationSelectData<string>> availableEmbeddingProviders = new();
#region Overrides of ComponentBase
/// <inheritdoc />
protected override async Task OnInitializedAsync()
{
// Register this component with the message bus:
this.MessageBus.RegisterComponent(this);
this.MessageBus.ApplyFilters(this, [], [ Event.CONFIGURATION_CHANGED ]);
this.UpdateProviders();
2025-03-16 13:03:43 +00:00
this.UpdateEmbeddingProviders();
await base.OnInitializedAsync();
}
#endregion
protected void Close() => this.MudDialog.Cancel();
[SuppressMessage("Usage", "MWAIS0001:Direct access to `Providers` is not allowed")]
private void UpdateProviders()
{
2025-03-16 13:03:43 +00:00
this.availableLLMProviders.Clear();
foreach (var provider in this.SettingsManager.ConfigurationData.Providers)
2025-03-16 13:03:43 +00:00
this.availableLLMProviders.Add(new (provider.InstanceName, provider.Id));
}
private void UpdateEmbeddingProviders()
{
this.availableEmbeddingProviders.Clear();
foreach (var provider in this.SettingsManager.ConfigurationData.EmbeddingProviders)
this.availableEmbeddingProviders.Add(new (provider.Name, provider.Id));
}
#region Implementation of IMessageBusReceiver
public string ComponentName => nameof(Settings);
public Task ProcessMessage<TMsg>(ComponentBase? sendingComponent, Event triggeredEvent, TMsg? data)
{
switch (triggeredEvent)
{
case Event.CONFIGURATION_CHANGED:
this.StateHasChanged();
break;
}
return Task.CompletedTask;
}
public Task<TResult?> ProcessMessageWithResult<TPayload, TResult>(ComponentBase? sendingComponent, Event triggeredEvent, TPayload? data)
{
return Task.FromResult<TResult?>(default);
}
#endregion
#region Implementation of IDisposable
public void Dispose()
{
this.MessageBus.Unregister(this);
}
#endregion
}