6
6
import { renderMarkdown } from 'vs/base/browser/markdownRenderer' ;
7
7
import { Barrier , raceCancellationError } from 'vs/base/common/async' ;
8
8
import { CancellationTokenSource } from 'vs/base/common/cancellation' ;
9
+ import { toErrorMessage } from 'vs/base/common/errorMessage' ;
9
10
import { Emitter , Event } from 'vs/base/common/event' ;
10
11
import { DisposableStore , toDisposable } from 'vs/base/common/lifecycle' ;
11
12
import { isEqual } from 'vs/base/common/resources' ;
@@ -24,6 +25,7 @@ import { InlineCompletionsController } from 'vs/editor/contrib/inlineCompletions
24
25
import { localize } from 'vs/nls' ;
25
26
import { IConfigurationService } from 'vs/platform/configuration/common/configuration' ;
26
27
import { IContextKey , IContextKeyService } from 'vs/platform/contextkey/common/contextkey' ;
28
+ import { IDialogService } from 'vs/platform/dialogs/common/dialogs' ;
27
29
import { IInstantiationService , ServicesAccessor } from 'vs/platform/instantiation/common/instantiation' ;
28
30
import { ILogService } from 'vs/platform/log/common/log' ;
29
31
import { EditResponse , EmptyResponse , ErrorResponse , IInteractiveEditorSessionService , MarkdownResponse , Session , SessionExchange } from 'vs/workbench/contrib/interactiveEditor/browser/interactiveEditorSession' ;
@@ -36,23 +38,23 @@ import { INotebookEditorService } from 'vs/workbench/contrib/notebook/browser/se
36
38
import { CellUri } from 'vs/workbench/contrib/notebook/common/notebookCommon' ;
37
39
38
40
const enum State {
39
- CREATE_SESSION ,
40
- INIT_UI ,
41
- WAIT_FOR_INPUT ,
42
- MAKE_REQUEST ,
43
- APPLY_RESPONSE ,
44
- SHOW_RESPONSE ,
45
- PAUSE ,
46
- DONE ,
41
+ CREATE_SESSION = 'CREATE_SESSION' ,
42
+ INIT_UI = 'INIT_UI' ,
43
+ WAIT_FOR_INPUT = 'WAIT_FOR_INPUT' ,
44
+ MAKE_REQUEST = 'MAKE_REQUEST' ,
45
+ APPLY_RESPONSE = 'APPLY_RESPONSE' ,
46
+ SHOW_RESPONSE = 'SHOW_RESPONSE' ,
47
+ PAUSE = 'PAUSE' ,
48
+ DONE = 'DONE' ,
47
49
}
48
50
49
51
const enum Message {
50
52
NONE = 0 ,
51
- END_SESSION = 2 ** 0 ,
52
- PAUSE_SESSION = 2 ** 1 ,
53
- CANCEL_REQUEST = 2 ** 2 ,
54
- CANCEL_INPUT = 2 ** 3 ,
55
- ACCEPT_INPUT = 2 ** 4
53
+ END_SESSION = 1 << 0 ,
54
+ PAUSE_SESSION = 1 << 1 ,
55
+ CANCEL_REQUEST = 1 << 2 ,
56
+ CANCEL_INPUT = 1 << 3 ,
57
+ ACCEPT_INPUT = 1 << 4
56
58
}
57
59
58
60
export interface InteractiveEditorRunOptions {
@@ -101,6 +103,7 @@ export class InteractiveEditorController implements IEditorContribution {
101
103
@IConfigurationService private readonly _configurationService : IConfigurationService ,
102
104
@IModelService private readonly _modelService : IModelService ,
103
105
@INotebookEditorService private readonly _notebookEditorService : INotebookEditorService ,
106
+ @IDialogService private readonly _dialogService : IDialogService ,
104
107
@IContextKeyService contextKeyService : IContextKeyService ,
105
108
) {
106
109
this . _ctxHasActiveRequest = CTX_INTERACTIVE_EDITOR_HAS_ACTIVE_REQUEST . bindTo ( contextKeyService ) ;
@@ -148,39 +151,13 @@ export class InteractiveEditorController implements IEditorContribution {
148
151
149
152
private async _nextState ( state : State , options : InteractiveEditorRunOptions | undefined ) : Promise < void > {
150
153
this . _logService . trace ( '[IE] setState to ' , state ) ;
151
- let nextState : State | undefined ;
152
- switch ( state ) {
153
- case State . CREATE_SESSION :
154
- nextState = await this . _createSession ( options ) ;
155
- break ;
156
- case State . INIT_UI :
157
- nextState = await this . _initUI ( ) ;
158
- break ;
159
- case State . WAIT_FOR_INPUT :
160
- nextState = await this . _waitForInput ( options ) ;
161
- break ;
162
- case State . MAKE_REQUEST :
163
- nextState = await this . _makeRequest ( ) ;
164
- break ;
165
- case State . APPLY_RESPONSE :
166
- nextState = await this . _applyResponse ( ) ;
167
- break ;
168
- case State . SHOW_RESPONSE :
169
- nextState = await this . _showResponse ( ) ;
170
- break ;
171
- case State . PAUSE :
172
- this . _pause ( ) ;
173
- break ;
174
- case State . DONE :
175
- this . _done ( ) ;
176
- break ;
177
- }
154
+ const nextState = await this [ state ] ( options ) ;
178
155
if ( nextState ) {
179
156
this . _nextState ( nextState , options ) ;
180
157
}
181
158
}
182
159
183
- private async _createSession ( options : InteractiveEditorRunOptions | undefined ) : Promise < State . DONE | State . INIT_UI > {
160
+ private async [ State . CREATE_SESSION ] ( options : InteractiveEditorRunOptions | undefined ) : Promise < State . DONE | State . INIT_UI > {
184
161
assertType ( this . _editor . hasModel ( ) ) ;
185
162
186
163
let session : Session | undefined = options ?. existingSession ;
@@ -206,6 +183,7 @@ export class InteractiveEditorController implements IEditorContribution {
206
183
delete options ?. existingSession ;
207
184
208
185
if ( ! session ) {
186
+ this . _dialogService . info ( localize ( 'create.fail' , "Failed to start editor chat" ) , localize ( 'create.fail.detail' , "Please consult the error log and try again later." ) ) ;
209
187
return State . DONE ;
210
188
}
211
189
@@ -225,7 +203,7 @@ export class InteractiveEditorController implements IEditorContribution {
225
203
return State . INIT_UI ;
226
204
}
227
205
228
- private async _initUI ( ) : Promise < State . WAIT_FOR_INPUT | State . SHOW_RESPONSE > {
206
+ private async [ State . INIT_UI ] ( ) : Promise < State . WAIT_FOR_INPUT | State . SHOW_RESPONSE > {
229
207
assertType ( this . _activeSession ) ;
230
208
231
209
// hide/cancel inline completions when invoking IE
@@ -296,7 +274,7 @@ export class InteractiveEditorController implements IEditorContribution {
296
274
}
297
275
}
298
276
299
- private async _waitForInput ( options : InteractiveEditorRunOptions | undefined ) : Promise < State . DONE | State . PAUSE | State . WAIT_FOR_INPUT | State . MAKE_REQUEST > {
277
+ private async [ State . WAIT_FOR_INPUT ] ( options : InteractiveEditorRunOptions | undefined ) : Promise < State . DONE | State . PAUSE | State . WAIT_FOR_INPUT | State . MAKE_REQUEST > {
300
278
assertType ( this . _activeSession ) ;
301
279
302
280
this . _zone . show ( this . _activeSession . wholeRange . getEndPosition ( ) ) ;
@@ -360,7 +338,7 @@ export class InteractiveEditorController implements IEditorContribution {
360
338
return State . MAKE_REQUEST ;
361
339
}
362
340
363
- private async _makeRequest ( ) : Promise < State . APPLY_RESPONSE | State . PAUSE | State . DONE > {
341
+ private async [ State . MAKE_REQUEST ] ( ) : Promise < State . APPLY_RESPONSE | State . PAUSE | State . DONE > {
364
342
assertType ( this . _editor . hasModel ( ) ) ;
365
343
assertType ( this . _activeSession ) ;
366
344
assertType ( this . _activeSession . lastInput ) ;
@@ -427,7 +405,7 @@ export class InteractiveEditorController implements IEditorContribution {
427
405
}
428
406
}
429
407
430
- private async _applyResponse ( ) : Promise < State . SHOW_RESPONSE | State . DONE > {
408
+ private async [ State . APPLY_RESPONSE ] ( ) : Promise < State . SHOW_RESPONSE | State . DONE > {
431
409
assertType ( this . _activeSession ) ;
432
410
assertType ( this . _strategy ) ;
433
411
@@ -461,7 +439,7 @@ export class InteractiveEditorController implements IEditorContribution {
461
439
return State . SHOW_RESPONSE ;
462
440
}
463
441
464
- private async _showResponse ( ) : Promise < State . WAIT_FOR_INPUT | State . DONE > {
442
+ private async [ State . SHOW_RESPONSE ] ( ) : Promise < State . WAIT_FOR_INPUT | State . DONE > {
465
443
assertType ( this . _activeSession ) ;
466
444
assertType ( this . _strategy ) ;
467
445
@@ -510,7 +488,7 @@ export class InteractiveEditorController implements IEditorContribution {
510
488
return State . WAIT_FOR_INPUT ;
511
489
}
512
490
513
- private async _pause ( ) {
491
+ private async [ State . PAUSE ] ( ) {
514
492
assertType ( this . _activeSession ) ;
515
493
516
494
this . _ctxLastEditKind . reset ( ) ;
@@ -528,10 +506,10 @@ export class InteractiveEditorController implements IEditorContribution {
528
506
this . _activeSession = undefined ;
529
507
}
530
508
531
- private async _done ( ) {
509
+ private async [ State . DONE ] ( ) {
532
510
assertType ( this . _activeSession ) ;
533
511
this . _interactiveEditorSessionService . releaseSession ( this . _activeSession ) ;
534
- this . _pause ( ) ;
512
+ this [ State . PAUSE ] ( ) ;
535
513
}
536
514
537
515
// ---- controller API
@@ -612,7 +590,13 @@ export class InteractiveEditorController implements IEditorContribution {
612
590
if ( this . _strategy ) {
613
591
const strategy = this . _strategy ;
614
592
this . _strategy = undefined ;
615
- await strategy ?. apply ( ) ;
593
+ try {
594
+ await strategy ?. apply ( ) ;
595
+ } catch ( err ) {
596
+ this . _dialogService . error ( localize ( 'err.apply' , "Failed to apply changes." , toErrorMessage ( err ) ) ) ;
597
+ this . _logService . error ( '[IE] FAILED to apply changes' ) ;
598
+ this . _logService . error ( err ) ;
599
+ }
616
600
strategy ?. dispose ( ) ;
617
601
this . _messages . fire ( Message . END_SESSION ) ;
618
602
@@ -626,7 +610,13 @@ export class InteractiveEditorController implements IEditorContribution {
626
610
if ( this . _strategy ) {
627
611
const strategy = this . _strategy ;
628
612
this . _strategy = undefined ;
629
- await strategy ?. cancel ( ) ;
613
+ try {
614
+ await strategy ?. cancel ( ) ;
615
+ } catch ( err ) {
616
+ this . _dialogService . error ( localize ( 'err.discard' , "Failed to discard changes." , toErrorMessage ( err ) ) ) ;
617
+ this . _logService . error ( '[IE] FAILED to discard changes' ) ;
618
+ this . _logService . error ( err ) ;
619
+ }
630
620
strategy ?. dispose ( ) ;
631
621
this . _messages . fire ( Message . END_SESSION ) ;
632
622
}
0 commit comments