refactor: rename constants to be model-specific

This commit is contained in:
Jacob Kim 2025-08-04 09:53:07 -07:00
parent 24428b97d2
commit 8e79ce3587
9 changed files with 84 additions and 75 deletions

View file

@ -32,7 +32,8 @@ import { myersDiff } from "../diff/myers.js";
import { localPathOrUriToPath } from "../util/pathToUri.js";
import { replaceEscapedCharacters } from "../util/text.js";
import {
CODE_TO_EDIT_OPEN,
MERCURY_CODE_TO_EDIT_OPEN,
MERCURY_SYSTEM_PROMPT,
NEXT_EDIT_EDITABLE_REGION_BOTTOM_MARGIN,
NEXT_EDIT_EDITABLE_REGION_TOP_MARGIN,
} from "./constants.js";
@ -450,8 +451,7 @@ export class NextEditProvider {
const systemPrompt: Prompt = {
role: "system",
content:
"You are Mercury, created by Inception Labs. You are an AI Assistant and an expert at coding. Your role as an AI assistant is to help developers complete their code tasks by assisting in editing specific sections of code marked by the <|code_to_edit|> and <|/code_to_edit|> tags.\n\nYou have access to the following information to help you make informed suggestions:\n\n- recently_viewed_code_snippets: These are code snippets that the developer has recently looked at, which might provide context or examples relevant to the current task. They are listed from oldest to newest, with line numbers in the form #| to help you understand the edit diff history. It'\''s possible these are entirely irrelevant to the developer'\''s change.\n- current_file_content: The content of the file the developer is currently working on, providing the broader context of the code. Line numbers in the form #| are included to help you understand the edit diff history.\n- edit_diff_history: A record of changes made to the code, helping you understand the evolution of the code and the developer'\''s intentions. These changes are listed from oldest to latest. It'\''s possible a lot of old edit diff history is entirely irrelevant to the developer'\''s change.\n- cursor position marked as <|cursor|>: Indicates where the developer'\''s cursor is currently located, which can be crucial for understanding what part of the code they are focusing on.\n\nYour task is to predict and complete the changes the developer would have made next in the <|code_to_edit|> section. The developer may have stopped in the middle of typing. Your goal is to keep the developer on the path that you think they'\''re following. Some examples include further implementing a class, method, or variable, or improving the quality of the code. Make sure the developer doesn'\''t get distracted and ensure your suggestion is relevant. Consider what changes need to be made next, if any. If you think changes should be made, ask yourself if this is truly what needs to happen. If you are confident about it, then proceed with the changes.\n\n# Steps\n\n1. **Review Context**: Analyze the context from the resources provided, such as recently viewed snippets, edit history, surrounding code, and cursor location.\n2. **Evaluate Current Code**: Determine if the current code within the tags requires any corrections or enhancements.\n3. **Suggest Edits**: If changes are required, ensure they align with the developers patterns and improve code quality.\n4. **Maintain Consistency**: Ensure indentation and formatting follow the existing code style.\n\n# Output Format\n\n- Provide only the revised code within the tags. If no changes are necessary, simply return the original code from within the <|code_to_edit|> and <|/code_to_edit|> tags.\n- There are line numbers in the form #| in the code displayed to you above, but these are just for your reference. Please do not include the numbers of the form #| in your response.\n- Ensure that you do not output duplicate code that exists outside of these tags. The output should be the revised code that was between these tags including the <|code_to_edit|> and <|/code_to_edit|> tags.\n\n# Notes\n\n- Avoid undoing or reverting the developer'\''s last change unless there are obvious typos or errors.\n- Don'\''t include the line numbers of the form #| in your response.",
content: MERCURY_SYSTEM_PROMPT,
};
return [systemPrompt, promptMetadata.prompt];
@ -523,9 +523,11 @@ export class NextEditProvider {
return undefined;
}
const nextCompletion = msg.content.split(`${CODE_TO_EDIT_OPEN}\n`)[1]
const nextCompletion = msg.content.split(
`${MERCURY_CODE_TO_EDIT_OPEN}\n`,
)[1]
? replaceEscapedCharacters(
msg.content.split(`${CODE_TO_EDIT_OPEN}\n`)[1],
msg.content.split(`${MERCURY_CODE_TO_EDIT_OPEN}\n`)[1],
).replace(/\n$/, "")
: "";

View file

@ -1,22 +1,27 @@
export const IS_NEXT_EDIT_ACTIVE = false;
export const NEXT_EDIT_EDITABLE_REGION_TOP_MARGIN = 0;
export const NEXT_EDIT_EDITABLE_REGION_BOTTOM_MARGIN = 5;
export const USER_CURSOR_IS_HERE_TOKEN = "<|user_cursor_is_here|>";
export const EDITABLE_REGION_START_TOKEN = "<|editable_region_start|>";
export const EDITABLE_REGION_END_TOKEN = "<|editable_region_end|>";
export const RECENTLY_VIEWED_CODE_SNIPPETS_OPEN =
// Model 1-specific tokens.
export const MODEL_1_USER_CURSOR_IS_HERE_TOKEN = "<|user_cursor_is_here|>";
export const MODEL_1_EDITABLE_REGION_START_TOKEN = "<|editable_region_start|>";
export const MODEL_1_EDITABLE_REGION_END_TOKEN = "<|editable_region_end|>";
// Mercury Coder Next Edit-specific tokens.
export const MERCURY_RECENTLY_VIEWED_CODE_SNIPPETS_OPEN =
"<|recently_viewed_code_snippets|>";
export const RECENTLY_VIEWED_CODE_SNIPPETS_CLOSE =
export const MERCURY_RECENTLY_VIEWED_CODE_SNIPPETS_CLOSE =
"<|/recently_viewed_code_snippets|>";
export const RECENTLY_VIEWED_CODE_SNIPPET_OPEN =
export const MERCURY_RECENTLY_VIEWED_CODE_SNIPPET_OPEN =
"<|recently_viewed_code_snippet|>";
export const RECENTLY_VIEWED_CODE_SNIPPET_CLOSE =
export const MERCURY_RECENTLY_VIEWED_CODE_SNIPPET_CLOSE =
"<|/recently_viewed_code_snippet|>";
export const CURRENT_FILE_CONTENT_OPEN = "<|current_file_content|>";
export const CURRENT_FILE_CONTENT_CLOSE = "<|/current_file_content|>";
export const CODE_TO_EDIT_OPEN = "<|code_to_edit|>";
export const CODE_TO_EDIT_CLOSE = "<|/code_to_edit|>";
export const EDIT_DIFF_HISTORY_OPEN = "<|edit_diff_history|>";
export const EDIT_DIFF_HISTORY_CLOSE = "<|/edit_diff_history|>";
export const CURSOR = "<|cursor|>";
export const MERCURY_CURRENT_FILE_CONTENT_OPEN = "<|current_file_content|>";
export const MERCURY_CURRENT_FILE_CONTENT_CLOSE = "<|/current_file_content|>";
export const MERCURY_CODE_TO_EDIT_OPEN = "<|code_to_edit|>";
export const MERCURY_CODE_TO_EDIT_CLOSE = "<|/code_to_edit|>";
export const MERCURY_EDIT_DIFF_HISTORY_OPEN = "<|edit_diff_history|>";
export const MERCURY_EDIT_DIFF_HISTORY_CLOSE = "<|/edit_diff_history|>";
export const MERCURY_CURSOR = "<|cursor|>";
export const MERCURY_SYSTEM_PROMPT =
"You are Mercury, created by Inception Labs. You are an AI Assistant and an expert at coding. Your role as an AI assistant is to help developers complete their code tasks by assisting in editing specific sections of code marked by the <|code_to_edit|> and <|/code_to_edit|> tags.\n\nYou have access to the following information to help you make informed suggestions:\n\n- recently_viewed_code_snippets: These are code snippets that the developer has recently looked at, which might provide context or examples relevant to the current task. They are listed from oldest to newest, with line numbers in the form #| to help you understand the edit diff history. It'\''s possible these are entirely irrelevant to the developer'\''s change.\n- current_file_content: The content of the file the developer is currently working on, providing the broader context of the code. Line numbers in the form #| are included to help you understand the edit diff history.\n- edit_diff_history: A record of changes made to the code, helping you understand the evolution of the code and the developer'\''s intentions. These changes are listed from oldest to latest. It'\''s possible a lot of old edit diff history is entirely irrelevant to the developer'\''s change.\n- cursor position marked as <|cursor|>: Indicates where the developer'\''s cursor is currently located, which can be crucial for understanding what part of the code they are focusing on.\n\nYour task is to predict and complete the changes the developer would have made next in the <|code_to_edit|> section. The developer may have stopped in the middle of typing. Your goal is to keep the developer on the path that you think they'\''re following. Some examples include further implementing a class, method, or variable, or improving the quality of the code. Make sure the developer doesn'\''t get distracted and ensure your suggestion is relevant. Consider what changes need to be made next, if any. If you think changes should be made, ask yourself if this is truly what needs to happen. If you are confident about it, then proceed with the changes.\n\n# Steps\n\n1. **Review Context**: Analyze the context from the resources provided, such as recently viewed snippets, edit history, surrounding code, and cursor location.\n2. **Evaluate Current Code**: Determine if the current code within the tags requires any corrections or enhancements.\n3. **Suggest Edits**: If changes are required, ensure they align with the developers patterns and improve code quality.\n4. **Maintain Consistency**: Ensure indentation and formatting follow the existing code style.\n\n# Output Format\n\n- Provide only the revised code within the tags. If no changes are necessary, simply return the original code from within the <|code_to_edit|> and <|/code_to_edit|> tags.\n- There are line numbers in the form #| in the code displayed to you above, but these are just for your reference. Please do not include the numbers of the form #| in your response.\n- Ensure that you do not output duplicate code that exists outside of these tags. The output should be the revised code that was between these tags including the <|code_to_edit|> and <|/code_to_edit|> tags.\n\n# Notes\n\n- Avoid undoing or reverting the developer'\''s last change unless there are obvious typos or errors.\n- Don'\''t include the line numbers of the form #| in your response.";

View file

@ -3,14 +3,14 @@ import { Position } from "../..";
import { SnippetPayload } from "../../autocomplete/snippets";
import { HelperVars } from "../../autocomplete/util/HelperVars";
import {
CURRENT_FILE_CONTENT_CLOSE,
CURRENT_FILE_CONTENT_OPEN,
CURSOR,
EDIT_DIFF_HISTORY_CLOSE,
EDIT_DIFF_HISTORY_OPEN,
RECENTLY_VIEWED_CODE_SNIPPETS_CLOSE,
RECENTLY_VIEWED_CODE_SNIPPETS_OPEN,
USER_CURSOR_IS_HERE_TOKEN,
MERCURY_CURRENT_FILE_CONTENT_CLOSE,
MERCURY_CURRENT_FILE_CONTENT_OPEN,
MERCURY_CURSOR,
MERCURY_EDIT_DIFF_HISTORY_CLOSE,
MERCURY_EDIT_DIFF_HISTORY_OPEN,
MERCURY_RECENTLY_VIEWED_CODE_SNIPPETS_CLOSE,
MERCURY_RECENTLY_VIEWED_CODE_SNIPPETS_OPEN,
MODEL_1_USER_CURSOR_IS_HERE_TOKEN,
} from "../constants";
import {
MercuryTemplateVars,
@ -40,7 +40,7 @@ export type NextEditModelName =
const NEXT_EDIT_MODEL_TEMPLATES: Record<NextEditModelName, NextEditTemplate> = {
"mercury-coder-nextedit": {
template: `${RECENTLY_VIEWED_CODE_SNIPPETS_OPEN}\n{{{recentlyViewedCodeSnippets}}}\n${RECENTLY_VIEWED_CODE_SNIPPETS_CLOSE}\n\n${CURRENT_FILE_CONTENT_OPEN}\n{{{currentFileContent}}}\n${CURRENT_FILE_CONTENT_CLOSE}\n\n${EDIT_DIFF_HISTORY_OPEN}\n{{{editDiffHistory}}}\n${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\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.`,
},
"model-1": {
template:
@ -111,7 +111,7 @@ export async function renderPrompt(
editedCodeWithTokens = insertTokens(
helper.fileContents.split("\n"),
helper.pos,
CURSOR,
MERCURY_CURSOR,
);
const mercuryCtx: MercuryTemplateVars = {
@ -138,7 +138,7 @@ export async function renderPrompt(
editedCodeWithTokens = insertTokens(
helper.fileContents.split("\n"),
helper.pos,
USER_CURSOR_IS_HERE_TOKEN,
MODEL_1_USER_CURSOR_IS_HERE_TOKEN,
);
const model1Ctx: Model1TemplateVars = {

View file

@ -4,14 +4,14 @@ import { SnippetPayload } from "../../autocomplete/snippets";
import { AutocompleteSnippetType } from "../../autocomplete/snippets/types";
import { HelperVars } from "../../autocomplete/util/HelperVars";
import {
CURRENT_FILE_CONTENT_CLOSE,
CURRENT_FILE_CONTENT_OPEN,
CURSOR,
EDIT_DIFF_HISTORY_CLOSE,
EDIT_DIFF_HISTORY_OPEN,
RECENTLY_VIEWED_CODE_SNIPPETS_CLOSE,
RECENTLY_VIEWED_CODE_SNIPPETS_OPEN,
USER_CURSOR_IS_HERE_TOKEN,
MERCURY_CURRENT_FILE_CONTENT_CLOSE,
MERCURY_CURRENT_FILE_CONTENT_OPEN,
MERCURY_CURSOR,
MERCURY_EDIT_DIFF_HISTORY_CLOSE,
MERCURY_EDIT_DIFF_HISTORY_OPEN,
MERCURY_RECENTLY_VIEWED_CODE_SNIPPETS_CLOSE,
MERCURY_RECENTLY_VIEWED_CODE_SNIPPETS_OPEN,
MODEL_1_USER_CURSOR_IS_HERE_TOKEN,
} from "../constants";
import {
NextEditModelName,
@ -75,15 +75,15 @@ describe("NextEditPromptEngine", () => {
expect(result.prompt.role).toBe("user");
const content = result.prompt.content;
expect(content).toContain(RECENTLY_VIEWED_CODE_SNIPPETS_OPEN);
expect(content).toContain(RECENTLY_VIEWED_CODE_SNIPPETS_CLOSE);
expect(content).toContain(CURRENT_FILE_CONTENT_OPEN);
expect(content).toContain(CURRENT_FILE_CONTENT_CLOSE);
expect(content).toContain(EDIT_DIFF_HISTORY_OPEN);
expect(content).toContain(EDIT_DIFF_HISTORY_CLOSE);
expect(content).toContain(MERCURY_RECENTLY_VIEWED_CODE_SNIPPETS_OPEN);
expect(content).toContain(MERCURY_RECENTLY_VIEWED_CODE_SNIPPETS_CLOSE);
expect(content).toContain(MERCURY_CURRENT_FILE_CONTENT_OPEN);
expect(content).toContain(MERCURY_CURRENT_FILE_CONTENT_CLOSE);
expect(content).toContain(MERCURY_EDIT_DIFF_HISTORY_OPEN);
expect(content).toContain(MERCURY_EDIT_DIFF_HISTORY_CLOSE);
expect(result.userEdits).toBe(ctx.editDiffHistory);
expect(result.userExcerpts).toContain(CURSOR);
expect(result.userExcerpts).toContain(MERCURY_CURSOR);
});
it("should render model-1 prompt correctly", async () => {
@ -96,7 +96,7 @@ describe("NextEditPromptEngine", () => {
expect(result.prompt.content).toContain("```ts");
expect(result.userEdits).toBe(ctx.userEdits);
expect(result.userExcerpts).toContain(USER_CURSOR_IS_HERE_TOKEN);
expect(result.userExcerpts).toContain(MODEL_1_USER_CURSOR_IS_HERE_TOKEN);
});
it("should handle 'this field is not used' model name", async () => {
@ -229,7 +229,7 @@ describe("NextEditPromptEngine", () => {
};
return renderPrompt(mercuryHelper, ctx).then((result) => {
expect(result.userExcerpts).toContain(CURSOR);
expect(result.userExcerpts).toContain(MERCURY_CURSOR);
});
});
});

View file

@ -1,5 +1,9 @@
import { Position, Range } from "../..";
import { CODE_TO_EDIT_CLOSE, CODE_TO_EDIT_OPEN, CURSOR } from "../constants";
import {
MERCURY_CODE_TO_EDIT_CLOSE,
MERCURY_CODE_TO_EDIT_OPEN,
MERCURY_CURSOR,
} from "../constants";
import { insertCursorToken } from "./utils";
export function recentlyViewedCodeSnippetsBlock(
@ -28,17 +32,17 @@ export function currentFileContentBlock(
const insertedCursorLines = insertCursorToken(
currentFileContentLines,
cursorPosition,
CURSOR,
MERCURY_CURSOR,
);
const instrumentedLines = [
...insertedCursorLines.slice(0, editableRegionStartLine),
CODE_TO_EDIT_OPEN,
MERCURY_CODE_TO_EDIT_OPEN,
...insertedCursorLines.slice(
editableRegionStartLine,
editableRegionEndLine + 1,
),
CODE_TO_EDIT_CLOSE,
MERCURY_CODE_TO_EDIT_CLOSE,
...insertedCursorLines.slice(editableRegionEndLine + 1),
];

View file

@ -1,6 +1,10 @@
import { describe, expect, it } from "vitest";
import { Position } from "../..";
import { CODE_TO_EDIT_CLOSE, CODE_TO_EDIT_OPEN, CURSOR } from "../constants";
import {
MERCURY_CODE_TO_EDIT_CLOSE,
MERCURY_CODE_TO_EDIT_OPEN,
MERCURY_CURSOR,
} from "../constants";
import {
currentFileContentBlock,
editHistoryBlock,
@ -51,14 +55,14 @@ describe("mercuryCoderNextEdit", () => {
const expected =
"line 1\n" +
CODE_TO_EDIT_OPEN +
MERCURY_CODE_TO_EDIT_OPEN +
"\n" +
"line 2\n" +
"lin" +
CURSOR +
MERCURY_CURSOR +
"e 3\n" +
"line 4\n" +
CODE_TO_EDIT_CLOSE +
MERCURY_CODE_TO_EDIT_CLOSE +
"\n" +
"line 5";
@ -78,9 +82,9 @@ describe("mercuryCoderNextEdit", () => {
cursorPosition,
);
expect(result).toContain(CODE_TO_EDIT_OPEN);
expect(result).toContain(CURSOR + "line 2");
expect(result).toContain(CODE_TO_EDIT_CLOSE);
expect(result).toContain(MERCURY_CODE_TO_EDIT_OPEN);
expect(result).toContain(MERCURY_CURSOR + "line 2");
expect(result).toContain(MERCURY_CODE_TO_EDIT_CLOSE);
});
it("should handle cursor at the end of a line", () => {
@ -96,7 +100,7 @@ describe("mercuryCoderNextEdit", () => {
cursorPosition,
);
expect(result).toContain("line 2" + CURSOR);
expect(result).toContain("line 2" + MERCURY_CURSOR);
});
});

View file

@ -1,7 +1,7 @@
import { Position } from "../..";
import {
EDITABLE_REGION_END_TOKEN,
EDITABLE_REGION_START_TOKEN,
MODEL_1_EDITABLE_REGION_END_TOKEN,
MODEL_1_EDITABLE_REGION_START_TOKEN,
NEXT_EDIT_EDITABLE_REGION_BOTTOM_MARGIN,
NEXT_EDIT_EDITABLE_REGION_TOP_MARGIN,
} from "../constants";
@ -53,9 +53,9 @@ export function insertEditableRegionTokensWithStaticRange(
const instrumentedLines = [
...lines.slice(0, editableRegionStart),
EDITABLE_REGION_START_TOKEN,
MODEL_1_EDITABLE_REGION_START_TOKEN,
...lines.slice(editableRegionStart, editableRegionEnd + 1),
EDITABLE_REGION_END_TOKEN,
MODEL_1_EDITABLE_REGION_END_TOKEN,
...lines.slice(editableRegionEnd + 1),
];

View file

@ -1,8 +1,8 @@
import { describe, expect, it } from "vitest";
import { Position } from "../..";
import {
EDITABLE_REGION_END_TOKEN,
EDITABLE_REGION_START_TOKEN,
MODEL_1_EDITABLE_REGION_END_TOKEN,
MODEL_1_EDITABLE_REGION_START_TOKEN,
NEXT_EDIT_EDITABLE_REGION_BOTTOM_MARGIN,
NEXT_EDIT_EDITABLE_REGION_TOP_MARGIN,
} from "../constants";
@ -88,9 +88,9 @@ describe("insertEditableRegionTokensWithStaticRange", () => {
const expected = [
...lines.slice(0, expectedStart),
EDITABLE_REGION_START_TOKEN,
MODEL_1_EDITABLE_REGION_START_TOKEN,
...lines.slice(expectedStart, expectedEnd + 1),
EDITABLE_REGION_END_TOKEN,
MODEL_1_EDITABLE_REGION_END_TOKEN,
...lines.slice(expectedEnd + 1),
];
@ -112,11 +112,11 @@ describe("insertEditableRegionTokensWithStaticRange", () => {
const expected = [
"line 0",
EDITABLE_REGION_START_TOKEN,
MODEL_1_EDITABLE_REGION_START_TOKEN,
"line 1",
"line 2",
"line 3",
EDITABLE_REGION_END_TOKEN,
MODEL_1_EDITABLE_REGION_END_TOKEN,
"line 4",
];

View file

@ -367,12 +367,6 @@ export class VsCodeExtension {
selectionManager.documentChanged();
}
// if (event.contentChanges.length > 0) {
// this.isTypingSession = true;
// this.lastDocumentChangeTime = Date.now();
// this.resetTypingSession();
// }
const editInfo = await handleTextDocumentChange(
event,
this.configHandler,