Skip to content

Commit 275ea1b

Browse files
committed
refactor: recreate the code-index services when the configuration change
1 parent 49f0554 commit 275ea1b

File tree

1 file changed

+68
-49
lines changed

1 file changed

+68
-49
lines changed

src/services/code-index/manager.ts

Lines changed: 68 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -123,54 +123,9 @@ export class CodeIndexManager {
123123
const needsServiceRecreation = !this._serviceFactory || requiresRestart
124124

125125
if (needsServiceRecreation) {
126-
// Stop watcher if it exists
127-
if (this._orchestrator) {
128-
this.stopWatcher()
129-
}
130-
131-
// (Re)Initialize service factory
132-
this._serviceFactory = new CodeIndexServiceFactory(
133-
this._configManager,
134-
this.workspacePath,
135-
this._cacheManager,
136-
)
137-
138-
const ignoreInstance = ignore()
139-
const ignorePath = path.join(getWorkspacePath(), ".gitignore")
140-
try {
141-
const content = await fs.readFile(ignorePath, "utf8")
142-
ignoreInstance.add(content)
143-
ignoreInstance.add(".gitignore")
144-
} catch (error) {
145-
// Should never happen: reading file failed even though it exists
146-
console.error("Unexpected error loading .gitignore:", error)
147-
}
148-
149-
// (Re)Create shared service instances
150-
const { embedder, vectorStore, scanner, fileWatcher } = this._serviceFactory.createServices(
151-
this.context,
152-
this._cacheManager,
153-
ignoreInstance,
154-
)
155-
156-
// (Re)Initialize orchestrator
157-
this._orchestrator = new CodeIndexOrchestrator(
158-
this._configManager,
159-
this._stateManager,
160-
this.workspacePath,
161-
this._cacheManager,
162-
vectorStore,
163-
scanner,
164-
fileWatcher,
165-
)
166-
167-
// (Re)Initialize search service
168-
this._searchService = new CodeIndexSearchService(
169-
this._configManager,
170-
this._stateManager,
171-
embedder,
172-
vectorStore,
173-
)
126+
// kilocode_change start: recreate services with new configuration
127+
await this._recreateServices()
128+
// kilocode_change end
174129
}
175130

176131
// 5. Handle Indexing Start/Restart
@@ -248,6 +203,63 @@ export class CodeIndexManager {
248203
return this._searchService!.searchIndex(query, directoryPrefix)
249204
}
250205

206+
// kilocode_change start
207+
/**
208+
* Private helper method to recreate services with current configuration.
209+
* Used by both initialize() and handleExternalSettingsChange().
210+
*/
211+
private async _recreateServices(): Promise<void> {
212+
// Stop watcher if it exists
213+
if (this._orchestrator) {
214+
this.stopWatcher()
215+
}
216+
217+
// (Re)Initialize service factory
218+
this._serviceFactory = new CodeIndexServiceFactory(
219+
this._configManager!,
220+
this.workspacePath,
221+
this._cacheManager!,
222+
)
223+
224+
const ignoreInstance = ignore()
225+
const ignorePath = path.join(getWorkspacePath(), ".gitignore")
226+
try {
227+
const content = await fs.readFile(ignorePath, "utf8")
228+
ignoreInstance.add(content)
229+
ignoreInstance.add(".gitignore")
230+
} catch (error) {
231+
// Should never happen: reading file failed even though it exists
232+
console.error("Unexpected error loading .gitignore:", error)
233+
}
234+
235+
// (Re)Create shared service instances
236+
const { embedder, vectorStore, scanner, fileWatcher } = this._serviceFactory.createServices(
237+
this.context,
238+
this._cacheManager!,
239+
ignoreInstance,
240+
)
241+
242+
// (Re)Initialize orchestrator
243+
this._orchestrator = new CodeIndexOrchestrator(
244+
this._configManager!,
245+
this._stateManager,
246+
this.workspacePath,
247+
this._cacheManager!,
248+
vectorStore,
249+
scanner,
250+
fileWatcher,
251+
)
252+
253+
// (Re)Initialize search service
254+
this._searchService = new CodeIndexSearchService(
255+
this._configManager!,
256+
this._stateManager,
257+
embedder,
258+
vectorStore,
259+
)
260+
}
261+
// kilocode_change end
262+
251263
/**
252264
* Handles external settings changes by reloading configuration.
253265
* This method should be called when API provider settings are updated
@@ -261,9 +273,16 @@ export class CodeIndexManager {
261273
const isFeatureEnabled = this.isFeatureEnabled
262274
const isFeatureConfigured = this.isFeatureConfigured
263275

264-
// If configuration changes require a restart and the manager is initialized, restart the service
276+
// If configuration changes require a restart and the manager is initialized, recreate services and restart
265277
if (requiresRestart && isFeatureEnabled && isFeatureConfigured && this.isInitialized) {
278+
// Stop watcher if it exists
266279
this.stopWatcher()
280+
281+
// kilocode_change start: recreate services with new configuration
282+
await this._recreateServices()
283+
// kilocode_change end
284+
285+
// Start indexing with new services
267286
await this.startIndexing()
268287
}
269288
}

0 commit comments

Comments
 (0)