Merge pull request #7029 from continuedev/jacob/con-3283
feat: set capabilities for next edit
This commit is contained in:
commit
e1d39bb943
11 changed files with 207 additions and 56 deletions
|
|
@ -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,
|
||||
};
|
||||
|
|
|
|||
1
core/index.d.ts
vendored
1
core/index.d.ts
vendored
|
|
@ -1141,6 +1141,7 @@ export interface BaseCompletionOptions {
|
|||
export interface ModelCapability {
|
||||
uploadImage?: boolean;
|
||||
tools?: boolean;
|
||||
nextEdit?: boolean;
|
||||
}
|
||||
|
||||
export interface ModelDescription {
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<NextEditModelName, NextEditTemplate> = {
|
||||
const NEXT_EDIT_MODEL_TEMPLATES: Record<NEXT_EDIT_MODELS, NextEditTemplate> = {
|
||||
"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<NextEditModelName, NextEditTemplate> = {
|
|||
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<PromptMetadata> {
|
||||
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.`,
|
||||
|
|
|
|||
|
|
@ -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" },
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -36,6 +36,7 @@ export type ModelRole = z.infer<typeof modelRolesSchema>;
|
|||
export const modelCapabilitySchema = z.union([
|
||||
z.literal("tool_use"),
|
||||
z.literal("image_input"),
|
||||
z.literal("next_edit"),
|
||||
]);
|
||||
export type ModelCapability = z.infer<typeof modelCapabilitySchema>;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue