| 1 | import { |
| 2 | AssistantUnrolled, |
| 3 | BLOCK_TYPES, |
| 4 | ConfigResult, |
| 5 | ConfigValidationError, |
| 6 | isAssistantUnrolledNonNullable, |
| 7 | mergeUnrolledAssistants, |
| 8 | ModelRole, |
| 9 | PackageIdentifier, |
| 10 | RegistryClient, |
| 11 | TEMPLATE_VAR_REGEX, |
| 12 | unrollAssistant, |
| 13 | validateConfigYaml, |
| 14 | } from "@continuedev/config-yaml"; |
| 15 | import { dirname } from "node:path"; |
| 16 | |
| 17 | import { |
| 18 | ContinueConfig, |
| 19 | IContextProvider, |
| 20 | IDE, |
| 21 | IdeInfo, |
| 22 | IdeSettings, |
| 23 | ILLMLogger, |
| 24 | } from "../.."; |
| 25 | import { MCPManagerSingleton } from "../../context/mcp/MCPManagerSingleton"; |
| 26 | import DocsContextProvider from "../../context/providers/DocsContextProvider"; |
| 27 | import FileContextProvider from "../../context/providers/FileContextProvider"; |
| 28 | import { contextProviderClassFromName } from "../../context/providers/index"; |
| 29 | import { ControlPlaneClient } from "../../control-plane/client"; |
| 30 | import TransformersJsEmbeddingsProvider from "../../llm/llms/TransformersJsEmbeddingsProvider"; |
| 31 | import { getAllPromptFiles } from "../../promptFiles/getPromptFiles"; |
| 32 | import { GlobalContext } from "../../util/GlobalContext"; |
| 33 | import { modifyAnyConfigWithSharedConfig } from "../sharedConfig"; |
| 34 | |
| 35 | import { convertPromptBlockToSlashCommand } from "../../commands/slash/promptBlockSlashCommand"; |
| 36 | import { slashCommandFromPromptFile } from "../../commands/slash/promptFileSlashCommand"; |
| 37 | import { getControlPlaneEnvSync } from "../../control-plane/env"; |
| 38 | import { getToolsForIde } from "../../tools"; |
| 39 | import { getCleanUriPath } from "../../util/uri"; |
| 40 | import { getAllDotContinueDefinitionFiles } from "../loadLocalAssistants"; |
| 41 | import { unrollLocalYamlBlocks } from "./loadLocalYamlBlocks"; |
| 42 | import { LocalPlatformClient } from "./LocalPlatformClient"; |
| 43 | import { llmsFromModelConfig } from "./models"; |
| 44 | import { |
| 45 | convertYamlMcpToContinueMcp, |
| 46 | convertYamlRuleToContinueRule, |
| 47 | } from "./yamlToContinueConfig"; |
| 48 | |
| 49 | async function loadConfigYaml(options: { |
| 50 | overrideConfigYaml: AssistantUnrolled | undefined; |
| 51 | controlPlaneClient: ControlPlaneClient; |
| 52 | orgScopeId: string | null; |
| 53 | ideSettings: IdeSettings; |
| 54 | ide: IDE; |
| 55 | packageIdentifier: PackageIdentifier; |
| 56 | }): Promise<ConfigResult<AssistantUnrolled>> { |
| 57 | const { |
| 58 | overrideConfigYaml, |
| 59 | controlPlaneClient, |
| 60 | orgScopeId, |
| 61 | ideSettings, |
| 62 | ide, |
| 63 | packageIdentifier, |
| 64 | } = options; |
| 65 | |
| 66 | // Add local .continue blocks |
| 67 | const localBlockPromises = BLOCK_TYPES.map(async (blockType) => { |
| 68 | const localBlocks = await getAllDotContinueDefinitionFiles( |
| 69 | ide, |
| 70 | { includeGlobal: true, includeWorkspace: true, fileExtType: "yaml" }, |
| 71 | blockType, |
| 72 | ); |
| 73 | return localBlocks.map((b) => ({ |
| 74 | uriType: "file" as const, |
| 75 | filePath: b.path, |
| 76 | })); |
| 77 | }); |
| 78 | const localPackageIdentifiers: PackageIdentifier[] = ( |
| 79 | await Promise.all(localBlockPromises) |
| 80 | ).flat(); |
| 81 | |
| 82 | // logger.info( |
| 83 | // `Loading config.yaml from ${JSON.stringify(packageIdentifier)} with root path ${rootPath}`, |
| 84 | // ); |
| 85 | |
| 86 | // Registry client is only used if local blocks are present, but logic same for hub/local assistants |
| 87 | const getRegistryClient = async () => { |
| 88 | const rootPath = |
| 89 | packageIdentifier.uriType === "file" |
| 90 | ? dirname(getCleanUriPath(packageIdentifier.filePath)) |
| 91 | : undefined; |
| 92 | return new RegistryClient({ |
| 93 | accessToken: await controlPlaneClient.getAccessToken(), |
| 94 | apiBase: getControlPlaneEnvSync(ideSettings.continueTestEnvironment) |
| 95 | .CONTROL_PLANE_URL, |
| 96 | rootPath, |
| 97 | }); |
| 98 | }; |
| 99 | |
| 100 | const errors: ConfigValidationError[] = []; |
| 101 | |
| 102 | let config: AssistantUnrolled | undefined; |
| 103 | |
| 104 | if (overrideConfigYaml) { |
| 105 | config = overrideConfigYaml; |
| 106 | if (localPackageIdentifiers.length > 0) { |
| 107 | const unrolledLocal = await unrollLocalYamlBlocks( |
| 108 | localPackageIdentifiers, |
| 109 | ide, |
| 110 | await getRegistryClient(), |
| 111 | orgScopeId, |
| 112 | controlPlaneClient, |
| 113 | ); |
| 114 | if (unrolledLocal.errors) { |
| 115 | errors.push(...unrolledLocal.errors); |
| 116 | } |
| 117 | if (unrolledLocal.config) { |
| 118 | config = mergeUnrolledAssistants(config, unrolledLocal.config); |
| 119 | } |
| 120 | } |
| 121 | } else { |
| 122 | // This is how we allow use of blocks locally |
| 123 | const unrollResult = await unrollAssistant( |
| 124 | packageIdentifier, |
| 125 | await getRegistryClient(), |
| 126 | { |
| 127 | renderSecrets: true, |
| 128 | currentUserSlug: "", |
| 129 | onPremProxyUrl: null, |
| 130 | orgScopeId, |
| 131 | platformClient: new LocalPlatformClient( |
| 132 | orgScopeId, |
| 133 | controlPlaneClient, |
| 134 | ide, |
| 135 | ), |
| 136 | injectBlocks: localPackageIdentifiers, |
| 137 | }, |
| 138 | ); |
| 139 | config = unrollResult.config; |
| 140 | if (unrollResult.errors) { |
| 141 | errors.push(...unrollResult.errors); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | if (config && isAssistantUnrolledNonNullable(config)) { |
| 146 | errors.push(...validateConfigYaml(config)); |
| 147 | } |
| 148 | |
| 149 | if (errors?.some((error) => error.fatal)) { |
| 150 | return { |
| 151 | errors, |
| 152 | config: undefined, |
| 153 | configLoadInterrupted: true, |
| 154 | }; |
| 155 | } |
| 156 | |
| 157 | // Set defaults if undefined (this lets us keep config.json uncluttered for new users) |
| 158 | return { |
| 159 | config, |
| 160 | errors, |
| 161 | configLoadInterrupted: false, |
| 162 | }; |
| 163 | } |
| 164 | |
| 165 | async function configYamlToContinueConfig(options: { |
| 166 | config: AssistantUnrolled; |
| 167 | ide: IDE; |
| 168 | ideSettings: IdeSettings; |
| 169 | ideInfo: IdeInfo; |
| 170 | uniqueId: string; |
| 171 | llmLogger: ILLMLogger; |
| 172 | workOsAccessToken: string | undefined; |
| 173 | }): Promise<{ config: ContinueConfig; errors: ConfigValidationError[] }> { |
| 174 | let { config, ide, ideSettings, ideInfo, uniqueId, llmLogger } = options; |
| 175 | |
| 176 | const localErrors: ConfigValidationError[] = []; |
| 177 | |
| 178 | const continueConfig: ContinueConfig = { |
| 179 | slashCommands: [], |
| 180 | tools: await getToolsForIde(ide), |
| 181 | mcpServerStatuses: [], |
| 182 | contextProviders: [], |
| 183 | modelsByRole: { |
| 184 | chat: [], |
| 185 | edit: [], |
| 186 | apply: [], |
| 187 | embed: [], |
| 188 | autocomplete: [], |
| 189 | rerank: [], |
| 190 | summarize: [], |
| 191 | }, |
| 192 | selectedModelByRole: { |
| 193 | chat: null, |
| 194 | edit: null, // not currently used |
| 195 | apply: null, |
| 196 | embed: null, |
| 197 | autocomplete: null, |
| 198 | rerank: null, |
| 199 | summarize: null, |
| 200 | }, |
| 201 | rules: [], |
| 202 | }; |
| 203 | |
| 204 | // Right now, if there are any missing packages in the config, then we will just throw an error |
| 205 | if (!isAssistantUnrolledNonNullable(config)) { |
| 206 | return { |
| 207 | config: continueConfig, |
| 208 | errors: [ |
| 209 | { |
| 210 | message: |
| 211 | "Failed to load config due to missing blocks, see which blocks are missing below", |
| 212 | fatal: true, |
| 213 | }, |
| 214 | ], |
| 215 | }; |
| 216 | } |
| 217 | |
| 218 | for (const rule of config.rules ?? []) { |
| 219 | continueConfig.rules.push(convertYamlRuleToContinueRule(rule)); |
| 220 | } |
| 221 | |
| 222 | continueConfig.data = config.data; |
| 223 | continueConfig.docs = config.docs?.map((doc) => ({ |
| 224 | title: doc.name, |
| 225 | startUrl: doc.startUrl, |
| 226 | rootUrl: doc.rootUrl, |
| 227 | faviconUrl: doc.faviconUrl, |
| 228 | useLocalCrawling: doc.useLocalCrawling, |
| 229 | sourceFile: doc.sourceFile, |
| 230 | })); |
| 231 | |
| 232 | config.mcpServers?.forEach((mcpServer) => { |
| 233 | const mcpArgVariables = |
| 234 | mcpServer.args?.filter((arg) => TEMPLATE_VAR_REGEX.test(arg)) ?? []; |
| 235 | |
| 236 | if (mcpArgVariables.length === 0) { |
| 237 | return; |
| 238 | } |
| 239 | |
| 240 | localErrors.push({ |
| 241 | fatal: false, |
| 242 | message: `MCP server "${mcpServer.name}" has unsubstituted variables in args: ${mcpArgVariables.join(", ")}. Please refer to https://docs.continue.dev/hub/secrets/secret-types for managing hub secrets.`, |
| 243 | }); |
| 244 | }); |
| 245 | |
| 246 | continueConfig.experimental = { |
| 247 | modelContextProtocolServers: config.mcpServers?.map( |
| 248 | convertYamlMcpToContinueMcp, |
| 249 | ), |
| 250 | }; |
| 251 | |
| 252 | // Prompt files - |
| 253 | try { |
| 254 | const promptFiles = await getAllPromptFiles(ide, undefined, true); |
| 255 | |
| 256 | promptFiles.forEach((file) => { |
| 257 | try { |
| 258 | const slashCommand = slashCommandFromPromptFile( |
| 259 | file.path, |
| 260 | file.content, |
| 261 | ); |
| 262 | if (slashCommand) { |
| 263 | continueConfig.slashCommands?.push(slashCommand); |
| 264 | } |
| 265 | } catch (e) { |
| 266 | localErrors.push({ |
| 267 | fatal: false, |
| 268 | message: `Failed to convert prompt file ${file.path} to slash command: ${e instanceof Error ? e.message : e}`, |
| 269 | }); |
| 270 | } |
| 271 | }); |
| 272 | } catch (e) { |
| 273 | localErrors.push({ |
| 274 | fatal: false, |
| 275 | message: `Error loading local prompt files: ${e instanceof Error ? e.message : e}`, |
| 276 | }); |
| 277 | } |
| 278 | |
| 279 | config.prompts?.forEach((prompt) => { |
| 280 | try { |
| 281 | const slashCommand = convertPromptBlockToSlashCommand(prompt); |
| 282 | continueConfig.slashCommands?.push(slashCommand); |
| 283 | } catch (e) { |
| 284 | localErrors.push({ |
| 285 | message: `Error loading prompt ${prompt.name}: ${e instanceof Error ? e.message : e}`, |
| 286 | fatal: false, |
| 287 | }); |
| 288 | } |
| 289 | }); |
| 290 | |
| 291 | // Models |
| 292 | let warnAboutFreeTrial = false; |
| 293 | const defaultModelRoles: ModelRole[] = ["chat", "summarize", "apply", "edit"]; |
| 294 | for (const model of config.models ?? []) { |
| 295 | model.roles = model.roles ?? defaultModelRoles; // Default to all 4 chat-esque roles if not specified |
| 296 | |
| 297 | if (model.provider === "free-trial") { |
| 298 | warnAboutFreeTrial = true; |
| 299 | } |
| 300 | try { |
| 301 | const llms = await llmsFromModelConfig({ |
| 302 | model, |
| 303 | ide, |
| 304 | uniqueId, |
| 305 | ideSettings, |
| 306 | llmLogger, |
| 307 | config: continueConfig, |
| 308 | }); |
| 309 | |
| 310 | if (model.roles?.includes("chat")) { |
| 311 | continueConfig.modelsByRole.chat.push(...llms); |
| 312 | } |
| 313 | |
| 314 | if (model.roles?.includes("summarize")) { |
| 315 | continueConfig.modelsByRole.summarize.push(...llms); |
| 316 | } |
| 317 | |
| 318 | if (model.roles?.includes("apply")) { |
| 319 | continueConfig.modelsByRole.apply.push(...llms); |
| 320 | } |
| 321 | |
| 322 | if (model.roles?.includes("edit")) { |
| 323 | continueConfig.modelsByRole.edit.push(...llms); |
| 324 | } |
| 325 | |
| 326 | if (model.roles?.includes("autocomplete")) { |
| 327 | continueConfig.modelsByRole.autocomplete.push(...llms); |
| 328 | } |
| 329 | |
| 330 | if (model.roles?.includes("embed")) { |
| 331 | const { provider } = model; |
| 332 | if (provider === "transformers.js") { |
| 333 | if (ideInfo.ideType === "vscode") { |
| 334 | continueConfig.modelsByRole.embed.push( |
| 335 | new TransformersJsEmbeddingsProvider(), |
| 336 | ); |
| 337 | } else { |
| 338 | localErrors.push({ |
| 339 | fatal: false, |
| 340 | message: `Transformers.js embeddings provider not supported in this IDE.`, |
| 341 | }); |
| 342 | } |
| 343 | } else { |
| 344 | continueConfig.modelsByRole.embed.push(...llms); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | if (model.roles?.includes("rerank")) { |
| 349 | continueConfig.modelsByRole.rerank.push(...llms); |
| 350 | } |
| 351 | } catch (e) { |
| 352 | localErrors.push({ |
| 353 | fatal: false, |
| 354 | message: `Failed to load model:\nName: ${model.name}\nModel: ${model.model}\nProvider: ${model.provider}\n${e instanceof Error ? e.message : e}`, |
| 355 | }); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | // Add transformers js to the embed models in vs code if not already added |
| 360 | if ( |
| 361 | ideInfo.ideType === "vscode" && |
| 362 | !continueConfig.modelsByRole.embed.find( |
| 363 | (m) => m.providerName === "transformers.js", |
| 364 | ) |
| 365 | ) { |
| 366 | continueConfig.modelsByRole.embed.push( |
| 367 | new TransformersJsEmbeddingsProvider(), |
| 368 | ); |
| 369 | } |
| 370 | |
| 371 | if (warnAboutFreeTrial) { |
| 372 | localErrors.push({ |
| 373 | fatal: false, |
| 374 | message: |
| 375 | "Model provider 'free-trial' is no longer supported, will be ignored.", |
| 376 | }); |
| 377 | } |
| 378 | |
| 379 | // Context providers |
| 380 | const DEFAULT_CONTEXT_PROVIDERS = [new FileContextProvider({})]; |
| 381 | |
| 382 | const DEFAULT_CONTEXT_PROVIDERS_TITLES = DEFAULT_CONTEXT_PROVIDERS.map( |
| 383 | ({ description: { title } }) => title, |
| 384 | ); |
| 385 | |
| 386 | continueConfig.contextProviders = (config.context |
| 387 | ?.map((context) => { |
| 388 | const cls = contextProviderClassFromName(context.provider) as any; |
| 389 | if (!cls) { |
| 390 | if (!DEFAULT_CONTEXT_PROVIDERS_TITLES.includes(context.provider)) { |
| 391 | localErrors.push({ |
| 392 | fatal: false, |
| 393 | message: `Unknown context provider ${context.provider}`, |
| 394 | }); |
| 395 | } |
| 396 | return undefined; |
| 397 | } |
| 398 | const instance: IContextProvider = new cls({ |
| 399 | name: context.name, |
| 400 | ...context.params, |
| 401 | }); |
| 402 | return instance; |
| 403 | }) |
| 404 | .filter((p) => !!p) ?? []) as IContextProvider[]; |
| 405 | continueConfig.contextProviders.push(...DEFAULT_CONTEXT_PROVIDERS); |
| 406 | |
| 407 | if ( |
| 408 | continueConfig.docs?.length && |
| 409 | !continueConfig.contextProviders?.some( |
| 410 | (cp) => cp.description.title === "docs", |
| 411 | ) |
| 412 | ) { |
| 413 | continueConfig.contextProviders.push(new DocsContextProvider({})); |
| 414 | } |
| 415 | |
| 416 | // Trigger MCP server refreshes (Config is reloaded again once connected!) |
| 417 | const mcpManager = MCPManagerSingleton.getInstance(); |
| 418 | mcpManager.setConnections( |
| 419 | (config.mcpServers ?? []).map((server) => ({ |
| 420 | id: server.name, |
| 421 | name: server.name, |
| 422 | sourceFile: server.sourceFile, |
| 423 | transport: { |
| 424 | type: "stdio", |
| 425 | args: [], |
| 426 | ...(server as any), // TODO: fix the types on mcpServers in config-yaml |
| 427 | }, |
| 428 | timeout: server.connectionTimeout, |
| 429 | })), |
| 430 | false, |
| 431 | { ide }, |
| 432 | ); |
| 433 | |
| 434 | return { config: continueConfig, errors: localErrors }; |
| 435 | } |
| 436 | |
| 437 | export async function loadContinueConfigFromYaml(options: { |
| 438 | ide: IDE; |
| 439 | ideSettings: IdeSettings; |
| 440 | ideInfo: IdeInfo; |
| 441 | uniqueId: string; |
| 442 | llmLogger: ILLMLogger; |
| 443 | workOsAccessToken: string | undefined; |
| 444 | overrideConfigYaml: AssistantUnrolled | undefined; |
| 445 | controlPlaneClient: ControlPlaneClient; |
| 446 | orgScopeId: string | null; |
| 447 | packageIdentifier: PackageIdentifier; |
| 448 | }): Promise<ConfigResult<ContinueConfig>> { |
| 449 | const { |
| 450 | ide, |
| 451 | ideSettings, |
| 452 | ideInfo, |
| 453 | uniqueId, |
| 454 | llmLogger, |
| 455 | workOsAccessToken, |
| 456 | overrideConfigYaml, |
| 457 | controlPlaneClient, |
| 458 | orgScopeId, |
| 459 | packageIdentifier, |
| 460 | } = options; |
| 461 | |
| 462 | const configYamlResult = await loadConfigYaml({ |
| 463 | overrideConfigYaml, |
| 464 | controlPlaneClient, |
| 465 | orgScopeId, |
| 466 | ideSettings, |
| 467 | ide, |
| 468 | packageIdentifier, |
| 469 | }); |
| 470 | |
| 471 | if (!configYamlResult.config || configYamlResult.configLoadInterrupted) { |
| 472 | return { |
| 473 | errors: configYamlResult.errors, |
| 474 | config: undefined, |
| 475 | configLoadInterrupted: true, |
| 476 | }; |
| 477 | } |
| 478 | |
| 479 | const { config: continueConfig, errors: localErrors } = |
| 480 | await configYamlToContinueConfig({ |
| 481 | config: configYamlResult.config, |
| 482 | ide, |
| 483 | ideSettings, |
| 484 | ideInfo, |
| 485 | uniqueId, |
| 486 | llmLogger, |
| 487 | workOsAccessToken, |
| 488 | }); |
| 489 | |
| 490 | // Apply shared config |
| 491 | // TODO: override several of these values with user/org shared config |
| 492 | // Don't try catch this - has security implications and failure should be fatal |
| 493 | const sharedConfig = new GlobalContext().getSharedConfig(); |
| 494 | const withShared = modifyAnyConfigWithSharedConfig( |
| 495 | continueConfig, |
| 496 | sharedConfig, |
| 497 | ); |
| 498 | if (withShared.allowAnonymousTelemetry === undefined) { |
| 499 | withShared.allowAnonymousTelemetry = true; |
| 500 | } |
| 501 | |
| 502 | return { |
| 503 | config: withShared, |
| 504 | errors: [...(configYamlResult.errors ?? []), ...localErrors], |
| 505 | configLoadInterrupted: false, |
| 506 | }; |
| 507 | } |