Skip to content

Commit 3953669

Browse files
committed
Move tool terminal init to functions
1 parent 7c7d964 commit 3953669

File tree

1 file changed

+31
-28
lines changed

1 file changed

+31
-28
lines changed

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

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -274,40 +274,15 @@ export class RunInTerminalTool extends Disposable implements IToolImpl {
274274
}
275275

276276
let error: string | undefined;
277+
const isNewSession = !args.isBackground && !this._sessionTerminalAssociations.has(chatSessionId);
277278

278279
const timingStart = Date.now();
279280
const termId = generateUuid();
280281

281-
let isNewSession = false;
282-
let toolTerminal: IToolTerminal;
283282
const store = new DisposableStore();
284283

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);
311286

312287
this._terminalService.setActiveInstance(toolTerminal.instance);
313288
const timingConnectMs = Date.now() - timingStart;
@@ -470,6 +445,34 @@ export class RunInTerminalTool extends Disposable implements IToolImpl {
470445
}
471446
}
472447

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+
473476
protected async _rewriteCommandIfNeeded(args: IRunInTerminalInputParams, instance: Pick<ITerminalInstance, 'getCwdResource'> | undefined, shell: string): Promise<string> {
474477
const commandLine = args.command;
475478
const os = await this._osBackend;

0 commit comments

Comments
 (0)