Refactor button click handling to use async Start method for improved cancellation support

This commit is contained in:
Thorsten Sommer 2025-04-26 18:17:57 +02:00
parent 125c3c1c66
commit fdfea1501c
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
2 changed files with 19 additions and 13 deletions

View File

@ -29,7 +29,7 @@
</CascadingValue> </CascadingValue>
<MudStack Row="true" AlignItems="AlignItems.Center" StretchItems="StretchItems.Start" Class="mb-3"> <MudStack Row="true" AlignItems="AlignItems.Center" StretchItems="StretchItems.Start" Class="mb-3">
<MudButton Disabled="@this.SubmitDisabled" Variant="Variant.Filled" OnClick="() => this.SubmitAction()" Style="@this.SubmitButtonStyle"> <MudButton Disabled="@this.SubmitDisabled" Variant="Variant.Filled" OnClick="async () => await this.Start()" Style="@this.SubmitButtonStyle">
@this.SubmitText @this.SubmitText
</MudButton> </MudButton>
@if (this.isProcessing && this.cancellationTokenSource is not null) @if (this.isProcessing && this.cancellationTokenSource is not null)

View File

@ -97,10 +97,10 @@ public abstract partial class AssistantBase<TSettings> : AssistantLowerBase, IMe
protected Profile currentProfile = Profile.NO_PROFILE; protected Profile currentProfile = Profile.NO_PROFILE;
protected ChatThread? chatThread; protected ChatThread? chatThread;
protected IContent? lastUserPrompt; protected IContent? lastUserPrompt;
protected CancellationTokenSource? cancellationTokenSource;
private readonly Timer formChangeTimer = new(TimeSpan.FromSeconds(1.6)); private readonly Timer formChangeTimer = new(TimeSpan.FromSeconds(1.6));
private CancellationTokenSource? cancellationTokenSource;
private ContentBlock? resultingContentBlock; private ContentBlock? resultingContentBlock;
private string[] inputIssues = []; private string[] inputIssues = [];
private bool isProcessing; private bool isProcessing;
@ -179,6 +179,16 @@ public abstract partial class AssistantBase<TSettings> : AssistantLowerBase, IMe
return null; return null;
} }
private async Task Start()
{
using (this.cancellationTokenSource = new())
{
await this.SubmitAction();
}
this.cancellationTokenSource = null;
}
private void TriggerFormChange(FormFieldChangedEventArgs _) private void TriggerFormChange(FormFieldChangedEventArgs _)
{ {
this.formChangeTimer.Stop(); this.formChangeTimer.Stop();
@ -286,16 +296,12 @@ public abstract partial class AssistantBase<TSettings> : AssistantLowerBase, IMe
this.isProcessing = true; this.isProcessing = true;
this.StateHasChanged(); this.StateHasChanged();
using (this.cancellationTokenSource = new()) // Use the selected provider to get the AI response.
{ // By awaiting this line, we wait for the entire
// Use the selected provider to get the AI response. // content to be streamed.
// By awaiting this line, we wait for the entire this.chatThread = await aiText.CreateFromProviderAsync(this.providerSettings.CreateProvider(this.Logger), this.providerSettings.Model, this.lastUserPrompt, this.chatThread, this.cancellationTokenSource!.Token);
// content to be streamed.
this.chatThread = await aiText.CreateFromProviderAsync(this.providerSettings.CreateProvider(this.Logger), this.providerSettings.Model, this.lastUserPrompt, this.chatThread, this.cancellationTokenSource.Token);
}
this.cancellationTokenSource = null;
this.isProcessing = false; this.isProcessing = false;
this.StateHasChanged(); this.StateHasChanged();