Changesets · Branches · Tags · Compare
| Changeset | 8e0cf6e8890ea14dc702a50e59ffb6e8590986b71fdfec14b537acb82c1dfe82 |
|---|---|
| Author | Dallin Romney <dallinromney@gmail.com> · 2025-09-10 18:39 -07:00 |
| Committer | GitHub <noreply@github.com> · 2025-09-10 18:39 -07:00 |
| Parents | 177d3bbe4f2a, f2cd634ac353 |
16 files changed
modified core/core.ts +6 −1
| @@ -29,8 +29,9 @@ | ||
| 29 | 29 | import { Telemetry } from "./util/posthog"; |
| 30 | 30 | import { |
| 31 | 31 | isProcessBackgrounded, |
| 32 | + killTerminalProcess, | |
| 32 | 33 | markProcessAsBackgrounded, |
| 33 | -} from "./util/processTerminalBackgroundStates"; | |
| 34 | +} from "./util/processTerminalStates"; | |
| 34 | 35 | import { getSymbolsForManyFiles } from "./util/treeSitter"; |
| 35 | 36 | import { TTS } from "./util/tts"; |
| 36 | 37 | |
| @@ -1062,6 +1063,10 @@ | ||
| 1062 | 1063 | }, |
| 1063 | 1064 | ); |
| 1064 | 1065 | |
| 1066 | + on("process/killTerminalProcess", async ({ data: { toolCallId } }) => { | |
| 1067 | + await killTerminalProcess(toolCallId); | |
| 1068 | + }); | |
| 1069 | + | |
| 1065 | 1070 | on("mdm/setLicenseKey", ({ data: { licenseKey } }) => { |
| 1066 | 1071 | const isValid = setMdmLicenseKey(licenseKey); |
| 1067 | 1072 | return isValid; |
modified core/protocol/core.ts +1 −0
| @@ -313,5 +313,6 @@ | ||
| 313 | 313 | ]; |
| 314 | 314 | "process/markAsBackgrounded": [{ toolCallId: string }, void]; |
| 315 | 315 | "process/isBackgrounded": [{ toolCallId: string }, boolean]; |
| 316 | + "process/killTerminalProcess": [{ toolCallId: string }, void]; | |
| 316 | 317 | "mdm/setLicenseKey": [{ licenseKey: string }, boolean]; |
| 317 | 318 | }; |
modified core/protocol/passThrough.ts +1 −0
| @@ -86,6 +86,7 @@ | ||
| 86 | 86 | "isItemTooBig", |
| 87 | 87 | "process/markAsBackgrounded", |
| 88 | 88 | "process/isBackgrounded", |
| 89 | + "process/killTerminalProcess", | |
| 89 | 90 | ]; |
| 90 | 91 | |
| 91 | 92 | // Message types to pass through from core to webview |
modified core/tools/implementations/runTerminalCommand.ts +55 −10
| @@ -36,8 +36,11 @@ | ||
| 36 | 36 | import { ToolImpl } from "."; |
| 37 | 37 | import { |
| 38 | 38 | isProcessBackgrounded, |
| 39 | + markProcessAsRunning, | |
| 39 | 40 | removeBackgroundedProcess, |
| 40 | -} from "../../util/processTerminalBackgroundStates"; | |
| 41 | + removeRunningProcess, | |
| 42 | + updateProcessOutput, | |
| 43 | +} from "../../util/processTerminalStates"; | |
| 41 | 44 | import { getBooleanArg, getStringArg } from "../parseArgs"; |
| 42 | 45 | |
| 43 | 46 | const asyncExec = util.promisify(childProcess.exec); |
| @@ -120,6 +123,16 @@ | ||
| 120 | 123 | env: getColorEnv(), // Add enhanced environment for colors |
| 121 | 124 | }); |
| 122 | 125 | |
| 126 | + // Track this process for foreground cancellation | |
| 127 | + if (toolCallId && waitForCompletion) { | |
| 128 | + markProcessAsRunning( | |
| 129 | + toolCallId, | |
| 130 | + childProc, | |
| 131 | + extras.onPartialOutput, | |
| 132 | + terminalOutput, | |
| 133 | + ); | |
| 134 | + } | |
| 135 | + | |
| 123 | 136 | childProc.stdout?.on("data", (data) => { |
| 124 | 137 | // Skip if this process has been backgrounded |
| 125 | 138 | if (isProcessBackgrounded(toolCallId)) return; |
| @@ -127,6 +140,11 @@ | ||
| 127 | 140 | const newOutput = getDecodedOutput(data); |
| 128 | 141 | terminalOutput += newOutput; |
| 129 | 142 | |
| 143 | + // Update the tracked output for potential cancellation notifications | |
| 144 | + if (toolCallId && waitForCompletion) { | |
| 145 | + updateProcessOutput(toolCallId, terminalOutput); | |
| 146 | + } | |
| 147 | + | |
| 130 | 148 | // Send partial output to UI |
| 131 | 149 | if (extras.onPartialOutput) { |
| 132 | 150 | const status = waitForCompletion |
| @@ -153,6 +171,11 @@ | ||
| 153 | 171 | const newOutput = getDecodedOutput(data); |
| 154 | 172 | terminalOutput += newOutput; |
| 155 | 173 | |
| 174 | + // Update the tracked output for potential cancellation notifications | |
| 175 | + if (toolCallId && waitForCompletion) { | |
| 176 | + updateProcessOutput(toolCallId, terminalOutput); | |
| 177 | + } | |
| 178 | + | |
| 156 | 179 | // Send partial output to UI, status is not required |
| 157 | 180 | if (extras.onPartialOutput) { |
| 158 | 181 | extras.onPartialOutput({ |
| @@ -182,15 +205,19 @@ | ||
| 182 | 205 | } |
| 183 | 206 | |
| 184 | 207 | 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; | |
| 208 | + // Clean up process tracking | |
| 209 | + if (toolCallId) { | |
| 210 | + if (isProcessBackgrounded(toolCallId)) { | |
| 211 | + removeBackgroundedProcess(toolCallId); | |
| 212 | + return; | |
| 213 | + } | |
| 214 | + // Remove from foreground tracking if it was tracked | |
| 215 | + removeRunningProcess(toolCallId); | |
| 189 | 216 | } |
| 190 | 217 | |
| 191 | 218 | if (waitForCompletion) { |
| 192 | 219 | // Normal completion, resolve now |
| 193 | - if (code === 0) { | |
| 220 | + if (!code || code === 0) { | |
| 194 | 221 | const status = "Command completed"; |
| 195 | 222 | resolve([ |
| 196 | 223 | { |
| @@ -234,10 +261,14 @@ | ||
| 234 | 261 | }); |
| 235 | 262 | |
| 236 | 263 | 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; | |
| 264 | + // Clean up process tracking | |
| 265 | + if (toolCallId) { | |
| 266 | + if (isProcessBackgrounded(toolCallId)) { | |
| 267 | + removeBackgroundedProcess(toolCallId); | |
| 268 | + return; | |
| 269 | + } | |
| 270 | + // Remove from foreground tracking if it was tracked | |
| 271 | + removeRunningProcess(toolCallId); | |
| 241 | 272 | } |
| 242 | 273 | |
| 243 | 274 | reject(error); |
| @@ -281,6 +312,11 @@ | ||
| 281 | 312 | }, |
| 282 | 313 | ); |
| 283 | 314 | |
| 315 | + // Track this process for foreground cancellation | |
| 316 | + if (toolCallId) { | |
| 317 | + markProcessAsRunning(toolCallId, childProc, undefined, ""); | |
| 318 | + } | |
| 319 | + | |
| 284 | 320 | let stdout = ""; |
| 285 | 321 | let stderr = ""; |
| 286 | 322 | |
| @@ -293,6 +329,11 @@ | ||
| 293 | 329 | }); |
| 294 | 330 | |
| 295 | 331 | childProc.on("close", (code) => { |
| 332 | + // Clean up process tracking | |
| 333 | + if (toolCallId) { | |
| 334 | + removeRunningProcess(toolCallId); | |
| 335 | + } | |
| 336 | + | |
| 296 | 337 | if (code === 0) { |
| 297 | 338 | resolve({ stdout, stderr }); |
| 298 | 339 | } else { |
| @@ -305,6 +346,10 @@ | ||
| 305 | 346 | }); |
| 306 | 347 | |
| 307 | 348 | childProc.on("error", (error) => { |
| 349 | + // Clean up process tracking | |
| 350 | + if (toolCallId) { | |
| 351 | + removeRunningProcess(toolCallId); | |
| 352 | + } | |
| 308 | 353 | reject(error); |
| 309 | 354 | }); |
| 310 | 355 | }, |
modified core/tools/implementations/runTerminalCommand.vitest.ts +3 −7
| @@ -12,7 +12,7 @@ | ||
| 12 | 12 | import * as os from "node:os"; |
| 13 | 13 | import * as path from "node:path"; |
| 14 | 14 | import { IDE, ToolExtras } from "../.."; |
| 15 | -import * as processBackgroundStates from "../../util/processTerminalBackgroundStates"; | |
| 15 | +import * as processTerminalStates from "../../util/processTerminalStates"; | |
| 16 | 16 | import { runTerminalCommandImpl } from "./runTerminalCommand"; |
| 17 | 17 | import { runTerminalCommandTool } from "../definitions/runTerminalCommand"; |
| 18 | 18 | |
| @@ -39,12 +39,8 @@ | ||
| 39 | 39 | vi.resetAllMocks(); |
| 40 | 40 | |
| 41 | 41 | // Setup backgrounded processes handling - don't mock, just make sure it's empty |
| 42 | - // Get any processes that might be already tracked and clear them | |
| 43 | - const processMap = (processBackgroundStates as any) | |
| 44 | - .processTerminalBackgroundStates; | |
| 45 | - if (processMap && typeof processMap.clear === "function") { | |
| 46 | - processMap.clear(); | |
| 47 | - } | |
| 42 | + // Clear any processes that might be already tracked | |
| 43 | + processTerminalStates.clearAllBackgroundProcesses(); | |
| 48 | 44 | |
| 49 | 45 | // Setup IDE mocks |
| 50 | 46 | mockGetIdeInfo.mockReturnValue(Promise.resolve({ remoteName: "local" })); |
modified core/util/messageContent.ts +15 −0
| @@ -35,6 +35,21 @@ | ||
| 35 | 35 | return contextItems.map((item) => item.content).join("\n\n"); |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | +export function renderContextItemsWithStatus(contextItems: any[]): string { | |
| 39 | + return contextItems | |
| 40 | + .map((item) => { | |
| 41 | + let result = item.content; | |
| 42 | + | |
| 43 | + // If this item has a status, append it directly after the content | |
| 44 | + if (item.status) { | |
| 45 | + result += `\n[Status: ${item.status}]`; | |
| 46 | + } | |
| 47 | + | |
| 48 | + return result; | |
| 49 | + }) | |
| 50 | + .join("\n\n"); | |
| 51 | +} | |
| 52 | + | |
| 38 | 53 | export function normalizeToMessageParts(message: ChatMessage): MessagePart[] { |
| 39 | 54 | switch (message.role) { |
| 40 | 55 | case "user": |
deleted core/util/processTerminalBackgroundStates.test.ts +0 −73
| @@ -1,73 +0,0 @@ | ||
| 1 | -import { | |
| 2 | - markProcessAsBackgrounded, | |
| 3 | - isProcessBackgrounded, | |
| 4 | - removeBackgroundedProcess, | |
| 5 | -} from "./processTerminalBackgroundStates"; | |
| 6 | - | |
| 7 | -describe("processTerminalBackgroundStates", () => { | |
| 8 | - beforeEach(() => { | |
| 9 | - // Clear any backgrounded processes before each test | |
| 10 | - const backgroundedProcesses = Array.from( | |
| 11 | - Object.getOwnPropertySymbols(global), | |
| 12 | - ).find( | |
| 13 | - (symbol) => String(symbol) === "Symbol(processTerminalBackgroundStates)", | |
| 14 | - ); | |
| 15 | - | |
| 16 | - if (backgroundedProcesses) { | |
| 17 | - // Reset the map by reimporting the module | |
| 18 | - jest.resetModules(); | |
| 19 | - } | |
| 20 | - }); | |
| 21 | - | |
| 22 | - test("should mark a process as backgrounded", () => { | |
| 23 | - const toolCallId = "test-123"; | |
| 24 | - markProcessAsBackgrounded(toolCallId); | |
| 25 | - expect(isProcessBackgrounded(toolCallId)).toBe(true); | |
| 26 | - }); | |
| 27 | - | |
| 28 | - test("should correctly identify non-backgrounded processes", () => { | |
| 29 | - const toolCallId = "test-123"; | |
| 30 | - const anotherToolCallId = "test-456"; | |
| 31 | - | |
| 32 | - markProcessAsBackgrounded(toolCallId); | |
| 33 | - | |
| 34 | - expect(isProcessBackgrounded(toolCallId)).toBe(true); | |
| 35 | - expect(isProcessBackgrounded(anotherToolCallId)).toBe(false); | |
| 36 | - }); | |
| 37 | - | |
| 38 | - test("should remove a process from backgrounded state", () => { | |
| 39 | - const toolCallId = "test-123"; | |
| 40 | - | |
| 41 | - markProcessAsBackgrounded(toolCallId); | |
| 42 | - expect(isProcessBackgrounded(toolCallId)).toBe(true); | |
| 43 | - | |
| 44 | - removeBackgroundedProcess(toolCallId); | |
| 45 | - expect(isProcessBackgrounded(toolCallId)).toBe(false); | |
| 46 | - }); | |
| 47 | - | |
| 48 | - test("should handle removing non-existent processes", () => { | |
| 49 | - const toolCallId = "test-123"; | |
| 50 | - | |
| 51 | - // Should not throw an error | |
| 52 | - removeBackgroundedProcess(toolCallId); | |
| 53 | - expect(isProcessBackgrounded(toolCallId)).toBe(false); | |
| 54 | - }); | |
| 55 | - | |
| 56 | - test("should handle multiple processes", () => { | |
| 57 | - const toolCallId1 = "test-123"; | |
| 58 | - const toolCallId2 = "test-456"; | |
| 59 | - const toolCallId3 = "test-789"; | |
| 60 | - | |
| 61 | - markProcessAsBackgrounded(toolCallId1); | |
| 62 | - markProcessAsBackgrounded(toolCallId2); | |
| 63 | - | |
| 64 | - expect(isProcessBackgrounded(toolCallId1)).toBe(true); | |
| 65 | - expect(isProcessBackgrounded(toolCallId2)).toBe(true); | |
| 66 | - expect(isProcessBackgrounded(toolCallId3)).toBe(false); | |
| 67 | - | |
| 68 | - removeBackgroundedProcess(toolCallId1); | |
| 69 | - | |
| 70 | - expect(isProcessBackgrounded(toolCallId1)).toBe(false); | |
| 71 | - expect(isProcessBackgrounded(toolCallId2)).toBe(true); | |
| 72 | - }); | |
| 73 | -}); |
deleted core/util/processTerminalBackgroundStates.ts +0 −17
| @@ -1,17 +0,0 @@ | ||
| 1 | -// Track which processes have been backgrounded | |
| 2 | -const processTerminalBackgroundStates = new Map<string, boolean>(); | |
| 3 | - | |
| 4 | -// Function to mark a process as backgrounded | |
| 5 | -export function markProcessAsBackgrounded(toolCallId: string): void { | |
| 6 | - processTerminalBackgroundStates.set(toolCallId, true); | |
| 7 | -} | |
| 8 | - | |
| 9 | -// Function to check if a process is backgrounded | |
| 10 | -export function isProcessBackgrounded(toolCallId: string): boolean { | |
| 11 | - return processTerminalBackgroundStates.has(toolCallId); | |
| 12 | -} | |
| 13 | - | |
| 14 | -// Function to remove a process from the backgrounded processes map | |
| 15 | -export function removeBackgroundedProcess(toolCallId: string): void { | |
| 16 | - processTerminalBackgroundStates.delete(toolCallId); | |
| 17 | -} |
added core/util/processTerminalStates.test.ts +367 −0
| @@ -0,0 +1,367 @@ | ||
| 1 | +import { ChildProcess } from "child_process"; | |
| 2 | +import { | |
| 3 | + clearAllBackgroundProcesses, | |
| 4 | + getAllBackgroundedProcessIds, | |
| 5 | + getAllRunningProcessIds, | |
| 6 | + getRunningProcess, | |
| 7 | + isProcessBackgrounded, | |
| 8 | + isProcessRunning, | |
| 9 | + killAllRunningTerminalProcesses, | |
| 10 | + killMultipleTerminalProcesses, | |
| 11 | + killTerminalProcess, | |
| 12 | + markProcessAsBackgrounded, | |
| 13 | + markProcessAsRunning, | |
| 14 | + removeBackgroundedProcess, | |
| 15 | + removeRunningProcess, | |
| 16 | + updateProcessOutput, | |
| 17 | +} from "./processTerminalStates"; | |
| 18 | + | |
| 19 | +// Mock ChildProcess | |
| 20 | +const createMockProcess = ( | |
| 21 | + pid: number = 123, | |
| 22 | + killed: boolean = false, | |
| 23 | +): jest.Mocked<ChildProcess> => { | |
| 24 | + const mockProcess = { | |
| 25 | + pid, | |
| 26 | + killed, | |
| 27 | + kill: jest.fn(), | |
| 28 | + } as unknown as jest.Mocked<ChildProcess>; | |
| 29 | + | |
| 30 | + // Make kill() update the killed property | |
| 31 | + mockProcess.kill.mockImplementation(() => { | |
| 32 | + (mockProcess as any).killed = true; | |
| 33 | + return true; | |
| 34 | + }); | |
| 35 | + | |
| 36 | + return mockProcess; | |
| 37 | +}; | |
| 38 | + | |
| 39 | +describe("processTerminalStates", () => { | |
| 40 | + beforeEach(() => { | |
| 41 | + // Clear all running processes | |
| 42 | + const runningIds = getAllRunningProcessIds(); | |
| 43 | + runningIds.forEach((id) => removeRunningProcess(id)); | |
| 44 | + | |
| 45 | + // Clear all backgrounded processes | |
| 46 | + const backgroundedIds = getAllBackgroundedProcessIds(); | |
| 47 | + backgroundedIds.forEach((id) => removeBackgroundedProcess(id)); | |
| 48 | + | |
| 49 | + // Clear all timers | |
| 50 | + jest.clearAllTimers(); | |
| 51 | + }); | |
| 52 | + | |
| 53 | + test("should mark a process as backgrounded", () => { | |
| 54 | + const toolCallId = "test-123"; | |
| 55 | + markProcessAsBackgrounded(toolCallId); | |
| 56 | + expect(isProcessBackgrounded(toolCallId)).toBe(true); | |
| 57 | + }); | |
| 58 | + | |
| 59 | + test("should correctly identify non-backgrounded processes", () => { | |
| 60 | + const toolCallId = "test-123"; | |
| 61 | + const anotherToolCallId = "test-456"; | |
| 62 | + | |
| 63 | + markProcessAsBackgrounded(toolCallId); | |
| 64 | + | |
| 65 | + expect(isProcessBackgrounded(toolCallId)).toBe(true); | |
| 66 | + expect(isProcessBackgrounded(anotherToolCallId)).toBe(false); | |
| 67 | + }); | |
| 68 | + | |
| 69 | + test("should remove a process from backgrounded state", () => { | |
| 70 | + const toolCallId = "test-123"; | |
| 71 | + | |
| 72 | + markProcessAsBackgrounded(toolCallId); | |
| 73 | + expect(isProcessBackgrounded(toolCallId)).toBe(true); | |
| 74 | + | |
| 75 | + removeBackgroundedProcess(toolCallId); | |
| 76 | + expect(isProcessBackgrounded(toolCallId)).toBe(false); | |
| 77 | + }); | |
| 78 | + | |
| 79 | + test("should handle removing non-existent processes", () => { | |
| 80 | + const toolCallId = "test-123"; | |
| 81 | + | |
| 82 | + // Should not throw an error | |
| 83 | + removeBackgroundedProcess(toolCallId); | |
| 84 | + expect(isProcessBackgrounded(toolCallId)).toBe(false); | |
| 85 | + }); | |
| 86 | + | |
| 87 | + test("should handle multiple processes", () => { | |
| 88 | + const toolCallId1 = "test-123"; | |
| 89 | + const toolCallId2 = "test-456"; | |
| 90 | + const toolCallId3 = "test-789"; | |
| 91 | + | |
| 92 | + markProcessAsBackgrounded(toolCallId1); | |
| 93 | + markProcessAsBackgrounded(toolCallId2); | |
| 94 | + | |
| 95 | + expect(isProcessBackgrounded(toolCallId1)).toBe(true); | |
| 96 | + expect(isProcessBackgrounded(toolCallId2)).toBe(true); | |
| 97 | + expect(isProcessBackgrounded(toolCallId3)).toBe(false); | |
| 98 | + | |
| 99 | + removeBackgroundedProcess(toolCallId1); | |
| 100 | + | |
| 101 | + expect(isProcessBackgrounded(toolCallId1)).toBe(false); | |
| 102 | + expect(isProcessBackgrounded(toolCallId2)).toBe(true); | |
| 103 | + }); | |
| 104 | + | |
| 105 | + describe("foreground process functions", () => { | |
| 106 | + test("should mark a process as running", () => { | |
| 107 | + const toolCallId = "test-123"; | |
| 108 | + const mockProcess = createMockProcess(); | |
| 109 | + const mockCallback = jest.fn(); | |
| 110 | + const initialOutput = "initial output"; | |
| 111 | + | |
| 112 | + markProcessAsRunning( | |
| 113 | + toolCallId, | |
| 114 | + mockProcess, | |
| 115 | + mockCallback, | |
| 116 | + initialOutput, | |
| 117 | + ); | |
| 118 | + | |
| 119 | + expect(isProcessRunning(toolCallId)).toBe(true); | |
| 120 | + expect(getRunningProcess(toolCallId)).toBe(mockProcess); | |
| 121 | + }); | |
| 122 | + | |
| 123 | + test("should handle process without callback or initial output", () => { | |
| 124 | + const toolCallId = "test-123"; | |
| 125 | + const mockProcess = createMockProcess(); | |
| 126 | + | |
| 127 | + markProcessAsRunning(toolCallId, mockProcess); | |
| 128 | + | |
| 129 | + expect(isProcessRunning(toolCallId)).toBe(true); | |
| 130 | + expect(getRunningProcess(toolCallId)).toBe(mockProcess); | |
| 131 | + }); | |
| 132 | + | |
| 133 | + test("should correctly identify non-running processes", () => { | |
| 134 | + const toolCallId = "test-123"; | |
| 135 | + const anotherToolCallId = "test-456"; | |
| 136 | + const mockProcess = createMockProcess(); | |
| 137 | + | |
| 138 | + markProcessAsRunning(toolCallId, mockProcess); | |
| 139 | + | |
| 140 | + expect(isProcessRunning(toolCallId)).toBe(true); | |
| 141 | + expect(isProcessRunning(anotherToolCallId)).toBe(false); | |
| 142 | + }); | |
| 143 | + | |
| 144 | + test("should update process output", () => { | |
| 145 | + const toolCallId = "test-123"; | |
| 146 | + const mockProcess = createMockProcess(); | |
| 147 | + const initialOutput = "initial"; | |
| 148 | + const updatedOutput = "updated output"; | |
| 149 | + | |
| 150 | + markProcessAsRunning(toolCallId, mockProcess, undefined, initialOutput); | |
| 151 | + updateProcessOutput(toolCallId, updatedOutput); | |
| 152 | + | |
| 153 | + // We can't directly test the output value, but we can ensure the function doesn't throw | |
| 154 | + expect(() => | |
| 155 | + updateProcessOutput(toolCallId, updatedOutput), | |
| 156 | + ).not.toThrow(); | |
| 157 | + }); | |
| 158 | + | |
| 159 | + test("should handle updating output for non-existent process", () => { | |
| 160 | + const toolCallId = "non-existent"; | |
| 161 | + | |
| 162 | + expect(() => | |
| 163 | + updateProcessOutput(toolCallId, "some output"), | |
| 164 | + ).not.toThrow(); | |
| 165 | + }); | |
| 166 | + | |
| 167 | + test("should remove a process from running state", () => { | |
| 168 | + const toolCallId = "test-123"; | |
| 169 | + const mockProcess = createMockProcess(); | |
| 170 | + | |
| 171 | + markProcessAsRunning(toolCallId, mockProcess); | |
| 172 | + expect(isProcessRunning(toolCallId)).toBe(true); | |
| 173 | + | |
| 174 | + removeRunningProcess(toolCallId); | |
| 175 | + expect(isProcessRunning(toolCallId)).toBe(false); | |
| 176 | + expect(getRunningProcess(toolCallId)).toBeUndefined(); | |
| 177 | + }); | |
| 178 | + | |
| 179 | + test("should handle removing non-existent running processes", () => { | |
| 180 | + const toolCallId = "test-123"; | |
| 181 | + | |
| 182 | + expect(() => removeRunningProcess(toolCallId)).not.toThrow(); | |
| 183 | + expect(isProcessRunning(toolCallId)).toBe(false); | |
| 184 | + }); | |
| 185 | + | |
| 186 | + test("should return undefined for non-existent running process", () => { | |
| 187 | + const toolCallId = "non-existent"; | |
| 188 | + | |
| 189 | + expect(getRunningProcess(toolCallId)).toBeUndefined(); | |
| 190 | + }); | |
| 191 | + }); | |
| 192 | + | |
| 193 | + describe("terminal command cancellation", () => { | |
| 194 | + beforeEach(() => { | |
| 195 | + jest.useFakeTimers(); | |
| 196 | + }); | |
| 197 | + | |
| 198 | + afterEach(() => { | |
| 199 | + jest.useRealTimers(); | |
| 200 | + }); | |
| 201 | + | |
| 202 | + test("should cancel a running terminal command", async () => { | |
| 203 | + const toolCallId = "test-123"; | |
| 204 | + const mockProcess = createMockProcess(); | |
| 205 | + | |
| 206 | + markProcessAsRunning(toolCallId, mockProcess); | |
| 207 | + expect(isProcessRunning(toolCallId)).toBe(true); | |
| 208 | + | |
| 209 | + await killTerminalProcess(toolCallId); | |
| 210 | + | |
| 211 | + expect(mockProcess.kill).toHaveBeenCalledWith("SIGTERM"); | |
| 212 | + expect(isProcessRunning(toolCallId)).toBe(false); | |
| 213 | + }); | |
| 214 | + | |
| 215 | + test("should handle cancelling non-existent process", async () => { | |
| 216 | + const toolCallId = "non-existent"; | |
| 217 | + | |
| 218 | + await expect(killTerminalProcess(toolCallId)).resolves.not.toThrow(); | |
| 219 | + }); | |
| 220 | + | |
| 221 | + test("should handle cancelling already killed process", async () => { | |
| 222 | + const toolCallId = "test-123"; | |
| 223 | + const mockProcess = createMockProcess(123, true); // Already killed | |
| 224 | + | |
| 225 | + markProcessAsRunning(toolCallId, mockProcess); | |
| 226 | + | |
| 227 | + await killTerminalProcess(toolCallId); | |
| 228 | + | |
| 229 | + expect(mockProcess.kill).not.toHaveBeenCalled(); | |
| 230 | + }); | |
| 231 | + | |
| 232 | + test("should send SIGKILL after timeout if process not killed", async () => { | |
| 233 | + const toolCallId = "test-123"; | |
| 234 | + const mockProcess = createMockProcess(); | |
| 235 | + | |
| 236 | + // Mock kill to not actually set killed=true for SIGTERM | |
| 237 | + mockProcess.kill.mockImplementation((signal) => { | |
| 238 | + if (signal === "SIGKILL") { | |
| 239 | + (mockProcess as any).killed = true; | |
| 240 | + } | |
| 241 | + return true; | |
| 242 | + }); | |
| 243 | + | |
| 244 | + markProcessAsRunning(toolCallId, mockProcess); | |
| 245 | + | |
| 246 | + const cancelPromise = killTerminalProcess(toolCallId); | |
| 247 | + | |
| 248 | + // Fast-forward time to trigger the timeout | |
| 249 | + jest.advanceTimersByTime(5000); | |
| 250 | + | |
| 251 | + await cancelPromise; | |
| 252 | + | |
| 253 | + expect(mockProcess.kill).toHaveBeenCalledWith("SIGTERM"); | |
| 254 | + expect(mockProcess.kill).toHaveBeenCalledWith("SIGKILL"); | |
| 255 | + }); | |
| 256 | + | |
| 257 | + test("should not send SIGKILL if process was already killed", async () => { | |
| 258 | + const toolCallId = "test-123"; | |
| 259 | + const mockProcess = createMockProcess(); | |
| 260 | + | |
| 261 | + markProcessAsRunning(toolCallId, mockProcess); | |
| 262 | + | |
| 263 | + const cancelPromise = killTerminalProcess(toolCallId); | |
| 264 | + | |
| 265 | + // Fast-forward time to trigger the timeout | |
| 266 | + jest.advanceTimersByTime(5000); | |
| 267 | + | |
| 268 | + await cancelPromise; | |
| 269 | + | |
| 270 | + expect(mockProcess.kill).toHaveBeenCalledWith("SIGTERM"); | |
| 271 | + expect(mockProcess.kill).toHaveBeenCalledTimes(1); // Only SIGTERM, no SIGKILL | |
| 272 | + }); | |
| 273 | + }); | |
| 274 | + | |
| 275 | + describe("utility functions", () => { | |
| 276 | + test("should get all running process IDs", () => { | |
| 277 | + const toolCallId1 = "test-123"; | |
| 278 | + const toolCallId2 = "test-456"; | |
| 279 | + const mockProcess1 = createMockProcess(123); | |
| 280 | + const mockProcess2 = createMockProcess(456); | |
| 281 | + | |
| 282 | + markProcessAsRunning(toolCallId1, mockProcess1); | |
| 283 | + markProcessAsRunning(toolCallId2, mockProcess2); | |
| 284 | + | |
| 285 | + const runningIds = getAllRunningProcessIds(); | |
| 286 | + expect(runningIds).toContain(toolCallId1); | |
| 287 | + expect(runningIds).toContain(toolCallId2); | |
| 288 | + expect(runningIds).toHaveLength(2); | |
| 289 | + }); | |
| 290 | + | |
| 291 | + test("should get all backgrounded process IDs", () => { | |
| 292 | + const toolCallId1 = "test-123"; | |
| 293 | + const toolCallId2 = "test-456"; | |
| 294 | + | |
| 295 | + markProcessAsBackgrounded(toolCallId1); | |
| 296 | + markProcessAsBackgrounded(toolCallId2); | |
| 297 | + | |
| 298 | + const backgroundedIds = getAllBackgroundedProcessIds(); | |
| 299 | + expect(backgroundedIds).toContain(toolCallId1); | |
| 300 | + expect(backgroundedIds).toContain(toolCallId2); | |
| 301 | + expect(backgroundedIds).toHaveLength(2); | |
| 302 | + }); | |
| 303 | + | |
| 304 | + test("should return empty arrays when no processes", () => { | |
| 305 | + expect(getAllRunningProcessIds()).toEqual([]); | |
| 306 | + expect(getAllBackgroundedProcessIds()).toEqual([]); | |
| 307 | + }); | |
| 308 | + | |
| 309 | + test("should cancel multiple terminal commands", async () => { | |
| 310 | + const toolCallId1 = "test-123"; | |
| 311 | + const toolCallId2 = "test-456"; | |
| 312 | + const mockProcess1 = createMockProcess(123); | |
| 313 | + const mockProcess2 = createMockProcess(456); | |
| 314 | + | |
| 315 | + markProcessAsRunning(toolCallId1, mockProcess1); | |
| 316 | + markProcessAsRunning(toolCallId2, mockProcess2); | |
| 317 | + | |
| 318 | + await killMultipleTerminalProcesses([toolCallId1, toolCallId2]); | |
| 319 | + | |
| 320 | + expect(mockProcess1.kill).toHaveBeenCalledWith("SIGTERM"); | |
| 321 | + expect(mockProcess2.kill).toHaveBeenCalledWith("SIGTERM"); | |
| 322 | + expect(isProcessRunning(toolCallId1)).toBe(false); | |
| 323 | + expect(isProcessRunning(toolCallId2)).toBe(false); | |
| 324 | + }); | |
| 325 | + | |
| 326 | + test("should cancel all running terminal commands", async () => { | |
| 327 | + const toolCallId1 = "test-123"; | |
| 328 | + const toolCallId2 = "test-456"; | |
| 329 | + const mockProcess1 = createMockProcess(123); | |
| 330 | + const mockProcess2 = createMockProcess(456); | |
| 331 | + | |
| 332 | + markProcessAsRunning(toolCallId1, mockProcess1); | |
| 333 | + markProcessAsRunning(toolCallId2, mockProcess2); | |
| 334 | + | |
| 335 | + const cancelledIds = await killAllRunningTerminalProcesses(); | |
| 336 | + | |
| 337 | + expect(cancelledIds).toContain(toolCallId1); | |
| 338 | + expect(cancelledIds).toContain(toolCallId2); | |
| 339 | + expect(cancelledIds).toHaveLength(2); | |
| 340 | + expect(mockProcess1.kill).toHaveBeenCalledWith("SIGTERM"); | |
| 341 | + expect(mockProcess2.kill).toHaveBeenCalledWith("SIGTERM"); | |
| 342 | + expect(isProcessRunning(toolCallId1)).toBe(false); | |
| 343 | + expect(isProcessRunning(toolCallId2)).toBe(false); | |
| 344 | + }); | |
| 345 | + | |
| 346 | + test("should return empty array when no running commands to cancel", async () => { | |
| 347 | + const cancelledIds = await killAllRunningTerminalProcesses(); | |
| 348 | + expect(cancelledIds).toEqual([]); | |
| 349 | + }); | |
| 350 | + | |
| 351 | + test("should clear all background processes", () => { | |
| 352 | + const toolCallId1 = "test-123"; | |
| 353 | + const toolCallId2 = "test-456"; | |
| 354 | + | |
| 355 | + markProcessAsBackgrounded(toolCallId1); | |
| 356 | + markProcessAsBackgrounded(toolCallId2); | |
| 357 | + | |
| 358 | + expect(getAllBackgroundedProcessIds()).toHaveLength(2); | |
| 359 | + | |
| 360 | + clearAllBackgroundProcesses(); | |
| 361 | + | |
| 362 | + expect(getAllBackgroundedProcessIds()).toEqual([]); | |
| 363 | + expect(isProcessBackgrounded(toolCallId1)).toBe(false); | |
| 364 | + expect(isProcessBackgrounded(toolCallId2)).toBe(false); | |
| 365 | + }); | |
| 366 | + }); | |
| 367 | +}); |
added core/util/processTerminalStates.ts +119 −0
| @@ -0,0 +1,119 @@ | ||
| 1 | +import { ChildProcess } from "child_process"; | |
| 2 | + | |
| 3 | +// Track which processes have been backgrounded | |
| 4 | +const processTerminalBackgroundStates = new Map<string, boolean>(); | |
| 5 | + | |
| 6 | +// Track which foreground processes are currently running | |
| 7 | +interface RunningProcessInfo { | |
| 8 | + process: ChildProcess; | |
| 9 | + onPartialOutput?: (params: { | |
| 10 | + toolCallId: string; | |
| 11 | + contextItems: any[]; | |
| 12 | + }) => void; | |
| 13 | + currentOutput: string; | |
| 14 | +} | |
| 15 | + | |
| 16 | +const processTerminalForegroundStates = new Map<string, RunningProcessInfo>(); | |
| 17 | + | |
| 18 | +// Background process functions (existing) | |
| 19 | +export function markProcessAsBackgrounded(toolCallId: string): void { | |
| 20 | + processTerminalBackgroundStates.set(toolCallId, true); | |
| 21 | +} | |
| 22 | + | |
| 23 | +export function isProcessBackgrounded(toolCallId: string): boolean { | |
| 24 | + return processTerminalBackgroundStates.has(toolCallId); | |
| 25 | +} | |
| 26 | + | |
| 27 | +export function removeBackgroundedProcess(toolCallId: string): void { | |
| 28 | + processTerminalBackgroundStates.delete(toolCallId); | |
| 29 | +} | |
| 30 | + | |
| 31 | +// Foreground process functions (new) | |
| 32 | +export function markProcessAsRunning( | |
| 33 | + toolCallId: string, | |
| 34 | + process: ChildProcess, | |
| 35 | + onPartialOutput?: (params: { | |
| 36 | + toolCallId: string; | |
| 37 | + contextItems: any[]; | |
| 38 | + }) => void, | |
| 39 | + currentOutput: string = "", | |
| 40 | +): void { | |
| 41 | + processTerminalForegroundStates.set(toolCallId, { | |
| 42 | + process, | |
| 43 | + onPartialOutput, | |
| 44 | + currentOutput, | |
| 45 | + }); | |
| 46 | +} | |
| 47 | + | |
| 48 | +export function isProcessRunning(toolCallId: string): boolean { | |
| 49 | + return processTerminalForegroundStates.has(toolCallId); | |
| 50 | +} | |
| 51 | + | |
| 52 | +export function getRunningProcess( | |
| 53 | + toolCallId: string, | |
| 54 | +): ChildProcess | undefined { | |
| 55 | + const info = processTerminalForegroundStates.get(toolCallId); | |
| 56 | + return info?.process; | |
| 57 | +} | |
| 58 | + | |
| 59 | +export function updateProcessOutput(toolCallId: string, output: string): void { | |
| 60 | + const info = processTerminalForegroundStates.get(toolCallId); | |
| 61 | + if (info) { | |
| 62 | + info.currentOutput = output; | |
| 63 | + } | |
| 64 | +} | |
| 65 | + | |
| 66 | +export function removeRunningProcess(toolCallId: string): void { | |
| 67 | + processTerminalForegroundStates.delete(toolCallId); | |
| 68 | +} | |
| 69 | + | |
| 70 | +export async function killTerminalProcess(toolCallId: string): Promise<void> { | |
| 71 | + const processInfo = processTerminalForegroundStates.get(toolCallId); | |
| 72 | + if (processInfo && !processInfo.process.killed) { | |
| 73 | + const { process } = processInfo; | |
| 74 | + | |
| 75 | + process.kill("SIGTERM"); | |
| 76 | + | |
| 77 | + // Force kill after 5 seconds if still running | |
| 78 | + setTimeout(() => { | |
| 79 | + if (!process.killed) { | |
| 80 | + process.kill("SIGKILL"); | |
| 81 | + } | |
| 82 | + }, 5000); | |
| 83 | + | |
| 84 | + processTerminalForegroundStates.delete(toolCallId); | |
| 85 | + } | |
| 86 | +} | |
| 87 | + | |
| 88 | +// Function to cancel multiple terminal commands at once | |
| 89 | +export async function killMultipleTerminalProcesses( | |
| 90 | + toolCallIds: string[], | |
| 91 | +): Promise<void> { | |
| 92 | + const cancelPromises = toolCallIds.map((toolCallId) => | |
| 93 | + killTerminalProcess(toolCallId), | |
| 94 | + ); | |
| 95 | + await Promise.all(cancelPromises); | |
| 96 | +} | |
| 97 | + | |
| 98 | +// Function to cancel ALL currently running terminal commands | |
| 99 | +export async function killAllRunningTerminalProcesses(): Promise<string[]> { | |
| 100 | + const runningIds = getAllRunningProcessIds(); | |
| 101 | + if (runningIds.length > 0) { | |
| 102 | + await killMultipleTerminalProcesses(runningIds); | |
| 103 | + } | |
| 104 | + return runningIds; // Return the IDs that were cancelled | |
| 105 | +} | |
| 106 | + | |
| 107 | +// Utility functions | |
| 108 | +export function getAllRunningProcessIds(): string[] { | |
| 109 | + return Array.from(processTerminalForegroundStates.keys()); | |
| 110 | +} | |
| 111 | + | |
| 112 | +export function getAllBackgroundedProcessIds(): string[] { | |
| 113 | + return Array.from(processTerminalBackgroundStates.keys()); | |
| 114 | +} | |
| 115 | + | |
| 116 | +// Utility function for testing - clears all background process states | |
| 117 | +export function clearAllBackgroundProcesses(): void { | |
| 118 | + processTerminalBackgroundStates.clear(); | |
| 119 | +} |
modified extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/constants/MessageTypes.kt +1 −0
| @@ -146,6 +146,7 @@ | ||
| 146 | 146 | "isItemTooBig", |
| 147 | 147 | "process/markAsBackgrounded", |
| 148 | 148 | "process/isBackgrounded", |
| 149 | + "process/killTerminalProcess" | |
| 149 | 150 | ) |
| 150 | 151 | } |
| 151 | 152 | } |
modified gui/src/components/mainInput/Lump/LumpToolbar/LumpToolbar.tsx +91 −11
| @@ -1,13 +1,15 @@ | ||
| 1 | +import { BuiltInToolNames } from "core/tools/builtIn"; | |
| 1 | 2 | import { useContext, useEffect } from "react"; |
| 2 | -import { useSelector } from "react-redux"; | |
| 3 | 3 | import { IdeMessengerContext } from "../../../../context/IdeMessenger"; |
| 4 | 4 | import { useAppDispatch, useAppSelector } from "../../../../redux/hooks"; |
| 5 | 5 | import { |
| 6 | 6 | selectFirstPendingToolCall, |
| 7 | 7 | selectPendingToolCalls, |
| 8 | + selectToolCallsByStatus, | |
| 8 | 9 | } from "../../../../redux/selectors/selectToolCalls"; |
| 9 | 10 | import { cancelToolCall } from "../../../../redux/slices/sessionSlice"; |
| 10 | 11 | import { callToolById } from "../../../../redux/thunks/callToolById"; |
| 12 | +import { cancelStream } from "../../../../redux/thunks/cancelStream"; | |
| 11 | 13 | import { logToolUsage } from "../../../../redux/util"; |
| 12 | 14 | import { isJetBrains } from "../../../../util"; |
| 13 | 15 | import { BlockSettingsTopToolbar } from "./BlockSettingsTopToolbar"; |
| @@ -35,6 +37,14 @@ | ||
| 35 | 37 | return modifierKey && event.key === "Backspace"; |
| 36 | 38 | }; |
| 37 | 39 | |
| 40 | +// Check if a tool call is a terminal command | |
| 41 | +const isTerminalCommand = (toolCallState: any) => { | |
| 42 | + return ( | |
| 43 | + toolCallState?.toolCall?.function?.name === | |
| 44 | + BuiltInToolNames.RunTerminalCommand | |
| 45 | + ); | |
| 46 | +}; | |
| 47 | + | |
| 38 | 48 | export function LumpToolbar() { |
| 39 | 49 | const dispatch = useAppDispatch(); |
| 40 | 50 | const ideMessenger = useContext(IdeMessengerContext); |
| @@ -55,13 +65,67 @@ | ||
| 55 | 65 | ); |
| 56 | 66 | const isApplying = applyStates.some((state) => state.status === "streaming"); |
| 57 | 67 | |
| 68 | + // Get ALL running terminal commands | |
| 69 | + const runningToolCalls = useAppSelector((state) => | |
| 70 | + selectToolCallsByStatus(state, "calling"), | |
| 71 | + ); | |
| 72 | + const runningTerminalCalls = runningToolCalls.filter(isTerminalCommand); | |
| 73 | + const hasRunningTerminalCommand = runningTerminalCalls.length > 0; | |
| 74 | + | |
| 75 | + // Simple handler: stop ALL running terminal commands | |
| 76 | + const handleStopAllTerminalCommands = async () => { | |
| 77 | + if (runningTerminalCalls.length === 0) { | |
| 78 | + return; | |
| 79 | + } | |
| 80 | + | |
| 81 | + // Stop all terminal commands concurrently | |
| 82 | + const stopPromises = runningTerminalCalls.map(async (terminalCall) => { | |
| 83 | + try { | |
| 84 | + // Cancel the process on the backend | |
| 85 | + await ideMessenger.request("process/killTerminalProcess", { | |
| 86 | + toolCallId: terminalCall.toolCallId, | |
| 87 | + }); | |
| 88 | + | |
| 89 | + // Cancel the tool call in the UI | |
| 90 | + dispatch( | |
| 91 | + cancelToolCall({ | |
| 92 | + toolCallId: terminalCall.toolCallId, | |
| 93 | + }), | |
| 94 | + ); | |
| 95 | + | |
| 96 | + logToolUsage(terminalCall, false, true, ideMessenger); | |
| 97 | + } catch (error) { | |
| 98 | + console.error( | |
| 99 | + `Failed to cancel terminal command ${terminalCall.toolCallId}:`, | |
| 100 | + error, | |
| 101 | + ); | |
| 102 | + } | |
| 103 | + }); | |
| 104 | + | |
| 105 | + // Wait for all cancellations to complete | |
| 106 | + await Promise.all(stopPromises); | |
| 107 | + }; | |
| 108 | + | |
| 109 | + // Combined stop handler | |
| 110 | + const handleStopAction = async () => { | |
| 111 | + // Stop all terminal commands if any are running | |
| 112 | + if (hasRunningTerminalCommand) { | |
| 113 | + await handleStopAllTerminalCommands(); | |
| 114 | + } | |
| 115 | + | |
| 116 | + // Also stop regular streaming if it's happening | |
| 117 | + if (isStreaming) { | |
| 118 | + dispatch(cancelStream()); | |
| 119 | + } | |
| 120 | + }; | |
| 121 | + | |
| 58 | 122 | useEffect(() => { |
| 59 | - if (!firstPendingToolCall) { | |
| 123 | + if (!firstPendingToolCall && !hasRunningTerminalCommand) { | |
| 60 | 124 | return; |
| 61 | 125 | } |
| 62 | 126 | |
| 63 | 127 | const handleToolCallKeyboardShortcuts = (event: KeyboardEvent) => { |
| 64 | - if (isExecuteToolCallShortcut(event)) { | |
| 128 | + if (isExecuteToolCallShortcut(event) && firstPendingToolCall) { | |
| 65 | 129 | event.preventDefault(); |
| 66 | 130 | event.stopPropagation(); |
| 67 | 131 | |
| @@ -71,13 +135,19 @@ | ||
| 71 | 135 | } else if (isCancelToolCallShortcut(event, jetbrains)) { |
| 72 | 136 | event.preventDefault(); |
| 73 | 137 | event.stopPropagation(); |
| 74 | - void dispatch( | |
| 75 | - cancelToolCall({ | |
| 76 | - toolCallId: firstPendingToolCall.toolCallId, | |
| 77 | - }), | |
| 78 | - ); | |
| 79 | 138 | |
| 80 | - logToolUsage(firstPendingToolCall, false, true, ideMessenger); | |
| 139 | + if (hasRunningTerminalCommand) { | |
| 140 | + // Stop running terminal commands | |
| 141 | + void handleStopAction(); | |
| 142 | + } else if (firstPendingToolCall) { | |
| 143 | + // Cancel pending tool call | |
| 144 | + void dispatch( | |
| 145 | + cancelToolCall({ | |
| 146 | + toolCallId: firstPendingToolCall.toolCallId, | |
| 147 | + }), | |
| 148 | + ); | |
| 149 | + logToolUsage(firstPendingToolCall, false, true, ideMessenger); | |
| 150 | + } | |
| 81 | 151 | } |
| 82 | 152 | }; |
| 83 | 153 | |
| @@ -85,7 +155,7 @@ | ||
| 85 | 155 | return () => { |
| 86 | 156 | document.removeEventListener("keydown", handleToolCallKeyboardShortcuts); |
| 87 | 157 | }; |
| 88 | - }, [firstPendingToolCall]); | |
| 158 | + }, [firstPendingToolCall, hasRunningTerminalCommand, runningTerminalCalls]); | |
| 89 | 159 | |
| 90 | 160 | if (isApplying) { |
| 91 | 161 | return <IsApplyingToolbar />; |
| @@ -103,8 +173,18 @@ | ||
| 103 | 173 | return <TtsActiveToolbar />; |
| 104 | 174 | } |
| 105 | 175 | |
| 176 | + // Only show terminal streaming for actual terminal commands | |
| 177 | + if (hasRunningTerminalCommand) { | |
| 178 | + const count = runningTerminalCalls.length; | |
| 179 | + const stopText = `Stop Terminal${count > 1 ? ` (${count})` : ""}`; | |
| 180 | + return ( | |
| 181 | + <StreamingToolbar onStop={handleStopAction} displayText={stopText} /> | |
| 182 | + ); | |
| 183 | + } | |
| 184 | + | |
| 185 | + // Regular streaming (non-terminal) | |
| 106 | 186 | if (isStreaming) { |
| 107 | - return <StreamingToolbar />; | |
| 187 | + return <StreamingToolbar onStop={() => dispatch(cancelStream())} />; | |
| 108 | 188 | } |
| 109 | 189 | |
| 110 | 190 | if (pendingToolCalls.length > 0) { |
modified gui/src/components/mainInput/Lump/LumpToolbar/StreamingToolbar.tsx +11 −6
| @@ -1,20 +1,25 @@ | ||
| 1 | -import { useAppDispatch } from "../../../../redux/hooks"; | |
| 2 | -import { cancelStream } from "../../../../redux/thunks/cancelStream"; | |
| 3 | 1 | import { getAltKeyLabel, getMetaKeyLabel, isJetBrains } from "../../../../util"; |
| 4 | 2 | import { GeneratingIndicator } from "./GeneratingIndicator"; |
| 5 | 3 | |
| 6 | -export function StreamingToolbar() { | |
| 4 | +interface StreamingToolbarProps { | |
| 5 | + onStop: () => void; | |
| 6 | + displayText?: string; | |
| 7 | +} | |
| 8 | + | |
| 9 | +export function StreamingToolbar({ | |
| 10 | + onStop, | |
| 11 | + displayText = "Stop", | |
| 12 | +}: StreamingToolbarProps) { | |
| 7 | 13 | const jetbrains = isJetBrains(); |
| 8 | - const dispatch = useAppDispatch(); | |
| 9 | 14 | |
| 10 | 15 | return ( |
| 11 | 16 | <div className="flex w-full items-center justify-between"> |
| 12 | 17 | <GeneratingIndicator /> |
| 13 | 18 | <div |
| 14 | - onClick={() => void dispatch(cancelStream())} | |
| 19 | + onClick={onStop} | |
| 15 | 20 | className="text-2xs cursor-pointer px-1.5 py-0.5 hover:brightness-125" |
| 16 | 21 | > |
| 17 | - <span className="text-description">Stop</span> | |
| 22 | + <span className="text-description">{displayText}</span> | |
| 18 | 23 | {/* JetBrains overrides cmd+backspace, so we have to use another shortcut */} |
| 19 | 24 | <span className="text-description-muted ml-1 opacity-75"> |
| 20 | 25 | {jetbrains ? getAltKeyLabel() : getMetaKeyLabel()}⌫ |
modified gui/src/redux/thunks/moveTerminalProcessToBackground.ts +1 −1
| @@ -46,7 +46,7 @@ | ||
| 46 | 46 | { |
| 47 | 47 | name: "Terminal", |
| 48 | 48 | description: "Terminal command output", |
| 49 | - content: existingContent + (existingContent ? "\n\n" : "") + status, | |
| 49 | + content: existingContent, | |
| 50 | 50 | status: status, |
| 51 | 51 | }, |
| 52 | 52 | ]; |
modified gui/src/redux/util/constructMessages.test.ts +343 −0
| @@ -8,6 +8,7 @@ | ||
| 8 | 8 | ToolResultChatMessage, |
| 9 | 9 | UserChatMessage, |
| 10 | 10 | } from "core"; |
| 11 | +import { BuiltInToolNames } from "core/tools/builtIn"; | |
| 11 | 12 | import { |
| 12 | 13 | CANCELLED_TOOL_CALL_MESSAGE, |
| 13 | 14 | NO_TOOL_CALL_OUTPUT_MESSAGE, |
| @@ -712,4 +713,346 @@ | ||
| 712 | 713 | expect(messages[0].content).toContain("Base System Message"); |
| 713 | 714 | expect(messages[0].content).not.toContain(LAST_MESSAGE_RULE.rule); |
| 714 | 715 | }); |
| 716 | + | |
| 717 | + // Tests for the specific block in lines 135-142 handling toolCallState.output | |
| 718 | + describe("toolCallState.output handling (lines 135-142)", () => { | |
| 719 | + test("should use CANCELLED_TOOL_CALL_MESSAGE for cancelled tool calls", () => { | |
| 720 | + const assistantWithToolCall: AssistantChatMessage = { | |
| 721 | + role: "assistant", | |
| 722 | + content: "I'll run a command", | |
| 723 | + toolCalls: [ | |
| 724 | + { | |
| 725 | + id: "tool-call-1", | |
| 726 | + type: "function", | |
| 727 | + function: { | |
| 728 | + name: "some_tool", | |
| 729 | + arguments: '{"command": "ls"}', | |
| 730 | + }, | |
| 731 | + }, | |
| 732 | + ], | |
| 733 | + }; | |
| 734 | + | |
| 735 | + mockHistory = [ | |
| 736 | + { | |
| 737 | + message: assistantWithToolCall, | |
| 738 | + contextItems: [], | |
| 739 | + toolCallStates: [ | |
| 740 | + { | |
| 741 | + toolCallId: "tool-call-1", | |
| 742 | + toolCall: { | |
| 743 | + id: "tool-call-1", | |
| 744 | + type: "function", | |
| 745 | + function: { | |
| 746 | + name: "some_tool", | |
| 747 | + arguments: '{"command": "ls"}', | |
| 748 | + }, | |
| 749 | + }, | |
| 750 | + status: "canceled", | |
| 751 | + parsedArgs: { command: "ls" }, | |
| 752 | + output: [ | |
| 753 | + createContextItem( | |
| 754 | + "result", | |
| 755 | + "This should be ignored due to cancelled status", | |
| 756 | + ), | |
| 757 | + ], | |
| 758 | + }, | |
| 759 | + ], | |
| 760 | + }, | |
| 761 | + ]; | |
| 762 | + | |
| 763 | + const { messages } = constructMessages( | |
| 764 | + mockHistory, | |
| 765 | + "Base System Message", | |
| 766 | + mockRules, | |
| 767 | + {}, | |
| 768 | + ); | |
| 769 | + | |
| 770 | + const toolMessage = messages[2] as ToolResultChatMessage; | |
| 771 | + expect(toolMessage.role).toBe("tool"); | |
| 772 | + expect(toolMessage.content).toBe(CANCELLED_TOOL_CALL_MESSAGE); | |
| 773 | + expect(toolMessage.content).not.toContain("This should be ignored"); | |
| 774 | + }); | |
| 775 | + | |
| 776 | + test("should use renderContextItemsWithStatus for RunTerminalCommand with output", () => { | |
| 777 | + const assistantWithTerminalCall: AssistantChatMessage = { | |
| 778 | + role: "assistant", | |
| 779 | + content: "I'll run a terminal command", | |
| 780 | + toolCalls: [ | |
| 781 | + { | |
| 782 | + id: "terminal-call-1", | |
| 783 | + type: "function", | |
| 784 | + function: { | |
| 785 | + name: BuiltInToolNames.RunTerminalCommand, | |
| 786 | + arguments: '{"command": "ls -la"}', | |
| 787 | + }, | |
| 788 | + }, | |
| 789 | + ], | |
| 790 | + }; | |
| 791 | + | |
| 792 | + const terminalOutputWithStatus = [ | |
| 793 | + { | |
| 794 | + ...createContextItem("terminal-output", "file1.txt\nfile2.txt"), | |
| 795 | + status: "completed", | |
| 796 | + }, | |
| 797 | + { | |
| 798 | + ...createContextItem("terminal-error", "Warning: deprecated flag"), | |
| 799 | + status: "warning", | |
| 800 | + }, | |
| 801 | + ]; | |
| 802 | + | |
| 803 | + mockHistory = [ | |
| 804 | + { | |
| 805 | + message: assistantWithTerminalCall, | |
| 806 | + contextItems: [], | |
| 807 | + toolCallStates: [ | |
| 808 | + { | |
| 809 | + toolCallId: "terminal-call-1", | |
| 810 | + toolCall: { | |
| 811 | + id: "terminal-call-1", | |
| 812 | + type: "function", | |
| 813 | + function: { | |
| 814 | + name: BuiltInToolNames.RunTerminalCommand, | |
| 815 | + arguments: '{"command": "ls -la"}', | |
| 816 | + }, | |
| 817 | + }, | |
| 818 | + status: "done", | |
| 819 | + parsedArgs: { command: "ls -la" }, | |
| 820 | + output: terminalOutputWithStatus, | |
| 821 | + }, | |
| 822 | + ], | |
| 823 | + }, | |
| 824 | + ]; | |
| 825 | + | |
| 826 | + const { messages } = constructMessages( | |
| 827 | + mockHistory, | |
| 828 | + "Base System Message", | |
| 829 | + mockRules, | |
| 830 | + {}, | |
| 831 | + ); | |
| 832 | + | |
| 833 | + const toolMessage = messages[2] as ToolResultChatMessage; | |
| 834 | + expect(toolMessage.role).toBe("tool"); | |
| 835 | + expect(toolMessage.toolCallId).toBe("terminal-call-1"); | |
| 836 | + // Should contain content with status appended | |
| 837 | + expect(toolMessage.content).toContain("file1.txt\nfile2.txt"); | |
| 838 | + expect(toolMessage.content).toContain("[Status: completed]"); | |
| 839 | + expect(toolMessage.content).toContain("Warning: deprecated flag"); | |
| 840 | + expect(toolMessage.content).toContain("[Status: warning]"); | |
| 841 | + // Should use renderContextItemsWithStatus format with double newlines between items | |
| 842 | + expect(toolMessage.content).toMatch( | |
| 843 | + /file1\.txt\nfile2\.txt\n\[Status: completed\]\n\nWarning: deprecated flag\n\[Status: warning\]/, | |
| 844 | + ); | |
| 845 | + }); | |
| 846 | + | |
| 847 | + test("should use renderContextItems for non-RunTerminalCommand tools with output", () => { | |
| 848 | + const assistantWithSearchCall: AssistantChatMessage = { | |
| 849 | + role: "assistant", | |
| 850 | + content: "I'll search for that", | |
| 851 | + toolCalls: [ | |
| 852 | + { | |
| 853 | + id: "search-call-1", | |
| 854 | + type: "function", | |
| 855 | + function: { | |
| 856 | + name: "search", // Not RunTerminalCommand | |
| 857 | + arguments: '{"query": "test"}', | |
| 858 | + }, | |
| 859 | + }, | |
| 860 | + ], | |
| 861 | + }; | |
| 862 | + | |
| 863 | + const searchOutput = [ | |
| 864 | + createContextItem("result1", "First search result"), | |
| 865 | + createContextItem("result2", "Second search result"), | |
| 866 | + ]; | |
| 867 | + | |
| 868 | + mockHistory = [ | |
| 869 | + { | |
| 870 | + message: assistantWithSearchCall, | |
| 871 | + contextItems: [], | |
| 872 | + toolCallStates: [ | |
| 873 | + { | |
| 874 | + toolCallId: "search-call-1", | |
| 875 | + toolCall: { | |
| 876 | + id: "search-call-1", | |
| 877 | + type: "function", | |
| 878 | + function: { | |
| 879 | + name: "search", | |
| 880 | + arguments: '{"query": "test"}', | |
| 881 | + }, | |
| 882 | + }, | |
| 883 | + status: "done", | |
| 884 | + parsedArgs: { query: "test" }, | |
| 885 | + output: searchOutput, | |
| 886 | + }, | |
| 887 | + ], | |
| 888 | + }, | |
| 889 | + ]; | |
| 890 | + | |
| 891 | + const { messages } = constructMessages( | |
| 892 | + mockHistory, | |
| 893 | + "Base System Message", | |
| 894 | + mockRules, | |
| 895 | + {}, | |
| 896 | + ); | |
| 897 | + | |
| 898 | + const toolMessage = messages[2] as ToolResultChatMessage; | |
| 899 | + expect(toolMessage.role).toBe("tool"); | |
| 900 | + expect(toolMessage.toolCallId).toBe("search-call-1"); | |
| 901 | + // Should use renderContextItems format (no status, double newlines between items) | |
| 902 | + expect(toolMessage.content).toBe( | |
| 903 | + "First search result\n\nSecond search result", | |
| 904 | + ); | |
| 905 | + expect(toolMessage.content).not.toContain("[Status:"); | |
| 906 | + }); | |
| 907 | + | |
| 908 | + test("should use NO_TOOL_CALL_OUTPUT_MESSAGE when no output exists", () => { | |
| 909 | + const assistantWithToolCall: AssistantChatMessage = { | |
| 910 | + role: "assistant", | |
| 911 | + content: "I'll search for that", | |
| 912 | + toolCalls: [ | |
| 913 | + { | |
| 914 | + id: "search-call-1", | |
| 915 | + type: "function", | |
| 916 | + function: { | |
| 917 | + name: "search", | |
| 918 | + arguments: '{"query": "test"}', | |
| 919 | + }, | |
| 920 | + }, | |
| 921 | + ], | |
| 922 | + }; | |
| 923 | + | |
| 924 | + mockHistory = [ | |
| 925 | + { | |
| 926 | + message: assistantWithToolCall, | |
| 927 | + contextItems: [], | |
| 928 | + toolCallStates: [ | |
| 929 | + { | |
| 930 | + toolCallId: "search-call-1", | |
| 931 | + toolCall: { | |
| 932 | + id: "search-call-1", | |
| 933 | + type: "function", | |
| 934 | + function: { | |
| 935 | + name: "search", | |
| 936 | + arguments: '{"query": "test"}', | |
| 937 | + }, | |
| 938 | + }, | |
| 939 | + status: "generating", | |
| 940 | + parsedArgs: { query: "test" }, | |
| 941 | + // No output field | |
| 942 | + }, | |
| 943 | + ], | |
| 944 | + }, | |
| 945 | + ]; | |
| 946 | + | |
| 947 | + const { messages } = constructMessages( | |
| 948 | + mockHistory, | |
| 949 | + "Base System Message", | |
| 950 | + mockRules, | |
| 951 | + {}, | |
| 952 | + ); | |
| 953 | + | |
| 954 | + const toolMessage = messages[2] as ToolResultChatMessage; | |
| 955 | + expect(toolMessage.role).toBe("tool"); | |
| 956 | + expect(toolMessage.toolCallId).toBe("search-call-1"); | |
| 957 | + expect(toolMessage.content).toBe(NO_TOOL_CALL_OUTPUT_MESSAGE); | |
| 958 | + }); | |
| 959 | + | |
| 960 | + test("should use NO_TOOL_CALL_OUTPUT_MESSAGE when toolCallState is undefined", () => { | |
| 961 | + const assistantWithToolCall: AssistantChatMessage = { | |
| 962 | + role: "assistant", | |
| 963 | + content: "I'll search for that", | |
| 964 | + toolCalls: [ | |
| 965 | + { | |
| 966 | + id: "search-call-1", | |
| 967 | + type: "function", | |
| 968 | + function: { | |
| 969 | + name: "search", | |
| 970 | + arguments: '{"query": "test"}', | |
| 971 | + }, | |
| 972 | + }, | |
| 973 | + ], | |
| 974 | + }; | |
| 975 | + | |
| 976 | + mockHistory = [ | |
| 977 | + { | |
| 978 | + message: assistantWithToolCall, | |
| 979 | + contextItems: [], | |
| 980 | + // No toolCallStates array | |
| 981 | + }, | |
| 982 | + ]; | |
| 983 | + | |
| 984 | + const { messages } = constructMessages( | |
| 985 | + mockHistory, | |
| 986 | + "Base System Message", | |
| 987 | + mockRules, | |
| 988 | + {}, | |
| 989 | + ); | |
| 990 | + | |
| 991 | + const toolMessage = messages[2] as ToolResultChatMessage; | |
| 992 | + expect(toolMessage.role).toBe("tool"); | |
| 993 | + expect(toolMessage.toolCallId).toBe("search-call-1"); | |
| 994 | + expect(toolMessage.content).toBe(NO_TOOL_CALL_OUTPUT_MESSAGE); | |
| 995 | + }); | |
| 996 | + | |
| 997 | + test("should prioritize cancelled status over output content", () => { | |
| 998 | + const assistantWithTerminalCall: AssistantChatMessage = { | |
| 999 | + role: "assistant", | |
| 1000 | + content: "I'll run a command", | |
| 1001 | + toolCalls: [ | |
| 1002 | + { | |
| 1003 | + id: "terminal-call-1", | |
| 1004 | + type: "function", | |
| 1005 | + function: { | |
| 1006 | + name: BuiltInToolNames.RunTerminalCommand, | |
| 1007 | + arguments: '{"command": "ls"}', | |
| 1008 | + }, | |
| 1009 | + }, | |
| 1010 | + ], | |
| 1011 | + }; | |
| 1012 | + | |
| 1013 | + mockHistory = [ | |
| 1014 | + { | |
| 1015 | + message: assistantWithTerminalCall, | |
| 1016 | + contextItems: [], | |
| 1017 | + toolCallStates: [ | |
| 1018 | + { | |
| 1019 | + toolCallId: "terminal-call-1", | |
| 1020 | + toolCall: { | |
| 1021 | + id: "terminal-call-1", | |
| 1022 | + type: "function", | |
| 1023 | + function: { | |
| 1024 | + name: BuiltInToolNames.RunTerminalCommand, | |
| 1025 | + arguments: '{"command": "ls"}', | |
| 1026 | + }, | |
| 1027 | + }, | |
| 1028 | + status: "canceled", // Cancelled status | |
| 1029 | + parsedArgs: { command: "ls" }, | |
| 1030 | + output: [ | |
| 1031 | + createContextItem( | |
| 1032 | + "terminal-output", | |
| 1033 | + "This output should be ignored", | |
| 1034 | + ), | |
| 1035 | + ], // Has output but is cancelled | |
| 1036 | + }, | |
| 1037 | + ], | |
| 1038 | + }, | |
| 1039 | + ]; | |
| 1040 | + | |
| 1041 | + const { messages } = constructMessages( | |
| 1042 | + mockHistory, | |
| 1043 | + "Base System Message", | |
| 1044 | + mockRules, | |
| 1045 | + {}, | |
| 1046 | + ); | |
| 1047 | + | |
| 1048 | + const toolMessage = messages[2] as ToolResultChatMessage; | |
| 1049 | + expect(toolMessage.role).toBe("tool"); | |
| 1050 | + expect(toolMessage.toolCallId).toBe("terminal-call-1"); | |
| 1051 | + // Should use cancelled message, not the output content | |
| 1052 | + expect(toolMessage.content).toBe(CANCELLED_TOOL_CALL_MESSAGE); | |
| 1053 | + expect(toolMessage.content).not.toContain( | |
| 1054 | + "This output should be ignored", | |
| 1055 | + ); | |
| 1056 | + }); | |
| 1057 | + }); | |
| 715 | 1058 | }); |
modified gui/src/redux/util/constructMessages.ts +13 −1
| @@ -10,6 +10,7 @@ | ||
| 10 | 10 | import { chatMessageIsEmpty } from "core/llm/messages"; |
| 11 | 11 | import { getSystemMessageWithRules } from "core/llm/rules/getSystemMessageWithRules"; |
| 12 | 12 | import { RulePolicies } from "core/llm/rules/types"; |
| 13 | +import { BuiltInToolNames } from "core/tools/builtIn"; | |
| 13 | 14 | import { |
| 14 | 15 | CANCELLED_TOOL_CALL_MESSAGE, |
| 15 | 16 | NO_TOOL_CALL_OUTPUT_MESSAGE, |
| @@ -20,8 +21,13 @@ | ||
| 20 | 21 | import { |
| 21 | 22 | normalizeToMessageParts, |
| 22 | 23 | renderContextItems, |
| 24 | + renderContextItemsWithStatus, | |
| 23 | 25 | } from "core/util/messageContent"; |
| 24 | 26 | import { toolCallStateToContextItems } from "../../pages/gui/ToolCallDiv/utils"; |
| 27 | + | |
| 28 | +// Helper function to render context items and append status information | |
| 29 | +// Helper function to render context items and append status information | |
| 30 | + | |
| 25 | 31 | interface MessageWithContextItems { |
| 26 | 32 | ctxItems: ContextItemWithId[]; |
| 27 | 33 | message: ChatMessage; |
| @@ -132,8 +138,14 @@ | ||
| 132 | 138 | |
| 133 | 139 | if (toolCallState?.status === "canceled") { |
| 134 | 140 | content = CANCELLED_TOOL_CALL_MESSAGE; |
| 141 | + } else if ( | |
| 142 | + toolCallState?.output && | |
| 143 | + toolCall.function?.name == BuiltInToolNames.RunTerminalCommand | |
| 144 | + ) { | |
| 145 | + // Add status for tools containing detailed status outcomes per context item | |
| 146 | + content = renderContextItemsWithStatus(toolCallState.output); | |
| 135 | 147 | } else if (toolCallState?.output) { |
| 136 | - content = renderContextItems(toolCallState.output); | |
| 148 | + content = renderContextItems(toolCallState?.output); | |
| 137 | 149 | } |
| 138 | 150 | |
| 139 | 151 | msgs.push({ |