@@ -274,40 +274,15 @@ export class RunInTerminalTool extends Disposable implements IToolImpl {
274
274
}
275
275
276
276
let error : string | undefined ;
277
+ const isNewSession = ! args . isBackground && ! this . _sessionTerminalAssociations . has ( chatSessionId ) ;
277
278
278
279
const timingStart = Date . now ( ) ;
279
280
const termId = generateUuid ( ) ;
280
281
281
- let isNewSession = false ;
282
- let toolTerminal : IToolTerminal ;
283
282
const store = new DisposableStore ( ) ;
284
283
285
- if ( args . isBackground ) {
286
- this . _logService . debug ( `RunInTerminalTool: Creating background terminal with ID=${ termId } ` ) ;
287
- toolTerminal = await this . _instantiationService . createInstance ( ToolTerminalCreator ) . createTerminal ( token ) ;
288
- this . _sessionTerminalAssociations . set ( chatSessionId , toolTerminal ) ;
289
- if ( token . isCancellationRequested ) {
290
- toolTerminal . instance . dispose ( ) ;
291
- throw new CancellationError ( ) ;
292
- }
293
- await this . _setupTerminalAssociation ( toolTerminal , chatSessionId , termId , args . isBackground ) ;
294
- } else {
295
- const cachedTerminal = this . _sessionTerminalAssociations . get ( chatSessionId ) ;
296
- isNewSession = ! cachedTerminal ;
297
- if ( cachedTerminal ) {
298
- this . _logService . debug ( `RunInTerminalTool: Using existing terminal with session ID \`${ chatSessionId } \`` ) ;
299
- toolTerminal = cachedTerminal ;
300
- } else {
301
- this . _logService . debug ( `RunInTerminalTool: Creating terminal with session ID \`${ chatSessionId } \`` ) ;
302
- toolTerminal = await this . _instantiationService . createInstance ( ToolTerminalCreator ) . createTerminal ( token ) ;
303
- this . _sessionTerminalAssociations . set ( chatSessionId , toolTerminal ) ;
304
- if ( token . isCancellationRequested ) {
305
- toolTerminal . instance . dispose ( ) ;
306
- throw new CancellationError ( ) ;
307
- }
308
- await this . _setupTerminalAssociation ( toolTerminal , chatSessionId , termId , args . isBackground ) ;
309
- }
310
- }
284
+ this . _logService . debug ( `RunInTerminalTool: Creating ${ args . isBackground ? 'background' : 'foreground' } terminal. termId=${ termId } , chatSessionId=${ chatSessionId } ` ) ;
285
+ const toolTerminal = await ( args . isBackground ? this . _initBackgroundTerminal : this . _initForegroundTerminal ) ( chatSessionId , termId , token ) ;
311
286
312
287
this . _terminalService . setActiveInstance ( toolTerminal . instance ) ;
313
288
const timingConnectMs = Date . now ( ) - timingStart ;
@@ -470,6 +445,34 @@ export class RunInTerminalTool extends Disposable implements IToolImpl {
470
445
}
471
446
}
472
447
448
+ private async _initBackgroundTerminal ( chatSessionId : string , termId : string , token : CancellationToken ) : Promise < IToolTerminal > {
449
+ this . _logService . debug ( `RunInTerminalTool: Creating background terminal with ID=${ termId } ` ) ;
450
+ const toolTerminal = await this . _instantiationService . createInstance ( ToolTerminalCreator ) . createTerminal ( token ) ;
451
+ this . _sessionTerminalAssociations . set ( chatSessionId , toolTerminal ) ;
452
+ if ( token . isCancellationRequested ) {
453
+ toolTerminal . instance . dispose ( ) ;
454
+ throw new CancellationError ( ) ;
455
+ }
456
+ await this . _setupTerminalAssociation ( toolTerminal , chatSessionId , termId , true ) ;
457
+ return toolTerminal ;
458
+ }
459
+
460
+ private async _initForegroundTerminal ( chatSessionId : string , termId : string , token : CancellationToken ) : Promise < IToolTerminal > {
461
+ const cachedTerminal = this . _sessionTerminalAssociations . get ( chatSessionId ) ;
462
+ if ( cachedTerminal ) {
463
+ this . _logService . debug ( `RunInTerminalTool: Using cached foreground terminal with session ID \`${ chatSessionId } \`` ) ;
464
+ return cachedTerminal ;
465
+ }
466
+ const toolTerminal = await this . _instantiationService . createInstance ( ToolTerminalCreator ) . createTerminal ( token ) ;
467
+ this . _sessionTerminalAssociations . set ( chatSessionId , toolTerminal ) ;
468
+ if ( token . isCancellationRequested ) {
469
+ toolTerminal . instance . dispose ( ) ;
470
+ throw new CancellationError ( ) ;
471
+ }
472
+ await this . _setupTerminalAssociation ( toolTerminal , chatSessionId , termId , false ) ;
473
+ return toolTerminal ;
474
+ }
475
+
473
476
protected async _rewriteCommandIfNeeded ( args : IRunInTerminalInputParams , instance : Pick < ITerminalInstance , 'getCwdResource' > | undefined , shell : string ) : Promise < string > {
474
477
const commandLine = args . command ;
475
478
const os = await this . _osBackend ;
0 commit comments