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

Commit a3d4aa4

Browse files
nikomatsakisClaude
andcommitted
Refactor Walkthrough communication to use consolidated Bus methods
- Add sendTextToActiveTerminal method to Bus for plain text messages - Replace walkthrough comment reply with Bus.sendToActiveTerminal() - Replace walkthrough action messages with Bus.sendTextToActiveTerminal() - Remove duplicate sendToActiveShell method (~70 lines of duplicate code) - Fix syntax error with extra closing brace This completes the consolidation of Ask Socratic Shell and Walkthrough communication into unified Bus methods, eliminating all duplicate terminal finding and communication logic. Co-authored-by: Claude <[email protected]>
1 parent 2f2853c commit a3d4aa4

File tree

2 files changed

+75
-83
lines changed

2 files changed

+75
-83
lines changed

extension/src/bus.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,73 @@ export class Bus {
142142
this.log(`Reference ${referenceId} sent to terminal ${selectedTerminal.terminal.name} (PID: ${selectedTerminal.shellPID})`);
143143
}
144144

145+
/**
146+
* Send plain text message to active terminal (no reference creation)
147+
* For simple text messages that don't need MCP reference storage
148+
*/
149+
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+
}
204+
205+
// Send text directly to terminal
206+
selectedTerminal.terminal.sendText(message, false); // false = don't execute, just insert text
207+
selectedTerminal.terminal.show(); // Bring terminal into focus
208+
209+
this.log(`Text message sent to terminal ${selectedTerminal.terminal.name} (PID: ${selectedTerminal.shellPID})`);
210+
}
211+
145212
getActiveTerminals(): Set<number> {
146213
return this.daemonClient.getActiveTerminals();
147214
}

extension/src/walkthroughWebview.ts

Lines changed: 8 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ export class WalkthroughWebviewProvider implements vscode.WebviewViewProvider {
191191
console.log('Walkthrough: action received:', message.message);
192192
this.bus.outputChannel.appendLine(`Action button clicked: ${message.message}`);
193193

194-
// Send message to active AI terminal
195-
await this.sendToActiveShell(message.message);
194+
// Send message to active AI terminal using Bus method
195+
await this.bus.sendTextToActiveTerminal(message.message);
196196
break;
197197
case 'showDiff':
198198
console.log('Walkthrough: showDiff command received:', message.filePath);
@@ -667,76 +667,6 @@ export class WalkthroughWebviewProvider implements vscode.WebviewViewProvider {
667667
}
668668
}
669669

670-
671-
672-
/**
673-
* Send a message to the active AI terminal (shared with Ask Socratic Shell)
674-
*/
675-
private async sendToActiveShell(message: string): Promise<void> {
676-
const terminals = vscode.window.terminals;
677-
if (terminals.length === 0) {
678-
vscode.window.showWarningMessage('No terminals found. Please open a terminal with an active AI assistant.');
679-
return;
680-
}
681-
682-
// Get active terminals with MCP servers from registry
683-
const activeTerminals = this.bus.getActiveTerminals();
684-
this.bus.outputChannel.appendLine(`Active MCP server terminals: [${Array.from(activeTerminals).join(', ')}]`);
685-
686-
if (activeTerminals.size === 0) {
687-
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.');
688-
return;
689-
}
690-
691-
// Filter terminals to only those with active MCP servers
692-
const terminalChecks = await Promise.all(
693-
terminals.map(async (terminal) => {
694-
const shellPID = await terminal.processId;
695-
const isAiEnabled = shellPID && activeTerminals.has(shellPID);
696-
return { terminal, isAiEnabled };
697-
})
698-
);
699-
700-
const aiEnabledTerminals = terminalChecks
701-
.filter(check => check.isAiEnabled)
702-
.map(check => check.terminal);
703-
704-
if (aiEnabledTerminals.length === 0) {
705-
vscode.window.showWarningMessage('No AI-enabled terminals found. Please ensure you have a terminal with an active MCP server running.');
706-
return;
707-
}
708-
709-
// Simple case - exactly one AI-enabled terminal
710-
if (aiEnabledTerminals.length === 1) {
711-
const terminal = aiEnabledTerminals[0];
712-
terminal.sendText(message, false); // false = don't execute, just insert text
713-
terminal.show(); // Bring terminal into focus
714-
this.bus.outputChannel.appendLine(`Message sent to terminal: ${terminal.name}`);
715-
vscode.window.showInformationMessage(`Message sent to ${terminal.name}`);
716-
return;
717-
}
718-
719-
// Multiple terminals - show picker (simplified version)
720-
const selectedTerminal = await vscode.window.showQuickPick(
721-
aiEnabledTerminals.map(terminal => ({
722-
label: terminal.name,
723-
description: 'Terminal with active AI assistant',
724-
terminal: terminal
725-
})),
726-
{
727-
placeHolder: 'Select terminal for AI message',
728-
title: 'Multiple AI-enabled terminals found'
729-
}
730-
);
731-
732-
if (selectedTerminal) {
733-
selectedTerminal.terminal.sendText(message, false);
734-
selectedTerminal.terminal.show();
735-
this.bus.outputChannel.appendLine(`Message sent to terminal: ${selectedTerminal.terminal.name}`);
736-
vscode.window.showInformationMessage(`Message sent to ${selectedTerminal.terminal.name}`);
737-
}
738-
}
739-
740670
public resolveWebviewView(
741671
webviewView: vscode.WebviewView,
742672
context: vscode.WebviewViewResolveContext,
@@ -829,21 +759,16 @@ export class WalkthroughWebviewProvider implements vscode.WebviewViewProvider {
829759
const lineNumber = thread.range.start.line + 1; // Convert to 1-based
830760
const filePath = vscode.workspace.asRelativePath(uri);
831761

832-
// Generate compact reference instead of verbose XML
833-
const referenceId = crypto.randomUUID();
834-
835-
// Store reference context via bus
836-
await this.bus.sendReferenceToActiveShell(referenceId, {
762+
// Use new consolidated sendToActiveTerminal method
763+
const referenceData = {
837764
file: filePath,
838765
line: lineNumber,
839766
selection: undefined,
840767
user_comment: text
841-
});
842-
843-
// Send compact ssref tag to terminal
844-
const compactRef = `<ssref id="${referenceId}"/>\n\n`;
845-
await this.sendToActiveShell(compactRef);
846-
this.bus.outputChannel.appendLine(`Comment reply sent as compact reference: ${referenceId}`);
768+
};
769+
770+
await this.bus.sendToActiveTerminal(referenceData);
771+
this.bus.outputChannel.appendLine(`Comment reply sent as compact reference for ${filePath}:${lineNumber}`);
847772
} catch (error) {
848773
console.error('[WALKTHROUGH] Error sending comment to shell:', error);
849774
this.bus.outputChannel.appendLine(`Error sending comment: ${error}`);

0 commit comments

Comments
 (0)