Skip to content

Commit 456aef9

Browse files
fix(vscode-nes): improve checking for reverting recent edits (#714)
This commit improves the logic for checking if a given solution is reverting recent user edits. It now checks against intermediate states of the last edit step, in addition to the start and end states of all previous edit steps. It also refines the system prompt to be more explicit about not reverting user edits. 🤖 Generated with [Pochi](https://getpochi.com) Co-authored-by: Pochi <[email protected]>
1 parent e2fcfa1 commit 456aef9

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

packages/vscode/src/nes/client/chat-model-client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,6 @@ function extractResult(text: string, segments: NESPromptSegments) {
118118
}
119119

120120
const SystemPromptTemplate =
121-
"You are an AI coding assistant that helps with code completion and editing. You will be given a code snippet with an editable region marked.\nYour task is to complete or modify the code within that region based on the following events that happened in past.\nYou should not undo or revert the edits. \n\nUser edits:\n\n```diff\n{{edits}}\n```\n";
121+
"You are an AI coding assistant that helps with code completion and editing. You will be given a code snippet with an editable region marked.\nYour task is to complete or modify the code within that region based on the following events that happened in past. \nNOTE: DO NOT undo or revert the user edits. \n\nUser edits:\n\n```diff\n{{edits}}\n```\n";
122122
const UserPromptTemplate =
123123
"```{{filepath}}\n{{prefix}}<|editable_region_start|>{{editableRegionPrefix}}<|user_cursor_is_here|>{{editableRegionSuffix}}<|editable_region_end|>{{suffix}}\n```";

packages/vscode/src/nes/solution/item-filters.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { NESRequestContext } from "@/nes/contexts";
2+
import { applyEdit } from "../utils";
23
import type { NESSolutionItem } from "./item";
34

45
export function isRevertingRecentEdits(
@@ -7,10 +8,23 @@ export function isRevertingRecentEdits(
78
): boolean {
89
const editSteps = context.documentContext.editHistory;
910
const targetText = item.target.getText();
11+
// For every EditStep, check the state of start
1012
for (const editStep of editSteps) {
1113
if (targetText === editStep.getBefore().getText()) {
1214
return true;
1315
}
1416
}
17+
// For the last EditStep, check every change
18+
if (editSteps.length > 0) {
19+
const lastEditStep = editSteps[editSteps.length - 1];
20+
let before = lastEditStep.getBefore().getText();
21+
for (const edit of lastEditStep.getEdits()) {
22+
const { text } = applyEdit(before, edit);
23+
if (targetText === text) {
24+
return true;
25+
}
26+
before = text;
27+
}
28+
}
1529
return false;
1630
}

0 commit comments

Comments
 (0)