From c766bb060b7d4d2c7bb82a22676a458de518dc51 Mon Sep 17 00:00:00 2001 From: Jacob Kim Date: Tue, 19 Aug 2025 00:22:35 -0700 Subject: [PATCH] feat: fix the next edit provider to use the new edit endpoint (#7233) * feat: handle new model output format * feat: remove trailing user prompt --- core/nextEdit/NextEditProvider.ts | 32 ++++++------------- .../templating/NextEditPromptEngine.ts | 4 +-- 2 files changed, 12 insertions(+), 24 deletions(-) diff --git a/core/nextEdit/NextEditProvider.ts b/core/nextEdit/NextEditProvider.ts index e6634896f..c3bc079d1 100644 --- a/core/nextEdit/NextEditProvider.ts +++ b/core/nextEdit/NextEditProvider.ts @@ -36,7 +36,6 @@ import { countTokens } from "../llm/countTokens.js"; import { localPathOrUriToPath } from "../util/pathToUri.js"; import { INSTINCT_SYSTEM_PROMPT, - MERCURY_CODE_TO_EDIT_OPEN, MERCURY_SYSTEM_PROMPT, MODEL_WINDOW_SIZES, UNIQUE_TOKEN, @@ -688,21 +687,14 @@ export class NextEditProvider { const llm = await this._prepareLlm(); if (!llm) return undefined; - // TODO: Check for mercury models, and inject some unique tokens so that - // the backend knows that mercury-coder is a next edit. - // TODO: Capture the unique tokens in llm Inception.ts and apis Inception.ts - // TODO: where does capabilities come in??? - // My best answer is that it's a non-mercury-specific way of determining if - // the model supports next edit. If it does, then we can inject the unique tokens. - // Check for mercury models, and inject unique token for next edit. + // Capture the unique tokens in llm Inception.ts and apis Inception.ts const modelName = helper.modelName; if ( modelName.includes(NEXT_EDIT_MODELS.MERCURY_CODER) || modelName.includes(NEXT_EDIT_MODELS.MERCURY_CODER_NEXTEDIT) ) { // Inject the unique token to the last prompt's content. - // TODO: Check if there is an actual modification of prompts. const lastPrompt = prompts[prompts.length - 1]; if (lastPrompt && typeof lastPrompt.content === "string") { lastPrompt.content += UNIQUE_TOKEN; @@ -712,24 +704,20 @@ export class NextEditProvider { const msg: ChatMessage = await llm.chat([prompts[1]], token, { stream: false, }); - console.log("message"); - console.log(msg.content); if (typeof msg.content !== "string") { return undefined; } - // const nextCompletion = msg.content.split( - // `${MERCURY_CODE_TO_EDIT_OPEN}\n`, - // )[1] - // ? replaceEscapedCharacters( - // msg.content.split(`${MERCURY_CODE_TO_EDIT_OPEN}\n`)[1], - // ).replace(/\n$/, "") - // : replaceEscapedCharacters(msg.content); - const nextCompletion = - msg.content - .split(`${MERCURY_CODE_TO_EDIT_OPEN}\n`)[1] - .replace(/\n$/, "") ?? msg.content; + // NOTE: Keep this in case there is a change in spec. + // const nextCompletion = + // msg.content + // .split(`${MERCURY_CODE_TO_EDIT_OPEN}\n`)[1] + // .replace(/\n$/, "") ?? msg.content; + const nextCompletion = msg.content.slice( + msg.content.indexOf("\`\`\`\n") + "\'\'\'\n".length, + msg.content.lastIndexOf("\n\n\`\`\`"), + ); if (opts?.usingFullFileDiff === false || !opts?.usingFullFileDiff) { return await this._handlePartialFileDiff( diff --git a/core/nextEdit/templating/NextEditPromptEngine.ts b/core/nextEdit/templating/NextEditPromptEngine.ts index 3ec20fb37..a8f8b32b9 100644 --- a/core/nextEdit/templating/NextEditPromptEngine.ts +++ b/core/nextEdit/templating/NextEditPromptEngine.ts @@ -40,10 +40,10 @@ type TemplateRenderer = (vars: TemplateVars) => string; const NEXT_EDIT_MODEL_TEMPLATES: Record = { "mercury-coder": { - 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.`, + 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`, }, "mercury-coder-nextedit": { - 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.`, + 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`, }, instinct: { 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:`,