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

feat: run dependent indexer based on context provider
This commit is contained in:
Dallin Romney 2025-09-09 11:29:19 -07:00 • committed by GitHub
commit e908d5da6a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 224 additions and 33 deletions

View file

@ -16,7 +16,7 @@ class CodeContextProvider extends BaseContextProvider {
displayTitle: "Code",
description: "Type to search",
type: "submenu",
dependsOnIndexing: true,
dependsOnIndexing: ["chunk", "codeSnippets"],
};
async getContextItems(

View file

@ -13,7 +13,7 @@ class CodebaseContextProvider extends BaseContextProvider {
description: "Automatically find relevant files",
type: "normal",
renderInlineAs: "",
dependsOnIndexing: true,
dependsOnIndexing: ["embeddings", "fullTextSearch", "chunk"],
};
async getContextItems(

View file

@ -19,7 +19,7 @@ class FolderContextProvider extends BaseContextProvider {
displayTitle: "Folder",
description: "Type to search",
type: "submenu",
dependsOnIndexing: true,
dependsOnIndexing: ["embeddings", "fullTextSearch", "chunk"],
};
async getContextItems(

View file

@ -174,6 +174,13 @@ export class Core {
}
};
this.codeBaseIndexer = new CodebaseIndexer(
this.configHandler,
this.ide,
this.messenger,
this.globalContext.get("indexingPaused"),
);
this.configHandler.onConfigUpdate((result) => {
void (async () => {
const serializedResult =
@ -186,6 +193,12 @@ export class Core {
selectedOrgId: this.configHandler.currentOrg?.id ?? null,
});
if (await this.codeBaseIndexer.wasAnyOneIndexAdded()) {
await this.codeBaseIndexer.refreshCodebaseIndex(
await this.ide.getWorkspaceDirs(),
);
}
// update additional submenu context providers registered via VSCode API
const additionalProviders =
this.configHandler.getAdditionalSubmenuContextProviders();
@ -197,13 +210,6 @@ export class Core {
})();
});
this.codeBaseIndexer = new CodebaseIndexer(
this.configHandler,
this.ide,
this.messenger,
this.globalContext.get("indexingPaused"),
);
// Dev Data Logger
const dataLogger = DataLogger.getInstance();
dataLogger.core = this;

7
core/index.d.ts vendored
View file

@ -173,6 +173,11 @@ export interface ModelInstaller {
}
export type ContextProviderType = "normal" | "query" | "submenu";
export type ContextIndexingType =
| "chunk"
| "embeddings"
| "fullTextSearch"
| "codeSnippets";
export interface ContextProviderDescription {
title: ContextProviderName;
@ -180,7 +185,7 @@ export interface ContextProviderDescription {
description: string;
renderInlineAs?: string;
type: ContextProviderType;
dependsOnIndexing?: boolean;
dependsOnIndexing?: ContextIndexingType[];
}
export type FetchFunction = (url: string | URL, init?: any) => Promise<any>;

View file

@ -21,6 +21,7 @@ import CodebaseContextProvider from "../context/providers/CodebaseContextProvide
import { ContinueConfig } from "../index.js";
import { localPathToUri } from "../util/pathToUri.js";
import { CodebaseIndexer } from "./CodebaseIndexer.js";
import { FullTextSearchCodebaseIndex } from "./FullTextSearchCodebaseIndex.js";
import { getComputeDeleteAddRemove } from "./refreshIndex.js";
import { TestCodebaseIndex } from "./TestCodebaseIndex.js";
import { CodebaseIndex } from "./types.js";
@ -675,5 +676,154 @@ describe("CodebaseIndexer", () => {
]);
});
});
describe("getIndexesToBuild", () => {
let indexer: CodebaseIndexer;
let mockConfig: any;
let mockEmbeddingsModel: any;
let mockIdeSettings: any;
beforeEach(async () => {
indexer = new CodebaseIndexer(
testConfigHandler,
testIde,
mockMessenger as any,
false,
);
mockEmbeddingsModel = {
maxEmbeddingChunkSize: 1000,
model: "test-model",
provider: "test-provider",
};
mockIdeSettings = {
remoteConfigServerUrl: "http://test.com",
userToken: "test-token",
};
mockConfig = {
selectedModelByRole: {
embed: mockEmbeddingsModel,
},
contextProviders: [],
};
jest
.spyOn(testIde, "getIdeSettings")
.mockResolvedValue(mockIdeSettings);
});
afterEach(() => {
jest.clearAllMocks();
});
test("should create specific indexTypes for the requested context providers", async () => {
mockConfig.contextProviders = [
{
description: {
dependsOnIndexing: ["chunk", "codeSnippets"],
},
},
];
jest.spyOn(testConfigHandler, "loadConfig").mockResolvedValue({
config: mockConfig,
errors: [],
configLoadInterrupted: false,
});
const indexes = (await (
indexer as any
).getIndexesToBuild()) as CodebaseIndex[];
expect(indexes).toHaveLength(2);
expect(indexes[0].artifactId).toBe("chunks");
expect(indexes[1].artifactId).toBe("codeSnippets");
});
test("should handle duplicate contexts", async () => {
mockConfig.contextProviders = [
{
description: {
dependsOnIndexing: ["codeSnippets"],
},
},
{
description: {
dependsOnIndexing: ["codeSnippets"],
},
},
];
jest.spyOn(testConfigHandler, "loadConfig").mockResolvedValue({
config: mockConfig,
errors: [],
configLoadInterrupted: false,
});
const indexes = await (indexer as any).getIndexesToBuild();
expect(indexes).toHaveLength(1);
expect(indexes[0].artifactId).toBe("codeSnippets");
});
});
});
describe("wasAnyOneIndexAdded", () => {
let testIndexer: TestCodebaseIndexer;
let mockGetIndexesToBuild: jest.MockedFunction<any>;
beforeEach(() => {
testIndexer = new TestCodebaseIndexer(
testConfigHandler,
testIde,
mockMessenger as any,
false,
);
// Mock getIndexesToBuild to control what indexes should be built
mockGetIndexesToBuild = jest
.spyOn(testIndexer as any, "getIndexesToBuild")
.mockResolvedValue([]);
jest
.spyOn(testIndexer, "refreshCodebaseIndex")
.mockImplementation(async () => {});
});
afterEach(() => {
jest.clearAllMocks();
});
test("should return true when indexes to build differ from built indexes", async () => {
const mockIndex1 = new TestCodebaseIndex();
const mockIndex2 = new FullTextSearchCodebaseIndex();
mockGetIndexesToBuild.mockResolvedValue([mockIndex1]);
(testIndexer as any).builtIndexes = [mockIndex2];
const result = await testIndexer.wasAnyOneIndexAdded();
expect(result).toBe(true);
});
test("should return true when there are more indexes to build than built indexes", async () => {
const mockIndex1 = new TestCodebaseIndex();
const mockIndex2 = new FullTextSearchCodebaseIndex();
mockGetIndexesToBuild.mockResolvedValue([mockIndex1, mockIndex2]);
(testIndexer as any).builtIndexes = [mockIndex1];
const result = await testIndexer.wasAnyOneIndexAdded();
expect(result).toBe(true);
});
test("should return false when there are fewer indexes to build than built indexes", async () => {
const mockIndex1 = new TestCodebaseIndex();
const mockIndex2 = new FullTextSearchCodebaseIndex();
mockGetIndexesToBuild.mockResolvedValue([mockIndex1]);
(testIndexer as any).builtIndexes = [mockIndex1, mockIndex2];
const result = await testIndexer.wasAnyOneIndexAdded();
expect(result).toBe(false);
});
});
});

View file

@ -2,6 +2,7 @@ import * as fs from "fs/promises";
import { ConfigHandler } from "../config/ConfigHandler.js";
import {
ContextIndexingType,
ContinueConfig,
IDE,
IndexingProgressUpdate,
@ -58,6 +59,7 @@ export class CodebaseIndexer {
private indexingCancellationController: AbortController;
private codebaseIndexingState: IndexingProgressUpdate;
private readonly pauseToken: PauseToken;
private builtIndexes: CodebaseIndex[] = [];
private getUserFriendlyIndexName(artifactId: string): string {
if (artifactId === FullTextSearchCodebaseIndex.artifactId)
@ -166,28 +168,46 @@ export class CodebaseIndexer {
return [];
}
const indexes: CodebaseIndex[] = [
new ChunkCodebaseIndex(
this.ide.readFile.bind(this.ide),
continueServerClient,
embeddingsModel.maxEmbeddingChunkSize,
), // Chunking must come first
];
const lanceDbIndex = await LanceDbIndex.create(
embeddingsModel,
this.ide.readFile.bind(this.ide),
const indexTypesToBuild = new Set( // use set to remove duplicates
config.contextProviders
.map((provider) => provider.description.dependsOnIndexing)
.filter((indexType) => Array.isArray(indexType)) // remove undefined indexTypes
.flat(),
);
if (lanceDbIndex) {
indexes.push(lanceDbIndex);
const indexTypeToIndexerMapping: Record<
ContextIndexingType,
() => Promise<CodebaseIndex | null>
> = {
chunk: async () =>
new ChunkCodebaseIndex(
this.ide.readFile.bind(this.ide),
continueServerClient,
embeddingsModel.maxEmbeddingChunkSize,
),
codeSnippets: async () => new CodeSnippetsCodebaseIndex(this.ide),
fullTextSearch: async () => new FullTextSearchCodebaseIndex(),
embeddings: async () => {
const lanceDbIndex = await LanceDbIndex.create(
embeddingsModel,
this.ide.readFile.bind(this.ide),
);
return lanceDbIndex;
},
};
const indexes: CodebaseIndex[] = [];
// not parallelizing to avoid race conditions in sqlite
for (const indexType of indexTypesToBuild) {
if (indexType && indexType in indexTypeToIndexerMapping) {
const index = await indexTypeToIndexerMapping[indexType]();
if (index) {
indexes.push(index);
}
}
}
indexes.push(
new FullTextSearchCodebaseIndex(),
new CodeSnippetsCodebaseIndex(this.ide),
);
this.builtIndexes = indexes;
return indexes;
}
@ -694,6 +714,15 @@ export class CodebaseIndexer {
}
}
public async wasAnyOneIndexAdded() {
const indexes = await this.getIndexesToBuild();
return !indexes.every((index) =>
this.builtIndexes.some(
(builtIndex) => builtIndex.artifactId === index.artifactId,
),
);
}
public async refreshCodebaseIndex(paths: string[]) {
if (!this.indexingCancellationController.signal.aborted) {
this.indexingCancellationController.abort();

View file

@ -208,7 +208,7 @@ export const SubmenuContextProvidersProvider = ({
const interval = setInterval(refreshOpenFiles, 2000);
refreshOpenFiles(); // Initial call
void refreshOpenFiles(); // Initial call
return () => {
isMounted = false;
@ -475,7 +475,8 @@ export const SubmenuContextProvidersProvider = ({
providers === "all"
? true
: providers === "dependsOnIndexing"
? description.dependsOnIndexing
? description.dependsOnIndexing &&
description.dependsOnIndexing?.length > 0
: providers.includes(description.title);
if (!refreshProvider) {
@ -589,7 +590,7 @@ export const SubmenuContextProvidersProvider = ({
useWebviewListener(
"refreshSubmenuItems",
async (data) => {
loadSubmenuItems(data.providers);
void loadSubmenuItems(data.providers);
},
[loadSubmenuItems],
);
@ -607,7 +608,7 @@ export const SubmenuContextProvidersProvider = ({
)
.map((provider) => provider.title);
if (newTitles.length > 0) {
loadSubmenuItems(newTitles);
void loadSubmenuItems(newTitles);
}
lastProviders.current = submenuContextProviders;
}, [loadSubmenuItems, submenuContextProviders]);