mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-11-14 20:48:27 +00:00
Co-authored-by: Thorsten Sommer <SommerEngineering@users.noreply.github.com>
112 lines
4.1 KiB
C#
112 lines
4.1 KiB
C#
using AIStudio.Provider;
|
|
using AIStudio.Settings;
|
|
using AIStudio.Tools.PluginSystem;
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
namespace AIStudio.Components;
|
|
|
|
public partial class ConfigurationProviderSelection : MSGComponentBase
|
|
{
|
|
private new static string TA(string fallbackEN) => I18N.I.TA(fallbackEN, typeof(ConfigurationProviderSelection).Namespace, nameof(ConfigurationProviderSelection));
|
|
|
|
[Parameter]
|
|
public Func<string> SelectedValue { get; set; } = () => string.Empty;
|
|
|
|
[Parameter]
|
|
public Action<string> SelectionUpdate { get; set; } = _ => { };
|
|
|
|
[Parameter]
|
|
public IEnumerable<ConfigurationSelectData<string>> Data { get; set; } = new List<ConfigurationSelectData<string>>();
|
|
|
|
[Parameter]
|
|
public Func<string> HelpText { get; set; } = () => TA("Select a provider that is preselected.");
|
|
|
|
[Parameter]
|
|
public Tools.Components Component { get; set; } = Tools.Components.NONE;
|
|
|
|
[Parameter]
|
|
public ConfidenceLevel ExplicitMinimumConfidence { get; set; } = ConfidenceLevel.UNKNOWN;
|
|
|
|
[Parameter]
|
|
public Func<bool> Disabled { get; set; } = () => false;
|
|
|
|
[Parameter]
|
|
public Func<bool> IsLocked { get; set; } = () => false;
|
|
|
|
/// <summary>
|
|
/// The label of the option, or null for "Preselected provider".
|
|
/// </summary>
|
|
[Parameter]
|
|
public string? OptionDescription { get; set; }
|
|
|
|
/// <summary>
|
|
/// The text of an entry which preselects no provider at all, or null to offer no such entry.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// The entry stands before "Use app default" and selects the ID of Provider.NONE, because an empty text already
|
|
/// means the provider of the whole app.
|
|
/// </remarks>
|
|
[Parameter]
|
|
public string? NoPreselectionText { get; set; }
|
|
|
|
private IEnumerable<ConfigurationSelectData<string>> FilteredData()
|
|
{
|
|
if (this.NoPreselectionText is not null)
|
|
yield return new(this.NoPreselectionText, AIStudio.Settings.Provider.NONE.Id);
|
|
|
|
if(this.Component is not Tools.Components.NONE and not Tools.Components.APP_SETTINGS)
|
|
yield return new(T("Use app default"), string.Empty);
|
|
|
|
//
|
|
// Filter the providers based on the minimum confidence level of this component, the enforced
|
|
// global minimum, and the explicit minimum level when it is higher. Providers which no longer
|
|
// exist resolve to `Provider.NONE` and are dropped by the confidence check as well:
|
|
//
|
|
foreach (var providerId in this.Data)
|
|
{
|
|
var provider = this.SettingsManager.GetProviderById(providerId.Value);
|
|
if (this.SettingsManager.IsProviderConfident(provider, this.Component, this.ExplicitMinimumConfidence))
|
|
yield return providerId;
|
|
}
|
|
}
|
|
|
|
private AIStudio.Settings.Provider? GetProvider(string providerId)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(providerId))
|
|
return null;
|
|
|
|
var provider = this.SettingsManager.GetProviderById(providerId);
|
|
return provider == AIStudio.Settings.Provider.NONE ? null : provider;
|
|
}
|
|
|
|
#region Overrides of MSGComponentBase
|
|
|
|
protected override async Task ProcessIncomingMessage<T>(ComponentBase? sendingComponent, Event triggeredEvent, T? data) where T : default
|
|
{
|
|
switch (triggeredEvent)
|
|
{
|
|
case Event.CONFIGURATION_CHANGED:
|
|
|
|
if(string.IsNullOrWhiteSpace(this.SelectedValue()))
|
|
break;
|
|
|
|
// Preselecting no provider stays valid, whichever providers exist:
|
|
if (this.NoPreselectionText is not null && this.SelectedValue() == AIStudio.Settings.Provider.NONE.Id)
|
|
break;
|
|
|
|
// Check if the selected value is still valid:
|
|
if (this.Data.All(x => x.Value != this.SelectedValue()))
|
|
{
|
|
this.SelectedValue = () => string.Empty;
|
|
this.SelectionUpdate(string.Empty);
|
|
await this.SettingsManager.StoreSettings();
|
|
}
|
|
|
|
this.StateHasChanged();
|
|
break;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
}
|