Skip to content

Commit 7c7d964

Browse files
committed
Share logic between fg/bg terminals
1 parent 09b93c2 commit 7c7d964

File tree

1 file changed

+38
-52
lines changed

1 file changed

+38
-52
lines changed

src/vs/workbench/contrib/terminalContrib/chatAgentTools/browser/runInTerminalTool.ts

Lines changed: 38 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -278,36 +278,56 @@ export class RunInTerminalTool extends Disposable implements IToolImpl {
278278
const timingStart = Date.now();
279279
const termId = generateUuid();
280280

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();
284284

285+
if (args.isBackground) {
285286
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);
287288
this._sessionTerminalAssociations.set(chatSessionId, toolTerminal);
288289
if (token.isCancellationRequested) {
289290
toolTerminal.instance.dispose();
290291
throw new CancellationError();
291292
}
292293
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+
}
293311

294-
this._terminalService.setActiveInstance(toolTerminal.instance);
295-
const timingConnectMs = Date.now() - timingStart;
312+
this._terminalService.setActiveInstance(toolTerminal.instance);
313+
const timingConnectMs = Date.now() - timingStart;
296314

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+
}
301319

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+
}));
310328

329+
if (args.isBackground) {
330+
let outputAndIdle: { terminalExecutionIdleBeforeTimeout: boolean; output: string; pollDurationMs?: number; modelOutputEvalResponse?: string } | undefined = undefined;
311331
try {
312332
this._logService.debug(`RunInTerminalTool: Starting background execution \`${command}\``);
313333

@@ -372,40 +392,6 @@ export class RunInTerminalTool extends Disposable implements IToolImpl {
372392
});
373393
}
374394
} 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-
409395
let terminalResult = '';
410396

411397
let outputLineCount = -1;

0 commit comments

Comments
 (0)