Code Index (Qdrant) recreate services when change configurations (#5152)

This commit is contained in:
Catriel Müller 2025-06-27 11:20:38 -03:00 committed by GitHub
parent 9bf31d3ff7
commit 426518bccd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 62 additions and 51 deletions

View file

@ -89,7 +89,7 @@ describe("CodeIndexManager - handleExternalSettingsChange regression", () => {
expect(manager.isInitialized).toBe(true)
// Mock the methods that would be called during restart
const stopWatcherSpy = vitest.spyOn(manager, "stopWatcher").mockImplementation(() => {})
const recreateServicesSpy = vitest.spyOn(manager as any, "_recreateServices").mockImplementation(() => {})
const startIndexingSpy = vitest.spyOn(manager, "startIndexing").mockResolvedValue()
// Mock the feature state
@ -100,7 +100,7 @@ describe("CodeIndexManager - handleExternalSettingsChange regression", () => {
// Verify that the restart sequence was called
expect(mockConfigManager.loadConfiguration).toHaveBeenCalled()
expect(stopWatcherSpy).toHaveBeenCalled()
expect(recreateServicesSpy).toHaveBeenCalled()
expect(startIndexingSpy).toHaveBeenCalled()
})

View file

@ -123,54 +123,7 @@ export class CodeIndexManager {
const needsServiceRecreation = !this._serviceFactory || requiresRestart
if (needsServiceRecreation) {
// Stop watcher if it exists
if (this._orchestrator) {
this.stopWatcher()
}
// (Re)Initialize service factory
this._serviceFactory = new CodeIndexServiceFactory(
this._configManager,
this.workspacePath,
this._cacheManager,
)
const ignoreInstance = ignore()
const ignorePath = path.join(getWorkspacePath(), ".gitignore")
try {
const content = await fs.readFile(ignorePath, "utf8")
ignoreInstance.add(content)
ignoreInstance.add(".gitignore")
} catch (error) {
// Should never happen: reading file failed even though it exists
console.error("Unexpected error loading .gitignore:", error)
}
// (Re)Create shared service instances
const { embedder, vectorStore, scanner, fileWatcher } = this._serviceFactory.createServices(
this.context,
this._cacheManager,
ignoreInstance,
)
// (Re)Initialize orchestrator
this._orchestrator = new CodeIndexOrchestrator(
this._configManager,
this._stateManager,
this.workspacePath,
this._cacheManager,
vectorStore,
scanner,
fileWatcher,
)
// (Re)Initialize search service
this._searchService = new CodeIndexSearchService(
this._configManager,
this._stateManager,
embedder,
vectorStore,
)
await this._recreateServices()
}
// 5. Handle Indexing Start/Restart
@ -248,6 +201,61 @@ export class CodeIndexManager {
return this._searchService!.searchIndex(query, directoryPrefix)
}
/**
* Private helper method to recreate services with current configuration.
* Used by both initialize() and handleExternalSettingsChange().
*/
private async _recreateServices(): Promise<void> {
// Stop watcher if it exists
if (this._orchestrator) {
this.stopWatcher()
}
// (Re)Initialize service factory
this._serviceFactory = new CodeIndexServiceFactory(
this._configManager!,
this.workspacePath,
this._cacheManager!,
)
const ignoreInstance = ignore()
const ignorePath = path.join(getWorkspacePath(), ".gitignore")
try {
const content = await fs.readFile(ignorePath, "utf8")
ignoreInstance.add(content)
ignoreInstance.add(".gitignore")
} catch (error) {
// Should never happen: reading file failed even though it exists
console.error("Unexpected error loading .gitignore:", error)
}
// (Re)Create shared service instances
const { embedder, vectorStore, scanner, fileWatcher } = this._serviceFactory.createServices(
this.context,
this._cacheManager!,
ignoreInstance,
)
// (Re)Initialize orchestrator
this._orchestrator = new CodeIndexOrchestrator(
this._configManager!,
this._stateManager,
this.workspacePath,
this._cacheManager!,
vectorStore,
scanner,
fileWatcher,
)
// (Re)Initialize search service
this._searchService = new CodeIndexSearchService(
this._configManager!,
this._stateManager,
embedder,
vectorStore,
)
}
/**
* Handles external settings changes by reloading configuration.
* This method should be called when API provider settings are updated
@ -263,7 +271,10 @@ export class CodeIndexManager {
// If configuration changes require a restart and the manager is initialized, restart the service
if (requiresRestart && isFeatureEnabled && isFeatureConfigured && this.isInitialized) {
this.stopWatcher()
// Recreate services with new configuration
await this._recreateServices()
// Start indexing with new services
await this.startIndexing()
}
}