Skip to content

Commit f9eb212

Browse files
Code Index (Qdrant) recreate services when change configurations
Code Index (Qdrant) recreate services when change configurations
2 parents 6f1a940 + c200b90 commit f9eb212

File tree

3 files changed

+73
-52
lines changed

3 files changed

+73
-52
lines changed

.changeset/little-facts-judge.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"kilo-code": patch
3+
---
4+
5+
Refresh CodeIndex Services (Qdrant) when change the configuration

src/services/code-index/__tests__/manager.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ describe("CodeIndexManager - handleExternalSettingsChange regression", () => {
8989
expect(manager.isInitialized).toBe(true)
9090

9191
// Mock the methods that would be called during restart
92-
const stopWatcherSpy = vitest.spyOn(manager, "stopWatcher").mockImplementation(() => {})
92+
const recreateServicesSpy = vitest.spyOn(manager as any, "_recreateServices").mockImplementation(() => {}) // kilocode_change
9393
const startIndexingSpy = vitest.spyOn(manager, "startIndexing").mockResolvedValue()
9494

9595
// Mock the feature state
@@ -100,7 +100,7 @@ describe("CodeIndexManager - handleExternalSettingsChange regression", () => {
100100

101101
// Verify that the restart sequence was called
102102
expect(mockConfigManager.loadConfiguration).toHaveBeenCalled()
103-
expect(stopWatcherSpy).toHaveBeenCalled()
103+
expect(recreateServicesSpy).toHaveBeenCalled() // kilocode_change
104104
expect(startIndexingSpy).toHaveBeenCalled()
105105
})
106106

src/services/code-index/manager.ts

Lines changed: 66 additions & 50 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,13 @@ 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) {
266-
this.stopWatcher()
278+
// kilocode_change start: recreate services with new configuration
279+
await this._recreateServices()
280+
// kilocode_change end
281+
282+
// Start indexing with new services
267283
await this.startIndexing()
268284
}
269285
}

0 commit comments

Comments
 (0)