From 0770b21dbfe984954605ebeeabb191ac31db15e4 Mon Sep 17 00:00:00 2001 From: chezsmithy Date: Wed, 7 May 2025 13:45:02 -0700 Subject: [PATCH] fix(files-context)-refresh-on-new-file-and-save --- core/core.ts | 97 +++++++++++++++++++++ core/index.d.ts | 4 +- core/tools/implementations/createNewFile.ts | 4 + 3 files changed, 104 insertions(+), 1 deletion(-) diff --git a/core/core.ts b/core/core.ts index 4fabefed3..648b1ed40 100644 --- a/core/core.ts +++ b/core/core.ts @@ -708,6 +708,7 @@ export class Core { tool, toolCallId: toolCall.id, onPartialOutput, + core: this, // Add a reference to the Core instance }); }); @@ -1008,4 +1009,100 @@ export class Core { return []; } }; + + private indexingCancellationController: AbortController | undefined; + private async sendIndexingErrorTelemetry(update: IndexingProgressUpdate) { + console.debug( + "Indexing failed with error: ", + update.desc, + update.debugInfo, + ); + void Telemetry.capture( + "indexing_error", + { + error: update.desc, + stack: update.debugInfo, + }, + false, + ); + } + + private async refreshCodebaseIndex(paths: string[]) { + if (this.indexingCancellationController) { + this.indexingCancellationController.abort(); + } + this.indexingCancellationController = new AbortController(); + try { + for await (const update of ( + await this.codebaseIndexerPromise + ).refreshDirs(paths, this.indexingCancellationController.signal)) { + let updateToSend = { ...update }; + + void this.messenger.request("indexProgress", updateToSend); + this.codebaseIndexingState = updateToSend; + + if (update.status === "failed") { + void this.sendIndexingErrorTelemetry(update); + } + } + } catch (e: any) { + console.log(`Failed refreshing codebase index directories : ${e}`); + this.handleIndexingError(e); + } + + this.messenger.send("refreshSubmenuItems", { + providers: "dependsOnIndexing", + }); + this.indexingCancellationController = undefined; + } + + private async refreshCodebaseIndexFiles(files: string[]) { + // Can be cancelled by codebase index but not vice versa + if ( + this.indexingCancellationController && + !this.indexingCancellationController.signal.aborted + ) { + return; + } + this.indexingCancellationController = new AbortController(); + try { + for await (const update of ( + await this.codebaseIndexerPromise + ).refreshFiles(files)) { + let updateToSend = { ...update }; + + void this.messenger.request("indexProgress", updateToSend); + this.codebaseIndexingState = updateToSend; + + if (update.status === "failed") { + void this.sendIndexingErrorTelemetry(update); + } + } + } catch (e: any) { + console.log(`Failed refreshing codebase index files : ${e}`); + this.handleIndexingError(e); + } + + this.messenger.send("refreshSubmenuItems", { + providers: "dependsOnIndexing", + }); + this.indexingCancellationController = undefined; + } + + // private + handleIndexingError(e: any) { + if (e instanceof LLMError) { + // Need to report this specific error to the IDE for special handling + void this.messenger.request("reportError", e); + } + // broadcast indexing error + let updateToSend: IndexingProgressUpdate = { + progress: 0, + status: "failed", + desc: e.message, + }; + void this.messenger.request("indexProgress", updateToSend); + this.codebaseIndexingState = updateToSend; + void this.sendIndexingErrorTelemetry(updateToSend); + } } diff --git a/core/index.d.ts b/core/index.d.ts index ad778d3d8..195ab6c47 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -5,6 +5,8 @@ import { } from "@continuedev/config-yaml"; import Parser from "web-tree-sitter"; import { LLMConfigurationStatuses } from "./llm/constants"; +import { GetGhTokenArgs } from "./protocol/ide"; +import { Core } from "./core"; declare global { interface Window { @@ -968,7 +970,7 @@ export interface ToolExtras { toolCallId: string; contextItems: ContextItem[]; }) => void; - config: ContinueConfig; + core?: Core; // Reference to the Core instance } export interface Tool { diff --git a/core/tools/implementations/createNewFile.ts b/core/tools/implementations/createNewFile.ts index 5a89141eb..4e211b32e 100644 --- a/core/tools/implementations/createNewFile.ts +++ b/core/tools/implementations/createNewFile.ts @@ -17,6 +17,10 @@ export const createNewFileImpl: ToolImpl = async (args, extras) => { } await extras.ide.writeFile(resolvedFileUri, args.contents); await extras.ide.openFile(resolvedFileUri); + await extras.ide.saveFile(resolvedFileUri); + if (extras.core) { + await extras.core.refreshCodebaseIndexFiles([resolvedFileUri]); + } return [ { name: getUriPathBasename(resolvedFileUri),