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