@@ -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 ( ) ) ;
0 commit comments