@@ -177,6 +177,7 @@ export class NotebookChatController extends Disposable implements INotebookEdito
177
177
private _sessionCtor : CancelablePromise < void > | undefined ;
178
178
private _activeSession ?: Session ;
179
179
private _warmupRequestCts ?: CancellationTokenSource ;
180
+ private _activeRequestCts ?: CancellationTokenSource ;
180
181
private readonly _ctxHasActiveRequest : IContextKey < boolean > ;
181
182
private readonly _ctxCellWidgetFocused : IContextKey < boolean > ;
182
183
private readonly _ctxUserDidEdit : IContextKey < boolean > ;
@@ -397,8 +398,9 @@ export class NotebookChatController extends Disposable implements INotebookEdito
397
398
}
398
399
399
400
async acceptInput ( ) {
400
- assertType ( this . _activeSession ) ;
401
401
assertType ( this . _widget ) ;
402
+ await this . _sessionCtor ;
403
+ assertType ( this . _activeSession ) ;
402
404
this . _warmupRequestCts ?. dispose ( true ) ;
403
405
this . _warmupRequestCts = undefined ;
404
406
this . _activeSession . addInput ( new SessionPrompt ( this . _widget . inlineChatWidget . value ) ) ;
@@ -449,18 +451,19 @@ export class NotebookChatController extends Disposable implements INotebookEdito
449
451
450
452
//TODO: update progress in a newly inserted cell below the widget instead of the fake editor
451
453
452
- const requestCts = new CancellationTokenSource ( ) ;
454
+ this . _activeRequestCts ?. cancel ( ) ;
455
+ this . _activeRequestCts = new CancellationTokenSource ( ) ;
453
456
const progressEdits : TextEdit [ ] [ ] = [ ] ;
454
457
455
458
const progressiveEditsQueue = new Queue ( ) ;
456
459
const progressiveEditsClock = StopWatch . create ( ) ;
457
460
const progressiveEditsAvgDuration = new MovingAverage ( ) ;
458
- const progressiveEditsCts = new CancellationTokenSource ( requestCts . token ) ;
461
+ const progressiveEditsCts = new CancellationTokenSource ( this . _activeRequestCts . token ) ;
459
462
let progressiveChatResponse : IInlineChatMessageAppender | undefined ;
460
463
const progress = new AsyncProgress < IInlineChatProgressItem > ( async data => {
461
464
// console.log('received chunk', data, request);
462
465
463
- if ( requestCts . token . isCancellationRequested ) {
466
+ if ( this . _activeRequestCts ? .token . isCancellationRequested ) {
464
467
return ;
465
468
}
466
469
@@ -502,7 +505,7 @@ export class NotebookChatController extends Disposable implements INotebookEdito
502
505
}
503
506
} ) ;
504
507
505
- const task = this . _activeSession . provider . provideResponse ( this . _activeSession . session , request , progress , requestCts . token ) ;
508
+ const task = this . _activeSession . provider . provideResponse ( this . _activeSession . session , request , progress , this . _activeRequestCts . token ) ;
506
509
let response : ReplyResponse | ErrorResponse | EmptyResponse ;
507
510
508
511
try {
@@ -512,7 +515,7 @@ export class NotebookChatController extends Disposable implements INotebookEdito
512
515
this . _widget ?. inlineChatWidget . updateInfo ( ! this . _activeSession . lastExchange ? localize ( 'thinking' , "Thinking\u2026" ) : '' ) ;
513
516
this . _ctxHasActiveRequest . set ( true ) ;
514
517
515
- const reply = await raceCancellationError ( Promise . resolve ( task ) , requestCts . token ) ;
518
+ const reply = await raceCancellationError ( Promise . resolve ( task ) , this . _activeRequestCts . token ) ;
516
519
if ( progressiveEditsQueue . size > 0 ) {
517
520
// we must wait for all edits that came in via progress to complete
518
521
await Event . toPromise ( progressiveEditsQueue . onDrained ) ;
@@ -575,7 +578,7 @@ export class NotebookChatController extends Disposable implements INotebookEdito
575
578
this . _widget ?. inlineChatWidget . updateInfo ( '' ) ;
576
579
this . _widget ?. inlineChatWidget . updateToolbar ( true ) ;
577
580
578
- this . _activeSession . addExchange ( new SessionExchange ( this . _activeSession . lastInput , response ) ) ;
581
+ this . _activeSession ? .addExchange ( new SessionExchange ( this . _activeSession . lastInput , response ) ) ;
579
582
this . _ctxLastResponseType . set ( response instanceof ReplyResponse ? response . raw . type : undefined ) ;
580
583
}
581
584
@@ -762,15 +765,12 @@ export class NotebookChatController extends Disposable implements INotebookEdito
762
765
this . _strategy ?. cancel ( ) ;
763
766
}
764
767
765
- if ( this . _activeSession ) {
766
- this . _inlineChatSessionService . releaseSession ( this . _activeSession ) ;
767
- }
768
-
769
- this . _activeSession = undefined ;
768
+ this . _activeRequestCts ?. cancel ( ) ;
770
769
}
771
770
772
771
discard ( ) {
773
772
this . _strategy ?. cancel ( ) ;
773
+ this . _activeRequestCts ?. cancel ( ) ;
774
774
this . _widget ?. discardChange ( ) ;
775
775
this . dismiss ( ) ;
776
776
}
0 commit comments