Skip to content

Commit d35b152

Browse files
Fixed multiple code indexing background tasks being triggered (#51)
1 parent 49e41b6 commit d35b152

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

.changeset/friendly-masks-marry.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"hai-build-code-generator": patch
3+
---
4+
5+
Fixed multiple code indexing background tasks being triggered.

src/core/webview/ClineProvider.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ export class ClineProvider implements vscode.WebviewViewProvider {
148148
private isSideBar: boolean
149149
fileSystemWatcher: HaiFileSystemWatcher | undefined
150150
private authManager: FirebaseAuthManager
151+
private isCodeIndexInProgress: boolean = false
151152

152153
constructor(
153154
readonly context: vscode.ExtensionContext,
@@ -229,9 +230,10 @@ export class ClineProvider implements vscode.WebviewViewProvider {
229230
}
230231

231232
async codeIndexBackground(filePaths?: string[], reIndex: boolean = false) {
232-
if (!this.isSideBar || this.codeIndexAbortController.signal.aborted) {
233+
if (!this.isSideBar || this.codeIndexAbortController.signal.aborted || this.isCodeIndexInProgress) {
233234
return
234235
}
236+
235237
await ensureFaissPlatformDeps()
236238
const state = (await this.customGetState("buildIndexProgress")) as HaiBuildIndexProgress | undefined
237239
const updateProgressState = async (data: Partial<HaiBuildIndexProgress>) => {
@@ -296,6 +298,10 @@ export class ClineProvider implements vscode.WebviewViewProvider {
296298
}
297299
await this.updateWorkspaceState("codeIndexUserConfirmation", true)
298300
}
301+
302+
// Setting a flag to prevent multiple code index background tasks.
303+
this.isCodeIndexInProgress = true
304+
299305
await vscode.window.withProgress(
300306
{
301307
cancellable: false,
@@ -322,6 +328,7 @@ export class ClineProvider implements vscode.WebviewViewProvider {
322328
type: "codeContext",
323329
isInProgress: false,
324330
})
331+
this.isCodeIndexInProgress = false
325332
})
326333
codeContextAgent.on("progress", async (progress: ICodeIndexProgress) => {
327334
this.outputChannel.appendLine(`codeContextAgentProgress ${progress.type} ${progress.value}%`)
@@ -371,6 +378,7 @@ export class ClineProvider implements vscode.WebviewViewProvider {
371378
vscode.window.showErrorMessage(`Code context failed: ${error.message}`)
372379

373380
this.codeIndexAbortController.abort()
381+
this.isCodeIndexInProgress = false
374382
})
375383
await codeContextAgent.start(filePaths, reIndex)
376384
if (!this.codeIndexAbortController.signal.aborted) {
@@ -399,6 +407,7 @@ export class ClineProvider implements vscode.WebviewViewProvider {
399407
type: "codeIndex",
400408
isInProgress: false,
401409
})
410+
this.isCodeIndexInProgress = false
402411
})
403412
vectorizeCodeAgent.on("progress", async (progress: ICodeIndexProgress) => {
404413
this.outputChannel.appendLine(`vectorizeCodeAgentProgress: ${progress.type} ${progress.value}%`)
@@ -446,6 +455,7 @@ export class ClineProvider implements vscode.WebviewViewProvider {
446455
console.error("Error during indexing:", error.message, error.error)
447456
vscode.window.showErrorMessage(`Indexing failed: ${error.message}`)
448457
this.codeIndexAbortController.abort()
458+
this.isCodeIndexInProgress = false
449459
})
450460
await vectorizeCodeAgent.start(filePaths)
451461
if (!this.codeIndexAbortController.signal.aborted) {
@@ -456,12 +466,16 @@ export class ClineProvider implements vscode.WebviewViewProvider {
456466
isInProgress: false,
457467
})
458468
}
469+
470+
// Resetting the flag after the entire process is complete.
471+
this.isCodeIndexInProgress = false
459472
},
460473
)
461474
}
462475
} catch (error) {
463476
console.error("codeIndexBackground", "Error listing files in workspace:", error)
464477
vscode.window.showErrorMessage(CodeContextErrorMessage)
478+
this.isCodeIndexInProgress = false
465479
}
466480
}
467481
}
@@ -2359,6 +2373,7 @@ export class ClineProvider implements vscode.WebviewViewProvider {
23592373
vscode.window.showInformationMessage("Resetting state...")
23602374
if (!this.codeIndexAbortController.signal.aborted) {
23612375
this.codeIndexAbortController.abort()
2376+
this.isCodeIndexInProgress = false
23622377
}
23632378
for (const key of this.context.workspaceState.keys()) {
23642379
await this.context.workspaceState.update(key, undefined)

0 commit comments

Comments
 (0)