Skip to content

Commit 02c1d7e

Browse files
authored
Add ability to use a dev endpoint for auto-model (#3038)
1 parent 2988ace commit 02c1d7e

File tree

3 files changed

+51
-8
lines changed

3 files changed

+51
-8
lines changed

extensions/ql-vscode/src/config.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,10 @@ const LLM_GENERATION_BATCH_SIZE = new Setting(
706706
"llmGenerationBatchSize",
707707
MODEL_SETTING,
708708
);
709+
const LLM_GENERATION_DEV_ENDPOINT = new Setting(
710+
"llmGenerationDevEndpoint",
711+
MODEL_SETTING,
712+
);
709713
const EXTENSIONS_DIRECTORY = new Setting("extensionsDirectory", MODEL_SETTING);
710714
const ENABLE_RUBY = new Setting("enableRuby", MODEL_SETTING);
711715

@@ -738,6 +742,14 @@ export class ModelConfigListener extends ConfigListener implements ModelConfig {
738742
return LLM_GENERATION_BATCH_SIZE.getValue<number | null>() || 5;
739743
}
740744

745+
/**
746+
* The URL of the endpoint to use for LLM generation. This should only be set
747+
* if you want to test against a dev server.
748+
*/
749+
public get llmGenerationDevEndpoint(): string | undefined {
750+
return LLM_GENERATION_DEV_ENDPOINT.getValue<string | undefined>();
751+
}
752+
741753
public getExtensionsDirectory(languageId: string): string | undefined {
742754
return EXTENSIONS_DIRECTORY.getValue<string>({
743755
languageId,

extensions/ql-vscode/src/model-editor/auto-model-api.ts

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { Credentials } from "../common/authentication";
22
import { OctokitResponse } from "@octokit/types";
3+
import fetch from "node-fetch";
4+
import { ModelConfigListener } from "../config";
35

46
export enum AutomodelMode {
57
Unspecified = "AUTOMODEL_MODE_UNSPECIFIED",
@@ -20,15 +22,44 @@ export interface ModelResponse {
2022
export async function autoModel(
2123
credentials: Credentials,
2224
request: ModelRequest,
25+
modelingConfig: ModelConfigListener,
2326
): Promise<ModelResponse> {
24-
const octokit = await credentials.getOctokit();
27+
const devEndpoint = modelingConfig.llmGenerationDevEndpoint;
28+
if (devEndpoint) {
29+
return callAutoModelDevEndpoint(devEndpoint, request);
30+
} else {
31+
const octokit = await credentials.getOctokit();
2532

26-
const response: OctokitResponse<ModelResponse> = await octokit.request(
27-
"POST /repos/github/codeql/code-scanning/codeql/auto-model",
28-
{
29-
data: request,
33+
const response: OctokitResponse<ModelResponse> = await octokit.request(
34+
"POST /repos/github/codeql/code-scanning/codeql/auto-model",
35+
{
36+
data: request,
37+
},
38+
);
39+
40+
return response.data;
41+
}
42+
}
43+
44+
async function callAutoModelDevEndpoint(
45+
endpoint: string,
46+
request: ModelRequest,
47+
): Promise<ModelResponse> {
48+
const json = JSON.stringify(request);
49+
const response = await fetch(endpoint, {
50+
method: "POST",
51+
headers: {
52+
"Content-Type": "application/json",
3053
},
31-
);
54+
body: json,
55+
});
56+
57+
if (!response.ok) {
58+
throw new Error(
59+
`Error calling auto-model API: ${response.status} ${response.statusText}`,
60+
);
61+
}
3262

33-
return response.data;
63+
const data = await response.json();
64+
return data as ModelResponse;
3465
}

extensions/ql-vscode/src/model-editor/auto-modeler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ export class AutoModeler {
238238
request: ModelRequest,
239239
): Promise<ModelResponse | null> {
240240
try {
241-
return await autoModel(this.app.credentials, request);
241+
return await autoModel(this.app.credentials, request, this.modelConfig);
242242
} catch (e) {
243243
if (e instanceof RequestError && e.status === 429) {
244244
void showAndLogExceptionWithTelemetry(

0 commit comments

Comments
 (0)