Elffuss BraidHub

kikocis/continue public

fc89067dc2f1 · 4.4 KiBRawPermalink
1import { AssistantUnrolled, Rule } from "@continuedev/config-yaml";
2
3import { BaseCommandOptions } from "./commands/BaseCommandOptions.js";
4import {
5 loadPackagesFromHub,
6 mcpProcessor,
7 modelProcessor,
8 processRule,
9} from "./hubLoader.js";
10import { logger } from "./util/logger.js";
11
12/**
13 * Enhances a configuration by injecting additional components from CLI flags
14 */
15export 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 = ruleSpec.includes("/") &&
63 !ruleSpec.startsWith(".") &&
64 !ruleSpec.startsWith("/");
65
66 if (isHubSlug) {
67 // Store as RuleObject with name (slug) and rule (content)
68 processedRules.push({
69 name: ruleSpec,
70 rule: processedContent,
71 });
72 } else {
73 // Store as plain string for file paths or direct content
74 processedRules.push(processedContent);
75 }
76 } catch (error: any) {
77 logger.warn(`Failed to process rule "${ruleSpec}": ${error.message}`);
78 }
79 }
80
81 // Clone the config to avoid mutating the original
82 const modifiedConfig = { ...config };
83
84 // Add processed rules to the config's rules array
85 if (processedRules.length > 0) {
86 // Combine with existing rules if any
87 const existingRules = modifiedConfig.rules || [];
88 modifiedConfig.rules = [...existingRules, ...processedRules];
89 }
90
91 return modifiedConfig;
92 }
93
94 /**
95 * Inject MCP servers into the configuration
96 */
97 private async injectMcps(
98 config: AssistantUnrolled,
99 mcps: string[],
100 ): Promise<AssistantUnrolled> {
101 const processedMcps = await loadPackagesFromHub(mcps, mcpProcessor);
102
103 // Clone the config to avoid mutating the original
104 const modifiedConfig = { ...config };
105
106 // Prepend processed MCPs to existing mcpServers array for consistency
107 const existingMcpServers = (modifiedConfig as any).mcpServers || [];
108 (modifiedConfig as any).mcpServers = [
109 ...processedMcps,
110 ...existingMcpServers,
111 ];
112
113 return modifiedConfig;
114 }
115
116 /**
117 * Inject models into the configuration
118 */
119 private async injectModels(
120 config: AssistantUnrolled,
121 models: string[],
122 ): Promise<AssistantUnrolled> {
123 const processedModels = await loadPackagesFromHub(models, modelProcessor);
124
125 // Clone the config to avoid mutating the original
126 const modifiedConfig = { ...config };
127
128 // Prepend processed models to existing models array so they become the default
129 const existingModels = (modifiedConfig as any).models || [];
130 (modifiedConfig as any).models = [...processedModels, ...existingModels];
131
132 return modifiedConfig;
133 }
134
135 /**
136 * Inject prompts into the configuration
137 */
138 private async injectPrompts(
139 config: AssistantUnrolled,
140 prompts: string[],
141 ): Promise<AssistantUnrolled> {
142 // For prompts, we'll need a different approach since they become slash commands
143 // This is a placeholder implementation
144 logger.debug("Prompt injection not yet implemented", { prompts });
145 return config;
146 }
147}
148
149/**
150 * Global config enhancer instance
151 */
152export const configEnhancer = new ConfigEnhancer();