Changesets · Branches · Tags · Compare
| Changeset | a394da99efe1154420b27ce4c25acf39650a74b7a1191bbd182c0367b6f9ce92 |
|---|---|
| Author | chezsmithy <chezsmithy@me.com> · 2025-07-28 17:41 -07:00 |
| Committer | chezsmithy <chezsmithy@me.com> · 2025-08-08 00:05 -07:00 |
| Parents | 245005345e6e |
2 files changed
modified core/tools/implementations/runTerminalCommand.test.ts +18 −12
| @@ -356,10 +356,12 @@ | ||
| 356 | 356 | it("should handle missing workspace directory gracefully", async () => { |
| 357 | 357 | // Mock IDE to return empty workspace directories |
| 358 | 358 | const mockEmptyWorkspace = jest.fn().mockReturnValue(Promise.resolve([])); |
| 359 | - | |
| 359 | + | |
| 360 | 360 | // Create IDE mock with empty workspace |
| 361 | 361 | const mockIde = { |
| 362 | - getIdeInfo: jest.fn().mockReturnValue(Promise.resolve({ remoteName: "local" })), | |
| 362 | + getIdeInfo: jest | |
| 363 | + .fn() | |
| 364 | + .mockReturnValue(Promise.resolve({ remoteName: "local" })), | |
| 363 | 365 | getWorkspaceDirs: mockEmptyWorkspace, |
| 364 | 366 | runCommand: jest.fn(), |
| 365 | 367 | getIdeSettings: jest.fn(), |
| @@ -393,7 +395,7 @@ | ||
| 393 | 395 | expect(result[0].description).toBe("Terminal command output"); |
| 394 | 396 | expect(result[0].content).toContain("no workspace test"); |
| 395 | 397 | expect(result[0].status).toBe("Command completed"); |
| 396 | - | |
| 398 | + | |
| 397 | 399 | // Verify workspace dirs was called but returned empty |
| 398 | 400 | expect(mockEmptyWorkspace).toHaveBeenCalled(); |
| 399 | 401 | }); |
| @@ -401,12 +403,12 @@ | ||
| 401 | 403 | it("should handle case where cwd fallbacks all fail", async () => { |
| 402 | 404 | // Mock IDE to return empty workspace directories |
| 403 | 405 | const mockEmptyWorkspace = jest.fn().mockReturnValue(Promise.resolve([])); |
| 404 | - | |
| 406 | + | |
| 405 | 407 | // Save original environment variables and process.cwd |
| 406 | 408 | const originalHome = process.env.HOME; |
| 407 | 409 | const originalUserProfile = process.env.USERPROFILE; |
| 408 | 410 | const originalCwd = process.cwd; |
| 409 | - | |
| 411 | + | |
| 410 | 412 | try { |
| 411 | 413 | // Mock all fallbacks to fail |
| 412 | 414 | delete process.env.HOME; |
| @@ -414,10 +416,12 @@ | ||
| 414 | 416 | process.cwd = jest.fn().mockImplementation(() => { |
| 415 | 417 | throw new Error("Current directory unavailable"); |
| 416 | 418 | }); |
| 417 | - | |
| 419 | + | |
| 418 | 420 | // Create IDE mock with empty workspace |
| 419 | 421 | const mockIde = { |
| 420 | - getIdeInfo: jest.fn().mockReturnValue(Promise.resolve({ remoteName: "local" })), | |
| 422 | + getIdeInfo: jest | |
| 423 | + .fn() | |
| 424 | + .mockReturnValue(Promise.resolve({ remoteName: "local" })), | |
| 421 | 425 | getWorkspaceDirs: mockEmptyWorkspace, |
| 422 | 426 | runCommand: jest.fn(), |
| 423 | 427 | getIdeSettings: jest.fn(), |
| @@ -445,15 +449,17 @@ | ||
| 445 | 449 | |
| 446 | 450 | // This should now handle the case gracefully by falling back to temp directory |
| 447 | 451 | const result = await runTerminalCommandImpl(args, extras); |
| 448 | - | |
| 452 | + | |
| 449 | 453 | // Should work using the temp directory as fallback |
| 450 | 454 | expect(result).toHaveLength(1); |
| 451 | 455 | expect(result[0].name).toBe("Terminal"); |
| 452 | 456 | expect(result[0].content).toContain("fallback test"); |
| 453 | 457 | expect(result[0].status).toBe("Command completed"); |
| 454 | - | |
| 455 | - console.log("Successfully handled cwd fallback to temp directory:", result[0].status); | |
| 456 | - | |
| 458 | + | |
| 459 | + console.log( | |
| 460 | + "Successfully handled cwd fallback to temp directory:", | |
| 461 | + result[0].status, | |
| 462 | + ); | |
| 457 | 463 | } finally { |
| 458 | 464 | // Always restore original values |
| 459 | 465 | if (originalHome !== undefined) { |
| @@ -465,4 +471,4 @@ | ||
| 465 | 471 | process.cwd = originalCwd; |
| 466 | 472 | } |
| 467 | 473 | }); |
| 468 | -}); | |
| No newline at the end of the file | ||
| 474 | +}); |
modified core/tools/implementations/runTerminalCommand.ts +118 −109
| @@ -78,7 +78,7 @@ | ||
| 78 | 78 | if (extras.onPartialOutput) { |
| 79 | 79 | try { |
| 80 | 80 | const workspaceDirs = await extras.ide.getWorkspaceDirs(); |
| 81 | - | |
| 81 | + | |
| 82 | 82 | // Handle case where no workspace is available |
| 83 | 83 | let cwd: string; |
| 84 | 84 | if (workspaceDirs.length > 0) { |
| @@ -119,58 +119,98 @@ | ||
| 119 | 119 | cwd, |
| 120 | 120 | env: getColorEnv(), // Add enhanced environment for colors |
| 121 | 121 | }); |
| 122 | + } | |
| 123 | + } | |
| 122 | 124 | |
| 123 | - childProc.stdout?.on("data", (data) => { | |
| 124 | - // Skip if this process has been backgrounded | |
| 125 | - if (isProcessBackgrounded(toolCallId)) return; | |
| 126 | - | |
| 127 | - const newOutput = getDecodedOutput(data); | |
| 128 | - terminalOutput += newOutput; | |
| 125 | + // Use spawn with color environment | |
| 126 | + const childProc = childProcess.spawn(command, { | |
| 127 | + cwd, | |
| 128 | + shell: true, | |
| 129 | + env: getColorEnv(), // Add enhanced environment for colors | |
| 130 | + }); | |
| 129 | 131 | |
| 130 | - // Send partial output to UI | |
| 131 | - if (extras.onPartialOutput) { | |
| 132 | - const status = waitForCompletion | |
| 133 | - ? "" | |
| 134 | - : "Command is running in the background..."; | |
| 135 | - extras.onPartialOutput({ | |
| 136 | - toolCallId, | |
| 137 | - contextItems: [ | |
| 138 | - { | |
| 139 | - name: "Terminal", | |
| 140 | - description: "Terminal command output", | |
| 141 | - content: terminalOutput, | |
| 142 | - status: status, | |
| 143 | - }, | |
| 144 | - ], | |
| 145 | - }); | |
| 146 | - } | |
| 132 | + childProc.stdout?.on("data", (data) => { | |
| 133 | + // Skip if this process has been backgrounded | |
| 134 | + if (isProcessBackgrounded(toolCallId)) return; | |
| 135 | + | |
| 136 | + const newOutput = getDecodedOutput(data); | |
| 137 | + terminalOutput += newOutput; | |
| 138 | + | |
| 139 | + // Send partial output to UI | |
| 140 | + if (extras.onPartialOutput) { | |
| 141 | + const status = waitForCompletion | |
| 142 | + ? "" | |
| 143 | + : "Command is running in the background..."; | |
| 144 | + extras.onPartialOutput({ | |
| 145 | + toolCallId, | |
| 146 | + contextItems: [ | |
| 147 | + { | |
| 148 | + name: "Terminal", | |
| 149 | + description: "Terminal command output", | |
| 150 | + content: terminalOutput, | |
| 151 | + status: status, | |
| 152 | + }, | |
| 153 | + ], | |
| 147 | 154 | }); |
| 155 | + } | |
| 156 | + }); | |
| 148 | 157 | |
| 149 | - childProc.stderr?.on("data", (data) => { | |
| 150 | - // Skip if this process has been backgrounded | |
| 151 | - if (isProcessBackgrounded(toolCallId)) return; | |
| 158 | + childProc.stderr?.on("data", (data) => { | |
| 159 | + // Skip if this process has been backgrounded | |
| 160 | + if (isProcessBackgrounded(toolCallId)) return; | |
| 161 | + | |
| 162 | + const newOutput = getDecodedOutput(data); | |
| 163 | + terminalOutput += newOutput; | |
| 164 | + | |
| 165 | + // Send partial output to UI, status is not required | |
| 166 | + if (extras.onPartialOutput) { | |
| 167 | + extras.onPartialOutput({ | |
| 168 | + toolCallId, | |
| 169 | + contextItems: [ | |
| 170 | + { | |
| 171 | + name: "Terminal", | |
| 172 | + description: "Terminal command output", | |
| 173 | + content: terminalOutput, | |
| 174 | + }, | |
| 175 | + ], | |
| 176 | + }); | |
| 177 | + } | |
| 178 | + }); | |
| 152 | 179 | |
| 153 | - const newOutput = getDecodedOutput(data); | |
| 154 | - terminalOutput += newOutput; | |
| 180 | + // If we don't need to wait for completion, resolve immediately | |
| 181 | + if (!waitForCompletion) { | |
| 182 | + const status = "Command is running in the background..."; | |
| 183 | + resolve([ | |
| 184 | + { | |
| 185 | + name: "Terminal", | |
| 186 | + description: "Terminal command output", | |
| 187 | + content: terminalOutput, | |
| 188 | + status: status, | |
| 189 | + }, | |
| 190 | + ]); | |
| 191 | + } | |
| 155 | 192 | |
| 156 | - // Send partial output to UI, status is not required | |
| 157 | - if (extras.onPartialOutput) { | |
| 158 | - extras.onPartialOutput({ | |
| 159 | - toolCallId, | |
| 160 | - contextItems: [ | |
| 161 | - { | |
| 162 | - name: "Terminal", | |
| 163 | - description: "Terminal command output", | |
| 164 | - content: terminalOutput, | |
| 165 | - }, | |
| 166 | - ], | |
| 167 | - }); | |
| 168 | - } | |
| 169 | - }); | |
| 193 | + childProc.on("close", (code) => { | |
| 194 | + // If this process has been backgrounded, clean it up from the map and return | |
| 195 | + if (isProcessBackgrounded(toolCallId)) { | |
| 196 | + removeBackgroundedProcess(toolCallId); | |
| 197 | + return; | |
| 198 | + } | |
| 170 | 199 | |
| 171 | - // If we don't need to wait for completion, resolve immediately | |
| 172 | - if (!waitForCompletion) { | |
| 173 | - const status = "Command is running in the background..."; | |
| 200 | + if (waitForCompletion) { | |
| 201 | + // Normal completion, resolve now | |
| 202 | + if (code === 0) { | |
| 203 | + const status = "Command completed"; | |
| 204 | + resolve([ | |
| 205 | + { | |
| 206 | + name: "Terminal", | |
| 207 | + description: "Terminal command output", | |
| 208 | + content: terminalOutput, | |
| 209 | + status: status, | |
| 210 | + }, | |
| 211 | + ]); | |
| 212 | + } else { | |
| 213 | + const status = `Command failed with exit code ${code}`; | |
| 174 | 214 | resolve([ |
| 175 | 215 | { |
| 176 | 216 | name: "Terminal", |
| @@ -180,76 +220,45 @@ | ||
| 180 | 220 | }, |
| 181 | 221 | ]); |
| 182 | 222 | } |
| 223 | + } else { | |
| 224 | + // Already resolved, just update the UI with final output | |
| 225 | + if (extras.onPartialOutput) { | |
| 226 | + const status = | |
| 227 | + code === 0 || !code | |
| 228 | + ? "\nBackground command completed" | |
| 229 | + : `\nBackground command failed with exit code ${code}`; | |
| 230 | + extras.onPartialOutput({ | |
| 231 | + toolCallId, | |
| 232 | + contextItems: [ | |
| 233 | + { | |
| 234 | + name: "Terminal", | |
| 235 | + description: "Terminal command output", | |
| 236 | + content: terminalOutput, | |
| 237 | + status: status, | |
| 238 | + }, | |
| 239 | + ], | |
| 240 | + }); | |
| 241 | + } | |
| 242 | + } | |
| 243 | + }); | |
| 183 | 244 | |
| 184 | - childProc.on("close", (code) => { | |
| 185 | - // If this process has been backgrounded, clean it up from the map and return | |
| 186 | - if (isProcessBackgrounded(toolCallId)) { | |
| 187 | - removeBackgroundedProcess(toolCallId); | |
| 188 | - return; | |
| 189 | - } | |
| 190 | - | |
| 191 | - if (waitForCompletion) { | |
| 192 | - // Normal completion, resolve now | |
| 193 | - if (code === 0) { | |
| 194 | - const status = "Command completed"; | |
| 195 | - resolve([ | |
| 196 | - { | |
| 197 | - name: "Terminal", | |
| 198 | - description: "Terminal command output", | |
| 199 | - content: terminalOutput, | |
| 200 | - status: status, | |
| 201 | - }, | |
| 202 | - ]); | |
| 203 | - } else { | |
| 204 | - const status = `Command failed with exit code ${code}`; | |
| 205 | - resolve([ | |
| 206 | - { | |
| 207 | - name: "Terminal", | |
| 208 | - description: "Terminal command output", | |
| 209 | - content: terminalOutput, | |
| 210 | - status: status, | |
| 211 | - }, | |
| 212 | - ]); | |
| 213 | - } | |
| 214 | - } else { | |
| 215 | - // Already resolved, just update the UI with final output | |
| 216 | - if (extras.onPartialOutput) { | |
| 217 | - const status = | |
| 218 | - code === 0 || !code | |
| 219 | - ? "\nBackground command completed" | |
| 220 | - : `\nBackground command failed with exit code ${code}`; | |
| 221 | - extras.onPartialOutput({ | |
| 222 | - toolCallId, | |
| 223 | - contextItems: [ | |
| 224 | - { | |
| 225 | - name: "Terminal", | |
| 226 | - description: "Terminal command output", | |
| 227 | - content: terminalOutput, | |
| 228 | - status: status, | |
| 229 | - }, | |
| 230 | - ], | |
| 231 | - }); | |
| 232 | - } | |
| 233 | - } | |
| 234 | - }); | |
| 235 | - | |
| 236 | - childProc.on("error", (error) => { | |
| 237 | - // If this process has been backgrounded, clean it up from the map and return | |
| 238 | - if (isProcessBackgrounded(toolCallId)) { | |
| 239 | - removeBackgroundedProcess(toolCallId); | |
| 240 | - return; | |
| 241 | - } | |
| 245 | + childProc.on("error", (error) => { | |
| 246 | + // If this process has been backgrounded, clean it up from the map and return | |
| 247 | + if (isProcessBackgrounded(toolCallId)) { | |
| 248 | + removeBackgroundedProcess(toolCallId); | |
| 249 | + return; | |
| 250 | + } | |
| 242 | 251 | |
| 243 | - reject(error); | |
| 244 | - }); | |
| 245 | - }); | |
| 252 | + reject(error); | |
| 253 | + }); | |
| 254 | + }); | |
| 246 | 255 | } catch (error: any) { |
| 247 | 256 | throw error; |
| 248 | 257 | } |
| 249 | 258 | } else { |
| 250 | 259 | // Fallback to non-streaming for older clients |
| 251 | 260 | const workspaceDirs = await extras.ide.getWorkspaceDirs(); |
| 252 | - | |
| 261 | + | |
| 253 | 262 | // Handle case where no workspace is available |
| 254 | 263 | let cwd: string; |
| 255 | 264 | if (workspaceDirs.length > 0) { |
| @@ -397,4 +406,4 @@ | ||
| 397 | 406 | status: "Command failed", |
| 398 | 407 | }, |
| 399 | 408 | ]; |
| 400 | -}; | |
| No newline at the end of the file | ||
| 409 | +}; |