2026-01-09 12:45:21 +01:00
using AIStudio.Components ;
using AIStudio.Provider ;
2026-08-30 16:45:14 +02:00
using AIStudio.Provider.HuggingFace ;
2026-01-09 12:45:21 +01:00
using AIStudio.Settings ;
using AIStudio.Tools.Services ;
using AIStudio.Tools.Validation ;
using Microsoft.AspNetCore.Components ;
using Host = AIStudio . Provider . SelfHosted . Host ;
namespace AIStudio.Dialogs ;
public partial class TranscriptionProviderDialog : MSGComponentBase , ISecretId
{
[CascadingParameter]
private IMudDialogInstance MudDialog { get ; set ; } = null !;
/// <summary>
/// The transcription provider's number in the list.
/// </summary>
[Parameter]
public uint DataNum { get ; set ; }
/// <summary>
/// The transcription provider's ID.
/// </summary>
[Parameter]
public string DataId { get ; set ; } = Guid . NewGuid (). ToString ();
/// <summary>
/// The user chosen name.
/// </summary>
[Parameter]
public string DataName { get ; set ; } = string . Empty ;
/// <summary>
/// The chosen hostname for self-hosted providers.
/// </summary>
[Parameter]
public string DataHostname { get ; set ; } = string . Empty ;
/// <summary>
/// The host to use, e.g., llama.cpp.
/// </summary>
[Parameter]
public Host DataHost { get ; set ; } = Host . NONE ;
/// <summary>
/// Is this provider self-hosted?
/// </summary>
[Parameter]
public bool IsSelfHosted { get ; set ; }
/// <summary>
/// The provider to use.
/// </summary>
[Parameter]
public LLMProviders DataLLMProvider { get ; set ; } = LLMProviders . NONE ;
2026-08-25 12:46:10 +02:00
/// <summary>
/// The validated custom icon supplied by a configuration plugin.
/// </summary>
[Parameter]
public string DataCustomIconDataUrl { get ; set ; } = string . Empty ;
2026-01-09 12:45:21 +01:00
/// <summary>
/// The transcription model to use.
/// </summary>
[Parameter]
public Model DataModel { get ; set ; }
2026-08-30 16:45:14 +02:00
/// <summary>
/// The Hugging Face inference provider to use.
/// </summary>
[Parameter]
public HFInferenceProvider HFInferenceProviderId { get ; set ; } = HFInferenceProvider . NONE ;
2026-01-09 12:45:21 +01:00
/// <summary>
/// Should the dialog be in editing mode?
/// </summary>
[Parameter]
public bool IsEditing { get ; init ; }
2026-08-15 11:20:36 +02:00
/// <summary>
/// Whether this transcription provider is managed by an enterprise configuration plugin. When
/// true, every field except the API key is locked, matching Settings.TranscriptionProvider.IsEnterpriseConfiguration.
/// </summary>
[Parameter]
public bool IsEnterpriseConfiguration { get ; set ; }
2026-01-09 12:45:21 +01:00
[Inject]
private RustService RustService { get ; init ; } = null !;
2026-01-18 17:15:18 +01:00
[Inject]
private ILogger < TranscriptionProviderDialog > Logger { get ; init ; } = null !;
2026-01-09 12:45:21 +01:00
private static readonly Dictionary < string , object? > SPELLCHECK_ATTRIBUTES = new ();
/// <summary>
/// The list of used instance names. We need this to check for uniqueness.
/// </summary>
private List < string > UsedInstanceNames { get ; set ; } = [];
private bool dataIsValid ;
private string [] dataIssues = [];
private string dataAPIKey = string . Empty ;
2026-08-15 11:20:36 +02:00
private bool dataHadStoredAPIKeyOnLoad ;
2026-01-09 12:45:21 +01:00
private string dataManuallyModel = string . Empty ;
private string dataAPIKeyStorageIssue = string . Empty ;
private string dataEditingPreviousInstanceName = string . Empty ;
2026-01-18 17:15:18 +01:00
private string dataLoadingModelsIssue = string . Empty ;
2026-01-09 12:45:21 +01:00
// We get the form reference from Blazor code to validate it manually:
private MudForm form = null !;
private readonly List < Model > availableModels = new ();
private readonly Encryption encryption = Program . ENCRYPTION ;
private readonly ProviderValidation providerValidation ;
public TranscriptionProviderDialog ()
{
this . providerValidation = new ()
{
GetProvider = () => this . DataLLMProvider ,
GetAPIKeyStorageIssue = () => this . dataAPIKeyStorageIssue ,
GetPreviousInstanceName = () => this . dataEditingPreviousInstanceName ,
GetUsedInstanceNames = () => this . UsedInstanceNames ,
GetHost = () => this . DataHost ,
2026-01-18 17:15:18 +01:00
IsModelProvidedManually = () => this . DataLLMProvider . IsTranscriptionModelProvidedManually ( this . DataHost ),
2026-01-09 12:45:21 +01:00
};
}
private TranscriptionProvider CreateTranscriptionProviderSettings ()
{
var cleanedHostname = this . DataHostname . Trim ();
2026-01-18 17:15:18 +01:00
// Determine the model based on the provider and host configuration:
Model model ;
if ( this . DataLLMProvider . IsTranscriptionModelSelectionHidden ( this . DataHost ))
{
// Use system model placeholder for hosts that don't support model selection (e.g., whisper.cpp):
model = Model . SYSTEM_MODEL ;
}
else if ( this . DataLLMProvider is LLMProviders . SELF_HOSTED )
2026-01-09 12:45:21 +01:00
{
switch ( this . DataHost )
{
case Host . OLLAMA :
model = new Model ( this . dataManuallyModel , null );
break ;
2026-01-18 17:15:18 +01:00
2026-01-09 12:45:21 +01:00
case Host . VLLM :
case Host . LM_STUDIO :
2026-01-18 17:15:18 +01:00
default :
2026-01-09 12:45:21 +01:00
model = this . DataModel ;
break ;
}
}
else
model = this . DataModel ;
2026-01-18 17:15:18 +01:00
2026-01-09 12:45:21 +01:00
return new ()
{
Num = this . DataNum ,
Id = this . DataId ,
Name = this . DataName ,
UsedLLMProvider = this . DataLLMProvider ,
Model = model ,
IsSelfHosted = this . DataLLMProvider is LLMProviders . SELF_HOSTED ,
Hostname = cleanedHostname . EndsWith ( '/' ) ? cleanedHostname [..^ 1 ] : cleanedHostname ,
Host = this . DataHost ,
2026-08-15 11:20:36 +02:00
IsEnterpriseConfiguration = this . IsEnterpriseConfiguration ,
2026-01-09 15:49:44 +01:00
EnterpriseConfigurationPluginId = Guid . Empty ,
2026-08-25 12:46:10 +02:00
CustomIconDataUrl = this . DataCustomIconDataUrl ,
2026-08-30 16:45:14 +02:00
HFInferenceProvider = this . HFInferenceProviderId ,
2026-01-09 12:45:21 +01:00
};
}
#region Overrides of ComponentBase
protected override async Task OnInitializedAsync ()
{
2026-01-11 16:02:28 +01:00
// Call the base initialization first so that the I18N is ready:
await base . OnInitializedAsync ();
2026-01-09 12:45:21 +01:00
// Configure the spellchecking for the instance name input:
this . SettingsManager . InjectSpellchecking ( SPELLCHECK_ATTRIBUTES );
// Load the used instance names:
this . UsedInstanceNames = this . SettingsManager . ConfigurationData . TranscriptionProviders . Select ( x => x . Name . ToLowerInvariant ()). ToList ();
// When editing, we need to load the data:
if ( this . IsEditing )
{
this . dataEditingPreviousInstanceName = this . DataName . ToLowerInvariant ();
// When using self-hosted models, we must copy the model name:
if ( this . DataLLMProvider is LLMProviders . SELF_HOSTED )
this . dataManuallyModel = this . DataModel . Id ;
//
// We cannot load the API key for self-hosted providers:
//
if ( this . DataLLMProvider is LLMProviders . SELF_HOSTED && this . DataHost is not Host . OLLAMA )
{
await this . ReloadModels ();
await base . OnInitializedAsync ();
return ;
}
// Load the API key:
2026-01-11 16:02:28 +01:00
var requestedSecret = await this . RustService . GetAPIKey ( this , SecretStoreType . TRANSCRIPTION_PROVIDER , isTrying : this . DataLLMProvider is LLMProviders . SELF_HOSTED );
2026-01-09 12:45:21 +01:00
if ( requestedSecret . Success )
2026-08-15 11:20:36 +02:00
{
2026-01-09 12:45:21 +01:00
this . dataAPIKey = await requestedSecret . Secret . Decrypt ( this . encryption );
2026-08-15 11:20:36 +02:00
this . dataHadStoredAPIKeyOnLoad = ! string . IsNullOrWhiteSpace ( this . dataAPIKey );
}
2026-01-09 12:45:21 +01:00
else
{
this . dataAPIKey = string . Empty ;
2026-08-15 11:20:36 +02:00
// For an enterprise-managed provider, having no key yet is the expected first-run
// state, not a storage failure -- the user is just about to set their own key:
if ( this . DataLLMProvider is not LLMProviders . SELF_HOSTED && ! this . IsEnterpriseConfiguration )
2026-01-09 12:45:21 +01:00
{
this . dataAPIKeyStorageIssue = string . Format ( T ( "Failed to load the API key from the operating system. The message was: {0}. You might ignore this message and provide the API key again." ), requestedSecret . Issue );
await this . form . Validate ();
}
}
await this . ReloadModels ();
}
}
protected override async Task OnAfterRenderAsync ( bool firstRender )
{
// 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.
if (! this . IsEditing && firstRender )
this . form . ResetValidation ();
await base . OnAfterRenderAsync ( firstRender );
}
#endregion
#region Implementation of ISecretId
2026-08-15 11:20:36 +02:00
// Must mirror Settings.TranscriptionProvider.SecretId exactly: when editing an enterprise-managed
// provider, the key has to be stored under the same "ENT::"-prefixed keyring row that the
// app reads from at runtime (see BaseProvider.SecretId). Otherwise, a key entered here would
// silently end up in the wrong keyring row and never be found again.
public string SecretId => this . IsEnterpriseConfiguration ? $"{ISecretId.ENTERPRISE_KEY_PREFIX}::{this.DataLLMProvider.ToSecretId()}" : this . DataLLMProvider . ToSecretId ();
2026-01-09 12:45:21 +01:00
public string SecretName => this . DataName ;
#endregion
private async Task Store ()
{
await this . form . Validate ();
this . dataAPIKeyStorageIssue = string . Empty ;
2026-01-18 17:15:18 +01:00
// Manually validate the model selection (needed when no models are loaded
// and the MudSelect is not rendered):
var modelValidationError = this . providerValidation . ValidatingModel ( this . DataModel );
if (! string . IsNullOrWhiteSpace ( modelValidationError ))
{
this . dataIssues = [.. this . dataIssues , modelValidationError ];
this . dataIsValid = false ;
}
2026-01-09 12:45:21 +01:00
// When the data is not valid, we don't store it:
if (! this . dataIsValid )
return ;
// Use the data model to store the provider.
// We just return this data to the parent component:
var addedProviderSettings = this . CreateTranscriptionProviderSettings ();
if (! string . IsNullOrWhiteSpace ( this . dataAPIKey ))
{
// Store the API key in the OS secure storage:
2026-01-11 16:02:28 +01:00
var storeResponse = await this . RustService . SetAPIKey ( this , this . dataAPIKey , SecretStoreType . TRANSCRIPTION_PROVIDER );
2026-01-09 12:45:21 +01:00
if (! storeResponse . Success )
{
this . dataAPIKeyStorageIssue = string . Format ( T ( "Failed to store the API key in the operating system. The message was: {0}. Please try again." ), storeResponse . Issue );
await this . form . Validate ();
return ;
}
2026-08-15 11:20:36 +02:00
this . dataHadStoredAPIKeyOnLoad = true ;
}
else if ( this . dataHadStoredAPIKeyOnLoad )
{
// The user cleared a previously stored key. Without this, the old key would simply
// stay in the OS keyring untouched and keep being used:
var deleteResponse = await this . RustService . DeleteAPIKey ( this , SecretStoreType . TRANSCRIPTION_PROVIDER );
if (! deleteResponse . Success )
{
this . dataAPIKeyStorageIssue = string . Format ( T ( "Failed to remove the API key from the operating system. The message was: {0}. Please try again." ), deleteResponse . Issue );
await this . form . Validate ();
return ;
}
this . dataHadStoredAPIKeyOnLoad = false ;
2026-01-09 12:45:21 +01:00
}
this . MudDialog . Close ( DialogResult . Ok ( addedProviderSettings ));
}
private string? ValidateManuallyModel ( string manuallyModel )
{
if ( this . DataLLMProvider is LLMProviders . SELF_HOSTED && string . IsNullOrWhiteSpace ( manuallyModel ))
return T ( "Please enter a transcription model name." );
2026-01-18 17:15:18 +01:00
2026-01-09 12:45:21 +01:00
return null ;
}
private void Cancel () => this . MudDialog . Cancel ();
2026-01-11 16:02:28 +01:00
private async Task OnAPIKeyChanged ( string apiKey )
{
this . dataAPIKey = apiKey ;
if (! string . IsNullOrWhiteSpace ( this . dataAPIKeyStorageIssue ))
{
this . dataAPIKeyStorageIssue = string . Empty ;
await this . form . Validate ();
}
}
2026-01-18 17:15:18 +01:00
private void OnHostChanged ( Host selectedHost )
{
// When the host changes, reset the model selection state:
this . DataHost = selectedHost ;
this . DataModel = default ;
this . dataManuallyModel = string . Empty ;
this . availableModels . Clear ();
this . dataLoadingModelsIssue = string . Empty ;
}
2026-08-30 16:45:14 +02:00
/// <summary>
/// Resets the model selection when the user picks another Hugging Face inference provider.
/// </summary>
/// <remarks>
/// Each inference provider offers transcription models of its own, so the models loaded for the
/// previous one say nothing about the new one.
/// </remarks>
/// <param name="selectedInferenceProvider">The inference provider the user chose.</param>
private void OnHFInferenceProviderChanged ( HFInferenceProvider selectedInferenceProvider )
{
this . HFInferenceProviderId = selectedInferenceProvider ;
this . DataModel = default ;
this . availableModels . Clear ();
this . dataLoadingModelsIssue = string . Empty ;
}
2026-01-09 12:45:21 +01:00
private async Task ReloadModels ()
{
2026-01-18 17:15:18 +01:00
this . dataLoadingModelsIssue = string . Empty ;
2026-01-09 12:45:21 +01:00
var currentTranscriptionProviderSettings = this . CreateTranscriptionProviderSettings ();
var provider = currentTranscriptionProviderSettings . CreateProvider ();
2026-01-18 17:15:18 +01:00
if ( provider is NoProvider )
2026-01-09 12:45:21 +01:00
return ;
2026-01-18 17:15:18 +01:00
try
{
2026-04-14 13:39:11 +02:00
var result = await provider . GetTranscriptionModels ( this . dataAPIKey );
if (! result . Success )
this . dataLoadingModelsIssue = result . FailureReason . ToUserMessage ( provider . InstanceName );
2026-01-18 17:15:18 +01:00
// Order descending by ID means that the newest models probably come first:
2026-04-14 13:39:11 +02:00
var orderedModels = result . Models . OrderByDescending ( n => n . Id );
2026-01-18 17:15:18 +01:00
this . availableModels . Clear ();
this . availableModels . AddRange ( orderedModels );
}
catch ( Exception e )
{
2026-08-02 21:41:12 +02:00
this . Logger . LogError ( $"Failed to load models from provider '{this.DataLLMProvider}' (host={this.DataHost}, hostname='{this.DataHostname}'): {e.Message}" );
2026-01-18 17:15:18 +01:00
this . dataLoadingModelsIssue = T ( "We are currently unable to communicate with the provider to load models. Please try again later." );
}
2026-01-09 12:45:21 +01:00
}
private string APIKeyText => this . DataLLMProvider switch
{
LLMProviders . SELF_HOSTED => T ( "(Optional) API Key" ),
_ => T ( "API Key" ),
};
private bool IsNoneProvider => this . DataLLMProvider is LLMProviders . NONE ;
2026-08-25 12:46:10 +02:00
}