create getModelByRole() util fn

This commit is contained in:
Test 2024-09-19 15:49:25 -07:00
parent 67d64d7a29
commit 158faa1e5e
8 changed files with 77 additions and 71 deletions

View file

@ -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<T extends keyof ModelRoles>(
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;
}

View file

@ -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<Chunk[]> {
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")) {

View file

@ -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"
}
}
}
}

View file

@ -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"
}
}
}
}

View file

@ -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"
}
}
}
}

View file

@ -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);

View file

@ -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);
}
});

View file

@ -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
);
}
/**