Skip to content

Commit c766bb0

Browse files
authored
feat: fix the next edit provider to use the new edit endpoint (#7233)
* feat: handle new model output format * feat: remove trailing user prompt
1 parent f5a69ea commit c766bb0

File tree

2 files changed

+12
-24
lines changed

2 files changed

+12
-24
lines changed

core/nextEdit/NextEditProvider.ts

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import { countTokens } from "../llm/countTokens.js";
3636
import { localPathOrUriToPath } from "../util/pathToUri.js";
3737
import {
3838
INSTINCT_SYSTEM_PROMPT,
39-
MERCURY_CODE_TO_EDIT_OPEN,
4039
MERCURY_SYSTEM_PROMPT,
4140
MODEL_WINDOW_SIZES,
4241
UNIQUE_TOKEN,
@@ -688,21 +687,14 @@ export class NextEditProvider {
688687
const llm = await this._prepareLlm();
689688
if (!llm) return undefined;
690689

691-
// TODO: Check for mercury models, and inject some unique tokens so that
692-
// the backend knows that mercury-coder is a next edit.
693-
// TODO: Capture the unique tokens in llm Inception.ts and apis Inception.ts
694-
// TODO: where does capabilities come in???
695-
// My best answer is that it's a non-mercury-specific way of determining if
696-
// the model supports next edit. If it does, then we can inject the unique tokens.
697-
698690
// Check for mercury models, and inject unique token for next edit.
691+
// Capture the unique tokens in llm Inception.ts and apis Inception.ts
699692
const modelName = helper.modelName;
700693
if (
701694
modelName.includes(NEXT_EDIT_MODELS.MERCURY_CODER) ||
702695
modelName.includes(NEXT_EDIT_MODELS.MERCURY_CODER_NEXTEDIT)
703696
) {
704697
// Inject the unique token to the last prompt's content.
705-
// TODO: Check if there is an actual modification of prompts.
706698
const lastPrompt = prompts[prompts.length - 1];
707699
if (lastPrompt && typeof lastPrompt.content === "string") {
708700
lastPrompt.content += UNIQUE_TOKEN;
@@ -712,24 +704,20 @@ export class NextEditProvider {
712704
const msg: ChatMessage = await llm.chat([prompts[1]], token, {
713705
stream: false,
714706
});
715-
console.log("message");
716-
console.log(msg.content);
717707

718708
if (typeof msg.content !== "string") {
719709
return undefined;
720710
}
721711

722-
// const nextCompletion = msg.content.split(
723-
// `${MERCURY_CODE_TO_EDIT_OPEN}\n`,
724-
// )[1]
725-
// ? replaceEscapedCharacters(
726-
// msg.content.split(`${MERCURY_CODE_TO_EDIT_OPEN}\n`)[1],
727-
// ).replace(/\n$/, "")
728-
// : replaceEscapedCharacters(msg.content);
729-
const nextCompletion =
730-
msg.content
731-
.split(`${MERCURY_CODE_TO_EDIT_OPEN}\n`)[1]
732-
.replace(/\n$/, "") ?? msg.content;
712+
// NOTE: Keep this in case there is a change in spec.
713+
// const nextCompletion =
714+
// msg.content
715+
// .split(`${MERCURY_CODE_TO_EDIT_OPEN}\n`)[1]
716+
// .replace(/\n$/, "") ?? msg.content;
717+
const nextCompletion = msg.content.slice(
718+
msg.content.indexOf("\`\`\`\n") + "\'\'\'\n".length,
719+
msg.content.lastIndexOf("\n\n\`\`\`"),
720+
);
733721

734722
if (opts?.usingFullFileDiff === false || !opts?.usingFullFileDiff) {
735723
return await this._handlePartialFileDiff(

core/nextEdit/templating/NextEditPromptEngine.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ type TemplateRenderer = (vars: TemplateVars) => string;
4040

4141
const NEXT_EDIT_MODEL_TEMPLATES: Record<NEXT_EDIT_MODELS, NextEditTemplate> = {
4242
"mercury-coder": {
43-
template: `${MERCURY_RECENTLY_VIEWED_CODE_SNIPPETS_OPEN}\n{{{recentlyViewedCodeSnippets}}}\n${MERCURY_RECENTLY_VIEWED_CODE_SNIPPETS_CLOSE}\n\n${MERCURY_CURRENT_FILE_CONTENT_OPEN}\n{{{currentFileContent}}}\n${MERCURY_CURRENT_FILE_CONTENT_CLOSE}\n\n${MERCURY_EDIT_DIFF_HISTORY_OPEN}\n{{{editDiffHistory}}}\n${MERCURY_EDIT_DIFF_HISTORY_CLOSE}\n\nThe developer was working on a section of code within the tags \`<|code_to_edit|>\` in the file located at {{{currentFilePath}}}.\nUsing the given \`recently_viewed_code_snippets\`, \`current_file_content\`, \`edit_diff_history\`, and the cursor position marked as \`<|cursor|>\`, please continue the developer's work. Update the \`code_to_edit\` section by predicting and completing the changes they would have made next. Provide the revised code that was between the \`<|code_to_edit|>\` and \`<|/code_to_edit|>\` tags, including the tags themselves.`,
43+
template: `${MERCURY_RECENTLY_VIEWED_CODE_SNIPPETS_OPEN}\n{{{recentlyViewedCodeSnippets}}}\n${MERCURY_RECENTLY_VIEWED_CODE_SNIPPETS_CLOSE}\n\n${MERCURY_CURRENT_FILE_CONTENT_OPEN}\n{{{currentFileContent}}}\n${MERCURY_CURRENT_FILE_CONTENT_CLOSE}\n\n${MERCURY_EDIT_DIFF_HISTORY_OPEN}\n{{{editDiffHistory}}}\n${MERCURY_EDIT_DIFF_HISTORY_CLOSE}\n`,
4444
},
4545
"mercury-coder-nextedit": {
46-
template: `${MERCURY_RECENTLY_VIEWED_CODE_SNIPPETS_OPEN}\n{{{recentlyViewedCodeSnippets}}}\n${MERCURY_RECENTLY_VIEWED_CODE_SNIPPETS_CLOSE}\n\n${MERCURY_CURRENT_FILE_CONTENT_OPEN}\n{{{currentFileContent}}}\n${MERCURY_CURRENT_FILE_CONTENT_CLOSE}\n\n${MERCURY_EDIT_DIFF_HISTORY_OPEN}\n{{{editDiffHistory}}}\n${MERCURY_EDIT_DIFF_HISTORY_CLOSE}\n\nThe developer was working on a section of code within the tags \`<|code_to_edit|>\` in the file located at {{{currentFilePath}}}.\nUsing the given \`recently_viewed_code_snippets\`, \`current_file_content\`, \`edit_diff_history\`, and the cursor position marked as \`<|cursor|>\`, please continue the developer's work. Update the \`code_to_edit\` section by predicting and completing the changes they would have made next. Provide the revised code that was between the \`<|code_to_edit|>\` and \`<|/code_to_edit|>\` tags, including the tags themselves.`,
46+
template: `${MERCURY_RECENTLY_VIEWED_CODE_SNIPPETS_OPEN}\n{{{recentlyViewedCodeSnippets}}}\n${MERCURY_RECENTLY_VIEWED_CODE_SNIPPETS_CLOSE}\n\n${MERCURY_CURRENT_FILE_CONTENT_OPEN}\n{{{currentFileContent}}}\n${MERCURY_CURRENT_FILE_CONTENT_CLOSE}\n\n${MERCURY_EDIT_DIFF_HISTORY_OPEN}\n{{{editDiffHistory}}}\n${MERCURY_EDIT_DIFF_HISTORY_CLOSE}\n`,
4747
},
4848
instinct: {
4949
template: `${INSTINCT_USER_PROMPT_PREFIX}\n\n### Context:\n{{{contextSnippets}}}\n\n### User Edits:\n\n{{{editDiffHistory}}}\n\n### User Excerpts:\n{{{currentFilePath}}}\n\n{{{currentFileContent}}}\`\`\`\n### Response:`,

0 commit comments

Comments
 (0)