Skip to content
This repository was archived by the owner on Sep 23, 2025. It is now read-only.

Commit fecedcf

Browse files
nikomatsakisClaude
andcommitted
Extract selectActiveTerminal method to eliminate duplicate logic
- Factor out shared terminal selection logic into private selectActiveTerminal method - Both sendToActiveTerminal and sendTextToActiveTerminal now use the same selection logic - Eliminates ~80 lines of duplicate terminal finding code - Improves maintainability with single source of truth for terminal selection - Fix type safety for shellPID handling Co-authored-by: Claude <[email protected]>
1 parent a3d4aa4 commit fecedcf

File tree

1 file changed

+34
-85
lines changed

1 file changed

+34
-85
lines changed

extension/src/bus.ts

Lines changed: 34 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -61,24 +61,22 @@ export class Bus {
6161
}
6262

6363
/**
64-
* Send reference data to active terminal with consolidated logic
65-
* Handles terminal finding, reference creation, and XML generation
64+
* Select an active AI-enabled terminal with picker for ambiguity resolution
65+
* Returns null if no suitable terminal found or user cancelled
6666
*/
67-
async sendToActiveTerminal(referenceData: any, includeNewline: boolean = true): Promise<void> {
68-
// Find active terminal using existing heuristics
67+
private async selectActiveTerminal(): Promise<{ terminal: vscode.Terminal; shellPID: number } | null> {
6968
const terminals = vscode.window.terminals;
7069
if (terminals.length === 0) {
7170
vscode.window.showWarningMessage('No terminals found. Please open a terminal with an active AI assistant.');
72-
return;
71+
return null;
7372
}
7473

75-
// Get active terminals with MCP servers from registry
7674
const activeTerminals = this.getActiveTerminals();
7775
this.log(`Active MCP server terminals: [${Array.from(activeTerminals).join(', ')}]`);
7876

7977
if (activeTerminals.size === 0) {
8078
vscode.window.showWarningMessage('No terminals with active MCP servers found. Please ensure you have a terminal with an active AI assistant (like Q chat or Claude CLI) running.');
81-
return;
79+
return null;
8280
}
8381

8482
// Filter terminals to only those with active MCP servers
@@ -91,39 +89,42 @@ export class Bus {
9189
);
9290

9391
const aiEnabledTerminals = terminalChecks
94-
.filter(check => check.isAiEnabled)
95-
.map(check => ({ terminal: check.terminal, shellPID: check.shellPID }));
92+
.filter(check => check.isAiEnabled && check.shellPID)
93+
.map(check => ({ terminal: check.terminal, shellPID: check.shellPID! }));
9694

9795
if (aiEnabledTerminals.length === 0) {
9896
vscode.window.showWarningMessage('No AI-enabled terminals found. Please ensure you have a terminal with an active MCP server running.');
99-
return;
97+
return null;
10098
}
10199

102-
let selectedTerminal;
103-
104100
// Simple case - exactly one AI-enabled terminal
105101
if (aiEnabledTerminals.length === 1) {
106-
selectedTerminal = aiEnabledTerminals[0];
107-
} else {
108-
// Multiple terminals - show picker for ambiguity resolution
109-
const terminalItems = aiEnabledTerminals.map(({ terminal, shellPID }) => ({
110-
label: terminal.name,
111-
description: `PID: ${shellPID}`,
112-
terminal,
113-
shellPID
114-
}));
115-
116-
const selected = await vscode.window.showQuickPick(terminalItems, {
117-
placeHolder: 'Select terminal to send reference to'
118-
});
119-
120-
if (!selected) {
121-
return; // User cancelled
122-
}
123-
124-
selectedTerminal = { terminal: selected.terminal, shellPID: selected.shellPID };
102+
return aiEnabledTerminals[0];
125103
}
126104

105+
// Multiple terminals - show picker for ambiguity resolution
106+
const terminalItems = aiEnabledTerminals.map(({ terminal, shellPID }) => ({
107+
label: terminal.name,
108+
description: `PID: ${shellPID}`,
109+
terminal,
110+
shellPID
111+
}));
112+
113+
const selected = await vscode.window.showQuickPick(terminalItems, {
114+
placeHolder: 'Select terminal'
115+
});
116+
117+
return selected ? { terminal: selected.terminal, shellPID: selected.shellPID } : null;
118+
}
119+
120+
/**
121+
* Send reference data to active terminal with consolidated logic
122+
* Handles terminal finding, reference creation, and XML generation
123+
*/
124+
async sendToActiveTerminal(referenceData: any, includeNewline: boolean = true): Promise<void> {
125+
const selectedTerminal = await this.selectActiveTerminal();
126+
if (!selectedTerminal) return;
127+
127128
// Generate fresh UUID for reference
128129
const referenceId = crypto.randomUUID();
129130

@@ -147,60 +148,8 @@ export class Bus {
147148
* For simple text messages that don't need MCP reference storage
148149
*/
149150
async sendTextToActiveTerminal(message: string): Promise<void> {
150-
// Find active terminal using same logic as sendToActiveTerminal
151-
const terminals = vscode.window.terminals;
152-
if (terminals.length === 0) {
153-
vscode.window.showWarningMessage('No terminals found. Please open a terminal with an active AI assistant.');
154-
return;
155-
}
156-
157-
const activeTerminals = this.getActiveTerminals();
158-
if (activeTerminals.size === 0) {
159-
vscode.window.showWarningMessage('No terminals with active MCP servers found. Please ensure you have a terminal with an active AI assistant (like Q chat or Claude CLI) running.');
160-
return;
161-
}
162-
163-
// Filter to AI-enabled terminals
164-
const terminalChecks = await Promise.all(
165-
terminals.map(async (terminal) => {
166-
const shellPID = await terminal.processId;
167-
const isAiEnabled = shellPID && activeTerminals.has(shellPID);
168-
return { terminal, shellPID, isAiEnabled };
169-
})
170-
);
171-
172-
const aiEnabledTerminals = terminalChecks
173-
.filter(check => check.isAiEnabled)
174-
.map(check => ({ terminal: check.terminal, shellPID: check.shellPID }));
175-
176-
if (aiEnabledTerminals.length === 0) {
177-
vscode.window.showWarningMessage('No AI-enabled terminals found. Please ensure you have a terminal with an active MCP server running.');
178-
return;
179-
}
180-
181-
let selectedTerminal;
182-
183-
if (aiEnabledTerminals.length === 1) {
184-
selectedTerminal = aiEnabledTerminals[0];
185-
} else {
186-
// Show picker for multiple terminals
187-
const terminalItems = aiEnabledTerminals.map(({ terminal, shellPID }) => ({
188-
label: terminal.name,
189-
description: `PID: ${shellPID}`,
190-
terminal,
191-
shellPID
192-
}));
193-
194-
const selected = await vscode.window.showQuickPick(terminalItems, {
195-
placeHolder: 'Select terminal to send message to'
196-
});
197-
198-
if (!selected) {
199-
return; // User cancelled
200-
}
201-
202-
selectedTerminal = { terminal: selected.terminal, shellPID: selected.shellPID };
203-
}
151+
const selectedTerminal = await this.selectActiveTerminal();
152+
if (!selectedTerminal) return;
204153

205154
// Send text directly to terminal
206155
selectedTerminal.terminal.sendText(message, false); // false = don't execute, just insert text

0 commit comments

Comments
 (0)