AI-Studio/app/MindWork AI Studio/Dialogs/EmbeddingMethodDialog.razor.cs
Thorsten Sommer ce8d84a12a
Some checks are pending
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-pc-windows-msvc.exe, win-x64, windows-latest, x86_64-pc-windows-msvc, nsis updater) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-unknown-linux-gnu, linux-x64, ubuntu-22.04, x86_64-unknown-linux-gnu, appimage deb updater) (push) Blocked by required conditions
Build and Release / Build app (linux-arm64) (push) Blocked by required conditions
Build and Release / Prepare & create release (push) Blocked by required conditions
Build and Release / Read metadata (push) Waiting to run
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-apple-darwin, osx-arm64, macos-latest, aarch64-apple-darwin, dmg updater) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-pc-windows-msvc.exe, win-arm64, windows-latest, aarch64-pc-windows-msvc, nsis updater) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-apple-darwin, osx-x64, macos-latest, x86_64-apple-darwin, dmg updater) (push) Blocked by required conditions
Build and Release / Publish release (push) Blocked by required conditions
Migrate to .NET 9 & MudBlazor 8 (#333)
2025-03-12 19:12:56 +01:00

144 lines
4.4 KiB
C#

using AIStudio.Assistants.ERI;
using AIStudio.Settings;
using Microsoft.AspNetCore.Components;
namespace AIStudio.Dialogs;
public partial class EmbeddingMethodDialog : ComponentBase
{
[CascadingParameter]
private IMudDialogInstance MudDialog { get; set; } = null!;
/// <summary>
/// The user chosen embedding name.
/// </summary>
[Parameter]
public string DataEmbeddingName { get; set; } = string.Empty;
/// <summary>
/// The user chosen embedding type.
/// </summary>
[Parameter]
public string DataEmbeddingType { get; set; } = string.Empty;
/// <summary>
/// The embedding description.
/// </summary>
[Parameter]
public string DataDescription { get; set; } = string.Empty;
/// <summary>
/// When is the embedding used?
/// </summary>
[Parameter]
public string DataUsedWhen { get; set; } = string.Empty;
/// <summary>
/// A link to the embedding documentation or the source code. Might be null, which means no link is provided.
/// </summary>
[Parameter]
public string DataLink { get; set; } = string.Empty;
/// <summary>
/// The embedding method names that are already used. The user must choose a unique name.
/// </summary>
[Parameter]
public IReadOnlyList<string> UsedEmbeddingMethodNames { get; set; } = new List<string>();
/// <summary>
/// Should the dialog be in editing mode?
/// </summary>
[Parameter]
public bool IsEditing { get; init; }
[Inject]
private SettingsManager SettingsManager { get; init; } = null!;
private static readonly Dictionary<string, object?> SPELLCHECK_ATTRIBUTES = new();
private bool dataIsValid;
private string[] dataIssues = [];
// We get the form reference from Blazor code to validate it manually:
private MudForm form = null!;
private EmbeddingInfo CreateEmbeddingInfo() => new(this.DataEmbeddingType, this.DataEmbeddingName, this.DataDescription, this.DataUsedWhen, this.DataLink);
#region Overrides of ComponentBase
protected override async Task OnInitializedAsync()
{
// Configure the spellchecking for the instance name input:
this.SettingsManager.InjectSpellchecking(SPELLCHECK_ATTRIBUTES);
await base.OnInitializedAsync();
}
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
private string? ValidateName(string name)
{
if (string.IsNullOrWhiteSpace(name))
return "The embedding name must not be empty. Please name the embedding.";
if (name.Length > 26)
return "The embedding name must not be longer than 26 characters.";
if (this.UsedEmbeddingMethodNames.Contains(name))
return $"The embedding method name '{name}' is already used. Please choose a unique name.";
return null;
}
private string? ValidateType(string type)
{
if (string.IsNullOrWhiteSpace(type))
return "The embedding type must not be empty. Please specify the embedding type.";
if (type.Length > 56)
return "The embedding type must not be longer than 56 characters.";
return null;
}
private string? ValidateDescription(string description)
{
if (string.IsNullOrWhiteSpace(description))
return "The description must not be empty. Please describe the embedding method.";
return null;
}
private string? ValidateUsedWhen(string usedWhen)
{
if (string.IsNullOrWhiteSpace(usedWhen))
return "Please describe when the embedding is used. Might be anytime or when certain keywords are present, etc.";
return null;
}
private async Task Store()
{
await this.form.Validate();
// When the data is not valid, we don't store it:
if (!this.dataIsValid)
return;
var embeddingInfo = this.CreateEmbeddingInfo();
this.MudDialog.Close(DialogResult.Ok(embeddingInfo));
}
private void Cancel() => this.MudDialog.Cancel();
}