From 72399ca5ccad459c32d800a732f4ba3ead6a0217 Mon Sep 17 00:00:00 2001 From: Jacob Kim Date: Mon, 11 Aug 2025 18:20:05 -0700 Subject: [PATCH] feat: use a list of diffContexts instead of a single one --- core/nextEdit/templating/instinct.ts | 80 ++++++++++--------- core/nextEdit/templating/instinct.vitest.ts | 52 ++++++++++-- .../templating/mercuryCoderNextEdit.ts | 7 +- .../templating/mercuryCoderNextEdit.vitest.ts | 4 +- 4 files changed, 95 insertions(+), 48 deletions(-) diff --git a/core/nextEdit/templating/instinct.ts b/core/nextEdit/templating/instinct.ts index b7e14f165..b2140d1da 100644 --- a/core/nextEdit/templating/instinct.ts +++ b/core/nextEdit/templating/instinct.ts @@ -67,56 +67,62 @@ export function currentFileContentBlock( } export function editHistoryBlock( - editDiffHistories: string, // unified diffs with Index headers + editDiffHistories: string[], // array of unified diffs with Index headers ) { - if (!editDiffHistories.trim()) { + if (!editDiffHistories.length) { return ""; } const blocks: string[] = []; - // Split on Index: lines to get the unified diff. - const diffSections = editDiffHistories - .split(/^Index: /m) - .filter((section) => section.trim()); + for (const editDiffHistory of editDiffHistories) { + if (!editDiffHistory.trim()) { + continue; + } - for (const section of diffSections) { - const lines = section.split("\n"); + // Split on Index: lines to get the unified diff. + const diffSections = editDiffHistory + .split(/^Index: /m) + .filter((section) => section.trim()); - // Extract filename from the first line (after "Index: " was split off). - const filename = lines[0]; + for (const section of diffSections) { + const lines = section.split("\n"); - // Find the start of the actual diff content (skip ---, +++, and === lines). - const diffLines = lines - .filter( + // Extract filename from the first line (after "Index: " was split off). + const filename = lines[0]; + + // Find the start of the actual diff content (skip ---, +++, and === lines). + const diffLines = lines + .filter( + (line) => + !line.startsWith("---") && + !line.startsWith("+++") && + !line.startsWith("===") && + line.trim() !== "", // remove empty lines from header section + ) + .slice(1); // remove the filename line + + // Only include lines that are actual diff content (@@, +, -, or context lines). + const actualDiffContent = diffLines.filter( (line) => - !line.startsWith("---") && - !line.startsWith("+++") && - !line.startsWith("===") && - line.trim() !== "", // remove empty lines from header section - ) - .slice(1); // remove the filename line + line.startsWith("@@") || + line.startsWith("+") || + line.startsWith("-") || + line.startsWith(" "), // context lines + ); - // Only include lines that are actual diff content (@@, +, -, or context lines). - const actualDiffContent = diffLines.filter( - (line) => - line.startsWith("@@") || - line.startsWith("+") || - line.startsWith("-") || - line.startsWith(" "), // context lines - ); + if (actualDiffContent.length === 0) continue; - if (actualDiffContent.length === 0) continue; + const diffBlock = [ + `User edited file "${filename}"`, + "", + "```diff", + actualDiffContent.join("\n"), + "```", + ].join("\n"); - const diffBlock = [ - `User edited file "${filename}"`, - "", - "```diff", - actualDiffContent.join("\n"), - "```", - ].join("\n"); - - blocks.push(diffBlock); + blocks.push(diffBlock); + } } return blocks.join("\n"); diff --git a/core/nextEdit/templating/instinct.vitest.ts b/core/nextEdit/templating/instinct.vitest.ts index aa15c2fcd..b7972dc5d 100644 --- a/core/nextEdit/templating/instinct.vitest.ts +++ b/core/nextEdit/templating/instinct.vitest.ts @@ -291,7 +291,7 @@ line 3`; describe("editHistoryBlock", () => { test("should format empty edit history", () => { - const result = editHistoryBlock(""); + const result = editHistoryBlock([]); expect(result).toBe(""); }); @@ -306,7 +306,7 @@ describe("editHistoryBlock", () => { + console.log("new"); }`; - const result = editHistoryBlock(diff); + const result = editHistoryBlock([diff]); const expected = `User edited file "test.js" \`\`\`diff @@ -337,7 +337,45 @@ Index: file2.js -let x = "old"; +let x = "new";`; - const result = editHistoryBlock(unifiedDiff); + const result = editHistoryBlock([unifiedDiff]); + const expected = `User edited file "file1.js" + +\`\`\`diff +@@ -1 +1 @@ +-const old = 1; ++const new = 1; +\`\`\` +User edited file "file2.js" + +\`\`\`diff +@@ -1 +1 @@ +-let x = "old"; ++let x = "new"; +\`\`\``; + + expect(result).toBe(expected); + }); + + test("should format multiple diff history entries", () => { + // Multiple diffs should be concatenated into a single string + const unifiedDiffs = [ + `Index: file1.js +=================================================================== +--- a/file1.js 2023-01-01 10:00:00.000000000 +0000 ++++ b/file1.js 2023-01-01 10:01:00.000000000 +0000 +@@ -1 +1 @@ +-const old = 1; ++const new = 1;`, + `Index: file2.js +=================================================================== +--- a/file2.js 2023-01-01 10:02:00.000000000 +0000 ++++ b/file2.js 2023-01-01 10:03:00.000000000 +0000 +@@ -1 +1 @@ +-let x = "old"; ++let x = "new";`, + ]; + + const result = editHistoryBlock(unifiedDiffs); const expected = `User edited file "file1.js" \`\`\`diff @@ -366,7 +404,7 @@ User edited file "file2.js" -export const Button = () => ; +export const Button = () => ;`; - const result = editHistoryBlock(diff); + const result = editHistoryBlock([diff]); const expected = `User edited file "src/components/Button/Button.tsx" \`\`\`diff @@ -388,7 +426,7 @@ User edited file "file2.js" + return "hello world"; +}`; - const result = editHistoryBlock(diff); + const result = editHistoryBlock([diff]); const expected = `User edited file "newfile.js" \`\`\`diff @@ -411,7 +449,7 @@ User edited file "file2.js" - return "goodbye"; -}`; - const result = editHistoryBlock(diff); + const result = editHistoryBlock([diff]); const expected = `User edited file "deletedfile.js" \`\`\`diff @@ -431,7 +469,7 @@ User edited file "file2.js" -old text +new text`; - const result = editHistoryBlock(diff); + const result = editHistoryBlock([diff]); const expected = `User edited file "simple.txt" \`\`\`diff diff --git a/core/nextEdit/templating/mercuryCoderNextEdit.ts b/core/nextEdit/templating/mercuryCoderNextEdit.ts index 1d7950e96..80c465fc1 100644 --- a/core/nextEdit/templating/mercuryCoderNextEdit.ts +++ b/core/nextEdit/templating/mercuryCoderNextEdit.ts @@ -50,12 +50,15 @@ export function currentFileContentBlock( } export function editHistoryBlock( - editDiffHistory: string, // could be a singe large unified diff + editDiffHistory: string[], // could be a singe large unified diff ) { // diffHistory is made from createDiff. // This uses createPatch from npm diff library, which includes an index line and a separator. // We get rid of these first two lines. - return editDiffHistory.split("\n").slice(2).join("\n"); + return editDiffHistory + .map((diff) => diff.split("\n").slice(2).join("\n")) + .join("\n"); + // return editDiffHistory.split("\n").slice(2).join("\n"); } function mercuryNextEditTemplateBuilder( diff --git a/core/nextEdit/templating/mercuryCoderNextEdit.vitest.ts b/core/nextEdit/templating/mercuryCoderNextEdit.vitest.ts index 6a1c35c74..24c1520e7 100644 --- a/core/nextEdit/templating/mercuryCoderNextEdit.vitest.ts +++ b/core/nextEdit/templating/mercuryCoderNextEdit.vitest.ts @@ -108,12 +108,12 @@ describe("mercuryCoderNextEdit", () => { it("should return the edit diff history unchanged", () => { const diffHistory = "diff --git a/file.ts b/file.ts\n==============================\n@@ -1,3 +1,4 @@"; - const result = editHistoryBlock(diffHistory); + const result = editHistoryBlock([diffHistory]); expect(result).toBe("@@ -1,3 +1,4 @@"); }); it("should handle empty diff history", () => { - const result = editHistoryBlock(""); + const result = editHistoryBlock([]); expect(result).toBe(""); }); });