diff --git a/core/config/yaml/models.ts b/core/config/yaml/models.ts index 3f571c3fb..102e4152b 100644 --- a/core/config/yaml/models.ts +++ b/core/config/yaml/models.ts @@ -68,6 +68,7 @@ async function modelConfigToBaseLLM({ capabilities: { tools: model.capabilities?.includes("tool_use"), uploadImage: model.capabilities?.includes("image_input"), + nextEdit: model.capabilities?.includes("next_edit"), }, autocompleteOptions: model.autocompleteOptions, }; diff --git a/core/index.d.ts b/core/index.d.ts index 8995b64a9..9941e2bc2 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -1141,6 +1141,7 @@ export interface BaseCompletionOptions { export interface ModelCapability { uploadImage?: boolean; tools?: boolean; + nextEdit?: boolean; } export interface ModelDescription { diff --git a/core/llm/autodetect.ts b/core/llm/autodetect.ts index 571a7a700..b2943e316 100644 --- a/core/llm/autodetect.ts +++ b/core/llm/autodetect.ts @@ -1,4 +1,5 @@ import { ChatMessage, ModelCapability, TemplateType } from "../index.js"; +import { NEXT_EDIT_MODELS } from "./constants.js"; import { anthropicTemplateMessages, @@ -173,6 +174,34 @@ function isProviderHandlesTemplatingOrNoTemplateTypeRequired( ); } +// NOTE: When updating this list, +// update core/nextEdit/templating/NextEditPromptEngine.ts as well. +const MODEL_SUPPORTS_NEXT_EDIT: string[] = [ + NEXT_EDIT_MODELS.MERCURY_CODER_NEXTEDIT, + NEXT_EDIT_MODELS.MODEL_1, +]; + +function modelSupportsNextEdit( + capabilities: ModelCapability | undefined, + model: string, + title: string | undefined, +): boolean { + if (capabilities?.nextEdit !== undefined) { + return capabilities.nextEdit; + } + + const lower = model.toLowerCase(); + if ( + MODEL_SUPPORTS_NEXT_EDIT.some( + (modelName) => lower.includes(modelName) || title?.includes(modelName), + ) + ) { + return true; + } + + return false; +} + function autodetectTemplateType(model: string): TemplateType | undefined { const lower = model.toLowerCase(); @@ -391,4 +420,5 @@ export { autodetectTemplateType, llmCanGenerateInParallel, modelSupportsImages, + modelSupportsNextEdit, }; diff --git a/core/llm/autodetect.vitest.ts b/core/llm/autodetect.vitest.ts index e9d7180ae..01d48aacc 100644 --- a/core/llm/autodetect.vitest.ts +++ b/core/llm/autodetect.vitest.ts @@ -1,5 +1,5 @@ -import { expect, test } from "vitest"; -import { autodetectTemplateType } from "./autodetect"; +import { describe, expect, it, test } from "vitest"; +import { autodetectTemplateType, modelSupportsNextEdit } from "./autodetect"; test("autodetectTemplateType returns 'codellama-70b' for CodeLlama 70B models", () => { expect(autodetectTemplateType("codellama-70b")).toBe("codellama-70b"); @@ -201,3 +201,137 @@ test("autodetectTemplateType handles models with mixed keywords", () => { expect(autodetectTemplateType("gpt-llama")).toBe(undefined); // gpt comes first, returns undefined expect(autodetectTemplateType("claude-llama")).toBe("llama2"); // llama comes first, returns llama2 }); + +describe("modelSupportsNextEdit", () => { + describe("when capabilities.nextEdit is defined", () => { + it("should return true when capabilities.nextEdit is true", () => { + expect( + modelSupportsNextEdit( + { + nextEdit: true, + }, + "any-model", + "Any Title", + ), + ).toBe(true); + }); + + it("should return false when capabilities.nextEdit is false", () => { + expect( + modelSupportsNextEdit( + { nextEdit: false }, + "mercury-coder-nextedit", + "Mercury Coder", + ), + ).toBe(false); + }); + + it("should prioritize capabilities over model name matching", () => { + // Even though model name matches, capabilities should take precedence. + expect( + modelSupportsNextEdit( + { nextEdit: false }, + "mercury-coder-nextedit", + "Mercury Coder", + ), + ).toBe(false); + }); + }); + + describe("when capabilities.nextEdit is undefined", () => { + it("should return true for mercury-coder-nextedit model (case insensitive)", () => { + expect( + modelSupportsNextEdit(undefined, "Mercury-Coder-Nextedit", undefined), + ).toBe(true); + }); + + it("should return true for model-1", () => { + expect(modelSupportsNextEdit(undefined, "model-1", undefined)).toBe(true); + }); + + it("should return true when model contains supported model name as substring", () => { + expect( + modelSupportsNextEdit( + undefined, + "provider/mercury-coder-nextedit-v2", + undefined, + ), + ).toBe(true); + }); + + it("should return true when title contains supported model name", () => { + expect( + modelSupportsNextEdit( + undefined, + "some-model", + "This is mercury-coder-nextedit model", + ), + ).toBe(true); + }); + + it("should return true when title contains model-1", () => { + expect( + modelSupportsNextEdit(undefined, "some-model", "model-1 deployment"), + ).toBe(true); + }); + + it("should return true for unsupported models that have capabilities explicitly set to true", () => { + expect( + modelSupportsNextEdit( + { + nextEdit: true, + }, + "gpt-4", + "GPT-4 Model", + ), + ).toBe(true); + }); + + it("should return false for unsupported models", () => { + expect(modelSupportsNextEdit(undefined, "gpt-4", "GPT-4 Model")).toBe( + false, + ); + }); + + it("should return false when model and title are both undefined/null", () => { + expect(modelSupportsNextEdit(undefined, "", undefined)).toBe(false); + }); + + it("should return false when model and title do not contain supported names", () => { + expect( + modelSupportsNextEdit(undefined, "claude-3", "Claude 3 Sonnet"), + ).toBe(false); + }); + }); + + describe("edge cases", () => { + it("should handle empty strings", () => { + expect(modelSupportsNextEdit(undefined, "", "")).toBe(false); + }); + + it("should handle undefined title gracefully", () => { + expect( + modelSupportsNextEdit(undefined, "mercury-coder-nextedit", undefined), + ).toBe(true); + }); + + it("should handle case sensitivity correctly", () => { + expect( + modelSupportsNextEdit(undefined, "MERCURY-CODER-NEXTEDIT", "MODEL-1"), + ).toBe(true); + }); + + it("should handle capabilities with other properties", () => { + expect( + modelSupportsNextEdit( + { + nextEdit: true, + uploadImage: false, + }, + "unsupported-model", + undefined, + ), + ).toBe(true); + }); + }); +}); diff --git a/core/llm/constants.ts b/core/llm/constants.ts index b6759663d..5b93048e9 100644 --- a/core/llm/constants.ts +++ b/core/llm/constants.ts @@ -19,6 +19,11 @@ export enum LLMConfigurationStatuses { MISSING_ENV_SECRET = "missing-env-secret", } +export enum NEXT_EDIT_MODELS { + MERCURY_CODER_NEXTEDIT = "mercury-coder-nextedit", + MODEL_1 = "model-1", +} + export { DEFAULT_ARGS, DEFAULT_CONTEXT_LENGTH, diff --git a/core/nextEdit/NextEditProvider.ts b/core/nextEdit/NextEditProvider.ts index 0b97c4491..ade13b4c3 100644 --- a/core/nextEdit/NextEditProvider.ts +++ b/core/nextEdit/NextEditProvider.ts @@ -30,6 +30,7 @@ import AutocompleteLruCache from "../autocomplete/util/AutocompleteLruCache.js"; import { HelperVars } from "../autocomplete/util/HelperVars.js"; import { AutocompleteInput } from "../autocomplete/util/types.js"; import { myersDiff } from "../diff/myers.js"; +import { modelSupportsNextEdit } from "../llm/autodetect.js"; import { countTokens } from "../llm/countTokens.js"; import { localPathOrUriToPath } from "../util/pathToUri.js"; import { replaceEscapedCharacters } from "../util/text.js"; @@ -341,6 +342,14 @@ export class NextEditProvider { return { token, startTime, helper: undefined }; } + // In vscode, this check is done in extensions/vscode/src/extension/VsCodeExtension.ts. + // For other editors, this check should be done in their respective config reloaders. + // This is left for a final check. + if (!modelSupportsNextEdit(llm.capabilities, llm.model, llm.title)) { + console.error(`${llm.model} is not capable of next edit.`); + return { token, startTime, helper: undefined }; + } + if (llm.promptTemplates?.autocomplete) { options.template = llm.promptTemplates.autocomplete as string; } diff --git a/core/nextEdit/templating/NextEditPromptEngine.ts b/core/nextEdit/templating/NextEditPromptEngine.ts index 24f16e681..29e958b18 100644 --- a/core/nextEdit/templating/NextEditPromptEngine.ts +++ b/core/nextEdit/templating/NextEditPromptEngine.ts @@ -30,15 +30,11 @@ import { insertCursorToken, insertEditableRegionTokensWithStaticRange, } from "./utils"; +import { NEXT_EDIT_MODELS } from "../../llm/constants"; type TemplateRenderer = (vars: TemplateVars) => string; -export type NextEditModelName = - | "mercury-coder-nextedit" - | "model-1" - | "this field is not used"; - -const NEXT_EDIT_MODEL_TEMPLATES: Record = { +const NEXT_EDIT_MODEL_TEMPLATES: Record = { "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.`, }, @@ -46,13 +42,10 @@ const NEXT_EDIT_MODEL_TEMPLATES: Record = { template: "### User Edits:\n\n{{{userEdits}}}\n\n### User Excerpts:\n\n```{{{languageShorthand}}}\n{{{userExcerpts}}}```", }, - "this field is not used": { - template: "NEXT_EDIT", - }, }; function templateRendererOfModel( - modelName: NextEditModelName, + modelName: NEXT_EDIT_MODELS, ): TemplateRenderer { let template = NEXT_EDIT_MODEL_TEMPLATES[modelName]; if (!template) { @@ -70,18 +63,7 @@ export async function renderPrompt( helper: HelperVars, ctx: any, ): Promise { - let modelName = helper.modelName as NextEditModelName; - - if (modelName === "this field is not used") { - return { - prompt: { - role: "user", - content: "NEXT_EDIT", - }, - userEdits: "", - userExcerpts: helper.fileContents, - }; - } + let modelName = helper.modelName as NEXT_EDIT_MODELS; // Validate that the modelName is actually a supported model. if (!Object.keys(NEXT_EDIT_MODEL_TEMPLATES).includes(modelName)) { @@ -91,7 +73,7 @@ export async function renderPrompt( ); if (matchingModel) { - modelName = matchingModel as NextEditModelName; + modelName = matchingModel as NEXT_EDIT_MODELS; } else { throw new Error( `${helper.modelName} is not yet supported for next edit.`, diff --git a/core/nextEdit/templating/NextEditPromptEngine.vitest.ts b/core/nextEdit/templating/NextEditPromptEngine.vitest.ts index 03ffd26e8..785dd79cc 100644 --- a/core/nextEdit/templating/NextEditPromptEngine.vitest.ts +++ b/core/nextEdit/templating/NextEditPromptEngine.vitest.ts @@ -14,11 +14,11 @@ import { MODEL_1_USER_CURSOR_IS_HERE_TOKEN, } from "../constants"; import { - NextEditModelName, renderDefaultSystemPrompt, renderDefaultUserPrompt, renderPrompt, } from "./NextEditPromptEngine"; +import { NEXT_EDIT_MODELS } from "../../llm/constants"; describe("NextEditPromptEngine", () => { describe("renderPrompt", () => { @@ -30,25 +30,19 @@ describe("NextEditPromptEngine", () => { beforeEach(() => { mercuryHelper = { - modelName: "inception/mercury-coder-nextedit" as NextEditModelName, + modelName: "inception/mercury-coder-nextedit" as NEXT_EDIT_MODELS, fileContents: "function test() {\n const a = 1;\n return a;\n}", pos: { line: 1, character: 12 } as Position, lang: { name: "typescript" }, } as HelperVars; model1Helper = { - modelName: "continuedev/model-1" as NextEditModelName, - fileContents: "function test() {\n const a = 1;\n return a;\n}", - pos: { line: 1, character: 12 } as Position, - lang: { name: "typescript" }, - } as HelperVars; - testHelper = { - modelName: "this field is not used" as NextEditModelName, + modelName: "continuedev/model-1" as NEXT_EDIT_MODELS, fileContents: "function test() {\n const a = 1;\n return a;\n}", pos: { line: 1, character: 12 } as Position, lang: { name: "typescript" }, } as HelperVars; unsupportedHelper = { - modelName: "mistral/codestral" as NextEditModelName, + modelName: "mistral/codestral" as NEXT_EDIT_MODELS, fileContents: "function test() {\n const a = 1;\n return a;\n}", pos: { line: 1, character: 12 } as Position, lang: { name: "typescript" }, @@ -99,16 +93,6 @@ describe("NextEditPromptEngine", () => { expect(result.userExcerpts).toContain(MODEL_1_USER_CURSOR_IS_HERE_TOKEN); }); - it("should handle 'this field is not used' model name", async () => { - const result = await renderPrompt(testHelper, ctx); - - expect(result).toHaveProperty("prompt"); - expect(result.prompt.role).toBe("user"); - expect(result.prompt.content).toBe("NEXT_EDIT"); - expect(result.userEdits).toBe(""); - expect(result.userExcerpts).toBe(testHelper.fileContents); - }); - it("should throw error for unsupported model name", async () => { await expect(renderPrompt(unsupportedHelper, ctx)).rejects.toThrow( "mistral/codestral is not yet supported for next edit.", @@ -209,7 +193,7 @@ describe("NextEditPromptEngine", () => { describe("insertTokens", () => { it("should correctly insert cursor and editable region tokens", () => { const mercuryHelper = { - modelName: "inception/mercury-coder-nextedit" as NextEditModelName, + modelName: "inception/mercury-coder-nextedit" as NEXT_EDIT_MODELS, fileContents: "function test() {\n const a = 1;\n return a;\n}", pos: { line: 1, character: 12 } as Position, lang: { name: "typescript" }, diff --git a/core/nextEdit/utils.ts b/core/nextEdit/utils.ts index 152e55933..bf1081ebe 100644 --- a/core/nextEdit/utils.ts +++ b/core/nextEdit/utils.ts @@ -1,15 +1,13 @@ -type NextEditModelName = "mercury-coder-nextedit"; +export function isNextEditTest(): boolean { + const enabled = process.env.NEXT_EDIT_TEST_ENABLED; -export function isModelCapableOfNextEdit(modelName: string): boolean { - // In test mode, we can control whether next edit is enabled via environment variable. - if (process.env.NEXT_EDIT_TEST_ENABLED === "false") { + if (enabled === "false") { return false; } - if (process.env.NEXT_EDIT_TEST_ENABLED === "true") { + if (enabled === "true") { return true; } - const supportedModels: NextEditModelName[] = ["mercury-coder-nextedit"]; - return supportedModels.some((supported) => modelName.includes(supported)); + return false; } diff --git a/extensions/vscode/src/extension/VsCodeExtension.ts b/extensions/vscode/src/extension/VsCodeExtension.ts index 6c32adedc..3861edcb7 100644 --- a/extensions/vscode/src/extension/VsCodeExtension.ts +++ b/extensions/vscode/src/extension/VsCodeExtension.ts @@ -43,9 +43,10 @@ import { ConfigYamlDocumentLinkProvider } from "./ConfigYamlDocumentLinkProvider import { VsCodeMessenger } from "./VsCodeMessenger"; import { getAst } from "core/autocomplete/util/ast"; +import { modelSupportsNextEdit } from "core/llm/autodetect"; import { DocumentHistoryTracker } from "core/nextEdit/DocumentHistoryTracker"; import { NextEditProvider } from "core/nextEdit/NextEditProvider"; -import { isModelCapableOfNextEdit } from "core/nextEdit/utils"; +import { isNextEditTest } from "core/nextEdit/utils"; import { localPathOrUriToPath } from "core/util/pathToUri"; import { JumpManager } from "../activation/JumpManager"; import setupNextEditWindowManager, { @@ -202,8 +203,13 @@ export class VsCodeExtension { async ({ config: newConfig, configLoadInterrupted }) => { const autocompleteModel = newConfig?.selectedModelByRole.autocomplete; if ( - autocompleteModel && - isModelCapableOfNextEdit(autocompleteModel.model) + (autocompleteModel && + modelSupportsNextEdit( + autocompleteModel.capabilities, + autocompleteModel.model, + autocompleteModel.title, + )) || + isNextEditTest() ) { // Set up next edit window manager only for Continue team members await setupNextEditWindowManager(context); diff --git a/packages/config-yaml/src/schemas/models.ts b/packages/config-yaml/src/schemas/models.ts index 283ec6781..0693675a2 100644 --- a/packages/config-yaml/src/schemas/models.ts +++ b/packages/config-yaml/src/schemas/models.ts @@ -36,6 +36,7 @@ export type ModelRole = z.infer; export const modelCapabilitySchema = z.union([ z.literal("tool_use"), z.literal("image_input"), + z.literal("next_edit"), ]); export type ModelCapability = z.infer;