| 1 | import { AssistantUnrolled, Rule } from "@continuedev/config-yaml"; |
| 2 | |
| 3 | import { BaseCommandOptions } from "./commands/BaseCommandOptions.js"; |
| 4 | import { |
| 5 | loadPackagesFromHub, |
| 6 | mcpProcessor, |
| 7 | modelProcessor, |
| 8 | processRule, |
| 9 | } from "./hubLoader.js"; |
| 10 | import { logger } from "./util/logger.js"; |
| 11 | |
| 12 | /** |
| 13 | * Enhances a configuration by injecting additional components from CLI flags |
| 14 | */ |
| 15 | export class ConfigEnhancer { |
| 16 | /** |
| 17 | * Apply all enhancements to a configuration |
| 18 | */ |
| 19 | async enhanceConfig( |
| 20 | config: AssistantUnrolled, |
| 21 | options: BaseCommandOptions, |
| 22 | ): Promise<AssistantUnrolled> { |
| 23 | let enhancedConfig = { ...config }; |
| 24 | |
| 25 | // Apply rules |
| 26 | if (options.rule && options.rule.length > 0) { |
| 27 | enhancedConfig = await this.injectRules(enhancedConfig, options.rule); |
| 28 | } |
| 29 | |
| 30 | // Apply MCPs |
| 31 | if (options.mcp && options.mcp.length > 0) { |
| 32 | enhancedConfig = await this.injectMcps(enhancedConfig, options.mcp); |
| 33 | } |
| 34 | |
| 35 | // Apply models |
| 36 | if (options.model && options.model.length > 0) { |
| 37 | enhancedConfig = await this.injectModels(enhancedConfig, options.model); |
| 38 | } |
| 39 | |
| 40 | // Apply prompts |
| 41 | if (options.prompt && options.prompt.length > 0) { |
| 42 | enhancedConfig = await this.injectPrompts(enhancedConfig, options.prompt); |
| 43 | } |
| 44 | |
| 45 | return enhancedConfig; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Inject rules into the system message |
| 50 | */ |
| 51 | private async injectRules( |
| 52 | config: AssistantUnrolled, |
| 53 | rules: string[], |
| 54 | ): Promise<AssistantUnrolled> { |
| 55 | const processedRules: Rule[] = []; |
| 56 | |
| 57 | for (const ruleSpec of rules) { |
| 58 | try { |
| 59 | const processedContent = await processRule(ruleSpec); |
| 60 | |
| 61 | // Check if this is a hub slug (contains / but doesn't start with . or /) |
| 62 | const isHubSlug = |
| 63 | ruleSpec.includes("/") && |
| 64 | !ruleSpec.startsWith(".") && |
| 65 | !ruleSpec.startsWith("/"); |
| 66 | |
| 67 | if (isHubSlug) { |
| 68 | // Store as RuleObject with name (slug) and rule (content) |
| 69 | processedRules.push({ |
| 70 | name: ruleSpec, |
| 71 | rule: processedContent, |
| 72 | }); |
| 73 | } else { |
| 74 | // Store as plain string for file paths or direct content |
| 75 | processedRules.push(processedContent); |
| 76 | } |
| 77 | } catch (error: any) { |
| 78 | logger.warn(`Failed to process rule "${ruleSpec}": ${error.message}`); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // Clone the config to avoid mutating the original |
| 83 | const modifiedConfig = { ...config }; |
| 84 | |
| 85 | // Add processed rules to the config's rules array |
| 86 | if (processedRules.length > 0) { |
| 87 | // Combine with existing rules if any |
| 88 | const existingRules = modifiedConfig.rules || []; |
| 89 | modifiedConfig.rules = [...existingRules, ...processedRules]; |
| 90 | } |
| 91 | |
| 92 | return modifiedConfig; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Inject MCP servers into the configuration |
| 97 | */ |
| 98 | private async injectMcps( |
| 99 | config: AssistantUnrolled, |
| 100 | mcps: string[], |
| 101 | ): Promise<AssistantUnrolled> { |
| 102 | const processedMcps = await loadPackagesFromHub(mcps, mcpProcessor); |
| 103 | |
| 104 | // Clone the config to avoid mutating the original |
| 105 | const modifiedConfig = { ...config }; |
| 106 | |
| 107 | // Prepend processed MCPs to existing mcpServers array for consistency |
| 108 | const existingMcpServers = (modifiedConfig as any).mcpServers || []; |
| 109 | (modifiedConfig as any).mcpServers = [ |
| 110 | ...processedMcps, |
| 111 | ...existingMcpServers, |
| 112 | ]; |
| 113 | |
| 114 | return modifiedConfig; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Inject models into the configuration |
| 119 | */ |
| 120 | private async injectModels( |
| 121 | config: AssistantUnrolled, |
| 122 | models: string[], |
| 123 | ): Promise<AssistantUnrolled> { |
| 124 | const processedModels = await loadPackagesFromHub(models, modelProcessor); |
| 125 | |
| 126 | // Clone the config to avoid mutating the original |
| 127 | const modifiedConfig = { ...config }; |
| 128 | |
| 129 | // Prepend processed models to existing models array so they become the default |
| 130 | const existingModels = (modifiedConfig as any).models || []; |
| 131 | (modifiedConfig as any).models = [...processedModels, ...existingModels]; |
| 132 | |
| 133 | return modifiedConfig; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Inject prompts into the configuration |
| 138 | */ |
| 139 | private async injectPrompts( |
| 140 | config: AssistantUnrolled, |
| 141 | prompts: string[], |
| 142 | ): Promise<AssistantUnrolled> { |
| 143 | // For prompts, we'll need a different approach since they become slash commands |
| 144 | // This is a placeholder implementation |
| 145 | logger.debug("Prompt injection not yet implemented", { prompts }); |
| 146 | return config; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Global config enhancer instance |
| 152 | */ |
| 153 | export const configEnhancer = new ConfigEnhancer(); |