Elffuss BraidHub

kikocis/continue public

main · 6.1 KiBRawPermalink
1import { DefaultApiInterface } from "@continuedev/sdk/dist/api/dist/index.js";
2
3import { AuthConfig, loadAuthConfig } from "../auth/workos.js";
4import { BaseCommandOptions } from "../commands/BaseCommandOptions.js";
5import { configEnhancer } from "../configEnhancer.js";
6import { logger } from "../util/logger.js";
7
8import { BaseService, ServiceWithDependencies } from "./BaseService.js";
9import { serviceContainer } from "./ServiceContainer.js";
10import {
11 ApiClientServiceState,
12 ConfigServiceState,
13 SERVICE_NAMES,
14} from "./types.js";
15
16/**
17 * Service for managing configuration state and operations
18 * Handles loading configs from files or assistant slugs
19 */
20export class ConfigService
21 extends BaseService<ConfigServiceState>
22 implements ServiceWithDependencies
23{
24 constructor() {
25 super("ConfigService", {
26 config: null,
27 configPath: undefined,
28 });
29 }
30
31 /**
32 * Declare dependencies on other services
33 */
34 getDependencies(): string[] {
35 return [SERVICE_NAMES.AUTH, SERVICE_NAMES.API_CLIENT];
36 }
37
38 /**
39 * Initialize the config service
40 */
41 async doInitialize(
42 authConfig: AuthConfig,
43 configPath: string | undefined,
44 _organizationId: string | null,
45 apiClient: DefaultApiInterface,
46 injectedConfigOptions?: BaseCommandOptions,
47 ): Promise<ConfigServiceState> {
48 // Use the new streamlined config loader
49 const { loadConfiguration } = await import("../configLoader.js");
50 const result = await loadConfiguration(authConfig, configPath, apiClient);
51
52 let config = result.config;
53
54 // Apply injected config if provided
55 if (
56 injectedConfigOptions &&
57 this.hasInjectedConfig(injectedConfigOptions)
58 ) {
59 config = await configEnhancer.enhanceConfig(
60 config,
61 injectedConfigOptions,
62 );
63
64 logger.debug("Applied injected configuration");
65 }
66
67 // Config URI persistence is now handled by the streamlined loader
68
69 logger.debug("ConfigService initialized successfully");
70
71 return {
72 config,
73 configPath,
74 };
75 }
76
77 /**
78 * Switch to a new configuration
79 */
80 async switchConfig(
81 newConfigPath: string,
82 authConfig: AuthConfig,
83 _organizationId: string | null,
84 apiClient: DefaultApiInterface,
85 injectedConfigOptions?: BaseCommandOptions,
86 ): Promise<ConfigServiceState> {
87 logger.debug("Switching configuration", {
88 from: this.currentState.configPath,
89 to: newConfigPath,
90 });
91
92 try {
93 // Use the new streamlined config loader
94 const { loadConfiguration } = await import("../configLoader.js");
95 const result = await loadConfiguration(
96 authConfig,
97 newConfigPath,
98 apiClient,
99 );
100
101 let config = result.config;
102
103 // Apply injected config if provided
104 if (
105 injectedConfigOptions &&
106 this.hasInjectedConfig(injectedConfigOptions)
107 ) {
108 config = await configEnhancer.enhanceConfig(
109 config,
110 injectedConfigOptions,
111 );
112
113 logger.debug("Applied injected configuration");
114 }
115
116 this.setState({
117 config,
118 configPath: newConfigPath,
119 });
120
121 // Config URI persistence is now handled by the streamlined loader
122
123 logger.debug("Configuration switched successfully", {
124 newConfigPath,
125 });
126
127 return this.getState();
128 } catch (error: any) {
129 logger.error("Failed to switch configuration:", error);
130 this.emit("error", error);
131 throw error;
132 }
133 }
134
135 /**
136 * Reload the current configuration
137 */
138 async reload(
139 authConfig: AuthConfig,
140 _organizationId: string | null,
141 apiClient: DefaultApiInterface,
142 injectedConfigOptions?: BaseCommandOptions,
143 ): Promise<ConfigServiceState> {
144 if (!this.currentState.configPath) {
145 throw new Error("No configuration path available for reload");
146 }
147
148 logger.debug("Reloading current configuration");
149
150 return this.switchConfig(
151 this.currentState.configPath,
152 authConfig,
153 _organizationId,
154 apiClient,
155 injectedConfigOptions,
156 );
157 }
158
159 /**
160 * Check if injected config options contain any config to inject
161 */
162 private hasInjectedConfig(options: BaseCommandOptions): boolean {
163 return !!(
164 (options.rule && options.rule.length > 0) ||
165 (options.mcp && options.mcp.length > 0) ||
166 (options.model && options.model.length > 0) ||
167 (options.prompt && options.prompt.length > 0)
168 );
169 }
170
171 /**
172 * Update the configuration path and notify the service container
173 * This triggers automatic dependent service reloads via the reactive system
174 */
175 async updateConfigPath(newConfigPath: string | undefined): Promise<void> {
176 logger.debug("Updating config path", {
177 from: this.currentState.configPath,
178 to: newConfigPath,
179 });
180
181 try {
182 // Get current auth and API client state needed for config loading
183 const authConfig = loadAuthConfig();
184 const apiClientState = await serviceContainer.get<ApiClientServiceState>(
185 SERVICE_NAMES.API_CLIENT,
186 );
187
188 if (!apiClientState.apiClient) {
189 throw new Error("API client not available");
190 }
191
192 // Load the new configuration using streamlined loader
193 const { loadConfiguration } = await import("../configLoader.js");
194 const result = await loadConfiguration(
195 authConfig,
196 newConfigPath,
197 apiClientState.apiClient,
198 );
199
200 // Update internal state
201 this.setState({
202 config: result.config,
203 configPath: newConfigPath,
204 });
205
206 // Config URI persistence is now handled by the streamlined loader
207
208 // Update the CONFIG service in the container
209 serviceContainer.set(SERVICE_NAMES.CONFIG, this.getState());
210
211 // Manually reload dependent services (MODEL, MCP) to pick up the new config
212 await serviceContainer.reload(SERVICE_NAMES.MODEL);
213 await serviceContainer.reload(SERVICE_NAMES.MCP);
214
215 logger.debug("Configuration path updated successfully", {
216 newConfigPath,
217 configName: result.config.name,
218 });
219 } catch (error: any) {
220 logger.error("Failed to update configuration path:", error);
221 this.emit("error", error);
222 throw error;
223 }
224 }
225}