Elffuss BraidHub

kikocis/continue public

Merge pull request #6323 from uinstinct/dependent-context-indexing

feat: run dependent indexer based on context provider
Changeset22a64be7284f0dfa2cb245ab97326ee62dd75af89912739e57ab0d567ed839a9
AuthorDallin Romney <dallinromney@gmail.com> · 2025-09-09 11:29 -07:00
CommitterGitHub <noreply@github.com> · 2025-09-09 11:29 -07:00
Parents27a6a4517a3b, 1ac6dff93cbc

Browse the files at this changeset

8 files changed

modified core/context/providers/CodeContextProvider.ts +1 −1
@@ -16,7 +16,7 @@
1616 displayTitle: "Code",
1717 description: "Type to search",
1818 type: "submenu",
19- dependsOnIndexing: true,
19+ dependsOnIndexing: ["chunk", "codeSnippets"],
2020 };
2121
2222 async getContextItems(
modified core/context/providers/CodebaseContextProvider.ts +1 −1
@@ -13,7 +13,7 @@
1313 description: "Automatically find relevant files",
1414 type: "normal",
1515 renderInlineAs: "",
16- dependsOnIndexing: true,
16+ dependsOnIndexing: ["embeddings", "fullTextSearch", "chunk"],
1717 };
1818
1919 async getContextItems(
modified core/context/providers/FolderContextProvider.ts +1 −1
@@ -19,7 +19,7 @@
1919 displayTitle: "Folder",
2020 description: "Type to search",
2121 type: "submenu",
22- dependsOnIndexing: true,
22+ dependsOnIndexing: ["embeddings", "fullTextSearch", "chunk"],
2323 };
2424
2525 async getContextItems(
modified core/core.ts +13 −7
@@ -174,6 +174,13 @@
174174 }
175175 };
176176
177+ this.codeBaseIndexer = new CodebaseIndexer(
178+ this.configHandler,
179+ this.ide,
180+ this.messenger,
181+ this.globalContext.get("indexingPaused"),
182+ );
183+
177184 this.configHandler.onConfigUpdate((result) => {
178185 void (async () => {
179186 const serializedResult =
@@ -186,6 +193,12 @@
186193 selectedOrgId: this.configHandler.currentOrg?.id ?? null,
187194 });
188195
196+ if (await this.codeBaseIndexer.wasAnyOneIndexAdded()) {
197+ await this.codeBaseIndexer.refreshCodebaseIndex(
198+ await this.ide.getWorkspaceDirs(),
199+ );
200+ }
201+
189202 // update additional submenu context providers registered via VSCode API
190203 const additionalProviders =
191204 this.configHandler.getAdditionalSubmenuContextProviders();
@@ -197,13 +210,6 @@
197210 })();
198211 });
199212
200- this.codeBaseIndexer = new CodebaseIndexer(
201- this.configHandler,
202- this.ide,
203- this.messenger,
204- this.globalContext.get("indexingPaused"),
205- );
206-
207213 // Dev Data Logger
208214 const dataLogger = DataLogger.getInstance();
209215 dataLogger.core = this;
modified core/index.d.ts +6 −1
@@ -173,6 +173,11 @@
173173 }
174174
175175 export type ContextProviderType = "normal" | "query" | "submenu";
176+export type ContextIndexingType =
177+ | "chunk"
178+ | "embeddings"
179+ | "fullTextSearch"
180+ | "codeSnippets";
176181
177182 export interface ContextProviderDescription {
178183 title: ContextProviderName;
@@ -180,7 +185,7 @@
180185 description: string;
181186 renderInlineAs?: string;
182187 type: ContextProviderType;
183- dependsOnIndexing?: boolean;
188+ dependsOnIndexing?: ContextIndexingType[];
184189 }
185190
186191 export type FetchFunction = (url: string | URL, init?: any) => Promise<any>;
modified core/indexing/CodebaseIndexer.test.ts +150 −0
@@ -21,6 +21,7 @@
2121 import { ContinueConfig } from "../index.js";
2222 import { localPathToUri } from "../util/pathToUri.js";
2323 import { CodebaseIndexer } from "./CodebaseIndexer.js";
24+import { FullTextSearchCodebaseIndex } from "./FullTextSearchCodebaseIndex.js";
2425 import { getComputeDeleteAddRemove } from "./refreshIndex.js";
2526 import { TestCodebaseIndex } from "./TestCodebaseIndex.js";
2627 import { CodebaseIndex } from "./types.js";
@@ -675,5 +676,154 @@
675676 ]);
676677 });
677678 });
679+
680+ describe("getIndexesToBuild", () => {
681+ let indexer: CodebaseIndexer;
682+ let mockConfig: any;
683+ let mockEmbeddingsModel: any;
684+ let mockIdeSettings: any;
685+
686+ beforeEach(async () => {
687+ indexer = new CodebaseIndexer(
688+ testConfigHandler,
689+ testIde,
690+ mockMessenger as any,
691+ false,
692+ );
693+
694+ mockEmbeddingsModel = {
695+ maxEmbeddingChunkSize: 1000,
696+ model: "test-model",
697+ provider: "test-provider",
698+ };
699+
700+ mockIdeSettings = {
701+ remoteConfigServerUrl: "http://test.com",
702+ userToken: "test-token",
703+ };
704+
705+ mockConfig = {
706+ selectedModelByRole: {
707+ embed: mockEmbeddingsModel,
708+ },
709+ contextProviders: [],
710+ };
711+
712+ jest
713+ .spyOn(testIde, "getIdeSettings")
714+ .mockResolvedValue(mockIdeSettings);
715+ });
716+
717+ afterEach(() => {
718+ jest.clearAllMocks();
719+ });
720+
721+ test("should create specific indexTypes for the requested context providers", async () => {
722+ mockConfig.contextProviders = [
723+ {
724+ description: {
725+ dependsOnIndexing: ["chunk", "codeSnippets"],
726+ },
727+ },
728+ ];
729+
730+ jest.spyOn(testConfigHandler, "loadConfig").mockResolvedValue({
731+ config: mockConfig,
732+ errors: [],
733+ configLoadInterrupted: false,
734+ });
735+
736+ const indexes = (await (
737+ indexer as any
738+ ).getIndexesToBuild()) as CodebaseIndex[];
739+
740+ expect(indexes).toHaveLength(2);
741+ expect(indexes[0].artifactId).toBe("chunks");
742+ expect(indexes[1].artifactId).toBe("codeSnippets");
743+ });
744+
745+ test("should handle duplicate contexts", async () => {
746+ mockConfig.contextProviders = [
747+ {
748+ description: {
749+ dependsOnIndexing: ["codeSnippets"],
750+ },
751+ },
752+ {
753+ description: {
754+ dependsOnIndexing: ["codeSnippets"],
755+ },
756+ },
757+ ];
758+
759+ jest.spyOn(testConfigHandler, "loadConfig").mockResolvedValue({
760+ config: mockConfig,
761+ errors: [],
762+ configLoadInterrupted: false,
763+ });
764+
765+ const indexes = await (indexer as any).getIndexesToBuild();
766+
767+ expect(indexes).toHaveLength(1);
768+ expect(indexes[0].artifactId).toBe("codeSnippets");
769+ });
770+ });
771+ });
772+
773+ describe("wasAnyOneIndexAdded", () => {
774+ let testIndexer: TestCodebaseIndexer;
775+ let mockGetIndexesToBuild: jest.MockedFunction<any>;
776+
777+ beforeEach(() => {
778+ testIndexer = new TestCodebaseIndexer(
779+ testConfigHandler,
780+ testIde,
781+ mockMessenger as any,
782+ false,
783+ );
784+
785+ // Mock getIndexesToBuild to control what indexes should be built
786+ mockGetIndexesToBuild = jest
787+ .spyOn(testIndexer as any, "getIndexesToBuild")
788+ .mockResolvedValue([]);
789+
790+ jest
791+ .spyOn(testIndexer, "refreshCodebaseIndex")
792+ .mockImplementation(async () => {});
793+ });
794+
795+ afterEach(() => {
796+ jest.clearAllMocks();
797+ });
798+
799+ test("should return true when indexes to build differ from built indexes", async () => {
800+ const mockIndex1 = new TestCodebaseIndex();
801+ const mockIndex2 = new FullTextSearchCodebaseIndex();
802+ mockGetIndexesToBuild.mockResolvedValue([mockIndex1]);
803+ (testIndexer as any).builtIndexes = [mockIndex2];
804+
805+ const result = await testIndexer.wasAnyOneIndexAdded();
806+ expect(result).toBe(true);
807+ });
808+
809+ test("should return true when there are more indexes to build than built indexes", async () => {
810+ const mockIndex1 = new TestCodebaseIndex();
811+ const mockIndex2 = new FullTextSearchCodebaseIndex();
812+ mockGetIndexesToBuild.mockResolvedValue([mockIndex1, mockIndex2]);
813+ (testIndexer as any).builtIndexes = [mockIndex1];
814+
815+ const result = await testIndexer.wasAnyOneIndexAdded();
816+ expect(result).toBe(true);
817+ });
818+
819+ test("should return false when there are fewer indexes to build than built indexes", async () => {
820+ const mockIndex1 = new TestCodebaseIndex();
821+ const mockIndex2 = new FullTextSearchCodebaseIndex();
822+ mockGetIndexesToBuild.mockResolvedValue([mockIndex1]);
823+ (testIndexer as any).builtIndexes = [mockIndex1, mockIndex2];
824+
825+ const result = await testIndexer.wasAnyOneIndexAdded();
826+ expect(result).toBe(false);
827+ });
678828 });
679829 });
modified core/indexing/CodebaseIndexer.ts +47 −18
@@ -2,6 +2,7 @@
22
33 import { ConfigHandler } from "../config/ConfigHandler.js";
44 import {
5+ ContextIndexingType,
56 ContinueConfig,
67 IDE,
78 IndexingProgressUpdate,
@@ -58,6 +59,7 @@
5859 private indexingCancellationController: AbortController;
5960 private codebaseIndexingState: IndexingProgressUpdate;
6061 private readonly pauseToken: PauseToken;
62+ private builtIndexes: CodebaseIndex[] = [];
6163
6264 private getUserFriendlyIndexName(artifactId: string): string {
6365 if (artifactId === FullTextSearchCodebaseIndex.artifactId)
@@ -166,28 +168,46 @@
166168 return [];
167169 }
168170
169- const indexes: CodebaseIndex[] = [
170- new ChunkCodebaseIndex(
171- this.ide.readFile.bind(this.ide),
172- continueServerClient,
173- embeddingsModel.maxEmbeddingChunkSize,
174- ), // Chunking must come first
175- ];
176-
177- const lanceDbIndex = await LanceDbIndex.create(
178- embeddingsModel,
179- this.ide.readFile.bind(this.ide),
171+ const indexTypesToBuild = new Set( // use set to remove duplicates
172+ config.contextProviders
173+ .map((provider) => provider.description.dependsOnIndexing)
174+ .filter((indexType) => Array.isArray(indexType)) // remove undefined indexTypes
175+ .flat(),
180176 );
181177
182- if (lanceDbIndex) {
183- indexes.push(lanceDbIndex);
184- }
178+ const indexTypeToIndexerMapping: Record<
179+ ContextIndexingType,
180+ () => Promise<CodebaseIndex | null>
181+ > = {
182+ chunk: async () =>
183+ new ChunkCodebaseIndex(
184+ this.ide.readFile.bind(this.ide),
185+ continueServerClient,
186+ embeddingsModel.maxEmbeddingChunkSize,
187+ ),
188+ codeSnippets: async () => new CodeSnippetsCodebaseIndex(this.ide),
189+ fullTextSearch: async () => new FullTextSearchCodebaseIndex(),
190+ embeddings: async () => {
191+ const lanceDbIndex = await LanceDbIndex.create(
192+ embeddingsModel,
193+ this.ide.readFile.bind(this.ide),
194+ );
195+ return lanceDbIndex;
196+ },
197+ };
185198
186- indexes.push(
187- new FullTextSearchCodebaseIndex(),
188- new CodeSnippetsCodebaseIndex(this.ide),
189- );
199+ const indexes: CodebaseIndex[] = [];
200+ // not parallelizing to avoid race conditions in sqlite
201+ for (const indexType of indexTypesToBuild) {
202+ if (indexType && indexType in indexTypeToIndexerMapping) {
203+ const index = await indexTypeToIndexerMapping[indexType]();
204+ if (index) {
205+ indexes.push(index);
206+ }
207+ }
208+ }
190209
210+ this.builtIndexes = indexes;
191211 return indexes;
192212 }
193213
@@ -694,6 +714,15 @@
694714 }
695715 }
696716
717+ public async wasAnyOneIndexAdded() {
718+ const indexes = await this.getIndexesToBuild();
719+ return !indexes.every((index) =>
720+ this.builtIndexes.some(
721+ (builtIndex) => builtIndex.artifactId === index.artifactId,
722+ ),
723+ );
724+ }
725+
697726 public async refreshCodebaseIndex(paths: string[]) {
698727 if (!this.indexingCancellationController.signal.aborted) {
699728 this.indexingCancellationController.abort();
modified gui/src/context/SubmenuContextProviders.tsx +5 −4
@@ -208,7 +208,7 @@
208208
209209 const interval = setInterval(refreshOpenFiles, 2000);
210210
211- refreshOpenFiles(); // Initial call
211+ void refreshOpenFiles(); // Initial call
212212
213213 return () => {
214214 isMounted = false;
@@ -475,7 +475,8 @@
475475 providers === "all"
476476 ? true
477477 : providers === "dependsOnIndexing"
478- ? description.dependsOnIndexing
478+ ? description.dependsOnIndexing &&
479+ description.dependsOnIndexing?.length > 0
479480 : providers.includes(description.title);
480481
481482 if (!refreshProvider) {
@@ -589,7 +590,7 @@
589590 useWebviewListener(
590591 "refreshSubmenuItems",
591592 async (data) => {
592- loadSubmenuItems(data.providers);
593+ void loadSubmenuItems(data.providers);
593594 },
594595 [loadSubmenuItems],
595596 );
@@ -607,7 +608,7 @@
607608 )
608609 .map((provider) => provider.title);
609610 if (newTitles.length > 0) {
610- loadSubmenuItems(newTitles);
611+ void loadSubmenuItems(newTitles);
611612 }
612613 lastProviders.current = submenuContextProviders;
613614 }, [loadSubmenuItems, submenuContextProviders]);