Elffuss BraidHub

kikocis/continue public

Merge pull request #7563 from continuedev/request-options-cli

refactor: shared function for constructing llm api
Changesetbf136946f6caaf7c7b7f0b04a1acf68b700dfb9c2b5af701fa930f875f243184
AuthorNate Sesti <33237525+sestinj@users.noreply.github.com> · 2025-09-11 15:11 -07:00
CommitterGitHub <noreply@github.com> · 2025-09-11 15:11 -07:00
Parents562def2ea40d, 97a51e0ee813

Browse the files at this changeset

6 files changed

modified extensions/cli/package-lock.json +0 −0

Not shown: the file is larger than the hub shows, or this page holds as much as it shows. View the file.

modified extensions/cli/package.json +1 −0
@@ -118,6 +118,7 @@
118118 "open": "^10.1.0",
119119 "posthog-node": "^5.6.0",
120120 "prettier": "^3.6.2",
121+ "prettier-plugin-tailwindcss": "^0.6.14",
121122 "react": "^19.1.0",
122123 "readline-sync": "^1.4.10",
123124 "semantic-release": "^24.2.7",
modified extensions/cli/src/__mocks__/config.ts +1 −2
@@ -13,5 +13,4 @@
1313 ) => [any, any]
1414 >();
1515 export const getModel = vi.fn<(config: any, modelName: string) => any>();
16-export const createLlmApi =
17- vi.fn<(modelConfig: any, authConfig: any, orgScopeId?: string) => any>();
16+export const createLlmApi = vi.fn<(model: any, authConfig: any) => any>();
modified extensions/cli/src/config.ts +34 −22
@@ -19,25 +19,14 @@
1919 import { env } from "./env.js";
2020 import { MCPService } from "./services/MCPService.js";
2121
22-export function getLlmApi(
23- assistant: AssistantUnrolled,
24- authConfig: AuthConfig,
25-): [BaseLlmApi, ModelConfig] {
26- if (!assistant.models || assistant.models.length === 0) {
27- throw new Error("No models found in the configured assistant");
28- }
29-
30- const model = assistant.models?.find(
31- (model) =>
32- model?.roles?.includes("chat") || (model && model.roles === undefined),
33- );
34-
35- if (!model) {
36- throw new Error(
37- "No models with the chat role found in the configured assistant",
38- );
39- }
40-
22+/**
23+ * Creates an LLM API instance from a ModelConfig and auth configuration
24+ * Handles special logic for continue-proxy provider and constructs the API
25+ */
26+export function createLlmApi(
27+ model: ModelConfig,
28+ authConfig: AuthConfig | null,
29+): BaseLlmApi | null {
4130 const accessToken = getAccessToken(authConfig);
4231 const organizationId = getOrganizationId(authConfig);
4332
@@ -50,9 +39,10 @@
5039 apiKey: accessToken ?? undefined,
5140 env: {
5241 apiKeyLocation: (model as any).apiKeyLocation,
53- // envSecretLocations: model.env,
5442 orgScopeId: organizationId,
55- proxyUrl: undefined, // TODO
43+ proxyUrl:
44+ (model as { onPremProxyUrl: string | undefined })
45+ .onPremProxyUrl ?? undefined,
5646 },
5747 }
5848 : {
@@ -63,7 +53,29 @@
6353 env: model.env,
6454 };
6555
66- const llmApi = constructLlmApi(config);
56+ return constructLlmApi(config) ?? null;
57+}
58+
59+export function getLlmApi(
60+ assistant: AssistantUnrolled,
61+ authConfig: AuthConfig,
62+): [BaseLlmApi, ModelConfig] {
63+ if (!assistant.models || assistant.models.length === 0) {
64+ throw new Error("No models found in the configured assistant");
65+ }
66+
67+ const model = assistant.models?.find(
68+ (model) =>
69+ model?.roles?.includes("chat") || (model && model.roles === undefined),
70+ );
71+
72+ if (!model) {
73+ throw new Error(
74+ "No models with the chat role found in the configured assistant",
75+ );
76+ }
77+
78+ const llmApi = createLlmApi(model, authConfig);
6779
6880 if (!llmApi) {
6981 throw new Error(
modified extensions/cli/src/services/ModelService.test.ts +10 −30
@@ -1,9 +1,7 @@
11 import { AssistantUnrolled, ModelConfig } from "@continuedev/config-yaml";
2-import * as openaiAdapters from "@continuedev/openai-adapters";
32 import { describe, expect, beforeEach, vi, test } from "vitest";
43
54 // Mock dependencies before imports
6-vi.mock("@continuedev/openai-adapters");
75 vi.mock("../config.js");
86 vi.mock("../auth/workos.js");
97
@@ -81,9 +79,7 @@
8179
8280 test("should initialize with persisted model if valid", async () => {
8381 vi.mocked(workos.getModelName).mockReturnValue("Claude 3");
84- vi.mocked(openaiAdapters.constructLlmApi).mockReturnValue(
85- mockLlmApi as any,
86- );
82+ vi.mocked(config.createLlmApi).mockReturnValue(mockLlmApi as any);
8783
8884 const state = await service.initialize(mockAssistant, mockAuthConfig);
8985
@@ -152,9 +148,7 @@
152148 await service.initialize(mockAssistant, mockAuthConfig);
153149
154150 const newLlmApi = { complete: vi.fn(), stream: vi.fn() };
155- vi.mocked(openaiAdapters.constructLlmApi).mockReturnValue(
156- newLlmApi as any,
157- );
151+ vi.mocked(config.createLlmApi).mockReturnValue(newLlmApi as any);
158152
159153 const state = await service.switchModel(1);
160154
@@ -204,24 +198,14 @@
204198 ]);
205199 await service.initialize(mockAssistant, mockAuthConfig);
206200
207- vi.mocked(workos.getAccessToken).mockReturnValue("proxy-token");
208- vi.mocked(workos.getOrganizationId).mockReturnValue("org-123");
209- vi.mocked(openaiAdapters.constructLlmApi).mockReturnValue(
210- mockLlmApi as any,
211- );
201+ vi.mocked(config.createLlmApi).mockReturnValue(mockLlmApi as any);
212202
213203 await service.switchModel(0);
214204
215- expect(vi.mocked(openaiAdapters.constructLlmApi)).toHaveBeenCalledWith({
216- provider: "continue-proxy",
217- apiBase: "https://proxy.continue.dev",
218- apiKey: "proxy-token",
219- env: {
220- apiKeyLocation: "env.PROXY_KEY",
221- orgScopeId: "org-123",
222- proxyUrl: undefined,
223- },
224- });
205+ expect(vi.mocked(config.createLlmApi)).toHaveBeenCalledWith(
206+ proxyModel,
207+ mockAuthConfig,
208+ );
225209 });
226210 });
227211
@@ -292,9 +276,7 @@
292276
293277 expect(service.getCurrentModelIndex()).toBe(0);
294278
295- vi.mocked(openaiAdapters.constructLlmApi).mockReturnValue(
296- mockLlmApi as any,
297- );
279+ vi.mocked(config.createLlmApi).mockReturnValue(mockLlmApi as any);
298280 await service.switchModel(1);
299281 expect(service.getCurrentModelIndex()).toBe(1);
300282 });
@@ -392,9 +374,7 @@
392374 service.on("stateChanged", listener);
393375
394376 const newLlmApi = { complete: vi.fn(), stream: vi.fn() };
395- vi.mocked(openaiAdapters.constructLlmApi).mockReturnValue(
396- newLlmApi as any,
397- );
377+ vi.mocked(config.createLlmApi).mockReturnValue(newLlmApi as any);
398378 await service.switchModel(1);
399379
400380 expect(listener).toHaveBeenCalledWith(
@@ -419,7 +399,7 @@
419399 const errorListener = vi.fn();
420400 service.on("error", errorListener);
421401
422- vi.mocked(openaiAdapters.constructLlmApi).mockReturnValue(null as any);
402+ vi.mocked(config.createLlmApi).mockReturnValue(null as any);
423403
424404 await expect(service.switchModel(1)).rejects.toThrow(
425405 "Failed to initialize LLM",
modified extensions/cli/src/services/ModelService.ts +5 −59
@@ -1,13 +1,7 @@
11 import { AssistantUnrolled, ModelConfig } from "@continuedev/config-yaml";
2-import { constructLlmApi, LLMConfig } from "@continuedev/openai-adapters";
3-
4-import {
5- AuthConfig,
6- getAccessToken,
7- getModelName,
8- getOrganizationId,
9-} from "../auth/workos.js";
10-import { getLlmApi } from "../config.js";
2+
3+import { AuthConfig, getModelName } from "../auth/workos.js";
4+import { createLlmApi, getLlmApi } from "../config.js";
115 import { logger } from "../util/logger.js";
126
137 import { BaseService, ServiceWithDependencies } from "./BaseService.js";
@@ -89,31 +83,7 @@
8983 name: (selectedModel as any).name || "unnamed",
9084 });
9185
92- const accessToken = getAccessToken(authConfig);
93- const organizationId = getOrganizationId(authConfig);
94-
95- const config: LLMConfig =
96- selectedModel.provider === "continue-proxy"
97- ? {
98- provider: selectedModel.provider,
99- requestOptions: selectedModel.requestOptions,
100- apiBase: selectedModel.apiBase,
101- apiKey: accessToken ?? undefined,
102- env: {
103- apiKeyLocation: (selectedModel as any).apiKeyLocation,
104- orgScopeId: organizationId,
105- proxyUrl: (selectedModel as any).onPremProxyUrl ?? undefined,
106- },
107- }
108- : {
109- provider: selectedModel.provider as any,
110- apiKey: selectedModel.apiKey,
111- apiBase: selectedModel.apiBase,
112- requestOptions: selectedModel.requestOptions,
113- env: selectedModel.env,
114- };
115-
116- const llmApi = constructLlmApi(config);
86+ const llmApi = createLlmApi(selectedModel, authConfig);
11787
11888 if (!llmApi) {
11989 throw new Error("Failed to initialize LLM with persisted model");
@@ -288,31 +258,7 @@
288258 });
289259
290260 try {
291- const accessToken = getAccessToken(authConfig);
292- const organizationId = getOrganizationId(authConfig);
293-
294- const config: LLMConfig =
295- selectedModel.provider === "continue-proxy"
296- ? {
297- provider: selectedModel.provider,
298- requestOptions: selectedModel.requestOptions,
299- apiBase: selectedModel.apiBase,
300- apiKey: accessToken ?? undefined,
301- env: {
302- apiKeyLocation: (selectedModel as any).apiKeyLocation,
303- orgScopeId: organizationId,
304- proxyUrl: (selectedModel as any).onPremProxyUrl ?? undefined,
305- },
306- }
307- : {
308- provider: selectedModel.provider as any,
309- apiKey: selectedModel.apiKey,
310- apiBase: selectedModel.apiBase,
311- requestOptions: selectedModel.requestOptions,
312- env: selectedModel.env,
313- };
314-
315- const llmApi = constructLlmApi(config);
261+ const llmApi = createLlmApi(selectedModel, authConfig);
316262
317263 if (!llmApi) {
318264 throw new Error("Failed to initialize LLM with selected model");