Elffuss BraidHub

kikocis/continue public

3fe1abb7e204 · 3.1 KiBRawPermalink
1import { AssistantUnrolled, ModelConfig } from "@continuedev/config-yaml";
2import {
3 BaseLlmApi,
4 constructLlmApi,
5 LLMConfig,
6} from "@continuedev/openai-adapters";
7import {
8 Configuration,
9 DefaultApi,
10 DefaultApiInterface,
11} from "@continuedev/sdk/dist/api/dist/index.js";
12
13import {
14 AuthConfig,
15 getAccessToken,
16 getOrganizationId,
17} from "./auth/workos.js";
18import { loadConfiguration } from "./configLoader.js";
19import { env } from "./env.js";
20import { MCPService } from "./services/MCPService.js";
21
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 */
26export function createLlmApi(
27 model: ModelConfig,
28 authConfig: AuthConfig | null,
29): BaseLlmApi | null {
30 const accessToken = getAccessToken(authConfig);
31 const organizationId = getOrganizationId(authConfig);
32
33 const config: LLMConfig =
34 model.provider === "continue-proxy"
35 ? {
36 provider: model.provider,
37 requestOptions: model.requestOptions,
38 apiBase: model.apiBase,
39 apiKey: accessToken ?? undefined,
40 env: {
41 apiKeyLocation: (model as any).apiKeyLocation,
42 orgScopeId: organizationId,
43 proxyUrl: (model as any).onPremProxyUrl ?? undefined,
44 },
45 }
46 : {
47 provider: model.provider as any,
48 apiKey: model.apiKey,
49 apiBase: model.apiBase,
50 requestOptions: model.requestOptions,
51 env: model.env,
52 };
53
54 return constructLlmApi(config);
55}
56
57export function getLlmApi(
58 assistant: AssistantUnrolled,
59 authConfig: AuthConfig,
60): [BaseLlmApi, ModelConfig] {
61 if (!assistant.models || assistant.models.length === 0) {
62 throw new Error("No models found in the configured assistant");
63 }
64
65 const model = assistant.models?.find(
66 (model) =>
67 model?.roles?.includes("chat") || (model && model.roles === undefined),
68 );
69
70 if (!model) {
71 throw new Error(
72 "No models with the chat role found in the configured assistant",
73 );
74 }
75
76 const llmApi = createLlmApi(model, authConfig);
77
78 if (!llmApi) {
79 throw new Error(
80 "Failed to initialize LLM. Please check your configuration.",
81 );
82 }
83
84 return [llmApi, model];
85}
86
87export function getApiClient(
88 accessToken: string | undefined | null,
89): DefaultApi {
90 return new DefaultApi(
91 new Configuration({
92 basePath: env.apiBase.replace(/\/$/, ""),
93 accessToken: accessToken ?? undefined,
94 }),
95 );
96}
97
98export async function initialize(
99 authConfig: AuthConfig,
100 configPath: string | undefined,
101): Promise<{
102 config: AssistantUnrolled;
103 llmApi: BaseLlmApi;
104 model: ModelConfig;
105 mcpService: MCPService;
106 apiClient: DefaultApiInterface;
107}> {
108 const apiClient = getApiClient(authConfig?.accessToken);
109 const result = await loadConfiguration(authConfig, configPath, apiClient);
110 const config = result.config;
111 const [llmApi, model] = getLlmApi(config, authConfig);
112 const mcpService = new MCPService();
113 await mcpService.initialize(config, false);
114
115 return { config, llmApi, model, mcpService, apiClient };
116}