diff --git a/core/context/retrieval/pipelines/BaseRetrievalPipeline.ts b/core/context/retrieval/pipelines/BaseRetrievalPipeline.ts index 6c1f9f578..d4118f0b4 100644 --- a/core/context/retrieval/pipelines/BaseRetrievalPipeline.ts +++ b/core/context/retrieval/pipelines/BaseRetrievalPipeline.ts @@ -20,6 +20,7 @@ export interface RetrievalPipelineOptions { nRetrieve: number; nFinal: number; tags: BranchAndDir[]; + pathSep: string; filterDirectory?: string; } @@ -30,8 +31,10 @@ export interface IRetrievalPipeline { export default class BaseRetrievalPipeline implements IRetrievalPipeline { private lanceDbIndex: LanceDbIndex; constructor(protected readonly options: RetrievalPipelineOptions) { - this.lanceDbIndex = new LanceDbIndex(options.embeddingsProvider, (path) => - options.ide.readFile(path), + this.lanceDbIndex = new LanceDbIndex( + options.embeddingsProvider, + (path) => options.ide.readFile(path), + options.pathSep ); } diff --git a/core/context/retrieval/retrieval.ts b/core/context/retrieval/retrieval.ts index 9d07ae5cc..d1e7342fa 100644 --- a/core/context/retrieval/retrieval.ts +++ b/core/context/retrieval/retrieval.ts @@ -70,6 +70,7 @@ export async function retrieveContextItemsFromEmbeddings( nFinal, nRetrieve, tags, + pathSep: await extras.ide.pathSep(), embeddingsProvider: extras.embeddingsProvider, reranker: extras.reranker, filterDirectory, diff --git a/core/indexing/CodebaseIndexer.ts b/core/indexing/CodebaseIndexer.ts index 7cc57a153..fdab9a1a2 100644 --- a/core/indexing/CodebaseIndexer.ts +++ b/core/indexing/CodebaseIndexer.ts @@ -35,16 +35,19 @@ export class CodebaseIndexer { protected async getIndexesToBuild(): Promise { const config = await this.configHandler.loadConfig(); + const pathSep = await this.ide.pathSep(); const indexes = [ new ChunkCodebaseIndex( this.ide.readFile.bind(this.ide), + pathSep, this.continueServerClient, config.embeddingsProvider.maxChunkSize, ), // Chunking must come first new LanceDbIndex( config.embeddingsProvider, this.ide.readFile.bind(this.ide), + pathSep, this.continueServerClient, ), new FullTextSearchCodebaseIndex(), diff --git a/core/indexing/LanceDbIndex.ts b/core/indexing/LanceDbIndex.ts index aefd24048..198232a95 100644 --- a/core/indexing/LanceDbIndex.ts +++ b/core/indexing/LanceDbIndex.ts @@ -12,7 +12,7 @@ import { } from "../index.js"; import { getBasename } from "../util/index.js"; import { getLanceDbPath, migrate } from "../util/paths.js"; -import { chunkDocument } from "./chunk/chunk.js"; +import { chunkDocument, shouldChunk } from "./chunk/chunk.js"; import { DatabaseConnection, SqliteDb, tagToString } from "./refreshIndex.js"; import { CodebaseIndex, @@ -40,6 +40,7 @@ export class LanceDbIndex implements CodebaseIndex { constructor( private readonly embeddingsProvider: EmbeddingsProvider, private readonly readFile: (filepath: string) => Promise, + private readonly pathSep: string, private readonly continueServerClient?: IContinueServerClient, ) {} @@ -86,6 +87,9 @@ export class LanceDbIndex implements CodebaseIndex { private async packToRows(item: PathAndCacheKey): Promise { const content = await this.readFile(item.path); + if (!shouldChunk(this.pathSep, item.path, content)) { + return []; + } const chunks: Chunk[] = []; const chunkParams = { filepath: item.path, @@ -99,10 +103,6 @@ export class LanceDbIndex implements CodebaseIndex { throw new Error("did not chunk properly"); } chunks.push(chunk); - if (chunks.length > 20) { - // Too many chunks to index, probably a larger file than we want to include - throw new Error("too large to index"); - } } const embeddings = await this.chunkListToEmbedding(chunks); if (chunks.length !== embeddings.length) { @@ -266,8 +266,6 @@ export class LanceDbIndex implements CodebaseIndex { status: "indexing", }; - console.log(results.compute); - const dbRows = await this.computeRows(results.compute); await this.insertRows(sqlite, dbRows); await Promise.all( diff --git a/core/indexing/chunk/ChunkCodebaseIndex.ts b/core/indexing/chunk/ChunkCodebaseIndex.ts index 5ed41b63d..008f90c62 100644 --- a/core/indexing/chunk/ChunkCodebaseIndex.ts +++ b/core/indexing/chunk/ChunkCodebaseIndex.ts @@ -11,7 +11,7 @@ import { RefreshIndexResults, type CodebaseIndex, } from "../types.js"; -import { chunkDocument } from "./chunk.js"; +import { chunkDocument, shouldChunk } from "./chunk.js"; export class ChunkCodebaseIndex implements CodebaseIndex { relativeExpectedTime: number = 1; @@ -20,6 +20,7 @@ export class ChunkCodebaseIndex implements CodebaseIndex { constructor( private readonly readFile: (filepath: string) => Promise, + private readonly pathSep: string, private readonly continueServerClient: IContinueServerClient, private readonly maxChunkSize: number, ) { @@ -154,7 +155,7 @@ export class ChunkCodebaseIndex implements CodebaseIndex { private async packToChunks(pack: PathAndCacheKey): Promise { const contents = await this.readFile(pack.path); - if (!contents.length) { + if (!shouldChunk(this.pathSep, pack.path, contents)) { return []; } const chunks: Chunk[] = []; diff --git a/core/indexing/chunk/chunk.ts b/core/indexing/chunk/chunk.ts index 92adea72f..97665ab03 100644 --- a/core/indexing/chunk/chunk.ts +++ b/core/indexing/chunk/chunk.ts @@ -1,4 +1,4 @@ -import { Chunk, ChunkWithoutID } from "../../index.js"; +import { Chunk, ChunkWithoutID, IDE } from "../../index.js"; import { countTokens, countTokensAsync } from "../../llm/countTokens.js"; import { supportedLanguages } from "../../util/treeSitter.js"; import { basicChunker } from "./basic.js"; @@ -77,3 +77,16 @@ export async function* chunkDocument({ yield chunk; } } + +export function shouldChunk(pathSep: string, filepath: string, contents: string): boolean { + if (contents.length > 1000000) { + // if a file has more than 1m characters then skip it + return false; + } + if (contents.length === 0) { + return false; + } + const basename = filepath.split(pathSep).pop(); + // files without extensions are often binary files, skip it if so + return basename?.includes(".") ?? false; +} \ No newline at end of file diff --git a/core/test/indexing/chunk/chunk.test.ts b/core/test/indexing/chunk/chunk.test.ts new file mode 100644 index 000000000..3f5130f71 --- /dev/null +++ b/core/test/indexing/chunk/chunk.test.ts @@ -0,0 +1,32 @@ +import { shouldChunk } from "../../../indexing/chunk/chunk"; +import path from "path"; + +describe("shouldChunk", () => { + test("should chunk a typescript file", () => { + const filePath = "directory/file.ts"; + const fileContent = generateString(10000); + expect(shouldChunk(path.sep, filePath, fileContent)).toBe(true); + }); + + test("should not chunk a large typescript file", () => { + const filePath = "directory/file.ts"; + const fileContent = generateString(1500000); + expect(shouldChunk(path.sep, filePath, fileContent)).toBe(false); + }); + + test("should not chunk an empty file", () => { + const filePath = "directory/file.ts"; + const fileContent = generateString(0); + expect(shouldChunk(path.sep, filePath, fileContent)).toBe(false); + }); + + test("should not chunk a file without extension", () => { + const filePath = "directory/with.dot/filename"; + const fileContent = generateString(10000); + expect(shouldChunk(path.sep, filePath, fileContent)).toBe(false); + }); +}); + +function generateString(length: number) { + return "a".repeat(length); +} \ No newline at end of file