2024-04-19 21:27:38 +00:00
using System.Text.RegularExpressions ;
2024-04-19 19:25:44 +00:00
using AIStudio.Provider ;
using Microsoft.AspNetCore.Components ;
namespace AIStudio.Settings ;
2024-05-04 08:55:00 +00:00
/// <summary>
/// The provider settings dialog.
/// </summary>
2024-04-19 19:25:44 +00:00
public partial class ProviderDialog : ComponentBase
{
[CascadingParameter]
private MudDialogInstance MudDialog { get ; set ; } = null ! ;
2024-05-19 18:28:25 +00:00
/// <summary>
/// The provider's number in the list.
/// </summary>
[Parameter]
public uint DataNum { get ; set ; }
2024-04-19 21:27:38 +00:00
2024-05-04 08:55:00 +00:00
/// <summary>
/// The provider's ID.
/// </summary>
2024-04-19 21:27:38 +00:00
[Parameter]
2024-04-20 15:06:50 +00:00
public string DataId { get ; set ; } = Guid . NewGuid ( ) . ToString ( ) ;
2024-05-04 08:55:00 +00:00
/// <summary>
/// The user chosen instance name.
/// </summary>
2024-04-20 15:06:50 +00:00
[Parameter]
public string DataInstanceName { get ; set ; } = string . Empty ;
2024-05-04 08:55:00 +00:00
/// <summary>
/// The provider to use.
/// </summary>
2024-04-20 15:06:50 +00:00
[Parameter]
public Providers DataProvider { get ; set ; } = Providers . NONE ;
2024-05-04 08:55:00 +00:00
2024-05-19 14:12:07 +00:00
/// <summary>
/// The LLM model to use, e.g., GPT-4o.
/// </summary>
[Parameter]
public Model DataModel { get ; set ; }
2024-05-04 08:55:00 +00:00
/// <summary>
/// Should the dialog be in editing mode?
/// </summary>
2024-04-20 15:06:50 +00:00
[Parameter]
public bool IsEditing { get ; init ; }
[Inject]
private SettingsManager SettingsManager { get ; set ; } = null ! ;
[Inject]
private IJSRuntime JsRuntime { get ; set ; } = null ! ;
2024-04-19 19:25:44 +00:00
2024-05-04 08:55:00 +00:00
/// <summary>
/// The list of used instance names. We need this to check for uniqueness.
/// </summary>
2024-05-19 14:12:50 +00:00
private List < string > UsedInstanceNames { get ; set ; } = [ ] ;
2024-04-20 15:06:50 +00:00
2024-04-19 19:25:44 +00:00
private bool dataIsValid ;
private string [ ] dataIssues = [ ] ;
2024-04-20 15:06:50 +00:00
private string dataAPIKey = string . Empty ;
private string dataAPIKeyStorageIssue = string . Empty ;
private string dataEditingPreviousInstanceName = string . Empty ;
2024-04-19 19:25:44 +00:00
2024-05-04 08:55:00 +00:00
// We get the form reference from Blazor code to validate it manually:
2024-04-19 19:25:44 +00:00
private MudForm form = null ! ;
2024-05-19 14:12:07 +00:00
private readonly List < Model > availableModels = new ( ) ;
2024-04-19 19:25:44 +00:00
2024-04-20 15:06:50 +00:00
#region Overrides of ComponentBase
protected override async Task OnInitializedAsync ( )
{
2024-05-04 08:55:00 +00:00
// Load the used instance names:
2024-05-19 14:12:50 +00:00
this . UsedInstanceNames = this . SettingsManager . ConfigurationData . Providers . Select ( x = > x . InstanceName . ToLowerInvariant ( ) ) . ToList ( ) ;
2024-04-20 15:06:50 +00:00
2024-05-04 08:55:00 +00:00
// When editing, we need to load the data:
2024-04-20 15:06:50 +00:00
if ( this . IsEditing )
{
this . dataEditingPreviousInstanceName = this . DataInstanceName . ToLowerInvariant ( ) ;
2024-05-19 14:09:52 +00:00
var provider = this . DataProvider . CreateProvider ( this . DataInstanceName ) ;
2024-04-20 15:06:50 +00:00
if ( provider is NoProvider )
return ;
2024-05-04 08:55:00 +00:00
// Load the API key:
2024-04-20 15:06:50 +00:00
var requestedSecret = await this . SettingsManager . GetAPIKey ( this . JsRuntime , provider ) ;
if ( requestedSecret . Success )
2024-05-19 14:12:07 +00:00
{
2024-04-20 15:06:50 +00:00
this . dataAPIKey = requestedSecret . Secret ;
2024-05-19 14:12:07 +00:00
// Now, we try to load the list of available models:
await this . ReloadModels ( ) ;
}
2024-04-20 15:06:50 +00:00
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 )
{
2024-05-04 08:55:00 +00:00
// Reset the validation when not editing and on the first render.
// We don't want to show validation errors when the user opens the dialog.
2024-04-20 15:06:50 +00:00
if ( ! this . IsEditing & & firstRender )
this . form . ResetValidation ( ) ;
await base . OnAfterRenderAsync ( firstRender ) ;
}
#endregion
private async Task Store ( )
2024-04-19 19:25:44 +00:00
{
await this . form . Validate ( ) ;
2024-04-20 15:06:50 +00:00
if ( ! string . IsNullOrWhiteSpace ( this . dataAPIKeyStorageIssue ) )
this . dataAPIKeyStorageIssue = string . Empty ;
2024-05-04 08:55:00 +00:00
// When the data is not valid, we don't store it:
2024-04-19 19:25:44 +00:00
if ( ! this . dataIsValid )
return ;
2024-05-04 08:55:00 +00:00
// Use the data model to store the provider.
// We just return this data to the parent component:
2024-04-19 19:25:44 +00:00
var addedProvider = new Provider
{
2024-05-19 18:28:25 +00:00
Num = this . DataNum ,
2024-04-20 15:06:50 +00:00
Id = this . DataId ,
InstanceName = this . DataInstanceName ,
UsedProvider = this . DataProvider ,
2024-05-19 14:12:07 +00:00
Model = this . DataModel ,
2024-04-19 19:25:44 +00:00
} ;
2024-05-04 08:55:00 +00:00
// We need to instantiate the provider to store the API key:
2024-05-19 14:09:52 +00:00
var provider = this . DataProvider . CreateProvider ( this . DataInstanceName ) ;
2024-04-20 15:06:50 +00:00
2024-05-04 08:55:00 +00:00
// Store the API key in the OS secure storage:
2024-04-20 15:06:50 +00:00
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 ;
}
2024-04-19 19:25:44 +00:00
this . MudDialog . Close ( DialogResult . Ok ( addedProvider ) ) ;
}
private string? ValidatingProvider ( Providers provider )
{
if ( provider = = Providers . NONE )
return "Please select a provider." ;
return null ;
}
2024-04-19 21:27:38 +00:00
2024-05-19 14:12:07 +00:00
private string? ValidatingModel ( Model model )
{
if ( model = = default )
return "Please select a model." ;
return null ;
}
2024-04-20 15:06:50 +00:00
[GeneratedRegex("^[a-zA-Z0-9 ] + $ ")]
private static partial Regex InstanceNameRegex ( ) ;
2024-04-19 21:27:38 +00:00
private string? ValidatingInstanceName ( string instanceName )
{
2024-04-20 15:06:50 +00:00
if ( string . IsNullOrWhiteSpace ( instanceName ) )
return "Please enter an instance name." ;
2024-04-19 21:27:38 +00:00
if ( instanceName . StartsWith ( ' ' ) )
return "The instance name must not start with a space." ;
if ( instanceName . EndsWith ( ' ' ) )
return "The instance name must not end with a space." ;
// The instance name must only contain letters, numbers, and spaces:
if ( ! InstanceNameRegex ( ) . IsMatch ( instanceName ) )
return "The instance name must only contain letters, numbers, and spaces." ;
if ( instanceName . Contains ( " " ) )
return "The instance name must not contain consecutive spaces." ;
// The instance name must be unique:
2024-04-20 15:06:50 +00:00
var lowerInstanceName = instanceName . ToLowerInvariant ( ) ;
2024-05-19 14:12:50 +00:00
if ( lowerInstanceName ! = this . dataEditingPreviousInstanceName & & this . UsedInstanceNames . Contains ( lowerInstanceName ) )
2024-04-19 21:27:38 +00:00
return "The instance name must be unique; the chosen name is already in use." ;
return null ;
}
2024-04-20 15:06:50 +00:00
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 ;
}
2024-04-19 19:25:44 +00:00
private void Cancel ( ) = > this . MudDialog . Cancel ( ) ;
2024-05-19 14:12:07 +00:00
2024-05-19 17:42:15 +00:00
private bool CanLoadModels = > ! string . IsNullOrWhiteSpace ( this . dataAPIKey ) & & this . DataProvider ! = Providers . NONE & & ! string . IsNullOrWhiteSpace ( this . DataInstanceName ) ;
2024-05-19 14:12:07 +00:00
private async Task ReloadModels ( )
{
var provider = this . DataProvider . CreateProvider ( this . DataInstanceName ) ;
if ( provider is NoProvider )
return ;
var models = await provider . GetTextModels ( this . JsRuntime , this . SettingsManager ) ;
// Order descending by ID means that the newest models probably come first:
var orderedModels = models . OrderByDescending ( n = > n . Id ) ;
this . availableModels . Clear ( ) ;
this . availableModels . AddRange ( orderedModels ) ;
}
2024-04-19 19:25:44 +00:00
}