Merge pull request #7563 from continuedev/request-options-cli
refactor: shared function for constructing llm api
This commit is contained in:
commit
01f4a0a66d
6 changed files with 142 additions and 116 deletions
88
extensions/cli/package-lock.json
generated
88
extensions/cli/package-lock.json
generated
|
|
@ -86,6 +86,7 @@
|
|||
"open": "^10.1.0",
|
||||
"posthog-node": "^5.6.0",
|
||||
"prettier": "^3.6.2",
|
||||
"prettier-plugin-tailwindcss": "^0.6.14",
|
||||
"react": "^19.1.0",
|
||||
"readline-sync": "^1.4.10",
|
||||
"semantic-release": "^24.2.7",
|
||||
|
|
@ -15972,6 +15973,93 @@
|
|||
"url": "https://github.com/prettier/prettier?sponsor=1"
|
||||
}
|
||||
},
|
||||
"node_modules/prettier-plugin-tailwindcss": {
|
||||
"version": "0.6.14",
|
||||
"resolved": "https://registry.npmjs.org/prettier-plugin-tailwindcss/-/prettier-plugin-tailwindcss-0.6.14.tgz",
|
||||
"integrity": "sha512-pi2e/+ZygeIqntN+vC573BcW5Cve8zUB0SSAGxqpB4f96boZF4M3phPVoOFCeypwkpRYdi7+jQ5YJJUwrkGUAg==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=14.21.3"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@ianvs/prettier-plugin-sort-imports": "*",
|
||||
"@prettier/plugin-hermes": "*",
|
||||
"@prettier/plugin-oxc": "*",
|
||||
"@prettier/plugin-pug": "*",
|
||||
"@shopify/prettier-plugin-liquid": "*",
|
||||
"@trivago/prettier-plugin-sort-imports": "*",
|
||||
"@zackad/prettier-plugin-twig": "*",
|
||||
"prettier": "^3.0",
|
||||
"prettier-plugin-astro": "*",
|
||||
"prettier-plugin-css-order": "*",
|
||||
"prettier-plugin-import-sort": "*",
|
||||
"prettier-plugin-jsdoc": "*",
|
||||
"prettier-plugin-marko": "*",
|
||||
"prettier-plugin-multiline-arrays": "*",
|
||||
"prettier-plugin-organize-attributes": "*",
|
||||
"prettier-plugin-organize-imports": "*",
|
||||
"prettier-plugin-sort-imports": "*",
|
||||
"prettier-plugin-style-order": "*",
|
||||
"prettier-plugin-svelte": "*"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@ianvs/prettier-plugin-sort-imports": {
|
||||
"optional": true
|
||||
},
|
||||
"@prettier/plugin-hermes": {
|
||||
"optional": true
|
||||
},
|
||||
"@prettier/plugin-oxc": {
|
||||
"optional": true
|
||||
},
|
||||
"@prettier/plugin-pug": {
|
||||
"optional": true
|
||||
},
|
||||
"@shopify/prettier-plugin-liquid": {
|
||||
"optional": true
|
||||
},
|
||||
"@trivago/prettier-plugin-sort-imports": {
|
||||
"optional": true
|
||||
},
|
||||
"@zackad/prettier-plugin-twig": {
|
||||
"optional": true
|
||||
},
|
||||
"prettier-plugin-astro": {
|
||||
"optional": true
|
||||
},
|
||||
"prettier-plugin-css-order": {
|
||||
"optional": true
|
||||
},
|
||||
"prettier-plugin-import-sort": {
|
||||
"optional": true
|
||||
},
|
||||
"prettier-plugin-jsdoc": {
|
||||
"optional": true
|
||||
},
|
||||
"prettier-plugin-marko": {
|
||||
"optional": true
|
||||
},
|
||||
"prettier-plugin-multiline-arrays": {
|
||||
"optional": true
|
||||
},
|
||||
"prettier-plugin-organize-attributes": {
|
||||
"optional": true
|
||||
},
|
||||
"prettier-plugin-organize-imports": {
|
||||
"optional": true
|
||||
},
|
||||
"prettier-plugin-sort-imports": {
|
||||
"optional": true
|
||||
},
|
||||
"prettier-plugin-style-order": {
|
||||
"optional": true
|
||||
},
|
||||
"prettier-plugin-svelte": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/pretty-format": {
|
||||
"version": "27.5.1",
|
||||
"resolved": "https://registry.npmjs.org/pretty-format/-/pretty-format-27.5.1.tgz",
|
||||
|
|
|
|||
|
|
@ -118,6 +118,7 @@
|
|||
"open": "^10.1.0",
|
||||
"posthog-node": "^5.6.0",
|
||||
"prettier": "^3.6.2",
|
||||
"prettier-plugin-tailwindcss": "^0.6.14",
|
||||
"react": "^19.1.0",
|
||||
"readline-sync": "^1.4.10",
|
||||
"semantic-release": "^24.2.7",
|
||||
|
|
|
|||
|
|
@ -13,5 +13,4 @@ export const getLlmApi =
|
|||
) => [any, any]
|
||||
>();
|
||||
export const getModel = vi.fn<(config: any, modelName: string) => any>();
|
||||
export const createLlmApi =
|
||||
vi.fn<(modelConfig: any, authConfig: any, orgScopeId?: string) => any>();
|
||||
export const createLlmApi = vi.fn<(model: any, authConfig: any) => any>();
|
||||
|
|
|
|||
|
|
@ -19,6 +19,43 @@ import { loadConfiguration } from "./configLoader.js";
|
|||
import { env } from "./env.js";
|
||||
import { MCPService } from "./services/MCPService.js";
|
||||
|
||||
/**
|
||||
* Creates an LLM API instance from a ModelConfig and auth configuration
|
||||
* Handles special logic for continue-proxy provider and constructs the API
|
||||
*/
|
||||
export function createLlmApi(
|
||||
model: ModelConfig,
|
||||
authConfig: AuthConfig | null,
|
||||
): BaseLlmApi | null {
|
||||
const accessToken = getAccessToken(authConfig);
|
||||
const organizationId = getOrganizationId(authConfig);
|
||||
|
||||
const config: LLMConfig =
|
||||
model.provider === "continue-proxy"
|
||||
? {
|
||||
provider: model.provider,
|
||||
requestOptions: model.requestOptions,
|
||||
apiBase: model.apiBase,
|
||||
apiKey: accessToken ?? undefined,
|
||||
env: {
|
||||
apiKeyLocation: (model as any).apiKeyLocation,
|
||||
orgScopeId: organizationId,
|
||||
proxyUrl:
|
||||
(model as { onPremProxyUrl: string | undefined })
|
||||
.onPremProxyUrl ?? undefined,
|
||||
},
|
||||
}
|
||||
: {
|
||||
provider: model.provider as any,
|
||||
apiKey: model.apiKey,
|
||||
apiBase: model.apiBase,
|
||||
requestOptions: model.requestOptions,
|
||||
env: model.env,
|
||||
};
|
||||
|
||||
return constructLlmApi(config) ?? null;
|
||||
}
|
||||
|
||||
export function getLlmApi(
|
||||
assistant: AssistantUnrolled,
|
||||
authConfig: AuthConfig,
|
||||
|
|
@ -38,32 +75,7 @@ export function getLlmApi(
|
|||
);
|
||||
}
|
||||
|
||||
const accessToken = getAccessToken(authConfig);
|
||||
const organizationId = getOrganizationId(authConfig);
|
||||
|
||||
const config: LLMConfig =
|
||||
model.provider === "continue-proxy"
|
||||
? {
|
||||
provider: model.provider,
|
||||
requestOptions: model.requestOptions,
|
||||
apiBase: model.apiBase,
|
||||
apiKey: accessToken ?? undefined,
|
||||
env: {
|
||||
apiKeyLocation: (model as any).apiKeyLocation,
|
||||
// envSecretLocations: model.env,
|
||||
orgScopeId: organizationId,
|
||||
proxyUrl: undefined, // TODO
|
||||
},
|
||||
}
|
||||
: {
|
||||
provider: model.provider as any,
|
||||
apiKey: model.apiKey,
|
||||
apiBase: model.apiBase,
|
||||
requestOptions: model.requestOptions,
|
||||
env: model.env,
|
||||
};
|
||||
|
||||
const llmApi = constructLlmApi(config);
|
||||
const llmApi = createLlmApi(model, authConfig);
|
||||
|
||||
if (!llmApi) {
|
||||
throw new Error(
|
||||
|
|
|
|||
|
|
@ -1,9 +1,7 @@
|
|||
import { AssistantUnrolled, ModelConfig } from "@continuedev/config-yaml";
|
||||
import * as openaiAdapters from "@continuedev/openai-adapters";
|
||||
import { describe, expect, beforeEach, vi, test } from "vitest";
|
||||
|
||||
// Mock dependencies before imports
|
||||
vi.mock("@continuedev/openai-adapters");
|
||||
vi.mock("../config.js");
|
||||
vi.mock("../auth/workos.js");
|
||||
|
||||
|
|
@ -81,9 +79,7 @@ describe("ModelService", () => {
|
|||
|
||||
test("should initialize with persisted model if valid", async () => {
|
||||
vi.mocked(workos.getModelName).mockReturnValue("Claude 3");
|
||||
vi.mocked(openaiAdapters.constructLlmApi).mockReturnValue(
|
||||
mockLlmApi as any,
|
||||
);
|
||||
vi.mocked(config.createLlmApi).mockReturnValue(mockLlmApi as any);
|
||||
|
||||
const state = await service.initialize(mockAssistant, mockAuthConfig);
|
||||
|
||||
|
|
@ -152,9 +148,7 @@ describe("ModelService", () => {
|
|||
await service.initialize(mockAssistant, mockAuthConfig);
|
||||
|
||||
const newLlmApi = { complete: vi.fn(), stream: vi.fn() };
|
||||
vi.mocked(openaiAdapters.constructLlmApi).mockReturnValue(
|
||||
newLlmApi as any,
|
||||
);
|
||||
vi.mocked(config.createLlmApi).mockReturnValue(newLlmApi as any);
|
||||
|
||||
const state = await service.switchModel(1);
|
||||
|
||||
|
|
@ -204,24 +198,14 @@ describe("ModelService", () => {
|
|||
]);
|
||||
await service.initialize(mockAssistant, mockAuthConfig);
|
||||
|
||||
vi.mocked(workos.getAccessToken).mockReturnValue("proxy-token");
|
||||
vi.mocked(workos.getOrganizationId).mockReturnValue("org-123");
|
||||
vi.mocked(openaiAdapters.constructLlmApi).mockReturnValue(
|
||||
mockLlmApi as any,
|
||||
);
|
||||
vi.mocked(config.createLlmApi).mockReturnValue(mockLlmApi as any);
|
||||
|
||||
await service.switchModel(0);
|
||||
|
||||
expect(vi.mocked(openaiAdapters.constructLlmApi)).toHaveBeenCalledWith({
|
||||
provider: "continue-proxy",
|
||||
apiBase: "https://proxy.continue.dev",
|
||||
apiKey: "proxy-token",
|
||||
env: {
|
||||
apiKeyLocation: "env.PROXY_KEY",
|
||||
orgScopeId: "org-123",
|
||||
proxyUrl: undefined,
|
||||
},
|
||||
});
|
||||
expect(vi.mocked(config.createLlmApi)).toHaveBeenCalledWith(
|
||||
proxyModel,
|
||||
mockAuthConfig,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
@ -292,9 +276,7 @@ describe("ModelService", () => {
|
|||
|
||||
expect(service.getCurrentModelIndex()).toBe(0);
|
||||
|
||||
vi.mocked(openaiAdapters.constructLlmApi).mockReturnValue(
|
||||
mockLlmApi as any,
|
||||
);
|
||||
vi.mocked(config.createLlmApi).mockReturnValue(mockLlmApi as any);
|
||||
await service.switchModel(1);
|
||||
expect(service.getCurrentModelIndex()).toBe(1);
|
||||
});
|
||||
|
|
@ -392,9 +374,7 @@ describe("ModelService", () => {
|
|||
service.on("stateChanged", listener);
|
||||
|
||||
const newLlmApi = { complete: vi.fn(), stream: vi.fn() };
|
||||
vi.mocked(openaiAdapters.constructLlmApi).mockReturnValue(
|
||||
newLlmApi as any,
|
||||
);
|
||||
vi.mocked(config.createLlmApi).mockReturnValue(newLlmApi as any);
|
||||
await service.switchModel(1);
|
||||
|
||||
expect(listener).toHaveBeenCalledWith(
|
||||
|
|
@ -419,7 +399,7 @@ describe("ModelService", () => {
|
|||
const errorListener = vi.fn();
|
||||
service.on("error", errorListener);
|
||||
|
||||
vi.mocked(openaiAdapters.constructLlmApi).mockReturnValue(null as any);
|
||||
vi.mocked(config.createLlmApi).mockReturnValue(null as any);
|
||||
|
||||
await expect(service.switchModel(1)).rejects.toThrow(
|
||||
"Failed to initialize LLM",
|
||||
|
|
|
|||
|
|
@ -1,13 +1,7 @@
|
|||
import { AssistantUnrolled, ModelConfig } from "@continuedev/config-yaml";
|
||||
import { constructLlmApi, LLMConfig } from "@continuedev/openai-adapters";
|
||||
|
||||
import {
|
||||
AuthConfig,
|
||||
getAccessToken,
|
||||
getModelName,
|
||||
getOrganizationId,
|
||||
} from "../auth/workos.js";
|
||||
import { getLlmApi } from "../config.js";
|
||||
import { AuthConfig, getModelName } from "../auth/workos.js";
|
||||
import { createLlmApi, getLlmApi } from "../config.js";
|
||||
import { logger } from "../util/logger.js";
|
||||
|
||||
import { BaseService, ServiceWithDependencies } from "./BaseService.js";
|
||||
|
|
@ -89,31 +83,7 @@ export class ModelService
|
|||
name: (selectedModel as any).name || "unnamed",
|
||||
});
|
||||
|
||||
const accessToken = getAccessToken(authConfig);
|
||||
const organizationId = getOrganizationId(authConfig);
|
||||
|
||||
const config: LLMConfig =
|
||||
selectedModel.provider === "continue-proxy"
|
||||
? {
|
||||
provider: selectedModel.provider,
|
||||
requestOptions: selectedModel.requestOptions,
|
||||
apiBase: selectedModel.apiBase,
|
||||
apiKey: accessToken ?? undefined,
|
||||
env: {
|
||||
apiKeyLocation: (selectedModel as any).apiKeyLocation,
|
||||
orgScopeId: organizationId,
|
||||
proxyUrl: (selectedModel as any).onPremProxyUrl ?? undefined,
|
||||
},
|
||||
}
|
||||
: {
|
||||
provider: selectedModel.provider as any,
|
||||
apiKey: selectedModel.apiKey,
|
||||
apiBase: selectedModel.apiBase,
|
||||
requestOptions: selectedModel.requestOptions,
|
||||
env: selectedModel.env,
|
||||
};
|
||||
|
||||
const llmApi = constructLlmApi(config);
|
||||
const llmApi = createLlmApi(selectedModel, authConfig);
|
||||
|
||||
if (!llmApi) {
|
||||
throw new Error("Failed to initialize LLM with persisted model");
|
||||
|
|
@ -288,31 +258,7 @@ export class ModelService
|
|||
});
|
||||
|
||||
try {
|
||||
const accessToken = getAccessToken(authConfig);
|
||||
const organizationId = getOrganizationId(authConfig);
|
||||
|
||||
const config: LLMConfig =
|
||||
selectedModel.provider === "continue-proxy"
|
||||
? {
|
||||
provider: selectedModel.provider,
|
||||
requestOptions: selectedModel.requestOptions,
|
||||
apiBase: selectedModel.apiBase,
|
||||
apiKey: accessToken ?? undefined,
|
||||
env: {
|
||||
apiKeyLocation: (selectedModel as any).apiKeyLocation,
|
||||
orgScopeId: organizationId,
|
||||
proxyUrl: (selectedModel as any).onPremProxyUrl ?? undefined,
|
||||
},
|
||||
}
|
||||
: {
|
||||
provider: selectedModel.provider as any,
|
||||
apiKey: selectedModel.apiKey,
|
||||
apiBase: selectedModel.apiBase,
|
||||
requestOptions: selectedModel.requestOptions,
|
||||
env: selectedModel.env,
|
||||
};
|
||||
|
||||
const llmApi = constructLlmApi(config);
|
||||
const llmApi = createLlmApi(selectedModel, authConfig);
|
||||
|
||||
if (!llmApi) {
|
||||
throw new Error("Failed to initialize LLM with selected model");
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue