Skip to content

Commit c67f17c

Browse files
Added a "stop generation" button to assistants (#365)
1 parent b948909 commit c67f17c

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

app/MindWork AI Studio/Assistants/AssistantBase.razor

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,17 @@
2828
@this.Body
2929
</CascadingValue>
3030

31-
<MudButton Disabled="@this.SubmitDisabled" Variant="Variant.Filled" Class="mb-3" OnClick="() => this.SubmitAction()" Style="@this.SubmitButtonStyle">
32-
@this.SubmitText
33-
</MudButton>
34-
31+
<MudStack Row="true" AlignItems="AlignItems.Center" StretchItems="StretchItems.Start" Class="mb-3">
32+
<MudButton Disabled="@this.SubmitDisabled" Variant="Variant.Filled" OnClick="() => this.SubmitAction()" Style="@this.SubmitButtonStyle">
33+
@this.SubmitText
34+
</MudButton>
35+
@if (this.isProcessing && this.cancellationTokenSource is not null)
36+
{
37+
<MudTooltip Text="Stop generation">
38+
<MudIconButton Variant="Variant.Filled" Icon="@Icons.Material.Filled.Stop" Color="Color.Error" OnClick="() => this.CancelStreaming()"/>
39+
</MudTooltip>
40+
}
41+
</MudStack>
3542
}
3643
</MudForm>
3744
<Issues IssuesData="@(this.inputIssues)"/>

app/MindWork AI Studio/Assistants/AssistantBase.razor.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ public abstract partial class AssistantBase<TSettings> : AssistantLowerBase, IMe
100100

101101
private readonly Timer formChangeTimer = new(TimeSpan.FromSeconds(1.6));
102102

103+
private CancellationTokenSource? cancellationTokenSource;
103104
private ContentBlock? resultingContentBlock;
104105
private string[] inputIssues = [];
105106
private bool isProcessing;
@@ -285,19 +286,30 @@ protected async Task<string> AddAIResponseAsync(DateTimeOffset time, bool hideCo
285286

286287
this.isProcessing = true;
287288
this.StateHasChanged();
288-
289-
// Use the selected provider to get the AI response.
290-
// By awaiting this line, we wait for the entire
291-
// content to be streamed.
292-
this.chatThread = await aiText.CreateFromProviderAsync(this.providerSettings.CreateProvider(this.Logger), this.providerSettings.Model, this.lastUserPrompt, this.chatThread);
293-
289+
290+
using (this.cancellationTokenSource = new())
291+
{
292+
// Use the selected provider to get the AI response.
293+
// By awaiting this line, we wait for the entire
294+
// content to be streamed.
295+
this.chatThread = await aiText.CreateFromProviderAsync(this.providerSettings.CreateProvider(this.Logger), this.providerSettings.Model, this.lastUserPrompt, this.chatThread, this.cancellationTokenSource.Token);
296+
}
297+
298+
this.cancellationTokenSource = null;
294299
this.isProcessing = false;
295300
this.StateHasChanged();
296301

297302
// Return the AI response:
298303
return aiText.Text;
299304
}
300305

306+
private async Task CancelStreaming()
307+
{
308+
if (this.cancellationTokenSource is not null)
309+
if(!this.cancellationTokenSource.IsCancellationRequested)
310+
await this.cancellationTokenSource.CancelAsync();
311+
}
312+
301313
protected async Task CopyToClipboard()
302314
{
303315
await this.RustService.CopyText2Clipboard(this.Snackbar, this.Result2Copy());
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# v0.9.38, build 213 (2025-03-xx xx:xx UTC)
2+
- Added the "stop generation" button to all assistants.
23
- Updated the ERI v1 specification for the ERI server assistant & fixed spelling of the `UNKNOWN` role.

0 commit comments

Comments
 (0)