| 1 | import { AssistantUnrolled, ModelConfig } from "@continuedev/config-yaml"; |
| 2 | import { |
| 3 | BaseLlmApi, |
| 4 | constructLlmApi, |
| 5 | LLMConfig, |
| 6 | } from "@continuedev/openai-adapters"; |
| 7 | import { |
| 8 | Configuration, |
| 9 | DefaultApi, |
| 10 | DefaultApiInterface, |
| 11 | } from "@continuedev/sdk/dist/api/dist/index.js"; |
| 12 | |
| 13 | import { |
| 14 | AuthConfig, |
| 15 | getAccessToken, |
| 16 | getOrganizationId, |
| 17 | } from "./auth/workos.js"; |
| 18 | import { loadConfiguration } from "./configLoader.js"; |
| 19 | import { env } from "./env.js"; |
| 20 | import { MCPService } from "./services/MCPService.js"; |
| 21 | |
| 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 | |
| 41 | const accessToken = getAccessToken(authConfig); |
| 42 | const organizationId = getOrganizationId(authConfig); |
| 43 | |
| 44 | const config: LLMConfig = |
| 45 | model.provider === "continue-proxy" |
| 46 | ? { |
| 47 | provider: model.provider, |
| 48 | requestOptions: model.requestOptions, |
| 49 | apiBase: model.apiBase, |
| 50 | apiKey: accessToken ?? undefined, |
| 51 | env: { |
| 52 | apiKeyLocation: (model as any).apiKeyLocation, |
| 53 | // envSecretLocations: model.env, |
| 54 | orgScopeId: organizationId, |
| 55 | proxyUrl: undefined, // TODO |
| 56 | }, |
| 57 | } |
| 58 | : { |
| 59 | provider: model.provider as any, |
| 60 | apiKey: model.apiKey, |
| 61 | apiBase: model.apiBase, |
| 62 | requestOptions: model.requestOptions, |
| 63 | env: model.env, |
| 64 | }; |
| 65 | |
| 66 | const llmApi = constructLlmApi(config); |
| 67 | |
| 68 | if (!llmApi) { |
| 69 | throw new Error( |
| 70 | "Failed to initialize LLM. Please check your configuration.", |
| 71 | ); |
| 72 | } |
| 73 | |
| 74 | return [llmApi, model]; |
| 75 | } |
| 76 | |
| 77 | export function getApiClient( |
| 78 | accessToken: string | undefined | null, |
| 79 | ): DefaultApi { |
| 80 | return new DefaultApi( |
| 81 | new Configuration({ |
| 82 | basePath: env.apiBase.replace(/\/$/, ""), |
| 83 | accessToken: accessToken ?? undefined, |
| 84 | }), |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | export async function initialize( |
| 89 | authConfig: AuthConfig, |
| 90 | configPath: string | undefined, |
| 91 | ): Promise<{ |
| 92 | config: AssistantUnrolled; |
| 93 | llmApi: BaseLlmApi; |
| 94 | model: ModelConfig; |
| 95 | mcpService: MCPService; |
| 96 | apiClient: DefaultApiInterface; |
| 97 | }> { |
| 98 | const apiClient = getApiClient(authConfig?.accessToken); |
| 99 | const result = await loadConfiguration(authConfig, configPath, apiClient); |
| 100 | const config = result.config; |
| 101 | const [llmApi, model] = getLlmApi(config, authConfig); |
| 102 | const mcpService = new MCPService(); |
| 103 | await mcpService.initialize(config, false); |
| 104 | |
| 105 | return { config, llmApi, model, mcpService, apiClient }; |
| 106 | } |