@@ -171,7 +171,7 @@ export class InlineChatController implements IEditorContribution {
171
171
return this . _zone . value . position ;
172
172
}
173
173
174
- async run ( options : InlineChatRunOptions | undefined ) : Promise < void > {
174
+ async run ( options : InlineChatRunOptions | undefined = { } ) : Promise < void > {
175
175
this . _log ( 'session starting' ) ;
176
176
await this . _finishExistingSession ( ) ;
177
177
this . _stashedSession . clear ( ) ;
@@ -195,15 +195,15 @@ export class InlineChatController implements IEditorContribution {
195
195
// ---- state machine
196
196
197
197
private _showWidget ( initialRender : boolean = false ) {
198
- assertType ( this . _activeSession ) ;
199
- assertType ( this . _strategy ) ;
200
198
assertType ( this . _editor . hasModel ( ) ) ;
201
199
202
- let widgetPosition : Position | undefined ;
200
+ let widgetPosition : Position ;
203
201
if ( initialRender ) {
204
202
widgetPosition = this . _editor . getSelection ( ) . getEndPosition ( ) ;
205
203
this . _zone . value . setMargins ( widgetPosition ) ;
206
204
} else {
205
+ assertType ( this . _activeSession ) ;
206
+ assertType ( this . _strategy ) ;
207
207
widgetPosition = this . _strategy . getWidgetPosition ( ) ?? this . _zone . value . position ?? this . _activeSession . wholeRange . value . getEndPosition ( ) ;
208
208
const needsMargin = this . _strategy . needsMargin ( ) ;
209
209
if ( ! needsMargin ) {
@@ -213,39 +213,50 @@ export class InlineChatController implements IEditorContribution {
213
213
this . _zone . value . show ( widgetPosition ) ;
214
214
}
215
215
216
- protected async _nextState ( state : State , options : InlineChatRunOptions | undefined ) : Promise < void > {
217
- this . _log ( 'setState to ' , state ) ;
218
- const nextState = await this [ state ] ( options ) ;
219
- if ( nextState ) {
220
- await this . _nextState ( nextState , options ) ;
216
+ protected async _nextState ( state : State , options : InlineChatRunOptions ) : Promise < void > {
217
+ let nextState : State | void = state ;
218
+ while ( nextState ) {
219
+ this . _log ( 'setState to ' , nextState ) ;
220
+ nextState = await this [ nextState ] ( options ) ;
221
221
}
222
222
}
223
223
224
- private async [ State . CREATE_SESSION ] ( options : InlineChatRunOptions | undefined ) : Promise < State . CANCEL | State . INIT_UI > {
224
+ private async [ State . CREATE_SESSION ] ( options : InlineChatRunOptions ) : Promise < State . CANCEL | State . INIT_UI > {
225
225
assertType ( this . _activeSession === undefined ) ;
226
226
assertType ( this . _editor . hasModel ( ) ) ;
227
227
228
- let session : Session | undefined = options ?. existingSession ;
228
+ let session : Session | undefined = options . existingSession ;
229
+
230
+ this . _showWidget ( true ) ;
231
+ this . _zone . value . widget . updateInfo ( localize ( 'welcome.1' , "AI-generated code may be incorrect" ) ) ;
232
+ this . _zone . value . widget . placeholder = this . _getPlaceholderText ( ) ;
229
233
230
234
if ( ! session ) {
231
235
const createSessionCts = new CancellationTokenSource ( ) ;
232
236
const msgListener = Event . once ( this . _messages . event ) ( m => {
233
237
this . _log ( 'state=_createSession) message received' , m ) ;
234
- createSessionCts . cancel ( ) ;
238
+ if ( m === Message . ACCEPT_INPUT ) {
239
+ // user accepted the input before having a session
240
+ options . autoSend = true ;
241
+ this . _zone . value . widget . updateProgress ( true ) ;
242
+ this . _zone . value . widget . updateInfo ( localize ( 'welcome.2' , "Getting ready..." ) ) ;
243
+ } else {
244
+ createSessionCts . cancel ( ) ;
245
+ }
235
246
} ) ;
236
247
237
248
session = await this . _inlineChatSessionService . createSession (
238
249
this . _editor ,
239
- { editMode : this . _getMode ( ) , wholeRange : options ? .initialRange } ,
250
+ { editMode : this . _getMode ( ) , wholeRange : options . initialRange } ,
240
251
createSessionCts . token
241
252
) ;
242
253
243
254
createSessionCts . dispose ( ) ;
244
255
msgListener . dispose ( ) ;
245
256
}
246
257
247
- delete options ? .initialRange ;
248
- delete options ? .existingSession ;
258
+ delete options . initialRange ;
259
+ delete options . existingSession ;
249
260
250
261
if ( ! session ) {
251
262
this . _dialogService . info ( localize ( 'create.fail' , "Failed to start editor chat" ) , localize ( 'create.fail.detail' , "Please consult the error log and try again later." ) ) ;
@@ -269,7 +280,7 @@ export class InlineChatController implements IEditorContribution {
269
280
return State . INIT_UI ;
270
281
}
271
282
272
- private async [ State . INIT_UI ] ( options : InlineChatRunOptions | undefined ) : Promise < State . WAIT_FOR_INPUT | State . SHOW_RESPONSE | State . APPLY_RESPONSE > {
283
+ private async [ State . INIT_UI ] ( options : InlineChatRunOptions ) : Promise < State . WAIT_FOR_INPUT | State . SHOW_RESPONSE | State . APPLY_RESPONSE > {
273
284
assertType ( this . _activeSession ) ;
274
285
275
286
// hide/cancel inline completions when invoking IE
@@ -287,10 +298,9 @@ export class InlineChatController implements IEditorContribution {
287
298
288
299
this . _zone . value . widget . updateSlashCommands ( this . _activeSession . session . slashCommands ?? [ ] ) ;
289
300
this . _zone . value . widget . placeholder = this . _getPlaceholderText ( ) ;
290
- this . _zone . value . widget . value = this . _activeSession . lastInput ?. value ?? '' ;
301
+ this . _zone . value . widget . value = this . _activeSession . lastInput ?. value ?? this . _zone . value . widget . value ;
291
302
this . _zone . value . widget . updateInfo ( this . _activeSession . session . message ?? localize ( 'welcome.1' , "AI-generated code may be incorrect" ) ) ;
292
303
this . _zone . value . widget . preferredExpansionState = this . _activeSession . lastExpansionState ;
293
- this . _showWidget ( true ) ;
294
304
295
305
this . _sessionStore . add ( this . _editor . onDidChangeModel ( ( e ) => {
296
306
const msg = this . _activeSession ?. lastExchange
@@ -321,7 +331,7 @@ export class InlineChatController implements IEditorContribution {
321
331
322
332
if ( ! this . _activeSession . lastExchange ) {
323
333
return State . WAIT_FOR_INPUT ;
324
- } else if ( options ? .isUnstashed ) {
334
+ } else if ( options . isUnstashed ) {
325
335
delete options . isUnstashed ;
326
336
return State . APPLY_RESPONSE ;
327
337
} else {
@@ -330,10 +340,7 @@ export class InlineChatController implements IEditorContribution {
330
340
}
331
341
332
342
private _getPlaceholderText ( ) : string {
333
- if ( ! this . _activeSession ) {
334
- return '' ;
335
- }
336
- let result = this . _activeSession . session . placeholder ?? localize ( 'default.placeholder' , "Ask a question" ) ;
343
+ let result = this . _activeSession ?. session . placeholder ?? localize ( 'default.placeholder' , "Ask a question" ) ;
337
344
if ( InlineChatController . _promptHistory . length > 0 ) {
338
345
const kb1 = this . _keybindingService . lookupKeybinding ( 'inlineChat.previousFromHistory' ) ?. getLabel ( ) ;
339
346
const kb2 = this . _keybindingService . lookupKeybinding ( 'inlineChat.nextFromHistory' ) ?. getLabel ( ) ;
@@ -375,22 +382,22 @@ export class InlineChatController implements IEditorContribution {
375
382
}
376
383
}
377
384
378
- private async [ State . WAIT_FOR_INPUT ] ( options : InlineChatRunOptions | undefined ) : Promise < State . ACCEPT | State . CANCEL | State . PAUSE | State . WAIT_FOR_INPUT | State . MAKE_REQUEST > {
385
+ private async [ State . WAIT_FOR_INPUT ] ( options : InlineChatRunOptions ) : Promise < State . ACCEPT | State . CANCEL | State . PAUSE | State . WAIT_FOR_INPUT | State . MAKE_REQUEST > {
379
386
assertType ( this . _activeSession ) ;
380
387
assertType ( this . _strategy ) ;
381
388
382
389
this . _zone . value . widget . placeholder = this . _getPlaceholderText ( ) ;
383
390
384
- if ( options ? .message ) {
385
- this . _zone . value . widget . value = options ? .message ;
391
+ if ( options . message ) {
392
+ this . _zone . value . widget . value = options . message ;
386
393
this . _zone . value . widget . selectAll ( ) ;
387
- delete options ? .message ;
394
+ delete options . message ;
388
395
}
389
396
390
397
let message = Message . NONE ;
391
- if ( options ? .autoSend ) {
398
+ if ( options . autoSend ) {
392
399
message = Message . ACCEPT_INPUT ;
393
- delete options ? .autoSend ;
400
+ delete options . autoSend ;
394
401
395
402
} else {
396
403
const barrier = new Barrier ( ) ;
0 commit comments