From 158faa1e5e4765e9953d97729fd8efa93cdd7c32 Mon Sep 17 00:00:00 2001 From: Test Date: Thu, 19 Sep 2024 15:49:25 -0700 Subject: [PATCH] create `getModelByRole()` util fn --- core/config/util.ts | 25 ++++++++++++- core/context/retrieval/repoMapRequest.ts | 7 ++-- docs/static/schemas/config.json | 15 ++++---- .../src/main/resources/config_schema.json | 15 ++++---- extensions/vscode/continue_rc_schema.json | 15 ++++---- extensions/vscode/src/commands.ts | 12 ++++--- .../vscode/src/extension/VsCodeMessenger.ts | 35 ++++++++++--------- .../src/quickEdit/QuickEditQuickPick.ts | 24 +++++-------- 8 files changed, 77 insertions(+), 71 deletions(-) diff --git a/core/config/util.ts b/core/config/util.ts index 5abf0b9b5..69e088a18 100644 --- a/core/config/util.ts +++ b/core/config/util.ts @@ -1,4 +1,10 @@ -import { ContextProviderWithParams, ModelDescription } from "../"; +import { + ContextProviderWithParams, + ContinueConfig, + ILLM, + ModelDescription, + ModelRoles, +} from "../"; import { editConfigJson } from "../util/paths"; function stringify(obj: any, indentation?: number): string { @@ -61,3 +67,20 @@ export function deleteModel(title: string) { return config; }); } + +export function getModelByRole( + config: ContinueConfig, + role: T, +): ILLM | undefined { + const roleTitle = config.experimental?.modelRoles?.[role]; + + if (!roleTitle) { + return undefined; + } + + const matchingModel = config.models.find( + (model) => model.title === roleTitle, + ); + + return matchingModel; +} diff --git a/core/context/retrieval/repoMapRequest.ts b/core/context/retrieval/repoMapRequest.ts index 8d2eb9e4f..1f07d61d0 100644 --- a/core/context/retrieval/repoMapRequest.ts +++ b/core/context/retrieval/repoMapRequest.ts @@ -1,4 +1,5 @@ import { Chunk, ContinueConfig, IDE, ILLM } from "../.."; +import { getModelByRole } from "../../config/util"; import { stripImages } from "../../llm/images"; import generateRepoMap from "../../util/repoMap"; @@ -9,11 +10,7 @@ export async function requestFilesFromRepoMap( input: string, filterDirectory?: string, ): Promise { - const llm = - config.models.find( - (model) => - model.title === config.experimental?.modelRoles?.repoMapFileSelection, - ) ?? defaultLlm; + const llm = getModelByRole(config, "repoMapFileSelection") ?? defaultLlm; // Only supported for Claude models right now if (!llm.model.toLowerCase().includes("claude")) { diff --git a/docs/static/schemas/config.json b/docs/static/schemas/config.json index cbf47d4b4..911278ebb 100644 --- a/docs/static/schemas/config.json +++ b/docs/static/schemas/config.json @@ -2476,13 +2476,16 @@ "type": "object", "properties": { "inlineEdit": { - "$ref": "#/definitions/ModelTitle" + "description": "The 'title' property of the model you wish to use for inline edits", + "type": "string" }, "applyCodeBlock": { - "$ref": "#/definitions/ModelTitle" + "description": "The 'title' property of the model you wish to use for applying code blocks", + "type": "string" }, "repoMapFileSelection": { - "$ref": "#/definitions/ModelTitle" + "description": "The 'title' property of the model you wish to use for repo map file selections", + "type": "string" } } }, @@ -2545,12 +2548,6 @@ } } } - }, - "ModelTitle": { - "type": "string", - "enum": { - "$data": "/models/title" - } } } } diff --git a/extensions/intellij/src/main/resources/config_schema.json b/extensions/intellij/src/main/resources/config_schema.json index cbf47d4b4..911278ebb 100644 --- a/extensions/intellij/src/main/resources/config_schema.json +++ b/extensions/intellij/src/main/resources/config_schema.json @@ -2476,13 +2476,16 @@ "type": "object", "properties": { "inlineEdit": { - "$ref": "#/definitions/ModelTitle" + "description": "The 'title' property of the model you wish to use for inline edits", + "type": "string" }, "applyCodeBlock": { - "$ref": "#/definitions/ModelTitle" + "description": "The 'title' property of the model you wish to use for applying code blocks", + "type": "string" }, "repoMapFileSelection": { - "$ref": "#/definitions/ModelTitle" + "description": "The 'title' property of the model you wish to use for repo map file selections", + "type": "string" } } }, @@ -2545,12 +2548,6 @@ } } } - }, - "ModelTitle": { - "type": "string", - "enum": { - "$data": "/models/title" - } } } } diff --git a/extensions/vscode/continue_rc_schema.json b/extensions/vscode/continue_rc_schema.json index 58453fc5c..101296fe6 100644 --- a/extensions/vscode/continue_rc_schema.json +++ b/extensions/vscode/continue_rc_schema.json @@ -2782,13 +2782,16 @@ "type": "object", "properties": { "inlineEdit": { - "$ref": "#/definitions/ModelTitle" + "description": "The 'title' property of the model you wish to use for inline edits", + "type": "string" }, "applyCodeBlock": { - "$ref": "#/definitions/ModelTitle" + "description": "The 'title' property of the model you wish to use for applying code blocks", + "type": "string" }, "repoMapFileSelection": { - "$ref": "#/definitions/ModelTitle" + "description": "The 'title' property of the model you wish to use for repo map file selections", + "type": "string" } } }, @@ -2864,12 +2867,6 @@ "markdownDescription": "If set to 'merge', .continuerc.json will be applied on top of config.json (arrays and objects are merged). If set to 'overwrite', then every top-level property of .continuerc.json will overwrite that property from config.json." } } - }, - "ModelTitle": { - "type": "string", - "enum": { - "$data": "/models/title" - } } } } \ No newline at end of file diff --git a/extensions/vscode/src/commands.ts b/extensions/vscode/src/commands.ts index abc33fe8b..7ac639326 100644 --- a/extensions/vscode/src/commands.ts +++ b/extensions/vscode/src/commands.ts @@ -30,6 +30,7 @@ import { Battery } from "./util/battery"; import { uriFromFilePath } from "./util/vscode"; import type { VsCodeWebviewProtocol } from "./webviewProtocol"; import { getFullyQualifiedPath } from "./util/util"; +import { getModelByRole } from "core/config/util"; let fullScreenPanel: vscode.WebviewPanel | undefined; @@ -208,12 +209,13 @@ const commandsMap: ( ) { const config = await configHandler.loadConfig(); + const defaultModelTitle = await sidebar.webviewProtocol.request( + "getDefaultModelTitle", + undefined, + ); + const modelTitle = - config.experimental?.modelRoles?.inlineEdit ?? - (await sidebar.webviewProtocol.request( - "getDefaultModelTitle", - undefined, - )); + getModelByRole(config, "inlineEdit")?.title ?? defaultModelTitle; sidebar.webviewProtocol.request("incrementFtc", undefined); diff --git a/extensions/vscode/src/extension/VsCodeMessenger.ts b/extensions/vscode/src/extension/VsCodeMessenger.ts index 792d504d3..786f5377a 100644 --- a/extensions/vscode/src/extension/VsCodeMessenger.ts +++ b/extensions/vscode/src/extension/VsCodeMessenger.ts @@ -27,6 +27,7 @@ import { import { getExtensionUri } from "../util/vscode"; import { VsCodeIde } from "../VsCodeIde"; import { VsCodeWebviewProtocol } from "../webviewProtocol"; +import { getModelByRole } from "core/config/util"; /** * A shared messenger class between Core and Webview @@ -145,26 +146,26 @@ export class VsCodeMessenger { // Get LLM from config const configHandler = await configHandlerPromise; const config = await configHandler.loadConfig(); - const modelTitle = - config.experimental?.modelRoles?.applyCodeBlock ?? - (await this.webviewProtocol.request("getDefaultModelTitle", undefined)); - const llm = config.models.find((model) => model.title === modelTitle); + + let llm = getModelByRole(config, "applyCodeBlock"); + if (!llm) { - vscode.window.showErrorMessage( - `Model ${modelTitle} not found in config.`, + const defaultModelTitle = await this.webviewProtocol.request( + "getDefaultModelTitle", + undefined, ); - return; + + llm = config.models.find((model) => model.title === defaultModelTitle); + + if (!llm) { + vscode.window.showErrorMessage( + `Model ${defaultModelTitle} not found in config.`, + ); + return; + } } - let fastLlm: ILLM | undefined; - if (config.experimental?.modelRoles?.repoMapFileSelection) { - fastLlm = config.models.find( - (model) => - model.title === - config.experimental?.modelRoles?.repoMapFileSelection, - ); - } - fastLlm ??= llm; + const fastLlm = getModelByRole(config, "repoMapFileSelection") ?? llm; // Generate the diff and pass through diff manager const [instant, diffLines] = await applyCodeBlock( @@ -179,7 +180,7 @@ export class VsCodeMessenger { verticalDiffManager.streamDiffLines(diffLines, instant); } else { const prompt = `The following code was suggested as an edit:\n\`\`\`\n${msg.data.text}\n\`\`\`\nPlease apply it to the previous code.`; - verticalDiffManager.streamEdit(prompt, modelTitle); + verticalDiffManager.streamEdit(prompt, llm.title); } }); diff --git a/extensions/vscode/src/quickEdit/QuickEditQuickPick.ts b/extensions/vscode/src/quickEdit/QuickEditQuickPick.ts index 6fc3404c6..b501c0250 100644 --- a/extensions/vscode/src/quickEdit/QuickEditQuickPick.ts +++ b/extensions/vscode/src/quickEdit/QuickEditQuickPick.ts @@ -13,6 +13,7 @@ import { getModelQuickPickVal } from "./ModelSelectionQuickPick"; import { ConfigHandler } from "core/config/ConfigHandler"; // @ts-ignore import MiniSearch from "minisearch"; +import { getModelByRole } from "core/config/util"; /** * Used to track what action to take after a user interacts @@ -222,30 +223,21 @@ export class QuickEdit { * Gets the model title the user has chosen, or their default model */ private async getCurModelTitle() { - const config = await this.configHandler.loadConfig(); - if (this._curModelTitle) { return this._curModelTitle; } - const inlineEditModel = config.experimental?.modelRoles?.inlineEdit; + const config = await this.configHandler.loadConfig(); - if (inlineEditModel) { - return inlineEditModel; - } - - let defaultModelTitle: string | undefined = - await this.webviewProtocol.request( + return ( + getModelByRole(config, "inlineEdit")?.title ?? + (await this.webviewProtocol.request( "getDefaultModelTitle", undefined, false, - ); - - if (!defaultModelTitle) { - defaultModelTitle = config.models[0].title; - } - - return defaultModelTitle || inlineEditModel; + )) ?? + config.models[0]?.title + ); } /**