diff --git a/app/MindWork AI Studio/Assistants/AssistantBase.razor.cs b/app/MindWork AI Studio/Assistants/AssistantBase.razor.cs index 9cbec101..32c4e9e9 100644 --- a/app/MindWork AI Studio/Assistants/AssistantBase.razor.cs +++ b/app/MindWork AI Studio/Assistants/AssistantBase.razor.cs @@ -129,6 +129,11 @@ public abstract partial class AssistantBase : AssistantLowerBase wher /// protected bool IsAssistantComponentDisposed => this.isDisposed; + /// + /// Gets whether this component has attached an assistant session snapshot. + /// + protected bool HasAssistantSession => this.assistantSessionId is not null; + /// /// Gets the assistant-specific identifier used to distinguish session slots. /// diff --git a/app/MindWork AI Studio/Assistants/I18N/AssistantI18N.razor b/app/MindWork AI Studio/Assistants/I18N/AssistantI18N.razor index 2aa1e547..0655db52 100644 --- a/app/MindWork AI Studio/Assistants/I18N/AssistantI18N.razor +++ b/app/MindWork AI Studio/Assistants/I18N/AssistantI18N.razor @@ -2,8 +2,8 @@ @using AIStudio.Settings @inherits AssistantBaseCore - - + + @if (this.isLoading) { @@ -20,7 +20,7 @@ else if (!this.isLoading && string.IsNullOrWhiteSpace(this.loadingIssue)) @this.AddedContentText - + @@ -50,7 +50,7 @@ else if (!this.isLoading && string.IsNullOrWhiteSpace(this.loadingIssue)) @this.RemovedContentText - + @@ -94,7 +94,7 @@ else if (!this.isLoading && string.IsNullOrWhiteSpace(this.loadingIssue)) @this.LocalizedContentText - + diff --git a/app/MindWork AI Studio/Assistants/I18N/AssistantI18N.razor.cs b/app/MindWork AI Studio/Assistants/I18N/AssistantI18N.razor.cs index b72fe92a..49d34783 100644 --- a/app/MindWork AI Studio/Assistants/I18N/AssistantI18N.razor.cs +++ b/app/MindWork AI Studio/Assistants/I18N/AssistantI18N.razor.cs @@ -118,6 +118,7 @@ public partial class AssistantI18N : AssistantBaseCore private Dictionary removedContent = []; private Dictionary localizedContent = []; private StringBuilder finalLuaCode = new(); + private string? activeSystemPromptLanguage; private static readonly AssistantSessionStateKey SELECTED_TARGET_LANGUAGE_STATE_KEY = new(nameof(selectedTargetLanguage)); private static readonly AssistantSessionStateKey CUSTOM_TARGET_LANGUAGE_STATE_KEY = new(nameof(customTargetLanguage)); private static readonly AssistantSessionStateKey IS_LOADING_STATE_KEY = new(nameof(isLoading)); @@ -170,26 +171,34 @@ public partial class AssistantI18N : AssistantBaseCore protected override async Task OnInitializedAsync() { await base.OnInitializedAsync(); + if (this.HasAssistantSession) + return; + await this.OnLanguagePluginChanged(this.selectedLanguagePluginId); - await this.LoadData(); } #endregion - private string SystemPromptLanguage() => this.selectedTargetLanguage switch + private string SystemPromptLanguage() => this.activeSystemPromptLanguage ?? (this.selectedTargetLanguage switch { CommonLanguages.OTHER => this.customTargetLanguage, _ => $"{this.selectedTargetLanguage.Name()}", - }; + }); private async Task OnLanguagePluginChanged(Guid pluginId) { + if (this.IsProcessing) + return; + this.selectedLanguagePluginId = pluginId; await this.OnChangedLanguage(); } private async Task OnChangedLanguage() { + if (this.IsProcessing) + return; + this.finalLuaCode.Clear(); this.localizedContent.Clear(); this.localizationPossible = false; @@ -308,6 +317,21 @@ public partial class AssistantI18N : AssistantBaseCore private int NumTotalItems => (this.selectedLanguagePlugin?.Content.Count ?? 0) + this.addedContent.Count - this.removedContent.Count; + /// + /// Gets a stable row snapshot for the added-content table. + /// + private KeyValuePair[] AddedContentRows => this.addedContent.ToArray(); + + /// + /// Gets a stable row snapshot for the removed-content table. + /// + private KeyValuePair[] RemovedContentRows => this.removedContent.ToArray(); + + /// + /// Gets a stable row snapshot for the localized-content table. + /// + private KeyValuePair[] LocalizedContentRows => this.localizedContent.ToArray(); + private string AddedContentText => string.Format(T("Added Content ({0} entries)"), this.addedContent.Count); private string RemovedContentText => string.Format(T("Removed Content ({0} entries)"), this.removedContent.Count); @@ -326,68 +350,87 @@ public partial class AssistantI18N : AssistantBaseCore if (this.selectedLanguagePlugin.IETFTag != this.selectedTargetLanguage.ToIETFTag()) return; - this.localizedContent.Clear(); - if (this.selectedTargetLanguage is not CommonLanguages.EN_US) - { - // Phase 1: Translate added content - await this.Phase1TranslateAddedContent(); - } - else - { - // Case: no translation needed - this.localizedContent = this.addedContent.ToDictionary(); - } + var addedContentSnapshot = this.addedContent.ToArray(); + var removedContentSnapshot = this.removedContent.ToArray(); + var removedContentKeys = removedContentSnapshot.Select(keyValuePair => keyValuePair.Key).ToHashSet(StringComparer.Ordinal); + var selectedLanguageContentSnapshot = this.selectedLanguagePlugin.Content.ToArray(); + var baseLanguageContentSnapshot = PluginFactory.BaseLanguage.Content.ToArray(); - if(this.CancellationTokenSource!.IsCancellationRequested) - return; - - // - // Now, we have localized the added content. Next, we must merge - // the localized content with the existing content. However, we - // must skip the removed content. We use the localizedContent - // dictionary for the final result: - // - foreach (var keyValuePair in this.selectedLanguagePlugin.Content) + this.localizedContent.Clear(); + this.activeSystemPromptLanguage = this.SystemPromptLanguage(); + try { - if (this.CancellationTokenSource!.IsCancellationRequested) - break; + if (this.selectedTargetLanguage is not CommonLanguages.EN_US) + { + // Phase 1: Translate added content + await this.Phase1TranslateAddedContent(addedContentSnapshot); + } + else + { + // Case: no translation needed + this.localizedContent = addedContentSnapshot.ToDictionary(keyValuePair => keyValuePair.Key, keyValuePair => keyValuePair.Value, StringComparer.Ordinal); + } + + if(this.CancellationTokenSource!.IsCancellationRequested) + return; - if (this.localizedContent.ContainsKey(keyValuePair.Key)) - continue; + // + // Now, we have localized the added content. Next, we must merge + // the localized content with the existing content. However, we + // must skip the removed content. We use the localizedContent + // dictionary for the final result: + // + foreach (var keyValuePair in selectedLanguageContentSnapshot) + { + if (this.CancellationTokenSource!.IsCancellationRequested) + break; + + if (this.localizedContent.ContainsKey(keyValuePair.Key)) + continue; + + if (removedContentKeys.Contains(keyValuePair.Key)) + continue; + + this.localizedContent.Add(keyValuePair.Key, keyValuePair.Value); + } + + if(this.CancellationTokenSource!.IsCancellationRequested) + return; - if (this.removedContent.ContainsKey(keyValuePair.Key)) - continue; + // + // Phase 2: Create the Lua code. We want to use the base language + // for the comments, though: + // + var commentContent = addedContentSnapshot.ToDictionary(keyValuePair => keyValuePair.Key, keyValuePair => keyValuePair.Value, StringComparer.Ordinal); + foreach (var keyValuePair in baseLanguageContentSnapshot) + { + if (this.CancellationTokenSource!.IsCancellationRequested) + break; + + if (removedContentKeys.Contains(keyValuePair.Key)) + continue; + + commentContent.TryAdd(keyValuePair.Key, keyValuePair.Value); + } - this.localizedContent.Add(keyValuePair.Key, keyValuePair.Value); + this.Phase2CreateLuaCode(commentContent); } - - if(this.CancellationTokenSource!.IsCancellationRequested) - return; - - // - // Phase 2: Create the Lua code. We want to use the base language - // for the comments, though: - // - var commentContent = new Dictionary(this.addedContent); - foreach (var keyValuePair in PluginFactory.BaseLanguage.Content) + finally { - if (this.CancellationTokenSource!.IsCancellationRequested) - break; - - if (this.removedContent.ContainsKey(keyValuePair.Key)) - continue; - - commentContent.TryAdd(keyValuePair.Key, keyValuePair.Value); + this.activeSystemPromptLanguage = null; } - - this.Phase2CreateLuaCode(commentContent); } - private async Task Phase1TranslateAddedContent() + /// + /// Translates the added text content from a stable snapshot. + /// + /// The added text entries captured when the job started. + /// A task that completes when all added text entries were translated or cancellation was requested. + private async Task Phase1TranslateAddedContent(KeyValuePair[] addedContentSnapshot) { var stopwatch = new Stopwatch(); var minimumTime = TimeSpan.FromMilliseconds(500); - foreach (var keyValuePair in this.addedContent) + foreach (var keyValuePair in addedContentSnapshot) { if(this.CancellationTokenSource!.IsCancellationRequested) break; diff --git a/app/MindWork AI Studio/Components/AssistantBlock.razor.cs b/app/MindWork AI Studio/Components/AssistantBlock.razor.cs index 267607fd..735b2974 100644 --- a/app/MindWork AI Studio/Components/AssistantBlock.razor.cs +++ b/app/MindWork AI Studio/Components/AssistantBlock.razor.cs @@ -93,7 +93,7 @@ public partial class AssistantBlock : MSGComponentBase where TSetting private AssistantSessionIndicatorData? AssistantSessionIndicator => this.AssistantSessionSnapshot?.Status switch { AssistantSessionStatus.RUNNING or AssistantSessionStatus.CANCELING => new(Icons.Material.Filled.ChangeCircle, Color.Info, this.T("Assistant is still running.")), - AssistantSessionStatus.COMPLETED => new(Icons.Material.Filled.TaskAlt, Color.Success, this.T("Result is ready.")), + AssistantSessionStatus.COMPLETED => new(Icons.Material.Filled.TaskAlt, Color.Success, this.T("The result is ready.")), AssistantSessionStatus.FAILED => new(Icons.Material.Filled.Error, Color.Error, this.T("Assistant failed. Open it to review the result.")), AssistantSessionStatus.CANCELED => new(Icons.Material.Filled.Cancel, Color.Warning, this.T("Assistant was canceled. Open it to review the result.")), _ => null, diff --git a/app/MindWork AI Studio/Components/EnumSelection.razor b/app/MindWork AI Studio/Components/EnumSelection.razor index bd5bc08a..db25cae2 100644 --- a/app/MindWork AI Studio/Components/EnumSelection.razor +++ b/app/MindWork AI Studio/Components/EnumSelection.razor @@ -2,7 +2,7 @@ @inherits EnumSelectionBase - + @foreach (var value in Enum.GetValues()) { @@ -12,6 +12,6 @@ @if (this.AllowOther && this.Value.Equals(this.OtherValue)) { - + } \ No newline at end of file diff --git a/app/MindWork AI Studio/Components/EnumSelection.razor.cs b/app/MindWork AI Studio/Components/EnumSelection.razor.cs index a7d93dd9..0429c738 100644 --- a/app/MindWork AI Studio/Components/EnumSelection.razor.cs +++ b/app/MindWork AI Studio/Components/EnumSelection.razor.cs @@ -38,6 +38,12 @@ public partial class EnumSelection : EnumSelectionBase where T : struct, Enum [Parameter] public string Icon { get; set; } = Icons.Material.Filled.ArrowDropDown; + + /// + /// Gets or sets whether the selection controls are disabled. + /// + [Parameter] + public bool Disabled { get; set; } /// /// Gets or sets the custom name function for selecting the display name of an enum value.