Skip to content

Commit e8747f1

Browse files
committed
Refactor StartPowershellConsoleInternal to return tuple and remove Replace workaround
1 parent 8aa2f34 commit e8747f1

1 file changed

Lines changed: 31 additions & 27 deletions

File tree

PowerShell.MCP.Proxy/Tools/PowerShellTools.cs

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,11 @@ public static async Task<string> InvokeExpression(
304304
{
305305
// No ready pipe - auto-start
306306
Console.Error.WriteLine($"[INFO] No ready PowerShell console found, auto-starting... Reason: {allPipesStatusInfo}");
307-
var startResult = await StartPowershellConsoleInternal(powerShellService, null, cancellationToken);
307+
var (success, locationResult) = await StartPowershellConsoleInternal(powerShellService, null, cancellationToken);
308+
if (!success)
309+
{
310+
return locationResult; // Error message
311+
}
308312

309313
// Collect completed outputs and busy status (after console start, using new pipe as exclude)
310314
var newPipeName = sessionManager.ActivePipeName;
@@ -313,7 +317,7 @@ public static async Task<string> InvokeExpression(
313317
// Extract PID from pipe name (format: PowerShell.MCP.Communication.{PID})
314318
var pid = GetPidString(newPipeName);
315319

316-
// Build response: busy status first + message + startResult + completedOutputs
320+
// Build response: busy status first + message + location + completedOutputs
317321
var response = new StringBuilder();
318322
if (busyStatusInfo.Length > 0)
319323
{
@@ -322,13 +326,10 @@ public static async Task<string> InvokeExpression(
322326
}
323327
response.AppendLine($"Started new console PID#{pid} with PowerShell.MCP module imported. Pipeline NOT executed - verify location and re-execute.");
324328
response.AppendLine();
325-
response.Append(startResult.Replace(
326-
"PowerShell console started successfully with PowerShell.MCP module imported.\r\n\r\n",
327-
"").Replace(
328-
"PowerShell console started successfully with PowerShell.MCP module imported.\n\n",
329-
""));
329+
response.Append(locationResult);
330330
if (completedOutputs.Length > 0)
331331
{
332+
response.AppendLine();
332333
response.AppendLine();
333334
response.Append(completedOutputs);
334335
}
@@ -395,7 +396,11 @@ public static async Task<string> InvokeExpression(
395396
{
396397
// Auto-start new console
397398
Console.Error.WriteLine($"[INFO] Runspace busy ({jsonResponse.Reason}), auto-starting new console...");
398-
var startResult = await StartPowershellConsoleInternal(powerShellService, null, cancellationToken);
399+
var (success, locationResult) = await StartPowershellConsoleInternal(powerShellService, null, cancellationToken);
400+
if (!success)
401+
{
402+
return locationResult; // Error message
403+
}
399404

400405
var newPipeName = sessionManager.ActivePipeName;
401406
var (completedOutputs, busyInfo) = await CollectAllCachedOutputsAsync(powerShellService, newPipeName, cancellationToken);
@@ -412,13 +417,10 @@ public static async Task<string> InvokeExpression(
412417
busyResponse.AppendLine();
413418
busyResponse.AppendLine($"Started new console PID#{newPid} with PowerShell.MCP module imported. Pipeline NOT executed - verify location and re-execute.");
414419
busyResponse.AppendLine();
415-
busyResponse.Append(startResult.Replace(
416-
"PowerShell console started successfully with PowerShell.MCP module imported.\r\n\r\n",
417-
"").Replace(
418-
"PowerShell console started successfully with PowerShell.MCP module imported.\n\n",
419-
""));
420+
busyResponse.Append(locationResult);
420421
if (completedOutputs.Length > 0)
421422
{
423+
busyResponse.AppendLine();
422424
busyResponse.AppendLine();
423425
busyResponse.Append(completedOutputs);
424426
}
@@ -711,14 +713,18 @@ public static async Task<string> StartPowershellConsole(
711713
string? banner = null,
712714
CancellationToken cancellationToken = default)
713715
{
714-
var startResult = await StartPowershellConsoleInternal(powerShellService, banner, cancellationToken);
716+
var (success, locationResult) = await StartPowershellConsoleInternal(powerShellService, banner, cancellationToken);
717+
if (!success)
718+
{
719+
return locationResult; // Error message
720+
}
715721

716722
// Collect busy status from Proxy side
717723
var sessionManager = ConsoleSessionManager.Instance;
718724
var newPipeName = sessionManager.ActivePipeName;
719725
var (completedOutput, busyStatusInfo) = await CollectAllCachedOutputsAsync(powerShellService, newPipeName, cancellationToken);
720726

721-
// Build response: busy status first + completed output + start message
727+
// Build response: busy status first + completed output + start message + location
722728
var response = new StringBuilder();
723729
if (busyStatusInfo.Length > 0)
724730
{
@@ -729,14 +735,17 @@ public static async Task<string> StartPowershellConsole(
729735
{
730736
response.Append(completedOutput);
731737
}
732-
response.Append(startResult);
738+
response.AppendLine("PowerShell console started successfully with PowerShell.MCP module imported.");
739+
response.AppendLine();
740+
response.Append(locationResult);
733741
return response.ToString();
734742
}
735743

736744
/// <summary>
737-
/// Internal method to start PowerShell console
745+
/// Internal method to start PowerShell console.
746+
/// Returns (success, result) where result is locationResult on success, or error message on failure.
738747
/// </summary>
739-
private static async Task<string> StartPowershellConsoleInternal(
748+
private static async Task<(bool success, string result)> StartPowershellConsoleInternal(
740749
IPowerShellService powerShellService,
741750
string? banner,
742751
CancellationToken cancellationToken)
@@ -751,7 +760,7 @@ private static async Task<string> StartPowershellConsoleInternal(
751760

752761
if (!success)
753762
{
754-
return "Failed to start PowerShell console or establish Named Pipe connection.\n\nPossible causes:\n- No supported terminal emulator found (gnome-terminal, konsole, xfce4-terminal, xterm, etc.)\n- Terminal emulator failed to start\n- PowerShell.MCP module failed to initialize\n\nPlease ensure a terminal emulator is installed and try again.";
763+
return (false, "Failed to start PowerShell console or establish Named Pipe connection.\n\nPossible causes:\n- No supported terminal emulator found (gnome-terminal, konsole, xfce4-terminal, xterm, etc.)\n- Terminal emulator failed to start\n- PowerShell.MCP module failed to initialize\n\nPlease ensure a terminal emulator is installed and try again.");
755764
}
756765

757766
// Register the new console
@@ -760,21 +769,16 @@ private static async Task<string> StartPowershellConsoleInternal(
760769
Console.Error.WriteLine($"[INFO] PowerShell console started successfully (pipe={pipeName}), getting current location...");
761770

762771
// Get current location from new console
763-
var newLocationResult = await powerShellService.GetCurrentLocationFromPipeAsync(pipeName, cancellationToken);
772+
var locationResult = await powerShellService.GetCurrentLocationFromPipeAsync(pipeName, cancellationToken);
764773

765774
Console.Error.WriteLine("[INFO] PowerShell console startup completed");
766775

767-
// Build response (busy info collection is done by caller)
768-
var response = new StringBuilder();
769-
response.AppendLine("PowerShell console started successfully with PowerShell.MCP module imported.");
770-
response.AppendLine();
771-
response.Append(newLocationResult);
772-
return response.ToString();
776+
return (true, locationResult);
773777
}
774778
catch (Exception ex)
775779
{
776780
Console.Error.WriteLine($"[ERROR] StartPowershellConsole failed: {ex.Message}");
777-
return $"Failed to start PowerShell console: {ex.Message}\n\nPlease check if a terminal emulator is available and try again.";
781+
return (false, $"Failed to start PowerShell console: {ex.Message}\n\nPlease check if a terminal emulator is available and try again.");
778782
}
779783
}
780784

0 commit comments

Comments
 (0)