| 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 | /** |
| 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 { |
| 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 ?? null, |
| 43 | proxyUrl: |
| 44 | (model as { onPremProxyUrl: string | undefined }) |
| 45 | .onPremProxyUrl ?? undefined, |
| 46 | }, |
| 47 | } |
| 48 | : { |
| 49 | provider: model.provider as any, |
| 50 | apiKey: model.apiKey, |
| 51 | apiBase: model.apiBase, |
| 52 | requestOptions: model.requestOptions, |
| 53 | env: model.env, |
| 54 | }; |
| 55 | |
| 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); |
| 79 | |
| 80 | if (!llmApi) { |
| 81 | throw new Error( |
| 82 | "Failed to initialize LLM. Please check your configuration.", |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | return [llmApi, model]; |
| 87 | } |
| 88 | |
| 89 | export function getApiClient( |
| 90 | accessToken: string | undefined | null, |
| 91 | ): DefaultApi { |
| 92 | return new DefaultApi( |
| 93 | new Configuration({ |
| 94 | basePath: env.apiBase.replace(/\/$/, ""), |
| 95 | accessToken: accessToken ?? undefined, |
| 96 | }), |
| 97 | ); |
| 98 | } |
| 99 | |
| 100 | export async function initialize( |
| 101 | authConfig: AuthConfig, |
| 102 | configPath: string | undefined, |
| 103 | ): Promise<{ |
| 104 | config: AssistantUnrolled; |
| 105 | llmApi: BaseLlmApi; |
| 106 | model: ModelConfig; |
| 107 | mcpService: MCPService; |
| 108 | apiClient: DefaultApiInterface; |
| 109 | }> { |
| 110 | const apiClient = getApiClient(authConfig?.accessToken); |
| 111 | const result = await loadConfiguration(authConfig, configPath, apiClient); |
| 112 | const config = result.config; |
| 113 | const [llmApi, model] = getLlmApi(config, authConfig); |
| 114 | const mcpService = new MCPService(); |
| 115 | await mcpService.initialize(config, false); |
| 116 | |
| 117 | return { config, llmApi, model, mcpService, apiClient }; |
| 118 | } |