From dcef0fc843538bf4b34e1f466b3036da7b782fa7 Mon Sep 17 00:00:00 2001 From: Chris Estreich Date: Tue, 29 Apr 2025 10:10:14 -0700 Subject: [PATCH] Prevent duplicate terminal output when combining terminal output messages (#3033) --- .../__tests__/combineCommandSequences.test.ts | 4 ++- src/shared/combineCommandSequences.ts | 30 +++++++++---------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/shared/__tests__/combineCommandSequences.test.ts b/src/shared/__tests__/combineCommandSequences.test.ts index fbc2dbc815..2bed68ba73 100644 --- a/src/shared/__tests__/combineCommandSequences.test.ts +++ b/src/shared/__tests__/combineCommandSequences.test.ts @@ -40,6 +40,8 @@ const messages: ClineMessage[] = [ describe("combineCommandSequences", () => { it("should combine command sequences", () => { const message = combineCommandSequences(messages).at(-1) - expect(message!.text!.length).toEqual(131) + expect(message!.text).toEqual( + "ping www.google.com\nOutput:PING www.google.com (142.251.46.228): 56 data bytes\n", + ) }) }) diff --git a/src/shared/combineCommandSequences.ts b/src/shared/combineCommandSequences.ts index abe71d75f2..4e1d2d7bb4 100644 --- a/src/shared/combineCommandSequences.ts +++ b/src/shared/combineCommandSequences.ts @@ -1,5 +1,7 @@ import { ClineMessage } from "./ExtensionMessage" +export const COMMAND_OUTPUT_STRING = "Output:" + /** * Combines sequences of command and command_output messages in an array of ClineMessages. * @@ -27,30 +29,28 @@ export function combineCommandSequences(messages: ClineMessage[]): ClineMessage[ for (let i = 0; i < messages.length; i++) { if (messages[i].type === "ask" && messages[i].ask === "command") { let combinedText = messages[i].text || "" - let didAddOutput = false let j = i + 1 + let previous: { type: "ask" | "say"; text: string } | undefined while (j < messages.length) { - if (messages[j].type === "ask" && messages[j].ask === "command") { - // Stop if we encounter the next command. - break + const { type, ask, say, text = "" } = messages[j] + + if (type === "ask" && ask === "command") { + break // Stop if we encounter the next command. } - if (messages[j].ask === "command_output" || messages[j].say === "command_output") { - if (!didAddOutput) { - // Add a newline before the first output. + if (ask === "command_output" || say === "command_output") { + if (!previous) { combinedText += `\n${COMMAND_OUTPUT_STRING}` - didAddOutput = true } - // Handle cases where we receive empty command_output (i.e. - // when extension is relinquishing control over exit command - // button). - const output = messages[j].text || "" + const isDuplicate = previous && previous.type !== type && previous.text === text - if (output.length > 0) { - combinedText += output + if (text.length > 0 && !isDuplicate) { + combinedText += text } + + previous = { type, text } } j++ @@ -109,5 +109,3 @@ export const splitCommandOutput = (text: string) => { .join(""), } } - -export const COMMAND_OUTPUT_STRING = "Output:"