@@ -278,36 +278,56 @@ export class RunInTerminalTool extends Disposable implements IToolImpl {
278
278
const timingStart = Date . now ( ) ;
279
279
const termId = generateUuid ( ) ;
280
280
281
- if ( args . isBackground ) {
282
- const store = new DisposableStore ( ) ;
283
- let outputAndIdle : { terminalExecutionIdleBeforeTimeout : boolean ; output : string ; pollDurationMs ?: number ; modelOutputEvalResponse ?: string } | undefined = undefined ;
281
+ let isNewSession = false ;
282
+ let toolTerminal : IToolTerminal ;
283
+ const store = new DisposableStore ( ) ;
284
284
285
+ if ( args . isBackground ) {
285
286
this . _logService . debug ( `RunInTerminalTool: Creating background terminal with ID=${ termId } ` ) ;
286
- const toolTerminal = await this . _instantiationService . createInstance ( ToolTerminalCreator ) . createTerminal ( token ) ;
287
+ toolTerminal = await this . _instantiationService . createInstance ( ToolTerminalCreator ) . createTerminal ( token ) ;
287
288
this . _sessionTerminalAssociations . set ( chatSessionId , toolTerminal ) ;
288
289
if ( token . isCancellationRequested ) {
289
290
toolTerminal . instance . dispose ( ) ;
290
291
throw new CancellationError ( ) ;
291
292
}
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
+ }
293
311
294
- this . _terminalService . setActiveInstance ( toolTerminal . instance ) ;
295
- const timingConnectMs = Date . now ( ) - timingStart ;
312
+ this . _terminalService . setActiveInstance ( toolTerminal . instance ) ;
313
+ const timingConnectMs = Date . now ( ) - timingStart ;
296
314
297
- const xterm = await toolTerminal . instance . xtermReadyPromise ;
298
- if ( ! xterm ) {
299
- throw new Error ( 'Instance was disposed before xterm.js was ready' ) ;
300
- }
315
+ const xterm = await toolTerminal . instance . xtermReadyPromise ;
316
+ if ( ! xterm ) {
317
+ throw new Error ( 'Instance was disposed before xterm.js was ready' ) ;
318
+ }
301
319
302
- let inputUserChars = 0 ;
303
- let inputUserSigint = false ;
304
- store . add ( xterm . raw . onData ( data => {
305
- if ( ! telemetryIgnoredSequences . includes ( data ) ) {
306
- inputUserChars += data . length ;
307
- }
308
- inputUserSigint ||= data === '\x03' ;
309
- } ) ) ;
320
+ let inputUserChars = 0 ;
321
+ let inputUserSigint = false ;
322
+ store . add ( xterm . raw . onData ( data => {
323
+ if ( ! telemetryIgnoredSequences . includes ( data ) ) {
324
+ inputUserChars += data . length ;
325
+ }
326
+ inputUserSigint ||= data === '\x03' ;
327
+ } ) ) ;
310
328
329
+ if ( args . isBackground ) {
330
+ let outputAndIdle : { terminalExecutionIdleBeforeTimeout : boolean ; output : string ; pollDurationMs ?: number ; modelOutputEvalResponse ?: string } | undefined = undefined ;
311
331
try {
312
332
this . _logService . debug ( `RunInTerminalTool: Starting background execution \`${ command } \`` ) ;
313
333
@@ -372,40 +392,6 @@ export class RunInTerminalTool extends Disposable implements IToolImpl {
372
392
} ) ;
373
393
}
374
394
} else {
375
- const store = new DisposableStore ( ) ;
376
- let toolTerminal : IToolTerminal | undefined = this . _sessionTerminalAssociations . get ( chatSessionId ) ;
377
- const isNewSession = ! toolTerminal ;
378
- if ( toolTerminal ) {
379
- this . _logService . debug ( `RunInTerminalTool: Using existing terminal with session ID \`${ chatSessionId } \`` ) ;
380
- } else {
381
- this . _logService . debug ( `RunInTerminalTool: Creating terminal with session ID \`${ chatSessionId } \`` ) ;
382
- toolTerminal = await this . _instantiationService . createInstance ( ToolTerminalCreator ) . createTerminal ( token ) ;
383
- this . _sessionTerminalAssociations . set ( chatSessionId , toolTerminal ) ;
384
- if ( token . isCancellationRequested ) {
385
- toolTerminal . instance . dispose ( ) ;
386
- throw new CancellationError ( ) ;
387
- }
388
- await this . _setupTerminalAssociation ( toolTerminal , chatSessionId , termId , args . isBackground ) ;
389
- }
390
-
391
- this . _terminalService . setActiveInstance ( toolTerminal . instance ) ;
392
-
393
- const timingConnectMs = Date . now ( ) - timingStart ;
394
-
395
- const xterm = await toolTerminal . instance . xtermReadyPromise ;
396
- if ( ! xterm ) {
397
- throw new Error ( 'Instance was disposed before xterm.js was ready' ) ;
398
- }
399
-
400
- let inputUserChars = 0 ;
401
- let inputUserSigint = false ;
402
- store . add ( xterm . raw . onData ( data => {
403
- if ( ! telemetryIgnoredSequences . includes ( data ) ) {
404
- inputUserChars += data . length ;
405
- }
406
- inputUserSigint ||= data === '\x03' ;
407
- } ) ) ;
408
-
409
395
let terminalResult = '' ;
410
396
411
397
let outputLineCount = - 1 ;
0 commit comments