Elffuss BraidHub

kikocis/continue public

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