diff --git a/core/tools/implementations/runTerminalCommand.test.ts b/core/tools/implementations/runTerminalCommand.test.ts index 2868a51bc..9ef39632b 100644 --- a/core/tools/implementations/runTerminalCommand.test.ts +++ b/core/tools/implementations/runTerminalCommand.test.ts @@ -356,10 +356,12 @@ describe("runTerminalCommandImpl", () => { it("should handle missing workspace directory gracefully", async () => { // Mock IDE to return empty workspace directories const mockEmptyWorkspace = jest.fn().mockReturnValue(Promise.resolve([])); - + // Create IDE mock with empty workspace const mockIde = { - getIdeInfo: jest.fn().mockReturnValue(Promise.resolve({ remoteName: "local" })), + getIdeInfo: jest + .fn() + .mockReturnValue(Promise.resolve({ remoteName: "local" })), getWorkspaceDirs: mockEmptyWorkspace, runCommand: jest.fn(), getIdeSettings: jest.fn(), @@ -393,7 +395,7 @@ describe("runTerminalCommandImpl", () => { expect(result[0].description).toBe("Terminal command output"); expect(result[0].content).toContain("no workspace test"); expect(result[0].status).toBe("Command completed"); - + // Verify workspace dirs was called but returned empty expect(mockEmptyWorkspace).toHaveBeenCalled(); }); @@ -401,12 +403,12 @@ describe("runTerminalCommandImpl", () => { it("should handle case where cwd fallbacks all fail", async () => { // Mock IDE to return empty workspace directories const mockEmptyWorkspace = jest.fn().mockReturnValue(Promise.resolve([])); - + // Save original environment variables and process.cwd const originalHome = process.env.HOME; const originalUserProfile = process.env.USERPROFILE; const originalCwd = process.cwd; - + try { // Mock all fallbacks to fail delete process.env.HOME; @@ -414,10 +416,12 @@ describe("runTerminalCommandImpl", () => { process.cwd = jest.fn().mockImplementation(() => { throw new Error("Current directory unavailable"); }); - + // Create IDE mock with empty workspace const mockIde = { - getIdeInfo: jest.fn().mockReturnValue(Promise.resolve({ remoteName: "local" })), + getIdeInfo: jest + .fn() + .mockReturnValue(Promise.resolve({ remoteName: "local" })), getWorkspaceDirs: mockEmptyWorkspace, runCommand: jest.fn(), getIdeSettings: jest.fn(), @@ -445,15 +449,17 @@ describe("runTerminalCommandImpl", () => { // This should now handle the case gracefully by falling back to temp directory const result = await runTerminalCommandImpl(args, extras); - + // Should work using the temp directory as fallback expect(result).toHaveLength(1); expect(result[0].name).toBe("Terminal"); expect(result[0].content).toContain("fallback test"); expect(result[0].status).toBe("Command completed"); - - console.log("Successfully handled cwd fallback to temp directory:", result[0].status); - + + console.log( + "Successfully handled cwd fallback to temp directory:", + result[0].status, + ); } finally { // Always restore original values if (originalHome !== undefined) { @@ -465,4 +471,4 @@ describe("runTerminalCommandImpl", () => { process.cwd = originalCwd; } }); -}); \ No newline at end of file +}); diff --git a/core/tools/implementations/runTerminalCommand.ts b/core/tools/implementations/runTerminalCommand.ts index 7486bf810..919bebdd7 100644 --- a/core/tools/implementations/runTerminalCommand.ts +++ b/core/tools/implementations/runTerminalCommand.ts @@ -78,7 +78,7 @@ export const runTerminalCommandImpl: ToolImpl = async (args, extras) => { if (extras.onPartialOutput) { try { const workspaceDirs = await extras.ide.getWorkspaceDirs(); - + // Handle case where no workspace is available let cwd: string; if (workspaceDirs.length > 0) { @@ -119,58 +119,98 @@ export const runTerminalCommandImpl: ToolImpl = async (args, extras) => { cwd, env: getColorEnv(), // Add enhanced environment for colors }); + } + } - childProc.stdout?.on("data", (data) => { - // Skip if this process has been backgrounded - if (isProcessBackgrounded(toolCallId)) return; + // Use spawn with color environment + const childProc = childProcess.spawn(command, { + cwd, + shell: true, + env: getColorEnv(), // Add enhanced environment for colors + }); - const newOutput = getDecodedOutput(data); - terminalOutput += newOutput; + childProc.stdout?.on("data", (data) => { + // Skip if this process has been backgrounded + if (isProcessBackgrounded(toolCallId)) return; - // Send partial output to UI - if (extras.onPartialOutput) { - const status = waitForCompletion - ? "" - : "Command is running in the background..."; - extras.onPartialOutput({ - toolCallId, - contextItems: [ - { - name: "Terminal", - description: "Terminal command output", - content: terminalOutput, - status: status, - }, - ], - }); - } + const newOutput = getDecodedOutput(data); + terminalOutput += newOutput; + + // Send partial output to UI + if (extras.onPartialOutput) { + const status = waitForCompletion + ? "" + : "Command is running in the background..."; + extras.onPartialOutput({ + toolCallId, + contextItems: [ + { + name: "Terminal", + description: "Terminal command output", + content: terminalOutput, + status: status, + }, + ], }); + } + }); - childProc.stderr?.on("data", (data) => { - // Skip if this process has been backgrounded - if (isProcessBackgrounded(toolCallId)) return; + childProc.stderr?.on("data", (data) => { + // Skip if this process has been backgrounded + if (isProcessBackgrounded(toolCallId)) return; - const newOutput = getDecodedOutput(data); - terminalOutput += newOutput; + const newOutput = getDecodedOutput(data); + terminalOutput += newOutput; - // Send partial output to UI, status is not required - if (extras.onPartialOutput) { - extras.onPartialOutput({ - toolCallId, - contextItems: [ - { - name: "Terminal", - description: "Terminal command output", - content: terminalOutput, - }, - ], - }); - } + // Send partial output to UI, status is not required + if (extras.onPartialOutput) { + extras.onPartialOutput({ + toolCallId, + contextItems: [ + { + name: "Terminal", + description: "Terminal command output", + content: terminalOutput, + }, + ], }); + } + }); - // If we don't need to wait for completion, resolve immediately - if (!waitForCompletion) { - const status = "Command is running in the background..."; + // If we don't need to wait for completion, resolve immediately + if (!waitForCompletion) { + const status = "Command is running in the background..."; + resolve([ + { + name: "Terminal", + description: "Terminal command output", + content: terminalOutput, + status: status, + }, + ]); + } + + childProc.on("close", (code) => { + // If this process has been backgrounded, clean it up from the map and return + if (isProcessBackgrounded(toolCallId)) { + removeBackgroundedProcess(toolCallId); + return; + } + + if (waitForCompletion) { + // Normal completion, resolve now + if (code === 0) { + const status = "Command completed"; + resolve([ + { + name: "Terminal", + description: "Terminal command output", + content: terminalOutput, + status: status, + }, + ]); + } else { + const status = `Command failed with exit code ${code}`; resolve([ { name: "Terminal", @@ -180,76 +220,45 @@ export const runTerminalCommandImpl: ToolImpl = async (args, extras) => { }, ]); } + } else { + // Already resolved, just update the UI with final output + if (extras.onPartialOutput) { + const status = + code === 0 || !code + ? "\nBackground command completed" + : `\nBackground command failed with exit code ${code}`; + extras.onPartialOutput({ + toolCallId, + contextItems: [ + { + name: "Terminal", + description: "Terminal command output", + content: terminalOutput, + status: status, + }, + ], + }); + } + } + }); - childProc.on("close", (code) => { - // If this process has been backgrounded, clean it up from the map and return - if (isProcessBackgrounded(toolCallId)) { - removeBackgroundedProcess(toolCallId); - return; - } + childProc.on("error", (error) => { + // If this process has been backgrounded, clean it up from the map and return + if (isProcessBackgrounded(toolCallId)) { + removeBackgroundedProcess(toolCallId); + return; + } - if (waitForCompletion) { - // Normal completion, resolve now - if (code === 0) { - const status = "Command completed"; - resolve([ - { - name: "Terminal", - description: "Terminal command output", - content: terminalOutput, - status: status, - }, - ]); - } else { - const status = `Command failed with exit code ${code}`; - resolve([ - { - name: "Terminal", - description: "Terminal command output", - content: terminalOutput, - status: status, - }, - ]); - } - } else { - // Already resolved, just update the UI with final output - if (extras.onPartialOutput) { - const status = - code === 0 || !code - ? "\nBackground command completed" - : `\nBackground command failed with exit code ${code}`; - extras.onPartialOutput({ - toolCallId, - contextItems: [ - { - name: "Terminal", - description: "Terminal command output", - content: terminalOutput, - status: status, - }, - ], - }); - } - } - }); - - childProc.on("error", (error) => { - // If this process has been backgrounded, clean it up from the map and return - if (isProcessBackgrounded(toolCallId)) { - removeBackgroundedProcess(toolCallId); - return; - } - - reject(error); - }); - }); + reject(error); + }); + }); } catch (error: any) { throw error; } } else { // Fallback to non-streaming for older clients const workspaceDirs = await extras.ide.getWorkspaceDirs(); - + // Handle case where no workspace is available let cwd: string; if (workspaceDirs.length > 0) { @@ -397,4 +406,4 @@ export const runTerminalCommandImpl: ToolImpl = async (args, extras) => { status: "Command failed", }, ]; -}; \ No newline at end of file +};